Same images...different byte arrays???
Same images...different byte arrays???
Same images...different byte arrays???
Same images...different byte arrays???
Same images...different byte arrays???
Same images...different byte arrays??? Same images...different byte arrays??? Same images...different byte arrays??? Same images...different byte arrays??? Same images...different byte arrays??? Same images...different byte arrays??? Same images...different byte arrays??? Same images...different byte arrays???
Same images...different byte arrays??? Same images...different byte arrays???
Same images...different byte arrays???
Go Back  Xtreme Visual Basic Talk > > > Same images...different byte arrays???


Reply
 
Thread Tools Display Modes
  #1  
Old 04-18-2008, 12:32 PM
Birchy's Avatar
Birchy Birchy is offline
Junior Contributor
 
Join Date: Aug 2005
Location: Hereford, UK
Posts: 346
Default Same images...different byte arrays???


Ok, i've had to admit defeat on this one and ask for some help. I've written a fairly simple function to compare 2 images to determine if they are the same. One image is Blitted from the screen into a picture box and the other is loaded into a picture box with LoadPicture() or LoadResPicture(), both of which use the same source image. Now if i step through my code, i can visually check the 2 picture boxes and can see that the 2 images being compared are equal, HOWEVER my function returns FALSE. I wrote another function to compare images using GetPixel() and this returns TRUE. So i suspected that my compare function was faulty but it returns TRUE if i pre-set the picture boxes using the SAME source image from my hdd. Soooo, the question is, how can 2 images be equal when scanned with GetPixel() but have different byte arrays??

Code:
Private Function CompareImages(image1 As StdPicture, image2 As StdPicture) As Boolean
    Dim bm1 As BITMAP, bm2 As BITMAP
    Dim ba1() As Byte, ba2() As Byte
    Dim i As Long
    
    ' load the objects
    Call GetObject(image1, Len(bm1), bm1)
    Call GetObject(image2, Len(bm2), bm2)
    ' allocate buffers
    ReDim ba1((bm1.bmBitsPixel \ 8) * bm1.bmWidth * bm1.bmHeight - 1)
    ReDim ba2((bm2.bmBitsPixel \ 8) * bm2.bmWidth * bm2.bmHeight - 1)
    ' compare array sizes
    If UBound(ba1) = UBound(ba2) Then
        ' images are the same size - fill byte arrays
        Call GetBitmapBits(image1, UBound(ba1), ba1(0))
        Call GetBitmapBits(image2, UBound(ba2), ba2(0))
        ' compare the byte arrays
        For i = 0 To UBound(ba1)
            If ba1(i) <> ba2(i) Then Exit Function
        Next i
        ' if we got this far, images are equal!
        CompareImages = True
    End If
End Function
Reply With Quote
  #2  
Old 04-18-2008, 02:26 PM
Birchy's Avatar
Birchy Birchy is offline
Junior Contributor
 
Join Date: Aug 2005
Location: Hereford, UK
Posts: 346
Default

I've just discovered that if i use SavePicture() on the picture box where i blitted the image and then reload the image using LoadPicture(), the images now match. So the byte array of an image loaded with LoadPicture() is different to the SAME image loaded using BitBlt(). Can anyone explain why this is so??
Reply With Quote
  #3  
Old 04-18-2008, 04:13 PM
gayouj's Avatar
gayoujSame images...different byte arrays??? gayouj is offline
Junior Contributor

* Expert *
 
Join Date: Jan 2006
Location: California
Posts: 317
Default

Could it be that the header data of the bitmaps differs? Try loading each bitmap into a byte array as follows:

Code:
' Assuming you know FileName
Dim FileName As String
FileName = "SomeName"

Dim Number As Long
Number = FreeFile

Dim Data() As Byte

Open FileName For Binary Access Read Write Lock Read Write As Number

ReDim Data(LOF(Number))
Get Number, , Data

Close Number

' Data is now a byte array containing the raw byte data of your bitmap file.
So do the above for both images, so you basically have Data1() and Data2(). The just compare the two together using whatever means seems best to you.
__________________
http://www.joshuagayou.com
Reply With Quote
  #4  
Old 04-18-2008, 04:17 PM
gayouj's Avatar
gayoujSame images...different byte arrays??? gayouj is offline
Junior Contributor

* Expert *
 
Join Date: Jan 2006
Location: California
Posts: 317
Default

Quote:
Originally Posted by Birchy View Post
Soooo, the question is, how can 2 images be equal when scanned with GetPixel() but have different byte arrays??
GetPixel only returns the color value of each visible pixel used to make the image. There's a bunch of information in the bitmap header that you won't see just using this function.
__________________
http://www.joshuagayou.com
Reply With Quote
  #5  
Old 04-18-2008, 04:32 PM
Birchy's Avatar
Birchy Birchy is offline
Junior Contributor
 
Join Date: Aug 2005
Location: Hereford, UK
Posts: 346
Default

