examples: slight cleanup

spelling/indent/condense a few lines
removal/restriction of extern "C" usage
add const where applicable

Change-Id: Idd49b3498b1d7fc391724b4cde63706022217cb0
This commit is contained in:
James Zern 2011-11-04 15:20:08 -07:00
parent abd030b573
commit 04e84cf1f1
3 changed files with 71 additions and 88 deletions

View File

@ -54,9 +54,17 @@ DEFINE_GUID(GUID_WICPixelFormat32bppRGBA,
#include "webp/encode.h" #include "webp/encode.h"
#include "stopwatch.h" #include "stopwatch.h"
#ifndef WEBP_DLL #ifndef WEBP_DLL
extern void* VP8GetCPUInfo; // opaque forward declaration. #if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif #endif
extern void* VP8GetCPUInfo; // opaque forward declaration.
#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif
#endif // WEBP_DLL
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
static int verbose = 0; static int verbose = 0;
@ -262,7 +270,7 @@ static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
dinfo.err = jpeg_std_error(&jerr.pub); dinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit; jerr.pub.error_exit = my_error_exit;
if (setjmp (jerr.setjmp_buffer)) { if (setjmp(jerr.setjmp_buffer)) {
Error: Error:
jpeg_destroy_decompress(&dinfo); jpeg_destroy_decompress(&dinfo);
goto End; goto End;
@ -306,8 +314,8 @@ static int ReadJPEG(FILE* in_file, WebPPicture* const pic) {
row_ptr += stride; row_ptr += stride;
} }
jpeg_finish_decompress (&dinfo); jpeg_finish_decompress(&dinfo);
jpeg_destroy_decompress (&dinfo); jpeg_destroy_decompress(&dinfo);
// WebP conversion. // WebP conversion.
pic->width = width; pic->width = width;
@ -502,7 +510,7 @@ static void PrintByteCount(const int bytes[4], int total_size,
total += bytes[s]; total += bytes[s];
if (totals) totals[s] += bytes[s]; if (totals) totals[s] += bytes[s];
} }
fprintf(stderr,"| %7d (%.1f%%)\n", total, 100.f * total / total_size); fprintf(stderr, "| %7d (%.1f%%)\n", total, 100.f * total / total_size);
} }
static void PrintPercents(const int counts[4], int total) { static void PrintPercents(const int counts[4], int total) {
@ -510,7 +518,7 @@ static void PrintPercents(const int counts[4], int total) {
for (s = 0; s < 4; ++s) { for (s = 0; s < 4; ++s) {
fprintf(stderr, "| %2d%%", 100 * counts[s] / total); fprintf(stderr, "| %2d%%", 100 * counts[s] / total);
} }
fprintf(stderr,"| %7d\n", total); fprintf(stderr, "| %7d\n", total);
} }
static void PrintValues(const int values[4]) { static void PrintValues(const int values[4]) {
@ -518,14 +526,14 @@ static void PrintValues(const int values[4]) {
for (s = 0; s < 4; ++s) { for (s = 0; s < 4; ++s) {
fprintf(stderr, "| %7d ", values[s]); fprintf(stderr, "| %7d ", values[s]);
} }
fprintf(stderr,"|\n"); fprintf(stderr, "|\n");
} }
static void PrintExtraInfo(const WebPPicture* const pic, int short_output) { static void PrintExtraInfo(const WebPPicture* const pic, int short_output) {
const WebPAuxStats* const stats = pic->stats; const WebPAuxStats* const stats = pic->stats;
if (short_output) { if (short_output) {
fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]); fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
} else{ } else {
const int num_i4 = stats->block_count[0]; const int num_i4 = stats->block_count[0];
const int num_i16 = stats->block_count[1]; const int num_i16 = stats->block_count[1];
const int num_skip = stats->block_count[2]; const int num_skip = stats->block_count[2];

View File

@ -40,14 +40,18 @@
#include "webp/decode.h" #include "webp/decode.h"
#include "stopwatch.h" #include "stopwatch.h"
static int verbose = 0;
#ifndef WEBP_DLL
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
extern "C" { extern "C" {
#endif #endif
static int verbose = 0;
#ifndef WEBP_DLL
extern void* VP8GetCPUInfo; // opaque forward declaration. extern void* VP8GetCPUInfo; // opaque forward declaration.
#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif #endif
#endif // WEBP_DLL
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -493,7 +497,3 @@ int main(int argc, const char *argv[]) {
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif

View File

@ -8,7 +8,7 @@
// Simple command-line to create a WebP container file and to extract or strip // Simple command-line to create a WebP container file and to extract or strip
// relevant data from the container file. // relevant data from the container file.
// //
// Compile with: gcc -o webpmux webpmux.c -lwebpmux // Compile with: gcc -o webpmux webpmux.c -lwebpmux -lwebp
// //
// //
// Authors: Vikas (vikaas.arora@gmail.com), // Authors: Vikas (vikaas.arora@gmail.com),
@ -17,7 +17,6 @@
/* Usage examples: /* Usage examples:
Create container WebP file: Create container WebP file:
webpmux -tile tile_1.webp +0+0 \ webpmux -tile tile_1.webp +0+0 \
-tile tile_2.webp +960+0 \ -tile tile_2.webp +960+0 \
-tile tile_3.webp +0+576 \ -tile tile_3.webp +0+576 \
@ -32,35 +31,21 @@
-o out_animation_container.webp -o out_animation_container.webp
webpmux -set icc image_profile.icc in.webp -o out_icc_container.webp webpmux -set icc image_profile.icc in.webp -o out_icc_container.webp
webpmux -set xmp image_metadata.xmp in.webp -o out_xmp_container.webp webpmux -set xmp image_metadata.xmp in.webp -o out_xmp_container.webp
Extract relevant data from WebP container file: Extract relevant data from WebP container file:
webpmux -get tile n in.webp -o out_tile.webp webpmux -get tile n in.webp -o out_tile.webp
webpmux -get frame n in.webp -o out_frame.webp webpmux -get frame n in.webp -o out_frame.webp
webpmux -get icc in.webp -o image_profile.icc webpmux -get icc in.webp -o image_profile.icc
webpmux -get xmp in.webp -o image_metadata.xmp webpmux -get xmp in.webp -o image_metadata.xmp
Strip data from WebP Container file: Strip data from WebP Container file:
webpmux -strip icc in.webp -o out.webp
webmux -strip icc in.webp -o out.webp webpmux -strip xmp in.webp -o out.webp
webmux -strip xmp in.webp -o out.webp
Misc: Misc:
webpmux -info in.webp webpmux -info in.webp
webpmux [ -h | -help ]
webpmux -help
webpmux -h
*/ */
#include <assert.h> #include <assert.h>
@ -69,10 +54,6 @@
#include <string.h> #include <string.h>
#include "webp/mux.h" #include "webp/mux.h"
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Config object to parse command-line arguments. // Config object to parse command-line arguments.
@ -121,8 +102,8 @@ typedef struct {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Helper functions. // Helper functions.
static int CountOccurences(const char* arglist[], int list_length, static int CountOccurrences(const char* arglist[], int list_length,
const char* arg) { const char* arg) {
int i; int i;
int num_occurences = 0; int num_occurences = 0;
@ -135,7 +116,7 @@ static int CountOccurences(const char* arglist[], int list_length,
} }
static int IsNotCompatible(int count1, int count2) { static int IsNotCompatible(int count1, int count2) {
return (count1 > 0) != (count2 > 0); return (count1 > 0) != (count2 > 0);
} }
#define RETURN_IF_ERROR(ERR_MSG) \ #define RETURN_IF_ERROR(ERR_MSG) \
@ -151,19 +132,25 @@ static int IsNotCompatible(int count1, int count2) {
} }
#define ERROR_GOTO1(ERR_MSG, LABEL) \ #define ERROR_GOTO1(ERR_MSG, LABEL) \
fprintf(stderr, ERR_MSG); \ do { \
ok = 0; \ fprintf(stderr, ERR_MSG); \
goto LABEL; ok = 0; \
goto LABEL; \
} while (0)
#define ERROR_GOTO2(ERR_MSG, FORMAT_STR, LABEL) \ #define ERROR_GOTO2(ERR_MSG, FORMAT_STR, LABEL) \
fprintf(stderr, ERR_MSG, FORMAT_STR); \ do { \
ok = 0; \ fprintf(stderr, ERR_MSG, FORMAT_STR); \
goto LABEL; ok = 0; \
goto LABEL; \
} while (0)
#define ERROR_GOTO3(ERR_MSG, FORMAT_STR1, FORMAT_STR2, LABEL) \ #define ERROR_GOTO3(ERR_MSG, FORMAT_STR1, FORMAT_STR2, LABEL) \
fprintf(stderr, ERR_MSG, FORMAT_STR1, FORMAT_STR2); \ do { \
ok = 0; \ fprintf(stderr, ERR_MSG, FORMAT_STR1, FORMAT_STR2); \
goto LABEL; ok = 0; \
goto LABEL; \
} while (0)
static WebPMuxError DisplayInfo(const WebPMux* mux) { static WebPMuxError DisplayInfo(const WebPMux* mux) {
int nFrames; int nFrames;
@ -202,7 +189,7 @@ static WebPMuxError DisplayInfo(const WebPMux* mux) {
int i; int i;
uint32_t x_offset, y_offset, duration; uint32_t x_offset, y_offset, duration;
fprintf(stderr, "No : x_offset y_offset duration\n"); fprintf(stderr, "No.: x_offset y_offset duration\n");
for (i = 1; i <= nFrames; i++) { for (i = 1; i <= nFrames; i++) {
err = WebPMuxGetFrame(mux, i, &data, &size, &x_offset, &y_offset, err = WebPMuxGetFrame(mux, i, &data, &size, &x_offset, &y_offset,
&duration); &duration);
@ -220,7 +207,7 @@ static WebPMuxError DisplayInfo(const WebPMux* mux) {
if (nTiles > 0) { if (nTiles > 0) {
int i; int i;
uint32_t x_offset, y_offset; uint32_t x_offset, y_offset;
fprintf(stderr, "No : x_offset y_offset\n"); fprintf(stderr, "No.: x_offset y_offset\n");
for (i = 1; i <= nTiles; i++) { for (i = 1; i <= nTiles; i++) {
err = WebPMuxGetTile(mux, i, &data, &size, &x_offset, &y_offset); err = WebPMuxGetTile(mux, i, &data, &size, &x_offset, &y_offset);
assert(err == WEBP_MUX_OK); assert(err == WEBP_MUX_OK);
@ -395,7 +382,7 @@ static int ReadImageData(const char* filename, int image_index,
} }
} else { } else {
fprintf(stderr, "Failed to extract image data from file %s. Error: %d\n", fprintf(stderr, "Failed to extract image data from file %s. Error: %d\n",
filename, err); filename, err);
ok = 0; ok = 0;
} }
WebPMuxDelete(mux); WebPMuxDelete(mux);
@ -404,7 +391,7 @@ static int ReadImageData(const char* filename, int image_index,
static int WriteData(const char* filename, void* data, uint32_t size) { static int WriteData(const char* filename, void* data, uint32_t size) {
int ok = 0; int ok = 0;
FILE* fout = strcmp(filename, "--") ? fopen(filename, "wb") : stdout; FILE* fout = strcmp(filename, "-") ? fopen(filename, "wb") : stdout;
if (!fout) { if (!fout) {
fprintf(stderr, "Error opening output WebP file %s!\n", filename); fprintf(stderr, "Error opening output WebP file %s!\n", filename);
return 0; return 0;
@ -472,29 +459,26 @@ static int ValidateCommandLine(int argc, const char* argv[],
*num_feature_args = 0; *num_feature_args = 0;
// Simple checks. // Simple checks.
if (CountOccurences(argv, argc, "-get") > 1) { if (CountOccurrences(argv, argc, "-get") > 1) {
ERROR_GOTO1("ERROR: Multiple '-get' arguments specified.\n", ErrValidate); ERROR_GOTO1("ERROR: Multiple '-get' arguments specified.\n", ErrValidate);
} }
if (CountOccurences(argv, argc, "-set") > 1) { if (CountOccurrences(argv, argc, "-set") > 1) {
ERROR_GOTO1("ERROR: Multiple '-set' arguments specified.\n", ErrValidate); ERROR_GOTO1("ERROR: Multiple '-set' arguments specified.\n", ErrValidate);
} }
if (CountOccurences(argv, argc, "-strip") > 1) { if (CountOccurrences(argv, argc, "-strip") > 1) {
ERROR_GOTO1("ERROR: Multiple '-strip' arguments specified.\n", ErrValidate); ERROR_GOTO1("ERROR: Multiple '-strip' arguments specified.\n", ErrValidate);
} }
if (CountOccurences(argv, argc, "-info") > 1) { if (CountOccurrences(argv, argc, "-info") > 1) {
ERROR_GOTO1("ERROR: Multiple '-info' arguments specified.\n", ErrValidate); ERROR_GOTO1("ERROR: Multiple '-info' arguments specified.\n", ErrValidate);
} }
if (CountOccurences(argv, argc, "-help") > 1) { if (CountOccurrences(argv, argc, "-o") > 1) {
ERROR_GOTO1("ERROR: Multiple '-help' arguments specified.\n", ErrValidate);
}
if (CountOccurences(argv, argc, "-o") > 1) {
ERROR_GOTO1("ERROR: Multiple output files specified.\n", ErrValidate); ERROR_GOTO1("ERROR: Multiple output files specified.\n", ErrValidate);
} }
// Compound checks. // Compound checks.
num_frame_args = CountOccurences(argv, argc, "-frame"); num_frame_args = CountOccurrences(argv, argc, "-frame");
num_tile_args = CountOccurences(argv, argc, "-tile"); num_tile_args = CountOccurrences(argv, argc, "-tile");
num_loop_args = CountOccurences(argv, argc, "-loop"); num_loop_args = CountOccurrences(argv, argc, "-loop");
if (num_loop_args > 1) { if (num_loop_args > 1) {
ERROR_GOTO1("ERROR: Multiple loop counts specified.\n", ErrValidate); ERROR_GOTO1("ERROR: Multiple loop counts specified.\n", ErrValidate);
@ -552,7 +536,7 @@ static int ParseCommandLine(int argc, const char* argv[],
while (i < argc) { while (i < argc) {
Feature* const feature = &config->feature_; Feature* const feature = &config->feature_;
FeatureArg* arg = &feature->args_[feature_arg_index]; FeatureArg* const arg = &feature->args_[feature_arg_index];
if (argv[i][0] == '-') { // One of the action types or output. if (argv[i][0] == '-') { // One of the action types or output.
if (!strcmp(argv[i], "-set")) { if (!strcmp(argv[i], "-set")) {
if (ACTION_IS_NIL) { if (ACTION_IS_NIL) {
@ -675,7 +659,7 @@ static int ParseCommandLine(int argc, const char* argv[],
arg->params_ = argv[i + 1]; arg->params_ = argv[i + 1];
++feature_arg_index; ++feature_arg_index;
i += 2; i += 2;
} else { // Assume input file. } else { // Assume input file.
if (config->input_ == NULL) { if (config->input_ == NULL) {
config->input_ = argv[i]; config->input_ = argv[i];
} else { } else {
@ -690,7 +674,7 @@ static int ParseCommandLine(int argc, const char* argv[],
return ok; return ok;
} }
// Additional Checks after config is filled. // Additional checks after config is filled.
static int ValidateConfig(WebPMuxConfig* config) { static int ValidateConfig(WebPMuxConfig* config) {
int ok = 1; int ok = 1;
Feature* const feature = &config->feature_; Feature* const feature = &config->feature_;
@ -751,12 +735,8 @@ static int InitializeConfig(int argc, const char* argv[],
} }
// Parse command-line. // Parse command-line.
if (!ParseCommandLine(argc, argv, *config)) { if (!ParseCommandLine(argc, argv, *config) ||
ERROR_GOTO1("Exiting due to command-line parsing error.\n", Err1); !ValidateConfig(*config)) {
}
// Additional Checks.
if (!ValidateConfig(*config)) {
ERROR_GOTO1("Exiting due to command-line parsing error.\n", Err1); ERROR_GOTO1("Exiting due to command-line parsing error.\n", Err1);
} }
@ -772,7 +752,8 @@ static int InitializeConfig(int argc, const char* argv[],
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Processing. // Processing.
static int GetFrameTile(WebPMux* mux, WebPMuxConfig* config, int isFrame) { static int GetFrameTile(const WebPMux* mux,
const WebPMuxConfig* config, int isFrame) {
const uint8_t* data = NULL; const uint8_t* data = NULL;
uint32_t size = 0; uint32_t size = 0;
uint32_t x_offset = 0; uint32_t x_offset = 0;
@ -819,7 +800,7 @@ static int GetFrameTile(WebPMux* mux, WebPMuxConfig* config, int isFrame) {
} }
// Read and process config. // Read and process config.
static int Process(WebPMuxConfig* config) { static int Process(const WebPMuxConfig* config) {
WebPMux* mux = NULL; WebPMux* mux = NULL;
const uint8_t* data = NULL; const uint8_t* data = NULL;
uint32_t size = 0; uint32_t size = 0;
@ -831,13 +812,13 @@ static int Process(WebPMuxConfig* config) {
long num; long num;
int index = 0; int index = 0;
int ok = 1; int ok = 1;
Feature* const feature = &config->feature_; const Feature* const feature = &config->feature_;
switch(config->action_type_) { switch (config->action_type_) {
case ACTION_GET: case ACTION_GET:
ok = ReadFile(config->input_, &mux); ok = ReadFile(config->input_, &mux);
if (!ok) goto Err2; if (!ok) goto Err2;
switch(feature->type_) { switch (feature->type_) {
case FEATURE_FRM: case FEATURE_FRM:
ok = GetFrameTile(mux, config, 1); ok = GetFrameTile(mux, config, 1);
break; break;
@ -869,7 +850,7 @@ static int Process(WebPMuxConfig* config) {
break; break;
case ACTION_SET: case ACTION_SET:
switch(feature->type_) { switch (feature->type_) {
case FEATURE_FRM: case FEATURE_FRM:
mux = WebPMuxNew(); mux = WebPMuxNew();
if (mux == NULL) { if (mux == NULL) {
@ -939,8 +920,7 @@ static int Process(WebPMuxConfig* config) {
case FEATURE_ICCP: case FEATURE_ICCP:
ok = ReadFile(config->input_, &mux); ok = ReadFile(config->input_, &mux);
if (!ok) goto Err2; if (!ok) goto Err2;
ok = ReadData(feature->args_[0].filename_, (void**)&data, ok = ReadData(feature->args_[0].filename_, (void**)&data, &size);
&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);
@ -952,8 +932,7 @@ static int Process(WebPMuxConfig* config) {
case FEATURE_XMP: case FEATURE_XMP:
ok = ReadFile(config->input_, &mux); ok = ReadFile(config->input_, &mux);
if (!ok) goto Err2; if (!ok) goto Err2;
ok = ReadData(feature->args_[0].filename_, (void**)&data, ok = ReadData(feature->args_[0].filename_, (void**)&data, &size);
&size);
if (!ok) goto Err2; if (!ok) goto Err2;
err = WebPMuxSetMetadata(mux, data, size, 1); err = WebPMuxSetMetadata(mux, data, size, 1);
free((void*)data); free((void*)data);
@ -972,7 +951,7 @@ static int Process(WebPMuxConfig* config) {
case ACTION_STRIP: case ACTION_STRIP:
ok = ReadFile(config->input_, &mux); ok = ReadFile(config->input_, &mux);
if (!ok) goto Err2; if (!ok) goto Err2;
switch(feature->type_) { switch (feature->type_) {
case FEATURE_ICCP: case FEATURE_ICCP:
err = WebPMuxDeleteColorProfile(mux); err = WebPMuxDeleteColorProfile(mux);
if (err != WEBP_MUX_OK) { if (err != WEBP_MUX_OK) {
@ -1027,7 +1006,3 @@ int main(int argc, const char* argv[]) {
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif