mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-12 22:14:29 +02:00
add example_util.[hc]
moves ReadFile to a common location Change-Id: Ia81230671f16d7d4d218b9954a5be55577a85413
This commit is contained in:
@ -1,10 +1,15 @@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/src
|
||||
|
||||
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_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_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_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
|
||||
|
@ -38,6 +38,7 @@
|
||||
#endif
|
||||
|
||||
#include "webp/decode.h"
|
||||
#include "./example_util.h"
|
||||
#include "stopwatch.h"
|
||||
|
||||
static int verbose = 0;
|
||||
@ -412,31 +413,15 @@ int main(int argc, const char *argv[]) {
|
||||
Stopwatch stop_watch;
|
||||
VP8StatusCode status = VP8_STATUS_OK;
|
||||
int ok;
|
||||
uint32_t data_size = 0;
|
||||
void* data = NULL;
|
||||
FILE* const in = fopen(in_file, "rb");
|
||||
size_t data_size = 0;
|
||||
const uint8_t* data = NULL;
|
||||
|
||||
if (!in) {
|
||||
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 (!ExUtilReadFile(in_file, &data, &data_size)) return -1;
|
||||
|
||||
if (verbose)
|
||||
StopwatchReadAndReset(&stop_watch);
|
||||
|
||||
status = WebPGetFeatures((const uint8_t*)data, data_size, bitstream);
|
||||
status = WebPGetFeatures(data, data_size, bitstream);
|
||||
if (status != VP8_STATUS_OK) {
|
||||
goto end;
|
||||
}
|
||||
@ -459,17 +444,17 @@ int main(int argc, const char *argv[]) {
|
||||
output_buffer->colorspace = MODE_YUVA;
|
||||
break;
|
||||
default:
|
||||
free(data);
|
||||
free((void*)data);
|
||||
return -1;
|
||||
}
|
||||
status = WebPDecode((const uint8_t*)data, data_size, &config);
|
||||
status = WebPDecode(data, data_size, &config);
|
||||
|
||||
if (verbose) {
|
||||
const double time = StopwatchReadAndReset(&stop_watch);
|
||||
printf("Time to decode picture: %.3fs\n", time);
|
||||
}
|
||||
end:
|
||||
free(data);
|
||||
free((void*)data);
|
||||
ok = (status == VP8_STATUS_OK);
|
||||
if (!ok) {
|
||||
fprintf(stderr, "Decoding of %s failed.\n", in_file);
|
||||
|
59
examples/example_util.c
Normal file
59
examples/example_util.c
Normal 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
30
examples/example_util.h
Normal 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_
|
@ -31,6 +31,8 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "./example_util.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
@ -159,41 +161,6 @@ static void StartDisplay(const WebPDecBuffer* const pic) {
|
||||
//------------------------------------------------------------------------------
|
||||
// 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) {
|
||||
WebPDecoderConfig* const config = kParams.config;
|
||||
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 =
|
||||
WebPMuxCreate(kParams.data.bytes_, kParams.data.size_, 0, NULL);
|
||||
|
@ -53,6 +53,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "webp/mux.h"
|
||||
#include "./example_util.h"
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Config object to parse command-line arguments.
|
||||
@ -319,66 +320,16 @@ static void PrintHelp(void) {
|
||||
printf("\nINPUT & OUTPUT are in webp format.\n");
|
||||
}
|
||||
|
||||
static int ReadData(const char* filename,
|
||||
uint8_t** data_ptr, uint32_t* size_ptr) {
|
||||
void* 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;
|
||||
static int CreateMux(const char* const filename, WebPMux** mux) {
|
||||
size_t size = 0;
|
||||
const uint8_t* data = NULL;
|
||||
WebPMuxState mux_state;
|
||||
|
||||
assert(mux != NULL);
|
||||
|
||||
if (!ReadData(filename, &data, &size)) return 0;
|
||||
if (!ExUtilReadFile(filename, &data, &size)) return 0;
|
||||
*mux = WebPMuxCreate(data, size, 1, &mux_state);
|
||||
free(data);
|
||||
free((void*)data);
|
||||
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",
|
||||
filename, mux_state);
|
||||
@ -387,18 +338,18 @@ static int ReadFile(const char* const filename, WebPMux** mux) {
|
||||
|
||||
static int ReadImage(const char* filename,
|
||||
WebPData* const image_ptr, WebPData* const alpha_ptr) {
|
||||
uint8_t* data = NULL;
|
||||
uint32_t size = 0;
|
||||
const uint8_t* data = NULL;
|
||||
size_t size = 0;
|
||||
WebPData image, alpha;
|
||||
WebPMux* mux;
|
||||
WebPMuxError err;
|
||||
int ok = 0;
|
||||
WebPMuxState mux_state;
|
||||
|
||||
if (!ReadData(filename, &data, &size)) return 0;
|
||||
if (!ExUtilReadFile(filename, &data, &size)) return 0;
|
||||
|
||||
mux = WebPMuxCreate(data, size, 1, &mux_state);
|
||||
free(data);
|
||||
free((void*)data);
|
||||
if (mux == NULL || mux_state != WEBP_MUX_STATE_COMPLETE) {
|
||||
fprintf(stderr,
|
||||
"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) {
|
||||
WebPMux* mux = NULL;
|
||||
WebPData webpdata;
|
||||
uint8_t* data = NULL;
|
||||
uint32_t size = 0;
|
||||
const uint8_t* data = NULL;
|
||||
size_t size = 0;
|
||||
uint32_t x_offset = 0;
|
||||
uint32_t y_offset = 0;
|
||||
WebPMuxError err = WEBP_MUX_OK;
|
||||
@ -849,7 +800,7 @@ static int Process(const WebPMuxConfig* config) {
|
||||
|
||||
switch (config->action_type_) {
|
||||
case ACTION_GET:
|
||||
ok = ReadFile(config->input_, &mux);
|
||||
ok = CreateMux(config->input_, &mux);
|
||||
if (!ok) goto Err2;
|
||||
switch (feature->type_) {
|
||||
case FEATURE_FRM:
|
||||
@ -956,9 +907,9 @@ static int Process(const WebPMuxConfig* config) {
|
||||
break;
|
||||
|
||||
case FEATURE_ICCP:
|
||||
ok = ReadFile(config->input_, &mux);
|
||||
ok = CreateMux(config->input_, &mux);
|
||||
if (!ok) goto Err2;
|
||||
ok = ReadData(feature->args_[0].filename_, &data, &size);
|
||||
ok = ExUtilReadFile(feature->args_[0].filename_, &data, &size);
|
||||
if (!ok) goto Err2;
|
||||
err = WebPMuxSetColorProfile(mux, data, size, 1);
|
||||
free((void*)data);
|
||||
@ -968,12 +919,12 @@ static int Process(const WebPMuxConfig* config) {
|
||||
break;
|
||||
|
||||
case FEATURE_XMP:
|
||||
ok = ReadFile(config->input_, &mux);
|
||||
ok = CreateMux(config->input_, &mux);
|
||||
if (!ok) goto Err2;
|
||||
ok = ReadData(feature->args_[0].filename_, &data, &size);
|
||||
ok = ExUtilReadFile(feature->args_[0].filename_, &data, &size);
|
||||
if (!ok) goto Err2;
|
||||
err = WebPMuxSetMetadata(mux, data, size, 1);
|
||||
free(data);
|
||||
free((void*)data);
|
||||
if (err != WEBP_MUX_OK) {
|
||||
ERROR_GOTO2("ERROR#%d: Could not set XMP metadata.\n", err, Err2);
|
||||
}
|
||||
@ -987,7 +938,7 @@ static int Process(const WebPMuxConfig* config) {
|
||||
break;
|
||||
|
||||
case ACTION_STRIP:
|
||||
ok = ReadFile(config->input_, &mux);
|
||||
ok = CreateMux(config->input_, &mux);
|
||||
if (!ok) goto Err2;
|
||||
switch (feature->type_) {
|
||||
case FEATURE_ICCP:
|
||||
@ -1012,7 +963,7 @@ static int Process(const WebPMuxConfig* config) {
|
||||
break;
|
||||
|
||||
case ACTION_INFO:
|
||||
ok = ReadFile(config->input_, &mux);
|
||||
ok = CreateMux(config->input_, &mux);
|
||||
if (!ok) goto Err2;
|
||||
ok = (DisplayInfo(mux) == WEBP_MUX_OK);
|
||||
break;
|
||||
|
Reference in New Issue
Block a user