diff --git a/examples/cwebp.c b/examples/cwebp.c index 7e7e4b53..35140ee5 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -27,6 +27,10 @@ #include #endif +#ifdef WEBP_HAVE_TIFF +#include +#endif + #ifdef HAVE_WINCODEC_H #ifdef __MINGW32__ #define INITGUID // Without this GUIDs are declared extern and fail to link @@ -449,9 +453,73 @@ static int ReadPNG(FILE* in_file, WebPPicture* const pic, int keep_alpha) { } #endif +#ifdef WEBP_HAVE_TIFF +static int ReadTIFF(const char* const filename, + WebPPicture* const pic, int keep_alpha) { + TIFF* const tif = TIFFOpen(filename, "r"); + uint32 width, height; + uint32* raster; + int ok = 0; + int dircount = 1; + + if (tif == NULL) { + fprintf(stderr, "Error! Cannot open TIFF file '%s'\n", filename); + return 0; + } + + while (TIFFReadDirectory(tif)) ++dircount; + + if (dircount > 1) { + fprintf(stderr, "Warning: multi-directory TIFF files are not supported.\n" + "Only the first will be used, %d will be ignored.\n", + dircount - 1); + } + + TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width); + TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height); + raster = (uint32*)_TIFFmalloc(width * height * sizeof(*raster)); + if (raster != NULL) { + if (TIFFReadRGBAImageOriented(tif, width, height, raster, + ORIENTATION_TOPLEFT, 1)) { + const int stride = width * sizeof(*raster); + pic->width = width; + pic->height = height; + // TIFF data is ABGR +#ifdef __BIG_ENDIAN__ + TIFFSwabArrayOfLong(raster, width * height); +#endif + ok = keep_alpha + ? WebPPictureImportRGBA(pic, (const uint8_t*)raster, stride) + : WebPPictureImportRGBX(pic, (const uint8_t*)raster, stride); + } + _TIFFfree(raster); + } else { + fprintf(stderr, "Error allocating TIFF RGBA memory!\n"); + } + + if (ok && keep_alpha == 2) { + WebPCleanupTransparentArea(pic); + } + + TIFFClose(tif); + return ok; +} +#else +static int ReadTIFF(const char* const filename, + WebPPicture* const pic, int keep_alpha) { + (void)filename; + (void)pic; + (void)keep_alpha; + fprintf(stderr, "TIFF support not compiled. Please install the libtiff " + "development package before building.\n"); + return 0; +} +#endif + typedef enum { PNG = 0, JPEG, + TIFF_, // 'TIFF' clashes with libtiff UNSUPPORTED } InputFileFormat; @@ -470,6 +538,8 @@ static InputFileFormat GetImageType(FILE* in_file) { format = PNG; } else if (magic >= 0xFFD8FF00U && magic <= 0xFFD8FFFFU) { format = JPEG; + } else if (magic == 0x49492A00 || magic == 0x4D4D002A) { + format = TIFF_; } return format; } @@ -490,6 +560,8 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic, ok = ReadPNG(in_file, pic, keep_alpha); } else if (format == JPEG) { ok = ReadJPEG(in_file, pic); + } else if (format == TIFF_) { + ok = ReadTIFF(filename, pic, keep_alpha); } } else { // If image size is specified, infer it as YUV format. diff --git a/makefile.unix b/makefile.unix index 909ae436..3c109ac9 100644 --- a/makefile.unix +++ b/makefile.unix @@ -12,8 +12,8 @@ # These flag assume you have libpng and libjpeg installed. If not, either # follow below install instructions or just comment out the next lines. -EXTRA_FLAGS= -DWEBP_HAVE_PNG -DWEBP_HAVE_JPEG -EXTRA_LIBS= -lpng -ljpeg -lz +EXTRA_FLAGS= -DWEBP_HAVE_PNG -DWEBP_HAVE_JPEG -DWEBP_HAVE_TIFF +EXTRA_LIBS= -lpng -ltiff -ljpeg -lz ifeq ($(strip $(shell uname)), Darwin) # Work around a problem linking tables marked as common symbols, # cf., src/enc/yuv.[hc] @@ -30,10 +30,12 @@ endif # 1. Install MacPorts (http://www.macports.org/install.php) # 2. Run "sudo port install jpeg" # 3. Run "sudo port install libpng" +# 4. Run "sudo port install tiff" # To install libraries on Linux: # 1. Run "sudo apt-get install libjpeg62-dev" # 2. Run "sudo apt-get install libpng12-dev" +# 3. Run "sudo apt-get install libtiff4-dev" # Uncomment for build for 32bit platform # Alternatively, you can just use the command