~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/file-jpeg/jpeg-settings.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-10-06 13:30:41 UTC
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: james.westby@ubuntu.com-20081006133041-3panbkcanaymfsmp
Tags: upstream-2.6.0
ImportĀ upstreamĀ versionĀ 2.6.0

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-settings.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
/*
 
23
 * Structure of the "jpeg-settings" parasite:
 
24
 *       1 byte  - JPEG color space (JCS_YCbCr, JCS_GRAYSCALE, JCS_CMYK, ...)
 
25
 *       1 byte  - quality (1..100 according to the IJG scale, or 0)
 
26
 *       1 byte  - number of components (0..4)
 
27
 *       1 byte  - number of quantization tables (0..4)
 
28
 *   C * 2 bytes - sampling factors for each component (1..4)
 
29
 * T * 128 bytes - quantization tables (only if different from IJG tables)
 
30
 *
 
31
 * Additional data following the quantization tables is currently
 
32
 * ignored and can be used for future extensions.
 
33
 *
 
34
 * In order to improve the compatibility with future versions of the
 
35
 * plug-in that may support more subsampling types ("subsmp"), the
 
36
 * parasite contains the original subsampling for each component
 
37
 * instead of saving only one byte containing the subsampling type as
 
38
 * used by the jpeg plug-in.  The same applies to the other settings:
 
39
 * for example, up to 4 quantization tables will be saved in the
 
40
 * parasite even if the current code cannot restore more than 2 of
 
41
 * them (4 tables may be needed by unusual JPEG color spaces such as
 
42
 * JCS_CMYK or JCS_YCCK).
 
43
 */
 
44
 
 
45
#include "config.h"
 
46
 
 
47
#include <string.h>
 
48
#include <setjmp.h>
 
49
 
 
50
#include <glib/gstdio.h>
 
51
 
 
52
#include <jpeglib.h>
 
53
 
 
54
#ifdef HAVE_EXIF
 
55
#include <libexif/exif-data.h>
 
56
#endif /* HAVE_EXIF */
 
57
 
 
58
#include <libgimp/gimp.h>
 
59
 
 
60
#include "libgimp/stdplugins-intl.h"
 
61
 
 
62
#include "jpeg.h"
 
63
#include "jpeg-quality.h"
 
64
#include "jpeg-settings.h"
 
65
 
 
66
/**
 
67
 * jpeg_detect_original_settings:
 
68
 * @cinfo: a pointer to a JPEG decompressor info.
 
69
 * @image_ID: the image to which the parasite should be attached.
 
70
 *
 
71
 * Analyze the image being decompressed (@cinfo) and extract the
 
72
 * sampling factors, quantization tables and overall image quality.
 
73
 * Store this information in a parasite and attach it to @image_ID.
 
74
 *
 
75
 * This function must be called after jpeg_read_header() so that
 
76
 * @cinfo contains the quantization tables and the sampling factors
 
77
 * for each component.
 
78
 *
 
79
 * Return Value: TRUE if a parasite has been attached to @image_ID.
 
80
 */
 
81
gboolean
 
82
jpeg_detect_original_settings (struct jpeg_decompress_struct *cinfo,
 
83
                               gint32                         image_ID)
 
