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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

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
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <string.h>
 
22
#include <errno.h>
 
23
#include <setjmp.h>
 
24
 
 
25
#include <glib/gstdio.h>
 
26
 
 
27
#include <jpeglib.h>
 
28
#include <jerror.h>
 
29
 
 
30
#ifdef HAVE_EXIF
 
31
#include <libexif/exif-data.h>
 
32
#endif /* HAVE_EXIF */
 
33
 
 
34
#include <libgimp/gimp.h>
 
35
#include <libgimp/gimpui.h>
 
36
 
 
37
#include "libgimp/stdplugins-intl.h"
 
38
 
 
39
#include "jpeg.h"
 
40
#include "jpeg-icc.h"
 
41
#include "jpeg-load.h"
 
42
 
 
43
gint32 volatile  image_ID_global;
 
44
GimpDrawable    *drawable_global;
 
45
gint32           layer_ID_global;
 
46
 
 
47
gint32
 
48
load_image (const gchar *filename,
 
49
            GimpRunMode  runmode,
 
50
            gboolean     preview)
 
51
{
 
52
  GimpPixelRgn     pixel_rgn;
 
53
  GimpDrawable    *drawable;
 
54
  gint32 volatile  image_ID;
 
55
  gint32           layer_ID;
 
56
  struct jpeg_decompress_struct cinfo;
 
57
  struct my_error_mgr           jerr;
 
58
  FILE    *infile;
 
59
  guchar  *buf;
 
60
  guchar  * volatile padded_buf = NULL;
 
61
  guchar **rowbuf;
 
62
  guchar  *profile;
 
63
  guint    profile_size;
 
64
  gint     image_type;
 
65
  gint     layer_type;
 
66
  gint     tile_height;
 
67
  gint     scanlines;
 
68
  gint     i, start, end;
 
69
  GString *local_image_comments = NULL;
 
70
  GimpParasite * volatile comment_parasite = NULL;
 
71
  jpeg_saved_marker_ptr marker;
 
72
 
 
73
  /* We set up the normal JPEG error routines. */
 
74
  cinfo.err = jpeg_std_error (&jerr.pub);
 
75
  jerr.pub.error_exit = my_error_exit;
 
76
 
 
77
  /* flag warnings, so we try to ignore corrupt EXIF data */
 
78
  if (!preview)
 
79
    {
 
80
      cinfo.client_data = GINT_TO_POINTER (FALSE);
 
81
 
 
82
      jerr.pub.emit_message   = my_emit_message;
 
83
      jerr.pub.output_message = my_output_message;
 
84
    }
 
85
 
 
86
  if ((infile = g_fopen (filename, "rb")) == NULL)
 
87
    {
 
88
      g_message (_("Could not open '%s' for reading: %s"),
 
89
                 gimp_filename_to_utf8 (filename), g_strerror (errno));
 
90
      return -1;
 
91
    }
 
92
 
 
93
  if (!preview)
 
94
    gimp_progress_init_printf (_("Opening '%s'"),
 
95
                               gimp_filename_to_utf8 (filename));
 
96
 
 
97
  image_ID = -1;
 
98
 
 
99
  /* Establish the setjmp return context for my_error_exit to use. */
 
100
  if (setjmp (jerr.setjmp_buffer))
 
101
    {
 
102
      /* If we get here, the JPEG code has signaled an error.
 
103
       * We need to clean up the JPEG object, close the input file, and return.
 
104
       */
 
105
      jpeg_destroy_decompress (&cinfo);
 
106
      if (infile)
 
107
        fclose (infile);
 
108
 
 
109
      if (image_ID != -1 && !preview)
 
110
        gimp_image_delete (image_ID);
 
111
 
 
112
      if (preview)
 
113
        destroy_preview();
 
114
 
 
115
      return -1;
 
116
    }
 
117
 
 
118
  /* Now we can initialize the JPEG decompression object. */
 
119
  jpeg_create_decompress (&cinfo);
 
120
 
 
121
  /* Step 2: specify data source (eg, a file) */
 
122
 
 
123
  jpeg_stdio_src (&cinfo, infile);
 
124
 
 
125
  if (! preview)
 
126
    {
 
127
      /* - step 2.1: tell the lib to save the comments */
 
128
      jpeg_save_markers (&cinfo, JPEG_COM, 0xffff);
 
129
 
 
130
      /* - step 2.2: tell the lib to save APP1 markers
 
131
       *   (may contain EXIF or XMP)
 
132
       */
 
133
      jpeg_save_markers (&cinfo, JPEG_APP0 + 1, 0xffff);
 
134
 
 
135
      /* - step 2.3: tell the lib to keep any APP2 data it may find */
 
136
      jpeg_icc_setup_read_profile (&cinfo);
 
137
    }
 
138
 
 
139
  /* Step 3: read file parameters with jpeg_read_header() */
 
140
 
 
141
  (void) jpeg_read_header (&cinfo, TRUE);
 
142
 
 
143
  /* We can ignore the return value from jpeg_read_header since
 
144
   *   (a) suspension is not possible with the stdio data source, and
 
145
   *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
 
146
   * See libjpeg.doc for more info.
 
147
   */
 
148
 
 
149
  /* Step 4: set parameters for decompression */
 
150
 
 
151
  /* In this example, we don't need to change any of the defaults set by
 
152
   * jpeg_read_header(), so we do nothing here.
 
153
   */
 
154
 
 
155
  /* Step 5: Start decompressor */
 
156
 
 
157
  jpeg_start_decompress (&cinfo);
 
158
 
 
159
  /* We may need to do some setup of our own at this point before reading
 
160
   * the data.  After jpeg_start_decompress() we have the correct scaled
 
161
   * output image dimensions available, as well as the output colormap
 
162
   * if we asked for color quantization.
 
163
   * In this example, we need to make an output work buffer of the right size.
 
164
   */
 
165
  /* temporary buffer */
 
166
  tile_height = gimp_tile_height ();
 
167
  buf = g_new (guchar,
 
168
               tile_height * cinfo.output_width * cinfo.output_components);
 
169
 
 
170
  rowbuf = g_new (guchar *, tile_height);
 
171
 
 
172
  for (i = 0; i < tile_height; i++)
 
173
    rowbuf[i] = buf + cinfo.output_width * cinfo.output_components * i;
 
174
 
 
175
  /* Create a new image of the proper size and associate the filename with it.
 
176
 
 
177
     Preview layers, not being on the bottom of a layer stack, MUST HAVE
 
178
     AN ALPHA CHANNEL!
 
179
   */
 
180
  switch (cinfo.output_components)
 
181
    {
 
182
    case 1:
 
183
      image_type = GIMP_GRAY;
 
184
      layer_type = preview ? GIMP_GRAYA_IMAGE : GIMP_GRAY_IMAGE;
 
185
      break;
 
186
 
 
187
    case 3:
 
188
      image_type = GIMP_RGB;
 
189
      layer_type = preview ? GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE;
 
190
      break;
 
191
 
 
192
    case 4:
 
193
      if (cinfo.out_color_space == JCS_CMYK)
 
194
        {
 
195
          image_type = GIMP_RGB;
 
196
          layer_type = GIMP_RGB_IMAGE;
 
197
          break;
 
198
        }
 
199
      /*fallthrough*/
 
200
 
 
201
    default:
 
202
      g_message ("Don't know how to load JPEG images "
 
203
                 "with %d color channels, using colorspace %d (%d).",
 
204
                 cinfo.output_components, cinfo.out_color_space,
 
205
                 cinfo.jpeg_color_space);
 
206
      return -1;
 
207
      break;
 
208
    }
 
209
 
 
210
  if (preview)
 
211
    padded_buf = g_new (guchar, tile_height * cinfo.output_width *
 
212
                        (cinfo.output_components + 1));
 
213
  else if (cinfo.out_color_space == JCS_CMYK)
 
214
    padded_buf = g_new (guchar, tile_height * cinfo.output_width * 3);
 
215
  else
 
216
    padded_buf = NULL;
 
217
 
 
218
  if (preview)
 
219
    {
 
220
      image_ID = image_ID_global;
 
221
    }
 
222
  else
 
223
    {
 
224
      image_ID = gimp_image_new (cinfo.output_width, cinfo.output_height,
 
225
                                 image_type);
 
226
      gimp_image_set_filename (image_ID, filename);
 
227
    }
 
228
 
 
229
  if (preview)
 
230
    {
 
231
      layer_ID_global = layer_ID =
 
232
        gimp_layer_new (image_ID, _("JPEG preview"),
 
233
                        cinfo.output_width,
 
234
                        cinfo.output_height,
 
235
                        layer_type, 100, GIMP_NORMAL_MODE);
 
236
    }
 
237
  else
 
238
    {
 
239
      layer_ID = gimp_layer_new (image_ID, _("Background"),
 
240
                                 cinfo.output_width,
 
241
                                 cinfo.output_height,
 
242
                                 layer_type, 100, GIMP_NORMAL_MODE);
 
243
    }
 
244
 
 
245
  drawable_global = drawable = gimp_drawable_get (layer_ID);
 
246
  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
 
247
                       drawable->width, drawable->height, TRUE, FALSE);
 
