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?
CodePudding user response:
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.