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