?
Solved

Convert 24bit grayscale to 8bit image

Posted on 2003-03-26
5
Medium Priority
?
6,074 Views
Last Modified: 2007-12-19
How can i convert 24 bit grayscale image to 8-bit grayscale image?
24bit image has same rgb values for one pixel.
0
Comment
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
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:emadat
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

by:emadat
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

by:
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
  // Loads the image
  Image1.Picture.LoadFromFile('my_picture.bmp');

  // 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

by:DavidRissato
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
  // Loads the image
  Image1.Picture.LoadFromFile('my_picture.bmp');

  // 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

by:mece
ID: 8220467
thanx
that is really what i need
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question