JPEG: qslots option through ImageMagick

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

JPEG: qslots option through ImageMagick

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: JPEG: qslots option through ImageMagick

Post 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>
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: JPEG: qslots option through ImageMagick

Post 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?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: JPEG: qslots option through ImageMagick

Post 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.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: JPEG: qslots option through ImageMagick

Post 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.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: JPEG: qslots option through ImageMagick

Post 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.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: JPEG: qslots option through ImageMagick

Post 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.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: JPEG: qslots option through ImageMagick

Post 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 :(
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: JPEG: qslots option through ImageMagick

Post 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.
Post Reply