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 | |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 49 | int canvas_width, canvas_height; |
Urvang Joshi | 6808e69 | 2012-07-06 11:35:36 +0530 | [diff] [blame] | 50 | int loop_count; |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 51 | uint32_t bg_color; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 52 | |
| 53 | const char* file_name; |
| 54 | WebPData data; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 55 | WebPDecoderConfig* config; |
| 56 | const WebPDecBuffer* pic; |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 57 | WebPDemuxer* dmux; |
| 58 | WebPIterator frameiter; |
| 59 | } kParams; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 60 | |
| 61 | static void ClearPreviousPic(void) { |
| 62 | WebPFreeDecBuffer((WebPDecBuffer*)kParams.pic); |
| 63 | kParams.pic = NULL; |
| 64 | } |
| 65 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 66 | static void ClearParams(void) { |
| 67 | ClearPreviousPic(); |
Urvang Joshi | f3bab8e | 2012-06-07 17:19:02 +0530 | [diff] [blame] | 68 | WebPDataClear(&kParams.data); |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 69 | WebPDemuxReleaseIterator(&kParams.frameiter); |
| 70 | WebPDemuxDelete(kParams.dmux); |
| 71 | kParams.dmux = NULL; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 72 | } |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 73 | |
| 74 | //------------------------------------------------------------------------------ |
| 75 | // Callbacks |
| 76 | |
| 77 | static void HandleKey(unsigned char key, int pos_x, int pos_y) { |
| 78 | (void)pos_x; |
| 79 | (void)pos_y; |
| 80 | if (key == 'q' || key == 'Q' || key == 27 /* Esc */) { |
| 81 | #ifdef FREEGLUT |
| 82 | glutLeaveMainLoop(); |
| 83 | #else |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 84 | ClearParams(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 85 | exit(0); |
| 86 | #endif |
| 87 | } else if (key == 'i') { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 88 | kParams.print_info = 1 - kParams.print_info; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 89 | glutPostRedisplay(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static void HandleReshape(int width, int height) { |
| 94 | // TODO(skal): proper handling of resize, esp. for large pictures. |
| 95 | // + key control of the zoom. |
| 96 | glViewport(0, 0, width, height); |
| 97 | glMatrixMode(GL_PROJECTION); |
| 98 | glLoadIdentity(); |
| 99 | glMatrixMode(GL_MODELVIEW); |
| 100 | glLoadIdentity(); |
| 101 | } |
| 102 | |
| 103 | static void PrintString(const char* const text) { |
| 104 | void* const font = GLUT_BITMAP_9_BY_15; |
| 105 | int i; |
| 106 | for (i = 0; text[i]; ++i) { |
| 107 | glutBitmapCharacter(font, text[i]); |
| 108 | } |
| 109 | } |
| 110 | |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 111 | static float GetColorf(uint32_t color, int shift) { |
| 112 | return (color >> shift) / 255.f; |
| 113 | } |
| 114 | |
James Zern | a73b897 | 2012-07-09 23:01:52 -0700 | [diff] [blame] | 115 | static void DrawCheckerBoard(void) { |
| 116 | const int square_size = 8; // must be a power of 2 |
| 117 | int x, y; |
| 118 | GLint viewport[4]; // x, y, width, height |
| 119 | |
| 120 | glPushMatrix(); |
| 121 | |
| 122 | glGetIntegerv(GL_VIEWPORT, viewport); |
| 123 | // shift to integer coordinates with (0,0) being top-left. |
| 124 | glOrtho(0, viewport[2], viewport[3], 0, -1, 1); |
| 125 | for (y = 0; y < viewport[3]; y += square_size) { |
| 126 | for (x = 0; x < viewport[2]; x += square_size) { |
| 127 | const GLubyte color = 128 + 64 * (!((x + y) & square_size)); |
| 128 | glColor3ub(color, color, color); |
| 129 | glRecti(x, y, x + square_size, y + square_size); |
| 130 | } |
| 131 | } |
| 132 | glPopMatrix(); |
| 133 | } |
| 134 | |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 135 | static void HandleDisplay(void) { |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 136 | const WebPDecBuffer* const pic = kParams.pic; |
| 137 | const WebPIterator* const iter = &kParams.frameiter; |
skal | 41a6ced | 2012-11-15 09:35:01 +0100 | [diff] [blame^] | 138 | GLfloat xoff, yoff; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 139 | if (pic == NULL) return; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 140 | glPushMatrix(); |
| 141 | glPixelZoom(1, -1); |
skal | 41a6ced | 2012-11-15 09:35:01 +0100 | [diff] [blame^] | 142 | xoff = (GLfloat)(2. * iter->x_offset / kParams.canvas_width); |
| 143 | yoff = (GLfloat)(2. * iter->y_offset / kParams.canvas_height); |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 144 | glRasterPos2f(-1. + xoff, 1. - yoff); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 145 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 146 | glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4); |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 147 | if (iter->dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) { |
| 148 | glClear(GL_COLOR_BUFFER_BIT); // use clear color |
| 149 | } |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 150 | glDrawPixels(pic->width, pic->height, |
| 151 | GL_RGBA, GL_UNSIGNED_BYTE, |
| 152 | (GLvoid*)pic->u.RGBA.rgba); |
| 153 | if (kParams.print_info) { |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 154 | char tmp[32]; |
| 155 | |
James Zern | b35c07d | 2012-07-09 22:36:58 -0700 | [diff] [blame] | 156 | glColor4f(0.0, 0.0, 0.0, 1.0); |
James Zern | 9117954 | 2011-12-16 19:30:33 -0800 | [diff] [blame] | 157 | glRasterPos2f(-0.95f, 0.90f); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 158 | PrintString(kParams.file_name); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 159 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 160 | snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height); |
James Zern | b35c07d | 2012-07-09 22:36:58 -0700 | [diff] [blame] | 161 | glColor4f(0.0, 0.0, 0.0, 1.0); |
James Zern | 9117954 | 2011-12-16 19:30:33 -0800 | [diff] [blame] | 162 | glRasterPos2f(-0.95f, 0.80f); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 163 | PrintString(tmp); |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 164 | if (iter->x_offset != 0 || iter->y_offset != 0) { |
| 165 | snprintf(tmp, sizeof(tmp), " (offset:%d,%d)", |
| 166 | iter->x_offset, iter->y_offset); |
| 167 | glRasterPos2f(-0.95f, 0.70f); |
| 168 | PrintString(tmp); |
| 169 | } |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 170 | } |
James Zern | a73b897 | 2012-07-09 23:01:52 -0700 | [diff] [blame] | 171 | glPopMatrix(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 172 | glFlush(); |
| 173 | } |
| 174 | |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 175 | static void StartDisplay(void) { |
| 176 | const int width = kParams.canvas_width; |
| 177 | const int height = kParams.canvas_height; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 178 | glutInitDisplayMode(GLUT_RGBA); |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 179 | glutInitWindowSize(width, height); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 180 | glutCreateWindow("WebP viewer"); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 181 | glutDisplayFunc(HandleDisplay); |
| 182 | glutIdleFunc(NULL); |
| 183 | glutKeyboardFunc(HandleKey); |
Urvang Joshi | 0822010 | 2012-06-20 11:18:35 -0700 | [diff] [blame] | 184 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 185 | glEnable(GL_BLEND); |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 186 | glClearColor(GetColorf(kParams.bg_color, 0), |
| 187 | GetColorf(kParams.bg_color, 8), |
| 188 | GetColorf(kParams.bg_color, 16), |
| 189 | GetColorf(kParams.bg_color, 24)); |
| 190 | HandleReshape(width, height); |
| 191 | glClear(GL_COLOR_BUFFER_BIT); |
| 192 | DrawCheckerBoard(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | //------------------------------------------------------------------------------ |
| 196 | // File decoding |
| 197 | |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 198 | static int Decode(void) { // Fills kParams.frameiter |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 199 | const WebPIterator* const iter = &kParams.frameiter; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 200 | WebPDecoderConfig* const config = kParams.config; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 201 | WebPDecBuffer* const output_buffer = &config->output; |
| 202 | int ok = 0; |
| 203 | |
| 204 | ClearPreviousPic(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 205 | output_buffer->colorspace = MODE_RGBA; |
Urvang Joshi | a00a3da | 2012-10-31 17:49:15 -0700 | [diff] [blame] | 206 | ok = (WebPDecode(iter->fragment.bytes, iter->fragment.size, |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 207 | config) == VP8_STATUS_OK); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 208 | if (!ok) { |
Urvang Joshi | a077072 | 2012-10-30 14:54:46 -0700 | [diff] [blame] | 209 | fprintf(stderr, "Decoding of frame #%d failed!\n", iter->frame_num); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 210 | } else { |
| 211 | kParams.pic = output_buffer; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 212 | } |
| 213 | return ok; |
| 214 | } |
| 215 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 216 | static void decode_callback(int what) { |
| 217 | if (what == 0 && !kParams.done) { |
Urvang Joshi | 6808e69 | 2012-07-06 11:35:36 +0530 | [diff] [blame] | 218 | int duration = 0; |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 219 | if (kParams.dmux != NULL) { |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 220 | WebPIterator* const iter = &kParams.frameiter; |
| 221 | if (!WebPDemuxNextFrame(iter)) { |
| 222 | WebPDemuxReleaseIterator(iter); |
| 223 | if (WebPDemuxGetFrame(kParams.dmux, 1, iter)) { |
| 224 | --kParams.loop_count; |
| 225 | kParams.done = (kParams.loop_count == 0); |
| 226 | } else { |
| 227 | kParams.decoding_error = 1; |
| 228 | kParams.done = 1; |
| 229 | return; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 232 | duration = iter->duration; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 233 | } |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 234 | if (!Decode()) { |
| 235 | kParams.decoding_error = 1; |
| 236 | kParams.done = 1; |
| 237 | } else { |
| 238 | glutPostRedisplay(); |
| 239 | glutTimerFunc(duration, decode_callback, what); |
| 240 | } |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 244 | //------------------------------------------------------------------------------ |
| 245 | // Main |
| 246 | |
| 247 | static void Help(void) { |
| 248 | printf("Usage: vwebp in_file [options]\n\n" |
| 249 | "Decodes the WebP image file and visualize it using OpenGL\n" |
| 250 | "Options are:\n" |
| 251 | " -version .... print version number and exit.\n" |
| 252 | " -nofancy ..... don't use the fancy YUV420 upscaler.\n" |
| 253 | " -nofilter .... disable in-loop filtering.\n" |
| 254 | " -mt .......... use multi-threading\n" |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 255 | " -info ........ print info.\n" |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 256 | " -h ....... this help message.\n" |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | int main(int argc, char *argv[]) { |
| 261 | WebPDecoderConfig config; |
| 262 | int c; |
| 263 | |
| 264 | if (!WebPInitDecoderConfig(&config)) { |
| 265 | fprintf(stderr, "Library version mismatch!\n"); |
| 266 | return -1; |
| 267 | } |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 268 | kParams.config = &config; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 269 | |
| 270 | for (c = 1; c < argc; ++c) { |
| 271 | if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) { |
| 272 | Help(); |
| 273 | return 0; |
| 274 | } else if (!strcmp(argv[c], "-nofancy")) { |
| 275 | config.options.no_fancy_upsampling = 1; |
| 276 | } else if (!strcmp(argv[c], "-nofilter")) { |
| 277 | config.options.bypass_filtering = 1; |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 278 | } else if (!strcmp(argv[c], "-info")) { |
| 279 | kParams.print_info = 1; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 280 | } else if (!strcmp(argv[c], "-version")) { |
| 281 | const int version = WebPGetDecoderVersion(); |
| 282 | printf("%d.%d.%d\n", |
| 283 | (version >> 16) & 0xff, (version >> 8) & 0xff, version & 0xff); |
| 284 | return 0; |
| 285 | } else if (!strcmp(argv[c], "-mt")) { |
| 286 | config.options.use_threads = 1; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 287 | } else if (argv[c][0] == '-') { |
| 288 | printf("Unknown option '%s'\n", argv[c]); |
| 289 | Help(); |
| 290 | return -1; |
| 291 | } else { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 292 | kParams.file_name = argv[c]; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 296 | if (kParams.file_name == NULL) { |
| 297 | printf("missing input file!!\n"); |
| 298 | Help(); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | if (!ExUtilReadFile(kParams.file_name, |
Urvang Joshi | a077072 | 2012-10-30 14:54:46 -0700 | [diff] [blame] | 303 | &kParams.data.bytes, &kParams.data.size)) { |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 304 | goto Error; |
| 305 | } |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 306 | |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 307 | kParams.dmux = WebPDemux(&kParams.data); |
| 308 | if (kParams.dmux == NULL) { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 309 | fprintf(stderr, "Could not create demuxing object!\n"); |
| 310 | goto Error; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Urvang Joshi | a00a3da | 2012-10-31 17:49:15 -0700 | [diff] [blame] | 313 | if (WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & FRAGMENTS_FLAG) { |
| 314 | fprintf(stderr, "Image fragments are not supported for now!\n"); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 315 | goto Error; |
| 316 | } |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 317 | kParams.canvas_width = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_WIDTH); |
| 318 | kParams.canvas_height = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_HEIGHT); |
| 319 | if (kParams.print_info) { |
| 320 | printf("Canvas: %d x %d\n", kParams.canvas_width, kParams.canvas_height); |
| 321 | } |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 322 | |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 323 | if (!WebPDemuxGetFrame(kParams.dmux, 1, &kParams.frameiter)) goto Error; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 324 | |
Urvang Joshi | a077072 | 2012-10-30 14:54:46 -0700 | [diff] [blame] | 325 | kParams.has_animation = (kParams.frameiter.num_frames > 1); |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 326 | kParams.loop_count = (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT); |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 327 | kParams.bg_color = WebPDemuxGetI(kParams.dmux, WEBP_FF_BACKGROUND_COLOR); |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 328 | printf("VP8X: Found %d images in file (loop count = %d)\n", |
Urvang Joshi | a077072 | 2012-10-30 14:54:46 -0700 | [diff] [blame] | 329 | kParams.frameiter.num_frames, kParams.loop_count); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 330 | |
| 331 | // Decode first frame |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 332 | if (!Decode()) goto Error; |
| 333 | |
| 334 | // Position iterator to last frame. Next call to HandleDisplay will wrap over. |
| 335 | // We take this into account by bumping up loop_count. |
| 336 | WebPDemuxGetFrame(kParams.dmux, 0, &kParams.frameiter); |
| 337 | if (kParams.loop_count) ++kParams.loop_count; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 338 | |
| 339 | // Start display (and timer) |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 340 | glutInit(&argc, argv); |
James Zern | 880fd98 | 2012-05-25 14:53:46 -0700 | [diff] [blame] | 341 | #ifdef FREEGLUT |
| 342 | glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION); |
| 343 | #endif |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 344 | StartDisplay(); |
| 345 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 346 | if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0); |
| 347 | glutMainLoop(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 348 | |
| 349 | // Should only be reached when using FREEGLUT: |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 350 | ClearParams(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 351 | return 0; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 352 | |
| 353 | Error: |
| 354 | ClearParams(); |
| 355 | return -1; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | //------------------------------------------------------------------------------ |