[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to access individual bits of 24 bit color bitmap

Posted on 1998-07-11
3
Medium Priority
?
176 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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

656 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