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