blob: 90e98d2971dcd645bb621588d52824fde6952f84 [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
James Zernb35c07d2012-07-09 22:36:58 -0700130 glColor4f(0.0, 0.0, 0.0, 1.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);
James Zernb35c07d2012-07-09 22:36:58 -0700135 glColor4f(0.0, 0.0, 0.0, 1.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);
Urvang Joshi08220102012-06-20 11:18:35 -0700149 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
150 glEnable(GL_BLEND);
Pascal Massimino7937b402011-12-13 14:02:04 -0800151 glClearColor(0.0, 0.0, 0.0, 0.0);
152 HandleReshape(pic->width, pic->height);
Pascal Massimino7937b402011-12-13 14:02:04 -0800153}
154
155//------------------------------------------------------------------------------
156// File decoding
157
Pascal Massimino861a5b72012-05-09 00:41:12 -0700158static int Decode(const int frame_number, uint32_t* const duration) {
159 WebPDecoderConfig* const config = kParams.config;
160 WebPData *data, image_data;
161 uint32_t x_off = 0, y_off = 0;
162 WebPDecBuffer* const output_buffer = &config->output;
163 int ok = 0;
164
165 ClearPreviousPic();
166 if (kParams.has_animation) {
Urvang Joshib74ed6e2012-06-20 10:59:40 -0700167 if (WebPMuxGetFrame(kParams.mux, frame_number, &image_data,
Pascal Massimino861a5b72012-05-09 00:41:12 -0700168 &x_off, &y_off, duration) != WEBP_MUX_OK) {
169 goto end;
170 }
171 if (x_off != 0 || y_off != 0) {
172 fprintf(stderr,
Pascal Massimino3927ff32012-05-09 01:57:15 -0700173 "Frame offsets not yet supported! Forcing offset to 0,0\n");
Pascal Massimino861a5b72012-05-09 00:41:12 -0700174 x_off = y_off = 0;
175 }
176 data = &image_data;
177 } else {
178 data = &kParams.data;
Pascal Massimino7937b402011-12-13 14:02:04 -0800179 }
180
181 output_buffer->colorspace = MODE_RGBA;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700182 ok = (WebPDecode(data->bytes_, data->size_, config) == VP8_STATUS_OK);
Pascal Massimino7937b402011-12-13 14:02:04 -0800183
184 end:
Pascal Massimino7937b402011-12-13 14:02:04 -0800185 if (!ok) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700186 fprintf(stderr, "Decoding of frame #%d failed!\n", frame_number);
187 } else {
188 kParams.pic = output_buffer;
Pascal Massimino7937b402011-12-13 14:02:04 -0800189 }
190 return ok;
191}
192
Pascal Massimino861a5b72012-05-09 00:41:12 -0700193static void decode_callback(int what) {
194 if (what == 0 && !kParams.done) {
195 uint32_t duration = 0;
196 if (kParams.mux != NULL) {
197 if (!Decode(kParams.frame_num, &duration)) {
198 kParams.decoding_error = 1;
199 kParams.done = 1;
200 } else {
201 ++kParams.frame_num;
202 if (kParams.frame_num > kParams.frame_max) {
203 kParams.frame_num = 1;
204 --kParams.loop_count;
205 kParams.done = (kParams.loop_count == 0);
206 }
207 }
208 }
209 glutPostRedisplay();
210 glutTimerFunc(duration, decode_callback, what);
211 }
212}
213
Pascal Massimino7937b402011-12-13 14:02:04 -0800214//------------------------------------------------------------------------------
215// Main
216
217static void Help(void) {
218 printf("Usage: vwebp in_file [options]\n\n"
219 "Decodes the WebP image file and visualize it using OpenGL\n"
220 "Options are:\n"
221 " -version .... print version number and exit.\n"
222 " -nofancy ..... don't use the fancy YUV420 upscaler.\n"
223 " -nofilter .... disable in-loop filtering.\n"
224 " -mt .......... use multi-threading\n"
225 " -crop <x> <y> <w> <h> ... crop output with the given rectangle\n"
226 " -scale <w> <h> .......... scale the output (*after* any cropping)\n"
227 " -h ....... this help message.\n"
228 );
229}
230
231int main(int argc, char *argv[]) {
232 WebPDecoderConfig config;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700233 WebPMuxError mux_err;
Pascal Massimino7937b402011-12-13 14:02:04 -0800234 int c;
235
236 if (!WebPInitDecoderConfig(&config)) {
237 fprintf(stderr, "Library version mismatch!\n");
238 return -1;
239 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700240 kParams.config = &config;
Pascal Massimino7937b402011-12-13 14:02:04 -0800241
242 for (c = 1; c < argc; ++c) {
243 if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) {
244 Help();
245 return 0;
246 } else if (!strcmp(argv[c], "-nofancy")) {
247 config.options.no_fancy_upsampling = 1;
248 } else if (!strcmp(argv[c], "-nofilter")) {
249 config.options.bypass_filtering = 1;
250 } else if (!strcmp(argv[c], "-version")) {
251 const int version = WebPGetDecoderVersion();
252 printf("%d.%d.%d\n",
253 (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff);
254 return 0;
255 } else if (!strcmp(argv[c], "-mt")) {
256 config.options.use_threads = 1;
257 } else if (!strcmp(argv[c], "-crop") && c < argc - 4) {
258 config.options.use_cropping = 1;
259 config.options.crop_left = strtol(argv[++c], NULL, 0);
260 config.options.crop_top = strtol(argv[++c], NULL, 0);
261 config.options.crop_width = strtol(argv[++c], NULL, 0);
262 config.options.crop_height = strtol(argv[++c], NULL, 0);
263 } else if (!strcmp(argv[c], "-scale") && c < argc - 2) {
264 config.options.use_scaling = 1;
265 config.options.scaled_width = strtol(argv[++c], NULL, 0);
266 config.options.scaled_height = strtol(argv[++c], NULL, 0);
267 } else if (argv[c][0] == '-') {
268 printf("Unknown option '%s'\n", argv[c]);
269 Help();
270 return -1;
271 } else {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700272 kParams.file_name = argv[c];
Pascal Massimino7937b402011-12-13 14:02:04 -0800273 }
274 }
275
James Zern061263a2012-05-11 16:00:57 -0700276 if (kParams.file_name == NULL) {
277 printf("missing input file!!\n");
278 Help();
279 return 0;
280 }
281
282 if (!ExUtilReadFile(kParams.file_name,
283 &kParams.data.bytes_, &kParams.data.size_)) {
284 goto Error;
285 }
Pascal Massimino861a5b72012-05-09 00:41:12 -0700286
Urvang Joshif3bab8e2012-06-07 17:19:02 +0530287 kParams.mux = WebPMuxCreate(&kParams.data, 0);
Pascal Massimino861a5b72012-05-09 00:41:12 -0700288 if (kParams.mux == NULL) {
289 fprintf(stderr, "Could not create demuxing object!\n");
290 goto Error;
Pascal Massimino7937b402011-12-13 14:02:04 -0800291 }
292
Pascal Massimino861a5b72012-05-09 00:41:12 -0700293 mux_err = WebPMuxGetFeatures(kParams.mux, &kParams.flags);
294 if (mux_err != WEBP_MUX_OK) {
295 goto Error;
296 }
297 if (kParams.flags & TILE_FLAG) {
298 fprintf(stderr, "Tiling is not supported for now!\n");
299 goto Error;
300 }
Pascal Massimino7937b402011-12-13 14:02:04 -0800301
Pascal Massimino861a5b72012-05-09 00:41:12 -0700302 kParams.has_animation = !!(kParams.flags & ANIMATION_FLAG);
303
304 if (kParams.has_animation) {
305 mux_err = WebPMuxGetLoopCount(kParams.mux, &kParams.loop_count);
306 if (mux_err != WEBP_MUX_OK && mux_err != WEBP_MUX_NOT_FOUND) {
307 goto Error;
308 }
Urvang Joshifbdcb7e2012-06-11 12:24:45 +0530309 mux_err = WebPMuxNumChunks(kParams.mux, WEBP_CHUNK_IMAGE,
Pascal Massimino861a5b72012-05-09 00:41:12 -0700310 &kParams.frame_max);
Pascal Massimino3927ff32012-05-09 01:57:15 -0700311 if (mux_err != WEBP_MUX_OK) {
Pascal Massimino861a5b72012-05-09 00:41:12 -0700312 goto Error;
313 }
314 printf("VP8X: Found %d images in file (loop count = %d)\n",
315 kParams.frame_max, kParams.loop_count);
316 }
317
318 // Decode first frame
319 {
320 uint32_t duration;
321 if (!Decode(1, &duration)) goto Error;
322 }
323
324 // Start display (and timer)
Pascal Massimino7937b402011-12-13 14:02:04 -0800325 glutInit(&argc, argv);
James Zern880fd982012-05-25 14:53:46 -0700326#ifdef FREEGLUT
327 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
328#endif
Pascal Massimino861a5b72012-05-09 00:41:12 -0700329 StartDisplay(kParams.pic);
330 if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0);
331 glutMainLoop();
Pascal Massimino7937b402011-12-13 14:02:04 -0800332
333 // Should only be reached when using FREEGLUT:
Pascal Massimino861a5b72012-05-09 00:41:12 -0700334 ClearParams();
Pascal Massimino7937b402011-12-13 14:02:04 -0800335 return 0;
Pascal Massimino861a5b72012-05-09 00:41:12 -0700336
337 Error:
338 ClearParams();
339 return -1;
Pascal Massimino7937b402011-12-13 14:02:04 -0800340}
341
342//------------------------------------------------------------------------------