Boian's TBitmap Visualizer and converting to grayscale

Posted by on in Blogs
Debugger Visualizers introduced in RAD Studio 2010, for both Delphi 2010 and C++Builder 2010, are one of the most interesting new IDE features. On the ITDevCon 2009 Delphi conference in Verona I have met Boian Mitov from Mitov Software, who actually wrote a sample "TBitmap" visualizer for Delphi 2010. Boian has architected lots of VCL components for all kinds of signal processing, including advanced video and audio processing implementation. Very cool and sophisticated stuff demonstrating the power of Delphi:-)

TBitmap Debugger Visualizer for Delphi is installed into the IDE

OK. I have downloaded and installed TBitmap visualizer into the IDE and it would be good to actually debug some code to try it out. So I thought about implementing an algorithm to convert arbitrary bitmaps to grayscale.

Colors in computing are typically represented by the combination of intensity of the main three colors: red, green and blue (RGB). There are also other representations possible, but let's stick to RGB. The color is considered gray when intensities for all three basic colors are the same. The special cases of gray are: black with zero intensities, and white with maximum possible intensities of all three colors.

One of the most popular color format is 24bit, where each color is represented by one byte, or a value from 0 to 255 range. The additional, fourth byte, is used to store the Alpha channel or - in other words - transparency level.

Few clicks away I have found the description of three algorithms to convert to grayscale as implemented by GIMP: lightness, average and luminosity. All these formulas take R, G and B values of the color and calculate a value in grayscale that can be assigned back to the original R, G and B parts yielding a gray color.

The Lightness method averages the most prominent and least prominent colors: (max(R, G, B) + min(R, G, B)) div 2.

The Average is simple average value of all three colors: (R + G + B) div 3.

The formula for Luminosity is 0.21 R + 0.71 G + 0.07 B. It is a weighted average to account for human perception. We’re more sensitive to green than other colors, so green is weighted most heavily.

That's a playing field for the TBitmap visualizer. You can see the contents of the bitmap before and after converting an image to a grayscale. You only need to set a breakpoint and select "Show Bitmap" in the debugger local variables "Visualizers" context menu. The visualizer displays the bitmap itself and information about its size, format and the color of a pixel if a mouse hovers on the bitmap.

Here is the actual source code for converting bitmaps to grayscale. I am sure this could be optimized for speed, but it is more for illustration purposes.

unit uBitmapUtils;


Graphics, Windows;

TColor2Grayscale = (

procedure ToGray(aBitmap: Graphics.TBitmap; cg: TColor2Grayscale = c2gLuminosity);
function ColorToGray(c: TColor; cg: TColor2Grayscale = c2gLuminosity): TColor;


SysUtils, Math;

function Min(const A, B, C: integer): integer;
Result := Math.Min(A, Math.Min(B,C));

function Max(const A, B, C: integer): integer;
Result := Math.Max(A, Math.Max(B,C));

function ColorToGray(c: TColor; cg: TColor2Grayscale = c2gLuminosity): TColor;
var R, G, B, X : Integer;
R := c and $ff;
G := (c and $ff00) shr 8;
B := (c and $ff0000) shr 16;

case cg of
X := (max(R, G, B) + min(R, G, B)) div 2;

X := (R + G + B) div 3;

X := round(0.21*R + 0.71*G + 0.07*B);

raise Exception.Create('Unknown Color2Grayscale value');

Result := TColor(RGB(X,X,X));

procedure ToGray(aBitmap: Graphics.TBitmap; cg: TColor2Grayscale = c2gLuminosity);
var i, j: integer;
if aBitmap <> nil then
for i := 0 to aBitmap.Width - 1 do
for j := 0 to aBitmap.Height - 1 do
aBitmap.Canvas.Pixels[i,j] := ColorToGray(aBitmap.Canvas.Pixels[i,j], cg);


The source code of this test app can be downloaded from EDN CodeCentral and Boian Mitov's "TBitmap" RAD Studio 2010 debugger visualizer can be found here on Mitov Software website.

Gold User, Rank: 9, Points: 364
Crazy about Delphi Programming!


  • Page :
  • 1

Check out more tips and tricks in this development video: