blob: 0508924ece1b42f94e6528b7a6564907434aee8a [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:
11// sudo apt-get install libglut3-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
22#include "webp/decode.h"
Pascal Massimino861a5b72012-05-09 00:41:12 -070023#include "webp/mux.h"
Pascal Massimino7937b402011-12-13 14:02:04 -080024
25#ifdef __APPLE__
26#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 Zern061263a2012-05-11 16:00:57 -070034#include "./example_util.h"
35
James Zern91179542011-12-16 19:30:33 -080036#ifdef _MSC_VER
37#define snprintf _snprintf
38#endif
39
Pascal Massimino861a5b72012-05-09 00:41:12 -070040static void Help(void);
41
42// Unfortunate global variables. Gathered into a struct for comfort.
43static struct {
44 int has_animation;
45 int done;
46 int decoding_error;
47 int print_info;
48
49 uint32_t flags;
Urvang Joshi6808e692012-07-06 11:35:36 +053050 int loop_count;
Pascal Massimino861a5b72012-05-09 00:41:12 -070051 int frame_num;
52 int frame_max;
53
54 const char* file_name;
55 WebPData data;
56 WebPMux* mux;
57 WebPDecoderConfig* config;
58 const WebPDecBuffer* pic;
59} kParams = {
60 0, 0, 0, 0, // has_animation, ...
61 0, 1, 1, 0, // flags, ...
62 NULL, { NULL, 0 }, // file_name, ...
63 NULL, NULL, NULL // mux, ...
64};
65
66static void ClearPreviousPic(void) {
67 WebPFreeDecBuffer((WebPDecBuffer*)kParams.pic);
68 kParams.pic = NULL;
69}
70
Pascal Massimino861a5b72012-05-09 00:41:12 -070071static void ClearParams(void) {
72 ClearPreviousPic();
Urvang Joshif3bab8e2012-06-07 17:19:02 +053073 WebPDataClear(&kParams.data);
Pascal Massimino861a5b72012-05-09 00:41:12 -070074 WebPMuxDelete(kParams.mux);
75 kParams.mux = NULL;
76}
Pascal Massimino7937b402011-12-13 14:02:04 -080077
78//------------------------------------------------------------------------------
79// Callbacks
80
81static void HandleKey(unsigned char key, int pos_x, int pos_y) {
82 (void)pos_x;
83 (void)pos_y;
84 if (key == 'q' || key == 'Q' || key == 27 /* Esc */) {
85#ifdef FREEGLUT
86 glutLeaveMainLoop();
87#else
Pascal Massimino861a5b72012-05-09 00:41:12 -070088 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -080089 exit(0);
90#endif
91 } else if (key == 'i') {
Pascal Massimino861a5b72012-05-09 00:41:12 -070092 kParams.print_info = 1 - kParams.print_info;
Pascal Massimino7937b402011-12-13 14:02:04 -080093 glutPostRedisplay();
94 }
95}
96
97static void HandleReshape(int width, int height) {
98 // TODO(skal): proper handling of resize, esp. for large pictures.
99 // + key control of the zoom.
100 glViewport(0, 0, width, height);
101 glMatrixMode(GL_PROJECTION);
102 glLoadIdentity();
103 glMatrixMode(GL_MODELVIEW);
104 glLoadIdentity();
105}
106
107static void PrintString(const char* const text) {
108 void* const font = GLUT_BITMAP_9_BY_15;
109 int i;
110 for (i = 0; text[i]; ++i) {
111 glutBitmapCharacter(font, text[i]);
112 }
113}
114
James Zerna73b8972012-07-09 23:01:52 -0700115static void DrawCheckerBoard(void) {
116 const int square_size = 8; // must be a power of 2
117 int x, y;
118 GLint viewport[4]; // x, y, width, height
119
120 glPushMatrix();
121
122 glGetIntegerv(GL_VIEWPORT, viewport);
123 // shift to integer coordinates with (0,0) being top-left.
124 glOrtho(0, viewport[2], viewport[3], 0, -1, 1);
125 for (y = 0; y < viewport[3]; y += square_size) {
126 for (x = 0; x < viewport[2]; x += square_size) {
127 const GLubyte color = 128 + 64 * (!((x + y) & square_size));
128 glColor3ub(color, color, color);
129 glRecti(x, y, x + square_size, y + square_size);
130 }
131 }
132 glPopMatrix();
133}
134
Pascal Massimino7937b402011-12-13 14:02:04 -0800135static void HandleDisplay(void) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700136 const WebPDecBuffer* pic = kParams.pic;
137 if (pic == NULL) return;
Pascal Massimino7937b402011-12-13 14:02:04 -0800138 glClear(GL_COLOR_BUFFER_BIT);
139 glPushMatrix();
140 glPixelZoom(1, -1);
141 glRasterPos2f(-1, 1);
142 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700143 glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4);
James Zerna73b8972012-07-09 23:01:52 -0700144 DrawCheckerBoard();
Pascal Massimino861a5b72012-05-09 00:41:12 -0700145 glDrawPixels(pic->width, pic->height,
146 GL_RGBA, GL_UNSIGNED_BYTE,
147 (GLvoid*)pic->u.RGBA.rgba);
148 if (kParams.print_info) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800149 char tmp[32];
150
James Zernb35c07d2012-07-09 22:36:58 -0700151 glColor4f(0.0, 0.0, 0.0, 1.0);
James Zern91179542011-12-16 19:30:33 -0800152 glRasterPos2f(-0.95f, 0.90f);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700153 PrintString(kParams.file_name);
Pascal Massimino7937b402011-12-13 14:02:04 -0800154
Pascal Massimino861a5b72012-05-09 00:41:12 -0700155 snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height);
James Zernb35c07d2012-07-09 22:36:58 -0700156 glColor4f(0.0, 0.0, 0.0, 1.0);
James Zern91179542011-12-16 19:30:33 -0800157 glRasterPos2f(-0.95f, 0.80f);
Pascal Massimino7937b402011-12-13 14:02:04 -0800158 PrintString(tmp);
159 }
James Zerna73b8972012-07-09 23:01:52 -0700160 glPopMatrix();
Pascal Massimino7937b402011-12-13 14:02:04 -0800161 glFlush();
162}
163
Pascal Massimino861a5b72012-05-09 00:41:12 -0700164static void StartDisplay(const WebPDecBuffer* const pic) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800165 glutInitDisplayMode(GLUT_RGBA);
166 glutInitWindowSize(pic->width, pic->height);
167 glutCreateWindow("WebP viewer");
Pascal Massimino7937b402011-12-13 14:02:04 -0800168 glutDisplayFunc(HandleDisplay);
169 glutIdleFunc(NULL);
170 glutKeyboardFunc(HandleKey);
Urvang Joshi08220102012-06-20 11:18:35 -0700171 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
172 glEnable(GL_BLEND);
Pascal Massimino7937b402011-12-13 14:02:04 -0800173 glClearColor(0.0, 0.0, 0.0, 0.0);
174 HandleReshape(pic->width, pic->height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800175}
176
177//------------------------------------------------------------------------------
178// File decoding
179
Urvang Joshi6808e692012-07-06 11:35:36 +0530180static int Decode(const int frame_number, int* const duration) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700181 WebPDecoderConfig* const config = kParams.config;
182 WebPData *data, image_data;
Urvang Joshi6808e692012-07-06 11:35:36 +0530183 int x_off = 0, y_off = 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700184 WebPDecBuffer* const output_buffer = &config->output;
185 int ok = 0;
186
187 ClearPreviousPic();
188 if (kParams.has_animation) {
Urvang Joshib74ed6e2012-06-20 10:59:40 -0700189 if (WebPMuxGetFrame(kParams.mux, frame_number, &image_data,
Pascal Massimino861a5b72012-05-09 00:41:12 -0700190 &x_off, &y_off, duration) != WEBP_MUX_OK) {
191 goto end;
192 }
193 if (x_off != 0 || y_off != 0) {
194 fprintf(stderr,
Pascal Massimino3927ff32012-05-09 01:57:15 -0700195 "Frame offsets not yet supported! Forcing offset to 0,0\n");
Pascal Massimino861a5b72012-05-09 00:41:12 -0700196 x_off = y_off = 0;
197 }
198 data = &image_data;
199 } else {
200 data = &kParams.data;
Pascal Massimino7937b402011-12-13 14:02:04 -0800201 }
202
203 output_buffer->colorspace = MODE_RGBA;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700204 ok = (WebPDecode(data->bytes_, data->size_, config) == VP8_STATUS_OK);
Pascal Massimino7937b402011-12-13 14:02:04 -0800205
206 end:
Pascal Massimino7937b402011-12-13 14:02:04 -0800207 if (!ok) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700208 fprintf(stderr, "Decoding of frame #%d failed!\n", frame_number);
209 } else {
210 kParams.pic = output_buffer;
Pascal Massimino7937b402011-12-13 14:02:04 -0800211 }
212 return ok;
213}
214
Pascal Massimino861a5b72012-05-09 00:41:12 -0700215static void decode_callback(int what) {
216 if (what == 0 && !kParams.done) {
Urvang Joshi6808e692012-07-06 11:35:36 +0530217 int duration = 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700218 if (kParams.mux != NULL) {
219 if (!Decode(kParams.frame_num, &duration)) {
220 kParams.decoding_error = 1;
221 kParams.done = 1;
222 } else {
223 ++kParams.frame_num;
224 if (kParams.frame_num > kParams.frame_max) {
225 kParams.frame_num = 1;
226 --kParams.loop_count;
227 kParams.done = (kParams.loop_count == 0);
228 }
229 }
230 }
231 glutPostRedisplay();
232 glutTimerFunc(duration, decode_callback, what);
233 }
234}
235
Pascal Massimino7937b402011-12-13 14:02:04 -0800236//------------------------------------------------------------------------------
237// Main
238
239static void Help(void) {
240 printf("Usage: vwebp in_file [options]\n\n"
241 "Decodes the WebP image file and visualize it using OpenGL\n"
242 "Options are:\n"
243 " -version .... print version number and exit.\n"
244 " -nofancy ..... don't use the fancy YUV420 upscaler.\n"
245 " -nofilter .... disable in-loop filtering.\n"
246 " -mt .......... use multi-threading\n"
247 " -crop <x> <y> <w> <h> ... crop output with the given rectangle\n"
248 " -scale <w> <h> .......... scale the output (*after* any cropping)\n"
249 " -h ....... this help message.\n"
250 );
251}
252
253int main(int argc, char *argv[]) {
254 WebPDecoderConfig config;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700255 WebPMuxError mux_err;
Pascal Massimino7937b402011-12-13 14:02:04 -0800256 int c;
257
258 if (!WebPInitDecoderConfig(&config)) {
259 fprintf(stderr, "Library version mismatch!\n");
260 return -1;
261 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700262 kParams.config = &config;
Pascal Massimino7937b402011-12-13 14:02:04 -0800263
264 for (c = 1; c < argc; ++c) {
265 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
266 Help();
267 return 0;
268 } else if (!strcmp(argv[c], "-nofancy")) {
269 config.options.no_fancy_upsampling = 1;
270 } else if (!strcmp(argv[c], "-nofilter")) {
271 config.options.bypass_filtering = 1;
272 } else if (!strcmp(argv[c], "-version")) {
273 const int version = WebPGetDecoderVersion();
274 printf("%d.%d.%d\n",
275 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
276 return 0;
277 } else if (!strcmp(argv[c], "-mt")) {
278 config.options.use_threads = 1;
279 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
280 config.options.use_cropping = 1;
281 config.options.crop_left = strtol(argv[++c], NULL, 0);
282 config.options.crop_top = strtol(argv[++c], NULL, 0);
283 config.options.crop_width = strtol(argv[++c], NULL, 0);
284 config.options.crop_height = strtol(argv[++c], NULL, 0);
285 } else if (!strcmp(argv[c], "-scale") && c < argc - 2) {
286 config.options.use_scaling = 1;
287 config.options.scaled_width = strtol(argv[++c], NULL, 0);
288 config.options.scaled_height = strtol(argv[++c], NULL, 0);
289 } else if (argv[c][0] == '-') {
290 printf("Unknown option '%s'\n", argv[c]);
291 Help();
292 return -1;
293 } else {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700294 kParams.file_name = argv[c];
Pascal Massimino7937b402011-12-13 14:02:04 -0800295 }
296 }
297
James Zern061263a2012-05-11 16:00:57 -0700298 if (kParams.file_name == NULL) {
299 printf("missing input file!!\n");
300 Help();
301 return 0;
302 }
303
304 if (!ExUtilReadFile(kParams.file_name,
305 &kParams.data.bytes_, &kParams.data.size_)) {
306 goto Error;
307 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700308
Urvang Joshif3bab8e2012-06-07 17:19:02 +0530309 kParams.mux = WebPMuxCreate(&kParams.data, 0);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700310 if (kParams.mux == NULL) {
311 fprintf(stderr, "Could not create demuxing object!\n");
312 goto Error;
Pascal Massimino7937b402011-12-13 14:02:04 -0800313 }
314
Pascal Massimino861a5b72012-05-09 00:41:12 -0700315 mux_err = WebPMuxGetFeatures(kParams.mux, &kParams.flags);
316 if (mux_err != WEBP_MUX_OK) {
317 goto Error;
318 }
319 if (kParams.flags & TILE_FLAG) {
320 fprintf(stderr, "Tiling is not supported for now!\n");
321 goto Error;
322 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800323
Pascal Massimino861a5b72012-05-09 00:41:12 -0700324 kParams.has_animation = !!(kParams.flags & ANIMATION_FLAG);
325
326 if (kParams.has_animation) {
327 mux_err = WebPMuxGetLoopCount(kParams.mux, &kParams.loop_count);
328 if (mux_err != WEBP_MUX_OK && mux_err != WEBP_MUX_NOT_FOUND) {
329 goto Error;
330 }
Urvang Joshifbdcb7e2012-06-11 12:24:45 +0530331 mux_err = WebPMuxNumChunks(kParams.mux, WEBP_CHUNK_IMAGE,
Pascal Massimino861a5b72012-05-09 00:41:12 -0700332 &kParams.frame_max);
Pascal Massimino3927ff32012-05-09 01:57:15 -0700333 if (mux_err != WEBP_MUX_OK) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700334 goto Error;
335 }
336 printf("VP8X: Found %d images in file (loop count = %d)\n",
337 kParams.frame_max, kParams.loop_count);
338 }
339
340 // Decode first frame
341 {
Urvang Joshi6808e692012-07-06 11:35:36 +0530342 int duration;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700343 if (!Decode(1, &duration)) goto Error;
344 }
345
346 // Start display (and timer)
Pascal Massimino7937b402011-12-13 14:02:04 -0800347 glutInit(&argc, argv);
James Zern880fd982012-05-25 14:53:46 -0700348#ifdef FREEGLUT
349 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
350#endif
Pascal Massimino861a5b72012-05-09 00:41:12 -0700351 StartDisplay(kParams.pic);
352 if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0);
353 glutMainLoop();
Pascal Massimino7937b402011-12-13 14:02:04 -0800354
355 // Should only be reached when using FREEGLUT:
Pascal Massimino861a5b72012-05-09 00:41:12 -0700356 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800357 return 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700358
359 Error:
360 ClearParams();
361 return -1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800362}
363
364//------------------------------------------------------------------------------