libwebp: Unicode command tools on Windows

Define macros in examples/unicode.h to use Unicode argv
on Windows. Keep char everywhere on Unix since it handles
UTF-8 without any change.

Impact:
 - All fopen () and SHCreateStreamOnFile(),
 - All fprintf() printing file paths,
 - All strcmp() used with "-",
 - File path parsing,
 - Gif reading.

Concerned executables from examples/ and extras/:
  anim_diff, anim_dump, vwebp, vwebp_sdl,
  cwebp, dwebp, gif2webp, img2webp,
  webpmux, webpinfo, webp_quality, get_disto

When compiled on Windows with Unicode enabled, webpmux and
img2webp will not work when used with an argument file and
will print "Reading arguments from a file is a feature
unavailable with Unicode binaries."

BUG=webp:398

Change-Id: Ic55d222a3ce1a715f9c4cce57ecbe2705d5ce317
This commit is contained in:
Yannis Guyon
2018-10-16 11:03:18 +02:00
parent 5030e90278
commit 9cf9841b5e
23 changed files with 485 additions and 214 deletions

View File

@ -24,6 +24,7 @@
#include "../imageio/image_dec.h"
#include "../imageio/imageio_util.h"
#include "./stopwatch.h"
#include "./unicode.h"
#include "webp/encode.h"
#ifndef WEBP_DLL
@ -88,7 +89,8 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic,
}
}
if (!ok) {
fprintf(stderr, "Error! Could not process file %s\n", filename);
WFPRINTF(stderr, "Error! Could not process file %s\n",
(const W_CHAR*)filename);
}
free((void*)data);
return ok;
@ -114,7 +116,8 @@ static int ReadPicture(const char* const filename, WebPPicture* const pic,
}
End:
if (!ok) {
fprintf(stderr, "Error! Could not process file %s\n", filename);
WFPRINTF(stderr, "Error! Could not process file %s\n",
(const W_CHAR*)filename);
}
free((void*)data);
return ok;
@ -185,7 +188,7 @@ static void PrintExtraInfoLossless(const WebPPicture* const pic,
if (short_output) {
fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]);
} else {
fprintf(stderr, "File: %s\n", file_name);
WFPRINTF(stderr, "File: %s\n", (const W_CHAR*)file_name);
fprintf(stderr, "Dimension: %d x %d\n", pic->width, pic->height);
fprintf(stderr, "Output: %d bytes (%.2f bpp)\n", stats->coded_size,
8.f * stats->coded_size / pic->width / pic->height);
@ -204,7 +207,7 @@ static void PrintExtraInfoLossy(const WebPPicture* const pic, int short_output,
const int num_i16 = stats->block_count[1];
const int num_skip = stats->block_count[2];
const int total = num_i4 + num_i16;
fprintf(stderr, "File: %s\n", file_name);
WFPRINTF(stderr, "File: %s\n", (const W_CHAR*)file_name);
fprintf(stderr, "Dimension: %d x %d%s\n",
pic->width, pic->height,
stats->alpha_data_size ? " (with alpha)" : "");
@ -309,7 +312,7 @@ static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) {
const int alpha_height =
WebPPictureHasTransparency(picture) ? picture->height : 0;
const int height = picture->height + uv_height + alpha_height;
FILE* const f = fopen(PGM_name, "wb");
FILE* const f = WFOPEN(PGM_name, "wb");
if (f == NULL) return 0;
fprintf(f, "P5\n%d %d\n255\n", stride, height);
for (y = 0; y < picture->height; ++y) {
@ -663,32 +666,34 @@ int main(int argc, const char *argv[]) {
Metadata metadata;
Stopwatch stop_watch;
INIT_WARGV(argc, argv);
MetadataInit(&metadata);
WebPMemoryWriterInit(&memory_writer);
if (!WebPPictureInit(&picture) ||
!WebPPictureInit(&original_picture) ||
!WebPConfigInit(&config)) {
fprintf(stderr, "Error! Version mismatch!\n");
return -1;
FREE_WARGV_AND_RETURN(-1);
}
if (argc == 1) {
HelpShort();
return 0;
FREE_WARGV_AND_RETURN(0);
}
for (c = 1; c < argc; ++c) {
int parse_error = 0;
if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
HelpShort();
return 0;
FREE_WARGV_AND_RETURN(0);
} else if (!strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
HelpLong();
return 0;
FREE_WARGV_AND_RETURN(0);
} else if (!strcmp(argv[c], "-o") && c < argc - 1) {
out_file = argv[++c];
out_file = (const char*)GET_WARGV(argv, ++c);
} else if (!strcmp(argv[c], "-d") && c < argc - 1) {
dump_file = argv[++c];
dump_file = (const char*)GET_WARGV(argv, ++c);
config.show_compressed = 1;
} else if (!strcmp(argv[c], "-print_psnr")) {
config.show_compressed = 1;
@ -816,7 +821,7 @@ int main(int argc, const char *argv[]) {
const int version = WebPGetEncoderVersion();
printf("%d.%d.%d\n",
(version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
return 0;
FREE_WARGV_AND_RETURN(0);
} else if (!strcmp(argv[c], "-progress")) {
show_progress = 1;
} else if (!strcmp(argv[c], "-quiet")) {
@ -878,8 +883,7 @@ int main(int argc, const char *argv[]) {
if (i == kNumTokens) {
fprintf(stderr, "Error! Unknown metadata type '%.*s'\n",
(int)(token - start), start);
HelpLong();
return -1;
FREE_WARGV_AND_RETURN(-1);
}
start = token + 1;
}
@ -893,19 +897,19 @@ int main(int argc, const char *argv[]) {
} else if (!strcmp(argv[c], "-v")) {
verbose = 1;
} else if (!strcmp(argv[c], "--")) {
if (c < argc - 1) in_file = argv[++c];
if (c < argc - 1) in_file = (const char*)GET_WARGV(argv, ++c);
break;
} else if (argv[c][0] == '-') {
fprintf(stderr, "Error! Unknown option '%s'\n", argv[c]);
HelpLong();
return -1;
FREE_WARGV_AND_RETURN(-1);
} else {
in_file = argv[c];
in_file = (const char*)GET_WARGV(argv, c);
}
if (parse_error) {
HelpLong();
return -1;
FREE_WARGV_AND_RETURN(-1);
}
}
if (in_file == NULL) {
@ -955,7 +959,8 @@ int main(int argc, const char *argv[]) {
}
if (!ReadPicture(in_file, &picture, keep_alpha,
(keep_metadata == 0) ? NULL : &metadata)) {
fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
WFPRINTF(stderr, "Error! Cannot read input picture file '%s'\n",
(const W_CHAR*)in_file);
goto Error;
}
picture.progress_hook = (show_progress && !quiet) ? ProgressReport : NULL;
@ -971,14 +976,15 @@ int main(int argc, const char *argv[]) {
// Open the output
if (out_file != NULL) {
const int use_stdout = !strcmp(out_file, "-");
out = use_stdout ? ImgIoUtilSetBinaryMode(stdout) : fopen(out_file, "wb");
const int use_stdout = !WSTRCMP(out_file, "-");
out = use_stdout ? ImgIoUtilSetBinaryMode(stdout) : WFOPEN(out_file, "wb");
if (out == NULL) {
fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
WFPRINTF(stderr, "Error! Cannot open output file '%s'\n",
(const W_CHAR*)out_file);
goto Error;
} else {
if (!short_output && !quiet) {
fprintf(stderr, "Saving file '%s'\n", out_file);
WFPRINTF(stderr, "Saving file '%s'\n", (const W_CHAR*)out_file);
}
}
if (keep_metadata == 0) {
@ -1093,7 +1099,8 @@ int main(int argc, const char *argv[]) {
fprintf(stderr, "Warning: can't dump file (-d option) "
"in lossless mode.\n");
} else if (!DumpPicture(&picture, dump_file)) {
fprintf(stderr, "Warning, couldn't dump picture %s\n", dump_file);
WFPRINTF(stderr, "Warning, couldn't dump picture %s\n",
(const W_CHAR*)dump_file);
}
}
@ -1169,7 +1176,7 @@ int main(int argc, const char *argv[]) {
fclose(out);
}
return return_value;
FREE_WARGV_AND_RETURN(return_value);
}
//------------------------------------------------------------------------------