Solved

Comparing bitmap data fast

Posted on 1997-05-07
7
630 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 90
Delphi selector screen 2 79
True Type Font Files? 4 89
Does barcode reader require a specific font installed? 2 27
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 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