Solved

How to access individual bits of 24 bit color bitmap

Posted on 1998-07-11
3
170 Views
Last Modified: 2013-12-03
How to access individual bits of 24 bit color bitmap.
I tried scanline, but it's too slow.
Using GetDIB() is only useful for 8bit bitmap.
0
Comment
Question by:639798
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
d003303 earned 100 total points
ID: 1357720
Yo,
here's a code snippet of one of my PAQs. I combined it to a (hopefuly) working solution. See the complete code at

http://www.experts-exchange.com/topics/comp/lang/delphi/Q.10056806

The GetPixel inline procedure is very fast and can be globalized. I hope you understand the way it works.
You can see the supported pixel formats in the case block of the function. The content of the resulting array can be interpreted depending on the color depth.

// code
type
  TPixelMem : array[0..4] of Byte;

function MyGetPixel(X, Y : Integer; ABitmap : TBitmap): TPixelMem;
var SrcMem : PByteArray;
    PixelSizeStep : Integer;
    APixel : TPixelMem;
 procedure GetPixel;
 var Index, Helper : Integer;
 begin
  if PixelSizeStep = 4 then
   begin
     Helper := ((X - 1) div 2) + ((Y - 1) * ABitmap.Width div 2);
     if ((X - 1) mod 2) = 0
      then APixel[0] := ((15 shl 4) and (SrcMem[Helper])) shr 4
      else APixel[0] := 15 and SrcMem[Helper];
   end
  else
   begin
     for Index := 0 to (PixelSizeStep div 8) - 1 do
      begin
        Helper := (X - 1) * (PixelSizeStep div 8) + (Y - 1) * ABitmap.Width * (PixelSizeStep div 8) + Index;
        APixel[Index] := SrcMem[Helper];
      end;
   end;
 end;
begin
  case ABitmap.PixelFormat of
    pf4bit  : PixelSizeStep := 4;
    pf8bit  : PixelSizeStep := 8;
    pf16bit : PixelSizeStep := 16;
    pf24bit : PixelSizeStep := 24;
    pf32bit : PixelSizeStep := 32;
  end;

  SrcMem := ABitmap.ScanLine[ABitmap.Height - 1];
  GetPixel(X, Y);
  Result := APixel;
end;

Hope this helps,

Slash/d003303
0
 

Author Comment

by:639798
ID: 1357721
Scanline is too slow. Anyway, good try!
0
 
LVL 4

Expert Comment

by:d003303
ID: 1357722
You can optimize everything as follows:
If you have subsequent calls to the pixels, store the ABitmap.ScanLine[ABitmap.Height - 1] memory pointer. All calls to that pointer are direct memory access, nothing is faster.
The only thing that slows everything down in this function is that it gets this pointer for every pixel.
Globalize the GetPixel function as follows:

function GetPixel(X, Y, BMPWidth, PixelSizeStep: Integer; SrcMem: PByteArray): TPixelMem;
var Index, Helper : Integer;
begin
 if PixelSizeStep = 4 then
  begin
    Helper := ((X - 1) div 2) + ((Y - 1) * BMPWidth div 2);
    if ((X - 1) mod 2) = 0
     then APixel[0] := ((15 shl 4) and (SrcMem[Helper])) shr 4
     else APixel[0] := 15 and SrcMem[Helper];
  end
 else
  begin
    for Index := 0 to (PixelSizeStep div 8) - 1 do
     begin
       Helper := (X - 1) * (PixelSizeStep div 8) + (Y - 1) * BMPWidth * (PixelSizeStep div 8) + Index;
       APixel[Index] := SrcMem[Helper];
     end;
  end;
end;

and setup the calls to this function with

procedure ManipulateBitmap;
var PixelSizeStep,
    BMPWidth      : Integer;
    SrcMem        : PByteArray;
    X, Y          : Integer;
    APixel        : TPixelMem;
begin
  case ABitmap.PixelFormat of
    pf4bit  : PixelSizeStep := 4;
    pf8bit  : PixelSizeStep := 8;
    pf16bit : PixelSizeStep := 16;
    pf24bit : PixelSizeStep := 24;
    pf32bit : PixelSizeStep := 32;
  end;
  BMPWidth := ABitmap.Width;
  SrcMem := ABitmap.ScanLine[ABitmap.Height - 1];
  repeat
    APixel := GetPixel(X, Y, BMPWidth, PixelSizeStep, SrcMem);
  until //whatever
end;

Now you have to call ScanLine only once. Remember, SrcMem is only the base point for the bitmaps memory representation. The GetPixel function calculates the memory position for every pixel depending on the bitmaps width and the color depth.
With that function I rotated a 1024x768 24Bit bitmap in a couple of milliseconds.

Anyway, if you did not get the point, yust ask. Maybe I can help you more if you describe what you want to achieve with that function.

Slash/d003303
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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