248
 
 
249
  /* Step 5.1: if the file had resolution information, set it on the image */
 
250
  if (!preview && cinfo.saw_JFIF_marker)
 
251
    {
 
252
      gdouble xresolution;
 
253
      gdouble yresolution;
 
254
      gdouble asymmetry;
 
255
 
 
256
      xresolution = cinfo.X_density;
 
257
      yresolution = cinfo.Y_density;
 
258
 
 
259
      switch (cinfo.density_unit)
 
260
        {
 
261
        case 0: /* unknown -> set the aspect ratio but use the default
 
262
                *  image resolution
 
263
                */
 
264
          if (cinfo.Y_density != 0)
 
265
            asymmetry = xresolution / yresolution;
 
266
          else
 
267
            asymmetry = 1.0;
 
268
 
 
269
          gimp_image_get_resolution (image_ID, &xresolution, &yresolution);
 
270
          xresolution *= asymmetry;
 
271
          break;
 
272
 
 
273
        case 1: /* dots per inch */
 
274
          break;
 
275
 
 
276
        case 2: /* dots per cm */
 
277
          xresolution *= 2.54;
 
278
          yresolution *= 2.54;
 
279
          gimp_image_set_unit (image_ID, GIMP_UNIT_MM);
 
280
          break;
 
281
 
 
282
        default:
 
283
          g_message ("Unknown density unit %d, assuming dots per inch.",
 
284
                     cinfo.density_unit);
 
285
          break;
 
286
        }
 
287
 
 
288
      gimp_image_set_resolution (image_ID, xresolution, yresolution);
 
289
    }
 
