Index: trunk/MagicSoft/Cosy/videodev/FilterLed.cc
===================================================================
--- trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 7787)
+++ trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 7788)
@@ -125,43 +125,59 @@
 				     float &my, unsigned int &sum) const
 {
+    //-------------------------------
+    // Improved algorithm:
+    // 1. Look for the largest five-pixel-cross signal inside the box
+    int x0 = TMath::Max(x-box+1,   0);
+    int y0 = TMath::Max(y-box+1,   0);
+
+    int x1 = TMath::Min(x+box+1-1, fW);
+    int y1 = TMath::Min(y+box+1-1, fH);
+
+    int maxx=0;
+    int maxy=0;
+
+    unsigned int max =0;
+    for (int dx=x0; dx<x1; dx++)
+    {
+        for (int dy=y0; dy<y1; dy++)
+        {
+            const unsigned int sumloc =
+                fImg[(dy+0)*fW + (dx-1)] +
+                fImg[(dy+0)*fW + (dx+1)] +
+                fImg[(dy+1)*fW + dx] +
+                fImg[(dy+0)*fW + dx] +
+                fImg[(dy-1)*fW + dx];
+
+            if(sumloc<=max)
+                continue;
+
+            maxx=dx;
+            maxy=dy;
+            max =sum;
+	}
+    }
+
+    // 2. Calculate mean position inside a circle around
+    // the highst cross-signal with radius of 6 pixels.
     unsigned int sumx=0;
     unsigned int sumy=0;
 
-    //-------------------------------
-    // Improved algorithm:
-    // 1. Look for the largest four-pixel signal inside box
-
-    int thissum=0, maxsum=0;
-    int maxx=0, maxy=0;
-    for (int dx=x-box; dx<x+box+1; dx++)
-        for (int dy=y-box; dy<y+box+1; dy++)
-        {
-            thissum = fImg[dy*fW+dx]+fImg[(dy+1)*fW+dx]+
-	      fImg[dy*fW+(dx+1)]+fImg[(dy+1)*fW+(dx+1)];
-	    if(thissum>maxsum)
-	      {
-		maxx=dx;
-		maxy=dy;
-		maxsum=thissum;
-	      }
-	}
-
-    // 2. Calculate mean position inside a circle around
-    // the highst four-pixel signal with radius of 5 pixels.
+    const int rad = 17;
+
+    x0 = TMath::Max(x-box,   maxx-rad);
+    y0 = TMath::Max(y-box,   maxy-rad);
+
+    x1 = TMath::Min(x+box+1, maxx+rad+1);
+    y1 = TMath::Min(y+box+1, maxy+rad+1);
 
     sum=0;
-    for (int dx=x-box; dx<x+box+1; dx++)
-        for (int dy=y-box; dy<y+box+1; dy++)
+    for (int dx=x0; dx<x1; dx++)
+        for (int dy=y0; dy<y1; dy++)
         {
             const byte &m = fImg[dy*fW+dx];
 
-	    // Circle
-	    if(sqrt((dx-maxx)*(dx-maxx)+
-		    (dy-maxy)*(dy-maxy)) <= 6)
-	      {
-		sumx += m*dx;
-		sumy += m*dy;
-		sum  += m;
-	      }
+            sumx += m*dx;
+            sumy += m*dy;
+            sum  += m;
         }
 
@@ -171,6 +187,4 @@
     return (int)my*fW + (int)mx;
 }
-
-
 
 int FilterLed::GetMeanPositionCircle(const int x, const int y, 
@@ -323,13 +337,8 @@
 void FilterLed::Execute(Leds &leds, int xc, int yc, double &bright) const
 {
-    int x0 = xc-fBox;
-    int x1 = xc+fBox;
-    int y0 = yc-fBox;
-    int y1 = yc+fBox;
-
-    if (x0<0) x0=0;
-    if (y0<0) y0=0;
-    if (x1>fW) x1=fW;
-    if (y1>fH) y1=fH;
+    const int x0 = TMath::Max(xc-fBox, 0);
+    const int y0 = TMath::Max(yc-fBox, 0);
+    const int x1 = TMath::Min(xc+fBox, fW);
+    const int y1 = TMath::Min(yc+fBox, fH);
 
     const int wx = x1-x0;
@@ -430,9 +439,7 @@
 
     RemoveTwinsInterpol(leds, first, 5);
-    
-   
-}
-
-void FilterLed::FindStar(Leds &leds, int xc, int yc) const
+}
+
+void FilterLed::FindStar(Leds &leds, int xc, int yc, bool circle) const
 {
     // fBox: radius of the inner (signal) box
@@ -442,13 +449,8 @@
     // Define inner box in which to search the signal
     //
-    int x0 = xc-fBox;
-    int x1 = xc+fBox;
-    int y0 = yc-fBox;
-    int y1 = yc+fBox;
-
-    if (x0<0) x0=0;
-    if (y0<0) y0=0;
-    if (x1>fW) x1=fW;
-    if (y1>fH) y1=fH;
+    const int x0 = TMath::Max(xc-fBox, 0);
+    const int y0 = TMath::Max(yc-fBox, 0);
+    const int x1 = TMath::Min(xc+fBox, fW);
+    const int y1 = TMath::Min(yc+fBox, fH);
 
     //
@@ -459,13 +461,8 @@
     const double sqrt2 = sqrt(2.);
 
-    int xa = xc-(int)rint(fBox*sqrt2);
-    int xb = xc+(int)rint(fBox*sqrt2);
-    int ya = yc-(int)rint(fBox*sqrt2);
-    int yb = yc+(int)rint(fBox*sqrt2);
-
-    if (xa<0) xa=0;
-    if (ya<0) ya=0;
-    if (xb>fW) xb=fW;
-    if (yb>fH) yb=fH;
+    const int xa = TMath::Max(xc-(int)rint(fBox*sqrt2), 0);
+    const int ya = TMath::Max(yc-(int)rint(fBox*sqrt2), 0);
+    const int xb = TMath::Min(xc+(int)rint(fBox*sqrt2), fW);
+    const int yb = TMath::Min(yc+(int)rint(fBox*sqrt2), fH);
 
     //
@@ -538,7 +535,7 @@
     float mx, my;
     unsigned int mag;
-    int pos = GetMeanPosition(xc, yc, fBox-1, mx, my, mag);
-
-    if (pos<0 || pos>=fW*fH && fImg[pos]<sum+fCut*sdev)
+    int pos = circle ? GetMeanPositionCircle(xc, yc, fBox-1, mx, my, mag) : GetMeanPosition(xc, yc, fBox-1, mx, my, mag);
+
+    if (pos<0 || pos>=fW*fH || fImg[pos]<sum+fCut*sdev)
         return;
 
@@ -549,124 +546,4 @@
 }
 
