9 tiff.c - implements reading and writing tiff files, uses io layer.
13 io_glue *ig = io_new_fd( fd );
14 i_img *im = i_readtiff_wiol(ig, -1); // no limit on how much is read
16 io_glue *ig = io_new_fd( fd );
17 return_code = i_writetiff_wiol(im, ig);
21 tiff.c implements the basic functions to read and write tiff files.
22 It uses the iolayer and needs either a seekable source or an entire
25 =head1 FUNCTION REFERENCE
27 Some of these functions are internal.
34 #define byteswap_macro(x) \
35 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
36 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
43 static struct tag_name text_tag_names[] =
45 { "tiff_documentname", TIFFTAG_DOCUMENTNAME, },
46 { "tiff_imagedescription", TIFFTAG_IMAGEDESCRIPTION, },
47 { "tiff_make", TIFFTAG_MAKE, },
48 { "tiff_model", TIFFTAG_MODEL, },
49 { "tiff_pagename", TIFFTAG_PAGENAME, },
50 { "tiff_software", TIFFTAG_SOFTWARE, },
51 { "tiff_datetime", TIFFTAG_DATETIME, },
52 { "tiff_artist", TIFFTAG_ARTIST, },
53 { "tiff_hostcomputer", TIFFTAG_HOSTCOMPUTER, },
56 static const int text_tag_count =
57 sizeof(text_tag_names) / sizeof(*text_tag_names);
59 static void error_handler(char const *module, char const *fmt, va_list ap) {
60 i_push_errorvf(0, fmt, ap);
63 #define WARN_BUFFER_LIMIT 10000
64 static char *warn_buffer = NULL;
65 static int warn_buffer_size = 0;
67 static void warn_handler(char const *module, char const *fmt, va_list ap) {
72 vsnprintf(buf, sizeof(buf), fmt, ap);
74 vsprintf(buf, fmt, ap);
76 if (!warn_buffer || strlen(warn_buffer)+strlen(buf)+2 > warn_buffer_size) {
77 int new_size = warn_buffer_size + strlen(buf) + 2;
78 char *old_buffer = warn_buffer;
79 if (new_size > WARN_BUFFER_LIMIT) {
80 new_size = WARN_BUFFER_LIMIT;
82 warn_buffer = myrealloc(warn_buffer, new_size);
83 if (!old_buffer) *warn_buffer = '\0';
84 warn_buffer_size = new_size;
86 if (strlen(warn_buffer)+strlen(buf)+2 <= warn_buffer_size) {
87 strcat(warn_buffer, buf);
88 strcat(warn_buffer, "\n");
92 static int save_tiff_tags(TIFF *tif, i_img *im);
94 static void expand_4bit_hl(unsigned char *buf, int count);
96 static void pack_4bit_hl(unsigned char *buf, int count);
99 static toff_t sizeproc(thandle_t x) {
105 =item comp_seek(h, o, w)
107 Compatability for 64 bit systems like latest freebsd (internal)
109 h - tiff handle, cast an io_glue object
118 comp_seek(thandle_t h, toff_t o, int w) {
119 io_glue *ig = (io_glue*)h;
120 return (toff_t) ig->seekcb(ig, o, w);
124 =item comp_mmap(thandle_t, tdata_t*, toff_t*)
128 This shouldn't ever be called but newer tifflibs want it anyway.
135 comp_mmap(thandle_t h, tdata_t*p, toff_t*off) {
140 =item comp_munmap(thandle_t h, tdata_t p, toff_t off)
144 This shouldn't ever be called but newer tifflibs want it anyway.
150 comp_munmap(thandle_t h, tdata_t p, toff_t off) {
154 static i_img *read_one_tiff(TIFF *tif) {
156 uint32 width, height;
158 uint32* raster = NULL;
162 int gotXres, gotYres;
164 uint16 bits_per_sample;
170 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
171 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
172 TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &channels);
173 tiled = TIFFIsTiled(tif);
174 TIFFGetFieldDefaulted(tif, TIFFTAG_PHOTOMETRIC, &photometric);
175 TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
177 mm_log((1, "i_readtiff_wiol: width=%d, height=%d, channels=%d\n", width, height, channels));
178 mm_log((1, "i_readtiff_wiol: %stiled\n", tiled?"":"not "));
179 mm_log((1, "i_readtiff_wiol: %sbyte swapped\n", TIFFIsByteSwapped(tif)?"":"not "));
181 if (photometric == PHOTOMETRIC_PALETTE && bits_per_sample <= 8) {
183 im = i_img_pal_new(width, height, channels, 256);
186 im = i_img_empty_ch(NULL, width, height, channels);
192 /* general metadata */
193 i_tags_addn(&im->tags, "tiff_bitspersample", 0, bits_per_sample);
194 i_tags_addn(&im->tags, "tiff_photometric", 0, photometric);
196 /* resolution tags */
197 TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &resunit);
198 gotXres = TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres);
199 gotYres = TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres);
200 if (gotXres || gotYres) {
205 i_tags_addn(&im->tags, "tiff_resolutionunit", 0, resunit);
206 if (resunit == RESUNIT_CENTIMETER) {
207 /* from dots per cm to dpi */
210 i_tags_add(&im->tags, "tiff_resolutionunit_name", 0, "centimeter", -1, 0);
212 else if (resunit == RESUNIT_NONE) {
213 i_tags_addn(&im->tags, "i_aspect_only", 0, 1);
214 i_tags_add(&im->tags, "tiff_resolutionunit_name", 0, "none", -1, 0);
216 else if (resunit == RESUNIT_INCH) {
217 i_tags_add(&im->tags, "tiff_resolutionunit_name", 0, "inch", -1, 0);
220 i_tags_add(&im->tags, "tiff_resolutionunit_name", 0, "unknown", -1, 0);
222 /* tifflib doesn't seem to provide a way to get to the original rational
223 value of these, which would let me provide a more reasonable
224 precision. So make up a number. */
225 i_tags_set_float2(&im->tags, "i_xres", 0, xres, 6);
226 i_tags_set_float2(&im->tags, "i_yres", 0, yres, 6);
230 for (i = 0; i < text_tag_count; ++i) {
232 if (TIFFGetField(tif, text_tag_names[i].tag, &data)) {
233 mm_log((1, "i_readtiff_wiol: tag %d has value %s\n",
234 text_tag_names[i].tag, data));
235 i_tags_add(&im->tags, text_tag_names[i].name, 0, data,
240 i_tags_add(&im->tags, "i_format", 0, "tiff", -1, 0);
241 if (warn_buffer && *warn_buffer) {
242 i_tags_add(&im->tags, "i_warning", 0, warn_buffer, -1, 0);
246 /* TIFFPrintDirectory(tif, stdout, 0); good for debugging */
248 if (photometric == PHOTOMETRIC_PALETTE &&
249 (bits_per_sample == 4 || bits_per_sample == 8)) {
254 unsigned char *buffer;
256 if (!TIFFGetField(tif, TIFFTAG_COLORMAP, maps+0, maps+1, maps+2)) {
257 i_push_error(0, "Cannot get colormap for paletted image");
261 buffer = (unsigned char *)_TIFFmalloc(width+2);
263 i_push_error(0, "out of memory");
268 memset(used, 0, sizeof(used));
269 while (row < height && TIFFReadScanline(tif, buffer, row, 0) > 0) {
270 if (bits_per_sample == 4)
271 expand_4bit_hl(buffer, (width+1)/2);
272 for (col = 0; col < width; ++col) {
273 used[buffer[col]] = 1;
275 i_ppal(im, 0, width, row, buffer);
281 /* Ideally we'd optimize the palette, but that could be expensive
282 since we'd have to re-index every pixel.
284 Optimizing the palette (even at this level) might not
285 be what the user wants, so I don't do it.
287 We'll add a function to optimize a paletted image instead.
289 maxused = (1 << bits_per_sample)-1;
291 while (maxused >= 0 && !used[maxused])
294 for (i = 0; i < 1 << bits_per_sample; ++i) {
296 for (ch = 0; ch < 3; ++ch) {
297 c.channel[ch] = Sample16To8(maps[ch][i]);
299 i_addcolors(im, &c, 1);
307 uint32 tile_width, tile_height;
309 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width);
310 TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height);
311 mm_log((1, "i_readtiff_wiol: tile_width=%d, tile_height=%d\n", tile_width, tile_height));
313 raster = (uint32*)_TIFFmalloc(tile_width * tile_height * sizeof (uint32));
316 i_push_error(0, "No space for raster buffer");
320 for( row = 0; row < height; row += tile_height ) {
321 for( col = 0; ok && col < width; col += tile_width ) {
322 uint32 i_row, x, newrows, newcols;
324 /* Read the tile into an RGBA array */
325 if (!TIFFReadRGBATile(tif, col, row, raster)) {
329 newrows = (row+tile_height > height) ? height-row : tile_height;
330 mm_log((1, "i_readtiff_wiol: newrows=%d\n", newrows));
331 newcols = (col+tile_width > width ) ? width-row : tile_width;
332 for( i_row = 0; i_row < tile_height; i_row++ ) {
333 for(x = 0; x < newcols; x++) {
335 uint32 temp = raster[x+tile_width*(tile_height-i_row-1)];
336 val.rgba.r = TIFFGetR(temp);
337 val.rgba.g = TIFFGetG(temp);
338 val.rgba.b = TIFFGetB(temp);
339 val.rgba.a = TIFFGetA(temp);
340 i_ppix(im, col+x, row+i_row, &val);
346 uint32 rowsperstrip, row;
347 int rc = TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
348 mm_log((1, "i_readtiff_wiol: rowsperstrip=%d rc = %d\n", rowsperstrip, rc));
350 if (rc != 1 || rowsperstrip==-1) {
351 rowsperstrip = height;
354 raster = (uint32*)_TIFFmalloc(width * rowsperstrip * sizeof (uint32));
357 i_push_error(0, "No space for raster buffer");
361 for( row = 0; row < height; row += rowsperstrip ) {
362 uint32 newrows, i_row;
364 if (!TIFFReadRGBAStrip(tif, row, raster)) {
369 newrows = (row+rowsperstrip > height) ? height-row : rowsperstrip;
370 mm_log((1, "newrows=%d\n", newrows));
372 for( i_row = 0; i_row < newrows; i_row++ ) {
374 for(x = 0; x<width; x++) {
376 uint32 temp = raster[x+width*(newrows-i_row-1)];
377 val.rgba.r = TIFFGetR(temp);
378 val.rgba.g = TIFFGetG(temp);
379 val.rgba.b = TIFFGetB(temp);
380 val.rgba.a = TIFFGetA(temp);
381 i_ppix(im, x, i_row+row, &val);
388 mm_log((1, "i_readtiff_wiol: error during reading\n"));
389 i_tags_addn(&im->tags, "i_incomplete", 0, 1);
398 =item i_readtiff_wiol(im, ig)
403 i_readtiff_wiol(io_glue *ig, int length) {
405 TIFFErrorHandler old_handler;
406 TIFFErrorHandler old_warn_handler;
410 old_handler = TIFFSetErrorHandler(error_handler);
411 old_warn_handler = TIFFSetWarningHandler(warn_handler);
415 /* Add code to get the filename info from the iolayer */
416 /* Also add code to check for mmapped code */
418 io_glue_commit_types(ig);
419 mm_log((1, "i_readtiff_wiol(ig %p, length %d)\n", ig, length));
421 tif = TIFFClientOpen("(Iolayer)",
424 (TIFFReadWriteProc) ig->readcb,
425 (TIFFReadWriteProc) ig->writecb,
426 (TIFFSeekProc) comp_seek,
427 (TIFFCloseProc) ig->closecb,
428 ig->sizecb ? (TIFFSizeProc) ig->sizecb : (TIFFSizeProc) sizeproc,
429 (TIFFMapFileProc) comp_mmap,
430 (TIFFUnmapFileProc) comp_munmap);
433 mm_log((1, "i_readtiff_wiol: Unable to open tif file\n"));
434 i_push_error(0, "opening file");
435 TIFFSetErrorHandler(old_handler);
436 TIFFSetWarningHandler(old_warn_handler);
440 im = read_one_tiff(tif);
442 if (TIFFLastDirectory(tif)) mm_log((1, "Last directory of tiff file\n"));
443 TIFFSetErrorHandler(old_handler);
444 TIFFSetWarningHandler(old_warn_handler);
450 =item i_readtiff_multi_wiol(ig, length, *count)
452 Reads multiple images from a TIFF.
457 i_readtiff_multi_wiol(io_glue *ig, int length, int *count) {
459 TIFFErrorHandler old_handler;
460 TIFFErrorHandler old_warn_handler;
461 i_img **results = NULL;
462 int result_alloc = 0;
466 old_handler = TIFFSetErrorHandler(error_handler);
467 old_warn_handler = TIFFSetWarningHandler(warn_handler);
471 /* Add code to get the filename info from the iolayer */
472 /* Also add code to check for mmapped code */
474 io_glue_commit_types(ig);
475 mm_log((1, "i_readtiff_wiol(ig %p, length %d)\n", ig, length));
477 tif = TIFFClientOpen("(Iolayer)",
480 (TIFFReadWriteProc) ig->readcb,
481 (TIFFReadWriteProc) ig->writecb,
482 (TIFFSeekProc) comp_seek,
483 (TIFFCloseProc) ig->closecb,
484 ig->sizecb ? (TIFFSizeProc) ig->sizecb : (TIFFSizeProc) sizeproc,
485 (TIFFMapFileProc) comp_mmap,
486 (TIFFUnmapFileProc) comp_munmap);
489 mm_log((1, "i_readtiff_wiol: Unable to open tif file\n"));
490 i_push_error(0, "opening file");
491 TIFFSetErrorHandler(old_handler);
492 TIFFSetWarningHandler(old_warn_handler);
498 i_img *im = read_one_tiff(tif);
501 if (++*count > result_alloc) {
502 if (result_alloc == 0) {
504 results = mymalloc(result_alloc * sizeof(i_img *));
509 newresults = myrealloc(results, result_alloc * sizeof(i_img *));
511 i_img_destroy(im); /* don't leak it */
514 results = newresults;
517 results[*count-1] = im;
518 } while (TIFFSetDirectory(tif, ++dirnum));
520 TIFFSetWarningHandler(old_warn_handler);
521 TIFFSetErrorHandler(old_handler);
527 i_writetiff_low_faxable(TIFF *tif, i_img *im, int fine) {
528 uint32 width, height;
529 unsigned char *linebuf = NULL;
534 float vres = fine ? 196 : 98;
540 switch (im->channels) {
550 /* This means a colorspace we don't handle yet */
551 mm_log((1, "i_writetiff_wiol_faxable: don't handle %d channel images.\n", im->channels));
555 /* Add code to get the filename info from the iolayer */
556 /* Also add code to check for mmapped code */
559 mm_log((1, "i_writetiff_wiol_faxable: width=%d, height=%d, channels=%d\n", width, height, im->channels));
561 if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width) )
562 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField width=%d failed\n", width)); return 0; }
563 if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height) )
564 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField length=%d failed\n", height)); return 0; }
565 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1))
566 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField samplesperpixel=1 failed\n")); return 0; }
567 if (!TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT))
568 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Orientation=topleft\n")); return 0; }
569 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 1) )
570 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField bitpersample=1\n")); return 0; }
571 if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG))
572 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField planarconfig\n")); return 0; }
573 if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK))
574 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField photometric=%d\n", PHOTOMETRIC_MINISBLACK)); return 0; }
575 if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, 3))
576 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField compression=3\n")); return 0; }
578 linebuf = (unsigned char *)_TIFFmalloc( TIFFScanlineSize(tif) );
580 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, -1))) {
581 mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField rowsperstrip=-1\n")); return 0; }
583 TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
584 TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rc);
586 mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField rowsperstrip=%d\n", rowsperstrip));
587 mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField scanlinesize=%d\n", TIFFScanlineSize(tif) ));
588 mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField planarconfig=%d == %d\n", rc, PLANARCONFIG_CONTIG));
590 if (!TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)204))
591 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Xresolution=204\n")); return 0; }
592 if (!TIFFSetField(tif, TIFFTAG_YRESOLUTION, vres))
593 { mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Yresolution=196\n")); return 0; }
594 if (!TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH)) {
595 mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField ResolutionUnit=%d\n", RESUNIT_INCH)); return 0;
598 if (!save_tiff_tags(tif, im)) {
602 for (y=0; y<height; y++) {
604 for(x=0; x<width; x+=8) {
609 linebuf[linebufpos]=0;
610 bits = width-x; if(bits>8) bits=8;
611 i_gsamp(im, x, x+8, y, luma, &luma_chan, 1);
612 for(bitpos=0;bitpos<bits;bitpos++) {
613 linebuf[linebufpos] |= ((luma[bitpos]>=128)?bitval:0);
618 if (TIFFWriteScanline(tif, linebuf, y, 0) < 0) {
619 mm_log((1, "i_writetiff_wiol_faxable: TIFFWriteScanline failed.\n"));
623 if (linebuf) _TIFFfree(linebuf);
629 i_writetiff_low(TIFF *tif, i_img *im) {
630 uint32 width, height;
632 uint16 predictor = 0;
634 int jpegcolormode = JPEGCOLORMODE_RGB;
635 uint16 compression = COMPRESSION_PACKBITS;
638 uint32 rowsperstrip = (uint32) -1; /* Let library pick default */
639 unsigned char *linebuf = NULL;
644 int got_xres, got_yres, aspect_only, resunit;
646 uint16 bitspersample = 8;
647 uint16 samplesperpixel;
648 uint16 *colors = NULL;
652 channels = im->channels;
656 photometric = PHOTOMETRIC_MINISBLACK;
659 photometric = PHOTOMETRIC_RGB;
660 if (compression == COMPRESSION_JPEG && jpegcolormode == JPEGCOLORMODE_RGB) photometric = PHOTOMETRIC_YCBCR;
661 else if (im->type == i_palette_type) {
662 photometric = PHOTOMETRIC_PALETTE;
666 /* This means a colorspace we don't handle yet */
667 mm_log((1, "i_writetiff_wiol: don't handle %d channel images.\n", channels));
671 /* Add code to get the filename info from the iolayer */
672 /* Also add code to check for mmapped code */
674 /*io_glue_commit_types(ig);*/
675 /*mm_log((1, "i_writetiff_wiol(im 0x%p, ig 0x%p)\n", im, ig));*/
677 mm_log((1, "i_writetiff_low: width=%d, height=%d, channels=%d\n", width, height, channels));
679 if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width) ) {
680 mm_log((1, "i_writetiff_wiol: TIFFSetField width=%d failed\n", width));
683 if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height) ) {
684 mm_log((1, "i_writetiff_wiol: TIFFSetField length=%d failed\n", height));
687 if (!TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT)) {
688 mm_log((1, "i_writetiff_wiol: TIFFSetField Orientation=topleft\n"));
691 if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)) {
692 mm_log((1, "i_writetiff_wiol: TIFFSetField planarconfig\n"));
695 if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric)) {
696 mm_log((1, "i_writetiff_wiol: TIFFSetField photometric=%d\n", photometric));
699 if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, compression)) {
700 mm_log((1, "i_writetiff_wiol: TIFFSetField compression=%d\n", compression));
703 samplesperpixel = channels;
704 if (photometric == PHOTOMETRIC_PALETTE) {
707 int count = i_colorcount(im);
716 size = 1 << bitspersample;
717 colors = (uint16 *)_TIFFmalloc(sizeof(uint16) * 3 * size);
719 out[1] = colors + size;
720 out[2] = colors + 2 * size;
722 for (i = 0; i < count; ++i) {
723 i_getcolors(im, i, &c, 1);
724 for (ch = 0; ch < 3; ++ch)
725 out[ch][i] = c.channel[ch] * 257;
727 for (; i < size; ++i) {
728 for (ch = 0; ch < 3; ++ch)
731 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bitspersample)) {
732 mm_log((1, "i_writetiff_wiol: TIFFSetField bitpersample=%d\n",
736 if (!TIFFSetField(tif, TIFFTAG_COLORMAP, out[0], out[1], out[2])) {
737 mm_log((1, "i_writetiff_wiol: TIFFSetField colormap\n"));
742 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bitspersample)) {
743 mm_log((1, "i_writetiff_wiol: TIFFSetField bitpersample=%d\n",
748 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel)) {
749 mm_log((1, "i_writetiff_wiol: TIFFSetField samplesperpixel=%d failed\n", samplesperpixel));
753 switch (compression) {
754 case COMPRESSION_JPEG:
755 mm_log((1, "i_writetiff_wiol: jpeg compression\n"));
756 if (!TIFFSetField(tif, TIFFTAG_JPEGQUALITY, quality) ) {
757 mm_log((1, "i_writetiff_wiol: TIFFSetField jpegquality=%d\n", quality));
760 if (!TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, jpegcolormode)) {
761 mm_log((1, "i_writetiff_wiol: TIFFSetField jpegcolormode=%d\n", jpegcolormode));
765 case COMPRESSION_LZW:
766 mm_log((1, "i_writetiff_wiol: lzw compression\n"));
768 case COMPRESSION_DEFLATE:
769 mm_log((1, "i_writetiff_wiol: deflate compression\n"));
771 if (!TIFFSetField(tif, TIFFTAG_PREDICTOR, predictor)) {
772 mm_log((1, "i_writetiff_wiol: TIFFSetField predictor=%d\n", predictor));
776 case COMPRESSION_PACKBITS:
777 mm_log((1, "i_writetiff_wiol: packbits compression\n"));
780 mm_log((1, "i_writetiff_wiol: unknown compression %d\n", compression));
784 linebytes = channels * width;
785 linebytes = TIFFScanlineSize(tif) > linebytes ? linebytes
786 : TIFFScanlineSize(tif);
787 /* working space for the scanlines - we go from 8-bit/pixel to 4 */
788 if (photometric == PHOTOMETRIC_PALETTE && bitspersample == 4)
789 linebytes += linebytes + 1;
790 linebuf = (unsigned char *)_TIFFmalloc(linebytes);
792 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, rowsperstrip))) {
793 mm_log((1, "i_writetiff_wiol: TIFFSetField rowsperstrip=%d\n", rowsperstrip)); return 0; }
795 TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
796 TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rc);
798 mm_log((1, "i_writetiff_wiol: TIFFGetField rowsperstrip=%d\n", rowsperstrip));
799 mm_log((1, "i_writetiff_wiol: TIFFGetField scanlinesize=%d\n", TIFFScanlineSize(tif) ));
800 mm_log((1, "i_writetiff_wiol: TIFFGetField planarconfig=%d == %d\n", rc, PLANARCONFIG_CONTIG));
801 mm_log((1, "i_writetiff_wiol: bitspersample = %d\n", bitspersample));
803 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
804 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
805 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
807 if (!i_tags_get_int(&im->tags, "tiff_resolutionunit", 0, &resunit))
808 resunit = RESUNIT_INCH;
809 if (got_xres || got_yres) {
815 resunit = RESUNIT_NONE;
818 if (resunit == RESUNIT_CENTIMETER) {
823 resunit = RESUNIT_INCH;
826 if (!TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)xres)) {
827 i_push_error(0, "cannot set TIFFTAG_XRESOLUTION tag");
830 if (!TIFFSetField(tif, TIFFTAG_YRESOLUTION, (float)yres)) {
831 i_push_error(0, "cannot set TIFFTAG_YRESOLUTION tag");
834 if (!TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, (uint16)resunit)) {
835 i_push_error(0, "cannot set TIFFTAG_RESOLUTIONUNIT tag");
840 if (!save_tiff_tags(tif, im)) {
844 if (photometric == PHOTOMETRIC_PALETTE) {
845 for (y = 0; y < height; ++y) {
846 i_gpal(im, 0, width, y, linebuf);
847 if (bitspersample == 4)
848 pack_4bit_hl(linebuf, width);
849 if (TIFFWriteScanline(tif, linebuf, y, 0) < 0) {
850 mm_log((1, "i_writetiff_wiol: TIFFWriteScanline failed.\n"));
851 if (linebuf) _TIFFfree(linebuf);
852 if (colors) _TIFFfree(colors);
858 for (y=0; y<height; y++) {
860 for(x=0; x<width; x++) {
861 (void) i_gpix(im, x, y,&val);
862 for(ch=0; ch<channels; ch++)
863 linebuf[ci++] = val.channel[ch];
865 if (TIFFWriteScanline(tif, linebuf, y, 0) < 0) {
866 mm_log((1, "i_writetiff_wiol: TIFFWriteScanline failed.\n"));
867 if (linebuf) _TIFFfree(linebuf);
868 if (colors) _TIFFfree(colors);
873 if (linebuf) _TIFFfree(linebuf);
874 if (colors) _TIFFfree(colors);
879 =item i_writetiff_multi_wiol(ig, imgs, count, fine_mode)
881 Stores an image in the iolayer object.
883 ig - io_object that defines source to write to
884 imgs,count - the images to write
890 i_writetiff_multi_wiol(io_glue *ig, i_img **imgs, int count) {
894 io_glue_commit_types(ig);
896 mm_log((1, "i_writetiff_multi_wiol(ig 0x%p, imgs 0x%p, count %d)\n",
899 /* FIXME: Enable the mmap interface */
901 tif = TIFFClientOpen("No name",
904 (TIFFReadWriteProc) ig->readcb,
905 (TIFFReadWriteProc) ig->writecb,
906 (TIFFSeekProc) comp_seek,
907 (TIFFCloseProc) ig->closecb,
908 ig->sizecb ? (TIFFSizeProc) ig->sizecb : (TIFFSizeProc) sizeproc,
909 (TIFFMapFileProc) comp_mmap,
910 (TIFFUnmapFileProc) comp_munmap);
915 mm_log((1, "i_writetiff_mulit_wiol: Unable to open tif file for writing\n"));
919 for (i = 0; i < count; ++i) {
920 if (!i_writetiff_low(tif, imgs[i])) {
925 if (!TIFFWriteDirectory(tif)) {
926 i_push_error(0, "Cannot write TIFF directory");
932 (void) TIFFClose(tif);
937 =item i_writetiff_multi_wiol_faxable(ig, imgs, count, fine_mode)
939 Stores an image in the iolayer object.
941 ig - io_object that defines source to write to
942 imgs,count - the images to write
943 fine_mode - select fine or normal mode fax images
950 i_writetiff_multi_wiol_faxable(io_glue *ig, i_img **imgs, int count, int fine) {
954 io_glue_commit_types(ig);
956 mm_log((1, "i_writetiff_multi_wiol(ig 0x%p, imgs 0x%p, count %d)\n",
959 /* FIXME: Enable the mmap interface */
961 tif = TIFFClientOpen("No name",
964 (TIFFReadWriteProc) ig->readcb,
965 (TIFFReadWriteProc) ig->writecb,
966 (TIFFSeekProc) comp_seek,
967 (TIFFCloseProc) ig->closecb,
968 ig->sizecb ? (TIFFSizeProc) ig->sizecb : (TIFFSizeProc) sizeproc,
969 (TIFFMapFileProc) comp_mmap,
970 (TIFFUnmapFileProc) comp_munmap);
975 mm_log((1, "i_writetiff_mulit_wiol: Unable to open tif file for writing\n"));
979 for (i = 0; i < count; ++i) {
980 if (!i_writetiff_low_faxable(tif, imgs[i], fine)) {
985 if (!TIFFWriteDirectory(tif)) {
986 i_push_error(0, "Cannot write TIFF directory");
992 (void) TIFFClose(tif);
997 =item i_writetiff_wiol(im, ig)
999 Stores an image in the iolayer object.
1001 im - image object to write out
1002 ig - io_object that defines source to write to
1007 i_writetiff_wiol(i_img *img, io_glue *ig) {
1010 io_glue_commit_types(ig);
1012 mm_log((1, "i_writetiff_wiol(img %p, ig 0x%p)\n", img, ig));
1014 /* FIXME: Enable the mmap interface */
1016 tif = TIFFClientOpen("No name",
1019 (TIFFReadWriteProc) ig->readcb,
1020 (TIFFReadWriteProc) ig->writecb,
1021 (TIFFSeekProc) comp_seek,
1022 (TIFFCloseProc) ig->closecb,
1023 ig->sizecb ? (TIFFSizeProc) ig->sizecb : (TIFFSizeProc) sizeproc,
1024 (TIFFMapFileProc) comp_mmap,
1025 (TIFFUnmapFileProc) comp_munmap);
1030 mm_log((1, "i_writetiff_wiol: Unable to open tif file for writing\n"));
1034 if (!i_writetiff_low(tif, img)) {
1039 (void) TIFFClose(tif);
1046 =item i_writetiff_wiol_faxable(i_img *, io_glue *)
1048 Stores an image in the iolayer object in faxable tiff format.
1050 im - image object to write out
1051 ig - io_object that defines source to write to
1053 Note, this may be rewritten to use to simply be a call to a
1054 lower-level function that gives more options for writing tiff at some
1061 i_writetiff_wiol_faxable(i_img *im, io_glue *ig, int fine) {
1064 io_glue_commit_types(ig);
1066 mm_log((1, "i_writetiff_wiol(img %p, ig 0x%p)\n", im, ig));
1068 /* FIXME: Enable the mmap interface */
1070 tif = TIFFClientOpen("No name",
1073 (TIFFReadWriteProc) ig->readcb,
1074 (TIFFReadWriteProc) ig->writecb,
1075 (TIFFSeekProc) comp_seek,
1076 (TIFFCloseProc) ig->closecb,
1077 ig->sizecb ? (TIFFSizeProc) ig->sizecb : (TIFFSizeProc) sizeproc,
1078 (TIFFMapFileProc) comp_mmap,
1079 (TIFFUnmapFileProc) comp_munmap);
1084 mm_log((1, "i_writetiff_wiol: Unable to open tif file for writing\n"));
1088 if (!i_writetiff_low_faxable(tif, im, fine)) {
1093 (void) TIFFClose(tif);
1097 static int save_tiff_tags(TIFF *tif, i_img *im) {
1100 for (i = 0; i < text_tag_count; ++i) {
1102 if (i_tags_find(&im->tags, text_tag_names[i].name, 0, &entry)) {
1103 if (!TIFFSetField(tif, text_tag_names[i].tag,
1104 im->tags.tags[entry].data)) {
1105 i_push_errorf(0, "cannot save %s to TIFF", text_tag_names[i].name);
1116 =item expand_4bit_hl(buf, count)
1118 Expands 4-bit/entry packed data into 1 byte/entry.
1120 buf must contain count bytes to be expanded and have 2*count bytes total
1123 The data is expanded in place.
1128 static void expand_4bit_hl(unsigned char *buf, int count) {
1129 while (--count >= 0) {
1130 buf[count*2+1] = buf[count] & 0xF;
1131 buf[count*2] = buf[count] >> 4;
1135 static void pack_4bit_hl(unsigned char *buf, int count) {
1138 buf[i/2] = (buf[i] << 4) + buf[i+1];
1148 Arnar M. Hrafnkelsson <addi@umich.edu>