blob: 22f14b237beb43d7fda73b791c034718614da48e [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//
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//
Pascal Massiminoa4e1cdb2013-04-01 23:10:35 +00008// Simple OpenGL-based WebP file viewer.
Pascal Massimino7937b402011-12-13 14:02:04 -08009//
10// Author: Skal (pascal.massimino@gmail.com)
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
Pascal Massimino7937b402011-12-13 14:02:04 -080016#ifdef __APPLE__
17#include <GLUT/glut.h>
18#else
19#include <GL/glut.h>
20#ifdef FREEGLUT
21#include <GL/freeglut.h>
22#endif
23#endif
24
James Zernddfee5d2013-03-07 19:31:27 -080025#ifdef WEBP_HAVE_QCMS
26#include <qcms.h>
27#endif
28
29#include "webp/decode.h"
30#include "webp/demux.h"
31
James Zern061263a2012-05-11 16:00:57 -070032#include "./example_util.h"
33
James Zern91179542011-12-16 19:30:33 -080034#ifdef _MSC_VER
35#define snprintf _snprintf
36#endif
37
Pascal Massimino861a5b72012-05-09 00:41:12 -070038static void Help(void);
39
40// Unfortunate global variables. Gathered into a struct for comfort.
41static struct {
42 int has_animation;
James Zernddfee5d2013-03-07 19:31:27 -080043 int has_color_profile;
Pascal Massimino861a5b72012-05-09 00:41:12 -070044 int done;
45 int decoding_error;
46 int print_info;
James Zernddfee5d2013-03-07 19:31:27 -080047 int use_color_profile;
Pascal Massimino861a5b72012-05-09 00:41:12 -070048
skal68f282f2012-11-15 09:15:04 +010049 int canvas_width, canvas_height;
Urvang Joshi6808e692012-07-06 11:35:36 +053050 int loop_count;
skal68f282f2012-11-15 09:15:04 +010051 uint32_t bg_color;
Pascal Massimino861a5b72012-05-09 00:41:12 -070052
53 const char* file_name;
54 WebPData data;
Pascal Massimino861a5b72012-05-09 00:41:12 -070055 WebPDecoderConfig* config;
56 const WebPDecBuffer* pic;
James Zernd7a5ac82012-09-26 23:44:59 -070057 WebPDemuxer* dmux;
58 WebPIterator frameiter;
James Zern04c7a2e2013-03-23 12:45:11 -070059 struct {
60 int width, height;
61 int x_offset, y_offset;
62 enum WebPMuxAnimDispose dispose_method;
63 } prev_frame;
James Zernddfee5d2013-03-07 19:31:27 -080064 WebPChunkIterator iccp;
James Zernd7a5ac82012-09-26 23:44:59 -070065} kParams;
Pascal Massimino861a5b72012-05-09 00:41:12 -070066
67static void ClearPreviousPic(void) {
68 WebPFreeDecBuffer((WebPDecBuffer*)kParams.pic);
69 kParams.pic = NULL;
70}
71
Pascal Massimino861a5b72012-05-09 00:41:12 -070072static void ClearParams(void) {
73 ClearPreviousPic();
Urvang Joshif3bab8e2012-06-07 17:19:02 +053074 WebPDataClear(&kParams.data);
James Zernd7a5ac82012-09-26 23:44:59 -070075 WebPDemuxReleaseIterator(&kParams.frameiter);
James Zernddfee5d2013-03-07 19:31:27 -080076 WebPDemuxReleaseChunkIterator(&kParams.iccp);
James Zernd7a5ac82012-09-26 23:44:59 -070077 WebPDemuxDelete(kParams.dmux);
78 kParams.dmux = NULL;
Pascal Massimino861a5b72012-05-09 00:41:12 -070079}
Pascal Massimino7937b402011-12-13 14:02:04 -080080
James Zernddfee5d2013-03-07 19:31:27 -080081// -----------------------------------------------------------------------------
82// Color profile handling
83static int ApplyColorProfile(const WebPData* const profile,
84 WebPDecBuffer* const rgba) {
85#ifdef WEBP_HAVE_QCMS
86 int i, ok = 0;
87 uint8_t* line;
88 uint8_t major_revision;
89 qcms_profile* input_profile = NULL;
90 qcms_profile* output_profile = NULL;
91 qcms_transform* transform = NULL;
92 const qcms_data_type input_type = QCMS_DATA_RGBA_8;
93 const qcms_data_type output_type = QCMS_DATA_RGBA_8;
94 const qcms_intent intent = QCMS_INTENT_DEFAULT;
95
96 if (profile == NULL || rgba == NULL) return 0;
97 if (profile->bytes == NULL || profile->size < 10) return 1;
98 major_revision = profile->bytes[8];
99
100 qcms_enable_iccv4();
101 input_profile = qcms_profile_from_memory(profile->bytes, profile->size);
102 // qcms_profile_is_bogus() is broken with ICCv4.
103 if (input_profile == NULL ||
104 (major_revision < 4 && qcms_profile_is_bogus(input_profile))) {
105 fprintf(stderr, "Color profile is bogus!\n");
106 goto Error;
107 }
108
109 output_profile = qcms_profile_sRGB();
110 if (output_profile == NULL) {
111 fprintf(stderr, "Error creating output color profile!\n");
112 goto Error;
113 }
114
115 qcms_profile_precache_output_transform(output_profile);
116 transform = qcms_transform_create(input_profile, input_type,
117 output_profile, output_type,
118 intent);
119 if (transform == NULL) {
120 fprintf(stderr, "Error creating color transform!\n");
121 goto Error;
122 }
123
124 line = rgba->u.RGBA.rgba;
125 for (i = 0; i < rgba->height; ++i, line += rgba->u.RGBA.stride) {
126 qcms_transform_data(transform, line, line, rgba->width);
127 }
128 ok = 1;
129
130 Error:
131 if (input_profile != NULL) qcms_profile_release(input_profile);
132 if (output_profile != NULL) qcms_profile_release(output_profile);
133 if (transform != NULL) qcms_transform_release(transform);
134 return ok;
135#else
136 (void)profile;
137 (void)rgba;
138 return 1;
139#endif // WEBP_HAVE_QCMS
140}
141
142//------------------------------------------------------------------------------
143// File decoding
144
145static int Decode(void) { // Fills kParams.frameiter
146 const WebPIterator* const iter = &kParams.frameiter;
147 WebPDecoderConfig* const config = kParams.config;
148 WebPDecBuffer* const output_buffer = &config->output;
149 int ok = 0;
150
151 ClearPreviousPic();
152 output_buffer->colorspace = MODE_RGBA;
153 ok = (WebPDecode(iter->fragment.bytes, iter->fragment.size,
154 config) == VP8_STATUS_OK);
155 if (!ok) {
156 fprintf(stderr, "Decoding of frame #%d failed!\n", iter->frame_num);
157 } else {
158 kParams.pic = output_buffer;
159 if (kParams.use_color_profile) {
160 ok = ApplyColorProfile(&kParams.iccp.chunk, output_buffer);
161 if (!ok) {
162 fprintf(stderr, "Applying color profile to frame #%d failed!\n",
163 iter->frame_num);
164 }
165 }
166 }
167 return ok;
168}
169
170static void decode_callback(int what) {
171 if (what == 0 && !kParams.done) {
172 int duration = 0;
173 if (kParams.dmux != NULL) {
174 WebPIterator* const iter = &kParams.frameiter;
175 if (!WebPDemuxNextFrame(iter)) {
176 WebPDemuxReleaseIterator(iter);
177 if (WebPDemuxGetFrame(kParams.dmux, 1, iter)) {
178 --kParams.loop_count;
179 kParams.done = (kParams.loop_count == 0);
180 } else {
181 kParams.decoding_error = 1;
182 kParams.done = 1;
183 return;
184 }
185 }
186 duration = iter->duration;
187 }
188 if (!Decode()) {
189 kParams.decoding_error = 1;
190 kParams.done = 1;
191 } else {
192 glutPostRedisplay();
193 glutTimerFunc(duration, decode_callback, what);
194 }
195 }
196}
197
Pascal Massimino7937b402011-12-13 14:02:04 -0800198//------------------------------------------------------------------------------
199// Callbacks
200
201static void HandleKey(unsigned char key, int pos_x, int pos_y) {
202 (void)pos_x;
203 (void)pos_y;
204 if (key == 'q' || key == 'Q' || key == 27 /* Esc */) {
205#ifdef FREEGLUT
206 glutLeaveMainLoop();
207#else
Pascal Massimino861a5b72012-05-09 00:41:12 -0700208 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800209 exit(0);
210#endif
James Zernddfee5d2013-03-07 19:31:27 -0800211 } else if (key == 'c') {
212 if (kParams.has_color_profile && !kParams.decoding_error) {
213 kParams.use_color_profile = 1 - kParams.use_color_profile;
214
215 if (kParams.has_animation) {
216 // Restart the completed animation to pickup the color profile change.
217 if (kParams.done && kParams.loop_count == 0) {
218 kParams.loop_count =
219 (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT) + 1;
220 kParams.done = 0;
221 // Start the decode loop immediately.
222 glutTimerFunc(0, decode_callback, 0);
223 }
224 } else {
225 Decode();
226 glutPostRedisplay();
227 }
228 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800229 } else if (key == 'i') {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700230 kParams.print_info = 1 - kParams.print_info;
Pascal Massimino7937b402011-12-13 14:02:04 -0800231 glutPostRedisplay();
232 }
233}
234
235static void HandleReshape(int width, int height) {
236 // TODO(skal): proper handling of resize, esp. for large pictures.
237 // + key control of the zoom.
238 glViewport(0, 0, width, height);
239 glMatrixMode(GL_PROJECTION);
240 glLoadIdentity();
241 glMatrixMode(GL_MODELVIEW);
242 glLoadIdentity();
243}
244
245static void PrintString(const char* const text) {
246 void* const font = GLUT_BITMAP_9_BY_15;
247 int i;
248 for (i = 0; text[i]; ++i) {
249 glutBitmapCharacter(font, text[i]);
250 }
251}
252
skal68f282f2012-11-15 09:15:04 +0100253static float GetColorf(uint32_t color, int shift) {
254 return (color >> shift) / 255.f;
255}
256
James Zerna73b8972012-07-09 23:01:52 -0700257static void DrawCheckerBoard(void) {
258 const int square_size = 8; // must be a power of 2
259 int x, y;
260 GLint viewport[4]; // x, y, width, height
261
262 glPushMatrix();
263
264 glGetIntegerv(GL_VIEWPORT, viewport);
265 // shift to integer coordinates with (0,0) being top-left.
266 glOrtho(0, viewport[2], viewport[3], 0, -1, 1);
267 for (y = 0; y < viewport[3]; y += square_size) {
268 for (x = 0; x < viewport[2]; x += square_size) {
269 const GLubyte color = 128 + 64 * (!((x + y) & square_size));
270 glColor3ub(color, color, color);
271 glRecti(x, y, x + square_size, y + square_size);
272 }
273 }
274 glPopMatrix();
275}
276
Pascal Massimino7937b402011-12-13 14:02:04 -0800277static void HandleDisplay(void) {
skal68f282f2012-11-15 09:15:04 +0100278 const WebPDecBuffer* const pic = kParams.pic;
279 const WebPIterator* const iter = &kParams.frameiter;
skal41a6ced2012-11-15 09:35:01 +0100280 GLfloat xoff, yoff;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700281 if (pic == NULL) return;
Pascal Massimino7937b402011-12-13 14:02:04 -0800282 glPushMatrix();
283 glPixelZoom(1, -1);
skal41a6ced2012-11-15 09:35:01 +0100284 xoff = (GLfloat)(2. * iter->x_offset / kParams.canvas_width);
285 yoff = (GLfloat)(2. * iter->y_offset / kParams.canvas_height);
James Zern013023e2013-03-15 12:17:45 -0700286 glRasterPos2f(-1.f + xoff, 1.f - yoff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800287 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700288 glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4);
James Zern04c7a2e2013-03-23 12:45:11 -0700289
290 if (kParams.prev_frame.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {
291 // TODO(later): these offsets and those above should factor in window size.
292 // they will be incorrect if the window is resized.
293 // glScissor() takes window coordinates (0,0 at bottom left).
294 const int window_x = kParams.prev_frame.x_offset;
295 const int window_y = kParams.canvas_height -
296 kParams.prev_frame.y_offset -
297 kParams.prev_frame.height;
298 glEnable(GL_SCISSOR_TEST);
299 // Only updated the requested area, not the whole canvas.
300 glScissor(window_x, window_y,
301 kParams.prev_frame.width, kParams.prev_frame.height);
302
skal68f282f2012-11-15 09:15:04 +0100303 glClear(GL_COLOR_BUFFER_BIT); // use clear color
James Zern04c7a2e2013-03-23 12:45:11 -0700304 DrawCheckerBoard();
305
306 glDisable(GL_SCISSOR_TEST);
skal68f282f2012-11-15 09:15:04 +0100307 }
James Zern04c7a2e2013-03-23 12:45:11 -0700308 kParams.prev_frame.width = iter->width;
309 kParams.prev_frame.height = iter->height;
310 kParams.prev_frame.x_offset = iter->x_offset;
311 kParams.prev_frame.y_offset = iter->y_offset;
312 kParams.prev_frame.dispose_method = iter->dispose_method;
313
Pascal Massimino861a5b72012-05-09 00:41:12 -0700314 glDrawPixels(pic->width, pic->height,
315 GL_RGBA, GL_UNSIGNED_BYTE,
316 (GLvoid*)pic->u.RGBA.rgba);
317 if (kParams.print_info) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800318 char tmp[32];
319
James Zern013023e2013-03-15 12:17:45 -0700320 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800321 glRasterPos2f(-0.95f, 0.90f);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700322 PrintString(kParams.file_name);
Pascal Massimino7937b402011-12-13 14:02:04 -0800323
Pascal Massimino861a5b72012-05-09 00:41:12 -0700324 snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height);
James Zern013023e2013-03-15 12:17:45 -0700325 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800326 glRasterPos2f(-0.95f, 0.80f);
Pascal Massimino7937b402011-12-13 14:02:04 -0800327 PrintString(tmp);
skal68f282f2012-11-15 09:15:04 +0100328 if (iter->x_offset != 0 || iter->y_offset != 0) {
329 snprintf(tmp, sizeof(tmp), " (offset:%d,%d)",
330 iter->x_offset, iter->y_offset);
331 glRasterPos2f(-0.95f, 0.70f);
332 PrintString(tmp);
333 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800334 }
James Zerna73b8972012-07-09 23:01:52 -0700335 glPopMatrix();
Pascal Massimino7937b402011-12-13 14:02:04 -0800336 glFlush();
337}
338
skal68f282f2012-11-15 09:15:04 +0100339static void StartDisplay(void) {
340 const int width = kParams.canvas_width;
341 const int height = kParams.canvas_height;
Pascal Massimino7937b402011-12-13 14:02:04 -0800342 glutInitDisplayMode(GLUT_RGBA);
skal68f282f2012-11-15 09:15:04 +0100343 glutInitWindowSize(width, height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800344 glutCreateWindow("WebP viewer");
Pascal Massimino7937b402011-12-13 14:02:04 -0800345 glutDisplayFunc(HandleDisplay);
346 glutIdleFunc(NULL);
347 glutKeyboardFunc(HandleKey);
Urvang Joshi08220102012-06-20 11:18:35 -0700348 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
349 glEnable(GL_BLEND);
skal68f282f2012-11-15 09:15:04 +0100350 glClearColor(GetColorf(kParams.bg_color, 0),
351 GetColorf(kParams.bg_color, 8),
352 GetColorf(kParams.bg_color, 16),
353 GetColorf(kParams.bg_color, 24));
354 HandleReshape(width, height);
355 glClear(GL_COLOR_BUFFER_BIT);
356 DrawCheckerBoard();
Pascal Massimino7937b402011-12-13 14:02:04 -0800357}
358
359//------------------------------------------------------------------------------
Pascal Massimino7937b402011-12-13 14:02:04 -0800360// Main
361
362static void Help(void) {
363 printf("Usage: vwebp in_file [options]\n\n"
364 "Decodes the WebP image file and visualize it using OpenGL\n"
365 "Options are:\n"
366 " -version .... print version number and exit.\n"
James Zernddfee5d2013-03-07 19:31:27 -0800367 " -noicc ....... don't use the icc profile if present.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800368 " -nofancy ..... don't use the fancy YUV420 upscaler.\n"
369 " -nofilter .... disable in-loop filtering.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800370 " -mt .......... use multi-threading.\n"
skal68f282f2012-11-15 09:15:04 +0100371 " -info ........ print info.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800372 " -h ....... this help message.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800373 "\n"
374 "Keyboard shortcuts:\n"
James Zernddfee5d2013-03-07 19:31:27 -0800375 " 'c' ................ toggle use of color profile.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800376 " 'i' ................ overlay file information.\n"
377 " 'q' / 'Q' / ESC .... quit.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800378 );
379}
380
381int main(int argc, char *argv[]) {
382 WebPDecoderConfig config;
383 int c;
384
385 if (!WebPInitDecoderConfig(&config)) {
386 fprintf(stderr, "Library version mismatch!\n");
387 return -1;
388 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700389 kParams.config = &config;
James Zernddfee5d2013-03-07 19:31:27 -0800390 kParams.use_color_profile = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800391
392 for (c = 1; c < argc; ++c) {
393 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
394 Help();
395 return 0;
James Zernddfee5d2013-03-07 19:31:27 -0800396 } else if (!strcmp(argv[c], "-noicc")) {
397 kParams.use_color_profile = 0;
Pascal Massimino7937b402011-12-13 14:02:04 -0800398 } else if (!strcmp(argv[c], "-nofancy")) {
399 config.options.no_fancy_upsampling = 1;
400 } else if (!strcmp(argv[c], "-nofilter")) {
401 config.options.bypass_filtering = 1;
skal68f282f2012-11-15 09:15:04 +0100402 } else if (!strcmp(argv[c], "-info")) {
403 kParams.print_info = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800404 } else if (!strcmp(argv[c], "-version")) {
Urvang Joshia5042a32013-02-26 14:22:06 -0800405 const int dec_version = WebPGetDecoderVersion();
406 const int dmux_version = WebPGetDemuxVersion();
407 printf("WebP Decoder version: %d.%d.%d\nWebP Demux version: %d.%d.%d\n",
408 (dec_version >> 16) & 0xff, (dec_version >> 8) & 0xff,
409 dec_version & 0xff, (dmux_version >> 16) & 0xff,
410 (dmux_version >> 8) & 0xff, dmux_version & 0xff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800411 return 0;
412 } else if (!strcmp(argv[c], "-mt")) {
413 config.options.use_threads = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800414 } else if (argv[c][0] == '-') {
415 printf("Unknown option '%s'\n", argv[c]);
416 Help();
417 return -1;
418 } else {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700419 kParams.file_name = argv[c];
Pascal Massimino7937b402011-12-13 14:02:04 -0800420 }
421 }
422
James Zern061263a2012-05-11 16:00:57 -0700423 if (kParams.file_name == NULL) {
424 printf("missing input file!!\n");
425 Help();
426 return 0;
427 }
428
429 if (!ExUtilReadFile(kParams.file_name,
Urvang Joshia0770722012-10-30 14:54:46 -0700430 &kParams.data.bytes, &kParams.data.size)) {
James Zern061263a2012-05-11 16:00:57 -0700431 goto Error;
432 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700433
James Zernd7a5ac82012-09-26 23:44:59 -0700434 kParams.dmux = WebPDemux(&kParams.data);
435 if (kParams.dmux == NULL) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700436 fprintf(stderr, "Could not create demuxing object!\n");
437 goto Error;
Pascal Massimino7937b402011-12-13 14:02:04 -0800438 }
439
Urvang Joshia00a3da2012-10-31 17:49:15 -0700440 if (WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & FRAGMENTS_FLAG) {
441 fprintf(stderr, "Image fragments are not supported for now!\n");
Pascal Massimino861a5b72012-05-09 00:41:12 -0700442 goto Error;
443 }
skal68f282f2012-11-15 09:15:04 +0100444 kParams.canvas_width = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_WIDTH);
445 kParams.canvas_height = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_HEIGHT);
446 if (kParams.print_info) {
447 printf("Canvas: %d x %d\n", kParams.canvas_width, kParams.canvas_height);
448 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800449
James Zern04c7a2e2013-03-23 12:45:11 -0700450 kParams.prev_frame.width = kParams.canvas_width;
451 kParams.prev_frame.height = kParams.canvas_height;
452 kParams.prev_frame.x_offset = kParams.prev_frame.y_offset = 0;
453 kParams.prev_frame.dispose_method = WEBP_MUX_DISPOSE_BACKGROUND;
454
James Zernddfee5d2013-03-07 19:31:27 -0800455 memset(&kParams.iccp, 0, sizeof(kParams.iccp));
456 kParams.has_color_profile =
457 !!(WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & ICCP_FLAG);
458 if (kParams.has_color_profile) {
459#ifdef WEBP_HAVE_QCMS
460 if (!WebPDemuxGetChunk(kParams.dmux, "ICCP", 1, &kParams.iccp)) goto Error;
461 printf("VP8X: Found color profile\n");
462#else
463 fprintf(stderr, "Warning: color profile present, but qcms is unavailable!\n"
464 "Build libqcms from Mozilla or Chromium and define WEBP_HAVE_QCMS "
465 "before building.\n");
466#endif
467 }
468
James Zernd7a5ac82012-09-26 23:44:59 -0700469 if (!WebPDemuxGetFrame(kParams.dmux, 1, &kParams.frameiter)) goto Error;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700470
Urvang Joshia0770722012-10-30 14:54:46 -0700471 kParams.has_animation = (kParams.frameiter.num_frames > 1);
James Zernd7a5ac82012-09-26 23:44:59 -0700472 kParams.loop_count = (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT);
skal68f282f2012-11-15 09:15:04 +0100473 kParams.bg_color = WebPDemuxGetI(kParams.dmux, WEBP_FF_BACKGROUND_COLOR);
James Zernd7a5ac82012-09-26 23:44:59 -0700474 printf("VP8X: Found %d images in file (loop count = %d)\n",
Urvang Joshia0770722012-10-30 14:54:46 -0700475 kParams.frameiter.num_frames, kParams.loop_count);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700476
477 // Decode first frame
skal68f282f2012-11-15 09:15:04 +0100478 if (!Decode()) goto Error;
479
480 // Position iterator to last frame. Next call to HandleDisplay will wrap over.
481 // We take this into account by bumping up loop_count.
482 WebPDemuxGetFrame(kParams.dmux, 0, &kParams.frameiter);
483 if (kParams.loop_count) ++kParams.loop_count;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700484
485 // Start display (and timer)
Pascal Massimino7937b402011-12-13 14:02:04 -0800486 glutInit(&argc, argv);
James Zern880fd982012-05-25 14:53:46 -0700487#ifdef FREEGLUT
488 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
489#endif
skal68f282f2012-11-15 09:15:04 +0100490 StartDisplay();
491
Pascal Massimino861a5b72012-05-09 00:41:12 -0700492 if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0);
493 glutMainLoop();
Pascal Massimino7937b402011-12-13 14:02:04 -0800494
495 // Should only be reached when using FREEGLUT:
Pascal Massimino861a5b72012-05-09 00:41:12 -0700496 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800497 return 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700498
499 Error:
500 ClearParams();
501 return -1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800502}
503
504//------------------------------------------------------------------------------