blob: eeaddc8607d6f4108c44cbfbabb2d2463fe5aed6 [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;
Urvang Joshidcf65222013-08-09 14:40:31 -070063 WebPIterator curr_frame;
64 WebPIterator prev_frame;
James Zernddfee5d2013-03-07 19:31:27 -080065 WebPChunkIterator iccp;
James Zernd7a5ac82012-09-26 23:44:59 -070066} kParams;
Pascal Massimino861a5b72012-05-09 00:41:12 -070067
68static void ClearPreviousPic(void) {
69 WebPFreeDecBuffer((WebPDecBuffer*)kParams.pic);
70 kParams.pic = NULL;
71}
72
Pascal Massimino861a5b72012-05-09 00:41:12 -070073static void ClearParams(void) {
74 ClearPreviousPic();
Urvang Joshif3bab8e2012-06-07 17:19:02 +053075 WebPDataClear(&kParams.data);
Urvang Joshidcf65222013-08-09 14:40:31 -070076 WebPDemuxReleaseIterator(&kParams.curr_frame);
77 WebPDemuxReleaseIterator(&kParams.prev_frame);
James Zernddfee5d2013-03-07 19:31:27 -080078 WebPDemuxReleaseChunkIterator(&kParams.iccp);
James Zernd7a5ac82012-09-26 23:44:59 -070079 WebPDemuxDelete(kParams.dmux);
80 kParams.dmux = NULL;
Pascal Massimino861a5b72012-05-09 00:41:12 -070081}
Pascal Massimino7937b402011-12-13 14:02:04 -080082
James Zernddfee5d2013-03-07 19:31:27 -080083// -----------------------------------------------------------------------------
84// Color profile handling
85static int ApplyColorProfile(const WebPData* const profile,
86 WebPDecBuffer* const rgba) {
87#ifdef WEBP_HAVE_QCMS
88 int i, ok = 0;
89 uint8_t* line;
90 uint8_t major_revision;
91 qcms_profile* input_profile = NULL;
92 qcms_profile* output_profile = NULL;
93 qcms_transform* transform = NULL;
94 const qcms_data_type input_type = QCMS_DATA_RGBA_8;
95 const qcms_data_type output_type = QCMS_DATA_RGBA_8;
96 const qcms_intent intent = QCMS_INTENT_DEFAULT;
97
98 if (profile == NULL || rgba == NULL) return 0;
99 if (profile->bytes == NULL || profile->size < 10) return 1;
100 major_revision = profile->bytes[8];
101
102 qcms_enable_iccv4();
103 input_profile = qcms_profile_from_memory(profile->bytes, profile->size);
104 // qcms_profile_is_bogus() is broken with ICCv4.
105 if (input_profile == NULL ||
106 (major_revision < 4 && qcms_profile_is_bogus(input_profile))) {
107 fprintf(stderr, "Color profile is bogus!\n");
108 goto Error;
109 }
110
111 output_profile = qcms_profile_sRGB();
112 if (output_profile == NULL) {
113 fprintf(stderr, "Error creating output color profile!\n");
114 goto Error;
115 }
116
117 qcms_profile_precache_output_transform(output_profile);
118 transform = qcms_transform_create(input_profile, input_type,
119 output_profile, output_type,
120 intent);
121 if (transform == NULL) {
122 fprintf(stderr, "Error creating color transform!\n");
123 goto Error;
124 }
125
126 line = rgba->u.RGBA.rgba;
127 for (i = 0; i < rgba->height; ++i, line += rgba->u.RGBA.stride) {
128 qcms_transform_data(transform, line, line, rgba->width);
129 }
130 ok = 1;
131
132 Error:
133 if (input_profile != NULL) qcms_profile_release(input_profile);
134 if (output_profile != NULL) qcms_profile_release(output_profile);
135 if (transform != NULL) qcms_transform_release(transform);
136 return ok;
137#else
138 (void)profile;
139 (void)rgba;
140 return 1;
141#endif // WEBP_HAVE_QCMS
142}
143
144//------------------------------------------------------------------------------
145// File decoding
146
Urvang Joshidcf65222013-08-09 14:40:31 -0700147static int Decode(void) { // Fills kParams.curr_frame
148 const WebPIterator* const curr = &kParams.curr_frame;
James Zernddfee5d2013-03-07 19:31:27 -0800149 WebPDecoderConfig* const config = kParams.config;
150 WebPDecBuffer* const output_buffer = &config->output;
151 int ok = 0;
152
153 ClearPreviousPic();
154 output_buffer->colorspace = MODE_RGBA;
Urvang Joshidcf65222013-08-09 14:40:31 -0700155 ok = (WebPDecode(curr->fragment.bytes, curr->fragment.size,
James Zernddfee5d2013-03-07 19:31:27 -0800156 config) == VP8_STATUS_OK);
157 if (!ok) {
Urvang Joshidcf65222013-08-09 14:40:31 -0700158 fprintf(stderr, "Decoding of frame #%d failed!\n", curr->frame_num);
James Zernddfee5d2013-03-07 19:31:27 -0800159 } else {
160 kParams.pic = output_buffer;
161 if (kParams.use_color_profile) {
162 ok = ApplyColorProfile(&kParams.iccp.chunk, output_buffer);
163 if (!ok) {
164 fprintf(stderr, "Applying color profile to frame #%d failed!\n",
Urvang Joshidcf65222013-08-09 14:40:31 -0700165 curr->frame_num);
James Zernddfee5d2013-03-07 19:31:27 -0800166 }
167 }
168 }
169 return ok;
170}
171
172static void decode_callback(int what) {
173 if (what == 0 && !kParams.done) {
174 int duration = 0;
175 if (kParams.dmux != NULL) {
Urvang Joshidcf65222013-08-09 14:40:31 -0700176 WebPIterator* const curr = &kParams.curr_frame;
177 if (!WebPDemuxNextFrame(curr)) {
178 WebPDemuxReleaseIterator(curr);
179 if (WebPDemuxGetFrame(kParams.dmux, 1, curr)) {
James Zernddfee5d2013-03-07 19:31:27 -0800180 --kParams.loop_count;
181 kParams.done = (kParams.loop_count == 0);
182 } else {
183 kParams.decoding_error = 1;
184 kParams.done = 1;
185 return;
186 }
187 }
Urvang Joshidcf65222013-08-09 14:40:31 -0700188 duration = curr->duration;
James Zernddfee5d2013-03-07 19:31:27 -0800189 }
190 if (!Decode()) {
191 kParams.decoding_error = 1;
192 kParams.done = 1;
193 } else {
194 glutPostRedisplay();
195 glutTimerFunc(duration, decode_callback, what);
196 }
197 }
198}
199
Pascal Massimino7937b402011-12-13 14:02:04 -0800200//------------------------------------------------------------------------------
201// Callbacks
202
203static void HandleKey(unsigned char key, int pos_x, int pos_y) {
204 (void)pos_x;
205 (void)pos_y;
206 if (key == 'q' || key == 'Q' || key == 27 /* Esc */) {
207#ifdef FREEGLUT
208 glutLeaveMainLoop();
209#else
Pascal Massimino861a5b72012-05-09 00:41:12 -0700210 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800211 exit(0);
212#endif
James Zernddfee5d2013-03-07 19:31:27 -0800213 } else if (key == 'c') {
214 if (kParams.has_color_profile && !kParams.decoding_error) {
215 kParams.use_color_profile = 1 - kParams.use_color_profile;
216
217 if (kParams.has_animation) {
218 // Restart the completed animation to pickup the color profile change.
219 if (kParams.done && kParams.loop_count == 0) {
220 kParams.loop_count =
221 (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT) + 1;
222 kParams.done = 0;
223 // Start the decode loop immediately.
224 glutTimerFunc(0, decode_callback, 0);
225 }
226 } else {
227 Decode();
228 glutPostRedisplay();
229 }
230 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800231 } else if (key == 'i') {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700232 kParams.print_info = 1 - kParams.print_info;
Pascal Massimino7937b402011-12-13 14:02:04 -0800233 glutPostRedisplay();
234 }
235}
236
237static void HandleReshape(int width, int height) {
238 // TODO(skal): proper handling of resize, esp. for large pictures.
239 // + key control of the zoom.
240 glViewport(0, 0, width, height);
241 glMatrixMode(GL_PROJECTION);
242 glLoadIdentity();
243 glMatrixMode(GL_MODELVIEW);
244 glLoadIdentity();
245}
246
247static void PrintString(const char* const text) {
248 void* const font = GLUT_BITMAP_9_BY_15;
249 int i;
250 for (i = 0; text[i]; ++i) {
251 glutBitmapCharacter(font, text[i]);
252 }
253}
254
skal68f282f2012-11-15 09:15:04 +0100255static float GetColorf(uint32_t color, int shift) {
256 return (color >> shift) / 255.f;
257}
258
James Zerna73b8972012-07-09 23:01:52 -0700259static void DrawCheckerBoard(void) {
260 const int square_size = 8; // must be a power of 2
261 int x, y;
262 GLint viewport[4]; // x, y, width, height
263
264 glPushMatrix();
265
266 glGetIntegerv(GL_VIEWPORT, viewport);
267 // shift to integer coordinates with (0,0) being top-left.
268 glOrtho(0, viewport[2], viewport[3], 0, -1, 1);
269 for (y = 0; y < viewport[3]; y += square_size) {
270 for (x = 0; x < viewport[2]; x += square_size) {
271 const GLubyte color = 128 + 64 * (!((x + y) & square_size));
272 glColor3ub(color, color, color);
273 glRecti(x, y, x + square_size, y + square_size);
274 }
275 }
276 glPopMatrix();
277}
278
Pascal Massimino7937b402011-12-13 14:02:04 -0800279static void HandleDisplay(void) {
skal68f282f2012-11-15 09:15:04 +0100280 const WebPDecBuffer* const pic = kParams.pic;
Urvang Joshidcf65222013-08-09 14:40:31 -0700281 const WebPIterator* const curr = &kParams.curr_frame;
282 WebPIterator* const prev = &kParams.prev_frame;
skal41a6ced2012-11-15 09:35:01 +0100283 GLfloat xoff, yoff;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700284 if (pic == NULL) return;
Pascal Massimino7937b402011-12-13 14:02:04 -0800285 glPushMatrix();
286 glPixelZoom(1, -1);
Urvang Joshidcf65222013-08-09 14:40:31 -0700287 xoff = (GLfloat)(2. * curr->x_offset / kParams.canvas_width);
288 yoff = (GLfloat)(2. * curr->y_offset / kParams.canvas_height);
James Zern013023e2013-03-15 12:17:45 -0700289 glRasterPos2f(-1.f + xoff, 1.f - yoff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800290 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700291 glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4);
James Zern04c7a2e2013-03-23 12:45:11 -0700292
Urvang Joshidcf65222013-08-09 14:40:31 -0700293 if (prev->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ||
294 curr->blend_method == WEBP_MUX_NO_BLEND) {
James Zern04c7a2e2013-03-23 12:45:11 -0700295 // TODO(later): these offsets and those above should factor in window size.
296 // they will be incorrect if the window is resized.
297 // glScissor() takes window coordinates (0,0 at bottom left).
Urvang Joshidcf65222013-08-09 14:40:31 -0700298 int window_x, window_y;
299 if (prev->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {
300 // Clear the previous frame rectangle.
301 window_x = prev->x_offset;
302 window_y = kParams.canvas_height - prev->y_offset - prev->height;
303 } else { // curr->blend_method == WEBP_MUX_NO_BLEND.
304 // We simulate no-blending behavior by first clearing the current frame
305 // rectangle (to a checker-board) and then alpha-blending against it.
306 window_x = curr->x_offset;
307 window_y = kParams.canvas_height - curr->y_offset - curr->height;
308 }
James Zern04c7a2e2013-03-23 12:45:11 -0700309 glEnable(GL_SCISSOR_TEST);
Urvang Joshidcf65222013-08-09 14:40:31 -0700310 // Only update the requested area, not the whole canvas.
311 glScissor(window_x, window_y, prev->width, prev->height);
James Zern04c7a2e2013-03-23 12:45:11 -0700312
skal68f282f2012-11-15 09:15:04 +0100313 glClear(GL_COLOR_BUFFER_BIT); // use clear color
James Zern04c7a2e2013-03-23 12:45:11 -0700314 DrawCheckerBoard();
315
316 glDisable(GL_SCISSOR_TEST);
skal68f282f2012-11-15 09:15:04 +0100317 }
Urvang Joshidcf65222013-08-09 14:40:31 -0700318
319 *prev = *curr;
James Zern04c7a2e2013-03-23 12:45:11 -0700320
Pascal Massimino861a5b72012-05-09 00:41:12 -0700321 glDrawPixels(pic->width, pic->height,
322 GL_RGBA, GL_UNSIGNED_BYTE,
323 (GLvoid*)pic->u.RGBA.rgba);
324 if (kParams.print_info) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800325 char tmp[32];
326
James Zern013023e2013-03-15 12:17:45 -0700327 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800328 glRasterPos2f(-0.95f, 0.90f);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700329 PrintString(kParams.file_name);
Pascal Massimino7937b402011-12-13 14:02:04 -0800330
Pascal Massimino861a5b72012-05-09 00:41:12 -0700331 snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height);
James Zern013023e2013-03-15 12:17:45 -0700332 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800333 glRasterPos2f(-0.95f, 0.80f);
Pascal Massimino7937b402011-12-13 14:02:04 -0800334 PrintString(tmp);
Urvang Joshidcf65222013-08-09 14:40:31 -0700335 if (curr->x_offset != 0 || curr->y_offset != 0) {
skal68f282f2012-11-15 09:15:04 +0100336 snprintf(tmp, sizeof(tmp), " (offset:%d,%d)",
Urvang Joshidcf65222013-08-09 14:40:31 -0700337 curr->x_offset, curr->y_offset);
skal68f282f2012-11-15 09:15:04 +0100338 glRasterPos2f(-0.95f, 0.70f);
339 PrintString(tmp);
340 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800341 }
James Zerna73b8972012-07-09 23:01:52 -0700342 glPopMatrix();
Pascal Massimino7937b402011-12-13 14:02:04 -0800343 glFlush();
344}
345
skal68f282f2012-11-15 09:15:04 +0100346static void StartDisplay(void) {
347 const int width = kParams.canvas_width;
348 const int height = kParams.canvas_height;
Pascal Massimino7937b402011-12-13 14:02:04 -0800349 glutInitDisplayMode(GLUT_RGBA);
skal68f282f2012-11-15 09:15:04 +0100350 glutInitWindowSize(width, height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800351 glutCreateWindow("WebP viewer");
Pascal Massimino7937b402011-12-13 14:02:04 -0800352 glutDisplayFunc(HandleDisplay);
353 glutIdleFunc(NULL);
354 glutKeyboardFunc(HandleKey);
Urvang Joshi08220102012-06-20 11:18:35 -0700355 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
356 glEnable(GL_BLEND);
skal68f282f2012-11-15 09:15:04 +0100357 glClearColor(GetColorf(kParams.bg_color, 0),
358 GetColorf(kParams.bg_color, 8),
359 GetColorf(kParams.bg_color, 16),
360 GetColorf(kParams.bg_color, 24));
361 HandleReshape(width, height);
362 glClear(GL_COLOR_BUFFER_BIT);
363 DrawCheckerBoard();
Pascal Massimino7937b402011-12-13 14:02:04 -0800364}
365
366//------------------------------------------------------------------------------
Pascal Massimino7937b402011-12-13 14:02:04 -0800367// Main
368
369static void Help(void) {
370 printf("Usage: vwebp in_file [options]\n\n"
371 "Decodes the WebP image file and visualize it using OpenGL\n"
372 "Options are:\n"
373 " -version .... print version number and exit.\n"
James Zernddfee5d2013-03-07 19:31:27 -0800374 " -noicc ....... don't use the icc profile if present.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800375 " -nofancy ..... don't use the fancy YUV420 upscaler.\n"
376 " -nofilter .... disable in-loop filtering.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800377 " -mt .......... use multi-threading.\n"
skal68f282f2012-11-15 09:15:04 +0100378 " -info ........ print info.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800379 " -h ....... this help message.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800380 "\n"
381 "Keyboard shortcuts:\n"
James Zernddfee5d2013-03-07 19:31:27 -0800382 " 'c' ................ toggle use of color profile.\n"
James Zern03cc23d2013-03-07 19:10:59 -0800383 " 'i' ................ overlay file information.\n"
384 " 'q' / 'Q' / ESC .... quit.\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800385 );
386}
387
388int main(int argc, char *argv[]) {
389 WebPDecoderConfig config;
390 int c;
Urvang Joshidcf65222013-08-09 14:40:31 -0700391 const WebPIterator* const curr = &kParams.curr_frame;
392 WebPIterator* const prev = &kParams.prev_frame;
Pascal Massimino7937b402011-12-13 14:02:04 -0800393
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
Pascal Massimino830f72b2013-06-10 05:46:22 -0700443 if (!WebPGetInfo(kParams.data.bytes, kParams.data.size, NULL, NULL)) {
444 fprintf(stderr, "Input file doesn't appear to be WebP format.\n");
445 goto Error;
446 }
447
James Zernd7a5ac82012-09-26 23:44:59 -0700448 kParams.dmux = WebPDemux(&kParams.data);
449 if (kParams.dmux == NULL) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700450 fprintf(stderr, "Could not create demuxing object!\n");
451 goto Error;
Pascal Massimino7937b402011-12-13 14:02:04 -0800452 }
453
Urvang Joshia00a3da2012-10-31 17:49:15 -0700454 if (WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & FRAGMENTS_FLAG) {
455 fprintf(stderr, "Image fragments are not supported for now!\n");
Pascal Massimino861a5b72012-05-09 00:41:12 -0700456 goto Error;
457 }
skal68f282f2012-11-15 09:15:04 +0100458 kParams.canvas_width = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_WIDTH);
459 kParams.canvas_height = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_HEIGHT);
460 if (kParams.print_info) {
461 printf("Canvas: %d x %d\n", kParams.canvas_width, kParams.canvas_height);
462 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800463
Urvang Joshidcf65222013-08-09 14:40:31 -0700464 prev->width = kParams.canvas_width;
465 prev->height = kParams.canvas_height;
466 prev->x_offset = prev->y_offset = 0;
467 prev->dispose_method = WEBP_MUX_DISPOSE_BACKGROUND;
James Zern04c7a2e2013-03-23 12:45:11 -0700468
James Zernddfee5d2013-03-07 19:31:27 -0800469 memset(&kParams.iccp, 0, sizeof(kParams.iccp));
470 kParams.has_color_profile =
471 !!(WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & ICCP_FLAG);
472 if (kParams.has_color_profile) {
473#ifdef WEBP_HAVE_QCMS
474 if (!WebPDemuxGetChunk(kParams.dmux, "ICCP", 1, &kParams.iccp)) goto Error;
475 printf("VP8X: Found color profile\n");
476#else
477 fprintf(stderr, "Warning: color profile present, but qcms is unavailable!\n"
478 "Build libqcms from Mozilla or Chromium and define WEBP_HAVE_QCMS "
479 "before building.\n");
480#endif
481 }
482
Urvang Joshidcf65222013-08-09 14:40:31 -0700483 if (!WebPDemuxGetFrame(kParams.dmux, 1, curr)) goto Error;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700484
Urvang Joshidcf65222013-08-09 14:40:31 -0700485 kParams.has_animation = (curr->num_frames > 1);
James Zernd7a5ac82012-09-26 23:44:59 -0700486 kParams.loop_count = (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT);
skal68f282f2012-11-15 09:15:04 +0100487 kParams.bg_color = WebPDemuxGetI(kParams.dmux, WEBP_FF_BACKGROUND_COLOR);
James Zernd7a5ac82012-09-26 23:44:59 -0700488 printf("VP8X: Found %d images in file (loop count = %d)\n",
Urvang Joshidcf65222013-08-09 14:40:31 -0700489 curr->num_frames, kParams.loop_count);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700490
491 // Decode first frame
skal68f282f2012-11-15 09:15:04 +0100492 if (!Decode()) goto Error;
493
494 // Position iterator to last frame. Next call to HandleDisplay will wrap over.
495 // We take this into account by bumping up loop_count.
Urvang Joshidcf65222013-08-09 14:40:31 -0700496 WebPDemuxGetFrame(kParams.dmux, 0, curr);
skal68f282f2012-11-15 09:15:04 +0100497 if (kParams.loop_count) ++kParams.loop_count;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700498
499 // Start display (and timer)
Pascal Massimino7937b402011-12-13 14:02:04 -0800500 glutInit(&argc, argv);
James Zern880fd982012-05-25 14:53:46 -0700501#ifdef FREEGLUT
502 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
503#endif
skal68f282f2012-11-15 09:15:04 +0100504 StartDisplay();
505
Pascal Massimino861a5b72012-05-09 00:41:12 -0700506 if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0);
507 glutMainLoop();
Pascal Massimino7937b402011-12-13 14:02:04 -0800508
509 // Should only be reached when using FREEGLUT:
Pascal Massimino861a5b72012-05-09 00:41:12 -0700510 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800511 return 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700512
513 Error:
514 ClearParams();
515 return -1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800516}
517
518//------------------------------------------------------------------------------