Fix buffer overflows in TIFF, PNG, IRIS, DPX, HDR and AVI loading.
[blender.git] / source / blender / imbuf / intern / tiff.c
index 2051b8d21955683394589e665862a9ef757fbb5c..afd28bb570b0195dfa35f4038be9f4496b3d90ea 100644 (file)
@@ -1,7 +1,4 @@
 /*
- * tiff.c
- *
- * 
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -39,7 +36,7 @@
  * high-level routine that loads all images as 32-bit RGBA, handling all the
  * required conversions between many different TIFF types internally.
  * 
- * Saving supports RGB, RGBA and BW (greyscale) images correctly, with
+ * Saving supports RGB, RGBA and BW (grayscale) images correctly, with
  * 8 bits per channel in all cases.  The "deflate" compression algorithm is
  * used to compress images.
  */
 #include "imbuf.h"
 
 #include "BLI_math.h"
-#include "BLI_string.h"
 #include "BLI_utildefines.h"
  
 #include "BKE_global.h"
 
-
 #include "IMB_imbuf_types.h"
 #include "IMB_imbuf.h"
 
-#include "IMB_allocimbuf.h"
 #include "IMB_filetype.h"
-#include "IMB_filter.h"
 
-#include "tiffio.h"
+#include "IMB_colormanagement.h"
+#include "IMB_colormanagement_intern.h"
 
+#include "tiffio.h"
 
+#ifdef WIN32
+#include "utfconv.h"
+#endif
 
 /***********************
  * Local declarations. *
@@ -75,17 +73,17 @@ static tsize_t imb_tiff_WriteProc(thandle_t handle, tdata_t data, tsize_t n);
 static toff_t  imb_tiff_SeekProc(thandle_t handle, toff_t ofs, int whence);
 static int     imb_tiff_CloseProc(thandle_t handle);
 static toff_t  imb_tiff_SizeProc(thandle_t handle);
-static int     imb_tiff_DummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize);
+static int     imb_tiff_DummyMapProc(thandle_t fd, tdata_t *pbase, toff_t *psize);
 static void    imb_tiff_DummyUnmapProc(thandle_t fd, tdata_t base, toff_t size);
 
 
 /* Structure for in-memory TIFF file. */
 typedef struct ImbTIFFMemFile {
-       unsigned char *mem;     /* Location of first byte of TIFF file. */
-       toff_t offset;          /* Current offset within the file.      */
-       tsize_t size;           /* Size of the TIFF file.               */
+       const unsigned char *mem;   /* Location of first byte of TIFF file. */
+       toff_t offset;              /* Current offset within the file.      */
+       tsize_t size;               /* Size of the TIFF file.               */
 } ImbTIFFMemFile;
-#define IMB_TIFF_GET_MEMFILE(x) ((ImbTIFFMemFile*)(x));
+#define IMB_TIFF_GET_MEMFILE(x) ((ImbTIFFMemFile *)(x))
 
 
 
@@ -101,7 +99,7 @@ static void imb_tiff_DummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
        (void)size;
 }
 
-static int imb_tiff_DummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize) 
+static int imb_tiff_DummyMapProc(thandle_t fd, tdata_t *pbase, toff_t *psize)
 {
        (void)fd;
        (void)pbase;
@@ -113,44 +111,44 @@ static int imb_tiff_DummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
 /**
  * Reads data from an in-memory TIFF file.
  *
- * @param handle: Handle of the TIFF file (pointer to ImbTIFFMemFile).
- * @param data:   Buffer to contain data (treat as void*).
- * @param n:      Number of bytes to read.
+ * \param handle: Handle of the TIFF file (pointer to ImbTIFFMemFile).
+ * \param data:   Buffer to contain data (treat as (void *)).
+ * \param n:      Number of bytes to read.
  *
- * @return: Number of bytes actually read.
- *      0 = EOF.
+ * \return: Number of bytes actually read.
+ *   0 = EOF.
  */
 static tsize_t imb_tiff_ReadProc(thandle_t handle, tdata_t data, tsize_t n)
 {
        tsize_t nRemaining, nCopy;
-       ImbTIFFMemFilemfile;
+       ImbTIFFMemFile *mfile;
        void *srcAddr;
 
        /* get the pointer to the in-memory file */
        mfile = IMB_TIFF_GET_MEMFILE(handle);
-       if(!mfile || !mfile->mem) {
+       if (!mfile || !mfile->mem) {
                fprintf(stderr, "imb_tiff_ReadProc: !mfile || !mfile->mem!\n");
                return 0;
        }
 
        /* find the actual number of bytes to read (copy) */
        nCopy = n;
-       if((tsize_t)mfile->offset >= mfile->size)
+       if ((tsize_t)mfile->offset >= mfile->size)
                nRemaining = 0;
        else
                nRemaining = mfile->size - mfile->offset;
        
-       if(nCopy > nRemaining)
+       if (nCopy > nRemaining)
                nCopy = nRemaining;
        
        /* on EOF, return immediately and read (copy) nothing */
-       if(nCopy <= 0)
+       if (nCopy <= 0)
                return (0);
 
        /* all set -> do the read (copy) */
-       srcAddr = (void*)(&(mfile->mem[mfile->offset]));
-       memcpy((void*)data, srcAddr, nCopy);
-       mfile->offset += nCopy;         /* advance file ptr by copied bytes */
+       srcAddr = (void *)(&(mfile->mem[mfile->offset]));
+       memcpy((void *)data, srcAddr, nCopy);
+       mfile->offset += nCopy;     /* advance file ptr by copied bytes */
        return nCopy;
 }
 
@@ -177,15 +175,15 @@ static tsize_t imb_tiff_WriteProc(thandle_t handle, tdata_t data, tsize_t n)
 /**
  * Seeks to a new location in an in-memory TIFF file.
  *
- * @param handle: Handle of the TIFF file (pointer to ImbTIFFMemFile).
- * @param ofs:    Offset value (interpreted according to whence below).
- * @param whence: This can be one of three values:
- *     SEEK_SET - The offset is set to ofs bytes.
- *     SEEK_CUR - The offset is set to its current location plus ofs bytes.
- *     SEEK_END - (This is unsupported and will return -1, indicating an
- *                 error).
+ * \param handle: Handle of the TIFF file (pointer to ImbTIFFMemFile).
+ * \param ofs:    Offset value (interpreted according to whence below).
+ * \param whence: This can be one of three values:
+ *  SEEK_SET - The offset is set to ofs bytes.
+ *  SEEK_CUR - The offset is set to its current location plus ofs bytes.
+ *  SEEK_END - (This is unsupported and will return -1, indicating an
+ *              error).
  *
- * @return: Resulting offset location within the file, measured in bytes from
+ * \return: Resulting offset location within the file, measured in bytes from
  *          the beginning of the file.  (-1) indicates an error.
  */
 static toff_t imb_tiff_SeekProc(thandle_t handle, toff_t ofs, int whence)
@@ -195,7 +193,7 @@ static toff_t imb_tiff_SeekProc(thandle_t handle, toff_t ofs, int whence)
 
        /* get the pointer to the in-memory file */
        mfile = IMB_TIFF_GET_MEMFILE(handle);
-       if(!mfile || !mfile->mem) {
+       if (!mfile || !mfile->mem) {
                fprintf(stderr, "imb_tiff_SeekProc: !mfile || !mfile->mem!\n");
                return (-1);
        }
@@ -211,8 +209,8 @@ static toff_t imb_tiff_SeekProc(thandle_t handle, toff_t ofs, int whence)
                default:
                        /* no other types are supported - return an error */
                        fprintf(stderr, 
-                               "imb_tiff_SeekProc: "
-                               "Unsupported TIFF SEEK type.\n");
+                               "imb_tiff_SeekProc: "
+                               "Unsupported TIFF SEEK type.\n");
                        return (-1);
        }
 
@@ -231,9 +229,9 @@ static toff_t imb_tiff_SeekProc(thandle_t handle, toff_t ofs, int whence)
  *       are made to access the file after that point.  However, no such
  *       attempts should ever be made (in theory).
  *
- * @param handle: Handle of the TIFF file (pointer to ImbTIFFMemFile).
+ * \param handle: Handle of the TIFF file (pointer to ImbTIFFMemFile).
  *
- * @return: 0
+ * \return: 0
  */
 static int imb_tiff_CloseProc(thandle_t handle)
 {
@@ -241,8 +239,8 @@ static int imb_tiff_CloseProc(thandle_t handle)
 
        /* get the pointer to the in-memory file */
        mfile = IMB_TIFF_GET_MEMFILE(handle);
-       if(!mfile || !mfile->mem) {
-               fprintf(stderr,"imb_tiff_CloseProc: !mfile || !mfile->mem!\n");
+       if (!mfile || !mfile->mem) {
+               fprintf(stderr, "imb_tiff_CloseProc: !mfile || !mfile->mem!\n");
                return (0);
        }
        
@@ -259,16 +257,16 @@ static int imb_tiff_CloseProc(thandle_t handle)
 /**
  * Returns the size of an in-memory TIFF file in bytes.
  *
- * @return: Size of file (in bytes).
+ * \return: Size of file (in bytes).
  */
 static toff_t imb_tiff_SizeProc(thandle_t handle)
 {
-       ImbTIFFMemFilemfile;
+       ImbTIFFMemFile *mfile;
 
        /* get the pointer to the in-memory file */
        mfile = IMB_TIFF_GET_MEMFILE(handle);
-       if(!mfile || !mfile->mem) {
-               fprintf(stderr,"imb_tiff_SizeProc: !mfile || !mfile->mem!\n");
+       if (!mfile || !mfile->mem) {
+               fprintf(stderr, "imb_tiff_SizeProc: !mfile || !mfile->mem!\n");
                return (0);
        }
 
@@ -276,7 +274,7 @@ static toff_t imb_tiff_SizeProc(thandle_t handle)
        return (toff_t)(mfile->size);
 }
 
-static TIFF *imb_tiff_client_open(ImbTIFFMemFile *memFile, unsigned char *mem, size_t size)
+static TIFF *imb_tiff_client_open(ImbTIFFMemFile *memFile, const unsigned char *mem, size_t size)
 {
        /* open the TIFF client layer interface to the in-memory file */
        memFile->mem = mem;
@@ -284,10 +282,10 @@ static TIFF *imb_tiff_client_open(ImbTIFFMemFile *memFile, unsigned char *mem, s
        memFile->size = size;
 
        return TIFFClientOpen("(Blender TIFF Interface Layer)", 
-               "r", (thandle_t)(memFile),
-               imb_tiff_ReadProc, imb_tiff_WriteProc,
-               imb_tiff_SeekProc, imb_tiff_CloseProc,
-               imb_tiff_SizeProc, imb_tiff_DummyMapProc, imb_tiff_DummyUnmapProc);
+                             "r", (thandle_t)(memFile),
+                             imb_tiff_ReadProc, imb_tiff_WriteProc,
+                             imb_tiff_SeekProc, imb_tiff_CloseProc,
+                             imb_tiff_SizeProc, imb_tiff_DummyMapProc, imb_tiff_DummyUnmapProc);
 }
 
 /**
@@ -297,58 +295,58 @@ static TIFF *imb_tiff_client_open(ImbTIFFMemFile *memFile, unsigned char *mem, s
  *     http://www.faqs.org/faqs/graphics/fileformats-faq/part4/section-9.html
  * The first four bytes of big-endian and little-endian TIFF files
  * respectively are (hex):
- *     4d 4d 00 2a
- *     49 49 2a 00
+ *  4d 4d 00 2a
+ *  49 49 2a 00
  * Note that TIFF files on *any* platform can be either big- or little-endian;
  * it's not platform-specific.
  *
  * AFAICT, libtiff doesn't provide a method to do this automatically, and
  * hence my manual comparison. - Jonathan Merritt (lancelet) 4th Sept 2005.
  */
-#define IMB_TIFF_NCB 4         /* number of comparison bytes used */
-int imb_is_a_tiff(unsigned char *mem)
+#define IMB_TIFF_NCB 4      /* number of comparison bytes used */
+int imb_is_a_tiff(const unsigned char *mem)
 {
        char big_endian[IMB_TIFF_NCB] = { 0x4d, 0x4d, 0x00, 0x2a };
        char lil_endian[IMB_TIFF_NCB] = { 0x49, 0x49, 0x2a, 0x00 };
 
-       return ( (memcmp(big_endian, mem, IMB_TIFF_NCB) == 0) ||
-                (memcmp(lil_endian, mem, IMB_TIFF_NCB) == 0) );
+       return ((memcmp(big_endian, mem, IMB_TIFF_NCB) == 0) ||
+               (memcmp(lil_endian, mem, IMB_TIFF_NCB) == 0));
 }
 
-static void scanline_contig_16bit(float *rectf, unsigned short *sbuf, int scanline_w, int spp)
+static void scanline_contig_16bit(float *rectf, const 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;
+       for (i = 0; i < scanline_w; i++) {
+               rectf[i * 4 + 0] = sbuf[i * spp + 0] / 65535.0;
+               rectf[i * 4 + 1] = (spp >= 3) ? sbuf[i * spp + 1] / 65535.0 : sbuf[i * spp + 0] / 65535.0;
+               rectf[i * 4 + 2] = (spp >= 3) ? sbuf[i * spp + 2] / 65535.0 : sbuf[i * spp + 0] / 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)
+static void scanline_contig_32bit(float *rectf, const 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.0f;
+       for (i = 0; i < scanline_w; i++) {
+               rectf[i * 4 + 0] = fbuf[i * spp + 0];
+               rectf[i * 4 + 1] = (spp >= 3) ? fbuf[i * spp + 1] : fbuf[i * spp + 0];
+               rectf[i * 4 + 2] = (spp >= 3) ? fbuf[i * spp + 2] : fbuf[i * spp + 0];
+               rectf[i * 4 + 3] = (spp == 4) ? fbuf[i * spp + 3] : 1.0f;
        }
 }
 
-static void scanline_separate_16bit(float *rectf, unsigned short *sbuf, int scanline_w, int chan)
+static void scanline_separate_16bit(float *rectf, const 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;
+       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)
+static void scanline_separate_32bit(float *rectf, const float *fbuf, int scanline_w, int chan)
 {
        int i;
-       for (i=0; i < scanline_w; i++)
-               rectf[i*4 + chan] = fbuf[i];
+       for (i = 0; i < scanline_w; i++)
+               rectf[i * 4 + chan] = fbuf[i];
 }
 
 static void imb_read_tiff_resolution(ImBuf *ibuf, TIFF *image)
@@ -361,13 +359,13 @@ static void imb_read_tiff_resolution(ImBuf *ibuf, TIFF *image)
        TIFFGetFieldDefaulted(image, TIFFTAG_XRESOLUTION, &xres);
        TIFFGetFieldDefaulted(image, TIFFTAG_YRESOLUTION, &yres);
 
-       if(unit == RESUNIT_CENTIMETER) {
-               ibuf->ppm[0]= (double)xres * 100.0;
-               ibuf->ppm[1]= (double)yres * 100.0;
+       if (unit == RESUNIT_CENTIMETER) {
+               ibuf->ppm[0] = (double)xres * 100.0;
+               ibuf->ppm[1] = (double)yres * 100.0;
        }
        else {
-               ibuf->ppm[0]= (double)xres / 0.0254;
-               ibuf->ppm[1]= (double)yres / 0.0254;
+               ibuf->ppm[0] = (double)xres / 0.0254;
+               ibuf->ppm[1] = (double)yres / 0.0254;
        }
 }
 
@@ -376,20 +374,37 @@ static void imb_read_tiff_resolution(ImBuf *ibuf, 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)
+static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image)
 {
-       ImBuf *tmpibuf;
-       int success= 0;
+       ImBuf *tmpibuf = NULL;
+       int success = 0;
        short bitspersample, spp, config;
        size_t scanline;
-       int ib_flag=0, row, chan;
-       float *fbuf=NULL;
-       unsigned short *sbuf=NULL;
+       int ib_flag = 0, row, chan;
+       float *fbuf = NULL;
+       unsigned short *sbuf = NULL;
 
        TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bitspersample);
-       TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);             /* number of 'channels' */
+       TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);     /* number of 'channels' */
        TIFFGetField(image, TIFFTAG_PLANARCONFIG, &config);
 
+       if (spp == 4) {
+               /* HACK: this is really tricky hack, which is only needed to force libtiff
+                *       do not touch RGB channels when there's alpha channel present
+                *       The thing is: libtiff will premul RGB if alpha mode is set to
+                *       unassociated, which really conflicts with blender's assumptions
+                *
+                *       Alternative would be to unpremul after load, but it'll be really
+                *       lossy and unwanted behavior
+                *
+                *       So let's keep this thing here for until proper solution is found (sergey)
+                */
+
+               unsigned short extraSampleTypes[1];
+               extraSampleTypes[0] = EXTRASAMPLE_ASSOCALPHA;
+               TIFFSetField(image, TIFFTAG_EXTRASAMPLES, 1, extraSampleTypes);
+       }
+
        imb_read_tiff_resolution(ibuf, image);
 
        scanline = TIFFScanlineSize(image);
@@ -397,14 +412,25 @@ static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image, int premul)
        if (bitspersample == 32) {
                ib_flag = IB_rectfloat;
                fbuf = (float *)_TIFFmalloc(scanline);
-       } else if (bitspersample == 16) {
+               if (!fbuf) {
+                       goto cleanup;
+               }
+       }
+       else if (bitspersample == 16) {
                ib_flag = IB_rectfloat;
                sbuf = (unsigned short *)_TIFFmalloc(scanline);
-       } else {
+               if (!sbuf) {
+                       goto cleanup;
+               }
+       }
+       else {
                ib_flag = IB_rect;
        }
        
-       tmpibuf= IMB_allocImBuf(ibuf->x, ibuf->y, ibuf->planes, ib_flag);
+       tmpibuf = IMB_allocImBuf(ibuf->x, ibuf->y, ibuf->planes, ib_flag);
+       if (!tmpibuf) {
+               goto cleanup;
+       }
        
        /* simple RGBA image */
        if (!(bitspersample == 32 || bitspersample == 16)) {
@@ -413,73 +439,75 @@ static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image, int premul)
        /* contiguous channels: RGBRGBRGB */
        else if (config == PLANARCONFIG_CONTIG) {
                for (row = 0; row < ibuf->y; row++) {
-                       int ib_offset = ibuf->x*ibuf->y*4 - ibuf->x*4 * (row+1);
+                       size_t ib_offset = (size_t)ibuf->x * 4 * ((size_t)ibuf->y - ((size_t)row + 1));
                
                        if (bitspersample == 32) {
                                success |= TIFFReadScanline(image, fbuf, row, 0);
-                               scanline_contig_32bit(tmpibuf->rect_float+ib_offset, fbuf, ibuf->x, spp);
+                               scanline_contig_32bit(tmpibuf->rect_float + ib_offset, fbuf, ibuf->x, spp);
                                
-                       } else if (bitspersample == 16) {
+                       }
+                       else if (bitspersample == 16) {
                                success |= TIFFReadScanline(image, sbuf, row, 0);
-                               scanline_contig_16bit(tmpibuf->rect_float+ib_offset, sbuf, ibuf->x, spp);
+                               scanline_contig_16bit(tmpibuf->rect_float + ib_offset, sbuf, ibuf->x, spp);
                        }
                }
-       /* separate channels: RRRGGGBBB */
-       } else if (config == PLANARCONFIG_SEPARATE) {
+               /* 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);
+                               size_t ib_offset = (size_t)ibuf->x * 4 * ((size_t)ibuf->y - ((size_t)row + 1));
                                
                                if (bitspersample == 32) {
                                        if (chan == 3 && spp == 3) /* fill alpha if only RGB TIFF */
-                                               memset(fbuf, 1.0, sizeof(fbuf));
+                                               copy_vn_fl(fbuf, ibuf->x, 1.0f);
+                                       else if (chan >= spp) /* for grayscale, duplicate first channel into G and B */
+                                               success |= TIFFReadScanline(image, fbuf, row, 0);
                                        else
                                                success |= TIFFReadScanline(image, fbuf, row, chan);
-                                       scanline_separate_32bit(tmpibuf->rect_float+ib_offset, fbuf, ibuf->x, chan);
+                                       scanline_separate_32bit(tmpibuf->rect_float + ib_offset, fbuf, ibuf->x, chan);
                                        
-                               } else if (bitspersample == 16) {
+                               }
+                               else if (bitspersample == 16) {
                                        if (chan == 3 && spp == 3) /* fill alpha if only RGB TIFF */
-                                               memset(sbuf, 65535, sizeof(sbuf));
+                                               copy_vn_ushort(sbuf, ibuf->x, 65535);
+                                       else if (chan >= spp) /* for grayscale, duplicate first channel into G and B */
+                                               success |= TIFFReadScanline(image, fbuf, row, 0);
                                        else
                                                success |= TIFFReadScanline(image, sbuf, row, chan);
-                                       scanline_separate_16bit(tmpibuf->rect_float+ib_offset, sbuf, ibuf->x, chan);
+                                       scanline_separate_16bit(tmpibuf->rect_float + ib_offset, sbuf, ibuf->x, chan);
                                        
                                }
                        }
                }
        }
