From e12f874eea8b434560cd65e36495e1f5f0615338 Mon Sep 17 00:00:00 2001 From: skal Date: Wed, 12 Mar 2014 19:48:00 +0100 Subject: [PATCH] allow 'cwebp -o -' to emit output to stdout Change-Id: I423d25898e1ba317ccbf456bb28ce45663a3b3d2 --- examples/cwebp.c | 24 ++++++++++++------------ man/cwebp.1 | 6 ++++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/examples/cwebp.c b/examples/cwebp.c index 93d051e7..b4ed68f7 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -310,18 +310,18 @@ static void PrintMapInfo(const WebPPicture* const pic) { for (x = 0; x < mb_w; ++x) { const int c = pic->extra_info[x + y * mb_w]; if (type == 1) { // intra4/intra16 - printf("%c", "+."[c]); + fprintf(stderr, "%c", "+."[c]); } else if (type == 2) { // segments - printf("%c", ".-*X"[c]); + fprintf(stderr, "%c", ".-*X"[c]); } else if (type == 3) { // quantizers - printf("%.2d ", c); + fprintf(stderr, "%.2d ", c); } else if (type == 6 || type == 7) { - printf("%3d ", c); + fprintf(stderr, "%3d ", c); } else { - printf("0x%.2x ", c); + fprintf(stderr, "0x%.2x ", c); } } - printf("\n"); + fprintf(stderr, "\n"); } } } @@ -534,9 +534,8 @@ static int WriteWebPWithMetadata(FILE* const out, //------------------------------------------------------------------------------ static int ProgressReport(int percent, const WebPPicture* const picture) { - printf("[%s]: %3d %% \r", - (char*)picture->user_data, percent); - fflush(stdout); + fprintf(stderr, "[%s]: %3d %% \r", + (char*)picture->user_data, percent); return 1; // all ok } @@ -975,8 +974,9 @@ int main(int argc, const char *argv[]) { } // Open the output - if (out_file) { - out = fopen(out_file, "wb"); + if (out_file != NULL) { + const int use_stdout = !strcmp(out_file, "-"); + out = use_stdout ? stdout : fopen(out_file, "wb"); if (out == NULL) { fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file); goto Error; @@ -1126,7 +1126,7 @@ int main(int argc, const char *argv[]) { MetadataFree(&metadata); WebPPictureFree(&picture); WebPPictureFree(&original_picture); - if (out != NULL) { + if (out != NULL && out != stdout) { fclose(out); } diff --git a/man/cwebp.1 b/man/cwebp.1 index 1dd27f66..02195260 100644 --- a/man/cwebp.1 +++ b/man/cwebp.1 @@ -19,6 +19,12 @@ The basic options are: .BI \-o " string Specify the name of the output WebP file. If omitted, \fBcwebp\fP will perform compression but only report statistics. +Using "\-" as output name will direct output to 'stdout'. +.TP +.BI \-\- " string +Explicitly specify the input file. This option is useful if the input +file starts with an '\-' for instance. This option must appear \fBlast\fP. +Any other options afterward will be ignored. .TP .B \-h, \-help A short usage summary.