Solved

Comparing bitmap data fast

Posted on 1997-05-07
7
624 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
  • 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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…

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

13 Experts available now in Live!

Get 1:1 Help Now