2019-06-16 21:08:52 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* png_driver_common.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2019-06-16 21:08:52 +02: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "png_driver_common.h"
|
|
|
|
|
|
|
|
#include "core/os/os.h"
|
|
|
|
|
|
|
|
#include <png.h>
|
|
|
|
#include <string.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;
|
|
|
|
}
|
|
|
|
|
2020-07-09 20:59:38 +02:00
|
|
|
Error png_to_image(const uint8_t *p_source, size_t p_size, bool p_force_linear, Ref<Image> p_image) {
|
2019-06-16 21:08:52 +02:00
|
|
|
png_image png_img;
|
|
|
|
zeromem(&png_img, 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);
|
2019-11-11 10:24:04 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(check_error(png_img), ERR_FILE_CORRUPT, png_img.message);
|
2019-06-16 21:08:52 +02:00
|
|
|
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
|
2019-08-15 04:57:49 +02:00
|
|
|
ERR_PRINT("Unsupported png format.");
|
2019-06-16 21:08:52 +02:00
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2020-07-09 20:59:38 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:08:52 +02:00
|
|
|
const png_uint_32 stride = PNG_IMAGE_ROW_STRIDE(png_img);
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> buffer;
|
2019-06-16 21:08:52 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *writer = buffer.ptrw();
|
2019-06-16 21:08:52 +02:00
|
|
|
|
|
|
|
// read image data to buffer and release libpng resources
|
2020-04-02 01:20:12 +02:00
|
|
|
success = png_image_finish_read(&png_img, nullptr, writer, stride, nullptr);
|
2019-11-11 10:24:04 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(check_error(png_img), ERR_FILE_CORRUPT, png_img.message);
|
2019-06-16 21:08:52 +02:00
|
|
|
ERR_FAIL_COND_V(!success, ERR_FILE_CORRUPT);
|
|
|
|
|
2019-10-31 23:54:21 +01:00
|
|
|
//print_line("png width: "+itos(png_img.width)+" height: "+itos(png_img.height));
|
2020-05-14 11:00:19 +02:00
|
|
|
p_image->create(png_img.width, png_img.height, false, dest_format, buffer);
|
2019-06-16 21:08:52 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Error image_to_png(const Ref<Image> &p_image, Vector<uint8_t> &p_buffer) {
|
2019-06-16 21:08:52 +02:00
|
|
|
Ref<Image> source_image = p_image->duplicate();
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (source_image->is_compressed()) {
|
2019-06-16 21:08:52 +02:00
|
|
|
source_image->decompress();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-06-16 21:08:52 +02:00
|
|
|
|
|
|
|
ERR_FAIL_COND_V(source_image->is_compressed(), FAILED);
|
|
|
|
|
|
|
|
png_image png_img;
|
|
|
|
zeromem(&png_img, 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()) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
const Vector<uint8_t> image_data = source_image->get_data();
|
|
|
|
const uint8_t *reader = image_data.ptr();
|
2019-06-16 21:08:52 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *writer = p_buffer.ptrw();
|
2019-06-16 21:08:52 +02:00
|
|
|
success = png_image_write_to_memory(&png_img, &writer[buffer_offset],
|
2020-04-02 01:20:12 +02:00
|
|
|
&compressed_size, 0, reader, 0, nullptr);
|
2019-11-11 10:24:04 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(check_error(png_img), FAILED, png_img.message);
|
2019-06-16 21:08:52 +02:00
|
|
|
}
|
|
|
|
if (!success) {
|
2019-08-15 04:57:49 +02:00
|
|
|
// buffer was big enough, must be some other error
|
|
|
|
ERR_FAIL_COND_V(compressed_size <= png_size_estimate, FAILED);
|
2019-06-16 21:08:52 +02:00
|
|
|
|
|
|
|
// write failed due to buffer size, resize and retry
|
|
|
|
Error err = p_buffer.resize(buffer_offset + compressed_size);
|
|
|
|
ERR_FAIL_COND_V(err, err);
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *writer = p_buffer.ptrw();
|
2019-06-16 21:08:52 +02:00
|
|
|
success = png_image_write_to_memory(&png_img, &writer[buffer_offset],
|
2020-04-02 01:20:12 +02:00
|
|
|
&compressed_size, 0, reader, 0, nullptr);
|
2019-11-11 10:24:04 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(check_error(png_img), FAILED, png_img.message);
|
2019-06-16 21:08:52 +02:00
|
|
|
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
|