Percentage difference between images

[quote=266096:@Jeff Tullin]What you get when you open a picture in Xojo is not what you started with.
Probably to do with color profiles, but in my experience the colors always change.[/quote]

On Mac that is due to Xojo applying the color profile.

This function returns 0 if the pictures are the same. Comparing a white with a black pic should return 100% difference.

It scales things down to speed up comparing HD images. For thumbnails, it’s better to pull that part out to make it a few ticks faster.

[code]Function picDifference(pPic1 As Picture, pPic2 As Picture) As Double
// Return -1 if pics sizes are not the same
If pPic1.Height <> pPic2.Height Or pPic1.Width <> pPic2.Width Then
Return -1
End If

// Width and Height of input pics
Dim iInW As Integer = pPic1.Width
Dim iInH As Integer = pPic1.Height

// Scale down to speed things up
Dim iMax As Integer = 500

// Or don’t if already smaller
If iInW <= iMax And iInH <= iMax Then
iMax = Max(iInW, iInH)
End If

// Scale ratio
Dim dRatio As Double = min(iMax / iInW, iMax / iInH)

// Work pics
Dim pWrkPic1 As New Picture(iInW * dRatio, iInH * dRatio)
Dim pWrkPic2 As New Picture(iInW * dRatio, iInH * dRatio)

// Width and Height of work pics
Dim iWrkW As Integer = pWrkPic1.Width
Dim iWrkH As Integer = pWrkPic1.Height

// Do the scaling
pWrkPic1.graphics.DrawPicture(pPic1, 0, 0, iWrkW, iWrkH, 0, 0, iInW, iInH)
pWrkPic2.graphics.DrawPicture(pPic2, 0, 0, iWrkW, iWrkH, 0, 0, iInW, iInH)

// Compare pixels per line
Dim dDiff As Double
Dim cPx1, cPx2 As Color
For y As Integer = 0 To iWrkH - 1

For x As Integer = 0 To iWrkW - 1
  
  cPx1 = pWrkPic1.Graphics.Pixel(x, y)
  cPx2 = pWrkPic2.Graphics.Pixel(x, y)
  
  dDiff = dDiff + ((Abs(cPx1.Red - cPx2.Red) + Abs(cPx1.Green - cPx2.Green) + Abs(cPx1.Blue - cPx2.Blue)) / 255)
  
Next x

Next y

dDiff = (100 * dDiff) / (iWrkW * iWrkH * 3)

Return dDiff
End Function
[/code]

Thanks Marco,

Fantastic! That is what I wanted, works great.

Thanks again.

Lennox