1 // Copyright 2017 Google Inc. All Rights Reserved.
2 //
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.
8 // -----------------------------------------------------------------------------
9 //
10 // Simple SDL-based WebP file viewer.
11 // Does not support animation, just static images.
12 //
13 // Press 'q' to exit.
14 //
15 // Author: James Zern (jzern@google.com)
16 
17 #include <stdio.h>
18 
19 #ifdef HAVE_CONFIG_H
20 #include "webp/config.h"
21 #endif
22 
23 #if defined(WEBP_HAVE_SDL)
24 
25 #include "webp_to_sdl.h"
26 #include "webp/decode.h"
27 #include "imageio/imageio_util.h"
28 
29 #if defined(WEBP_HAVE_JUST_SDL_H)
30 #include <SDL.h>
31 #else
32 #include <SDL/SDL.h>
33 #endif
34 
ProcessEvents(void)35 static void ProcessEvents(void) {
36   int done = 0;
37   SDL_Event event;
38   while (!done && SDL_WaitEvent(&event)) {
39     switch (event.type) {
40       case SDL_KEYUP:
41         switch (event.key.keysym.sym) {
42           case SDLK_q: done = 1; break;
43           default: break;
44         }
45         break;
46       default: break;
47     }
48   }
49 }
50 
main(int argc,char * argv[])51 int main(int argc, char* argv[]) {
52   int c;
53   int ok = 0;
54   for (c = 1; c < argc; ++c) {
55     const char* file = NULL;
56     const uint8_t* webp = NULL;
57     size_t webp_size = 0;
58     if (!strcmp(argv[c], "-h")) {
59       printf("Usage: %s [-h] image.webp [more_files.webp...]\n", argv[0]);
60       return 0;
61     } else {
62       file = argv[c];
63     }
64     if (file == NULL) continue;
65     if (!ImgIoUtilReadFile(file, &webp, &webp_size)) {
66       fprintf(stderr, "Error opening file: %s\n", file);
67       goto Error;
68     }
69     if (webp_size != (size_t)(int)webp_size) {
70       fprintf(stderr, "File too large.\n");
71       goto Error;
72     }
73     ok = WebpToSDL((const char*)webp, (int)webp_size);
74     free((void*)webp);
75     if (!ok) {
76       fprintf(stderr, "Error decoding file %s\n", file);
77       goto Error;
78     }
79     ProcessEvents();
80   }
81   ok = 1;
82 
83  Error:
84   SDL_Quit();
85   return ok ? 0 : 1;
86 }
87 
88 #else  // !WEBP_HAVE_SDL
89 
main(int argc,const char * argv[])90 int main(int argc, const char *argv[]) {
91   fprintf(stderr, "SDL support not enabled in %s.\n", argv[0]);
92   (void)argc;
93   return 0;
94 }
95 
96 #endif
97