|
| 1 | +// mcpplibs.mwrap.wuffs, installed: Wuffs through the module alone. |
| 2 | +import std; |
| 3 | +import mcpplibs.mwrap.wuffs; |
| 4 | + |
| 5 | +using namespace mcpplibs::mwrap::wuffs; // the adapted static inline API |
| 6 | +using namespace mcpplibs::mwrap::wuffs::macros; // WUFFS_VERSION, WUFFS_BASE__FOURCC__PNG … |
| 7 | + |
| 8 | +namespace { |
| 9 | +// 2x2 RGB PNG: red, green / blue, white. |
| 10 | +constexpr std::uint8_t png[] = { |
| 11 | + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, |
| 12 | + 0x00, 0x00, 0x00, 0x02, 0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a, 0x73, 0x00, 0x00, 0x00, 0x12, 0x49, 0x44, 0x41, |
| 13 | + 0x54, 0x78, 0xda, 0x63, 0xf8, 0xcf, 0xc0, 0xc0, 0x00, 0xc2, 0x0c, 0xff, 0x81, 0x00, 0x00, 0x1f, 0xee, 0x05, 0xfb, 0xf1, |
| 14 | + 0xab, 0xba, 0x77, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82}; |
| 15 | +// 1x1 GIF, a red pixel. |
| 16 | +constexpr std::uint8_t gif[] = {0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0x00, 0x00, 0xff, 0x00, 0x00, |
| 17 | + 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, |
| 18 | + 0x01, 0x00, 0x3b}; |
| 19 | + |
| 20 | +int failures = 0; |
| 21 | +void check(bool ok, std::string_view what) { |
| 22 | + std::println("{} {}", ok ? "ok " : "FAIL", what); |
| 23 | + failures += !ok; |
| 24 | +} |
| 25 | + |
| 26 | +std::vector<std::uint32_t> pixels(const std::uint8_t* p, std::size_t n, std::string& error) { |
| 27 | + wuffs_aux::DecodeImageCallbacks callbacks; |
| 28 | + wuffs_aux::sync_io::MemoryInput input(p, n); |
| 29 | + wuffs_aux::DecodeImageResult res = wuffs_aux::DecodeImage(callbacks, input); |
| 30 | + error = res.error_message; |
| 31 | + std::vector<std::uint32_t> out; |
| 32 | + if (!error.empty()) return out; |
| 33 | + wuffs_base__table_u8 plane = res.pixbuf.plane(0); |
| 34 | + for (std::uint32_t y = 0; y < res.pixbuf.pixcfg.height(); ++y) |
| 35 | + for (std::uint32_t x = 0; x < res.pixbuf.pixcfg.width(); ++x) |
| 36 | + out.push_back(wuffs_base__peek_u32le__no_bounds_check(plane.ptr + y * plane.stride + x * 4)); |
| 37 | + return out; |
| 38 | +} |
| 39 | +} // namespace |
| 40 | + |
| 41 | +int main() { |
| 42 | + std::string error; |
| 43 | + // BGRA little-endian u32 = 0xAARRGGBB. |
| 44 | + check(pixels(png, sizeof png, error) == std::vector<std::uint32_t>{0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF}, |
| 45 | + "PNG decoded pixel by pixel"); |
| 46 | + check(pixels(gif, sizeof gif, error) == std::vector<std::uint32_t>{0xFFFF0000}, "GIF decoded"); |
| 47 | + pixels(png, 40, error); |
| 48 | + check(error == "png: truncated input", "a truncated PNG is refused: " + error); |
| 49 | + check(wuffs_base__magic_number_guess_fourcc(wuffs_base__make_slice_u8(const_cast<std::uint8_t*>(png), sizeof png), true) == |
| 50 | + WUFFS_BASE__FOURCC__PNG, |
| 51 | + "magic number"); |
| 52 | + |
| 53 | + const std::string doc = R"({"a":[1,2,3]})"; |
| 54 | + struct Count : wuffs_aux::DecodeJsonCallbacks { |
| 55 | + std::int64_t sum = 0; |
| 56 | + std::string AppendNull() override { return ""; } |
| 57 | + std::string AppendBool(bool) override { return ""; } |
| 58 | + std::string AppendF64(double) override { return ""; } |
| 59 | + std::string AppendI64(std::int64_t v) override { sum += v; return ""; } |
| 60 | + std::string AppendTextString(std::string&&) override { return ""; } |
| 61 | + std::string Push(std::uint32_t) override { return ""; } |
| 62 | + std::string Pop(std::uint32_t) override { return ""; } |
| 63 | + } count; |
| 64 | + wuffs_aux::sync_io::MemoryInput input(doc.data(), doc.size()); |
| 65 | + check(wuffs_aux::DecodeJson(count, input).error_message.empty() && count.sum == 6, "JSON walked"); |
| 66 | + check(WUFFS_VERSION_MINOR == 3 && std::string_view(WUFFS_VERSION_STRING).starts_with("0.3.6"), "the release"); |
| 67 | + return failures ? 1 : 0; |
| 68 | +} |
0 commit comments