-void FilterLed::FindStarCircle(Leds &leds, int xc, int yc) const
-{
-    // fBox: radius of the inner (signal) box
-    // Radius of the outer box is fBox*sqrt(2)
-
-    //
-    // Define inner box in which to search the signal
-    //
-    int x0 = xc-fBox;
-    int x1 = xc+fBox;
-    int y0 = yc-fBox;
-    int y1 = yc+fBox;
-
-    if (x0<0) x0=0;
-    if (y0<0) y0=0;
-    if (x1>fW) x1=fW;
-    if (y1>fH) y1=fH;
-
-    //
-    // Define outer box (excluding inner box) having almost
-    // the same number of pixels in which the background
-    // is calculated
-    //
-    const double sqrt2 = sqrt(2.);
-
-    int xa = xc-(int)rint(fBox*sqrt2);
-    int xb = xc+(int)rint(fBox*sqrt2);
-    int ya = yc-(int)rint(fBox*sqrt2);
-    int yb = yc+(int)rint(fBox*sqrt2);
-
-    if (xa<0) xa=0;
-    if (ya<0) ya=0;
-    if (xb>fW) xb=fW;
-    if (yb>fH) yb=fH;
-
-    //
-    // Calculate average and sdev for a square
-    // excluding the inner part were we expect
-    // the signal to be.
-    //
-    double sum = 0;
-    double sq  = 0;
-
-    int n=0;
-    for (int x=xa; x<xb; x++)
-        for (int y=ya; y<yb; y++)
-        {
-            if (x>=x0 && x<x1 && y>=y0 && y<y1)
-                continue;
-
-            byte &b = fImg[y*fW+x];
-
-            sum += b;
-            sq  += b*b;
-            n++;
-        }
-
-    sum /= n;
-    sq  /= n;
-
-    // 254 because b<=max and not b<max
-    const double sdev = sqrt(sq-sum*sum);
-    const byte   max  = sum+fCut*sdev>254 ? 254 : (byte)(sum+fCut*sdev);
-
-    //
-    // clean image from noise
-    // (FIXME: A lookup table could accelerate things...
-    //
-    n=0;
-    for (int x=x0; x<x1; x++)
-        for (int y=y0; y<y1; y++)
-        {
-            byte &b = fImg[y*fW+x];
-            if (b<=max)
-                b = 0;
-            else
-                n++;
-        }
-
-    //
-    // Mark the background region
-    //
-    for (int x=xa; x<xb; x+=2)
-    {
-        fImg[ya*fW+x]=0xf0;
-        fImg[yb*fW+x]=0xf0;
-    }
-    for (int y=ya; y<yb; y+=2)
-    {
-        fImg[y*fW+xa]=0xf0;
-        fImg[y*fW+xb]=0xf0;
-    }
-
-    //
-    // Check if any pixel found...
-    //
-    if (n<5)
-        return;
-
-    //
-    // Get the mean position of the star
-    //
-    float mx, my;
-    unsigned int mag;
-
-    //    int pos = GetMeanPosition(xc, yc, fBox-1, mx, my, mag);
-
-    // try new method
-    int pos = GetMeanPositionCircle(xc, yc, fBox-1, mx, my, mag);
-    
-    if (pos<0 || pos>=fW*fH && fImg[pos]<sum+fCut*sdev)
-        return;
-
-    cout << "Mean=" << sum << "  SDev=" << sdev << "  :  ";
-    cout << "Sum/n = " << sum << "/" << n << " = " << (n==0?0:mag/n) << endl;
-
-    leds.Add(mx, my, 0, 0, -2.5*log10((float)mag)+13.7);
-}
-
-
 void FilterLed::Stretch() const
 {
@@ -698,3 +575,2 @@
     }
 }
-
Index: trunk/MagicSoft/Cosy/videodev/FilterLed.h
===================================================================
--- trunk/MagicSoft/Cosy/videodev/FilterLed.h	(revision 7787)
+++ trunk/MagicSoft/Cosy/videodev/FilterLed.h	(revision 7788)
@@ -49,6 +49,5 @@
     void SetBox(int box)   { fBox = box; }
     void SetCut(float cut) { fCut = cut; } 
-    void FindStar(Leds &leds, int xc, int yc) const;
-    void FindStarCircle(Leds &leds, int xc, int yc) const;
+    void FindStar(Leds &leds, int xc, int yc, bool circle=false) const;
     
     void Execute(Leds &leds, int xc, int yc, double &bright) const;