290
 
 
291
  /* Step 5.2: check for metadata (comments, markers containing EXIF or XMP) */
 
292
  for (marker = cinfo.marker_list; marker; marker = marker->next)
 
293
    {
 
294
      const gchar *data = (const gchar *) marker->data;
 
295
      gsize        len  = marker->data_length;
 
296
 
 
297
      if (marker->marker == JPEG_COM)
 
298
        {
 
299
          g_print ("jpeg-load: found image comment (%d bytes)\n",
 
300
                   marker->data_length);
 
301
 
 
302
          if (!local_image_comments)
 
303
            {
 
304
              local_image_comments = g_string_new_len (data, len);
 
305
            }
 
306
          else
 
307
            {
 
308
              g_string_append_c (local_image_comments, '\n');
 
309
              g_string_append_len (local_image_comments, data, len);
 
310
            }
 
311
        }
 
312
      else if ((marker->marker == JPEG_APP0 + 1)
 
313
               && (len > 13)
 
314
               && ! strcmp (JPEG_APP_HEADER_EXIF, data))
 
315
        {
 
316
          /* FIXME: handle EXIF here once we don't use libexif anymore */
 
317
          g_print ("jpeg-load: found EXIF block (%d bytes)\n",
 
318
                   (gint) (len - sizeof (JPEG_APP_HEADER_EXIF)));
 
319
          /* Note: maybe split the loop to ensure that the EXIF block is */
 
320
          /*       always parsed before any XMP packet */
 
321
        }
 
322
      else if ((marker->marker == JPEG_APP0 + 1)
 
323
               && (len > 37)
 
324
               && ! strcmp (JPEG_APP_HEADER_XMP, data))
 
325
        {
 
326
          GimpParam *return_vals;
 
327
          gint       nreturn_vals;
 
328
          gchar     *xmp_packet;
 
329
 
 
330
          g_print ("jpeg-load: found XMP packet (%d bytes)\n",
 
331
                   (gint) (len - sizeof (JPEG_APP_HEADER_XMP)));
 
332
 
 
333
          xmp_packet = g_strndup (data + sizeof (JPEG_APP_HEADER_XMP),
 
334
                                  len - sizeof (JPEG_APP_HEADER_XMP));
 
335
 
 
336
          /* FIXME: running this through the PDB is not very efficient */
 
337
          return_vals = gimp_run_procedure ("plug-in-metadata-decode-xmp",
 
338
                                            &nreturn_vals,
 
339
                                            GIMP_PDB_IMAGE, image_ID,
 
340
                                            GIMP_PDB_STRING, xmp_packet,
 
341
                                            GIMP_PDB_END);
 
342
 
 
343
          if (return_vals[0].data.d_status != GIMP_PDB_SUCCESS)
 
344
            {
 
345
              g_warning ("JPEG - unable to decode XMP metadata packet");
 
346
            }
 
347
 
 
348
          gimp_destroy_params (return_vals, nreturn_vals);
 
349
          g_free (xmp_packet);
 
350
        }
 
351
    }
 
352
 
 
353
  /* Step 5.3: check for an embedded ICC profile */
 
