PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, May 7, 2022

[FIXED] How can one do a pixel by pixel comparison of an image created by cairo and an image loaded from a file

 May 07, 2022     cairo, diff, image, nim-lang     No comments   

Issue

I have some code in nim that creates a picture using Cairo (https://github.com/nim-lang/cairo). I would like to compare that picture to another using diffimg (https://github.com/SolitudeSF/diffimg, https://github.com/SolitudeSF/imageman).

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?


Solution

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
  else:
    b - a

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

func genDiffImage*[T: Color](a, b: Image[T]): Image[T] =
  result = initImage[T](a.w, a.h)
  for p in 0..result.data.high:
    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.



Answered By - Grzegorz Adam Hankiewicz
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing