Solved

Convert 24bit grayscale to 8bit image

Posted on 2003-03-26
Medium Priority
6,074 Views
How can i convert 24 bit grayscale image to 8-bit grayscale image?
24bit image has same rgb values for one pixel.
0
Question by:mece
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 3

Expert Comment

ID: 8213459
For each pixel in your image,

Color := Canvas.Pixels[X, Y];
Gray := Round((0.30 * GetRValue(Color)) + (0.59 * GetGValue(Color)) + (0.11 * GetBValue(Color)));
0

LVL 3

Expert Comment

ID: 8213476
Sorry it is as follows:

Color := Canvas.Pixels[X, Y];
Gray := Round((0.30 * GetRValue(Color)) + (0.59 * GetGValue(Color)) + (0.11 * GetBValue(Color)));
Color := RGB(Gray, Gray, Gray);
Canvas.Pixels[X, Y] := Color;
0

LVL 1

Accepted Solution

DavidRissato earned 80 total points
ID: 8213637
You will need to create a gray-scale palette and then copy the original bitmap to there.

I've made a little example below:

procedure TForm1.Button1Click(Sender: TObject);
type
TLogPal = record
lpal : TLogPalette;
colorSpace : Array[0..255] of TPaletteEntry; // This allocate room to
// new palette colors
// since palPalEntry member of
// TLogPalette is declared as
// Array [0..0] of TPaletteEntry
end;
var
bmp : TBitmap;
pal : TLogPal;
iCount : integer;
begin

// Create a 256 gray-scale palette
pal.lpal.palVersion:=\$300;
pal.lpal.palNumEntries := 256;
for iCount := 0 to 255 do
with pal.lpal.palPalEntry[iCount] do
begin
peRed := iCount;
peGreen := iCount;
peBlue := iCount;
end;

// Create a temporary bitmap
bmp := TBitmap.Create;
try
// Define bitmap as 8 bit color
bmp.PixelFormat := pf8bit;

// Define width and height
bmp.Width := Image1.Picture.Width;
bmp.Height := Image1.Picture.Height;

// Create our new grayscale palette
bmp.Palette := CreatePalette(pal.lpal);

// Draw the image on bmp surface
bmp.Canvas.Draw(0,0,Image1.Picture.Graphic);

// Save the new 8bit file
bmp.SaveToFile('my_picture-8bit.bmp');

finally
// Free temp bitmap
bmp.Free;
end;
end;

{}'s
David Rissato Cruz
0

LVL 1

Expert Comment

ID: 8213641
You will need to create a gray-scale palette and then copy the original bitmap to there.

I've made a little example below:

procedure TForm1.Button1Click(Sender: TObject);
type
TLogPal = record
lpal : TLogPalette;
colorSpace : Array[0..255] of TPaletteEntry; // This allocate room to
// new palette colors
// since palPalEntry member of
// TLogPalette is declared as
// Array [0..0] of TPaletteEntry
end;
var
bmp : TBitmap;
pal : TLogPal;
iCount : integer;
begin

// Create a 256 gray-scale palette
pal.lpal.palVersion:=\$300;
pal.lpal.palNumEntries := 256;
for iCount := 0 to 255 do
with pal.lpal.palPalEntry[iCount] do
begin
peRed := iCount;
peGreen := iCount;
peBlue := iCount;
end;

// Create a temporary bitmap
bmp := TBitmap.Create;
try
// Define bitmap as 8 bit color
bmp.PixelFormat := pf8bit;

// Define width and height
bmp.Width := Image1.Picture.Width;
bmp.Height := Image1.Picture.Height;

// Create our new grayscale palette
bmp.Palette := CreatePalette(pal.lpal);

// Draw the image on bmp surface
bmp.Canvas.Draw(0,0,Image1.Picture.Graphic);

// Save the new 8bit file
bmp.SaveToFile('my_picture-8bit.bmp');

finally
// Free temp bitmap
bmp.Free;
end;
end;

{}'s
David Rissato Cruz
0

Author Comment

ID: 8220467
thanx
that is really what i need
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question