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 | 9117954 | 2011-12-16 19:30:33 -0800 | [diff] [blame] | 34 | #ifdef _MSC_VER |
| 35 | #define snprintf _snprintf |
| 36 | #endif |
| 37 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 38 | static void Help(void); |
| 39 | |
| 40 | // Unfortunate global variables. Gathered into a struct for comfort. |
| 41 | static struct { |
| 42 | int has_animation; |
| 43 | int done; |
| 44 | int decoding_error; |
| 45 | int print_info; |
| 46 | |
| 47 | uint32_t flags; |
| 48 | uint32_t loop_count; |
| 49 | int frame_num; |
| 50 | int frame_max; |
| 51 | |
| 52 | const char* file_name; |
| 53 | WebPData data; |
| 54 | WebPMux* mux; |
| 55 | WebPDecoderConfig* config; |
| 56 | const WebPDecBuffer* pic; |
| 57 | } kParams = { |
| 58 | 0, 0, 0, 0, // has_animation, ... |
| 59 | 0, 1, 1, 0, // flags, ... |
| 60 | NULL, { NULL, 0 }, // file_name, ... |
| 61 | NULL, NULL, NULL // mux, ... |
| 62 | }; |
| 63 | |
| 64 | static void ClearPreviousPic(void) { |
| 65 | WebPFreeDecBuffer((WebPDecBuffer*)kParams.pic); |
| 66 | kParams.pic = NULL; |
| 67 | } |
| 68 | |
| 69 | static void ClearData(WebPData* data) { |
| 70 | if (data != NULL) { |
| 71 | free((void*)data->bytes_); |
| 72 | data->bytes_ = NULL; |
| 73 | data->size_ = 0; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static void ClearParams(void) { |
| 78 | ClearPreviousPic(); |
| 79 | ClearData(&kParams.data); |
| 80 | WebPMuxDelete(kParams.mux); |
| 81 | kParams.mux = NULL; |
| 82 | } |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 83 | |
| 84 | //------------------------------------------------------------------------------ |
| 85 | // Callbacks |
| 86 | |
| 87 | static void HandleKey(unsigned char key, int pos_x, int pos_y) { |
| 88 | (void)pos_x; |
| 89 | (void)pos_y; |
| 90 | if (key == 'q' || key == 'Q' || key == 27 /* Esc */) { |
| 91 | #ifdef FREEGLUT |
| 92 | glutLeaveMainLoop(); |
| 93 | #else |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 94 | ClearParams(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 95 | exit(0); |
| 96 | #endif |
| 97 | } else if (key == 'i') { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 98 | kParams.print_info = 1 - kParams.print_info; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 99 | glutPostRedisplay(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static void HandleReshape(int width, int height) { |
| 104 | // TODO(skal): proper handling of resize, esp. for large pictures. |
| 105 | // + key control of the zoom. |
| 106 | glViewport(0, 0, width, height); |
| 107 | glMatrixMode(GL_PROJECTION); |
| 108 | glLoadIdentity(); |
| 109 | glMatrixMode(GL_MODELVIEW); |
| 110 | glLoadIdentity(); |
| 111 | } |
| 112 | |
| 113 | static void PrintString(const char* const text) { |
| 114 | void* const font = GLUT_BITMAP_9_BY_15; |
| 115 | int i; |
| 116 | for (i = 0; text[i]; ++i) { |
| 117 | glutBitmapCharacter(font, text[i]); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | static void HandleDisplay(void) { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 122 | const WebPDecBuffer* pic = kParams.pic; |
| 123 | if (pic == NULL) return; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 124 | glClear(GL_COLOR_BUFFER_BIT); |
| 125 | glPushMatrix(); |
| 126 | glPixelZoom(1, -1); |
| 127 | glRasterPos2f(-1, 1); |
| 128 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 129 | glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4); |
| 130 | glDrawPixels(pic->width, pic->height, |
| 131 | GL_RGBA, GL_UNSIGNED_BYTE, |
| 132 | (GLvoid*)pic->u.RGBA.rgba); |
| 133 | if (kParams.print_info) { |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 134 | char tmp[32]; |
| 135 | |
| 136 | glColor4f(0.0, 0.0, 0.0, 0.0); |
James Zern | 9117954 | 2011-12-16 19:30:33 -0800 | [diff] [blame] | 137 | glRasterPos2f(-0.95f, 0.90f); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 138 | PrintString(kParams.file_name); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 139 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 140 | snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 141 | glColor4f(0.0, 0.0, 0.0, 0.0); |
James Zern | 9117954 | 2011-12-16 19:30:33 -0800 | [diff] [blame] | 142 | glRasterPos2f(-0.95f, 0.80f); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 143 | PrintString(tmp); |
| 144 | } |
| 145 | glFlush(); |
| 146 | } |
| 147 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 148 | static void StartDisplay(const WebPDecBuffer* const pic) { |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 149 | glutInitDisplayMode(GLUT_RGBA); |
| 150 | glutInitWindowSize(pic->width, pic->height); |
| 151 | glutCreateWindow("WebP viewer"); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 152 | glutDisplayFunc(HandleDisplay); |
| 153 | glutIdleFunc(NULL); |
| 154 | glutKeyboardFunc(HandleKey); |
| 155 | glClearColor(0.0, 0.0, 0.0, 0.0); |
| 156 | HandleReshape(pic->width, pic->height); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | //------------------------------------------------------------------------------ |
| 160 | // File decoding |
| 161 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 162 | static int ReadFile(void) { |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 163 | int ok; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 164 | void* data = NULL; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 165 | size_t data_size = 0; |
| 166 | const char* const file_name = kParams.file_name; |
| 167 | FILE* in = NULL; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 168 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 169 | if (file_name == NULL) { |
| 170 | printf("missing input file!!\n"); |
| 171 | Help(); |
| 172 | return 0; |
| 173 | } |
| 174 | in = fopen(file_name, "rb"); |
| 175 | if (in == NULL) { |
| 176 | fprintf(stderr, "cannot open input file '%s'\n", file_name); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | fseek(in, 0, SEEK_END); |
| 180 | data_size = ftell(in); |
| 181 | fseek(in, 0, SEEK_SET); |
| 182 | data = malloc(data_size); |
| 183 | if (data == NULL) return 0; |
| 184 | ok = (fread(data, data_size, 1, in) == 1); |
| 185 | fclose(in); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 186 | kParams.data.bytes_ = data; |
| 187 | kParams.data.size_ = data_size; |
| 188 | |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 189 | if (!ok) { |
| 190 | fprintf(stderr, "Could not read %zu bytes of data from file %s\n", |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 191 | data_size, file_name); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 192 | return 0; |
| 193 | } |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 194 | return 1; |
| 195 | } |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 196 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 197 | static int Decode(const int frame_number, uint32_t* const duration) { |
| 198 | WebPDecoderConfig* const config = kParams.config; |
| 199 | WebPData *data, image_data; |
| 200 | uint32_t x_off = 0, y_off = 0; |
| 201 | WebPDecBuffer* const output_buffer = &config->output; |
| 202 | int ok = 0; |
| 203 | |
| 204 | ClearPreviousPic(); |
| 205 | if (kParams.has_animation) { |
| 206 | if (WebPMuxGetFrame(kParams.mux, frame_number, &image_data, NULL, |
| 207 | &x_off, &y_off, duration) != WEBP_MUX_OK) { |
| 208 | goto end; |
| 209 | } |
| 210 | if (x_off != 0 || y_off != 0) { |
| 211 | fprintf(stderr, |
Pascal Massimino | 3927ff3 | 2012-05-09 01:57:15 -0700 | [diff] [blame] | 212 | "Frame offsets not yet supported! Forcing offset to 0,0\n"); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 213 | x_off = y_off = 0; |
| 214 | } |
| 215 | data = &image_data; |
| 216 | } else { |
| 217 | data = &kParams.data; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | output_buffer->colorspace = MODE_RGBA; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 221 | ok = (WebPDecode(data->bytes_, data->size_, config) == VP8_STATUS_OK); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 222 | |
| 223 | end: |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 224 | if (!ok) { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 225 | fprintf(stderr, "Decoding of frame #%d failed!\n", frame_number); |
| 226 | } else { |
| 227 | kParams.pic = output_buffer; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 228 | } |
| 229 | return ok; |
| 230 | } |
| 231 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 232 | static void decode_callback(int what) { |
| 233 | if (what == 0 && !kParams.done) { |
| 234 | uint32_t duration = 0; |
| 235 | if (kParams.mux != NULL) { |
| 236 | if (!Decode(kParams.frame_num, &duration)) { |
| 237 | kParams.decoding_error = 1; |
| 238 | kParams.done = 1; |
| 239 | } else { |
| 240 | ++kParams.frame_num; |
| 241 | if (kParams.frame_num > kParams.frame_max) { |
| 242 | kParams.frame_num = 1; |
| 243 | --kParams.loop_count; |
| 244 | kParams.done = (kParams.loop_count == 0); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | glutPostRedisplay(); |
| 249 | glutTimerFunc(duration, decode_callback, what); |
| 250 | } |
| 251 | } |
| 252 | |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 253 | //------------------------------------------------------------------------------ |
| 254 | // Main |
| 255 | |
| 256 | static void Help(void) { |
| 257 | printf("Usage: vwebp in_file [options]\n\n" |
| 258 | "Decodes the WebP image file and visualize it using OpenGL\n" |
| 259 | "Options are:\n" |
| 260 | " -version .... print version number and exit.\n" |
| 261 | " -nofancy ..... don't use the fancy YUV420 upscaler.\n" |
| 262 | " -nofilter .... disable in-loop filtering.\n" |
| 263 | " -mt .......... use multi-threading\n" |
| 264 | " -crop <x> <y> <w> <h> ... crop output with the given rectangle\n" |
| 265 | " -scale <w> <h> .......... scale the output (*after* any cropping)\n" |
| 266 | " -h ....... this help message.\n" |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | int main(int argc, char *argv[]) { |
| 271 | WebPDecoderConfig config; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 272 | WebPMuxError mux_err; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 273 | int c; |
| 274 | |
| 275 | if (!WebPInitDecoderConfig(&config)) { |
| 276 | fprintf(stderr, "Library version mismatch!\n"); |
| 277 | return -1; |
| 278 | } |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 279 | kParams.config = &config; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 280 | |
| 281 | for (c = 1; c < argc; ++c) { |
| 282 | if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) { |
| 283 | Help(); |
| 284 | return 0; |
| 285 | } else if (!strcmp(argv[c], "-nofancy")) { |
| 286 | config.options.no_fancy_upsampling = 1; |
| 287 | } else if (!strcmp(argv[c], "-nofilter")) { |
| 288 | config.options.bypass_filtering = 1; |
| 289 | } else if (!strcmp(argv[c], "-version")) { |
| 290 | const int version = WebPGetDecoderVersion(); |
| 291 | printf("%d.%d.%d\n", |
| 292 | (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff); |
| 293 | return 0; |
| 294 | } else if (!strcmp(argv[c], "-mt")) { |
| 295 | config.options.use_threads = 1; |
| 296 | } else if (!strcmp(argv[c], "-crop") && c < argc - 4) { |
| 297 | config.options.use_cropping = 1; |
| 298 | config.options.crop_left = strtol(argv[++c], NULL, 0); |
| 299 | config.options.crop_top = strtol(argv[++c], NULL, 0); |
| 300 | config.options.crop_width = strtol(argv[++c], NULL, 0); |
| 301 | config.options.crop_height = strtol(argv[++c], NULL, 0); |
| 302 | } else if (!strcmp(argv[c], "-scale") && c < argc - 2) { |
| 303 | config.options.use_scaling = 1; |
| 304 | config.options.scaled_width = strtol(argv[++c], NULL, 0); |
| 305 | config.options.scaled_height = strtol(argv[++c], NULL, 0); |
| 306 | } else if (argv[c][0] == '-') { |
| 307 | printf("Unknown option '%s'\n", argv[c]); |
| 308 | Help(); |
| 309 | return -1; |
| 310 | } else { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 311 | kParams.file_name = argv[c]; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 315 | if (!ReadFile()) goto Error; |
| 316 | |
| 317 | kParams.mux = |
| 318 | WebPMuxCreate(kParams.data.bytes_, kParams.data.size_, 0, NULL); |
| 319 | if (kParams.mux == NULL) { |
| 320 | fprintf(stderr, "Could not create demuxing object!\n"); |
| 321 | goto Error; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 324 | mux_err = WebPMuxGetFeatures(kParams.mux, &kParams.flags); |
| 325 | if (mux_err != WEBP_MUX_OK) { |
| 326 | goto Error; |
| 327 | } |
| 328 | if (kParams.flags & TILE_FLAG) { |
| 329 | fprintf(stderr, "Tiling is not supported for now!\n"); |
| 330 | goto Error; |
| 331 | } |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 332 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 333 | kParams.has_animation = !!(kParams.flags & ANIMATION_FLAG); |
| 334 | |
| 335 | if (kParams.has_animation) { |
| 336 | mux_err = WebPMuxGetLoopCount(kParams.mux, &kParams.loop_count); |
| 337 | if (mux_err != WEBP_MUX_OK && mux_err != WEBP_MUX_NOT_FOUND) { |
| 338 | goto Error; |
| 339 | } |
| 340 | mux_err = WebPMuxNumNamedElements(kParams.mux, "image", |
| 341 | &kParams.frame_max); |
Pascal Massimino | 3927ff3 | 2012-05-09 01:57:15 -0700 | [diff] [blame] | 342 | if (mux_err != WEBP_MUX_OK) { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 343 | goto Error; |
| 344 | } |
| 345 | printf("VP8X: Found %d images in file (loop count = %d)\n", |
| 346 | kParams.frame_max, kParams.loop_count); |
| 347 | } |
| 348 | |
| 349 | // Decode first frame |
| 350 | { |
| 351 | uint32_t duration; |
| 352 | if (!Decode(1, &duration)) goto Error; |
| 353 | } |
| 354 | |
| 355 | // Start display (and timer) |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 356 | glutInit(&argc, argv); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 357 | StartDisplay(kParams.pic); |
| 358 | if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0); |
| 359 | glutMainLoop(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 360 | |
| 361 | // Should only be reached when using FREEGLUT: |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 362 | ClearParams(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 363 | return 0; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 364 | |
| 365 | Error: |
| 366 | ClearParams(); |
| 367 | return -1; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | //------------------------------------------------------------------------------ |