Solved

Comparing bitmap data fast

Posted on 1997-05-07
7
633 Views
Last Modified: 2013-12-03
I need to compare two identical size bitmaps to see if they are identical (picture data-wise).
At the moment, I iterate through the Canvas.Pixels[x,y] and compare each individual pixel, breaking out if I find a pair that don't match.  Is there a nice Windows graphics call to do this faster?
I'm coding a sort of fractal compressor, which at the moment takes over 10 hours on a P200 to compress a 300x300 image!
On the same lines, how do I find the palette index of a colour in an 8-bit bitmap?  At the moment I'm streaming the RGB colour, which obviously wastes 2 bytes per pixel.

Thanks,
Bryan
0
Comment
Question by:bcrotaz
[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
  • 4
  • 3
7 Comments
 
LVL 2

Accepted Solution

by:
gysbert1 earned 120 total points
ID: 1335989
The way to do this is to use the API bitmap functions.

Have a look in the API help for the TBitmap Structure and for the function call GetDIBits. From the API help "function GetDIBits(DC: HDC; Bitmap: HBitmap; StartScan, NumScans: Word; Bits: Pointer; var BitInfo: TBitmapInfo; Usage: Word): Integer;

The GetDIBits function retrieves the bits of the specified bitmap and copies them, in device-independent format, into the buffer pointed to by the Bits parameter. The BitInfo parameter retrieves the color format for the device-independent bits.
"

With this you can get to the pointer that points to the bitmap's bits and you do not have to use the slooow canvas.pixels(x,y) function.

I did this a while ago. I will have a look if I have still got the source at home. As far as I remember you can pass the handle of the Delphi TBitmap to the API call since the object is a desendant of the API TBitmap structure.

I'll get back to you on that though ...
0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1335990
Oh yes. This TBitmap API structure contains the Palette as well (which you probably know you can get as a property in the Delphi Implementation). The bits pointer points to a c_style array that contains the 8bit, 24bit, 4bit,etc data as specified. This means that for an 8bit bitmap it will point to an array of palette indices. Pixels[x,y] is slow since it does all this every time. it finds the specified pixel in the bits array and converts the color format to the windows TColor which is 32bit and then returns this. Since function calls takes extremely long compared to other operations this accounts for a significant change of execution speed.

I have soem nice C-code with me to do this which I can send to you if you like. (I am not sure if I still have the delphi code).

0
 
LVL 1

Author Comment

by:bcrotaz
ID: 1335991
So if it's a 256 col bitmap, the bits array is an array of bytes?
Do the scans run from top down, or bottom up?
How is the palette arranged?
Where is the palette in the structure? I can't see it!

Thanks,
bryan
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:gysbert1
ID: 1335992
The scan runs from bottom left to right top if I remember correctly.

The delphi TBitmap object has (as one of it's key properties) a palette of type HPalette.

If you are using TPicture (are you?) the bitmap is one of it's properties
0
 
LVL 1

Author Comment

by:bcrotaz
ID: 1335993
The Hpalette is just a handle to a palette, though.
How do I get the structure of the palette and work out what the 256 colours actually are?
0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1335994
Man ! You really could find this in the API help files.

I am a nice guy though so I will still help you ;^)

There is an API call to get the palette if you have the handle. Remember this ! If you have a Handle to anything in windows you have everything you will ever need ... Everything in windows in handled by handles eg. windows, palettes, even some data structures.

Here is a section of the API help files for you. PS.Delphi 2's help really sucks. Delphi 1's help was much, much better.

API Help Entries ...

function GetPaletteEntries(Palette: HPalette; StartIndex, NumEntries: Word; var
 PaletteEntries): Word;

The GetPaletteEntries function retrieves a range of palette entries in a logical palette.

Parameter      Description

Palette      Identifies the logical palette.
StartIndex      Specifies the first logical-palette entry to be retrieved.
NumEntries      Specifies the number of logical-palette entries to be retrieved.
PaletteEntries      An array of TPALETTEENTRY structures that will receive the palette entries. The array must contain at least as many structures as specified by the NumEntries parameter.

Returns

The return value is the number of entries retrieved from the logical palette, if the function is successful. Otherwise, it is zero.

See Also

GetSystemPaletteEntries
TPALETTEENTRY
GDI functions (3.1)
0
 
LVL 1

Author Comment

by:bcrotaz
ID: 1335995
Thanks.
I agree about the help.
That's why I'm asking the question, cos I couldn't understand it!
It seems to assume you know all about the concepts.  There's no help for you if you don't.


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…

752 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