|
| 1 | +#include <csetjmp> // setjmp, for png_jmpbuf |
| 2 | + |
| 3 | +import std; |
| 4 | +import mcpplibs.mwrap.libpng; |
| 5 | + |
| 6 | +using namespace mcpplibs::mwrap::libpng::macros; |
| 7 | + |
| 8 | +namespace { |
| 9 | +struct Source { |
| 10 | + const png_byte* data; |
| 11 | + std::size_t size, at = 0; |
| 12 | +}; |
| 13 | +void read_from(png_structp png, png_bytep out, std::size_t n) { |
| 14 | + auto* s = static_cast<Source*>(png_get_io_ptr(png)); |
| 15 | + if (s->at + n > s->size) png_error(png, "truncated"); |
| 16 | + std::memcpy(out, s->data + s->at, n); |
| 17 | + s->at += n; |
| 18 | +} |
| 19 | +// Expand to RGBA and read every row; png_error longjmps back here. |
| 20 | +int read_rgba(png_structp png, png_infop info, png_bytep rows, png_uint_32 stride) { |
| 21 | + if (setjmp(png_jmpbuf(png))) return -1; |
| 22 | + png_read_info(png, info); |
| 23 | + png_set_expand(png); |
| 24 | + png_set_filler(png, 0xff, PNG_FILLER_AFTER); |
| 25 | + png_read_update_info(png, info); |
| 26 | + for (png_uint_32 y = 0; y < png_get_image_height(png, info); ++y) png_read_row(png, rows + y * stride, nullptr); |
| 27 | + png_read_end(png, nullptr); |
| 28 | + return 0; |
| 29 | +} |
| 30 | +} // namespace |
| 31 | + |
| 32 | +int main() { |
| 33 | + static_assert(PNG_SIMPLIFIED_READ_SUPPORTED && PNG_SETJMP_SUPPORTED && PNG_LIBPNG_VER == 10643); |
| 34 | + // A grey ramp written with the simplified API, to memory. |
| 35 | + constexpr png_uint_32 w = 9, h = 4; |
| 36 | + png_image image{}; |
| 37 | + image.version = PNG_IMAGE_VERSION; |
| 38 | + image.width = w; |
| 39 | + image.height = h; |
| 40 | + image.format = PNG_FORMAT_GRAY; |
| 41 | + std::vector<png_byte> grey(PNG_IMAGE_SIZE(image)); |
| 42 | + for (std::size_t i = 0; i < grey.size(); ++i) grey[i] = static_cast<png_byte>(i * 7); |
| 43 | + png_alloc_size_t size = 0; |
| 44 | + if (!png_image_write_get_memory_size(image, size, 0, grey.data(), 0, nullptr)) return 1; |
| 45 | + std::vector<png_byte> file(size); |
| 46 | + if (!png_image_write_to_memory(&image, file.data(), &size, 0, grey.data(), 0, nullptr)) return 2; |
| 47 | + if (!png_check_sig(file.data(), 8)) return 3; |
| 48 | + // Read it back with the low-level reader, expanded to RGBA. |
| 49 | + std::vector<png_byte> rgba(w * h * 4); |
| 50 | + Source source{file.data(), size}; |
| 51 | + png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 52 | + png_infop info = png_create_info_struct(png); |
| 53 | + png_set_read_fn(png, &source, read_from); |
| 54 | + png_set_gray_to_rgb(png); |
| 55 | + const int status = read_rgba(png, info, rgba.data(), w * 4); |
| 56 | + png_destroy_read_struct(&png, &info, nullptr); |
| 57 | + if (status != 0) return 4; |
| 58 | + for (std::size_t p = 0; p < grey.size(); ++p) |
| 59 | + if (rgba[p * 4] != grey[p] || rgba[p * 4 + 2] != grey[p] || rgba[p * 4 + 3] != 255) return 5; |
| 60 | + // A truncated file: png_error in the read callback, longjmp to read_rgba. |
| 61 | + Source cut{file.data(), size / 2}; |
| 62 | + png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 63 | + info = png_create_info_struct(png); |
| 64 | + png_set_read_fn(png, &cut, read_from); |
| 65 | + const int failed = read_rgba(png, info, rgba.data(), w * 4); |
| 66 | + png_destroy_read_struct(&png, &info, nullptr); |
| 67 | + if (failed != -1) return 6; |
| 68 | + std::println("libpng-image: ok (libpng {}, {} bytes for {}x{})", png_libpng_ver, size, w, h); |
| 69 | + return 0; |
| 70 | +} |
0 commit comments