From 7eaee9f1ac6b04fdd66af924d62d2d6e02f25af0 Mon Sep 17 00:00:00 2001 From: James Zern Date: Fri, 11 Jan 2013 12:25:36 -0800 Subject: [PATCH] cwebp: add -metadata option currently has no effect except to disable metadata extraction from the input when the value is 'none'. Change-Id: Ic50d4c9d634cc1f6b72ae4e130e99736c85a6477 --- README | 4 +++ examples/cwebp.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++- man/cwebp.1 | 10 +++++++- 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/README b/README index f9ddb1ca..25c040a9 100644 --- a/README +++ b/README @@ -171,6 +171,10 @@ options: -hint ......... Specify image characteristics hint. One of: photo, picture or graph + -metadata ..... comma separated list of metadata to + copy from the input to the output if present. + Valid values: all, none (default), exif, iccp, xmp + -short ................. condense printed message -quiet ................. don't print anything. -version ............... print version number and exit. diff --git a/examples/cwebp.c b/examples/cwebp.c index e75100cf..e33215d7 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -545,6 +545,16 @@ static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) { return 1; } +// ----------------------------------------------------------------------------- +// Metadata writing. + +enum { + METADATA_EXIF = (1 << 0), + METADATA_ICCP = (1 << 1), + METADATA_XMP = (1 << 2), + METADATA_ALL = METADATA_EXIF | METADATA_ICCP | METADATA_XMP +}; + //------------------------------------------------------------------------------ static int ProgressReport(int percent, const WebPPicture* const picture) { @@ -617,6 +627,13 @@ static void HelpLong(void) { printf(" -hint ......... Specify image characteristics hint.\n"); printf(" One of: photo, picture or graph\n"); + printf("\n"); + printf(" -metadata ..... comma separated list of metadata to\n"); + printf(" "); + printf("copy from the input to the output if present.\n"); + printf(" " + "Valid values: all, none (default), exif, iccp, xmp\n"); + printf("\n"); printf(" -short ................. condense printed message\n"); printf(" -quiet ................. don't print anything.\n"); @@ -669,6 +686,7 @@ int main(int argc, const char *argv[]) { int crop = 0, crop_x = 0, crop_y = 0, crop_w = 0, crop_h = 0; int resize_w = 0, resize_h = 0; int show_progress = 0; + int keep_metadata = 0; WebPPicture picture; int print_distortion = -1; // -1=off, 0=PSNR, 1=SSIM, 2=LSIM WebPPicture original_picture; // when PSNR or SSIM is requested @@ -832,6 +850,50 @@ int main(int argc, const char *argv[]) { fprintf(stderr, "Error! Could initialize configuration with preset.\n"); goto Error; } + } else if (!strcmp(argv[c], "-metadata") && c < argc - 1) { + static const struct { + const char* option; + int flag; + } kTokens[] = { + { "all", METADATA_ALL }, + { "none", 0 }, + { "exif", METADATA_EXIF }, + { "iccp", METADATA_ICCP }, + { "xmp", METADATA_XMP }, + }; + const size_t kNumTokens = sizeof(kTokens) / sizeof(kTokens[0]); + const char* start = argv[++c]; + const char* const end = start + strlen(start); + + while (start < end) { + size_t i; + const char* token = strchr(start, ','); + if (token == NULL) token = end; + + for (i = 0; i < kNumTokens; ++i) { + if ((size_t)(token - start) == strlen(kTokens[i].option) && + !strncmp(start, kTokens[i].option, strlen(kTokens[i].option))) { + if (kTokens[i].flag != 0) { + keep_metadata |= kTokens[i].flag; + } else { + keep_metadata = 0; + } + break; + } + } + if (i == kNumTokens) { + fprintf(stderr, "Error! Unknown metadata type '%.*s'\n", + (int)(token - start), start); + HelpLong(); + return -1; + } + start = token + 1; + } + if (keep_metadata != 0) { + // TODO(jzern): remove when -metadata is supported on all platforms. + fprintf(stderr, "Warning: -metadata is currently unsupported on this" + " platform. Ignoring this option!\n"); + } } else if (!strcmp(argv[c], "-v")) { verbose = 1; } else if (argv[c][0] == '-') { @@ -870,7 +932,8 @@ int main(int argc, const char *argv[]) { if (verbose) { StopwatchReadAndReset(&stop_watch); } - if (!ReadPicture(in_file, &picture, keep_alpha, &metadata)) { + if (!ReadPicture(in_file, &picture, keep_alpha, + (keep_metadata == 0) ? NULL : &metadata)) { fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file); goto Error; } diff --git a/man/cwebp.1 b/man/cwebp.1 index 536689e3..0463272d 100644 --- a/man/cwebp.1 +++ b/man/cwebp.1 @@ -1,5 +1,5 @@ .\" Hey, EMACS: -*- nroff -*- -.TH CWEBP 1 "November 15, 2012" +.TH CWEBP 1 "January 11, 2013" .SH NAME cwebp \- compress an image file to a WebP file .SH SYNOPSIS @@ -172,6 +172,14 @@ Encode the image without any loss. Specify the hint about input image type. Possible values are: \fBphoto\fP, \fBpicture\fP or \fBgraph\fP. .TP +.B \-metadata string +A comma separated list of metadata to copy from the input to the output if +present. +Valid values: \fBall\fP, \fBnone\fP, \fBexif\fP, \fBiccp\fP, \fBxmp\fP. +The default is \fBnone\fP. + +Note: each input format may not support all combinations. +.TP .B \-noasm Disable all assembly optimizations. .TP