cwebp: extract jpeg decoding to its own module

Change-Id: I45e1f0fa7b34286dd98926e0485e5a8ab1964570
This commit is contained in:
James Zern 2012-12-06 13:48:21 -08:00
parent 2ee228f9a5
commit 6a871d66a4
6 changed files with 152 additions and 98 deletions

View File

@ -172,6 +172,7 @@ DSP_OBJS = \
$(DIROBJ)\dsp\yuv.obj \ $(DIROBJ)\dsp\yuv.obj \
EX_FORMAT_DEC_OBJS = \ EX_FORMAT_DEC_OBJS = \
$(DIROBJ)\examples/jpegdec.obj \
$(DIROBJ)\examples/pngdec.obj \ $(DIROBJ)\examples/pngdec.obj \
EX_UTIL_OBJS = \ EX_UTIL_OBJS = \

View File

@ -21,6 +21,7 @@ dwebp_CPPFLAGS += $(JPEG_INCLUDES) $(PNG_INCLUDES)
dwebp_LDADD = libexampleutil.la ../src/libwebp.la $(PNG_LIBS) $(JPEG_LIBS) dwebp_LDADD = libexampleutil.la ../src/libwebp.la $(PNG_LIBS) $(JPEG_LIBS)
cwebp_SOURCES = cwebp.c metadata.h stopwatch.h cwebp_SOURCES = cwebp.c metadata.h stopwatch.h
cwebp_SOURCES += jpegdec.c jpegdec.h
cwebp_SOURCES += pngdec.c pngdec.h cwebp_SOURCES += pngdec.c pngdec.h
cwebp_CPPFLAGS = $(AM_CPPFLAGS) $(USE_EXPERIMENTAL_CODE) cwebp_CPPFLAGS = $(AM_CPPFLAGS) $(USE_EXPERIMENTAL_CODE)
cwebp_CPPFLAGS += $(JPEG_INCLUDES) $(PNG_INCLUDES) $(TIFF_INCLUDES) cwebp_CPPFLAGS += $(JPEG_INCLUDES) $(PNG_INCLUDES) $(TIFF_INCLUDES)

View File

