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