• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

How to access individual bits of 24 bit color bitmap

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
639798
Asked:
639798
  • 2
1 Solution
 
d003303Commented:
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
 
639798Author Commented:
Scanline is too slow. Anyway, good try!
0
 
d003303Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now