Page 1 of 1

JPEG: qslots option through ImageMagick

Posted: 2013-04-19T14:10:36-07:00
by NicolasRobidoux
Would it be possible to have access to the "qslots" option of cjpeg (and libjpeg-turbo) through ImageMagick's convert?
Or is there already a "back door" way of passing this through?

Code: Select all

       -qslots N[,...]
              Select which quantization table to use for each color component.
This is the one missing option for me to be able to do exactly what I want using ImageMagick on its own.

Re: JPEG: qslots option through ImageMagick

Posted: 2013-04-21T11:15:11-07:00
by magick
You can define the slot in quantization-table.xml, activated with -define jpeg:q-table=quantization-table.xml. The table that comes with the ImageMagick distribution includes this sample table, customize to fit your requirements:

Code: Select all

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE quantization-tables [
<!ELEMENT quantization-tables (table)+>
<!ELEMENT table (description , levels)>
<!ELEMENT description (CDATA)>
<!ELEMENT levels (CDATA)>
<!ATTLIST table slot ID #REQUIRED>
<!ATTLIST levels width CDATA #REQUIRED>
<!ATTLIST levels height CDATA #REQUIRED>
<!ATTLIST levels divisor CDATA #REQUIRED>
]>
<!--
  JPEG quantization tables.
-->
<quantization-tables>
  <table slot="0" alias="luminance">
    <description>Luminance Quantization Table</description>
    <levels width="8" height="8" divisor="1">
      16, 12, 14, 17, 22, 30, 45, 72,
      12, 13, 14, 17, 22, 31, 46, 74,
      14, 14, 16, 19, 25, 35, 52, 83,
      17, 17, 19, 23, 30, 41, 62, 100,
      22, 22, 25, 30, 39, 54, 80, 129,
      30, 31, 35, 41, 54, 74, 111, 178,
      45, 46, 52, 62, 80, 111, 166, 267,
      72, 74, 83, 100, 129, 178, 267, 428
    </levels>
  </table>

  <table slot="1" alias="chrominance">
    <description>Chrominance Quantization Table</description>
    <levels width="8" height="8" divisor="1">
      17, 18, 22, 31, 50, 92, 193, 465,
      18, 19, 24, 33, 54, 98, 207, 498,
      22, 24, 29, 41, 66, 120, 253, 609,
      31, 33, 41, 57, 92, 169, 355, 854,
      50, 54, 66, 92, 148, 271, 570, 1370,
      92, 98, 120, 169, 271, 498, 1046, 2516,
      193, 207, 253, 355, 570, 1046, 2198, 5289,
      465, 498, 609, 854, 1370, 2516, 5289, 12725
    </levels>
  </table>
</quantization-tables>

Re: JPEG: qslots option through ImageMagick

Posted: 2013-04-21T14:44:00-07:00
by NicolasRobidoux
I want to do something slightly different: Use only one quantization table for all three channels, making sure that only one copy is embedded (instead of two or three).
Of course I can get the same quantization table for Luma and Chroma, by copying it twice.
How can I get the equivalent of -qslots 0, which only puts one single copy of the first quantization table in the image, and uses the same one across the board?

Re: JPEG: qslots option through ImageMagick

Posted: 2013-04-21T15:21:23-07:00
by magick
Ok, we'll get a patch into ImageMagick 6.8.5-0 Beta by sometime tomorrow. Set slot-0 in the the quantization table xml and it will replicate to the other components.

Re: JPEG: qslots option through ImageMagick

Posted: 2013-04-21T17:39:18-07:00
by NicolasRobidoux
Thank you.
The cjpeg flag I want to reproduce is "-qslots 0", which does exactly this: Use the first quantization table for all channels, and only embed one copy in the jpeg image, instead of copying it two or three times.

Re: JPEG: qslots option through ImageMagick

Posted: 2013-04-21T19:23:56-07:00
by NicolasRobidoux
And, BTW, my newest qtable (the "April Fool's" one) is really good in the 80 to 85 quality range, when used with -sample 2x2 -dct float -optimize -qslots 0.

Re: JPEG: qslots option through ImageMagick

Posted: 2013-05-03T18:21:41-07:00
by NicolasRobidoux
Cool! It works!

djpeg confirms that only one copy of the q-table is embedded when only one is specified (for slot 0) in the IM xml quantization table.

Re: JPEG: qslots option through ImageMagick

Posted: 2013-05-03T19:04:11-07:00
by NicolasRobidoux
I just replaced the quantization table example in config/ by my current, well tested, favorite for the usual 2x2 Chroma subsampling and (IJG-style) quality about 75 (for both Luma and Chroma).

I generally avoid to use 2x2 Chroma subsampling. But it gives decent compression if you don't mind a slightly "greyed out" look in regions where there is a lot of colour detail, which is why it is standard. Good and solid option, in other words.

(I also forgot to use svn changelist, making undesired changes in some config files in the IM7 svn :(

Re: JPEG: qslots option through ImageMagick

Posted: 2013-05-03T19:05:40-07:00
by NicolasRobidoux
I also changed the "luminance/chrominance" aliases to "luma/chroma" because these labels reflect that JPEG-type compression is really meant to be used in a perceptual colour space.

Kind of a snobbish thing to do.

I'll take no offence if overridden.