From 1c1c5646a50d11f7c484e4f56418410380eee1d5 Mon Sep 17 00:00:00 2001 From: James Zern Date: Thu, 6 Dec 2012 14:04:36 -0800 Subject: [PATCH] cwebp: extract tiff decoding to its own module Change-Id: If98d0d37de34b63ac10d826150237b5f99446532 --- Makefile.vc | 1 + examples/Makefile.am | 1 + examples/cwebp.c | 68 +---------------------------------- examples/tiffdec.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ examples/tiffdec.h | 30 ++++++++++++++++ makefile.unix | 1 + 6 files changed, 119 insertions(+), 67 deletions(-) create mode 100644 examples/tiffdec.c create mode 100644 examples/tiffdec.h diff --git a/Makefile.vc b/Makefile.vc index b23c4c82..75a12ee4 100644 --- a/Makefile.vc +++ b/Makefile.vc @@ -174,6 +174,7 @@ DSP_OBJS = \ EX_FORMAT_DEC_OBJS = \ $(DIROBJ)\examples/jpegdec.obj \ $(DIROBJ)\examples/pngdec.obj \ + $(DIROBJ)\examples/tiffdec.obj \ EX_UTIL_OBJS = \ $(DIROBJ)\examples\example_util.obj \ diff --git a/examples/Makefile.am b/examples/Makefile.am index 0a52e4e4..b7988e85 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -23,6 +23,7 @@ dwebp_LDADD = libexampleutil.la ../src/libwebp.la $(PNG_LIBS) $(JPEG_LIBS) cwebp_SOURCES = cwebp.c metadata.h stopwatch.h cwebp_SOURCES += jpegdec.c jpegdec.h cwebp_SOURCES += pngdec.c pngdec.h +cwebp_SOURCES += tiffdec.c tiffdec.h cwebp_CPPFLAGS = $(AM_CPPFLAGS) $(USE_EXPERIMENTAL_CODE) cwebp_CPPFLAGS += $(JPEG_INCLUDES) $(PNG_INCLUDES) $(TIFF_INCLUDES) cwebp_LDADD = ../src/libwebp.la $(JPEG_LIBS) $(PNG_LIBS) $(TIFF_LIBS) diff --git a/examples/cwebp.c b/examples/cwebp.c index 1358b1e3..47a9c3b5 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -18,10 +18,6 @@ #include "config.h" #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 @@ -41,6 +37,7 @@ #include "./metadata.h" #include "./pngdec.h" #include "./stopwatch.h" +#include "./tiffdec.h" #ifndef WEBP_DLL #if defined(__cplusplus) || defined(c_plusplus) extern "C" { @@ -288,69 +285,6 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic, #else // !HAVE_WINCODEC_H -#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_, diff --git a/examples/tiffdec.c b/examples/tiffdec.c new file mode 100644 index 00000000..c93be49a --- /dev/null +++ b/examples/tiffdec.c @@ -0,0 +1,85 @@ +// Copyright 2012 Google Inc. All Rights Reserved. +// +// This code is licensed under the same terms as WebM: +// Software License Agreement: http://www.webmproject.org/license/software/ +// Additional IP Rights Grant: http://www.webmproject.org/license/additional/ +// ----------------------------------------------------------------------------- +// +// TIFF decode. + +#include "./tiffdec.h" + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#ifdef WEBP_HAVE_TIFF +#include + +#include "webp/encode.h" + +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 // !WEBP_HAVE_TIFF +int ReadTIFF(const char* const filename, + struct 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 // WEBP_HAVE_TIFF + +// ----------------------------------------------------------------------------- diff --git a/examples/tiffdec.h b/examples/tiffdec.h new file mode 100644 index 00000000..1f0a7b2a --- /dev/null +++ b/examples/tiffdec.h @@ -0,0 +1,30 @@ +// Copyright 2012 Google Inc. All Rights Reserved. +// +// This code is licensed under the same terms as WebM: +// Software License Agreement: http://www.webmproject.org/license/software/ +// Additional IP Rights Grant: http://www.webmproject.org/license/additional/ +// ----------------------------------------------------------------------------- +// +// TIFF decode. + +#ifndef WEBP_EXAMPLES_TIFFDEC_H_ +#define WEBP_EXAMPLES_TIFFDEC_H_ + +#if defined(__cplusplus) || defined(c_plusplus) +extern "C" { +#endif + +struct WebPPicture; + +// Reads a TIFF from 'filename', returning the decoded output in 'pic'. +// If 'keep_alpha' is true and the TIFF has an alpha channel, the output is RGBA +// otherwise it will be RGB. +// Returns true on success. +int ReadTIFF(const char* const filename, + struct WebPPicture* const pic, int keep_alpha); + +#if defined(__cplusplus) || defined(c_plusplus) +} // extern "C" +#endif + +#endif // WEBP_EXAMPLES_TIFFDEC_H_ diff --git a/makefile.unix b/makefile.unix index 5cd2dec8..7fcc6033 100644 --- a/makefile.unix +++ b/makefile.unix @@ -127,6 +127,7 @@ ENC_OBJS = \ EX_FORMAT_DEC_OBJS = \ examples/jpegdec.o \ examples/pngdec.o \ + examples/tiffdec.o \ EX_UTIL_OBJS = \ examples/example_util.o \