mirror of
https://github.com/webmproject/libwebp.git
synced 2024-11-19 20:08:28 +01:00
* add decoding measurement using stopwatch.h (use -v option)
* support PNG output through WIC on Win32 Change-Id: I1dbcc6ab1360010aca25da80f30a74781aadc76d
This commit is contained in:
parent
746a4820b9
commit
b04b857a35
@ -1,6 +1,6 @@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/src
|
||||
|
||||
dwebp_SOURCES = dwebp.c
|
||||
dwebp_SOURCES = dwebp.c stopwatch.h
|
||||
dwebp_CPPFLAGS = $(AM_CPPFLAGS) $(PNG_INCLUDES)
|
||||
dwebp_LDADD = ../src/dec/libwebpdecode.la $(PNG_LIBS)
|
||||
|
||||
|
@ -184,7 +184,7 @@ top_build_prefix = @top_build_prefix@
|
||||
top_builddir = @top_builddir@
|
||||
top_srcdir = @top_srcdir@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/src
|
||||
dwebp_SOURCES = dwebp.c
|
||||
dwebp_SOURCES = dwebp.c stopwatch.h
|
||||
dwebp_CPPFLAGS = $(AM_CPPFLAGS) $(PNG_INCLUDES)
|
||||
dwebp_LDADD = ../src/dec/libwebpdecode.la $(PNG_LIBS)
|
||||
all: all-am
|
||||
|
202
examples/dwebp.c
202
examples/dwebp.c
@ -21,7 +21,18 @@
|
||||
#include <png.h>
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#define CINTERFACE
|
||||
#define COBJMACROS
|
||||
#define _WIN32_IE 0x500 // Workaround bug in shlwapi.h when compiling C++
|
||||
// code with COBJMACROS.
|
||||
#include <shlwapi.h>
|
||||
#include <windows.h>
|
||||
#include <wincodec.h>
|
||||
#endif
|
||||
|
||||
#include "webp/decode.h"
|
||||
#include "stopwatch.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
@ -29,7 +40,80 @@ extern "C" {
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifdef WEBP_HAVE_PNG
|
||||
static int verbose = 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#define IFS(fn) \
|
||||
do { \
|
||||
if (SUCCEEDED(hr)) \
|
||||
{ \
|
||||
hr = (fn); \
|
||||
if (FAILED(hr) && verbose) \
|
||||
printf(#fn " failed %08x\n", hr); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define MAKE_REFGUID(x) (x)
|
||||
#else
|
||||
#define MAKE_REFGUID(x) &(x)
|
||||
#endif
|
||||
|
||||
static HRESULT CreateOutputStream(const char* out_file_name, IStream** ppStream) {
|
||||
HRESULT hr = S_OK;
|
||||
IFS(SHCreateStreamOnFileA(out_file_name, STGM_WRITE | STGM_CREATE, ppStream));
|
||||
if (FAILED(hr))
|
||||
printf("Error opening output file %s (%08x)\n", out_file_name, hr);
|
||||
return hr;
|
||||
}
|
||||
|
||||
static HRESULT WriteUsingWIC(const char* out_file_name, REFGUID container_guid,
|
||||
unsigned char* rgb, int stride,
|
||||
uint32_t width, uint32_t height) {
|
||||
HRESULT hr = S_OK;
|
||||
IWICImagingFactory* pFactory = NULL;
|
||||
IWICBitmapFrameEncode* pFrame = NULL;
|
||||
IWICBitmapEncoder* pEncoder = NULL;
|
||||
IStream* pStream = NULL;
|
||||
GUID pixel_format = GUID_WICPixelFormat24bppBGR;
|
||||
|
||||
IFS(CoInitialize(NULL));
|
||||
IFS(CoCreateInstance(MAKE_REFGUID(CLSID_WICImagingFactory), NULL,
|
||||
CLSCTX_INPROC_SERVER, MAKE_REFGUID(IID_IWICImagingFactory),
|
||||
(LPVOID*)&pFactory));
|
||||
if (hr == REGDB_E_CLASSNOTREG) {
|
||||
printf("Couldn't access Windows Imaging Component (are you running \n");
|
||||
printf("Windows XP SP3 or newer?). PNG support not available.\n");
|
||||
printf("Use -ppm or -pgm for available PPM and PGM formats.\n");
|
||||
}
|
||||
IFS(CreateOutputStream(out_file_name, &pStream));
|
||||
IFS(IWICImagingFactory_CreateEncoder(pFactory, container_guid, NULL,
|
||||
&pEncoder));
|
||||
IFS(IWICBitmapEncoder_CreateNewFrame(pEncoder, &pFrame, NULL));
|
||||
IFS(IWICBitmapFrameEncode_Initialize(pFrame, NULL));
|
||||
IFS(IWICBitmapFrameEncode_SetSize(pFrame, width, height));
|
||||
IFS(IWICBitmapFrameEncode_SetPixelFormat(pFrame, &pixel_format));
|
||||
IFS(IWICBitmapFrameEncode_WritePixels(pFrame, height, stride,
|
||||
height * stride, rgb));
|
||||
IFS(IWICBitmapFrameEncode_Commit(pFrame));
|
||||
IFS(IWICBitmapEncoder_Commit(pEncoder));
|
||||
|
||||
if (pFrame != NULL) IUnknown_Release(pFrame);
|
||||
if (pEncoder != NULL) IUnknown_Release(pEncoder);
|
||||
if (pFactory != NULL) IUnknown_Release(pFactory);
|
||||
if (pStream != NULL) IUnknown_Release(pStream);
|
||||
return hr;
|
||||
}
|
||||
|
||||
static int WritePNG(const char* out_file_name, unsigned char* rgb, int stride,
|
||||
uint32_t width, uint32_t height) {
|
||||
return SUCCEEDED(WriteUsingWIC(out_file_name,
|
||||
MAKE_REFGUID(GUID_ContainerFormatPng), rgb, stride, width,
|
||||
height));
|
||||
}
|
||||
|
||||
#elif defined(WEBP_HAVE_PNG) // !WIN32
|
||||
static void PNGAPI error_function(png_structp png, png_const_charp dummy) {
|
||||
longjmp(png_jmpbuf(png), 1);
|
||||
}
|
||||
@ -67,18 +151,50 @@ static int WritePNG(FILE* out_file, unsigned char* rgb, int stride,
|
||||
png_destroy_write_struct(&png, &info);
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
#else // !WIN32 && !WEBP_HAVE_PNG
|
||||
|
||||
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("PNG support not compiled. Please install the libpng development "
|
||||
"package before building.\n");
|
||||
printf("You can run with -ppm flag to decode in PPM format.\n");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int WritePPM(FILE* fout, unsigned char* rgb,
|
||||
uint32_t width, uint32_t height) {
|
||||
fprintf(fout, "P6\n%d %d\n255\n", width, height);
|
||||
return (fwrite(rgb, width * height, 3, fout) == 3);
|
||||
}
|
||||
|
||||
static int WritePGM(FILE* fout,
|
||||
unsigned char* y_plane, unsigned char *u, unsigned char* v,
|
||||
int y_stride, int uv_stride,
|
||||
uint32_t width, uint32_t height) {
|
||||
// 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
|
||||
// odd dimensions.
|
||||
int ok = 1;
|
||||
unsigned int y;
|
||||
const unsigned int uv_width = (width + 1) / 2;
|
||||
const unsigned int uv_height = (height + 1) / 2;
|
||||
const unsigned int out_stride = (width + 1) & ~1;
|
||||
fprintf(fout, "P5\n%d %d\n255\n", out_stride, height + uv_height);
|
||||
for (y = 0; ok && y < height; ++y) {
|
||||
ok &= (fwrite(y_plane + y * y_stride, width, 1, fout) == 1);
|
||||
if (width & 1) fputc(0, fout); // padding byte
|
||||
}
|
||||
for (y = 0; ok && y < uv_height; ++y) {
|
||||
ok &= (fwrite(u + y * uv_stride, uv_width, 1, fout) == 1);
|
||||
ok &= (fwrite(v + y * uv_stride, uv_width, 1, fout) == 1);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
PNG = 0,
|
||||
PPM,
|
||||
@ -87,12 +203,13 @@ typedef enum {
|
||||
|
||||
static void help(const char *s) {
|
||||
printf("Usage: dwebp "
|
||||
"[in_file] [-h] [-ppm] [-pgm] [-o out_file]\n\n"
|
||||
"[in_file] [-h] [-v] [-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"
|
||||
"Use -v for verbose (e.g. print encoding/decoding times)\n"
|
||||
);
|
||||
}
|
||||
|
||||
@ -103,6 +220,7 @@ int main(int argc, const char *argv[]) {
|
||||
int width, height, stride, uv_stride;
|
||||
uint8_t* out = NULL, *u = NULL, *v = NULL;
|
||||
OutputFileFormat format = PNG;
|
||||
Stopwatch stop_watch;
|
||||
int c;
|
||||
for (c = 1; c < argc; ++c) {
|
||||
if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
|
||||
@ -114,6 +232,8 @@ int main(int argc, const char *argv[]) {
|
||||
format = PPM;
|
||||
} else if (!strcmp(argv[c], "-pgm")) {
|
||||
format = PGM;
|
||||
} else if (!strcmp(argv[c], "-v")) {
|
||||
verbose = 1;
|
||||
} else if (argv[c][0] == '-') {
|
||||
printf("Unknown option '%s'\n", argv[c]);
|
||||
help(argv[0]);
|
||||
@ -132,24 +252,33 @@ int main(int argc, const char *argv[]) {
|
||||
{
|
||||
uint32_t data_size = 0;
|
||||
void* data = NULL;
|
||||
int ok;
|
||||
FILE* const in = fopen(in_file, "rb");
|
||||
if (!in) {
|
||||
printf("cannot open input file '%s'\n", in_file);
|
||||
fprintf(stderr, "cannot open input file '%s'\n", in_file);
|
||||
return 1;
|
||||
}
|
||||
fseek(in, 0, SEEK_END);
|
||||
data_size = ftell(in);
|
||||
fseek(in, 0, SEEK_SET);
|
||||
data = malloc(data_size);
|
||||
const int ok = (fread(data, data_size, 1, in) == 1);
|
||||
ok = (fread(data, data_size, 1, in) == 1);
|
||||
fclose(in);
|
||||
if (!ok) {
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
StopwatchReadAndReset(&stop_watch);
|
||||
switch (format) {
|
||||
case PNG:
|
||||
#ifdef _WIN32
|
||||
out = WebPDecodeBGR((const uint8_t*)data, data_size, &width, &height);
|
||||
#else
|
||||
out = WebPDecodeRGB((const uint8_t*)data, data_size, &width, &height);
|
||||
#endif
|
||||
break;
|
||||
case PPM:
|
||||
out = WebPDecodeRGB((const uint8_t*)data, data_size, &width, &height);
|
||||
break;
|
||||
@ -162,53 +291,64 @@ int main(int argc, const char *argv[]) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
const double time = StopwatchReadAndReset(&stop_watch);
|
||||
printf("Time to decode picture: %.3fs\n", time);
|
||||
}
|
||||
|
||||
free(data);
|
||||
}
|
||||
|
||||
if (!out) {
|
||||
printf("Decoding of %s failed.\n", in_file);
|
||||
fprintf(stderr, "Decoding of %s failed.\n", in_file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (out_file) {
|
||||
FILE* const fout = fopen(out_file, "wb");
|
||||
if (fout) {
|
||||
FILE* fout = NULL;
|
||||
int needs_open_file = 0;
|
||||
StopwatchReadAndReset(&stop_watch);
|
||||
#ifdef _WIN32
|
||||
if (format != PNG) {
|
||||
needs_open_file = 1;
|
||||
}
|
||||
#else
|
||||
needs_open_file = 1;
|
||||
#endif
|
||||
if (needs_open_file) fout = fopen(out_file, "wb");
|
||||
if (!needs_open_file || fout) {
|
||||
int ok = 1;
|
||||
if (format == PNG) {
|
||||
#ifdef _WIN32
|
||||
ok &= WritePNG(out_file, out, 3 * width, width, height);
|
||||
#else
|
||||
ok &= WritePNG(fout, out, 3 * width, width, height);
|
||||
#endif
|
||||
} else if (format == PPM) {
|
||||
fprintf(fout, "P6\n%d %d\n255\n", width, height);
|
||||
ok &= (fwrite(out, width * height, 3, fout) == 3);
|
||||
ok &= WritePPM(fout, out, width, height);
|
||||
} 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
|
||||
// odd dimensions.
|
||||
int y;
|
||||
const int uv_width = (width + 1) / 2;
|
||||
const int uv_height = (height + 1) / 2;
|
||||
const int out_stride = (width + 1) & ~1;
|
||||
fprintf(fout, "P5\n%d %d\n255\n", out_stride, height + uv_height);
|
||||
for (y = 0; ok && y < height; ++y) {
|
||||
ok &= (fwrite(out + y * stride, width, 1, fout) == 1);
|
||||
if (width & 1) fputc(0, fout); // padding byte
|
||||
}
|
||||
for (y = 0; ok && y < uv_height; ++y) {
|
||||
ok &= (fwrite(u + y * uv_stride, uv_width, 1, fout) == 1);
|
||||
ok &= (fwrite(v + y * uv_stride, uv_width, 1, fout) == 1);
|
||||
}
|
||||
ok &= WritePGM(fout, out, u, v, stride, uv_stride, width, height);
|
||||
}
|
||||
if (fout)
|
||||
fclose(fout);
|
||||
if (ok) {
|
||||
printf("Saved file %s\n", out_file);
|
||||
} else {
|
||||
printf("Error writing file %s !!\n", out_file);
|
||||
if (verbose) {
|
||||
const double time = StopwatchReadAndReset(&stop_watch);
|
||||
printf("Time to write output: %.3fs\n", time);
|
||||
}
|
||||
} else {
|
||||
printf("Error opening output file %s\n", out_file);
|
||||
fprintf(stderr, "Error writing file %s !!\n", out_file);
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "Error opening output file %s\n", out_file);
|
||||
}
|
||||
printf("Decoded %s. Dimensions: %d x %d.\n", in_file, width, height);
|
||||
} else {
|
||||
printf("File %s can be decoded (dimensions: %d x %d).\n",
|
||||
in_file, width, height);
|
||||
printf("Nothing written; use -o flag to save the result as e.g. PNG.\n");
|
||||
}
|
||||
free(out);
|
||||
|
||||
return 0;
|
||||
|
47
examples/stopwatch.h
Normal file
47
examples/stopwatch.h
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright 2011 Google Inc.
|
||||
//
|
||||
// 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/
|
||||
// -----------------------------------------------------------------------------
|
||||
//
|
||||
// Helper functions to measure elapsed time.
|
||||
//
|
||||
// Author: Mikolaj Zalewski (mikolajz@google.com)
|
||||
|
||||
#ifndef WEBP_EXAMPLES_STOPWATCH_H_
|
||||
#define WEBP_EXAMPLES_STOPWATCH_H_
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
|
||||
typedef LARGE_INTEGER Stopwatch;
|
||||
|
||||
static inline double StopwatchReadAndReset(Stopwatch* watch) {
|
||||
const LARGE_INTEGER old_value = *watch;
|
||||
LARGE_INTEGER freq;
|
||||
if (!QueryPerformanceCounter(watch))
|
||||
return 0.0;
|
||||
if (!QueryPerformanceFrequency(&freq))
|
||||
return 0.0;
|
||||
if (freq.QuadPart == 0)
|
||||
return 0.0;
|
||||
return (watch->QuadPart - old_value.QuadPart) / (double)freq.QuadPart;
|
||||
}
|
||||
|
||||
|
||||
#else // !_WIN32
|
||||
#include <sys/time.h>
|
||||
|
||||
typedef struct timeval Stopwatch;
|
||||
|
||||
static inline double StopwatchReadAndReset(Stopwatch* watch) {
|
||||
const struct timeval old_value = *watch;
|
||||
gettimeofday(watch, NULL);
|
||||
return watch->tv_sec - old_value.tv_sec +
|
||||
(watch->tv_usec - old_value.tv_usec) / 1000000.0;
|
||||
}
|
||||
|
||||
#endif // !_WIN32
|
||||
|
||||
#endif // WEBP_EXAMPLES_STOPWATCH_H_
|
Loading…
Reference in New Issue
Block a user