2016-07-21 03:25:00 +02:00
|
|
|
// Copyright 2016 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Utility functions used by the image decoders.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "./imageio_util.h"
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <fcntl.h> // for _O_BINARY
|
|
|
|
#include <io.h> // for _setmode()
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
libwebp: Unicode command tools on Windows
Define macros in examples/unicode.h to use Unicode argv
on Windows. Keep char everywhere on Unix since it handles
UTF-8 without any change.
Impact:
- All fopen () and SHCreateStreamOnFile(),
- All fprintf() printing file paths,
- All strcmp() used with "-",
- File path parsing,
- Gif reading.
Concerned executables from examples/ and extras/:
anim_diff, anim_dump, vwebp, vwebp_sdl,
cwebp, dwebp, gif2webp, img2webp,
webpmux, webpinfo, webp_quality, get_disto
When compiled on Windows with Unicode enabled, webpmux and
img2webp will not work when used with an argument file and
will print "Reading arguments from a file is a feature
unavailable with Unicode binaries."
BUG=webp:398
Change-Id: Ic55d222a3ce1a715f9c4cce57ecbe2705d5ce317
2018-10-16 11:03:18 +02:00
|
|
|
#include "../examples/unicode.h"
|
2016-07-21 03:25:00 +02:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// File I/O
|
|
|
|
|
2016-07-22 00:36:29 +02:00
|
|
|
FILE* ImgIoUtilSetBinaryMode(FILE* file) {
|
2016-07-21 03:25:00 +02:00
|
|
|
#if defined(_WIN32)
|
|
|
|
if (_setmode(_fileno(file), _O_BINARY) == -1) {
|
|
|
|
fprintf(stderr, "Failed to reopen file in O_BINARY mode.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2016-07-22 00:36:29 +02:00
|
|
|
int ImgIoUtilReadFromStdin(const uint8_t** data, size_t* data_size) {
|
2016-07-21 03:25:00 +02:00
|
|
|
static const size_t kBlockSize = 16384; // default initial size
|
|
|
|
size_t max_size = 0;
|
|
|
|
size_t size = 0;
|
|
|
|
uint8_t* input = NULL;
|
|
|
|
|
|
|
|
if (data == NULL || data_size == NULL) return 0;
|
|
|
|
*data = NULL;
|
|
|
|
*data_size = 0;
|
|
|
|
|
2016-07-22 00:36:29 +02:00
|
|
|
if (!ImgIoUtilSetBinaryMode(stdin)) return 0;
|
2016-07-21 03:25:00 +02:00
|
|
|
|
|
|
|
while (!feof(stdin)) {
|
|
|
|
// We double the buffer size each time and read as much as possible.
|
|
|
|
const size_t extra_size = (max_size == 0) ? kBlockSize : max_size;
|
2017-12-01 09:21:13 +01:00
|
|
|
// we allocate one extra byte for the \0 terminator
|
|
|
|
void* const new_data = realloc(input, max_size + extra_size + 1);
|
2016-07-21 03:25:00 +02:00
|
|
|
if (new_data == NULL) goto Error;
|
|
|
|
input = (uint8_t*)new_data;
|
|
|
|
max_size += extra_size;
|
|
|
|
size += fread(input + size, 1, extra_size, stdin);
|
|
|
|
if (size < max_size) break;
|
|
|
|
}
|
|
|
|
if (ferror(stdin)) goto Error;
|
2017-12-01 09:21:13 +01:00
|
|
|
if (input != NULL) input[size] = '\0'; // convenient 0-terminator
|
2016-07-21 03:25:00 +02:00
|
|
|
*data = input;
|
|
|
|
*data_size = size;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
Error:
|
|
|
|
free(input);
|
|
|
|
fprintf(stderr, "Could not read from stdin\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-22 00:36:29 +02:00
|
|
|
int ImgIoUtilReadFile(const char* const file_name,
|
|
|
|
const uint8_t** data, size_t* data_size) {
|
2016-07-21 03:25:00 +02:00
|
|
|
int ok;
|
2017-12-01 09:21:13 +01:00
|
|
|
uint8_t* file_data;
|
2016-07-21 03:25:00 +02:00
|
|
|
size_t file_size;
|
|
|
|
FILE* in;
|
libwebp: Unicode command tools on Windows
Define macros in examples/unicode.h to use Unicode argv
on Windows. Keep char everywhere on Unix since it handles
UTF-8 without any change.
Impact:
- All fopen () and SHCreateStreamOnFile(),
- All fprintf() printing file paths,
- All strcmp() used with "-",
- File path parsing,
- Gif reading.
Concerned executables from examples/ and extras/:
anim_diff, anim_dump, vwebp, vwebp_sdl,
cwebp, dwebp, gif2webp, img2webp,
webpmux, webpinfo, webp_quality, get_disto
When compiled on Windows with Unicode enabled, webpmux and
img2webp will not work when used with an argument file and
will print "Reading arguments from a file is a feature
unavailable with Unicode binaries."
BUG=webp:398
Change-Id: Ic55d222a3ce1a715f9c4cce57ecbe2705d5ce317
2018-10-16 11:03:18 +02:00
|
|
|
const int from_stdin = (file_name == NULL) || !WSTRCMP(file_name, "-");
|
2016-07-21 03:25:00 +02:00
|
|
|
|
2016-07-22 00:36:29 +02:00
|
|
|
if (from_stdin) return ImgIoUtilReadFromStdin(data, data_size);
|
2016-07-21 03:25:00 +02:00
|
|
|
|
|
|
|
if (data == NULL || data_size == NULL) return 0;
|
|
|
|
*data = NULL;
|
|
|
|
*data_size = 0;
|
|
|
|
|
libwebp: Unicode command tools on Windows
Define macros in examples/unicode.h to use Unicode argv
on Windows. Keep char everywhere on Unix since it handles
UTF-8 without any change.
Impact:
- All fopen () and SHCreateStreamOnFile(),
- All fprintf() printing file paths,
- All strcmp() used with "-",
- File path parsing,
- Gif reading.
Concerned executables from examples/ and extras/:
anim_diff, anim_dump, vwebp, vwebp_sdl,
cwebp, dwebp, gif2webp, img2webp,
webpmux, webpinfo, webp_quality, get_disto
When compiled on Windows with Unicode enabled, webpmux and
img2webp will not work when used with an argument file and
will print "Reading arguments from a file is a feature
unavailable with Unicode binaries."
BUG=webp:398
Change-Id: Ic55d222a3ce1a715f9c4cce57ecbe2705d5ce317
2018-10-16 11:03:18 +02:00
|
|
|
in = WFOPEN(file_name, "rb");
|
2016-07-21 03:25:00 +02:00
|
|
|
if (in == NULL) {
|
libwebp: Unicode command tools on Windows
Define macros in examples/unicode.h to use Unicode argv
on Windows. Keep char everywhere on Unix since it handles
UTF-8 without any change.
Impact:
- All fopen () and SHCreateStreamOnFile(),
- All fprintf() printing file paths,
- All strcmp() used with "-",
- File path parsing,
- Gif reading.
Concerned executables from examples/ and extras/:
anim_diff, anim_dump, vwebp, vwebp_sdl,
cwebp, dwebp, gif2webp, img2webp,
webpmux, webpinfo, webp_quality, get_disto
When compiled on Windows with Unicode enabled, webpmux and
img2webp will not work when used with an argument file and
will print "Reading arguments from a file is a feature
unavailable with Unicode binaries."
BUG=webp:398
Change-Id: Ic55d222a3ce1a715f9c4cce57ecbe2705d5ce317
2018-10-16 11:03:18 +02:00
|
|
|
WFPRINTF(stderr, "cannot open input file '%s'\n", (const W_CHAR*)file_name);
|
2016-07-21 03:25:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
fseek(in, 0, SEEK_END);
|
|
|
|
file_size = ftell(in);
|
|
|
|
fseek(in, 0, SEEK_SET);
|
2017-12-01 09:21:13 +01:00
|
|
|
// we allocate one extra byte for the \0 terminator
|
2020-10-16 15:37:26 +02:00
|
|
|
file_data = (uint8_t*)WebPMalloc(file_size + 1);
|
2017-11-29 21:30:22 +01:00
|
|
|
if (file_data == NULL) {
|
|
|
|
fclose(in);
|
libwebp: Unicode command tools on Windows
Define macros in examples/unicode.h to use Unicode argv
on Windows. Keep char everywhere on Unix since it handles
UTF-8 without any change.
Impact:
- All fopen () and SHCreateStreamOnFile(),
- All fprintf() printing file paths,
- All strcmp() used with "-",
- File path parsing,
- Gif reading.
Concerned executables from examples/ and extras/:
anim_diff, anim_dump, vwebp, vwebp_sdl,
cwebp, dwebp, gif2webp, img2webp,
webpmux, webpinfo, webp_quality, get_disto
When compiled on Windows with Unicode enabled, webpmux and
img2webp will not work when used with an argument file and
will print "Reading arguments from a file is a feature
unavailable with Unicode binaries."
BUG=webp:398
Change-Id: Ic55d222a3ce1a715f9c4cce57ecbe2705d5ce317
2018-10-16 11:03:18 +02:00
|
|
|
WFPRINTF(stderr, "memory allocation failure when reading file %s\n",
|
|
|
|
(const W_CHAR*)file_name);
|
2017-11-29 21:30:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2016-07-21 03:25:00 +02:00
|
|
|
ok = (fread(file_data, file_size, 1, in) == 1);
|
|
|
|
fclose(in);
|
|
|
|
|
|
|
|
if (!ok) {
|
libwebp: Unicode command tools on Windows
Define macros in examples/unicode.h to use Unicode argv
on Windows. Keep char everywhere on Unix since it handles
UTF-8 without any change.
Impact:
- All fopen () and SHCreateStreamOnFile(),
- All fprintf() printing file paths,
- All strcmp() used with "-",
- File path parsing,
- Gif reading.
Concerned executables from examples/ and extras/:
anim_diff, anim_dump, vwebp, vwebp_sdl,
cwebp, dwebp, gif2webp, img2webp,
webpmux, webpinfo, webp_quality, get_disto
When compiled on Windows with Unicode enabled, webpmux and
img2webp will not work when used with an argument file and
will print "Reading arguments from a file is a feature
unavailable with Unicode binaries."
BUG=webp:398
Change-Id: Ic55d222a3ce1a715f9c4cce57ecbe2705d5ce317
2018-10-16 11:03:18 +02:00
|
|
|
WFPRINTF(stderr, "Could not read %d bytes of data from file %s\n",
|
|
|
|
(int)file_size, (const W_CHAR*)file_name);
|
2020-10-16 15:37:26 +02:00
|
|
|
WebPFree(file_data);
|
2016-07-21 03:25:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-12-01 09:21:13 +01:00
|
|
|
file_data[file_size] = '\0'; // convenient 0-terminator
|
|
|
|
*data = file_data;
|
2016-07-21 03:25:00 +02:00
|
|
|
*data_size = file_size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-12-01 09:21:13 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2016-07-22 00:36:29 +02:00
|
|
|
int ImgIoUtilWriteFile(const char* const file_name,
|
|
|
|
const uint8_t* data, size_t data_size) {
|
2016-07-21 03:25:00 +02:00
|
|
|
int ok;
|
|
|
|
FILE* out;
|
libwebp: Unicode command tools on Windows
Define macros in examples/unicode.h to use Unicode argv
on Windows. Keep char everywhere on Unix since it handles
UTF-8 without any change.
Impact:
- All fopen () and SHCreateStreamOnFile(),
- All fprintf() printing file paths,
- All strcmp() used with "-",
- File path parsing,
- Gif reading.
Concerned executables from examples/ and extras/:
anim_diff, anim_dump, vwebp, vwebp_sdl,
cwebp, dwebp, gif2webp, img2webp,
webpmux, webpinfo, webp_quality, get_disto
When compiled on Windows with Unicode enabled, webpmux and
img2webp will not work when used with an argument file and
will print "Reading arguments from a file is a feature
unavailable with Unicode binaries."
BUG=webp:398
Change-Id: Ic55d222a3ce1a715f9c4cce57ecbe2705d5ce317
2018-10-16 11:03:18 +02:00
|
|
|
const int to_stdout = (file_name == NULL) || !WSTRCMP(file_name, "-");
|
2016-07-21 03:25:00 +02:00
|
|
|
|
|
|
|
if (data == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
libwebp: Unicode command tools on Windows
Define macros in examples/unicode.h to use Unicode argv
on Windows. Keep char everywhere on Unix since it handles
UTF-8 without any change.
Impact:
- All fopen () and SHCreateStreamOnFile(),
- All fprintf() printing file paths,
- All strcmp() used with "-",
- File path parsing,
- Gif reading.
Concerned executables from examples/ and extras/:
anim_diff, anim_dump, vwebp, vwebp_sdl,
cwebp, dwebp, gif2webp, img2webp,
webpmux, webpinfo, webp_quality, get_disto
When compiled on Windows with Unicode enabled, webpmux and
img2webp will not work when used with an argument file and
will print "Reading arguments from a file is a feature
unavailable with Unicode binaries."
BUG=webp:398
Change-Id: Ic55d222a3ce1a715f9c4cce57ecbe2705d5ce317
2018-10-16 11:03:18 +02:00
|
|
|
out = to_stdout ? ImgIoUtilSetBinaryMode(stdout) : WFOPEN(file_name, "wb");
|
2016-07-21 03:25:00 +02:00
|
|
|
if (out == NULL) {
|
libwebp: Unicode command tools on Windows
Define macros in examples/unicode.h to use Unicode argv
on Windows. Keep char everywhere on Unix since it handles
UTF-8 without any change.
Impact:
- All fopen () and SHCreateStreamOnFile(),
- All fprintf() printing file paths,
- All strcmp() used with "-",
- File path parsing,
- Gif reading.
Concerned executables from examples/ and extras/:
anim_diff, anim_dump, vwebp, vwebp_sdl,
cwebp, dwebp, gif2webp, img2webp,
webpmux, webpinfo, webp_quality, get_disto
When compiled on Windows with Unicode enabled, webpmux and
img2webp will not work when used with an argument file and
will print "Reading arguments from a file is a feature
unavailable with Unicode binaries."
BUG=webp:398
Change-Id: Ic55d222a3ce1a715f9c4cce57ecbe2705d5ce317
2018-10-16 11:03:18 +02:00
|
|
|
WFPRINTF(stderr, "Error! Cannot open output file '%s'\n",
|
|
|
|
(const W_CHAR*)file_name);
|
2016-07-21 03:25:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ok = (fwrite(data, data_size, 1, out) == 1);
|
|
|
|
if (out != stdout) fclose(out);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2016-07-22 00:36:29 +02:00
|
|
|
void ImgIoUtilCopyPlane(const uint8_t* src, int src_stride,
|
|
|
|
uint8_t* dst, int dst_stride, int width, int height) {
|
2016-07-21 03:25:00 +02:00
|
|
|
while (height-- > 0) {
|
|
|
|
memcpy(dst, src, width * sizeof(*dst));
|
|
|
|
src += src_stride;
|
|
|
|
dst += dst_stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2016-10-07 22:18:29 +02:00
|
|
|
|
|
|
|
int ImgIoUtilCheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) {
|
|
|
|
const uint64_t total_size = nmemb * size;
|
2017-08-01 21:07:43 +02:00
|
|
|
int ok = (total_size == (size_t)total_size);
|
|
|
|
#if defined(WEBP_MAX_IMAGE_SIZE)
|
|
|
|
ok = ok && (total_size <= (uint64_t)WEBP_MAX_IMAGE_SIZE);
|
|
|
|
#endif
|
|
|
|
return ok;
|
2016-10-07 22:18:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|