Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) GetPixel

 

GetPixel is a VCL graphics code snippet to get one single TImage pixel's color.

 

One version of GetPixel uses RGB values, the other uses a TColor value. The version using TColor is already used for the Win32 API. Therefore, I call the VCL function of GetPixel GetPixelVcl.

 

For the VCL version of GetPixel, I've also added two functions to obtain the average RGB values of a line and square of pixels.

 

 

 

 

 

CLX version of GetPixel

 

//---------------------------------------------------------------------------
#include <cassert>
#include <QExtCtrls.hpp>
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppGetPixel.htm
const TColor GetPixelClx(
  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 == pf32bit && "Bitmap must be 32 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");
  return static_cast<TColor>(RGB(
    (static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*4+2]), //Red
    (static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*4+1]), //Green
    (static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*4+0]) //Blue
    ));
}
//---------------------------------------------------------------------------
//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 == pf32bit && "Bitmap must be 32 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");
  red = (static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*4+2]);
  green = (static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*4+1]);
  blue = (static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*4+0]);
}

 

 

 

 

 

VCL version of GetPixel

 

//---------------------------------------------------------------------------
#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;
}
//---------------------------------------------------------------------------

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict