diff --git a/Source/FreeImage/PluginTIFF.cpp b/Source/FreeImage/PluginTIFF.cpp index 8455495..578a00b 100644 --- a/Source/FreeImage/PluginTIFF.cpp +++ b/Source/FreeImage/PluginTIFF.cpp @@ -1,7 +1,7 @@ // ========================================================== // TIFF Loader and Writer // -// Design and implementation by +// Design and implementation by // - Floris van den Berg (flvdberg@wxs.nl) // - Hervé Drolon (drolon@infonie.fr) // - Markus Loibl (markus.loibl@epost.de) @@ -24,7 +24,7 @@ // Use at your own risk! // ========================================================== -#ifdef _MSC_VER +#ifdef _MSC_VER #pragma warning (disable : 4786) // identifier was truncated to 'number' characters #endif @@ -67,10 +67,10 @@ void tiff_ConvertLineRGBToXYZ(BYTE *target, BYTE *source, int width_in_pixels); /** Supported loading methods */ typedef enum { - LoadAsRBGA = 0, - LoadAsCMYK = 1, - LoadAs8BitTrns = 2, - LoadAsGenericStrip = 3, + LoadAsRBGA = 0, + LoadAsCMYK = 1, + LoadAs8BitTrns = 2, + LoadAsGenericStrip = 3, LoadAsTiled = 4, LoadAsLogLuv = 5, LoadAsHalfFloat = 6 @@ -133,10 +133,10 @@ typedef struct { } fi_TIFFIO; // ---------------------------------------------------------- -// libtiff interface +// libtiff interface // ---------------------------------------------------------- -static tmsize_t +static tmsize_t _tiffReadProc(thandle_t handle, void *buf, tmsize_t size) { fi_TIFFIO *fio = (fi_TIFFIO*)handle; return fio->io->read_proc(buf, (unsigned)size, 1, fio->handle) * size; @@ -197,26 +197,18 @@ TIFFFdOpen(thandle_t handle, const char *name, const char *mode) { return tif; } -/** -Open a TIFF file for reading or writing -@param name -@param mode -*/ -TIFF* -TIFFOpen(const char* name, const char* mode) { - return 0; -} - // ---------------------------------------------------------- // TIFF library FreeImage-specific routines. // ---------------------------------------------------------- void* _TIFFmalloc(tmsize_t s) { + if (s == 0) + return ((void *)NULL); return malloc(s); } -void* +void* _TIFFcalloc(tmsize_t nmemb, tmsize_t siz) { if (nmemb == 0 || siz == 0) { return ((void *)NULL); @@ -231,28 +223,29 @@ _TIFFfree(void *p) { void* _TIFFrealloc(void* p, tmsize_t s) { - return realloc(p, s); + return realloc(p, (size_t)s); } void _TIFFmemset(void* p, int v, tmsize_t c) { - memset(p, v, (size_t) c); + memset(p, v, (size_t)c); } void _TIFFmemcpy(void* d, const void* s, tmsize_t c) { - memcpy(d, s, (size_t) c); + memcpy(d, s, (size_t)c); } int _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c) { - return (memcmp(p1, p2, (size_t) c)); + return memcmp(p1, p2, (size_t)c); } // ---------------------------------------------------------- // in FreeImage warnings and errors are disabled // ---------------------------------------------------------- +#ifndef _WIN32 static void msdosWarningHandler(const char* module, const char* fmt, va_list ap) { } @@ -273,7 +266,7 @@ msdosErrorHandler(const char* module, const char* fmt, va_list ap) { } TIFFErrorHandler _TIFFerrorHandler = msdosErrorHandler; - +#endif // ndef _WIN32 // ---------------------------------------------------------- #define CVT(x) (((x) * 255L) / ((1L<<16)-1)) @@ -320,7 +313,7 @@ GetPhotometric(FIBITMAP *dib) { /** Get the resolution from the TIFF and fill the dib with universal units */ -static void +static void ReadResolution(TIFF *tiff, FIBITMAP *dib) { float fResX = 300.0; float fResY = 300.0; @@ -329,7 +322,7 @@ ReadResolution(TIFF *tiff, FIBITMAP *dib) { TIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &resUnit); TIFFGetField(tiff, TIFFTAG_XRESOLUTION, &fResX); TIFFGetField(tiff, TIFFTAG_YRESOLUTION, &fResY); - + // If we don't have a valid resolution unit and valid resolution is specified then assume inch if (resUnit == RESUNIT_NONE && fResX > 0.0 && fResY > 0.0) { resUnit = RESUNIT_INCH; @@ -346,7 +339,7 @@ ReadResolution(TIFF *tiff, FIBITMAP *dib) { /** Set the resolution to the TIFF using english units */ -static void +static void WriteResolution(TIFF *tiff, FIBITMAP *dib) { double res; @@ -362,7 +355,7 @@ WriteResolution(TIFF *tiff, FIBITMAP *dib) { /** Fill the dib palette according to the TIFF photometric */ -static void +static void ReadPalette(TIFF *tiff, uint16_t photometric, uint16_t bitspersample, FIBITMAP *dib) { RGBQUAD *pal = FreeImage_GetPalette(dib); @@ -405,8 +398,8 @@ ReadPalette(TIFF *tiff, uint16_t photometric, uint16_t bitspersample, FIBITMAP * uint16_t *red; uint16_t *green; uint16_t *blue; - - TIFFGetField(tiff, TIFFTAG_COLORMAP, &red, &green, &blue); + + TIFFGetField(tiff, TIFFTAG_COLORMAP, &red, &green, &blue); // load the palette in the DIB @@ -414,13 +407,13 @@ ReadPalette(TIFF *tiff, uint16_t photometric, uint16_t bitspersample, FIBITMAP * for (int i = (1 << bitspersample) - 1; i >= 0; i--) { pal[i].rgbRed =(BYTE) CVT(red[i]); pal[i].rgbGreen = (BYTE) CVT(green[i]); - pal[i].rgbBlue = (BYTE) CVT(blue[i]); + pal[i].rgbBlue = (BYTE) CVT(blue[i]); } } else { for (int i = (1 << bitspersample) - 1; i >= 0; i--) { pal[i].rgbRed = (BYTE) red[i]; pal[i].rgbGreen = (BYTE) green[i]; - pal[i].rgbBlue = (BYTE) blue[i]; + pal[i].rgbBlue = (BYTE) blue[i]; } } @@ -428,7 +421,7 @@ ReadPalette(TIFF *tiff, uint16_t photometric, uint16_t bitspersample, FIBITMAP * } } -/** +/** Allocate a FIBITMAP @param header_only If TRUE, allocate a 'header only' FIBITMAP, otherwise allocate a full FIBITMAP @param fit Image type @@ -438,7 +431,7 @@ Allocate a FIBITMAP @param samplesperpixel # samples per pixel @return Returns the allocated image if successful, returns NULL otherwise */ -static FIBITMAP* +static FIBITMAP* CreateImageType(BOOL header_only, FREE_IMAGE_TYPE fit, int width, int height, uint16_t bitspersample, uint16_t samplesperpixel) { FIBITMAP *dib = NULL; @@ -450,10 +443,10 @@ CreateImageType(BOOL header_only, FREE_IMAGE_TYPE fit, int width, int height, ui int bpp = bitspersample * samplesperpixel; if(fit == FIT_BITMAP) { - // standard bitmap type + // standard bitmap type if(bpp == 16) { - + if((samplesperpixel == 2) && (bitspersample == 8)) { // 8-bit indexed + 8-bit alpha channel -> convert to 8-bit transparent dib = FreeImage_AllocateHeader(header_only, width, height, 8); @@ -461,7 +454,7 @@ CreateImageType(BOOL header_only, FREE_IMAGE_TYPE fit, int width, int height, ui // 16-bit RGB -> expect it to be 565 dib = FreeImage_AllocateHeader(header_only, width, height, bpp, FI16_565_RED_MASK, FI16_565_GREEN_MASK, FI16_565_BLUE_MASK); } - + } else if (bpp <= 32) { dib = FreeImage_AllocateHeader(header_only, width, height, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK); @@ -469,24 +462,24 @@ CreateImageType(BOOL header_only, FREE_IMAGE_TYPE fit, int width, int height, ui } else { // other bitmap types - + dib = FreeImage_AllocateHeaderT(header_only, fit, width, height, bpp); } return dib; } -/** +/** Read the TIFFTAG_SAMPLEFORMAT tag and convert to FREE_IMAGE_TYPE @param tiff LibTIFF TIFF Handle @param bitspersample # bit per sample @param samplesperpixel # samples per pixel @return Returns the image type as a FREE_IMAGE_TYPE value */ -static FREE_IMAGE_TYPE +static FREE_IMAGE_TYPE ReadImageType(TIFF *tiff, uint16_t bitspersample, uint16_t samplesperpixel) { uint16_t sampleformat = 0; - FREE_IMAGE_TYPE fit = FIT_BITMAP ; + FREE_IMAGE_TYPE fit = FIT_BITMAP ; uint16_t bpp = bitspersample * samplesperpixel; @@ -592,7 +585,7 @@ ReadImageType(TIFF *tiff, uint16_t bitspersample, uint16_t samplesperpixel) { case 16: fit = FIT_UINT16; break; - + case 32: fit = FIT_UINT32; break; @@ -601,7 +594,7 @@ ReadImageType(TIFF *tiff, uint16_t bitspersample, uint16_t samplesperpixel) { else if(samplesperpixel == 3) { if(bpp == 48) fit = FIT_RGB16; } - else if(samplesperpixel >= 4) { + else if(samplesperpixel >= 4) { if(bitspersample == 16) { fit = FIT_RGBA16; } @@ -612,12 +605,12 @@ ReadImageType(TIFF *tiff, uint16_t bitspersample, uint16_t samplesperpixel) { return fit; } -/** +/** Convert FREE_IMAGE_TYPE and write TIFFTAG_SAMPLEFORMAT @param tiff LibTIFF TIFF Handle @param fit Image type as a FREE_IMAGE_TYPE value */ -static void +static void WriteImageType(TIFF *tiff, FREE_IMAGE_TYPE fit) { switch(fit) { case FIT_BITMAP: // standard image: 1-, 4-, 8-, 16-, 24-, 32-bit @@ -655,7 +648,7 @@ Select the compression algorithm @param photometric @param flags */ -static void +static void WriteCompression(TIFF *tiff, uint16_t bitspersample, uint16_t samplesperpixel, uint16_t photometric, int flags) { uint16_t compression = COMPRESSION_LZW; uint16_t bitsperpixel = bitspersample * samplesperpixel; @@ -731,13 +724,13 @@ WriteCompression(TIFF *tiff, uint16_t bitspersample, uint16_t samplesperpixel, u TIFFSetField(tiff, TIFFTAG_COMPRESSION, compression); if ((compression == COMPRESSION_LZW) || (compression == COMPRESSION_ADOBE_DEFLATE)) { - // This option is only meaningful with LZW or ADOBE DEFLATE compression: a predictor value of 2 - // causes each scanline of the output image to undergo horizontal differencing + // This option is only meaningful with LZW or ADOBE DEFLATE compression: a predictor value of 2 + // causes each scanline of the output image to undergo horizontal differencing // before it is encoded; a value of 1 forces each scanline to be encoded without differencing. - // Found on LibTIFF mailing list : - // LZW without differencing works well for 1-bit images, 4-bit grayscale images, - // and many palette-color images. But natural 24-bit color images and some 8-bit + // Found on LibTIFF mailing list : + // LZW without differencing works well for 1-bit images, 4-bit grayscale images, + // and many palette-color images. But natural 24-bit color images and some 8-bit // grayscale images do much better with differencing. // set the default predictor value @@ -777,7 +770,7 @@ WriteCompression(TIFF *tiff, uint16_t bitspersample, uint16_t samplesperpixel, u /** Read the TIFFTAG_RICHTIFFIPTC tag (IPTC/NAA or Adobe Photoshop profile) */ -static BOOL +static BOOL tiff_read_iptc_profile(TIFF *tiff, FIBITMAP *dib) { BYTE *profile = NULL; uint32_t profile_size = 0; @@ -799,7 +792,7 @@ tiff_read_iptc_profile(TIFF *tiff, FIBITMAP *dib) { @param tiff LibTIFF TIFF handle @return Returns TRUE if successful, FALSE otherwise */ -static BOOL +static BOOL tiff_read_xmp_profile(TIFF *tiff, FIBITMAP *dib) { BYTE *profile = NULL; uint32_t profile_size = 0; @@ -834,7 +827,7 @@ tiff_read_xmp_profile(TIFF *tiff, FIBITMAP *dib) { @param tiff LibTIFF TIFF handle @return Returns TRUE if successful, FALSE otherwise */ -static BOOL +static BOOL tiff_read_exif_profile(FreeImageIO *io, fi_handle handle, TIFF *tiff, FIBITMAP *dib) { BOOL bResult = FALSE; toff_t exif_offset = 0; @@ -864,7 +857,7 @@ tiff_read_exif_profile(FreeImageIO *io, fi_handle handle, TIFF *tiff, FIBITMAP * /** Read TIFF special profiles */ -static void +static void ReadMetadata(FreeImageIO *io, fi_handle handle, TIFF *tiff, FIBITMAP *dib) { // IPTC/NAA @@ -885,7 +878,7 @@ ReadMetadata(FreeImageIO *io, fi_handle handle, TIFF *tiff, FIBITMAP *dib) { /** Write the TIFFTAG_RICHTIFFIPTC tag (IPTC/NAA or Adobe Photoshop profile) */ -static BOOL +static BOOL tiff_write_iptc_profile(TIFF *tiff, FIBITMAP *dib) { if(FreeImage_GetMetadataCount(FIMD_IPTC, dib)) { BYTE *profile = NULL; @@ -923,16 +916,16 @@ tiff_write_iptc_profile(TIFF *tiff, FIBITMAP *dib) { @param tiff LibTIFF TIFF handle @return Returns TRUE if successful, FALSE otherwise */ -static BOOL +static BOOL tiff_write_xmp_profile(TIFF *tiff, FIBITMAP *dib) { FITAG *tag_xmp = NULL; FreeImage_GetMetadata(FIMD_XMP, dib, g_TagLib_XMPFieldName, &tag_xmp); if(tag_xmp && (NULL != FreeImage_GetTagValue(tag_xmp))) { - + TIFFSetField(tiff, TIFFTAG_XMLPACKET, (uint32_t)FreeImage_GetTagLength(tag_xmp), (BYTE*)FreeImage_GetTagValue(tag_xmp)); - return TRUE; + return TRUE; } return FALSE; @@ -947,7 +940,7 @@ tiff_write_xmp_profile(TIFF *tiff, FIBITMAP *dib) { static BOOL tiff_write_exif_profile(TIFF *tiff, FIBITMAP *dib) { BOOL bResult = FALSE; - + // write EXIF_MAIN tags, EXIF_EXIF not supported yet bResult = tiff_write_exif_tags(tiff, TagLib::EXIF_MAIN, dib); @@ -957,17 +950,17 @@ tiff_write_exif_profile(TIFF *tiff, FIBITMAP *dib) { /** Write TIFF special profiles */ -static void +static void WriteMetadata(TIFF *tiff, FIBITMAP *dib) { // IPTC tiff_write_iptc_profile(tiff, dib); - + // Adobe XMP tiff_write_xmp_profile(tiff, dib); - + // EXIF_MAIN tags tiff_write_exif_profile(tiff, dib); - + // GeoTIFF tags tiff_write_geotiff_profile(tiff, dib); } @@ -1002,7 +995,7 @@ MimeType() { } static BOOL DLL_CALLCONV -Validate(FreeImageIO *io, fi_handle handle) { +Validate(FreeImageIO *io, fi_handle handle) { // TIFF signatures static const BYTE tiff_C_II[] = { 0x49, 0x49, 0x2A, 0x00 }; // Classic TIFF, little-endian static const BYTE tiff_C_MM[] = { 0x4D, 0x4D, 0x00, 0x2A }; // Classic TIFF, big-endian @@ -1051,7 +1044,7 @@ SupportsExportDepth(int depth) { ); } -static BOOL DLL_CALLCONV +static BOOL DLL_CALLCONV SupportsExportType(FREE_IMAGE_TYPE type) { return ( (type == FIT_BITMAP) || @@ -1061,9 +1054,9 @@ SupportsExportType(FREE_IMAGE_TYPE type) { (type == FIT_INT32) || (type == FIT_FLOAT) || (type == FIT_DOUBLE) || - (type == FIT_COMPLEX) || - (type == FIT_RGB16) || - (type == FIT_RGBA16) || + (type == FIT_COMPLEX) || + (type == FIT_RGB16) || + (type == FIT_RGBA16) || (type == FIT_RGBF) || (type == FIT_RGBAF) ); @@ -1077,7 +1070,7 @@ SupportsICCProfiles() { static BOOL DLL_CALLCONV SupportsNoPixels() { return TRUE; -} +} // ---------------------------------------------------------- @@ -1128,7 +1121,7 @@ PageCount(FreeImageIO *io, fi_handle handle, void *data) { do { nr_ifd++; } while (TIFFReadDirectory(tif)); - + return nr_ifd; } @@ -1144,7 +1137,7 @@ check for uncommon bitspersample values (e.g. 10, 12, ...) @param samplesperpixel TIFFTAG_SAMPLESPERPIXEL tiff tag @return Returns FALSE if a uncommon bit-depth is encountered, returns TRUE otherwise */ -static BOOL +static BOOL IsValidBitsPerSample(uint16_t photometric, uint16_t bitspersample, uint16_t samplesperpixel) { // get the pixel depth in bits const uint16_t pixel_depth = bitspersample * samplesperpixel; @@ -1171,7 +1164,7 @@ IsValidBitsPerSample(uint16_t photometric, uint16_t bitspersample, uint16_t samp switch(bitspersample) { case 1: case 4: - if((photometric == PHOTOMETRIC_MINISWHITE) || (photometric == PHOTOMETRIC_MINISBLACK) || (photometric == PHOTOMETRIC_PALETTE)) { + if((photometric == PHOTOMETRIC_MINISWHITE) || (photometric == PHOTOMETRIC_MINISBLACK) || (photometric == PHOTOMETRIC_PALETTE)) { return TRUE; } else { return FALSE; @@ -1180,14 +1173,14 @@ IsValidBitsPerSample(uint16_t photometric, uint16_t bitspersample, uint16_t samp case 8: return TRUE; case 16: - if(photometric != PHOTOMETRIC_PALETTE) { + if(photometric != PHOTOMETRIC_PALETTE) { return TRUE; } else { return FALSE; } break; case 32: - if((photometric == PHOTOMETRIC_MINISWHITE) || (photometric == PHOTOMETRIC_MINISBLACK) || (photometric == PHOTOMETRIC_LOGLUV)) { + if((photometric == PHOTOMETRIC_MINISWHITE) || (photometric == PHOTOMETRIC_MINISBLACK) || (photometric == PHOTOMETRIC_LOGLUV)) { return TRUE; } else if((photometric == PHOTOMETRIC_RGB) && (samplesperpixel == 3) || (samplesperpixel == 4)) { // RGB[A]F @@ -1198,7 +1191,7 @@ IsValidBitsPerSample(uint16_t photometric, uint16_t bitspersample, uint16_t samp break; case 64: case 128: - if(photometric == PHOTOMETRIC_MINISBLACK) { + if(photometric == PHOTOMETRIC_MINISBLACK) { return TRUE; } else { return FALSE; @@ -1207,11 +1200,11 @@ IsValidBitsPerSample(uint16_t photometric, uint16_t bitspersample, uint16_t samp default: return FALSE; } - + return FALSE; } -static TIFFLoadMethod +static TIFFLoadMethod FindLoadMethod(TIFF *tif, FREE_IMAGE_TYPE image_type, int flags) { uint16_t bitspersample = (uint16_t)-1; uint16_t samplesperpixel = (uint16_t)-1; @@ -1231,9 +1224,9 @@ FindLoadMethod(TIFF *tif, FREE_IMAGE_TYPE image_type, int flags) { // convert to 24 or 32 bits RGB if the image is full color case PHOTOMETRIC_RGB: if((image_type == FIT_RGB16) || (image_type == FIT_RGBA16)) { - // load 48-bit RGB and 64-bit RGBA without conversion + // load 48-bit RGB and 64-bit RGBA without conversion loadMethod = LoadAsGenericStrip; - } + } else if(image_type == FIT_RGBF) { if((samplesperpixel == 3) && (bitspersample == 16)) { // load 3 x 16-bit half as RGBF @@ -1251,11 +1244,11 @@ FindLoadMethod(TIFF *tif, FREE_IMAGE_TYPE image_type, int flags) { loadMethod = LoadAsLogLuv; break; case PHOTOMETRIC_SEPARATED: - // if image is PHOTOMETRIC_SEPARATED _and_ comes with an ICC profile, - // then the image should preserve its original (CMYK) colour model and - // should be read as CMYK (to keep the match of pixel and profile and - // to avoid multiple conversions. Conversion can be done by changing - // the profile from it's original CMYK to an RGB profile with an + // if image is PHOTOMETRIC_SEPARATED _and_ comes with an ICC profile, + // then the image should preserve its original (CMYK) colour model and + // should be read as CMYK (to keep the match of pixel and profile and + // to avoid multiple conversions. Conversion can be done by changing + // the profile from it's original CMYK to an RGB profile with an // apropriate color management system. Works with non-tiled TIFFs. if(!bIsTiled) { loadMethod = LoadAsCMYK; @@ -1295,16 +1288,16 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data); /** Read embedded thumbnail */ -static void +static void ReadThumbnail(FreeImageIO *io, fi_handle handle, void *data, TIFF *tiff, FIBITMAP *dib) { FIBITMAP* thumbnail = NULL; fi_TIFFIO *fio = (fi_TIFFIO*)data; /* - Thumbnail loading can cause recursions because of the way + Thumbnail loading can cause recursions because of the way functions TIFFLastDirectory and TIFFSetSubDirectory are working. - We use here a hack to count the number of times the ReadThumbnail function was called. + We use here a hack to count the number of times the ReadThumbnail function was called. We only allow one call, check for this */ if (fio->thumbnailCount > 0) { @@ -1314,20 +1307,20 @@ ReadThumbnail(FreeImageIO *io, fi_handle handle, void *data, TIFF *tiff, FIBITMA // update the thumbnail count (used to avoid recursion) fio->thumbnailCount++; } - + // read exif thumbnail (IFD 1) ... - + toff_t exif_offset = 0; if(TIFFGetField(tiff, TIFFTAG_EXIFIFD, &exif_offset)) { - + // this code can cause unwanted recursion causing an overflow, because of the way TIFFLastDirectory work - // => this is checked using + // => this is checked using if(!TIFFLastDirectory(tiff)) { // save current position const long tell_pos = io->tell_proc(handle); const uint16_t cur_dir = TIFFCurrentDirectory(tiff); - + // load the thumbnail int page = 1; int flags = TIFF_DEFAULT; @@ -1335,22 +1328,22 @@ ReadThumbnail(FreeImageIO *io, fi_handle handle, void *data, TIFF *tiff, FIBITMA // store the thumbnail (remember to release it before return) FreeImage_SetThumbnail(dib, thumbnail); - + // restore current position io->seek_proc(handle, tell_pos, SEEK_SET); TIFFSetDirectory(tiff, cur_dir); } } - + // ... or read the first subIFD - + if(!thumbnail) { uint16_t subIFD_count = 0; toff_t* subIFD_offsets = NULL; - + // This will also read the first (and only) subIFD from a Photoshop-created "pyramid" file. - // Subsequent, smaller images are 'nextIFD' in that subIFD. Currently we only load the first one. - + // Subsequent, smaller images are 'nextIFD' in that subIFD. Currently we only load the first one. + if(TIFFGetField(tiff, TIFFTAG_SUBIFD, &subIFD_count, &subIFD_offsets)) { if(subIFD_count > 0) { // save current position @@ -1358,45 +1351,45 @@ ReadThumbnail(FreeImageIO *io, fi_handle handle, void *data, TIFF *tiff, FIBITMA const uint16_t cur_dir = TIFFCurrentDirectory(tiff); // this code can cause unwanted recursion causing an overflow, because of the way TIFFSetSubDirectory work - + if(TIFFSetSubDirectory(tiff, subIFD_offsets[0])) { // load the thumbnail - int page = -1; + int page = -1; int flags = TIFF_DEFAULT; thumbnail = Load(io, handle, page, flags, data); // store the thumbnail (remember to release it before return) FreeImage_SetThumbnail(dib, thumbnail); } - + // restore current position io->seek_proc(handle, tell_pos, SEEK_SET); TIFFSetDirectory(tiff, cur_dir); } } } - + // ... or read Photoshop thumbnail - + if(!thumbnail) { uint32_t ps_size = 0; void *ps_data = NULL; - + if(TIFFGetField(tiff, TIFFTAG_PHOTOSHOP, &ps_size, &ps_data)) { FIMEMORY *handle = FreeImage_OpenMemory((BYTE*)ps_data, ps_size); - + FreeImageIO io; SetMemoryIO(&io); - + psdParser parser; parser.ReadImageResources(&io, handle, ps_size); FreeImage_SetThumbnail(dib, parser.GetThumbnail()); - + FreeImage_CloseMemory(handle); } } - + // release thumbnail FreeImage_Unload(thumbnail); } @@ -1408,33 +1401,33 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { if (!handle || !data ) { return NULL; } - + TIFF *tif = NULL; - uint32_t height = 0; - uint32_t width = 0; + uint32_t height = 0; + uint32_t width = 0; uint16_t bitspersample = 1; uint16_t samplesperpixel = 1; - uint32_t rowsperstrip = (uint32_t)-1; + uint32_t rowsperstrip = (uint32_t)-1; uint16_t photometric = PHOTOMETRIC_MINISWHITE; uint16_t compression = (uint16_t)-1; uint16_t planar_config; FIBITMAP *dib = NULL; uint32_t iccSize = 0; // ICC profile length - void *iccBuf = NULL; // ICC profile data + void *iccBuf = NULL; // ICC profile data const BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS; - - try { + + try { fi_TIFFIO *fio = (fi_TIFFIO*)data; tif = fio->tif; if (page != -1) { if (!tif || !TIFFSetDirectory(tif, (uint16_t)page)) { - throw "Error encountered while opening TIFF file"; + throw "Error encountered while opening TIFF file"; } } - + const BOOL asCMYK = (flags & TIFF_CMYK) == TIFF_CMYK; // first, get the photometric, the compression and basic metadata @@ -1461,7 +1454,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height); TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel); TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample); - TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); + TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); TIFFGetField(tif, TIFFTAG_ICCPROFILE, &iccSize, &iccBuf); TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planar_config); @@ -1469,8 +1462,8 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { // --------------------------------------------------------------------------------- if(IsValidBitsPerSample(photometric, bitspersample, samplesperpixel) == FALSE) { - FreeImage_OutputMessageProc(s_format_id, - "Unable to handle this format: bitspersample = %d, samplesperpixel = %d, photometric = %d", + FreeImage_OutputMessageProc(s_format_id, + "Unable to handle this format: bitspersample = %d, samplesperpixel = %d, photometric = %d", (int)bitspersample, (int)samplesperpixel, (int)photometric); throw (char*)NULL; } @@ -1492,12 +1485,12 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { // RGB[A] loading using the TIFFReadRGBAImage() API // --------------------------------------------------------------------------------- - BOOL has_alpha = FALSE; + BOOL has_alpha = FALSE; - // Read the whole image into one big RGBA buffer and then + // Read the whole image into one big RGBA buffer and then // convert it to a DIB. This is using the traditional // TIFFReadRGBAImage() API that we trust. - + uint32_t *raster = NULL; if(!header_only) { @@ -1515,7 +1508,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { } } // TIFFReadRGBAImage always deliveres 3 or 4 samples per pixel images - // (RGB or RGBA, see below). Cut-off possibly present channels (additional + // (RGB or RGBA, see below). Cut-off possibly present channels (additional // alpha channels) from e.g. Photoshop. Any CMYK(A..) is now treated as RGB, // any additional alpha channel on RGB(AA..) is lost on conversion to RGB(A) @@ -1524,7 +1517,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { samplesperpixel = 4; } - // create a new DIB (take care of different samples-per-pixel in case + // create a new DIB (take care of different samples-per-pixel in case // of converted CMYK image (RGB conversion is on sample per pixel less) if (photometric == PHOTOMETRIC_SEPARATED && samplesperpixel == 4) { @@ -1539,7 +1532,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { } throw FI_MSG_ERROR_DIB_MEMORY; } - + // fill in the resolution (english or universal) ReadResolution(tif, dib); @@ -1548,7 +1541,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { // read the raster lines and save them in the DIB // with RGB mode, we have to change the order of the 3 samples RGB - // We use macros for extracting components from the packed ABGR + // We use macros for extracting components from the packed ABGR // form returned by TIFFReadRGBAImage. uint32_t *row = &raster[0]; @@ -1588,7 +1581,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { _TIFFfree(raster); } - + // ### Not correct when header only FreeImage_SetTransparent(dib, has_alpha); @@ -1608,23 +1601,23 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { ReadResolution(tif, dib); - // set up the colormap based on photometric + // set up the colormap based on photometric ReadPalette(tif, photometric, bitspersample, dib); // calculate the line + pitch (separate for scr & dest) const tmsize_t src_line = TIFFScanlineSize(tif); - // here, the pitch is 2x less than the original as we only keep the first layer + // here, the pitch is 2x less than the original as we only keep the first layer int dst_pitch = FreeImage_GetPitch(dib); // transparency table for 8-bit + 8-bit alpha images - BYTE trns[256]; + BYTE trns[256]; // clear the transparency table memset(trns, 0xFF, 256 * sizeof(BYTE)); - // In the tiff file the lines are saved from up to down + // In the tiff file the lines are saved from up to down // In a DIB the lines must be saved from down to up BYTE *bits = FreeImage_GetScanLine(dib, height - 1); @@ -1664,7 +1657,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { p++; b += dst_spp; - } + } bits -= dst_pitch; } } @@ -1686,11 +1679,11 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, y, 0), grey, nrow * src_line) == -1) { free(buf); throw FI_MSG_ERROR_PARSING; - } + } if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, y, 1), alpha, nrow * src_line) == -1) { free(buf); throw FI_MSG_ERROR_PARSING; - } + } for (int l = 0; l < nrow; l++) { BYTE *p = bits; @@ -1714,7 +1707,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { free(buf); } - + FreeImage_SetTransparencyTable(dib, &trns[0], 256); FreeImage_SetTransparent(dib, TRUE); @@ -1725,14 +1718,14 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { // At this place, samplesperpixel could be > 4, esp. when a CMYK(A) format // is recognized. Where all other formats are handled straight-forward, this - // format has to be handled special + // format has to be handled special BOOL isCMYKA = (photometric == PHOTOMETRIC_SEPARATED) && (samplesperpixel > 4); // We use a temp dib to store the alpha for the CMYKA to RGBA conversion // NOTE this is until we have Extra channels implementation. // Also then it will be possible to merge LoadAsCMYK with LoadAsGenericStrip - + FIBITMAP *alpha = NULL; unsigned alpha_pitch = 0; BYTE *alpha_bits = NULL; @@ -1744,7 +1737,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { } else if (bitspersample == 8) { alpha = FreeImage_Allocate(width, height, 8); } - + if(!alpha) { FreeImage_OutputMessageProc(s_format_id, "Failed to allocate temporary alpha channel"); } else { @@ -1752,9 +1745,9 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { alpha_pitch = FreeImage_GetPitch(alpha); alpha_Bpp = FreeImage_GetBPP(alpha) / 8; } - + } - + // create a new DIB const uint16_t chCount = MIN(samplesperpixel, 4); dib = CreateImageType(header_only, image_type, width, height, bitspersample, chCount); @@ -1778,9 +1771,9 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { const unsigned Bpc = dibBpp / chCount; const unsigned srcBpp = bitspersample * samplesperpixel / 8; - assert(Bpc <= 2); //< CMYK is only BYTE or SHORT - - // In the tiff file the lines are save from up to down + assert(Bpc <= 2); //< CMYK is only BYTE or SHORT + + // In the tiff file the lines are save from up to down // In a DIB the lines must be saved from down to up BYTE *bits = FreeImage_GetScanLine(dib, height - 1); @@ -1794,9 +1787,9 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { } if(planar_config == PLANARCONFIG_CONTIG) { - + // - loop for strip blocks - - + for (uint32_t y = 0; y < height; y += rowsperstrip) { const int32_t strips = (y + rowsperstrip > height ? height - y : rowsperstrip); @@ -1804,14 +1797,14 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { free(buf); FreeImage_Unload(alpha); throw FI_MSG_ERROR_PARSING; - } - + } + // - loop for strips - - + if(src_line != dst_line) { // CMYKA+ if(alpha) { - for (int l = 0; l < strips; l++) { + for (int l = 0; l < strips; l++) { for(BYTE *pixel = bits, *al_pixel = alpha_bits, *src_pixel = buf + l * src_line; pixel < bits + dib_pitch; pixel += dibBpp, al_pixel += alpha_Bpp, src_pixel += srcBpp) { // copy pixel byte by byte BYTE b = 0; @@ -1819,13 +1812,13 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { pixel[b] = src_pixel[b]; } // TODO write the remaining bytes to extra channel(s) - + // HACK write the first alpha to a separate dib (assume BYTE or WORD) al_pixel[0] = src_pixel[b]; if(Bpc > 1) { al_pixel[1] = src_pixel[b + 1]; } - + } bits -= dib_pitch; alpha_bits -= alpha_pitch; @@ -1841,7 +1834,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { } } } - else { + else { // CMYK to CMYK for (int l = 0; l < strips; l++) { BYTE *b = buf + l * src_line; @@ -1851,7 +1844,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { } } // height - + } else if(planar_config == PLANARCONFIG_SEPARATE) { @@ -1859,20 +1852,20 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { BYTE *al_strip = alpha_bits; // - loop for strip blocks - - + for (uint32_t y = 0; y < height; y += rowsperstrip) { const int32_t strips = (y + rowsperstrip > height ? height - y : rowsperstrip); - + // - loop for channels (planes) - - + for(uint16_t sample = 0; sample < samplesperpixel; sample++) { - + if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, y, sample), buf, strips * src_line) == -1) { free(buf); FreeImage_Unload(alpha); throw FI_MSG_ERROR_PARSING; - } - + } + BYTE *dst_strip = dib_strip; unsigned dst_pitch = dib_pitch; uint16_t ch = sample; @@ -1880,7 +1873,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { if(sample >= chCount) { // TODO Write to Extra Channel - + // HACK redirect write to temp alpha if(alpha && sample == chCount) { @@ -1891,48 +1884,48 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { Bpp = alpha_Bpp; } else { - break; + break; } } - - const unsigned channelOffset = ch * Bpc; - + + const unsigned channelOffset = ch * Bpc; + // - loop for strips in block - - + BYTE *src_line_begin = buf; BYTE *dst_line_begin = dst_strip; for (int l = 0; l < strips; l++, src_line_begin += src_line, dst_line_begin -= dst_pitch ) { // - loop for pixels in strip - - + const BYTE* const src_line_end = src_line_begin + src_line; for (BYTE *src_bits = src_line_begin, * dst_bits = dst_line_begin; src_bits < src_line_end; src_bits += Bpc, dst_bits += Bpp) { - AssignPixel(dst_bits + channelOffset, src_bits, Bpc); + AssignPixel(dst_bits + channelOffset, src_bits, Bpc); } // line - + } // strips - + } // channels - + // done with a strip block, incr to the next dib_strip -= strips * dib_pitch; al_strip -= strips * alpha_pitch; - + } //< height - + } free(buf); - + if(!asCMYK) { ConvertCMYKtoRGBA(dib); - + // The ICC Profile is invalid, clear it iccSize = 0; iccBuf = NULL; - + if(isCMYKA) { // HACK until we have Extra channels. (ConvertCMYKtoRGBA will then do the work) - + FreeImage_SetChannel(dib, alpha, FICC_ALPHA); FreeImage_Unload(alpha); alpha = NULL; @@ -1948,9 +1941,9 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { } } } - + } // !header_only - + } else if(loadMethod == LoadAsGenericStrip) { // --------------------------------------------------------------------------------- // Generic loading @@ -1967,10 +1960,10 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { ReadResolution(tif, dib); - // set up the colormap based on photometric + // set up the colormap based on photometric ReadPalette(tif, photometric, bitspersample, dib); - + if(!header_only) { // calculate the line + pitch (separate for scr & dest) @@ -1980,7 +1973,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { const unsigned Bpp = FreeImage_GetBPP(dib) / 8; const unsigned srcBpp = bitspersample * samplesperpixel / 8; - // In the tiff file the lines are save from up to down + // In the tiff file the lines are save from up to down // In a DIB the lines must be saved from down to up BYTE *bits = FreeImage_GetScanLine(dib, height - 1); @@ -1992,9 +1985,9 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { throw FI_MSG_ERROR_MEMORY; } memset(buf, 0, TIFFStripSize(tif) * sizeof(BYTE)); - + BOOL bThrowMessage = FALSE; - + if(planar_config == PLANARCONFIG_CONTIG) { for (uint32_t y = 0; y < height; y += rowsperstrip) { @@ -2002,15 +1995,15 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, y, 0), buf, strips * src_line) == -1) { // ignore errors as they can be frequent and not really valid errors, especially with fax images - bThrowMessage = TRUE; + bThrowMessage = TRUE; /* free(buf); throw FI_MSG_ERROR_PARSING; */ - } + } if(src_line == dst_line) { // channel count match - for (int l = 0; l < strips; l++) { + for (int l = 0; l < strips; l++) { memcpy(bits, buf + l * src_line, src_line); bits -= dst_pitch; } @@ -2026,67 +2019,67 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { } } else if(planar_config == PLANARCONFIG_SEPARATE) { - + const unsigned Bpc = bitspersample / 8; BYTE* dib_strip = bits; // - loop for strip blocks - - + for (uint32_t y = 0; y < height; y += rowsperstrip) { const int32_t strips = (y + rowsperstrip > height ? height - y : rowsperstrip); - + // - loop for channels (planes) - - + for(uint16_t sample = 0; sample < samplesperpixel; sample++) { - + if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, y, sample), buf, strips * src_line) == -1) { // ignore errors as they can be frequent and not really valid errors, especially with fax images - bThrowMessage = TRUE; - } - + bThrowMessage = TRUE; + } + if(sample >= chCount) { // TODO Write to Extra Channel - break; + break; } - - const unsigned channelOffset = sample * Bpc; - + + const unsigned channelOffset = sample * Bpc; + // - loop for strips in block - - + BYTE* src_line_begin = buf; BYTE* dst_line_begin = dib_strip; for (int l = 0; l < strips; l++, src_line_begin += src_line, dst_line_begin -= dst_pitch ) { - + // - loop for pixels in strip - - + const BYTE* const src_line_end = src_line_begin + src_line; for (BYTE* src_bits = src_line_begin, * dst_bits = dst_line_begin; src_bits < src_line_end; src_bits += Bpc, dst_bits += Bpp) { // actually assigns channel - AssignPixel(dst_bits + channelOffset, src_bits, Bpc); + AssignPixel(dst_bits + channelOffset, src_bits, Bpc); } // line } // strips } // channels - + // done with a strip block, incr to the next dib_strip -= strips * dst_pitch; - + } // height } free(buf); - + if(bThrowMessage) { FreeImage_OutputMessageProc(s_format_id, "Warning: parsing error. Image may be incomplete or contain invalid data !"); } - + #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR SwapRedBlue32(dib); #endif } // !header only - + } else if(loadMethod == LoadAsTiled) { // --------------------------------------------------------------------------------- // Tiled image loading @@ -2105,7 +2098,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { ReadResolution(tif, dib); - // set up the colormap based on photometric + // set up the colormap based on photometric ReadPalette(tif, photometric, bitspersample, dib); @@ -2117,7 +2110,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { // read the tiff lines and save them in the DIB if(planar_config == PLANARCONFIG_CONTIG && !header_only) { - + // get the maximum number of bytes required to contain a tile tmsize_t tileSize = TIFFTileSize(tif); @@ -2133,13 +2126,13 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { uint32_t imageRowSize = (uint32_t)TIFFScanlineSize(tif); - // In the tiff file the lines are saved from up to down + // In the tiff file the lines are saved from up to down // In a DIB the lines must be saved from down to up BYTE *bits = FreeImage_GetScanLine(dib, height - 1); - - for (uint32_t y = 0; y < height; y += tileHeight) { - int32_t nrows = (y + tileHeight > height ? height - y : tileHeight); + + for (uint32_t y = 0; y < height; y += tileHeight) { + int32_t nrows = (y + tileHeight > height ? height - y : tileHeight); for (uint32_t x = 0, rowSize = 0; x < width; x += tileWidth, rowSize += tileRowSize) { memset(tileBuffer, 0, tileSize); @@ -2173,7 +2166,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { free(tileBuffer); } else if(planar_config == PLANARCONFIG_SEPARATE) { - throw "Separated tiled TIFF images are not supported"; + throw "Separated tiled TIFF images are not supported"; } @@ -2186,7 +2179,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { if (!TIFFGetField(tif, TIFFTAG_STONITS, &stonits)) { stonits = 1; } - + // create a new DIB dib = CreateImageType(header_only, image_type, width, height, bitspersample, samplesperpixel); if (dib == NULL) { @@ -2203,7 +2196,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { tmsize_t src_line = TIFFScanlineSize(tif); int dst_pitch = FreeImage_GetPitch(dib); - // In the tiff file the lines are save from up to down + // In the tiff file the lines are save from up to down // In a DIB the lines must be saved from down to up BYTE *bits = FreeImage_GetScanLine(dib, height - 1); @@ -2221,9 +2214,9 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, y, 0), buf, nrow * src_line) == -1) { free(buf); throw FI_MSG_ERROR_PARSING; - } + } // convert from XYZ to RGB - for (int l = 0; l < nrow; l++) { + for (int l = 0; l < nrow; l++) { tiff_ConvertLineXYZToRGB(bits, buf + l * src_line, stonits, width); bits -= dst_pitch; } @@ -2258,7 +2251,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { tmsize_t src_line = TIFFScanlineSize(tif); unsigned dst_pitch = FreeImage_GetPitch(dib); - // In the tiff file the lines are save from up to down + // In the tiff file the lines are save from up to down // In a DIB the lines must be saved from down to up BYTE *bits = FreeImage_GetScanLine(dib, height - 1); @@ -2278,7 +2271,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, y, 0), buf, nrow * src_line) == -1) { free(buf); throw FI_MSG_ERROR_PARSING; - } + } // convert from half (16-bit) to float (32-bit) // !!! use OpenEXR half helper class @@ -2304,7 +2297,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { // this use case was never encountered yet throw "Unable to handle PLANARCONFIG_SEPARATE RGB half float images"; } - + } // !header only } else { @@ -2314,13 +2307,13 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { throw FI_MSG_ERROR_UNSUPPORTED_FORMAT; } - + // copy TIFF metadata (must be done after FreeImage_Allocate) ReadMetadata(io, handle, tif, dib); // copy ICC profile data (must be done after FreeImage_Allocate) - + FreeImage_CreateICCProfile(dib, iccBuf, iccSize); if (photometric == PHOTOMETRIC_SEPARATED) { if (asCMYK) { @@ -2334,12 +2327,12 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { } // copy TIFF thumbnail (must be done after FreeImage_Allocate) - + ReadThumbnail(io, handle, data, tif, dib); return dib; - } catch (const char *message) { + } catch (const char *message) { if(dib) { FreeImage_Unload(dib); } @@ -2348,7 +2341,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) { } return NULL; } - + } // -------------------------------------------------------------------------- @@ -2366,13 +2359,13 @@ Save a single image into a TIF @param ifdCount 1 if no thumbnail to save, 2 if image + thumbnail to save @return Returns TRUE if successful, returns FALSE otherwise */ -static BOOL +static BOOL SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data, unsigned ifd, unsigned ifdCount) { if (!dib || !handle || !data) { return FALSE; - } - - try { + } + + try { fi_TIFFIO *fio = (fi_TIFFIO*)data; TIFF *out = fio->tif; @@ -2383,9 +2376,9 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag const uint16_t bitsperpixel = (uint16_t)FreeImage_GetBPP(dib); const FIICCPROFILE* iccProfile = FreeImage_GetICCProfile(dib); - + // setup out-variables based on dib and flag options - + uint16_t bitspersample; uint16_t samplesperpixel; uint16_t photometric; @@ -2413,7 +2406,7 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag } else if(photometric == PHOTOMETRIC_RGB) { // transparency mask support - uint16_t sampleinfo[1]; + uint16_t sampleinfo[1]; // unassociated alpha data is transparency information sampleinfo[0] = EXTRASAMPLE_UNASSALPHA; TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, sampleinfo); @@ -2439,7 +2432,7 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag else { photometric = PHOTOMETRIC_RGB; // transparency mask support - uint16_t sampleinfo[1]; + uint16_t sampleinfo[1]; // unassociated alpha data is transparency information sampleinfo[0] = EXTRASAMPLE_UNASSALPHA; TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, sampleinfo); @@ -2453,22 +2446,22 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag if((flags & TIFF_LOGLUV) == TIFF_LOGLUV) { photometric = PHOTOMETRIC_LOGLUV; TIFFSetField(out, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_FLOAT); - // TIFFSetField(out, TIFFTAG_STONITS, 1.0); // assume unknown + // TIFFSetField(out, TIFFTAG_STONITS, 1.0); // assume unknown } else { // store with default compression (LZW) or with input compression flag photometric = PHOTOMETRIC_RGB; } - + } else if (image_type == FIT_RGBAF) { // 128-bit RGBAF => store with default compression (LZW) or with input compression flag - + samplesperpixel = 4; bitspersample = bitsperpixel / samplesperpixel; photometric = PHOTOMETRIC_RGB; } else { // special image type (int, long, double, ...) - + samplesperpixel = 1; bitspersample = bitsperpixel; photometric = PHOTOMETRIC_MINISBLACK; @@ -2477,7 +2470,7 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag // set image data type WriteImageType(out, image_type); - + // write possible ICC profile if (iccProfile->size && iccProfile->data) { @@ -2491,10 +2484,10 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel); TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bitspersample); TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric); - TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); // single image plane + TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); // single image plane TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(out, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); - TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, (uint32_t) -1)); + TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, (uint32_t) -1)); // handle metrics @@ -2511,7 +2504,7 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag TIFFSetField(out, TIFFTAG_PAGENAME, page_number); } else { - // is it a thumbnail ? + // is it a thumbnail ? TIFFSetField(out, TIFFTAG_SUBFILETYPE, (ifd == 0) ? (uint32_t)0 : (uint32_t)FILETYPE_REDUCEDIMAGE); } @@ -2560,12 +2553,12 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag // read the DIB lines from bottom to top // and save them in the TIF // ------------------------------------- - + const uint32_t pitch = FreeImage_GetPitch(dib); if(image_type == FIT_BITMAP) { // standard bitmap type - + switch(bitsperpixel) { case 1 : case 4 : @@ -2621,7 +2614,7 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag } break; - } + } case 24: case 32: @@ -2639,9 +2632,9 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR if (photometric != PHOTOMETRIC_SEPARATED) { // TIFFs store color data RGB(A) instead of BGR(A) - + BYTE *pBuf = buffer; - + for (uint32_t x = 0; x < width; x++) { INPLACESWAP(pBuf[0], pBuf[2]); pBuf += samplesperpixel; @@ -2676,12 +2669,12 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag free(buffer); } else { // just dump the dib (tiff supports all dib types) - + BYTE *buffer = (BYTE *)malloc(pitch * sizeof(BYTE)); if(buffer == NULL) { throw FI_MSG_ERROR_MEMORY; } - + for (uint32_t y = 0; y < height; y++) { // get a copy of the scanline memcpy(buffer, FreeImage_GetScanLine(dib, height - y - 1), pitch); @@ -2699,21 +2692,21 @@ SaveOneTIFF(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flag } return TRUE; - + } catch(const char *text) { FreeImage_OutputMessageProc(s_format_id, text); return FALSE; - } + } } static BOOL DLL_CALLCONV Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data) { BOOL bResult = FALSE; - + // handle thumbnail as SubIFD const BOOL bHasThumbnail = (FreeImage_GetThumbnail(dib) != NULL); const unsigned ifdCount = bHasThumbnail ? 2 : 1; - + FIBITMAP *bitmap = dib; for(unsigned ifd = 0; ifd < ifdCount; ifd++) { @@ -2741,7 +2734,7 @@ InitTIFF(Plugin *plugin, int format_id) { // Set up the callback for extended TIFF directory tag support (see XTIFF.cpp) // Must be called before using libtiff - XTIFFInitialize(); + XTIFFInitialize(); plugin->format_proc = Format; plugin->description_proc = Description; @@ -2758,5 +2751,5 @@ InitTIFF(Plugin *plugin, int format_id) { plugin->supports_export_bpp_proc = SupportsExportDepth; plugin->supports_export_type_proc = SupportsExportType; plugin->supports_icc_profiles_proc = SupportsICCProfiles; - plugin->supports_no_pixels_proc = SupportsNoPixels; + plugin->supports_no_pixels_proc = SupportsNoPixels; } diff --git a/Source/LibTIFF4/tif_win32.c b/Source/LibTIFF4/tif_win32.c index 5bc1799..b3f66fe 100644 --- a/Source/LibTIFF4/tif_win32.c +++ b/Source/LibTIFF4/tif_win32.c @@ -384,42 +384,6 @@ TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts) return tif; } -#endif /* ndef _WIN32_WCE */ - -void *_TIFFmalloc(tmsize_t s) -{ - if (s == 0) - return ((void *)NULL); - - return (malloc((size_t)s)); -} - -void *_TIFFcalloc(tmsize_t nmemb, tmsize_t siz) -{ - if (nmemb == 0 || siz == 0) - return ((void *)NULL); - - return calloc((size_t)nmemb, (size_t)siz); -} - -void _TIFFfree(void *p) { free(p); } - -void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); } - -void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); } - -void _TIFFmemcpy(void *d, const void *s, tmsize_t c) -{ - memcpy(d, s, (size_t)c); -} - -int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c) -{ - return (memcmp(p1, p2, (size_t)c)); -} - -#ifndef _WIN32_WCE - static void Win32WarningHandler(const char *module, const char *fmt, va_list ap) { if (module != NULL)