James Zern | ad1e163 | 2012-01-06 14:49:06 -0800 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 2 | // |
| 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 Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 12 | // gcc -o vwebp vwebp.c -O3 -lwebp -lwebpmux -lglut -lGL -lpthread -lm |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 13 | // Compiling on Mac + XCode: |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 14 | // gcc -o vwebp vwebp.c -lwebp -lwebpmux -framework GLUT -framework OpenGL |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 15 | // |
| 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 Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 23 | #include "webp/mux.h" |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 24 | |
| 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 Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 34 | #include "./example_util.h" |
| 35 | |
James Zern | 9117954 | 2011-12-16 19:30:33 -0800 | [diff] [blame] | 36 | #ifdef _MSC_VER |
| 37 | #define snprintf _snprintf |
| 38 | #endif |
| 39 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 40 | static void Help(void); |
| 41 | |
| 42 | // Unfortunate global variables. Gathered into a struct for comfort. |
| 43 | static 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 | |
| 66 | static void ClearPreviousPic(void) { |
| 67 | WebPFreeDecBuffer((WebPDecBuffer*)kParams.pic); |
| 68 | kParams.pic = NULL; |
| 69 | } |
| 70 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 71 | static void ClearParams(void) { |
| 72 | ClearPreviousPic(); |
Urvang Joshi | f3bab8e | 2012-06-07 17:19:02 +0530 | [diff] [blame] | 73 | WebPDataClear(&kParams.data); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 74 | WebPMuxDelete(kParams.mux); |
| 75 | kParams.mux = NULL; |
| 76 | } |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 77 | |
| 78 | //------------------------------------------------------------------------------ |
| 79 | // Callbacks |
| 80 | |
| 81 | static 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 Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 88 | ClearParams(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 89 | exit(0); |
| 90 | #endif |
| 91 | } else if (key == 'i') { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 92 | kParams.print_info = 1 - kParams.print_info; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 93 | glutPostRedisplay(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static 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 | |
| 107 | static 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 | |
| 115 | static void HandleDisplay(void) { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 116 | const WebPDecBuffer* pic = kParams.pic; |
| 117 | if (pic == NULL) return; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 118 | glClear(GL_COLOR_BUFFER_BIT); |
| 119 | glPushMatrix(); |
| 120 | glPixelZoom(1, -1); |
| 121 | glRasterPos2f(-1, 1); |
| 122 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 123 | 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 Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 128 | char tmp[32]; |
| 129 | |
| 130 | glColor4f(0.0, 0.0, 0.0, 0.0); |
James Zern | 9117954 | 2011-12-16 19:30:33 -0800 | [diff] [blame] | 131 | glRasterPos2f(-0.95f, 0.90f); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 132 | PrintString(kParams.file_name); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 133 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 134 | snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 135 | glColor4f(0.0, 0.0, 0.0, 0.0); |
James Zern | 9117954 | 2011-12-16 19:30:33 -0800 | [diff] [blame] | 136 | glRasterPos2f(-0.95f, 0.80f); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 137 | PrintString(tmp); |
| 138 | } |
| 139 | glFlush(); |
| 140 | } |
| 141 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 142 | static void StartDisplay(const WebPDecBuffer* const pic) { |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 143 | glutInitDisplayMode(GLUT_RGBA); |
| 144 | glutInitWindowSize(pic->width, pic->height); |
| 145 | glutCreateWindow("WebP viewer"); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 146 | 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 Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | //------------------------------------------------------------------------------ |
| 154 | // File decoding |
| 155 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 156 | static 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) { |
| 165 | if (WebPMuxGetFrame(kParams.mux, frame_number, &image_data, NULL, |
| 166 | &x_off, &y_off, duration) != WEBP_MUX_OK) { |
| 167 | goto end; |
| 168 | } |
| 169 | if (x_off != 0 || y_off != 0) { |
| 170 | fprintf(stderr, |
Pascal Massimino | 3927ff3 | 2012-05-09 01:57:15 -0700 | [diff] [blame] | 171 | "Frame offsets not yet supported! Forcing offset to 0,0\n"); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 172 | x_off = y_off = 0; |
| 173 | } |
| 174 | data = &image_data; |
| 175 | } else { |
| 176 | data = &kParams.data; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | output_buffer->colorspace = MODE_RGBA; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 180 | ok = (WebPDecode(data->bytes_, data->size_, config) == VP8_STATUS_OK); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 181 | |
| 182 | end: |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 183 | if (!ok) { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 184 | fprintf(stderr, "Decoding of frame #%d failed!\n", frame_number); |
| 185 | } else { |
| 186 | kParams.pic = output_buffer; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 187 | } |
| 188 | return ok; |
| 189 | } |
| 190 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 191 | static 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 Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 212 | //------------------------------------------------------------------------------ |
| 213 | // Main |
| 214 | |
| 215 | static 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 | |
| 229 | int main(int argc, char *argv[]) { |
| 230 | WebPDecoderConfig config; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 231 | WebPMuxError mux_err; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 232 | int c; |
| 233 | |
| 234 | if (!WebPInitDecoderConfig(&config)) { |
| 235 | fprintf(stderr, "Library version mismatch!\n"); |
| 236 | return -1; |
| 237 | } |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 238 | kParams.config = &config; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 239 | |
| 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 Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 270 | kParams.file_name = argv[c]; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 274 | 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 Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 284 | |
Urvang Joshi | f3bab8e | 2012-06-07 17:19:02 +0530 | [diff] [blame] | 285 | kParams.mux = WebPMuxCreate(&kParams.data, 0); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 286 | if (kParams.mux == NULL) { |
| 287 | fprintf(stderr, "Could not create demuxing object!\n"); |
| 288 | goto Error; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 291 | 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 Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 299 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 300 | 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 Joshi | fbdcb7e | 2012-06-11 12:24:45 +0530 | [diff] [blame] | 307 | mux_err = WebPMuxNumChunks(kParams.mux, WEBP_CHUNK_IMAGE, |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 308 | &kParams.frame_max); |
Pascal Massimino | 3927ff3 | 2012-05-09 01:57:15 -0700 | [diff] [blame] | 309 | if (mux_err != WEBP_MUX_OK) { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 310 | 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 Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 323 | glutInit(&argc, argv); |
James Zern | 880fd98 | 2012-05-25 14:53:46 -0700 | [diff] [blame] | 324 | #ifdef FREEGLUT |
| 325 | glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION); |
| 326 | #endif |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 327 | StartDisplay(kParams.pic); |
| 328 | if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0); |
| 329 | glutMainLoop(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 330 | |
| 331 | // Should only be reached when using FREEGLUT: |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 332 | ClearParams(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 333 | return 0; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 334 | |
| 335 | Error: |
| 336 | ClearParams(); |
| 337 | return -1; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | //------------------------------------------------------------------------------ |