84
{
 
85
  guint         parasite_size;
 
86
  guchar       *parasite_data;
 
87
  GimpParasite *parasite;
 
88
  guchar       *dest;
 
89
  gint          quality;
 
90
  gint          num_quant_tables = 0;
 
91
  gint          t;
 
92
  gint          i;
 
93
 
 
94
  g_return_val_if_fail (cinfo != NULL, FALSE);
 
95
  if (cinfo->jpeg_color_space == JCS_UNKNOWN
 
96
      || cinfo->out_color_space == JCS_UNKNOWN)
 
97
    return FALSE;
 
98
 
 
99
  quality = jpeg_detect_quality (cinfo);
 
100
  /* no need to attach quantization tables if they are the ones from IJG */
 
101
  if (quality <= 0)
 
102
    {
 
103
      for (t = 0; t < 4; t++)
 
104
        if (cinfo->quant_tbl_ptrs[t])
 
105
          num_quant_tables++;
 
106
    }
 
107
 
 
108
  parasite_size = 4 + cinfo->num_components * 2 + num_quant_tables * 128;
 
109
  parasite_data = g_new (guchar, parasite_size);
 
110
  dest = parasite_data;
 
111
 
 
112
  *dest++ = CLAMP0255 (cinfo->jpeg_color_space);
 
113
  *dest++ = ABS (quality);
 
114
  *dest++ = CLAMP0255 (cinfo->num_components);
 
115
  *dest++ = num_quant_tables;
 
116
 
 
117
  for (i = 0; i < cinfo->num_components; i++)
 
118
    {
 
119
      *dest++ = CLAMP0255 (cinfo->comp_info[i].h_samp_factor);
 
120
      *dest++ = CLAMP0255 (cinfo->comp_info[i].v_samp_factor);
 
121
    }
 
122
 
 
123
  if (quality <= 0)
 
124
    {
 
125
      for (t = 0; t < 4; t++)
 
126
        if (cinfo->quant_tbl_ptrs[t])
 
127
          for (i = 0; i < DCTSIZE2; i++)
 
128
            {
 
129
              guint16 c = cinfo->quant_tbl_ptrs[t]->quantval[i];
 
130
              *dest++ = c / 256;
 
131
              *dest++ = c & 255;
 
132
            }
 
133
    }
 
134
 
 
135
  parasite = gimp_parasite_new ("jpeg-settings",
 
136
                                GIMP_PARASITE_PERSISTENT,
 
137
                                parasite_size,
 
138
                                parasite_data);
 
139
  g_free (parasite_data);
 
140
  gimp_image_parasite_attach (image_ID, parasite);
 
141
  gimp_parasite_free (parasite);
 
142
  return TRUE;
 
143
}
 
144
 
 
145
 
 
146
/*
 
147
 * TODO: compare the JPEG color space found in the parasite with the
 
148
 * GIMP color space of the drawable to be saved.  If one of them is
 
149
 * grayscale and the other isn't, then the quality setting may be used
 
150
 * but the subsampling parameters and quantization tables should be
 
151
 * ignored.  The drawable_ID needs to be passed around because the
 
152
 * color space of the drawable may be different from that of the image
 
153
 * (e.g., when saving a mask or channel).
 
154
 */
 
155
 
 
156
/**
 
157
 * jpeg_restore_original_settings:
 
158
 * @image_ID: the image that may contain original jpeg settings in a parasite.
 
159
 * @quality: where to store the original jpeg quality.
 
160
 * @subsmp: where to store the original subsampling type.
 
161
 * @num_quant_tables: where to store the number of quantization tables found.
 
162
 *
 
163
 * Retrieve the original JPEG settings (quality, type of subsampling
 
164
 * and number of quantization tables) from the parasite attached to
 
165
 * @image_ID.  If the number of quantization tables is greater than
 
166
 * zero, then these tables can be retrieved from the parasite by
 
167
 * calling jpeg_restore_original_tables().
 
168
 *
 
169
 * Return Value: TRUE if a valid parasite was attached to the image
 
170
 */
 
171
gboolean
 
172
jpeg_restore_original_settings (gint32           image_ID,
 
173
                                gint            *quality,
 
174
                                JpegSubsampling *subsmp,
 
175
                                gint            *num_quant_tables)
 
