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