Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Ebb <matt@mke3.net>2010-05-21 07:25:38 +0400
committerMatt Ebb <matt@mke3.net>2010-05-21 07:25:38 +0400
commit2be851c9665caab9e858f4c06ec59e7bb4201e17 (patch)
treeb7c27c3ae46813520ffe9e84f13739243d3ced27 /source/blender/imbuf/intern/tiff.c
parente8ed1f1f557bb469bfc277448d2ee833e4867086 (diff)
Blender TIFF support
* Removed dynamic linking libTIFF code and change it to static linking (built into the blender executable). Dynamic linking made things a fair bit more complicated and wasn't working at all before on OS X - the dylib didn't exist and wasn't being copied. Since TIFF is more heavily depended upon now in Blender, it makes sense to make it less 'optional' and more in line with other libraries. I've updated both CMake and scons, and CMake on OS X/64bit works fine. It's now up to other platform/build system maintainers to enable this for their respective platforms (Campbell will check it for linux). For windows, and non-64bit osx, we need static libtiff libraries in /lib. I've added options WITH_TIFF for CMake and WITH_BF_TIFF for scons, so if blender won't build because of this, you should be able to disable these options until your build system has been updated. * Bonus feature: while doing this, I added support for loading 16bit and 32bit per channel TIFFs - they get converted to Blender's float buffers. Handy for zbrush displacement maps!
Diffstat (limited to 'source/blender/imbuf/intern/tiff.c')
-rw-r--r--source/blender/imbuf/intern/tiff.c307
1 files changed, 245 insertions, 62 deletions
diff --git a/source/blender/imbuf/intern/tiff.c b/source/blender/imbuf/intern/tiff.c
index 3bc3f616da0..1e4b2ea7110 100644
--- a/source/blender/imbuf/intern/tiff.c
+++ b/source/blender/imbuf/intern/tiff.c
@@ -40,10 +40,12 @@
* used to compress images.
*/
+#ifdef WITH_TIFF
+
#include <string.h>
#include "imbuf.h"
-
+
#include "BKE_global.h"
#include "BKE_utildefines.h"
@@ -57,7 +59,7 @@
#include "IMB_filetype.h"
#include "IMB_filter.h"
-#include "dynlibtiff.h"
+#include "tiffio.h"
@@ -267,7 +269,7 @@ static TIFF *imb_tiff_client_open(ImbTIFFMemFile *memFile, unsigned char *mem, i
memFile->offset = 0;
memFile->size = size;
- return libtiff_TIFFClientOpen("(Blender TIFF Interface Layer)",
+ return TIFFClientOpen("(Blender TIFF Interface Layer)",
"r", (thandle_t)(memFile),
imb_tiff_ReadProc, imb_tiff_WriteProc,
imb_tiff_SeekProc, imb_tiff_CloseProc,
@@ -299,16 +301,77 @@ int imb_is_a_tiff(unsigned char *mem)
(memcmp(lil_endian, mem, IMB_TIFF_NCB) == 0) );
}
-static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image, int premul)
+static void scanline_contig_8bit(unsigned char *rect, unsigned char *cbuf, int scanline_w, int spp)
+{
+ int i;
+ for (i=0; i < scanline_w; i++) {
+ rect[i*4 + 0] = cbuf[i*spp + 0];
+ rect[i*4 + 1] = cbuf[i*spp + 1];
+ rect[i*4 + 2] = cbuf[i*spp + 2];
+ rect[i*4 + 3] = (spp==4)?cbuf[i*spp + 3]:255;
+ }
+}
+
+static void scanline_contig_16bit(float *rectf, unsigned short *sbuf, int scanline_w, int spp)
+{
+ int i;
+ for (i=0; i < scanline_w; i++) {
+ rectf[i*4 + 0] = sbuf[i*spp + 0] / 65535.0;
+ rectf[i*4 + 1] = sbuf[i*spp + 1] / 65535.0;
+ rectf[i*4 + 2] = sbuf[i*spp + 2] / 65535.0;
+ rectf[i*4 + 3] = (spp==4)?(sbuf[i*spp + 3] / 65535.0):1.0;
+ }
+}
+
+static void scanline_contig_32bit(float *rectf, float *fbuf, int scanline_w, int spp)
+{
+ int i;
+ for (i=0; i < scanline_w; i++) {
+ rectf[i*4 + 0] = fbuf[i*spp + 0];
+ rectf[i*4 + 1] = fbuf[i*spp + 1];
+ rectf[i*4 + 2] = fbuf[i*spp + 2];
+ rectf[i*4 + 3] = (spp==4)?fbuf[i*spp + 3]:1.0;
+ }
+}
+
+static void scanline_separate_8bit(unsigned char *rect, unsigned char *cbuf, int scanline_w, int chan)
+{
+ int i;
+ for (i=0; i < scanline_w; i++)
+ rect[i*4 + chan] = cbuf[i];
+}
+
+static void scanline_separate_16bit(float *rectf, unsigned short *sbuf, int scanline_w, int chan)
+{
+ int i;
+ for (i=0; i < scanline_w; i++)
+ rectf[i*4 + chan] = sbuf[i] / 65535.0;
+}
+
+static void scanline_separate_32bit(float *rectf, float *fbuf, int scanline_w, int chan)
+{
+ int i;
+ for (i=0; i < scanline_w; i++)
+ rectf[i*4 + chan] = fbuf[i];
+}
+
+
+/*
+ * Use the libTIFF RGBAImage API to read a TIFF image.
+ * This function uses the "RGBA Image" support from libtiff, which enables
+ * it to load most commonly-encountered TIFF formats. libtiff handles format
+ * conversion, color depth conversion, etc.
+ */
+static int imb_read_tiff_pixels_rgba(ImBuf *ibuf, TIFF *image, int premul)
{
ImBuf *tmpibuf;
int success;
-
+
tmpibuf= IMB_allocImBuf(ibuf->x, ibuf->y, 32, IB_rect, 0);
- success= libtiff_TIFFReadRGBAImage(image, ibuf->x, ibuf->y, tmpibuf->rect, 0);
+ success= TIFFReadRGBAImage(image, ibuf->x, ibuf->y, tmpibuf->rect, 0);
if(ENDIAN_ORDER == B_ENDIAN)
- IMB_convert_rgba_to_abgr(tmpibuf);
+ IMB_convert_rgba_to_abgr(tmpibuf);
if(premul) {
IMB_premultiply_alpha(tmpibuf);
ibuf->flags |= IB_premul;
@@ -325,12 +388,133 @@ static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image, int premul)
return success;
}
+/*
+ * Use the libTIFF scanline API to read a TIFF image.
+ * This method is most flexible and can handle multiple different bit depths
+ * and RGB channel orderings.
+ */
+static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image, int premul)
+{
+ ImBuf *tmpibuf;
+ int success;
+ short bitspersample, spp, config;
+ size_t scanline;
+ int ib_flag=0, row, chan;
+ float *fbuf=NULL;
+ unsigned short *sbuf=NULL;
+ unsigned char *cbuf=NULL;
+
+ TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bitspersample);
+ TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp); /* number of 'channels' */
+ TIFFGetField(image, TIFFTAG_PLANARCONFIG, &config);
+ scanline = TIFFScanlineSize(image);
+
+ /* if file has an unsupported channel count, use libTIFF to
+ * convert to an 8 bit RGBA image */
+ if (!ELEM(spp, 3, 4))
+ return imb_read_tiff_pixels_rgba(ibuf, image, premul);
+
+
+ if (bitspersample == 32) {
+ ib_flag = IB_rectfloat;
+ fbuf = (float *)_TIFFmalloc(scanline);
+ } else if (bitspersample == 16) {
+ ib_flag = IB_rectfloat;
+ sbuf = (unsigned short *)_TIFFmalloc(scanline);
+ } else if (bitspersample == 8) {
+ ib_flag = IB_rect;
+ cbuf = (unsigned char *)_TIFFmalloc(scanline);
+ }
+
+ tmpibuf= IMB_allocImBuf(ibuf->x, ibuf->y, ibuf->depth, ib_flag, 0);
+
+ /* contiguous channels: RGBRGBRGB */
+ if (config == PLANARCONFIG_CONTIG) {
+ for (row = 0; row < ibuf->y; row++) {
+ int ib_offset = ibuf->x*ibuf->y*4 - ibuf->x*4 * (row+1);
+
+ if (bitspersample == 32) {
+ success = TIFFReadScanline(image, fbuf, row, 0);
+ scanline_contig_32bit(tmpibuf->rect_float+ib_offset, fbuf, ibuf->x, spp);
+
+ } else if (bitspersample == 16) {
+ success = TIFFReadScanline(image, sbuf, row, 0);
+ scanline_contig_16bit(tmpibuf->rect_float+ib_offset, sbuf, ibuf->x, spp);
+
+ } else if (bitspersample == 8) {
+ unsigned char *crect = (unsigned char*)tmpibuf->rect;
+ success = TIFFReadScanline(image, cbuf, row, 0);
+ scanline_contig_8bit(crect+ib_offset, cbuf, ibuf->x, spp);
+ }
+ }
+ /* separate channels: RRRGGGBBB */
+ } else if (config == PLANARCONFIG_SEPARATE) {
+
+ /* imbufs always have 4 channels of data, so we iterate over all of them
+ * but only fill in from the TIFF scanline where necessary. */
+ for (chan = 0; chan < 4; chan++) {
+ for (row = 0; row < ibuf->y; row++) {
+ int ib_offset = ibuf->x*ibuf->y*4 - ibuf->x*4 * (row+1);
+
+ if (bitspersample == 32) {
+ if (chan == 3 && spp == 3) /* fill alpha if only RGB TIFF */
+ memset(fbuf, 1.0, sizeof(fbuf));
+ else
+ success = TIFFReadScanline(image, fbuf, row, chan);
+ scanline_separate_32bit(tmpibuf->rect_float+ib_offset, fbuf, ibuf->x, chan);
+
+ } else if (bitspersample == 16) {
+ if (chan == 3 && spp == 3) /* fill alpha if only RGB TIFF */
+ memset(sbuf, 65535, sizeof(sbuf));
+ else
+ success = TIFFReadScanline(image, sbuf, row, chan);
+ scanline_separate_16bit(tmpibuf->rect_float+ib_offset, sbuf, ibuf->x, chan);
+
+ } else if (bitspersample == 8) {
+ unsigned char *crect = (unsigned char*)tmpibuf->rect;
+ if (chan == 3 && spp == 3) /* fill alpha if only RGB TIFF */
+ memset(cbuf, 255, sizeof(cbuf));
+ else
+ success = TIFFReadScanline(image, cbuf, row, chan);
+ scanline_separate_8bit(crect+ib_offset, cbuf, ibuf->x, chan);
+ }
+ }
+ }
+ }
+
+ ibuf->profile = (bitspersample==32)?IB_PROFILE_LINEAR_RGB:IB_PROFILE_SRGB;
+
+ if (bitspersample == 32)
+ _TIFFfree(fbuf);
+ else if (bitspersample == 16)
+ _TIFFfree(sbuf);
+ else if (bitspersample == 8)
+ _TIFFfree(cbuf);
+
+ if(ENDIAN_ORDER == B_ENDIAN)
+ IMB_convert_rgba_to_abgr(tmpibuf);
+ if(premul) {
+ IMB_premultiply_alpha(tmpibuf);
+ ibuf->flags |= IB_premul;
+ }
+
+ /* assign rect last */
+ if (tmpibuf->rect_float)
+ ibuf->rect_float= tmpibuf->rect_float;
+ else
+ ibuf->rect= tmpibuf->rect;
+ ibuf->mall |= ib_flag;
+ ibuf->flags |= ib_flag;
+
+ tmpibuf->mall &= ~ib_flag;
+ IMB_freeImBuf(tmpibuf);
+
+ return success;
+}
+
/**
* Loads a TIFF file.
*
- * This function uses the "RGBA Image" support from libtiff, which enables
- * it to load most commonly-encountered TIFF formats. libtiff handles format
- * conversion, color depth conversion, etc.
*
* @param mem: Memory containing the TIFF file.
* @param size: Size of the mem buffer.
@@ -347,9 +531,8 @@ ImBuf *imb_loadtiff(unsigned char *mem, int size, int flags)
uint32 width, height;
char *format = NULL;
int level;
-
- if(!G.have_libtiff)
- return NULL;
+ short spp;
+ int ib_depth;
/* check whether or not we have a TIFF file */
if(size < IMB_TIFF_NCB) {
@@ -367,24 +550,27 @@ ImBuf *imb_loadtiff(unsigned char *mem, int size, int flags)
}
/* allocate the image buffer */
- libtiff_TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
- libtiff_TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
- ibuf = IMB_allocImBuf(width, height, 32, 0, 0);
+ TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
+ TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
+ TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);
+
+ ib_depth = (spp==3)?24:32;
+
+ ibuf = IMB_allocImBuf(width, height, ib_depth, 0, 0);
if(ibuf) {
ibuf->ftype = TIF;
- ibuf->profile = IB_PROFILE_SRGB;
}
else {
fprintf(stderr,
"imb_loadtiff: could not allocate memory for TIFF " \
"image.\n");
- libtiff_TIFFClose(image);
+ TIFFClose(image);
return NULL;
}
/* if testing, we're done */
if(flags & IB_test) {
- libtiff_TIFFClose(image);
+ TIFFClose(image);
return ibuf;
}
@@ -392,14 +578,14 @@ ImBuf *imb_loadtiff(unsigned char *mem, int size, int flags)
we don't read pixels but leave it to the cache to load tiles */
if(flags & IB_tilecache) {
format= NULL;
- libtiff_TIFFGetField(image, TIFFTAG_PIXAR_TEXTUREFORMAT, &format);
+ TIFFGetField(image, TIFFTAG_PIXAR_TEXTUREFORMAT, &format);
- if(format && strcmp(format, "Plain Texture")==0 && libtiff_TIFFIsTiled(image)) {
- int numlevel = libtiff_TIFFNumberOfDirectories(image);
+ if(format && strcmp(format, "Plain Texture")==0 && TIFFIsTiled(image)) {
+ int numlevel = TIFFNumberOfDirectories(image);
/* create empty mipmap levels in advance */
for(level=0; level<numlevel; level++) {
- if(!libtiff_TIFFSetDirectory(image, level))
+ if(!TIFFSetDirectory(image, level))
break;
if(level > 0) {
@@ -418,8 +604,8 @@ ImBuf *imb_loadtiff(unsigned char *mem, int size, int flags)
else
hbuf= ibuf;
- libtiff_TIFFGetField(image, TIFFTAG_TILEWIDTH, &hbuf->tilex);
- libtiff_TIFFGetField(image, TIFFTAG_TILELENGTH, &hbuf->tiley);
+ TIFFGetField(image, TIFFTAG_TILEWIDTH, &hbuf->tilex);
+ TIFFGetField(image, TIFFTAG_TILELENGTH, &hbuf->tiley);
hbuf->xtiles= ceil(hbuf->x/(float)hbuf->tilex);
hbuf->ytiles= ceil(hbuf->y/(float)hbuf->tiley);
@@ -434,12 +620,12 @@ ImBuf *imb_loadtiff(unsigned char *mem, int size, int flags)
/* read pixels */
if(!(ibuf->flags & IB_tilecache) && !imb_read_tiff_pixels(ibuf, image, 0)) {
fprintf(stderr, "imb_loadtiff: Failed to read tiff image.\n");
- libtiff_TIFFClose(image);
+ TIFFClose(image);
return NULL;
}
/* close the client layer interface to the in-memory file */
- libtiff_TIFFClose(image);
+ TIFFClose(image);
/* return successfully */
return ibuf;
@@ -458,15 +644,15 @@ void imb_loadtiletiff(ImBuf *ibuf, unsigned char *mem, int size, int tx, int ty,
return;
}
- if(libtiff_TIFFSetDirectory(image, ibuf->miplevel)) {
+ if(TIFFSetDirectory(image, ibuf->miplevel)) {
/* allocate the image buffer */
- libtiff_TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
- libtiff_TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
+ TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
+ TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
if(width == ibuf->x && height == ibuf->y) {
if(rect) {
/* tiff pixels are bottom to top, tiles are top to bottom */
- if(libtiff_TIFFReadRGBATile(image, tx*ibuf->tilex, (ibuf->ytiles - 1 - ty)*ibuf->tiley, rect) == 1) {
+ if(TIFFReadRGBATile(image, tx*ibuf->tilex, (ibuf->ytiles - 1 - ty)*ibuf->tiley, rect) == 1) {
if(ibuf->tiley > ibuf->y)
memmove(rect, rect+ibuf->tilex*(ibuf->tiley - ibuf->y), sizeof(int)*ibuf->tilex*ibuf->y);
@@ -484,7 +670,7 @@ void imb_loadtiletiff(ImBuf *ibuf, unsigned char *mem, int size, int tx, int ty,
printf("imb_loadtiff: could not find mipmap level %d\n", ibuf->miplevel);
/* close the client layer interface to the in-memory file */
- libtiff_TIFFClose(image);
+ TIFFClose(image);
}
/**
@@ -515,10 +701,6 @@ int imb_savetiff(ImBuf *ibuf, char *name, int flags)
int x, y, from_i, to_i, i;
int extraSampleTypes[1] = { EXTRASAMPLE_ASSOCALPHA };
- if(!G.have_libtiff) {
- fprintf(stderr, "imb_savetiff: no tiff library available.\n");
- return (0);
- }
/* check for a valid number of bytes per pixel. Like the PNG writer,
* the TIFF writer supports 1, 3 or 4 bytes per pixel, corresponding
@@ -546,7 +728,7 @@ int imb_savetiff(ImBuf *ibuf, char *name, int flags)
}
else {
/* create image as a file */
- image = libtiff_TIFFOpen(name, "w");
+ image = TIFFOpen(name, "w");
}
if(image == NULL) {
fprintf(stderr,
@@ -557,16 +739,16 @@ int imb_savetiff(ImBuf *ibuf, char *name, int flags)
/* allocate array for pixel data */
npixels = ibuf->x * ibuf->y;
if(bitspersample == 16)
- pixels16 = (unsigned short*)libtiff__TIFFmalloc(npixels *
+ pixels16 = (unsigned short*)_TIFFmalloc(npixels *
samplesperpixel * sizeof(unsigned short));
else
- pixels = (unsigned char*)libtiff__TIFFmalloc(npixels *
+ pixels = (unsigned char*)_TIFFmalloc(npixels *
samplesperpixel * sizeof(unsigned char));
if(pixels == NULL && pixels16 == NULL) {
fprintf(stderr,
"imb_savetiff: could not allocate pixels array.\n");
- libtiff_TIFFClose(image);
+ TIFFClose(image);
return (0);
}
@@ -581,24 +763,24 @@ int imb_savetiff(ImBuf *ibuf, char *name, int flags)
}
/* setup samples per pixel */
- libtiff_TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, bitspersample);
- libtiff_TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
+ TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, bitspersample);
+ TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
if(samplesperpixel == 4) {
/* RGBA images */
- libtiff_TIFFSetField(image, TIFFTAG_EXTRASAMPLES, 1,
+ TIFFSetField(image, TIFFTAG_EXTRASAMPLES, 1,
extraSampleTypes);
- libtiff_TIFFSetField(image, TIFFTAG_PHOTOMETRIC,
+ TIFFSetField(image, TIFFTAG_PHOTOMETRIC,
PHOTOMETRIC_RGB);
}
else if(samplesperpixel == 3) {
/* RGB images */
- libtiff_TIFFSetField(image, TIFFTAG_PHOTOMETRIC,
+ TIFFSetField(image, TIFFTAG_PHOTOMETRIC,
PHOTOMETRIC_RGB);
}
else if(samplesperpixel == 1) {
/* greyscale images, 1 channel */
- libtiff_TIFFSetField(image, TIFFTAG_PHOTOMETRIC,
+ TIFFSetField(image, TIFFTAG_PHOTOMETRIC,
PHOTOMETRIC_MINISBLACK);
}
@@ -635,30 +817,31 @@ int imb_savetiff(ImBuf *ibuf, char *name, int flags)
}
/* write the actual TIFF file */
- libtiff_TIFFSetField(image, TIFFTAG_IMAGEWIDTH, ibuf->x);
- libtiff_TIFFSetField(image, TIFFTAG_IMAGELENGTH, ibuf->y);
- libtiff_TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, ibuf->y);
- libtiff_TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
- libtiff_TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
- libtiff_TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
- libtiff_TIFFSetField(image, TIFFTAG_XRESOLUTION, 150.0);
- libtiff_TIFFSetField(image, TIFFTAG_YRESOLUTION, 150.0);
- libtiff_TIFFSetField(image, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
- if(libtiff_TIFFWriteEncodedStrip(image, 0,
+ TIFFSetField(image, TIFFTAG_IMAGEWIDTH, ibuf->x);
+ TIFFSetField(image, TIFFTAG_IMAGELENGTH, ibuf->y);
+ TIFFSetField(image, TIFFTAG_ROWSPERSTRIP, ibuf->y);
+ TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
+ TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
+ TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+ TIFFSetField(image, TIFFTAG_XRESOLUTION, 150.0);
+ TIFFSetField(image, TIFFTAG_YRESOLUTION, 150.0);
+ TIFFSetField(image, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
+ if(TIFFWriteEncodedStrip(image, 0,
(bitspersample == 16)? (unsigned char*)pixels16: pixels,
ibuf->x*ibuf->y*samplesperpixel*bitspersample/8) == -1) {
fprintf(stderr,
"imb_savetiff: Could not write encoded TIFF.\n");
- libtiff_TIFFClose(image);
- if(pixels) libtiff__TIFFfree(pixels);
- if(pixels16) libtiff__TIFFfree(pixels16);
+ TIFFClose(image);
+ if(pixels) _TIFFfree(pixels);
+ if(pixels16) _TIFFfree(pixels16);
return (1);
}
/* close the TIFF file */
- libtiff_TIFFClose(image);
- if(pixels) libtiff__TIFFfree(pixels);
- if(pixels16) libtiff__TIFFfree(pixels16);
+ TIFFClose(image);
+ if(pixels) _TIFFfree(pixels);
+ if(pixels16) _TIFFfree(pixels16);
return (1);
}
+#endif /* WITH_TIFF */ \ No newline at end of file