1 /* tiff loading for GIMP
2 * -Peter Mattis
3 *
4 * The TIFF loading code has been completely revamped by Nick Lamb
5 * njl195@zepler.org.uk -- 18 May 1998
6 * And it now gains support for tiles (and doubtless a zillion bugs)
7 * njl195@zepler.org.uk -- 12 June 1999
8 * LZW patent fuss continues :(
9 * njl195@zepler.org.uk -- 20 April 2000
10 * The code for this filter is based on "tifftopnm" and "pnmtotiff",
11 * 2 programs that are a part of the netpbm package.
12 * khk@khk.net -- 13 May 2000
13 * Added support for ICCPROFILE tiff tag. If this tag is present in a
14 * TIFF file, then a parasite is created and vice versa.
15 * peter@kirchgessner.net -- 29 Oct 2002
16 * Progress bar only when run interactive
17 * Added support for layer offsets - pablo.dangelo@web.de -- 7 Jan 2004
18 * Honor EXTRASAMPLES tag while loading images with alphachannel
19 * pablo.dangelo@web.de -- 16 Jan 2004
20 */
21
22 /*
23 * tifftopnm.c - converts a Tagged Image File to a portable anymap
24 *
25 * Derived by Jef Poskanzer from tif2ras.c, which is:
26 *
27 * Copyright (c) 1990 by Sun Microsystems, Inc.
28 *
29 * Author: Patrick J. Naughton
30 * naughton@wind.sun.com
31 *
32 * Permission to use, copy, modify, and distribute this software and its
33 * documentation for any purpose and without fee is hereby granted,
34 * provided that the above copyright notice appear in all copies and that
35 * both that copyright notice and this permission notice appear in
36 * supporting documentation.
37 *
38 * This file is provided AS IS with no warranties of any kind. The author
39 * shall have no liability with respect to the infringement of copyrights,
40 * trade secrets or any patents by this file or any part thereof. In no
41 * event will the author be liable for any lost revenue or profits or
42 * other special, indirect and consequential damages.
43 */
44
45 #include "config.h"
46
47 #include <errno.h>
48 #include <string.h>
49
50 #include <tiffio.h>
51
52 #include <libgimp/gimp.h>
53 #include <libgimp/gimpui.h>
54
55 #include "file-tiff-io.h"
56 #include "file-tiff-load.h"
57
58 #include "libgimp/stdplugins-intl.h"
59
60
61 #define PLUG_IN_ROLE "gimp-file-tiff-load"
62
63
64 typedef struct
65 {
66 gint compression;
67 gint fillorder;
68 gboolean save_transp_pixels;
69 } TiffSaveVals;
70
71 typedef struct
72 {
73 gint32 ID;
74 GeglBuffer *buffer;
75 const Babl *format;
76 guchar *pixels;
77 guchar *pixel;
78 } ChannelData;
79
80 typedef enum
81 {
82 GIMP_TIFF_LOAD_ASSOCALPHA,
83 GIMP_TIFF_LOAD_UNASSALPHA,
84 GIMP_TIFF_LOAD_CHANNEL
85 } DefaultExtra;
86
87 typedef enum
88 {
89 GIMP_TIFF_DEFAULT,
90 GIMP_TIFF_INDEXED,
91 GIMP_TIFF_GRAY,
92 GIMP_TIFF_GRAY_MINISWHITE,
93 } TiffColorMode;
94
95 /* Declare some local functions */
96
97 static GimpColorProfile * load_profile (TIFF *tif);
98
99 static void load_rgba (TIFF *tif,
100 ChannelData *channel);
101 static void load_contiguous (TIFF *tif,
102 ChannelData *channel,
103 const Babl *type,
104 gushort bps,
105 gushort spp,
106 TiffColorMode tiff_mode,
107 gboolean is_signed,
108 gint extra);
109 static void load_separate (TIFF *tif,
110 ChannelData *channel,
111 const Babl *type,
112 gushort bps,
113 gushort spp,
114 TiffColorMode tiff_mode,
115 gboolean is_signed,
116 gint extra);
117 static void load_paths (TIFF *tif,
118 gint image,
119 gint width,
120 gint height,
121 gint offset_x,
122 gint offset_y);
123
124 static gboolean is_non_conformant_tiff (gushort photomet,
125 gushort spp);
126 static gushort get_extra_channels_count (gushort photomet,
127 gushort spp,
128 gboolean alpha);
129
130 static void fill_bit2byte (TiffColorMode tiff_mode);
131 static void fill_2bit2byte (TiffColorMode tiff_mode);
132 static void fill_4bit2byte (TiffColorMode tiff_mode);
133 static void convert_bit2byte (const guchar *src,
134 guchar *dest,
135 gint width,
136 gint height);
137 static void convert_2bit2byte (const guchar *src,
138 guchar *dest,
139 gint width,
140 gint height);
141 static void convert_4bit2byte (const guchar *src,
142 guchar *dest,
143 gint width,
144 gint height);
145
146 static void convert_miniswhite (guchar *buffer,
147 gint width,
148 gint height);
149 static void convert_int2uint (guchar *buffer,
150 gint bps,
151 gint spp,
152 gint width,
153 gint height,
154 gint stride);
155
156 static gboolean load_dialog (TIFF *tif,
157 const gchar *help_id,
158 TiffSelectedPages *pages,
159 const gchar *extra_message,
160 DefaultExtra *default_extra);
161
162
163 static TiffSaveVals tsvals =
164 {
165 COMPRESSION_NONE, /* compression */
166 TRUE, /* alpha handling */
167 };
168
169
170 /* returns a pointer into the TIFF */
171 static const gchar *
tiff_get_page_name(TIFF * tif)172 tiff_get_page_name (TIFF *tif)
173 {
174 static gchar *name;
175
176 if (TIFFGetField (tif, TIFFTAG_PAGENAME, &name) &&
177 g_utf8_validate (name, -1, NULL))
178 {
179 return name;
180 }
181
182 return NULL;
183 }
184
185 /* is_non_conformant_tiff assumes TIFFTAG_EXTRASAMPLES was not set */
186 static gboolean
is_non_conformant_tiff(gushort photomet,gushort spp)187 is_non_conformant_tiff (gushort photomet, gushort spp)
188 {
189 switch (photomet)
190 {
191 case PHOTOMETRIC_RGB:
192 case PHOTOMETRIC_YCBCR:
193 case PHOTOMETRIC_CIELAB:
194 case PHOTOMETRIC_ICCLAB:
195 case PHOTOMETRIC_ITULAB:
196 case PHOTOMETRIC_LOGLUV:
197 return (spp > 3 || (spp == 2 && photomet != PHOTOMETRIC_RGB));
198 break;
199 case PHOTOMETRIC_SEPARATED:
200 return (spp > 4);
201 break;
202 default:
203 return (spp > 1);
204 break;
205 }
206 }
207
208 /* get_extra_channels_count returns number of channels excluding
209 * alpha and color channels
210 */
211 static gushort
get_extra_channels_count(gushort photomet,gushort spp,gboolean alpha)212 get_extra_channels_count (gushort photomet, gushort spp, gboolean alpha)
213 {
214 switch (photomet)
215 {
216 case PHOTOMETRIC_RGB:
217 case PHOTOMETRIC_YCBCR:
218 case PHOTOMETRIC_CIELAB:
219 case PHOTOMETRIC_ICCLAB:
220 case PHOTOMETRIC_ITULAB:
221 case PHOTOMETRIC_LOGLUV:
222 if (spp >= 3)
223 return spp - 3 - (alpha? 1 : 0);
224 else
225 return spp - 1 - (alpha? 1 : 0);
226 break;
227 case PHOTOMETRIC_SEPARATED:
228 return spp - 4 - (alpha? 1 : 0);
229 break;
230 default:
231 return spp - 1 - (alpha? 1 : 0);
232 break;
233 }
234 }
235
236 GimpPDBStatusType
load_image(GFile * file,GimpRunMode run_mode,gint32 * image,gboolean * resolution_loaded,gboolean * profile_loaded,GError ** error)237 load_image (GFile *file,
238 GimpRunMode run_mode,
239 gint32 *image,
240 gboolean *resolution_loaded,
241 gboolean *profile_loaded,
242 GError **error)
243 {
244 TIFF *tif;
245 TiffSelectedPages pages;
246
247 GList *images_list = NULL;
248 DefaultExtra default_extra = GIMP_TIFF_LOAD_UNASSALPHA;
249 gint first_image_type = GIMP_RGB;
250 gint min_row = G_MAXINT;
251 gint min_col = G_MAXINT;
252 gint max_row = 0;
253 gint max_col = 0;
254 GimpColorProfile *first_profile = NULL;
255 const gchar *extra_message = NULL;
256 gint li;
257
258 *image = 0;
259 gimp_progress_init_printf (_("Opening '%s'"),
260 gimp_file_get_utf8_name (file));
261
262 tif = tiff_open (file, "r", error);
263 if (! tif)
264 {
265 if (! (error && *error))
266 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
267 _("Not a TIFF image or image is corrupt."));
268
269 return GIMP_PDB_EXECUTION_ERROR;
270 }
271
272 pages.target = GIMP_PAGE_SELECTOR_TARGET_LAYERS;
273 gimp_get_data (LOAD_PROC "-target", &pages.target);
274
275 pages.keep_empty_space = TRUE;
276 gimp_get_data (LOAD_PROC "-keep-empty-space",
277 &pages.keep_empty_space);
278
279 pages.n_pages = pages.o_pages = TIFFNumberOfDirectories (tif);
280 if (pages.n_pages == 0)
281 {
282 /* See #5837.
283 * It seems we might be able to rescue some data even though the
284 * TIFF is possibly syntactically wrong.
285 */
286
287 /* libtiff says max number of directory is 65535. */
288 for (li = 0; li < 65536; li++)
289 {
290 if (TIFFSetDirectory (tif, li) == 0)
291 break;
292 }
293 pages.n_pages = li;
294 if (pages.n_pages == 0)
295 {
296 TIFFClose (tif);
297 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
298 _("TIFF '%s' does not contain any directories"),
299 gimp_file_get_utf8_name (file));
300
301 return GIMP_PDB_EXECUTION_ERROR;
302 }
303
304 TIFFSetDirectory (tif, 0);
305 g_message (ngettext ("TIFF '%s' directory count by header failed "
306 "though there seems to be %d page."
307 " Attempting to load the file with this assumption.",
308 "TIFF '%s' directory count by header failed "
309 "though there seem to be %d pages."
310 " Attempting to load the file with this assumption.",
311 pages.n_pages),
312 gimp_file_get_utf8_name (file), pages.n_pages);
313 }
314
315 pages.pages = NULL;
316 pages.n_filtered_pages = pages.n_pages;
317
318 pages.filtered_pages = g_new0 (gint, pages.n_pages);
319 for (li = 0; li < pages.n_pages; li++)
320 pages.filtered_pages[li] = li;
321
322 if (pages.n_pages == 1)
323 {
324 pages.pages = g_new0 (gint, pages.n_pages);
325 pages.target = GIMP_PAGE_SELECTOR_TARGET_LAYERS;
326 }
327
328 /* Check all pages if any has an unspecified or unset channel. */
329 for (li = 0; li < pages.n_pages; li++)
330 {
331 gushort spp;
332 gushort photomet;
333 gushort extra;
334 gushort *extra_types;
335 gushort file_type = 0;
336 gboolean first_page_old_jpeg = FALSE;
337
338 if (TIFFSetDirectory (tif, li) == 0)
339 continue;
340
341 TIFFGetFieldDefaulted (tif, TIFFTAG_SAMPLESPERPIXEL, &spp);
342 if (! TIFFGetField (tif, TIFFTAG_PHOTOMETRIC, &photomet))
343 {
344 guint16 compression;
345
346 if (TIFFGetField (tif, TIFFTAG_COMPRESSION, &compression) &&
347 (compression == COMPRESSION_CCITTFAX3 ||
348 compression == COMPRESSION_CCITTFAX4 ||
349 compression == COMPRESSION_CCITTRLE ||
350 compression == COMPRESSION_CCITTRLEW))
351 {
352 photomet = PHOTOMETRIC_MINISWHITE;
353 }
354 else
355 {
356 /* old AppleScan software misses out the photometric tag
357 * (and incidentally assumes min-is-white, but xv
358 * assumes min-is-black, so we follow xv's lead. It's
359 * not much hardship to invert the image later).
360 */
361 photomet = PHOTOMETRIC_MINISBLACK;
362 }
363 }
364 if (! TIFFGetField (tif, TIFFTAG_EXTRASAMPLES, &extra, &extra_types))
365 extra = 0;
366
367 /* Try to detect if a TIFF page is a thumbnail.
368 * Easy case: if subfiletype is set to FILETYPE_REDUCEDIMAGE.
369 * If no subfiletype is defined we try to detect it ourselves.
370 * We will consider it a thumbnail if:
371 * - It's the second page
372 * - PhotometricInterpretation is YCbCr
373 * - Compression is old style jpeg
374 * - First page uses a different compression or PhotometricInterpretation
375 *
376 * We could also add a check for the presence of TIFFTAG_EXIFIFD since
377 * this should usually be a thumbnail part of EXIF metadata. Since that
378 * probably won't make a difference, I will leave that out for now.
379 */
380 if (li == 0)
381 {
382 guint16 compression;
383
384 if (TIFFGetField (tif, TIFFTAG_COMPRESSION, &compression) &&
385 compression == COMPRESSION_OJPEG &&
386 photomet == PHOTOMETRIC_YCBCR)
387 first_page_old_jpeg = TRUE;
388 }
389
390 if (TIFFGetField (tif, TIFFTAG_SUBFILETYPE, &file_type))
391 {
392 if (file_type == FILETYPE_REDUCEDIMAGE)
393 {
394 /* file_type is a mask but we will only filter out pages
395 * that only have FILETYPE_REDUCEDIMAGE set */
396 pages.filtered_pages[li] = -1;
397 pages.n_filtered_pages--;
398 g_debug ("Page %d is a FILETYPE_REDUCEDIMAGE thumbnail.\n", li);
399 }
400 }
401 else
402 {
403 if (li == 1 && photomet == PHOTOMETRIC_YCBCR &&
404 ! first_page_old_jpeg)
405 {
406 guint16 compression;
407
408 if (TIFFGetField (tif, TIFFTAG_COMPRESSION, &compression) &&
409 compression == COMPRESSION_OJPEG)
410 {
411 pages.filtered_pages[li] = -1;
412 pages.n_filtered_pages--;
413 g_debug ("Page %d is most likely a thumbnail.\n", li);
414 }
415 }
416 }
417
418 /* TODO: current code always assumes that the alpha channel
419 * will be the first extra channel, though the TIFF spec does
420 * not mandate such assumption. A future improvement should be
421 * to actually loop through the extra channels and save the
422 * alpha channel index.
423 * Of course, this is an edge case, as most image would likely
424 * have only a single extra channel anyway. But still we could
425 * be more accurate.
426 */
427 if (extra > 0 && (extra_types[0] == EXTRASAMPLE_UNSPECIFIED))
428 {
429 extra_message = _("Extra channels with unspecified data.");
430 break;
431 }
432 else if (extra == 0 && is_non_conformant_tiff (photomet, spp))
433 {
434 /* ExtraSamples field not set, yet we have more channels than
435 * the PhotometricInterpretation field suggests.
436 * This should not happen as the spec clearly says "This field
437 * must be present if there are extra samples". So the files
438 * can be considered non-conformant.
439 * Let's ask what to do with the channel.
440 */
441 extra_message = _("Non-conformant TIFF: extra channels without 'ExtraSamples' field.");
442 }
443 }
444 TIFFSetDirectory (tif, 0);
445
446 if (run_mode == GIMP_RUN_INTERACTIVE &&
447 (pages.n_pages > 1 || extra_message) &&
448 ! load_dialog (tif, LOAD_PROC, &pages,
449 extra_message, &default_extra))
450 {
451 TIFFClose (tif);
452 g_clear_pointer (&pages.pages, g_free);
453
454 return GIMP_PDB_CANCEL;
455 }
456 /* Adjust pages to take filtered out pages into account. */
457 if (pages.o_pages > pages.n_filtered_pages)
458 {
459 gint fi;
460 gint sel_index = 0;
461 gint sel_add = 0;
462
463 for (fi = 0; fi < pages.o_pages && sel_index < pages.n_pages; fi++)
464 {
465 if (pages.filtered_pages[fi] == -1)
466 {
467 sel_add++;
468 }
469 if (pages.pages[sel_index] + sel_add == fi)
470 {
471 pages.pages[sel_index] = fi;
472 sel_index++;
473 }
474 }
475 }
476
477 gimp_set_data (LOAD_PROC "-target",
478 &pages.target, sizeof (pages.target));
479 gimp_set_data (LOAD_PROC "-keep-empty-space",
480 &pages.keep_empty_space,
481 sizeof (pages.keep_empty_space));
482
483 /* We will loop through the all pages in case of multipage TIFF
484 * and load every page as a separate layer.
485 */
486 for (li = 0; li < pages.n_pages; li++)
487 {
488 gint ilayer;
489 gushort bps;
490 gushort spp;
491 gushort photomet;
492 gshort sampleformat;
493 GimpColorProfile *profile;
494 gboolean profile_linear = FALSE;
495 GimpPrecision image_precision;
496 const Babl *type;
497 const Babl *base_format = NULL;
498 guint16 orientation;
499 gint cols;
500 gint rows;
501 gboolean alpha;
502 gint image_type = GIMP_RGB;
503 gint layer;
504 gint layer_type = GIMP_RGB_IMAGE;
505 float layer_offset_x = 0.0;
506 float layer_offset_y = 0.0;
507 gint layer_offset_x_pixel = 0;
508 gint layer_offset_y_pixel = 0;
509 gushort extra;
510 gushort *extra_types;
511 ChannelData *channel = NULL;
512 uint16 planar = PLANARCONFIG_CONTIG;
513 TiffColorMode tiff_mode;
514 gboolean is_signed;
515 gint i;
516 gboolean worst_case = FALSE;
517 TiffSaveVals save_vals;
518 const gchar *name;
519
520 if (TIFFSetDirectory (tif, pages.pages[li]) == 0)
521 {
522 g_message (_("Couldn't read page %d of %d. Image might be corrupt.\n"),
523 li+1, pages.n_pages);
524 continue;
525 }
526 ilayer = pages.pages[li];
527
528 gimp_progress_update (0.0);
529
530 TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE, &bps);
531
532 TIFFGetFieldDefaulted (tif, TIFFTAG_SAMPLEFORMAT, &sampleformat);
533
534 profile = load_profile (tif);
535 if (! profile && first_profile)
536 {
537 profile = first_profile;
538 g_object_ref (profile);
539 }
540
541 if (profile)
542 {
543 profile_linear = gimp_color_profile_is_linear (profile);
544
545 if (! first_profile)
546 {
547 first_profile = profile;
548 g_object_ref (first_profile);
549
550 if (profile_linear && li > 0 && pages.target != GIMP_PAGE_SELECTOR_TARGET_IMAGES)
551 g_message (_("This image has a linear color profile but "
552 "it was not set on the first layer. "
553 "The layers below layer # %d will be "
554 "interpreted as non linear."), li+1);
555 }
556 else if (pages.target != GIMP_PAGE_SELECTOR_TARGET_IMAGES &&
557 ! gimp_color_profile_is_equal (first_profile, profile))
558 {
559 g_message (_("This image has multiple color profiles. "
560 "We will use the first one. If this leads "
561 "to incorrect results you should consider "
562 "loading each layer as a separate image."));
563 }
564
565 if (! *image)
566 *profile_loaded = TRUE;
567 }
568
569 if (bps > 64)
570 {
571 g_message (_("Suspicious bit depth: %d for page %d. Image may be corrupt."),
572 bps, li+1);
573 continue;
574 }
575
576 if (bps > 8 && bps != 8 && bps != 16 && bps != 32 && bps != 64)
577 worst_case = TRUE; /* Wrong sample width => RGBA */
578
579 switch (bps)
580 {
581 case 1:
582 case 2:
583 case 4:
584 case 8:
585 if (profile_linear)
586 image_precision = GIMP_PRECISION_U8_LINEAR;
587 else
588 image_precision = GIMP_PRECISION_U8_GAMMA;
589
590 type = babl_type ("u8");
591 break;
592
593 case 16:
594 if (sampleformat == SAMPLEFORMAT_IEEEFP)
595 {
596 if (profile_linear)
597 image_precision = GIMP_PRECISION_HALF_LINEAR;
598 else
599 image_precision = GIMP_PRECISION_HALF_GAMMA;
600
601 type = babl_type ("half");
602 }
603 else
604 {
605 if (profile_linear)
606 image_precision = GIMP_PRECISION_U16_LINEAR;
607 else
608 image_precision = GIMP_PRECISION_U16_GAMMA;
609
610 type = babl_type ("u16");
611 }
612 break;
613
614 case 32:
615 if (sampleformat == SAMPLEFORMAT_IEEEFP)
616 {
617 if (profile_linear)
618 image_precision = GIMP_PRECISION_FLOAT_LINEAR;
619 else
620 image_precision = GIMP_PRECISION_FLOAT_GAMMA;
621
622 type = babl_type ("float");
623 }
624 else
625 {
626 if (profile_linear)
627 image_precision = GIMP_PRECISION_U32_LINEAR;
628 else
629 image_precision = GIMP_PRECISION_U32_GAMMA;
630
631 type = babl_type ("u32");
632 }
633 break;
634
635 case 64:
636 if (profile_linear)
637 image_precision = GIMP_PRECISION_DOUBLE_LINEAR;
638 else
639 image_precision = GIMP_PRECISION_DOUBLE_GAMMA;
640
641 type = babl_type ("double");
642 break;
643
644 default:
645 g_message (_("Unsupported bit depth: %d for page %d."),
646 bps, li+1);
647 continue;
648 }
649
650 g_printerr ("bps: %d\n", bps);
651
652 TIFFGetFieldDefaulted (tif, TIFFTAG_SAMPLESPERPIXEL, &spp);
653
654 if (! TIFFGetField (tif, TIFFTAG_EXTRASAMPLES, &extra, &extra_types))
655 extra = 0;
656
657 if (! TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &cols))
658 {
659 TIFFClose (tif);
660 g_message (_("Could not get image width from '%s'"),
661 gimp_file_get_utf8_name (file));
662 return GIMP_PDB_EXECUTION_ERROR;
663 }
664
665 if (! TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &rows))
666 {
667 TIFFClose (tif);
668 g_message (_("Could not get image length from '%s'"),
669 gimp_file_get_utf8_name (file));
670 return GIMP_PDB_EXECUTION_ERROR;
671 }
672
673 if (cols > GIMP_MAX_IMAGE_SIZE || cols <= 0 ||
674 rows > GIMP_MAX_IMAGE_SIZE || rows <= 0)
675 {
676 g_message (_("Invalid image dimensions (%u x %u) for page %d. "
677 "Image may be corrupt."),
678 (guint32) cols, (guint32) rows, li+1);
679 continue;
680 }
681 else
682 {
683 g_printerr ("Image dimensions: %u x %u.\n",
684 (guint32) cols, (guint32) rows);
685 }
686
687 if (! TIFFGetField (tif, TIFFTAG_PHOTOMETRIC, &photomet))
688 {
689 guint16 compression;
690
691 if (TIFFGetField (tif, TIFFTAG_COMPRESSION, &compression) &&
692 (compression == COMPRESSION_CCITTFAX3 ||
693 compression == COMPRESSION_CCITTFAX4 ||
694 compression == COMPRESSION_CCITTRLE ||
695 compression == COMPRESSION_CCITTRLEW))
696 {
697 g_message (_("Could not get photometric from '%s'. "
698 "Image is CCITT compressed, assuming min-is-white"),
699 gimp_file_get_utf8_name (file));
700 photomet = PHOTOMETRIC_MINISWHITE;
701 }
702 else
703 {
704 g_message (_("Could not get photometric from '%s'. "
705 "Assuming min-is-black"),
706 gimp_file_get_utf8_name (file));
707
708 /* old AppleScan software misses out the photometric tag
709 * (and incidentally assumes min-is-white, but xv
710 * assumes min-is-black, so we follow xv's lead. It's
711 * not much hardship to invert the image later).
712 */
713 photomet = PHOTOMETRIC_MINISBLACK;
714 }
715 }
716
717 /* test if the extrasample represents an associated alpha channel... */
718 if (extra > 0 && (extra_types[0] == EXTRASAMPLE_ASSOCALPHA))
719 {
720 alpha = TRUE;
721 tsvals.save_transp_pixels = FALSE;
722 extra--;
723 }
724 else if (extra > 0 && (extra_types[0] == EXTRASAMPLE_UNASSALPHA))
725 {
726 alpha = TRUE;
727 tsvals.save_transp_pixels = TRUE;
728 extra--;
729 }
730 else if (extra > 0 && (extra_types[0] == EXTRASAMPLE_UNSPECIFIED))
731 {
732 if (run_mode != GIMP_RUN_INTERACTIVE)
733 /* In non-interactive mode, we assume unassociated alpha if unspecified.
734 * We don't output messages in interactive mode as the user
735 * has already the ability to choose through a dialog. */
736 g_message (_("Alpha channel type not defined for %s. "
737 "Assuming alpha is not premultiplied"),
738 gimp_file_get_utf8_name (file));
739
740 switch (default_extra)
741 {
742 case GIMP_TIFF_LOAD_ASSOCALPHA:
743 alpha = TRUE;
744 tsvals.save_transp_pixels = FALSE;
745 break;
746 case GIMP_TIFF_LOAD_UNASSALPHA:
747 alpha = TRUE;
748 tsvals.save_transp_pixels = TRUE;
749 break;
750 default: /* GIMP_TIFF_LOAD_CHANNEL */
751 alpha = FALSE;
752 break;
753 }
754 extra--;
755 }
756 else /* extra == 0 */
757 {
758 if (is_non_conformant_tiff (photomet, spp))
759 {
760 if (run_mode != GIMP_RUN_INTERACTIVE)
761 g_message (_("Image '%s' does not conform to the TIFF specification: "
762 "ExtraSamples field is not set while extra channels are present. "
763 "Assuming the first extra channel is non-premultiplied alpha."),
764 gimp_file_get_utf8_name (file));
765
766 switch (default_extra)
767 {
768 case GIMP_TIFF_LOAD_ASSOCALPHA:
769 alpha = TRUE;
770 tsvals.save_transp_pixels = FALSE;
771 break;
772 case GIMP_TIFF_LOAD_UNASSALPHA:
773 alpha = TRUE;
774 tsvals.save_transp_pixels = TRUE;
775 break;
776 default: /* GIMP_TIFF_LOAD_CHANNEL */
777 alpha = FALSE;
778 break;
779 }
780 }
781 else
782 {
783 alpha = FALSE;
784 }
785 }
786
787 extra = get_extra_channels_count (photomet, spp, alpha);
788
789 tiff_mode = GIMP_TIFF_DEFAULT;
790 is_signed = sampleformat == SAMPLEFORMAT_INT;
791
792 switch (photomet)
793 {
794 case PHOTOMETRIC_MINISBLACK:
795 case PHOTOMETRIC_MINISWHITE:
796 if (photomet == PHOTOMETRIC_MINISWHITE)
797 tiff_mode = GIMP_TIFF_GRAY_MINISWHITE;
798 else
799 tiff_mode = GIMP_TIFF_GRAY;
800
801 if ((bps == 1 || bps == 2 || bps == 4) && ! alpha && spp == 1)
802 {
803 if (bps == 1)
804 fill_bit2byte (tiff_mode);
805 else if (bps == 2)
806 fill_2bit2byte (tiff_mode);
807 else if (bps == 4)
808 fill_4bit2byte (tiff_mode);
809 }
810 image_type = GIMP_GRAY;
811 layer_type = alpha ? GIMP_GRAYA_IMAGE : GIMP_GRAY_IMAGE;
812
813 if (alpha)
814 {
815 if (tsvals.save_transp_pixels)
816 {
817 if (profile_linear)
818 {
819 base_format = babl_format_new (babl_model ("YA"),
820 type,
821 babl_component ("Y"),
822 babl_component ("A"),
823 NULL);
824 }
825 else
826 {
827 base_format = babl_format_new (babl_model ("Y'A"),
828 type,
829 babl_component ("Y'"),
830 babl_component ("A"),
831 NULL);
832 }
833 }
834 else
835 {
836 if (profile_linear)
837 {
838 base_format = babl_format_new (babl_model ("YaA"),
839 type,
840 babl_component ("Ya"),
841 babl_component ("A"),
842 NULL);
843 }
844 else
845 {
846 base_format = babl_format_new (babl_model ("Y'aA"),
847 type,
848 babl_component ("Y'a"),
849 babl_component ("A"),
850 NULL);
851 }
852 }
853 }
854 else
855 {
856 if (profile_linear)
857 {
858 base_format = babl_format_new (babl_model ("Y"),
859 type,
860 babl_component ("Y"),
861 NULL);
862 }
863 else
864 {
865 base_format = babl_format_new (babl_model ("Y'"),
866 type,
867 babl_component ("Y'"),
868 NULL);
869 }
870 }
871 break;
872
873 case PHOTOMETRIC_RGB:
874 image_type = GIMP_RGB;
875 layer_type = alpha ? GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE;
876
877 if (alpha)
878 {
879 if (tsvals.save_transp_pixels)
880 {
881 if (profile_linear)
882 {
883 base_format = babl_format_new (babl_model ("RGBA"),
884 type,
885 babl_component ("R"),
886 babl_component ("G"),
887 babl_component ("B"),
888 babl_component ("A"),
889 NULL);
890 }
891 else
892 {
893 base_format = babl_format_new (babl_model ("R'G'B'A"),
894 type,
895 babl_component ("R'"),
896 babl_component ("G'"),
897 babl_component ("B'"),
898 babl_component ("A"),
899 NULL);
900 }
901 }
902 else
903 {
904 if (profile_linear)
905 {
906 base_format = babl_format_new (babl_model ("RaGaBaA"),
907 type,
908 babl_component ("Ra"),
909 babl_component ("Ga"),
910 babl_component ("Ba"),
911 babl_component ("A"),
912 NULL);
913 }
914 else
915 {
916 base_format = babl_format_new (babl_model ("R'aG'aB'aA"),
917 type,
918 babl_component ("R'a"),
919 babl_component ("G'a"),
920 babl_component ("B'a"),
921 babl_component ("A"),
922 NULL);
923 }
924 }
925 }
926 else
927 {
928 if (profile_linear)
929 {
930 base_format = babl_format_new (babl_model ("RGB"),
931 type,
932 babl_component ("R"),
933 babl_component ("G"),
934 babl_component ("B"),
935 NULL);
936 }
937 else
938 {
939 base_format = babl_format_new (babl_model ("R'G'B'"),
940 type,
941 babl_component ("R'"),
942 babl_component ("G'"),
943 babl_component ("B'"),
944 NULL);
945 }
946 }
947 break;
948
949 case PHOTOMETRIC_PALETTE:
950 image_type = GIMP_INDEXED;
951 layer_type = alpha ? GIMP_INDEXEDA_IMAGE : GIMP_INDEXED_IMAGE;
952
953 if (bps < 8)
954 tiff_mode = GIMP_TIFF_INDEXED; /* Only bps < 8 needs special handling. */
955
956 if (bps == 1)
957 fill_bit2byte (tiff_mode);
958 else if (bps == 2)
959 fill_2bit2byte (tiff_mode);
960 else if (bps == 4)
961 fill_4bit2byte (tiff_mode);
962 break;
963
964 default:
965 g_printerr ("photomet: %d (%d)\n", photomet, PHOTOMETRIC_PALETTE);
966 worst_case = TRUE;
967 break;
968 }
969
970 /* attach a parasite containing the compression */
971 {
972 guint16 compression = COMPRESSION_NONE;
973
974 if (TIFFGetField (tif, TIFFTAG_COMPRESSION, &compression))
975 {
976 switch (compression)
977 {
978 case COMPRESSION_NONE:
979 case COMPRESSION_LZW:
980 case COMPRESSION_PACKBITS:
981 case COMPRESSION_DEFLATE:
982 case COMPRESSION_ADOBE_DEFLATE:
983 case COMPRESSION_JPEG:
984 case COMPRESSION_CCITTFAX3:
985 case COMPRESSION_CCITTFAX4:
986 break;
987
988 case COMPRESSION_OJPEG:
989 worst_case = TRUE;
990 compression = COMPRESSION_JPEG;
991 break;
992
993 default:
994 g_message (_("Invalid or unknown compression %u. "
995 "Setting compression to none."),
996 compression);
997 compression = COMPRESSION_NONE;
998 break;
999 }
1000 }
1001
1002 save_vals.compression = compression;
1003 }
1004
1005 if (worst_case)
1006 {
1007 image_type = GIMP_RGB;
1008 layer_type = GIMP_RGBA_IMAGE;
1009
1010 if (profile_linear)
1011 {
1012 base_format = babl_format_new (babl_model ("RaGaBaA"),
1013 type,
1014 babl_component ("Ra"),
1015 babl_component ("Ga"),
1016 babl_component ("Ba"),
1017 babl_component ("A"),
1018 NULL);
1019 }
1020 else
1021 {
1022 base_format = babl_format_new (babl_model ("R'aG'aB'aA"),
1023 type,
1024 babl_component ("R'a"),
1025 babl_component ("G'a"),
1026 babl_component ("B'a"),
1027 babl_component ("A"),
1028 NULL);
1029 }
1030 }
1031
1032 if (pages.target == GIMP_PAGE_SELECTOR_TARGET_LAYERS)
1033 {
1034 if (li == 0)
1035 {
1036 first_image_type = image_type;
1037 }
1038 else if (image_type != first_image_type)
1039 {
1040 continue;
1041 }
1042 }
1043
1044 if ((pages.target == GIMP_PAGE_SELECTOR_TARGET_IMAGES) || (! *image))
1045 {
1046 *image = gimp_image_new_with_precision (cols, rows, image_type,
1047 image_precision);
1048
1049 if (*image < 1)
1050 {
1051 TIFFClose (tif);
1052 g_message (_("Could not create a new image: %s"),
1053 gimp_get_pdb_error ());
1054 return GIMP_PDB_EXECUTION_ERROR;
1055 }
1056
1057 gimp_image_undo_disable (*image);
1058
1059 if (pages.target == GIMP_PAGE_SELECTOR_TARGET_IMAGES)
1060 {
1061 gchar *fname = g_strdup_printf ("%s-%d", g_file_get_uri (file),
1062 ilayer);
1063
1064 gimp_image_set_filename (*image, fname);
1065 g_free (fname);
1066
1067 images_list = g_list_prepend (images_list,
1068 GINT_TO_POINTER (*image));
1069 }
1070 else if (pages.o_pages != pages.n_pages)
1071 {
1072 gchar *fname = g_strdup_printf (_("%s-%d-of-%d-pages"),
1073 g_file_get_uri (file),
1074 pages.n_pages, pages.o_pages);
1075
1076 gimp_image_set_filename (*image, fname);
1077 g_free (fname);
1078 }
1079 else
1080 {
1081 gimp_image_set_filename (*image, g_file_get_uri (file));
1082 }
1083 }
1084
1085 /* attach color profile */
1086 if (profile)
1087 {
1088 if (pages.target == GIMP_PAGE_SELECTOR_TARGET_IMAGES || profile == first_profile)
1089 gimp_image_set_color_profile (*image, profile);
1090
1091 g_object_unref (profile);
1092 }
1093
1094 /* attach parasites */
1095 {
1096 GimpParasite *parasite;
1097 const gchar *img_desc;
1098
1099 parasite = gimp_parasite_new ("tiff-save-options", 0,
1100 sizeof (save_vals), &save_vals);
1101 gimp_image_attach_parasite (*image, parasite);
1102 gimp_parasite_free (parasite);
1103
1104 /* Attach a parasite containing the image description.
1105 * Pretend to be a gimp comment so other plugins will use this
1106 * description as an image comment where appropriate.
1107 */
1108 if (TIFFGetField (tif, TIFFTAG_IMAGEDESCRIPTION, &img_desc) &&
1109 g_utf8_validate (img_desc, -1, NULL))
1110 {
1111 parasite = gimp_parasite_new ("gimp-comment",
1112 GIMP_PARASITE_PERSISTENT,
1113 strlen (img_desc) + 1, img_desc);
1114 gimp_image_attach_parasite (*image, parasite);
1115 gimp_parasite_free (parasite);
1116 }
1117 }
1118
1119 /* Attach GeoTIFF Tags as Parasite, If available */
1120 {
1121 GimpParasite *parasite = NULL;
1122 void *geotag_data = NULL;
1123 uint32 count = 0;
1124
1125 if (TIFFGetField (tif, GEOTIFF_MODELPIXELSCALE, &count, &geotag_data))
1126 {
1127 parasite = gimp_parasite_new ("Gimp_GeoTIFF_ModelPixelScale",
1128 GIMP_PARASITE_PERSISTENT,
1129 (TIFFDataWidth (TIFF_DOUBLE) * count),
1130 geotag_data);
1131 gimp_image_attach_parasite (*image, parasite);
1132 gimp_parasite_free (parasite);
1133 }
1134
1135 if (TIFFGetField (tif, GEOTIFF_MODELTIEPOINT, &count, &geotag_data))
1136 {
1137 parasite = gimp_parasite_new ("Gimp_GeoTIFF_ModelTiePoint",
1138 GIMP_PARASITE_PERSISTENT,
1139 (TIFFDataWidth (TIFF_DOUBLE) * count),
1140 geotag_data);
1141 gimp_image_attach_parasite (*image, parasite);
1142 gimp_parasite_free (parasite);
1143 }
1144
1145 if (TIFFGetField (tif, GEOTIFF_MODELTRANSFORMATION, &count, &geotag_data))
1146 {
1147 parasite = gimp_parasite_new ("Gimp_GeoTIFF_ModelTransformation",
1148 GIMP_PARASITE_PERSISTENT,
1149 (TIFFDataWidth (TIFF_DOUBLE) * count),
1150 geotag_data);
1151 gimp_image_attach_parasite (*image, parasite);
1152 gimp_parasite_free (parasite);
1153 }
1154
1155 if (TIFFGetField (tif, GEOTIFF_KEYDIRECTORY, &count, &geotag_data) )
1156 {
1157 parasite = gimp_parasite_new ("Gimp_GeoTIFF_KeyDirectory",
1158 GIMP_PARASITE_PERSISTENT,
1159 (TIFFDataWidth (TIFF_SHORT) * count),
1160 geotag_data);
1161 gimp_image_attach_parasite (*image, parasite);
1162 gimp_parasite_free (parasite);
1163 }
1164
1165 if (TIFFGetField (tif, GEOTIFF_DOUBLEPARAMS, &count, &geotag_data))
1166 {
1167 parasite = gimp_parasite_new ("Gimp_GeoTIFF_DoubleParams",
1168 GIMP_PARASITE_PERSISTENT,
1169 (TIFFDataWidth (TIFF_DOUBLE) * count),
1170 geotag_data);
1171 gimp_image_attach_parasite (*image, parasite);
1172 gimp_parasite_free (parasite);
1173 }
1174
1175 if (TIFFGetField (tif, GEOTIFF_ASCIIPARAMS, &count, &geotag_data))
1176 {
1177 parasite = gimp_parasite_new ("Gimp_GeoTIFF_Asciiparams",
1178 GIMP_PARASITE_PERSISTENT,
1179 (TIFFDataWidth (TIFF_ASCII) * count),
1180 geotag_data);
1181 gimp_image_attach_parasite (*image, parasite);
1182 gimp_parasite_free (parasite);
1183 }
1184 }
1185
1186 /* any resolution info in the file? */
1187 {
1188 gfloat xres = 72.0;
1189 gfloat yres = 72.0;
1190 gushort read_unit;
1191 GimpUnit unit = GIMP_UNIT_PIXEL; /* invalid unit */
1192
1193 if (TIFFGetField (tif, TIFFTAG_XRESOLUTION, &xres))
1194 {
1195 if (TIFFGetField (tif, TIFFTAG_YRESOLUTION, &yres))
1196 {
1197 if (TIFFGetFieldDefaulted (tif, TIFFTAG_RESOLUTIONUNIT,
1198 &read_unit))
1199 {
1200 switch (read_unit)
1201 {
1202 case RESUNIT_NONE:
1203 /* ImageMagick writes files with this silly resunit */
1204 break;
1205
1206 case RESUNIT_INCH:
1207 unit = GIMP_UNIT_INCH;
1208 break;
1209
1210 case RESUNIT_CENTIMETER:
1211 xres *= 2.54;
1212 yres *= 2.54;
1213 unit = GIMP_UNIT_MM; /* this is our default metric unit */
1214 break;
1215
1216 default:
1217 g_message (_("Unknown resolution "
1218 "unit type %d, assuming dpi"), read_unit);
1219 break;
1220 }
1221 }
1222 else
1223 {
1224 /* no res unit tag */
1225
1226 /* old AppleScan software produces these */
1227 g_message (_("Warning: resolution specified without "
1228 "unit type, assuming dpi"));
1229 }
1230 }
1231 else
1232 {
1233 /* xres but no yres */
1234
1235 g_message (_("Warning: no y resolution info, assuming same as x"));
1236 yres = xres;
1237 }
1238
1239 /* now set the new image's resolution info */
1240
1241 /* If it is invalid, instead of forcing 72dpi, do not set
1242 * the resolution at all. Gimp will then use the default
1243 * set by the user
1244 */
1245 if (read_unit != RESUNIT_NONE)
1246 {
1247 gimp_image_set_resolution (*image, xres, yres);
1248 if (unit != GIMP_UNIT_PIXEL)
1249 gimp_image_set_unit (*image, unit);
1250
1251 *resolution_loaded = TRUE;
1252 }
1253 }
1254
1255 /* no x res tag => we assume we have no resolution info, so we
1256 * don't care. Older versions of this plugin used to write
1257 * files with no resolution tags at all.
1258 */
1259
1260 /* TODO: haven't caught the case where yres tag is present,
1261 * but not xres. This is left as an exercise for the reader -
1262 * they should feel free to shoot the author of the broken
1263 * program that produced the damaged TIFF file in the first
1264 * place.
1265 */
1266
1267 /* handle layer offset */
1268 if (! TIFFGetField (tif, TIFFTAG_XPOSITION, &layer_offset_x))
1269 layer_offset_x = 0.0;
1270
1271 if (! TIFFGetField (tif, TIFFTAG_YPOSITION, &layer_offset_y))
1272 layer_offset_y = 0.0;
1273
1274 /* round floating point position to integer position required
1275 * by GIMP
1276 */
1277 layer_offset_x_pixel = ROUND (layer_offset_x * xres);
1278 layer_offset_y_pixel = ROUND (layer_offset_y * yres);
1279 }
1280
1281 /* Install colormap for INDEXED images only */
1282 if (image_type == GIMP_INDEXED)
1283 {
1284 guchar cmap[768];
1285 gushort *redmap;
1286 gushort *greenmap;
1287 gushort *bluemap;
1288 gint i, j;
1289
1290 if (! TIFFGetField (tif, TIFFTAG_COLORMAP,
1291 &redmap, &greenmap, &bluemap))
1292 {
1293 TIFFClose (tif);
1294 g_message (_("Could not get colormaps from '%s'"),
1295 gimp_file_get_utf8_name (file));
1296 return GIMP_PDB_EXECUTION_ERROR;
1297 }
1298
1299 for (i = 0, j = 0; i < (1 << bps); i++)
1300 {
1301 cmap[j++] = redmap[i] >> 8;
1302 cmap[j++] = greenmap[i] >> 8;
1303 cmap[j++] = bluemap[i] >> 8;
1304 }
1305
1306 gimp_image_set_colormap (*image, cmap, (1 << bps));
1307 }
1308
1309 if (pages.target != GIMP_PAGE_SELECTOR_TARGET_IMAGES)
1310 load_paths (tif, *image, cols, rows,
1311 layer_offset_x_pixel, layer_offset_y_pixel);
1312 else
1313 load_paths (tif, *image, cols, rows, 0, 0);
1314
1315 /* Allocate ChannelData for all channels, even the background layer */
1316 channel = g_new0 (ChannelData, extra + 1);
1317
1318 /* try and use layer name from tiff file */
1319 name = tiff_get_page_name (tif);
1320
1321 if (name)
1322 {
1323 layer = gimp_layer_new (*image, name,
1324 cols, rows,
1325 layer_type,
1326 100,
1327 gimp_image_get_default_new_layer_mode (*image));
1328 }
1329 else
1330 {
1331 gchar *name;
1332
1333 if (ilayer == 0)
1334 name = g_strdup (_("Background"));
1335 else
1336 name = g_strdup_printf (_("Page %d"), ilayer);
1337
1338 layer = gimp_layer_new (*image, name,
1339 cols, rows,
1340 layer_type,
1341 100,
1342 gimp_image_get_default_new_layer_mode (*image));
1343 g_free (name);
1344 }
1345
1346 if (! base_format && image_type == GIMP_INDEXED)
1347 {
1348 /* can't create the palette format here, need to get it from
1349 * an existing layer
1350 */
1351 base_format = gimp_drawable_get_format (layer);
1352 }
1353
1354 channel[0].ID = layer;
1355 channel[0].buffer = gimp_drawable_get_buffer (layer);
1356 channel[0].format = base_format;
1357
1358 if (extra > 0 && ! worst_case)
1359 {
1360 /* Add extra channels as appropriate */
1361 for (i = 1; i <= extra; i++)
1362 {
1363 GimpRGB color;
1364
1365 gimp_rgb_set (&color, 0.0, 0.0, 0.0);
1366
1367 channel[i].ID = gimp_channel_new (*image, _("TIFF Channel"),
1368 cols, rows,
1369 100.0, &color);
1370 gimp_image_insert_channel (*image, channel[i].ID, -1, 0);
1371 channel[i].buffer = gimp_drawable_get_buffer (channel[i].ID);
1372 channel[i].format = babl_format_new (babl_model ("Y'"),
1373 type,
1374 babl_component ("Y'"),
1375 NULL);
1376 }
1377 }
1378
1379 TIFFGetField (tif, TIFFTAG_PLANARCONFIG, &planar);
1380
1381 if (worst_case)
1382 {
1383 load_rgba (tif, channel);
1384 }
1385 else if (planar == PLANARCONFIG_CONTIG)
1386 {
1387 load_contiguous (tif, channel, type, bps, spp,
1388 tiff_mode, is_signed, extra);
1389 }
1390 else
1391 {
1392 load_separate (tif, channel, type, bps, spp,
1393 tiff_mode, is_signed, extra);
1394 }
1395
1396 if (TIFFGetField (tif, TIFFTAG_ORIENTATION, &orientation))
1397 {
1398 gboolean flip_horizontal = FALSE;
1399 gboolean flip_vertical = FALSE;
1400
1401 switch (orientation)
1402 {
1403 case ORIENTATION_TOPLEFT:
1404 break;
1405
1406 case ORIENTATION_TOPRIGHT:
1407 flip_horizontal = TRUE;
1408 break;
1409
1410 case ORIENTATION_BOTRIGHT:
1411 flip_horizontal = TRUE;
1412 flip_vertical = TRUE;
1413 break;
1414
1415 case ORIENTATION_BOTLEFT:
1416 flip_vertical = TRUE;
1417 break;
1418
1419 default:
1420 g_warning ("Orientation %d not handled yet!", orientation);
1421 break;
1422 }
1423
1424 if (flip_horizontal)
1425 gimp_item_transform_flip_simple (layer,
1426 GIMP_ORIENTATION_HORIZONTAL,
1427 TRUE /* auto_center */,
1428 -1.0 /* axis */);
1429
1430 if (flip_vertical)
1431 gimp_item_transform_flip_simple (layer,
1432 GIMP_ORIENTATION_VERTICAL,
1433 TRUE /* auto_center */,
1434 -1.0 /* axis */);
1435 }
1436
1437 for (i = 0; i <= extra; i++)
1438 {
1439 if (channel[i].buffer)
1440 g_object_unref (channel[i].buffer);
1441 }
1442
1443 g_free (channel);
1444 channel = NULL;
1445
1446 if (pages.target != GIMP_PAGE_SELECTOR_TARGET_IMAGES)
1447 {
1448 /* compute bounding box of all layers read so far */
1449 if (min_col > layer_offset_x_pixel)
1450 min_col = layer_offset_x_pixel;
1451 if (min_row > layer_offset_y_pixel)
1452 min_row = layer_offset_y_pixel;
1453
1454 if (max_col < layer_offset_x_pixel + cols)
1455 max_col = layer_offset_x_pixel + cols;
1456 if (max_row < layer_offset_y_pixel + rows)
1457 max_row = layer_offset_y_pixel + rows;
1458
1459 /* position the layer */
1460 if (layer_offset_x_pixel > 0 ||
1461 layer_offset_y_pixel > 0)
1462 {
1463 gimp_layer_set_offsets (layer,
1464 layer_offset_x_pixel,
1465 layer_offset_y_pixel);
1466 }
1467 }
1468
1469 gimp_image_insert_layer (*image, layer, -1, -1);
1470
1471 if (pages.target == GIMP_PAGE_SELECTOR_TARGET_IMAGES)
1472 {
1473 gimp_image_undo_enable (*image);
1474 gimp_image_clean_all (*image);
1475 }
1476
1477 gimp_progress_update (1.0);
1478 }
1479 g_clear_object (&first_profile);
1480
1481 if (pages.target == GIMP_PAGE_SELECTOR_TARGET_IMAGES)
1482 {
1483 GList *list = images_list;
1484
1485 if (list)
1486 {
1487 *image = GPOINTER_TO_INT (list->data);
1488
1489 list = g_list_next (list);
1490 }
1491
1492 for (; list; list = g_list_next (list))
1493 {
1494 gimp_display_new (GPOINTER_TO_INT (list->data));
1495 }
1496
1497 g_list_free (images_list);
1498 }
1499 else
1500 {
1501 if (! (*image))
1502 {
1503 TIFFClose (tif);
1504 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
1505 _("No data could be read from TIFF '%s'. The file is probably corrupted."),
1506 gimp_file_get_utf8_name (file));
1507
1508 return GIMP_PDB_EXECUTION_ERROR;
1509 }
1510
1511 if (pages.keep_empty_space)
1512 {
1513 /* unfortunately we have no idea about empty space
1514 at the bottom/right of layers */
1515 min_col = 0;
1516 min_row = 0;
1517 }
1518
1519 /* resize image to bounding box of all layers */
1520 gimp_image_resize (*image,
1521 max_col - min_col, max_row - min_row,
1522 -min_col, -min_row);
1523
1524 gimp_image_undo_enable (*image);
1525 }
1526
1527 g_free (pages.pages);
1528 TIFFClose (tif);
1529
1530 return GIMP_PDB_SUCCESS;
1531 }
1532
1533 static GimpColorProfile *
load_profile(TIFF * tif)1534 load_profile (TIFF *tif)
1535 {
1536 GimpColorProfile *profile = NULL;
1537
1538 #ifdef TIFFTAG_ICCPROFILE
1539 /* If TIFFTAG_ICCPROFILE is defined we are dealing with a
1540 * libtiff version that can handle ICC profiles. Otherwise just
1541 * return a NULL profile.
1542 */
1543 uint32 profile_size;
1544 guchar *icc_profile;
1545
1546 /* set the ICC profile - if found in the TIFF */
1547 if (TIFFGetField (tif, TIFFTAG_ICCPROFILE, &profile_size, &icc_profile))
1548 {
1549 profile = gimp_color_profile_new_from_icc_profile (icc_profile,
1550 profile_size,
1551 NULL);
1552 }
1553 #endif
1554
1555 return profile;
1556 }
1557
1558 static void
load_rgba(TIFF * tif,ChannelData * channel)1559 load_rgba (TIFF *tif,
1560 ChannelData *channel)
1561 {
1562 guint32 image_width;
1563 guint32 image_height;
1564 guint32 row;
1565 guint32 *buffer;
1566
1567 g_printerr ("%s\n", __func__);
1568
1569 TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &image_width);
1570 TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &image_height);
1571
1572 buffer = g_new (uint32, image_width * image_height);
1573
1574 if (! TIFFReadRGBAImage (tif, image_width, image_height, buffer, 0))
1575 {
1576 g_message (_("%s: Unsupported image format, no RGBA loader available"),
1577 G_STRFUNC);
1578 g_free (buffer);
1579 return;
1580 }
1581
1582 for (row = 0; row < image_height; row++)
1583 {
1584 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
1585 /* Make sure our channels are in the right order */
1586 guint32 row_start = row * image_width;
1587 guint32 row_end = row_start + image_width;
1588 guint32 i;
1589
1590 for (i = row_start; i < row_end; i++)
1591 buffer[i] = GUINT32_TO_LE (buffer[i]);
1592 #endif
1593
1594 gegl_buffer_set (channel[0].buffer,
1595 GEGL_RECTANGLE (0, image_height - row - 1,
1596 image_width, 1),
1597 0, channel[0].format,
1598 ((guchar *) buffer) + row * image_width * 4,
1599 GEGL_AUTO_ROWSTRIDE);
1600
1601 if ((row % 32) == 0)
1602 gimp_progress_update ((gdouble) row / (gdouble) image_height);
1603 }
1604
1605 g_free (buffer);
1606 }
1607
1608 static void
load_paths(TIFF * tif,gint image,gint width,gint height,gint offset_x,gint offset_y)1609 load_paths (TIFF *tif,
1610 gint image,
1611 gint width,
1612 gint height,
1613 gint offset_x,
1614 gint offset_y)
1615 {
1616 gsize n_bytes;
1617 gchar *bytes;
1618 gint path_index;
1619 gsize pos;
1620
1621 if (! TIFFGetField (tif, TIFFTAG_PHOTOSHOP, &n_bytes, &bytes))
1622 return;
1623
1624 path_index = 0;
1625 pos = 0;
1626
1627 while (pos < n_bytes)
1628 {
1629 guint16 id;
1630 gsize len;
1631 gchar *name;
1632 guint32 *val32;
1633 guint16 *val16;
1634
1635 if (n_bytes-pos < 7 ||
1636 strncmp (bytes + pos, "8BIM", 4) != 0)
1637 break;
1638
1639 pos += 4;
1640
1641 val16 = (guint16 *) (bytes + pos);
1642 id = GUINT16_FROM_BE (*val16);
1643 pos += 2;
1644
1645 /* g_printerr ("id: %x\n", id); */
1646 len = (guchar) bytes[pos];
1647
1648 if (n_bytes - pos < len + 1)
1649 break; /* block not big enough */
1650
1651 /* do we have the UTF-marker? is it valid UTF-8?
1652 * if so, we assume an utf-8 encoded name, otherwise we
1653 * assume iso8859-1
1654 */
1655 name = bytes + pos + 1;
1656 if (len >= 3 &&
1657 name[0] == '\xEF' && name[1] == '\xBB' && name[2] == '\xBF' &&
1658 g_utf8_validate (name, len, NULL))
1659 {
1660 name = g_strndup (name + 3, len - 3);
1661 }
1662 else
1663 {
1664 name = g_convert (name, len, "utf-8", "iso8859-1", NULL, NULL, NULL);
1665 }
1666
1667 if (! name)
1668 name = g_strdup ("(imported path)");
1669
1670 pos += len + 1;
1671
1672 if (pos % 2) /* padding */
1673 pos++;
1674
1675 if (n_bytes - pos < 4)
1676 break; /* block not big enough */
1677
1678 val32 = (guint32 *) (bytes + pos);
1679 len = GUINT32_FROM_BE (*val32);
1680 pos += 4;
1681
1682 if (n_bytes - pos < len)
1683 break; /* block not big enough */
1684
1685 if (id >= 2000 && id <= 2998)
1686 {
1687 /* path information */
1688 guint16 type;
1689 gint rec = pos;
1690 gint32 vectors;
1691 gdouble *points = NULL;
1692 gint expected_points = 0;
1693 gint pointcount = 0;
1694 gboolean closed = FALSE;
1695
1696 vectors = gimp_vectors_new (image, name);
1697 gimp_image_insert_vectors (image, vectors, -1, path_index);
1698 path_index++;
1699
1700 while (rec < pos + len)
1701 {
1702 /* path records */
1703 val16 = (guint16 *) (bytes + rec);
1704 type = GUINT16_FROM_BE (*val16);
1705
1706 switch (type)
1707 {
1708 case 0: /* new closed subpath */
1709 case 3: /* new open subpath */
1710 val16 = (guint16 *) (bytes + rec + 2);
1711 expected_points = GUINT16_FROM_BE (*val16);
1712 pointcount = 0;
1713 closed = (type == 0);
1714
1715 if (n_bytes - rec < (expected_points + 1) * 26)
1716 {
1717 g_printerr ("not enough point records\n");
1718 rec = pos + len;
1719 continue;
1720 }
1721
1722 if (points)
1723 g_free (points);
1724 points = g_new (gdouble, expected_points * 6);
1725 break;
1726
1727 case 1: /* closed subpath bezier knot, linked */
1728 case 2: /* closed subpath bezier knot, unlinked */
1729 case 4: /* open subpath bezier knot, linked */
1730 case 5: /* open subpath bezier knot, unlinked */
1731 /* since we already know if the subpath is open
1732 * or closed and since we don't differentiate between
1733 * linked and unlinked, just treat all the same... */
1734
1735 if (pointcount < expected_points)
1736 {
1737 gint j;
1738
1739 for (j = 0; j < 6; j++)
1740 {
1741 gdouble f;
1742 guint32 coord;
1743
1744 const gint size = j % 2 ? width : height;
1745 const gint offset = j % 2 ? offset_x : offset_y;
1746
1747 val32 = (guint32 *) (bytes + rec + 2 + j * 4);
1748 coord = GUINT32_FROM_BE (*val32);
1749
1750 f = (double) ((gchar) ((coord >> 24) & 0xFF)) +
1751 (double) (coord & 0x00FFFFFF) /
1752 (double) 0xFFFFFF;
1753
1754 /* coords are stored with vertical component
1755 * first, gimp expects the horizontal
1756 * component first. Sigh.
1757 */
1758 points[pointcount * 6 + (j ^ 1)] = f * size + offset;
1759 }
1760
1761 pointcount++;
1762
1763 if (pointcount == expected_points)
1764 {
1765 gimp_vectors_stroke_new_from_points (vectors,
1766 GIMP_VECTORS_STROKE_TYPE_BEZIER,
1767 pointcount * 6,
1768 points,
1769 closed);
1770 }
1771 }
1772 else
1773 {
1774 g_printerr ("Oops - unexpected point record\n");
1775 }
1776
1777 break;
1778
1779 case 6: /* path fill rule record */
1780 case 7: /* clipboard record (?) */
1781 case 8: /* initial fill rule record (?) */
1782 /* we cannot use this information */
1783
1784 default:
1785 break;
1786 }
1787
1788 rec += 26;
1789 }
1790
1791 if (points)
1792 g_free (points);
1793 }
1794
1795 pos += len;
1796
1797 if (pos % 2) /* padding */
1798 pos++;
1799
1800 g_free (name);
1801 }
1802 }
1803
1804
1805 static void
load_contiguous(TIFF * tif,ChannelData * channel,const Babl * type,gushort bps,gushort spp,TiffColorMode tiff_mode,gboolean is_signed,gint extra)1806 load_contiguous (TIFF *tif,
1807 ChannelData *channel,
1808 const Babl *type,
1809 gushort bps,
1810 gushort spp,
1811 TiffColorMode tiff_mode,
1812 gboolean is_signed,
1813 gint extra)
1814 {
1815 guint32 image_width;
1816 guint32 image_height;
1817 guint32 tile_width;
1818 guint32 tile_height;
1819 gint bytes_per_pixel;
1820 const Babl *src_format;
1821 guchar *buffer;
1822 guchar *bw_buffer = NULL;
1823 gdouble progress = 0.0;
1824 gdouble one_row;
1825 guint32 y;
1826 gint i;
1827 gboolean needs_upscale = FALSE;
1828
1829 g_printerr ("%s\n", __func__);
1830
1831 TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &image_width);
1832 TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &image_height);
1833
1834 tile_width = image_width;
1835
1836 if (TIFFIsTiled (tif))
1837 {
1838 TIFFGetField (tif, TIFFTAG_TILEWIDTH, &tile_width);
1839 TIFFGetField (tif, TIFFTAG_TILELENGTH, &tile_height);
1840
1841 buffer = g_malloc (TIFFTileSize (tif));
1842 }
1843 else
1844 {
1845 tile_width = image_width;
1846 tile_height = 1;
1847
1848 buffer = g_malloc (TIFFScanlineSize (tif));
1849 }
1850
1851 if (tiff_mode != GIMP_TIFF_DEFAULT && bps < 8)
1852 {
1853 needs_upscale = TRUE;
1854 bw_buffer = g_malloc (tile_width * tile_height);
1855 }
1856
1857 one_row = (gdouble) tile_height / (gdouble) image_height;
1858
1859 src_format = babl_format_n (type, spp);
1860
1861 /* consistency check */
1862 bytes_per_pixel = 0;
1863 for (i = 0; i <= extra; i++)
1864 bytes_per_pixel += babl_format_get_bytes_per_pixel (channel[i].format);
1865
1866 g_printerr ("bytes_per_pixel: %d, format: %d\n",
1867 bytes_per_pixel,
1868 babl_format_get_bytes_per_pixel (src_format));
1869
1870 for (y = 0; y < image_height; y += tile_height)
1871 {
1872 guint32 x;
1873
1874 for (x = 0; x < image_width; x += tile_width)
1875 {
1876 GeglBuffer *src_buf;
1877 guint32 rows;
1878 guint32 cols;
1879 gint offset;
1880
1881 gimp_progress_update (progress + one_row *
1882 ((gdouble) x / (gdouble) image_width));
1883
1884 if (TIFFIsTiled (tif))
1885 {
1886 if (TIFFReadTile (tif, buffer, x, y, 0, 0) == -1)
1887 {
1888 g_message (_("Reading tile failed. Image may be corrupt at line %d."), y);
1889 g_free (buffer);
1890 g_free (bw_buffer);
1891 return;
1892 }
1893 }
1894 else if (TIFFReadScanline (tif, buffer, y, 0) == -1)
1895 {
1896 /* Error reading scanline, stop loading */
1897 g_message (_("Reading scanline failed. Image may be corrupt at line %d."), y);
1898 g_free (buffer);
1899 g_free (bw_buffer);
1900 return;
1901 }
1902
1903 cols = MIN (image_width - x, tile_width);
1904 rows = MIN (image_height - y, tile_height);
1905
1906 if (needs_upscale)
1907 {
1908 if (bps == 1)
1909 convert_bit2byte (buffer, bw_buffer, cols, rows);
1910 else if (bps == 2)
1911 convert_2bit2byte (buffer, bw_buffer, cols, rows);
1912 else if (bps == 4)
1913 convert_4bit2byte (buffer, bw_buffer, cols, rows);
1914 }
1915 else if (is_signed)
1916 {
1917 convert_int2uint (buffer, bps, spp, cols, rows,
1918 tile_width * bytes_per_pixel);
1919 }
1920
1921 if (tiff_mode == GIMP_TIFF_GRAY_MINISWHITE && bps == 8)
1922 {
1923 convert_miniswhite (buffer, cols, rows);
1924 }
1925
1926 src_buf = gegl_buffer_linear_new_from_data (needs_upscale ? bw_buffer : buffer,
1927 src_format,
1928 GEGL_RECTANGLE (0, 0, cols, rows),
1929 tile_width * bytes_per_pixel,
1930 NULL, NULL);
1931
1932 offset = 0;
1933
1934 for (i = 0; i <= extra; i++)
1935 {
1936 GeglBufferIterator *iter;
1937 gint src_bpp;
1938 gint dest_bpp;
1939
1940 src_bpp = babl_format_get_bytes_per_pixel (src_format);
1941 dest_bpp = babl_format_get_bytes_per_pixel (channel[i].format);
1942
1943 iter = gegl_buffer_iterator_new (src_buf,
1944 GEGL_RECTANGLE (0, 0, cols, rows),
1945 0, NULL,
1946 GEGL_ACCESS_READ,
1947 GEGL_ABYSS_NONE, 2);
1948 gegl_buffer_iterator_add (iter, channel[i].buffer,
1949 GEGL_RECTANGLE (x, y, cols, rows),
1950 0, channel[i].format,
1951 GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
1952
1953 while (gegl_buffer_iterator_next (iter))
1954 {
1955 guchar *s = iter->items[0].data;
1956 guchar *d = iter->items[1].data;
1957 gint length = iter->length;
1958
1959 s += offset;
1960
1961 while (length--)
1962 {
1963 memcpy (d, s, dest_bpp);
1964 d += dest_bpp;
1965 s += src_bpp;
1966 }
1967 }
1968
1969 offset += dest_bpp;
1970 }
1971
1972 g_object_unref (src_buf);
1973 }
1974
1975 progress += one_row;
1976 }
1977
1978 g_free (buffer);
1979 g_free (bw_buffer);
1980 }
1981
1982
1983 static void
load_separate(TIFF * tif,ChannelData * channel,const Babl * type,gushort bps,gushort spp,TiffColorMode tiff_mode,gboolean is_signed,gint extra)1984 load_separate (TIFF *tif,
1985 ChannelData *channel,
1986 const Babl *type,
1987 gushort bps,
1988 gushort spp,
1989 TiffColorMode tiff_mode,
1990 gboolean is_signed,
1991 gint extra)
1992 {
1993 guint32 image_width;
1994 guint32 image_height;
1995 guint32 tile_width;
1996 guint32 tile_height;
1997 gint bytes_per_pixel;
1998 const Babl *src_format;
1999 guchar *buffer;
2000 guchar *bw_buffer = NULL;
2001 gdouble progress = 0.0;
2002 gdouble one_row;
2003 gint i, compindex;
2004 gboolean needs_upscale = FALSE;
2005
2006 g_printerr ("%s\n", __func__);
2007
2008 TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &image_width);
2009 TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &image_height);
2010
2011 tile_width = image_width;
2012
2013 if (TIFFIsTiled (tif))
2014 {
2015 TIFFGetField (tif, TIFFTAG_TILEWIDTH, &tile_width);
2016 TIFFGetField (tif, TIFFTAG_TILELENGTH, &tile_height);
2017
2018 buffer = g_malloc (TIFFTileSize (tif));
2019 }
2020 else
2021 {
2022 tile_width = image_width;
2023 tile_height = 1;
2024
2025 buffer = g_malloc (TIFFScanlineSize (tif));
2026 }
2027
2028 if (tiff_mode != GIMP_TIFF_DEFAULT && bps < 8)
2029 {
2030 needs_upscale = TRUE;
2031 bw_buffer = g_malloc (tile_width * tile_height);
2032 }
2033
2034 one_row = (gdouble) tile_height / (gdouble) image_height;
2035
2036 src_format = babl_format_n (type, 1);
2037
2038 /* consistency check */
2039 bytes_per_pixel = 0;
2040 for (i = 0; i <= extra; i++)
2041 bytes_per_pixel += babl_format_get_bytes_per_pixel (channel[i].format);
2042
2043 g_printerr ("bytes_per_pixel: %d, format: %d\n",
2044 bytes_per_pixel,
2045 babl_format_get_bytes_per_pixel (src_format));
2046
2047 compindex = 0;
2048
2049 for (i = 0; i <= extra; i++)
2050 {
2051 gint n_comps;
2052 gint src_bpp;
2053 gint dest_bpp;
2054 gint offset;
2055 gint j;
2056
2057 n_comps = babl_format_get_n_components (channel[i].format);
2058 src_bpp = babl_format_get_bytes_per_pixel (src_format);
2059 dest_bpp = babl_format_get_bytes_per_pixel (channel[i].format);
2060
2061 offset = 0;
2062
2063 for (j = 0; j < n_comps; j++)
2064 {
2065 guint32 y;
2066
2067 for (y = 0; y < image_height; y += tile_height)
2068 {
2069 guint32 x;
2070
2071 for (x = 0; x < image_width; x += tile_width)
2072 {
2073 GeglBuffer *src_buf;
2074 GeglBufferIterator *iter;
2075 guint32 rows;
2076 guint32 cols;
2077
2078 gimp_progress_update (progress + one_row *
2079 ((gdouble) x / (gdouble) image_width));
2080
2081 if (TIFFIsTiled (tif))
2082 {
2083 if (TIFFReadTile (tif, buffer, x, y, 0, compindex) == -1)
2084 {
2085 g_message (_("Reading tile failed. Image may be corrupt at line %d."), y);
2086 g_free (buffer);
2087 g_free (bw_buffer);
2088 return;
2089 }
2090 }
2091 else if (TIFFReadScanline (tif, buffer, y, compindex) == -1)
2092 {
2093 /* Error reading scanline, stop loading */
2094 g_message (_("Reading scanline failed. Image may be corrupt at line %d."), y);
2095 g_free (buffer);
2096 g_free (bw_buffer);
2097 return;
2098 }
2099
2100 cols = MIN (image_width - x, tile_width);
2101 rows = MIN (image_height - y, tile_height);
2102
2103 if (needs_upscale)
2104 {
2105 if (bps == 1)
2106 convert_bit2byte (buffer, bw_buffer, cols, rows);
2107 else if (bps == 2)
2108 convert_2bit2byte (buffer, bw_buffer, cols, rows);
2109 else if (bps == 4)
2110 convert_4bit2byte (buffer, bw_buffer, cols, rows);
2111 }
2112 else if (is_signed)
2113 {
2114 convert_int2uint (buffer, bps, 1, cols, rows,
2115 tile_width * bytes_per_pixel);
2116 }
2117
2118 if (tiff_mode == GIMP_TIFF_GRAY_MINISWHITE && bps == 8)
2119 {
2120 convert_miniswhite (buffer, cols, rows);
2121 }
2122
2123 src_buf = gegl_buffer_linear_new_from_data (needs_upscale ? bw_buffer : buffer,
2124 src_format,
2125 GEGL_RECTANGLE (0, 0, cols, rows),
2126 GEGL_AUTO_ROWSTRIDE,
2127 NULL, NULL);
2128
2129 iter = gegl_buffer_iterator_new (src_buf,
2130 GEGL_RECTANGLE (0, 0, cols, rows),
2131 0, NULL,
2132 GEGL_ACCESS_READ,
2133 GEGL_ABYSS_NONE, 2);
2134 gegl_buffer_iterator_add (iter, channel[i].buffer,
2135 GEGL_RECTANGLE (x, y, cols, rows),
2136 0, channel[i].format,
2137 GEGL_ACCESS_READWRITE,
2138 GEGL_ABYSS_NONE);
2139
2140 while (gegl_buffer_iterator_next (iter))
2141 {
2142 guchar *s = iter->items[0].data;
2143 guchar *d = iter->items[1].data;
2144 gint length = iter->length;
2145
2146 d += offset;
2147
2148 while (length--)
2149 {
2150 memcpy (d, s, src_bpp);
2151 d += dest_bpp;
2152 s += src_bpp;
2153 }
2154 }
2155
2156 g_object_unref (src_buf);
2157 }
2158 }
2159
2160 offset += src_bpp;
2161 compindex++;
2162 }
2163
2164 progress += one_row;
2165 }
2166
2167 g_free (buffer);
2168 g_free (bw_buffer);
2169 }
2170
2171
2172 static guchar bit2byte[256 * 8];
2173 static guchar _2bit2byte[256 * 4];
2174 static guchar _4bit2byte[256 * 2];
2175
2176 static const guchar _1_to_8_bitmap [2] =
2177 {
2178 0, 255
2179 };
2180
2181 static const guchar _1_to_8_bitmap_rev [2] =
2182 {
2183 255, 0
2184 };
2185
2186 static const guchar _2_to_8_bitmap [4] =
2187 {
2188 0, 85, 170, 255
2189 };
2190
2191 static const guchar _2_to_8_bitmap_rev [4] =
2192 {
2193 255, 170, 85, 0
2194 };
2195
2196 static const guchar _4_to_8_bitmap [16] =
2197 {
2198 0, 17, 34, 51, 68, 85, 102, 119,
2199 136, 153, 170, 187, 204, 221, 238, 255
2200 };
2201
2202 static const guchar _4_to_8_bitmap_rev [16] =
2203 {
2204 255, 238, 221, 204, 187, 170, 153, 136,
2205 119, 102, 85, 68, 51, 34, 17, 0
2206 };
2207
2208 static void
fill_bit2byte(TiffColorMode tiff_mode)2209 fill_bit2byte (TiffColorMode tiff_mode)
2210 {
2211 static gboolean filled = FALSE;
2212
2213 guchar *dest;
2214 gint i, j;
2215
2216 if (filled)
2217 return;
2218
2219 dest = bit2byte;
2220
2221 if (tiff_mode == GIMP_TIFF_INDEXED)
2222 {
2223 for (j = 0; j < 256; j++)
2224 for (i = 7; i >= 0; i--)
2225 {
2226 *(dest++) = ((j & (1 << i)) != 0);
2227 }
2228 }
2229 else if (tiff_mode != GIMP_TIFF_DEFAULT)
2230 {
2231 guchar *_to_8_bitmap = NULL;
2232
2233 if (tiff_mode == GIMP_TIFF_GRAY)
2234 _to_8_bitmap = (guchar *) &_1_to_8_bitmap;
2235 else if (tiff_mode == GIMP_TIFF_GRAY_MINISWHITE)
2236 _to_8_bitmap = (guchar *) &_1_to_8_bitmap_rev;
2237
2238 for (j = 0; j < 256; j++)
2239 for (i = 7; i >= 0; i--)
2240 {
2241 gint idx;
2242
2243 idx = ((j & (1 << i)) != 0);
2244 *(dest++) = _to_8_bitmap[idx];
2245 }
2246 }
2247
2248 filled = TRUE;
2249 }
2250
2251 static void
fill_2bit2byte(TiffColorMode tiff_mode)2252 fill_2bit2byte (TiffColorMode tiff_mode)
2253 {
2254 static gboolean filled2 = FALSE;
2255
2256 guchar *dest;
2257 gint i, j;
2258
2259 if (filled2)
2260 return;
2261
2262 dest = _2bit2byte;
2263
2264 if (tiff_mode == GIMP_TIFF_INDEXED)
2265 {
2266 for (j = 0; j < 256; j++)
2267 {
2268 for (i = 3; i >= 0; i--)
2269 {
2270 *(dest++) = ((j & (3 << (2*i))) >> (2*i));
2271 }
2272 }
2273 }
2274 else if (tiff_mode != GIMP_TIFF_DEFAULT)
2275 {
2276 guchar *_to_8_bitmap = NULL;
2277
2278 if (tiff_mode == GIMP_TIFF_GRAY)
2279 _to_8_bitmap = (guchar *) &_2_to_8_bitmap;
2280 else if (tiff_mode == GIMP_TIFF_GRAY_MINISWHITE)
2281 _to_8_bitmap = (guchar *) &_2_to_8_bitmap_rev;
2282
2283 for (j = 0; j < 256; j++)
2284 {
2285 for (i = 3; i >= 0; i--)
2286 {
2287 gint idx;
2288
2289 idx = ((j & (3 << (2*i))) >> (2*i));
2290 *(dest++) = _to_8_bitmap[idx];
2291 }
2292 }
2293 }
2294
2295 filled2 = TRUE;
2296 }
2297
2298 static void
fill_4bit2byte(TiffColorMode tiff_mode)2299 fill_4bit2byte (TiffColorMode tiff_mode)
2300 {
2301 static gboolean filled4 = FALSE;
2302
2303 guchar *dest;
2304 gint i, j;
2305
2306 if (filled4)
2307 return;
2308
2309 dest = _4bit2byte;
2310
2311 if (tiff_mode == GIMP_TIFF_INDEXED)
2312 {
2313 for (j = 0; j < 256; j++)
2314 {
2315 for (i = 1; i >= 0; i--)
2316 {
2317 *(dest++) = ((j & (15 << (4*i))) >> (4*i));
2318 }
2319 }
2320 }
2321 else if (tiff_mode != GIMP_TIFF_DEFAULT)
2322 {
2323 guchar *_to_8_bitmap = NULL;
2324
2325 if (tiff_mode == GIMP_TIFF_GRAY)
2326 _to_8_bitmap = (guchar *) &_4_to_8_bitmap;
2327 else if (tiff_mode == GIMP_TIFF_GRAY_MINISWHITE)
2328 _to_8_bitmap = (guchar *) &_4_to_8_bitmap_rev;
2329
2330 for (j = 0; j < 256; j++)
2331 {
2332 for (i = 1; i >= 0; i--)
2333 {
2334 gint idx;
2335
2336 idx = ((j & (15 << (4*i))) >> (4*i));
2337 *(dest++) = _to_8_bitmap[idx];
2338 }
2339 }
2340 }
2341
2342 filled4 = TRUE;
2343 }
2344
2345 static void
convert_bit2byte(const guchar * src,guchar * dest,gint width,gint height)2346 convert_bit2byte (const guchar *src,
2347 guchar *dest,
2348 gint width,
2349 gint height)
2350 {
2351 gint y;
2352
2353 for (y = 0; y < height; y++)
2354 {
2355 gint x = width;
2356
2357 while (x >= 8)
2358 {
2359 memcpy (dest, bit2byte + *src * 8, 8);
2360 dest += 8;
2361 x -= 8;
2362 src++;
2363 }
2364
2365 if (x > 0)
2366 {
2367 memcpy (dest, bit2byte + *src * 8, x);
2368 dest += x;
2369 src++;
2370 }
2371 }
2372 }
2373
2374 static void
convert_2bit2byte(const guchar * src,guchar * dest,gint width,gint height)2375 convert_2bit2byte (const guchar *src,
2376 guchar *dest,
2377 gint width,
2378 gint height)
2379 {
2380 gint y;
2381
2382 for (y = 0; y < height; y++)
2383 {
2384 gint x = width;
2385
2386 while (x >= 4)
2387 {
2388 memcpy (dest, _2bit2byte + *src * 4, 4);
2389 dest += 4;
2390 x -= 4;
2391 src++;
2392 }
2393
2394 if (x > 0)
2395 {
2396 memcpy (dest, _2bit2byte + *src * 4, x);
2397 dest += x;
2398 src++;
2399 }
2400 }
2401 }
2402
2403 static void
convert_4bit2byte(const guchar * src,guchar * dest,gint width,gint height)2404 convert_4bit2byte (const guchar *src,
2405 guchar *dest,
2406 gint width,
2407 gint height)
2408 {
2409 gint y;
2410
2411 for (y = 0; y < height; y++)
2412 {
2413 gint x = width;
2414
2415 while (x >= 2)
2416 {
2417 memcpy (dest, _4bit2byte + *src * 2, 2);
2418 dest += 2;
2419 x -= 2;
2420 src++;
2421 }
2422
2423 if (x > 0)
2424 {
2425 memcpy (dest, _4bit2byte + *src * 2, x);
2426 dest += x;
2427 src++;
2428 }
2429 }
2430 }
2431
2432 static void
convert_miniswhite(guchar * buffer,gint width,gint height)2433 convert_miniswhite (guchar *buffer,
2434 gint width,
2435 gint height)
2436 {
2437 gint y;
2438 guchar *buf = buffer;
2439
2440 for (y = 0; y < height; y++)
2441 {
2442 gint x;
2443
2444 for (x = 0; x < width; x++)
2445 {
2446 *buf = ~*buf;
2447 buf++;
2448 }
2449 }
2450 }
2451
2452 static void
convert_int2uint(guchar * buffer,gint bps,gint spp,gint width,gint height,gint stride)2453 convert_int2uint (guchar *buffer,
2454 gint bps,
2455 gint spp,
2456 gint width,
2457 gint height,
2458 gint stride)
2459 {
2460 gint bytes_per_pixel = bps / 8;
2461 gint y;
2462
2463 for (y = 0; y < height; y++)
2464 {
2465 guchar *d = buffer + stride * y;
2466 gint x;
2467
2468 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
2469 d += bytes_per_pixel - 1;
2470 #endif
2471
2472 for (x = 0; x < width * spp; x++)
2473 {
2474 *d ^= 0x80;
2475
2476 d += bytes_per_pixel;
2477 }
2478 }
2479 }
2480
2481 static gboolean
load_dialog(TIFF * tif,const gchar * help_id,TiffSelectedPages * pages,const gchar * extra_message,DefaultExtra * default_extra)2482 load_dialog (TIFF *tif,
2483 const gchar *help_id,
2484 TiffSelectedPages *pages,
2485 const gchar *extra_message,
2486 DefaultExtra *default_extra)
2487 {
2488 GtkWidget *dialog;
2489 GtkWidget *vbox;
2490 GtkWidget *selector = NULL;
2491 GtkWidget *crop_option = NULL;
2492 GtkWidget *extra_radio = NULL;
2493 gboolean run;
2494
2495 dialog = gimp_dialog_new (_("Import from TIFF"), PLUG_IN_ROLE,
2496 NULL, 0,
2497 gimp_standard_help_func, help_id,
2498
2499 _("_Cancel"), GTK_RESPONSE_CANCEL,
2500 _("_Import"), GTK_RESPONSE_OK,
2501
2502 NULL);
2503
2504 gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
2505 GTK_RESPONSE_OK,
2506 GTK_RESPONSE_CANCEL,
2507 -1);
2508
2509 gimp_window_set_transient (GTK_WINDOW (dialog));
2510
2511 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
2512 gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
2513 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
2514 vbox, TRUE, TRUE, 0);
2515
2516 if (pages->n_pages > 1)
2517 {
2518 gint i, j;
2519
2520 /* Page Selector */
2521 selector = gimp_page_selector_new ();
2522 gtk_widget_set_size_request (selector, 300, 200);
2523 gtk_box_pack_start (GTK_BOX (vbox), selector, TRUE, TRUE, 0);
2524
2525 gimp_page_selector_set_n_pages (GIMP_PAGE_SELECTOR (selector),
2526 pages->n_filtered_pages);
2527 gimp_page_selector_set_target (GIMP_PAGE_SELECTOR (selector), pages->target);
2528
2529 for (i = 0, j = 0; i < pages->n_pages && j < pages->n_filtered_pages; i++)
2530 {
2531 if (pages->filtered_pages[i] != -1)
2532 {
2533 const gchar *name = tiff_get_page_name (tif);
2534
2535 if (name)
2536 gimp_page_selector_set_page_label (GIMP_PAGE_SELECTOR (selector),
2537 j, name);
2538 j++;
2539 }
2540
2541 TIFFReadDirectory (tif);
2542 }
2543
2544 g_signal_connect_swapped (selector, "activate",
2545 G_CALLBACK (gtk_window_activate_default),
2546 dialog);
2547
2548 /* Option to shrink the loaded image to its bounding box
2549 or keep as much empty space as possible.
2550 Note that there seems to be no way to keep the empty
2551 space on the right and bottom. */
2552 crop_option = gtk_check_button_new_with_mnemonic (_("_Keep empty space around imported layers"));
2553 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (crop_option),
2554 pages->keep_empty_space);
2555 gtk_box_pack_start (GTK_BOX (vbox), crop_option, TRUE, TRUE, 0);
2556 }
2557
2558 if (extra_message)
2559 {
2560 GtkWidget *warning;
2561
2562 warning = g_object_new (GIMP_TYPE_HINT_BOX,
2563 "icon-name", GIMP_ICON_DIALOG_WARNING,
2564 "hint", extra_message,
2565 NULL);
2566 gtk_box_pack_start (GTK_BOX (vbox), warning, TRUE, TRUE, 0);
2567 gtk_widget_show (warning);
2568
2569 extra_radio = gimp_int_radio_group_new (TRUE, _("Process extra channel as:"),
2570 (GCallback) gimp_radio_button_update,
2571 default_extra, GIMP_TIFF_LOAD_UNASSALPHA,
2572 _("_Non-premultiplied alpha"), GIMP_TIFF_LOAD_UNASSALPHA, NULL,
2573 _("Pre_multiplied alpha"), GIMP_TIFF_LOAD_ASSOCALPHA, NULL,
2574 _("Channe_l"), GIMP_TIFF_LOAD_CHANNEL, NULL,
2575 NULL);
2576 gtk_box_pack_start (GTK_BOX (vbox), extra_radio, TRUE, TRUE, 0);
2577 gtk_widget_show (extra_radio);
2578 }
2579
2580 /* Setup done; display the dialog */
2581 gtk_widget_show_all (dialog);
2582
2583 /* run the dialog */
2584 run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
2585
2586 if (run)
2587 {
2588 if (pages->n_pages > 1)
2589 {
2590 pages->target =
2591 gimp_page_selector_get_target (GIMP_PAGE_SELECTOR (selector));
2592
2593 pages->pages =
2594 gimp_page_selector_get_selected_pages (GIMP_PAGE_SELECTOR (selector),
2595 &pages->n_pages);
2596
2597 pages->keep_empty_space =
2598 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (crop_option));
2599
2600 /* select all if none selected */
2601 if (pages->n_pages == 0)
2602 {
2603 gimp_page_selector_select_all (GIMP_PAGE_SELECTOR (selector));
2604
2605 pages->pages =
2606 gimp_page_selector_get_selected_pages (GIMP_PAGE_SELECTOR (selector),
2607 &pages->n_pages);
2608 }
2609 }
2610 }
2611
2612 return run;
2613 }
2614