basic bitmap storage
Useful for comparing two JPEG images saved with a different compression ratios.

You can use these pictures for testing (use the full-size version of each):



50% quality JPEG

100% quality JPEG







link to full size 50% image

link to full size 100% image

The expected difference for these two images is 1.62125%

link printf   # for main only
 
procedure main()    # % difference between images
   fn1 := "Lenna100.jpg"
   fn2 := "Lenna50.jpg"
   printf("%%difference of files %i & %i = %r\n",fn1,fn2,ImageDiff(fn1,fn2)) 
end
 
procedure ImageDiff(fn1,fn2)  #: return % difference of two images
   img1 := open(1,"g","canvas=hidden","image="||fn1) | stop("Open failed ",fn1)
   img2 := open(2,"g","canvas=hidden","image="||fn2) | stop("Open failed ",fn2)
 
   if WAttrib(img1,"width") ~= WAttrib(img2,"width") | 
      WAttrib(img1,"height") ~= WAttrib(img2,"height") then 
         stop("Images must be the same size")
 
   pix1    := create Pixel(img1)  # access pixels one at a time
   pix2    := create Pixel(img2)  # ... facilitate interleaved access
 
   sum := pix := 0 
   while pix +:= 1 & p1 := csv2l(@pix1) & p2 := csv2l(@pix2) do 
      every sum +:= abs(p1[i := 1 to *p1] - p2[i])  
   every close(img1|img2)      
   return sum / (pix * 3 * 65535 / 100. )
end
 
procedure csv2l(p)   #: return a list from a comma separated string
L := []
p ? until pos(0) do {
   put(L,tab(find(",")|0)) 
   move(1)
   }
return L
end