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 #include "../examples/unicode.h"
29 
30 #if defined(WEBP_HAVE_JUST_SDL_H)
31 #include <SDL.h>
32 #else
33 #include <SDL/SDL.h>
34 #endif
35 
ProcessEvents(void)36 static void ProcessEvents(void) {
37   int done = 0;
38   SDL_Event event;
39   while (!done && SDL_WaitEvent(&event)) {
40     switch (event.type) {
41       case SDL_KEYUP:
42         switch (event.key.keysym.sym) {
43           case SDLK_q: done = 1; break;
44           default: break;
45         }
46         break;
47       default: break;
48     }
49   }
50 }
51 
main(int argc,char * argv[])52 int main(int argc, char* argv[]) {
53   int c;
54   int ok = 0;
55 
56   INIT_WARGV(argc, argv);
57 
58   for (c = 1; c < argc; ++c) {
59     const char* file = NULL;
60     const uint8_t* webp = NULL;
61     size_t webp_size = 0;
62     if (!strcmp(argv[c], "-h")) {
63       printf("Usage: %s [-h] image.webp [more_files.webp...]\n", argv[0]);
64       FREE_WARGV_AND_RETURN(0);
65     } else {
66       file = (const char*)GET_WARGV(argv, c);
67     }
68     if (file == NULL) continue;
69     if (!ImgIoUtilReadFile(file, &webp, &webp_size)) {
70       WFPRINTF(stderr, "Error opening file: %s\n", (const W_CHAR*)file);
71       goto Error;
72     }
73     if (webp_size != (size_t)(int)webp_size) {
74       free((void*)webp);
75       fprintf(stderr, "File too large.\n");
76       goto Error;
77     }
78     ok = WebpToSDL((const char*)webp, (int)webp_size);
79     free((void*)webp);
80     if (!ok) {
81       WFPRINTF(stderr, "Error decoding file %s\n", (const W_CHAR*)file);
82       goto Error;
83     }
84     ProcessEvents();
85   }
86   ok = 1;
87 
88  Error:
89   SDL_Quit();
90   FREE_WARGV_AND_RETURN(ok ? 0 : 1);
91 }
92 
93 #else  // !WEBP_HAVE_SDL
94 
main(int argc,const char * argv[])95 int main(int argc, const char* argv[]) {
96   fprintf(stderr, "SDL support not enabled in %s.\n", argv[0]);
97   (void)argc;
98   return 0;
99 }
100 
101 #endif
102