Solved

Comparing bitmap data fast

Posted on 1997-05-07
7
632 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 7 - Fuzzy Fonts 11 80
LAN or WAN ? 11 95
Sources to create infographic resume 2 20
Is there any way to treat Lock record in table with clientdataset? 3 16
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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