Debian Codegraph @

TIFF/imtiff.c

raw
1 #include <tiffio.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <errno.h>
5 #include "imtiff.h"
6 #include "imext.h"
7
8 /* 4.3.0 (aka 20210416) made the old tifflib specific types deprecated in
9 favour of the C99 <inttypes.h> types.
10 */
11 #if TIFFLIB_VERSION >= 20210416
12 typedef uint8_t tf_uint8;
13 typedef uint16_t tf_uint16;
14 typedef uint32_t tf_uint32;
15 #else
16 typedef uint8 tf_uint8;
17 typedef uint16 tf_uint16;
18 typedef uint32 tf_uint32;
19 #endif
20
21 #if TIFFLIB_VERSION >= 20221213
22 # define USE_TIFFOPEN_OPTIONS
23 #endif
24
25 #if TIFFLIB_VERSION >= 20241029
26 # define USE_SET_WARN_ABOUT_UNKNOWN_TAGS
27 #endif
28
29 /*
30 =head1 NAME
31
32 tiff.c - implements reading and writing tiff files, uses io layer.
33
34 =head1 SYNOPSIS
35
36 io_glue *ig = io_new_fd( fd );
37 i_img *im = i_readtiff_wiol(ig, -1); // no limit on how much is read
38 // or
39 io_glue *ig = io_new_fd( fd );
40 return_code = i_writetiff_wiol(im, ig);
41
42 =head1 DESCRIPTION
43
44 tiff.c implements the basic functions to read and write tiff files.
45 It uses the iolayer and needs either a seekable source or an entire
46 memory mapped buffer.
47
48 =head1 FUNCTION REFERENCE
49
50 Some of these functions are internal.
51
52 =over
53
54 =cut
55 */
56
57 #define CLAMP8(x) ((x) < 0 ? 0 : (x) > 255 ? 255 : (x))
58 #define CLAMP16(x) ((x) < 0 ? 0 : (x) > 65535 ? 65535 : (x))
59
60 #define Sample16To8(num) ((num) / 257)
61
62 struct tag_name {
63 const char *name;
64 tf_uint32 tag;
65 };
66
67 static i_img *read_one_rgb_tiled(TIFF *tif, i_img_dim width, i_img_dim height, int allow_incomplete);
68 static i_img *read_one_rgb_lines(TIFF *tif, i_img_dim width, i_img_dim height, int allow_incomplete);
69
70 static const struct tag_name
71 text_tag_names[] =
72 {
73 { "tiff_documentname", TIFFTAG_DOCUMENTNAME, },
74 { "tiff_imagedescription", TIFFTAG_IMAGEDESCRIPTION, },
75 { "tiff_make", TIFFTAG_MAKE, },
76 { "tiff_model", TIFFTAG_MODEL, },
77 { "tiff_pagename", TIFFTAG_PAGENAME, },
78 { "tiff_software", TIFFTAG_SOFTWARE, },
79 { "tiff_datetime", TIFFTAG_DATETIME, },
80 { "tiff_artist", TIFFTAG_ARTIST, },
81 { "tiff_hostcomputer", TIFFTAG_HOSTCOMPUTER, },
82 };
83
84 static const struct tag_name
85 compress_values[] =
86 {
87 { "none", COMPRESSION_NONE },
88 { "ccittrle", COMPRESSION_CCITTRLE },
89 { "fax3", COMPRESSION_CCITTFAX3 },
90 { "t4", COMPRESSION_CCITTFAX3 },
91 { "fax4", COMPRESSION_CCITTFAX4 },
92 { "t6", COMPRESSION_CCITTFAX4 },
93 { "lzw", COMPRESSION_LZW },
94 { "oldjpeg", COMPRESSION_OJPEG },
95 { "jpeg", COMPRESSION_JPEG },
96 { "next", COMPRESSION_NEXT },
97 { "packbits", COMPRESSION_PACKBITS },
98 { "thunder", COMPRESSION_THUNDERSCAN },
99 { "deflate", COMPRESSION_ADOBE_DEFLATE },
100 { "zip", COMPRESSION_ADOBE_DEFLATE },
101 { "pixarlog", COMPRESSION_PIXARLOG },
102 { "oldzip", COMPRESSION_DEFLATE },
103 { "ccittrlew", COMPRESSION_CCITTRLEW },
104 { "jbig", COMPRESSION_JBIG },
105 { "sgilog", COMPRESSION_SGILOG },
106 { "sgilog24", COMPRESSION_SGILOG24 },
107 #ifdef COMPRESSION_LERC
108 { "lerc", COMPRESSION_LERC },
109 #endif
110 { "lzma", COMPRESSION_LZMA },
111 #ifdef COMPRESSION_ZSTD
112 { "zstd", COMPRESSION_ZSTD },
113 #endif
114 #ifdef COMPRESSION_WEBP
115 { "webp", COMPRESSION_WEBP },
116 #endif
117 };
118
119 static const int compress_value_count =
120 sizeof(compress_values) / sizeof(*compress_values);
121
122 static const struct tag_name
123 sample_format_values[] =
124 {
125 { "uint", SAMPLEFORMAT_UINT },
126 { "int", SAMPLEFORMAT_INT },
127 { "ieeefp", SAMPLEFORMAT_IEEEFP },
128 { "undefined", SAMPLEFORMAT_VOID },
129 };
130
131 static const int sample_format_value_count =
132 sizeof(sample_format_values) / sizeof(*sample_format_values);
133
134 typedef struct read_state_tag read_state_t;
135 /* the setup function creates the image object, allocates the line buffer */
136 typedef int (*read_setup_t)(read_state_t *state);
137
138 /* the putter writes the image data provided by the getter to the
139 image, x, y, width, height describe the target area of the image,
140 extras is the extra number of pixels stored for each scanline in
141 the raster buffer, (for tiles against the right side of the
142 image) */
143
144 typedef int (*read_putter_t)(read_state_t *state, i_img_dim x, i_img_dim y,
145 i_img_dim width, i_img_dim height, int extras);
146
147 /* reads from a tiled or strip image and calls the putter.
148 This may need a second type for handling non-contiguous images
149 at some point */
150 typedef int (*read_getter_t)(read_state_t *state, read_putter_t putter);
151
152 struct read_state_tag {
153 TIFF *tif;
154 i_img *img;
155 void *raster;
156 i_img_dim pixels_read;
157 int allow_incomplete;
158 void *line_buf;
159 tf_uint32 width, height;
160 tf_uint16 bits_per_sample;
161 tf_uint16 photometric;
162
163 /* the total number of channels (samples per pixel) */
164 int samples_per_pixel;
165
166 /* if non-zero, which channel is the alpha channel, typically 3 for rgb */
167 int alpha_chan;
168
169 /* whether or not to scale the color channels based on the alpha
170 channel. TIFF has 2 types of alpha channel, if the alpha channel
171 we use is EXTRASAMPLE_ASSOCALPHA then the color data will need to
172 be scaled to match Imager's conventions */
173 int scale_alpha;
174
175 /* number of color samples (not including alpha) */
176 int color_channels;
177
178 /* SampleFormat is 2 */
179 int sample_signed;
180
181 int sample_format;
182 };
183
184 static int tile_contig_getter(read_state_t *state, read_putter_t putter);
185 static int strip_contig_getter(read_state_t *state, read_putter_t putter);
186
187 static int setup_paletted(read_state_t *state);
188 static int paletted_putter8(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
189 static int paletted_putter4(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
190
191 static int setup_16_rgb(read_state_t *state);
192 static int setup_16_grey(read_state_t *state);
193 static int putter_16(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
194
195 static int setup_8_rgb(read_state_t *state);
196 static int setup_8_grey(read_state_t *state);
197 static int putter_8(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
198
199 static int setup_32_rgb(read_state_t *state);
200 static int setup_32_grey(read_state_t *state);
201 static int putter_32(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
202
203 static int setup_bilevel(read_state_t *state);
204 static int putter_bilevel(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
205
206 static int setup_cmyk8(read_state_t *state);
207 static int putter_cmyk8(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
208
209 static int setup_cmyk16(read_state_t *state);
210 static int putter_cmyk16(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
211 static void
212 rgb_channels(read_state_t *state, int *out_channels);
213 static void
214 grey_channels(read_state_t *state, int *out_channels);
215 static void
216 cmyk_channels(read_state_t *state, int *out_channels);
217 static void
218 fallback_rgb_channels(TIFF *tif, int *channels, int *alpha_chan);
219
220 static const int text_tag_count =
221 sizeof(text_tag_names) / sizeof(*text_tag_names);
222
223 #define TIFFIO_MAGIC 0xC6A340CC
224
225 typedef struct {
226 unsigned magic;
227 io_glue *ig;
228 io_glue *warn_buf;
229 } tiffio_context_t;
230
231 static void
232 do_warn_handler(tiffio_context_t *c, const char *module, const char *fmt, va_list ap) {
233 char buf[200];
234
235 if (c->magic != TIFFIO_MAGIC)
236 return;
237
238 buf[0] = '\0';
239 vsnprintf(buf, sizeof(buf), fmt, ap);
240 mm_log((1, "tiff warning %s\n", buf));
241
242 if (!c->warn_buf)
243 c->warn_buf = io_new_bufchain();
244
245 i_io_write(c->warn_buf, module, strlen(module));
246 i_io_write(c->warn_buf, ": ", 2);
247 i_io_write(c->warn_buf, buf, strlen(buf));
248 i_io_write(c->warn_buf, "\n", 1);
249 }
250
251 #ifdef USE_TIFFOPEN_OPTIONS
252
253 static int
254 error_handler_extr(TIFF *tif, void *user_data, const char *module,
255 const char *fmt, va_list ap) {
256 (void)tif;
257 (void)user_data;
258 (void)module;
259 mm_log((1, "tiff error fmt %s\n", fmt));
260 i_push_errorvf(0, fmt, ap);
261 return 1;
262 }
263
264 static int
265 warn_handler_extr(TIFF *tif, void *user_data, const char *module,
266 const char *fmt, va_list ap) {
267 tiffio_context_t *c = (tiffio_context_t *)user_data;
268
269 (void)tif;
270
271 do_warn_handler(c, module, fmt, ap);
272
273 return 1;
274 }
275
276 #else
277
278 static void
279 error_handler(char const *module, char const *fmt, va_list ap) {
280 (void)module;
281 mm_log((1, "tiff error fmt %s\n", fmt));
282 i_push_errorvf(0, fmt, ap);
283 }
284
285 static void
286 warn_handler_ex(thandle_t h, const char *module, const char *fmt, va_list ap) {
287 tiffio_context_t *c = (tiffio_context_t *)h;
288
289 do_warn_handler(c, module, fmt, ap);
290 }
291
292 #endif
293
294 static void
295 tiffio_context_init(tiffio_context_t *c, io_glue *ig);
296 static void
297 tiffio_context_final(tiffio_context_t *c);
298
299 static toff_t
300 sizeproc(thandle_t h) {
301 io_glue *ig = ((tiffio_context_t *)h)->ig;
302
303 /* iolayer doesn't have a size callback, use seek() to find
304 the end
305 */
306 off_t orig_off = i_io_seek(ig, 0, SEEK_CUR);
307 if (orig_off < 0) {
308 i_push_error(errno, "seek to current failed");
309 return -1;
310 }
311 off_t size = i_io_seek(ig, 0, SEEK_END);
312 if (size < 0) {
313 i_push_error(errno, "seek to end failed");
314 return -1;
315 }
316 if (i_io_seek(ig, orig_off, SEEK_SET) < 0) {
317 i_push_error(errno, "seek restore failed");
318 return -1;
319 }
320 return size;
321 }
322
323
324 /*
325 =item comp_seek(h, o, w)
326
327 Compatability for 64 bit systems like latest freebsd (internal)
328
329 h - tiff handle, cast an io_glue object
330 o - offset
331 w - whence
332
333 =cut
334 */
335
336 static
337 toff_t
338 comp_seek(thandle_t h, toff_t o, int w) {
339 io_glue *ig = ((tiffio_context_t *)h)->ig;
340 return (toff_t) i_io_seek(ig, o, w);
341 }
342
343 /*
344 =item comp_mmap(thandle_t, tdata_t*, toff_t*)
345
346 Dummy mmap stub.
347
348 This shouldn't ever be called but newer tifflibs want it anyway.
349
350 =cut
351 */
352
353 static
354 int
355 comp_mmap(thandle_t h, tdata_t*p, toff_t*off) {
356 (void)h;
357 (void)p;
358 (void)off;
359 return -1;
360 }
361
362 /*
363 =item comp_munmap(thandle_t h, tdata_t p, toff_t off)
364
365 Dummy munmap stub.
366
367 This shouldn't ever be called but newer tifflibs want it anyway.
368
369 =cut
370 */
371
372 static void
373 comp_munmap(thandle_t h, tdata_t p, toff_t off) {
374 (void)h;
375 (void)p;
376 (void)off;
377 /* do nothing */
378 }
379
380 static tsize_t
381 comp_read(thandle_t h, tdata_t p, tsize_t size) {
382 return i_io_read(((tiffio_context_t *)h)->ig, p, size);
383 }
384
385 static tsize_t
386 comp_write(thandle_t h, tdata_t p, tsize_t size) {
387 return i_io_write(((tiffio_context_t *)h)->ig, p, size);
388 }
389
390 static int
391 comp_close(thandle_t h) {
392 return i_io_close(((tiffio_context_t *)h)->ig);
393 }
394
395 #ifndef USE_TIFFOPEN_OPTIONS
396 static i_mutex_t mutex;
397 #endif
398
399 typedef struct {
400 TIFF *tif;
401 tiffio_context_t ctx;
402 #ifndef USE_TIFFOPEN_OPTIONS
403 TIFFErrorHandler old_error;
404 TIFFErrorHandler old_warn;
405 TIFFErrorHandlerExt old_warn_ext;
406 #endif
407 } tiff_state;
408
409 static TIFF *
410 do_tiff_open(tiff_state *state, io_glue *ig, const char *mode) {
411 memset(state, 0, sizeof(*state));
412 tiffio_context_init(&state->ctx, ig);
413 #ifdef USE_TIFFOPEN_OPTIONS
414 TIFFOpenOptions *options = TIFFOpenOptionsAlloc();
415 #ifdef USE_SET_WARN_ABOUT_UNKNOWN_TAGS
416 TIFFOpenOptionsSetWarnAboutUnknownTags(options, 1);
417 #endif
418 TIFFOpenOptionsSetErrorHandlerExtR(options, error_handler_extr, &state->ctx);
419 TIFFOpenOptionsSetWarningHandlerExtR(options, warn_handler_extr, &state->ctx);
420 TIFF *tif = TIFFClientOpenExt("(Iolayer)",
421 mode,
422 (thandle_t) &state->ctx,
423 comp_read,
424 comp_write,
425 comp_seek,
426 comp_close,
427 sizeproc,
428 comp_mmap,
429 comp_munmap,
430 options);
431 TIFFOpenOptionsFree(options);
432 #else
433 i_mutex_lock(mutex);
434
435 state->old_error = TIFFSetErrorHandler(error_handler);
436 state->old_warn = TIFFSetWarningHandler(NULL);
437 state->old_warn_ext = TIFFSetWarningHandlerExt(warn_handler_ex);
438
439 TIFF *tif = TIFFClientOpen("(Iolayer)",
440 mode,
441 (thandle_t) &state->ctx,
442 comp_read,
443 comp_write,
444 comp_seek,
445 comp_close,
446 sizeproc,
447 comp_mmap,
448 comp_munmap);
449 if (!tif) {
450 TIFFSetErrorHandler(state->old_error);
451 TIFFSetWarningHandler(state->old_warn);
452 TIFFSetWarningHandlerExt(state->old_warn_ext);
453 i_mutex_unlock(mutex);
454 }
455 #endif
456 if (!tif) {
457 tiffio_context_final(&state->ctx);
458 }
459 state->tif = tif;
460
461 return tif;
462 }
463
464 static void
465 do_tiff_close(tiff_state *state) {
466 TIFFClose(state->tif);
467 #ifndef USE_TIFFOPEN_OPTIONS
468 TIFFSetErrorHandler(state->old_error);
469 TIFFSetWarningHandler(state->old_warn);
470 TIFFSetWarningHandlerExt(state->old_warn_ext);
471 i_mutex_unlock(mutex);
472 #endif
473 tiffio_context_final(&state->ctx);
474 }
475
476 void
477 i_tiff_init(void) {
478 #ifndef USE_TIFFOPEN_OPTIONS
479 mutex = i_mutex_new();
480 #endif
481 }
482
483 static int save_tiff_tags(TIFF *tif, i_img *im);
484
485 static void
486 pack_4bit_to(unsigned char *dest, const unsigned char *src, i_img_dim count);
487
488
489 static i_img *read_one_tiff(TIFF *tif, int allow_incomplete) {
490 i_img *im;
491 tf_uint32 width, height;
492 tf_uint16 samples_per_pixel;
493 int tiled;
494 float xres, yres;
495 tf_uint16 resunit;
496 int gotXres, gotYres;
497 tf_uint16 photometric;
498 tf_uint16 bits_per_sample;
499 tf_uint16 planar_config;
500 tf_uint16 inkset;
501 tf_uint16 compress;
502 tf_uint16 sample_format;
503 int i;
504 read_state_t state;
505 read_setup_t setupf = NULL;
506 read_getter_t getterf = NULL;
507 read_putter_t putterf = NULL;
508 int channels = MAXCHANNELS;
509 size_t sample_size = ~0; /* force failure if some code doesn't set it */
510 i_img_dim total_pixels;
511 int samples_integral;
512
513 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
514 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
515 TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
516 tiled = TIFFIsTiled(tif);
517 TIFFGetFieldDefaulted(tif, TIFFTAG_PHOTOMETRIC, &photometric);
518 TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
519 TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planar_config);
520 TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
521
522 if (samples_per_pixel == 0) {
523 i_push_error(0, "invalid image: SamplesPerPixel is 0");
524 return NULL;
525 }
526
527 TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLEFORMAT, &sample_format);
528
529 mm_log((1, "i_readtiff_wiol: width=%d, height=%d, channels=%d\n", width, height, samples_per_pixel));
530 mm_log((1, "i_readtiff_wiol: %stiled\n", tiled?"":"not "));
531 mm_log((1, "i_readtiff_wiol: %sbyte swapped\n", TIFFIsByteSwapped(tif)?"":"not "));
532
533 total_pixels = (i_img_dim)width * height;
534 memset(&state, 0, sizeof(state));
535 state.tif = tif;
536 state.allow_incomplete = allow_incomplete;
537 state.width = width;
538 state.height = height;
539 state.bits_per_sample = bits_per_sample;
540 state.samples_per_pixel = samples_per_pixel;
541 state.photometric = photometric;
542 state.sample_signed = sample_format == SAMPLEFORMAT_INT;
543 state.sample_format = sample_format;
544
545 samples_integral = sample_format == SAMPLEFORMAT_UINT
546 || sample_format == SAMPLEFORMAT_INT
547 || sample_format == SAMPLEFORMAT_VOID; /* sample as UINT */
548
549 /* yes, this if() is horrible */
550 if (photometric == PHOTOMETRIC_PALETTE && bits_per_sample <= 8
551 && samples_integral) {
552 setupf = setup_paletted;
553 if (bits_per_sample == 8)
554 putterf = paletted_putter8;
555 else if (bits_per_sample == 4)
556 putterf = paletted_putter4;
557 else
558 mm_log((1, "unsupported paletted bits_per_sample %d\n", bits_per_sample));
559
560 sample_size = sizeof(i_sample_t);
561 channels = 1;
562 }
563 else if (bits_per_sample == 16
564 && photometric == PHOTOMETRIC_RGB
565 && samples_per_pixel >= 3
566 && samples_integral) {
567 setupf = setup_16_rgb;
568 putterf = putter_16;
569 sample_size = 2;
570 rgb_channels(&state, &channels);
571 }
572 else if (bits_per_sample == 16
573 && photometric == PHOTOMETRIC_MINISBLACK
574 && samples_integral) {
575 setupf = setup_16_grey;
576 putterf = putter_16;
577 sample_size = 2;
578 grey_channels(&state, &channels);
579 }
580 else if (bits_per_sample == 8
581 && photometric == PHOTOMETRIC_MINISBLACK
582 && samples_integral) {
583 setupf = setup_8_grey;
584 putterf = putter_8;
585 sample_size = 1;
586 grey_channels(&state, &channels);
587 }
588 else if (bits_per_sample == 8
589 && photometric == PHOTOMETRIC_RGB
590 && samples_integral) {
591 setupf = setup_8_rgb;
592 putterf = putter_8;
593 sample_size = 1;
594 rgb_channels(&state, &channels);
595 }
596 else if (bits_per_sample == 32
597 && photometric == PHOTOMETRIC_RGB
598 && samples_per_pixel >= 3) {
599 setupf = setup_32_rgb;
600 putterf = putter_32;
601 sample_size = sizeof(i_fsample_t);
602 rgb_channels(&state, &channels);
603 }
604 else if (bits_per_sample == 32
605 && photometric == PHOTOMETRIC_MINISBLACK) {
606 setupf = setup_32_grey;
607 putterf = putter_32;
608 sample_size = sizeof(i_fsample_t);
609 grey_channels(&state, &channels);
610 }
611 else if (bits_per_sample == 1
612 && (photometric == PHOTOMETRIC_MINISBLACK
613 || photometric == PHOTOMETRIC_MINISWHITE)
614 && samples_per_pixel == 1) {
615 setupf = setup_bilevel;
616 putterf = putter_bilevel;
617 sample_size = sizeof(i_palidx);
618 channels = 1;
619 }
620 else if (bits_per_sample == 8
621 && photometric == PHOTOMETRIC_SEPARATED
622 && inkset == INKSET_CMYK
623 && samples_per_pixel >= 4
624 && samples_integral) {
625 setupf = setup_cmyk8;
626 putterf = putter_cmyk8;
627 sample_size = 1;
628 cmyk_channels(&state, &channels);
629 }
630 else if (bits_per_sample == 16
631 && photometric == PHOTOMETRIC_SEPARATED
632 && inkset == INKSET_CMYK
633 && samples_per_pixel >= 4
634 && samples_integral) {
635 setupf = setup_cmyk16;
636 putterf = putter_cmyk16;
637 sample_size = 2;
638 cmyk_channels(&state, &channels);
639 }
640 else {
641 int alpha;
642 fallback_rgb_channels(tif, &channels, &alpha);
643 sample_size = 1;
644 }
645
646 if (!i_int_check_image_file_limits(width, height, channels, sample_size)) {
647 return NULL;
648 }
649
650 if (tiled) {
651 if (planar_config == PLANARCONFIG_CONTIG)
652 getterf = tile_contig_getter;
653 }
654 else {
655 if (planar_config == PLANARCONFIG_CONTIG)
656 getterf = strip_contig_getter;
657 }
658 if (setupf && getterf && putterf) {
659
660 if (!setupf(&state))
661 return NULL;
662 if (!getterf(&state, putterf) || !state.pixels_read) {
663 if (state.img)
664 i_img_destroy(state.img);
665 if (state.raster)
666 _TIFFfree(state.raster);
667 if (state.line_buf)
668 myfree(state.line_buf);
669
670 return NULL;
671 }
672
673 if (allow_incomplete && state.pixels_read < total_pixels) {
674 i_tags_setn(&(state.img->tags), "i_incomplete", 1);
675 i_tags_setn(&(state.img->tags), "i_lines_read",
676 state.pixels_read / width);
677 }
678 im = state.img;
679
680 if (state.raster)
681 _TIFFfree(state.raster);
682 if (state.line_buf)
683 myfree(state.line_buf);
684 }
685 else {
686 if (tiled) {
687 im = read_one_rgb_tiled(tif, width, height, allow_incomplete);
688 }
689 else {
690 im = read_one_rgb_lines(tif, width, height, allow_incomplete);
691 }
692 }
693
694 if (!im)
695 return NULL;
696
697 /* general metadata */
698 i_tags_setn(&im->tags, "tiff_bitspersample", bits_per_sample);
699 i_tags_setn(&im->tags, "tiff_photometric", photometric);
700 TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
701
702 /* resolution tags */
703 TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &resunit);
704 gotXres = TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres);
705 gotYres = TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres);
706 if (gotXres || gotYres) {
707 if (!gotXres)
708 xres = yres;
709 else if (!gotYres)
710 yres = xres;
711 i_tags_setn(&im->tags, "tiff_resolutionunit", resunit);
712 if (resunit == RESUNIT_CENTIMETER) {
713 /* from dots per cm to dpi */
714 xres *= 2.54;
715 yres *= 2.54;
716 i_tags_set(&im->tags, "tiff_resolutionunit_name", "centimeter", -1);
717 }
718 else if (resunit == RESUNIT_NONE) {
719 i_tags_setn(&im->tags, "i_aspect_only", 1);
720 i_tags_set(&im->tags, "tiff_resolutionunit_name", "none", -1);
721 }
722 else if (resunit == RESUNIT_INCH) {
723 i_tags_set(&im->tags, "tiff_resolutionunit_name", "inch", -1);
724 }
725 else {
726 i_tags_set(&im->tags, "tiff_resolutionunit_name", "unknown", -1);
727 }
728 /* tifflib doesn't seem to provide a way to get to the original rational
729 value of these, which would let me provide a more reasonable
730 precision. So make up a number. */
731 i_tags_set_float2(&im->tags, "i_xres", 0, xres, 6);
732 i_tags_set_float2(&im->tags, "i_yres", 0, yres, 6);
733 }
734
735 /* Text tags */
736 for (i = 0; i < text_tag_count; ++i) {
737 char *data;
738 if (TIFFGetField(tif, text_tag_names[i].tag, &data)) {
739 mm_log((1, "i_readtiff_wiol: tag %d has value %s\n",
740 text_tag_names[i].tag, data));
741 i_tags_set(&im->tags, text_tag_names[i].name, data, -1);
742 }
743 }
744
745 i_tags_set(&im->tags, "i_format", "tiff", 4);
746 {
747 tiffio_context_t *ctx = TIFFClientdata(tif);
748 if (ctx->warn_buf) {
749 i_io_flush(ctx->warn_buf);
750 unsigned char *data = NULL;
751 size_t len = io_slurp(ctx->warn_buf, &data);
752
753 i_tags_set(&im->tags, "i_warning", (const char *)data, len);
754 io_glue_destroy(ctx->warn_buf);
755 myfree(data);
756 ctx->warn_buf = NULL;
757 }
758 }
759
760 for (i = 0; i < compress_value_count; ++i) {
761 if (compress_values[i].tag == compress) {
762 i_tags_set(&im->tags, "tiff_compression", compress_values[i].name, -1);
763 break;
764 }
765 }
766
767 if (TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &sample_format)) {
768 /* only set the tag if the the TIFF tag is present */
769 i_tags_setn(&im->tags, "tiff_sample_format", sample_format);
770
771 for (i = 0; i < sample_format_value_count; ++i) {
772 if (sample_format_values[i].tag == sample_format) {
773 i_tags_set(&im->tags, "tiff_sample_format_name",
774 sample_format_values[i].name, -1);
775 break;
776 }
777 }
778 }
779
780 return im;
781 }
782
783 /*
784 =item i_readtiff_wiol(ig, allow_incomplete, page)
785
786 =cut
787 */
788 i_img*
789 i_readtiff_wiol(io_glue *ig, int allow_incomplete, int page) {
790 int current_page;
791
792 i_clear_error();
793
794 /* Add code to get the filename info from the iolayer */
795 /* Also add code to check for mmapped code */
796
797 mm_log((1, "i_readtiff_wiol(ig %p, allow_incomplete %d, page %d)\n", ig, allow_incomplete, page));
798
799 tiff_state ts;
800 TIFF *tif = do_tiff_open(&ts, ig, "rm");
801
802 if (!tif) {
803 mm_log((1, "i_readtiff_wiol: Unable to open tif file\n"));
804 i_push_error(0, "Error opening file");
805 return NULL;
806 }
807
808 for (current_page = 0; current_page < page; ++current_page) {
809 if (!TIFFReadDirectory(tif)) {
810 mm_log((1, "i_readtiff_wiol: Unable to switch to directory %d\n", page));
811 i_push_errorf(0, "could not switch to page %d", page);
812 goto fail;
813 }
814 }
815
816 i_img *im = read_one_tiff(tif, allow_incomplete);
817
818 if (TIFFLastDirectory(tif))
819 mm_log((1, "Last directory of tiff file\n"));
820
821 do_tiff_close(&ts);
822
823 return im;
824
825 fail:
826 do_tiff_close(&ts);
827 return NULL;
828 }
829
830 /*
831 =item i_readtiff_multi_wiol(ig, *count)
832
833 Reads multiple images from a TIFF.
834
835 =cut
836 */
837 i_img**
838 i_readtiff_multi_wiol(io_glue *ig, int *count) {
839 i_img **results = NULL;
840 int result_alloc = 0;
841
842
843 i_clear_error();
844
845 /* Add code to get the filename info from the iolayer */
846 /* Also add code to check for mmapped code */
847
848 mm_log((1, "i_readtiff_wiol(ig %p)\n", ig));
849
850 tiff_state ts;
851 TIFF *tif = do_tiff_open(&ts, ig, "rm");
852 if (!tif) {
853 mm_log((1, "i_readtiff_wiol: Unable to open tif file\n"));
854 i_push_error(0, "Error opening file");
855 return NULL;
856 }
857
858 *count = 0;
859 do {
860 i_img *im = read_one_tiff(tif, 0);
861 if (!im)
862 break;
863 if (++*count > result_alloc) {
864 if (result_alloc == 0) {
865 result_alloc = 5;
866 results = mymalloc(result_alloc * sizeof(i_img *));
867 }
868 else {
869 i_img **newresults;
870 result_alloc *= 2;
871 newresults = myrealloc(results, result_alloc * sizeof(i_img *));
872 if (!newresults) {
873 i_img_destroy(im); /* don't leak it */
874 break;
875 }
876 results = newresults;
877 }
878 }
879 results[*count-1] = im;
880 } while (TIFFReadDirectory(tif));
881
882 do_tiff_close(&ts);
883
884 return results;
885 }
886
887 undef_int
888 i_writetiff_low_faxable(TIFF *tif, i_img *im, int fine) {
889 tf_uint32 width, height;
890 tf_uint32 y;
891 int rc;
892 tf_uint32 x;
893 tf_uint32 rowsperstrip;
894 float vres = fine ? 196 : 98;
895 int luma_chan;
896
897 width = im->xsize;
898 height = im->ysize;
899
900 if ((i_img_dim)width != im->xsize || (i_img_dim)height != im->ysize) {
901 i_push_error(0, "image too large for TIFF");
902 return 0;
903 }
904
905 switch (im->channels) {
906 case 1:
907 case 2:
908 luma_chan = 0;
909 break;
910 case 3:
911 case 4:
912 luma_chan = 1;
913 break;
914 default:
915 /* This means a colorspace we don't handle yet */
916 mm_log((1, "i_writetiff_wiol_faxable: don't handle %d channel images.\n", im->channels));
917 return 0;
918 }
919
920 /* Add code to get the filename info from the iolayer */
921 /* Also add code to check for mmapped code */
922
923
924 mm_log((1, "i_writetiff_wiol_faxable: width=%d, height=%d, channels=%d\n", width, height, im->channels));
925
926 if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width) )
927 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField width=%d failed\n", width)); return 0; }
928 if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height) )
929 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField length=%d failed\n", height)); return 0; }
930 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (tf_uint16)1U))
931 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField samplesperpixel=1 failed\n")); return 0; }
932 if (!TIFFSetField(tif, TIFFTAG_ORIENTATION, (tf_uint16)ORIENTATION_TOPLEFT))
933 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Orientation=topleft\n")); return 0; }
934 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, (tf_uint16)1U) )
935 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField bitpersample=1\n")); return 0; }
936 if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, (tf_uint16)PLANARCONFIG_CONTIG))
937 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField planarconfig\n")); return 0; }
938 if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, (tf_uint16)PHOTOMETRIC_MINISWHITE))
939 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField photometric=%d\n", PHOTOMETRIC_MINISBLACK)); return 0; }
940 if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, (tf_uint16)COMPRESSION_CCITTFAX3))
941 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField compression=COMPRESSION_CCITTFAX3\n")); return 0; }
942
943 size_t scan_line_size = TIFFScanlineSize(tif);
944 rowsperstrip = TIFFDefaultStripSize(tif, -1);
945 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip)) {
946 mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField rowsperstrip=%u\n", rowsperstrip)); return 0; }
947
948 TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rc);
949
950 mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField rowsperstrip=%u\n", rowsperstrip));
951 mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField scanlinesize=%zu\n", scan_line_size ));
952 mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField planarconfig=%d\n", PLANARCONFIG_CONTIG));
953
954 if (!TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)204))
955 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Xresolution=204\n")); return 0; }
956 if (!TIFFSetField(tif, TIFFTAG_YRESOLUTION, vres))
957 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Yresolution=196\n")); return 0; }
958 if (!TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, (tf_uint16)RESUNIT_INCH)) {
959 mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField ResolutionUnit=%d\n", RESUNIT_INCH)); return 0;
960 }
961
962 if (!save_tiff_tags(tif, im)) {
963 return 0;
964 }
965
966 unsigned char *linebuf = (unsigned char *)_TIFFmalloc( scan_line_size );
967
968 for (y=0; y<height; y++) {
969 int linebufpos=0;
970 for(x=0; x<width; x+=8) {
971 int bits;
972 int bitpos;
973 i_sample_t luma[8];
974 tf_uint8 bitval = 128;
975 linebuf[linebufpos]=0;
976 bits = width-x; if(bits>8) bits=8;
977 i_gsamp(im, x, x+bits, y, luma, &luma_chan, 1);
978 for(bitpos=0;bitpos<bits;bitpos++) {
979 linebuf[linebufpos] |= ((luma[bitpos] < 128) ? bitval : 0);
980 bitval >>= 1;
981 }
982 linebufpos++;
983 }
984 if (TIFFWriteScanline(tif, linebuf, y, 0) < 0) {
985 mm_log((1, "i_writetiff_wiol_faxable: TIFFWriteScanline failed.\n"));
986 goto fail;
987 }
988 }
989 _TIFFfree(linebuf);
990
991 return 1;
992
993 fail:
994 _TIFFfree(linebuf);
995 return 0;
996 }
997
998 static tf_uint16
999 find_compression(char const *name, tf_uint16 *compress) {
1000 int i;
1001
1002 for (i = 0; i < compress_value_count; ++i) {
1003 if (strcmp(compress_values[i].name, name) == 0) {
1004 *compress = (tf_uint16)compress_values[i].tag;
1005 return 1;
1006 }
1007 }
1008 *compress = COMPRESSION_NONE;
1009
1010 return 0;
1011 }
1012
1013 static tf_uint16
1014 get_compression(i_img *im, tf_uint16 def_compress) {
1015 int entry;
1016 int value;
1017
1018 if (i_tags_find(&im->tags, "tiff_compression", 0, &entry)
1019 && im->tags.tags[entry].data) {
1020 const char *name = im->tags.tags[entry].data;
1021 tf_uint16 compress;
1022 if (find_compression(name, &compress)
1023 && TIFFIsCODECConfigured(compress))
1024 return compress;
1025
1026 TIFFCodec *codecs = TIFFGetConfiguredCODECs();
1027 for (TIFFCodec *c = codecs; c->name; ++c) {
1028 if (strcmp(c->name, name) == 0) {
1029 tf_uint16 scheme = c->scheme;
1030 _TIFFfree(codecs);
1031 return scheme;
1032 }
1033 }
1034 _TIFFfree(codecs);
1035 }
1036 if (i_tags_get_int(&im->tags, "tiff_compression", 0, &value)) {
1037 if ((tf_uint16)value == value
1038 && TIFFIsCODECConfigured((tf_uint16)value))
1039 return (tf_uint16)value;
1040 }
1041
1042 return def_compress;
1043 }
1044
1045 int
1046 i_tiff_has_compression(const char *name) {
1047 tf_uint16 compress;
1048
1049 if (!find_compression(name, &compress))
1050 return 0;
1051
1052 return TIFFIsCODECConfigured(compress);
1053 }
1054
1055 static int
1056 set_base_tags(TIFF *tif, i_img *im, tf_uint16 compress, tf_uint16 photometric,
1057 tf_uint16 bits_per_sample, tf_uint16 samples_per_pixel) {
1058 double xres, yres;
1059 int resunit;
1060 int got_xres, got_yres;
1061 int aspect_only;
1062
1063 if ((tf_uint32)im->xsize != (i_img_dim_u)im->xsize ||
1064 (tf_uint32)im->ysize != (i_img_dim_u)im->ysize) {
1065 i_push_error(0, "image too large for TIFF");
1066 return 0;
1067 }
1068
1069 if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, (tf_uint32)im->xsize)) {
1070 i_push_error(0, "write TIFF: setting width tag");
1071 return 0;
1072 }
1073 if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, (tf_uint32)im->ysize)) {
1074 i_push_error(0, "write TIFF: setting length tag");
1075 return 0;
1076 }
1077 if (!TIFFSetField(tif, TIFFTAG_ORIENTATION, (tf_uint16)ORIENTATION_TOPLEFT)) {
1078 i_push_error(0, "write TIFF: setting orientation tag");
1079 return 0;
1080 }
1081 if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, (tf_uint16)PLANARCONFIG_CONTIG)) {
1082 i_push_error(0, "write TIFF: setting planar configuration tag");
1083 return 0;
1084 }
1085 if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric)) {
1086 i_push_error(0, "write TIFF: setting photometric tag");
1087 return 0;
1088 }
1089 if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, compress)) {
1090 i_push_error(0, "write TIFF: setting compression tag");
1091 return 0;
1092 }
1093 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bits_per_sample)) {
1094 i_push_error(0, "write TIFF: setting bits per sample tag");
1095 return 0;
1096 }
1097 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel)) {
1098 i_push_error(0, "write TIFF: setting samples per pixel tag");
1099 return 0;
1100 }
1101
1102 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
1103 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
1104 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
1105 aspect_only = 0;
1106 if (!i_tags_get_int(&im->tags, "tiff_resolutionunit", 0, &resunit))
1107 resunit = RESUNIT_INCH;
1108 if (got_xres || got_yres) {
1109 if (!got_xres)
1110 xres = yres;
1111 else if (!got_yres)
1112 yres = xres;
1113 if (aspect_only) {
1114 resunit = RESUNIT_NONE;
1115 }
1116 else {
1117 if (resunit == RESUNIT_CENTIMETER) {
1118 xres /= 2.54;
1119 yres /= 2.54;
1120 }
1121 else {
1122 resunit = RESUNIT_INCH;
1123 }
1124 }
1125 if (!TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)xres)) {
1126 i_push_error(0, "write TIFF: setting xresolution tag");
1127 return 0;
1128 }
1129 if (!TIFFSetField(tif, TIFFTAG_YRESOLUTION, (float)yres)) {
1130 i_push_error(0, "write TIFF: setting yresolution tag");
1131 return 0;
1132 }
1133 if (!TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, (tf_uint16)resunit)) {
1134 i_push_error(0, "write TIFF: setting resolutionunit tag");
1135 return 0;
1136 }
1137 }
1138
1139 return 1;
1140 }
1141
1142 static int
1143 write_one_bilevel(TIFF *tif, i_img *im, int zero_is_white) {
1144 tf_uint16 compress = get_compression(im, COMPRESSION_PACKBITS);
1145 tf_uint16 photometric;
1146 unsigned char *in_row;
1147 unsigned char *out_row;
1148 unsigned out_size;
1149 i_img_dim x, y;
1150 int invert;
1151
1152 mm_log((1, "tiff - write_one_bilevel(tif %p, im %p, zero_is_white %d)\n",
1153 tif, im, zero_is_white));
1154
1155 /* ignore a silly choice */
1156 if (compress == COMPRESSION_JPEG)
1157 compress = COMPRESSION_PACKBITS;
1158
1159 switch (compress) {
1160 case COMPRESSION_CCITTRLE:
1161 case COMPRESSION_CCITTFAX3:
1162 case COMPRESSION_CCITTFAX4:
1163 /* natural fax photometric */
1164 photometric = PHOTOMETRIC_MINISWHITE;
1165 break;
1166
1167 default:
1168 /* natural for most computer images */
1169 photometric = PHOTOMETRIC_MINISBLACK;
1170 break;
1171 }
1172
1173 if (!set_base_tags(tif, im, compress, photometric, 1, 1))
1174 return 0;
1175
1176 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, -1))) {
1177 i_push_error(0, "write TIFF: setting rows per strip tag");
1178 return 0;
1179 }
1180
1181 out_size = TIFFScanlineSize(tif);
1182 out_row = (unsigned char *)_TIFFmalloc( out_size );
1183 in_row = mymalloc(im->xsize);
1184
1185 invert = (photometric == PHOTOMETRIC_MINISWHITE) != (zero_is_white != 0);
1186
1187 for (y = 0; y < im->ysize; ++y) {
1188 int mask = 0x80;
1189 unsigned char *outp = out_row;
1190 memset(out_row, 0, out_size);
1191 i_gpal(im, 0, im->xsize, y, in_row);
1192 for (x = 0; x < im->xsize; ++x) {
1193 if (invert ? !in_row[x] : in_row[x]) {
1194 *outp |= mask;
1195 }
1196 mask >>= 1;
1197 if (!mask) {
1198 ++outp;
1199 mask = 0x80;
1200 }
1201 }
1202 if (TIFFWriteScanline(tif, out_row, y, 0) < 0) {
1203 _TIFFfree(out_row);
1204 myfree(in_row);
1205 i_push_error(0, "write TIFF: write scan line failed");
1206 return 0;
1207 }
1208 }
1209
1210 _TIFFfree(out_row);
1211 myfree(in_row);
1212
1213 return 1;
1214 }
1215
1216 static int
1217 set_palette(TIFF *tif, i_img *im, int size) {
1218 int count;
1219 tf_uint16 *colors;
1220 tf_uint16 *out[3];
1221 i_color c;
1222 int i, ch;
1223
1224 colors = (tf_uint16 *)_TIFFmalloc(sizeof(tf_uint16) * 3 * size);
1225 out[0] = colors;
1226 out[1] = colors + size;
1227 out[2] = colors + 2 * size;
1228
1229 count = i_colorcount(im);
1230 for (i = 0; i < count; ++i) {
1231 i_getcolors(im, i, &c, 1);
1232 for (ch = 0; ch < 3; ++ch)
1233 out[ch][i] = c.channel[ch] * 257;
1234 }
1235 for (; i < size; ++i) {
1236 for (ch = 0; ch < 3; ++ch)
1237 out[ch][i] = 0;
1238 }
1239 if (!TIFFSetField(tif, TIFFTAG_COLORMAP, out[0], out[1], out[2])) {
1240 _TIFFfree(colors);
1241 i_push_error(0, "write TIFF: setting color map");
1242 return 0;
1243 }
1244 _TIFFfree(colors);
1245
1246 return 1;
1247 }
1248
1249 static int
1250 write_one_paletted8(TIFF *tif, i_img *im) {
1251 tf_uint16 compress = get_compression(im, COMPRESSION_PACKBITS);
1252 unsigned char *out_row;
1253 unsigned out_size;
1254 i_img_dim y;
1255
1256 mm_log((1, "tiff - write_one_paletted8(tif %p, im %p)\n", tif, im));
1257
1258 /* ignore a silly choice */
1259 if (compress == COMPRESSION_JPEG ||
1260 compress == COMPRESSION_CCITTRLE ||
1261 compress == COMPRESSION_CCITTFAX3 ||
1262 compress == COMPRESSION_CCITTFAX4)
1263 compress = COMPRESSION_PACKBITS;
1264
1265 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, -1))) {
1266 i_push_error(0, "write TIFF: setting rows per strip tag");
1267 return 0;
1268 }
1269
1270 if (!set_base_tags(tif, im, compress, PHOTOMETRIC_PALETTE, 8, 1))
1271 return 0;
1272
1273 if (!set_palette(tif, im, 256))
1274 return 0;
1275
1276 out_size = TIFFScanlineSize(tif);
1277 out_row = (unsigned char *)_TIFFmalloc( out_size );
1278
1279 for (y = 0; y < im->ysize; ++y) {
1280 i_gpal(im, 0, im->xsize, y, out_row);
1281 if (TIFFWriteScanline(tif, out_row, y, 0) < 0) {
1282 _TIFFfree(out_row);
1283 i_push_error(0, "write TIFF: write scan line failed");
1284 return 0;
1285 }
1286 }
1287
1288 _TIFFfree(out_row);
1289
1290 return 1;
1291 }
1292
1293 static int
1294 write_one_paletted4(TIFF *tif, i_img *im) {
1295 tf_uint16 compress = get_compression(im, COMPRESSION_PACKBITS);
1296 unsigned char *in_row;
1297 unsigned char *out_row;
1298 size_t out_size;
1299 i_img_dim y;
1300
1301 mm_log((1, "tiff - write_one_paletted4(tif %p, im %p)\n", tif, im));
1302
1303 /* ignore a silly choice */
1304 if (compress == COMPRESSION_JPEG ||
1305 compress == COMPRESSION_CCITTRLE ||
1306 compress == COMPRESSION_CCITTFAX3 ||
1307 compress == COMPRESSION_CCITTFAX4)
1308 compress = COMPRESSION_PACKBITS;
1309
1310 if (!set_base_tags(tif, im, compress, PHOTOMETRIC_PALETTE, 4, 1))
1311 return 0;
1312
1313 if (!set_palette(tif, im, 16))
1314 return 0;
1315
1316 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, -1))) {
1317 i_push_error(0, "write TIFF: setting rows per strip tag");
1318 return 0;
1319 }
1320
1321 in_row = mymalloc(im->xsize);
1322 out_size = TIFFScanlineSize(tif);
1323 out_row = (unsigned char *)_TIFFmalloc( out_size );
1324
1325 for (y = 0; y < im->ysize; ++y) {
1326 i_gpal(im, 0, im->xsize, y, in_row);
1327 memset(out_row, 0, out_size);
1328 pack_4bit_to(out_row, in_row, im->xsize);
1329 if (TIFFWriteScanline(tif, out_row, y, 0) < 0) {
1330 _TIFFfree(out_row);
1331 i_push_error(0, "write TIFF: write scan line failed");
1332 return 0;
1333 }
1334 }
1335
1336 myfree(in_row);
1337 _TIFFfree(out_row);
1338
1339 return 1;
1340 }
1341
1342 static int
1343 set_direct_tags(TIFF *tif, i_img *im, tf_uint16 compress,
1344 tf_uint16 bits_per_sample) {
1345 tf_uint16 extras = EXTRASAMPLE_ASSOCALPHA;
1346 tf_uint16 extra_count = im->channels == 2 || im->channels == 4;
1347 tf_uint16 photometric = im->channels >= 3 ?
1348 PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK;
1349
1350 if (!set_base_tags(tif, im, compress, photometric, bits_per_sample,
1351 im->channels)) {
1352 return 0;
1353 }
1354
1355 if (extra_count) {
1356 if (!TIFFSetField(tif, TIFFTAG_EXTRASAMPLES, extra_count, &extras)) {
1357 i_push_error(0, "write TIFF: setting extra samples tag");
1358 return 0;
1359 }
1360 }
1361
1362 if (compress == COMPRESSION_JPEG) {
1363 int jpeg_quality;
1364 if (i_tags_get_int(&im->tags, "tiff_jpegquality", 0, &jpeg_quality)
1365 && jpeg_quality >= 0 && jpeg_quality <= 100) {
1366 if (!TIFFSetField(tif, TIFFTAG_JPEGQUALITY, jpeg_quality)) {
1367 i_push_error(0, "write TIFF: setting jpeg quality pseudo-tag");
1368 return 0;
1369 }
1370 }
1371 }
1372
1373 return 1;
1374 }
1375
1376 static int
1377 write_one_32(TIFF *tif, i_img *im) {
1378 tf_uint16 compress = get_compression(im, COMPRESSION_PACKBITS);
1379 unsigned *in_row;
1380 size_t out_size;
1381 tf_uint32 *out_row;
1382 i_img_dim y;
1383 size_t sample_count = im->xsize * im->channels;
1384 size_t sample_index;
1385
1386 mm_log((1, "tiff - write_one_32(tif %p, im %p)\n", tif, im));
1387
1388 /* only 8 and 12 bit samples are supported by jpeg compression */
1389 if (compress == COMPRESSION_JPEG)
1390 compress = COMPRESSION_PACKBITS;
1391
1392 if (!set_direct_tags(tif, im, compress, 32))
1393 return 0;
1394
1395 in_row = mymalloc(sample_count * sizeof(unsigned));
1396 out_size = TIFFScanlineSize(tif);
1397 out_row = (tf_uint32 *)_TIFFmalloc( out_size );
1398
1399 for (y = 0; y < im->ysize; ++y) {
1400 if (i_gsamp_bits(im, 0, im->xsize, y, in_row, NULL, im->channels, 32) <= 0) {
1401 i_push_error(0, "Cannot read 32-bit samples");
1402 return 0;
1403 }
1404 for (sample_index = 0; sample_index < sample_count; ++sample_index)
1405 out_row[sample_index] = in_row[sample_index];
1406 if (TIFFWriteScanline(tif, out_row, y, 0) < 0) {
1407 myfree(in_row);
1408 _TIFFfree(out_row);
1409 i_push_error(0, "write TIFF: write scan line failed");
1410 return 0;
1411 }
1412 }
1413
1414 myfree(in_row);
1415 _TIFFfree(out_row);
1416
1417 return 1;
1418 }
1419
1420 static int
1421 write_one_16(TIFF *tif, i_img *im) {
1422 tf_uint16 compress = get_compression(im, COMPRESSION_PACKBITS);
1423 unsigned *in_row;
1424 size_t out_size;
1425 tf_uint16 *out_row;
1426 i_img_dim y;
1427 size_t sample_count = im->xsize * im->channels;
1428 size_t sample_index;
1429
1430 mm_log((1, "tiff - write_one_16(tif %p, im %p)\n", tif, im));
1431
1432 /* only 8 and 12 bit samples are supported by jpeg compression */
1433 if (compress == COMPRESSION_JPEG)
1434 compress = COMPRESSION_PACKBITS;
1435
1436 if (!set_direct_tags(tif, im, compress, 16))
1437 return 0;
1438
1439 in_row = mymalloc(sample_count * sizeof(unsigned));
1440 out_size = TIFFScanlineSize(tif);
1441 out_row = (tf_uint16 *)_TIFFmalloc( out_size );
1442
1443 for (y = 0; y < im->ysize; ++y) {
1444 if (i_gsamp_bits(im, 0, im->xsize, y, in_row, NULL, im->channels, 16) <= 0) {
1445 i_push_error(0, "Cannot read 16-bit samples");
1446 return 0;
1447 }
1448 for (sample_index = 0; sample_index < sample_count; ++sample_index)
1449 out_row[sample_index] = in_row[sample_index];
1450 if (TIFFWriteScanline(tif, out_row, y, 0) < 0) {
1451 myfree(in_row);
1452 _TIFFfree(out_row);
1453 i_push_error(0, "write TIFF: write scan line failed");
1454 return 0;
1455 }
1456 }
1457
1458 myfree(in_row);
1459 _TIFFfree(out_row);
1460
1461 return 1;
1462 }
1463
1464 static int
1465 write_one_8(TIFF *tif, i_img *im) {
1466 tf_uint16 compress = get_compression(im, COMPRESSION_PACKBITS);
1467 size_t out_size;
1468 unsigned char *out_row;
1469 i_img_dim y;
1470 size_t sample_count = im->xsize * im->channels;
1471
1472 mm_log((1, "tiff - write_one_8(tif %p, im %p)\n", tif, im));
1473
1474 if (!set_direct_tags(tif, im, compress, 8))
1475 return 0;
1476
1477 out_size = TIFFScanlineSize(tif);
1478 if (out_size < sample_count)
1479 out_size = sample_count;
1480 out_row = (unsigned char *)_TIFFmalloc( out_size );
1481
1482 for (y = 0; y < im->ysize; ++y) {
1483 if (i_gsamp(im, 0, im->xsize, y, out_row, NULL, im->channels) <= 0) {
1484 i_push_error(0, "Cannot read 8-bit samples");
1485 return 0;
1486 }
1487 if (TIFFWriteScanline(tif, out_row, y, 0) < 0) {
1488 _TIFFfree(out_row);
1489 i_push_error(0, "write TIFF: write scan line failed");
1490 return 0;
1491 }
1492 }
1493 _TIFFfree(out_row);
1494
1495 return 1;
1496 }
1497
1498 static int
1499 i_writetiff_low(TIFF *tif, i_img *im) {
1500 tf_uint32 width, height;
1501 tf_uint16 channels;
1502 int zero_is_white;
1503
1504 width = im->xsize;
1505 height = im->ysize;
1506 channels = im->channels;
1507
1508 if ((i_img_dim)width != im->xsize || (i_img_dim)height != im->ysize) {
1509 i_push_error(0, "image too large for TIFF");
1510 return 0;
1511 }
1512
1513 mm_log((1, "i_writetiff_low: width=%d, height=%d, channels=%d, bits=%d\n", width, height, channels, im->bits));
1514 if (im->type == i_palette_type) {
1515 mm_log((1, "i_writetiff_low: paletted, colors=%d\n", i_colorcount(im)));
1516 }
1517
1518 if (i_img_is_monochrome(im, &zero_is_white)) {
1519 if (!write_one_bilevel(tif, im, zero_is_white))
1520 return 0;
1521 }
1522 else if (im->type == i_palette_type) {
1523 if (i_colorcount(im) <= 16) {
1524 if (!write_one_paletted4(tif, im))
1525 return 0;
1526 }
1527 else {
1528 if (!write_one_paletted8(tif, im))
1529 return 0;
1530 }
1531 }
1532 else if (im->bits > 16) {
1533 if (!write_one_32(tif, im))
1534 return 0;
1535 }
1536 else if (im->bits > 8) {
1537 if (!write_one_16(tif, im))
1538 return 0;
1539 }
1540 else {
1541 if (!write_one_8(tif, im))
1542 return 0;
1543 }
1544
1545 if (!save_tiff_tags(tif, im))
1546 return 0;
1547
1548 return 1;
1549 }
1550
1551 /*
1552 =item i_writetiff_multi_wiol(ig, imgs, count, fine_mode)
1553
1554 Stores an image in the iolayer object.
1555
1556 ig - io_object that defines source to write to
1557 imgs,count - the images to write
1558
1559 =cut
1560 */
1561
1562 undef_int
1563 i_writetiff_multi_wiol(io_glue *ig, i_img **imgs, int count) {
1564 int i;
1565
1566 i_clear_error();
1567 mm_log((1, "i_writetiff_multi_wiol(ig %p, imgs %p, count %d)\n",
1568 ig, imgs, count));
1569
1570
1571 tiff_state ts;
1572 TIFF *tif = do_tiff_open(&ts, ig, "wm");
1573
1574
1575 if (!tif) {
1576 mm_log((1, "i_writetiff_multi_wiol: Unable to open tif file for writing\n"));
1577 i_push_error(0, "Could not create TIFF object");
1578 return 0;
1579 }
1580
1581 for (i = 0; i < count; ++i) {
1582 if (!i_writetiff_low(tif, imgs[i])) {
1583 do_tiff_close(&ts);
1584 return 0;
1585 }
1586
1587 if (!TIFFWriteDirectory(tif)) {
1588 i_push_error(0, "Cannot write TIFF directory");
1589 do_tiff_close(&ts);
1590 return 0;
1591 }
1592 }
1593
1594 do_tiff_close(&ts);
1595
1596 if (i_io_close(ig))
1597 return 0;
1598
1599 return 1;
1600 }
1601
1602 /*
1603 =item i_writetiff_multi_wiol_faxable(ig, imgs, count, fine_mode)
1604
1605 Stores an image in the iolayer object.
1606
1607 ig - io_object that defines source to write to
1608 imgs,count - the images to write
1609 fine_mode - select fine or normal mode fax images
1610
1611 =cut
1612 */
1613
1614
1615 undef_int
1616 i_writetiff_multi_wiol_faxable(io_glue *ig, i_img **imgs, int count, int fine) {
1617 int i;
1618
1619 i_clear_error();
1620 mm_log((1, "i_writetiff_multi_wiol(ig %p, imgs %p, count %d)\n",
1621 ig, imgs, count));
1622
1623 tiff_state ts;
1624 TIFF *tif = do_tiff_open(&ts, ig, "wm");
1625
1626 if (!tif) {
1627 mm_log((1, "i_writetiff_mulit_wiol: Unable to open tif file for writing\n"));
1628 i_push_error(0, "Could not create TIFF object");
1629 return 0;
1630 }
1631
1632 for (i = 0; i < count; ++i) {
1633 if (!i_writetiff_low_faxable(tif, imgs[i], fine)) {
1634 do_tiff_close(&ts);
1635 return 0;
1636 }
1637
1638 if (!TIFFWriteDirectory(tif)) {
1639 i_push_error(0, "Cannot write TIFF directory");
1640 do_tiff_close(&ts);
1641 return 0;
1642 }
1643 }
1644
1645 do_tiff_close(&ts);
1646
1647 if (i_io_close(ig))
1648 return 0;
1649
1650 return 1;
1651 }
1652
1653 /*
1654 =item i_writetiff_wiol(im, ig)
1655
1656 Stores an image in the iolayer object.
1657
1658 im - image object to write out
1659 ig - io_object that defines source to write to
1660
1661 =cut
1662 */
1663 undef_int
1664 i_writetiff_wiol(i_img *img, io_glue *ig) {
1665 i_clear_error();
1666 mm_log((1, "i_writetiff_wiol(img %p, ig %p)\n", img, ig));
1667
1668 tiff_state ts;
1669 TIFF *tif = do_tiff_open(&ts, ig, "wm");
1670
1671
1672 if (!tif) {
1673 mm_log((1, "i_writetiff_wiol: Unable to open tif file for writing\n"));
1674 i_push_error(0, "Could not create TIFF object");
1675 return 0;
1676 }
1677
1678 if (!i_writetiff_low(tif, img)) {
1679 do_tiff_close(&ts);
1680 return 0;
1681 }
1682
1683 do_tiff_close(&ts);
1684
1685 if (i_io_close(ig))
1686 return 0;
1687
1688 return 1;
1689 }
1690
1691
1692
1693 /*
1694 =item i_writetiff_wiol_faxable(i_img *, io_glue *)
1695
1696 Stores an image in the iolayer object in faxable tiff format.
1697
1698 im - image object to write out
1699 ig - io_object that defines source to write to
1700
1701 Note, this may be rewritten to use to simply be a call to a
1702 lower-level function that gives more options for writing tiff at some
1703 point.
1704
1705 =cut
1706 */
1707
1708 undef_int
1709 i_writetiff_wiol_faxable(i_img *im, io_glue *ig, int fine) {
1710 i_clear_error();
1711 mm_log((1, "i_writetiff_wiol(img %p, ig %p)\n", im, ig));
1712
1713 tiff_state ts;
1714 TIFF* tif = do_tiff_open(&ts, ig, "wm");
1715
1716 if (!tif) {
1717 mm_log((1, "i_writetiff_wiol: Unable to open tif file for writing\n"));
1718 i_push_error(0, "Could not create TIFF object");
1719 return 0;
1720 }
1721
1722 if (!i_writetiff_low_faxable(tif, im, fine)) {
1723 do_tiff_close(&ts);
1724 return 0;
1725 }
1726
1727 do_tiff_close(&ts);
1728
1729 if (i_io_close(ig))
1730 return 0;
1731
1732 return 1;
1733 }
1734
1735 static int save_tiff_tags(TIFF *tif, i_img *im) {
1736 int i;
1737
1738 for (i = 0; i < text_tag_count; ++i) {
1739 int entry;
1740 if (i_tags_find(&im->tags, text_tag_names[i].name, 0, &entry)) {
1741 if (!TIFFSetField(tif, text_tag_names[i].tag,
1742 im->tags.tags[entry].data)) {
1743 i_push_errorf(0, "cannot save %s to TIFF", text_tag_names[i].name);
1744 return 0;
1745 }
1746 }
1747 }
1748
1749 return 1;
1750 }
1751
1752
1753 static void
1754 unpack_4bit_to(unsigned char *dest, const unsigned char *src,
1755 size_t src_byte_count) {
1756 while (src_byte_count > 0) {
1757 *dest++ = *src >> 4;
1758 *dest++ = *src++ & 0xf;
1759 --src_byte_count;
1760 }
1761 }
1762
1763 static void pack_4bit_to(unsigned char *dest, const unsigned char *src,
1764 i_img_dim pixel_count) {
1765 int i = 0;
1766 while (i < pixel_count) {
1767 if ((i & 1) == 0) {
1768 *dest = *src++ << 4;
1769 }
1770 else {
1771 *dest++ |= *src++;
1772 }
1773 ++i;
1774 }
1775 }
1776
1777 /*
1778 =item fallback_rgb_channels
1779
1780 Calculate the number of output channels when we fallback to the RGBA
1781 family of functions.
1782
1783 =cut
1784 */
1785
1786 static void
1787 fallback_rgb_channels(TIFF *tif, int *channels, int *alpha_chan) {
1788 tf_uint16 photometric;
1789 tf_uint16 in_channels;
1790 tf_uint16 extra_count;
1791 tf_uint16 *extras;
1792
1793 TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &in_channels);
1794 TIFFGetFieldDefaulted(tif, TIFFTAG_PHOTOMETRIC, &photometric);
1795
1796 switch (photometric) {
1797 case PHOTOMETRIC_SEPARATED:
1798 *channels = 3;
1799 break;
1800
1801 case PHOTOMETRIC_MINISWHITE:
1802 case PHOTOMETRIC_MINISBLACK:
1803 /* the TIFF RGBA functions expand single channel grey into RGB,
1804 so reduce it, we move the alpha channel into the right place
1805 if needed */
1806 *channels = 1;
1807 break;
1808
1809 default:
1810 *channels = 3;
1811 break;
1812 }
1813 /* TIFF images can have more than one alpha channel, but Imager can't
1814 this ignores the possibility of 2 channel images with 2 alpha,
1815 but there's not much I can do about that */
1816 *alpha_chan = 0;
1817 if (TIFFGetField(tif, TIFFTAG_EXTRASAMPLES, &extra_count, &extras)
1818 && extra_count) {
1819 *alpha_chan = (*channels)++;
1820 }
1821 }
1822
1823 static i_img *
1824 make_rgb(TIFF *tif, i_img_dim width, i_img_dim height, int *alpha_chan) {
1825 int channels = 0;
1826
1827 fallback_rgb_channels(tif, &channels, alpha_chan);
1828
1829 return i_img_8_new(width, height, channels);
1830 }
1831
1832 static i_img *
1833 read_one_rgb_lines(TIFF *tif, i_img_dim width, i_img_dim height, int allow_incomplete) {
1834 i_img *im;
1835 tf_uint32* raster = NULL;
1836 tf_uint32 rowsperstrip;
1837 i_img_dim_u row;
1838 i_color *line_buf;
1839 int alpha_chan;
1840 int rc;
1841 i_img_dim_u uheight = height;
1842
1843 im = make_rgb(tif, width, height, &alpha_chan);
1844 if (!im)
1845 return NULL;
1846
1847 rc = TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
1848 mm_log((1, "i_readtiff_wiol: rowsperstrip=%u rc = %d\n",
1849 (unsigned)rowsperstrip, rc));
1850
1851 if (rc != 1 || rowsperstrip == (tf_uint32)-1) {
1852 rowsperstrip = height;
1853 }
1854
1855 raster = (tf_uint32*)_TIFFmalloc(width * rowsperstrip * sizeof(tf_uint32));
1856 if (!raster) {
1857 i_img_destroy(im);
1858 i_push_error(0, "No space for raster buffer");
1859 return NULL;
1860 }
1861
1862 line_buf = mymalloc(sizeof(i_color) * width);
1863
1864 for( row = 0; row < uheight; row += rowsperstrip ) {
1865 tf_uint32 newrows, i_row;
1866
1867 if (!TIFFReadRGBAStrip(tif, row, raster)) {
1868 if (allow_incomplete) {
1869 i_tags_setn(&im->tags, "i_lines_read", row);
1870 i_tags_setn(&im->tags, "i_incomplete", 1);
1871 break;
1872 }
1873 else {
1874 i_push_error(0, "could not read TIFF image strip");
1875 _TIFFfree(raster);
1876 i_img_destroy(im);
1877 return NULL;
1878 }
1879 }
1880
1881 newrows = (row+rowsperstrip > uheight) ? uheight-row : rowsperstrip;
1882 mm_log((1, "newrows=%u\n", (unsigned)newrows));
1883
1884 for( i_row = 0; i_row < newrows; i_row++ ) {
1885 tf_uint32 x;
1886 i_color *outp = line_buf;
1887
1888 for(x = 0; x < (i_img_dim_u)width; x++) {
1889 tf_uint32 temp = raster[x+width*(newrows-i_row-1)];
1890 outp->rgba.r = TIFFGetR(temp);
1891 outp->rgba.g = TIFFGetG(temp);
1892 outp->rgba.b = TIFFGetB(temp);
1893
1894 if (alpha_chan) {
1895 /* the libtiff RGBA code expands greyscale into RGBA, so put the
1896 alpha in the right place and scale it */
1897 int ch;
1898 outp->channel[alpha_chan] = TIFFGetA(temp);
1899 if (outp->channel[alpha_chan]) {
1900 for (ch = 0; ch < alpha_chan; ++ch) {
1901 outp->channel[ch] = outp->channel[ch] * 255 / outp->channel[alpha_chan];
1902 }
1903 }
1904 }
1905
1906 outp++;
1907 }
1908 i_plin(im, 0, width, i_row+row, line_buf);
1909 }
1910 }
1911
1912 myfree(line_buf);
1913 _TIFFfree(raster);
1914
1915 return im;
1916 }
1917
1918 static i_img *
1919 read_one_rgb_tiled(TIFF *tif, i_img_dim width, i_img_dim height, int allow_incomplete) {
1920 i_img *im;
1921 tf_uint32* raster = NULL;
1922 int ok = 1;
1923 tf_uint32 row, col;
1924 tf_uint32 tile_width, tile_height;
1925 unsigned long pixels = 0;
1926 i_color *line;
1927 int alpha_chan;
1928 i_img_dim_u uheight = height;
1929 i_img_dim_u uwidth = width;
1930
1931 im = make_rgb(tif, width, height, &alpha_chan);
1932 if (!im)
1933 return NULL;
1934
1935 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width);
1936 TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height);
1937 mm_log((1, "i_readtiff_wiol: tile_width=%d, tile_height=%d\n", tile_width, tile_height));
1938
1939 raster = (tf_uint32*)_TIFFmalloc(sizeof(tf_uint32) * tile_width * tile_height);
1940 if (!raster) {
1941 i_img_destroy(im);
1942 i_push_error(0, "No space for raster buffer");
1943 return NULL;
1944 }
1945 line = mymalloc(tile_width * sizeof(i_color));
1946
1947 for( row = 0; row < (i_img_dim_u)height; row += tile_height ) {
1948 for( col = 0; col < (i_img_dim_u)width; col += tile_width ) {
1949
1950 /* Read the tile into an RGBA array */
1951 if (TIFFReadRGBATileExt(tif, col, row, raster, 1)) {
1952 tf_uint32 i_row, x;
1953 tf_uint32 newrows = (row+tile_height > uheight) ? height-row : tile_height;
1954 tf_uint32 newcols = (col+tile_width > uwidth ) ? width-col : tile_width;
1955
1956 mm_log((1, "i_readtiff_wiol: tile(%d, %d) newcols=%d newrows=%d\n", col, row, newcols, newrows));
1957 for( i_row = 0; i_row < newrows; i_row++ ) {
1958 i_color *outp = line;
1959 for(x = 0; x < newcols; x++) {
1960 tf_uint32 temp = raster[x+tile_width*(tile_height-i_row-1)];
1961 outp->rgba.r = TIFFGetR(temp);
1962 outp->rgba.g = TIFFGetG(temp);
1963 outp->rgba.b = TIFFGetB(temp);
1964 outp->rgba.a = TIFFGetA(temp);
1965
1966 if (alpha_chan) {
1967 /* the libtiff RGBA code expands greyscale into RGBA, so put the
1968 alpha in the right place and scale it */
1969 int ch;
1970 outp->channel[alpha_chan] = TIFFGetA(temp);
1971
1972 if (outp->channel[alpha_chan]) {
1973 for (ch = 0; ch < alpha_chan; ++ch) {
1974 outp->channel[ch] = outp->channel[ch] * 255 / outp->channel[alpha_chan];
1975 }
1976 }
1977 }
1978
1979 ++outp;
1980 }
1981 i_plin(im, col, col+newcols, row+i_row, line);
1982 }
1983 pixels += (unsigned long)newrows * newcols;
1984 }
1985 else {
1986 if (allow_incomplete) {
1987 ok = 0;
1988 }
1989 else {
1990 goto error;
1991 }
1992 }
1993 }
1994 }
1995
1996 if (!ok) {
1997 if (pixels == 0) {
1998 i_push_error(0, "TIFF: No image data could be read from the image");
1999 goto error;
2000 }
2001
2002 /* incomplete image */
2003 i_tags_setn(&im->tags, "i_incomplete", 1);
2004 i_tags_setn(&im->tags, "i_lines_read", pixels / width);
2005 }
2006
2007 myfree(line);
2008 _TIFFfree(raster);
2009
2010 return im;
2011
2012 error:
2013 myfree(line);
2014 _TIFFfree(raster);
2015 i_img_destroy(im);
2016 return NULL;
2017 }
2018
2019 char const *
2020 i_tiff_libversion(void) {
2021 return TIFFGetVersion();
2022 }
2023
2024 #define Q(x) Q_(x)
2025 #define Q_(x) #x
2026
2027 char const *
2028 i_tiff_builddate(void) {
2029 return Q(TIFFLIB_VERSION);
2030 }
2031
2032 char const *
2033 i_tiff_buildversion(void) {
2034 #ifdef TIFFLIB_MAJOR_VERSION
2035 return Q(TIFFLIB_MAJOR_VERSION) "." Q(TIFFLIB_MINOR_VERSION) "." Q(TIFFLIB_MICRO_VERSION);
2036 #else
2037 return "";
2038 #endif
2039 }
2040
2041 static int
2042 setup_paletted(read_state_t *state) {
2043 tf_uint16 *maps[3];
2044 int i, ch;
2045 int color_count = 1 << state->bits_per_sample;
2046
2047 state->img = i_img_pal_new(state->width, state->height, 3, 256);
2048 if (!state->img)
2049 return 0;
2050
2051 /* setup the color map */
2052 if (!TIFFGetField(state->tif, TIFFTAG_COLORMAP, maps+0, maps+1, maps+2)) {
2053 i_push_error(0, "Cannot get colormap for paletted image");
2054 i_img_destroy(state->img);
2055 return 0;
2056 }
2057 for (i = 0; i < color_count; ++i) {
2058 i_color c;
2059 for (ch = 0; ch < 3; ++ch) {
2060 c.channel[ch] = Sample16To8(maps[ch][i]);
2061 }
2062 i_addcolors(state->img, &c, 1);
2063 }
2064
2065 return 1;
2066 }
2067
2068 static int
2069 tile_contig_getter(read_state_t *state, read_putter_t putter) {
2070 tf_uint32 tile_width, tile_height;
2071 tf_uint32 this_tile_height, this_tile_width;
2072 tf_uint32 rows_left, cols_left;
2073 tf_uint32 x, y;
2074
2075 state->raster = _TIFFmalloc(TIFFTileSize(state->tif));
2076 if (!state->raster) {
2077 i_push_error(0, "tiff: Out of memory allocating tile buffer");
2078 return 0;
2079 }
2080
2081 TIFFGetField(state->tif, TIFFTAG_TILEWIDTH, &tile_width);
2082 TIFFGetField(state->tif, TIFFTAG_TILELENGTH, &tile_height);
2083 rows_left = state->height;
2084 for (y = 0; y < state->height; y += this_tile_height) {
2085 this_tile_height = rows_left > tile_height ? tile_height : rows_left;
2086
2087 cols_left = state->width;
2088 for (x = 0; x < state->width; x += this_tile_width) {
2089 this_tile_width = cols_left > tile_width ? tile_width : cols_left;
2090
2091 if (TIFFReadTile(state->tif,
2092 state->raster,
2093 x, y, 0, 0) < 0) {
2094 if (!state->allow_incomplete) {
2095 return 0;
2096 }
2097 }
2098 else {
2099 putter(state, x, y, this_tile_width, this_tile_height, tile_width - this_tile_width);
2100 }
2101
2102 cols_left -= this_tile_width;
2103 }
2104
2105 rows_left -= this_tile_height;
2106 }
2107
2108 return 1;
2109 }
2110
2111 static int
2112 strip_contig_getter(read_state_t *state, read_putter_t putter) {
2113 tf_uint32 rows_per_strip;
2114 tsize_t strip_size = TIFFStripSize(state->tif);
2115 tf_uint32 y, strip_rows, rows_left;
2116
2117 state->raster = _TIFFmalloc(strip_size);
2118 if (!state->raster) {
2119 i_push_error(0, "tiff: Out of memory allocating strip buffer");
2120 return 0;
2121 }
2122
2123 TIFFGetFieldDefaulted(state->tif, TIFFTAG_ROWSPERSTRIP, &rows_per_strip);
2124 rows_left = state->height;
2125 for (y = 0; y < state->height; y += strip_rows) {
2126 strip_rows = rows_left > rows_per_strip ? rows_per_strip : rows_left;
2127 if (TIFFReadEncodedStrip(state->tif,
2128 TIFFComputeStrip(state->tif, y, 0),
2129 state->raster,
2130 strip_size) < 0) {
2131 if (!state->allow_incomplete)
2132 return 0;
2133 }
2134 else {
2135 putter(state, 0, y, state->width, strip_rows, 0);
2136 }
2137 rows_left -= strip_rows;
2138 }
2139
2140 return 1;
2141 }
2142
2143 static int
2144 paletted_putter8(read_state_t *state, i_img_dim x, i_img_dim y, i_img_dim width, i_img_dim height, int extras) {
2145 unsigned char *p = state->raster;
2146
2147 state->pixels_read += width * height;
2148 while (height > 0) {
2149 i_ppal(state->img, x, x + width, y, p);
2150 p += width + extras;
2151 --height;
2152 ++y;
2153 }
2154
2155 return 1;
2156 }
2157
2158 static int
2159 paletted_putter4(read_state_t *state, i_img_dim x, i_img_dim y, i_img_dim width, i_img_dim height, int extras) {
2160 tf_uint32 img_line_size = (width + 1) / 2;
2161 tf_uint32 skip_line_size = (width + extras + 1) / 2;
2162 unsigned char *p = state->raster;
2163
2164 if (!state->line_buf)
2165 state->line_buf = mymalloc(state->width);
2166
2167 state->pixels_read += width * height;
2168 while (height > 0) {
2169 unpack_4bit_to(state->line_buf, p, img_line_size);
2170 i_ppal(state->img, x, x + width, y, state->line_buf);
2171 p += skip_line_size;
2172 --height;
2173 ++y;
2174 }
2175
2176 return 1;
2177 }
2178
2179 static void
2180 rgb_channels(read_state_t *state, int *out_channels) {
2181 tf_uint16 extra_count;
2182 tf_uint16 *extras;
2183
2184 /* safe defaults */
2185 *out_channels = 3;
2186 state->alpha_chan = 0;
2187 state->scale_alpha = 0;
2188 state->color_channels = 3;
2189
2190 /* plain RGB */
2191 if (state->samples_per_pixel == 3)
2192 return;
2193
2194 if (!TIFFGetField(state->tif, TIFFTAG_EXTRASAMPLES, &extra_count, &extras)) {
2195 mm_log((1, "tiff: samples != 3 but no extra samples tag\n"));
2196 return;
2197 }
2198
2199 if (!extra_count) {
2200 mm_log((1, "tiff: samples != 3 but no extra samples listed"));
2201 return;
2202 }
2203
2204 ++*out_channels;
2205 state->alpha_chan = 3;
2206 switch (*extras) {
2207 case EXTRASAMPLE_UNSPECIFIED:
2208 case EXTRASAMPLE_ASSOCALPHA:
2209 state->scale_alpha = 1;
2210 break;
2211
2212 case EXTRASAMPLE_UNASSALPHA:
2213 state->scale_alpha = 0;
2214 break;
2215
2216 default:
2217 mm_log((1, "tiff: unknown extra sample type %d, treating as assoc alpha\n",
2218 *extras));
2219 state->scale_alpha = 1;
2220 break;
2221 }
2222 mm_log((1, "tiff alpha channel %d scale %d\n", state->alpha_chan, state->scale_alpha));
2223 }
2224
2225 static void
2226 grey_channels(read_state_t *state, int *out_channels) {
2227 tf_uint16 extra_count;
2228 tf_uint16 *extras;
2229
2230 /* safe defaults */
2231 *out_channels = 1;
2232 state->alpha_chan = 0;
2233 state->scale_alpha = 0;
2234 state->color_channels = 1;
2235
2236 /* plain grey */
2237 if (state->samples_per_pixel == 1)
2238 return;
2239
2240 if (!TIFFGetField(state->tif, TIFFTAG_EXTRASAMPLES, &extra_count, &extras)) {
2241 mm_log((1, "tiff: samples != 1 but no extra samples tag\n"));
2242 return;
2243 }
2244
2245 if (!extra_count) {
2246 mm_log((1, "tiff: samples != 1 but no extra samples listed"));
2247 return;
2248 }
2249
2250 ++*out_channels;
2251 state->alpha_chan = 1;
2252 switch (*extras) {
2253 case EXTRASAMPLE_UNSPECIFIED:
2254 case EXTRASAMPLE_ASSOCALPHA:
2255 state->scale_alpha = 1;
2256 break;
2257
2258 case EXTRASAMPLE_UNASSALPHA:
2259 state->scale_alpha = 0;
2260 break;
2261
2262 default:
2263 mm_log((1, "tiff: unknown extra sample type %d, treating as assoc alpha\n",
2264 *extras));
2265 state->scale_alpha = 1;
2266 break;
2267 }
2268 }
2269
2270 static int
2271 setup_16_rgb(read_state_t *state) {
2272 int out_channels;
2273
2274 rgb_channels(state, &out_channels);
2275
2276 state->img = i_img_16_new(state->width, state->height, out_channels);
2277 if (!state->img)
2278 return 0;
2279 state->line_buf = mymalloc(sizeof(unsigned) * state->width * out_channels);
2280
2281 return 1;
2282 }
2283
2284 static int
2285 setup_16_grey(read_state_t *state) {
2286 int out_channels;
2287
2288 grey_channels(state, &out_channels);
2289
2290 state->img = i_img_16_new(state->width, state->height, out_channels);
2291 if (!state->img)
2292 return 0;
2293 state->line_buf = mymalloc(sizeof(unsigned) * state->width * out_channels);
2294
2295 return 1;
2296 }
2297
2298 static int
2299 putter_16(read_state_t *state, i_img_dim x, i_img_dim y, i_img_dim width, i_img_dim height,
2300 int row_extras) {
2301 tf_uint16 *p = state->raster;
2302 int out_chan = state->img->channels;
2303
2304 state->pixels_read += width * height;
2305 while (height > 0) {
2306 i_img_dim i;
2307 int ch;
2308 unsigned *outp = state->line_buf;
2309
2310 for (i = 0; i < width; ++i) {
2311 for (ch = 0; ch < out_chan; ++ch) {
2312 outp[ch] = p[ch];
2313 }
2314 if (state->sample_signed) {
2315 for (ch = 0; ch < state->color_channels; ++ch)
2316 outp[ch] ^= 0x8000;
2317 }
2318 if (state->alpha_chan && state->scale_alpha && outp[state->alpha_chan]) {
2319 for (ch = 0; ch < state->alpha_chan; ++ch) {
2320 int result = 0.5 + (outp[ch] * 65535.0 / outp[state->alpha_chan]);
2321 outp[ch] = CLAMP16(result);
2322 }
2323 }
2324 p += state->samples_per_pixel;
2325 outp += out_chan;
2326 }
2327
2328 i_psamp_bits(state->img, x, x + width, y, state->line_buf, NULL, out_chan, 16);
2329
2330 p += row_extras * state->samples_per_pixel;
2331 --height;
2332 ++y;
2333 }
2334
2335 return 1;
2336 }
2337
2338 static int
2339 setup_8_rgb(read_state_t *state) {
2340 int out_channels;
2341
2342 rgb_channels(state, &out_channels);
2343
2344 state->img = i_img_8_new(state->width, state->height, out_channels);
2345 if (!state->img)
2346 return 0;
2347 state->line_buf = mymalloc(sizeof(unsigned) * state->width * out_channels);
2348
2349 return 1;
2350 }
2351
2352 static int
2353 setup_8_grey(read_state_t *state) {
2354 int out_channels;
2355
2356 grey_channels(state, &out_channels);
2357
2358 state->img = i_img_8_new(state->width, state->height, out_channels);
2359 if (!state->img)
2360 return 0;
2361 state->line_buf = mymalloc(sizeof(i_color) * state->width * out_channels);
2362
2363 return 1;
2364 }
2365
2366 static int
2367 putter_8(read_state_t *state, i_img_dim x, i_img_dim y, i_img_dim width, i_img_dim height,
2368 int row_extras) {
2369 unsigned char *p = state->raster;
2370 int out_chan = state->img->channels;
2371
2372 state->pixels_read += width * height;
2373 while (height > 0) {
2374 i_img_dim i;
2375 int ch;
2376 i_color *outp = state->line_buf;
2377
2378 for (i = 0; i < width; ++i) {
2379 for (ch = 0; ch < out_chan; ++ch) {
2380 outp->channel[ch] = p[ch];
2381 }
2382 if (state->sample_signed) {
2383 for (ch = 0; ch < state->color_channels; ++ch)
2384 outp->channel[ch] ^= 0x80;
2385 }
2386 if (state->alpha_chan && state->scale_alpha
2387 && outp->channel[state->alpha_chan]) {
2388 for (ch = 0; ch < state->alpha_chan; ++ch) {
2389 int result = (outp->channel[ch] * 255 + 127) / outp->channel[state->alpha_chan];
2390
2391 outp->channel[ch] = CLAMP8(result);
2392 }
2393 }
2394 p += state->samples_per_pixel;
2395 outp++;
2396 }
2397
2398 i_plin(state->img, x, x + width, y, state->line_buf);
2399
2400 p += row_extras * state->samples_per_pixel;
2401 --height;
2402 ++y;
2403 }
2404
2405 return 1;
2406 }
2407
2408 static int
2409 setup_32_rgb(read_state_t *state) {
2410 int out_channels;
2411
2412 rgb_channels(state, &out_channels);
2413
2414 state->img = i_img_double_new(state->width, state->height, out_channels);
2415 if (!state->img)
2416 return 0;
2417 state->line_buf = mymalloc(sizeof(i_fcolor) * state->width);
2418
2419 return 1;
2420 }
2421
2422 static int
2423 setup_32_grey(read_state_t *state) {
2424 int out_channels;
2425
2426 grey_channels(state, &out_channels);
2427
2428 state->img = i_img_double_new(state->width, state->height, out_channels);
2429 if (!state->img)
2430 return 0;
2431 state->line_buf = mymalloc(sizeof(i_fcolor) * state->width);
2432
2433 return 1;
2434 }
2435
2436 static int
2437 putter_32(read_state_t *state, i_img_dim x, i_img_dim y, i_img_dim width, i_img_dim height,
2438 int row_extras) {
2439 tf_uint32 *p = state->raster;
2440 int out_chan = state->img->channels;
2441
2442 state->pixels_read += width * height;
2443 while (height > 0) {
2444 i_img_dim i;
2445 int ch;
2446 i_fcolor *outp = state->line_buf;
2447
2448 for (i = 0; i < width; ++i) {
2449 #ifdef IEEEFP_TYPES
2450 if (state->sample_format == SAMPLEFORMAT_IEEEFP) {
2451 const float *pv = (const float *)p;
2452 for (ch = 0; ch < out_chan; ++ch) {
2453 outp->channel[ch] = pv[ch];
2454 }
2455 }
2456 else {
2457 #endif
2458 for (ch = 0; ch < out_chan; ++ch) {
2459 if (state->sample_signed && ch < state->color_channels)
2460 outp->channel[ch] = (p[ch] ^ 0x80000000UL) / 4294967295.0;
2461 else
2462 outp->channel[ch] = p[ch] / 4294967295.0;
2463 }
2464 #ifdef IEEEFP_TYPES
2465 }
2466 #endif
2467
2468 if (state->alpha_chan && state->scale_alpha && outp->channel[state->alpha_chan]) {
2469 for (ch = 0; ch < state->alpha_chan; ++ch)
2470 outp->channel[ch] /= outp->channel[state->alpha_chan];
2471 }
2472 p += state->samples_per_pixel;
2473 outp++;
2474 }
2475
2476 i_plinf(state->img, x, x + width, y, state->line_buf);
2477
2478 p += row_extras * state->samples_per_pixel;
2479 --height;
2480 ++y;
2481 }
2482
2483 return 1;
2484 }
2485
2486 static int
2487 setup_bilevel(read_state_t *state) {
2488 i_color black, white;
2489 state->img = i_img_pal_new(state->width, state->height, 1, 256);
2490 if (!state->img)
2491 return 0;
2492 black.channel[0] = black.channel[1] = black.channel[2] =
2493 black.channel[3] = 0;
2494 white.channel[0] = white.channel[1] = white.channel[2] =
2495 white.channel[3] = 255;
2496 if (state->photometric == PHOTOMETRIC_MINISBLACK) {
2497 i_addcolors(state->img, &black, 1);
2498 i_addcolors(state->img, &white, 1);
2499 }
2500 else {
2501 i_addcolors(state->img, &white, 1);
2502 i_addcolors(state->img, &black, 1);
2503 }
2504 state->line_buf = mymalloc(state->width);
2505
2506 return 1;
2507 }
2508
2509 static int
2510 putter_bilevel(read_state_t *state, i_img_dim x, i_img_dim y, i_img_dim width, i_img_dim height,
2511 int row_extras) {
2512 unsigned char *line_in = state->raster;
2513 size_t line_size = (width + row_extras + 7) / 8;
2514
2515 /* tifflib returns the bits in MSB2LSB order even when the file is
2516 in LSB2MSB, so we only need to handle MSB2LSB */
2517 state->pixels_read += width * height;
2518 while (height > 0) {
2519 i_img_dim i;
2520 unsigned char *outp = state->line_buf;
2521 unsigned char *inp = line_in;
2522 unsigned mask = 0x80;
2523
2524 for (i = 0; i < width; ++i) {
2525 *outp++ = *inp & mask ? 1 : 0;
2526 mask >>= 1;
2527 if (!mask) {
2528 ++inp;
2529 mask = 0x80;
2530 }
2531 }
2532
2533 i_ppal(state->img, x, x + width, y, state->line_buf);
2534
2535 line_in += line_size;
2536 --height;
2537 ++y;
2538 }
2539
2540 return 1;
2541 }
2542
2543 static void
2544 cmyk_channels(read_state_t *state, int *out_channels) {
2545 tf_uint16 extra_count;
2546 tf_uint16 *extras;
2547
2548 /* safe defaults */
2549 *out_channels = 3;
2550 state->alpha_chan = 0;
2551 state->scale_alpha = 0;
2552 state->color_channels = 3;
2553
2554 /* plain CMYK */
2555 if (state->samples_per_pixel == 4)
2556 return;
2557
2558 if (!TIFFGetField(state->tif, TIFFTAG_EXTRASAMPLES, &extra_count, &extras)) {
2559 mm_log((1, "tiff: CMYK samples != 4 but no extra samples tag\n"));
2560 return;
2561 }
2562
2563 if (!extra_count) {
2564 mm_log((1, "tiff: CMYK samples != 4 but no extra samples listed"));
2565 return;
2566 }
2567
2568 ++*out_channels;
2569 state->alpha_chan = 4;
2570 switch (*extras) {
2571 case EXTRASAMPLE_UNSPECIFIED:
2572 case EXTRASAMPLE_ASSOCALPHA:
2573 state->scale_alpha = 1;
2574 break;
2575
2576 case EXTRASAMPLE_UNASSALPHA:
2577 state->scale_alpha = 0;
2578 break;
2579
2580 default:
2581 mm_log((1, "tiff: unknown extra sample type %d, treating as assoc alpha\n",
2582 *extras));
2583 state->scale_alpha = 1;
2584 break;
2585 }
2586 }
2587
2588 static int
2589 setup_cmyk8(read_state_t *state) {
2590 int channels;
2591
2592 cmyk_channels(state, &channels);
2593 state->img = i_img_8_new(state->width, state->height, channels);
2594
2595 state->line_buf = mymalloc(sizeof(i_color) * state->width);
2596
2597 return 1;
2598 }
2599
2600 static int
2601 putter_cmyk8(read_state_t *state, i_img_dim x, i_img_dim y, i_img_dim width, i_img_dim height,
2602 int row_extras) {
2603 unsigned char *p = state->raster;
2604
2605 state->pixels_read += width * height;
2606 while (height > 0) {
2607 i_img_dim i;
2608 int ch;
2609 i_color *outp = state->line_buf;
2610
2611 for (i = 0; i < width; ++i) {
2612 unsigned char c, m, y, k;
2613 c = p[0];
2614 m = p[1];
2615 y = p[2];
2616 k = 255 - p[3];
2617 if (state->sample_signed) {
2618 c ^= 0x80;
2619 m ^= 0x80;
2620 y ^= 0x80;
2621 k ^= 0x80;
2622 }
2623 outp->rgba.r = (k * (255 - c)) / 255;
2624 outp->rgba.g = (k * (255 - m)) / 255;
2625 outp->rgba.b = (k * (255 - y)) / 255;
2626 if (state->alpha_chan) {
2627 outp->rgba.a = p[state->alpha_chan];
2628 if (state->scale_alpha
2629 && outp->rgba.a) {
2630 for (ch = 0; ch < 3; ++ch) {
2631 int result = (outp->channel[ch] * 255 + 127) / outp->rgba.a;
2632 outp->channel[ch] = CLAMP8(result);
2633 }
2634 }
2635 }
2636 p += state->samples_per_pixel;
2637 outp++;
2638 }
2639
2640 i_plin(state->img, x, x + width, y, state->line_buf);
2641
2642 p += row_extras * state->samples_per_pixel;
2643 --height;
2644 ++y;
2645 }
2646
2647 return 1;
2648 }
2649
2650 static int
2651 setup_cmyk16(read_state_t *state) {
2652 int channels;
2653
2654 cmyk_channels(state, &channels);
2655 state->img = i_img_16_new(state->width, state->height, channels);
2656
2657 state->line_buf = mymalloc(sizeof(unsigned) * state->width * channels);
2658
2659 return 1;
2660 }
2661
2662 static int
2663 putter_cmyk16(read_state_t *state, i_img_dim x, i_img_dim y, i_img_dim width, i_img_dim height,
2664 int row_extras) {
2665 tf_uint16 *p = state->raster;
2666 int out_chan = state->img->channels;
2667
2668 mm_log((4, "putter_cmyk16(%p, %" i_DF ", %" i_DF ", %" i_DF
2669 ", %" i_DF ", %d)\n", state, i_DFcp(x, y), i_DFcp(width, height),
2670 row_extras));
2671
2672 state->pixels_read += width * height;
2673 while (height > 0) {
2674 i_img_dim i;
2675 int ch;
2676 unsigned *outp = state->line_buf;
2677
2678 for (i = 0; i < width; ++i) {
2679 unsigned c, m, y, k;
2680 c = p[0];
2681 m = p[1];
2682 y = p[2];
2683 k = 65535 - p[3];
2684 if (state->sample_signed) {
2685 c ^= 0x8000;
2686 m ^= 0x8000;
2687 y ^= 0x8000;
2688 k ^= 0x8000;
2689 }
2690 outp[0] = (k * (65535U - c)) / 65535U;
2691 outp[1] = (k * (65535U - m)) / 65535U;
2692 outp[2] = (k * (65535U - y)) / 65535U;
2693 if (state->alpha_chan) {
2694 outp[3] = p[state->alpha_chan];
2695 if (state->scale_alpha
2696 && outp[3]) {
2697 for (ch = 0; ch < 3; ++ch) {
2698 int result = (outp[ch] * 65535 + 32767) / outp[3];
2699 outp[3] = CLAMP16(result);
2700 }
2701 }
2702 }
2703 p += state->samples_per_pixel;
2704 outp += out_chan;
2705 }
2706
2707 i_psamp_bits(state->img, x, x + width, y, state->line_buf, NULL, out_chan, 16);
2708
2709 p += row_extras * state->samples_per_pixel;
2710 --height;
2711 ++y;
2712 }
2713
2714 return 1;
2715 }
2716
2717 static void
2718 tiffio_context_init(tiffio_context_t *c, io_glue *ig) {
2719 c->magic = TIFFIO_MAGIC;
2720 c->ig = ig;
2721 c->warn_buf = NULL;
2722 }
2723
2724 static void
2725 tiffio_context_final(tiffio_context_t *c) {
2726 c->magic = TIFFIO_MAGIC;
2727 if (c->warn_buf)
2728 io_glue_destroy(c->warn_buf);
2729 }
2730
2731 i_tiff_codec *
2732 i_tiff_get_codecs(size_t *pcount) {
2733 TIFFCodec *codecs = TIFFGetConfiguredCODECs();
2734
2735 if (!codecs)
2736 return NULL;
2737
2738 int count;
2739 for (count = 0; codecs[count].name; ++count) {
2740 /* just counting */
2741 }
2742 i_tiff_codec *mycodecs = mymalloc(sizeof(i_tiff_codec) * count);
2743 for (int i = 0; i < count; ++i) {
2744 mycodecs[i].description = codecs[i].name;
2745 mycodecs[i].name = "";
2746 for (int j = 0; j < compress_value_count; ++j) {
2747 if (codecs[i].scheme == compress_values[j].tag) {
2748 mycodecs[i].name = compress_values[j].name;
2749 break;
2750 }
2751 }
2752 mycodecs[i].code = codecs[i].scheme;
2753 }
2754 _TIFFfree(codecs);
2755
2756 *pcount = count;
2757 return mycodecs;
2758 }
2759
2760 /*
2761 =back
2762
2763 =head1 AUTHOR
2764
2765 Arnar M. Hrafnkelsson <[email protected]>, Tony Cook <[email protected]>
2766
2767 =head1 SEE ALSO
2768
2769 Imager(3)
2770
2771 =cut
2772 */