James Zern | ad1e163 | 2012-01-06 14:49:06 -0800 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 2 | // |
| 3 | // This code is licensed under the same terms as WebM: |
| 4 | // Software License Agreement: http://www.webmproject.org/license/software/ |
| 5 | // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
| 6 | // ----------------------------------------------------------------------------- |
| 7 | // |
| 8 | // Simple command-line to create a WebP container file and to extract or strip |
| 9 | // relevant data from the container file. |
| 10 | // |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 11 | // Compile with: gcc -o webpmux webpmux.c -lwebpmux -lwebp |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 12 | // |
| 13 | // |
| 14 | // Authors: Vikas (vikaas.arora@gmail.com), |
| 15 | // Urvang (urvang@google.com) |
| 16 | |
| 17 | /* Usage examples: |
| 18 | |
| 19 | Create container WebP file: |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 20 | webpmux -tile tile_1.webp +0+0 \ |
| 21 | -tile tile_2.webp +960+0 \ |
| 22 | -tile tile_3.webp +0+576 \ |
| 23 | -tile tile_4.webp +960+576 \ |
| 24 | -o out_tile_container.webp |
| 25 | |
| 26 | webpmux -frame anim_1.webp +0+0+0 \ |
| 27 | -frame anim_2.webp +25+25+100 \ |
| 28 | -frame anim_3.webp +50+50+100 \ |
| 29 | -frame anim_4.webp +0+0+100 \ |
| 30 | -loop 10 \ |
| 31 | -o out_animation_container.webp |
| 32 | |
| 33 | webpmux -set icc image_profile.icc in.webp -o out_icc_container.webp |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 34 | webpmux -set xmp image_metadata.xmp in.webp -o out_xmp_container.webp |
| 35 | |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 36 | Extract relevant data from WebP container file: |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 37 | webpmux -get tile n in.webp -o out_tile.webp |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 38 | webpmux -get frame n in.webp -o out_frame.webp |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 39 | webpmux -get icc in.webp -o image_profile.icc |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 40 | webpmux -get xmp in.webp -o image_metadata.xmp |
| 41 | |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 42 | Strip data from WebP Container file: |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 43 | webpmux -strip icc in.webp -o out.webp |
| 44 | webpmux -strip xmp in.webp -o out.webp |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 45 | |
| 46 | Misc: |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 47 | webpmux -info in.webp |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 48 | webpmux [ -h | -help ] |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | #include <assert.h> |
| 52 | #include <stdio.h> |
| 53 | #include <stdlib.h> |
| 54 | #include <string.h> |
| 55 | #include "webp/mux.h" |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 56 | #include "./example_util.h" |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 57 | |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 58 | //------------------------------------------------------------------------------ |
| 59 | // Config object to parse command-line arguments. |
| 60 | |
| 61 | typedef enum { |
| 62 | NIL_ACTION = 0, |
| 63 | ACTION_GET, |
| 64 | ACTION_SET, |
| 65 | ACTION_STRIP, |
| 66 | ACTION_INFO, |
| 67 | ACTION_HELP |
| 68 | } ActionType; |
| 69 | |
| 70 | typedef enum { |
| 71 | NIL_SUBTYPE = 0, |
| 72 | SUBTYPE_FRM, |
| 73 | SUBTYPE_LOOP |
| 74 | } FeatureSubType; |
| 75 | |
| 76 | typedef struct { |
| 77 | FeatureSubType subtype_; |
| 78 | const char* filename_; |
| 79 | const char* params_; |
| 80 | } FeatureArg; |
| 81 | |
| 82 | typedef enum { |
| 83 | NIL_FEATURE = 0, |
| 84 | FEATURE_XMP, |
| 85 | FEATURE_ICCP, |
| 86 | FEATURE_FRM, |
| 87 | FEATURE_TILE |
| 88 | } FeatureType; |
| 89 | |
| 90 | typedef struct { |
| 91 | FeatureType type_; |
| 92 | FeatureArg* args_; |
| 93 | int arg_count_; |
| 94 | } Feature; |
| 95 | |
| 96 | typedef struct { |
| 97 | ActionType action_type_; |
| 98 | const char* input_; |
| 99 | const char* output_; |
| 100 | Feature feature_; |
| 101 | } WebPMuxConfig; |
| 102 | |
| 103 | //------------------------------------------------------------------------------ |
| 104 | // Helper functions. |
| 105 | |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 106 | static int CountOccurrences(const char* arglist[], int list_length, |
| 107 | const char* arg) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 108 | int i; |
| 109 | int num_occurences = 0; |
| 110 | |
| 111 | for (i = 0; i < list_length; ++i) { |
| 112 | if (!strcmp(arglist[i], arg)) { |
| 113 | ++num_occurences; |
| 114 | } |
| 115 | } |
| 116 | return num_occurences; |
| 117 | } |
| 118 | |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 119 | static const char* const kErrorMessages[] = { |
Urvang Joshi | a4b9b1c | 2012-07-06 17:33:59 +0530 | [diff] [blame] | 120 | "WEBP_MUX_NOT_FOUND", "WEBP_MUX_INVALID_ARGUMENT", "WEBP_MUX_BAD_DATA", |
| 121 | "WEBP_MUX_MEMORY_ERROR", "WEBP_MUX_NOT_ENOUGH_DATA" |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static const char* ErrorString(WebPMuxError err) { |
Urvang Joshi | a4b9b1c | 2012-07-06 17:33:59 +0530 | [diff] [blame] | 125 | assert(err <= WEBP_MUX_NOT_FOUND && err >= WEBP_MUX_NOT_ENOUGH_DATA); |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 126 | return kErrorMessages[-err]; |
| 127 | } |
| 128 | |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 129 | static int IsNotCompatible(int count1, int count2) { |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 130 | return (count1 > 0) != (count2 > 0); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 131 | } |
| 132 | |
James Zern | a0b2736 | 2012-01-27 17:39:47 -0800 | [diff] [blame] | 133 | #define RETURN_IF_ERROR(ERR_MSG) \ |
| 134 | if (err != WEBP_MUX_OK) { \ |
| 135 | fprintf(stderr, ERR_MSG); \ |
| 136 | return err; \ |
| 137 | } |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 138 | |
James Zern | a0b2736 | 2012-01-27 17:39:47 -0800 | [diff] [blame] | 139 | #define RETURN_IF_ERROR2(ERR_MSG, FORMAT_STR) \ |
| 140 | if (err != WEBP_MUX_OK) { \ |
| 141 | fprintf(stderr, ERR_MSG, FORMAT_STR); \ |
| 142 | return err; \ |
| 143 | } |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 144 | |
James Zern | a0b2736 | 2012-01-27 17:39:47 -0800 | [diff] [blame] | 145 | #define ERROR_GOTO1(ERR_MSG, LABEL) \ |
| 146 | do { \ |
| 147 | fprintf(stderr, ERR_MSG); \ |
| 148 | ok = 0; \ |
| 149 | goto LABEL; \ |
| 150 | } while (0) |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 151 | |
James Zern | a0b2736 | 2012-01-27 17:39:47 -0800 | [diff] [blame] | 152 | #define ERROR_GOTO2(ERR_MSG, FORMAT_STR, LABEL) \ |
| 153 | do { \ |
| 154 | fprintf(stderr, ERR_MSG, FORMAT_STR); \ |
| 155 | ok = 0; \ |
| 156 | goto LABEL; \ |
| 157 | } while (0) |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 158 | |
James Zern | a0b2736 | 2012-01-27 17:39:47 -0800 | [diff] [blame] | 159 | #define ERROR_GOTO3(ERR_MSG, FORMAT_STR1, FORMAT_STR2, LABEL) \ |
| 160 | do { \ |
| 161 | fprintf(stderr, ERR_MSG, FORMAT_STR1, FORMAT_STR2); \ |
| 162 | ok = 0; \ |
| 163 | goto LABEL; \ |
| 164 | } while (0) |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 165 | |
| 166 | static WebPMuxError DisplayInfo(const WebPMux* mux) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 167 | uint32_t flag; |
| 168 | |
| 169 | WebPMuxError err = WebPMuxGetFeatures(mux, &flag); |
| 170 | RETURN_IF_ERROR("Failed to retrieve features\n"); |
| 171 | |
| 172 | if (flag == 0) { |
| 173 | fprintf(stderr, "No features present.\n"); |
| 174 | return err; |
| 175 | } |
| 176 | |
| 177 | // Print the features present. |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 178 | printf("Features present:"); |
| 179 | if (flag & ANIMATION_FLAG) printf(" animation"); |
| 180 | if (flag & TILE_FLAG) printf(" tiling"); |
| 181 | if (flag & ICCP_FLAG) printf(" icc profile"); |
| 182 | if (flag & META_FLAG) printf(" metadata"); |
| 183 | if (flag & ALPHA_FLAG) printf(" transparency"); |
| 184 | printf("\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 185 | |
| 186 | if (flag & ANIMATION_FLAG) { |
James Zern | eec4b87 | 2012-01-07 12:44:01 -0800 | [diff] [blame] | 187 | int nFrames; |
Urvang Joshi | 6808e69 | 2012-07-06 11:35:36 +0530 | [diff] [blame] | 188 | int loop_count; |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 189 | err = WebPMuxGetLoopCount(mux, &loop_count); |
| 190 | RETURN_IF_ERROR("Failed to retrieve loop count\n"); |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 191 | printf("Loop Count : %d\n", loop_count); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 192 | |
Urvang Joshi | fbdcb7e | 2012-06-11 12:24:45 +0530 | [diff] [blame] | 193 | err = WebPMuxNumChunks(mux, WEBP_CHUNK_FRAME, &nFrames); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 194 | RETURN_IF_ERROR("Failed to retrieve number of frames\n"); |
| 195 | |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 196 | printf("Number of frames: %d\n", nFrames); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 197 | if (nFrames > 0) { |
| 198 | int i; |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 199 | printf("No.: x_offset y_offset duration image_size"); |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 200 | printf("\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 201 | for (i = 1; i <= nFrames; i++) { |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 202 | WebPMuxFrameInfo frame; |
| 203 | err = WebPMuxGetFrame(mux, i, &frame); |
Urvang Joshi | cdf97aa | 2011-12-01 16:11:51 +0530 | [diff] [blame] | 204 | RETURN_IF_ERROR2("Failed to retrieve frame#%d\n", i); |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 205 | printf("%3d: %8d %8d %8d %10zu", i, frame.x_offset_, frame.y_offset_, |
| 206 | frame.duration_, frame.bitstream_.size_); |
| 207 | WebPDataClear(&frame.bitstream_); |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 208 | printf("\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (flag & TILE_FLAG) { |
James Zern | eec4b87 | 2012-01-07 12:44:01 -0800 | [diff] [blame] | 214 | int nTiles; |
Urvang Joshi | fbdcb7e | 2012-06-11 12:24:45 +0530 | [diff] [blame] | 215 | err = WebPMuxNumChunks(mux, WEBP_CHUNK_TILE, &nTiles); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 216 | RETURN_IF_ERROR("Failed to retrieve number of tiles\n"); |
| 217 | |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 218 | printf("Number of tiles: %d\n", nTiles); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 219 | if (nTiles > 0) { |
| 220 | int i; |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 221 | printf("No.: x_offset y_offset image_size"); |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 222 | printf("\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 223 | for (i = 1; i <= nTiles; i++) { |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 224 | WebPMuxFrameInfo tile; |
| 225 | err = WebPMuxGetTile(mux, i, &tile); |
Urvang Joshi | cdf97aa | 2011-12-01 16:11:51 +0530 | [diff] [blame] | 226 | RETURN_IF_ERROR2("Failed to retrieve tile#%d\n", i); |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 227 | printf("%3d: %8d %8d %10zu", |
| 228 | i, tile.x_offset_, tile.y_offset_, tile.bitstream_.size_); |
| 229 | WebPDataClear(&tile.bitstream_); |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 230 | printf("\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (flag & ICCP_FLAG) { |
James Zern | eec4b87 | 2012-01-07 12:44:01 -0800 | [diff] [blame] | 236 | WebPData icc_profile; |
Urvang Joshi | 1c04a0d | 2012-08-23 15:28:20 +0530 | [diff] [blame] | 237 | err = WebPMuxGetChunk(mux, "ICCP", &icc_profile); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 238 | RETURN_IF_ERROR("Failed to retrieve the color profile\n"); |
James Zern | 95667b8 | 2012-04-18 17:13:34 -0700 | [diff] [blame] | 239 | printf("Size of the color profile data: %zu\n", icc_profile.size_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | if (flag & META_FLAG) { |
James Zern | eec4b87 | 2012-01-07 12:44:01 -0800 | [diff] [blame] | 243 | WebPData metadata; |
Urvang Joshi | 1c04a0d | 2012-08-23 15:28:20 +0530 | [diff] [blame] | 244 | err = WebPMuxGetChunk(mux, "META", &metadata); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 245 | RETURN_IF_ERROR("Failed to retrieve the XMP metadata\n"); |
James Zern | 95667b8 | 2012-04-18 17:13:34 -0700 | [diff] [blame] | 246 | printf("Size of the XMP metadata: %zu\n", metadata.size_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 247 | } |
| 248 | |
Urvang Joshi | cdf97aa | 2011-12-01 16:11:51 +0530 | [diff] [blame] | 249 | if ((flag & ALPHA_FLAG) && !(flag & (ANIMATION_FLAG | TILE_FLAG))) { |
Urvang Joshi | b74ed6e | 2012-06-20 10:59:40 -0700 | [diff] [blame] | 250 | WebPData bitstream; |
| 251 | err = WebPMuxGetImage(mux, &bitstream); |
Urvang Joshi | cdf97aa | 2011-12-01 16:11:51 +0530 | [diff] [blame] | 252 | RETURN_IF_ERROR("Failed to retrieve the image\n"); |
Urvang Joshi | b74ed6e | 2012-06-20 10:59:40 -0700 | [diff] [blame] | 253 | printf("Size of the image (with alpha): %zu\n", bitstream.size_); |
Urvang Joshi | cdf97aa | 2011-12-01 16:11:51 +0530 | [diff] [blame] | 254 | } |
| 255 | |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 256 | return WEBP_MUX_OK; |
| 257 | } |
| 258 | |
Pascal Massimino | abd030b | 2011-11-01 06:24:34 -0700 | [diff] [blame] | 259 | static void PrintHelp(void) { |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 260 | printf("Usage: webpmux -get GET_OPTIONS INPUT -o OUTPUT\n"); |
| 261 | printf(" webpmux -set SET_OPTIONS INPUT -o OUTPUT\n"); |
| 262 | printf(" webpmux -strip STRIP_OPTIONS INPUT -o OUTPUT\n"); |
| 263 | printf(" webpmux -tile TILE_OPTIONS [-tile...] -o OUTPUT\n"); |
| 264 | printf(" webpmux -frame FRAME_OPTIONS [-frame...]"); |
| 265 | printf(" -loop LOOP_COUNT -o OUTPUT\n"); |
| 266 | printf(" webpmux -info INPUT\n"); |
| 267 | printf(" webpmux [-h|-help]\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 268 | |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 269 | printf("\n"); |
| 270 | printf("GET_OPTIONS:\n"); |
| 271 | printf(" Extract relevant data.\n"); |
| 272 | printf(" icc Get ICCP Color profile.\n"); |
| 273 | printf(" xmp Get XMP metadata.\n"); |
| 274 | printf(" tile n Get nth tile.\n"); |
| 275 | printf(" frame n Get nth frame.\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 276 | |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 277 | printf("\n"); |
| 278 | printf("SET_OPTIONS:\n"); |
| 279 | printf(" Set color profile/metadata.\n"); |
Urvang Joshi | ddfe871 | 2012-08-23 15:45:39 +0530 | [diff] [blame^] | 280 | printf(" icc file.icc Set ICC Color profile.\n"); |
| 281 | printf(" xmp file.xmp Set XMP metadata.\n"); |
| 282 | printf(" where: 'file.icc' contains the color profile to be set,\n"); |
| 283 | printf(" 'file.xmp' contains the metadata to be set\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 284 | |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 285 | printf("\n"); |
| 286 | printf("STRIP_OPTIONS:\n"); |
| 287 | printf(" Strip color profile/metadata.\n"); |
| 288 | printf(" icc Strip ICCP color profile.\n"); |
| 289 | printf(" xmp Strip XMP metadata.\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 290 | |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 291 | printf("\n"); |
| 292 | printf("TILE_OPTIONS(i):\n"); |
| 293 | printf(" Create tiled image.\n"); |
| 294 | printf(" file_i +xi+yi\n"); |
| 295 | printf(" where: 'file_i' is the i'th tile (webp format),\n"); |
| 296 | printf(" 'xi','yi' specify the image offset for this tile.\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 297 | |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 298 | printf("\n"); |
| 299 | printf("FRAME_OPTIONS(i):\n"); |
| 300 | printf(" Create animation.\n"); |
| 301 | printf(" file_i +xi+yi+di\n"); |
| 302 | printf(" where: 'file_i' is the i'th animation frame (webp format),\n"); |
| 303 | printf(" 'xi','yi' specify the image offset for this frame.\n"); |
| 304 | printf(" 'di' is the pause duration before next frame.\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 305 | |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 306 | printf("\nINPUT & OUTPUT are in webp format.\n"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 307 | } |
| 308 | |
Urvang Joshi | 4fc4a47 | 2012-06-05 14:20:45 +0530 | [diff] [blame] | 309 | static int ReadFileToWebPData(const char* const filename, |
| 310 | WebPData* const webp_data) { |
| 311 | const uint8_t* data; |
| 312 | size_t size; |
| 313 | if (!ExUtilReadFile(filename, &data, &size)) return 0; |
| 314 | webp_data->bytes_ = data; |
| 315 | webp_data->size_ = size; |
| 316 | return 1; |
| 317 | } |
| 318 | |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 319 | static int CreateMux(const char* const filename, WebPMux** mux) { |
Urvang Joshi | 4fc4a47 | 2012-06-05 14:20:45 +0530 | [diff] [blame] | 320 | WebPData bitstream; |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 321 | assert(mux != NULL); |
Urvang Joshi | 4fc4a47 | 2012-06-05 14:20:45 +0530 | [diff] [blame] | 322 | if (!ReadFileToWebPData(filename, &bitstream)) return 0; |
Urvang Joshi | 6d5c797 | 2012-06-07 13:45:06 +0530 | [diff] [blame] | 323 | *mux = WebPMuxCreate(&bitstream, 1); |
Urvang Joshi | 4fc4a47 | 2012-06-05 14:20:45 +0530 | [diff] [blame] | 324 | free((void*)bitstream.bytes_); |
Urvang Joshi | 6d5c797 | 2012-06-07 13:45:06 +0530 | [diff] [blame] | 325 | if (*mux != NULL) return 1; |
| 326 | fprintf(stderr, "Failed to create mux object from file %s.\n", filename); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
James Zern | 0f7820e | 2012-01-24 14:08:27 -0800 | [diff] [blame] | 330 | static int WriteData(const char* filename, const WebPData* const webpdata) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 331 | int ok = 0; |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 332 | FILE* fout = strcmp(filename, "-") ? fopen(filename, "wb") : stdout; |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 333 | if (!fout) { |
| 334 | fprintf(stderr, "Error opening output WebP file %s!\n", filename); |
| 335 | return 0; |
| 336 | } |
James Zern | 0f7820e | 2012-01-24 14:08:27 -0800 | [diff] [blame] | 337 | if (fwrite(webpdata->bytes_, webpdata->size_, 1, fout) != 1) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 338 | fprintf(stderr, "Error writing file %s!\n", filename); |
| 339 | } else { |
James Zern | 95667b8 | 2012-04-18 17:13:34 -0700 | [diff] [blame] | 340 | fprintf(stderr, "Saved file %s (%zu bytes)\n", filename, webpdata->size_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 341 | ok = 1; |
| 342 | } |
| 343 | if (fout != stdout) fclose(fout); |
| 344 | return ok; |
| 345 | } |
| 346 | |
| 347 | static int WriteWebP(WebPMux* const mux, const char* filename) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 348 | int ok; |
Urvang Joshi | f1df558 | 2012-06-07 11:04:57 +0530 | [diff] [blame] | 349 | WebPData webp_data; |
| 350 | const WebPMuxError err = WebPMuxAssemble(mux, &webp_data); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 351 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 352 | fprintf(stderr, "Error (%s) assembling the WebP file.\n", ErrorString(err)); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 353 | return 0; |
| 354 | } |
Urvang Joshi | f1df558 | 2012-06-07 11:04:57 +0530 | [diff] [blame] | 355 | ok = WriteData(filename, &webp_data); |
| 356 | WebPDataClear(&webp_data); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 357 | return ok; |
| 358 | } |
| 359 | |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 360 | static int ParseFrameArgs(const char* args, WebPMuxFrameInfo* const info) { |
| 361 | return (sscanf(args, "+%d+%d+%d", |
| 362 | &info->x_offset_, &info->y_offset_, &info->duration_) == 3); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 363 | } |
| 364 | |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 365 | static int ParseTileArgs(const char* args, WebPMuxFrameInfo* const info) { |
| 366 | return (sscanf(args, "+%d+%d", &info->x_offset_, &info->y_offset_) == 2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | //------------------------------------------------------------------------------ |
| 370 | // Clean-up. |
| 371 | |
| 372 | static void DeleteConfig(WebPMuxConfig* config) { |
| 373 | if (config != NULL) { |
| 374 | free(config->feature_.args_); |
| 375 | free(config); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | //------------------------------------------------------------------------------ |
| 380 | // Parsing. |
| 381 | |
| 382 | // Basic syntactic checks on the command-line arguments. |
| 383 | // Returns 1 on valid, 0 otherwise. |
| 384 | // Also fills up num_feature_args to be number of feature arguments given. |
| 385 | // (e.g. if there are 4 '-frame's and 1 '-loop', then num_feature_args = 5). |
| 386 | static int ValidateCommandLine(int argc, const char* argv[], |
| 387 | int* num_feature_args) { |
| 388 | int num_frame_args; |
| 389 | int num_tile_args; |
| 390 | int num_loop_args; |
| 391 | int ok = 1; |
| 392 | |
| 393 | assert(num_feature_args != NULL); |
| 394 | *num_feature_args = 0; |
| 395 | |
| 396 | // Simple checks. |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 397 | if (CountOccurrences(argv, argc, "-get") > 1) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 398 | ERROR_GOTO1("ERROR: Multiple '-get' arguments specified.\n", ErrValidate); |
| 399 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 400 | if (CountOccurrences(argv, argc, "-set") > 1) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 401 | ERROR_GOTO1("ERROR: Multiple '-set' arguments specified.\n", ErrValidate); |
| 402 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 403 | if (CountOccurrences(argv, argc, "-strip") > 1) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 404 | ERROR_GOTO1("ERROR: Multiple '-strip' arguments specified.\n", ErrValidate); |
| 405 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 406 | if (CountOccurrences(argv, argc, "-info") > 1) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 407 | ERROR_GOTO1("ERROR: Multiple '-info' arguments specified.\n", ErrValidate); |
| 408 | } |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 409 | if (CountOccurrences(argv, argc, "-o") > 1) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 410 | ERROR_GOTO1("ERROR: Multiple output files specified.\n", ErrValidate); |
| 411 | } |
| 412 | |
| 413 | // Compound checks. |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 414 | num_frame_args = CountOccurrences(argv, argc, "-frame"); |
| 415 | num_tile_args = CountOccurrences(argv, argc, "-tile"); |
| 416 | num_loop_args = CountOccurrences(argv, argc, "-loop"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 417 | |
| 418 | if (num_loop_args > 1) { |
| 419 | ERROR_GOTO1("ERROR: Multiple loop counts specified.\n", ErrValidate); |
| 420 | } |
| 421 | |
| 422 | if (IsNotCompatible(num_frame_args, num_loop_args)) { |
| 423 | ERROR_GOTO1("ERROR: Both frames and loop count have to be specified.\n", |
| 424 | ErrValidate); |
| 425 | } |
| 426 | if (num_frame_args > 0 && num_tile_args > 0) { |
| 427 | ERROR_GOTO1("ERROR: Only one of frames & tiles can be specified at a time." |
| 428 | "\n", ErrValidate); |
| 429 | } |
| 430 | |
| 431 | assert(ok == 1); |
| 432 | if (num_frame_args == 0 && num_tile_args == 0) { |
| 433 | // Single argument ('set' action for XMP or ICCP, OR a 'get' action). |
| 434 | *num_feature_args = 1; |
| 435 | } else { |
| 436 | // Multiple arguments ('set' action for animation or tiling). |
| 437 | if (num_frame_args > 0) { |
| 438 | *num_feature_args = num_frame_args + num_loop_args; |
| 439 | } else { |
| 440 | *num_feature_args = num_tile_args; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | ErrValidate: |
| 445 | return ok; |
| 446 | } |
| 447 | |
| 448 | #define ACTION_IS_NIL (config->action_type_ == NIL_ACTION) |
| 449 | |
| 450 | #define FEATURETYPE_IS_NIL (feature->type_ == NIL_FEATURE) |
| 451 | |
| 452 | #define CHECK_NUM_ARGS_LESS(NUM, LABEL) \ |
| 453 | if (argc < i + (NUM)) { \ |
| 454 | fprintf(stderr, "ERROR: Too few arguments for '%s'.\n", argv[i]); \ |
| 455 | goto LABEL; \ |
| 456 | } |
| 457 | |
| 458 | #define CHECK_NUM_ARGS_NOT_EQUAL(NUM, LABEL) \ |
| 459 | if (argc != i + (NUM)) { \ |
| 460 | fprintf(stderr, "ERROR: Too many arguments for '%s'.\n", argv[i]); \ |
| 461 | goto LABEL; \ |
| 462 | } |
| 463 | |
| 464 | // Parses command-line arguments to fill up config object. Also performs some |
| 465 | // semantic checks. |
| 466 | static int ParseCommandLine(int argc, const char* argv[], |
| 467 | WebPMuxConfig* config) { |
| 468 | int i = 0; |
| 469 | int feature_arg_index = 0; |
| 470 | int ok = 1; |
| 471 | |
| 472 | while (i < argc) { |
| 473 | Feature* const feature = &config->feature_; |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 474 | FeatureArg* const arg = &feature->args_[feature_arg_index]; |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 475 | if (argv[i][0] == '-') { // One of the action types or output. |
| 476 | if (!strcmp(argv[i], "-set")) { |
| 477 | if (ACTION_IS_NIL) { |
| 478 | config->action_type_ = ACTION_SET; |
| 479 | } else { |
| 480 | ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse); |
| 481 | } |
| 482 | ++i; |
| 483 | } else if (!strcmp(argv[i], "-get")) { |
| 484 | if (ACTION_IS_NIL) { |
| 485 | config->action_type_ = ACTION_GET; |
| 486 | } else { |
| 487 | ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse); |
| 488 | } |
| 489 | ++i; |
| 490 | } else if (!strcmp(argv[i], "-strip")) { |
| 491 | if (ACTION_IS_NIL) { |
| 492 | config->action_type_ = ACTION_STRIP; |
| 493 | feature->arg_count_ = 0; |
| 494 | } else { |
| 495 | ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse); |
| 496 | } |
| 497 | ++i; |
| 498 | } else if (!strcmp(argv[i], "-frame")) { |
| 499 | CHECK_NUM_ARGS_LESS(3, ErrParse); |
| 500 | if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) { |
| 501 | config->action_type_ = ACTION_SET; |
| 502 | } else { |
| 503 | ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse); |
| 504 | } |
| 505 | if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_FRM) { |
| 506 | feature->type_ = FEATURE_FRM; |
| 507 | } else { |
| 508 | ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse); |
| 509 | } |
| 510 | arg->subtype_ = SUBTYPE_FRM; |
| 511 | arg->filename_ = argv[i + 1]; |
| 512 | arg->params_ = argv[i + 2]; |
| 513 | ++feature_arg_index; |
| 514 | i += 3; |
| 515 | } else if (!strcmp(argv[i], "-loop")) { |
| 516 | CHECK_NUM_ARGS_LESS(2, ErrParse); |
| 517 | if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) { |
| 518 | config->action_type_ = ACTION_SET; |
| 519 | } else { |
| 520 | ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse); |
| 521 | } |
| 522 | if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_FRM) { |
| 523 | feature->type_ = FEATURE_FRM; |
| 524 | } else { |
| 525 | ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse); |
| 526 | } |
| 527 | arg->subtype_ = SUBTYPE_LOOP; |
| 528 | arg->params_ = argv[i + 1]; |
| 529 | ++feature_arg_index; |
| 530 | i += 2; |
| 531 | } else if (!strcmp(argv[i], "-tile")) { |
| 532 | CHECK_NUM_ARGS_LESS(3, ErrParse); |
| 533 | if (ACTION_IS_NIL || config->action_type_ == ACTION_SET) { |
| 534 | config->action_type_ = ACTION_SET; |
| 535 | } else { |
| 536 | ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse); |
| 537 | } |
| 538 | if (FEATURETYPE_IS_NIL || feature->type_ == FEATURE_TILE) { |
| 539 | feature->type_ = FEATURE_TILE; |
| 540 | } else { |
| 541 | ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse); |
| 542 | } |
| 543 | arg->filename_ = argv[i + 1]; |
| 544 | arg->params_ = argv[i + 2]; |
| 545 | ++feature_arg_index; |
| 546 | i += 3; |
| 547 | } else if (!strcmp(argv[i], "-o")) { |
| 548 | CHECK_NUM_ARGS_LESS(2, ErrParse); |
| 549 | config->output_ = argv[i + 1]; |
| 550 | i += 2; |
| 551 | } else if (!strcmp(argv[i], "-info")) { |
| 552 | CHECK_NUM_ARGS_NOT_EQUAL(2, ErrParse); |
| 553 | if (config->action_type_ != NIL_ACTION) { |
| 554 | ERROR_GOTO1("ERROR: Multiple actions specified.\n", ErrParse); |
| 555 | } else { |
| 556 | config->action_type_ = ACTION_INFO; |
| 557 | feature->arg_count_ = 0; |
| 558 | config->input_ = argv[i + 1]; |
| 559 | } |
| 560 | i += 2; |
| 561 | } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "-help")) { |
| 562 | PrintHelp(); |
| 563 | DeleteConfig(config); |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 564 | exit(0); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 565 | } else { |
| 566 | ERROR_GOTO2("ERROR: Unknown option: '%s'.\n", argv[i], ErrParse); |
| 567 | } |
| 568 | } else { // One of the feature types or input. |
| 569 | if (ACTION_IS_NIL) { |
| 570 | ERROR_GOTO1("ERROR: Action must be specified before other arguments.\n", |
| 571 | ErrParse); |
| 572 | } |
| 573 | if (!strcmp(argv[i], "icc") || !strcmp(argv[i], "xmp")) { |
| 574 | if (FEATURETYPE_IS_NIL) { |
| 575 | feature->type_ = (!strcmp(argv[i], "icc")) ? FEATURE_ICCP : |
| 576 | FEATURE_XMP; |
| 577 | } else { |
| 578 | ERROR_GOTO1("ERROR: Multiple features specified.\n", ErrParse); |
| 579 | } |
| 580 | if (config->action_type_ == ACTION_SET) { |
| 581 | CHECK_NUM_ARGS_LESS(2, ErrParse); |
| 582 | arg->filename_ = argv[i + 1]; |
| 583 | ++feature_arg_index; |
| 584 | i += 2; |
| 585 | } else { |
| 586 | ++i; |
| 587 | } |
| 588 | } else if ((!strcmp(argv[i], "frame") || |
| 589 | !strcmp(argv[i], "tile")) && |
| 590 | (config->action_type_ == ACTION_GET)) { |
| 591 | CHECK_NUM_ARGS_LESS(2, ErrParse); |
| 592 | feature->type_ = (!strcmp(argv[i], "frame")) ? FEATURE_FRM : |
| 593 | FEATURE_TILE; |
| 594 | arg->params_ = argv[i + 1]; |
| 595 | ++feature_arg_index; |
| 596 | i += 2; |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 597 | } else { // Assume input file. |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 598 | if (config->input_ == NULL) { |
| 599 | config->input_ = argv[i]; |
| 600 | } else { |
| 601 | ERROR_GOTO2("ERROR at '%s': Multiple input files specified.\n", |
| 602 | argv[i], ErrParse); |
| 603 | } |
| 604 | ++i; |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | ErrParse: |
| 609 | return ok; |
| 610 | } |
| 611 | |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 612 | // Additional checks after config is filled. |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 613 | static int ValidateConfig(WebPMuxConfig* config) { |
| 614 | int ok = 1; |
| 615 | Feature* const feature = &config->feature_; |
| 616 | |
| 617 | // Action. |
| 618 | if (ACTION_IS_NIL) { |
| 619 | ERROR_GOTO1("ERROR: No action specified.\n", ErrValidate2); |
| 620 | } |
| 621 | |
| 622 | // Feature type. |
| 623 | if (FEATURETYPE_IS_NIL && config->action_type_ != ACTION_INFO) { |
| 624 | ERROR_GOTO1("ERROR: No feature specified.\n", ErrValidate2); |
| 625 | } |
| 626 | |
| 627 | // Input file. |
| 628 | if (config->input_ == NULL) { |
| 629 | if (config->action_type_ != ACTION_SET) { |
| 630 | ERROR_GOTO1("ERROR: No input file specified.\n", ErrValidate2); |
| 631 | } else if (feature->type_ != FEATURE_FRM && |
| 632 | feature->type_ != FEATURE_TILE) { |
| 633 | ERROR_GOTO1("ERROR: No input file specified.\n", ErrValidate2); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | // Output file. |
| 638 | if (config->output_ == NULL && config->action_type_ != ACTION_INFO) { |
| 639 | ERROR_GOTO1("ERROR: No output file specified.\n", ErrValidate2); |
| 640 | } |
| 641 | |
| 642 | ErrValidate2: |
| 643 | return ok; |
| 644 | } |
| 645 | |
| 646 | // Create config object from command-line arguments. |
| 647 | static int InitializeConfig(int argc, const char* argv[], |
| 648 | WebPMuxConfig** config) { |
| 649 | int num_feature_args = 0; |
| 650 | int ok = 1; |
| 651 | |
| 652 | assert(config != NULL); |
| 653 | *config = NULL; |
| 654 | |
| 655 | // Validate command-line arguments. |
| 656 | if (!ValidateCommandLine(argc, argv, &num_feature_args)) { |
| 657 | ERROR_GOTO1("Exiting due to command-line parsing error.\n", Err1); |
| 658 | } |
| 659 | |
| 660 | // Allocate memory. |
| 661 | *config = (WebPMuxConfig*)calloc(1, sizeof(**config)); |
| 662 | if (*config == NULL) { |
| 663 | ERROR_GOTO1("ERROR: Memory allocation error.\n", Err1); |
| 664 | } |
| 665 | (*config)->feature_.arg_count_ = num_feature_args; |
| 666 | (*config)->feature_.args_ = |
| 667 | (FeatureArg*)calloc(num_feature_args, sizeof(FeatureArg)); |
| 668 | if ((*config)->feature_.args_ == NULL) { |
| 669 | ERROR_GOTO1("ERROR: Memory allocation error.\n", Err1); |
| 670 | } |
| 671 | |
| 672 | // Parse command-line. |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 673 | if (!ParseCommandLine(argc, argv, *config) || |
| 674 | !ValidateConfig(*config)) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 675 | ERROR_GOTO1("Exiting due to command-line parsing error.\n", Err1); |
| 676 | } |
| 677 | |
| 678 | Err1: |
| 679 | return ok; |
| 680 | } |
| 681 | |
| 682 | #undef ACTION_IS_NIL |
| 683 | #undef FEATURETYPE_IS_NIL |
| 684 | #undef CHECK_NUM_ARGS_LESS |
| 685 | #undef CHECK_NUM_ARGS_MORE |
| 686 | |
| 687 | //------------------------------------------------------------------------------ |
| 688 | // Processing. |
| 689 | |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 690 | static int GetFrameTile(const WebPMux* mux, |
| 691 | const WebPMuxConfig* config, int isFrame) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 692 | WebPMuxError err = WEBP_MUX_OK; |
| 693 | WebPMux* mux_single = NULL; |
| 694 | long num = 0; |
| 695 | int ok = 1; |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 696 | WebPMuxFrameInfo info; |
| 697 | WebPDataInit(&info.bitstream_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 698 | |
| 699 | num = strtol(config->feature_.args_[0].params_, NULL, 10); |
| 700 | if (num < 0) { |
| 701 | ERROR_GOTO1("ERROR: Frame/Tile index must be non-negative.\n", ErrGet); |
| 702 | } |
| 703 | |
| 704 | if (isFrame) { |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 705 | err = WebPMuxGetFrame(mux, num, &info); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 706 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 707 | ERROR_GOTO3("ERROR (%s): Could not get frame %ld.\n", |
| 708 | ErrorString(err), num, ErrGet); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 709 | } |
| 710 | } else { |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 711 | err = WebPMuxGetTile(mux, num, &info); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 712 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 713 | ERROR_GOTO3("ERROR (%s): Could not get frame %ld.\n", |
| 714 | ErrorString(err), num, ErrGet); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
| 718 | mux_single = WebPMuxNew(); |
| 719 | if (mux_single == NULL) { |
| 720 | err = WEBP_MUX_MEMORY_ERROR; |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 721 | ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n", |
| 722 | ErrorString(err), ErrGet); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 723 | } |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 724 | err = WebPMuxSetImage(mux_single, &info.bitstream_, 1); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 725 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 726 | ERROR_GOTO2("ERROR (%s): Could not create single image mux object.\n", |
| 727 | ErrorString(err), ErrGet); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 728 | } |
| 729 | ok = WriteWebP(mux_single, config->output_); |
| 730 | |
| 731 | ErrGet: |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 732 | WebPDataClear(&info.bitstream_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 733 | WebPMuxDelete(mux_single); |
| 734 | return ok; |
| 735 | } |
| 736 | |
| 737 | // Read and process config. |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 738 | static int Process(const WebPMuxConfig* config) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 739 | WebPMux* mux = NULL; |
Urvang Joshi | 4fc4a47 | 2012-06-05 14:20:45 +0530 | [diff] [blame] | 740 | WebPData metadata, color_profile; |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 741 | WebPMuxError err = WEBP_MUX_OK; |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 742 | int index = 0; |
| 743 | int ok = 1; |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 744 | const Feature* const feature = &config->feature_; |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 745 | |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 746 | switch (config->action_type_) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 747 | case ACTION_GET: |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 748 | ok = CreateMux(config->input_, &mux); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 749 | if (!ok) goto Err2; |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 750 | switch (feature->type_) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 751 | case FEATURE_FRM: |
| 752 | ok = GetFrameTile(mux, config, 1); |
| 753 | break; |
| 754 | |
| 755 | case FEATURE_TILE: |
| 756 | ok = GetFrameTile(mux, config, 0); |
| 757 | break; |
| 758 | |
| 759 | case FEATURE_ICCP: |
Urvang Joshi | 1c04a0d | 2012-08-23 15:28:20 +0530 | [diff] [blame] | 760 | err = WebPMuxGetChunk(mux, "ICCP", &color_profile); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 761 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 762 | ERROR_GOTO2("ERROR (%s): Could not get color profile.\n", |
| 763 | ErrorString(err), Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 764 | } |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 765 | ok = WriteData(config->output_, &color_profile); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 766 | break; |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 767 | case FEATURE_XMP: |
Urvang Joshi | 1c04a0d | 2012-08-23 15:28:20 +0530 | [diff] [blame] | 768 | err = WebPMuxGetChunk(mux, "META", &metadata); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 769 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 770 | ERROR_GOTO2("ERROR (%s): Could not get XMP metadata.\n", |
| 771 | ErrorString(err), Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 772 | } |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 773 | ok = WriteData(config->output_, &metadata); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 774 | break; |
| 775 | |
| 776 | default: |
| 777 | ERROR_GOTO1("ERROR: Invalid feature for action 'get'.\n", Err2); |
| 778 | break; |
| 779 | } |
| 780 | break; |
| 781 | |
| 782 | case ACTION_SET: |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 783 | switch (feature->type_) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 784 | case FEATURE_FRM: |
| 785 | mux = WebPMuxNew(); |
| 786 | if (mux == NULL) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 787 | ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n", |
| 788 | ErrorString(WEBP_MUX_MEMORY_ERROR), Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 789 | } |
| 790 | for (index = 0; index < feature->arg_count_; ++index) { |
| 791 | if (feature->args_[index].subtype_ == SUBTYPE_LOOP) { |
James Zern | 0f7820e | 2012-01-24 14:08:27 -0800 | [diff] [blame] | 792 | const long num = strtol(feature->args_[index].params_, NULL, 10); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 793 | if (num < 0) { |
| 794 | ERROR_GOTO1("ERROR: Loop count must be non-negative.\n", Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 795 | } |
James Zern | 0f7820e | 2012-01-24 14:08:27 -0800 | [diff] [blame] | 796 | err = WebPMuxSetLoopCount(mux, num); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 797 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 798 | ERROR_GOTO2("ERROR (%s): Could not set loop count.\n", |
| 799 | ErrorString(err), Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 800 | } |
| 801 | } else if (feature->args_[index].subtype_ == SUBTYPE_FRM) { |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 802 | WebPMuxFrameInfo frame; |
Urvang Joshi | b74ed6e | 2012-06-20 10:59:40 -0700 | [diff] [blame] | 803 | ok = ReadFileToWebPData(feature->args_[index].filename_, |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 804 | &frame.bitstream_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 805 | if (!ok) goto Err2; |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 806 | ok = ParseFrameArgs(feature->args_[index].params_, &frame); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 807 | if (!ok) { |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 808 | WebPDataClear(&frame.bitstream_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 809 | ERROR_GOTO1("ERROR: Could not parse frame properties.\n", Err2); |
| 810 | } |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 811 | err = WebPMuxPushFrame(mux, &frame, 1); |
| 812 | WebPDataClear(&frame.bitstream_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 813 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 814 | ERROR_GOTO3("ERROR (%s): Could not add a frame at index %d.\n", |
| 815 | ErrorString(err), index, Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 816 | } |
| 817 | } else { |
| 818 | ERROR_GOTO1("ERROR: Invalid subtype for 'frame'", Err2); |
| 819 | } |
| 820 | } |
| 821 | break; |
| 822 | |
| 823 | case FEATURE_TILE: |
| 824 | mux = WebPMuxNew(); |
| 825 | if (mux == NULL) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 826 | ERROR_GOTO2("ERROR (%s): Could not allocate a mux object.\n", |
| 827 | ErrorString(WEBP_MUX_MEMORY_ERROR), Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 828 | } |
| 829 | for (index = 0; index < feature->arg_count_; ++index) { |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 830 | WebPMuxFrameInfo tile; |
| 831 | ok = ReadFileToWebPData(feature->args_[index].filename_, |
| 832 | &tile.bitstream_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 833 | if (!ok) goto Err2; |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 834 | ok = ParseTileArgs(feature->args_[index].params_, &tile); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 835 | if (!ok) { |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 836 | WebPDataClear(&tile.bitstream_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 837 | ERROR_GOTO1("ERROR: Could not parse tile properties.\n", Err2); |
| 838 | } |
Urvang Joshi | ab3234a | 2012-08-23 15:18:51 +0530 | [diff] [blame] | 839 | err = WebPMuxPushTile(mux, &tile, 1); |
| 840 | WebPDataClear(&tile.bitstream_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 841 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 842 | ERROR_GOTO3("ERROR (%s): Could not add a tile at index %d.\n", |
| 843 | ErrorString(err), index, Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | break; |
| 847 | |
| 848 | case FEATURE_ICCP: |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 849 | ok = CreateMux(config->input_, &mux); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 850 | if (!ok) goto Err2; |
Urvang Joshi | 4fc4a47 | 2012-06-05 14:20:45 +0530 | [diff] [blame] | 851 | ok = ReadFileToWebPData(feature->args_[0].filename_, &color_profile); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 852 | if (!ok) goto Err2; |
Urvang Joshi | 1c04a0d | 2012-08-23 15:28:20 +0530 | [diff] [blame] | 853 | err = WebPMuxSetChunk(mux, "ICCP", &color_profile, 1); |
Urvang Joshi | 4fc4a47 | 2012-06-05 14:20:45 +0530 | [diff] [blame] | 854 | free((void*)color_profile.bytes_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 855 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 856 | ERROR_GOTO2("ERROR (%s): Could not set color profile.\n", |
| 857 | ErrorString(err), Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 858 | } |
| 859 | break; |
| 860 | |
| 861 | case FEATURE_XMP: |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 862 | ok = CreateMux(config->input_, &mux); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 863 | if (!ok) goto Err2; |
Urvang Joshi | 4fc4a47 | 2012-06-05 14:20:45 +0530 | [diff] [blame] | 864 | ok = ReadFileToWebPData(feature->args_[0].filename_, &metadata); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 865 | if (!ok) goto Err2; |
Urvang Joshi | 1c04a0d | 2012-08-23 15:28:20 +0530 | [diff] [blame] | 866 | err = WebPMuxSetChunk(mux, "META", &metadata, 1); |
Urvang Joshi | 4fc4a47 | 2012-06-05 14:20:45 +0530 | [diff] [blame] | 867 | free((void*)metadata.bytes_); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 868 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 869 | ERROR_GOTO2("ERROR (%s): Could not set XMP metadata.\n", |
| 870 | ErrorString(err), Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 871 | } |
| 872 | break; |
| 873 | |
| 874 | default: |
| 875 | ERROR_GOTO1("ERROR: Invalid feature for action 'set'.\n", Err2); |
| 876 | break; |
| 877 | } |
| 878 | ok = WriteWebP(mux, config->output_); |
| 879 | break; |
| 880 | |
| 881 | case ACTION_STRIP: |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 882 | ok = CreateMux(config->input_, &mux); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 883 | if (!ok) goto Err2; |
James Zern | 04e84cf | 2011-11-04 15:20:08 -0700 | [diff] [blame] | 884 | switch (feature->type_) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 885 | case FEATURE_ICCP: |
Urvang Joshi | 1c04a0d | 2012-08-23 15:28:20 +0530 | [diff] [blame] | 886 | err = WebPMuxDeleteChunk(mux, "ICCP"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 887 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 888 | ERROR_GOTO2("ERROR (%s): Could not delete color profile.\n", |
| 889 | ErrorString(err), Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 890 | } |
| 891 | break; |
| 892 | case FEATURE_XMP: |
Urvang Joshi | 1c04a0d | 2012-08-23 15:28:20 +0530 | [diff] [blame] | 893 | err = WebPMuxDeleteChunk(mux, "META"); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 894 | if (err != WEBP_MUX_OK) { |
Urvang Joshi | d11f6fc | 2012-06-27 16:55:01 +0530 | [diff] [blame] | 895 | ERROR_GOTO2("ERROR (%s): Could not delete XMP metadata.\n", |
| 896 | ErrorString(err), Err2); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 897 | } |
| 898 | break; |
| 899 | default: |
| 900 | ERROR_GOTO1("ERROR: Invalid feature for action 'strip'.\n", Err2); |
| 901 | break; |
| 902 | } |
| 903 | ok = WriteWebP(mux, config->output_); |
| 904 | break; |
| 905 | |
| 906 | case ACTION_INFO: |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 907 | ok = CreateMux(config->input_, &mux); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 908 | if (!ok) goto Err2; |
| 909 | ok = (DisplayInfo(mux) == WEBP_MUX_OK); |
| 910 | break; |
| 911 | |
| 912 | default: |
| 913 | assert(0); // Invalid action. |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | Err2: |
| 918 | WebPMuxDelete(mux); |
| 919 | return ok; |
| 920 | } |
| 921 | |
| 922 | //------------------------------------------------------------------------------ |
| 923 | // Main. |
| 924 | |
| 925 | int main(int argc, const char* argv[]) { |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 926 | WebPMuxConfig* config; |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 927 | int ok = InitializeConfig(argc - 1, argv + 1, &config); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 928 | if (ok) { |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 929 | ok = Process(config); |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 930 | } else { |
| 931 | PrintHelp(); |
| 932 | } |
| 933 | DeleteConfig(config); |
James Zern | 974aaff | 2012-01-24 12:46:46 -0800 | [diff] [blame] | 934 | return !ok; |
Urvang Joshi | a4f32ca | 2011-09-30 11:07:01 +0530 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | //------------------------------------------------------------------------------ |