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