add example_util.[hc]

moves ReadFile to a common location

Change-Id: Ia81230671f16d7d4d218b9954a5be55577a85413
This commit is contained in:
James Zern 2012-05-11 16:00:57 -07:00
parent 2924a5aee6
commit 061263a787
8 changed files with 157 additions and 139 deletions

View File

@ -172,6 +172,9 @@ DSP_OBJS = \
$(DIROBJ)\dsp\upsampling_sse2.obj \ $(DIROBJ)\dsp\upsampling_sse2.obj \
$(DIROBJ)\dsp\yuv.obj \ $(DIROBJ)\dsp\yuv.obj \
EX_UTIL_OBJS = \
$(DIROBJ)\examples\example_util.obj \
ENC_OBJS = \ ENC_OBJS = \
$(DIROBJ)\enc\alpha.obj \ $(DIROBJ)\enc\alpha.obj \
$(DIROBJ)\enc\analysis.obj \ $(DIROBJ)\enc\analysis.obj \
@ -239,7 +242,7 @@ clean::
@-erase /s $(DIROBJ)\$(DLLC) $(DIROBJ)\$(DLLINC) 2> NUL @-erase /s $(DIROBJ)\$(DLLC) $(DIROBJ)\$(DLLINC) 2> NUL
!ENDIF !ENDIF
$(EXAMPLES_OBJS): $(DIRLIB)\$(TARGET) $(EXAMPLES_OBJS): $(EX_UTIL_OBJS) $(DIRLIB)\$(TARGET)
$(OUTPUT_DIRS): $(OUTPUT_DIRS):
@if not exist "$(@)" mkdir "$(@)" @if not exist "$(@)" mkdir "$(@)"
@ -276,7 +279,7 @@ $(DIROBJ)\$(DLLC): $(DIROBJ)\$(DLLINC)
$(CC) $(CFLAGS) /Fd$(LIBPDBNAME) /Fo$(DIROBJ)\utils\ $< $(CC) $(CFLAGS) /Fd$(LIBPDBNAME) /Fo$(DIROBJ)\utils\ $<
{$(DIROBJ)\examples}.obj{$(DIRBIN)}.exe: {$(DIROBJ)\examples}.obj{$(DIRBIN)}.exe:
$(LNKEXE) $(LDFLAGS) /OUT:"$@" $< \ $(LNKEXE) $(LDFLAGS) /OUT:"$@" $< $(EX_UTIL_OBJS) \
ole32.lib windowscodecs.lib shlwapi.lib $(DIRLIB)\$(TARGET) ole32.lib windowscodecs.lib shlwapi.lib $(DIRLIB)\$(TARGET)
$(MT) -manifest $@.manifest -outputresource:$@;1 $(MT) -manifest $@.manifest -outputresource:$@;1
del $@.manifest del $@.manifest

View File

@ -1,10 +1,15 @@
AM_CPPFLAGS = -I$(top_srcdir)/src AM_CPPFLAGS = -I$(top_srcdir)/src
bin_PROGRAMS = dwebp cwebp webpmux bin_PROGRAMS = dwebp cwebp webpmux
noinst_LTLIBRARIES = libexampleutil.la
libexampleutil_la_SOURCES = example_util.c
libexampleutilinclude_HEADERS = example_util.h
libexampleutilincludedir =
dwebp_SOURCES = dwebp.c stopwatch.h dwebp_SOURCES = dwebp.c stopwatch.h
dwebp_CPPFLAGS = $(AM_CPPFLAGS) $(PNG_INCLUDES) $(JPEG_INCLUDES) $(USE_EXPERIMENTAL_CODE) dwebp_CPPFLAGS = $(AM_CPPFLAGS) $(PNG_INCLUDES) $(JPEG_INCLUDES) $(USE_EXPERIMENTAL_CODE)
dwebp_LDADD = ../src/libwebp.la $(PNG_LIBS) $(JPEG_LIBS) dwebp_LDADD = libexampleutil.la ../src/libwebp.la $(PNG_LIBS) $(JPEG_LIBS)
cwebp_SOURCES = cwebp.c stopwatch.h cwebp_SOURCES = cwebp.c stopwatch.h
cwebp_CPPFLAGS = $(AM_CPPFLAGS) $(PNG_INCLUDES) $(JPEG_INCLUDES) $(USE_EXPERIMENTAL_CODE) cwebp_CPPFLAGS = $(AM_CPPFLAGS) $(PNG_INCLUDES) $(JPEG_INCLUDES) $(USE_EXPERIMENTAL_CODE)
@ -12,4 +17,4 @@ cwebp_LDADD = ../src/libwebp.la $(PNG_LIBS) $(JPEG_LIBS)
webpmux_SOURCES = webpmux.c webpmux_SOURCES = webpmux.c
webpmux_CPPFLAGS = $(AM_CPPFLAGS) $(USE_EXPERIMENTAL_CODE) webpmux_CPPFLAGS = $(AM_CPPFLAGS) $(USE_EXPERIMENTAL_CODE)
webpmux_LDADD = ../src/mux/libwebpmux.la ../src/libwebp.la webpmux_LDADD = libexampleutil.la ../src/mux/libwebpmux.la ../src/libwebp.la

