Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
Graphics code snippet to get the greyness of one or more pixels of a
VCL TImage.
The greyness of a pixels is the averaged red, green and blue value of it. You can
get a single pixel's greyness, or from a square of pixels.
To set pixels' their greynesses, use SetGreyness.
#include <cassert>
#include <vcl.h>
//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<unsigned char *>(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 <cassert>
#include <vcl.h>
//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<unsigned char *>(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 <cassert>
#include <vcl.h>
//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;
}
|
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.