354
  if (!preview && jpeg_icc_read_profile (&cinfo, &profile, &profile_size))
 
355
    {
 
356
      GimpParasite *parasite = gimp_parasite_new ("icc-profile",
 
357
                                                  0, profile_size, profile);
 
358
      gimp_image_parasite_attach (image_ID, parasite);
 
359
      gimp_parasite_free (parasite);
 
360
 
 
361
      g_free (profile);
 
362
    }
 
363
 
 
364
  if (!preview)
 
365
    {
 
366
      /* if we had any comments then make a parasite for them */
 
367
      if (local_image_comments && local_image_comments->len)
 
368
        {
 
369
          gchar *comment = local_image_comments->str;
 
370
 
 
371
          g_string_free (local_image_comments, FALSE);
 
372
 
 
373
          local_image_comments = NULL;
 
374
 
 
375
          if (g_utf8_validate (comment, -1, NULL))
 
376
            comment_parasite = gimp_parasite_new ("gimp-comment",
 
377
                                                  GIMP_PARASITE_PERSISTENT,
 
378
                                                  strlen (comment) + 1,
 
379
                                                  comment);
 
380
          g_free (comment);
 
381
        }
 
382
 
 
383
      /* Do not attach the "jpeg-save-options" parasite to the image
 
384
       * because this conflicts with the global defaults (bug #75398).
 
385
       */
 
386
    }
 
387
 
 
388
  /* Step 6: while (scan lines remain to be read) */
 
389
  /*           jpeg_read_scanlines(...); */
 
390
 
 
391
  /* Here we use the library's state variable cinfo.output_scanline as the
 
392
   * loop counter, so that we don't have to keep track ourselves.
 
393
   */
 
394
  while (cinfo.output_scanline < cinfo.output_height)
 
395
    {
 
396
      start = cinfo.output_scanline;
 
397
      end   = cinfo.output_scanline + tile_height;
 
398
      end   = MIN (end, cinfo.output_height);
 
399
      scanlines = end - start;
 
400
 
 
401
      for (i = 0; i < scanlines; i++)
 
402
        jpeg_read_scanlines (&cinfo, (JSAMPARRAY) &rowbuf[i], 1);
 
403
 
 
404
      if (preview) /* Add a dummy alpha channel -- convert buf to padded_buf */
 
405
        {
 
406
          guchar *dest = padded_buf;
 
407
          guchar *src  = buf;
 
408
          gint    num  = drawable->width * scanlines;
 
409
 
 
410
          switch (cinfo.output_components)
 
411
            {
 
412
            case 1:
 
413
              for (i = 0; i < num; i++)
 
414
                {
 
415
                  *(dest++) = *(src++);
 
416
                  *(dest++) = 255;
 
417
                }
 
418
              break;
 
419
 
 
420
            case 3:
 
421
              for (i = 0; i < num; i++)
 
422
                {
 
423
                  *(dest++) = *(src++);
 
424
                  *(dest++) = *(src++);
 
425
                  *(dest++) = *(src++);
 
426
                  *(dest++) = 255;
 
427
                }
 
428
              break;
 
429
 
 
430
            default:
 
431
              g_warning ("JPEG - shouldn't have gotten here.\n"
 
432
                         "Report to http://bugzilla.gnome.org/");
 
433
              break;
 
434
            }
 
435
        }
 
436
      else if (cinfo.out_color_space == JCS_CMYK) /* buf-> RGB in padded_buf */
 
437
        {
 
438
          guchar *dest = padded_buf;
 
439
          guchar *src  = buf;
 
440
          gint    num  = drawable->width * scanlines;
 
441
 
 
442
          for (i = 0; i < num; i++)
 
443
            {
 
444
              guint r_c, g_m, b_y, a_k;
 
445
 
 
446
              r_c = *(src++);
 
447
              g_m = *(src++);
 
448
              b_y = *(src++);
 
449
              a_k = *(src++);
 
450
              *(dest++) = (r_c * a_k) / 255;
 
451
              *(dest++) = (g_m * a_k) / 255;
 
452
              *(dest++) = (b_y * a_k) / 255;
 
453
            }
 
454
        }
 
455
 
 
456
      gimp_pixel_rgn_set_rect (&pixel_rgn, padded_buf ? padded_buf : buf,
 
457
                               0, start, drawable->width, scanlines);
 
458
 
 
459
      if (! preview)
 
460
        gimp_progress_update ((gdouble) cinfo.output_scanline /
 
461
                              (gdouble) cinfo.output_height);
 
462
    }
 
463
 
 
464
  /* Step 7: Finish decompression */
 
465
 
 
466
  jpeg_finish_decompress (&cinfo);
 
