Add webp buffer loader for Image

Cleanup the code memory load related code for Image
Fix jpeg buff load function always returns OK event failed
This commit is contained in:
geequlim 2018-07-15 01:42:37 +08:00
parent 1fc7973a00
commit c5efe5d625
5 changed files with 70 additions and 53 deletions

View File

@ -1898,8 +1898,9 @@ void Image::fill(const Color &c) {
unlock(); unlock();
} }
Ref<Image> (*Image::_png_mem_loader_func)(const uint8_t *, int) = NULL; ImageMemLoadFunc Image::_png_mem_loader_func = NULL;
Ref<Image> (*Image::_jpg_mem_loader_func)(const uint8_t *, int) = NULL; ImageMemLoadFunc Image::_jpg_mem_loader_func = NULL;
ImageMemLoadFunc Image::_webp_mem_loader_func = NULL;
void (*Image::_image_compress_bc_func)(Image *, Image::CompressSource) = NULL; void (*Image::_image_compress_bc_func)(Image *, Image::CompressSource) = NULL;
void (*Image::_image_compress_pvrtc2_func)(Image *) = NULL; void (*Image::_image_compress_pvrtc2_func)(Image *) = NULL;
@ -2357,6 +2358,7 @@ void Image::_bind_methods() {
ClassDB::bind_method(D_METHOD("load_png_from_buffer", "buffer"), &Image::load_png_from_buffer); ClassDB::bind_method(D_METHOD("load_png_from_buffer", "buffer"), &Image::load_png_from_buffer);
ClassDB::bind_method(D_METHOD("load_jpg_from_buffer", "buffer"), &Image::load_jpg_from_buffer); ClassDB::bind_method(D_METHOD("load_jpg_from_buffer", "buffer"), &Image::load_jpg_from_buffer);
ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer"), &Image::load_webp_from_buffer);
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data"); ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data");
@ -2649,32 +2651,26 @@ String Image::get_format_name(Format p_format) {
} }
Error Image::load_png_from_buffer(const PoolVector<uint8_t> &p_array) { Error Image::load_png_from_buffer(const PoolVector<uint8_t> &p_array) {
return _load_from_buffer(p_array, _png_mem_loader_func);
int buffer_size = p_array.size();
ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(!_png_mem_loader_func, ERR_INVALID_PARAMETER);
PoolVector<uint8_t>::Read r = p_array.read();
Ref<Image> image = _png_mem_loader_func(r.ptr(), buffer_size);
ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR);
copy_internals_from(image);
return OK;
} }
Error Image::load_jpg_from_buffer(const PoolVector<uint8_t> &p_array) { Error Image::load_jpg_from_buffer(const PoolVector<uint8_t> &p_array) {
return _load_from_buffer(p_array, _jpg_mem_loader_func);
}
Error Image::load_webp_from_buffer(const PoolVector<uint8_t> &p_array) {
return _load_from_buffer(p_array, _webp_mem_loader_func);
}
Error Image::_load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader) {
int buffer_size = p_array.size(); int buffer_size = p_array.size();
ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(!_jpg_mem_loader_func, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(!p_loader, ERR_INVALID_PARAMETER);
PoolVector<uint8_t>::Read r = p_array.read(); PoolVector<uint8_t>::Read r = p_array.read();
Ref<Image> image = _jpg_mem_loader_func(r.ptr(), buffer_size); Ref<Image> image = p_loader(r.ptr(), buffer_size);
ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR); ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR);
copy_internals_from(image); copy_internals_from(image);

View File

@ -47,6 +47,7 @@
class Image; class Image;
typedef Error (*SavePNGFunc)(const String &p_path, const Ref<Image> &p_img); typedef Error (*SavePNGFunc)(const String &p_path, const Ref<Image> &p_img);
typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size);
class Image : public Resource { class Image : public Resource {
GDCLASS(Image, Resource); GDCLASS(Image, Resource);
@ -118,8 +119,9 @@ public:
//some functions provided by something else //some functions provided by something else
static Ref<Image> (*_png_mem_loader_func)(const uint8_t *p_png, int p_size); static ImageMemLoadFunc _png_mem_loader_func;
static Ref<Image> (*_jpg_mem_loader_func)(const uint8_t *p_png, int p_size); static ImageMemLoadFunc _jpg_mem_loader_func;
static ImageMemLoadFunc _webp_mem_loader_func;
static void (*_image_compress_bc_func)(Image *, CompressSource p_source); static void (*_image_compress_bc_func)(Image *, CompressSource p_source);
static void (*_image_compress_pvrtc2_func)(Image *); static void (*_image_compress_pvrtc2_func)(Image *);
@ -175,6 +177,8 @@ private:
void _set_data(const Dictionary &p_data); void _set_data(const Dictionary &p_data);
Dictionary _get_data() const; Dictionary _get_data() const;
Error _load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader);
public: public:
int get_width() const; ///< Get image width int get_width() const; ///< Get image width
int get_height() const; ///< Get image height int get_height() const; ///< Get image height
@ -302,6 +306,7 @@ public:
Error load_png_from_buffer(const PoolVector<uint8_t> &p_array); Error load_png_from_buffer(const PoolVector<uint8_t> &p_array);
Error load_jpg_from_buffer(const PoolVector<uint8_t> &p_array); Error load_jpg_from_buffer(const PoolVector<uint8_t> &p_array);
Error load_webp_from_buffer(const PoolVector<uint8_t> &p_array);
Image(const uint8_t *p_mem_png_jpg, int p_len = -1); Image(const uint8_t *p_mem_png_jpg, int p_len = -1);
Image(const char **p_xpm); Image(const char **p_xpm);

View File

@ -350,6 +350,15 @@
Loads an image from the binary contents of a PNG file. Loads an image from the binary contents of a PNG file.
</description> </description>
</method> </method>
<method name="load_webp_from_buffer">
<return type="int" enum="Error">
</return>
<argument index="0" name="buffer" type="PoolByteArray">
</argument>
<description>
Loads an image from the binary contents of a WebP file.
</description>
</method>
<method name="lock"> <method name="lock">
<return type="void"> <return type="void">
</return> </return>

View File

@ -121,9 +121,7 @@ static Ref<Image> _jpegd_mem_loader_func(const uint8_t *p_png, int p_size) {
Ref<Image> img; Ref<Image> img;
img.instance(); img.instance();
Error err = jpeg_load_image_from_buffer(img.ptr(), p_png, p_size); Error err = jpeg_load_image_from_buffer(img.ptr(), p_png, p_size);
if (err) ERR_FAIL_COND_V(err, Ref<Image>());
ERR_PRINT("Couldn't initialize ImageLoaderJPG with the given resource.");
return img; return img;
} }

