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 | // |
James Zern | d640614 | 2013-06-06 23:05:58 -0700 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the COPYING file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 8 | // ----------------------------------------------------------------------------- |
| 9 | // |
Pascal Massimino | a4e1cdb | 2013-04-01 23:10:35 +0000 | [diff] [blame] | 10 | // Simple OpenGL-based WebP file viewer. |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 11 | // |
| 12 | // Author: Skal (pascal.massimino@gmail.com) |
James Zern | 0e513f7 | 2013-05-01 14:47:56 -0700 | [diff] [blame] | 13 | #ifdef HAVE_CONFIG_H |
| 14 | #include "config.h" |
| 15 | #endif |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
James Zern | 0e513f7 | 2013-05-01 14:47:56 -0700 | [diff] [blame] | 21 | #if defined(HAVE_GLUT_GLUT_H) |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 22 | #include <GLUT/glut.h> |
| 23 | #else |
| 24 | #include <GL/glut.h> |
| 25 | #ifdef FREEGLUT |
| 26 | #include <GL/freeglut.h> |
| 27 | #endif |
| 28 | #endif |
| 29 | |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 30 | #ifdef WEBP_HAVE_QCMS |
| 31 | #include <qcms.h> |
| 32 | #endif |
| 33 | |
| 34 | #include "webp/decode.h" |
| 35 | #include "webp/demux.h" |
| 36 | |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 37 | #include "./example_util.h" |
| 38 | |
James Zern | 9117954 | 2011-12-16 19:30:33 -0800 | [diff] [blame] | 39 | #ifdef _MSC_VER |
| 40 | #define snprintf _snprintf |
| 41 | #endif |
| 42 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 43 | static void Help(void); |
| 44 | |
| 45 | // Unfortunate global variables. Gathered into a struct for comfort. |
| 46 | static struct { |
| 47 | int has_animation; |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 48 | int has_color_profile; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 49 | int done; |
| 50 | int decoding_error; |
| 51 | int print_info; |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 52 | int use_color_profile; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 53 | |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 54 | int canvas_width, canvas_height; |
Urvang Joshi | 6808e69 | 2012-07-06 11:35:36 +0530 | [diff] [blame] | 55 | int loop_count; |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 56 | uint32_t bg_color; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 57 | |
| 58 | const char* file_name; |
| 59 | WebPData data; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 60 | WebPDecoderConfig* config; |
| 61 | const WebPDecBuffer* pic; |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 62 | WebPDemuxer* dmux; |
| 63 | WebPIterator frameiter; |
James Zern | 04c7a2e | 2013-03-23 12:45:11 -0700 | [diff] [blame] | 64 | struct { |
| 65 | int width, height; |
| 66 | int x_offset, y_offset; |
| 67 | enum WebPMuxAnimDispose dispose_method; |
| 68 | } prev_frame; |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 69 | WebPChunkIterator iccp; |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 70 | } kParams; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 71 | |
| 72 | static void ClearPreviousPic(void) { |
| 73 | WebPFreeDecBuffer((WebPDecBuffer*)kParams.pic); |
| 74 | kParams.pic = NULL; |
| 75 | } |
| 76 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 77 | static void ClearParams(void) { |
| 78 | ClearPreviousPic(); |
Urvang Joshi | f3bab8e | 2012-06-07 17:19:02 +0530 | [diff] [blame] | 79 | WebPDataClear(&kParams.data); |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 80 | WebPDemuxReleaseIterator(&kParams.frameiter); |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 81 | WebPDemuxReleaseChunkIterator(&kParams.iccp); |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 82 | WebPDemuxDelete(kParams.dmux); |
| 83 | kParams.dmux = NULL; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 84 | } |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 85 | |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 86 | // ----------------------------------------------------------------------------- |
| 87 | // Color profile handling |
| 88 | static int ApplyColorProfile(const WebPData* const profile, |
| 89 | WebPDecBuffer* const rgba) { |
| 90 | #ifdef WEBP_HAVE_QCMS |
| 91 | int i, ok = 0; |
| 92 | uint8_t* line; |
| 93 | uint8_t major_revision; |
| 94 | qcms_profile* input_profile = NULL; |
| 95 | qcms_profile* output_profile = NULL; |
| 96 | qcms_transform* transform = NULL; |
| 97 | const qcms_data_type input_type = QCMS_DATA_RGBA_8; |
| 98 | const qcms_data_type output_type = QCMS_DATA_RGBA_8; |
| 99 | const qcms_intent intent = QCMS_INTENT_DEFAULT; |
| 100 | |
| 101 | if (profile == NULL || rgba == NULL) return 0; |
| 102 | if (profile->bytes == NULL || profile->size < 10) return 1; |
| 103 | major_revision = profile->bytes[8]; |
| 104 | |
| 105 | qcms_enable_iccv4(); |
| 106 | input_profile = qcms_profile_from_memory(profile->bytes, profile->size); |
| 107 | // qcms_profile_is_bogus() is broken with ICCv4. |
| 108 | if (input_profile == NULL || |
| 109 | (major_revision < 4 && qcms_profile_is_bogus(input_profile))) { |
| 110 | fprintf(stderr, "Color profile is bogus!\n"); |
| 111 | goto Error; |
| 112 | } |
| 113 | |
| 114 | output_profile = qcms_profile_sRGB(); |
| 115 | if (output_profile == NULL) { |
| 116 | fprintf(stderr, "Error creating output color profile!\n"); |
| 117 | goto Error; |
| 118 | } |
| 119 | |
| 120 | qcms_profile_precache_output_transform(output_profile); |
| 121 | transform = qcms_transform_create(input_profile, input_type, |
| 122 | output_profile, output_type, |
| 123 | intent); |
| 124 | if (transform == NULL) { |
| 125 | fprintf(stderr, "Error creating color transform!\n"); |
| 126 | goto Error; |
| 127 | } |
| 128 | |
| 129 | line = rgba->u.RGBA.rgba; |
| 130 | for (i = 0; i < rgba->height; ++i, line += rgba->u.RGBA.stride) { |
| 131 | qcms_transform_data(transform, line, line, rgba->width); |
| 132 | } |
| 133 | ok = 1; |
| 134 | |
| 135 | Error: |
| 136 | if (input_profile != NULL) qcms_profile_release(input_profile); |
| 137 | if (output_profile != NULL) qcms_profile_release(output_profile); |
| 138 | if (transform != NULL) qcms_transform_release(transform); |
| 139 | return ok; |
| 140 | #else |
| 141 | (void)profile; |
| 142 | (void)rgba; |
| 143 | return 1; |
| 144 | #endif // WEBP_HAVE_QCMS |
| 145 | } |
| 146 | |
| 147 | //------------------------------------------------------------------------------ |
| 148 | // File decoding |
| 149 | |
| 150 | static int Decode(void) { // Fills kParams.frameiter |
| 151 | const WebPIterator* const iter = &kParams.frameiter; |
| 152 | WebPDecoderConfig* const config = kParams.config; |
| 153 | WebPDecBuffer* const output_buffer = &config->output; |
| 154 | int ok = 0; |
| 155 | |
| 156 | ClearPreviousPic(); |
| 157 | output_buffer->colorspace = MODE_RGBA; |
| 158 | ok = (WebPDecode(iter->fragment.bytes, iter->fragment.size, |
| 159 | config) == VP8_STATUS_OK); |
| 160 | if (!ok) { |
| 161 | fprintf(stderr, "Decoding of frame #%d failed!\n", iter->frame_num); |
| 162 | } else { |
| 163 | kParams.pic = output_buffer; |
| 164 | if (kParams.use_color_profile) { |
| 165 | ok = ApplyColorProfile(&kParams.iccp.chunk, output_buffer); |
| 166 | if (!ok) { |
| 167 | fprintf(stderr, "Applying color profile to frame #%d failed!\n", |
| 168 | iter->frame_num); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | return ok; |
| 173 | } |
| 174 | |
| 175 | static void decode_callback(int what) { |
| 176 | if (what == 0 && !kParams.done) { |
| 177 | int duration = 0; |
| 178 | if (kParams.dmux != NULL) { |
| 179 | WebPIterator* const iter = &kParams.frameiter; |
| 180 | if (!WebPDemuxNextFrame(iter)) { |
| 181 | WebPDemuxReleaseIterator(iter); |
| 182 | if (WebPDemuxGetFrame(kParams.dmux, 1, iter)) { |
| 183 | --kParams.loop_count; |
| 184 | kParams.done = (kParams.loop_count == 0); |
| 185 | } else { |
| 186 | kParams.decoding_error = 1; |
| 187 | kParams.done = 1; |
| 188 | return; |
| 189 | } |
| 190 | } |
| 191 | duration = iter->duration; |
| 192 | } |
| 193 | if (!Decode()) { |
| 194 | kParams.decoding_error = 1; |
| 195 | kParams.done = 1; |
| 196 | } else { |
| 197 | glutPostRedisplay(); |
| 198 | glutTimerFunc(duration, decode_callback, what); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 203 | //------------------------------------------------------------------------------ |
| 204 | // Callbacks |
| 205 | |
| 206 | static void HandleKey(unsigned char key, int pos_x, int pos_y) { |
| 207 | (void)pos_x; |
| 208 | (void)pos_y; |
| 209 | if (key == 'q' || key == 'Q' || key == 27 /* Esc */) { |
| 210 | #ifdef FREEGLUT |
| 211 | glutLeaveMainLoop(); |
| 212 | #else |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 213 | ClearParams(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 214 | exit(0); |
| 215 | #endif |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 216 | } else if (key == 'c') { |
| 217 | if (kParams.has_color_profile && !kParams.decoding_error) { |
| 218 | kParams.use_color_profile = 1 - kParams.use_color_profile; |
| 219 | |
| 220 | if (kParams.has_animation) { |
| 221 | // Restart the completed animation to pickup the color profile change. |
| 222 | if (kParams.done && kParams.loop_count == 0) { |
| 223 | kParams.loop_count = |
| 224 | (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT) + 1; |
| 225 | kParams.done = 0; |
| 226 | // Start the decode loop immediately. |
| 227 | glutTimerFunc(0, decode_callback, 0); |
| 228 | } |
| 229 | } else { |
| 230 | Decode(); |
| 231 | glutPostRedisplay(); |
| 232 | } |
| 233 | } |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 234 | } else if (key == 'i') { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 235 | kParams.print_info = 1 - kParams.print_info; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 236 | glutPostRedisplay(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | static void HandleReshape(int width, int height) { |
| 241 | // TODO(skal): proper handling of resize, esp. for large pictures. |
| 242 | // + key control of the zoom. |
| 243 | glViewport(0, 0, width, height); |
| 244 | glMatrixMode(GL_PROJECTION); |
| 245 | glLoadIdentity(); |
| 246 | glMatrixMode(GL_MODELVIEW); |
| 247 | glLoadIdentity(); |
| 248 | } |
| 249 | |
| 250 | static void PrintString(const char* const text) { |
| 251 | void* const font = GLUT_BITMAP_9_BY_15; |
| 252 | int i; |
| 253 | for (i = 0; text[i]; ++i) { |
| 254 | glutBitmapCharacter(font, text[i]); |
| 255 | } |
| 256 | } |
| 257 | |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 258 | static float GetColorf(uint32_t color, int shift) { |
| 259 | return (color >> shift) / 255.f; |
| 260 | } |
| 261 | |
James Zern | a73b897 | 2012-07-09 23:01:52 -0700 | [diff] [blame] | 262 | static void DrawCheckerBoard(void) { |
| 263 | const int square_size = 8; // must be a power of 2 |
| 264 | int x, y; |
| 265 | GLint viewport[4]; // x, y, width, height |
| 266 | |
| 267 | glPushMatrix(); |
| 268 | |
| 269 | glGetIntegerv(GL_VIEWPORT, viewport); |
| 270 | // shift to integer coordinates with (0,0) being top-left. |
| 271 | glOrtho(0, viewport[2], viewport[3], 0, -1, 1); |
| 272 | for (y = 0; y < viewport[3]; y += square_size) { |
| 273 | for (x = 0; x < viewport[2]; x += square_size) { |
| 274 | const GLubyte color = 128 + 64 * (!((x + y) & square_size)); |
| 275 | glColor3ub(color, color, color); |
| 276 | glRecti(x, y, x + square_size, y + square_size); |
| 277 | } |
| 278 | } |
| 279 | glPopMatrix(); |
| 280 | } |
| 281 | |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 282 | static void HandleDisplay(void) { |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 283 | const WebPDecBuffer* const pic = kParams.pic; |
| 284 | const WebPIterator* const iter = &kParams.frameiter; |
skal | 41a6ced | 2012-11-15 09:35:01 +0100 | [diff] [blame] | 285 | GLfloat xoff, yoff; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 286 | if (pic == NULL) return; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 287 | glPushMatrix(); |
| 288 | glPixelZoom(1, -1); |
skal | 41a6ced | 2012-11-15 09:35:01 +0100 | [diff] [blame] | 289 | xoff = (GLfloat)(2. * iter->x_offset / kParams.canvas_width); |
| 290 | yoff = (GLfloat)(2. * iter->y_offset / kParams.canvas_height); |
James Zern | 013023e | 2013-03-15 12:17:45 -0700 | [diff] [blame] | 291 | glRasterPos2f(-1.f + xoff, 1.f - yoff); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 292 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 293 | glPixelStorei(GL_UNPACK_ROW_LENGTH, pic->u.RGBA.stride / 4); |
James Zern | 04c7a2e | 2013-03-23 12:45:11 -0700 | [diff] [blame] | 294 | |
| 295 | if (kParams.prev_frame.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND) { |
| 296 | // TODO(later): these offsets and those above should factor in window size. |
| 297 | // they will be incorrect if the window is resized. |
| 298 | // glScissor() takes window coordinates (0,0 at bottom left). |
| 299 | const int window_x = kParams.prev_frame.x_offset; |
| 300 | const int window_y = kParams.canvas_height - |
| 301 | kParams.prev_frame.y_offset - |
| 302 | kParams.prev_frame.height; |
| 303 | glEnable(GL_SCISSOR_TEST); |
| 304 | // Only updated the requested area, not the whole canvas. |
| 305 | glScissor(window_x, window_y, |
| 306 | kParams.prev_frame.width, kParams.prev_frame.height); |
| 307 | |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 308 | glClear(GL_COLOR_BUFFER_BIT); // use clear color |
James Zern | 04c7a2e | 2013-03-23 12:45:11 -0700 | [diff] [blame] | 309 | DrawCheckerBoard(); |
| 310 | |
| 311 | glDisable(GL_SCISSOR_TEST); |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 312 | } |
James Zern | 04c7a2e | 2013-03-23 12:45:11 -0700 | [diff] [blame] | 313 | kParams.prev_frame.width = iter->width; |
| 314 | kParams.prev_frame.height = iter->height; |
| 315 | kParams.prev_frame.x_offset = iter->x_offset; |
| 316 | kParams.prev_frame.y_offset = iter->y_offset; |
| 317 | kParams.prev_frame.dispose_method = iter->dispose_method; |
| 318 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 319 | glDrawPixels(pic->width, pic->height, |
| 320 | GL_RGBA, GL_UNSIGNED_BYTE, |
| 321 | (GLvoid*)pic->u.RGBA.rgba); |
| 322 | if (kParams.print_info) { |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 323 | char tmp[32]; |
| 324 | |
James Zern | 013023e | 2013-03-15 12:17:45 -0700 | [diff] [blame] | 325 | glColor4f(0.90f, 0.0f, 0.90f, 1.0f); |
James Zern | 9117954 | 2011-12-16 19:30:33 -0800 | [diff] [blame] | 326 | glRasterPos2f(-0.95f, 0.90f); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 327 | PrintString(kParams.file_name); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 328 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 329 | snprintf(tmp, sizeof(tmp), "Dimension:%d x %d", pic->width, pic->height); |
James Zern | 013023e | 2013-03-15 12:17:45 -0700 | [diff] [blame] | 330 | glColor4f(0.90f, 0.0f, 0.90f, 1.0f); |
James Zern | 9117954 | 2011-12-16 19:30:33 -0800 | [diff] [blame] | 331 | glRasterPos2f(-0.95f, 0.80f); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 332 | PrintString(tmp); |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 333 | if (iter->x_offset != 0 || iter->y_offset != 0) { |
| 334 | snprintf(tmp, sizeof(tmp), " (offset:%d,%d)", |
| 335 | iter->x_offset, iter->y_offset); |
| 336 | glRasterPos2f(-0.95f, 0.70f); |
| 337 | PrintString(tmp); |
| 338 | } |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 339 | } |
James Zern | a73b897 | 2012-07-09 23:01:52 -0700 | [diff] [blame] | 340 | glPopMatrix(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 341 | glFlush(); |
| 342 | } |
| 343 | |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 344 | static void StartDisplay(void) { |
| 345 | const int width = kParams.canvas_width; |
| 346 | const int height = kParams.canvas_height; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 347 | glutInitDisplayMode(GLUT_RGBA); |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 348 | glutInitWindowSize(width, height); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 349 | glutCreateWindow("WebP viewer"); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 350 | glutDisplayFunc(HandleDisplay); |
| 351 | glutIdleFunc(NULL); |
| 352 | glutKeyboardFunc(HandleKey); |
Urvang Joshi | 0822010 | 2012-06-20 11:18:35 -0700 | [diff] [blame] | 353 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 354 | glEnable(GL_BLEND); |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 355 | glClearColor(GetColorf(kParams.bg_color, 0), |
| 356 | GetColorf(kParams.bg_color, 8), |
| 357 | GetColorf(kParams.bg_color, 16), |
| 358 | GetColorf(kParams.bg_color, 24)); |
| 359 | HandleReshape(width, height); |
| 360 | glClear(GL_COLOR_BUFFER_BIT); |
| 361 | DrawCheckerBoard(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | //------------------------------------------------------------------------------ |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 365 | // Main |
| 366 | |
| 367 | static void Help(void) { |
| 368 | printf("Usage: vwebp in_file [options]\n\n" |
| 369 | "Decodes the WebP image file and visualize it using OpenGL\n" |
| 370 | "Options are:\n" |
| 371 | " -version .... print version number and exit.\n" |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 372 | " -noicc ....... don't use the icc profile if present.\n" |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 373 | " -nofancy ..... don't use the fancy YUV420 upscaler.\n" |
| 374 | " -nofilter .... disable in-loop filtering.\n" |
James Zern | 03cc23d | 2013-03-07 19:10:59 -0800 | [diff] [blame] | 375 | " -mt .......... use multi-threading.\n" |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 376 | " -info ........ print info.\n" |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 377 | " -h ....... this help message.\n" |
James Zern | 03cc23d | 2013-03-07 19:10:59 -0800 | [diff] [blame] | 378 | "\n" |
| 379 | "Keyboard shortcuts:\n" |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 380 | " 'c' ................ toggle use of color profile.\n" |
James Zern | 03cc23d | 2013-03-07 19:10:59 -0800 | [diff] [blame] | 381 | " 'i' ................ overlay file information.\n" |
| 382 | " 'q' / 'Q' / ESC .... quit.\n" |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 383 | ); |
| 384 | } |
| 385 | |
| 386 | int main(int argc, char *argv[]) { |
| 387 | WebPDecoderConfig config; |
| 388 | int c; |
| 389 | |
| 390 | if (!WebPInitDecoderConfig(&config)) { |
| 391 | fprintf(stderr, "Library version mismatch!\n"); |
| 392 | return -1; |
| 393 | } |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 394 | kParams.config = &config; |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 395 | kParams.use_color_profile = 1; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 396 | |
| 397 | for (c = 1; c < argc; ++c) { |
| 398 | if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) { |
| 399 | Help(); |
| 400 | return 0; |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 401 | } else if (!strcmp(argv[c], "-noicc")) { |
| 402 | kParams.use_color_profile = 0; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 403 | } else if (!strcmp(argv[c], "-nofancy")) { |
| 404 | config.options.no_fancy_upsampling = 1; |
| 405 | } else if (!strcmp(argv[c], "-nofilter")) { |
| 406 | config.options.bypass_filtering = 1; |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 407 | } else if (!strcmp(argv[c], "-info")) { |
| 408 | kParams.print_info = 1; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 409 | } else if (!strcmp(argv[c], "-version")) { |
Urvang Joshi | a5042a3 | 2013-02-26 14:22:06 -0800 | [diff] [blame] | 410 | const int dec_version = WebPGetDecoderVersion(); |
| 411 | const int dmux_version = WebPGetDemuxVersion(); |
| 412 | printf("WebP Decoder version: %d.%d.%d\nWebP Demux version: %d.%d.%d\n", |
| 413 | (dec_version >> 16) & 0xff, (dec_version >> 8) & 0xff, |
| 414 | dec_version & 0xff, (dmux_version >> 16) & 0xff, |
| 415 | (dmux_version >> 8) & 0xff, dmux_version & 0xff); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 416 | return 0; |
| 417 | } else if (!strcmp(argv[c], "-mt")) { |
| 418 | config.options.use_threads = 1; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 419 | } else if (argv[c][0] == '-') { |
| 420 | printf("Unknown option '%s'\n", argv[c]); |
| 421 | Help(); |
| 422 | return -1; |
| 423 | } else { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 424 | kParams.file_name = argv[c]; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 428 | if (kParams.file_name == NULL) { |
| 429 | printf("missing input file!!\n"); |
| 430 | Help(); |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | if (!ExUtilReadFile(kParams.file_name, |
Urvang Joshi | a077072 | 2012-10-30 14:54:46 -0700 | [diff] [blame] | 435 | &kParams.data.bytes, &kParams.data.size)) { |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 436 | goto Error; |
| 437 | } |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 438 | |
Pascal Massimino | 830f72b | 2013-06-10 05:46:22 -0700 | [diff] [blame] | 439 | if (!WebPGetInfo(kParams.data.bytes, kParams.data.size, NULL, NULL)) { |
| 440 | fprintf(stderr, "Input file doesn't appear to be WebP format.\n"); |
| 441 | goto Error; |
| 442 | } |
| 443 | |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 444 | kParams.dmux = WebPDemux(&kParams.data); |
| 445 | if (kParams.dmux == NULL) { |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 446 | fprintf(stderr, "Could not create demuxing object!\n"); |
| 447 | goto Error; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Urvang Joshi | a00a3da | 2012-10-31 17:49:15 -0700 | [diff] [blame] | 450 | if (WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & FRAGMENTS_FLAG) { |
| 451 | fprintf(stderr, "Image fragments are not supported for now!\n"); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 452 | goto Error; |
| 453 | } |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 454 | kParams.canvas_width = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_WIDTH); |
| 455 | kParams.canvas_height = WebPDemuxGetI(kParams.dmux, WEBP_FF_CANVAS_HEIGHT); |
| 456 | if (kParams.print_info) { |
| 457 | printf("Canvas: %d x %d\n", kParams.canvas_width, kParams.canvas_height); |
| 458 | } |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 459 | |
James Zern | 04c7a2e | 2013-03-23 12:45:11 -0700 | [diff] [blame] | 460 | kParams.prev_frame.width = kParams.canvas_width; |
| 461 | kParams.prev_frame.height = kParams.canvas_height; |
| 462 | kParams.prev_frame.x_offset = kParams.prev_frame.y_offset = 0; |
| 463 | kParams.prev_frame.dispose_method = WEBP_MUX_DISPOSE_BACKGROUND; |
| 464 | |
James Zern | ddfee5d | 2013-03-07 19:31:27 -0800 | [diff] [blame] | 465 | memset(&kParams.iccp, 0, sizeof(kParams.iccp)); |
| 466 | kParams.has_color_profile = |
| 467 | !!(WebPDemuxGetI(kParams.dmux, WEBP_FF_FORMAT_FLAGS) & ICCP_FLAG); |
| 468 | if (kParams.has_color_profile) { |
| 469 | #ifdef WEBP_HAVE_QCMS |
| 470 | if (!WebPDemuxGetChunk(kParams.dmux, "ICCP", 1, &kParams.iccp)) goto Error; |
| 471 | printf("VP8X: Found color profile\n"); |
| 472 | #else |
| 473 | fprintf(stderr, "Warning: color profile present, but qcms is unavailable!\n" |
| 474 | "Build libqcms from Mozilla or Chromium and define WEBP_HAVE_QCMS " |
| 475 | "before building.\n"); |
| 476 | #endif |
| 477 | } |
| 478 | |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 479 | if (!WebPDemuxGetFrame(kParams.dmux, 1, &kParams.frameiter)) goto Error; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 480 | |
Urvang Joshi | a077072 | 2012-10-30 14:54:46 -0700 | [diff] [blame] | 481 | kParams.has_animation = (kParams.frameiter.num_frames > 1); |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 482 | kParams.loop_count = (int)WebPDemuxGetI(kParams.dmux, WEBP_FF_LOOP_COUNT); |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 483 | kParams.bg_color = WebPDemuxGetI(kParams.dmux, WEBP_FF_BACKGROUND_COLOR); |
James Zern | d7a5ac8 | 2012-09-26 23:44:59 -0700 | [diff] [blame] | 484 | printf("VP8X: Found %d images in file (loop count = %d)\n", |
Urvang Joshi | a077072 | 2012-10-30 14:54:46 -0700 | [diff] [blame] | 485 | kParams.frameiter.num_frames, kParams.loop_count); |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 486 | |
| 487 | // Decode first frame |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 488 | if (!Decode()) goto Error; |
| 489 | |
| 490 | // Position iterator to last frame. Next call to HandleDisplay will wrap over. |
| 491 | // We take this into account by bumping up loop_count. |
| 492 | WebPDemuxGetFrame(kParams.dmux, 0, &kParams.frameiter); |
| 493 | if (kParams.loop_count) ++kParams.loop_count; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 494 | |
| 495 | // Start display (and timer) |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 496 | glutInit(&argc, argv); |
James Zern | 880fd98 | 2012-05-25 14:53:46 -0700 | [diff] [blame] | 497 | #ifdef FREEGLUT |
| 498 | glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION); |
| 499 | #endif |
skal | 68f282f | 2012-11-15 09:15:04 +0100 | [diff] [blame] | 500 | StartDisplay(); |
| 501 | |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 502 | if (kParams.has_animation) glutTimerFunc(0, decode_callback, 0); |
| 503 | glutMainLoop(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 504 | |
| 505 | // Should only be reached when using FREEGLUT: |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 506 | ClearParams(); |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 507 | return 0; |
Pascal Massimino | 861a5b7 | 2012-05-09 00:41:12 -0700 | [diff] [blame] | 508 | |
| 509 | Error: |
| 510 | ClearParams(); |
| 511 | return -1; |
Pascal Massimino | 7937b40 | 2011-12-13 14:02:04 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | //------------------------------------------------------------------------------ |