467
  /* We can ignore the return value since suspension is not possible
 
468
   * with the stdio data source.
 
469
   */
 
470
 
 
471
  /* Step 8: Release JPEG decompression object */
 
472
 
 
473
  /* This is an important step since it will release a good deal of memory. */
 
474
  jpeg_destroy_decompress (&cinfo);
 
475
 
 
476
  /* free up the temporary buffers */
 
477
  g_free (rowbuf);
 
478
  g_free (buf);
 
479
  g_free (padded_buf);
 
480
 
 
481
  /* After finish_decompress, we can close the input file.
 
482
   * Here we postpone it until after no more JPEG errors are possible,
 
483
   * so as to simplify the setjmp error logic above.  (Actually, I don't
 
484
   * think that jpeg_destroy can do an error exit, but why assume anything...)
 
485
   */
 
486
  fclose (infile);
 
487
 
 
488
  /* At this point you may want to check to see whether any corrupt-data
 
489
   * warnings occurred (test whether jerr.num_warnings is nonzero).
 
490
   */
 
491
 
 
492
  /* Detach from the drawable and add it to the image.
 
493
   */
 
494
  if (!preview)
 
495
    gimp_drawable_detach (drawable);
 
496
  gimp_image_add_layer (image_ID, layer_ID, 0);
 
497
 
 
498
  /* pw - Last of all, attach the parasites (couldn't do it earlier -
 
499
     there was no image. */
 
500
 
 
501
  if (!preview)
 
502
    {
 
503
      if (comment_parasite)
 
504
        {
 
505
          gimp_image_parasite_attach (image_ID, comment_parasite);
 
506
          gimp_parasite_free (comment_parasite);
 
507
 
 
508
          comment_parasite = NULL;
 
509
        }
 
510
 
 
511
#ifdef HAVE_EXIF
 
512
 
 
513
      if (! GPOINTER_TO_INT (cinfo.client_data))
 
514
        jpeg_apply_exif_data_to_image (filename, image_ID);
 
515
 
 
516
#endif
 
517
    }
 
518
 
 
519
  return image_ID;
 
520
}
 
521
 
 
522
#ifdef HAVE_EXIF
 
523
 
 
524
typedef struct
 
525
{
 
526
  struct jpeg_source_mgr pub;   /* public fields */
 
527
 
 
528
  guchar  *buffer;
 
529
  gint     size;
 
530
  JOCTET   terminal[2];
 
531
} my_source_mgr;
 
532
 
 
533
typedef my_source_mgr * my_src_ptr;
 
534
 
 
535
static void
 
536
init_source (j_decompress_ptr cinfo)
 
537
{
 
538
}
 
539
 
 
540
 
 
541
static boolean
 
542
fill_input_buffer (j_decompress_ptr cinfo)
 
543
{
 
544
  my_src_ptr src = (my_src_ptr) cinfo->src;
 
545
 
 
546
  /* Since we have given all we have got already
 
547
   * we simply fake an end of file
 
548
   */
 
549
 
 
550
  src->pub.next_input_byte = src->terminal;
 
551
  src->pub.bytes_in_buffer = 2;
 
552
  src->terminal[0]         = (JOCTET) 0xFF;
 
553
  src->terminal[1]         = (JOCTET) JPEG_EOI;
 
554
 
 
555
  return TRUE;
 
556
}
 
557
 
 
558
static void
 
559
skip_input_data (j_decompress_ptr cinfo,
 
560
                 long             num_bytes)
 
561
{
 
562
  my_src_ptr src = (my_src_ptr) cinfo->src;
 
563
 
 
564
  src->pub.next_input_byte = src->pub.next_input_byte + num_bytes;
 
565
}
 
566
 
 
567
static void
 
568
term_source (j_decompress_ptr cinfo)
 
569
{
 
570
}
 
571
 
 
572
gint32
 
573
load_thumbnail_image (const gchar *filename,
 
574
                      gint        *width,
 
575
                      gint        *height)
 