View File

@ -116,64 +116,73 @@ static Ref<Image> _webp_lossy_unpack(const PoolVector<uint8_t> &p_buffer) {
return img; return img;
} }
Error ImageLoaderWEBP::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) { Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
uint32_t size = f->get_len(); ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER);
PoolVector<uint8_t> src_image;
src_image.resize(size);
WebPBitstreamFeatures features; WebPBitstreamFeatures features;
if (WebPGetFeatures(p_buffer, p_buffer_len, &features) != VP8_STATUS_OK) {
PoolVector<uint8_t>::Write src_w = src_image.write(); // ERR_EXPLAIN("Error decoding WEBP image");
f->get_buffer(src_w.ptr(), size);
ERR_FAIL_COND_V(f->eof_reached(), ERR_FILE_EOF);
if (WebPGetFeatures(src_w.ptr(), size, &features) != VP8_STATUS_OK) {
f->close();
//ERR_EXPLAIN("Error decoding WEBP image: "+p_file);
ERR_FAIL_V(ERR_FILE_CORRUPT); ERR_FAIL_V(ERR_FILE_CORRUPT);
} }
/*
print_line("width: " + itos(features.width));
print_line("height: " + itos(features.height));
print_line("alpha: " + itos(features.has_alpha));
*/
src_w = PoolVector<uint8_t>::Write();
PoolVector<uint8_t> dst_image; PoolVector<uint8_t> dst_image;
int datasize = features.width * features.height * (features.has_alpha ? 4 : 3); int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
dst_image.resize(datasize); dst_image.resize(datasize);
PoolVector<uint8_t>::Read src_r = src_image.read();
PoolVector<uint8_t>::Write dst_w = dst_image.write(); PoolVector<uint8_t>::Write dst_w = dst_image.write();
bool errdec = false; bool errdec = false;
if (features.has_alpha) { if (features.has_alpha) {
errdec = WebPDecodeRGBAInto(src_r.ptr(), size, dst_w.ptr(), datasize, 4 * features.width) == NULL; errdec = WebPDecodeRGBAInto(p_buffer, p_buffer_len, dst_w.ptr(), datasize, 4 * features.width) == NULL;
} else { } else {
errdec = WebPDecodeRGBInto(src_r.ptr(), size, dst_w.ptr(), datasize, 3 * features.width) == NULL; errdec = WebPDecodeRGBInto(p_buffer, p_buffer_len, dst_w.ptr(), datasize, 3 * features.width) == NULL;
} }
//ERR_EXPLAIN("Error decoding webp! - "+p_file);
ERR_FAIL_COND_V(errdec, ERR_FILE_CORRUPT);
src_r = PoolVector<uint8_t>::Read();
dst_w = PoolVector<uint8_t>::Write(); dst_w = PoolVector<uint8_t>::Write();
//ERR_EXPLAIN("Error decoding webp!");
ERR_FAIL_COND_V(errdec, ERR_FILE_CORRUPT);
p_image->create(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image); p_image->create(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image);
return OK; return OK;
} }
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) {
PoolVector<uint8_t> src_image;
int src_image_len = f->get_len();
ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
src_image.resize(src_image_len);
PoolVector<uint8_t>::Write w = src_image.write();
f->get_buffer(&w[0], src_image_len);
f->close();
Error err = webp_load_image_from_buffer(p_image.ptr(), w.ptr(), src_image_len);
w = PoolVector<uint8_t>::Write();
return err;
}
void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const { void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("webp"); p_extensions->push_back("webp");
} }
ImageLoaderWEBP::ImageLoaderWEBP() { ImageLoaderWEBP::ImageLoaderWEBP() {
Image::_webp_mem_loader_func = _webp_mem_loader_func;
Image::lossy_packer = _webp_lossy_pack; Image::lossy_packer = _webp_lossy_pack;
Image::lossy_unpacker = _webp_lossy_unpack; Image::lossy_unpacker = _webp_lossy_unpack;
} }