2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* image_loader_webp.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "image_loader_webp.h"
|
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/io/marshalls.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/print_string.h"
|
2016-10-11 21:35:58 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include <stdlib.h>
|
2016-10-11 21:35:58 +00:00
|
|
|
#include <webp/decode.h>
|
|
|
|
#include <webp/encode.h>
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
static Vector<uint8_t> _webp_lossy_pack(const Ref<Image> &p_image, float p_quality) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
ERR_FAIL_COND_V(p_image.is_null() || p_image->empty(), Vector<uint8_t>());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
Ref<Image> img = p_image->duplicate();
|
|
|
|
if (img->detect_alpha())
|
|
|
|
img->convert(Image::FORMAT_RGBA8);
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-05-17 10:36:47 +00:00
|
|
|
img->convert(Image::FORMAT_RGB8);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
Size2 s(img->get_width(), img->get_height());
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> data = img->get_data();
|
|
|
|
const uint8_t *r = data.ptr();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
uint8_t *dst_buff = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
size_t dst_size = 0;
|
2017-05-17 10:36:47 +00:00
|
|
|
if (img->get_format() == Image::FORMAT_RGB8) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
dst_size = WebPEncodeRGB(r, s.width, s.height, 3 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2020-02-17 21:06:54 +00:00
|
|
|
dst_size = WebPEncodeRGBA(r, s.width, s.height, 4 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
ERR_FAIL_COND_V(dst_size == 0, Vector<uint8_t>());
|
|
|
|
Vector<uint8_t> dst;
|
2017-03-05 15:44:50 +00:00
|
|
|
dst.resize(4 + dst_size);
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *w = dst.ptrw();
|
2017-03-05 15:44:50 +00:00
|
|
|
w[0] = 'W';
|
|
|
|
w[1] = 'E';
|
|
|
|
w[2] = 'B';
|
|
|
|
w[3] = 'P';
|
|
|
|
copymem(&w[4], dst_buff, dst_size);
|
2014-02-10 01:10:30 +00:00
|
|
|
free(dst_buff);
|
2020-02-17 21:06:54 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
static Ref<Image> _webp_lossy_unpack(const Vector<uint8_t> &p_buffer) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int size = p_buffer.size() - 4;
|
2017-05-17 10:36:47 +00:00
|
|
|
ERR_FAIL_COND_V(size <= 0, Ref<Image>());
|
2020-02-17 21:06:54 +00:00
|
|
|
const uint8_t *r = p_buffer.ptr();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
ERR_FAIL_COND_V(r[0] != 'W' || r[1] != 'E' || r[2] != 'B' || r[3] != 'P', Ref<Image>());
|
2014-02-10 01:10:30 +00:00
|
|
|
WebPBitstreamFeatures features;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (WebPGetFeatures(&r[4], size, &features) != VP8_STATUS_OK) {
|
2019-08-11 08:49:53 +00:00
|
|
|
ERR_FAIL_V_MSG(Ref<Image>(), "Error unpacking WEBP image.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 08:32:52 +00:00
|
|
|
/*
|
|
|
|
print_line("width: "+itos(features.width));
|
|
|
|
print_line("height: "+itos(features.height));
|
|
|
|
print_line("alpha: "+itos(features.has_alpha));
|
|
|
|
*/
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> dst_image;
|
2017-03-05 15:44:50 +00:00
|
|
|
int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
|
2014-02-10 01:10:30 +00:00
|
|
|
dst_image.resize(datasize);
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *dst_w = dst_image.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool errdec = false;
|
|
|
|
if (features.has_alpha) {
|
2020-04-01 23:20:12 +00:00
|
|
|
errdec = WebPDecodeRGBAInto(&r[4], size, dst_w, datasize, 4 * features.width) == nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2020-04-01 23:20:12 +00:00
|
|
|
errdec = WebPDecodeRGBInto(&r[4], size, dst_w, datasize, 3 * features.width) == nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 11:04:33 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(errdec, Ref<Image>(), "Failed decoding WebP image.");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
Ref<Image> img = memnew(Image(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image));
|
|
|
|
return img;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 17:42:37 +00:00
|
|
|
Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-14 17:42:37 +00:00
|
|
|
ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
WebPBitstreamFeatures features;
|
2018-07-14 17:42:37 +00:00
|
|
|
if (WebPGetFeatures(p_buffer, p_buffer_len, &features) != VP8_STATUS_OK) {
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_V(ERR_FILE_CORRUPT);
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> dst_image;
|
2017-03-05 15:44:50 +00:00
|
|
|
int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
|
2014-02-10 01:10:30 +00:00
|
|
|
dst_image.resize(datasize);
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *dst_w = dst_image.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool errdec = false;
|
|
|
|
if (features.has_alpha) {
|
2020-04-01 23:20:12 +00:00
|
|
|
errdec = WebPDecodeRGBAInto(p_buffer, p_buffer_len, dst_w, datasize, 4 * features.width) == nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2020-04-01 23:20:12 +00:00
|
|
|
errdec = WebPDecodeRGBInto(p_buffer, p_buffer_len, dst_w, datasize, 3 * features.width) == nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 11:04:33 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(errdec, ERR_FILE_CORRUPT, "Failed decoding WebP image.");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
p_image->create(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2018-07-14 17:42:37 +00:00
|
|
|
static Ref<Image> _webp_mem_loader_func(const uint8_t *p_png, int p_size) {
|
|
|
|
|
|
|
|
Ref<Image> img;
|
|
|
|
img.instance();
|
|
|
|
Error err = webp_load_image_from_buffer(img.ptr(), p_png, p_size);
|
|
|
|
ERR_FAIL_COND_V(err, Ref<Image>());
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error ImageLoaderWEBP::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<uint8_t> src_image;
|
2018-07-14 17:42:37 +00:00
|
|
|
int src_image_len = f->get_len();
|
|
|
|
ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
|
|
|
|
src_image.resize(src_image_len);
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
uint8_t *w = src_image.ptrw();
|
2018-07-14 17:42:37 +00:00
|
|
|
|
|
|
|
f->get_buffer(&w[0], src_image_len);
|
|
|
|
|
|
|
|
f->close();
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Error err = webp_load_image_from_buffer(p_image.ptr(), w, src_image_len);
|
2018-07-14 17:42:37 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
p_extensions->push_back("webp");
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageLoaderWEBP::ImageLoaderWEBP() {
|
2018-07-14 17:42:37 +00:00
|
|
|
Image::_webp_mem_loader_func = _webp_mem_loader_func;
|
2017-03-05 15:44:50 +00:00
|
|
|
Image::lossy_packer = _webp_lossy_pack;
|
|
|
|
Image::lossy_unpacker = _webp_lossy_unpack;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|