576
{
 
577
  gint32 volatile  image_ID;
 
578
  ExifData        *exif_data;
 
579
  GimpPixelRgn     pixel_rgn;
 
580
  GimpDrawable    *drawable;
 
581
  gint32           layer_ID;
 
582
  struct jpeg_decompress_struct cinfo;
 
583
  struct my_error_mgr           jerr;
 
584
  guchar     *buf;
 
585
  guchar  * volatile padded_buf = NULL;
 
586
  guchar    **rowbuf;
 
587
  gint        image_type;
 
588
  gint        layer_type;
 
589
  gint        tile_height;
 
590
  gint        scanlines;
 
591
  gint        i, start, end;
 
592
  my_src_ptr  src;
 
593
  FILE       *infile;
 
594
 
 
595
  image_ID = -1;
 
596
  exif_data = jpeg_exif_data_new_from_file (filename, NULL);
 
597
 
 
598
  if (! ((exif_data) && (exif_data->data) && (exif_data->size > 0)))
 
599
    return -1;
 
600
 
 
601
  cinfo.err = jpeg_std_error (&jerr.pub);
 
602
  jerr.pub.error_exit = my_error_exit;
 
603
 
 
604
  cinfo.client_data = GINT_TO_POINTER (FALSE);
 
605
 
 
606
  jerr.pub.emit_message   = my_emit_message;
 
607
  jerr.pub.output_message = my_output_message;
 
608
 
 
609
  gimp_progress_init_printf (_("Opening thumbnail for '%s'"),
 
610
                             gimp_filename_to_utf8 (filename));
 
611
 
 
612
  /* Establish the setjmp return context for my_error_exit to use. */
 
613
  if (setjmp (jerr.setjmp_buffer))
 
614
    {
 
615
      /* If we get here, the JPEG code has signaled an error.  We
 
616
       * need to clean up the JPEG object, close the input file,
 
617
       * and return.
 
618
       */
 
619
      jpeg_destroy_decompress (&cinfo);
 
620
 
 
621
      if (image_ID != -1)
 
622
        gimp_image_delete (image_ID);
 
623
 
 
624
      if (exif_data)
 
625
        {
 
626
          exif_data_unref (exif_data);
 
627
          exif_data = NULL;
 
628
        }
 
629
 
 
630
      return -1;
 
631
    }
 
632
 
 
633
  /* Now we can initialize the JPEG decompression object. */
 
634
  jpeg_create_decompress (&cinfo);
 
635
 
 
636
  /* Step 2: specify data source (eg, a file) */
 
637
 
 
638
  if (cinfo.src == NULL)
 
639
    cinfo.src = (struct jpeg_source_mgr *)(*cinfo.mem->alloc_small)
 
640
      ((j_common_ptr) &cinfo, JPOOL_PERMANENT,
 
641
       sizeof (my_source_mgr));
 
642
 
 
643
  src = (my_src_ptr) cinfo.src;
 
644
 
 
645
  src->pub.init_source       = init_source;
 
646
  src->pub.fill_input_buffer = fill_input_buffer;
 
647
  src->pub.skip_input_data   = skip_input_data;
 
648
  src->pub.resync_to_restart = jpeg_resync_to_restart;
 
649
  src->pub.term_source       = term_source;
 
650
 
 
651
  src->pub.bytes_in_buffer   = exif_data->size;
 
652
  src->pub.next_input_byte   = exif_data->data;
 
653
 
 
654
  src->buffer = exif_data->data;
 
655
  src->size = exif_data->size;
 
656
 
 
657
  /* Step 3: read file parameters with jpeg_read_header() */
 
658
 
 
659
  jpeg_read_header (&cinfo, TRUE);
 
660
 
 
661
  /* Step 4: set parameters for decompression */
 
662
 
 
663
  /* In this example, we don't need to change any of the defaults set by
 
664
   * jpeg_read_header(), so we do nothing here.
 
665
   */
 
666
 
 
667
  /* Step 5: Start decompressor */
 
668
 
 
669
  jpeg_start_decompress (&cinfo);
 
670
 
 
671
  /* We may need to do some setup of our own at this point before
 
672
   * reading the data.  After jpeg_start_decompress() we have the
 
673
   * correct scaled output image dimensions available, as well as
 
674
   * the output colormap if we asked for color quantization.  In
 
675
   * this example, we need to make an output work buffer of the
 
676
   * right size.
 
677
   */
 
678
 
 
679
  /* temporary buffer */
 
680
  tile_height = gimp_tile_height ();
 
681
  buf = g_new (guchar,
 
682
               tile_height * cinfo.output_width * cinfo.output_components);
 
683
 
 
684
  rowbuf = g_new (guchar *, tile_height);
 
685
 
 
686
  for (i = 0; i < tile_height; i++)
 
687
    rowbuf[i] = buf + cinfo.output_width * cinfo.output_components * i;
 
688
 
 
689
  /* Create a new image of the proper size and associate the
 
690
   * filename with it.
 
691
   *
 
692
   * Preview layers, not being on the bottom of a layer stack,
 
693
   * MUST HAVE AN ALPHA CHANNEL!
 
694
   */
 
695
  switch (cinfo.output_components)
 
696
    {
 
697
    case 1:
 
698
      image_type = GIMP_GRAY;
 
699
      layer_type = GIMP_GRAY_IMAGE;
 
700
      break;
 
701
 
 
702
    case 3:
 
703
      image_type = GIMP_RGB;
 
704
      layer_type = GIMP_RGB_IMAGE;
 
705
      break;
 
706
 
 
707
    case 4:
 
708
      if (cinfo.out_color_space == JCS_CMYK)
 
709
        {
 
710
          image_type = GIMP_RGB;
 
711
          layer_type = GIMP_RGB_IMAGE;
 
712
          break;
 
713
        }
 
714
      /*fallthrough*/
 
715
 
 
716
    default:
 
717
      g_message ("Don't know how to load JPEG images "
 
718
                 "with %d color channels, using colorspace %d (%d).",
 
719
                 cinfo.output_components, cinfo.out_color_space,
 
720
                 cinfo.jpeg_color_space);
 
721
 
 
722
      if (exif_data)
 
723
        {
 
724
          exif_data_unref (exif_data);
 
725
          exif_data = NULL;
 
726
        }
 
727
 
 
728
      return -1;
 
729
      break;
 
730
    }
 
731
 
 
732
  if (cinfo.out_color_space == JCS_CMYK)
 
733
    padded_buf = g_new (guchar, tile_height * cinfo.output_width * 3);
 
734
  else
 
735
    padded_buf = NULL;
 
736
 
 
737
  image_ID = gimp_image_new (cinfo.output_width, cinfo.output_height,
 
738
                             image_type);
 
739
  gimp_image_set_filename (image_ID, filename);
 
740
 
 
741
  layer_ID = gimp_layer_new (image_ID, _("Background"),
 
742
                             cinfo.output_width,
 
743
                             cinfo.output_height,
 
744
                             layer_type, 100, GIMP_NORMAL_MODE);
 
745
 
 
746
  drawable_global = drawable = gimp_drawable_get (layer_ID);
 
747
  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
 
748
                       drawable->width, drawable->height, TRUE, FALSE);
 
