| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include "./imageio_util.h" |
| 14 |
|
| 15 |
#if defined(_WIN32) |
| 16 |
#include <fcntl.h> |
| 17 |
#include <io.h> |
| 18 |
#endif |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include "../examples/unicode.h" |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 36 |
int ImgIoUtilReadFromStdin(const uint8_t** data, size_t* data_size) { |
| 37 |
static const size_t kBlockSize = 16384; |
| 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 |
|
| 50 |
const size_t extra_size = (max_size == 0) ? kBlockSize : max_size; |
| 51 |
|
| 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'; |
| 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 |
|
| 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 |
fseek(in, 0, SEEK_SET); |
| 93 |
|
| 94 |
file_data = (uint8_t*)WebPMalloc(file_size + 1); |
| 95 |
if (file_data == NULL) { |
| 96 |
fclose(in); |
| 97 |
WFPRINTF(stderr, "memory allocation failure when reading file %s\n", |
| 98 |
(const W_CHAR*)file_name); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
ok = (fread(file_data, file_size, 1, in) == 1); |
| 102 |
fclose(in); |
| 103 |
|
| 104 |
if (!ok) { |
| 105 |
WFPRINTF(stderr, "Could not read %d bytes of data from file %s\n", |
| 106 |
(int)file_size, (const W_CHAR*)file_name); |
| 107 |
WebPFree(file_data); |
| 108 |
return 0; |
| 109 |
} |
| 110 |
file_data[file_size] = '\0'; |
| 111 |
*data = file_data; |
| 112 |
*data_size = file_size; |
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
int ImgIoUtilWriteFile(const char* const file_name, |
| 119 |
const uint8_t* data, size_t data_size) { |
| 120 |
int ok; |
| 121 |
FILE* out; |
| 122 |
const int to_stdout = (file_name == NULL) || !WSTRCMP(file_name, "-"); |
| 123 |
|
| 124 |
if (data == NULL) { |
| 125 |
return 0; |
| 126 |
} |
| 127 |
out = to_stdout ? ImgIoUtilSetBinaryMode(stdout) : WFOPEN(file_name, "wb"); |
| 128 |
if (out == NULL) { |
| 129 |
WFPRINTF(stderr, "Error! Cannot open output file '%s'\n", |
| 130 |
(const W_CHAR*)file_name); |
| 131 |
return 0; |
| 132 |
} |
| 133 |
ok = (fwrite(data, data_size, 1, out) == 1); |
| 134 |
if (out != stdout) fclose(out); |
| 135 |
return ok; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
void ImgIoUtilCopyPlane(const uint8_t* src, int src_stride, |
| 141 |
uint8_t* dst, int dst_stride, int width, int height) { |
| 142 |
while (height-- > 0) { |
| 143 |
memcpy(dst, src, width * sizeof(*dst)); |
| 144 |
src += src_stride; |
| 145 |
dst += dst_stride; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
int ImgIoUtilCheckSizeArgumentsOverflow(uint64_t stride, size_t height) { |
| 152 |
const uint64_t total_size = stride * height; |
| 153 |
int ok = (total_size == (size_t)total_size); |
| 154 |
|
| 155 |
ok = ok && ((uint64_t)(int)stride == stride); |
| 156 |
#if defined(WEBP_MAX_IMAGE_SIZE) |
| 157 |
ok = ok && (total_size <= (uint64_t)WEBP_MAX_IMAGE_SIZE); |
| 158 |
#endif |
| 159 |
return ok; |
| 160 |
} |
| 161 |
|
| 162 |
|