//---------------------------------------------------------------------------
#include <cassert>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
//Get a pixel's TColor value
//From http://www.richelbilderbeek.nl/CppGetPixel.htm
const TColor GetPixelVcl(
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<unsigned char *>(image->Picture->Bitmap->ScanLine[y]);
return static_cast<TColor>(RGB(
line[x*3+2], //Red
line[x*3+1], //Green
line[x*3+0] //Blue
));
}
//---------------------------------------------------------------------------
//Get a pixel's RGB values
//From http://www.richelbilderbeek.nl/CppGetPixel.htm
void GetPixel(
const TImage * const image,
const int x,
const int y,
unsigned char& red,
unsigned char& green,
unsigned char& blue)
{
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<unsigned char *>(image->Picture->Bitmap->ScanLine[y]);
red = line[x*3+2];
green = line[x*3+1];
blue = line[x*3+0];
}
//---------------------------------------------------------------------------
//Get a line of pixel's average RGB value
//From http://www.richelbilderbeek.nl
void GetPixel(
const TImage * const image,
const int x1,
const int x2,
const int y,
unsigned char& red,
unsigned char& green,
unsigned char& blue)
{
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<unsigned char *>(image->Picture->Bitmap->ScanLine[y]);
const int nPixels = x2 - x1;
int sumRed = 0;
int sumGreen = 0;
int sumBlue = 0;
for (int x=x1; x!=x2; ++x)
{
const unsigned char r = line[x*3+2];
const unsigned char g = line[x*3+1];
const unsigned char b = line[x*3+0];
sumRed+=r;
sumGreen+=g;
sumBlue+=b;
}
const int averageRed = sumRed / nPixels;
const int averageGreen = sumGreen / nPixels;
const int averageBlue = sumBlue / nPixels;
assert(averageRed >= 0 && averageRed < 256);
assert(averageGreen >= 0 && averageGreen < 256);
assert(averageBlue >= 0 && averageBlue < 256);
red = averageRed;
green = averageGreen;
blue = averageBlue;
}
//---------------------------------------------------------------------------
//Get a square of pixels' average RGB value
//From http://www.richelbilderbeek.nl
void GetPixel(
const TImage * const image,
const int x1,
const int y1,
const int x2,
const int y2,
unsigned char& red,
unsigned char& green,
unsigned char& blue)
{
assert(x1 < x2 );
assert(y1 < y2 );
const int nPixelsVertical = y2 - y1;
int sumRed = 0;
int sumGreen = 0;
int sumBlue = 0;
for (int y=y1; y!=y2; ++y)
{
unsigned char r,g,b;
GetPixel(image,x1,x2,y,r,g,b);
sumRed+=r;
sumGreen+=g;
sumBlue+=b;
}
const int averageRed = sumRed / nPixelsVertical;
const int averageGreen = sumGreen / nPixelsVertical;
const int averageBlue = sumBlue / nPixelsVertical;
assert(averageRed >= 0 && averageRed < 256);
assert(averageGreen >= 0 && averageGreen < 256);
assert(averageBlue >= 0 && averageBlue < 256);
red = averageRed;
green = averageGreen;
blue = averageBlue;
}
//---------------------------------------------------------------------------
|