blob: e231cde5b07041d95833b0a9d9939720571193b9 [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)
James Zern0e513f72013-05-01 14:47:56 -070017#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
Pascal Massimino7937b402011-12-13 14:02:04 -080020
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
James Zern0e513f72013-05-01 14:47:56 -070025#if defined(HAVE_GLUT_GLUT_H)
Pascal Massimino7937b402011-12-13 14:02:04 -080026#include <GLUT/glut.h>
27#else
28#include <GL/glut.h>
29#ifdef FREEGLUT
30#include <GL/freeglut.h>
31#endif
32#endif
33
James Zernddfee5d2013-03-07 19:31:27 -080034#ifdef WEBP_HAVE_QCMS
35#include <qcms.h>
36#endif
37
38#include "webp/decode.h"
39#include "webp/demux.h"
40
James Zern061263a2012-05-11 16:00:57 -070041#include "./example_util.h"
42
James Zern91179542011-12-16 19:30:33 -080043#ifdef _MSC_VER
44#define snprintf _snprintf
45#endif
46
Pascal Massimino861a5b72012-05-09 00:41:12 -070047static void Help(void);
48
49// Unfortunate global variables. Gathered into a struct for comfort.
50static struct {
51 int has_animation;
James Zernddfee5d2013-03-07 19:31:27 -080052 int has_color_profile;
Pascal Massimino861a5b72012-05-09 00:41:12 -070053 int done;
54 int decoding_error;
55 int print_info;
James Zernddfee5d2013-03-07 19:31:27 -080056 int use_color_profile;
Pascal Massimino861a5b72012-05-09 00:41:12 -070057
skal68f282f2012-11-15 09:15:04 +010058 int canvas_width, canvas_height;
Urvang Joshi6808e692012-07-06 11:35:36 +053059 int loop_count;
skal68f282f2012-11-15 09:15:04 +010060 uint32_t bg_color;
Pascal Massimino861a5b72012-05-09 00:41:12 -070061
62 const char* file_name;
63 WebPData data;
Pascal Massimino861a5b72012-05-09 00:41:12 -070064 WebPDecoderConfig* config;
65 const WebPDecBuffer* pic;
James Zernd7a5ac82012-09-26 23:44:59 -070066 WebPDemuxer* dmux;
67 WebPIterator frameiter;
James Zern04c7a2e2013-03-23 12:45:11 -070068 struct {
69 int width, height;
70 int x_offset, y_offset;
71 enum WebPMuxAnimDispose dispose_method;
72 } prev_frame;
James Zernddfee5d2013-03-07 19:31:27 -080073 WebPChunkIterator iccp;
James Zernd7a5ac82012-09-26 23:44:59 -070074} kParams;
Pascal Massimino861a5b72012-05-09 00:41:12 -070075
76static void ClearPreviousPic(void) {
77 WebPFreeDecBuffer((WebPDecBuffer*)kParams.pic);
78 kParams.pic = NULL;
79}
80
Pascal Massimino861a5b72012-05-09 00:41:12 -070081static void ClearParams(void) {
82 ClearPreviousPic();
Urvang Joshif3bab8e2012-06-07 17:19:02 +053083 WebPDataClear(&kParams.data);
James Zernd7a5ac82012-09-26 23:44:59 -070084 WebPDemuxReleaseIterator(&kParams.frameiter);
James Zernddfee5d2013-03-07 19:31:27 -080085 WebPDemuxReleaseChunkIterator(&kParams.iccp);
James Zernd7a5ac82012-09-26 23:44:59 -070086 WebPDemuxDelete(kParams.dmux);
87 kParams.dmux = NULL;
Pascal Massimino861a5b72012-05-09 00:41:12 -070088}
Pascal Massimino7937b402011-12-13 14:02:04 -080089
James Zernddfee5d2013-03-07 19:31:27 -080090// -----------------------------------------------------------------------------
91// Color profile handling
92static int ApplyColorProfile(const WebPData* const profile,
93 WebPDecBuffer* const rgba) {
94#ifdef WEBP_HAVE_QCMS
95 int i, ok = 0;
96 uint8_t* line;
97 uint8_t major_revision;
98 qcms_profile* input_profile = NULL;
99 qcms_profile* output_profile = NULL;
100 qcms_transform* transform = NULL;
101 const qcms_data_type input_type = QCMS_DATA_RGBA_8;
102 const qcms_data_type output_type = QCMS_DATA_RGBA_8;
103 const qcms_intent intent = QCMS_INTENT_DEFAULT;
104
105 if (profile == NULL || rgba == NULL) return 0;
106 if (profile->bytes == NULL || profile->size < 10) return 1;
107 major_revision = profile->bytes[8];
108
109 qcms_enable_iccv4();
110 input_profile = qcms_profile_from_memory(profile->bytes, profile->size);
111 // qcms_profile_is_bogus() is broken with ICCv4.
112 if (input_profile == NULL ||
113 (major_revision < 4 && qcms_profile_is_bogus(input_profile))) {
114 fprintf(stderr, "Color profile is bogus!\n");
115 goto Error;
116 }
117
118 output_profile = qcms_profile_sRGB();
119 if (output_profile == NULL) {
120 fprintf(stderr, "Error creating output color profile!\n");
121 goto Error;
122 }
123
124 qcms_profile_precache_output_transform(output_profile);
125 transform = qcms_transform_create(input_profile, input_type,
126 output_profile, output_type,
127 intent);
128 if (transform == NULL) {
129 fprintf(stderr, "Error creating color transform!\n");
130 goto Error;
131 }
132
133 line = rgba->u.RGBA.rgba;
134 for (i = 0; i < rgba->height; ++i, line += rgba->u.RGBA.stride) {
135 qcms_transform_data(transform, line, line, rgba->width);
136 }
137 ok = 1;
138
139 Error:
140 if (input_profile != NULL) qcms_profile_release(input_profile);
141 if (output_profile != NULL) qcms_profile_release(output_profile);
142 if (transform != NULL) qcms_transform_release(transform);
143 return ok;
144#else
145 (void)profile;
146 (void)rgba;
147 return 1;
148#endif // WEBP_HAVE_QCMS
149}
150
151//------------------------------------------------------------------------------
152// File decoding
153
154static int Decode(void) { // Fills kParams.frameiter
155 const WebPIterator* const iter = &kParams.frameiter;
156 WebPDecoderConfig* const config = kParams.config;
157 WebPDecBuffer* const output_buffer = &config->output;
158 int ok = 0;
159
160 ClearPreviousPic();
161 output_buffer->colorspace = MODE_RGBA;
162 ok = (WebPDecode(iter->fragment.bytes, iter->fragment.size,
163 config) == VP8_STATUS_OK);
164 if (!ok) {
165 fprintf(stderr, "Decoding of frame #%d failed!\n", iter->frame_num);
166 } else {
167 kParams.pic = output_buffer;
168 if (kParams.use_color_profile) {
169 ok = ApplyColorProfile(&kParams.iccp.chunk, output_buffer);
170 if (!ok) {
171 fprintf(stderr, "Applying color profile to frame #%d failed!\n",
172 iter->frame_num);
173 }
174 }
175 }
176 return ok;
177}
178
179static void decode_callback(int what) {
180 if (what == 0 && !kParams.done) {
181 int duration = 0;
182 if (kParams.dmux != NULL) {
183 WebPIterator* const iter = &kParams.frameiter;
184 if (!WebPDemuxNextFrame(iter)) {
185 WebPDemuxReleaseIterator(iter);
186 if (WebPDemuxGetFrame(kParams.dmux, 1, iter)) {
187 --kParams.loop_count;
188 kParams.done = (kParams.loop_count == 0);
189 } else {
190 kParams.decoding_error = 1;
191 kParams.done = 1;
192 return;
193 }
194 }
195 duration = iter->duration;
196 }
197 if (!Decode()) {
198 kParams.decoding_error = 1;
199 kParams.done = 1;
200 } else {
201 glutPostRedisplay();
202 glutTimerFunc(duration, decode_callback, what);
203 }
204 }
205}
206
Pascal Massimino7937b402011-12-13 14:02:04 -0800207//------------------------------------------------------------------------------
208// Callbacks
209
210static void HandleKey(unsigned char key, int pos_x, int pos_y) {
211 (void)pos_x;
212 (void)pos_y;
213 if (key == 'q' || key == 'Q' || key == 27 /* Esc */) {
214#ifdef FREEGLUT
215 glutLeaveMainLoop();
216#else
Pascal Massimino861a5b72012-05-09 00:41:12 -0700217 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800218 exit(0);
219#endif
James Zernddfee5d2013-03-07 19:31:27 -0800220 } else if (key == 'c') {
221 if (kParams.has_color_profile && !kParams.decoding_error) {
222 kParams.use_color_profile = 1 - kParams.use_color_profile;
223
224 if (kParams.has_animation) {
225 // Restart the completed animation to pickup the color profile change.
226 if (kParams.done && kParams.loop_count == 0) {
227 kParams.loop_count =
228 (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT) + 1;
229 kParams.done = 0;
230 // Start the decode loop immediately.
231 glutTimerFunc(0, decode_callback, 0);
232 }
233 } else {
234 Decode();
235 glutPostRedisplay();
236 }
237 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800238 } else if (key == 'i') {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700239 kParams.print_info = 1 - kParams.print_info;
Pascal Massimino7937b402011-12-13 14:02:04 -0800240 glutPostRedisplay();
241 }
242}
243
244static void HandleReshape(int width, int height) {
245 // TODO(skal): proper handling of resize, esp. for large pictures.
246 // + key control of the zoom.
247 glViewport(0, 0, width, height);
248 glMatrixMode(GL_PROJECTION);
249 glLoadIdentity();
250 glMatrixMode(GL_MODELVIEW);
251 glLoadIdentity();
252}
253
254static void PrintString(const char* const text) {
255 void* const font = GLUT_BITMAP_9_BY_15;
256 int i;
257 for (i = 0; text[i]; ++i) {
258 glutBitmapCharacter(font, text[i]);
259 }
260}
261
skal68f282f2012-11-15 09:15:04 +0100262static float GetColorf(uint32_t color, int shift) {
263 return (color >> shift) / 255.f;
264}
265
James Zerna73b8972012-07-09 23:01:52 -0700266static void DrawCheckerBoard(void) {
267 const int square_size = 8; // must be a power of 2
268 int x, y;
269 GLint viewport[4]; // x, y, width, height
270
271 glPushMatrix();
272
273 glGetIntegerv(GL_VIEWPORT, viewport);
274 // shift to integer coordinates with (0,0) being top-left.
275 glOrtho(0, viewport[2], viewport[3], 0, -1, 1);
276 for (y = 0; y < viewport[3]; y += square_size) {
277 for (x = 0; x < viewport[2]; x += square_size) {
278 const GLubyte color = 128 + 64 * (!((x + y) & square_size));
279 glColor3ub(color, color, color);
280 glRecti(x, y, x + square_size, y + square_size);
281 }
282 }
283 glPopMatrix();
284}
285
Pascal Massimino7937b402011-12-13 14:02:04 -0800286static void HandleDisplay(void) {
skal68f282f2012-11-15 09:15:04 +0100287 const WebPDecBuffer* const pic = kParams.pic;
288 const WebPIterator* const iter = &kParams.frameiter;
skal41a6ced2012-11-15 09:35:01 +0100289 GLfloat xoff, yoff;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700290 if (pic == NULL) return;
Pascal Massimino7937b402011-12-13 14:02:04 -0800291 glPushMatrix();
292 glPixelZoom(1, -1);
skal41a6ced2012-11-15 09:35:01 +0100293 xoff = (GLfloat)(2. * iter->x_offset / kParams.canvas_width);
294 yoff = (GLfloat)(2. * iter->y_offset / kParams.canvas_height);
James Zern013023e2013-03-15 12:17:45 -0700295 glRasterPos2f(-1.f + xoff, 1.f - yoff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800296 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700297 glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4);
James Zern04c7a2e2013-03-23 12:45:11 -0700298
299 if (kParams.prev_frame.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {
300 // TODO(later): these offsets and those above should factor in window size.
301 // they will be incorrect if the window is resized.
302 // glScissor() takes window coordinates (0,0 at bottom left).
303 const int window_x = kParams.prev_frame.x_offset;
304 const int window_y = kParams.canvas_height -
305 kParams.prev_frame.y_offset -
306 kParams.prev_frame.height;
307 glEnable(GL_SCISSOR_TEST);
308 // Only updated the requested area, not the whole canvas.
309 glScissor(window_x, window_y,
310 kParams.prev_frame.width, kParams.prev_frame.height);
311
skal68f282f2012-11-15 09:15:04 +0100312 glClear(GL_COLOR_BUFFER_BIT); // use clear color
James Zern04c7a2e2013-03-23 12:45:11 -0700313 DrawCheckerBoard();
314
315 glDisable(GL_SCISSOR_TEST);
skal68f282f2012-11-15 09:15:04 +0100316 }
James Zern04c7a2e2013-03-23 12:45:11 -0700317 kParams.prev_frame.width = iter->width;
318 kParams.prev_frame.height = iter->height;
319 kParams.prev_frame.x_offset = iter->x_offset;
320 kParams.prev_frame.y_offset = iter->y_offset;
321 kParams.prev_frame.dispose_method = iter->dispose_method;
322
Pascal Massimino861a5b72012-05-09 00:41:12 -0700323 glDrawPixels(pic->width, pic->height,
324 GL_RGBA, GL_UNSIGNED_BYTE,
325 (GLvoid*)pic->u.RGBA.rgba);
326 if (kParams.print_info) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800327 char tmp[32];
328
James Zern013023e2013-03-15 12:17:45 -0700329 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800330 glRasterPos2f(-0.95f, 0.90f);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700331 PrintString(kParams.file_name);
Pascal Massimino7937b402011-12-13 14:02:04 -0800332
Pascal Massimino861a5b72012-05-09 00:41:12 -0700333 snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height);
James Zern013023e2013-03-15 12:17:45 -0700334 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800335 glRasterPos2f(-0.95f, 0.80f);
Pascal Massimino7937b402011-12-13 14:02:04 -0800336 PrintString(tmp);
skal68f282f2012-11-15 09:15:04 +0100337 if (iter->x_offset != 0 || iter->y_offset != 0) {
338 snprintf(tmp, sizeof(tmp), " (offset:%d,%d)",
339 iter->x_offset, iter->y_offset);
340 glRasterPos2f(-0.95f, 0.70f);
341 PrintString(tmp);
342 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800343 }
James Zerna73b8972012-07-09 23:01:52 -0700344 glPopMatrix();
Pascal Massimino7937b402011-12-13 14:02:04 -0800345 glFlush();
346}
347
skal68f282f2012-11-15 09:15:04 +0100348static void StartDisplay(void) {
349 const int width = kParams.canvas_width;
350 const int height = kParams.canvas_height;
Pascal Massimino7937b402011-12-13 14:02:04 -0800351 glutInitDisplayMode(GLUT_RGBA);
skal68f282f2012-11-15 09:15:04 +0100352 glutInitWindowSize(width, height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800353 glutCreateWindow("WebP viewer");
Pascal Massimino7937b402011-12-13 14:02:04 -0800354 glutDisplayFunc(HandleDisplay);
355 glutIdleFunc(NULL);
356 glutKeyboardFunc(HandleKey);
Urvang Joshi08220102012-06-20 11:18:35 -0700357 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
358 glEnable(GL_BLEND);
skal68f282f2012-11-15 09:15:04 +0100359 glClearColor(GetColorf(kParams.bg_color, 0),
360 GetColorf(kParams.bg_color, 8),
361 GetColorf(kParams.bg_color, 16),
362 GetColorf(kParams.bg_color, 24));
363 HandleReshape(width, height);
364 glClear(GL_COLOR_BUFFER_BIT);
365 DrawCheckerBoard();
Pascal Massimino7937b402011-12-13 14:02:04 -0800366}
367
368//------------------------------------------------------------------------------
Pascal Massimino7937b402011-12-13 14:02:04 -0800369// Main
370
371static void Help(void) {
372 printf("Usage: vwebp in_file [options]\n\n"
373 "Decodes the WebP image file and visualize it using OpenGL\n"
374 "Options are:\n"
375 " -version .... print version number and exit.\n"
James Zernddfee5d2013-03-07 19:31:27 -0800376 " -noicc ....... don't use the icc profile if present.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800377 " -nofancy ..... don't use the fancy YUV420 upscaler.\n"
378 " -nofilter .... disable in-loop filtering.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800379 " -mt .......... use multi-threading.\n"
skal68f282f2012-11-15 09:15:04 +0100380 " -info ........ print info.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800381 " -h ....... this help message.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800382 "\n"
383 "Keyboard shortcuts:\n"
James Zernddfee5d2013-03-07 19:31:27 -0800384 " 'c' ................ toggle use of color profile.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800385 " 'i' ................ overlay file information.\n"
386 " 'q' / 'Q' / ESC .... quit.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800387 );
388}
389
390int main(int argc, char *argv[]) {
391 WebPDecoderConfig config;
392 int c;
393
394 if (!WebPInitDecoderConfig(&config)) {
395 fprintf(stderr, "Library version mismatch!\n");
396 return -1;
397 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700398 kParams.config = &config;
James Zernddfee5d2013-03-07 19:31:27 -0800399 kParams.use_color_profile = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800400
401 for (c = 1; c < argc; ++c) {
402 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
403 Help();
404 return 0;
James Zernddfee5d2013-03-07 19:31:27 -0800405 } else if (!strcmp(argv[c], "-noicc")) {
406 kParams.use_color_profile = 0;
Pascal Massimino7937b402011-12-13 14:02:04 -0800407 } else if (!strcmp(argv[c], "-nofancy")) {
408 config.options.no_fancy_upsampling = 1;
409 } else if (!strcmp(argv[c], "-nofilter")) {
410 config.options.bypass_filtering = 1;
skal68f282f2012-11-15 09:15:04 +0100411 } else if (!strcmp(argv[c], "-info")) {
412 kParams.print_info = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800413 } else if (!strcmp(argv[c], "-version")) {
Urvang Joshia5042a32013-02-26 14:22:06 -0800414 const int dec_version = WebPGetDecoderVersion();
415 const int dmux_version = WebPGetDemuxVersion();
416 printf("WebP Decoder version: %d.%d.%d\nWebP Demux version: %d.%d.%d\n",
417 (dec_version >> 16) & 0xff, (dec_version >> 8) & 0xff,
418 dec_version & 0xff, (dmux_version >> 16) & 0xff,
419 (dmux_version >> 8) & 0xff, dmux_version & 0xff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800420 return 0;
421 } else if (!strcmp(argv[c], "-mt")) {
422 config.options.use_threads = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800423 } else if (argv[c][0] == '-') {
424 printf("Unknown option '%s'\n", argv[c]);
425 Help();
426 return -1;
427 } else {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700428 kParams.file_name = argv[c];
Pascal Massimino7937b402011-12-13 14:02:04 -0800429 }
430 }
431
James Zern061263a2012-05-11 16:00:57 -0700432 if (kParams.file_name == NULL) {
433 printf("missing input file!!\n");
434 Help();
435 return 0;
436 }
437
438 if (!ExUtilReadFile(kParams.file_name,
Urvang Joshia0770722012-10-30 14:54:46 -0700439 &kParams.data.bytes, &kParams.data.size)) {
James Zern061263a2012-05-11 16:00:57 -0700440 goto Error;
441 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700442
James Zernd7a5ac82012-09-26 23:44:59 -0700443 kParams.dmux = WebPDemux(&kParams.data);
444 if (kParams.dmux == NULL) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700445 fprintf(stderr, "Could not create demuxing object!\n");
446 goto Error;
Pascal Massimino7937b402011-12-13 14:02:04 -0800447 }
448
Urvang Joshia00a3da2012-10-31 17:49:15 -0700449 if (WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & FRAGMENTS_FLAG) {
450 fprintf(stderr, "Image fragments are not supported for now!\n");
Pascal Massimino861a5b72012-05-09 00:41:12 -0700451 goto Error;
452 }
skal68f282f2012-11-15 09:15:04 +0100453 kParams.canvas_width = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_WIDTH);
454 kParams.canvas_height = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_HEIGHT);
455 if (kParams.print_info) {
456 printf("Canvas: %d x %d\n", kParams.canvas_width, kParams.canvas_height);
457 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800458
James Zern04c7a2e2013-03-23 12:45:11 -0700459 kParams.prev_frame.width = kParams.canvas_width;
460 kParams.prev_frame.height = kParams.canvas_height;
461 kParams.prev_frame.x_offset = kParams.prev_frame.y_offset = 0;
462 kParams.prev_frame.dispose_method = WEBP_MUX_DISPOSE_BACKGROUND;
463
James Zernddfee5d2013-03-07 19:31:27 -0800464 memset(&kParams.iccp, 0, sizeof(kParams.iccp));
465 kParams.has_color_profile =
466 !!(WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & ICCP_FLAG);
467 if (kParams.has_color_profile) {
468#ifdef WEBP_HAVE_QCMS
469 if (!WebPDemuxGetChunk(kParams.dmux, "ICCP", 1, &kParams.iccp)) goto Error;
470 printf("VP8X: Found color profile\n");
471#else
472 fprintf(stderr, "Warning: color profile present, but qcms is unavailable!\n"
473 "Build libqcms from Mozilla or Chromium and define WEBP_HAVE_QCMS "
474 "before building.\n");
475#endif
476 }
477
James Zernd7a5ac82012-09-26 23:44:59 -0700478 if (!WebPDemuxGetFrame(kParams.dmux, 1, &kParams.frameiter)) goto Error;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700479
Urvang Joshia0770722012-10-30 14:54:46 -0700480 kParams.has_animation = (kParams.frameiter.num_frames > 1);
James Zernd7a5ac82012-09-26 23:44:59 -0700481 kParams.loop_count = (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT);
skal68f282f2012-11-15 09:15:04 +0100482 kParams.bg_color = WebPDemuxGetI(kParams.dmux, WEBP_FF_BACKGROUND_COLOR);
James Zernd7a5ac82012-09-26 23:44:59 -0700483 printf("VP8X: Found %d images in file (loop count = %d)\n",
Urvang Joshia0770722012-10-30 14:54:46 -0700484 kParams.frameiter.num_frames, kParams.loop_count);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700485
486 // Decode first frame
skal68f282f2012-11-15 09:15:04 +0100487 if (!Decode()) goto Error;
488
489 // Position iterator to last frame. Next call to HandleDisplay will wrap over.
490 // We take this into account by bumping up loop_count.
491 WebPDemuxGetFrame(kParams.dmux, 0, &kParams.frameiter);
492 if (kParams.loop_count) ++kParams.loop_count;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700493
494 // Start display (and timer)
Pascal Massimino7937b402011-12-13 14:02:04 -0800495 glutInit(&argc, argv);
James Zern880fd982012-05-25 14:53:46 -0700496#ifdef FREEGLUT
497 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
498#endif
skal68f282f2012-11-15 09:15:04 +0100499 StartDisplay();
500
Pascal Massimino861a5b72012-05-09 00:41:12 -0700501 if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0);
502 glutMainLoop();
Pascal Massimino7937b402011-12-13 14:02:04 -0800503
504 // Should only be reached when using FREEGLUT:
Pascal Massimino861a5b72012-05-09 00:41:12 -0700505 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800506 return 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700507
508 Error:
509 ClearParams();
510 return -1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800511}
512
513//------------------------------------------------------------------------------