News:

Function Finder  Find all the functions within source code files

Main Menu

Compare Images / Delta Images

Started by kevin, October 25, 2012, 12:04:57 AM

Previous topic - Next topic

kevin

  Compare Images / Delta Images

  This source code snippet runs through and compares two same sized images together.    The idea here being that when making media for our game, it might be easier to create a number of variations of a background picture,  by simply saving the entire picture each time,  but from a media stand point, this can soon blow out the size of the distribution.   So this routine makes a transparent version of the second image that and be drawn over the first image to recreate the original second image.  So the second image becomes the difference between the two images.   Which is sometimes called a delta frame.

 
PlayBASIC Code: [Select]
   ; -------------------------------------------   
; a pair of images to compare
; -------------------------------------------
Sky1=MakeIMage1(800,600)

Sky2=MakeIMage1(800,600)
rendertoimage Sky2
loadfont "arial",2,100
setfont 2
centertext 400,300,"Hello World"
setfont 1
rendertoscreen

rgbmaskimage sky1,$00ffffff
rgbmaskimage sky2,$00ffffff



; -------------------------------------------
; Compares images
; -------------------------------------------

MaskCOlour=$ff00ff

Dim IMage1(1,1)
Dim IMage2(1,1)

Read_Pixels(IMage1(),Sky1)
Read_Pixels(IMage2(),Sky2)
Sync

drawimage sky1,0,0,false
if Compare_Images(IMage1(),IMage2(),MaskColour)

select Paint_Array(Sky2, IMage2(),MaskColour)

case 0
print "Images are identical."

case 1
print "Section Of IMages are the same"

endselect

else
print "error:images are not the same size."
endif


print "IMage two now hold only the pixels that are different from image 1"

Sync
waitkey


; -------------------------------------------
; Demo images
; -------------------------------------------

Setfps 10

Transparent=true
Do
cls
drawimage Sky1,0,0,false

if Spacekey()
drawimage Sky2,0,0,transparent
endif

if EnterKey()
Transparent=1- Transparent
Flushkeys
endif

print "Space to Show Masked IMage 2 over image 1"
print "enter Toggle transparent mode"

Sync
loop




Function MakeIMage1(Width,Height)
IMage=NewImage(Width,Height,2)

rendertoimage image
shadebox 0,0,width,height,$203040,$406040,$ff4050,$405550

rendertoscreen

EndFunction IMage



Function Paint_Array(TargetIMage,IMage(),MaskColour)

rendertoimage TargetIMage
IMageMaskCOlour TargetIMage,MaskCOlour
cls MaskColour


RowCount=0

Width =GetArrayElements(Image(),1)
Height =GetArrayElements(IMage(),2)

rectx1=Width
recty1=Height
rectx2=0
recty2=0

lockbuffer
For ylp=0 to height-1

UsedRow=false
For Xlp=0 to Width-1
pixel=IMage(xlp,ylp)
if pixel<>maskColour
dotc xlp,ylp,Pixel
if xlp<RectX1 then RectX1=xlp
if xlp>RectX2 then RectX2=xlp
UsedRow=true
endif

next

if UsedRow
if ylp<RectY1 then RectY1=ylp
if ylp>RectY2 then RectY2=ylp
RowCount++
endif
next
unlockbuffer

rendertoscreen


; draw the second image, over the first image with the zone around it
drawimage targetimage ,0,0,true

; high light the area around
box rectx1,recty1,rectx2,recty2,false


EndFunction RowCount

Login required to view complete source code