Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's Code Snippets.
The 'pixelate' function generalizes multiple pixels into one. The function relies heavily on the GetPixel, GetGreyness, SetPixel and SetGreyness functions.
'Pixelate' works under both CLX and VCL.
* View an example image demonstrating the 'Pixelate' function.
* View the code of 'Pixelate' in plain text.
#include <cassert>
#include <algorithm>
//From http://www.richelbilderbeek.nl
void DoPixelateRgb(
const TImage * const imageOriginal,
TImage * const imageResult,
const int pixelSize)
{
assert(imageOriginal!=0);
assert(imageResult!=0);
imageResult->Picture->Graphic = imageOriginal->Picture->Graphic;
assert(imageOriginal->Picture->Bitmap->Width == imageResult->Picture->Bitmap->Width );
assert(imageOriginal->Picture->Bitmap->Height == imageResult->Picture->Bitmap->Height);
const int width = imageOriginal->Picture->Bitmap->Width;
const int height = imageOriginal->Picture->Bitmap->Height;
const int maxx = 1 + (width / pixelSize);
const int maxy = 1 + (height / pixelSize);
for (int y=0; y!=maxy; ++y)
{
const int y1 = (y * pixelSize);
if (y1 >= height) continue;
const int y2 = std::min( y1 + pixelSize, height);
assert(y1 <= height);
assert(y2 <= height);
assert(y1!=y2);
for (int x=0; x!=maxx; ++x)
{
const int x1 = (x * pixelSize);
if (x1 >= width) continue;
const int x2 = std::min( x1 + pixelSize, width);
assert(x1 <= width);
assert(x2 <= width);
assert(x1!=x2);
unsigned char r,g,b;
GetPixel(imageOriginal,
x1, y1, x2, y2,
r,g,b);
SetPixel(
imageResult,
x1, y1, x2, y2,
r,g,b);
}
}
}
#include <cassert>
#include <algorithm>
//From http://www.richelbilderbeek.nl
void DoPixelateGrey(
const TImage * const imageOriginal,
TImage * const imageResult,
const int pixelSize)
{
assert(imageOriginal!=0);
assert(imageResult!=0);
imageResult->Picture->Graphic = imageOriginal->Picture->Graphic;
assert(imageOriginal->Picture->Bitmap->Width == imageResult->Picture->Bitmap->Width );
assert(imageOriginal->Picture->Bitmap->Height == imageResult->Picture->Bitmap->Height);
const int width = imageOriginal->Picture->Bitmap->Width;
const int height = imageOriginal->Picture->Bitmap->Height;
const int maxx = 1 + (width / pixelSize);
const int maxy = 1 + (height / pixelSize);
for (int y=0; y!=maxy; ++y)
{
const int y1 = (y * pixelSize);
if (y1 >= height) continue;
const int y2 = std::min( y1 + pixelSize, height );
assert(y1 <= height);
assert(y2 <= height);
assert(y1!=y2);
for (int x=0; x!=maxx; ++x)
{
const int x1 = (x * pixelSize);
if (x1 >= width) continue;
const int x2 = std::min( x1 + pixelSize, width );
assert(x1 <= width);
assert(x2 <= width);
assert(x1!=x2);
const int grey = GetGreyness(imageOriginal,x1,y1,x2,y2);
SetGreyness(imageResult,x1,y1,x2,y2,grey);
}
}
}
Go back to Richel Bilderbeek's Code Snippets.
Go back to Richel Bilderbeek's homepage.