18#include <webp/decode.h>
19#include <webp/encode.h>
34 if (WebPGetInfo(mem,
size,
nullptr,
nullptr)) {
46 WebPBitstreamFeatures features;
47 if (WebPGetFeatures(mem,
size, &features) != VP8_STATUS_OK) {
48 fprintf(stderr,
"WebP: Failed to parse features\n");
52 const int planes = features.has_alpha ? 32 : 24;
55 if (ibuf ==
nullptr) {
56 fprintf(stderr,
"WebP: Failed to allocate image memory\n");
61 ibuf->
ftype = IMB_FTYPE_WEBP;
65 if (WebPDecodeRGBAInto(mem,
size, last_row,
size_t(ibuf->
x) * ibuf->
y * 4, -4 * ibuf->
x) ==
68 fprintf(stderr,
"WebP: Failed to decode image\n");
77 const size_t max_thumb_size,
91 if (mmap_file ==
nullptr) {
98 WebPDecoderConfig config;
99 if (!
data || !WebPInitDecoderConfig(&config) ||
100 WebPGetFeatures(
data, data_size, &config.input) != VP8_STATUS_OK)
102 fprintf(stderr,
"WebP: Invalid file\n");
110 *r_width = size_t(config.input.width);
111 *r_height = size_t(config.input.height);
113 const float scale = float(max_thumb_size) / std::max(config.input.width, config.input.height);
114 const int dest_w = std::max(
int(config.input.width * scale), 1);
115 const int dest_h = std::max(
int(config.input.height * scale), 1);
118 if (ibuf ==
nullptr) {
119 fprintf(stderr,
"WebP: Failed to allocate image memory\n");
126 config.options.no_fancy_upsampling = 1;
127 config.options.use_scaling = 1;
128 config.options.scaled_width = dest_w;
129 config.options.scaled_height = dest_h;
130 config.options.bypass_filtering = 1;
131 config.options.use_threads = 0;
132 config.options.flip = 1;
133 config.output.is_external_memory = 1;
134 config.output.colorspace = MODE_RGBA;
136 config.output.u.RGBA.stride = 4 * ibuf->
x;
137 config.output.u.RGBA.size = size_t(config.output.u.RGBA.stride) * size_t(ibuf->
y);
139 if (WebPDecode(
data, data_size, &config) != VP8_STATUS_OK) {
140 fprintf(stderr,
"WebP: Failed to decode image\n");
150 WebPFreeDecBuffer(&config.output);
161 const uint limit = 16383;
162 if (ibuf->
x > limit || ibuf->
y > limit) {
163 fprintf(stderr,
"WebP: image x/y exceeds %u\n", limit);
167 const int bytesperpixel = (ibuf->
planes + 7) >> 3;
168 uchar *encoded_data, *last_row;
169 size_t encoded_data_size;
171 if (bytesperpixel == 3) {
176 for (
size_t i = 0;
i < num_pixels;
i++) {
177 rgb_rect[
i * 3 + 0] = rgba_rect[
i * 4 + 0];
178 rgb_rect[
i * 3 + 1] = rgba_rect[
i * 4 + 1];
179 rgb_rect[
i * 3 + 2] = rgba_rect[
i * 4 + 2];
182 last_row = (
uchar *)(rgb_rect + (
size_t(ibuf->
y - 1) *
size_t(ibuf->
x) * 3));
185 encoded_data_size = WebPEncodeLosslessRGB(
186 last_row, ibuf->
x, ibuf->
y, -3 * ibuf->
x, &encoded_data);
189 encoded_data_size = WebPEncodeRGB(
194 else if (bytesperpixel == 4) {
198 encoded_data_size = WebPEncodeLosslessRGBA(
199 last_row, ibuf->
x, ibuf->
y, -4 * ibuf->
x, &encoded_data);
202 encoded_data_size = WebPEncodeRGBA(
208 stderr,
"WebP: Unsupported bytes per pixel: %d for file: '%s'\n", bytesperpixel, filepath);
212 if (encoded_data !=
nullptr) {
216 fprintf(stderr,
"WebP: Cannot open file for writing: '%s'\n", filepath);
219 fwrite(encoded_data, encoded_data_size, 1, fp);
File and directory operations.
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_kdtree_nd_ free(KDTree *tree)
void * BLI_mmap_get_pointer(BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
void BLI_mmap_free(BLI_mmap_file *file) ATTR_NONNULL(1)
BLI_mmap_file * BLI_mmap_open(int fd) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
size_t BLI_mmap_get_length(const BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
bool IMB_alloc_byte_pixels(ImBuf *ibuf, bool initialize_pixels=true)
void IMB_freeImBuf(ImBuf *ibuf)
size_t IMB_get_pixel_count(const ImBuf *ibuf)
Get the length of the data of the given image buffer in pixels.
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, unsigned char planes, unsigned int flags)
Read Guarded memory(de)allocation.
BMesh const char void * data
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
void MEM_freeN(void *vmemh)
ImbFormatOptions foptions
ImBufByteBuffer byte_buffer