176
{
 
177
  GimpParasite *parasite;
 
178
  const guchar *src;
 
179
  glong         src_size;
 
180
  gint          color_space;
 
181
  gint          q;
 
182
  gint          num_components;
 
183
  gint          num_tables;
 
184
  guchar        h[3];
 
185
  guchar        v[3];
 
186
 
 
187
  g_return_val_if_fail (quality != NULL, FALSE);
 
188
  g_return_val_if_fail (subsmp != NULL, FALSE);
 
189
  g_return_val_if_fail (num_quant_tables != NULL, FALSE);
 
190
 
 
191
  parasite = gimp_image_parasite_find (image_ID, "jpeg-settings");
 
192
  if (parasite)
 
193
    {
 
194
      src = gimp_parasite_data (parasite);
 
195
      src_size = gimp_parasite_data_size (parasite);
 
196
      if (src_size >= 4)
 
197
        {
 
198
          color_space      = *src++;
 
199
          q                = *src++;
 
200
          num_components   = *src++;
 
201
          num_tables       = *src++;
 
202
 
 
203
          if (src_size >= (4 + num_components * 2 + num_tables * 128)
 
204
              && q <= 100 && num_tables <= 4)
 
205
            {
 
206
              *quality = q;
 
207
 
 
208
              /* the current plug-in can only create grayscale or YCbCr JPEGs */
 
209
              if (color_space == JCS_GRAYSCALE || color_space == JCS_YCbCr)
 
210
                *num_quant_tables = num_tables;
 
211
              else
 
212
                *num_quant_tables = -1;
 
213
 
 
214
              /* the current plug-in can only use subsampling for YCbCr (3) */
 
215
              *subsmp = -1;
 
216
              if (num_components == 3)
 
217
                {
 
218
                  h[0] = *src++;
 
219
                  v[0] = *src++;
 
220
                  h[1] = *src++;
 
221
                  v[1] = *src++;
 
222
                  h[2] = *src++;
 
223
                  v[2] = *src++;
 
224
 
 
225
                  if (h[1] == 1 && v[1] == 1 && h[2] == 1 && v[2] == 1)
 
226
                    {
 
227
                      if (h[0] == 1 && v[0] == 1)
 
228
                        *subsmp = JPEG_SUPSAMPLING_1x1_1x1_1x1;
 
229
                      else if (h[0] == 2 && v[0] == 1)
 
230
                        *subsmp = JPEG_SUPSAMPLING_2x1_1x1_1x1;
 
231
                      else if (h[0] == 1 && v[0] == 2)
 
232
                        *subsmp = JPEG_SUPSAMPLING_1x2_1x1_1x1;
 
233
                      else if (h[0] == 2 && v[0] == 2)
 
234
                        *subsmp = JPEG_SUPSAMPLING_2x2_1x1_1x1;
 
235
                    }
 
236
                }
 
237
 
 
238
              gimp_parasite_free (parasite);
 
239
              return TRUE;
 
240
            }
 
241
        }
 
242
 
 
243
      gimp_parasite_free (parasite);
 
244
    }
 
245
 
 
246
  *quality = -1;
 
247
  *subsmp = -1;
 
248
  *num_quant_tables = 0;
 
249
 
 
250
  return FALSE;
 
251
}
 
252
 
 
253
 
 
254
/**
 
255
 * jpeg_restore_original_tables:
 
256
 * @image_ID: the image that may contain original jpeg settings in a parasite.
 
257
 * @num_quant_tables: the number of quantization tables to restore.
 
258
 *
 
259
 * Retrieve the original quantization tables from the parasite
 
260
 * attached to @image_ID.  Each table is an array of coefficients that
 
261
 * can be associated with a component of a JPEG image when saving it.
 
262
 *
 
263
 * An array of newly allocated tables is returned if @num_quant_tables
 
264
 * matches the number of tables saved in the parasite.  These tables
 
265
 * are returned as arrays of unsigned integers even if they will never
 
266
 * use more than 16 bits (8 bits in most cases) because the IJG JPEG
 
267
 * library expects arrays of unsigned integers.  When these tables are
 
268
 * not needed anymore, the caller should free them using g_free().  If
 
269
 * no parasite exists or if it cannot be used, this function returns
 
270
 * NULL.
 
271
 *
 
272
 * Return Value: an array of quantization tables, or NULL.
 
273
 */
 
274
guint **
 
275
jpeg_restore_original_tables (gint32    image_ID,
 
276
                              gint      num_quant_tables)
 
