Solved

How to access individual bits of 24 bit color bitmap

Posted on 1998-07-11
3
165 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now