View File

@ -38,6 +38,7 @@
#endif #endif
#include "webp/decode.h" #include "webp/decode.h"
#include "./example_util.h"
#include "stopwatch.h" #include "stopwatch.h"
static int verbose = 0; static int verbose = 0;
@ -412,31 +413,15 @@ int main(int argc, const char *argv[]) {
Stopwatch stop_watch; Stopwatch stop_watch;
VP8StatusCode status = VP8_STATUS_OK; VP8StatusCode status = VP8_STATUS_OK;
int ok; int ok;
uint32_t data_size = 0; size_t data_size = 0;
void* data = NULL; const uint8_t* data = NULL;
FILE* const in = fopen(in_file, "rb");
if (!in) { if (!ExUtilReadFile(in_file, &data, &data_size)) return -1;
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);
ok = (fread(data, data_size, 1, in) == 1);
fclose(in);
if (!ok) {
fprintf(stderr, "Could not read %d bytes of data from file %s\n",
data_size, in_file);
free(data);
return -1;
}
if (verbose) if (verbose)
StopwatchReadAndReset(&stop_watch); StopwatchReadAndReset(&stop_watch);
status = WebPGetFeatures((const uint8_t*)data, data_size, bitstream); status = WebPGetFeatures(data, data_size, bitstream);
if (status != VP8_STATUS_OK) { if (status != VP8_STATUS_OK) {
goto end; goto end;
} }
@ -459,17 +444,17 @@ int main(int argc, const char *argv[]) {
output_buffer->colorspace = MODE_YUVA; output_buffer->colorspace = MODE_YUVA;
break; break;
default: default:
free(data); free((void*)data);
return -1; return -1;
} }
status = WebPDecode((const uint8_t*)data, data_size, &config); status = WebPDecode(data, data_size, &config);
if (verbose) { if (verbose) {
const double time = StopwatchReadAndReset(&stop_watch); const double time = StopwatchReadAndReset(&stop_watch);
printf("Time to decode picture: %.3fs\n", time); printf("Time to decode picture: %.3fs\n", time);
} }
end: end:
free(data); free((void*)data);
ok = (status == VP8_STATUS_OK); ok = (status == VP8_STATUS_OK);
if (!ok) { if (!ok) {
fprintf(stderr, "Decoding of %s failed.\n", in_file); fprintf(stderr, "Decoding of %s failed.\n", in_file);

59
examples/example_util.c Normal file
View File

@ -0,0 +1,59 @@
// 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/
// -----------------------------------------------------------------------------
//
// Utility functions used by the example programs.
//
#include "./example_util.h"
#include <stdio.h>
#include <stdlib.h>
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
// -----------------------------------------------------------------------------
// File I/O
int ExUtilReadFile(const char* const file_name,
const uint8_t** data, size_t* data_size) {
int ok;
void* file_data;
size_t file_size;
FILE* in;
if (file_name == NULL || data == NULL || data_size == NULL) return 0;
*data = NULL;
*data_size = 0;
in = fopen(file_name, "rb");
if (in == NULL) {
fprintf(stderr, "cannot open input file '%s'\n", file_name);
return 0;
}
fseek(in, 0, SEEK_END);
file_size = ftell(in);
fseek(in, 0, SEEK_SET);
file_data = malloc(file_size);
if (file_data == NULL) return 0;
ok = (fread(file_data, file_size, 1, in) == 1);
fclose(in);
if (!ok) {
fprintf(stderr, "Could not read %zu bytes of data from file %s\n",
file_size, file_name);
free(file_data);
return 0;
}
*data = (uint8_t*)file_data;
*data_size = file_size;
return 1;
}
#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif

30
examples/example_util.h Normal file
View 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/
// -----------------------------------------------------------------------------
//
// Utility functions used by the example programs.
//
#ifndef WEBP_EXAMPLES_EXAMPLE_UTIL_H_
#define WEBP_EXAMPLES_EXAMPLE_UTIL_H_
#include "webp/types.h"
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
// Allocates storage for entire file 'file_name' and returns contents and size
// in 'data' and 'data_size'. Returns 1 on success, 0 otherwise. '*data' should
// be deleted using free().
int ExUtilReadFile(const char* const file_name,
const uint8_t** data, size_t* data_size);
#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif
#endif // WEBP_EXAMPLES_EXAMPLE_UTIL_H_

View File

@ -31,6 +31,8 @@
#endif #endif
#endif #endif
#include "./example_util.h"
#ifdef _MSC_VER #ifdef _MSC_VER
#define snprintf _snprintf #define snprintf _snprintf
#endif #endif
@ -159,41 +161,6 @@ static void StartDisplay(const WebPDecBuffer* const pic) {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// File decoding // File decoding
static int ReadFile(void) {
int ok;
void* data = NULL;
size_t data_size = 0;
const char* const file_name = kParams.file_name;
FILE* in = NULL;
if (file_name == NULL) {
printf("missing input file!!\n");
Help();
return 0;
}
in = fopen(file_name, "rb");
if (in == NULL) {
fprintf(stderr, "cannot open input file '%s'\n", file_name);
return 0;
}
fseek(in, 0, SEEK_END);
data_size = ftell(in);
fseek(in, 0, SEEK_SET);
data = malloc(data_size);
if (data == NULL) return 0;
ok = (fread(data, data_size, 1, in) == 1);
fclose(in);
kParams.data.bytes_ = data;
kParams.data.size_ = data_size;
if (!ok) {
fprintf(stderr, "Could not read %zu bytes of data from file %s\n",
data_size, file_name);
return 0;
}
return 1;
}
static int Decode(const int frame_number, uint32_t* const duration) { static int Decode(const int frame_number, uint32_t* const duration) {
WebPDecoderConfig* const config = kParams.config; WebPDecoderConfig* const config = kParams.config;
WebPData *data, image_data; WebPData *data, image_data;
@ -312,7 +279,16 @@ int main(int argc, char *argv[]) {
} }
} }
if (!ReadFile()) goto Error; if (kParams.file_name == NULL) {
printf("missing input file!!\n");
Help();
return 0;
}
if (!ExUtilReadFile(kParams.file_name,
&kParams.data.bytes_, &kParams.data.size_)) {
goto Error;
}
kParams.mux = kParams.mux =
WebPMuxCreate(kParams.data.bytes_, kParams.data.size_, 0, NULL); WebPMuxCreate(kParams.data.bytes_, kParams.data.size_, 0, NULL);

View File

@ -53,6 +53,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "webp/mux.h" #include "webp/mux.h"
#include "./example_util.h"
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Config object to parse command-line arguments. // Config object to parse command-line arguments.
@ -319,66 +320,16 @@ static void PrintHelp(void) {
printf("\nINPUT & OUTPUT are in webp format.\n"); printf("\nINPUT & OUTPUT are in webp format.\n");
} }
static int ReadData(const char* filename, static int CreateMux(const char* const filename, WebPMux** mux) {
uint8_t** data_ptr, uint32_t* size_ptr) { size_t size = 0;
void* data = NULL; const uint8_t* data = NULL;
long size = 0;
int ok = 0;
FILE* in;
*size_ptr = 0;
in = fopen(filename, "rb");
if (!in) {
fprintf(stderr, "Failed to open file %s\n", filename);
return 0;
}
fseek(in, 0, SEEK_END);
size = ftell(in);
fseek(in, 0, SEEK_SET);
if (size > 0xffffffffu) {
fprintf(stderr, "Size (%ld bytes) is out of range for file %s\n",
size, filename);
size = 0;
goto Err;
}
if (size < 0) {
size = 0;
goto Err;
}
data = malloc(size);
if (data) {
if (fread(data, size, 1, in) != 1) {
free(data);
data = NULL;
size = 0;
fprintf(stderr, "Failed to read %ld bytes from file %s\n",
size, filename);
goto Err;
}
ok = 1;
} else {
fprintf(stderr, "Failed to allocate %ld bytes for reading file %s\n",
size, filename);
size = 0;
}
Err:
if (in != stdin) fclose(in);
*size_ptr = (uint32_t)size;
*data_ptr = (uint8_t*)data;
return ok;
}
static int ReadFile(const char* const filename, WebPMux** mux) {
uint32_t size = 0;
uint8_t* data = NULL;
WebPMuxState mux_state; WebPMuxState mux_state;
assert(mux != NULL); assert(mux != NULL);
if (!ReadData(filename, &data, &size)) return 0; if (!ExUtilReadFile(filename, &data, &size)) return 0;
*mux = WebPMuxCreate(data, size, 1, &mux_state); *mux = WebPMuxCreate(data, size, 1, &mux_state);
free(data); free((void*)data);
if (*mux != NULL && mux_state == WEBP_MUX_STATE_COMPLETE) return 1; if (*mux != NULL && mux_state == WEBP_MUX_STATE_COMPLETE) return 1;
fprintf(stderr, "Failed to create mux object from file %s. mux_state = %d.\n", fprintf(stderr, "Failed to create mux object from file %s. mux_state = %d.\n",
filename, mux_state); filename, mux_state);
@ -387,18 +338,18 @@ static int ReadFile(const char* const filename, WebPMux** mux) {
static int ReadImage(const char* filename, static int ReadImage(const char* filename,
WebPData* const image_ptr, WebPData* const alpha_ptr) { WebPData* const image_ptr, WebPData* const alpha_ptr) {
uint8_t* data = NULL; const uint8_t* data = NULL;
uint32_t size = 0; size_t size = 0;
WebPData image, alpha; WebPData image, alpha;
WebPMux* mux; WebPMux* mux;
WebPMuxError err; WebPMuxError err;
int ok = 0; int ok = 0;
WebPMuxState mux_state; WebPMuxState mux_state;
if (!ReadData(filename, &data, &size)) return 0; if (!ExUtilReadFile(filename, &data, &size)) return 0;
mux = WebPMuxCreate(data, size, 1, &mux_state); mux = WebPMuxCreate(data, size, 1, &mux_state);
free(data); free((void*)data);
if (mux == NULL || mux_state != WEBP_MUX_STATE_COMPLETE) { if (mux == NULL || mux_state != WEBP_MUX_STATE_COMPLETE) {
fprintf(stderr, fprintf(stderr,
"Failed to create mux object from file %s. mux_state = %d.\n", "Failed to create mux object from file %s. mux_state = %d.\n",
@ -838,8 +789,8 @@ static int GetFrameTile(const WebPMux* mux,
static int Process(const WebPMuxConfig* config) { static int Process(const WebPMuxConfig* config) {
WebPMux* mux = NULL; WebPMux* mux = NULL;
WebPData webpdata; WebPData webpdata;
uint8_t* data = NULL; const uint8_t* data = NULL;
uint32_t size = 0; size_t size = 0;
uint32_t x_offset = 0; uint32_t x_offset = 0;
uint32_t y_offset = 0; uint32_t y_offset = 0;
WebPMuxError err = WEBP_MUX_OK; WebPMuxError err = WEBP_MUX_OK;
@ -849,7 +800,7 @@ static int Process(const WebPMuxConfig* config) {
switch (config->action_type_) { switch (config->action_type_) {
case ACTION_GET: case ACTION_GET:
ok = ReadFile(config->input_, &mux); ok = CreateMux(config->input_, &mux);
if (!ok) goto Err2; if (!ok) goto Err2;
switch (feature->type_) { switch (feature->type_) {
case FEATURE_FRM: case FEATURE_FRM:
@ -956,9 +907,9 @@ static int Process(const WebPMuxConfig* config) {
break; break;
case FEATURE_ICCP: case FEATURE_ICCP:
ok = ReadFile(config->input_, &mux); ok = CreateMux(config->input_, &mux);
if (!ok) goto Err2; if (!ok) goto Err2;
ok = ReadData(feature->args_[0].filename_, &data, &size); ok = ExUtilReadFile(feature->args_[0].filename_, &data, &size);
if (!ok) goto Err2; if (!ok) goto Err2;
err = WebPMuxSetColorProfile(mux, data, size, 1); err = WebPMuxSetColorProfile(mux, data, size, 1);
free((void*)data); free((void*)data);
@ -968,12 +919,12 @@ static int Process(const WebPMuxConfig* config) {
break; break;
case FEATURE_XMP: case FEATURE_XMP:
ok = ReadFile(config->input_, &mux); ok = CreateMux(config->input_, &mux);
if (!ok) goto Err2; if (!ok) goto Err2;
ok = ReadData(feature->args_[0].filename_, &data, &size); ok = ExUtilReadFile(feature->args_[0].filename_, &data, &size);
if (!ok) goto Err2; if (!ok) goto Err2;
err = WebPMuxSetMetadata(mux, data, size, 1); err = WebPMuxSetMetadata(mux, data, size, 1);
free(data); free((void*)data);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
ERROR_GOTO2("ERROR#%d: Could not set XMP metadata.\n", err, Err2); ERROR_GOTO2("ERROR#%d: Could not set XMP metadata.\n", err, Err2);
} }
@ -987,7 +938,7 @@ static int Process(const WebPMuxConfig* config) {
break; break;
case ACTION_STRIP: case ACTION_STRIP:
ok = ReadFile(config->input_, &mux); ok = CreateMux(config->input_, &mux);
if (!ok) goto Err2; if (!ok) goto Err2;
switch (feature->type_) { switch (feature->type_) {
case FEATURE_ICCP: case FEATURE_ICCP:
@ -1012,7 +963,7 @@ static int Process(const WebPMuxConfig* config) {
break; break;
case ACTION_INFO: case ACTION_INFO:
ok = ReadFile(config->input_, &mux); ok = CreateMux(config->input_, &mux);
if (!ok) goto Err2; if (!ok) goto Err2;
ok = (DisplayInfo(mux) == WEBP_MUX_OK); ok = (DisplayInfo(mux) == WEBP_MUX_OK);
break; break;

View File

@ -106,6 +106,9 @@ ENC_OBJS = \
src/enc/vp8l.o \ src/enc/vp8l.o \
src/enc/webpenc.o \ src/enc/webpenc.o \
EX_UTIL_OBJS = \
examples/example_util.o \
MUX_OBJS = \ MUX_OBJS = \
src/mux/muxedit.o \ src/mux/muxedit.o \
src/mux/muxinternal.o \ src/mux/muxinternal.o \
@ -152,7 +155,7 @@ HDRS = \
src/webp/mux.h \ src/webp/mux.h \
src/webp/types.h \ src/webp/types.h \
OUT_LIBS = src/libwebp.a src/mux/libwebpmux.a OUT_LIBS = examples/libexample_util.a src/libwebp.a src/mux/libwebpmux.a
OUT_EXAMPLES = examples/cwebp examples/dwebp examples/webpmux OUT_EXAMPLES = examples/cwebp examples/dwebp examples/webpmux
OUTPUT = $(OUT_LIBS) $(OUT_EXAMPLES) examples/vwebp OUTPUT = $(OUT_LIBS) $(OUT_EXAMPLES) examples/vwebp
@ -162,19 +165,25 @@ all: ex
%.o: %.c $(HDRS) %.o: %.c $(HDRS)
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
examples/libexample_util.a: $(EX_UTIL_OBJS)
src/libwebp.a: $(LIBWEBP_OBJS) src/libwebp.a: $(LIBWEBP_OBJS)
$(AR) $(ARFLAGS) $@ $^
src/mux/libwebpmux.a: $(LIBWEBPMUX_OBJS) src/mux/libwebpmux.a: $(LIBWEBPMUX_OBJS)
%.a:
$(AR) $(ARFLAGS) $@ $^ $(AR) $(ARFLAGS) $@ $^
ex: $(OUT_EXAMPLES) ex: $(OUT_EXAMPLES)
examples/cwebp: examples/cwebp.o src/libwebp.a examples/cwebp: examples/cwebp.o
examples/dwebp: examples/dwebp.o src/libwebp.a examples/dwebp: examples/dwebp.o
examples/vwebp: examples/vwebp.o src/mux/libwebpmux.a src/libwebp.a examples/vwebp: examples/vwebp.o
examples/webpmux: examples/webpmux.o
examples/cwebp: src/libwebp.a
examples/dwebp: examples/libexample_util.a src/libwebp.a
examples/vwebp: examples/libexample_util.a src/mux/libwebpmux.a src/libwebp.a
examples/vwebp: EXTRA_LIBS += $(GL_LIBS) examples/vwebp: EXTRA_LIBS += $(GL_LIBS)
examples/webpmux: examples/webpmux.o src/mux/libwebpmux.a src/libwebp.a examples/webpmux: examples/libexample_util.a src/mux/libwebpmux.a src/libwebp.a
$(OUT_EXAMPLES) examples/vwebp: $(OUT_EXAMPLES) examples/vwebp:
$(CC) -o $@ $^ $(LDFLAGS) $(CC) -o $@ $^ $(LDFLAGS)