Hmmm, sounds like it's header related then. I know that i can save the file and reload it but i've already proven that the byte arrays are the same doing it this way. Is it possible to ignore the header somehow? All i want to do is compare a Blitted image to an image stored in memory as a StdPicture, but in the most efficient way as GetPixel() is far too slow.
Reply With Quote
  #6  
Old 04-18-2008, 05:03 PM
gayouj's Avatar
gayoujSame images...different byte arrays??? gayouj is offline
Junior Contributor

* Expert *
 
Join Date: Jan 2006
Location: California
Posts: 317
Default

Quote:
Originally Posted by Birchy View Post
Hmmm, sounds like it's header related then. I know that i can save the file and reload it but i've already proven that the byte arrays are the same doing it this way. Is it possible to ignore the header somehow? All i want to do is compare a Blitted image to an image stored in memory as a StdPicture, but in the most efficient way as GetPixel() is far too slow.
If you use GetBitmapBits you can load only the image data into a buffer.

Code:
Declare Function GetBitmapBits Lib "gdi32" Alias "GetBitmapBits" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Code:
'Create a new project, add a command button and a picture box to the project, load a picture into the picture box.
'Paste this code into Form1
Private Type BITMAP
    bmType As Long
    bmWidth As Long
    bmHeight As Long
    bmWidthBytes As Long
    bmPlanes As Integer
    bmBitsPixel As Integer
    bmBits As Long
End Type
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Dim PicBits() As Byte, PicInfo As BITMAP
Dim Cnt As Long, BytesPerLine as Long
Private Sub Command1_Click()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    'Get information (such as height and width) about the picturebox
    GetObject Picture1.Image, Len(PicInfo), PicInfo
    'reallocate storage space
    BytesPerLine = (PicInfo.bmWidth * 3 + 3) And &HFFFFFFFC
    ReDim PicBits(1 To BytesPerLine * PicInfo.bmHeight * 3) As Byte
    'Copy the bitmapbits to the array
    GetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)
    'Invert the bits
    For Cnt = 1 To UBound(PicBits)
        PicBits(Cnt) = 255 - PicBits(Cnt)
    Next Cnt
    'Set the bits back to the picture
    SetBitmapBits Picture1.Image, UBound(PicBits), PicBits(1)
    'refresh
    Picture1.Refresh
End Sub
Example code copied from AllAPI network.
__________________
http://www.joshuagayou.com
Reply With Quote
  #7  
Old 04-18-2008, 05:30 PM
Birchy's Avatar
Birchy Birchy is offline
Junior Contributor
 
Join Date: Aug 2005
Location: Hereford, UK
Posts: 346
Default

Sorry, you've lost me there. My function as posted above was based upon the AllAPI function you copy/pasted but i had to modify it for my own needs.
Reply With Quote
  #8  
Old 04-18-2008, 07:00 PM
Birchy's Avatar
Birchy Birchy is offline
Junior Contributor
 
Join Date: Aug 2005
Location: Hereford, UK
Posts: 346
Default

It seems that every 4th byte is a mismatch, so i've done a "workaround" by adding a Step 4 to my byte array loop. I'm not a graphics guru but i imagine it's no coincidence that every 4th byte is different for whatever reason. Obviously the compare is less accurate but it's plenty accurate enough for my needs and it's now 4x faster.
Reply With Quote
  #9  
Old 04-19-2008, 11:36 AM
darkforcesjedi's Avatar
darkforcesjediSame images...different byte arrays??? darkforcesjedi is offline
Trust me, I'm an

* Expert *
 
Join Date: Apr 2001
Location: In ur base, pwnin d00dz
Posts: 1,964
Default

Quote:
Originally Posted by Birchy View Post
It seems that every 4th byte is a mismatch, so i've done a "workaround" by adding a Step 4 to my byte array loop. I'm not a graphics guru but i imagine it's no coincidence that every 4th byte is different for whatever reason. Obviously the compare is less accurate but it's plenty accurate enough for my needs and it's now 4x faster.
The byte arrays of the graphics data will only be the same if the images are the same color depth and, if they're less than True Color, the palettes contain the same colors in the same order. My best guess is that the image is not a True Color bitmap and one of the images is being converted to True Color when you load it such that they no longer match. When you load/save the second image it becomes a match as well because it's been converted to True Color as well.

Load the palettes from each image. Then go pixel by pixel (which will not necessarily be the same number of bits in each picture) and get the COLOR of the pixel to compare.
__________________
To err is human; to debug, divine.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Same images...different byte arrays???
Same images...different byte arrays???
Same images...different byte arrays??? Same images...different byte arrays???
Same images...different byte arrays???
Same images...different byte arrays???
Same images...different byte arrays??? Same images...different byte arrays??? Same images...different byte arrays??? Same images...different byte arrays??? Same images...different byte arrays??? Same images...different byte arrays??? Same images...different byte arrays???
Same images...different byte arrays???
Same images...different byte arrays???
 
Same images...different byte arrays???
Same images...different byte arrays???
 
-->