blob: dc7f299769705aef11a76f01f76e991632c1b2b9 [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
71static void ClearData(WebPData* data) {
72 if (data != NULL) {
73 free((void*)data->bytes_);
74 data->bytes_ = NULL;
75 data->size_ = 0;
76 }
77}
78
79static void ClearParams(void) {
80 ClearPreviousPic();
81 ClearData(&kParams.data);
82 WebPMuxDelete(kParams.mux);
83 kParams.mux = NULL;
84}
Pascal Massimino7937b402011-12-13 14:02:04 -080085
86//------------------------------------------------------------------------------
87// Callbacks
88
89static void HandleKey(unsigned char key, int pos_x, int pos_y) {
90 (void)pos_x;
91 (void)pos_y;
92 if (key == 'q' || key == 'Q' || key == 27 /* Esc */) {
93#ifdef FREEGLUT
94 glutLeaveMainLoop();
95#else
Pascal Massimino861a5b72012-05-09 00:41:12 -070096 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -080097 exit(0);
98#endif
99 } else if (key == 'i') {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700100 kParams.print_info = 1 - kParams.print_info;
Pascal Massimino7937b402011-12-13 14:02:04 -0800101 glutPostRedisplay();
102 }
103}
104
105static void HandleReshape(int width, int height) {
106 // TODO(skal): proper handling of resize, esp. for large pictures.
107 // + key control of the zoom.
108 glViewport(0, 0, width, height);
109 glMatrixMode(GL_PROJECTION);
110 glLoadIdentity();
111 glMatrixMode(GL_MODELVIEW);
112 glLoadIdentity();
113}
114
115static void PrintString(const char* const text) {
116 void* const font = GLUT_BITMAP_9_BY_15;
117 int i;
118 for (i = 0; text[i]; ++i) {
119 glutBitmapCharacter(font, text[i]);
120 }
121}
122
123static void HandleDisplay(void) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700124 const WebPDecBuffer* pic = kParams.pic;
125 if (pic == NULL) return;
Pascal Massimino7937b402011-12-13 14:02:04 -0800126 glClear(GL_COLOR_BUFFER_BIT);
127 glPushMatrix();
128 glPixelZoom(1, -1);
129 glRasterPos2f(-1, 1);
130 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700131 glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4);
132 glDrawPixels(pic->width, pic->height,
133 GL_RGBA, GL_UNSIGNED_BYTE,
134 (GLvoid*)pic->u.RGBA.rgba);
135 if (kParams.print_info) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800136 char tmp[32];
137
138 glColor4f(0.0, 0.0, 0.0, 0.0);
James Zern91179542011-12-16 19:30:33 -0800139 glRasterPos2f(-0.95f, 0.90f);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700140 PrintString(kParams.file_name);
Pascal Massimino7937b402011-12-13 14:02:04 -0800141
Pascal Massimino861a5b72012-05-09 00:41:12 -0700142 snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800143 glColor4f(0.0, 0.0, 0.0, 0.0);
James Zern91179542011-12-16 19:30:33 -0800144 glRasterPos2f(-0.95f, 0.80f);
Pascal Massimino7937b402011-12-13 14:02:04 -0800145 PrintString(tmp);
146 }
147 glFlush();
148}
149
Pascal Massimino861a5b72012-05-09 00:41:12 -0700150static void StartDisplay(const WebPDecBuffer* const pic) {
Pascal Massimino7937b402011-12-13 14:02:04 -0800151 glutInitDisplayMode(GLUT_RGBA);
152 glutInitWindowSize(pic->width, pic->height);
153 glutCreateWindow("WebP viewer");
Pascal Massimino7937b402011-12-13 14:02:04 -0800154 glutDisplayFunc(HandleDisplay);
155 glutIdleFunc(NULL);
156 glutKeyboardFunc(HandleKey);
157 glClearColor(0.0, 0.0, 0.0, 0.0);
158 HandleReshape(pic->width, pic->height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800159}
160
161//------------------------------------------------------------------------------
162// File decoding
163
Pascal Massimino861a5b72012-05-09 00:41:12 -0700164static int Decode(const int frame_number, uint32_t* const duration) {
165 WebPDecoderConfig* const config = kParams.config;
166 WebPData *data, image_data;
167 uint32_t x_off = 0, y_off = 0;
168 WebPDecBuffer* const output_buffer = &config->output;
169 int ok = 0;
170
171 ClearPreviousPic();
172 if (kParams.has_animation) {
173 if (WebPMuxGetFrame(kParams.mux, frame_number, &image_data, NULL,
174 &x_off, &y_off, duration) != WEBP_MUX_OK) {
175 goto end;
176 }
177 if (x_off != 0 || y_off != 0) {
178 fprintf(stderr,
Pascal Massimino3927ff32012-05-09 01:57:15 -0700179 "Frame offsets not yet supported! Forcing offset to 0,0\n");
Pascal Massimino861a5b72012-05-09 00:41:12 -0700180 x_off = y_off = 0;
181 }
182 data = &image_data;
183 } else {
184 data = &kParams.data;
Pascal Massimino7937b402011-12-13 14:02:04 -0800185 }
186
187 output_buffer->colorspace = MODE_RGBA;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700188 ok = (WebPDecode(data->bytes_, data->size_, config) == VP8_STATUS_OK);
Pascal Massimino7937b402011-12-13 14:02:04 -0800189
190 end:
Pascal Massimino7937b402011-12-13 14:02:04 -0800191 if (!ok) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700192 fprintf(stderr, "Decoding of frame #%d failed!\n", frame_number);
193 } else {
194 kParams.pic = output_buffer;
Pascal Massimino7937b402011-12-13 14:02:04 -0800195 }
196 return ok;
197}
198
Pascal Massimino861a5b72012-05-09 00:41:12 -0700199static void decode_callback(int what) {
200 if (what == 0 && !kParams.done) {
201 uint32_t duration = 0;
202 if (kParams.mux != NULL) {
203 if (!Decode(kParams.frame_num, &duration)) {
204 kParams.decoding_error = 1;
205 kParams.done = 1;
206 } else {
207 ++kParams.frame_num;
208 if (kParams.frame_num > kParams.frame_max) {
209 kParams.frame_num = 1;
210 --kParams.loop_count;
211 kParams.done = (kParams.loop_count == 0);
212 }
213 }
214 }
215 glutPostRedisplay();
216 glutTimerFunc(duration, decode_callback, what);
217 }
218}
219
Pascal Massimino7937b402011-12-13 14:02:04 -0800220//------------------------------------------------------------------------------
221// Main
222
223static void Help(void) {
224 printf("Usage: vwebp in_file [options]\n\n"
225 "Decodes the WebP image file and visualize it using OpenGL\n"
226 "Options are:\n"
227 " -version .... print version number and exit.\n"
228 " -nofancy ..... don't use the fancy YUV420 upscaler.\n"
229 " -nofilter .... disable in-loop filtering.\n"
230 " -mt .......... use multi-threading\n"
231 " -crop <x> <y> <w> <h> ... crop output with the given rectangle\n"
232 " -scale <w> <h> .......... scale the output (*after* any cropping)\n"
233 " -h ....... this help message.\n"
234 );
235}
236
237int main(int argc, char *argv[]) {
238 WebPDecoderConfig config;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700239 WebPMuxError mux_err;
Pascal Massimino7937b402011-12-13 14:02:04 -0800240 int c;
241
242 if (!WebPInitDecoderConfig(&config)) {
243 fprintf(stderr, "Library version mismatch!\n");
244 return -1;
245 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700246 kParams.config = &config;
Pascal Massimino7937b402011-12-13 14:02:04 -0800247
248 for (c = 1; c < argc; ++c) {
249 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
250 Help();
251 return 0;
252 } else if (!strcmp(argv[c], "-nofancy")) {
253 config.options.no_fancy_upsampling = 1;
254 } else if (!strcmp(argv[c], "-nofilter")) {
255 config.options.bypass_filtering = 1;
256 } else if (!strcmp(argv[c], "-version")) {
257 const int version = WebPGetDecoderVersion();
258 printf("%d.%d.%d\n",
259 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
260 return 0;
261 } else if (!strcmp(argv[c], "-mt")) {
262 config.options.use_threads = 1;
263 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
264 config.options.use_cropping = 1;
265 config.options.crop_left = strtol(argv[++c], NULL, 0);
266 config.options.crop_top = strtol(argv[++c], NULL, 0);
267 config.options.crop_width = strtol(argv[++c], NULL, 0);
268 config.options.crop_height = strtol(argv[++c], NULL, 0);
269 } else if (!strcmp(argv[c], "-scale") && c < argc - 2) {
270 config.options.use_scaling = 1;
271 config.options.scaled_width = strtol(argv[++c], NULL, 0);
272 config.options.scaled_height = strtol(argv[++c], NULL, 0);
273 } else if (argv[c][0] == '-') {
274 printf("Unknown option '%s'\n", argv[c]);
275 Help();
276 return -1;
277 } else {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700278 kParams.file_name = argv[c];
Pascal Massimino7937b402011-12-13 14:02:04 -0800279 }
280 }
281
James Zern061263a2012-05-11 16:00:57 -0700282 if (kParams.file_name == NULL) {
283 printf("missing input file!!\n");
284 Help();
285 return 0;
286 }
287
288 if (!ExUtilReadFile(kParams.file_name,
289 &kParams.data.bytes_, &kParams.data.size_)) {
290 goto Error;
291 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700292
293 kParams.mux =
294 WebPMuxCreate(kParams.data.bytes_, kParams.data.size_, 0, NULL);
295 if (kParams.mux == NULL) {
296 fprintf(stderr, "Could not create demuxing object!\n");
297 goto Error;
Pascal Massimino7937b402011-12-13 14:02:04 -0800298 }
299
Pascal Massimino861a5b72012-05-09 00:41:12 -0700300 mux_err = WebPMuxGetFeatures(kParams.mux, &kParams.flags);
301 if (mux_err != WEBP_MUX_OK) {
302 goto Error;
303 }
304 if (kParams.flags & TILE_FLAG) {
305 fprintf(stderr, "Tiling is not supported for now!\n");
306 goto Error;
307 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800308
Pascal Massimino861a5b72012-05-09 00:41:12 -0700309 kParams.has_animation = !!(kParams.flags & ANIMATION_FLAG);
310
311 if (kParams.has_animation) {
312 mux_err = WebPMuxGetLoopCount(kParams.mux, &kParams.loop_count);
313 if (mux_err != WEBP_MUX_OK && mux_err != WEBP_MUX_NOT_FOUND) {
314 goto Error;
315 }
316 mux_err = WebPMuxNumNamedElements(kParams.mux, "image",
317 &kParams.frame_max);
Pascal Massimino3927ff32012-05-09 01:57:15 -0700318 if (mux_err != WEBP_MUX_OK) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700319 goto Error;
320 }
321 printf("VP8X: Found %d images in file (loop count = %d)\n",
322 kParams.frame_max, kParams.loop_count);
323 }
324
325 // Decode first frame
326 {
327 uint32_t duration;
328 if (!Decode(1, &duration)) goto Error;
329 }
330
331 // Start display (and timer)
Pascal Massimino7937b402011-12-13 14:02:04 -0800332 glutInit(&argc, argv);
James Zern880fd982012-05-25 14:53:46 -0700333#ifdef FREEGLUT
334 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
335#endif
Pascal Massimino861a5b72012-05-09 00:41:12 -0700336 StartDisplay(kParams.pic);
337 if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0);
338 glutMainLoop();
Pascal Massimino7937b402011-12-13 14:02:04 -0800339
340 // Should only be reached when using FREEGLUT:
Pascal Massimino861a5b72012-05-09 00:41:12 -0700341 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800342 return 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700343
344 Error:
345 ClearParams();
346 return -1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800347}
348
349//------------------------------------------------------------------------------