749
 
 
750
  /* Step 5.1: if the file had resolution information, set it on the image */
 
751
  if (cinfo.saw_JFIF_marker)
 
752
    {
 
753
      gdouble xresolution;
 
754
      gdouble yresolution;
 
755
      gdouble asymmetry;
 
756
 
 
757
      xresolution = cinfo.X_density;
 
758
      yresolution = cinfo.Y_density;
 
759
 
 
760
      switch (cinfo.density_unit)
 
761
        {
 
762
        case 0: /* unknown -> set the aspect ratio but use the default
 
763
                 *  image resolution
 
764
                 */
 
765
          if (cinfo.Y_density != 0)
 
766
            asymmetry = xresolution / yresolution;
 
767
          else
 
768
            asymmetry = 1.0;
 
769
 
 
770
          gimp_image_get_resolution (image_ID, &xresolution, &yresolution);
 
771
          xresolution *= asymmetry;
 
772
          break;
 
773
 
 
774
        case 1: /* dots per inch */
 
775
          break;
 
776
 
 
777
        case 2: /* dots per cm */
 
778
          xresolution *= 2.54;
 
779
          yresolution *= 2.54;
 
780
          gimp_image_set_unit (image_ID, GIMP_UNIT_MM);
 
781
          break;
 
782
 
 
783
        default:
 
784
          g_message ("Unknown density unit %d, assuming dots per inch.",
 
785
                     cinfo.density_unit);
 
786
          break;
 
787
        }
 
788
 
 
789
      gimp_image_set_resolution (image_ID, xresolution, yresolution);
 
790
    }
 
791
 
 
792
  /* Step 6: while (scan lines remain to be read) */
 
793
  /*           jpeg_read_scanlines(...); */
 
794
 
 
795
  /* Here we use the library's state variable cinfo.output_scanline as the
 
796
   * loop counter, so that we don't have to keep track ourselves.
 
797
   */
 
798
  while (cinfo.output_scanline < cinfo.output_height)
 
799
    {
 
800
      start = cinfo.output_scanline;
 
801
      end   = cinfo.output_scanline + tile_height;
 
802
      end   = MIN (end, cinfo.output_height);
 
803
      scanlines = end - start;
 
804
 
 
805
      for (i = 0; i < scanlines; i++)
 
806
        jpeg_read_scanlines (&cinfo, (JSAMPARRAY) &rowbuf[i], 1);
 
807
 
 
808
      if (cinfo.out_color_space == JCS_CMYK) /* buf-> RGB in padded_buf */
 
809
        {
 
810
          guchar *dest = padded_buf;
 
811
          guchar *src  = buf;
 
812
          gint    num  = drawable->width * scanlines;
 
813
 
 
814
          for (i = 0; i < num; i++)
 
815
            {
 
816
              guint r_c, g_m, b_y, a_k;
 
817
 
 
818
              r_c = *(src++);
 
819
              g_m = *(src++);
 
820
              b_y = *(src++);
 
821
              a_k = *(src++);
 
822
              *(dest++) = (r_c * a_k) / 255;
 
823
              *(dest++) = (g_m * a_k) / 255;
 
824
              *(dest++) = (b_y * a_k) / 255;
 
825
            }
 
826
        }
 
827
 
 
828
      gimp_pixel_rgn_set_rect (&pixel_rgn, padded_buf ? padded_buf : buf,
 
829
                               0, start, drawable->width, scanlines);
 
830
 
 
831
      gimp_progress_update ((gdouble) cinfo.output_scanline /
 
832
                            (gdouble) cinfo.output_height);
 
833
    }
 