-       
-       if (bitspersample == 32)
-               _TIFFfree(fbuf);
-       else if (bitspersample == 16)
-               _TIFFfree(sbuf);
-
-       if(success) {
-               ibuf->profile = (bitspersample==32)?IB_PROFILE_LINEAR_RGB:IB_PROFILE_SRGB;
 
-//             Code seems to be not needed for 16 bits tif, on PPC G5 OSX (ton)
-               if(bitspersample < 16)
-                       if(ENDIAN_ORDER == B_ENDIAN)
+       if (success) {
+               /* Code seems to be not needed for 16 bits tif, on PPC G5 OSX (ton) */
+               if (bitspersample < 16)
+                       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->rect_float = tmpibuf->rect_float;
+               else
+                       ibuf->rect = tmpibuf->rect;
                ibuf->mall |= ib_flag;
                ibuf->flags |= ib_flag;
                
                tmpibuf->mall &= ~ib_flag;
        }
 
+cleanup:
+       if (bitspersample == 32)
+               _TIFFfree(fbuf);
+       else if (bitspersample == 16)
+               _TIFFfree(sbuf);
+
        IMB_freeImBuf(tmpibuf);
        
        return success;
@@ -487,7 +515,7 @@ static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image, int premul)
 
 void imb_inittiff(void)
 {
-       if (!(G.f & G_DEBUG))
+       if (!(G.debug & G_DEBUG))
                TIFFSetErrorHandler(NULL);
 }
 
