mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-12 22:14:29 +02:00
add support for PNG output (default)
regularize include guards Change-Id: Ia1d0fa49c8c98e2c11f775b839d04a24e5450170
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/src
|
||||
|
||||
dwebp_SOURCES = dwebp.c
|
||||
dwebp_LDADD = ../src/dec/libwebpdecode.la
|
||||
dwebp_CPPFLAGS = $(AM_CPPFLAGS) $(PNG_INCLUDES)
|
||||
dwebp_LDADD = ../src/dec/libwebpdecode.la $(PNG_LIBS)
|
||||
|
||||
bin_PROGRAMS = dwebp
|
||||
|
@ -47,9 +47,10 @@ CONFIG_CLEAN_FILES =
|
||||
CONFIG_CLEAN_VPATH_FILES =
|
||||
am__installdirs = "$(DESTDIR)$(bindir)"
|
||||
PROGRAMS = $(bin_PROGRAMS)
|
||||
am_dwebp_OBJECTS = dwebp.$(OBJEXT)
|
||||
am_dwebp_OBJECTS = dwebp-dwebp.$(OBJEXT)
|
||||
dwebp_OBJECTS = $(am_dwebp_OBJECTS)
|
||||
dwebp_DEPENDENCIES = ../src/dec/libwebpdecode.la
|
||||
am__DEPENDENCIES_1 =
|
||||
dwebp_DEPENDENCIES = ../src/dec/libwebpdecode.la $(am__DEPENDENCIES_1)
|
||||
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
|
||||
depcomp = $(SHELL) $(top_srcdir)/depcomp
|
||||
am__depfiles_maybe = depfiles
|
||||
@ -100,6 +101,7 @@ INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
|
||||
LD = @LD@
|
||||
LDFLAGS = @LDFLAGS@
|
||||
LIBOBJS = @LIBOBJS@
|
||||
LIBPNG_CONFIG = @LIBPNG_CONFIG@
|
||||
LIBS = @LIBS@
|
||||
LIBTOOL = @LIBTOOL@
|
||||
LIPO = @LIPO@
|
||||
@ -121,6 +123,8 @@ PACKAGE_TARNAME = @PACKAGE_TARNAME@
|
||||
PACKAGE_URL = @PACKAGE_URL@
|
||||
PACKAGE_VERSION = @PACKAGE_VERSION@
|
||||
PATH_SEPARATOR = @PATH_SEPARATOR@
|
||||
PNG_INCLUDES = @PNG_INCLUDES@
|
||||
PNG_LIBS = @PNG_LIBS@
|
||||
RANLIB = @RANLIB@
|
||||
SED = @SED@
|
||||
SET_MAKE = @SET_MAKE@
|
||||
@ -181,7 +185,8 @@ top_builddir = @top_builddir@
|
||||
top_srcdir = @top_srcdir@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/src
|
||||
dwebp_SOURCES = dwebp.c
|
||||
dwebp_LDADD = ../src/dec/libwebpdecode.la
|
||||
dwebp_CPPFLAGS = $(AM_CPPFLAGS) $(PNG_INCLUDES)
|
||||
dwebp_LDADD = ../src/dec/libwebpdecode.la $(PNG_LIBS)
|
||||
all: all-am
|
||||
|
||||
.SUFFIXES:
|
||||
@ -269,7 +274,7 @@ mostlyclean-compile:
|
||||
distclean-compile:
|
||||
-rm -f *.tab.c
|
||||
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dwebp.Po@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dwebp-dwebp.Po@am__quote@
|
||||
|
||||
.c.o:
|
||||
@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
|
||||
@ -292,6 +297,20 @@ distclean-compile:
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCC_FALSE@ $(LTCOMPILE) -c -o $@ $<
|
||||
|
||||
dwebp-dwebp.o: dwebp.c
|
||||
@am__fastdepCC_TRUE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(dwebp_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT dwebp-dwebp.o -MD -MP -MF $(DEPDIR)/dwebp-dwebp.Tpo -c -o dwebp-dwebp.o `test -f 'dwebp.c' || echo '$(srcdir)/'`dwebp.c
|
||||
@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/dwebp-dwebp.Tpo $(DEPDIR)/dwebp-dwebp.Po
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='dwebp.c' object='dwebp-dwebp.o' libtool=no @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(dwebp_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o dwebp-dwebp.o `test -f 'dwebp.c' || echo '$(srcdir)/'`dwebp.c
|
||||
|
||||
dwebp-dwebp.obj: dwebp.c
|
||||
@am__fastdepCC_TRUE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(dwebp_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT dwebp-dwebp.obj -MD -MP -MF $(DEPDIR)/dwebp-dwebp.Tpo -c -o dwebp-dwebp.obj `if test -f 'dwebp.c'; then $(CYGPATH_W) 'dwebp.c'; else $(CYGPATH_W) '$(srcdir)/dwebp.c'; fi`
|
||||
@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/dwebp-dwebp.Tpo $(DEPDIR)/dwebp-dwebp.Po
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='dwebp.c' object='dwebp-dwebp.obj' libtool=no @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(dwebp_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o dwebp-dwebp.obj `if test -f 'dwebp.c'; then $(CYGPATH_W) 'dwebp.c'; else $(CYGPATH_W) '$(srcdir)/dwebp.c'; fi`
|
||||
|
||||
mostlyclean-libtool:
|
||||
-rm -f *.lo
|
||||
|
||||
|
102
examples/dwebp.c
102
examples/dwebp.c
@ -17,6 +17,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef WEBP_HAVE_PNG
|
||||
#include <png.h>
|
||||
#endif
|
||||
|
||||
#include "webp/decode.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
@ -25,10 +29,69 @@ extern "C" {
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifdef WEBP_HAVE_PNG
|
||||
static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
|
||||
longjmp(png_jmpbuf(png), 1);
|
||||
}
|
||||
|
||||
static int WritePNG(FILE* out_file, unsigned char* rgb, int stride,
|
||||
png_uint_32 width, png_uint_32 height) {
|
||||
png_structp png;
|
||||
png_infop info;
|
||||
int y;
|
||||
|
||||
png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
|
||||
NULL, error_function, NULL);
|
||||
if (png == NULL) {
|
||||
return 0;
|
||||
}
|
||||
info = png_create_info_struct(png);
|
||||
if (info == NULL) {
|
||||
png_destroy_write_struct(&png, NULL);
|
||||
return 0;
|
||||
}
|
||||
if (setjmp(png_jmpbuf(png))) {
|
||||
png_destroy_write_struct(&png, &info);
|
||||
return 0;
|
||||
}
|
||||
png_init_io(png, out_file);
|
||||
png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGB,
|
||||
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
|
||||
PNG_FILTER_TYPE_DEFAULT);
|
||||
png_write_info(png, info);
|
||||
for (y = 0; y < height; ++y) {
|
||||
png_bytep row = rgb + y * stride;
|
||||
png_write_rows(png, &row, 1);
|
||||
}
|
||||
png_write_end(png, info);
|
||||
png_destroy_write_struct(&png, &info);
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
|
||||
typedef uint32_t png_uint_32;
|
||||
|
||||
static int WritePNG(FILE* out_file, unsigned char* rgb, int stride,
|
||||
png_uint_32 width, png_uint_32 height) {
|
||||
printf("PNG support not compiled. Please use ./configure --enable-png\n");
|
||||
printf("You can run with -ppm flag to decode in PPM format.\n");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
PNG = 0,
|
||||
PPM,
|
||||
PGM,
|
||||
} OutputFileFormat;
|
||||
|
||||
static void help(const char *s) {
|
||||
printf("Usage: dwebp "
|
||||
"[options] [in_file] [-h] [-raw] [-o ppm_file]\n\n"
|
||||
" -raw: save the raw YUV samples as a grayscale PGM\n"
|
||||
"[in_file] [-h] [-ppm] [-pgm] [-o out_file]\n\n"
|
||||
"Decodes the WebP image file to PNG format [Default]\n"
|
||||
"Use following options to convert into alternate image formats:\n"
|
||||
" -ppm: save the raw RGB samples as color PPM\n"
|
||||
" -pgm: save the raw YUV samples as a grayscale PGM\n"
|
||||
" file with IMC4 layout.\n"
|
||||
);
|
||||
}
|
||||
@ -36,20 +99,21 @@ static void help(const char *s) {
|
||||
int main(int argc, const char *argv[]) {
|
||||
const char *in_file = NULL;
|
||||
const char *out_file = NULL;
|
||||
int raw_output = 0;
|
||||
|
||||
int width, height, stride, uv_stride;
|
||||
uint8_t* out = NULL, *u = NULL, *v = NULL;
|
||||
|
||||
OutputFileFormat format = PNG;
|
||||
int c;
|
||||
for (c = 1; c < argc; ++c) {
|
||||
if (!strcmp(argv[c], "-h")) {
|
||||
if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
|
||||
help(argv[0]);
|
||||
return 0;
|
||||
} else if (!strcmp(argv[c], "-o") && c < argc - 1) {
|
||||
out_file = argv[++c];
|
||||
} else if (!strcmp(argv[c], "-raw")) {
|
||||
raw_output = 1;
|
||||
} else if (!strcmp(argv[c], "-ppm")) {
|
||||
format = PPM;
|
||||
} else if (!strcmp(argv[c], "-pgm")) {
|
||||
format = PGM;
|
||||
} else if (argv[c][0] == '-') {
|
||||
printf("Unknown option '%s'\n", argv[c]);
|
||||
help(argv[0]);
|
||||
@ -84,12 +148,20 @@ int main(int argc, const char *argv[]) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!raw_output) {
|
||||
out = WebPDecodeRGB((const uint8_t*)data, data_size, &width, &height);
|
||||
} else {
|
||||
out = WebPDecodeYUV((const uint8_t*)data, data_size, &width, &height,
|
||||
&u, &v, &stride, &uv_stride);
|
||||
switch (format) {
|
||||
case PNG:
|
||||
case PPM:
|
||||
out = WebPDecodeRGB((const uint8_t*)data, data_size, &width, &height);
|
||||
break;
|
||||
case PGM:
|
||||
out = WebPDecodeYUV((const uint8_t*)data, data_size, &width, &height,
|
||||
&u, &v, &stride, &uv_stride);
|
||||
break;
|
||||
default:
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(data);
|
||||
}
|
||||
|
||||
@ -102,10 +174,12 @@ int main(int argc, const char *argv[]) {
|
||||
FILE* const fout = fopen(out_file, "wb");
|
||||
if (fout) {
|
||||
int ok = 1;
|
||||
if (!raw_output) {
|
||||
if (format == PNG) {
|
||||
ok &= WritePNG(fout, out, 3 * width, width, height);
|
||||
} else if (format == PPM) {
|
||||
fprintf(fout, "P6\n%d %d\n255\n", width, height);
|
||||
ok &= (fwrite(out, width * height, 3, fout) == 3);
|
||||
} else {
|
||||
} else if (format == PGM) {
|
||||
// Save a grayscale PGM file using the IMC4 layout
|
||||
// (http://www.fourcc.org/yuv.php#IMC4). This is a very
|
||||
// convenient format for viewing the samples, esp. for
|
||||
|
Reference in New Issue
Block a user