1/**************************************************************************/
2/* png_driver_common.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "png_driver_common.h"
32
33#include "core/os/os.h"
34
35#include <png.h>
36#include <string.h>
37
38namespace PNGDriverCommon {
39
40// Print any warnings.
41// On error, set explain and return true.
42// Call should be wrapped in ERR_FAIL_COND
43static bool check_error(const png_image &image) {
44 const png_uint_32 failed = PNG_IMAGE_FAILED(image);
45 if (failed & PNG_IMAGE_ERROR) {
46 return true;
47 } else if (failed) {
48#ifdef TOOLS_ENABLED
49 // suppress this warning, to avoid log spam when opening assetlib
50 const static char *const noisy = "iCCP: known incorrect sRGB profile";
51 const Engine *const eng = Engine::get_singleton();
52 if (eng && eng->is_editor_hint() && !strcmp(image.message, noisy)) {
53 return false;
54 }
55#endif
56 WARN_PRINT(image.message);
57 }
58 return false;
59}
60
61Error png_to_image(const uint8_t *p_source, size_t p_size, bool p_force_linear, Ref<Image> p_image) {
62 png_image png_img;
63 memset(&png_img, 0, sizeof(png_img));
64 png_img.version = PNG_IMAGE_VERSION;
65
66 // fetch image properties
67 int success = png_image_begin_read_from_memory(&png_img, p_source, p_size);
68 ERR_FAIL_COND_V_MSG(check_error(png_img), ERR_FILE_CORRUPT, png_img.message);
69 ERR_FAIL_COND_V(!success, ERR_FILE_CORRUPT);
70
71 // flags to be masked out of input format to give target format
72 const png_uint_32 format_mask = ~(
73 // convert component order to RGBA
74 PNG_FORMAT_FLAG_BGR | PNG_FORMAT_FLAG_AFIRST
75 // convert 16 bit components to 8 bit
76 | PNG_FORMAT_FLAG_LINEAR
77 // convert indexed image to direct color
78 | PNG_FORMAT_FLAG_COLORMAP);
79
80 png_img.format &= format_mask;
81
82 Image::Format dest_format;
83 switch (png_img.format) {
84 case PNG_FORMAT_GRAY:
85 dest_format = Image::FORMAT_L8;
86 break;
87 case PNG_FORMAT_GA:
88 dest_format = Image::FORMAT_LA8;
89 break;
90 case PNG_FORMAT_RGB:
91 dest_format = Image::FORMAT_RGB8;
92 break;
93 case PNG_FORMAT_RGBA:
94 dest_format = Image::FORMAT_RGBA8;
95 break;
96 default:
97 png_image_free(&png_img); // only required when we return before finish_read
98 ERR_PRINT("Unsupported png format.");
99 return ERR_UNAVAILABLE;
100 }
101
102 if (!p_force_linear) {
103 // assume 16 bit pngs without sRGB or gAMA chunks are in sRGB format
104 png_img.flags |= PNG_IMAGE_FLAG_16BIT_sRGB;
105 }
106
107 const png_uint_32 stride = PNG_IMAGE_ROW_STRIDE(png_img);
108 Vector<uint8_t> buffer;
109 Error err = buffer.resize(PNG_IMAGE_BUFFER_SIZE(png_img, stride));
110 if (err) {
111 png_image_free(&png_img); // only required when we return before finish_read
112 return err;
113 }
114 uint8_t *writer = buffer.ptrw();
115
116 // read image data to buffer and release libpng resources
117 success = png_image_finish_read(&png_img, nullptr, writer, stride, nullptr);
118 ERR_FAIL_COND_V_MSG(check_error(png_img), ERR_FILE_CORRUPT, png_img.message);
119 ERR_FAIL_COND_V(!success, ERR_FILE_CORRUPT);
120
121 //print_line("png width: "+itos(png_img.width)+" height: "+itos(png_img.height));
122 p_image->set_data(png_img.width, png_img.height, false, dest_format, buffer);
123
124 return OK;
125}
126
127Error image_to_png(const Ref<Image> &p_image, Vector<uint8_t> &p_buffer) {
128 Ref<Image> source_image = p_image->duplicate();
129
130 if (source_image->is_compressed()) {
131 source_image->decompress();
132 }
133
134 ERR_FAIL_COND_V(source_image->is_compressed(), FAILED);
135
136 png_image png_img;
137 memset(&png_img, 0, sizeof(png_img));
138 png_img.version = PNG_IMAGE_VERSION;
139 png_img.width = source_image->get_width();
140 png_img.height = source_image->get_height();
141
142 switch (source_image->get_format()) {
143 case Image::FORMAT_L8:
144 png_img.format = PNG_FORMAT_GRAY;
145 break;
146 case Image::FORMAT_LA8:
147 png_img.format = PNG_FORMAT_GA;
148 break;
149 case Image::FORMAT_RGB8:
150 png_img.format = PNG_FORMAT_RGB;
151 break;
152 case Image::FORMAT_RGBA8:
153 png_img.format = PNG_FORMAT_RGBA;
154 break;
155 default:
156 if (source_image->detect_alpha()) {
157 source_image->convert(Image::FORMAT_RGBA8);
158 png_img.format = PNG_FORMAT_RGBA;
159 } else {
160 source_image->convert(Image::FORMAT_RGB8);
161 png_img.format = PNG_FORMAT_RGB;
162 }
163 }
164
165 const Vector<uint8_t> image_data = source_image->get_data();
166 const uint8_t *reader = image_data.ptr();
167
168 // we may be passed a buffer with existing content we're expected to append to
169 const int buffer_offset = p_buffer.size();
170
171 const size_t png_size_estimate = PNG_IMAGE_PNG_SIZE_MAX(png_img);
172
173 // try with estimated size
174 size_t compressed_size = png_size_estimate;
175 int success = 0;
176 { // scope writer lifetime
177 Error err = p_buffer.resize(buffer_offset + png_size_estimate);
178 ERR_FAIL_COND_V(err, err);
179
180 uint8_t *writer = p_buffer.ptrw();
181 success = png_image_write_to_memory(&png_img, &writer[buffer_offset],
182 &compressed_size, 0, reader, 0, nullptr);
183 ERR_FAIL_COND_V_MSG(check_error(png_img), FAILED, png_img.message);
184 }
185 if (!success) {
186 // buffer was big enough, must be some other error
187 ERR_FAIL_COND_V(compressed_size <= png_size_estimate, FAILED);
188
189 // write failed due to buffer size, resize and retry
190 Error err = p_buffer.resize(buffer_offset + compressed_size);
191 ERR_FAIL_COND_V(err, err);
192
193 uint8_t *writer = p_buffer.ptrw();
194 success = png_image_write_to_memory(&png_img, &writer[buffer_offset],
195 &compressed_size, 0, reader, 0, nullptr);
196 ERR_FAIL_COND_V_MSG(check_error(png_img), FAILED, png_img.message);
197 ERR_FAIL_COND_V(!success, FAILED);
198 }
199
200 // trim buffer size to content
201 Error err = p_buffer.resize(buffer_offset + compressed_size);
202 ERR_FAIL_COND_V(err, err);
203
204 return OK;
205}
206} // namespace PNGDriverCommon
207