@@ -495,14 +523,14 @@ void imb_inittiff(void)
  * Loads a TIFF file.
  *
  *
- * @param mem:   Memory containing the TIFF file.
- * @param size:  Size of the mem buffer.
- * @param flags: If flags has IB_test set then the file is not actually loaded,
+ * \param mem:   Memory containing the TIFF file.
+ * \param size:  Size of the mem buffer.
+ * \param flags: If flags has IB_test set then the file is not actually loaded,
  *                but all other operations take place.
  *
- * @return: A newly allocated ImBuf structure if successful, otherwise NULL.
+ * \return: A newly allocated ImBuf structure if successful, otherwise NULL.
  */
-ImBuf *imb_loadtiff(unsigned char *mem, size_t size, int flags)
+ImBuf *imb_loadtiff(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
 {
        TIFF *image = NULL;
        ImBuf *ibuf = NULL, *hbuf;
@@ -512,18 +540,22 @@ ImBuf *imb_loadtiff(unsigned char *mem, size_t size, int flags)
        int level;
        short spp;
        int ib_depth;
+       int found;
 
        /* check whether or not we have a TIFF file */
-       if(size < IMB_TIFF_NCB) {
+       if (size < IMB_TIFF_NCB) {
                fprintf(stderr, "imb_loadtiff: size < IMB_TIFF_NCB\n");
                return NULL;
        }
-       if(imb_is_a_tiff(mem) == 0)
+       if (imb_is_a_tiff(mem) == 0)
                return NULL;
 
+       /* both 8 and 16 bit PNGs are default to standard byte colorspace */
+       colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE);
+
        image = imb_tiff_client_open(&memFile, mem, size);
 
-       if(image == NULL) {
+       if (image == NULL) {
                printf("imb_loadtiff: could not open TIFF IO layer.\n");
                return NULL;
        }
@@ -533,62 +565,72 @@ ImBuf *imb_loadtiff(unsigned char *mem, size_t size, int flags)
        TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
        TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);
        
-       ib_depth = (spp==3)?24:32;
+       ib_depth = (spp == 3) ? 24 : 32;
        
        ibuf = IMB_allocImBuf(width, height, ib_depth, 0);
-       if(ibuf) {
-               ibuf->ftype = TIF;
+       if (ibuf) {
+               ibuf->ftype = IMB_FTYPE_TIF;
        }
        else {
                fprintf(stderr, 
-                       "imb_loadtiff: could not allocate memory for TIFF " \
-                       "image.\n");
+                       "imb_loadtiff: could not allocate memory for TIFF "
+                       "image.\n");
                TIFFClose(image);
                return NULL;
        }
 
+       /* get alpha mode from file header */
+       if (flags & IB_alphamode_detect) {
+               if (spp == 4) {
+                       unsigned short extra, *extraSampleTypes;
+
+                       found = TIFFGetField(image, TIFFTAG_EXTRASAMPLES, &extra, &extraSampleTypes);
+
+                       if (found && (extraSampleTypes[0] == EXTRASAMPLE_ASSOCALPHA)) {
+                               ibuf->flags |= IB_alphamode_premul;
+                       }
+               }
+       }
+
        /* if testing, we're done */
-       if(flags & IB_test) {
+       if (flags & IB_test) {
                TIFFClose(image);
                return ibuf;
        }
 
        /* detect if we are reading a tiled/mipmapped texture, in that case
-          we don't read pixels but leave it to the cache to load tiles */
-       if(flags & IB_tilecache) {
-               format= NULL;
+        * we don't read pixels but leave it to the cache to load tiles */
+       if (flags & IB_tilecache) {
+               format = NULL;
                TIFFGetField(image, TIFFTAG_PIXAR_TEXTUREFORMAT, &format);
 
-               if(format && strcmp(format, "Plain Texture")==0 && TIFFIsTiled(image)) {
+               if (format && STREQ(format, "Plain Texture") && TIFFIsTiled(image)) {
                        int numlevel = TIFFNumberOfDirectories(image);
 
                        /* create empty mipmap levels in advance */
-                       for(level=0; level<numlevel; level++) {
-                               if(!TIFFSetDirectory(image, level))
+                       for (level = 0; level < numlevel; level++) {
+                               if (!TIFFSetDirectory(image, level))
                                        break;
 
-                               if(level > 0) {
-                                       width= (width > 1)? width/2: 1;
-                                       height= (height > 1)? height/2: 1;
+                               if (level > 0) {
+                                       width = (width > 1) ? width / 2 : 1;
+                                       height = (height > 1) ? height / 2 : 1;
 
-                                       hbuf= IMB_allocImBuf(width, height, 32, 0);
-                                       hbuf->miplevel= level;
-                                       hbuf->ftype= ibuf->ftype;
-                                       ibuf->mipmap[level-1] = hbuf;
-
-                                       if(flags & IB_premul)
-                                               hbuf->flags |= IB_premul;
+                                       hbuf = IMB_allocImBuf(width, height, 32, 0);
+                                       hbuf->miplevel = level;
+                                       hbuf->ftype = ibuf->ftype;
+                                       ibuf->mipmap[level - 1] = hbuf;
                                }
                                else
-                                       hbuf= ibuf;
+                                       hbuf = ibuf;
 
                                hbuf->flags |= IB_tilecache;
 
                                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);
+                               hbuf->xtiles = ceil(hbuf->x / (float)hbuf->tilex);
+                               hbuf->ytiles = ceil(hbuf->y / (float)hbuf->tiley);
 
                                imb_addtilesImBuf(hbuf);
 
@@ -598,7 +640,7 @@ ImBuf *imb_loadtiff(unsigned char *mem, size_t size, int flags)
        }
 
        /* read pixels */
-       if(!(ibuf->flags & IB_tilecache) && !imb_read_tiff_pixels(ibuf, image, 0)) {
+       if (!(ibuf->flags & IB_tilecache) && !imb_read_tiff_pixels(ibuf, image)) {
                fprintf(stderr, "imb_loadtiff: Failed to read tiff image.\n");
                TIFFClose(image);
                return NULL;
@@ -611,7 +653,7 @@ ImBuf *imb_loadtiff(unsigned char *mem, size_t size, int flags)
        return ibuf;
 }
 
-void imb_loadtiletiff(ImBuf *ibuf, unsigned char *mem, size_t size, int tx, int ty, unsigned int *rect)
+void imb_loadtiletiff(ImBuf *ibuf, const unsigned char *mem, size_t size, int tx, int ty, unsigned int *rect)
 {
        TIFF *image = NULL;
        uint32 width, height;
@@ -619,24 +661,21 @@ void imb_loadtiletiff(ImBuf *ibuf, unsigned char *mem, size_t size, int tx, int
 
        image = imb_tiff_client_open(&memFile, mem, size);
 
-       if(image == NULL) {
+       if (image == NULL) {
                printf("imb_loadtiff: could not open TIFF IO layer for loading mipmap level.\n");
                return;
        }
 
-       if(TIFFSetDirectory(image, ibuf->miplevel)) { /* allocate the image buffer */
+       if (TIFFSetDirectory(image, ibuf->miplevel)) { /* allocate the image buffer */
                TIFFGetField(image, TIFFTAG_IMAGEWIDTH,  &width);
                TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
 
-               if(width == ibuf->x && height == ibuf->y) {
-                       if(rect) {
+               if (width == ibuf->x && height == ibuf->y) {
+                       if (rect) {
                                /* tiff pixels are bottom to top, tiles are top to bottom */
-                               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);
-
-                                       if(ibuf->flags & IB_premul)
-                                               IMB_premultiply_rect(rect, 32, ibuf->tilex, ibuf->tiley);
+                               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);
                                }
                                else
                                        printf("imb_loadtiff: failed to read tiff tile at mipmap level %d\n", ibuf->miplevel);
@@ -661,11 +700,11 @@ void imb_loadtiletiff(ImBuf *ibuf, unsigned char *mem, size_t size, int tx, int
  * Blender by setting "Premul" alpha handling.  Other alpha conventions are
  * not strictly correct, but are permitted anyhow.
  *
- * @param ibuf:  Image buffer.
- * @param name:  Name of the TIFF file to create.
- * @param flags: Currently largely ignored.
+ * \param ibuf:  Image buffer.
+ * \param name:  Name of the TIFF file to create.
+ * \param flags: Currently largely ignored.
  *
- * @return: 1 if the function is successful, 0 on failure.
+ * \return: 1 if the function is successful, 0 on failure.
  */
 
 int imb_savetiff(ImBuf *ibuf, const char *name, int flags)
@@ -679,66 +718,78 @@ int imb_savetiff(ImBuf *ibuf, const char *name, int flags)
        float *fromf = NULL;
        float xres, yres;
        int x, y, from_i, to_i, i;
-       int extraSampleTypes[1] = { EXTRASAMPLE_ASSOCALPHA };
-       
+       int compress_mode = COMPRESSION_NONE;
 
        /* 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
         * to gray, RGB, RGBA respectively. */
        samplesperpixel = (uint16)((ibuf->planes + 7) >> 3);
-       if((samplesperpixel > 4) || (samplesperpixel == 2)) {
+       if ((samplesperpixel > 4) || (samplesperpixel == 2)) {
                fprintf(stderr,
-                       "imb_savetiff: unsupported number of bytes per " 
-                       "pixel: %d\n", samplesperpixel);
+                       "imb_savetiff: unsupported number of bytes per "
+                       "pixel: %d\n", samplesperpixel);
                return (0);
        }
 
-       if((ibuf->ftype & TIF_16BIT) && ibuf->rect_float)
+       if ((ibuf->foptions.flag & TIF_16BIT) && ibuf->rect_float)
                bitspersample = 16;
        else
                bitspersample = 8;
 
+       if (ibuf->foptions.flag & TIF_COMPRESS_DEFLATE)
+               compress_mode = COMPRESSION_DEFLATE;
+       else if (ibuf->foptions.flag & TIF_COMPRESS_LZW)
+               compress_mode = COMPRESSION_LZW;
+       else if (ibuf->foptions.flag & TIF_COMPRESS_PACKBITS)
+               compress_mode = COMPRESSION_PACKBITS;
+
        /* open TIFF file for writing */
-       if(flags & IB_mem) {
+       if (flags & IB_mem) {
                /* bork at the creation of a TIFF in memory */
                fprintf(stderr,
-                       "imb_savetiff: creation of in-memory TIFF files is " 
-                       "not yet supported.\n");
+                       "imb_savetiff: creation of in-memory TIFF files is "
+                       "not yet supported.\n");
                return (0);
        }
        else {
                /* create image as a file */
+#ifdef WIN32
+               wchar_t *wname = alloc_utf16_from_8(name, 0);
+               image = TIFFOpenW(wname, "w");
+               free(wname);
+#else
                image = TIFFOpen(name, "w");
+#endif
        }
-       if(image == NULL) {
+       if (image == NULL) {
                fprintf(stderr,
-                       "imb_savetiff: could not open TIFF for writing.\n");
+                       "imb_savetiff: could not open TIFF for writing.\n");
                return (0);
        }
 
        /* allocate array for pixel data */
        npixels = ibuf->x * ibuf->y;
-       if(bitspersample == 16)
-               pixels16 = (unsigned short*)_TIFFmalloc(npixels *
-                       samplesperpixel * sizeof(unsigned short));
+       if (bitspersample == 16)
+               pixels16 = (unsigned short *)_TIFFmalloc(npixels *
+                                                        samplesperpixel * sizeof(unsigned short));
        else
-               pixels = (unsigned char*)_TIFFmalloc(npixels *
-                       samplesperpixel * sizeof(unsigned char));
+               pixels = (unsigned char *)_TIFFmalloc(npixels *
+                                                     samplesperpixel * sizeof(unsigned char));
 
-       if(pixels == NULL && pixels16 == NULL) {
+       if (pixels == NULL && pixels16 == NULL) {
                fprintf(stderr,
-                       "imb_savetiff: could not allocate pixels array.\n");
+                       "imb_savetiff: could not allocate pixels array.\n");
                TIFFClose(image);
                return (0);
        }
 
        /* setup pointers */
-       if(bitspersample == 16) {
+       if (bitspersample == 16) {
                fromf = ibuf->rect_float;
                to16   = pixels16;
        }
        else {
-               from = (unsigned char*)ibuf->rect;
+               from = (unsigned char *)ibuf->rect;
                to   = pixels;
        }
 
@@ -746,51 +797,82 @@ int imb_savetiff(ImBuf *ibuf, const char *name, int flags)
        TIFFSetField(image, TIFFTAG_BITSPERSAMPLE, bitspersample);
        TIFFSetField(image, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
 
-       if(samplesperpixel == 4) {
+       if (samplesperpixel == 4) {
+               unsigned short extraSampleTypes[1];
+
+               if (bitspersample == 16)
+                       extraSampleTypes[0] = EXTRASAMPLE_ASSOCALPHA;
+               else
+                       extraSampleTypes[0] = EXTRASAMPLE_UNASSALPHA;
+
                /* RGBA images */
                TIFFSetField(image, TIFFTAG_EXTRASAMPLES, 1,
-                               extraSampleTypes);
+                            extraSampleTypes);
                TIFFSetField(image, TIFFTAG_PHOTOMETRIC, 
-                               PHOTOMETRIC_RGB);
+                            PHOTOMETRIC_RGB);
        }
-       else if(samplesperpixel == 3) {
+       else if (samplesperpixel == 3) {
                /* RGB images */
                TIFFSetField(image, TIFFTAG_PHOTOMETRIC,
-                               PHOTOMETRIC_RGB);
+                            PHOTOMETRIC_RGB);
        }
-       else if(samplesperpixel == 1) {
-               /* greyscale images, 1 channel */
+       else if (samplesperpixel == 1) {
+               /* grayscale images, 1 channel */
                TIFFSetField(image, TIFFTAG_PHOTOMETRIC,
-                               PHOTOMETRIC_MINISBLACK);
+                            PHOTOMETRIC_MINISBLACK);
        }
 
        /* copy pixel data.  While copying, we flip the image vertically. */
-       for(x = 0; x < ibuf->x; x++) {
-               for(y = 0; y < ibuf->y; y++) {
-                       from_i = 4*(y*ibuf->x+x);
-                       to_i   = samplesperpixel*((ibuf->y-y-1)*ibuf->x+x);
+       const int channels_in_float = ibuf->channels ? ibuf->channels : 4;
+       for (x = 0; x < ibuf->x; x++) {
+               for (y = 0; y < ibuf->y; y++) {
+                       from_i = ((size_t)channels_in_float) * (y * ibuf->x + x);
+                       to_i   = samplesperpixel * ((ibuf->y - y - 1) * ibuf->x + x);
 
-                       if(pixels16) {
+                       if (pixels16) {
                                /* convert from float source */
-                               float rgb[3];
-                               
-                               if (ibuf->profile == IB_PROFILE_LINEAR_RGB)
-                                       linearrgb_to_srgb_v3_v3(rgb, &fromf[from_i]);
-                               else
-                                       copy_v3_v3(rgb, &fromf[from_i]);
-
-                               to16[to_i+0] = FTOUSHORT(rgb[0]);
-                               to16[to_i+1] = FTOUSHORT(rgb[1]);
-                               to16[to_i+2] = FTOUSHORT(rgb[2]);
-                               to_i += 3; from_i+=3;
-                               
-                               if (samplesperpixel == 4) {
-                                       to16[to_i+3] = FTOUSHORT(fromf[from_i+3]);
-                                       /*to_i++; from_i++;*/ /*unused, set on each loop */
+                               float rgb[4];
+
+                               if (channels_in_float == 3 || channels_in_float == 4) {
+                                       if (ibuf->float_colorspace ||
+                                           (ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA))
+                                       {
+                                               /* Float buffer was managed already, no need in color
+                                                * space conversion.
+                                                */
+                                               copy_v3_v3(rgb, &fromf[from_i]);
+                                       }
+                                       else {
+                                               /* Standard linear-to-srgb conversion if float buffer
+                                                * wasn't managed.
+                                                */
+                                               linearrgb_to_srgb_v3_v3(rgb, &fromf[from_i]);
+                                       }
+                                       if (channels_in_float == 4) {
+                                               rgb[3] = fromf[from_i + 3];
+                                       }
+                                       else {
+                                               rgb[3] = 1.0f;
+                                       }
+                               }
+                               else {
+                                       if (ibuf->float_colorspace ||
+                                           (ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA))
+                                       {
+                                               rgb[0] = fromf[from_i];
+                                       }
+                                       else {
+                                               rgb[0] = linearrgb_to_srgb(fromf[from_i]);
+                                       }
+                                       rgb[1] = rgb[2] = rgb[0];
+                                       rgb[3] = 1.0f;
                                }
+
+                               for (i = 0; i < samplesperpixel; i++, to_i++)
+                                       to16[to_i] = FTOUSHORT(rgb[i]);
                        }
                        else {
-                               for(i = 0; i < samplesperpixel; i++, to_i++, from_i++)
+                               for (i = 0; i < samplesperpixel; i++, to_i++, from_i++)
                                        to[to_i] = from[from_i];
                        }
                }
@@ -800,36 +882,37 @@ int imb_savetiff(ImBuf *ibuf, const char *name, int flags)
        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_COMPRESSION, compress_mode);
        TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
        TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
 
 
-       if(ibuf->ppm[0] > 0.0 && ibuf->ppm[1] > 0.0) {
-               xres= (float)(ibuf->ppm[0] * 0.0254);
-               yres= (float)(ibuf->ppm[1] * 0.0254);
+       if (ibuf->ppm[0] > 0.0 && ibuf->ppm[1] > 0.0) {
+               xres = (float)(ibuf->ppm[0] * 0.0254);
+               yres = (float)(ibuf->ppm[1] * 0.0254);
        }
        else {
-               xres= yres= 150.0f;
+               xres = yres = IMB_DPI_DEFAULT;
        }
 
        TIFFSetField(image, TIFFTAG_XRESOLUTION,     xres);
        TIFFSetField(image, TIFFTAG_YRESOLUTION,     yres);
        TIFFSetField(image, TIFFTAG_RESOLUTIONUNIT,  RESUNIT_INCH);
-       if(TIFFWriteEncodedStrip(image, 0,
-                       (bitspersample == 16)? (unsigned char*)pixels16: pixels,
-                       ibuf->x*ibuf->y*samplesperpixel*bitspersample/8) == -1) {
+       if (TIFFWriteEncodedStrip(image, 0,
+                                 (bitspersample == 16) ? (unsigned char *)pixels16 : pixels,
+                                 (size_t)ibuf->x * ibuf->y * samplesperpixel * bitspersample / 8) == -1)
+       {
                fprintf(stderr,
-                       "imb_savetiff: Could not write encoded TIFF.\n");
+                       "imb_savetiff: Could not write encoded TIFF.\n");
                TIFFClose(image);
-               if(pixels) _TIFFfree(pixels);
-               if(pixels16) _TIFFfree(pixels16);
+               if (pixels) _TIFFfree(pixels);
+               if (pixels16) _TIFFfree(pixels16);
                return (1);
        }
 
        /* close the TIFF file */
        TIFFClose(image);
-       if(pixels) _TIFFfree(pixels);
-       if(pixels16) _TIFFfree(pixels16);
+       if (pixels) _TIFFfree(pixels);
+       if (pixels16) _TIFFfree(pixels16);
        return (1);
 }