#include #include //Get a pixel's greyness //From http://www.richelbilderbeek.nl/CppGetGreyness.htm const unsigned char GetGreyness( const TImage * const image, const int x, const int y) { assert(image!=0 && "Image is NULL"); assert(image->Picture->Bitmap!=0 && "Bitmap is NULL"); assert(image->Picture->Bitmap->PixelFormat == pf24bit && "Bitmap must be 24 bit"); assert( x >= 0 && "x coordinat is below zero"); assert( y >= 0 && "y coordinat is below zero"); assert( x < image->Picture->Bitmap->Width && "x coordinat is beyond image width"); assert( y < image->Picture->Bitmap->Height && "y coordinat is beyond image height"); const unsigned char * const line = static_cast(image->Picture->Bitmap->ScanLine[y]); const unsigned char red = line[x*3+2]; const unsigned char green = line[x*3+1]; const unsigned char blue = line[x*3+0]; const int grey = (red + green + blue) / 3; assert(grey >= 0 && grey < 256); return grey; } #include #include //Get a line of pixel's average greyness //From http://www.richelbilderbeek.nl/CppGetGreyness.htm const unsigned char GetGreyness( const TImage * const image, const int x1, const int x2, const int y) { assert(image!=0 && "Image is NULL"); assert(image->Picture->Bitmap!=0 && "Bitmap is NULL"); assert(image->Picture->Bitmap->PixelFormat == pf24bit && "Bitmap must be 24 bit"); assert( x1 >= 0 && "x1 coordinat is below zero"); assert( x2 >= 0 && "x2 coordinat is below zero"); assert( y >= 0 && "y coordinat is below zero"); assert( x1 < image->Picture->Bitmap->Width && "x1 coordinat is beyond image width"); assert( x2 < image->Picture->Bitmap->Width && "x2 coordinat is beyond image width"); assert( y < image->Picture->Bitmap->Height && "y coordinat is beyond image height"); assert( x1 < x2); const unsigned char * const line = static_cast(image->Picture->Bitmap->ScanLine[y]); const int nPixels = x2 - x1; int sum = 0; for (int x=x1; x!=x2; ++x) { const unsigned char red = line[x*3+2]; const unsigned char green = line[x*3+1]; const unsigned char blue = line[x*3+0]; const int grey = (red + green + blue) / 3; assert(grey >= 0 && grey < 256); sum+=grey; } const int averageGrey = sum / nPixels; assert(averageGrey >= 0 && averageGrey < 256); return averageGrey; } #include #include //Get a square of pixels' average greyness //From http://www.richelbilderbeek.nl/CppGetGreyness.htm const unsigned char GetGreyness( const TImage * const image, const int x1, const int y1, const int x2, const int y2) { assert(x1 < x2 ); assert(y1 < y2 ); const int nPixelsVertical = y2 - y1; int sum = 0; for (int y=y1; y!=y2; ++y) { const int grey = GetGreyness(image,x1,x2,y); assert(grey >= 0 && grey < 256); sum+=grey; } const int grey = sum / nPixelsVertical; assert(grey >=0 && grey < 256); return grey; }