• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 //  Utility functions used by the image decoders.
11 //
12 
13 #include "./imageio_util.h"
14 
15 #if defined(_WIN32)
16 #include <fcntl.h>   // for _O_BINARY
17 #include <io.h>      // for _setmode()
18 #endif
19 #include <stdlib.h>
20 #include <string.h>
21 #include "../examples/unicode.h"
22 
23 // -----------------------------------------------------------------------------
24 // File I/O
25 
ImgIoUtilSetBinaryMode(FILE * file)26 FILE* ImgIoUtilSetBinaryMode(FILE* file) {
27 #if defined(_WIN32)
28   if (_setmode(_fileno(file), _O_BINARY) == -1) {
29     fprintf(stderr, "Failed to reopen file in O_BINARY mode.\n");
30     return NULL;
31   }
32 #endif
33   return file;
34 }
35 
ImgIoUtilReadFromStdin(const uint8_t ** data,size_t * data_size)36 int ImgIoUtilReadFromStdin(const uint8_t** data, size_t* data_size) {
37   static const size_t kBlockSize = 16384;  // default initial size
38   size_t max_size = 0;
39   size_t size = 0;
40   uint8_t* input = NULL;
41 
42   if (data == NULL || data_size == NULL) return 0;
43   *data = NULL;
44   *data_size = 0;
45 
46   if (!ImgIoUtilSetBinaryMode(stdin)) return 0;
47 
48   while (!feof(stdin)) {
49     // We double the buffer size each time and read as much as possible.
50     const size_t extra_size = (max_size == 0) ? kBlockSize : max_size;
51     // we allocate one extra byte for the \0 terminator
52     void* const new_data = realloc(input, max_size + extra_size + 1);
53     if (new_data == NULL) goto Error;
54     input = (uint8_t*)new_data;
55     max_size += extra_size;
56     size += fread(input + size, 1, extra_size, stdin);
57     if (size < max_size) break;
58   }
59   if (ferror(stdin)) goto Error;
60   if (input != NULL) input[size] = '\0';  // convenient 0-terminator
61   *data = input;
62   *data_size = size;
63   return 1;
64 
65  Error:
66   free(input);
67   fprintf(stderr, "Could not read from stdin\n");
68   return 0;
69 }
70 
ImgIoUtilReadFile(const char * const file_name,const uint8_t ** data,size_t * data_size)71 int ImgIoUtilReadFile(const char* const file_name,
72                       const uint8_t** data, size_t* data_size) {
73   int ok;
74   uint8_t* file_data;
75   size_t file_size;
76   FILE* in;
77   const int from_stdin = (file_name == NULL) || !WSTRCMP(file_name, "-");
78 
79   if (from_stdin) return ImgIoUtilReadFromStdin(data, data_size);
80 
81   if (data == NULL || data_size == NULL) return 0;
82   *data = NULL;
83   *data_size = 0;
84 
85   in = WFOPEN(file_name, "rb");
86   if (in == NULL) {
87     WFPRINTF(stderr, "cannot open input file '%s'\n", (const W_CHAR*)file_name);
88     return 0;
89   }
90   fseek(in, 0, SEEK_END);
91   file_size = ftell(in);
92   if (file_size == (size_t)-1) {
93     fclose(in);
94     WFPRINTF(stderr, "error getting size of '%s'\n", (const W_CHAR*)file_name);
95     return 0;
96   }
97   fseek(in, 0, SEEK_SET);
98   // we allocate one extra byte for the \0 terminator
99   file_data = (uint8_t*)WebPMalloc(file_size + 1);
100   if (file_data == NULL) {
101     fclose(in);
102     WFPRINTF(stderr, "memory allocation failure when reading file %s\n",
103              (const W_CHAR*)file_name);
104     return 0;
105   }
106   ok = (fread(file_data, file_size, 1, in) == 1);
107   fclose(in);
108 
109   if (!ok) {
110     WFPRINTF(stderr, "Could not read %d bytes of data from file %s\n",
111              (int)file_size, (const W_CHAR*)file_name);
112     WebPFree(file_data);
113     return 0;
114   }
115   file_data[file_size] = '\0';  // convenient 0-terminator
116   *data = file_data;
117   *data_size = file_size;
118   return 1;
119 }
120 
121 // -----------------------------------------------------------------------------
122 
ImgIoUtilWriteFile(const char * const file_name,const uint8_t * data,size_t data_size)123 int ImgIoUtilWriteFile(const char* const file_name,
124                        const uint8_t* data, size_t data_size) {
125   int ok;
126   FILE* out;
127   const int to_stdout = (file_name == NULL) || !WSTRCMP(file_name, "-");
128 
129   if (data == NULL) {
130     return 0;
131   }
132   out = to_stdout ? ImgIoUtilSetBinaryMode(stdout) : WFOPEN(file_name, "wb");
133   if (out == NULL) {
134     WFPRINTF(stderr, "Error! Cannot open output file '%s'\n",
135              (const W_CHAR*)file_name);
136     return 0;
137   }
138   ok = (fwrite(data, data_size, 1, out) == 1);
139   if (out != stdout) fclose(out);
140   return ok;
141 }
142 
143 // -----------------------------------------------------------------------------
144 
ImgIoUtilCopyPlane(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,int width,int height)145 void ImgIoUtilCopyPlane(const uint8_t* src, int src_stride,
146                         uint8_t* dst, int dst_stride, int width, int height) {
147   while (height-- > 0) {
148     memcpy(dst, src, width * sizeof(*dst));
149     src += src_stride;
150     dst += dst_stride;
151   }
152 }
153 
154 // -----------------------------------------------------------------------------
155 
ImgIoUtilCheckSizeArgumentsOverflow(uint64_t stride,size_t height)156 int ImgIoUtilCheckSizeArgumentsOverflow(uint64_t stride, size_t height) {
157   const uint64_t total_size = stride * height;
158   int ok = (total_size == (size_t)total_size);
159   // check that 'stride' is representable as int:
160   ok = ok && ((uint64_t)(int)stride == stride);
161 #if defined(WEBP_MAX_IMAGE_SIZE)
162   ok = ok && (total_size <= (uint64_t)WEBP_MAX_IMAGE_SIZE);
163 #endif
164   return ok;
165 }
166 
167 // -----------------------------------------------------------------------------
168