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