@ -18,11 +18,6 @@
#include "config.h" #include "config.h"
#endif #endif
#ifdef WEBP_HAVE_JPEG
#include <setjmp.h>
#include <jpeglib.h>
#endif
#ifdef WEBP_HAVE_TIFF #ifdef WEBP_HAVE_TIFF
#include <tiffio.h> #include <tiffio.h>
#endif #endif
@ -42,6 +37,7 @@
#include "webp/encode.h" #include "webp/encode.h"
#include "./jpegdec.h"
#include "./metadata.h" #include "./metadata.h"
#include "./pngdec.h" #include "./pngdec.h"
#include "./stopwatch.h" #include "./stopwatch.h"
@ -292,99 +288,6 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic,
#else // !HAVE_WINCODEC_H #else // !HAVE_WINCODEC_H
#ifdef WEBP_HAVE_JPEG
struct my_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
static void my_error_exit(j_common_ptr dinfo) {
struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
(*dinfo->err->output_message) (dinfo);
longjmp(myerr->setjmp_buffer, 1);
}
static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
int ok = 0;
int stride, width, height;
uint8_t* rgb = NULL;
uint8_t* row_ptr = NULL;
struct jpeg_decompress_struct dinfo;
struct my_error_mgr jerr;
JSAMPARRAY buffer;
dinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer)) {
Error:
jpeg_destroy_decompress(&dinfo);
goto End;
}
jpeg_create_decompress(&dinfo);
jpeg_stdio_src(&dinfo, in_file);
jpeg_read_header(&dinfo, TRUE);
dinfo.out_color_space = JCS_RGB;
dinfo.dct_method = JDCT_IFAST;
dinfo.do_fancy_upsampling = TRUE;
jpeg_start_decompress(&dinfo);
if (dinfo.output_components != 3) {
goto Error;
}
width = dinfo.output_width;
height = dinfo.output_height;
stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
rgb = (uint8_t*)malloc(stride * height);
if (rgb == NULL) {
goto End;
}
row_ptr = rgb;
buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
JPOOL_IMAGE, stride, 1);
if (buffer == NULL) {
goto End;
}
while (dinfo.output_scanline < dinfo.output_height) {
if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
goto End;
}
memcpy(row_ptr, buffer[0], stride);
row_ptr += stride;
}
jpeg_finish_decompress(&dinfo);
jpeg_destroy_decompress(&dinfo);
// WebP conversion.
pic->width = width;
pic->height = height;
ok = WebPPictureImportRGB(pic, rgb, stride);
End:
if (rgb) {
free(rgb);
}
return ok;
}
#else
static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
(void)in_file;
(void)pic;
fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
"development package before building.\n");
return 0;
}
#endif
#ifdef WEBP_HAVE_TIFF #ifdef WEBP_HAVE_TIFF
static int ReadTIFF(const char* const filename, static int ReadTIFF(const char* const filename,
WebPPicture* const pic, int keep_alpha) { WebPPicture* const pic, int keep_alpha) {

117
examples/jpegdec.c Normal file
View File

@ -0,0 +1,117 @@
// 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/
// -----------------------------------------------------------------------------
//
// JPEG decode.
#include "./jpegdec.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#ifdef WEBP_HAVE_JPEG
#include <jpeglib.h>
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
#include "webp/encode.h"
struct my_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
static void my_error_exit(j_common_ptr dinfo) {
struct my_error_mgr* myerr = (struct my_error_mgr*) dinfo->err;
(*dinfo->err->output_message) (dinfo);
longjmp(myerr->setjmp_buffer, 1);
}
int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
int ok = 0;
int stride, width, height;
uint8_t* rgb = NULL;
uint8_t* row_ptr = NULL;
struct jpeg_decompress_struct dinfo;
struct my_error_mgr jerr;
JSAMPARRAY buffer;
dinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer)) {
Error:
jpeg_destroy_decompress(&dinfo);
goto End;
}
jpeg_create_decompress(&dinfo);
jpeg_stdio_src(&dinfo, in_file);
jpeg_read_header(&dinfo, TRUE);
dinfo.out_color_space = JCS_RGB;
dinfo.dct_method = JDCT_IFAST;
dinfo.do_fancy_upsampling = TRUE;
jpeg_start_decompress(&dinfo);
if (dinfo.output_components != 3) {
goto Error;
}
width = dinfo.output_width;
height = dinfo.output_height;
stride = dinfo.output_width * dinfo.output_components * sizeof(*rgb);
rgb = (uint8_t*)malloc(stride * height);
if (rgb == NULL) {
goto End;
}
row_ptr = rgb;
buffer = (*dinfo.mem->alloc_sarray) ((j_common_ptr) &dinfo,
JPOOL_IMAGE, stride, 1);
if (buffer == NULL) {
goto End;
}
while (dinfo.output_scanline < dinfo.output_height) {
if (jpeg_read_scanlines(&dinfo, buffer, 1) != 1) {
goto End;
}
memcpy(row_ptr, buffer[0], stride);
row_ptr += stride;
}
jpeg_finish_decompress(&dinfo);
jpeg_destroy_decompress(&dinfo);
// WebP conversion.
pic->width = width;
pic->height = height;
ok = WebPPictureImportRGB(pic, rgb, stride);
End:
if (rgb) {
free(rgb);
}
return ok;
}
#else // !WEBP_HAVE_JPEG
int ReadJPEG(FILE* in_file, struct WebPPicture* const pic) {
(void)in_file;
(void)pic;
fprintf(stderr, "JPEG support not compiled. Please install the libjpeg "
"development package before building.\n");
return 0;
}
#endif // WEBP_HAVE_JPEG
// -----------------------------------------------------------------------------

31
examples/jpegdec.h Normal file
View File

@ -0,0 +1,31 @@
// 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/
// -----------------------------------------------------------------------------
//
// JPEG decode.
#ifndef WEBP_EXAMPLES_JPEGDEC_H_
#define WEBP_EXAMPLES_JPEGDEC_H_
#include <stdio.h>
#include "webp/types.h"
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
struct WebPPicture;
// Reads a JPEG from 'in_file', returning the decoded output in 'pic'.
// The output is RGB.
// Returns true on success.
int ReadJPEG(FILE* in_file, struct WebPPicture* const pic);
#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif
#endif // WEBP_EXAMPLES_JPEGDEC_H_

View File

@ -125,6 +125,7 @@ ENC_OBJS = \
src/enc/webpenc.o \ src/enc/webpenc.o \
EX_FORMAT_DEC_OBJS = \ EX_FORMAT_DEC_OBJS = \
examples/jpegdec.o \
examples/pngdec.o \ examples/pngdec.o \
EX_UTIL_OBJS = \ EX_UTIL_OBJS = \