blob: 32a993322b37e4cab773e1fb4edfa8087f4accd4 [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
James Zern32b31372014-06-10 17:53:44 -070014#include "webp/config.h"
James Zern0e513f72013-05-01 14:47:56 -070015#endif
Pascal Massimino7937b402011-12-13 14:02:04 -080016
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
Pascal Massimino79ff0342013-09-12 04:03:51 -070021#if defined(WEBP_HAVE_GL)
22
James Zern0e513f72013-05-01 14:47:56 -070023#if defined(HAVE_GLUT_GLUT_H)
Pascal Massimino7937b402011-12-13 14:02:04 -080024#include <GLUT/glut.h>
25#else
26#include <GL/glut.h>
27#ifdef FREEGLUT
28#include <GL/freeglut.h>
29#endif
30#endif
31
James Zernddfee5d2013-03-07 19:31:27 -080032#ifdef WEBP_HAVE_QCMS
33#include <qcms.h>
34#endif
35
36#include "webp/decode.h"
37#include "webp/demux.h"
38
James Zern061263a2012-05-11 16:00:57 -070039#include "./example_util.h"
40
James Zern91179542011-12-16 19:30:33 -080041#ifdef _MSC_VER
42#define snprintf _snprintf
43#endif
44
Pascal Massimino861a5b72012-05-09 00:41:12 -070045// 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;
skald51f45f2013-09-12 09:32:28 +020060 WebPDecoderConfig config;
Pascal Massimino861a5b72012-05-09 00:41:12 -070061 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;
skald51f45f2013-09-12 09:32:28 +0200149 WebPDecoderConfig* const config = &kParams.config;
James Zernddfee5d2013-03-07 19:31:27 -0800150 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);
James Zern0f017b52015-02-02 20:05:54 -0800182 if (kParams.done) return;
James Zernddfee5d2013-03-07 19:31:27 -0800183 } else {
184 kParams.decoding_error = 1;
185 kParams.done = 1;
186 return;
187 }
188 }
Urvang Joshidcf65222013-08-09 14:40:31 -0700189 duration = curr->duration;
James Zernddfee5d2013-03-07 19:31:27 -0800190 }
191 if (!Decode()) {
192 kParams.decoding_error = 1;
193 kParams.done = 1;
194 } else {
195 glutPostRedisplay();
196 glutTimerFunc(duration, decode_callback, what);
197 }
198 }
199}
200
Pascal Massimino7937b402011-12-13 14:02:04 -0800201//------------------------------------------------------------------------------
202// Callbacks
203
204static void HandleKey(unsigned char key, int pos_x, int pos_y) {
205 (void)pos_x;
206 (void)pos_y;
207 if (key == 'q' || key == 'Q' || key == 27 /* Esc */) {
208#ifdef FREEGLUT
209 glutLeaveMainLoop();
210#else
Pascal Massimino861a5b72012-05-09 00:41:12 -0700211 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800212 exit(0);
213#endif
James Zernddfee5d2013-03-07 19:31:27 -0800214 } else if (key == 'c') {
215 if (kParams.has_color_profile && !kParams.decoding_error) {
216 kParams.use_color_profile = 1 - kParams.use_color_profile;
217
218 if (kParams.has_animation) {
219 // Restart the completed animation to pickup the color profile change.
220 if (kParams.done && kParams.loop_count == 0) {
221 kParams.loop_count =
222 (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT) + 1;
223 kParams.done = 0;
224 // Start the decode loop immediately.
225 glutTimerFunc(0, decode_callback, 0);
226 }
227 } else {
228 Decode();
229 glutPostRedisplay();
230 }
231 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800232 } else if (key == 'i') {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700233 kParams.print_info = 1 - kParams.print_info;
Pascal Massimino7937b402011-12-13 14:02:04 -0800234 glutPostRedisplay();
235 }
236}
237
238static void HandleReshape(int width, int height) {
239 // TODO(skal): proper handling of resize, esp. for large pictures.
240 // + key control of the zoom.
241 glViewport(0, 0, width, height);
242 glMatrixMode(GL_PROJECTION);
243 glLoadIdentity();
244 glMatrixMode(GL_MODELVIEW);
245 glLoadIdentity();
246}
247
248static void PrintString(const char* const text) {
249 void* const font = GLUT_BITMAP_9_BY_15;
250 int i;
251 for (i = 0; text[i]; ++i) {
252 glutBitmapCharacter(font, text[i]);
253 }
254}
255
skal68f282f2012-11-15 09:15:04 +0100256static float GetColorf(uint32_t color, int shift) {
257 return (color >> shift) / 255.f;
258}
259
James Zerna73b8972012-07-09 23:01:52 -0700260static void DrawCheckerBoard(void) {
261 const int square_size = 8; // must be a power of 2
262 int x, y;
263 GLint viewport[4]; // x, y, width, height
264
265 glPushMatrix();
266
267 glGetIntegerv(GL_VIEWPORT, viewport);
268 // shift to integer coordinates with (0,0) being top-left.
269 glOrtho(0, viewport[2], viewport[3], 0, -1, 1);
270 for (y = 0; y < viewport[3]; y += square_size) {
271 for (x = 0; x < viewport[2]; x += square_size) {
272 const GLubyte color = 128 + 64 * (!((x + y) & square_size));
273 glColor3ub(color, color, color);
274 glRecti(x, y, x + square_size, y + square_size);
275 }
276 }
277 glPopMatrix();
278}
279
Pascal Massimino7937b402011-12-13 14:02:04 -0800280static void HandleDisplay(void) {
skal68f282f2012-11-15 09:15:04 +0100281 const WebPDecBuffer* const pic = kParams.pic;
Urvang Joshidcf65222013-08-09 14:40:31 -0700282 const WebPIterator* const curr = &kParams.curr_frame;
283 WebPIterator* const prev = &kParams.prev_frame;
skal41a6ced2012-11-15 09:35:01 +0100284 GLfloat xoff, yoff;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700285 if (pic == NULL) return;
Pascal Massimino7937b402011-12-13 14:02:04 -0800286 glPushMatrix();
287 glPixelZoom(1, -1);
Urvang Joshidcf65222013-08-09 14:40:31 -0700288 xoff = (GLfloat)(2. * curr->x_offset / kParams.canvas_width);
289 yoff = (GLfloat)(2. * curr->y_offset / kParams.canvas_height);
James Zern013023e2013-03-15 12:17:45 -0700290 glRasterPos2f(-1.f + xoff, 1.f - yoff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800291 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700292 glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4);
James Zern04c7a2e2013-03-23 12:45:11 -0700293
Urvang Joshidcf65222013-08-09 14:40:31 -0700294 if (prev->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ||
295 curr->blend_method == WEBP_MUX_NO_BLEND) {
James Zern04c7a2e2013-03-23 12:45:11 -0700296 // 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).
Urvang Joshidcf65222013-08-09 14:40:31 -0700299 int window_x, window_y;
300 if (prev->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) {
301 // Clear the previous frame rectangle.
302 window_x = prev->x_offset;
303 window_y = kParams.canvas_height - prev->y_offset - prev->height;
304 } else { // curr->blend_method == WEBP_MUX_NO_BLEND.
305 // We simulate no-blending behavior by first clearing the current frame
306 // rectangle (to a checker-board) and then alpha-blending against it.
307 window_x = curr->x_offset;
308 window_y = kParams.canvas_height - curr->y_offset - curr->height;
309 }
James Zern04c7a2e2013-03-23 12:45:11 -0700310 glEnable(GL_SCISSOR_TEST);
Urvang Joshidcf65222013-08-09 14:40:31 -0700311 // Only update the requested area, not the whole canvas.
312 glScissor(window_x, window_y, prev->width, prev->height);
James Zern04c7a2e2013-03-23 12:45:11 -0700313
skal68f282f2012-11-15 09:15:04 +0100314 glClear(GL_COLOR_BUFFER_BIT); // use clear color
James Zern04c7a2e2013-03-23 12:45:11 -0700315 DrawCheckerBoard();
316
317 glDisable(GL_SCISSOR_TEST);
skal68f282f2012-11-15 09:15:04 +0100318 }
Urvang Joshidcf65222013-08-09 14:40:31 -0700319
320 *prev = *curr;
James Zern04c7a2e2013-03-23 12:45:11 -0700321
Pascal Massimino861a5b72012-05-09 00:41:12 -0700322 glDrawPixels(pic->width, pic->height,
323 GL_RGBA, GL_UNSIGNED_BYTE,
324 (GLvoid*)pic->u.RGBA.rgba);
325 if (kParams.print_info) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800326 char tmp[32];
327
James Zern013023e2013-03-15 12:17:45 -0700328 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800329 glRasterPos2f(-0.95f, 0.90f);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700330 PrintString(kParams.file_name);
Pascal Massimino7937b402011-12-13 14:02:04 -0800331
Pascal Massimino861a5b72012-05-09 00:41:12 -0700332 snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height);
James Zern013023e2013-03-15 12:17:45 -0700333 glColor4f(0.90f, 0.0f, 0.90f, 1.0f);
James Zern91179542011-12-16 19:30:33 -0800334 glRasterPos2f(-0.95f, 0.80f);
Pascal Massimino7937b402011-12-13 14:02:04 -0800335 PrintString(tmp);
Urvang Joshidcf65222013-08-09 14:40:31 -0700336 if (curr->x_offset != 0 || curr->y_offset != 0) {
skal68f282f2012-11-15 09:15:04 +0100337 snprintf(tmp, sizeof(tmp), " (offset:%d,%d)",
Urvang Joshidcf65222013-08-09 14:40:31 -0700338 curr->x_offset, curr->y_offset);
skal68f282f2012-11-15 09:15:04 +0100339 glRasterPos2f(-0.95f, 0.70f);
340 PrintString(tmp);
341 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800342 }
James Zerna73b8972012-07-09 23:01:52 -0700343 glPopMatrix();
Pascal Massimino7937b402011-12-13 14:02:04 -0800344 glFlush();
345}
346
skal68f282f2012-11-15 09:15:04 +0100347static void StartDisplay(void) {
348 const int width = kParams.canvas_width;
349 const int height = kParams.canvas_height;
Pascal Massimino7937b402011-12-13 14:02:04 -0800350 glutInitDisplayMode(GLUT_RGBA);
skal68f282f2012-11-15 09:15:04 +0100351 glutInitWindowSize(width, height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800352 glutCreateWindow("WebP viewer");
Pascal Massimino7937b402011-12-13 14:02:04 -0800353 glutDisplayFunc(HandleDisplay);
354 glutIdleFunc(NULL);
355 glutKeyboardFunc(HandleKey);
Urvang Joshi08220102012-06-20 11:18:35 -0700356 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
357 glEnable(GL_BLEND);
skal68f282f2012-11-15 09:15:04 +0100358 glClearColor(GetColorf(kParams.bg_color, 0),
359 GetColorf(kParams.bg_color, 8),
360 GetColorf(kParams.bg_color, 16),
361 GetColorf(kParams.bg_color, 24));
362 HandleReshape(width, height);
363 glClear(GL_COLOR_BUFFER_BIT);
364 DrawCheckerBoard();
Pascal Massimino7937b402011-12-13 14:02:04 -0800365}
366
367//------------------------------------------------------------------------------
Pascal Massimino7937b402011-12-13 14:02:04 -0800368// Main
369
370static void Help(void) {
371 printf("Usage: vwebp in_file [options]\n\n"
372 "Decodes the WebP image file and visualize it using OpenGL\n"
373 "Options are:\n"
skal0a8b8862014-06-18 16:45:34 +0200374 " -version .... print version number and exit\n"
375 " -noicc ....... don't use the icc profile if present\n"
376 " -nofancy ..... don't use the fancy YUV420 upscaler\n"
377 " -nofilter .... disable in-loop filtering\n"
378 " -dither <int> dithering strength (0..100), default=50\n"
379 " -noalphadither disable alpha plane dithering\n"
380 " -mt .......... use multi-threading\n"
381 " -info ........ print info\n"
382 " -h ....... this help message\n"
James Zern03cc23d2013-03-07 19:10:59 -0800383 "\n"
384 "Keyboard shortcuts:\n"
skal0a8b8862014-06-18 16:45:34 +0200385 " 'c' ................ toggle use of color profile\n"
386 " 'i' ................ overlay file information\n"
387 " 'q' / 'Q' / ESC .... quit\n"
Pascal Massimino7937b402011-12-13 14:02:04 -0800388 );
389}
390
391int main(int argc, char *argv[]) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800392 int c;
skald51f45f2013-09-12 09:32:28 +0200393 WebPDecoderConfig* const config = &kParams.config;
Pascal Massimino14dd5e72013-08-15 00:21:15 -0700394 WebPIterator* const curr = &kParams.curr_frame;
Urvang Joshidcf65222013-08-09 14:40:31 -0700395 WebPIterator* const prev = &kParams.prev_frame;
Pascal Massimino7937b402011-12-13 14:02:04 -0800396
skald51f45f2013-09-12 09:32:28 +0200397 if (!WebPInitDecoderConfig(config)) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800398 fprintf(stderr, "Library version mismatch!\n");
399 return -1;
400 }
skalcbdd3e62013-11-26 22:59:02 +0100401 config->options.dithering_strength = 50;
skalbbe32df2014-06-14 00:06:16 +0200402 config->options.alpha_dithering_strength = 100;
James Zernddfee5d2013-03-07 19:31:27 -0800403 kParams.use_color_profile = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800404
405 for (c = 1; c < argc; ++c) {
James Zern96d43a82014-09-10 23:35:48 -0700406 int parse_error = 0;
Pascal Massimino7937b402011-12-13 14:02:04 -0800407 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
408 Help();
409 return 0;
James Zernddfee5d2013-03-07 19:31:27 -0800410 } else if (!strcmp(argv[c], "-noicc")) {
411 kParams.use_color_profile = 0;
Pascal Massimino7937b402011-12-13 14:02:04 -0800412 } else if (!strcmp(argv[c], "-nofancy")) {
skald51f45f2013-09-12 09:32:28 +0200413 config->options.no_fancy_upsampling = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800414 } else if (!strcmp(argv[c], "-nofilter")) {
skald51f45f2013-09-12 09:32:28 +0200415 config->options.bypass_filtering = 1;
skalbbe32df2014-06-14 00:06:16 +0200416 } else if (!strcmp(argv[c], "-noalphadither")) {
417 config->options.alpha_dithering_strength = 0;
skalcbdd3e62013-11-26 22:59:02 +0100418 } else if (!strcmp(argv[c], "-dither") && c + 1 < argc) {
James Zern96d43a82014-09-10 23:35:48 -0700419 config->options.dithering_strength =
420 ExUtilGetInt(argv[++c], 0, &parse_error);
skal68f282f2012-11-15 09:15:04 +0100421 } else if (!strcmp(argv[c], "-info")) {
422 kParams.print_info = 1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800423 } else if (!strcmp(argv[c], "-version")) {
Urvang Joshia5042a32013-02-26 14:22:06 -0800424 const int dec_version = WebPGetDecoderVersion();
425 const int dmux_version = WebPGetDemuxVersion();
426 printf("WebP Decoder version: %d.%d.%d\nWebP Demux version: %d.%d.%d\n",
427 (dec_version >> 16) & 0xff, (dec_version >> 8) & 0xff,
428 dec_version & 0xff, (dmux_version >> 16) & 0xff,
429 (dmux_version >> 8) & 0xff, dmux_version & 0xff);
Pascal Massimino7937b402011-12-13 14:02:04 -0800430 return 0;
431 } else if (!strcmp(argv[c], "-mt")) {
skald51f45f2013-09-12 09:32:28 +0200432 config->options.use_threads = 1;
James Zerna4b0aa02013-12-12 20:20:08 -0800433 } else if (!strcmp(argv[c], "--")) {
434 if (c < argc - 1) kParams.file_name = argv[++c];
435 break;
Pascal Massimino7937b402011-12-13 14:02:04 -0800436 } else if (argv[c][0] == '-') {
437 printf("Unknown option '%s'\n", argv[c]);
438 Help();
439 return -1;
440 } else {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700441 kParams.file_name = argv[c];
Pascal Massimino7937b402011-12-13 14:02:04 -0800442 }
James Zern96d43a82014-09-10 23:35:48 -0700443
444 if (parse_error) {
445 Help();
446 return -1;
447 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800448 }
449
James Zern061263a2012-05-11 16:00:57 -0700450 if (kParams.file_name == NULL) {
451 printf("missing input file!!\n");
452 Help();
453 return 0;
454 }
455
456 if (!ExUtilReadFile(kParams.file_name,
Urvang Joshia0770722012-10-30 14:54:46 -0700457 &kParams.data.bytes, &kParams.data.size)) {
James Zern061263a2012-05-11 16:00:57 -0700458 goto Error;
459 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700460
Pascal Massimino830f72b2013-06-10 05:46:22 -0700461 if (!WebPGetInfo(kParams.data.bytes, kParams.data.size, NULL, NULL)) {
462 fprintf(stderr, "Input file doesn't appear to be WebP format.\n");
463 goto Error;
464 }
465
James Zernd7a5ac82012-09-26 23:44:59 -0700466 kParams.dmux = WebPDemux(&kParams.data);
467 if (kParams.dmux == NULL) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700468 fprintf(stderr, "Could not create demuxing object!\n");
469 goto Error;
Pascal Massimino7937b402011-12-13 14:02:04 -0800470 }
471
Urvang Joshia00a3da2012-10-31 17:49:15 -0700472 if (WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & FRAGMENTS_FLAG) {
473 fprintf(stderr, "Image fragments are not supported for now!\n");
Pascal Massimino861a5b72012-05-09 00:41:12 -0700474 goto Error;
475 }
skal68f282f2012-11-15 09:15:04 +0100476 kParams.canvas_width = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_WIDTH);
477 kParams.canvas_height = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_HEIGHT);
478 if (kParams.print_info) {
479 printf("Canvas: %d x %d\n", kParams.canvas_width, kParams.canvas_height);
480 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800481
Urvang Joshidcf65222013-08-09 14:40:31 -0700482 prev->width = kParams.canvas_width;
483 prev->height = kParams.canvas_height;
484 prev->x_offset = prev->y_offset = 0;
485 prev->dispose_method = WEBP_MUX_DISPOSE_BACKGROUND;
James Zern04c7a2e2013-03-23 12:45:11 -0700486
James Zernddfee5d2013-03-07 19:31:27 -0800487 memset(&kParams.iccp, 0, sizeof(kParams.iccp));
488 kParams.has_color_profile =
489 !!(WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & ICCP_FLAG);
490 if (kParams.has_color_profile) {
491#ifdef WEBP_HAVE_QCMS
492 if (!WebPDemuxGetChunk(kParams.dmux, "ICCP", 1, &kParams.iccp)) goto Error;
493 printf("VP8X: Found color profile\n");
494#else
495 fprintf(stderr, "Warning: color profile present, but qcms is unavailable!\n"
496 "Build libqcms from Mozilla or Chromium and define WEBP_HAVE_QCMS "
497 "before building.\n");
498#endif
499 }
500
Urvang Joshidcf65222013-08-09 14:40:31 -0700501 if (!WebPDemuxGetFrame(kParams.dmux, 1, curr)) goto Error;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700502
Urvang Joshidcf65222013-08-09 14:40:31 -0700503 kParams.has_animation = (curr->num_frames > 1);
James Zernd7a5ac82012-09-26 23:44:59 -0700504 kParams.loop_count = (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT);
skal68f282f2012-11-15 09:15:04 +0100505 kParams.bg_color = WebPDemuxGetI(kParams.dmux, WEBP_FF_BACKGROUND_COLOR);
James Zernd7a5ac82012-09-26 23:44:59 -0700506 printf("VP8X: Found %d images in file (loop count = %d)\n",
Urvang Joshidcf65222013-08-09 14:40:31 -0700507 curr->num_frames, kParams.loop_count);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700508
509 // Decode first frame
skal68f282f2012-11-15 09:15:04 +0100510 if (!Decode()) goto Error;
511
512 // Position iterator to last frame. Next call to HandleDisplay will wrap over.
513 // We take this into account by bumping up loop_count.
Urvang Joshidcf65222013-08-09 14:40:31 -0700514 WebPDemuxGetFrame(kParams.dmux, 0, curr);
skal68f282f2012-11-15 09:15:04 +0100515 if (kParams.loop_count) ++kParams.loop_count;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700516
517 // Start display (and timer)
Pascal Massimino7937b402011-12-13 14:02:04 -0800518 glutInit(&argc, argv);
James Zern880fd982012-05-25 14:53:46 -0700519#ifdef FREEGLUT
520 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
521#endif
skal68f282f2012-11-15 09:15:04 +0100522 StartDisplay();
523
Pascal Massimino861a5b72012-05-09 00:41:12 -0700524 if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0);
525 glutMainLoop();
Pascal Massimino7937b402011-12-13 14:02:04 -0800526
527 // Should only be reached when using FREEGLUT:
Pascal Massimino861a5b72012-05-09 00:41:12 -0700528 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800529 return 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700530
531 Error:
532 ClearParams();
533 return -1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800534}
535
Pascal Massimino79ff0342013-09-12 04:03:51 -0700536#else // !WEBP_HAVE_GL
537
538int main(int argc, const char *argv[]) {
539 fprintf(stderr, "OpenGL support not enabled in %s.\n", argv[0]);
540 (void)argc;
541 return 0;
542}
543
544#endif
545
Pascal Massimino7937b402011-12-13 14:02:04 -0800546//------------------------------------------------------------------------------