Debian Codegraph @

examples/webpinfo.c

raw
1 // Copyright 2017 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // Command-line tool to print out the chunk level structure of WebP files
11 // along with basic integrity checks.
12 //
13 // Author: Hui Su ([email protected])
14
15 #include <assert.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #ifdef HAVE_CONFIG_H
20 #include "webp/config.h"
21 #endif
22
23 #include "../imageio/imageio_util.h"
24 #include "./unicode.h"
25 #include "webp/decode.h"
26 #include "webp/format_constants.h"
27 #include "webp/mux_types.h"
28
29 #if defined(_MSC_VER) && _MSC_VER < 1900
30 #define snprintf _snprintf
31 #endif
32
33 #define LOG_ERROR(MESSAGE) \
34 do { \
35 if (webp_info->show_diagnosis_) { \
36 fprintf(stderr, "Error: %s\n", MESSAGE); \
37 } \
38 } while (0)
39
40 #define LOG_WARN(MESSAGE) \
41 do { \
42 if (webp_info->show_diagnosis_) { \
43 fprintf(stderr, "Warning: %s\n", MESSAGE); \
44 } \
45 ++webp_info->num_warnings_; \
46 } while (0)
47
48 static const char* const kFormats[3] = {
49 "Unknown",
50 "Lossy",
51 "Lossless"
52 };
53
54 static const char* const kLosslessTransforms[4] = {
55 "Predictor",
56 "Cross Color",
57 "Subtract Green",
58 "Color Indexing"
59 };
60
61 static const char* const kAlphaFilterMethods[4] = {
62 "None",
63 "Horizontal",
64 "Vertical",
65 "Gradient"
66 };
67
68 typedef enum {
69 WEBP_INFO_OK = 0,
70 WEBP_INFO_TRUNCATED_DATA,
71 WEBP_INFO_PARSE_ERROR,
72 WEBP_INFO_INVALID_PARAM,
73 WEBP_INFO_BITSTREAM_ERROR,
74 WEBP_INFO_MISSING_DATA,
75 WEBP_INFO_INVALID_COMMAND
76 } WebPInfoStatus;
77
78 typedef enum ChunkID {
79 CHUNK_VP8,
80 CHUNK_VP8L,
81 CHUNK_VP8X,
82 CHUNK_ALPHA,
83 CHUNK_ANIM,
84 CHUNK_ANMF,
85 CHUNK_ICCP,
86 CHUNK_EXIF,
87 CHUNK_XMP,
88 CHUNK_UNKNOWN,
89 CHUNK_TYPES = CHUNK_UNKNOWN
90 } ChunkID;
91
92 typedef struct {
93 size_t start_;
94 size_t end_;
95 const uint8_t* buf_;
96 } MemBuffer;
97
98 typedef struct {
99 size_t offset_;
100 size_t size_;
101 const uint8_t* payload_;
102 ChunkID id_;
103 } ChunkData;
104
105 typedef struct WebPInfo {
106 int canvas_width_;
107 int canvas_height_;
108 int loop_count_;
109 int num_frames_;
110 int chunk_counts_[CHUNK_TYPES];
111 int anmf_subchunk_counts_[3]; // 0 VP8; 1 VP8L; 2 ALPH.
112 uint32_t bgcolor_;
113 int feature_flags_;
114 int has_alpha_;
115 // Used for parsing ANMF chunks.
116 int frame_width_, frame_height_;
117 size_t anim_frame_data_size_;
118 int is_processing_anim_frame_, seen_alpha_subchunk_, seen_image_subchunk_;
119 // Print output control.
120 int quiet_, show_diagnosis_, show_summary_;
121 int num_warnings_;
122 int parse_bitstream_;
123 } WebPInfo;
124
125 static void WebPInfoInit(WebPInfo* const webp_info) {
126 memset(webp_info, 0, sizeof(*webp_info));
127 }
128
129 static const uint32_t kWebPChunkTags[CHUNK_TYPES] = {
130 MKFOURCC('V', 'P', '8', ' '),
131 MKFOURCC('V', 'P', '8', 'L'),
132 MKFOURCC('V', 'P', '8', 'X'),
133 MKFOURCC('A', 'L', 'P', 'H'),
134 MKFOURCC('A', 'N', 'I', 'M'),
135 MKFOURCC('A', 'N', 'M', 'F'),
136 MKFOURCC('I', 'C', 'C', 'P'),
137 MKFOURCC('E', 'X', 'I', 'F'),
138 MKFOURCC('X', 'M', 'P', ' '),
139 };
140
141 // -----------------------------------------------------------------------------
142 // Data reading.
143
144 static int GetLE16(const uint8_t* const data) {
145 return (data[0] << 0) | (data[1] << 8);
146 }
147
148 static int GetLE24(const uint8_t* const data) {
149 return GetLE16(data) | (data[2] << 16);
150 }
151
152 static uint32_t GetLE32(const uint8_t* const data) {
153 return GetLE16(data) | ((uint32_t)GetLE16(data + 2) << 16);
154 }
155
156 static int ReadLE16(const uint8_t** data) {
157 const int val = GetLE16(*data);
158 *data += 2;
159 return val;
160 }
161
162 static int ReadLE24(const uint8_t** data) {
163 const int val = GetLE24(*data);
164 *data += 3;
165 return val;
166 }
167
168 static uint32_t ReadLE32(const uint8_t** data) {
169 const uint32_t val = GetLE32(*data);
170 *data += 4;
171 return val;
172 }
173
174 static int ReadFileToWebPData(const char* const filename,
175 WebPData* const webp_data) {
176 const uint8_t* data;
177 size_t size;
178 if (!ImgIoUtilReadFile(filename, &data, &size)) return 0;
179 webp_data->bytes = data;
180 webp_data->size = size;
181 return 1;
182 }
183
184 // -----------------------------------------------------------------------------
185 // MemBuffer object.
186
187 static void InitMemBuffer(MemBuffer* const mem, const WebPData* webp_data) {
188 mem->buf_ = webp_data->bytes;
189 mem->start_ = 0;
190 mem->end_ = webp_data->size;
191 }
192
193 static size_t MemDataSize(const MemBuffer* const mem) {
194 return (mem->end_ - mem->start_);
195 }
196
197 static const uint8_t* GetBuffer(MemBuffer* const mem) {
198 return mem->buf_ + mem->start_;
199 }
200
201 static void Skip(MemBuffer* const mem, size_t size) {
202 mem->start_ += size;
203 }
204
205 static uint32_t ReadMemBufLE32(MemBuffer* const mem) {
206 const uint8_t* const data = mem->buf_ + mem->start_;
207 const uint32_t val = GetLE32(data);
208 assert(MemDataSize(mem) >= 4);
209 Skip(mem, 4);
210 return val;
211 }
212
213 // -----------------------------------------------------------------------------
214 // Lossy bitstream analysis.
215
216 static int GetBits(const uint8_t* const data, size_t data_size, size_t nb,
217 int* val, uint64_t* const bit_pos) {
218 *val = 0;
219 while (nb-- > 0) {
220 const uint64_t p = (*bit_pos)++;
221 if ((p >> 3) >= data_size) {
222 return 0;
223 } else {
224 const int bit = !!(data[p >> 3] & (128 >> ((p & 7))));
225 *val = (*val << 1) | bit;
226 }
227 }
228 return 1;
229 }
230
231 static int GetSignedBits(const uint8_t* const data, size_t data_size, size_t nb,
232 int* val, uint64_t* const bit_pos) {
233 int sign;
234 if (!GetBits(data, data_size, nb, val, bit_pos)) return 0;
235 if (!GetBits(data, data_size, 1, &sign, bit_pos)) return 0;
236 if (sign) *val = -(*val);
237 return 1;
238 }
239
240 #define GET_BITS(v, n) \
241 do { \
242 if (!GetBits(data, data_size, n, &(v), bit_pos)) { \
243 LOG_ERROR("Truncated lossy bitstream."); \
244 return WEBP_INFO_TRUNCATED_DATA; \
245 } \
246 } while (0)
247
248 #define GET_SIGNED_BITS(v, n) \
249 do { \
250 if (!GetSignedBits(data, data_size, n, &(v), bit_pos)) { \
251 LOG_ERROR("Truncated lossy bitstream."); \
252 return WEBP_INFO_TRUNCATED_DATA; \
253 } \
254 } while (0)
255
256 static WebPInfoStatus ParseLossySegmentHeader(const WebPInfo* const webp_info,
257 const uint8_t* const data,
258 size_t data_size,
259 uint64_t* const bit_pos) {
260 int use_segment;
261 GET_BITS(use_segment, 1);
262 printf(" Use segment: %d\n", use_segment);
263 if (use_segment) {
264 int update_map, update_data;
265 GET_BITS(update_map, 1);
266 GET_BITS(update_data, 1);
267 printf(" Update map: %d\n"
268 " Update data: %d\n",
269 update_map, update_data);
270 if (update_data) {
271 int i, a_delta;
272 int quantizer[4] = {0, 0, 0, 0};
273 int filter_strength[4] = {0, 0, 0, 0};
274 GET_BITS(a_delta, 1);
275 printf(" Absolute delta: %d\n", a_delta);
276 for (i = 0; i < 4; ++i) {
277 int bit;
278 GET_BITS(bit, 1);
279 if (bit) GET_SIGNED_BITS(quantizer[i], 7);
280 }
281 for (i = 0; i < 4; ++i) {
282 int bit;
283 GET_BITS(bit, 1);
284 if (bit) GET_SIGNED_BITS(filter_strength[i], 6);
285 }
286 printf(" Quantizer: %d %d %d %d\n", quantizer[0], quantizer[1],
287 quantizer[2], quantizer[3]);
288 printf(" Filter strength: %d %d %d %d\n", filter_strength[0],
289 filter_strength[1], filter_strength[2], filter_strength[3]);
290 }
291 if (update_map) {
292 int i;
293 int prob_segment[3] = {255, 255, 255};
294 for (i = 0; i < 3; ++i) {
295 int bit;
296 GET_BITS(bit, 1);
297 if (bit) GET_BITS(prob_segment[i], 8);
298 }
299 printf(" Prob segment: %d %d %d\n",
300 prob_segment[0], prob_segment[1], prob_segment[2]);
301 }
302 }
303 return WEBP_INFO_OK;
304 }
305
306 static WebPInfoStatus ParseLossyFilterHeader(const WebPInfo* const webp_info,
307 const uint8_t* const data,
308 size_t data_size,
309 uint64_t* const bit_pos) {
310 int simple_filter, level, sharpness, use_lf_delta;
311 GET_BITS(simple_filter, 1);
312 GET_BITS(level, 6);
313 GET_BITS(sharpness, 3);
314 GET_BITS(use_lf_delta, 1);
315 printf(" Simple filter: %d\n", simple_filter);
316 printf(" Level: %d\n", level);
317 printf(" Sharpness: %d\n", sharpness);
318 printf(" Use lf delta: %d\n", use_lf_delta);
319 if (use_lf_delta) {
320 int update;
321 GET_BITS(update, 1);
322 printf(" Update lf delta: %d\n", update);
323 if (update) {
324 int i;
325 for (i = 0; i < 4 + 4; ++i) {
326 int temp;
327 GET_BITS(temp, 1);
328 if (temp) GET_BITS(temp, 7);
329 }
330 }
331 }
332 return WEBP_INFO_OK;
333 }
334
335 static WebPInfoStatus ParseLossyHeader(const ChunkData* const chunk_data,
336 const WebPInfo* const webp_info) {
337 const uint8_t* data = chunk_data->payload_;
338 size_t data_size = chunk_data->size_ - CHUNK_HEADER_SIZE;
339 const uint32_t bits = (uint32_t)data[0] | (data[1] << 8) | (data[2] << 16);
340 const int key_frame = !(bits & 1);
341 const int profile = (bits >> 1) & 7;
342 const int display = (bits >> 4) & 1;
343 const uint32_t partition0_length = (bits >> 5);
344 WebPInfoStatus status = WEBP_INFO_OK;
345 uint64_t bit_position = 0;
346 uint64_t* const bit_pos = &bit_position;
347 int colorspace, clamp_type;
348 printf(" Parsing lossy bitstream...\n");
349 // Calling WebPGetFeatures() in ProcessImageChunk() should ensure this.
350 assert(chunk_data->size_ >= CHUNK_HEADER_SIZE + 10);
351 if (profile > 3) {
352 LOG_ERROR("Unknown profile.");
353 return WEBP_INFO_BITSTREAM_ERROR;
354 }
355 if (!display) {
356 LOG_ERROR("Frame is not displayable.");
357 return WEBP_INFO_BITSTREAM_ERROR;
358 }
359 data += 3;
360 data_size -= 3;
361 printf(
362 " Key frame: %s\n"
363 " Profile: %d\n"
364 " Display: Yes\n"
365 " Part. 0 length: %d\n",
366 key_frame ? "Yes" : "No", profile, partition0_length);
367 if (key_frame) {
368 if (!(data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a)) {
369 LOG_ERROR("Invalid lossy bitstream signature.");
370 return WEBP_INFO_BITSTREAM_ERROR;
371 }
372 printf(" Width: %d\n"
373 " X scale: %d\n"
374 " Height: %d\n"
375 " Y scale: %d\n",
376 ((data[4] << 8) | data[3]) & 0x3fff, data[4] >> 6,
377 ((data[6] << 8) | data[5]) & 0x3fff, data[6] >> 6);
378 data += 7;
379 data_size -= 7;
380 } else {
381 LOG_ERROR("Non-keyframe detected in lossy bitstream.");
382 return WEBP_INFO_BITSTREAM_ERROR;
383 }
384 if (partition0_length >= data_size) {
385 LOG_ERROR("Bad partition length.");
386 return WEBP_INFO_BITSTREAM_ERROR;
387 }
388 GET_BITS(colorspace, 1);
389 GET_BITS(clamp_type, 1);
390 printf(" Color space: %d\n", colorspace);
391 printf(" Clamp type: %d\n", clamp_type);
392 status = ParseLossySegmentHeader(webp_info, data, data_size, bit_pos);
393 if (status != WEBP_INFO_OK) return status;
394 status = ParseLossyFilterHeader(webp_info, data, data_size, bit_pos);
395 if (status != WEBP_INFO_OK) return status;
396 { // Partition number and size.
397 const uint8_t* part_size = data + partition0_length;
398 int num_parts, i;
399 size_t part_data_size;
400 GET_BITS(num_parts, 2);
401 num_parts = 1 << num_parts;
402 if ((int)(data_size - partition0_length) < (num_parts - 1) * 3) {
403 LOG_ERROR("Truncated lossy bitstream.");
404 return WEBP_INFO_TRUNCATED_DATA;
405 }
406 part_data_size = data_size - partition0_length - (num_parts - 1) * 3;
407 printf(" Total partitions: %d\n", num_parts);
408 for (i = 1; i < num_parts; ++i) {
409 const size_t psize =
410 part_size[0] | (part_size[1] << 8) | (part_size[2] << 16);
411 if (psize > part_data_size) {
412 LOG_ERROR("Truncated partition.");
413 return WEBP_INFO_TRUNCATED_DATA;
414 }
415 printf(" Part. %d length: %d\n", i, (int)psize);
416 part_data_size -= psize;
417 part_size += 3;
418 }
419 }
420 // Quantizer.
421 {
422 int base_q, bit;
423 int dq_y1_dc = 0, dq_y2_dc = 0, dq_y2_ac = 0, dq_uv_dc = 0, dq_uv_ac = 0;
424 GET_BITS(base_q, 7);
425 GET_BITS(bit, 1);
426 if (bit) GET_SIGNED_BITS(dq_y1_dc, 4);
427 GET_BITS(bit, 1);
428 if (bit) GET_SIGNED_BITS(dq_y2_dc, 4);
429 GET_BITS(bit, 1);
430 if (bit) GET_SIGNED_BITS(dq_y2_ac, 4);
431 GET_BITS(bit, 1);
432 if (bit) GET_SIGNED_BITS(dq_uv_dc, 4);
433 GET_BITS(bit, 1);
434 if (bit) GET_SIGNED_BITS(dq_uv_ac, 4);
435 printf(" Base Q: %d\n", base_q);
436 printf(" DQ Y1 DC: %d\n", dq_y1_dc);
437 printf(" DQ Y2 DC: %d\n", dq_y2_dc);
438 printf(" DQ Y2 AC: %d\n", dq_y2_ac);
439 printf(" DQ UV DC: %d\n", dq_uv_dc);
440 printf(" DQ UV AC: %d\n", dq_uv_ac);
441 }
442 if ((*bit_pos >> 3) >= partition0_length) {
443 LOG_ERROR("Truncated lossy bitstream.");
444 return WEBP_INFO_TRUNCATED_DATA;
445 }
446 return WEBP_INFO_OK;
447 }
448
449 // -----------------------------------------------------------------------------
450 // Lossless bitstream analysis.
451
452 static int LLGetBits(const uint8_t* const data, size_t data_size, size_t nb,
453 int* val, uint64_t* const bit_pos) {
454 uint32_t i = 0;
455 *val = 0;
456 while (i < nb) {
457 const uint64_t p = (*bit_pos)++;
458 if ((p >> 3) >= data_size) {
459 return 0;
460 } else {
461 const int bit = !!(data[p >> 3] & (1 << ((p & 7))));
462 *val = *val | (bit << i);
463 ++i;
464 }
465 }
466 return 1;
467 }
468
469 #define LL_GET_BITS(v, n) \
470 do { \
471 if (!LLGetBits(data, data_size, n, &(v), bit_pos)) { \
472 LOG_ERROR("Truncated lossless bitstream."); \
473 return WEBP_INFO_TRUNCATED_DATA; \
474 } \
475 } while (0)
476
477 static WebPInfoStatus ParseLosslessTransform(WebPInfo* const webp_info,
478 const uint8_t* const data,
479 size_t data_size,
480 uint64_t* const bit_pos) {
481 int use_transform, block_size, n_colors;
482 LL_GET_BITS(use_transform, 1);
483 printf(" Use transform: %s\n", use_transform ? "Yes" : "No");
484 if (use_transform) {
485 int type;
486 LL_GET_BITS(type, 2);
487 printf(" 1st transform: %s (%d)\n", kLosslessTransforms[type], type);
488 switch (type) {
489 case PREDICTOR_TRANSFORM:
490 case CROSS_COLOR_TRANSFORM:
491 LL_GET_BITS(block_size, 3);
492 block_size = 1 << (block_size + 2);
493 printf(" Tran. block size: %d\n", block_size);
494 break;
495 case COLOR_INDEXING_TRANSFORM:
496 LL_GET_BITS(n_colors, 8);
497 n_colors += 1;
498 printf(" No. of colors: %d\n", n_colors);
499 break;
500 default: break;
501 }
502 }
503 return WEBP_INFO_OK;
504 }
505
506 static WebPInfoStatus ParseLosslessHeader(const ChunkData* const chunk_data,
507 WebPInfo* const webp_info) {
508 const uint8_t* data = chunk_data->payload_;
509 size_t data_size = chunk_data->size_ - CHUNK_HEADER_SIZE;
510 uint64_t bit_position = 0;
511 uint64_t* const bit_pos = &bit_position;
512 WebPInfoStatus status;
513 printf(" Parsing lossless bitstream...\n");
514 if (data_size < VP8L_FRAME_HEADER_SIZE) {
515 LOG_ERROR("Truncated lossless bitstream.");
516 return WEBP_INFO_TRUNCATED_DATA;
517 }
518 if (data[0] != VP8L_MAGIC_BYTE) {
519 LOG_ERROR("Invalid lossless bitstream signature.");
520 return WEBP_INFO_BITSTREAM_ERROR;
521 }
522 data += 1;
523 data_size -= 1;
524 {
525 int width, height, has_alpha, version;
526 LL_GET_BITS(width, 14);
527 LL_GET_BITS(height, 14);
528 LL_GET_BITS(has_alpha, 1);
529 LL_GET_BITS(version, 3);
530 width += 1;
531 height += 1;
532 printf(" Width: %d\n", width);
533 printf(" Height: %d\n", height);
534 printf(" Alpha: %d\n", has_alpha);
535 printf(" Version: %d\n", version);
536 }
537 status = ParseLosslessTransform(webp_info, data, data_size, bit_pos);
538 if (status != WEBP_INFO_OK) return status;
539 return WEBP_INFO_OK;
540 }
541
542 static WebPInfoStatus ParseAlphaHeader(const ChunkData* const chunk_data,
543 WebPInfo* const webp_info) {
544 const uint8_t* data = chunk_data->payload_;
545 size_t data_size = chunk_data->size_ - CHUNK_HEADER_SIZE;
546 if (data_size <= ALPHA_HEADER_LEN) {
547 LOG_ERROR("Truncated ALPH chunk.");
548 return WEBP_INFO_TRUNCATED_DATA;
549 }
550 printf(" Parsing ALPH chunk...\n");
551 {
552 const int compression_method = (data[0] >> 0) & 0x03;
553 const int filter = (data[0] >> 2) & 0x03;
554 const int pre_processing = (data[0] >> 4) & 0x03;
555 const int reserved_bits = (data[0] >> 6) & 0x03;
556 printf(" Compression: %d\n", compression_method);
557 printf(" Filter: %s (%d)\n",
558 kAlphaFilterMethods[filter], filter);
559 printf(" Pre-processing: %d\n", pre_processing);
560 if (compression_method > ALPHA_LOSSLESS_COMPRESSION) {
561 LOG_ERROR("Invalid Alpha compression method.");
562 return WEBP_INFO_BITSTREAM_ERROR;
563 }
564 if (pre_processing > ALPHA_PREPROCESSED_LEVELS) {
565 LOG_ERROR("Invalid Alpha pre-processing method.");
566 return WEBP_INFO_BITSTREAM_ERROR;
567 }
568 if (reserved_bits != 0) {
569 LOG_WARN("Reserved bits in ALPH chunk header are not all 0.");
570 }
571 data += ALPHA_HEADER_LEN;
572 data_size -= ALPHA_HEADER_LEN;
573 if (compression_method == ALPHA_LOSSLESS_COMPRESSION) {
574 uint64_t bit_pos = 0;
575 WebPInfoStatus status =
576 ParseLosslessTransform(webp_info, data, data_size, &bit_pos);
577 if (status != WEBP_INFO_OK) return status;
578 }
579 }
580 return WEBP_INFO_OK;
581 }
582
583 // -----------------------------------------------------------------------------
584 // Chunk parsing.
585
586 static WebPInfoStatus ParseRIFFHeader(WebPInfo* const webp_info,
587 MemBuffer* const mem) {
588 const size_t min_size = RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE;
589 size_t riff_size;
590
591 if (MemDataSize(mem) < min_size) {
592 LOG_ERROR("Truncated data detected when parsing RIFF header.");
593 return WEBP_INFO_TRUNCATED_DATA;
594 }
595 if (memcmp(GetBuffer(mem), "RIFF", CHUNK_SIZE_BYTES) ||
596 memcmp(GetBuffer(mem) + CHUNK_HEADER_SIZE, "WEBP", CHUNK_SIZE_BYTES)) {
597 LOG_ERROR("Corrupted RIFF header.");
598 return WEBP_INFO_PARSE_ERROR;
599 }
600 riff_size = GetLE32(GetBuffer(mem) + TAG_SIZE);
601 if (riff_size < CHUNK_HEADER_SIZE) {
602 LOG_ERROR("RIFF size is too small.");
603 return WEBP_INFO_PARSE_ERROR;
604 }
605 if (riff_size > MAX_CHUNK_PAYLOAD) {
606 LOG_ERROR("RIFF size is over limit.");
607 return WEBP_INFO_PARSE_ERROR;
608 }
609 riff_size += CHUNK_HEADER_SIZE;
610 if (!webp_info->quiet_) {
611 printf("RIFF HEADER:\n");
612 printf(" File size: %6d\n", (int)riff_size);
613 }
614 if (riff_size < mem->end_) {
615 LOG_WARN("RIFF size is smaller than the file size.");
616 mem->end_ = riff_size;
617 } else if (riff_size > mem->end_) {
618 LOG_ERROR("Truncated data detected when parsing RIFF payload.");
619 return WEBP_INFO_TRUNCATED_DATA;
620 }
621 Skip(mem, RIFF_HEADER_SIZE);
622 return WEBP_INFO_OK;
623 }
624
625 static WebPInfoStatus ParseChunk(const WebPInfo* const webp_info,
626 MemBuffer* const mem,
627 ChunkData* const chunk_data) {
628 memset(chunk_data, 0, sizeof(*chunk_data));
629 if (MemDataSize(mem) < CHUNK_HEADER_SIZE) {
630 LOG_ERROR("Truncated data detected when parsing chunk header.");
631 return WEBP_INFO_TRUNCATED_DATA;
632 } else {
633 const size_t chunk_start_offset = mem->start_;
634 const uint32_t fourcc = ReadMemBufLE32(mem);
635 const uint32_t payload_size = ReadMemBufLE32(mem);
636 const uint32_t payload_size_padded = payload_size + (payload_size & 1);
637 const size_t chunk_size = CHUNK_HEADER_SIZE + payload_size_padded;
638 int i;
639 if (payload_size > MAX_CHUNK_PAYLOAD) {
640 LOG_ERROR("Size of chunk payload is over limit.");
641 return WEBP_INFO_INVALID_PARAM;
642 }
643 if (payload_size_padded > MemDataSize(mem)){
644 LOG_ERROR("Truncated data detected when parsing chunk payload.");
645 return WEBP_INFO_TRUNCATED_DATA;
646 }
647 for (i = 0; i < CHUNK_TYPES; ++i) {
648 if (kWebPChunkTags[i] == fourcc) break;
649 }
650 chunk_data->offset_ = chunk_start_offset;
651 chunk_data->size_ = chunk_size;
652 chunk_data->id_ = (ChunkID)i;
653 chunk_data->payload_ = GetBuffer(mem);
654 if (chunk_data->id_ == CHUNK_ANMF) {
655 if (payload_size != payload_size_padded) {
656 LOG_ERROR("ANMF chunk size should always be even.");
657 return WEBP_INFO_PARSE_ERROR;
658 }
659 // There are sub-chunks to be parsed in an ANMF chunk.
660 Skip(mem, ANMF_CHUNK_SIZE);
661 } else {
662 Skip(mem, payload_size_padded);
663 }
664 return WEBP_INFO_OK;
665 }
666 }
667
668 // -----------------------------------------------------------------------------
669 // Chunk analysis.
670
671 static WebPInfoStatus ProcessVP8XChunk(const ChunkData* const chunk_data,
672 WebPInfo* const webp_info) {
673 const uint8_t* data = chunk_data->payload_;
674 if (webp_info->chunk_counts_[CHUNK_VP8] ||
675 webp_info->chunk_counts_[CHUNK_VP8L] ||
676 webp_info->chunk_counts_[CHUNK_VP8X]) {
677 LOG_ERROR("Already seen a VP8/VP8L/VP8X chunk when parsing VP8X chunk.");
678 return WEBP_INFO_PARSE_ERROR;
679 }
680 if (chunk_data->size_ != VP8X_CHUNK_SIZE + CHUNK_HEADER_SIZE) {
681 LOG_ERROR("Corrupted VP8X chunk.");
682 return WEBP_INFO_PARSE_ERROR;
683 }
684 ++webp_info->chunk_counts_[CHUNK_VP8X];
685 webp_info->feature_flags_ = *data;
686 data += 4;
687 webp_info->canvas_width_ = 1 + ReadLE24(&data);
688 webp_info->canvas_height_ = 1 + ReadLE24(&data);
689 if (!webp_info->quiet_) {
690 printf(" ICCP: %d\n Alpha: %d\n EXIF: %d\n XMP: %d\n Animation: %d\n",
691 (webp_info->feature_flags_ & ICCP_FLAG) != 0,
692 (webp_info->feature_flags_ & ALPHA_FLAG) != 0,
693 (webp_info->feature_flags_ & EXIF_FLAG) != 0,
694 (webp_info->feature_flags_ & XMP_FLAG) != 0,
695 (webp_info->feature_flags_ & ANIMATION_FLAG) != 0);
696 printf(" Canvas size %d x %d\n",
697 webp_info->canvas_width_, webp_info->canvas_height_);
698 }
699 if (webp_info->canvas_width_ > MAX_CANVAS_SIZE) {
700 LOG_WARN("Canvas width is out of range in VP8X chunk.");
701 }
702 if (webp_info->canvas_height_ > MAX_CANVAS_SIZE) {
703 LOG_WARN("Canvas height is out of range in VP8X chunk.");
704 }
705 if ((uint64_t)webp_info->canvas_width_ * webp_info->canvas_height_ >
706 MAX_IMAGE_AREA) {
707 LOG_WARN("Canvas area is out of range in VP8X chunk.");
708 }
709 return WEBP_INFO_OK;
710 }
711
712 static WebPInfoStatus ProcessANIMChunk(const ChunkData* const chunk_data,
713 WebPInfo* const webp_info) {
714 const uint8_t* data = chunk_data->payload_;
715 if (!webp_info->chunk_counts_[CHUNK_VP8X]) {
716 LOG_ERROR("ANIM chunk detected before VP8X chunk.");
717 return WEBP_INFO_PARSE_ERROR;
718 }
719 if (chunk_data->size_ != ANIM_CHUNK_SIZE + CHUNK_HEADER_SIZE) {
720 LOG_ERROR("Corrupted ANIM chunk.");
721 return WEBP_INFO_PARSE_ERROR;
722 }
723 webp_info->bgcolor_ = ReadLE32(&data);
724 webp_info->loop_count_ = ReadLE16(&data);
725 ++webp_info->chunk_counts_[CHUNK_ANIM];
726 if (!webp_info->quiet_) {
727 printf(" Background color:(ARGB) %02x %02x %02x %02x\n",
728 (webp_info->bgcolor_ >> 24) & 0xff,
729 (webp_info->bgcolor_ >> 16) & 0xff,
730 (webp_info->bgcolor_ >> 8) & 0xff,
731 webp_info->bgcolor_ & 0xff);
732 printf(" Loop count : %d\n", webp_info->loop_count_);
733 }
734 if (webp_info->loop_count_ > MAX_LOOP_COUNT) {
735 LOG_WARN("Loop count is out of range in ANIM chunk.");
736 }
737 return WEBP_INFO_OK;
738 }
739
740 static WebPInfoStatus ProcessANMFChunk(const ChunkData* const chunk_data,
741 WebPInfo* const webp_info) {
742 const uint8_t* data = chunk_data->payload_;
743 int offset_x, offset_y, width, height, duration, blend, dispose, temp;
744 if (webp_info->is_processing_anim_frame_) {
745 LOG_ERROR("ANMF chunk detected within another ANMF chunk.");
746 return WEBP_INFO_PARSE_ERROR;
747 }
748 if (!webp_info->chunk_counts_[CHUNK_ANIM]) {
749 LOG_ERROR("ANMF chunk detected before ANIM chunk.");
750 return WEBP_INFO_PARSE_ERROR;
751 }
752 if (chunk_data->size_ <= CHUNK_HEADER_SIZE + ANMF_CHUNK_SIZE) {
753 LOG_ERROR("Truncated data detected when parsing ANMF chunk.");
754 return WEBP_INFO_TRUNCATED_DATA;
755 }
756 offset_x = 2 * ReadLE24(&data);
757 offset_y = 2 * ReadLE24(&data);
758 width = 1 + ReadLE24(&data);
759 height = 1 + ReadLE24(&data);
760 duration = ReadLE24(&data);
761 temp = *data;
762 dispose = temp & 1;
763 blend = (temp >> 1) & 1;
764 ++webp_info->chunk_counts_[CHUNK_ANMF];
765 if (!webp_info->quiet_) {
766 printf(" Offset_X: %d\n Offset_Y: %d\n Width: %d\n Height: %d\n"
767 " Duration: %d\n Dispose: %d\n Blend: %d\n",
768 offset_x, offset_y, width, height, duration, dispose, blend);
769 }
770 if (duration > MAX_DURATION) {
771 LOG_ERROR("Invalid duration parameter in ANMF chunk.");
772 return WEBP_INFO_INVALID_PARAM;
773 }
774 if (offset_x > MAX_POSITION_OFFSET || offset_y > MAX_POSITION_OFFSET) {
775 LOG_ERROR("Invalid offset parameters in ANMF chunk.");
776 return WEBP_INFO_INVALID_PARAM;
777 }
778 if ((uint64_t)offset_x + width > (uint64_t)webp_info->canvas_width_ ||
779 (uint64_t)offset_y + height > (uint64_t)webp_info->canvas_height_) {
780 LOG_ERROR("Frame exceeds canvas in ANMF chunk.");
781 return WEBP_INFO_INVALID_PARAM;
782 }
783 webp_info->is_processing_anim_frame_ = 1;
784 webp_info->seen_alpha_subchunk_ = 0;
785 webp_info->seen_image_subchunk_ = 0;
786 webp_info->frame_width_ = width;
787 webp_info->frame_height_ = height;
788 webp_info->anim_frame_data_size_ =
789 chunk_data->size_ - CHUNK_HEADER_SIZE - ANMF_CHUNK_SIZE;
790 return WEBP_INFO_OK;
791 }
792
793 static WebPInfoStatus ProcessImageChunk(const ChunkData* const chunk_data,
794 WebPInfo* const webp_info) {
795 const uint8_t* data = chunk_data->payload_ - CHUNK_HEADER_SIZE;
796 WebPBitstreamFeatures features;
797 const VP8StatusCode vp8_status =
798 WebPGetFeatures(data, chunk_data->size_, &features);
799 if (vp8_status != VP8_STATUS_OK) {
800 LOG_ERROR("VP8/VP8L bitstream error.");
801 return WEBP_INFO_BITSTREAM_ERROR;
802 }
803 if (!webp_info->quiet_) {
804 assert(features.format >= 0 && features.format <= 2);
805 printf(" Width: %d\n Height: %d\n Alpha: %d\n Animation: %d\n"
806 " Format: %s (%d)\n",
807 features.width, features.height, features.has_alpha,
808 features.has_animation, kFormats[features.format], features.format);
809 }
810 if (webp_info->is_processing_anim_frame_) {
811 ++webp_info->anmf_subchunk_counts_[chunk_data->id_ == CHUNK_VP8 ? 0 : 1];
812 if (chunk_data->id_ == CHUNK_VP8L && webp_info->seen_alpha_subchunk_) {
813 LOG_ERROR("Both VP8L and ALPH sub-chunks are present in an ANMF chunk.");
814 return WEBP_INFO_PARSE_ERROR;
815 }
816 if (webp_info->frame_width_ != features.width ||
817 webp_info->frame_height_ != features.height) {
818 LOG_ERROR("Frame size in VP8/VP8L sub-chunk differs from ANMF header.");
819 return WEBP_INFO_PARSE_ERROR;
820 }
821 if (webp_info->seen_image_subchunk_) {
822 LOG_ERROR("Consecutive VP8/VP8L sub-chunks in an ANMF chunk.");
823 return WEBP_INFO_PARSE_ERROR;
824 }
825 webp_info->seen_image_subchunk_ = 1;
826 } else {
827 if (webp_info->chunk_counts_[CHUNK_VP8] ||
828 webp_info->chunk_counts_[CHUNK_VP8L]) {
829 LOG_ERROR("Multiple VP8/VP8L chunks detected.");
830 return WEBP_INFO_PARSE_ERROR;
831 }
832 if (chunk_data->id_ == CHUNK_VP8L &&
833 webp_info->chunk_counts_[CHUNK_ALPHA]) {
834 LOG_WARN("Both VP8L and ALPH chunks are detected.");
835 }
836 if (webp_info->chunk_counts_[CHUNK_ANIM] ||
837 webp_info->chunk_counts_[CHUNK_ANMF]) {
838 LOG_ERROR("VP8/VP8L chunk and ANIM/ANMF chunk are both detected.");
839 return WEBP_INFO_PARSE_ERROR;
840 }
841 if (webp_info->chunk_counts_[CHUNK_VP8X]) {
842 if (webp_info->canvas_width_ != features.width ||
843 webp_info->canvas_height_ != features.height) {
844 LOG_ERROR("Image size in VP8/VP8L chunk differs from VP8X chunk.");
845 return WEBP_INFO_PARSE_ERROR;
846 }
847 } else {
848 webp_info->canvas_width_ = features.width;
849 webp_info->canvas_height_ = features.height;
850 if (webp_info->canvas_width_ < 1 || webp_info->canvas_height_ < 1 ||
851 webp_info->canvas_width_ > MAX_CANVAS_SIZE ||
852 webp_info->canvas_height_ > MAX_CANVAS_SIZE ||
853 (uint64_t)webp_info->canvas_width_ * webp_info->canvas_height_ >
854 MAX_IMAGE_AREA) {
855 LOG_WARN("Invalid parameters in VP8/VP8L chunk.");
856 }
857 }
858 ++webp_info->chunk_counts_[chunk_data->id_];
859 }
860 ++webp_info->num_frames_;
861 webp_info->has_alpha_ |= features.has_alpha;
862 if (webp_info->parse_bitstream_) {
863 const int is_lossy = (chunk_data->id_ == CHUNK_VP8);
864 const WebPInfoStatus status =
865 is_lossy ? ParseLossyHeader(chunk_data, webp_info)
866 : ParseLosslessHeader(chunk_data, webp_info);
867 if (status != WEBP_INFO_OK) return status;
868 }
869 return WEBP_INFO_OK;
870 }
871
872 static WebPInfoStatus ProcessALPHChunk(const ChunkData* const chunk_data,
873 WebPInfo* const webp_info) {
874 if (webp_info->is_processing_anim_frame_) {
875 ++webp_info->anmf_subchunk_counts_[2];
876 if (webp_info->seen_alpha_subchunk_) {
877 LOG_ERROR("Consecutive ALPH sub-chunks in an ANMF chunk.");
878 return WEBP_INFO_PARSE_ERROR;
879 }
880 webp_info->seen_alpha_subchunk_ = 1;
881
882 if (webp_info->seen_image_subchunk_) {
883 LOG_ERROR("ALPHA sub-chunk detected after VP8 sub-chunk "
884 "in an ANMF chunk.");
885 return WEBP_INFO_PARSE_ERROR;
886 }
887 } else {
888 if (webp_info->chunk_counts_[CHUNK_ANIM] ||
889 webp_info->chunk_counts_[CHUNK_ANMF]) {
890 LOG_ERROR("ALPHA chunk and ANIM/ANMF chunk are both detected.");
891 return WEBP_INFO_PARSE_ERROR;
892 }
893 if (!webp_info->chunk_counts_[CHUNK_VP8X]) {
894 LOG_ERROR("ALPHA chunk detected before VP8X chunk.");
895 return WEBP_INFO_PARSE_ERROR;
896 }
897 if (webp_info->chunk_counts_[CHUNK_VP8]) {
898 LOG_ERROR("ALPHA chunk detected after VP8 chunk.");
899 return WEBP_INFO_PARSE_ERROR;
900 }
901 if (webp_info->chunk_counts_[CHUNK_ALPHA]) {
902 LOG_ERROR("Multiple ALPHA chunks detected.");
903 return WEBP_INFO_PARSE_ERROR;
904 }
905 ++webp_info->chunk_counts_[CHUNK_ALPHA];
906 }
907 webp_info->has_alpha_ = 1;
908 if (webp_info->parse_bitstream_) {
909 const WebPInfoStatus status = ParseAlphaHeader(chunk_data, webp_info);
910 if (status != WEBP_INFO_OK) return status;
911 }
912 return WEBP_INFO_OK;
913 }
914
915 static WebPInfoStatus ProcessICCPChunk(const ChunkData* const chunk_data,
916 WebPInfo* const webp_info) {
917 (void)chunk_data;
918 if (!webp_info->chunk_counts_[CHUNK_VP8X]) {
919 LOG_ERROR("ICCP chunk detected before VP8X chunk.");
920 return WEBP_INFO_PARSE_ERROR;
921 }
922 if (webp_info->chunk_counts_[CHUNK_VP8] ||
923 webp_info->chunk_counts_[CHUNK_VP8L] ||
924 webp_info->chunk_counts_[CHUNK_ANIM]) {
925 LOG_ERROR("ICCP chunk detected after image data.");
926 return WEBP_INFO_PARSE_ERROR;
927 }
928 ++webp_info->chunk_counts_[CHUNK_ICCP];
929 return WEBP_INFO_OK;
930 }
931
932 static WebPInfoStatus ProcessChunk(const ChunkData* const chunk_data,
933 WebPInfo* const webp_info) {
934 WebPInfoStatus status = WEBP_INFO_OK;
935 ChunkID id = chunk_data->id_;
936 if (chunk_data->id_ == CHUNK_UNKNOWN) {
937 char error_message[50];
938 snprintf(error_message, 50, "Unknown chunk at offset %6d, length %6d",
939 (int)chunk_data->offset_, (int)chunk_data->size_);
940 LOG_WARN(error_message);
941 } else {
942 if (!webp_info->quiet_) {
943 char tag[4];
944 uint32_t fourcc = kWebPChunkTags[chunk_data->id_];
945 #ifdef WORDS_BIGENDIAN
946 fourcc = (fourcc >> 24) | ((fourcc >> 8) & 0xff00) |
947 ((fourcc << 8) & 0xff0000) | (fourcc << 24);
948 #endif
949 memcpy(tag, &fourcc, sizeof(tag));
950 printf("Chunk %c%c%c%c at offset %6d, length %6d\n",
951 tag[0], tag[1], tag[2], tag[3], (int)chunk_data->offset_,
952 (int)chunk_data->size_);
953 }
954 }
955 switch (id) {
956 case CHUNK_VP8:
957 case CHUNK_VP8L:
958 status = ProcessImageChunk(chunk_data, webp_info);
959 break;
960 case CHUNK_VP8X:
961 status = ProcessVP8XChunk(chunk_data, webp_info);
962 break;
963 case CHUNK_ALPHA:
964 status = ProcessALPHChunk(chunk_data, webp_info);
965 break;
966 case CHUNK_ANIM:
967 status = ProcessANIMChunk(chunk_data, webp_info);
968 break;
969 case CHUNK_ANMF:
970 status = ProcessANMFChunk(chunk_data, webp_info);
971 break;
972 case CHUNK_ICCP:
973 status = ProcessICCPChunk(chunk_data, webp_info);
974 break;
975 case CHUNK_EXIF:
976 case CHUNK_XMP:
977 ++webp_info->chunk_counts_[id];
978 break;
979 case CHUNK_UNKNOWN:
980 default:
981 break;
982 }
983 if (webp_info->is_processing_anim_frame_ && id != CHUNK_ANMF) {
984 if (webp_info->anim_frame_data_size_ == chunk_data->size_) {
985 if (!webp_info->seen_image_subchunk_) {
986 LOG_ERROR("No VP8/VP8L chunk detected in an ANMF chunk.");
987 return WEBP_INFO_PARSE_ERROR;
988 }
989 webp_info->is_processing_anim_frame_ = 0;
990 } else if (webp_info->anim_frame_data_size_ > chunk_data->size_) {
991 webp_info->anim_frame_data_size_ -= chunk_data->size_;
992 } else {
993 LOG_ERROR("Truncated data detected when parsing ANMF chunk.");
994 return WEBP_INFO_TRUNCATED_DATA;
995 }
996 }
997 return status;
998 }
999
1000 static WebPInfoStatus Validate(WebPInfo* const webp_info) {
1001 if (webp_info->num_frames_ < 1) {
1002 LOG_ERROR("No image/frame detected.");
1003 return WEBP_INFO_MISSING_DATA;
1004 }
1005 if (webp_info->chunk_counts_[CHUNK_VP8X]) {
1006 const int iccp = !!(webp_info->feature_flags_ & ICCP_FLAG);
1007 const int exif = !!(webp_info->feature_flags_ & EXIF_FLAG);
1008 const int xmp = !!(webp_info->feature_flags_ & XMP_FLAG);
1009 const int animation = !!(webp_info->feature_flags_ & ANIMATION_FLAG);
1010 const int alpha = !!(webp_info->feature_flags_ & ALPHA_FLAG);
1011 if (!alpha && webp_info->has_alpha_) {
1012 LOG_ERROR("Unexpected alpha data detected.");
1013 return WEBP_INFO_PARSE_ERROR;
1014 }
1015 if (alpha && !webp_info->has_alpha_) {
1016 LOG_WARN("Alpha flag is set with no alpha data present.");
1017 }
1018 if (iccp && !webp_info->chunk_counts_[CHUNK_ICCP]) {
1019 LOG_ERROR("Missing ICCP chunk.");
1020 return WEBP_INFO_MISSING_DATA;
1021 }
1022 if (exif && !webp_info->chunk_counts_[CHUNK_EXIF]) {
1023 LOG_ERROR("Missing EXIF chunk.");
1024 return WEBP_INFO_MISSING_DATA;
1025 }
1026 if (xmp && !webp_info->chunk_counts_[CHUNK_XMP]) {
1027 LOG_ERROR("Missing XMP chunk.");
1028 return WEBP_INFO_MISSING_DATA;
1029 }
1030 if (!iccp && webp_info->chunk_counts_[CHUNK_ICCP]) {
1031 LOG_ERROR("Unexpected ICCP chunk detected.");
1032 return WEBP_INFO_PARSE_ERROR;
1033 }
1034 if (!exif && webp_info->chunk_counts_[CHUNK_EXIF]) {
1035 LOG_ERROR("Unexpected EXIF chunk detected.");
1036 return WEBP_INFO_PARSE_ERROR;
1037 }
1038 if (!xmp && webp_info->chunk_counts_[CHUNK_XMP]) {
1039 LOG_ERROR("Unexpected XMP chunk detected.");
1040 return WEBP_INFO_PARSE_ERROR;
1041 }
1042 // Incomplete animation frame.
1043 if (webp_info->is_processing_anim_frame_) return WEBP_INFO_MISSING_DATA;
1044 if (!animation && webp_info->num_frames_ > 1) {
1045 LOG_ERROR("More than 1 frame detected in non-animation file.");
1046 return WEBP_INFO_PARSE_ERROR;
1047 }
1048 if (animation && (!webp_info->chunk_counts_[CHUNK_ANIM] ||
1049 !webp_info->chunk_counts_[CHUNK_ANMF])) {
1050 LOG_ERROR("No ANIM/ANMF chunk detected in animation file.");
1051 return WEBP_INFO_PARSE_ERROR;
1052 }
1053 }
1054 return WEBP_INFO_OK;
1055 }
1056
1057 static void ShowSummary(const WebPInfo* const webp_info) {
1058 int i;
1059 printf("Summary:\n");
1060 printf("Number of frames: %d\n", webp_info->num_frames_);
1061 printf("Chunk type : VP8 VP8L VP8X ALPH ANIM ANMF(VP8 /VP8L/ALPH) ICCP "
1062 "EXIF XMP\n");
1063 printf("Chunk counts: ");
1064 for (i = 0; i < CHUNK_TYPES; ++i) {
1065 printf("%4d ", webp_info->chunk_counts_[i]);
1066 if (i == CHUNK_ANMF) {
1067 printf("%4d %4d %4d ",
1068 webp_info->anmf_subchunk_counts_[0],
1069 webp_info->anmf_subchunk_counts_[1],
1070 webp_info->anmf_subchunk_counts_[2]);
1071 }
1072 }
1073 printf("\n");
1074 }
1075
1076 static WebPInfoStatus AnalyzeWebP(WebPInfo* const webp_info,
1077 const WebPData* webp_data) {
1078 ChunkData chunk_data;
1079 MemBuffer mem_buffer;
1080 WebPInfoStatus webp_info_status = WEBP_INFO_OK;
1081
1082 InitMemBuffer(&mem_buffer, webp_data);
1083 webp_info_status = ParseRIFFHeader(webp_info, &mem_buffer);
1084 if (webp_info_status != WEBP_INFO_OK) goto Error;
1085
1086 // Loop through all the chunks. Terminate immediately in case of error.
1087 while (webp_info_status == WEBP_INFO_OK && MemDataSize(&mem_buffer) > 0) {
1088 webp_info_status = ParseChunk(webp_info, &mem_buffer, &chunk_data);
1089 if (webp_info_status != WEBP_INFO_OK) goto Error;
1090 webp_info_status = ProcessChunk(&chunk_data, webp_info);
1091 }
1092 if (webp_info_status != WEBP_INFO_OK) goto Error;
1093 if (webp_info->show_summary_) ShowSummary(webp_info);
1094
1095 // Final check.
1096 webp_info_status = Validate(webp_info);
1097
1098 Error:
1099 if (!webp_info->quiet_) {
1100 if (webp_info_status == WEBP_INFO_OK) {
1101 printf("No error detected.\n");
1102 } else {
1103 printf("Errors detected.\n");
1104 }
1105 if (webp_info->num_warnings_ > 0) {
1106 printf("There were %d warning(s).\n", webp_info->num_warnings_);
1107 }
1108 }
1109 return webp_info_status;
1110 }
1111
1112 static void Help(void) {
1113 printf("Usage: webpinfo [options] in_files\n"
1114 "Note: there could be multiple input files;\n"
1115 " options must come before input files.\n"
1116 "Options:\n"
1117 " -version ........... Print version number and exit.\n"
1118 " -quiet ............. Do not show chunk parsing information.\n"
1119 " -diag .............. Show parsing error diagnosis.\n"
1120 " -summary ........... Show chunk stats summary.\n"
1121 " -bitstream_info .... Parse bitstream header.\n");
1122 }
1123
1124 // Returns EXIT_SUCCESS on success, EXIT_FAILURE on failure.
1125 int main(int argc, const char* argv[]) {
1126 int c, quiet = 0, show_diag = 0, show_summary = 0;
1127 int parse_bitstream = 0;
1128 WebPInfoStatus webp_info_status = WEBP_INFO_OK;
1129 WebPInfo webp_info;
1130
1131 INIT_WARGV(argc, argv);
1132
1133 if (argc == 1) {
1134 Help();
1135 FREE_WARGV_AND_RETURN(EXIT_FAILURE);
1136 }
1137
1138 // Parse command-line input.
1139 for (c = 1; c < argc; ++c) {
1140 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help") ||
1141 !strcmp(argv[c], "-H") || !strcmp(argv[c], "-longhelp")) {
1142 Help();
1143 FREE_WARGV_AND_RETURN(EXIT_SUCCESS);
1144 } else if (!strcmp(argv[c], "-quiet")) {
1145 quiet = 1;
1146 } else if (!strcmp(argv[c], "-diag")) {
1147 show_diag = 1;
1148 } else if (!strcmp(argv[c], "-summary")) {
1149 show_summary = 1;
1150 } else if (!strcmp(argv[c], "-bitstream_info")) {
1151 parse_bitstream = 1;
1152 } else if (!strcmp(argv[c], "-version")) {
1153 const int version = WebPGetDecoderVersion();
1154 printf("WebP Decoder version: %d.%d.%d\n",
1155 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
1156 FREE_WARGV_AND_RETURN(EXIT_SUCCESS);
1157 } else { // Assume the remaining are all input files.
1158 break;
1159 }
1160 }
1161
1162 if (c == argc) {
1163 Help();
1164 FREE_WARGV_AND_RETURN(EXIT_FAILURE);
1165 }
1166
1167 // Process input files one by one.
1168 for (; c < argc; ++c) {
1169 WebPData webp_data;
1170 const W_CHAR* in_file = NULL;
1171 WebPInfoInit(&webp_info);
1172 webp_info.quiet_ = quiet;
1173 webp_info.show_diagnosis_ = show_diag;
1174 webp_info.show_summary_ = show_summary;
1175 webp_info.parse_bitstream_ = parse_bitstream;
1176 in_file = GET_WARGV(argv, c);
1177 if (in_file == NULL ||
1178 !ReadFileToWebPData((const char*)in_file, &webp_data)) {
1179 webp_info_status = WEBP_INFO_INVALID_COMMAND;
1180 WFPRINTF(stderr, "Failed to open input file %s.\n", in_file);
1181 continue;
1182 }
1183 if (!webp_info.quiet_) WPRINTF("File: %s\n", in_file);
1184 webp_info_status = AnalyzeWebP(&webp_info, &webp_data);
1185 WebPDataClear(&webp_data);
1186 }
1187 FREE_WARGV_AND_RETURN((webp_info_status == WEBP_INFO_OK) ? EXIT_SUCCESS
1188 : EXIT_FAILURE);
1189 }