2

I'm writing a junit test case to validate a cropping and resizing class. The class is fairly simple, it just delegates the calls to ImageMagick via Im4Java. But to do things properly, I am trying to write up a jUnit test case.

I manually created expected result images using Photoshop and am trying to compare them to the images produced by my crop/resize class. The problem is that the headers that ImageMagick write and those by Photoshop differ. So a simple byte-by-byte comparison will not work.

ImageMagick offers a compare function, which when run from command line shows no pixel differences between the expected result and that produced by my class. But Im4Java does not provide any wrapper methods to this compare function.

Short of having to call Runtime.exec() myself, is there a way I can compare these two images in my test case?

Eric B.
  • 23,425
  • 50
  • 169
  • 316
  • 1
    I don't know Im4Java, but it looks like it has a [`CompareCmd`](http://im4java.sourceforge.net/api/org/im4java/core/CompareCmd.html) class. Will that not work? – yshavit Sep 07 '13 at 04:42
  • 1
    Depending on your compression techniques for the image (e.g. JPG) enough noise might be generated that byte-wise comparison of raster values might not work anyway, even though the images are identical to the human eye. See http://stackoverflow.com/questions/3400660/testing-graphics-generation-with-junit for some ideas of testing image generation. – lreeder Sep 07 '13 at 04:44
  • Outside of that, if you have a library that understands the specific image format you're using (jpeg, gif, whatever), you can probably use that to validate that both images have the same dimensions, and then iterate over all of the pixels in each and compare them (edit: Ireeder's link has some good ideas for that). – yshavit Sep 07 '13 at 04:44
  • @yshavit Thanks. I missed that one in the API docs. If you repost as a proper answer, I can credit you appropriately. – Eric B. Sep 10 '13 at 01:58
  • Ah cool, we all win. Thanks. :) – yshavit Sep 10 '13 at 02:59

1 Answers1

2

It looks like Im4Java has a CompareCmd class that will do the trick.

yshavit
  • 42,327
  • 7
  • 87
  • 124