blob: f13b88fcd6c37737868c3d125bda9983c84d3457 [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
14#include "config.h"
15#endif
Pascal Massimino7937b402011-12-13 14:02:04 -080016
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
James Zern0e513f72013-05-01 14:47:56 -070021#if defined(HAVE_GLUT_GLUT_H)
Pascal Massimino7937b402011-12-13 14:02:04 -080022#include <GLUT/glut.h>
23#else
24#include <GL/glut.h>
25#ifdef FREEGLUT
26#include <GL/freeglut.h>
27#endif
28#endif
29
James Zernddfee5d2013-03-07 19:31:27 -080030#ifdef WEBP_HAVE_QCMS
31#include <qcms.h>
32#endif
33
34#include "webp/decode.h"
35#include "webp/demux.h"
36
James Zern061263a2012-05-11 16:00:57 -070037#include "./example_util.h"
38
James Zern91179542011-12-16 19:30:33 -080039#ifdef _MSC_VER
40#define snprintf _snprintf
41#endif
42
Pascal Massimino861a5b72012-05-09 00:41:12 -070043static void Help(void);
44
45// Unfortunate global variables. Gathered into a struct for comfort.
46static struct {
47 int has_animation;
James Zernddfee5d2013-03-07 19:31:27 -080048 int has_color_profile;
Pascal Massimino861a5b72012-05-09 00:41:12 -070049 int done;
50 int decoding_error;
51 int print_info;
James Zernddfee5d2013-03-07 19:31:27 -080052 int use_color_profile;
Pascal Massimino861a5b72012-05-09 00:41:12 -070053
skal68f282f2012-11-15 09:15:04 +010054 int canvas_width, canvas_height;
Urvang Joshi6808e692012-07-06 11:35:36 +053055 int loop_count;
skal68f282f2012-11-15 09:15:04 +010056 uint32_t bg_color;
Pascal Massimino861a5b72012-05-09 00:41:12 -070057
58 const char* file_name;
59 WebPData data;
Pascal Massimino861a5b72012-05-09 00:41:12 -070060 WebPDecoderConfig* config;
61 const WebPDecBuffer* pic;
James Zernd7a5ac82012-09-26 23:44:59 -070062 WebPDemuxer* dmux;
63 WebPIterator frameiter;
James Zern04c7a2e2013-03-23 12:45:11 -070064 struct {
65 int width, height;
66 int x_offset, y_offset;
67 enum WebPMuxAnimDispose dispose_method;
68 } prev_frame;
James Zernddfee5d2013-03-07 19:31:27 -080069 WebPChunkIterator iccp;
James Zernd7a5ac82012-09-26 23:44:59 -070070} kParams;
Pascal Massimino861a5b72012-05-09 00:41:12 -070071
72static void ClearPreviousPic(void) {
73 WebPFreeDecBuffer((WebPDecBuffer*)kParams.pic);
74 kParams.pic = NULL;
75}
76
Pascal Massimino861a5b72012-05-09 00:41:12 -070077static void ClearParams(void) {
78 ClearPreviousPic();
Urvang Joshif3bab8e2012-06-07 17:19:02 +053079 WebPDataClear(&kParams.data);
James Zernd7a5ac82012-09-26 23:44:59 -070080 WebPDemuxReleaseIterator(&kParams.frameiter);
James Zernddfee5d2013-03-07 19:31:27 -080081 WebPDemuxReleaseChunkIterator(&kParams.iccp);
James Zernd7a5ac82012-09-26 23:44:59 -070082 WebPDemuxDelete(kParams.dmux);
83 kParams.dmux = NULL;
Pascal Massimino861a5b72012-05-09 00:41:12 -070084}
Pascal Massimino7937b402011-12-13 14:02:04 -080085
James Zernddfee5d2013-03-07 19:31:27 -080086// -----------------------------------------------------------------------------
87// Color profile handling
88static int ApplyColorProfile(const WebPData* const profile,
89 WebPDecBuffer* const rgba) {
90#ifdef WEBP_HAVE_QCMS
91 int i, ok = 0;
92 uint8_t* line;
93 uint8_t major_revision;
94 qcms_profile* input_profile = NULL;
95 qcms_profile* output_profile = NULL;
96 qcms_transform* transform = NULL;
97 const qcms_data_type input_type = QCMS_DATA_RGBA_8;
98 const qcms_data_type output_type = QCMS_DATA_RGBA_8;
99 const qcms_intent intent = QCMS_INTENT_DEFAULT;
100
101 if (profile == NULL || rgba == NULL) return 0;
102 if (profile->bytes == NULL || profile->size < 10) return 1;
103 major_revision = profile->bytes[8];
104
105 qcms_enable_iccv4();
106 input_profile = qcms_profile_from_memory(profile->bytes, profile->size);
107 // qcms_profile_is_bogus() is broken with ICCv4.
108 if (input_profile == NULL ||
109 (major_revision < 4 && qcms_profile_is_bogus(input_profile))) {
110 fprintf(stderr, "Color profile is bogus!\n");
111 goto Error;
112 }
113
114 output_profile = qcms_profile_sRGB();
115 if (output_profile == NULL) {
116 fprintf(stderr, "Error creating output color profile!\n");
117 goto Error;
118 }
119
120 qcms_profile_precache_output_transform(output_profile);
121 transform = qcms_transform_create(input_profile, input_type,
122 output_profile, output_type,
123 intent);
124 if (transform == NULL) {
125 fprintf(stderr, "Error creating color transform!\n");
126 goto Error;
127 }
128
129 line = rgba->u.RGBA.rgba;
130 for (i = 0; i < rgba->height; ++i, line += rgba->u.RGBA.stride) {
131 qcms_transform_data(transform, line, line, rgba->width);
132 }
133 ok = 1;
134
135 Error:
136 if (input_profile != NULL) qcms_profile_release(input_profile);
137 if (output_profile != NULL) qcms_profile_release(output_profile);
138 if (transform != NULL) qcms_transform_release(transform);
139 return ok;
140#else
141 (void)profile;
142 (void)rgba;
143 return 1;
144#endif // WEBP_HAVE_QCMS
145}
146
147//------------------------------------------------------------------------------
148// File decoding
149
150static int Decode(void) { // Fills kParams.frameiter
151 const WebPIterator* const iter = &kParams.frameiter;
152 WebPDecoderConfig* const config = kParams.config;
153 WebPDecBuffer* const output_buffer = &config->output;
154 int ok = 0;
155
156 ClearPreviousPic();
157 output_buffer->colorspace = MODE_RGBA;
158 ok = (WebPDecode(iter->fragment.bytes, iter->fragment.size,
159 config) == VP8_STATUS_OK);
160 if (!ok) {
161 fprintf(stderr, "Decoding of frame #%d failed!\n", iter->frame_num);
162 } else {
163 kParams.pic = output_buffer;
164 if (kParams.use_color_profile) {
165 ok = ApplyColorProfile(&kParams.iccp.chunk, output_buffer);
166 if (!ok) {
167 fprintf(stderr, "Applying color profile to frame #%d failed!\n",
168 iter->frame_num);
169 }
170 }
171 }
172 return ok;
173}
174
175static void decode_callback(int what) {
176 if (what == 0 && !kParams.done) {
177 int duration = 0;
178 if (kParams.dmux != NULL) {
179 WebPIterator* const iter = &kParams.frameiter;
180 if (!WebPDemuxNextFrame(iter)) {
181 WebPDemuxReleaseIterator(iter);
182 if (WebPDemuxGetFrame(kParams.dmux, 1, iter)) {
183 --kParams.loop_count;
184 kParams.done = (kParams.loop_count == 0);
185 } else {
186 kParams.decoding_error = 1;
187 kParams.done = 1;
188 return;
189 }
190 }
191 duration = iter->duration;
192 }
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;
284 const WebPIterator* const iter = &kParams.frameiter;
skal41a6ced2012-11-15 09:35:01 +0100285 GLfloat xoff, yoff;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700286 if (pic == NULL) return;
Pascal Massimino7937b402011-12-13 14:02:04 -0800287 glPushMatrix();
288 glPixelZoom(1, -1);
skal41a6ced2012-11-15 09:35:01 +0100289 xoff = (GLfloat)(2. * iter->x_offset / kParams.canvas_width);
290 yoff = (GLfloat)(2. * iter->y_offset / kParams.canvas_height);
James Zern013023e2013-03-15 12:17:45 -0700291 glRasterPos2f(-1.f + xoff, 1.f - yoff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800292 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700293 glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4);
James Zern04c7a2e2013-03-23 12:45:11 -0700294
295 if (kParams.prev_frame.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {
296 // TODO(later): these offsets and those above should factor in window size.
297 // they will be incorrect if the window is resized.
298 // glScissor() takes window coordinates (0,0 at bottom left).
299 const int window_x = kParams.prev_frame.x_offset;
300 const int window_y = kParams.canvas_height -
301 kParams.prev_frame.y_offset -
302 kParams.prev_frame.height;
303 glEnable(GL_SCISSOR_TEST);
304 // Only updated the requested area, not the whole canvas.
305 glScissor(window_x, window_y,
306 kParams.prev_frame.width, kParams.prev_frame.height);
307
skal68f282f2012-11-15 09:15:04 +0100308 glClear(GL_COLOR_BUFFER_BIT); // use clear color
James Zern04c7a2e2013-03-23 12:45:11 -0700309 DrawCheckerBoard();
310
311 glDisable(GL_SCISSOR_TEST);
skal68f282f2012-11-15 09:15:04 +0100312 }
James Zern04c7a2e2013-03-23 12:45:11 -0700313 kParams.prev_frame.width = iter->width;
314 kParams.prev_frame.height = iter->height;
315 kParams.prev_frame.x_offset = iter->x_offset;
316 kParams.prev_frame.y_offset = iter->y_offset;
317 kParams.prev_frame.dispose_method = iter->dispose_method;
318
Pascal Massimino861a5b72012-05-09 00:41:12 -0700319 glDrawPixels(pic->width, pic->height,
320 GL_RGBA, GL_UNSIGNED_BYTE,
321 (GLvoid*)pic->u.RGBA.rgba);
322 if (kParams.print_info) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800323 char tmp[32];
324
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.90f);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700327 PrintString(kParams.file_name);
Pascal Massimino7937b402011-12-13 14:02:04 -0800328
Pascal Massimino861a5b72012-05-09 00:41:12 -0700329 snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height);
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.80f);
Pascal Massimino7937b402011-12-13 14:02:04 -0800332 PrintString(tmp);
skal68f282f2012-11-15 09:15:04 +0100333 if (iter->x_offset != 0 || iter->y_offset != 0) {
334 snprintf(tmp, sizeof(tmp), " (offset:%d,%d)",
335 iter->x_offset, iter->y_offset);
336 glRasterPos2f(-0.95f, 0.70f);
337 PrintString(tmp);
338 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800339 }
James Zerna73b8972012-07-09 23:01:52 -0700340 glPopMatrix();
Pascal Massimino7937b402011-12-13 14:02:04 -0800341 glFlush();
342}
343
skal68f282f2012-11-15 09:15:04 +0100344static void StartDisplay(void) {
345 const int width = kParams.canvas_width;
346 const int height = kParams.canvas_height;
Pascal Massimino7937b402011-12-13 14:02:04 -0800347 glutInitDisplayMode(GLUT_RGBA);
skal68f282f2012-11-15 09:15:04 +0100348 glutInitWindowSize(width, height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800349 glutCreateWindow("WebP viewer");
Pascal Massimino7937b402011-12-13 14:02:04 -0800350 glutDisplayFunc(HandleDisplay);
351 glutIdleFunc(NULL);
352 glutKeyboardFunc(HandleKey);
Urvang Joshi08220102012-06-20 11:18:35 -0700353 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
354 glEnable(GL_BLEND);
skal68f282f2012-11-15 09:15:04 +0100355 glClearColor(GetColorf(kParams.bg_color, 0),
356 GetColorf(kParams.bg_color, 8),
357 GetColorf(kParams.bg_color, 16),
358 GetColorf(kParams.bg_color, 24));
359 HandleReshape(width, height);
360 glClear(GL_COLOR_BUFFER_BIT);
361 DrawCheckerBoard();
Pascal Massimino7937b402011-12-13 14:02:04 -0800362}
363
364//------------------------------------------------------------------------------
Pascal Massimino7937b402011-12-13 14:02:04 -0800365// Main
366
367static void Help(void) {
368 printf("Usage: vwebp in_file [options]\n\n"
369 "Decodes the WebP image file and visualize it using OpenGL\n"
370 "Options are:\n"
371 " -version .... print version number and exit.\n"
James Zernddfee5d2013-03-07 19:31:27 -0800372 " -noicc ....... don't use the icc profile if present.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800373 " -nofancy ..... don't use the fancy YUV420 upscaler.\n"
374 " -nofilter .... disable in-loop filtering.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800375 " -mt .......... use multi-threading.\n"
skal68f282f2012-11-15 09:15:04 +0100376 " -info ........ print info.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800377 " -h ....... this help message.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800378 "\n"
379 "Keyboard shortcuts:\n"
James Zernddfee5d2013-03-07 19:31:27 -0800380 " 'c' ................ toggle use of color profile.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800381 " 'i' ................ overlay file information.\n"
382 " 'q' / 'Q' / ESC .... quit.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800383 );
384}
385
386int main(int argc, char *argv[]) {
387 WebPDecoderConfig config;
388 int c;
389
390 if (!WebPInitDecoderConfig(&config)) {
391 fprintf(stderr, "Library version mismatch!\n");
392 return -1;
393 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700394 kParams.config = &config;
James Zernddfee5d2013-03-07 19:31:27 -0800395 kParams.use_color_profile = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800396
397 for (c = 1; c < argc; ++c) {
398 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
399 Help();
400 return 0;
James Zernddfee5d2013-03-07 19:31:27 -0800401 } else if (!strcmp(argv[c], "-noicc")) {
402 kParams.use_color_profile = 0;
Pascal Massimino7937b402011-12-13 14:02:04 -0800403 } else if (!strcmp(argv[c], "-nofancy")) {
404 config.options.no_fancy_upsampling = 1;
405 } else if (!strcmp(argv[c], "-nofilter")) {
406 config.options.bypass_filtering = 1;
skal68f282f2012-11-15 09:15:04 +0100407 } else if (!strcmp(argv[c], "-info")) {
408 kParams.print_info = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800409 } else if (!strcmp(argv[c], "-version")) {
Urvang Joshia5042a32013-02-26 14:22:06 -0800410 const int dec_version = WebPGetDecoderVersion();
411 const int dmux_version = WebPGetDemuxVersion();
412 printf("WebP Decoder version: %d.%d.%d\nWebP Demux version: %d.%d.%d\n",
413 (dec_version >> 16) & 0xff, (dec_version >> 8) & 0xff,
414 dec_version & 0xff, (dmux_version >> 16) & 0xff,
415 (dmux_version >> 8) & 0xff, dmux_version & 0xff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800416 return 0;
417 } else if (!strcmp(argv[c], "-mt")) {
418 config.options.use_threads = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800419 } else if (argv[c][0] == '-') {
420 printf("Unknown option '%s'\n", argv[c]);
421 Help();
422 return -1;
423 } else {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700424 kParams.file_name = argv[c];
Pascal Massimino7937b402011-12-13 14:02:04 -0800425 }
426 }
427
James Zern061263a2012-05-11 16:00:57 -0700428 if (kParams.file_name == NULL) {
429 printf("missing input file!!\n");
430 Help();
431 return 0;
432 }
433
434 if (!ExUtilReadFile(kParams.file_name,
Urvang Joshia0770722012-10-30 14:54:46 -0700435 &kParams.data.bytes, &kParams.data.size)) {
James Zern061263a2012-05-11 16:00:57 -0700436 goto Error;
437 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700438
Pascal Massimino830f72b2013-06-10 05:46:22 -0700439 if (!WebPGetInfo(kParams.data.bytes, kParams.data.size, NULL, NULL)) {
440 fprintf(stderr, "Input file doesn't appear to be WebP format.\n");
441 goto Error;
442 }
443
James Zernd7a5ac82012-09-26 23:44:59 -0700444 kParams.dmux = WebPDemux(&kParams.data);
445 if (kParams.dmux == NULL) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700446 fprintf(stderr, "Could not create demuxing object!\n");
447 goto Error;
Pascal Massimino7937b402011-12-13 14:02:04 -0800448 }
449
Urvang Joshia00a3da2012-10-31 17:49:15 -0700450 if (WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & FRAGMENTS_FLAG) {
451 fprintf(stderr, "Image fragments are not supported for now!\n");
Pascal Massimino861a5b72012-05-09 00:41:12 -0700452 goto Error;
453 }
skal68f282f2012-11-15 09:15:04 +0100454 kParams.canvas_width = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_WIDTH);
455 kParams.canvas_height = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_HEIGHT);
456 if (kParams.print_info) {
457 printf("Canvas: %d x %d\n", kParams.canvas_width, kParams.canvas_height);
458 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800459
James Zern04c7a2e2013-03-23 12:45:11 -0700460 kParams.prev_frame.width = kParams.canvas_width;
461 kParams.prev_frame.height = kParams.canvas_height;
462 kParams.prev_frame.x_offset = kParams.prev_frame.y_offset = 0;
463 kParams.prev_frame.dispose_method = WEBP_MUX_DISPOSE_BACKGROUND;
464
James Zernddfee5d2013-03-07 19:31:27 -0800465 memset(&kParams.iccp, 0, sizeof(kParams.iccp));
466 kParams.has_color_profile =
467 !!(WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & ICCP_FLAG);
468 if (kParams.has_color_profile) {
469#ifdef WEBP_HAVE_QCMS
470 if (!WebPDemuxGetChunk(kParams.dmux, "ICCP", 1, &kParams.iccp)) goto Error;
471 printf("VP8X: Found color profile\n");
472#else
473 fprintf(stderr, "Warning: color profile present, but qcms is unavailable!\n"
474 "Build libqcms from Mozilla or Chromium and define WEBP_HAVE_QCMS "
475 "before building.\n");
476#endif
477 }
478
James Zernd7a5ac82012-09-26 23:44:59 -0700479 if (!WebPDemuxGetFrame(kParams.dmux, 1, &kParams.frameiter)) goto Error;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700480
Urvang Joshia0770722012-10-30 14:54:46 -0700481 kParams.has_animation = (kParams.frameiter.num_frames > 1);
James Zernd7a5ac82012-09-26 23:44:59 -0700482 kParams.loop_count = (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT);
skal68f282f2012-11-15 09:15:04 +0100483 kParams.bg_color = WebPDemuxGetI(kParams.dmux, WEBP_FF_BACKGROUND_COLOR);
James Zernd7a5ac82012-09-26 23:44:59 -0700484 printf("VP8X: Found %d images in file (loop count = %d)\n",
Urvang Joshia0770722012-10-30 14:54:46 -0700485 kParams.frameiter.num_frames, kParams.loop_count);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700486
487 // Decode first frame
skal68f282f2012-11-15 09:15:04 +0100488 if (!Decode()) goto Error;
489
490 // Position iterator to last frame. Next call to HandleDisplay will wrap over.
491 // We take this into account by bumping up loop_count.
492 WebPDemuxGetFrame(kParams.dmux, 0, &kParams.frameiter);
493 if (kParams.loop_count) ++kParams.loop_count;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700494
495 // Start display (and timer)
Pascal Massimino7937b402011-12-13 14:02:04 -0800496 glutInit(&argc, argv);
James Zern880fd982012-05-25 14:53:46 -0700497#ifdef FREEGLUT
498 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
499#endif
skal68f282f2012-11-15 09:15:04 +0100500 StartDisplay();
501
Pascal Massimino861a5b72012-05-09 00:41:12 -0700502 if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0);
503 glutMainLoop();
Pascal Massimino7937b402011-12-13 14:02:04 -0800504
505 // Should only be reached when using FREEGLUT:
Pascal Massimino861a5b72012-05-09 00:41:12 -0700506 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800507 return 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700508
509 Error:
510 ClearParams();
511 return -1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800512}
513
514//------------------------------------------------------------------------------