blob: 8fe49acd3697d79af054b7ad9425187494a3cf7a [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//
8// Simple WebP file viewer.
9//
10// Compiling on linux:
James Zernddfee5d2013-03-07 19:31:27 -080011// sudo apt-get install freeglut3-dev mesa-common-dev
Pascal Massimino861a5b72012-05-09 00:41:12 -070012// gcc -o vwebp vwebp.c -O3 -lwebp -lwebpmux -lglut -lGL -lpthread -lm
Pascal Massimino7937b402011-12-13 14:02:04 -080013// Compiling on Mac + XCode:
Pascal Massimino861a5b72012-05-09 00:41:12 -070014// gcc -o vwebp vwebp.c -lwebp -lwebpmux -framework GLUT -framework OpenGL
Pascal Massimino7937b402011-12-13 14:02:04 -080015//
16// Author: Skal (pascal.massimino@gmail.com)
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
Pascal Massimino7937b402011-12-13 14:02:04 -080022#ifdef __APPLE__
23#include <GLUT/glut.h>
24#else
25#include <GL/glut.h>
26#ifdef FREEGLUT
27#include <GL/freeglut.h>
28#endif
29#endif
30
James Zernddfee5d2013-03-07 19:31:27 -080031#ifdef WEBP_HAVE_QCMS
32#include <qcms.h>
33#endif
34
35#include "webp/decode.h"
36#include "webp/demux.h"
37
James Zern061263a2012-05-11 16:00:57 -070038#include "./example_util.h"
39
James Zern91179542011-12-16 19:30:33 -080040#ifdef _MSC_VER
41#define snprintf _snprintf
42#endif
43
Pascal Massimino861a5b72012-05-09 00:41:12 -070044static void Help(void);
45
46// Unfortunate global variables. Gathered into a struct for comfort.
47static struct {
48 int has_animation;
James Zernddfee5d2013-03-07 19:31:27 -080049 int has_color_profile;
Pascal Massimino861a5b72012-05-09 00:41:12 -070050 int done;
51 int decoding_error;
52 int print_info;
James Zernddfee5d2013-03-07 19:31:27 -080053 int use_color_profile;
Pascal Massimino861a5b72012-05-09 00:41:12 -070054
skal68f282f2012-11-15 09:15:04 +010055 int canvas_width, canvas_height;
Urvang Joshi6808e692012-07-06 11:35:36 +053056 int loop_count;
skal68f282f2012-11-15 09:15:04 +010057 uint32_t bg_color;
Pascal Massimino861a5b72012-05-09 00:41:12 -070058
59 const char* file_name;
60 WebPData data;
Pascal Massimino861a5b72012-05-09 00:41:12 -070061 WebPDecoderConfig* config;
62 const WebPDecBuffer* pic;
James Zernd7a5ac82012-09-26 23:44:59 -070063 WebPDemuxer* dmux;
64 WebPIterator frameiter;
James Zern04c7a2e2013-03-23 12:45:11 -070065 struct {
66 int width, height;
67 int x_offset, y_offset;
68 enum WebPMuxAnimDispose dispose_method;
69 } prev_frame;
James Zernddfee5d2013-03-07 19:31:27 -080070 WebPChunkIterator iccp;
James Zernd7a5ac82012-09-26 23:44:59 -070071} kParams;
Pascal Massimino861a5b72012-05-09 00:41:12 -070072
73static void ClearPreviousPic(void) {
74 WebPFreeDecBuffer((WebPDecBuffer*)kParams.pic);
75 kParams.pic = NULL;
76}
77
Pascal Massimino861a5b72012-05-09 00:41:12 -070078static void ClearParams(void) {
79 ClearPreviousPic();
Urvang Joshif3bab8e2012-06-07 17:19:02 +053080 WebPDataClear(&kParams.data);
James Zernd7a5ac82012-09-26 23:44:59 -070081 WebPDemuxReleaseIterator(&kParams.frameiter);
James Zernddfee5d2013-03-07 19:31:27 -080082 WebPDemuxReleaseChunkIterator(&kParams.iccp);
James Zernd7a5ac82012-09-26 23:44:59 -070083 WebPDemuxDelete(kParams.dmux);
84 kParams.dmux = NULL;
Pascal Massimino861a5b72012-05-09 00:41:12 -070085}
Pascal Massimino7937b402011-12-13 14:02:04 -080086
James Zernddfee5d2013-03-07 19:31:27 -080087// -----------------------------------------------------------------------------
88// Color profile handling
89static int ApplyColorProfile(const WebPData* const profile,
90 WebPDecBuffer* const rgba) {
91#ifdef WEBP_HAVE_QCMS
92 int i, ok = 0;
93 uint8_t* line;
94 uint8_t major_revision;
95 qcms_profile* input_profile = NULL;
96 qcms_profile* output_profile = NULL;
97 qcms_transform* transform = NULL;
98 const qcms_data_type input_type = QCMS_DATA_RGBA_8;
99 const qcms_data_type output_type = QCMS_DATA_RGBA_8;
100 const qcms_intent intent = QCMS_INTENT_DEFAULT;
101
102 if (profile == NULL || rgba == NULL) return 0;
103 if (profile->bytes == NULL || profile->size < 10) return 1;
104 major_revision = profile->bytes[8];
105
106 qcms_enable_iccv4();
107 input_profile = qcms_profile_from_memory(profile->bytes, profile->size);
108 // qcms_profile_is_bogus() is broken with ICCv4.
109 if (input_profile == NULL ||
110 (major_revision < 4 && qcms_profile_is_bogus(input_profile))) {
111 fprintf(stderr, "Color profile is bogus!\n");
112 goto Error;
113 }
114
115 output_profile = qcms_profile_sRGB();
116 if (output_profile == NULL) {
117 fprintf(stderr, "Error creating output color profile!\n");
118 goto Error;
119 }
120
121 qcms_profile_precache_output_transform(output_profile);
122 transform = qcms_transform_create(input_profile, input_type,
123 output_profile, output_type,
124 intent);
125 if (transform == NULL) {
126 fprintf(stderr, "Error creating color transform!\n");
127 goto Error;
128 }
129
130 line = rgba->u.RGBA.rgba;
131 for (i = 0; i < rgba->height; ++i, line += rgba->u.RGBA.stride) {
132 qcms_transform_data(transform, line, line, rgba->width);
133 }
134 ok = 1;
135
136 Error:
137 if (input_profile != NULL) qcms_profile_release(input_profile);
138 if (output_profile != NULL) qcms_profile_release(output_profile);
139 if (transform != NULL) qcms_transform_release(transform);
140 return ok;
141#else
142 (void)profile;
143 (void)rgba;
144 return 1;
145#endif // WEBP_HAVE_QCMS
146}
147
148//------------------------------------------------------------------------------
149// File decoding
150
151static int Decode(void) { // Fills kParams.frameiter
152 const WebPIterator* const iter = &kParams.frameiter;
153 WebPDecoderConfig* const config = kParams.config;
154 WebPDecBuffer* const output_buffer = &config->output;
155 int ok = 0;
156
157 ClearPreviousPic();
158 output_buffer->colorspace = MODE_RGBA;
159 ok = (WebPDecode(iter->fragment.bytes, iter->fragment.size,
160 config) == VP8_STATUS_OK);
161 if (!ok) {
162 fprintf(stderr, "Decoding of frame #%d failed!\n", iter->frame_num);
163 } else {
164 kParams.pic = output_buffer;
165 if (kParams.use_color_profile) {
166 ok = ApplyColorProfile(&kParams.iccp.chunk, output_buffer);
167 if (!ok) {
168 fprintf(stderr, "Applying color profile to frame #%d failed!\n",
169 iter->frame_num);
170 }
171 }
172 }
173 return ok;
174}
175
176static void decode_callback(int what) {
177 if (what == 0 && !kParams.done) {
178 int duration = 0;
179 if (kParams.dmux != NULL) {
180 WebPIterator* const iter = &kParams.frameiter;
181 if (!WebPDemuxNextFrame(iter)) {
182 WebPDemuxReleaseIterator(iter);
183 if (WebPDemuxGetFrame(kParams.dmux, 1, iter)) {
184 --kParams.loop_count;
185 kParams.done = (kParams.loop_count == 0);
186 } else {
187 kParams.decoding_error = 1;
188 kParams.done = 1;
189 return;
190 }
191 }
192 duration = iter->duration;
193 }
194 if (!Decode()) {
195 kParams.decoding_error = 1;
196 kParams.done = 1;
197 } else {
198 glutPostRedisplay();
199 glutTimerFunc(duration, decode_callback, what);
200 }
201 }
202}
203
Pascal Massimino7937b402011-12-13 14:02:04 -0800204//------------------------------------------------------------------------------
205// Callbacks
206
207static void HandleKey(unsigned char key, int pos_x, int pos_y) {
208 (void)pos_x;
209 (void)pos_y;
210 if (key == 'q' || key == 'Q' || key == 27 /* Esc */) {
211#ifdef FREEGLUT
212 glutLeaveMainLoop();
213#else
Pascal Massimino861a5b72012-05-09 00:41:12 -0700214 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800215 exit(0);
216#endif
James Zernddfee5d2013-03-07 19:31:27 -0800217 } else if (key == 'c') {
218 if (kParams.has_color_profile && !kParams.decoding_error) {
219 kParams.use_color_profile = 1 - kParams.use_color_profile;
220
221 if (kParams.has_animation) {
222 // Restart the completed animation to pickup the color profile change.
223 if (kParams.done && kParams.loop_count == 0) {
224 kParams.loop_count =
225 (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT) + 1;
226 kParams.done = 0;
227 // Start the decode loop immediately.
228 glutTimerFunc(0, decode_callback, 0);
229 }
230 } else {
231 Decode();
232 glutPostRedisplay();
233 }
234 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800235 } else if (key == 'i') {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700236 kParams.print_info = 1 - kParams.print_info;
Pascal Massimino7937b402011-12-13 14:02:04 -0800237 glutPostRedisplay();
238 }
239}
240
241static void HandleReshape(int width, int height) {
242 // TODO(skal): proper handling of resize, esp. for large pictures.
243 // + key control of the zoom.
244 glViewport(0, 0, width, height);
245 glMatrixMode(GL_PROJECTION);
246 glLoadIdentity();
247 glMatrixMode(GL_MODELVIEW);
248 glLoadIdentity();
249}
250
251static void PrintString(const char* const text) {
252 void* const font = GLUT_BITMAP_9_BY_15;
253 int i;
254 for (i = 0; text[i]; ++i) {
255 glutBitmapCharacter(font, text[i]);
256 }
257}
258
skal68f282f2012-11-15 09:15:04 +0100259static float GetColorf(uint32_t color, int shift) {
260 return (color >> shift) / 255.f;
261}
262
James Zerna73b8972012-07-09 23:01:52 -0700263static void DrawCheckerBoard(void) {
264 const int square_size = 8; // must be a power of 2
265 int x, y;
266 GLint viewport[4]; // x, y, width, height
267
268 glPushMatrix();
269
270 glGetIntegerv(GL_VIEWPORT, viewport);
271 // shift to integer coordinates with (0,0) being top-left.
272 glOrtho(0, viewport[2], viewport[3], 0, -1, 1);
273 for (y = 0; y < viewport[3]; y += square_size) {
274 for (x = 0; x < viewport[2]; x += square_size) {
275 const GLubyte color = 128 + 64 * (!((x + y) & square_size));
276 glColor3ub(color, color, color);
277 glRecti(x, y, x + square_size, y + square_size);
278 }
279 }
280 glPopMatrix();
281}
282
Pascal Massimino7937b402011-12-13 14:02:04 -0800283static void HandleDisplay(void) {
skal68f282f2012-11-15 09:15:04 +0100284 const WebPDecBuffer* const pic = kParams.pic;
285 const WebPIterator* const iter = &kParams.frameiter;
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);
skal41a6ced2012-11-15 09:35:01 +0100290 xoff = (GLfloat)(2. * iter->x_offset / kParams.canvas_width);
291 yoff = (GLfloat)(2. * iter->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
296 if (kParams.prev_frame.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {
297 // TODO(later): these offsets and those above should factor in window size.
298 // they will be incorrect if the window is resized.
299 // glScissor() takes window coordinates (0,0 at bottom left).
300 const int window_x = kParams.prev_frame.x_offset;
301 const int window_y = kParams.canvas_height -
302 kParams.prev_frame.y_offset -
303 kParams.prev_frame.height;
304 glEnable(GL_SCISSOR_TEST);
305 // Only updated the requested area, not the whole canvas.
306 glScissor(window_x, window_y,
307 kParams.prev_frame.width, kParams.prev_frame.height);
308
skal68f282f2012-11-15 09:15:04 +0100309 glClear(GL_COLOR_BUFFER_BIT); // use clear color
James Zern04c7a2e2013-03-23 12:45:11 -0700310 DrawCheckerBoard();
311
312 glDisable(GL_SCISSOR_TEST);
skal68f282f2012-11-15 09:15:04 +0100313 }
James Zern04c7a2e2013-03-23 12:45:11 -0700314 kParams.prev_frame.width = iter->width;
315 kParams.prev_frame.height = iter->height;
316 kParams.prev_frame.x_offset = iter->x_offset;
317 kParams.prev_frame.y_offset = iter->y_offset;
318 kParams.prev_frame.dispose_method = iter->dispose_method;
319
Pascal Massimino861a5b72012-05-09 00:41:12 -0700320 glDrawPixels(pic->width, pic->height,
321 GL_RGBA, GL_UNSIGNED_BYTE,
322 (GLvoid*)pic->u.RGBA.rgba);
323 if (kParams.print_info) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800324 char tmp[32];
325
James Zern013023e2013-03-15 12:17:45 -0700326 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800327 glRasterPos2f(-0.95f, 0.90f);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700328 PrintString(kParams.file_name);
Pascal Massimino7937b402011-12-13 14:02:04 -0800329
Pascal Massimino861a5b72012-05-09 00:41:12 -0700330 snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height);
James Zern013023e2013-03-15 12:17:45 -0700331 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800332 glRasterPos2f(-0.95f, 0.80f);
Pascal Massimino7937b402011-12-13 14:02:04 -0800333 PrintString(tmp);
skal68f282f2012-11-15 09:15:04 +0100334 if (iter->x_offset != 0 || iter->y_offset != 0) {
335 snprintf(tmp, sizeof(tmp), " (offset:%d,%d)",
336 iter->x_offset, iter->y_offset);
337 glRasterPos2f(-0.95f, 0.70f);
338 PrintString(tmp);
339 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800340 }
James Zerna73b8972012-07-09 23:01:52 -0700341 glPopMatrix();
Pascal Massimino7937b402011-12-13 14:02:04 -0800342 glFlush();
343}
344
skal68f282f2012-11-15 09:15:04 +0100345static void StartDisplay(void) {
346 const int width = kParams.canvas_width;
347 const int height = kParams.canvas_height;
Pascal Massimino7937b402011-12-13 14:02:04 -0800348 glutInitDisplayMode(GLUT_RGBA);
skal68f282f2012-11-15 09:15:04 +0100349 glutInitWindowSize(width, height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800350 glutCreateWindow("WebP viewer");
Pascal Massimino7937b402011-12-13 14:02:04 -0800351 glutDisplayFunc(HandleDisplay);
352 glutIdleFunc(NULL);
353 glutKeyboardFunc(HandleKey);
Urvang Joshi08220102012-06-20 11:18:35 -0700354 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
355 glEnable(GL_BLEND);
skal68f282f2012-11-15 09:15:04 +0100356 glClearColor(GetColorf(kParams.bg_color, 0),
357 GetColorf(kParams.bg_color, 8),
358 GetColorf(kParams.bg_color, 16),
359 GetColorf(kParams.bg_color, 24));
360 HandleReshape(width, height);
361 glClear(GL_COLOR_BUFFER_BIT);
362 DrawCheckerBoard();
Pascal Massimino7937b402011-12-13 14:02:04 -0800363}
364
365//------------------------------------------------------------------------------
Pascal Massimino7937b402011-12-13 14:02:04 -0800366// Main
367
368static void Help(void) {
369 printf("Usage: vwebp in_file [options]\n\n"
370 "Decodes the WebP image file and visualize it using OpenGL\n"
371 "Options are:\n"
372 " -version .... print version number and exit.\n"
James Zernddfee5d2013-03-07 19:31:27 -0800373 " -noicc ....... don't use the icc profile if present.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800374 " -nofancy ..... don't use the fancy YUV420 upscaler.\n"
375 " -nofilter .... disable in-loop filtering.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800376 " -mt .......... use multi-threading.\n"
skal68f282f2012-11-15 09:15:04 +0100377 " -info ........ print info.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800378 " -h ....... this help message.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800379 "\n"
380 "Keyboard shortcuts:\n"
James Zernddfee5d2013-03-07 19:31:27 -0800381 " 'c' ................ toggle use of color profile.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800382 " 'i' ................ overlay file information.\n"
383 " 'q' / 'Q' / ESC .... quit.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800384 );
385}
386
387int main(int argc, char *argv[]) {
388 WebPDecoderConfig config;
389 int c;
390
391 if (!WebPInitDecoderConfig(&config)) {
392 fprintf(stderr, "Library version mismatch!\n");
393 return -1;
394 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700395 kParams.config = &config;
James Zernddfee5d2013-03-07 19:31:27 -0800396 kParams.use_color_profile = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800397
398 for (c = 1; c < argc; ++c) {
399 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
400 Help();
401 return 0;
James Zernddfee5d2013-03-07 19:31:27 -0800402 } else if (!strcmp(argv[c], "-noicc")) {
403 kParams.use_color_profile = 0;
Pascal Massimino7937b402011-12-13 14:02:04 -0800404 } else if (!strcmp(argv[c], "-nofancy")) {
405 config.options.no_fancy_upsampling = 1;
406 } else if (!strcmp(argv[c], "-nofilter")) {
407 config.options.bypass_filtering = 1;
skal68f282f2012-11-15 09:15:04 +0100408 } else if (!strcmp(argv[c], "-info")) {
409 kParams.print_info = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800410 } else if (!strcmp(argv[c], "-version")) {
Urvang Joshia5042a32013-02-26 14:22:06 -0800411 const int dec_version = WebPGetDecoderVersion();
412 const int dmux_version = WebPGetDemuxVersion();
413 printf("WebP Decoder version: %d.%d.%d\nWebP Demux version: %d.%d.%d\n",
414 (dec_version >> 16) & 0xff, (dec_version >> 8) & 0xff,
415 dec_version & 0xff, (dmux_version >> 16) & 0xff,
416 (dmux_version >> 8) & 0xff, dmux_version & 0xff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800417 return 0;
418 } else if (!strcmp(argv[c], "-mt")) {
419 config.options.use_threads = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800420 } else if (argv[c][0] == '-') {
421 printf("Unknown option '%s'\n", argv[c]);
422 Help();
423 return -1;
424 } else {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700425 kParams.file_name = argv[c];
Pascal Massimino7937b402011-12-13 14:02:04 -0800426 }
427 }
428
James Zern061263a2012-05-11 16:00:57 -0700429 if (kParams.file_name == NULL) {
430 printf("missing input file!!\n");
431 Help();
432 return 0;
433 }
434
435 if (!ExUtilReadFile(kParams.file_name,
Urvang Joshia0770722012-10-30 14:54:46 -0700436 &kParams.data.bytes, &kParams.data.size)) {
James Zern061263a2012-05-11 16:00:57 -0700437 goto Error;
438 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700439
James Zernd7a5ac82012-09-26 23:44:59 -0700440 kParams.dmux = WebPDemux(&kParams.data);
441 if (kParams.dmux == NULL) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700442 fprintf(stderr, "Could not create demuxing object!\n");
443 goto Error;
Pascal Massimino7937b402011-12-13 14:02:04 -0800444 }
445
Urvang Joshia00a3da2012-10-31 17:49:15 -0700446 if (WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & FRAGMENTS_FLAG) {
447 fprintf(stderr, "Image fragments are not supported for now!\n");
Pascal Massimino861a5b72012-05-09 00:41:12 -0700448 goto Error;
449 }
skal68f282f2012-11-15 09:15:04 +0100450 kParams.canvas_width = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_WIDTH);
451 kParams.canvas_height = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_HEIGHT);
452 if (kParams.print_info) {
453 printf("Canvas: %d x %d\n", kParams.canvas_width, kParams.canvas_height);
454 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800455
James Zern04c7a2e2013-03-23 12:45:11 -0700456 kParams.prev_frame.width = kParams.canvas_width;
457 kParams.prev_frame.height = kParams.canvas_height;
458 kParams.prev_frame.x_offset = kParams.prev_frame.y_offset = 0;
459 kParams.prev_frame.dispose_method = WEBP_MUX_DISPOSE_BACKGROUND;
460
James Zernddfee5d2013-03-07 19:31:27 -0800461 memset(&kParams.iccp, 0, sizeof(kParams.iccp));
462 kParams.has_color_profile =
463 !!(WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & ICCP_FLAG);
464 if (kParams.has_color_profile) {
465#ifdef WEBP_HAVE_QCMS
466 if (!WebPDemuxGetChunk(kParams.dmux, "ICCP", 1, &kParams.iccp)) goto Error;
467 printf("VP8X: Found color profile\n");
468#else
469 fprintf(stderr, "Warning: color profile present, but qcms is unavailable!\n"
470 "Build libqcms from Mozilla or Chromium and define WEBP_HAVE_QCMS "
471 "before building.\n");
472#endif
473 }
474
James Zernd7a5ac82012-09-26 23:44:59 -0700475 if (!WebPDemuxGetFrame(kParams.dmux, 1, &kParams.frameiter)) goto Error;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700476
Urvang Joshia0770722012-10-30 14:54:46 -0700477 kParams.has_animation = (kParams.frameiter.num_frames > 1);
James Zernd7a5ac82012-09-26 23:44:59 -0700478 kParams.loop_count = (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT);
skal68f282f2012-11-15 09:15:04 +0100479 kParams.bg_color = WebPDemuxGetI(kParams.dmux, WEBP_FF_BACKGROUND_COLOR);
James Zernd7a5ac82012-09-26 23:44:59 -0700480 printf("VP8X: Found %d images in file (loop count = %d)\n",
Urvang Joshia0770722012-10-30 14:54:46 -0700481 kParams.frameiter.num_frames, kParams.loop_count);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700482
483 // Decode first frame
skal68f282f2012-11-15 09:15:04 +0100484 if (!Decode()) goto Error;
485
486 // Position iterator to last frame. Next call to HandleDisplay will wrap over.
487 // We take this into account by bumping up loop_count.
488 WebPDemuxGetFrame(kParams.dmux, 0, &kParams.frameiter);
489 if (kParams.loop_count) ++kParams.loop_count;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700490
491 // Start display (and timer)
Pascal Massimino7937b402011-12-13 14:02:04 -0800492 glutInit(&argc, argv);
James Zern880fd982012-05-25 14:53:46 -0700493#ifdef FREEGLUT
494 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
495#endif
skal68f282f2012-11-15 09:15:04 +0100496 StartDisplay();
497
Pascal Massimino861a5b72012-05-09 00:41:12 -0700498 if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0);
499 glutMainLoop();
Pascal Massimino7937b402011-12-13 14:02:04 -0800500
501 // Should only be reached when using FREEGLUT:
Pascal Massimino861a5b72012-05-09 00:41:12 -0700502 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800503 return 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700504
505 Error:
506 ClearParams();
507 return -1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800508}
509
510//------------------------------------------------------------------------------