mozjpeg icon indicating copy to clipboard operation
mozjpeg copied to clipboard

output from `-quality 75` doesn't match default, no flag output.

Open olizilla opened this issue 9 years ago • 14 comments

usage.txt suggests the default quality is 75 https://github.com/mozilla/mozjpeg/blob/master/usage.txt#L73

but running cjpeg -quality 75 foo.jpg > foo.q75.jpg produces different output to cjpeg foo.jpg > foo.default.jpg

What else does setting the quality flag change in the output?

Also... when I specify the quality flag, then gm identify verbose it doesn't match what I passed to cjpeg...

$ cjpeg -quality 75 master.732.jpg > master.732.q75.jpg
$ gm identify -verbose master.732.q75.jpg | grep Quality
  JPEG-Quality: 40

But running it without a quality flag does indeed produce images that gm thinks are at quality 75

$ gm identify -verbose master.732.default.jpg | grep Quality
  JPEG-Quality: 75

so I'm a bit confused. Can anyone explain how the quality flag works?

olizilla avatar Jul 03 '15 15:07 olizilla

JPEG doesn't store quality as a single number. GraphicsMagic/ImageMagick's identify function does a trick that is based on assumption that the stock libjpeg was used, and it's not compatible with other quantization tables.

That quality output is not related to actual, human-perceived quality.

I suggest opening bug against gm/ImageMagick and ask them to tune the identify function to support other quantization tables.

kornelski avatar Jul 03 '15 16:07 kornelski

Thanks! I guess my real question is does specifying the -quality flag change anything else? If 75 is the default then my assumption was that specifying -quality 75 is the same as not specifying it.

olizilla avatar Jul 03 '15 16:07 olizilla

I'm just learning, and trying to get my head around this:

cjpeg pug.jpg -quality 75 pug.mozjpeg.q75.jpg
cjpeg pug.jpg > pug.mozjpeg.default.jpg
diff pug.mozjpeg.default.jpg pug.mozjpeg.q75.jpg
Binary files pug.mozjpeg.default.jpg and pug.mozjpeg.q75.jpg differ

I've stripped the exif data, but they still differ. Does mozjpeg use the -quality to change some other settings, or am i just misunderstanding how it works?

olizilla avatar Jul 08 '15 09:07 olizilla

Oh, it may be a bug.

In cjpeg when you use -quality, then set_quality_ratings() is called and it calls a LOCAL jpeg_default_qtables(), which sets quantization tables based on the new JINT_BASE_QUANT_TBL_IDX option.

Otherwise jpeg_set_defaults() is used, which calls jpeg_set_quality(cinfo, 75, TRUE); and jpeg_set_linear_quality, which relies on cinfo->master->quant_tbl_master_idx being set previously.

kornelski avatar Jul 08 '15 12:07 kornelski

@pornel many thanks for the explanation! Is there a flag to encourage mozjpeg to use the new quantization tables even when the -quality flag is used?

olizilla avatar Jul 09 '15 15:07 olizilla

When you use -quality you get the new quantization tables. It's only the default quality that also unintentionally defaults to the old tables.

kornelski avatar Jul 09 '15 15:07 kornelski

:+1: got it.

olizilla avatar Jul 09 '15 15:07 olizilla

@pornel This issue seems resolved but earlier you wrote "Oh, it may be a bug." Is there a bug here? If so, can you write out an explanation of what it is?

bdaehlie avatar May 25 '16 13:05 bdaehlie

If I remember correctly, the code that sets the default quality and code that sets quality explicitly have quite different implementations and they find qtable to use differently.

kornelski avatar May 25 '16 16:05 kornelski

Should they use the same implementation? Specifically, should we just change things so that default quality is set exactly the same as if the user had passed "-quality 75"?

bdaehlie avatar May 26 '16 12:05 bdaehlie

Yes, I think qtables should be selected the same way whether you set quality or not, and it'd be nice if there was a DRY implementation for it.

kornelski avatar May 26 '16 16:05 kornelski

@pornel Want to do this for v3.2?

bdaehlie avatar May 31 '16 04:05 bdaehlie

I can do it, but I can't promise I'll have time to do it for v3.2

kornelski avatar May 31 '16 08:05 kornelski

You need to use cjpeg -quant-table 0 -quality 75 to match it. See https://github.com/mozilla/mozjpeg/issues/395.

ValZapod avatar Jul 06 '21 22:07 ValZapod