summaryrefslogtreecommitdiff
path: root/drivers/png
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2017-05-17 07:36:47 -0300
committerJuan Linietsky <reduzio@gmail.com>2017-05-17 07:37:45 -0300
commit98a329670227c726a5d7a196e5cba8dbdd54301b (patch)
tree7346f7a3e38eb35b784ac1a68a227d07cc8881b4 /drivers/png
parentd801ff2b3db2de105c1b36a74ce116e360143d4e (diff)
Removal of Image from Variant, converted to a Resource.
Diffstat (limited to 'drivers/png')
-rw-r--r--drivers/png/image_loader_png.cpp42
-rw-r--r--drivers/png/image_loader_png.h4
-rw-r--r--drivers/png/resource_saver_png.cpp26
-rw-r--r--drivers/png/resource_saver_png.h3
4 files changed, 40 insertions, 35 deletions
diff --git a/drivers/png/image_loader_png.cpp b/drivers/png/image_loader_png.cpp
index c9677bed11..fe2372e3af 100644
--- a/drivers/png/image_loader_png.cpp
+++ b/drivers/png/image_loader_png.cpp
@@ -68,7 +68,7 @@ static void _png_warn_function(png_structp, png_const_charp text) {
typedef void(PNGAPI *png_error_ptr) PNGARG((png_structp, png_const_charp));
-Error ImageLoaderPNG::_load_image(void *rf_up, png_rw_ptr p_func, Image *p_image) {
+Error ImageLoaderPNG::_load_image(void *rf_up, png_rw_ptr p_func, Ref<Image> p_image) {
png_structp png;
png_infop info;
@@ -201,7 +201,7 @@ Error ImageLoaderPNG::_load_image(void *rf_up, png_rw_ptr p_func, Image *p_image
return OK;
}
-Error ImageLoaderPNG::load_image(Image *p_image, FileAccess *f) {
+Error ImageLoaderPNG::load_image(Ref<Image> p_image, FileAccess *f) {
Error err = _load_image(f, _read_png_data, p_image);
f->close();
@@ -238,25 +238,26 @@ static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t p_len
}
}
-static Image _load_mem_png(const uint8_t *p_png, int p_size) {
+static Ref<Image> _load_mem_png(const uint8_t *p_png, int p_size) {
PNGReadStatus prs;
prs.image = p_png;
prs.offset = 0;
prs.size = p_size;
- Image img;
- Error err = ImageLoaderPNG::_load_image(&prs, user_read_data, &img);
- ERR_FAIL_COND_V(err, Image());
+ Ref<Image> img;
+ img.instance();
+ Error err = ImageLoaderPNG::_load_image(&prs, user_read_data, img);
+ ERR_FAIL_COND_V(err, Ref<Image>());
return img;
}
-static Image _lossless_unpack_png(const PoolVector<uint8_t> &p_data) {
+static Ref<Image> _lossless_unpack_png(const PoolVector<uint8_t> &p_data) {
int len = p_data.size();
PoolVector<uint8_t>::Read r = p_data.read();
- ERR_FAIL_COND_V(r[0] != 'P' || r[1] != 'N' || r[2] != 'G' || r[3] != ' ', Image());
+ ERR_FAIL_COND_V(r[0] != 'P' || r[1] != 'N' || r[2] != 'G' || r[3] != ' ', Ref<Image>());
return _load_mem_png(&r[4], len - 4);
}
@@ -271,13 +272,14 @@ static void _write_png_data(png_structp png_ptr, png_bytep data, png_size_t p_le
//print_line("png write: "+itos(p_length));
}
-static PoolVector<uint8_t> _lossless_pack_png(const Image &p_image) {
+static PoolVector<uint8_t> _lossless_pack_png(const Ref<Image> &p_image) {
- Image img = p_image;
- if (img.is_compressed())
- img.decompress();
+ Ref<Image> img = p_image->duplicate();
- ERR_FAIL_COND_V(img.is_compressed(), PoolVector<uint8_t>());
+ if (img->is_compressed())
+ img->decompress();
+
+ ERR_FAIL_COND_V(img->is_compressed(), PoolVector<uint8_t>());
png_structp png_ptr;
png_infop info_ptr;
@@ -311,7 +313,7 @@ static PoolVector<uint8_t> _lossless_pack_png(const Image &p_image) {
int pngf = 0;
int cs = 0;
- switch (img.get_format()) {
+ switch (img->get_format()) {
case Image::FORMAT_L8: {
@@ -335,22 +337,22 @@ static PoolVector<uint8_t> _lossless_pack_png(const Image &p_image) {
} break;
default: {
- if (img.detect_alpha()) {
+ if (img->detect_alpha()) {
- img.convert(Image::FORMAT_RGBA8);
+ img->convert(Image::FORMAT_RGBA8);
pngf = PNG_COLOR_TYPE_RGB_ALPHA;
cs = 4;
} else {
- img.convert(Image::FORMAT_RGB8);
+ img->convert(Image::FORMAT_RGB8);
pngf = PNG_COLOR_TYPE_RGB;
cs = 3;
}
}
}
- int w = img.get_width();
- int h = img.get_height();
+ int w = img->get_width();
+ int h = img->get_height();
png_set_IHDR(png_ptr, info_ptr, w, h,
8, pngf, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
@@ -362,7 +364,7 @@ static PoolVector<uint8_t> _lossless_pack_png(const Image &p_image) {
ERR_FAIL_V(PoolVector<uint8_t>());
}
- PoolVector<uint8_t>::Read r = img.get_data().read();
+ PoolVector<uint8_t>::Read r = img->get_data().read();
row_pointers = (png_bytep *)memalloc(sizeof(png_bytep) * h);
for (int i = 0; i < h; i++) {
diff --git a/drivers/png/image_loader_png.h b/drivers/png/image_loader_png.h
index 78e4252e26..f94b8bce6e 100644
--- a/drivers/png/image_loader_png.h
+++ b/drivers/png/image_loader_png.h
@@ -42,8 +42,8 @@ class ImageLoaderPNG : public ImageFormatLoader {
static void _read_png_data(png_structp png_ptr, png_bytep data, png_size_t p_length);
public:
- static Error _load_image(void *rf_up, png_rw_ptr p_func, Image *p_image);
- virtual Error load_image(Image *p_image, FileAccess *f);
+ static Error _load_image(void *rf_up, png_rw_ptr p_func, Ref<Image> p_image);
+ virtual Error load_image(Ref<Image> p_image, FileAccess *f);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderPNG();
};
diff --git a/drivers/png/resource_saver_png.cpp b/drivers/png/resource_saver_png.cpp
index b754ef97b4..1700603489 100644
--- a/drivers/png/resource_saver_png.cpp
+++ b/drivers/png/resource_saver_png.cpp
@@ -50,7 +50,7 @@ Error ResourceSaverPNG::save(const String &p_path, const RES &p_resource, uint32
ERR_EXPLAIN("Can't save empty texture as PNG");
ERR_FAIL_COND_V(!texture->get_width() || !texture->get_height(), ERR_INVALID_PARAMETER);
- Image img = texture->get_data();
+ Ref<Image> img = texture->get_data();
Error err = save_image(p_path, img);
@@ -95,12 +95,14 @@ Error ResourceSaverPNG::save(const String &p_path, const RES &p_resource, uint32
return err;
};
-Error ResourceSaverPNG::save_image(const String &p_path, Image &p_img) {
+Error ResourceSaverPNG::save_image(const String &p_path, const Ref<Image> &p_img) {
- if (p_img.is_compressed())
- p_img.decompress();
+ Ref<Image> img = p_img->duplicate();
- ERR_FAIL_COND_V(p_img.is_compressed(), ERR_INVALID_PARAMETER);
+ if (img->is_compressed())
+ img->decompress();
+
+ ERR_FAIL_COND_V(img->is_compressed(), ERR_INVALID_PARAMETER);
png_structp png_ptr;
png_infop info_ptr;
@@ -135,7 +137,7 @@ Error ResourceSaverPNG::save_image(const String &p_path, Image &p_img) {
int pngf = 0;
int cs = 0;
- switch (p_img.get_format()) {
+ switch (img->get_format()) {
case Image::FORMAT_L8: {
@@ -159,22 +161,22 @@ Error ResourceSaverPNG::save_image(const String &p_path, Image &p_img) {
} break;
default: {
- if (p_img.detect_alpha()) {
+ if (img->detect_alpha()) {
- p_img.convert(Image::FORMAT_RGBA8);
+ img->convert(Image::FORMAT_RGBA8);
pngf = PNG_COLOR_TYPE_RGB_ALPHA;
cs = 4;
} else {
- p_img.convert(Image::FORMAT_RGB8);
+ img->convert(Image::FORMAT_RGB8);
pngf = PNG_COLOR_TYPE_RGB;
cs = 3;
}
}
}
- int w = p_img.get_width();
- int h = p_img.get_height();
+ int w = img->get_width();
+ int h = img->get_height();
png_set_IHDR(png_ptr, info_ptr, w, h,
8, pngf, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
@@ -187,7 +189,7 @@ Error ResourceSaverPNG::save_image(const String &p_path, Image &p_img) {
ERR_FAIL_V(ERR_CANT_OPEN);
}
- PoolVector<uint8_t>::Read r = p_img.get_data().read();
+ PoolVector<uint8_t>::Read r = img->get_data().read();
row_pointers = (png_bytep *)memalloc(sizeof(png_bytep) * h);
for (int i = 0; i < h; i++) {
diff --git a/drivers/png/resource_saver_png.h b/drivers/png/resource_saver_png.h
index c25a01c1f6..31ec35c192 100644
--- a/drivers/png/resource_saver_png.h
+++ b/drivers/png/resource_saver_png.h
@@ -30,11 +30,12 @@
#ifndef RESOURCE_SAVER_PNG_H
#define RESOURCE_SAVER_PNG_H
+#include "image.h"
#include "io/resource_saver.h"
class ResourceSaverPNG : public ResourceFormatSaver {
public:
- static Error save_image(const String &p_path, Image &p_img);
+ static Error save_image(const String &p_path, const Ref<Image> &p_img);
virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
virtual bool recognize(const RES &p_resource) const;