834
 
 
835
  /* Step 7: Finish decompression */
 
836
 
 
837
  jpeg_finish_decompress (&cinfo);
 
838
  /* We can ignore the return value since suspension is not possible
 
839
   * with the stdio data source.
 
840
   */
 
841
 
 
842
  /* Step 8: Release JPEG decompression object */
 
843
 
 
844
  /* This is an important step since it will release a good deal
 
845
   * of memory.
 
846
   */
 
847
  jpeg_destroy_decompress (&cinfo);
 
848
 
 
849
  /* free up the temporary buffers */
 
850
  g_free (rowbuf);
 
851
  g_free (buf);
 
852
  g_free (padded_buf);
 
853
 
 
854
  /* At this point you may want to check to see whether any
 
855
   * corrupt-data warnings occurred (test whether
 
856
   * jerr.num_warnings is nonzero).
 
857
   */
 
858
  gimp_image_add_layer (image_ID, layer_ID, 0);
 
859
 
 
860
 
 
861
  /* NOW to get the dimensions of the actual image to return the
 
862
   * calling app
 
863
   */
 
864
  cinfo.err = jpeg_std_error (&jerr.pub);
 
865
  jerr.pub.error_exit = my_error_exit;
 
866
 
 
867
  cinfo.client_data = GINT_TO_POINTER (FALSE);
 
868
 
 
869
  jerr.pub.emit_message   = my_emit_message;
 
870
  jerr.pub.output_message = my_output_message;
 
871
 
 
872
  if ((infile = g_fopen (filename, "rb")) == NULL)
 
873
    {
 
874
      g_message (_("Could not open '%s' for reading: %s"),
 
875
                 gimp_filename_to_utf8 (filename), g_strerror (errno));
 
876
 
 
877
      if (exif_data)
 
878
        {
 
879
          exif_data_unref (exif_data);
 
880
          exif_data = NULL;
 
881
        }
 
882
 
 
883
      return -1;
 
884
    }
 
885
 
 
886
  /* Establish the setjmp return context for my_error_exit to use. */
 
887
  if (setjmp (jerr.setjmp_buffer))
 
888
    {
 
889
      /* If we get here, the JPEG code has signaled an error.  We
 
890
       * need to clean up the JPEG object, close the input file,
 
891
       * and return.
 
892
       */
 
893
      jpeg_destroy_decompress (&cinfo);
 
894
 
 
895
      if (image_ID != -1)
 
896
        gimp_image_delete (image_ID);
 
897
 
 
898
      if (exif_data)
 
899
        {
 
900
          exif_data_unref (exif_data);
 
901
          exif_data = NULL;
 
902
        }
 
903
 
 
904
      return -1;
 
905
    }
 
906
 
 
907
  /* Now we can initialize the JPEG decompression object. */
 
908
  jpeg_create_decompress (&cinfo);
 
909
 
 
910
  /* Step 2: specify data source (eg, a file) */
 
911
 
 
912
  jpeg_stdio_src (&cinfo, infile);
 
913
 
 
914
  /* Step 3: read file parameters with jpeg_read_header() */
 
915
 
 
916
  jpeg_read_header (&cinfo, TRUE);
 
917
 
 
918
  jpeg_start_decompress (&cinfo);
 
919
 
 
920
  *width  = cinfo.output_width;
 
921
  *height = cinfo.output_height;
 
922
 
 
923
  /* Step 4: Release JPEG decompression object */
 
924
 
 
925
  /* This is an important step since it will release a good deal
 
926
   * of memory.
 
927
   */
 
928
  jpeg_destroy_decompress (&cinfo);
 
929
 
 
930
  fclose (infile);
 
931
 
 
932
  if (exif_data)
 
933
    {
 
934
      exif_data_unref (exif_data);
 
935
      exif_data = NULL;
 
936
    }
 
937
 
 
938
  return image_ID;
 
939
}
 
940
 
 
941
#endif /* HAVE_EXIF */