277
{
 
278
  GimpParasite *parasite;
 
279
  const guchar *src;
 
280
  glong         src_size;
 
281
  gint          num_components;
 
282
  gint          num_tables;
 
283
  guint       **quant_tables;
 
284
  gint          t;
 
285
  gint          i;
 
286
 
 
287
  parasite = gimp_image_parasite_find (image_ID, "jpeg-settings");
 
288
  if (parasite)
 
289
    {
 
290
      src_size = gimp_parasite_data_size (parasite);
 
291
      if (src_size >= 4)
 
292
        {
 
293
          src = gimp_parasite_data (parasite);
 
294
          num_components = src[2];
 
295
          num_tables     = src[3];
 
296
 
 
297
          if (src_size >= (4 + num_components * 2 + num_tables * 128)
 
298
              && num_tables == num_quant_tables)
 
299
            {
 
300
              src += 4 + num_components * 2;
 
301
              quant_tables = g_new (guint *, num_tables);
 
302
 
 
303
              for (t = 0; t < num_tables; t++)
 
304
                {
 
305
                  quant_tables[t] = g_new (guint, 128);
 
306
                  for (i = 0; i < 64; i++)
 
307
                    {
 
308
                      guint c;
 
309
 
 
310
                      c = *src++ * 256;
 
311
                      c += *src++;
 
312
                      quant_tables[t][i] = c;
 
313
                    }
 
314
                }
 
315
              gimp_parasite_free (parasite);
 
316
              return quant_tables;
 
317
            }
 
318
        }
 
319
      gimp_parasite_free (parasite);
 
320
    }
 
321
  return NULL;
 
322
}
 
323
 
 
324
 
 
325
/**
 
326
 * jpeg_swap_original_settings:
 
327
 * @image_ID: the image that may contain original jpeg settings in a parasite.
 
328
 *
 
329
 * Swap the horizontal and vertical axis for the saved subsampling
 
330
 * parameters and quantization tables.  This should be done if the
 
331
 * image has been rotated by +90 or -90 degrees or if it has been
 
332
 * mirrored along its diagonal.
 
333
 */
 
334
void
 
335
jpeg_swap_original_settings (gint32 image_ID)
 
336
{
 
337
  GimpParasite *parasite;
 
338
  const guchar *src;
 
339
  glong         src_size;
 
340
  gint          num_components;
 
341
  gint          num_tables;
 
342
  guchar       *new_data;
 
343
  guchar       *dest;
 
344
  gint          t;
 
345
  gint          i;
 
346
  gint          j;
 
347
 
 
348
  parasite = gimp_image_parasite_find (image_ID, "jpeg-settings");
 
349
  if (parasite)
 
350
    {
 
351
      src_size = gimp_parasite_data_size (parasite);
 
352
      if (src_size >= 4)
 
353
        {
 
354
          src = gimp_parasite_data (parasite);
 
355
          num_components = src[2];
 
356
          num_tables     = src[3];
 
357
 
 
358
          if (src_size >= (4 + num_components * 2 + num_tables * 128))
 
359
            {
 
360
              new_data = g_new (guchar, src_size);
 
361
              dest = new_data;
 
362
              *dest++ = *src++;
 
363
              *dest++ = *src++;
 
364
              *dest++ = *src++;
 
365
              *dest++ = *src++;
 
366
              for (i = 0; i < num_components; i++)
 
367
                {
 
368
                  dest[0] = src[1];
 
369
                  dest[1] = src[0];
 
370
                  dest += 2;
 
371
                  src += 2;
 
372
                }
 
373
              for (t = 0; t < num_tables; t++)
 
374
                {
 
375
                  for (i = 0; i < 8; i++)
 
376
                    {
 
377
                      for (j = 0; j < 8; j++)
 
378
                        {
 
379
                          dest[i * 16 + j * 2]     = src[j * 16 + i * 2];
 
380
                          dest[i * 16 + j * 2 + 1] = src[j * 16 + i * 2 + 1];
 
381
                        }
 
382
                    }
 
383
                  dest += 128;
 
384
                  src += 128;
 
385
                  if (src_size > (4 + num_components * 2 + num_tables * 128))
 
386
                    {
 
387
                      memcpy (dest, src, src_size - (4 + num_components * 2
 
388
                                                     + num_tables * 128));
 
389
                    }
 
390
                }
 
391
              gimp_parasite_free (parasite);
 
392
              parasite = gimp_parasite_new ("jpeg-settings",
 
393
                                            GIMP_PARASITE_PERSISTENT,
 
394
                                            src_size,
 
395
                                            new_data);
 
396
              g_free (new_data);
 
397
              gimp_image_parasite_attach (image_ID, parasite);
 
398
            }
 
399
        }
 
400
      gimp_parasite_free (parasite);
 
401
    }
 
402
}