blob: 734c6df96bd6f0c590cecb16b7a6882d69061185 [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;
50 uint32_t loop_count;
51 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
115static void HandleDisplay(void) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700116 const WebPDecBuffer* pic = kParams.pic;
117 if (pic == NULL) return;
Pascal Massimino7937b402011-12-13 14:02:04 -0800118 glClear(GL_COLOR_BUFFER_BIT);
119 glPushMatrix();
120 glPixelZoom(1, -1);
121 glRasterPos2f(-1, 1);
122 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700123 glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4);
124 glDrawPixels(pic->width, pic->height,
125 GL_RGBA, GL_UNSIGNED_BYTE,
126 (GLvoid*)pic->u.RGBA.rgba);
127 if (kParams.print_info) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800128 char tmp[32];
129
130 glColor4f(0.0, 0.0, 0.0, 0.0);
James Zern91179542011-12-16 19:30:33 -0800131 glRasterPos2f(-0.95f, 0.90f);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700132 PrintString(kParams.file_name);
Pascal Massimino7937b402011-12-13 14:02:04 -0800133
Pascal Massimino861a5b72012-05-09 00:41:12 -0700134 snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800135 glColor4f(0.0, 0.0, 0.0, 0.0);
James Zern91179542011-12-16 19:30:33 -0800136 glRasterPos2f(-0.95f, 0.80f);
Pascal Massimino7937b402011-12-13 14:02:04 -0800137 PrintString(tmp);
138 }
139 glFlush();
140}
141
Pascal Massimino861a5b72012-05-09 00:41:12 -0700142static void StartDisplay(const WebPDecBuffer* const pic) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800143 glutInitDisplayMode(GLUT_RGBA);
144 glutInitWindowSize(pic->width, pic->height);
145 glutCreateWindow("WebP viewer");
Pascal Massimino7937b402011-12-13 14:02:04 -0800146 glutDisplayFunc(HandleDisplay);
147 glutIdleFunc(NULL);
148 glutKeyboardFunc(HandleKey);
149 glClearColor(0.0, 0.0, 0.0, 0.0);
150 HandleReshape(pic->width, pic->height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800151}
152
153//------------------------------------------------------------------------------
154// File decoding
155
Pascal Massimino861a5b72012-05-09 00:41:12 -0700156static int Decode(const int frame_number, uint32_t* const duration) {
157 WebPDecoderConfig* const config = kParams.config;
158 WebPData *data, image_data;
159 uint32_t x_off = 0, y_off = 0;
160 WebPDecBuffer* const output_buffer = &config->output;
161 int ok = 0;
162
163 ClearPreviousPic();
164 if (kParams.has_animation) {
Urvang Joshib74ed6e2012-06-20 10:59:40 -0700165 if (WebPMuxGetFrame(kParams.mux, frame_number, &image_data,
Pascal Massimino861a5b72012-05-09 00:41:12 -0700166 &x_off, &y_off, duration) != WEBP_MUX_OK) {
167 goto end;
168 }
169 if (x_off != 0 || y_off != 0) {
170 fprintf(stderr,
Pascal Massimino3927ff32012-05-09 01:57:15 -0700171 "Frame offsets not yet supported! Forcing offset to 0,0\n");
Pascal Massimino861a5b72012-05-09 00:41:12 -0700172 x_off = y_off = 0;
173 }
174 data = &image_data;
175 } else {
176 data = &kParams.data;
Pascal Massimino7937b402011-12-13 14:02:04 -0800177 }
178
179 output_buffer->colorspace = MODE_RGBA;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700180 ok = (WebPDecode(data->bytes_, data->size_, config) == VP8_STATUS_OK);
Pascal Massimino7937b402011-12-13 14:02:04 -0800181
182 end:
Pascal Massimino7937b402011-12-13 14:02:04 -0800183 if (!ok) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700184 fprintf(stderr, "Decoding of frame #%d failed!\n", frame_number);
185 } else {
186 kParams.pic = output_buffer;
Pascal Massimino7937b402011-12-13 14:02:04 -0800187 }
188 return ok;
189}
190
Pascal Massimino861a5b72012-05-09 00:41:12 -0700191static void decode_callback(int what) {
192 if (what == 0 && !kParams.done) {
193 uint32_t duration = 0;
194 if (kParams.mux != NULL) {
195 if (!Decode(kParams.frame_num, &duration)) {
196 kParams.decoding_error = 1;
197 kParams.done = 1;
198 } else {
199 ++kParams.frame_num;
200 if (kParams.frame_num > kParams.frame_max) {
201 kParams.frame_num = 1;
202 --kParams.loop_count;
203 kParams.done = (kParams.loop_count == 0);
204 }
205 }
206 }
207 glutPostRedisplay();
208 glutTimerFunc(duration, decode_callback, what);
209 }
210}
211
Pascal Massimino7937b402011-12-13 14:02:04 -0800212//------------------------------------------------------------------------------
213// Main
214
215static void Help(void) {
216 printf("Usage: vwebp in_file [options]\n\n"
217 "Decodes the WebP image file and visualize it using OpenGL\n"
218 "Options are:\n"
219 " -version .... print version number and exit.\n"
220 " -nofancy ..... don't use the fancy YUV420 upscaler.\n"
221 " -nofilter .... disable in-loop filtering.\n"
222 " -mt .......... use multi-threading\n"
223 " -crop <x> <y> <w> <h> ... crop output with the given rectangle\n"
224 " -scale <w> <h> .......... scale the output (*after* any cropping)\n"
225 " -h ....... this help message.\n"
226 );
227}
228
229int main(int argc, char *argv[]) {
230 WebPDecoderConfig config;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700231 WebPMuxError mux_err;
Pascal Massimino7937b402011-12-13 14:02:04 -0800232 int c;
233
234 if (!WebPInitDecoderConfig(&config)) {
235 fprintf(stderr, "Library version mismatch!\n");
236 return -1;
237 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700238 kParams.config = &config;
Pascal Massimino7937b402011-12-13 14:02:04 -0800239
240 for (c = 1; c < argc; ++c) {
241 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
242 Help();
243 return 0;
244 } else if (!strcmp(argv[c], "-nofancy")) {
245 config.options.no_fancy_upsampling = 1;
246 } else if (!strcmp(argv[c], "-nofilter")) {
247 config.options.bypass_filtering = 1;
248 } else if (!strcmp(argv[c], "-version")) {
249 const int version = WebPGetDecoderVersion();
250 printf("%d.%d.%d\n",
251 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
252 return 0;
253 } else if (!strcmp(argv[c], "-mt")) {
254 config.options.use_threads = 1;
255 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
256 config.options.use_cropping = 1;
257 config.options.crop_left = strtol(argv[++c], NULL, 0);
258 config.options.crop_top = strtol(argv[++c], NULL, 0);
259 config.options.crop_width = strtol(argv[++c], NULL, 0);
260 config.options.crop_height = strtol(argv[++c], NULL, 0);
261 } else if (!strcmp(argv[c], "-scale") && c < argc - 2) {
262 config.options.use_scaling = 1;
263 config.options.scaled_width = strtol(argv[++c], NULL, 0);
264 config.options.scaled_height = strtol(argv[++c], NULL, 0);
265 } else if (argv[c][0] == '-') {
266 printf("Unknown option '%s'\n", argv[c]);
267 Help();
268 return -1;
269 } else {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700270 kParams.file_name = argv[c];
Pascal Massimino7937b402011-12-13 14:02:04 -0800271 }
272 }
273
James Zern061263a2012-05-11 16:00:57 -0700274 if (kParams.file_name == NULL) {
275 printf("missing input file!!\n");
276 Help();
277 return 0;
278 }
279
280 if (!ExUtilReadFile(kParams.file_name,
281 &kParams.data.bytes_, &kParams.data.size_)) {
282 goto Error;
283 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700284
Urvang Joshif3bab8e2012-06-07 17:19:02 +0530285 kParams.mux = WebPMuxCreate(&kParams.data, 0);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700286 if (kParams.mux == NULL) {
287 fprintf(stderr, "Could not create demuxing object!\n");
288 goto Error;
Pascal Massimino7937b402011-12-13 14:02:04 -0800289 }
290
Pascal Massimino861a5b72012-05-09 00:41:12 -0700291 mux_err = WebPMuxGetFeatures(kParams.mux, &kParams.flags);
292 if (mux_err != WEBP_MUX_OK) {
293 goto Error;
294 }
295 if (kParams.flags & TILE_FLAG) {
296 fprintf(stderr, "Tiling is not supported for now!\n");
297 goto Error;
298 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800299
Pascal Massimino861a5b72012-05-09 00:41:12 -0700300 kParams.has_animation = !!(kParams.flags & ANIMATION_FLAG);
301
302 if (kParams.has_animation) {
303 mux_err = WebPMuxGetLoopCount(kParams.mux, &kParams.loop_count);
304 if (mux_err != WEBP_MUX_OK && mux_err != WEBP_MUX_NOT_FOUND) {
305 goto Error;
306 }
Urvang Joshifbdcb7e2012-06-11 12:24:45 +0530307 mux_err = WebPMuxNumChunks(kParams.mux, WEBP_CHUNK_IMAGE,
Pascal Massimino861a5b72012-05-09 00:41:12 -0700308 &kParams.frame_max);
Pascal Massimino3927ff32012-05-09 01:57:15 -0700309 if (mux_err != WEBP_MUX_OK) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700310 goto Error;
311 }
312 printf("VP8X: Found %d images in file (loop count = %d)\n",
313 kParams.frame_max, kParams.loop_count);
314 }
315
316 // Decode first frame
317 {
318 uint32_t duration;
319 if (!Decode(1, &duration)) goto Error;
320 }
321
322 // Start display (and timer)
Pascal Massimino7937b402011-12-13 14:02:04 -0800323 glutInit(&argc, argv);
James Zern880fd982012-05-25 14:53:46 -0700324#ifdef FREEGLUT
325 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
326#endif
Pascal Massimino861a5b72012-05-09 00:41:12 -0700327 StartDisplay(kParams.pic);
328 if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0);
329 glutMainLoop();
Pascal Massimino7937b402011-12-13 14:02:04 -0800330
331 // Should only be reached when using FREEGLUT:
Pascal Massimino861a5b72012-05-09 00:41:12 -0700332 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800333 return 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700334
335 Error:
336 ClearParams();
337 return -1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800338}
339
340//------------------------------------------------------------------------------