Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's Code Snippets.
To
set one single TImage pixel's color in both CLX and VCL. The differences
between CLX and VCL are highlighted.
One
version of 'SetPixel' 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 'SetPixel' 'SetPixelVcl'.
Note
my love for assert, it will save me answer a lot of 'it does not
work'-questions.
* View the code of 'SetPixel' in plain text (CLX).
* View the code of 'SetPixel' in plain text (VCL).
#include
<QExtCtrls.hpp>
#include <cassert>
//From
http://www.richelbilderbeek.nl
void SetPixel (
TImage * const image,
const int x,
const int y,
const unsigned char red,
const unsigned char green,
const 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");
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
void SetPixelClx(
TImage * const image,
const int x,
const int y,
const TColor color)
{
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");
static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*4+2] = GetRValue(color);
static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*4+1] = GetGValue(color);
static_cast<unsigned char*>(image->Picture->Bitmap->ScanLine[y])[x*4+0] = GetBValue(color);
}
Add
a TButton and TImage on your TForm. Load a 32-bit bitmap in your TImage. Then
add an OnClick Event to Button1:
void __fastcall
TForm1::Button1Click(TObject *Sender)
{
SetPixel(Image1,10,10,126,126,126);
SetPixel(Image1,15,15,clLime);
Image1->Refresh();
}
It
looks like a 24-bit bitmap also does the job.
#include
<ExtCtrls.hpp>
#include <cassert>
//From
http://www.richelbilderbeek.nl
void SetPixelVcl(
TImage * const image,
const int x,
const int y,
const TColor color)
{
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");
unsigned char * const line
= static_cast<unsigned char
*>(image->Picture->Bitmap->ScanLine[y]);
line[x*3+2] = GetRValue(color);
line[x*3+1] = GetGValue(color);
line[x*3+0] = GetBValue(color);
}
#include
<ExtCtrls.hpp>
#include <cassert>
//Set a pixel's RGB values
//From
http://www.richelbilderbeek.nl
void SetPixel(
TImage * const image,
const int x,
const int y,
const unsigned char red,
const unsigned char green,
const 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");
unsigned char * const line
= static_cast<unsigned char
*>(image->Picture->Bitmap->ScanLine[y]);
line[x*3+2] = red;
line[x*3+1] = green;
line[x*3+0] = blue;
}
//Set a line of pixels' RGB values
//From
http://www.richelbilderbeek.nl
void SetPixel(
TImage * const image,
const int x1,
const int x2,
const int y,
const unsigned char red,
const unsigned char green,
const 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");
unsigned char * const myLine
= static_cast<unsigned char
*>(image->Picture->Bitmap->ScanLine[y]);
for (int x=x1; x!=x2; ++x)
{
myLine[x*3+2] = red;
myLine[x*3+1] = green;
myLine[x*3+0] = blue;
}
}
//Set a square of pixels' RGB values
//From
http://www.richelbilderbeek.nl
void SetPixel(
TImage * const image,
const int x1,
const int y1,
const int x2,
const int y2,
const unsigned char red,
const unsigned char green,
const unsigned char blue)
{
for (int y=y1; y!=y2; ++y)
{
SetPixel(image,x1,x2,y,red,green,blue);
}
}
Go back to Richel Bilderbeek's Code Snippets.
Go back to Richel Bilderbeek's homepage.