summaryrefslogtreecommitdiff
path: root/drivers/png
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/png')
-rw-r--r--drivers/png/image_loader_png.cpp30
-rw-r--r--drivers/png/image_loader_png.h8
-rw-r--r--drivers/png/png_driver_common.cpp25
-rw-r--r--drivers/png/png_driver_common.h7
-rw-r--r--drivers/png/resource_saver_png.cpp19
-rw-r--r--drivers/png/resource_saver_png.h5
6 files changed, 52 insertions, 42 deletions
diff --git a/drivers/png/image_loader_png.cpp b/drivers/png/image_loader_png.cpp
index f257fafd93..4cb93a821c 100644
--- a/drivers/png/image_loader_png.cpp
+++ b/drivers/png/image_loader_png.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -39,19 +39,19 @@
Error ImageLoaderPNG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
const size_t buffer_size = f->get_len();
- PoolVector<uint8_t> file_buffer;
+ Vector<uint8_t> file_buffer;
Error err = file_buffer.resize(buffer_size);
if (err) {
f->close();
return err;
}
{
- PoolVector<uint8_t>::Write writer = file_buffer.write();
- f->get_buffer(writer.ptr(), buffer_size);
+ uint8_t *writer = file_buffer.ptrw();
+ f->get_buffer(writer, buffer_size);
f->close();
}
- PoolVector<uint8_t>::Read reader = file_buffer.read();
- return PNGDriverCommon::png_to_image(reader.ptr(), buffer_size, p_image);
+ const uint8_t *reader = file_buffer.ptr();
+ return PNGDriverCommon::png_to_image(reader, buffer_size, p_image);
}
void ImageLoaderPNG::get_recognized_extensions(List<String> *p_extensions) const {
@@ -70,34 +70,34 @@ Ref<Image> ImageLoaderPNG::load_mem_png(const uint8_t *p_png, int p_size) {
return img;
}
-Ref<Image> ImageLoaderPNG::lossless_unpack_png(const PoolVector<uint8_t> &p_data) {
+Ref<Image> ImageLoaderPNG::lossless_unpack_png(const Vector<uint8_t> &p_data) {
const int len = p_data.size();
ERR_FAIL_COND_V(len < 4, Ref<Image>());
- PoolVector<uint8_t>::Read r = p_data.read();
+ const uint8_t *r = p_data.ptr();
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);
}
-PoolVector<uint8_t> ImageLoaderPNG::lossless_pack_png(const Ref<Image> &p_image) {
+Vector<uint8_t> ImageLoaderPNG::lossless_pack_png(const Ref<Image> &p_image) {
- PoolVector<uint8_t> out_buffer;
+ Vector<uint8_t> out_buffer;
// add Godot's own "PNG " prefix
if (out_buffer.resize(4) != OK) {
- ERR_FAIL_V(PoolVector<uint8_t>());
+ ERR_FAIL_V(Vector<uint8_t>());
}
// scope for writer lifetime
{
// must be closed before call to image_to_png
- PoolVector<uint8_t>::Write writer = out_buffer.write();
- copymem(writer.ptr(), "PNG ", 4);
+ uint8_t *writer = out_buffer.ptrw();
+ copymem(writer, "PNG ", 4);
}
Error err = PNGDriverCommon::image_to_png(p_image, out_buffer);
if (err) {
- ERR_FAIL_V(PoolVector<uint8_t>());
+ ERR_FAIL_V(Vector<uint8_t>());
}
return out_buffer;
diff --git a/drivers/png/image_loader_png.h b/drivers/png/image_loader_png.h
index c910c31f1e..0154be0398 100644
--- a/drivers/png/image_loader_png.h
+++ b/drivers/png/image_loader_png.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -35,8 +35,8 @@
class ImageLoaderPNG : public ImageFormatLoader {
private:
- static PoolVector<uint8_t> lossless_pack_png(const Ref<Image> &p_image);
- static Ref<Image> lossless_unpack_png(const PoolVector<uint8_t> &p_data);
+ static Vector<uint8_t> lossless_pack_png(const Ref<Image> &p_image);
+ static Ref<Image> lossless_unpack_png(const Vector<uint8_t> &p_data);
static Ref<Image> load_mem_png(const uint8_t *p_png, int p_size);
public:
diff --git a/drivers/png/png_driver_common.cpp b/drivers/png/png_driver_common.cpp
index 46e45d07d3..efd488fd5c 100644
--- a/drivers/png/png_driver_common.cpp
+++ b/drivers/png/png_driver_common.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -101,25 +101,26 @@ Error png_to_image(const uint8_t *p_source, size_t p_size, Ref<Image> p_image) {
}
const png_uint_32 stride = PNG_IMAGE_ROW_STRIDE(png_img);
- PoolVector<uint8_t> buffer;
+ Vector<uint8_t> buffer;
Error err = buffer.resize(PNG_IMAGE_BUFFER_SIZE(png_img, stride));
if (err) {
png_image_free(&png_img); // only required when we return before finish_read
return err;
}
- PoolVector<uint8_t>::Write writer = buffer.write();
+ uint8_t *writer = buffer.ptrw();
// read image data to buffer and release libpng resources
- success = png_image_finish_read(&png_img, NULL, writer.ptr(), stride, NULL);
+ success = png_image_finish_read(&png_img, NULL, writer, stride, NULL);
ERR_FAIL_COND_V_MSG(check_error(png_img), ERR_FILE_CORRUPT, png_img.message);
ERR_FAIL_COND_V(!success, ERR_FILE_CORRUPT);
+ //print_line("png width: "+itos(png_img.width)+" height: "+itos(png_img.height));
p_image->create(png_img.width, png_img.height, 0, dest_format, buffer);
return OK;
}
-Error image_to_png(const Ref<Image> &p_image, PoolVector<uint8_t> &p_buffer) {
+Error image_to_png(const Ref<Image> &p_image, Vector<uint8_t> &p_buffer) {
Ref<Image> source_image = p_image->duplicate();
@@ -157,8 +158,8 @@ Error image_to_png(const Ref<Image> &p_image, PoolVector<uint8_t> &p_buffer) {
}
}
- const PoolVector<uint8_t> image_data = source_image->get_data();
- const PoolVector<uint8_t>::Read reader = image_data.read();
+ const Vector<uint8_t> image_data = source_image->get_data();
+ const uint8_t *reader = image_data.ptr();
// we may be passed a buffer with existing content we're expected to append to
const int buffer_offset = p_buffer.size();
@@ -172,9 +173,9 @@ Error image_to_png(const Ref<Image> &p_image, PoolVector<uint8_t> &p_buffer) {
Error err = p_buffer.resize(buffer_offset + png_size_estimate);
ERR_FAIL_COND_V(err, err);
- PoolVector<uint8_t>::Write writer = p_buffer.write();
+ uint8_t *writer = p_buffer.ptrw();
success = png_image_write_to_memory(&png_img, &writer[buffer_offset],
- &compressed_size, 0, reader.ptr(), 0, NULL);
+ &compressed_size, 0, reader, 0, NULL);
ERR_FAIL_COND_V_MSG(check_error(png_img), FAILED, png_img.message);
}
if (!success) {
@@ -186,9 +187,9 @@ Error image_to_png(const Ref<Image> &p_image, PoolVector<uint8_t> &p_buffer) {
Error err = p_buffer.resize(buffer_offset + compressed_size);
ERR_FAIL_COND_V(err, err);
- PoolVector<uint8_t>::Write writer = p_buffer.write();
+ uint8_t *writer = p_buffer.ptrw();
success = png_image_write_to_memory(&png_img, &writer[buffer_offset],
- &compressed_size, 0, reader.ptr(), 0, NULL);
+ &compressed_size, 0, reader, 0, NULL);
ERR_FAIL_COND_V_MSG(check_error(png_img), FAILED, png_img.message);
ERR_FAIL_COND_V(!success, FAILED);
}
diff --git a/drivers/png/png_driver_common.h b/drivers/png/png_driver_common.h
index 3ff87759fb..12129f034e 100644
--- a/drivers/png/png_driver_common.h
+++ b/drivers/png/png_driver_common.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -32,7 +32,6 @@
#define PNG_DRIVER_COMMON_H
#include "core/image.h"
-#include "core/pool_vector.h"
namespace PNGDriverCommon {
@@ -41,7 +40,7 @@ Error png_to_image(const uint8_t *p_source, size_t p_size, Ref<Image> p_image);
// Append p_image, as a png, to p_buffer.
// Contents of p_buffer is unspecified if error returned.
-Error image_to_png(const Ref<Image> &p_image, PoolVector<uint8_t> &p_buffer);
+Error image_to_png(const Ref<Image> &p_image, Vector<uint8_t> &p_buffer);
} // namespace PNGDriverCommon
diff --git a/drivers/png/resource_saver_png.cpp b/drivers/png/resource_saver_png.cpp
index 3b3f1506dc..2380c2685f 100644
--- a/drivers/png/resource_saver_png.cpp
+++ b/drivers/png/resource_saver_png.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -51,15 +51,15 @@ Error ResourceSaverPNG::save(const String &p_path, const RES &p_resource, uint32
Error ResourceSaverPNG::save_image(const String &p_path, const Ref<Image> &p_img) {
- PoolVector<uint8_t> buffer;
+ Vector<uint8_t> buffer;
Error err = PNGDriverCommon::image_to_png(p_img, buffer);
ERR_FAIL_COND_V_MSG(err, err, "Can't convert image to PNG.");
FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err);
ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save PNG at path: '%s'.", p_path));
- PoolVector<uint8_t>::Read reader = buffer.read();
+ const uint8_t *reader = buffer.ptr();
- file->store_buffer(reader.ptr(), buffer.size());
+ file->store_buffer(reader, buffer.size());
if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
memdelete(file);
return ERR_CANT_CREATE;
@@ -71,6 +71,14 @@ Error ResourceSaverPNG::save_image(const String &p_path, const Ref<Image> &p_img
return OK;
}
+Vector<uint8_t> ResourceSaverPNG::save_image_to_buffer(const Ref<Image> &p_img) {
+
+ Vector<uint8_t> buffer;
+ Error err = PNGDriverCommon::image_to_png(p_img, buffer);
+ ERR_FAIL_COND_V_MSG(err, Vector<uint8_t>(), "Can't convert image to PNG.");
+ return buffer;
+}
+
bool ResourceSaverPNG::recognize(const RES &p_resource) const {
return (p_resource.is_valid() && p_resource->is_class("ImageTexture"));
@@ -86,4 +94,5 @@ void ResourceSaverPNG::get_recognized_extensions(const RES &p_resource, List<Str
ResourceSaverPNG::ResourceSaverPNG() {
Image::save_png_func = &save_image;
+ Image::save_png_buffer_func = &save_image_to_buffer;
};
diff --git a/drivers/png/resource_saver_png.h b/drivers/png/resource_saver_png.h
index 9267f2d678..c32b383521 100644
--- a/drivers/png/resource_saver_png.h
+++ b/drivers/png/resource_saver_png.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
+/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -37,6 +37,7 @@
class ResourceSaverPNG : public ResourceFormatSaver {
public:
static Error save_image(const String &p_path, const Ref<Image> &p_img);
+ static Vector<uint8_t> save_image_to_buffer(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;