dasch-swiss / sipi

Simple Image Presentation Interface
https://sipi.io
GNU Affero General Public License v3.0
32 stars 8 forks source link

ImageMagick reports distortion in some JP2<->TIFF conversions #155

Open benjamingeer opened 7 years ago

benjamingeer commented 7 years ago

These are the results of converting ISO 15444-4 test images 1, 5, and 7 (because Sipi crashes on the others), and checking peak absolute error (PAE) using ImageMagick's compare program with the OpenJPEG library.

ImageMagick reports no distortion with image 1. With images 5 and 7, the interesting thing is that we find exactly the same distortion, regardless of whether we convert from TIFF to JP2 or from JP2 to TIFF. And if we do a round-trip conversion, these two distortions seem to cancel each other out, so we get back the original, undistorted image.

Image 1: Reference tif -> jp2 (compare with reference jp2)
    From: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/reference_jp2/jp2_1.tif
    To: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmpbigdcgdq/sipi_file1.jp2
    Compare with: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/testfiles_jp2/file1.jp2
    PAE: 0 (0)

Image 1: Reference jp2 -> tif (compare with reference tif)
    From: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/testfiles_jp2/file1.jp2
    To: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmp55yklb25/sipi_jp2_1.tif
    Compare with: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/reference_jp2/jp2_1.tif
    PAE: 0 (0)

Image 1: Converted jp2 -> tif (compare with reference tif)
    From: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmpbigdcgdq/sipi_file1.jp2
    To: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmpn9l61or5/sipi_sipi_jp2_1.tif
    Compare with: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/reference_jp2/jp2_1.tif
    PAE: 0 (0)

Image 5: Reference tif -> jp2 (compare with reference jp2)
    From: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/reference_jp2/jp2_5.tif
    To: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmpq7prmwas/sipi_file5.jp2
    Compare with: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/testfiles_jp2/file5.jp2
    PAE: 32382 (0.494118)

Image 5: Reference jp2 -> tif (compare with reference tif)
    From: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/testfiles_jp2/file5.jp2
    To: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmp5usker4z/sipi_jp2_5.tif
    Compare with: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/reference_jp2/jp2_5.tif
    PAE: 32382 (0.494118)

Image 5: Converted jp2 -> tif (compare with reference tif)
    From: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmpq7prmwas/sipi_file5.jp2
    To: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmp72ouln1d/sipi_sipi_jp2_5.tif
    Compare with: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/reference_jp2/jp2_5.tif
    PAE: 0 (0)

Image 7: Reference tif -> jp2 (compare with reference jp2)
    From: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/reference_jp2/jp2_7.tif
    To: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmpqr7ax9rx/sipi_file7.jp2
    Compare with: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/testfiles_jp2/file7.jp2
    PAE: 24626 (0.375769)

Image 7: Reference jp2 -> tif (compare with reference tif)
    From: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/testfiles_jp2/file7.jp2
    To: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmp_wuev7z6/sipi_jp2_7.tif
    Compare with: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/reference_jp2/jp2_7.tif
    PAE: 24626 (0.375769)

Image 7: Converted jp2 -> tif (compare with reference tif)
    From: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmpqr7ax9rx/sipi_file7.jp2
    To: /var/folders/k8/mc3q8h_92jv3gn9wm3v_jblw0000gp/T/tmpy4_qk3b8/sipi_sipi_jp2_7.tif
    Compare with: /Users/benjamingeer/git/Sipi/test/_test_data/images/iso-15444-4/reference_jp2/jp2_7.tif
    PAE: 0 (0)

You can run these by typing make check on the wip/jp2-conformance-testing branch. It requires an installation of ImageMagick with OpenJPEG built in.

benjamingeer commented 7 years ago

The reference JPEG 2000 images:

https://drive.switch.ch/index.php/s/kdlHhsjsPOfchRI

The reference TIFF images:

https://drive.switch.ch/index.php/s/b6fggVqWlL7ZIsT

benjamingeer commented 7 years ago

To reproduce with one image:

$ local/bin/sipi -f reference_jp2/jp2_5.tif sipi_file5.jp2
$ compare -metric PAE testfiles_jp2/file5.jp2 sipi_file5.jp2 null:
32382 (0.494118)
$ local/bin/sipi -f testfiles_jp2/file5.jp2 sipi_jp2_5.tif
$ compare -metric PAE reference_jp2/jp2_5.tif sipi_jp2_5.tif null:
32382 (0.494118)
$ local/bin/sipi -f sipi_file5.jp2 sipi_sipi_jp2_5.tif
$ compare -metric PAE sipi_sipi_jp2_5.tif reference_jp2/jp2_5.tif null:
0 (0)
benjamingeer commented 7 years ago

OK here's something really interesting: I get exactly the same results if I convert the files with macOS Preview instead of with Sipi:

$ compare -metric PAE testfiles_jp2/file5.jp2 preview_file5.jp2 null:
32382 (0.494118)
$ compare -metric PAE reference_jp2/jp2_5.tif preview_jp2_5.tif null:
32382 (0.494118)
$ compare -metric PAE reference_jp2/jp2_5.tif preview_preview_jp2_5.tif null:
0 (0)
benjamingeer commented 7 years ago

And I get the same thing if I use ImageMagick to convert the files!

$ convert reference_jp2/jp2_5.tif im_file5.jp2
$ compare -metric PAE testfiles_jp2/file5.jp2 im_file5.jp2 null:
32382 (0.494118)
$ convert testfiles_jp2/file5.jp2 im_jp2_5.tif
$ compare -metric PAE reference_jp2/jp2_5.tif im_jp2_5.tif null:
32382 (0.494118)
$ convert im_file5.jp2 im_im_jp2_5.tif
$ compare -metric PAE reference_jp2/jp2_5.tif im_im_jp2_5.tif null:
0 (0)
benjamingeer commented 7 years ago

I opened an ImageMagick issue to ask about this: https://github.com/ImageMagick/ImageMagick/issues/409

benjamingeer commented 7 years ago

ImageMagick have confirmed it's a bug in compare:

We are not detecting the colorspace properly. Some of the images are in the Rec601YCbCr colorspace.... We will apply a patch soon to properly detect the colorspace in JP2 images.