~ubuntu-branches/ubuntu/intrepid/gimp/intrepid

« back to all changes in this revision

Viewing changes to plug-ins/jpeg/jpeg-quality.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-10-06 13:30:41 UTC
  • mfrom: (1.1.15 upstream)
  • Revision ID: james.westby@ubuntu.com-20081006133041-axco233xt49jobn7
Tags: 2.6.0-1ubuntu1
* Sync on debian and new version (lp: #276839)
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch:
  - updated some strings for ubuntu
* debian/rules:
  - updated translation templates

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* GIMP - The GNU Image Manipulation Program
2
 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3
 
 *
4
 
 * jpeg-quality.c
5
 
 * Copyright (C) 2007 Raphaël Quinet <raphael@gimp.org>
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
 
 */
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include <glib/gstdio.h>
25
 
 
26
 
#include <jpeglib.h>
27
 
 
28
 
#include "jpeg-quality.h"
29
 
 
30
 
/*
31
 
 * Note that although 0 is a valid quality for IJG's JPEG library, the
32
 
 * baseline quantization tables for quality 0 and 1 are identical (all
33
 
 * divisors are set to the maximum value 255).  So 0 can be used here
34
 
 * to flag unusual settings.
35
 
 */
36
 
 
37
 
/* sum of the luminance divisors for quality = 0..100 (IJG standard tables) */
38
 
static gint std_luminance_sum[101] =
39
 
{
40
 
  G_MAXINT,
41
 
  16320, 16315, 15946, 15277, 14655, 14073, 13623, 13230, 12859, 12560, 12240,
42
 
  11861, 11456, 11081, 10714, 10360, 10027,  9679,  9368,  9056,  8680,  8331,
43
 
   7995,  7668,  7376,  7084,  6823,  6562,  6345,  6125,  5939,  5756,  5571,
44
 
   5421,  5240,  5086,  4976,  4829,  4719,  4616,  4463,  4393,  4280,  4166,
45
 
   4092,  3980,  3909,  3835,  3755,  3688,  3621,  3541,  3467,  3396,  3323,
46
 
   3247,  3170,  3096,  3021,  2952,  2874,  2804,  2727,  2657,  2583,  2509,
47
 
   2437,  2362,  2290,  2211,  2136,  2068,  1996,  1915,  1858,  1773,  1692,
48
 
   1620,  1552,  1477,  1398,  1326,  1251,  1179,  1109,  1031,   961,   884,
49
 
    814,   736,   667,   592,   518,   441,   369,   292,   221,   151,    86,
50
 
     64
51
 
};
52
 
 
53
 
/* sum of the chrominance divisors for quality = 0..100 (IJG standard tables) */
54
 
static gint std_chrominance_sum[101] =
55
 
{
56
 
  G_MAXINT,
57
 
  16320, 16320, 16320, 16218, 16010, 15731, 15523, 15369, 15245, 15110, 14985,
58
 
  14864, 14754, 14635, 14526, 14429, 14346, 14267, 14204, 13790, 13121, 12511,
59
 
  11954, 11453, 11010, 10567, 10175,  9787,  9455,  9122,  8844,  8565,  8288,
60
 
   8114,  7841,  7616,  7447,  7227,  7060,  6897,  6672,  6562,  6396,  6226,
61
 
   6116,  5948,  5838,  5729,  5614,  5505,  5396,  5281,  5172,  5062,  4947,
62
 
   4837,  4726,  4614,  4506,  4395,  4282,  4173,  4061,  3950,  3839,  3727,
63
 
   3617,  3505,  3394,  3284,  3169,  3060,  2949,  2836,  2780,  2669,  2556,
64
 
   2445,  2336,  2221,  2111,  2000,  1888,  1778,  1666,  1555,  1444,  1332,
65
 
   1223,  1110,   999,   891,   779,   668,   558,   443,   333,   224,   115,
66
 
     64
67
 
};
68
 
 
69
 
/**
70
 
 * jpeg_detect_quality:
71
 
 * @cinfo: a pointer to a JPEG decompressor info.
72
 
 *
73
 
 * Returns the exact or estimated quality value that was used to save
74
 
 * the JPEG image by analyzing the quantization table divisors.
75
 
 *
76
 
 * If an exact match for the IJG quantization tables is found, then a
77
 
 * quality setting in the range 1..100 is returned.  If the quality
78
 
 * can only be estimated, then a negative number in the range -1..-100
79
 
 * is returned; its absolute value represents the maximum IJG quality
80
 
 * setting to use.  If the quality cannot be reliably determined, then
81
 
 * 0 is returned.
82
 
 *
83
 
 * This function must be called after jpeg_read_header() so that
84
 
 * @cinfo contains the quantization tables read from the DQT markers
85
 
 * in the file.
86
 
 *
87
 
 * Return Value: the JPEG quality setting in the range 1..100, -1..-100 or 0.
88
 
 */
89
 
gint
90
 
jpeg_detect_quality (struct jpeg_decompress_struct *cinfo)
91
 
{
92
 
  gint t;
93
 
  gint i;
94
 
  gint sum[3];
95
 
  gint q;
96
 
 
97
 
  /* files using CMYK or having 4 quantization tables are unusual */
98
 
  if (!cinfo || cinfo->output_components > 3 || cinfo->quant_tbl_ptrs[3])
99
 
    return 0;
100
 
 
101
 
  /* Most files use table 0 for luminance divisors (Y) and table 1 for
102
 
   * chrominance divisors (Cb and Cr).  Some files use separate tables
103
 
   * for Cb and Cr, so table 2 may also be used.
104
 
   */
105
 
  for (t = 0; t < 3; t++)
106
 
    {
107
 
      sum[t] = 0;
108
 
      if (cinfo->quant_tbl_ptrs[t])
109
 
        for (i = 0; i < DCTSIZE2; i++)
110
 
          sum[t] += cinfo->quant_tbl_ptrs[t]->quantval[i];
111
 
    }
112
 
 
113
 
  if (cinfo->output_components > 1)
114
 
    {
115
 
      gint sums;
116
 
 
117
 
      if (sum[0] < 64 || sum[1] < 64)
118
 
        return 0;
119
 
 
120
 
      /* compare with the chrominance table having the lowest sum */
121
 
      if (sum[1] < sum[2] || sum[2] <= 0)
122
 
        sums = sum[0] + sum[1];
123
 
      else
124
 
        sums = sum[0] + sum[2];
125
 
 
126
 
      q = 100;
127
 
      while (sums > std_luminance_sum[q] + std_chrominance_sum[q])
128
 
        q--;
129
 
 
130
 
      if (sum[0] == std_luminance_sum[q] && sum[1] == std_chrominance_sum[q])
131
 
        return q;
132
 
      else
133
 
        return -q;
134
 
    }
135
 
  else
136
 
    {
137
 
      if (sum[0] < 64)
138
 
        return 0;
139
 
 
140
 
      q = 100;
141
 
      while (sum[0] > std_luminance_sum[q])
142
 
        q--;
143
 
 
144
 
      if (sum[0] == std_luminance_sum[q])
145
 
        return q;
146
 
      else
147
 
        return -q;
148
 
    }
149
 
}