2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Contributor(s): Jonathan Merritt.
20 * ***** END GPL LICENSE BLOCK *****
23 /** \file blender/imbuf/intern/tiff.c
29 * Provides TIFF file loading and saving for Blender, via libtiff.
31 * The task of loading is complicated somewhat by the fact that Blender has
32 * already loaded the file into a memory buffer. libtiff is not well
33 * configured to handle files in memory, so a client wrapper is written to
34 * surround the memory and turn it into a virtual file. Currently, reading
35 * of TIFF files is done using libtiff's RGBAImage support. This is a
36 * high-level routine that loads all images as 32-bit RGBA, handling all the
37 * required conversions between many different TIFF types internally.
39 * Saving supports RGB, RGBA and BW (grayscale) images correctly, with
40 * 8 bits per channel in all cases. The "deflate" compression algorithm is
41 * used to compress images.
49 #include "BLI_utildefines.h"
51 #include "BKE_global.h"
53 #include "IMB_imbuf_types.h"
54 #include "IMB_imbuf.h"
56 #include "IMB_filetype.h"
58 #include "IMB_colormanagement.h"
59 #include "IMB_colormanagement_intern.h"
67 /***********************
68 * Local declarations. *
69 ***********************/
70 /* Reading and writing of an in-memory TIFF file. */
71 static tsize_t imb_tiff_ReadProc(thandle_t handle, tdata_t data, tsize_t n);
72 static tsize_t imb_tiff_WriteProc(thandle_t handle, tdata_t data, tsize_t n);
73 static toff_t imb_tiff_SeekProc(thandle_t handle, toff_t ofs, int whence);
74 static int imb_tiff_CloseProc(thandle_t handle);
75 static toff_t imb_tiff_SizeProc(thandle_t handle);
76 static int imb_tiff_DummyMapProc(thandle_t fd, tdata_t *pbase, toff_t *psize);
77 static void imb_tiff_DummyUnmapProc(thandle_t fd, tdata_t base, toff_t size);
80 /* Structure for in-memory TIFF file. */
81 typedef struct ImbTIFFMemFile {
82 unsigned char *mem; /* Location of first byte of TIFF file. */
83 toff_t offset; /* Current offset within the file. */
84 tsize_t size; /* Size of the TIFF file. */
86 #define IMB_TIFF_GET_MEMFILE(x) ((ImbTIFFMemFile *)(x))
90 /*****************************
91 * Function implementations. *
92 *****************************/
95 static void imb_tiff_DummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
102 static int imb_tiff_DummyMapProc(thandle_t fd, tdata_t *pbase, toff_t *psize)
112 * Reads data from an in-memory TIFF file.
114 * \param handle: Handle of the TIFF file (pointer to ImbTIFFMemFile).
115 * \param data: Buffer to contain data (treat as (void *)).
116 * \param n: Number of bytes to read.
118 * \return: Number of bytes actually read.
121 static tsize_t imb_tiff_ReadProc(thandle_t handle, tdata_t data, tsize_t n)
123 tsize_t nRemaining, nCopy;
124 ImbTIFFMemFile *mfile;
127 /* get the pointer to the in-memory file */
128 mfile = IMB_TIFF_GET_MEMFILE(handle);
129 if (!mfile || !mfile->mem) {
130 fprintf(stderr, "imb_tiff_ReadProc: !mfile || !mfile->mem!\n");
134 /* find the actual number of bytes to read (copy) */
136 if ((tsize_t)mfile->offset >= mfile->size)
139 nRemaining = mfile->size - mfile->offset;
141 if (nCopy > nRemaining)
144 /* on EOF, return immediately and read (copy) nothing */
148 /* all set -> do the read (copy) */
149 srcAddr = (void *)(&(mfile->mem[mfile->offset]));
150 memcpy((void *)data, srcAddr, nCopy);
151 mfile->offset += nCopy; /* advance file ptr by copied bytes */
158 * Writes data to an in-memory TIFF file.
160 * NOTE: The current Blender implementation should not need this function. It
163 static tsize_t imb_tiff_WriteProc(thandle_t handle, tdata_t data, tsize_t n)
169 printf("imb_tiff_WriteProc: this function should not be called.\n");
176 * Seeks to a new location in an in-memory TIFF file.
178 * \param handle: Handle of the TIFF file (pointer to ImbTIFFMemFile).
179 * \param ofs: Offset value (interpreted according to whence below).
180 * \param whence: This can be one of three values:
181 * SEEK_SET - The offset is set to ofs bytes.
182 * SEEK_CUR - The offset is set to its current location plus ofs bytes.
183 * SEEK_END - (This is unsupported and will return -1, indicating an
186 * \return: Resulting offset location within the file, measured in bytes from
187 * the beginning of the file. (-1) indicates an error.
189 static toff_t imb_tiff_SeekProc(thandle_t handle, toff_t ofs, int whence)
191 ImbTIFFMemFile *mfile;
194 /* get the pointer to the in-memory file */
195 mfile = IMB_TIFF_GET_MEMFILE(handle);
196 if (!mfile || !mfile->mem) {
197 fprintf(stderr, "imb_tiff_SeekProc: !mfile || !mfile->mem!\n");
201 /* find the location we plan to seek to */
207 new_offset = mfile->offset + ofs;
210 /* no other types are supported - return an error */
212 "imb_tiff_SeekProc: "
213 "Unsupported TIFF SEEK type.\n");
217 /* set the new location */
218 mfile->offset = new_offset;
219 return mfile->offset;
225 * Closes (virtually) an in-memory TIFF file.
227 * NOTE: All this function actually does is sets the data pointer within the
228 * TIFF file to NULL. That should trigger assertion errors if attempts
229 * are made to access the file after that point. However, no such
230 * attempts should ever be made (in theory).
232 * \param handle: Handle of the TIFF file (pointer to ImbTIFFMemFile).
236 static int imb_tiff_CloseProc(thandle_t handle)
238 ImbTIFFMemFile *mfile;
240 /* get the pointer to the in-memory file */
241 mfile = IMB_TIFF_GET_MEMFILE(handle);
242 if (!mfile || !mfile->mem) {
243 fprintf(stderr, "imb_tiff_CloseProc: !mfile || !mfile->mem!\n");
247 /* virtually close the file */
258 * Returns the size of an in-memory TIFF file in bytes.
260 * \return: Size of file (in bytes).
262 static toff_t imb_tiff_SizeProc(thandle_t handle)
264 ImbTIFFMemFile *mfile;
266 /* get the pointer to the in-memory file */
267 mfile = IMB_TIFF_GET_MEMFILE(handle);
268 if (!mfile || !mfile->mem) {
269 fprintf(stderr, "imb_tiff_SizeProc: !mfile || !mfile->mem!\n");
273 /* return the size */
274 return (toff_t)(mfile->size);
277 static TIFF *imb_tiff_client_open(ImbTIFFMemFile *memFile, unsigned char *mem, size_t size)
279 /* open the TIFF client layer interface to the in-memory file */
282 memFile->size = size;
284 return TIFFClientOpen("(Blender TIFF Interface Layer)",
285 "r", (thandle_t)(memFile),
286 imb_tiff_ReadProc, imb_tiff_WriteProc,
287 imb_tiff_SeekProc, imb_tiff_CloseProc,
288 imb_tiff_SizeProc, imb_tiff_DummyMapProc, imb_tiff_DummyUnmapProc);
292 * Checks whether a given memory buffer contains a TIFF file.
294 * This method uses the format identifiers from:
295 * http://www.faqs.org/faqs/graphics/fileformats-faq/part4/section-9.html
296 * The first four bytes of big-endian and little-endian TIFF files
297 * respectively are (hex):
300 * Note that TIFF files on *any* platform can be either big- or little-endian;
301 * it's not platform-specific.
303 * AFAICT, libtiff doesn't provide a method to do this automatically, and
304 * hence my manual comparison. - Jonathan Merritt (lancelet) 4th Sept 2005.
306 #define IMB_TIFF_NCB 4 /* number of comparison bytes used */
307 int imb_is_a_tiff(unsigned char *mem)
309 char big_endian[IMB_TIFF_NCB] = { 0x4d, 0x4d, 0x00, 0x2a };
310 char lil_endian[IMB_TIFF_NCB] = { 0x49, 0x49, 0x2a, 0x00 };
312 return ((memcmp(big_endian, mem, IMB_TIFF_NCB) == 0) ||
313 (memcmp(lil_endian, mem, IMB_TIFF_NCB) == 0));
316 static void scanline_contig_16bit(float *rectf, const unsigned short *sbuf, int scanline_w, int spp)
319 for (i = 0; i < scanline_w; i++) {
320 rectf[i * 4 + 0] = sbuf[i * spp + 0] / 65535.0;
321 rectf[i * 4 + 1] = (spp >= 3) ? sbuf[i * spp + 1] / 65535.0 : sbuf[i * spp + 0] / 65535.0;
322 rectf[i * 4 + 2] = (spp >= 3) ? sbuf[i * spp + 2] / 65535.0 : sbuf[i * spp + 0] / 65535.0;
323 rectf[i * 4 + 3] = (spp == 4) ? (sbuf[i * spp + 3] / 65535.0) : 1.0;
327 static void scanline_contig_32bit(float *rectf, const float *fbuf, int scanline_w, int spp)
330 for (i = 0; i < scanline_w; i++) {
331 rectf[i * 4 + 0] = fbuf[i * spp + 0];
332 rectf[i * 4 + 1] = (spp >= 3) ? fbuf[i * spp + 1] : fbuf[i * spp + 0];
333 rectf[i * 4 + 2] = (spp >= 3) ? fbuf[i * spp + 2] : fbuf[i * spp + 0];
334 rectf[i * 4 + 3] = (spp == 4) ? fbuf[i * spp + 3] : 1.0f;
338 static void scanline_separate_16bit(float *rectf, const unsigned short *sbuf, int scanline_w, int chan)
341 for (i = 0; i < scanline_w; i++)
342 rectf[i * 4 + chan] = sbuf[i] / 65535.0;
345 static void scanline_separate_32bit(float *rectf, const float *fbuf, int scanline_w, int chan)
348 for (i = 0; i < scanline_w; i++)
349 rectf[i * 4 + chan] = fbuf[i];
352 static void imb_read_tiff_resolution(ImBuf *ibuf, TIFF *image)
358 TIFFGetFieldDefaulted(image, TIFFTAG_RESOLUTIONUNIT, &unit);
359 TIFFGetFieldDefaulted(image, TIFFTAG_XRESOLUTION, &xres);
360 TIFFGetFieldDefaulted(image, TIFFTAG_YRESOLUTION, &yres);
362 if (unit == RESUNIT_CENTIMETER) {
363 ibuf->ppm[0] = (double)xres * 100.0;
364 ibuf->ppm[1] = (double)yres * 100.0;
367 ibuf->ppm[0] = (double)xres / 0.0254;
368 ibuf->ppm[1] = (double)yres / 0.0254;
373 * Use the libTIFF scanline API to read a TIFF image.
374 * This method is most flexible and can handle multiple different bit depths
375 * and RGB channel orderings.
377 static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image)
381 short bitspersample, spp, config;
383 int ib_flag = 0, row, chan;
385 unsigned short *sbuf = NULL;
387 TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bitspersample);
388 TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp); /* number of 'channels' */
389 TIFFGetField(image, TIFFTAG_PLANARCONFIG, &config);
392 /* HACK: this is really tricky hack, which is only needed to force libtiff
393 * do not touch RGB channels when there's alpha channel present
394 * The thing is: libtiff will premul RGB if alpha mode is set to
395 * unassociated, which really conflicts with blender's assumptions
397 * Alternative would be to unpremul after load, but it'll be really
398 * lossy and unwanted behavior
400 * So let's keep this thing here for until proper solution is found (sergey)
403 unsigned short extraSampleTypes[1];
404 extraSampleTypes[0] = EXTRASAMPLE_ASSOCALPHA;
405 TIFFSetField(image, TIFFTAG_EXTRASAMPLES, 1, extraSampleTypes);
408 imb_read_tiff_resolution(ibuf, image);
410 scanline = TIFFScanlineSize(image);
412 if (bitspersample == 32) {
413 ib_flag = IB_rectfloat;
414 fbuf = (float *)_TIFFmalloc(scanline);
416 else if (bitspersample == 16) {
417 ib_flag = IB_rectfloat;
418 sbuf = (unsigned short *)_TIFFmalloc(scanline);
424 tmpibuf = IMB_allocImBuf(ibuf->x, ibuf->y, ibuf->planes, ib_flag);
426 /* simple RGBA image */
427 if (!(bitspersample == 32 || bitspersample == 16)) {
428 success |= TIFFReadRGBAImage(image, ibuf->x, ibuf->y, tmpibuf->rect, 0);
430 /* contiguous channels: RGBRGBRGB */
431 else if (config == PLANARCONFIG_CONTIG) {
432 for (row = 0; row < ibuf->y; row++) {
433 int ib_offset = ibuf->x * ibuf->y * 4 - ibuf->x * 4 * (row + 1);
435 if (bitspersample == 32) {
436 success |= TIFFReadScanline(image, fbuf, row, 0);
437 scanline_contig_32bit(tmpibuf->rect_float + ib_offset, fbuf, ibuf->x, spp);
440 else if (bitspersample == 16) {
441 success |= TIFFReadScanline(image, sbuf, row, 0);
442 scanline_contig_16bit(tmpibuf->rect_float + ib_offset, sbuf, ibuf->x, spp);
445 /* separate channels: RRRGGGBBB */
447 else if (config == PLANARCONFIG_SEPARATE) {
449 /* imbufs always have 4 channels of data, so we iterate over all of them
450 * but only fill in from the TIFF scanline where necessary. */
451 for (chan = 0; chan < 4; chan++) {
452 for (row = 0; row < ibuf->y; row++) {
453 int ib_offset = ibuf->x * ibuf->y * 4 - ibuf->x * 4 * (row + 1);
455 if (bitspersample == 32) {
456 if (chan == 3 && spp == 3) /* fill alpha if only RGB TIFF */
457 fill_vn_fl(fbuf, ibuf->x, 1.0f);
458 else if (chan >= spp) /* for grayscale, duplicate first channel into G and B */
459 success |= TIFFReadScanline(image, fbuf, row, 0);
461 success |= TIFFReadScanline(image, fbuf, row, chan);
462 scanline_separate_32bit(tmpibuf->rect_float + ib_offset, fbuf, ibuf->x, chan);
465 else if (bitspersample == 16) {
466 if (chan == 3 && spp == 3) /* fill alpha if only RGB TIFF */
467 fill_vn_ushort(sbuf, ibuf->x, 65535);
468 else if (chan >= spp) /* for grayscale, duplicate first channel into G and B */
469 success |= TIFFReadScanline(image, fbuf, row, 0);
471 success |= TIFFReadScanline(image, sbuf, row, chan);
472 scanline_separate_16bit(tmpibuf->rect_float + ib_offset, sbuf, ibuf->x, chan);
479 if (bitspersample == 32)
481 else if (bitspersample == 16)
485 /* Code seems to be not needed for 16 bits tif, on PPC G5 OSX (ton) */
486 if (bitspersample < 16)
487 if (ENDIAN_ORDER == B_ENDIAN)
488 IMB_convert_rgba_to_abgr(tmpibuf);
490 /* assign rect last */
491 if (tmpibuf->rect_float)
492 ibuf->rect_float = tmpibuf->rect_float;
494 ibuf->rect = tmpibuf->rect;
495 ibuf->mall |= ib_flag;
496 ibuf->flags |= ib_flag;
498 tmpibuf->mall &= ~ib_flag;
501 IMB_freeImBuf(tmpibuf);
506 void imb_inittiff(void)
508 if (!(G.debug & G_DEBUG))
509 TIFFSetErrorHandler(NULL);
516 * \param mem: Memory containing the TIFF file.
517 * \param size: Size of the mem buffer.
518 * \param flags: If flags has IB_test set then the file is not actually loaded,
519 * but all other operations take place.
521 * \return: A newly allocated ImBuf structure if successful, otherwise NULL.
523 ImBuf *imb_loadtiff(unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
526 ImBuf *ibuf = NULL, *hbuf;
527 ImbTIFFMemFile memFile;
528 uint32 width, height;
535 /* check whether or not we have a TIFF file */
536 if (size < IMB_TIFF_NCB) {
537 fprintf(stderr, "imb_loadtiff: size < IMB_TIFF_NCB\n");
540 if (imb_is_a_tiff(mem) == 0)
543 /* both 8 and 16 bit PNGs are default to standard byte colorspace */
544 colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE);
546 image = imb_tiff_client_open(&memFile, mem, size);
549 printf("imb_loadtiff: could not open TIFF IO layer.\n");
553 /* allocate the image buffer */
554 TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
555 TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
556 TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);
558 ib_depth = (spp == 3) ? 24 : 32;
560 ibuf = IMB_allocImBuf(width, height, ib_depth, 0);
566 "imb_loadtiff: could not allocate memory for TIFF "
572 /* get alpha mode from file header */
573 if (flags & IB_alphamode_detect) {
575 unsigned short extra, *extraSampleTypes;
577 found = TIFFGetField(image, TIFFTAG_EXTRASAMPLES, &extra, &extraSampleTypes);
579 if (found && (extraSampleTypes[0] == EXTRASAMPLE_ASSOCALPHA)) {
580 ibuf->flags |= IB_alphamode_premul;
585 /* if testing, we're done */
586 if (flags & IB_test) {
591 /* detect if we are reading a tiled/mipmapped texture, in that case
592 * we don't read pixels but leave it to the cache to load tiles */
593 if (flags & IB_tilecache) {
595 TIFFGetField(image, TIFFTAG_PIXAR_TEXTUREFORMAT, &format);
597 if (format && STREQ(format, "Plain Texture") && TIFFIsTiled(image)) {
598 int numlevel = TIFFNumberOfDirectories(image);
600 /* create empty mipmap levels in advance */
601 for (level = 0; level < numlevel; level++) {
602 if (!TIFFSetDirectory(image, level))
606 width = (width > 1) ? width / 2 : 1;
607 height = (height > 1) ? height / 2 : 1;
609 hbuf = IMB_allocImBuf(width, height, 32, 0);
610 hbuf->miplevel = level;
611 hbuf->ftype = ibuf->ftype;
612 ibuf->mipmap[level - 1] = hbuf;
617 hbuf->flags |= IB_tilecache;
619 TIFFGetField(image, TIFFTAG_TILEWIDTH, &hbuf->tilex);
620 TIFFGetField(image, TIFFTAG_TILELENGTH, &hbuf->tiley);
622 hbuf->xtiles = ceil(hbuf->x / (float)hbuf->tilex);
623 hbuf->ytiles = ceil(hbuf->y / (float)hbuf->tiley);
625 imb_addtilesImBuf(hbuf);
633 if (!(ibuf->flags & IB_tilecache) && !imb_read_tiff_pixels(ibuf, image)) {
634 fprintf(stderr, "imb_loadtiff: Failed to read tiff image.\n");
639 /* close the client layer interface to the in-memory file */
642 /* return successfully */
646 void imb_loadtiletiff(ImBuf *ibuf, unsigned char *mem, size_t size, int tx, int ty, unsigned int *rect)
649 uint32 width, height;
650 ImbTIFFMemFile memFile;
652 image = imb_tiff_client_open(&memFile, mem, size);
655 printf("imb_loadtiff: could not open TIFF IO layer for loading mipmap level.\n");
659 if (TIFFSetDirectory(image, ibuf->miplevel)) { /* allocate the image buffer */
660 TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
661 TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
663 if (width == ibuf->x && height == ibuf->y) {
665 /* tiff pixels are bottom to top, tiles are top to bottom */
666 if (TIFFReadRGBATile(image, tx * ibuf->tilex, (ibuf->ytiles - 1 - ty) * ibuf->tiley, rect) == 1) {
667 if (ibuf->tiley > ibuf->y)
668 memmove(rect, rect + ibuf->tilex * (ibuf->tiley - ibuf->y), sizeof(int) * ibuf->tilex * ibuf->y);
671 printf("imb_loadtiff: failed to read tiff tile at mipmap level %d\n", ibuf->miplevel);
675 printf("imb_loadtiff: mipmap level %d has unexpected size %ux%u instead of %dx%d\n", ibuf->miplevel, width, height, ibuf->x, ibuf->y);
678 printf("imb_loadtiff: could not find mipmap level %d\n", ibuf->miplevel);
680 /* close the client layer interface to the in-memory file */
687 * ImBuf structures with 1, 3 or 4 bytes per pixel (GRAY, RGB, RGBA
688 * respectively) are accepted, and interpreted correctly. Note that the TIFF
689 * convention is to use pre-multiplied alpha, which can be achieved within
690 * Blender by setting "Premul" alpha handling. Other alpha conventions are
691 * not strictly correct, but are permitted anyhow.
693 * \param ibuf: Image buffer.
694 * \param name: Name of the TIFF file to create.
695 * \param flags: Currently largely ignored.
697 * \return: 1 if the function is successful, 0 on failure.
700 int imb_savetiff(ImBuf *ibuf, const char *name, int flags)
703 uint16 samplesperpixel, bitspersample;
705 unsigned char *pixels = NULL;
706 unsigned char *from = NULL, *to = NULL;
707 unsigned short *pixels16 = NULL, *to16 = NULL;
710 int x, y, from_i, to_i, i;
712 /* check for a valid number of bytes per pixel. Like the PNG writer,
713 * the TIFF writer supports 1, 3 or 4 bytes per pixel, corresponding
714 * to gray, RGB, RGBA respectively. */
715 samplesperpixel = (uint16)((ibuf->planes + 7) >> 3);
716 if ((samplesperpixel > 4) || (samplesperpixel == 2)) {
718 "imb_savetiff: unsupported number of bytes per "
719 "pixel: %d\n", samplesperpixel);
723 if ((ibuf->ftype & TIF_16BIT) && ibuf->rect_float)
728 /* open TIFF file for writing */
729 if (flags & IB_mem) {
730 /* bork at the creation of a TIFF in memory */
732 "imb_savetiff: creation of in-memory TIFF files is "
733 "not yet supported.\n");
737 /* create image as a file */
739 wchar_t *wname = alloc_utf16_from_8(name, 0);
740 image = TIFFOpenW(wname, "w");
743 image = TIFFOpen(name, "w");
748 "imb_savetiff: could not open TIFF for writing.\n");
752 /* allocate array for pixel data */
753 npixels = ibuf->x * ibuf->y;
754 if (bitspersample == 16)
755 pixels16 = (unsigned short *)_TIFFmalloc(npixels *
756 samplesperpixel * sizeof(unsigned short));
758 pixels = (unsigned char *)_TIFFmalloc(npixels *
759 samplesperpixel * sizeof(unsigned char));
761 if (pixels == NULL && pixels16 == NULL) {
763 "imb_savetiff: could not allocate pixels array.\n");
769 if (bitspersample == 16) {
770 fromf = ibuf->rect_float;
774 from = (unsigned char *)ibuf->rect;
778 /* setup samples per pixel */
779 TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, bitspersample);
780 TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
782 if (samplesperpixel == 4) {
783 unsigned short extraSampleTypes[1];
785 if (bitspersample == 16)
786 extraSampleTypes[0] = EXTRASAMPLE_ASSOCALPHA;
788 extraSampleTypes[0] = EXTRASAMPLE_UNASSALPHA;
791 TIFFSetField(image, TIFFTAG_EXTRASAMPLES, 1,
793 TIFFSetField(image, TIFFTAG_PHOTOMETRIC,
796 else if (samplesperpixel == 3) {
798 TIFFSetField(image, TIFFTAG_PHOTOMETRIC,
801 else if (samplesperpixel == 1) {
802 /* grayscale images, 1 channel */
803 TIFFSetField(image, TIFFTAG_PHOTOMETRIC,
804 PHOTOMETRIC_MINISBLACK);
807 /* copy pixel data. While copying, we flip the image vertically. */
808 for (x = 0; x < ibuf->x; x++) {
809 for (y = 0; y < ibuf->y; y++) {
810 from_i = 4 * (y * ibuf->x + x);
811 to_i = samplesperpixel * ((ibuf->y - y - 1) * ibuf->x + x);
814 /* convert from float source */
817 if (ibuf->float_colorspace) {
818 /* float buffer was managed already, no need in color space conversion */
819 copy_v3_v3(rgb, &fromf[from_i]);
822 /* standard linear-to-srgb conversion if float buffer wasn't managed */
823 linearrgb_to_srgb_v3_v3(rgb, &fromf[from_i]);
826 rgb[3] = fromf[from_i + 3];
828 for (i = 0; i < samplesperpixel; i++, to_i++)
829 to16[to_i] = FTOUSHORT(rgb[i]);
832 for (i = 0; i < samplesperpixel; i++, to_i++, from_i++)
833 to[to_i] = from[from_i];
838 /* write the actual TIFF file */
839 TIFFSetField(image, TIFFTAG_IMAGEWIDTH, ibuf->x);
840 TIFFSetField(image, TIFFTAG_IMAGELENGTH, ibuf->y);
841 TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, ibuf->y);
842 TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
843 TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
844 TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
847 if (ibuf->ppm[0] > 0.0 && ibuf->ppm[1] > 0.0) {
848 xres = (float)(ibuf->ppm[0] * 0.0254);
849 yres = (float)(ibuf->ppm[1] * 0.0254);
852 xres = yres = IMB_DPI_DEFAULT;
855 TIFFSetField(image, TIFFTAG_XRESOLUTION, xres);
856 TIFFSetField(image, TIFFTAG_YRESOLUTION, yres);
857 TIFFSetField(image, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
858 if (TIFFWriteEncodedStrip(image, 0,
859 (bitspersample == 16) ? (unsigned char *)pixels16 : pixels,
860 (size_t)ibuf->x * ibuf->y * samplesperpixel * bitspersample / 8) == -1)
863 "imb_savetiff: Could not write encoded TIFF.\n");
865 if (pixels) _TIFFfree(pixels);
866 if (pixels16) _TIFFfree(pixels16);
870 /* close the TIFF file */
872 if (pixels) _TIFFfree(pixels);
873 if (pixels16) _TIFFfree(pixels16);