blob: fe932ec317308fd1c0a6fd15d75b279e2a2cf59b [file] [log] [blame]
James Zernad1e1632012-01-06 14:49:06 -08001// Copyright 2011 Google Inc. All Rights Reserved.
Pascal Massimino7937b402011-12-13 14:02:04 -08002//
James Zernd6406142013-06-06 23:05:58 -07003// 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.
Pascal Massimino7937b402011-12-13 14:02:04 -08008// -----------------------------------------------------------------------------
9//
Pascal Massiminoa4e1cdb2013-04-01 23:10:35 +000010// Simple OpenGL-based WebP file viewer.
Pascal Massimino7937b402011-12-13 14:02:04 -080011//
12// Author: Skal (pascal.massimino@gmail.com)
James Zern0e513f72013-05-01 14:47:56 -070013#ifdef HAVE_CONFIG_H
James Zern32b31372014-06-10 17:53:44 -070014#include "webp/config.h"
James Zern0e513f72013-05-01 14:47:56 -070015#endif
Pascal Massimino7937b402011-12-13 14:02:04 -080016
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
Pascal Massimino79ff0342013-09-12 04:03:51 -070021#if defined(WEBP_HAVE_GL)
22
James Zern0e513f72013-05-01 14:47:56 -070023#if defined(HAVE_GLUT_GLUT_H)
Pascal Massimino7937b402011-12-13 14:02:04 -080024#include <GLUT/glut.h>
25#else
26#include <GL/glut.h>
27#ifdef FREEGLUT
28#include <GL/freeglut.h>
29#endif
30#endif
31
James Zernddfee5d2013-03-07 19:31:27 -080032#ifdef WEBP_HAVE_QCMS
33#include <qcms.h>
34#endif
35
36#include "webp/decode.h"
37#include "webp/demux.h"
38
James Zern061263a2012-05-11 16:00:57 -070039#include "./example_util.h"
40
James Zern91179542011-12-16 19:30:33 -080041#ifdef _MSC_VER
42#define snprintf _snprintf
43#endif
44
Pascal Massimino861a5b72012-05-09 00:41:12 -070045static void Help(void);
46
47// Unfortunate global variables. Gathered into a struct for comfort.
48static struct {
49 int has_animation;
James Zernddfee5d2013-03-07 19:31:27 -080050 int has_color_profile;
Pascal Massimino861a5b72012-05-09 00:41:12 -070051 int done;
52 int decoding_error;
53 int print_info;
James Zernddfee5d2013-03-07 19:31:27 -080054 int use_color_profile;
Pascal Massimino861a5b72012-05-09 00:41:12 -070055
skal68f282f2012-11-15 09:15:04 +010056 int canvas_width, canvas_height;
Urvang Joshi6808e692012-07-06 11:35:36 +053057 int loop_count;
skal68f282f2012-11-15 09:15:04 +010058 uint32_t bg_color;
Pascal Massimino861a5b72012-05-09 00:41:12 -070059
60 const char* file_name;
61 WebPData data;
skald51f45f2013-09-12 09:32:28 +020062 WebPDecoderConfig config;
Pascal Massimino861a5b72012-05-09 00:41:12 -070063 const WebPDecBuffer* pic;
James Zernd7a5ac82012-09-26 23:44:59 -070064 WebPDemuxer* dmux;
Urvang Joshidcf65222013-08-09 14:40:31 -070065 WebPIterator curr_frame;
66 WebPIterator prev_frame;
James Zernddfee5d2013-03-07 19:31:27 -080067 WebPChunkIterator iccp;
James Zernd7a5ac82012-09-26 23:44:59 -070068} kParams;
Pascal Massimino861a5b72012-05-09 00:41:12 -070069
70static void ClearPreviousPic(void) {
71 WebPFreeDecBuffer((WebPDecBuffer*)kParams.pic);
72 kParams.pic = NULL;
73}
74
Pascal Massimino861a5b72012-05-09 00:41:12 -070075static void ClearParams(void) {
76 ClearPreviousPic();
Urvang Joshif3bab8e2012-06-07 17:19:02 +053077 WebPDataClear(&kParams.data);
Urvang Joshidcf65222013-08-09 14:40:31 -070078 WebPDemuxReleaseIterator(&kParams.curr_frame);
79 WebPDemuxReleaseIterator(&kParams.prev_frame);
James Zernddfee5d2013-03-07 19:31:27 -080080 WebPDemuxReleaseChunkIterator(&kParams.iccp);
James Zernd7a5ac82012-09-26 23:44:59 -070081 WebPDemuxDelete(kParams.dmux);
82 kParams.dmux = NULL;
Pascal Massimino861a5b72012-05-09 00:41:12 -070083}
Pascal Massimino7937b402011-12-13 14:02:04 -080084
James Zernddfee5d2013-03-07 19:31:27 -080085// -----------------------------------------------------------------------------
86// Color profile handling
87static int ApplyColorProfile(const WebPData* const profile,
88 WebPDecBuffer* const rgba) {
89#ifdef WEBP_HAVE_QCMS
90 int i, ok = 0;
91 uint8_t* line;
92 uint8_t major_revision;
93 qcms_profile* input_profile = NULL;
94 qcms_profile* output_profile = NULL;
95 qcms_transform* transform = NULL;
96 const qcms_data_type input_type = QCMS_DATA_RGBA_8;
97 const qcms_data_type output_type = QCMS_DATA_RGBA_8;
98 const qcms_intent intent = QCMS_INTENT_DEFAULT;
99
100 if (profile == NULL || rgba == NULL) return 0;
101 if (profile->bytes == NULL || profile->size < 10) return 1;
102 major_revision = profile->bytes[8];
103
104 qcms_enable_iccv4();
105 input_profile = qcms_profile_from_memory(profile->bytes, profile->size);
106 // qcms_profile_is_bogus() is broken with ICCv4.
107 if (input_profile == NULL ||
108 (major_revision < 4 && qcms_profile_is_bogus(input_profile))) {
109 fprintf(stderr, "Color profile is bogus!\n");
110 goto Error;
111 }
112
113 output_profile = qcms_profile_sRGB();
114 if (output_profile == NULL) {
115 fprintf(stderr, "Error creating output color profile!\n");
116 goto Error;
117 }
118
119 qcms_profile_precache_output_transform(output_profile);
120 transform = qcms_transform_create(input_profile, input_type,
121 output_profile, output_type,
122 intent);
123 if (transform == NULL) {
124 fprintf(stderr, "Error creating color transform!\n");
125 goto Error;
126 }
127
128 line = rgba->u.RGBA.rgba;
129 for (i = 0; i < rgba->height; ++i, line += rgba->u.RGBA.stride) {
130 qcms_transform_data(transform, line, line, rgba->width);
131 }
132 ok = 1;
133
134 Error:
135 if (input_profile != NULL) qcms_profile_release(input_profile);
136 if (output_profile != NULL) qcms_profile_release(output_profile);
137 if (transform != NULL) qcms_transform_release(transform);
138 return ok;
139#else
140 (void)profile;
141 (void)rgba;
142 return 1;
143#endif // WEBP_HAVE_QCMS
144}
145
146//------------------------------------------------------------------------------
147// File decoding
148
Urvang Joshidcf65222013-08-09 14:40:31 -0700149static int Decode(void) { // Fills kParams.curr_frame
150 const WebPIterator* const curr = &kParams.curr_frame;
skald51f45f2013-09-12 09:32:28 +0200151 WebPDecoderConfig* const config = &kParams.config;
James Zernddfee5d2013-03-07 19:31:27 -0800152 WebPDecBuffer* const output_buffer = &config->output;
153 int ok = 0;
154
155 ClearPreviousPic();
156 output_buffer->colorspace = MODE_RGBA;
Urvang Joshidcf65222013-08-09 14:40:31 -0700157 ok = (WebPDecode(curr->fragment.bytes, curr->fragment.size,
James Zernddfee5d2013-03-07 19:31:27 -0800158 config) == VP8_STATUS_OK);
159 if (!ok) {
Urvang Joshidcf65222013-08-09 14:40:31 -0700160 fprintf(stderr, "Decoding of frame #%d failed!\n", curr->frame_num);
James Zernddfee5d2013-03-07 19:31:27 -0800161 } else {
162 kParams.pic = output_buffer;
163 if (kParams.use_color_profile) {
164 ok = ApplyColorProfile(&kParams.iccp.chunk, output_buffer);
165 if (!ok) {
166 fprintf(stderr, "Applying color profile to frame #%d failed!\n",
Urvang Joshidcf65222013-08-09 14:40:31 -0700167 curr->frame_num);
James Zernddfee5d2013-03-07 19:31:27 -0800168 }
169 }
170 }
171 return ok;
172}
173
174static void decode_callback(int what) {
175 if (what == 0 && !kParams.done) {
176 int duration = 0;
177 if (kParams.dmux != NULL) {
Urvang Joshidcf65222013-08-09 14:40:31 -0700178 WebPIterator* const curr = &kParams.curr_frame;
179 if (!WebPDemuxNextFrame(curr)) {
180 WebPDemuxReleaseIterator(curr);
181 if (WebPDemuxGetFrame(kParams.dmux, 1, curr)) {
James Zernddfee5d2013-03-07 19:31:27 -0800182 --kParams.loop_count;
183 kParams.done = (kParams.loop_count == 0);
James Zern0f017b52015-02-02 20:05:54 -0800184 if (kParams.done) return;
James Zernddfee5d2013-03-07 19:31:27 -0800185 } else {
186 kParams.decoding_error = 1;
187 kParams.done = 1;
188 return;
189 }
190 }
Urvang Joshidcf65222013-08-09 14:40:31 -0700191 duration = curr->duration;
James Zernddfee5d2013-03-07 19:31:27 -0800192 }
193 if (!Decode()) {
194 kParams.decoding_error = 1;
195 kParams.done = 1;
196 } else {
197 glutPostRedisplay();
198 glutTimerFunc(duration, decode_callback, what);
199 }
200 }
201}
202
Pascal Massimino7937b402011-12-13 14:02:04 -0800203//------------------------------------------------------------------------------
204// Callbacks
205
206static void HandleKey(unsigned char key, int pos_x, int pos_y) {
207 (void)pos_x;
208 (void)pos_y;
209 if (key == 'q' || key == 'Q' || key == 27 /* Esc */) {
210#ifdef FREEGLUT
211 glutLeaveMainLoop();
212#else
Pascal Massimino861a5b72012-05-09 00:41:12 -0700213 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800214 exit(0);
215#endif
James Zernddfee5d2013-03-07 19:31:27 -0800216 } else if (key == 'c') {
217 if (kParams.has_color_profile && !kParams.decoding_error) {
218 kParams.use_color_profile = 1 - kParams.use_color_profile;
219
220 if (kParams.has_animation) {
221 // Restart the completed animation to pickup the color profile change.
222 if (kParams.done && kParams.loop_count == 0) {
223 kParams.loop_count =
224 (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT) + 1;
225 kParams.done = 0;
226 // Start the decode loop immediately.
227 glutTimerFunc(0, decode_callback, 0);
228 }
229 } else {
230 Decode();
231 glutPostRedisplay();
232 }
233 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800234 } else if (key == 'i') {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700235 kParams.print_info = 1 - kParams.print_info;
Pascal Massimino7937b402011-12-13 14:02:04 -0800236 glutPostRedisplay();
237 }
238}
239
240static void HandleReshape(int width, int height) {
241 // TODO(skal): proper handling of resize, esp. for large pictures.
242 // + key control of the zoom.
243 glViewport(0, 0, width, height);
244 glMatrixMode(GL_PROJECTION);
245 glLoadIdentity();
246 glMatrixMode(GL_MODELVIEW);
247 glLoadIdentity();
248}
249
250static void PrintString(const char* const text) {
251 void* const font = GLUT_BITMAP_9_BY_15;
252 int i;
253 for (i = 0; text[i]; ++i) {
254 glutBitmapCharacter(font, text[i]);
255 }
256}
257
skal68f282f2012-11-15 09:15:04 +0100258static float GetColorf(uint32_t color, int shift) {
259 return (color >> shift) / 255.f;
260}
261
James Zerna73b8972012-07-09 23:01:52 -0700262static void DrawCheckerBoard(void) {
263 const int square_size = 8; // must be a power of 2
264 int x, y;
265 GLint viewport[4]; // x, y, width, height
266
267 glPushMatrix();
268
269 glGetIntegerv(GL_VIEWPORT, viewport);
270 // shift to integer coordinates with (0,0) being top-left.
271 glOrtho(0, viewport[2], viewport[3], 0, -1, 1);
272 for (y = 0; y < viewport[3]; y += square_size) {
273 for (x = 0; x < viewport[2]; x += square_size) {
274 const GLubyte color = 128 + 64 * (!((x + y) & square_size));
275 glColor3ub(color, color, color);
276 glRecti(x, y, x + square_size, y + square_size);
277 }
278 }
279 glPopMatrix();
280}
281
Pascal Massimino7937b402011-12-13 14:02:04 -0800282static void HandleDisplay(void) {
skal68f282f2012-11-15 09:15:04 +0100283 const WebPDecBuffer* const pic = kParams.pic;
Urvang Joshidcf65222013-08-09 14:40:31 -0700284 const WebPIterator* const curr = &kParams.curr_frame;
285 WebPIterator* const prev = &kParams.prev_frame;
skal41a6ced2012-11-15 09:35:01 +0100286 GLfloat xoff, yoff;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700287 if (pic == NULL) return;
Pascal Massimino7937b402011-12-13 14:02:04 -0800288 glPushMatrix();
289 glPixelZoom(1, -1);
Urvang Joshidcf65222013-08-09 14:40:31 -0700290 xoff = (GLfloat)(2. * curr->x_offset / kParams.canvas_width);
291 yoff = (GLfloat)(2. * curr->y_offset / kParams.canvas_height);
James Zern013023e2013-03-15 12:17:45 -0700292 glRasterPos2f(-1.f + xoff, 1.f - yoff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800293 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700294 glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4);
James Zern04c7a2e2013-03-23 12:45:11 -0700295
Urvang Joshidcf65222013-08-09 14:40:31 -0700296 if (prev->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ||
297 curr->blend_method == WEBP_MUX_NO_BLEND) {
James Zern04c7a2e2013-03-23 12:45:11 -0700298 // TODO(later): these offsets and those above should factor in window size.
299 // they will be incorrect if the window is resized.
300 // glScissor() takes window coordinates (0,0 at bottom left).
Urvang Joshidcf65222013-08-09 14:40:31 -0700301 int window_x, window_y;
302 if (prev->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {
303 // Clear the previous frame rectangle.
304 window_x = prev->x_offset;
305 window_y = kParams.canvas_height - prev->y_offset - prev->height;
306 } else { // curr->blend_method == WEBP_MUX_NO_BLEND.
307 // We simulate no-blending behavior by first clearing the current frame
308 // rectangle (to a checker-board) and then alpha-blending against it.
309 window_x = curr->x_offset;
310 window_y = kParams.canvas_height - curr->y_offset - curr->height;
311 }
James Zern04c7a2e2013-03-23 12:45:11 -0700312 glEnable(GL_SCISSOR_TEST);
Urvang Joshidcf65222013-08-09 14:40:31 -0700313 // Only update the requested area, not the whole canvas.
314 glScissor(window_x, window_y, prev->width, prev->height);
James Zern04c7a2e2013-03-23 12:45:11 -0700315
skal68f282f2012-11-15 09:15:04 +0100316 glClear(GL_COLOR_BUFFER_BIT); // use clear color
James Zern04c7a2e2013-03-23 12:45:11 -0700317 DrawCheckerBoard();
318
319 glDisable(GL_SCISSOR_TEST);
skal68f282f2012-11-15 09:15:04 +0100320 }
Urvang Joshidcf65222013-08-09 14:40:31 -0700321
322 *prev = *curr;
James Zern04c7a2e2013-03-23 12:45:11 -0700323
Pascal Massimino861a5b72012-05-09 00:41:12 -0700324 glDrawPixels(pic->width, pic->height,
325 GL_RGBA, GL_UNSIGNED_BYTE,
326 (GLvoid*)pic->u.RGBA.rgba);
327 if (kParams.print_info) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800328 char tmp[32];
329
James Zern013023e2013-03-15 12:17:45 -0700330 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800331 glRasterPos2f(-0.95f, 0.90f);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700332 PrintString(kParams.file_name);
Pascal Massimino7937b402011-12-13 14:02:04 -0800333
Pascal Massimino861a5b72012-05-09 00:41:12 -0700334 snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height);
James Zern013023e2013-03-15 12:17:45 -0700335 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800336 glRasterPos2f(-0.95f, 0.80f);
Pascal Massimino7937b402011-12-13 14:02:04 -0800337 PrintString(tmp);
Urvang Joshidcf65222013-08-09 14:40:31 -0700338 if (curr->x_offset != 0 || curr->y_offset != 0) {
skal68f282f2012-11-15 09:15:04 +0100339 snprintf(tmp, sizeof(tmp), " (offset:%d,%d)",
Urvang Joshidcf65222013-08-09 14:40:31 -0700340 curr->x_offset, curr->y_offset);
skal68f282f2012-11-15 09:15:04 +0100341 glRasterPos2f(-0.95f, 0.70f);
342 PrintString(tmp);
343 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800344 }
James Zerna73b8972012-07-09 23:01:52 -0700345 glPopMatrix();
Pascal Massimino7937b402011-12-13 14:02:04 -0800346 glFlush();
347}
348
skal68f282f2012-11-15 09:15:04 +0100349static void StartDisplay(void) {
350 const int width = kParams.canvas_width;
351 const int height = kParams.canvas_height;
Pascal Massimino7937b402011-12-13 14:02:04 -0800352 glutInitDisplayMode(GLUT_RGBA);
skal68f282f2012-11-15 09:15:04 +0100353 glutInitWindowSize(width, height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800354 glutCreateWindow("WebP viewer");
Pascal Massimino7937b402011-12-13 14:02:04 -0800355 glutDisplayFunc(HandleDisplay);
356 glutIdleFunc(NULL);
357 glutKeyboardFunc(HandleKey);
Urvang Joshi08220102012-06-20 11:18:35 -0700358 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
359 glEnable(GL_BLEND);
skal68f282f2012-11-15 09:15:04 +0100360 glClearColor(GetColorf(kParams.bg_color, 0),
361 GetColorf(kParams.bg_color, 8),
362 GetColorf(kParams.bg_color, 16),
363 GetColorf(kParams.bg_color, 24));
364 HandleReshape(width, height);
365 glClear(GL_COLOR_BUFFER_BIT);
366 DrawCheckerBoard();
Pascal Massimino7937b402011-12-13 14:02:04 -0800367}
368
369//------------------------------------------------------------------------------
Pascal Massimino7937b402011-12-13 14:02:04 -0800370// Main
371
372static void Help(void) {
373 printf("Usage: vwebp in_file [options]\n\n"
374 "Decodes the WebP image file and visualize it using OpenGL\n"
375 "Options are:\n"
skal0a8b8862014-06-18 16:45:34 +0200376 " -version .... print version number and exit\n"
377 " -noicc ....... don't use the icc profile if present\n"
378 " -nofancy ..... don't use the fancy YUV420 upscaler\n"
379 " -nofilter .... disable in-loop filtering\n"
380 " -dither <int> dithering strength (0..100), default=50\n"
381 " -noalphadither disable alpha plane dithering\n"
382 " -mt .......... use multi-threading\n"
383 " -info ........ print info\n"
384 " -h ....... this help message\n"
James Zern03cc23d2013-03-07 19:10:59 -0800385 "\n"
386 "Keyboard shortcuts:\n"
skal0a8b8862014-06-18 16:45:34 +0200387 " 'c' ................ toggle use of color profile\n"
388 " 'i' ................ overlay file information\n"
389 " 'q' / 'Q' / ESC .... quit\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800390 );
391}
392
393int main(int argc, char *argv[]) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800394 int c;
skald51f45f2013-09-12 09:32:28 +0200395 WebPDecoderConfig* const config = &kParams.config;
Pascal Massimino14dd5e72013-08-15 00:21:15 -0700396 WebPIterator* const curr = &kParams.curr_frame;
Urvang Joshidcf65222013-08-09 14:40:31 -0700397 WebPIterator* const prev = &kParams.prev_frame;
Pascal Massimino7937b402011-12-13 14:02:04 -0800398
skald51f45f2013-09-12 09:32:28 +0200399 if (!WebPInitDecoderConfig(config)) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800400 fprintf(stderr, "Library version mismatch!\n");
401 return -1;
402 }
skalcbdd3e62013-11-26 22:59:02 +0100403 config->options.dithering_strength = 50;
skalbbe32df2014-06-14 00:06:16 +0200404 config->options.alpha_dithering_strength = 100;
James Zernddfee5d2013-03-07 19:31:27 -0800405 kParams.use_color_profile = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800406
407 for (c = 1; c < argc; ++c) {
James Zern96d43a82014-09-10 23:35:48 -0700408 int parse_error = 0;
Pascal Massimino7937b402011-12-13 14:02:04 -0800409 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
410 Help();
411 return 0;
James Zernddfee5d2013-03-07 19:31:27 -0800412 } else if (!strcmp(argv[c], "-noicc")) {
413 kParams.use_color_profile = 0;
Pascal Massimino7937b402011-12-13 14:02:04 -0800414 } else if (!strcmp(argv[c], "-nofancy")) {
skald51f45f2013-09-12 09:32:28 +0200415 config->options.no_fancy_upsampling = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800416 } else if (!strcmp(argv[c], "-nofilter")) {
skald51f45f2013-09-12 09:32:28 +0200417 config->options.bypass_filtering = 1;
skalbbe32df2014-06-14 00:06:16 +0200418 } else if (!strcmp(argv[c], "-noalphadither")) {
419 config->options.alpha_dithering_strength = 0;
skalcbdd3e62013-11-26 22:59:02 +0100420 } else if (!strcmp(argv[c], "-dither") && c + 1 < argc) {
James Zern96d43a82014-09-10 23:35:48 -0700421 config->options.dithering_strength =
422 ExUtilGetInt(argv[++c], 0, &parse_error);
skal68f282f2012-11-15 09:15:04 +0100423 } else if (!strcmp(argv[c], "-info")) {
424 kParams.print_info = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800425 } else if (!strcmp(argv[c], "-version")) {
Urvang Joshia5042a32013-02-26 14:22:06 -0800426 const int dec_version = WebPGetDecoderVersion();
427 const int dmux_version = WebPGetDemuxVersion();
428 printf("WebP Decoder version: %d.%d.%d\nWebP Demux version: %d.%d.%d\n",
429 (dec_version >> 16) & 0xff, (dec_version >> 8) & 0xff,
430 dec_version & 0xff, (dmux_version >> 16) & 0xff,
431 (dmux_version >> 8) & 0xff, dmux_version & 0xff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800432 return 0;
433 } else if (!strcmp(argv[c], "-mt")) {
skald51f45f2013-09-12 09:32:28 +0200434 config->options.use_threads = 1;
James Zerna4b0aa02013-12-12 20:20:08 -0800435 } else if (!strcmp(argv[c], "--")) {
436 if (c < argc - 1) kParams.file_name = argv[++c];
437 break;
Pascal Massimino7937b402011-12-13 14:02:04 -0800438 } else if (argv[c][0] == '-') {
439 printf("Unknown option '%s'\n", argv[c]);
440 Help();
441 return -1;
442 } else {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700443 kParams.file_name = argv[c];
Pascal Massimino7937b402011-12-13 14:02:04 -0800444 }
James Zern96d43a82014-09-10 23:35:48 -0700445
446 if (parse_error) {
447 Help();
448 return -1;
449 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800450 }
451
James Zern061263a2012-05-11 16:00:57 -0700452 if (kParams.file_name == NULL) {
453 printf("missing input file!!\n");
454 Help();
455 return 0;
456 }
457
458 if (!ExUtilReadFile(kParams.file_name,
Urvang Joshia0770722012-10-30 14:54:46 -0700459 &kParams.data.bytes, &kParams.data.size)) {
James Zern061263a2012-05-11 16:00:57 -0700460 goto Error;
461 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700462
Pascal Massimino830f72b2013-06-10 05:46:22 -0700463 if (!WebPGetInfo(kParams.data.bytes, kParams.data.size, NULL, NULL)) {
464 fprintf(stderr, "Input file doesn't appear to be WebP format.\n");
465 goto Error;
466 }
467
James Zernd7a5ac82012-09-26 23:44:59 -0700468 kParams.dmux = WebPDemux(&kParams.data);
469 if (kParams.dmux == NULL) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700470 fprintf(stderr, "Could not create demuxing object!\n");
471 goto Error;
Pascal Massimino7937b402011-12-13 14:02:04 -0800472 }
473
Urvang Joshia00a3da2012-10-31 17:49:15 -0700474 if (WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & FRAGMENTS_FLAG) {
475 fprintf(stderr, "Image fragments are not supported for now!\n");
Pascal Massimino861a5b72012-05-09 00:41:12 -0700476 goto Error;
477 }
skal68f282f2012-11-15 09:15:04 +0100478 kParams.canvas_width = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_WIDTH);
479 kParams.canvas_height = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_HEIGHT);
480 if (kParams.print_info) {
481 printf("Canvas: %d x %d\n", kParams.canvas_width, kParams.canvas_height);
482 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800483
Urvang Joshidcf65222013-08-09 14:40:31 -0700484 prev->width = kParams.canvas_width;
485 prev->height = kParams.canvas_height;
486 prev->x_offset = prev->y_offset = 0;
487 prev->dispose_method = WEBP_MUX_DISPOSE_BACKGROUND;
James Zern04c7a2e2013-03-23 12:45:11 -0700488
James Zernddfee5d2013-03-07 19:31:27 -0800489 memset(&kParams.iccp, 0, sizeof(kParams.iccp));
490 kParams.has_color_profile =
491 !!(WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & ICCP_FLAG);
492 if (kParams.has_color_profile) {
493#ifdef WEBP_HAVE_QCMS
494 if (!WebPDemuxGetChunk(kParams.dmux, "ICCP", 1, &kParams.iccp)) goto Error;
495 printf("VP8X: Found color profile\n");
496#else
497 fprintf(stderr, "Warning: color profile present, but qcms is unavailable!\n"
498 "Build libqcms from Mozilla or Chromium and define WEBP_HAVE_QCMS "
499 "before building.\n");
500#endif
501 }
502
Urvang Joshidcf65222013-08-09 14:40:31 -0700503 if (!WebPDemuxGetFrame(kParams.dmux, 1, curr)) goto Error;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700504
Urvang Joshidcf65222013-08-09 14:40:31 -0700505 kParams.has_animation = (curr->num_frames > 1);
James Zernd7a5ac82012-09-26 23:44:59 -0700506 kParams.loop_count = (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT);
skal68f282f2012-11-15 09:15:04 +0100507 kParams.bg_color = WebPDemuxGetI(kParams.dmux, WEBP_FF_BACKGROUND_COLOR);
James Zernd7a5ac82012-09-26 23:44:59 -0700508 printf("VP8X: Found %d images in file (loop count = %d)\n",
Urvang Joshidcf65222013-08-09 14:40:31 -0700509 curr->num_frames, kParams.loop_count);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700510
511 // Decode first frame
skal68f282f2012-11-15 09:15:04 +0100512 if (!Decode()) goto Error;
513
514 // Position iterator to last frame. Next call to HandleDisplay will wrap over.
515 // We take this into account by bumping up loop_count.
Urvang Joshidcf65222013-08-09 14:40:31 -0700516 WebPDemuxGetFrame(kParams.dmux, 0, curr);
skal68f282f2012-11-15 09:15:04 +0100517 if (kParams.loop_count) ++kParams.loop_count;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700518
519 // Start display (and timer)
Pascal Massimino7937b402011-12-13 14:02:04 -0800520 glutInit(&argc, argv);
James Zern880fd982012-05-25 14:53:46 -0700521#ifdef FREEGLUT
522 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
523#endif
skal68f282f2012-11-15 09:15:04 +0100524 StartDisplay();
525
Pascal Massimino861a5b72012-05-09 00:41:12 -0700526 if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0);
527 glutMainLoop();
Pascal Massimino7937b402011-12-13 14:02:04 -0800528
529 // Should only be reached when using FREEGLUT:
Pascal Massimino861a5b72012-05-09 00:41:12 -0700530 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800531 return 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700532
533 Error:
534 ClearParams();
535 return -1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800536}
537
Pascal Massimino79ff0342013-09-12 04:03:51 -0700538#else // !WEBP_HAVE_GL
539
540int main(int argc, const char *argv[]) {
541 fprintf(stderr, "OpenGL support not enabled in %s.\n", argv[0]);
542 (void)argc;
543 return 0;
544}
545
546#endif
547
Pascal Massimino7937b402011-12-13 14:02:04 -0800548//------------------------------------------------------------------------------