?
Solved

How to access individual bits of 24 bit color bitmap

Posted on 1998-07-11
3
Medium Priority
?
175 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
[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
3 Comments
 
LVL 4

Accepted Solution

by:
d003303 earned 200 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month14 days, 2 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