#ifndef CAOS_FilterLed
#define CAOS_FilterLed

#ifndef __CINT__
#include <TROOT.h>
#endif

typedef unsigned char byte;

class Led;
class Leds;
class Ring;

class FilterLed
{
    byte *fImg;
    int fW;
    int fH;
    int fBoxX;
    int fBoxY;
    float fCut;

    Float_t FindCluster(int &cnt, float *sum, UInt_t x, UInt_t y,
                        UInt_t x0, UInt_t y0, UInt_t x1, UInt_t y1) const;

    void GetMinMax(const int offset, byte *min, byte *max) const;
    int  GetMeanPosition(const int x, const int y, const int boxx, const int boxy) const;
    int  GetMeanPosition(const int x, const int y, const int boxx, const int boxy,
			 float &mx, float &my, unsigned int &sum) const;

    int  GetMeanPositionBox(const int x, const int y,
                            const int boxx, const int boxy) const;
    int  GetMeanPositionBox(const int x, const int y,
                            const int boxx, const int boxy, float &mx, float &my,
                            unsigned int &sum) const;

    void DrawBox(const int x1, const int y1,
                 const int x2, const int y2,
                 const int col) const;

public:
    FilterLed(byte *img, int w, int h, double cut=2.5)
      : fImg(img), fW(w), fH(h), fBoxX(w), fBoxY(h), fCut(cut)
    {
    }

    FilterLed(byte *img, int w, int h, int boxx, int boxy, double cut=2.5)
      : fImg(img), fW(w), fH(h), fBoxX(boxx), fBoxY(boxy), fCut(cut)
    {
    }
    virtual ~FilterLed() { }

    void SetBox(int box)   { fBoxX = fBoxY = box; }
    void SetBox(int boxx, int boxy)   { fBoxX = boxx; fBoxY = boxy; }
    void SetCut(float cut) { fCut = cut; } 
    void FindStar(Leds &leds, int xc, int yc, bool circle=false) const;
    
    void Execute(Leds &leds, int xc, int yc, double &bright) const;
    void Execute(Leds &leds, int xc, int yc) const;
    void Execute(Leds &leds) const { Execute(leds, fW/2, fH/2); }
    void ExecuteAndMark(Leds &leds, int xc, int yc) const;
    void ExecuteAndMark(Leds &leds, int xc, int yc, double &bright) const;
    void ExecuteAndMark(Leds &leds) const { ExecuteAndMark(leds, fW/2, fH/2); }
    void Execute(int xc, int yc) const;
    void Execute() const { Execute(fW/2, fH/2); }
    void MarkPoint(const Led &led) const;
    void MarkPoint(Float_t x, Float_t y, Float_t mag) const;
    void Stretch() const;
    void DrawCircle(float cx, float cy, float r, byte col=0x40) const;
    void DrawCircle(float r, byte col=0x40) const { DrawCircle(fW/2, fH/2, r, col); }
    void DrawCircle(const Ring &c, byte col=0x40) const;
    void DrawCircle(const Ring &c, double r, byte col) const;
    void DrawHexagon(float cx, float cy, float r, byte col=0x40) const;
    void DrawHexagon(const Ring &c, double r, byte col) const;

    ClassDef(FilterLed, 0)
};

#endif
