JPEG quantization tables and progressive scan scripts

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

JPEG quantization tables and progressive scan scripts

Post by NicolasRobidoux »

Would it be possible to add the ability to pass alternative JPEG quantization tables/progressive scan scripts to, say, convert? (I'm pretty sure it's not possible now.)

These features are documented in the wizard.doc file included with the ImageMagick source distro (a copy of the JPEG Group's wizard.txt file).

One possible use (besides my own shenanigans :-)) would be to recompress a medium/low quality JPEG with exactly the quantization table used to create it (figured using, say, http://www.impulseadventure.com/photo/j ... ation.html) to avoid rounding error, chopping off high modes with progressive scan to make the file smaller, and doing this without rounding/truncation error messing up the low modes. What this basically does is remove fine detail without affecting the rest (it's using progressive encoding to perform low pass filtering, reducing file size in the process). (Which sounds like and expert only thing to do, but is actually pretty straightforward if one is provided with step-by-step instructions.) A similar dirty trick is that you could figure out which quantization table to use to re-encode so that integers mostly land on integers (that is, so that rounding is minimized), with a different overall quality setting (this particular dirty trick definitely requires some math), the simplest version being multiplying all entries of the "incoming" effective table resulting from the quantization/quality combination by 2, and then using -quality 100, to reduce file size and quality a big notch.
Last edited by NicolasRobidoux on 2012-05-23T06:33:12-07:00, edited 5 times in total.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: JPEG quantization tables and progressive scan scripts

Post by magick »

Sure, we'll code up support, perhaps this weekend.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: JPEG quantization tables and progressive scan scripts

Post by NicolasRobidoux »

@magick: Many thanks!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: JPEG quantization tables and progressive scan scripts

Post by magick »

Here's a peek at the new JPEG quantization tables invoked with "-define jpeg:q-table=myQTables.xml":

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

Re: JPEG quantization tables and progressive scan scripts

Post by magick »

The custom JPEG quantization table support is in ImageMagick 6.5.7-8 Beta, available by sometime tomorrow. Let us know have any suggestions concerning how we activate custom quantization tables (currently -define jpeg:q-table=path) or the format of the custom quantization table in XML. We include a sample quantization table in the ImageMagick distribution and call it quantization-table.xml. If you have a better name, let us know.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: JPEG quantization tables and progressive scan scripts

Post by NicolasRobidoux »

Many many thanks magick.

Will look into it ASAP.
lorents
Posts: 42
Joined: 2010-07-06T04:27:53-07:00
Authentication code: 8675308

Re: JPEG quantization tables and progressive scan scripts

Post by lorents »

Good afternoon!
Prompt, how it is possible to optimize jpeg through huffman table?
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: JPEG quantization tables and progressive scan scripts

Post by NicolasRobidoux »

lorents wrote:...how it is possible to optimize jpeg through huffman table?
Optimizing the Huffman tables is on by default. You can turn this optimization off (although why you'd do this is beyond me). See

Code: Select all

-define jpeg:optimize-coding=false
at http://www.imagemagick.org/Usage/formats/#jpg_write
lorents
Posts: 42
Joined: 2010-07-06T04:27:53-07:00
Authentication code: 8675308

Re: JPEG quantization tables and progressive scan scripts

Post by lorents »

I have not much other situation, I mean as it is possible to touch all possible huffman table values and to create JPEG optimize c in the smallest size?
cuongnh
Posts: 12
Joined: 2012-05-23T03:05:20-07:00
Authentication code: 13

Re: JPEG quantization tables and progressive scan scripts

Post by cuongnh »

Is there some code MagickCore or Magick++ code equivalent with this ?

-define jpeg:q-table=path
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: JPEG quantization tables and progressive scan scripts

Post by magick »

In MagickCore, use SetImageOption(). In Magick++, use defineValue().
cuongnh
Posts: 12
Joined: 2012-05-23T03:05:20-07:00
Authentication code: 13

Re: JPEG quantization tables and progressive scan scripts

Post by cuongnh »

Sorry

Could I use array instead of path to use quantization table in Magick Core
cuongnh
Posts: 12
Joined: 2012-05-23T03:05:20-07:00
Authentication code: 13

Re: JPEG quantization tables and progressive scan scripts

Post by cuongnh »

I mean may be it's array of quantization tables. Dont need to write quantization which i compute to xml file.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: JPEG quantization tables and progressive scan scripts

Post by anthony »

You must write it to a xml file in the format that is exampled (see above).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
cuongnh
Posts: 12
Joined: 2012-05-23T03:05:20-07:00
Authentication code: 13

Re: JPEG quantization tables and progressive scan scripts

Post by cuongnh »

Hello

I meet this bug and hope some one can fix for me.

My code:

Code: Select all

SetImageOption(image_info, "jpeg:q-table","quantization-table.xml");
When I use lib on cygwin which I download from home page or I build by myself I meet this bug

$ ./Magick_Compress.exe Adobe1.jpg resize.jpg
1 [sig] Magick_Compress 1576 open_stackdumpfile: Dumping stack trace to Ma
gick_Compress.exe.stackdump

But I use this library: CORE_RL_magick_.lib bug is fixed. So How I can use on cygwin?
Post Reply