mirror of
https://github.com/webmproject/libwebp.git
synced 2024-12-25 05:08:21 +01:00
cwebp: extract tiff decoding to its own module
Change-Id: If98d0d37de34b63ac10d826150237b5f99446532
This commit is contained in:
parent
6a871d66a4
commit
1c1c5646a5
@ -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 \
|
||||
|
@ -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)
|
||||
|
@ -18,10 +18,6 @@
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#ifdef WEBP_HAVE_TIFF
|
||||
#include <tiffio.h>
|
||||
#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_,
|
||||
|
85
examples/tiffdec.c
Normal file
85
examples/tiffdec.c
Normal file
@ -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 <stdio.h>
|
||||
|
||||
#ifdef WEBP_HAVE_TIFF
|
||||
#include <tiffio.h>
|
||||
|
||||
#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
|
||||
|
||||
// -----------------------------------------------------------------------------
|
30
examples/tiffdec.h
Normal file
30
examples/tiffdec.h
Normal file
@ -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_
|
@ -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 \
|
||||
|
Loading…
Reference in New Issue
Block a user