namespace Images { UnitTest::Suite PixelBufferTestSuite { "Images::PixelBuffer", {"it can create red/green gradient", [](UnitTest::Examiner & examiner) { PixelLayout2D pixel_layout(PixelLayout2D::Size{256, 256}); PixelBuffer2D pixel_buffer(pixel_layout); examiner << "Image data was allocated" << std::endl; examiner.check_equal(pixel_buffer.size(), 256*256*4); for (std::size_t i = 0; i < 256; i += 1) { for (std::size_t j = 0; j < 256; j += 1) { pixel_buffer[{i, j}] = PixelFormat::RGBA8{(PixelFormat::U8)i, (PixelFormat::U8)j, 0, 255}; } } auto output_buffer = PNGImage::save(pixel_layout, pixel_buffer.begin()); output_buffer->write_to_file("Red-Green Gradient.png"); } }, {"it can create alpha box", [](UnitTest::Examiner & examiner) { PixelLayout2D pixel_layout(PixelLayout2D::Size{32, 32}); PixelBuffer2D pixel_buffer(pixel_layout); for (std::size_t i = 0; i < 32; i += 1) { for (std::size_t j = 0; j < 32; j += 1) { pixel_buffer[{i, j}] = PixelFormat::RGBA8{128, 128, 128, 128}; } } PNGImage::save(pixel_layout, pixel_buffer.begin())->write_to_file("Alpha Box.png"); WebPImage::save(pixel_layout, pixel_buffer.begin())->write_to_file("Alpha Box.webp"); } }, {"it can create alpha gradient", [](UnitTest::Examiner & examiner) { PixelLayout2D pixel_layout(PixelLayout2D::Size{256, 256}); PixelBuffer2D pixel_buffer(pixel_layout); examiner << "Image data was allocated" << std::endl; examiner.check_equal(pixel_buffer.size(), 256*256*4); for (std::size_t i = 0; i < 256; i += 1) { for (std::size_t j = 0; j < 256; j += 1) { pixel_buffer[{i, j}] = PixelFormat::RGBA8{(PixelFormat::U8)i, (PixelFormat::U8)i, (PixelFormat::U8)i, (PixelFormat::U8)j}; } } PNGImage::save(pixel_layout, pixel_buffer.begin())->write_to_file("Alpha Gradient.png"); WebPImage::save(pixel_layout, pixel_buffer.begin())->write_to_file("Alpha Gradient.webp"); } }, {"it can create solid gradient bar", [](UnitTest::Examiner & examiner) { PixelLayout2D pixel_layout(PixelLayout2D::Size{256, 32}); PixelBuffer2D pixel_buffer(pixel_layout); examiner << "Image data was allocated" << std::endl; examiner.check_equal(pixel_buffer.size(), 256*32*4); for (std::size_t i = 0; i < 256; i += 1) { for (std::size_t j = 0; j < 32; j += 1) { auto c = static_cast<PixelFormat::U8>(i); pixel_buffer[{i, j}] = PixelFormat::RGBA8{c, c, c, 255}; } } PNGImage::save(pixel_layout, pixel_buffer.begin())->write_to_file("Solid Gradient Bar.png"); WebPImage::save(pixel_layout, pixel_buffer.begin())->write_to_file("Solid Gradient Bar.webp"); } }, {"it can create alpha gradient bar", [](UnitTest::Examiner & examiner) { PixelLayout2D pixel_layout(PixelLayout2D::Size{256, 32}); PixelBuffer2D pixel_buffer(pixel_layout); examiner << "Image data was allocated" << std::endl; examiner.check_equal(pixel_buffer.size(), 256*32*4); for (std::size_t i = 0; i < 256; i += 1) { for (std::size_t j = 0; j < 32; j += 1) { pixel_buffer[{i, j}] = PixelFormat::RGBA8{0, 0, 0, static_cast<PixelFormat::U8>(255-i)}; } } PNGImage::save(pixel_layout, pixel_buffer.begin())->write_to_file("Alpha Gradient Bar.png"); WebPImage::save(pixel_layout, pixel_buffer.begin())->write_to_file("Alpha Gradient Bar.webp"); } }, }; }
namespace Imaging { UnitTest::Suite PixelBufferTestSuite { "Dream::Imaging::PixelBuffer", {"PixelBuffer Initialization", [](UnitTest::Examiner & examiner) { PixelBufferLayout2D pixel_layout(PixelBufferLayout2D::SizeType{8, 8}); Ref<PixelBuffer2D> pixel_buffer = new PixelBuffer2D(pixel_layout); examiner << "Image data was allocated" << std::endl; examiner.check_equal(pixel_buffer->buffer().size(), 8*8*4); /* image->fill(0xFF); examiner << "Image was filled with white" << std::endl; auto pixels = reader(*image); Vector<3, Byte> pixel = pixels[Vec2(0, 0)], white(255, 255, 255); examiner.check_equal(pixel, white); Ref<Image> small_black_image = new Image({4, 4}, PixelFormat::L, DataType::BYTE); small_black_image->fill(0); examiner << "Copy black image into corner of white image" << std::endl; writer(*image).copy(reader(*small_black_image), 0, 0, small_black_image->size()); const unsigned char black_row[] = {0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}; const unsigned char white_row[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; for (std::size_t i = 0; i < 4; i += 1) examiner.check_equal(std::memcmp(pixels[Vec2(0, i)], black_row, 8), 0); for (std::size_t i = 4; i < 8; i += 1) examiner.check_equal(std::memcmp(pixels[Vec2(0, i)], white_row, 8), 0); */ } }, /* {"Pixel Writer", [](UnitTest::Examiner & examiner) { Ref<Image> image = new Image({8, 8}, PixelFormat::RGB, DataType::BYTE); examiner << "Image data was allocated" << std::endl; examiner.check_equal(image->buffer().size(), 8*8*3); image->fill(0xFF); Vector<3, Byte> color {0x11, 0x22, 0x33}; writer(*image).set({0, 0}, color); Vector<3, Byte> output; reader(*image).get<3>({0, 0}, output); examiner << "Check the colour is correct" << std::endl; examiner.check_equal(output, color); } } */ }; }