1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
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.
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.
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.
25
#include <glib/gstdio.h>
31
#include <libexif/exif-data.h>
32
#endif /* HAVE_EXIF */
34
#include <libgimp/gimp.h>
35
#include <libgimp/gimpui.h>
37
#include "libgimp/stdplugins-intl.h"
41
#include "jpeg-load.h"
43
gint32 volatile image_ID_global;
44
GimpDrawable *drawable_global;
45
gint32 layer_ID_global;
48
load_image (const gchar *filename,
52
GimpPixelRgn pixel_rgn;
53
GimpDrawable *drawable;
54
gint32 volatile image_ID;
56
struct jpeg_decompress_struct cinfo;
57
struct my_error_mgr jerr;
60
guchar * volatile padded_buf = NULL;
69
GString *local_image_comments = NULL;
70
GimpParasite * volatile comment_parasite = NULL;
71
jpeg_saved_marker_ptr marker;
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;
77
/* flag warnings, so we try to ignore corrupt EXIF data */
80
cinfo.client_data = GINT_TO_POINTER (FALSE);
82
jerr.pub.emit_message = my_emit_message;
83
jerr.pub.output_message = my_output_message;
86
if ((infile = g_fopen (filename, "rb")) == NULL)
88
g_message (_("Could not open '%s' for reading: %s"),
89
gimp_filename_to_utf8 (filename), g_strerror (errno));
94
gimp_progress_init_printf (_("Opening '%s'"),
95
gimp_filename_to_utf8 (filename));
99
/* Establish the setjmp return context for my_error_exit to use. */
100
if (setjmp (jerr.setjmp_buffer))
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.
105
jpeg_destroy_decompress (&cinfo);
109
if (image_ID != -1 && !preview)
110
gimp_image_delete (image_ID);
118
/* Now we can initialize the JPEG decompression object. */
119
jpeg_create_decompress (&cinfo);
121
/* Step 2: specify data source (eg, a file) */
123
jpeg_stdio_src (&cinfo, infile);
127
/* - step 2.1: tell the lib to save the comments */
128
jpeg_save_markers (&cinfo, JPEG_COM, 0xffff);
130
/* - step 2.2: tell the lib to save APP1 markers
131
* (may contain EXIF or XMP)
133
jpeg_save_markers (&cinfo, JPEG_APP0 + 1, 0xffff);
135
/* - step 2.3: tell the lib to keep any APP2 data it may find */
136
jpeg_icc_setup_read_profile (&cinfo);
139
/* Step 3: read file parameters with jpeg_read_header() */
141
(void) jpeg_read_header (&cinfo, TRUE);
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.
149
/* Step 4: set parameters for decompression */
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.
155
/* Step 5: Start decompressor */
157
jpeg_start_decompress (&cinfo);
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.
165
/* temporary buffer */
166
tile_height = gimp_tile_height ();
168
tile_height * cinfo.output_width * cinfo.output_components);
170
rowbuf = g_new (guchar *, tile_height);
172
for (i = 0; i < tile_height; i++)
173
rowbuf[i] = buf + cinfo.output_width * cinfo.output_components * i;
175
/* Create a new image of the proper size and associate the filename with it.
177
Preview layers, not being on the bottom of a layer stack, MUST HAVE
180
switch (cinfo.output_components)
183
image_type = GIMP_GRAY;
184
layer_type = preview ? GIMP_GRAYA_IMAGE : GIMP_GRAY_IMAGE;
188
image_type = GIMP_RGB;
189
layer_type = preview ? GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE;
193
if (cinfo.out_color_space == JCS_CMYK)
195
image_type = GIMP_RGB;
196
layer_type = GIMP_RGB_IMAGE;
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);
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);
220
image_ID = image_ID_global;
224
image_ID = gimp_image_new (cinfo.output_width, cinfo.output_height,
226
gimp_image_set_filename (image_ID, filename);
231
layer_ID_global = layer_ID =
232
gimp_layer_new (image_ID, _("JPEG preview"),
235
layer_type, 100, GIMP_NORMAL_MODE);
239
layer_ID = gimp_layer_new (image_ID, _("Background"),
242
layer_type, 100, GIMP_NORMAL_MODE);
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);
249
/* Step 5.1: if the file had resolution information, set it on the image */
250
if (!preview && cinfo.saw_JFIF_marker)
256
xresolution = cinfo.X_density;
257
yresolution = cinfo.Y_density;
259
switch (cinfo.density_unit)
261
case 0: /* unknown -> set the aspect ratio but use the default
264
if (cinfo.Y_density != 0)
265
asymmetry = xresolution / yresolution;
269
gimp_image_get_resolution (image_ID, &xresolution, &yresolution);
270
xresolution *= asymmetry;
273
case 1: /* dots per inch */
276
case 2: /* dots per cm */
279
gimp_image_set_unit (image_ID, GIMP_UNIT_MM);
283
g_message ("Unknown density unit %d, assuming dots per inch.",
288
gimp_image_set_resolution (image_ID, xresolution, yresolution);
291
/* Step 5.2: check for metadata (comments, markers containing EXIF or XMP) */
292
for (marker = cinfo.marker_list; marker; marker = marker->next)
294
const gchar *data = (const gchar *) marker->data;
295
gsize len = marker->data_length;
297
if (marker->marker == JPEG_COM)
299
g_print ("jpeg-load: found image comment (%d bytes)\n",
300
marker->data_length);
302
if (!local_image_comments)
304
local_image_comments = g_string_new_len (data, len);
308
g_string_append_c (local_image_comments, '\n');
309
g_string_append_len (local_image_comments, data, len);
312
else if ((marker->marker == JPEG_APP0 + 1)
314
&& ! strcmp (JPEG_APP_HEADER_EXIF, data))
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 */
322
else if ((marker->marker == JPEG_APP0 + 1)
324
&& ! strcmp (JPEG_APP_HEADER_XMP, data))
326
GimpParam *return_vals;
330
g_print ("jpeg-load: found XMP packet (%d bytes)\n",
331
(gint) (len - sizeof (JPEG_APP_HEADER_XMP)));
333
xmp_packet = g_strndup (data + sizeof (JPEG_APP_HEADER_XMP),
334
len - sizeof (JPEG_APP_HEADER_XMP));
336
/* FIXME: running this through the PDB is not very efficient */
337
return_vals = gimp_run_procedure ("plug-in-metadata-decode-xmp",
339
GIMP_PDB_IMAGE, image_ID,
340
GIMP_PDB_STRING, xmp_packet,
343
if (return_vals[0].data.d_status != GIMP_PDB_SUCCESS)
345
g_warning ("JPEG - unable to decode XMP metadata packet");
348
gimp_destroy_params (return_vals, nreturn_vals);
353
/* Step 5.3: check for an embedded ICC profile */
354
if (!preview && jpeg_icc_read_profile (&cinfo, &profile, &profile_size))
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);
366
/* if we had any comments then make a parasite for them */
367
if (local_image_comments && local_image_comments->len)
369
gchar *comment = local_image_comments->str;
371
g_string_free (local_image_comments, FALSE);
373
local_image_comments = NULL;
375
if (g_utf8_validate (comment, -1, NULL))
376
comment_parasite = gimp_parasite_new ("gimp-comment",
377
GIMP_PARASITE_PERSISTENT,
378
strlen (comment) + 1,
383
/* Do not attach the "jpeg-save-options" parasite to the image
384
* because this conflicts with the global defaults (bug #75398).
388
/* Step 6: while (scan lines remain to be read) */
389
/* jpeg_read_scanlines(...); */
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.
394
while (cinfo.output_scanline < cinfo.output_height)
396
start = cinfo.output_scanline;
397
end = cinfo.output_scanline + tile_height;
398
end = MIN (end, cinfo.output_height);
399
scanlines = end - start;
401
for (i = 0; i < scanlines; i++)
402
jpeg_read_scanlines (&cinfo, (JSAMPARRAY) &rowbuf[i], 1);
404
if (preview) /* Add a dummy alpha channel -- convert buf to padded_buf */
406
guchar *dest = padded_buf;
408
gint num = drawable->width * scanlines;
410
switch (cinfo.output_components)
413
for (i = 0; i < num; i++)
415
*(dest++) = *(src++);
421
for (i = 0; i < num; i++)
423
*(dest++) = *(src++);
424
*(dest++) = *(src++);
425
*(dest++) = *(src++);
431
g_warning ("JPEG - shouldn't have gotten here.\n"
432
"Report to http://bugzilla.gnome.org/");
436
else if (cinfo.out_color_space == JCS_CMYK) /* buf-> RGB in padded_buf */
438
guchar *dest = padded_buf;
440
gint num = drawable->width * scanlines;
442
for (i = 0; i < num; i++)
444
guint r_c, g_m, b_y, a_k;
450
*(dest++) = (r_c * a_k) / 255;
451
*(dest++) = (g_m * a_k) / 255;
452
*(dest++) = (b_y * a_k) / 255;
456
gimp_pixel_rgn_set_rect (&pixel_rgn, padded_buf ? padded_buf : buf,
457
0, start, drawable->width, scanlines);
460
gimp_progress_update ((gdouble) cinfo.output_scanline /
461
(gdouble) cinfo.output_height);
464
/* Step 7: Finish decompression */
466
jpeg_finish_decompress (&cinfo);
467
/* We can ignore the return value since suspension is not possible
468
* with the stdio data source.
471
/* Step 8: Release JPEG decompression object */
473
/* This is an important step since it will release a good deal of memory. */
474
jpeg_destroy_decompress (&cinfo);
476
/* free up the temporary buffers */
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...)
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).
492
/* Detach from the drawable and add it to the image.
495
gimp_drawable_detach (drawable);
496
gimp_image_add_layer (image_ID, layer_ID, 0);
498
/* pw - Last of all, attach the parasites (couldn't do it earlier -
499
there was no image. */
503
if (comment_parasite)
505
gimp_image_parasite_attach (image_ID, comment_parasite);
506
gimp_parasite_free (comment_parasite);
508
comment_parasite = NULL;
513
if (! GPOINTER_TO_INT (cinfo.client_data))
514
jpeg_apply_exif_data_to_image (filename, image_ID);
526
struct jpeg_source_mgr pub; /* public fields */
533
typedef my_source_mgr * my_src_ptr;
536
init_source (j_decompress_ptr cinfo)
542
fill_input_buffer (j_decompress_ptr cinfo)
544
my_src_ptr src = (my_src_ptr) cinfo->src;
546
/* Since we have given all we have got already
547
* we simply fake an end of file
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;
559
skip_input_data (j_decompress_ptr cinfo,
562
my_src_ptr src = (my_src_ptr) cinfo->src;
564
src->pub.next_input_byte = src->pub.next_input_byte + num_bytes;
568
term_source (j_decompress_ptr cinfo)
573
load_thumbnail_image (const gchar *filename,
577
gint32 volatile image_ID;
579
GimpPixelRgn pixel_rgn;
580
GimpDrawable *drawable;
582
struct jpeg_decompress_struct cinfo;
583
struct my_error_mgr jerr;
585
guchar * volatile padded_buf = NULL;
596
exif_data = jpeg_exif_data_new_from_file (filename, NULL);
598
if (! ((exif_data) && (exif_data->data) && (exif_data->size > 0)))
601
cinfo.err = jpeg_std_error (&jerr.pub);
602
jerr.pub.error_exit = my_error_exit;
604
cinfo.client_data = GINT_TO_POINTER (FALSE);
606
jerr.pub.emit_message = my_emit_message;
607
jerr.pub.output_message = my_output_message;
609
gimp_progress_init_printf (_("Opening thumbnail for '%s'"),
610
gimp_filename_to_utf8 (filename));
612
/* Establish the setjmp return context for my_error_exit to use. */
613
if (setjmp (jerr.setjmp_buffer))
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,
619
jpeg_destroy_decompress (&cinfo);
622
gimp_image_delete (image_ID);
626
exif_data_unref (exif_data);
633
/* Now we can initialize the JPEG decompression object. */
634
jpeg_create_decompress (&cinfo);
636
/* Step 2: specify data source (eg, a file) */
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));
643
src = (my_src_ptr) cinfo.src;
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;
651
src->pub.bytes_in_buffer = exif_data->size;
652
src->pub.next_input_byte = exif_data->data;
654
src->buffer = exif_data->data;
655
src->size = exif_data->size;
657
/* Step 3: read file parameters with jpeg_read_header() */
659
jpeg_read_header (&cinfo, TRUE);
661
/* Step 4: set parameters for decompression */
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.
667
/* Step 5: Start decompressor */
669
jpeg_start_decompress (&cinfo);
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
679
/* temporary buffer */
680
tile_height = gimp_tile_height ();
682
tile_height * cinfo.output_width * cinfo.output_components);
684
rowbuf = g_new (guchar *, tile_height);
686
for (i = 0; i < tile_height; i++)
687
rowbuf[i] = buf + cinfo.output_width * cinfo.output_components * i;
689
/* Create a new image of the proper size and associate the
692
* Preview layers, not being on the bottom of a layer stack,
693
* MUST HAVE AN ALPHA CHANNEL!
695
switch (cinfo.output_components)
698
image_type = GIMP_GRAY;
699
layer_type = GIMP_GRAY_IMAGE;
703
image_type = GIMP_RGB;
704
layer_type = GIMP_RGB_IMAGE;
708
if (cinfo.out_color_space == JCS_CMYK)
710
image_type = GIMP_RGB;
711
layer_type = GIMP_RGB_IMAGE;
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);
724
exif_data_unref (exif_data);
732
if (cinfo.out_color_space == JCS_CMYK)
733
padded_buf = g_new (guchar, tile_height * cinfo.output_width * 3);
737
image_ID = gimp_image_new (cinfo.output_width, cinfo.output_height,
739
gimp_image_set_filename (image_ID, filename);
741
layer_ID = gimp_layer_new (image_ID, _("Background"),
744
layer_type, 100, GIMP_NORMAL_MODE);
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);
750
/* Step 5.1: if the file had resolution information, set it on the image */
751
if (cinfo.saw_JFIF_marker)
757
xresolution = cinfo.X_density;
758
yresolution = cinfo.Y_density;
760
switch (cinfo.density_unit)
762
case 0: /* unknown -> set the aspect ratio but use the default
765
if (cinfo.Y_density != 0)
766
asymmetry = xresolution / yresolution;
770
gimp_image_get_resolution (image_ID, &xresolution, &yresolution);
771
xresolution *= asymmetry;
774
case 1: /* dots per inch */
777
case 2: /* dots per cm */
780
gimp_image_set_unit (image_ID, GIMP_UNIT_MM);
784
g_message ("Unknown density unit %d, assuming dots per inch.",
789
gimp_image_set_resolution (image_ID, xresolution, yresolution);
792
/* Step 6: while (scan lines remain to be read) */
793
/* jpeg_read_scanlines(...); */
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.
798
while (cinfo.output_scanline < cinfo.output_height)
800
start = cinfo.output_scanline;
801
end = cinfo.output_scanline + tile_height;
802
end = MIN (end, cinfo.output_height);
803
scanlines = end - start;
805
for (i = 0; i < scanlines; i++)
806
jpeg_read_scanlines (&cinfo, (JSAMPARRAY) &rowbuf[i], 1);
808
if (cinfo.out_color_space == JCS_CMYK) /* buf-> RGB in padded_buf */
810
guchar *dest = padded_buf;
812
gint num = drawable->width * scanlines;
814
for (i = 0; i < num; i++)
816
guint r_c, g_m, b_y, a_k;
822
*(dest++) = (r_c * a_k) / 255;
823
*(dest++) = (g_m * a_k) / 255;
824
*(dest++) = (b_y * a_k) / 255;
828
gimp_pixel_rgn_set_rect (&pixel_rgn, padded_buf ? padded_buf : buf,
829
0, start, drawable->width, scanlines);
831
gimp_progress_update ((gdouble) cinfo.output_scanline /
832
(gdouble) cinfo.output_height);
835
/* Step 7: Finish decompression */
837
jpeg_finish_decompress (&cinfo);
838
/* We can ignore the return value since suspension is not possible
839
* with the stdio data source.
842
/* Step 8: Release JPEG decompression object */
844
/* This is an important step since it will release a good deal
847
jpeg_destroy_decompress (&cinfo);
849
/* free up the temporary buffers */
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).
858
gimp_image_add_layer (image_ID, layer_ID, 0);
861
/* NOW to get the dimensions of the actual image to return the
864
cinfo.err = jpeg_std_error (&jerr.pub);
865
jerr.pub.error_exit = my_error_exit;
867
cinfo.client_data = GINT_TO_POINTER (FALSE);
869
jerr.pub.emit_message = my_emit_message;
870
jerr.pub.output_message = my_output_message;
872
if ((infile = g_fopen (filename, "rb")) == NULL)
874
g_message (_("Could not open '%s' for reading: %s"),
875
gimp_filename_to_utf8 (filename), g_strerror (errno));
879
exif_data_unref (exif_data);
886
/* Establish the setjmp return context for my_error_exit to use. */
887
if (setjmp (jerr.setjmp_buffer))
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,
893
jpeg_destroy_decompress (&cinfo);
896
gimp_image_delete (image_ID);
900
exif_data_unref (exif_data);
907
/* Now we can initialize the JPEG decompression object. */
908
jpeg_create_decompress (&cinfo);
910
/* Step 2: specify data source (eg, a file) */
912
jpeg_stdio_src (&cinfo, infile);
914
/* Step 3: read file parameters with jpeg_read_header() */
916
jpeg_read_header (&cinfo, TRUE);
918
jpeg_start_decompress (&cinfo);
920
*width = cinfo.output_width;
921
*height = cinfo.output_height;
923
/* Step 4: Release JPEG decompression object */
925
/* This is an important step since it will release a good deal
928
jpeg_destroy_decompress (&cinfo);
934
exif_data_unref (exif_data);
941
#endif /* HAVE_EXIF */