Resolved: How can one do a pixel by pixel comparison of an image created by cairo and an image loaded from a file


I have some code in nim that creates a picture using Cairo ( I would like to compare that picture to another using diffimg (,
But there does not seem to be a standard in memory image type. Is there any way to do this that does not involve saving the image to a file first?


Probably the most easy way is surprisingly to implement yourself the diffimg algorithm. Looking at the source of diffimg shows the comparison algoritm is about 20 lines of code:
func absDiff[T: ColorComponent](a, b: T): T {.inline.} =
  if a > b:
    a - b
    b - a

func getDiffRatio*[T: Color](a, b: Image[T]): float =
  for p in
    for c in 0..T.high:
      result += absDiff(a[p][c], b[p][c]).float
  result / (T.maxComponentValue.float * * T.len.float)

func genDiffImage*[T: Color](a, b: Image[T]): Image[T] =
  result = initImage[T](a.w, a.h)
  for p in
    for c in 0..T.high:
      result[p][c] = absDiff(a[p][c], b[p][c])
The actual trouble of loading the image is left to imageman, but all in all it seems to substract pixel component values between the two images and create some kind of average/ratio. Since the cairo library seems to generate its own, possibly incompatible, memory layout for images, most likely you want to ignore imageman and load the image you want to compare to yourself into a cairo memory buffer, then replicate the diffing algorithm iterating through the pixels of each caro image. Or maybe convert the cairo buffer to an imageman buffer and let the diffimg implementation do its magic.

If you have better answer, please add a comment about this, thank you!