18#include <webp/decode.h>
19#include <webp/encode.h>
35 if (WebPGetInfo(buf, size,
nullptr,
nullptr)) {
49 WebPBitstreamFeatures features;
50 if (WebPGetFeatures(mem, size, &features) != VP8_STATUS_OK) {
51 fprintf(stderr,
"WebP: Failed to parse features\n");
55 const int planes = features.has_alpha ? 32 : 24;
58 if (ibuf ==
nullptr) {
59 fprintf(stderr,
"WebP: Failed to allocate image memory\n");
64 ibuf->
ftype = IMB_FTYPE_WEBP;
68 if (WebPDecodeRGBAInto(mem, size, last_row,
size_t(ibuf->
x) * ibuf->
y * 4, -4 * ibuf->
x) ==
71 fprintf(stderr,
"WebP: Failed to decode image\n");
80 const size_t max_thumb_size,
94 if (mmap_file ==
nullptr) {
101 WebPDecoderConfig config;
102 if (!data || !WebPInitDecoderConfig(&config) ||
103 WebPGetFeatures(data, data_size, &config.input) != VP8_STATUS_OK)
105 fprintf(stderr,
"WebP: Invalid file\n");
113 *r_width = size_t(config.input.width);
114 *r_height = size_t(config.input.height);
116 const float scale =
float(max_thumb_size) / std::max(config.input.width, config.input.height);
117 const int dest_w = std::max(
int(config.input.width * scale), 1);
118 const int dest_h = std::max(
int(config.input.height * scale), 1);
122 if (ibuf ==
nullptr) {
123 fprintf(stderr,
"WebP: Failed to allocate image memory\n");
130 config.options.no_fancy_upsampling = 1;
131 config.options.use_scaling = 1;
132 config.options.scaled_width = dest_w;
133 config.options.scaled_height = dest_h;
134 config.options.bypass_filtering = 1;
135 config.options.use_threads = 0;
136 config.options.flip = 1;
137 config.output.is_external_memory = 1;
138 config.output.colorspace = MODE_RGBA;
140 config.output.u.RGBA.stride = 4 * ibuf->
x;
141 config.output.u.RGBA.size = size_t(config.output.u.RGBA.stride * ibuf->
y);
143 if (WebPDecode(data, data_size, &config) != VP8_STATUS_OK) {
144 fprintf(stderr,
"WebP: Failed to decode image\n");
152 WebPFreeDecBuffer(&config.output);
163 const int bytesperpixel = (ibuf->
planes + 7) >> 3;
164 uchar *encoded_data, *last_row;
165 size_t encoded_data_size;
167 if (bytesperpixel == 3) {
169 const size_t num_pixels = ibuf->
x * ibuf->
y;
173 for (
int i = 0; i < num_pixels; i++) {
174 rgb_rect[i * 3 + 0] = rgba_rect[i * 4 + 0];
175 rgb_rect[i * 3 + 1] = rgba_rect[i * 4 + 1];
176 rgb_rect[i * 3 + 2] = rgba_rect[i * 4 + 2];
179 last_row = (
uchar *)(rgb_rect + (ibuf->
y - 1) * ibuf->
x * 3);
182 encoded_data_size = WebPEncodeLosslessRGB(
183 last_row, ibuf->
x, ibuf->
y, -3 * ibuf->
x, &encoded_data);
186 encoded_data_size = WebPEncodeRGB(
191 else if (bytesperpixel == 4) {
195 encoded_data_size = WebPEncodeLosslessRGBA(
196 last_row, ibuf->
x, ibuf->
y, -4 * ibuf->
x, &encoded_data);
199 encoded_data_size = WebPEncodeRGBA(
205 stderr,
"WebP: Unsupported bytes per pixel: %d for file: '%s'\n", bytesperpixel, filepath);
209 if (encoded_data !=
nullptr) {
213 fprintf(stderr,
"WebP: Cannot open file for writing: '%s'\n", filepath);
216 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
@ COLOR_ROLE_DEFAULT_BYTE
bool imb_addrectImBuf(ImBuf *ibuf, bool initialize_pixels=true)
Contains defines and structs used throughout the imbuf module.
Read Guarded memory(de)allocation.
void colorspace_set_default_role(char *colorspace, int size, int role)
draw_view in_light_buf[] float
struct ImBuf * IMB_allocImBuf(unsigned int, unsigned int, unsigned char, unsigned int)
void *(* MEM_mallocN)(size_t len, const char *str)
void MEM_freeN(void *vmemh)
ImbFormatOptions foptions
ImBufByteBuffer byte_buffer
bool imb_savewebp(ImBuf *ibuf, const char *filepath, int)
ImBuf * imb_load_filepath_thumbnail_webp(const char *filepath, const int, const size_t max_thumb_size, char colorspace[], size_t *r_width, size_t *r_height)
bool imb_is_a_webp(const uchar *buf, size_t size)
ImBuf * imb_loadwebp(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])