102 lines
3.9 KiB
C++
102 lines
3.9 KiB
C++
#include "assets/image_pixels.h"
|
|
#include "test_harness.h"
|
|
|
|
#include <array>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
using pp::assets::decode_png_rgba8;
|
|
using pp::assets::encode_png_rgba8;
|
|
using pp::foundation::StatusCode;
|
|
|
|
namespace {
|
|
|
|
constexpr std::array<std::byte, 68> transparent_png_1x1 {
|
|
std::byte { 0x89 }, std::byte { 0x50 }, std::byte { 0x4e }, std::byte { 0x47 },
|
|
std::byte { 0x0d }, std::byte { 0x0a }, std::byte { 0x1a }, std::byte { 0x0a },
|
|
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x0d },
|
|
std::byte { 0x49 }, std::byte { 0x48 }, std::byte { 0x44 }, std::byte { 0x52 },
|
|
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x01 },
|
|
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x01 },
|
|
std::byte { 0x08 }, std::byte { 0x06 }, std::byte { 0x00 }, std::byte { 0x00 },
|
|
std::byte { 0x00 }, std::byte { 0x1f }, std::byte { 0x15 }, std::byte { 0xc4 },
|
|
std::byte { 0x89 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 },
|
|
std::byte { 0x0b }, std::byte { 0x49 }, std::byte { 0x44 }, std::byte { 0x41 },
|
|
std::byte { 0x54 }, std::byte { 0x78 }, std::byte { 0x9c }, std::byte { 0x63 },
|
|
std::byte { 0x60 }, std::byte { 0x00 }, std::byte { 0x02 }, std::byte { 0x00 },
|
|
std::byte { 0x00 }, std::byte { 0x05 }, std::byte { 0x00 }, std::byte { 0x01 },
|
|
std::byte { 0x7a }, std::byte { 0x5e }, std::byte { 0xab }, std::byte { 0x3f },
|
|
std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 }, std::byte { 0x00 },
|
|
std::byte { 0x49 }, std::byte { 0x45 }, std::byte { 0x4e }, std::byte { 0x44 },
|
|
std::byte { 0xae }, std::byte { 0x42 }, std::byte { 0x60 }, std::byte { 0x82 },
|
|
};
|
|
|
|
void decodes_png_to_rgba8_pixels(pp::tests::Harness& h)
|
|
{
|
|
const auto image = decode_png_rgba8(transparent_png_1x1);
|
|
|
|
PP_EXPECT(h, image.ok());
|
|
PP_EXPECT(h, image.value().width == 1U);
|
|
PP_EXPECT(h, image.value().height == 1U);
|
|
PP_EXPECT(h, image.value().pixels.size() == 4U);
|
|
PP_EXPECT(h, image.value().pixels[0] == 0U);
|
|
PP_EXPECT(h, image.value().pixels[1] == 0U);
|
|
PP_EXPECT(h, image.value().pixels[2] == 0U);
|
|
PP_EXPECT(h, image.value().pixels[3] == 0U);
|
|
}
|
|
|
|
void rejects_corrupt_png_payload(pp::tests::Harness& h)
|
|
{
|
|
auto corrupt = transparent_png_1x1;
|
|
corrupt[0] = std::byte { 0x00 };
|
|
|
|
const auto image = decode_png_rgba8(corrupt);
|
|
|
|
PP_EXPECT(h, !image.ok());
|
|
PP_EXPECT(h, image.status().code == StatusCode::invalid_argument);
|
|
}
|
|
|
|
void encodes_rgba8_pixels_to_decodable_png(pp::tests::Harness& h)
|
|
{
|
|
const std::vector<std::uint8_t> pixels {
|
|
255, 0, 0, 255,
|
|
0, 255, 0, 128,
|
|
};
|
|
|
|
const auto encoded = encode_png_rgba8(2, 1, pixels);
|
|
|
|
PP_EXPECT(h, encoded.ok());
|
|
const auto decoded = decode_png_rgba8(encoded.value());
|
|
PP_EXPECT(h, decoded.ok());
|
|
PP_EXPECT(h, decoded.value().width == 2U);
|
|
PP_EXPECT(h, decoded.value().height == 1U);
|
|
PP_EXPECT(h, decoded.value().pixels == pixels);
|
|
}
|
|
|
|
void rejects_invalid_png_encode_inputs(pp::tests::Harness& h)
|
|
{
|
|
const std::vector<std::uint8_t> pixels { 0, 0, 0, 0 };
|
|
|
|
const auto no_size = encode_png_rgba8(0, 1, pixels);
|
|
const auto wrong_payload_size = encode_png_rgba8(2, 1, pixels);
|
|
|
|
PP_EXPECT(h, !no_size.ok());
|
|
PP_EXPECT(h, no_size.status().code == StatusCode::invalid_argument);
|
|
PP_EXPECT(h, !wrong_payload_size.ok());
|
|
PP_EXPECT(h, wrong_payload_size.status().code == StatusCode::invalid_argument);
|
|
}
|
|
|
|
}
|
|
|
|
int main()
|
|
{
|
|
pp::tests::Harness harness;
|
|
harness.run("decodes_png_to_rgba8_pixels", decodes_png_to_rgba8_pixels);
|
|
harness.run("rejects_corrupt_png_payload", rejects_corrupt_png_payload);
|
|
harness.run("encodes_rgba8_pixels_to_decodable_png", encodes_rgba8_pixels_to_decodable_png);
|
|
harness.run("rejects_invalid_png_encode_inputs", rejects_invalid_png_encode_inputs);
|
|
return harness.finish();
|
|
}
|