29[[noreturn]]
static void handleError(png_structp png_ptr, png_const_charp error_msg)
31 const auto* operation = std::bit_cast<const char*>(
32 png_get_error_ptr(png_ptr));
33 throw MSXException(
"Error while ", operation,
" PNG: ", error_msg);
36static void handleWarning(png_structp png_ptr, png_const_charp warning_msg)
38 const auto* operation = std::bit_cast<const char*>(
39 png_get_error_ptr(png_ptr));
40 std::cerr <<
"Warning while " << operation <<
" PNG: "
41 << warning_msg <<
'\n';
88 png_destroy_read_struct(&
ptr,
info ? &
info :
nullptr,
nullptr);
94 png_structp
ptr =
nullptr;
98static void readData(png_structp ctx, png_bytep area, png_size_t size)
100 auto* file = std::bit_cast<File*>(png_get_io_ptr(ctx));
101 file->read(std::span{area, size});
111 png.
ptr = png_create_read_struct(
112 PNG_LIBPNG_VER_STRING,
113 const_cast<char*
>(
"decoding"), handleError, handleWarning);
119 png.
info = png_create_info_struct(png.
ptr);
121 throw MSXException(
"Failed to allocate image info struct");
125 png_set_read_fn(png.
ptr, &file, readData);
128 png_read_info(png.
ptr, png.
info);
129 png_uint_32 width, height;
130 int bit_depth, color_type, interlace_type;
131 png_get_IHDR(png.
ptr, png.
info, &width, &height, &bit_depth,
132 &color_type, &interlace_type,
nullptr,
nullptr);
135 png_set_strip_16(png.
ptr);
139 png_set_packing(png.
ptr);
145 png_set_expand(png.
ptr);
148 png_set_filler(png.
ptr, 0xff, PNG_FILLER_AFTER);
154 png_set_gray_to_rgb(png.
ptr);
156 png_read_update_info(png.
ptr, png.
info);
158 png_get_IHDR(png.
ptr, png.
info, &width, &height, &bit_depth,
159 &color_type, &interlace_type,
nullptr,
nullptr);
162 constexpr unsigned MAX_SIZE = 2048;
163 if (width > MAX_SIZE) {
165 "Attempted to create a surface with excessive width: ",
166 width,
", max ", MAX_SIZE);
168 if (height > MAX_SIZE) {
170 "Attempted to create a surface with excessive height: ",
171 height,
", max ", MAX_SIZE);
173 int bpp = png_get_channels(png.
ptr, png.
info) * 8;
174 assert(bpp ==
one_of(24, 32));
178 ((bpp == 32) ? pixelOps.
getAmask() : 0));
181 VLA(png_bytep, rowPointers, height);
182 for (
auto row :
xrange(height)) {
183 rowPointers[row] = std::bit_cast<png_bytep>(
188 png_read_image(png.
ptr, rowPointers.data());
197 "Error while loading PNG file \"", filename,
"\": ",
211 png_destroy_write_struct(&
ptr,
info ? &
info :
nullptr);
217 png_structp
ptr =
nullptr;
221static void writeData(png_structp ctx, png_bytep area, png_size_t size)
223 auto* file = std::bit_cast<File*>(png_get_io_ptr(ctx));
224 file->write(std::span{area, size});
227static void flushData(png_structp ctx)
229 auto* file = std::bit_cast<File*>(png_get_io_ptr(ctx));
233static void IMG_SavePNG_RW(
size_t width, std::span<const void*> rowPointers,
234 const std::string& filename,
bool color)
236 auto height = rowPointers.size();
237 assert(width <= std::numeric_limits<png_uint_32>::max());
238 assert(height <= std::numeric_limits<png_uint_32>::max());
243 png.ptr = png_create_write_struct(
244 PNG_LIBPNG_VER_STRING,
245 const_cast<char*
>(
"encoding"), handleError, handleWarning);
247 throw MSXException(
"Failed to allocate main struct");
251 png.info = png_create_info_struct(png.ptr);
254 throw MSXException(
"Failed to allocate image info struct");
258 png_set_write_fn(png.ptr, &file, writeData, flushData);
262 std::array<png_text, 2> text;
263 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
264 text[0].key =
const_cast<char*
>(
"Software");
265 text[0].text =
const_cast<char*
>(version.c_str());
266 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
267 text[1].key =
const_cast<char*
>(
"Creation Time");
275 static constexpr size_t size = (10 + 1 + 8 + 1) + 44;
276 time_t now = time(
nullptr);
277 struct tm* tm = localtime(&now);
278 std::array<char, size> timeStr;
279 snprintf(timeStr.data(),
sizeof(timeStr),
"%04d-%02d-%02d %02d:%02d:%02d",
280 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
281 tm->tm_hour, tm->tm_min, tm->tm_sec);
282 text[1].text = timeStr.data();
284 png_set_text(png.ptr, png.info, text.data(), narrow<int>(text.size()));
286 png_set_IHDR(png.ptr, png.info,
287 narrow<png_uint_32>(width), narrow<png_uint_32>(height),
289 color ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_GRAY,
290 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
291 PNG_FILTER_TYPE_BASE);
294 png_write_info(png.ptr, png.info);
299 std::bit_cast<png_bytep*>(
const_cast<void**
>(rowPointers.data())));
300 png_write_end(png.ptr, png.info);
301 }
catch (MSXException& e) {
303 "Error while writing PNG file \"", filename,
"\": ",
308static void save(SDL_Surface*
image,
const std::string& filename)
311 Endian::BIG ? SDL_PIXELFORMAT_BGR24 : SDL_PIXELFORMAT_RGB24));
315 VLA(
const void*, row_pointers,
image->h);
317 row_pointers[i] = surf24.getLinePtr(i);
320 IMG_SavePNG_RW(
image->w, row_pointers, filename,
true);
323void saveRGBA(
size_t width, std::span<const void*> rowPointers,
324 const std::string& filename)
327 auto height = narrow<unsigned>(rowPointers.size());
328 static constexpr int bpp = 32;
331 narrow<unsigned>(width), height, bpp,
334 for (
auto y :
xrange(height)) {
336 rowPointers[y], width *
sizeof(uint32_t));
338 save(surface.
get(), filename);
342 const std::string& filename)
344 IMG_SavePNG_RW(width, rowPointers, filename,
false);