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