Quality loss after compressing JPEG image to lossless JP2

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
johan
Posts: 7
Joined: 2010-10-12T09:08:23-07:00
Authentication code: 8675308

Quality loss after compressing JPEG image to lossless JP2

Post by johan »

After compressing a JPEG image to lossless (!) JP2, the pixel values of the resulting JP2 are not identical to those of the source JPEG. Full description, tests and sample images here:

https://github.com/bitsgalore/jpegToLosslessJP2

The issue doesn't occur for other input formats (and neither for different output formats), and it looks like IM somehow doesn't properly decode the source JPEG.

Issue encountered with ImageMagick 6.8.9-8 Q16 x64 2014-08-26; running under Windows 7.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Quality loss after compressing JPEG image to lossless JP2

Post by fmw42 »

I do not believe you can start with a simple jpg and save to jp2 and expect to get a perfect comparison. You lose quality when the jpg is loaded and decompressed. Try starting with jp2 and going to another jp2. However, I am not a compression or JP2 expert and would defer to some one who is more expert in the field.
johan
Posts: 7
Joined: 2010-10-12T09:08:23-07:00
Authentication code: 8675308

Re: Quality loss after compressing JPEG image to lossless JP2

Post by johan »

If you look at the JPEG to JP2 via TIFF example in my original link, you see that it is actually possible to get a perfect match (also if I do the comparison between the source JPEG and the result TIFF both are identical). So it is definitely possible, but the JPEG decode behaviour appears to depend on the selected output format (which shouldn't be the case).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Quality loss after compressing JPEG image to lossless JP2

Post by fmw42 »

Perhaps I misunderstand, but if you start with a tiff, (unless it has lossy jpg compression,) then there is no decompression involved in reading the tiff and then converting it to JP2. So the comparison of tiff to jp2 should be exact.
compare -metric PSNR balloon.jpg balloon_im.tiff balloon_im_from_tiff.jp2 NUL
I am not sure IM can do a 3 image compare. So I am not sure what which images are being compared. Also it should be null: not NUL

Try comparing only two images at a time.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Quality loss after compressing JPEG image to lossless JP2

Post by magick »

Try convert image.jpg -quality 0 image.jp2.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Quality loss after compressing JPEG image to lossless JP2

Post by dlemstra »

I just submitted a patch to our git repository to no longer use the quality of the input image but use the specified '-quality' instead. You can use magick's until the next version has been released.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
johan
Posts: 7
Joined: 2010-10-12T09:08:23-07:00
Authentication code: 8675308

Re: Quality loss after compressing JPEG image to lossless JP2

Post by johan »

Great, I can confirm that setting -quality to 0 fixes the issue. Changing the default behaviour makes a lot of sense, good to see this will be changed in the next version. Many thanks all for the fast responses!
johan
Posts: 7
Joined: 2010-10-12T09:08:23-07:00
Authentication code: 8675308

Re: Quality loss after compressing JPEG image to lossless JP2

Post by johan »

fmw42 wrote: I am not sure IM can do a 3 image compare. So I am not sure what which images are being compared. Also it should be null: not NUL
Oops, I see I made a typo there; that should've been:

Code: Select all

compare -metric PSNR balloon.jpg balloon_im_from_tiff.jp2 NUL
Result is still 1.#INF though.

(BTW NUL seems to be correct for Windows, if I use null it creates a diff file with that name!)
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Quality loss after compressing JPEG image to lossless JP2

Post by dlemstra »

It should be 'null:' not 'null'.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply