1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
5
* Copyright (C) 2007 Raphaƫl Quinet <raphael@gimp.org>
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.
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.
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.
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)
31
* Additional data following the quantization tables is currently
32
* ignored and can be used for future extensions.
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).
50
#include <glib/gstdio.h>
55
#include <libexif/exif-data.h>
56
#endif /* HAVE_EXIF */
58
#include <libgimp/gimp.h>
60
#include "libgimp/stdplugins-intl.h"
63
#include "jpeg-quality.h"
64
#include "jpeg-settings.h"
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.
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.
75
* This function must be called after jpeg_read_header() so that
76
* @cinfo contains the quantization tables and the sampling factors
79
* Return Value: TRUE if a parasite has been attached to @image_ID.
82
jpeg_detect_original_settings (struct jpeg_decompress_struct *cinfo,
86
guchar *parasite_data;
87
GimpParasite *parasite;
90
gint num_quant_tables = 0;
94
g_return_val_if_fail (cinfo != NULL, FALSE);
95
if (cinfo->jpeg_color_space == JCS_UNKNOWN
96
|| cinfo->out_color_space == JCS_UNKNOWN)
99
quality = jpeg_detect_quality (cinfo);
100
/* no need to attach quantization tables if they are the ones from IJG */
103
for (t = 0; t < 4; t++)
104
if (cinfo->quant_tbl_ptrs[t])
108
parasite_size = 4 + cinfo->num_components * 2 + num_quant_tables * 128;
109
parasite_data = g_new (guchar, parasite_size);
110
dest = parasite_data;
112
*dest++ = CLAMP0255 (cinfo->jpeg_color_space);
113
*dest++ = ABS (quality);
114
*dest++ = CLAMP0255 (cinfo->num_components);
115
*dest++ = num_quant_tables;
117
for (i = 0; i < cinfo->num_components; i++)
119
*dest++ = CLAMP0255 (cinfo->comp_info[i].h_samp_factor);
120
*dest++ = CLAMP0255 (cinfo->comp_info[i].v_samp_factor);
125
for (t = 0; t < 4; t++)
126
if (cinfo->quant_tbl_ptrs[t])
127
for (i = 0; i < DCTSIZE2; i++)
129
guint16 c = cinfo->quant_tbl_ptrs[t]->quantval[i];
135
parasite = gimp_parasite_new ("jpeg-settings",
136
GIMP_PARASITE_PERSISTENT,
139
g_free (parasite_data);
140
gimp_image_parasite_attach (image_ID, parasite);
141
gimp_parasite_free (parasite);
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).
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.
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().
169
* Return Value: TRUE if a valid parasite was attached to the image
172
jpeg_restore_original_settings (gint32 image_ID,
174
JpegSubsampling *subsmp,
175
gint *num_quant_tables)
177
GimpParasite *parasite;
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);
191
parasite = gimp_image_parasite_find (image_ID, "jpeg-settings");
194
src = gimp_parasite_data (parasite);
195
src_size = gimp_parasite_data_size (parasite);
198
color_space = *src++;
200
num_components = *src++;
203
if (src_size >= (4 + num_components * 2 + num_tables * 128)
204
&& q <= 100 && num_tables <= 4)
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;
212
*num_quant_tables = -1;
214
/* the current plug-in can only use subsampling for YCbCr (3) */
216
if (num_components == 3)
225
if (h[1] == 1 && v[1] == 1 && h[2] == 1 && v[2] == 1)
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;
238
gimp_parasite_free (parasite);
243
gimp_parasite_free (parasite);
248
*num_quant_tables = 0;
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.
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.
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
272
* Return Value: an array of quantization tables, or NULL.
275
jpeg_restore_original_tables (gint32 image_ID,
276
gint num_quant_tables)
278
GimpParasite *parasite;
283
guint **quant_tables;
287
parasite = gimp_image_parasite_find (image_ID, "jpeg-settings");
290
src_size = gimp_parasite_data_size (parasite);
293
src = gimp_parasite_data (parasite);
294
num_components = src[2];
297
if (src_size >= (4 + num_components * 2 + num_tables * 128)
298
&& num_tables == num_quant_tables)
300
src += 4 + num_components * 2;
301
quant_tables = g_new (guint *, num_tables);
303
for (t = 0; t < num_tables; t++)
305
quant_tables[t] = g_new (guint, 128);
306
for (i = 0; i < 64; i++)
312
quant_tables[t][i] = c;
315
gimp_parasite_free (parasite);
319
gimp_parasite_free (parasite);
326
* jpeg_swap_original_settings:
327
* @image_ID: the image that may contain original jpeg settings in a parasite.
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.
335
jpeg_swap_original_settings (gint32 image_ID)
337
GimpParasite *parasite;
348
parasite = gimp_image_parasite_find (image_ID, "jpeg-settings");
351
src_size = gimp_parasite_data_size (parasite);
354
src = gimp_parasite_data (parasite);
355
num_components = src[2];
358
if (src_size >= (4 + num_components * 2 + num_tables * 128))
360
new_data = g_new (guchar, src_size);
366
for (i = 0; i < num_components; i++)
373
for (t = 0; t < num_tables; t++)
375
for (i = 0; i < 8; i++)
377
for (j = 0; j < 8; j++)
379
dest[i * 16 + j * 2] = src[j * 16 + i * 2];
380
dest[i * 16 + j * 2 + 1] = src[j * 16 + i * 2 + 1];
385
if (src_size > (4 + num_components * 2 + num_tables * 128))
387
memcpy (dest, src, src_size - (4 + num_components * 2
388
+ num_tables * 128));
391
gimp_parasite_free (parasite);
392
parasite = gimp_parasite_new ("jpeg-settings",
393
GIMP_PARASITE_PERSISTENT,
397
gimp_image_parasite_attach (image_ID, parasite);
400
gimp_parasite_free (parasite);