| 1 |
// Copyright 2017 Google Inc. All Rights Reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style license |
| 4 |
// that can be found in the COPYING file in the root of the source |
| 5 |
// tree. An additional intellectual property rights grant can be found |
| 6 |
// in the file PATENTS. All contributing project authors may |
| 7 |
// be found in the AUTHORS file in the root of the source tree. |
| 8 |
// ----------------------------------------------------------------------------- |
| 9 |
// |
| 10 |
// Decodes an animated WebP file and dumps the decoded frames as PNG or TIFF. |
| 11 |
// |
| 12 |
// Author: Skal ([email protected]) |
| 13 |
|
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> // for 'strcmp'. |
| 17 |
|
| 18 |
#include "./anim_util.h" |
| 19 |
#include "webp/decode.h" |
| 20 |
#include "../imageio/image_enc.h" |
| 21 |
#include "./unicode.h" |
| 22 |
|
| 23 |
#if defined(_MSC_VER) && _MSC_VER < 1900 |
| 24 |
#define snprintf _snprintf |
| 25 |
#endif |
| 26 |
|
| 27 |
static void Help(void) { |
| 28 |
printf("Usage: anim_dump [options] files...\n"); |
| 29 |
printf("\nOptions:\n"); |
| 30 |
printf(" -folder <string> .... dump folder (default: '.')\n"); |
| 31 |
printf(" -prefix <string> .... prefix for dumped frames " |
| 32 |
"(default: 'dump_')\n"); |
| 33 |
printf(" -tiff ............... save frames as TIFF\n"); |
| 34 |
printf(" -pam ................ save frames as PAM\n"); |
| 35 |
printf(" -h .................. this help\n"); |
| 36 |
printf(" -version ............ print version number and exit\n"); |
| 37 |
} |
| 38 |
|
| 39 |
// Returns EXIT_SUCCESS on success, EXIT_FAILURE on failure. |
| 40 |
int main(int argc, const char* argv[]) { |
| 41 |
int error = 0; |
| 42 |
const W_CHAR* dump_folder = TO_W_CHAR("."); |
| 43 |
const W_CHAR* prefix = TO_W_CHAR("dump_"); |
| 44 |
const W_CHAR* suffix = TO_W_CHAR("png"); |
| 45 |
WebPOutputFileFormat format = PNG; |
| 46 |
int c; |
| 47 |
|
| 48 |
INIT_WARGV(argc, argv); |
| 49 |
|
| 50 |
if (argc < 2) { |
| 51 |
Help(); |
| 52 |
FREE_WARGV_AND_RETURN(EXIT_FAILURE); |
| 53 |
} |
| 54 |
|
| 55 |
for (c = 1; !error && c < argc; ++c) { |
| 56 |
if (!strcmp(argv[c], "-folder")) { |
| 57 |
if (c + 1 == argc) { |
| 58 |
fprintf(stderr, "missing argument after option '%s'\n", argv[c]); |
| 59 |
error = 1; |
| 60 |
break; |
| 61 |
} |
| 62 |
dump_folder = GET_WARGV(argv, ++c); |
| 63 |
} else if (!strcmp(argv[c], "-prefix")) { |
| 64 |
if (c + 1 == argc) { |
| 65 |
fprintf(stderr, "missing argument after option '%s'\n", argv[c]); |
| 66 |
error = 1; |
| 67 |
break; |
| 68 |
} |
| 69 |
prefix = GET_WARGV(argv, ++c); |
| 70 |
} else if (!strcmp(argv[c], "-tiff")) { |
| 71 |
format = TIFF; |
| 72 |
suffix = TO_W_CHAR("tiff"); |
| 73 |
} else if (!strcmp(argv[c], "-pam")) { |
| 74 |
format = PAM; |
| 75 |
suffix = TO_W_CHAR("pam"); |
| 76 |
} else if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) { |
| 77 |
Help(); |
| 78 |
FREE_WARGV_AND_RETURN(EXIT_SUCCESS); |
| 79 |
} else if (!strcmp(argv[c], "-version")) { |
| 80 |
int dec_version, demux_version; |
| 81 |
GetAnimatedImageVersions(&dec_version, &demux_version); |
| 82 |
printf("WebP Decoder version: %d.%d.%d\nWebP Demux version: %d.%d.%d\n", |
| 83 |
(dec_version >> 16) & 0xff, (dec_version >> 8) & 0xff, |
| 84 |
(dec_version >> 0) & 0xff, |
| 85 |
(demux_version >> 16) & 0xff, (demux_version >> 8) & 0xff, |
| 86 |
(demux_version >> 0) & 0xff); |
| 87 |
FREE_WARGV_AND_RETURN(EXIT_SUCCESS); |
| 88 |
} else { |
| 89 |
uint32_t i; |
| 90 |
AnimatedImage image; |
| 91 |
const W_CHAR* const file = GET_WARGV(argv, c); |
| 92 |
memset(&image, 0, sizeof(image)); |
| 93 |
WPRINTF("Decoding file: %s as %s/%sxxxx.%s\n", |
| 94 |
file, dump_folder, prefix, suffix); |
| 95 |
if (!ReadAnimatedImage((const char*)file, &image, 0, NULL)) { |
| 96 |
WFPRINTF(stderr, "Error decoding file: %s\n Aborting.\n", file); |
| 97 |
error = 1; |
| 98 |
break; |
| 99 |
} |
| 100 |
for (i = 0; !error && i < image.num_frames; ++i) { |
| 101 |
W_CHAR out_file[1024]; |
| 102 |
WebPDecBuffer buffer; |
| 103 |
if (!WebPInitDecBuffer(&buffer)) { |
| 104 |
fprintf(stderr, "Cannot init dec buffer\n"); |
| 105 |
error = 1; |
| 106 |
continue; |
| 107 |
} |
| 108 |
buffer.colorspace = MODE_RGBA; |
| 109 |
buffer.is_external_memory = 1; |
| 110 |
buffer.width = image.canvas_width; |
| 111 |
buffer.height = image.canvas_height; |
| 112 |
buffer.u.RGBA.rgba = image.frames[i].rgba; |
| 113 |
buffer.u.RGBA.stride = buffer.width * sizeof(uint32_t); |
| 114 |
buffer.u.RGBA.size = buffer.u.RGBA.stride * buffer.height; |
| 115 |
WSNPRINTF(out_file, sizeof(out_file), "%s/%s%.4d.%s", |
| 116 |
dump_folder, prefix, i, suffix); |
| 117 |
if (!WebPSaveImage(&buffer, format, (const char*)out_file)) { |
| 118 |
WFPRINTF(stderr, "Error while saving image '%s'\n", out_file); |
| 119 |
error = 1; |
| 120 |
} |
| 121 |
WebPFreeDecBuffer(&buffer); |
| 122 |
} |
| 123 |
ClearAnimatedImage(&image); |
| 124 |
} |
| 125 |
} |
| 126 |
FREE_WARGV_AND_RETURN(error ? EXIT_FAILURE : EXIT_SUCCESS); |
| 127 |
} |