From ae2a7222cea9c3417f1a54d907c8d666b55edc54 Mon Sep 17 00:00:00 2001 From: Pascal Massimino Date: Mon, 30 May 2016 21:45:38 -0700 Subject: [PATCH] collect all decoding utilities from examples/ in libexampledec.a adds a generic examples/image_dec.[ch] entry point too. WebPGuessImageType() can be used to infer image type. Change-Id: I8337e7b6ad91863c9cf118e4791668d2d175079b --- CMakeLists.txt | 40 +++++++++++++++++++++------------- Makefile.vc | 1 + build.gradle | 26 +++++++++++++++++----- examples/Android.mk | 28 ++++++++++++++++++------ examples/Makefile.am | 22 +++++++++++-------- examples/cwebp.c | 49 ++++++------------------------------------ examples/image_dec.c | 35 ++++++++++++++++++++++++++++++ examples/image_dec.h | 51 ++++++++++++++++++++++++++++++++++++++++++++ makefile.unix | 7 ++++-- 9 files changed, 180 insertions(+), 79 deletions(-) create mode 100644 examples/image_dec.c create mode 100644 examples/image_dec.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c36c873d..3cb9c3bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -287,6 +287,28 @@ if(WEBP_BUILD_CWEBP OR WEBP_BUILD_DWEBP) target_link_libraries(exampleutil webp ${WEBP_DEP_LIBRARIES}) endif() +if(WEBP_BUILD_CWEBP) + # Image-decoding utility library. + set(exampledec_SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/examples/image_dec.c + ${CMAKE_CURRENT_SOURCE_DIR}/examples/image_dec.h + ${CMAKE_CURRENT_SOURCE_DIR}/examples/jpegdec.c + ${CMAKE_CURRENT_SOURCE_DIR}/examples/jpegdec.h + ${CMAKE_CURRENT_SOURCE_DIR}/examples/metadata.c + ${CMAKE_CURRENT_SOURCE_DIR}/examples/metadata.h + ${CMAKE_CURRENT_SOURCE_DIR}/examples/pngdec.c + ${CMAKE_CURRENT_SOURCE_DIR}/examples/pngdec.h + ${CMAKE_CURRENT_SOURCE_DIR}/examples/tiffdec.c + ${CMAKE_CURRENT_SOURCE_DIR}/examples/tiffdec.h + ${CMAKE_CURRENT_SOURCE_DIR}/examples/webpdec.c + ${CMAKE_CURRENT_SOURCE_DIR}/examples/webpdec.h + ${CMAKE_CURRENT_SOURCE_DIR}/examples/wicdec.c + ${CMAKE_CURRENT_SOURCE_DIR}/examples/wicdec.h) + add_library(exampledec ${exampledec_SRCS}) + target_link_libraries(exampledec webp ${WEBP_DEP_LIBRARIES} + ${WEBP_DEP_IMG_LIBRARIES}) +endif() + if(WEBP_BUILD_DWEBP) # dwebp include_directories(${WEBP_DEP_IMG_INCLUDE_DIRS}) @@ -304,20 +326,8 @@ if(WEBP_BUILD_CWEBP) include_directories(${WEBP_DEP_IMG_INCLUDE_DIRS}) add_executable(cwebp ${CMAKE_CURRENT_SOURCE_DIR}/examples/cwebp.c - ${CMAKE_CURRENT_SOURCE_DIR}/examples/metadata.c - ${CMAKE_CURRENT_SOURCE_DIR}/examples/metadata.h - ${CMAKE_CURRENT_SOURCE_DIR}/examples/stopwatch.h - ${CMAKE_CURRENT_SOURCE_DIR}/examples/jpegdec.c - ${CMAKE_CURRENT_SOURCE_DIR}/examples/jpegdec.h - ${CMAKE_CURRENT_SOURCE_DIR}/examples/pngdec.c - ${CMAKE_CURRENT_SOURCE_DIR}/examples/pngdec.h - ${CMAKE_CURRENT_SOURCE_DIR}/examples/tiffdec.c - ${CMAKE_CURRENT_SOURCE_DIR}/examples/tiffdec.h - ${CMAKE_CURRENT_SOURCE_DIR}/examples/webpdec.c - ${CMAKE_CURRENT_SOURCE_DIR}/examples/webpdec.h - ${CMAKE_CURRENT_SOURCE_DIR}/examples/wicdec.c - ${CMAKE_CURRENT_SOURCE_DIR}/examples/wicdec.h) - target_link_libraries(cwebp webp exampleutil ${WEBP_DEP_LIBRARIES} - ${WEBP_DEP_IMG_LIBRARIES} + ${CMAKE_CURRENT_SOURCE_DIR}/examples/stopwatch.h) + target_link_libraries(cwebp exampledec webp exampleutil + ${WEBP_DEP_LIBRARIES} ${WEBP_DEP_IMG_LIBRARIES} ) endif() diff --git a/Makefile.vc b/Makefile.vc index df0282f8..06461a1f 100644 --- a/Makefile.vc +++ b/Makefile.vc @@ -244,6 +244,7 @@ EX_ANIM_UTIL_OBJS = \ $(DIROBJ)\examples\anim_util.obj \ EX_FORMAT_DEC_OBJS = \ + $(DIROBJ)\examples\image_dec.obj \ $(DIROBJ)\examples\jpegdec.obj \ $(DIROBJ)\examples\metadata.obj \ $(DIROBJ)\examples\pngdec.obj \ diff --git a/build.gradle b/build.gradle index 85aeda3e..a498b2aa 100644 --- a/build.gradle +++ b/build.gradle @@ -241,10 +241,31 @@ model { } } + example_dec(NativeLibrarySpec) { + binaries { + all { + lib library: "webp", linkage: "static" + } + } + sources { + c { + source { + srcDir "./examples" + include "image_dec.c" + include "jpegdec.c" + include "metadata.c" + include "pngdec.c" + include "tiffdec.c" + include "webpdec.c" + } + } + } + } cwebp(NativeExecutableSpec) { binaries { all { lib library: "example_util", linkage: "static" + lib library: "example_dec", linkage: "static" lib library: "webp", linkage: "static" } } @@ -253,11 +274,6 @@ model { source { srcDir "./examples" include "cwebp.c" - include "jpegdec.c" - include "metadata.c" - include "pngdec.c" - include "tiffdec.c" - include "webpdec.c" } } } diff --git a/examples/Android.mk b/examples/Android.mk index 5489f03b..81490f61 100644 --- a/examples/Android.mk +++ b/examples/Android.mk @@ -15,6 +15,27 @@ LOCAL_MODULE := example_util include $(BUILD_STATIC_LIBRARY) + +################################################################################ +# libexample_dec + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + image_dec.c \ + jpegdec.c \ + metadata.c \ + pngdec.c \ + tiffdec.c \ + webpdec.c \ + +LOCAL_CFLAGS := $(WEBP_CFLAGS) +LOCAL_C_INCLUDES := $(LOCAL_PATH)/../src + +LOCAL_MODULE := example_dec + +include $(BUILD_STATIC_LIBRARY) + ################################################################################ # cwebp @@ -24,15 +45,10 @@ include $(CLEAR_VARS) # minor modification to their Android.mk files. LOCAL_SRC_FILES := \ cwebp.c \ - jpegdec.c \ - metadata.c \ - pngdec.c \ - tiffdec.c \ - webpdec.c \ LOCAL_CFLAGS := $(WEBP_CFLAGS) LOCAL_C_INCLUDES := $(LOCAL_PATH)/../src -LOCAL_STATIC_LIBRARIES := example_util webp +LOCAL_STATIC_LIBRARIES := example_util example_dec webp LOCAL_MODULE := cwebp diff --git a/examples/Makefile.am b/examples/Makefile.am index 3b9e5b20..17a3688a 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -12,10 +12,20 @@ if BUILD_GIF2WEBP bin_PROGRAMS += gif2webp endif -noinst_LTLIBRARIES = libexampleutil.la +noinst_LTLIBRARIES = libexampleutil.la libexampledec.la libexampleutil_la_SOURCES = example_util.c example_util.h stopwatch.h +libexampledec_la_SOURCES = image_dec.c image_dec.h +libexampledec_la_SOURCES += jpegdec.c jpegdec.h +libexampledec_la_SOURCES += metadata.c metadata.h +libexampledec_la_SOURCES += pngdec.c pngdec.h +libexampledec_la_SOURCES += tiffdec.c tiffdec.h +libexampledec_la_SOURCES += webpdec.c webpdec.h +libexampledec_la_SOURCES += wicdec.c wicdec.h +libexampledec_la_CPPFLAGS = $(JPEG_INCLUDES) $(PNG_INCLUDES) $(TIFF_INCLUDES) +libexampledec_la_CPPFLAGS += $(AM_CPPFLAGS) $(USE_EXPERIMENTAL_CODE) + if BUILD_ANIMDIFF noinst_PROGRAMS = anim_diff endif @@ -31,15 +41,9 @@ dwebp_CPPFLAGS = $(AM_CPPFLAGS) $(USE_EXPERIMENTAL_CODE) dwebp_CPPFLAGS += $(JPEG_INCLUDES) $(PNG_INCLUDES) dwebp_LDADD = libexampleutil.la $(PNG_LIBS) $(JPEG_LIBS) -cwebp_SOURCES = cwebp.c metadata.c metadata.h stopwatch.h -cwebp_SOURCES += jpegdec.c jpegdec.h -cwebp_SOURCES += pngdec.c pngdec.h -cwebp_SOURCES += tiffdec.c tiffdec.h -cwebp_SOURCES += webpdec.c webpdec.h -cwebp_SOURCES += wicdec.c wicdec.h +cwebp_SOURCES = cwebp.c stopwatch.h cwebp_CPPFLAGS = $(AM_CPPFLAGS) $(USE_EXPERIMENTAL_CODE) -cwebp_CPPFLAGS += $(JPEG_INCLUDES) $(PNG_INCLUDES) $(TIFF_INCLUDES) -cwebp_LDADD = libexampleutil.la ../src/libwebp.la +cwebp_LDADD = libexampleutil.la libexampledec.la ../src/libwebp.la cwebp_LDADD += $(JPEG_LIBS) $(PNG_LIBS) $(TIFF_LIBS) gif2webp_SOURCES = gif2webp.c gifdec.c gifdec.h diff --git a/examples/cwebp.c b/examples/cwebp.c index b8d513c4..80924072 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -20,17 +20,10 @@ #include "webp/config.h" #endif -#include "webp/encode.h" - #include "./example_util.h" -#include "./metadata.h" +#include "./image_dec.h" #include "./stopwatch.h" - -#include "./jpegdec.h" -#include "./pngdec.h" -#include "./tiffdec.h" -#include "./webpdec.h" -#include "./wicdec.h" +#include "webp/encode.h" #ifndef WEBP_DLL #ifdef __cplusplus @@ -102,34 +95,6 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic, #else // !HAVE_WINCODEC_H -typedef enum { - PNG_ = 0, - JPEG_, - TIFF_, // 'TIFF' clashes with libtiff - WEBP_, - UNSUPPORTED -} InputFileFormat; - -static uint32_t GetBE32(const uint8_t buf[]) { - return ((uint32_t)buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; -} - -static InputFileFormat GuessImageType(const uint8_t buf[12]) { - InputFileFormat format = UNSUPPORTED; - const uint32_t magic1 = GetBE32(buf + 0); - const uint32_t magic2 = GetBE32(buf + 8); - if (magic1 == 0x89504E47U) { - format = PNG_; - } else if (magic1 >= 0xFFD8FF00U && magic1 <= 0xFFD8FFFFU) { - format = JPEG_; - } else if (magic1 == 0x49492A00 || magic1 == 0x4D4D002A) { - format = TIFF_; - } else if (magic1 == 0x52494646 && magic2 == 0x57454250) { - format = WEBP_; - } - return format; -} - static int ReadPicture(const char* const filename, WebPPicture* const pic, int keep_alpha, Metadata* const metadata) { const uint8_t* data = NULL; @@ -142,14 +107,14 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic, if (pic->width == 0 || pic->height == 0) { ok = 0; if (data_size >= 12) { - const InputFileFormat format = GuessImageType(data); - if (format == PNG_) { + const WebPInputFileFormat format = WebPGuessImageType(data, data_size); + if (format == WEBP_PNG_FORMAT) { ok = ReadPNG(data, data_size, pic, keep_alpha, metadata); - } else if (format == JPEG_) { + } else if (format == WEBP_JPEG_FORMAT) { ok = ReadJPEG(data, data_size, pic, metadata); - } else if (format == TIFF_) { + } else if (format == WEBP_TIFF_FORMAT) { ok = ReadTIFF(data, data_size, pic, keep_alpha, metadata); - } else if (format == WEBP_) { + } else if (format == WEBP_WEBP_FORMAT) { ok = ReadWebP(data, data_size, pic, keep_alpha, metadata); } } diff --git a/examples/image_dec.c b/examples/image_dec.c new file mode 100644 index 00000000..0b3ea160 --- /dev/null +++ b/examples/image_dec.c @@ -0,0 +1,35 @@ +// Copyright 2016 Google Inc. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the COPYING file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. +// ----------------------------------------------------------------------------- +// +// Generic image-type guessing. + +#include "./image_dec.h" + +static WEBP_INLINE uint32_t GetBE32(const uint8_t buf[]) { + return ((uint32_t)buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]; +} + +WebPInputFileFormat WebPGuessImageType(const uint8_t* const data, + size_t data_size) { + WebPInputFileFormat format = WEBP_UNSUPPORTED_FORMAT; + if (data != NULL && data_size >= 12) { + const uint32_t magic1 = GetBE32(data + 0); + const uint32_t magic2 = GetBE32(data + 8); + if (magic1 == 0x89504E47U) { + format = WEBP_PNG_FORMAT; + } else if (magic1 >= 0xFFD8FF00U && magic1 <= 0xFFD8FFFFU) { + format = WEBP_JPEG_FORMAT; + } else if (magic1 == 0x49492A00 || magic1 == 0x4D4D002A) { + format = WEBP_TIFF_FORMAT; + } else if (magic1 == 0x52494646 && magic2 == 0x57454250) { + format = WEBP_WEBP_FORMAT; + } + } + return format; +} diff --git a/examples/image_dec.h b/examples/image_dec.h new file mode 100644 index 00000000..b12b3a85 --- /dev/null +++ b/examples/image_dec.h @@ -0,0 +1,51 @@ +// Copyright 2016 Google Inc. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the COPYING file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. +// ----------------------------------------------------------------------------- +// +// All-in-one library to decode PNG/JPEG/WebP/TIFF/WIC input images. +// +// Author: Skal (pascal.massimino@gmail.com) + +#ifndef WEBP_EXAMPLES_IMAGE_DEC_H_ +#define WEBP_EXAMPLES_IMAGE_DEC_H_ + +#include "webp/types.h" + +#ifdef HAVE_CONFIG_H +#include "webp/config.h" +#endif + +#include "./metadata.h" +#include "./jpegdec.h" +#include "./pngdec.h" +#include "./tiffdec.h" +#include "./webpdec.h" +#include "./wicdec.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + WEBP_PNG_FORMAT = 0, + WEBP_JPEG_FORMAT, + WEBP_TIFF_FORMAT, + WEBP_WEBP_FORMAT, + WEBP_UNSUPPORTED_FORMAT +} WebPInputFileFormat; + +// Try to infer the image format. 'data_size' should be larger than 12. +// Returns WEBP_UNSUPPORTED_FORMAT if format can't be guess safely. +WebPInputFileFormat WebPGuessImageType(const uint8_t* const data, + size_t data_size); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // WEBP_EXAMPLES_IMAGE_DEC_H_ diff --git a/makefile.unix b/makefile.unix index 20a8e841..3fa5d5f7 100644 --- a/makefile.unix +++ b/makefile.unix @@ -201,6 +201,7 @@ ENC_OBJS = \ src/enc/webpenc.o \ EX_FORMAT_DEC_OBJS = \ + examples/image_dec.o \ examples/jpegdec.o \ examples/metadata.o \ examples/pngdec.o \ @@ -321,6 +322,7 @@ src/utils/bit_writer.o: src/utils/endian_inl.h $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ examples/libanim_util.a: $(ANIM_UTIL_OBJS) +examples/libexample_dec.a: $(EX_FORMAT_DEC_OBJS) examples/libexample_util.a: $(EX_UTIL_OBJS) examples/libgifdec.a: $(GIFDEC_OBJS) src/libwebpdecoder.a: $(LIBWEBPDECODER_OBJS) @@ -333,7 +335,7 @@ src/libwebpextras.a: $(LIBWEBPEXTRA_OBJS) $(AR) $(ARFLAGS) $@ $^ examples/anim_diff: examples/anim_diff.o $(ANIM_UTIL_OBJS) $(GIFDEC_OBJS) -examples/cwebp: examples/cwebp.o $(EX_FORMAT_DEC_OBJS) +examples/cwebp: examples/cwebp.o examples/dwebp: examples/dwebp.o examples/gif2webp: examples/gif2webp.o $(GIFDEC_OBJS) examples/vwebp: examples/vwebp.o @@ -344,7 +346,8 @@ examples/anim_diff: src/demux/libwebpdemux.a examples/libexample_util.a examples/anim_diff: src/libwebp.a examples/anim_diff: EXTRA_LIBS += $(GIF_LIBS) examples/anim_diff: EXTRA_FLAGS += -DWEBP_HAVE_GIF -examples/cwebp: examples/libexample_util.a src/libwebp.a +examples/cwebp: examples/libexample_util.a examples/libexample_dec.a +examples/cwebp: src/libwebp.a examples/cwebp: EXTRA_LIBS += $(CWEBP_LIBS) examples/dwebp: examples/libexample_util.a src/libwebpdecoder.a examples/dwebp: EXTRA_LIBS += $(DWEBP_LIBS)