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