initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
75
drivers/png/SCsub
Normal file
75
drivers/png/SCsub
Normal file
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env python
|
||||
from misc.utility.scons_hints import *
|
||||
|
||||
Import("env")
|
||||
|
||||
env_png = env.Clone()
|
||||
|
||||
# Thirdparty source files
|
||||
|
||||
thirdparty_obj = []
|
||||
|
||||
if env["builtin_libpng"]:
|
||||
thirdparty_dir = "#thirdparty/libpng/"
|
||||
thirdparty_sources = [
|
||||
"png.c",
|
||||
"pngerror.c",
|
||||
"pngget.c",
|
||||
"pngmem.c",
|
||||
"pngpread.c",
|
||||
"pngread.c",
|
||||
"pngrio.c",
|
||||
"pngrtran.c",
|
||||
"pngrutil.c",
|
||||
"pngset.c",
|
||||
"pngtrans.c",
|
||||
"pngwio.c",
|
||||
"pngwrite.c",
|
||||
"pngwtran.c",
|
||||
"pngwutil.c",
|
||||
]
|
||||
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
|
||||
|
||||
env_png.Prepend(CPPEXTPATH=[thirdparty_dir])
|
||||
# Needed for drivers includes and in platform/web.
|
||||
env.Prepend(CPPEXTPATH=[thirdparty_dir])
|
||||
|
||||
env_thirdparty = env_png.Clone()
|
||||
env_thirdparty.disable_warnings()
|
||||
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources)
|
||||
|
||||
if env["arch"].startswith("arm"):
|
||||
if env.msvc: # Can't compile assembly files with MSVC.
|
||||
env_thirdparty.Append(CPPDEFINES=[("PNG_ARM_NEON_OPT", 0)])
|
||||
else:
|
||||
env_neon = env_thirdparty.Clone()
|
||||
if "S_compiler" in env:
|
||||
env_neon["CC"] = env["S_compiler"]
|
||||
neon_sources = []
|
||||
neon_sources.append(env_neon.Object(thirdparty_dir + "arm/arm_init.c"))
|
||||
neon_sources.append(env_neon.Object(thirdparty_dir + "arm/filter_neon_intrinsics.c"))
|
||||
neon_sources.append(env_neon.Object(thirdparty_dir + "arm/palette_neon_intrinsics.c"))
|
||||
thirdparty_obj += neon_sources
|
||||
elif env["arch"].startswith("x86"):
|
||||
env_thirdparty.Append(CPPDEFINES=["PNG_INTEL_SSE"])
|
||||
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "intel/intel_init.c")
|
||||
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "intel/filter_sse2_intrinsics.c")
|
||||
elif env["arch"] == "loongarch64":
|
||||
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "loongarch/loongarch_lsx_init.c")
|
||||
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "loongarch/filter_lsx_intrinsics.c")
|
||||
elif env["arch"] == "ppc64":
|
||||
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "powerpc/powerpc_init.c")
|
||||
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_dir + "powerpc/filter_vsx_intrinsics.c")
|
||||
|
||||
env.drivers_sources += thirdparty_obj
|
||||
|
||||
|
||||
# Godot source files
|
||||
|
||||
driver_obj = []
|
||||
|
||||
env_png.add_source_files(driver_obj, "*.cpp")
|
||||
env.drivers_sources += driver_obj
|
||||
|
||||
# Needed to force rebuilding the driver files when the thirdparty library is updated.
|
||||
env.Depends(driver_obj, thirdparty_obj)
|
103
drivers/png/image_loader_png.cpp
Normal file
103
drivers/png/image_loader_png.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/**************************************************************************/
|
||||
/* image_loader_png.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "image_loader_png.h"
|
||||
|
||||
#include "drivers/png/png_driver_common.h"
|
||||
|
||||
Error ImageLoaderPNG::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
|
||||
const uint64_t buffer_size = f->get_length();
|
||||
Vector<uint8_t> file_buffer;
|
||||
Error err = file_buffer.resize(buffer_size);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
{
|
||||
uint8_t *writer = file_buffer.ptrw();
|
||||
f->get_buffer(writer, buffer_size);
|
||||
}
|
||||
const uint8_t *reader = file_buffer.ptr();
|
||||
return PNGDriverCommon::png_to_image(reader, buffer_size, p_flags & FLAG_FORCE_LINEAR, p_image);
|
||||
}
|
||||
|
||||
void ImageLoaderPNG::get_recognized_extensions(List<String> *p_extensions) const {
|
||||
p_extensions->push_back("png");
|
||||
}
|
||||
|
||||
Ref<Image> ImageLoaderPNG::load_mem_png(const uint8_t *p_png, int p_size) {
|
||||
Ref<Image> img;
|
||||
img.instantiate();
|
||||
|
||||
// the value of p_force_linear does not matter since it only applies to 16 bit
|
||||
Error err = PNGDriverCommon::png_to_image(p_png, p_size, false, img);
|
||||
ERR_FAIL_COND_V(err, Ref<Image>());
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
Ref<Image> ImageLoaderPNG::unpack_mem_png(const uint8_t *p_png, int p_size) {
|
||||
ERR_FAIL_COND_V(p_size < 4, Ref<Image>());
|
||||
ERR_FAIL_COND_V(p_png[0] != 'P' || p_png[1] != 'N' || p_png[2] != 'G' || p_png[3] != ' ', Ref<Image>());
|
||||
return load_mem_png(&p_png[4], p_size - 4);
|
||||
}
|
||||
|
||||
Ref<Image> ImageLoaderPNG::lossless_unpack_png(const Vector<uint8_t> &p_data) {
|
||||
return unpack_mem_png(p_data.ptr(), p_data.size());
|
||||
}
|
||||
|
||||
Vector<uint8_t> ImageLoaderPNG::lossless_pack_png(const Ref<Image> &p_image) {
|
||||
Vector<uint8_t> out_buffer;
|
||||
|
||||
// add Godot's own "PNG " prefix
|
||||
if (out_buffer.resize(4) != OK) {
|
||||
ERR_FAIL_V(Vector<uint8_t>());
|
||||
}
|
||||
|
||||
// scope for writer lifetime
|
||||
{
|
||||
// must be closed before call to image_to_png
|
||||
uint8_t *writer = out_buffer.ptrw();
|
||||
memcpy(writer, "PNG ", 4);
|
||||
}
|
||||
|
||||
Error err = PNGDriverCommon::image_to_png(p_image, out_buffer);
|
||||
if (err) {
|
||||
ERR_FAIL_V(Vector<uint8_t>());
|
||||
}
|
||||
|
||||
return out_buffer;
|
||||
}
|
||||
|
||||
ImageLoaderPNG::ImageLoaderPNG() {
|
||||
Image::_png_mem_loader_func = load_mem_png;
|
||||
Image::_png_mem_unpacker_func = unpack_mem_png;
|
||||
Image::png_unpacker = lossless_unpack_png;
|
||||
Image::png_packer = lossless_pack_png;
|
||||
}
|
46
drivers/png/image_loader_png.h
Normal file
46
drivers/png/image_loader_png.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/**************************************************************************/
|
||||
/* image_loader_png.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/io/image_loader.h"
|
||||
|
||||
class ImageLoaderPNG : public ImageFormatLoader {
|
||||
private:
|
||||
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> unpack_mem_png(const uint8_t *p_png, int p_size);
|
||||
static Ref<Image> load_mem_png(const uint8_t *p_png, int p_size);
|
||||
|
||||
public:
|
||||
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale);
|
||||
virtual void get_recognized_extensions(List<String> *p_extensions) const;
|
||||
ImageLoaderPNG();
|
||||
};
|
205
drivers/png/png_driver_common.cpp
Normal file
205
drivers/png/png_driver_common.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
/**************************************************************************/
|
||||
/* png_driver_common.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "png_driver_common.h"
|
||||
|
||||
#include "core/config/engine.h"
|
||||
|
||||
#include <png.h>
|
||||
|
||||
namespace PNGDriverCommon {
|
||||
|
||||
// Print any warnings.
|
||||
// On error, set explain and return true.
|
||||
// Call should be wrapped in ERR_FAIL_COND
|
||||
static bool check_error(const png_image &image) {
|
||||
const png_uint_32 failed = PNG_IMAGE_FAILED(image);
|
||||
if (failed & PNG_IMAGE_ERROR) {
|
||||
return true;
|
||||
} else if (failed) {
|
||||
#ifdef TOOLS_ENABLED
|
||||
// suppress this warning, to avoid log spam when opening assetlib
|
||||
const static char *const noisy = "iCCP: known incorrect sRGB profile";
|
||||
const Engine *const eng = Engine::get_singleton();
|
||||
if (eng && eng->is_editor_hint() && !strcmp(image.message, noisy)) {
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
WARN_PRINT(image.message);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Error png_to_image(const uint8_t *p_source, size_t p_size, bool p_force_linear, Ref<Image> p_image) {
|
||||
png_image png_img;
|
||||
memset(&png_img, 0, sizeof(png_img));
|
||||
png_img.version = PNG_IMAGE_VERSION;
|
||||
|
||||
// fetch image properties
|
||||
int success = png_image_begin_read_from_memory(&png_img, p_source, p_size);
|
||||
ERR_FAIL_COND_V_MSG(check_error(png_img), ERR_FILE_CORRUPT, png_img.message);
|
||||
ERR_FAIL_COND_V(!success, ERR_FILE_CORRUPT);
|
||||
|
||||
// flags to be masked out of input format to give target format
|
||||
const png_uint_32 format_mask = ~(
|
||||
// convert component order to RGBA
|
||||
PNG_FORMAT_FLAG_BGR | PNG_FORMAT_FLAG_AFIRST
|
||||
// convert 16 bit components to 8 bit
|
||||
| PNG_FORMAT_FLAG_LINEAR
|
||||
// convert indexed image to direct color
|
||||
| PNG_FORMAT_FLAG_COLORMAP);
|
||||
|
||||
png_img.format &= format_mask;
|
||||
|
||||
Image::Format dest_format;
|
||||
switch (png_img.format) {
|
||||
case PNG_FORMAT_GRAY:
|
||||
dest_format = Image::FORMAT_L8;
|
||||
break;
|
||||
case PNG_FORMAT_GA:
|
||||
dest_format = Image::FORMAT_LA8;
|
||||
break;
|
||||
case PNG_FORMAT_RGB:
|
||||
dest_format = Image::FORMAT_RGB8;
|
||||
break;
|
||||
case PNG_FORMAT_RGBA:
|
||||
dest_format = Image::FORMAT_RGBA8;
|
||||
break;
|
||||
default:
|
||||
png_image_free(&png_img); // only required when we return before finish_read
|
||||
ERR_PRINT("Unsupported png format.");
|
||||
return ERR_UNAVAILABLE;
|
||||
}
|
||||
|
||||
if (!p_force_linear) {
|
||||
// assume 16 bit pngs without sRGB or gAMA chunks are in sRGB format
|
||||
png_img.flags |= PNG_IMAGE_FLAG_16BIT_sRGB;
|
||||
}
|
||||
|
||||
const png_uint_32 stride = PNG_IMAGE_ROW_STRIDE(png_img);
|
||||
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;
|
||||
}
|
||||
uint8_t *writer = buffer.ptrw();
|
||||
|
||||
// read image data to buffer and release libpng resources
|
||||
success = png_image_finish_read(&png_img, nullptr, writer, stride, nullptr);
|
||||
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->set_data(png_img.width, png_img.height, false, dest_format, buffer);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error image_to_png(const Ref<Image> &p_image, Vector<uint8_t> &p_buffer) {
|
||||
Ref<Image> source_image = p_image->duplicate();
|
||||
|
||||
if (source_image->is_compressed()) {
|
||||
source_image->decompress();
|
||||
}
|
||||
|
||||
ERR_FAIL_COND_V(source_image->is_compressed(), FAILED);
|
||||
|
||||
png_image png_img;
|
||||
memset(&png_img, 0, sizeof(png_img));
|
||||
png_img.version = PNG_IMAGE_VERSION;
|
||||
png_img.width = source_image->get_width();
|
||||
png_img.height = source_image->get_height();
|
||||
|
||||
switch (source_image->get_format()) {
|
||||
case Image::FORMAT_L8:
|
||||
png_img.format = PNG_FORMAT_GRAY;
|
||||
break;
|
||||
case Image::FORMAT_LA8:
|
||||
png_img.format = PNG_FORMAT_GA;
|
||||
break;
|
||||
case Image::FORMAT_RGB8:
|
||||
png_img.format = PNG_FORMAT_RGB;
|
||||
break;
|
||||
case Image::FORMAT_RGBA8:
|
||||
png_img.format = PNG_FORMAT_RGBA;
|
||||
break;
|
||||
default:
|
||||
if (source_image->detect_alpha() != Image::ALPHA_NONE) {
|
||||
source_image->convert(Image::FORMAT_RGBA8);
|
||||
png_img.format = PNG_FORMAT_RGBA;
|
||||
} else {
|
||||
source_image->convert(Image::FORMAT_RGB8);
|
||||
png_img.format = PNG_FORMAT_RGB;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
const size_t png_size_estimate = PNG_IMAGE_PNG_SIZE_MAX(png_img);
|
||||
|
||||
// try with estimated size
|
||||
size_t compressed_size = png_size_estimate;
|
||||
int success = 0;
|
||||
{ // scope writer lifetime
|
||||
Error err = p_buffer.resize(buffer_offset + png_size_estimate);
|
||||
ERR_FAIL_COND_V(err, err);
|
||||
|
||||
uint8_t *writer = p_buffer.ptrw();
|
||||
success = png_image_write_to_memory(&png_img, &writer[buffer_offset],
|
||||
&compressed_size, 0, reader, 0, nullptr);
|
||||
ERR_FAIL_COND_V_MSG(check_error(png_img), FAILED, png_img.message);
|
||||
}
|
||||
if (!success) {
|
||||
// buffer was big enough, must be some other error
|
||||
ERR_FAIL_COND_V(compressed_size <= png_size_estimate, FAILED);
|
||||
|
||||
// write failed due to buffer size, resize and retry
|
||||
Error err = p_buffer.resize(buffer_offset + compressed_size);
|
||||
ERR_FAIL_COND_V(err, err);
|
||||
|
||||
uint8_t *writer = p_buffer.ptrw();
|
||||
success = png_image_write_to_memory(&png_img, &writer[buffer_offset],
|
||||
&compressed_size, 0, reader, 0, nullptr);
|
||||
ERR_FAIL_COND_V_MSG(check_error(png_img), FAILED, png_img.message);
|
||||
ERR_FAIL_COND_V(!success, FAILED);
|
||||
}
|
||||
|
||||
// trim buffer size to content
|
||||
Error err = p_buffer.resize(buffer_offset + compressed_size);
|
||||
ERR_FAIL_COND_V(err, err);
|
||||
|
||||
return OK;
|
||||
}
|
||||
} // namespace PNGDriverCommon
|
43
drivers/png/png_driver_common.h
Normal file
43
drivers/png/png_driver_common.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/**************************************************************************/
|
||||
/* png_driver_common.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/io/image.h"
|
||||
|
||||
namespace PNGDriverCommon {
|
||||
|
||||
// Attempt to load png from buffer (p_source, p_size) into p_image
|
||||
Error png_to_image(const uint8_t *p_source, size_t p_size, bool p_force_linear, 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, Vector<uint8_t> &p_buffer);
|
||||
} // namespace PNGDriverCommon
|
88
drivers/png/resource_saver_png.cpp
Normal file
88
drivers/png/resource_saver_png.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
/**************************************************************************/
|
||||
/* resource_saver_png.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "resource_saver_png.h"
|
||||
|
||||
#include "core/io/file_access.h"
|
||||
#include "core/io/image.h"
|
||||
#include "drivers/png/png_driver_common.h"
|
||||
#include "scene/resources/image_texture.h"
|
||||
|
||||
Error ResourceSaverPNG::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
|
||||
Ref<ImageTexture> texture = p_resource;
|
||||
|
||||
ERR_FAIL_COND_V_MSG(texture.is_null(), ERR_INVALID_PARAMETER, "Can't save invalid texture as PNG.");
|
||||
ERR_FAIL_COND_V_MSG(!texture->get_width(), ERR_INVALID_PARAMETER, "Can't save empty texture as PNG.");
|
||||
|
||||
Ref<Image> img = texture->get_image();
|
||||
|
||||
Error err = save_image(p_path, img);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
Error ResourceSaverPNG::save_image(const String &p_path, const Ref<Image> &p_img) {
|
||||
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.");
|
||||
Ref<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));
|
||||
|
||||
const uint8_t *reader = buffer.ptr();
|
||||
|
||||
file->store_buffer(reader, buffer.size());
|
||||
if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
|
||||
return ERR_CANT_CREATE;
|
||||
}
|
||||
|
||||
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 Ref<Resource> &p_resource) const {
|
||||
return (p_resource.is_valid() && p_resource->is_class("ImageTexture"));
|
||||
}
|
||||
|
||||
void ResourceSaverPNG::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
|
||||
if (Object::cast_to<ImageTexture>(*p_resource)) {
|
||||
p_extensions->push_back("png");
|
||||
}
|
||||
}
|
||||
|
||||
ResourceSaverPNG::ResourceSaverPNG() {
|
||||
Image::save_png_func = &save_image;
|
||||
Image::save_png_buffer_func = &save_image_to_buffer;
|
||||
}
|
46
drivers/png/resource_saver_png.h
Normal file
46
drivers/png/resource_saver_png.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/**************************************************************************/
|
||||
/* resource_saver_png.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/io/image.h"
|
||||
#include "core/io/resource_saver.h"
|
||||
|
||||
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 Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0) override;
|
||||
virtual bool recognize(const Ref<Resource> &p_resource) const override;
|
||||
virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const override;
|
||||
|
||||
ResourceSaverPNG();
|
||||
};
|
Reference in New Issue
Block a user