Skip to content

Commit 7c461b8

Browse files
committed
Core (Tests): Add unit test for loading PNGs.
1 parent 9c932a6 commit 7c461b8

10 files changed

+180
-0
lines changed
4.27 KB
Loading
63 KB
Binary file not shown.
3.1 KB
Loading
15.8 KB
Binary file not shown.
768 Bytes
Binary file not shown.
3.83 KB
Loading
47.3 KB
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Loading

libvisual/tests/video_test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ LV_BUILD_TEST(video_blit_test
66
SOURCES video_blit_test.cpp
77
)
88

9+
LV_BUILD_TEST(video_load_test
10+
SOURCES video_load_test.cpp
11+
)
12+
913
IF(HAVE_SDL)
1014
LV_BUILD_TEST(video_scale_test
1115
SOURCES video_scale_test.cpp
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#include "test.h"
2+
#include <libvisual/libvisual.h>
3+
#include <bit>
4+
#include <filesystem>
5+
#include <fstream>
6+
#include <optional>
7+
8+
namespace
9+
{
10+
LV::VideoPtr load_raw_image (std::filesystem::path const& path, int width, int height, VisVideoDepth depth)
11+
{
12+
auto image {LV::Video::create (width, height, depth)};
13+
14+
std::size_t const content_bytes_per_row = image->get_width () * image->get_bpp ();
15+
16+
{
17+
std::ifstream input {path, std::ios::binary};
18+
if (!input) {
19+
return nullptr;
20+
}
21+
22+
for (int y = 0; y < image->get_height (); y++) {
23+
auto pixel_row_ptr = static_cast<char*> (image->get_pixel_ptr (0, y));
24+
if (!input.read (pixel_row_ptr, content_bytes_per_row)) {
25+
return nullptr;
26+
}
27+
}
28+
}
29+
30+
if constexpr (std::endian::native == std::endian::little) {
31+
auto byteswapped_image {LV::Video::create (width, height, depth)};
32+
byteswapped_image->flip_pixel_bytes (image);
33+
return byteswapped_image;
34+
} else {
35+
return image;
36+
}
37+
}
38+
39+
std::optional<LV::Palette> load_raw_palette(std::filesystem::path const& path)
40+
{
41+
std::vector<std::uint8_t> palette_buffer (256 * 3);
42+
{
43+
std::ifstream input {path, std::ios::binary};
44+
if (!input.read(reinterpret_cast<char*>(palette_buffer.data()), palette_buffer.size())) {
45+
return {};
46+
}
47+
}
48+
49+
LV::Palette palette;
50+
palette.colors.reserve(256);
51+
for (unsigned int i = 0; i < 256; i++) {
52+
palette.colors.emplace_back(palette_buffer[i*3], palette_buffer[i*3+1], palette_buffer[i*3+2]);
53+
}
54+
55+
return palette;
56+
}
57+
58+
LV::VideoPtr load_raw_indexed_image (std::filesystem::path const& image_path,
59+
std::filesystem::path const& palette_path,
60+
int width,
61+
int height,
62+
VisVideoDepth depth)
63+
{
64+
if (depth != VISUAL_VIDEO_DEPTH_24BIT) {
65+
return nullptr;
66+
}
67+
68+
auto image {LV::Video::create (width, height, VISUAL_VIDEO_DEPTH_8BIT)};
69+
70+
std::size_t const content_bytes_per_row = image->get_width () * image->get_bpp ();
71+
72+
{
73+
std::ifstream input {image_path, std::ios::binary};
74+
if (!input) {
75+
return nullptr;
76+
}
77+
78+
for (int y = 0; y < image->get_height (); y++) {
79+
auto pixel_row_ptr = static_cast<char*> (image->get_pixel_ptr (0, y));
80+
if (!input.read (pixel_row_ptr, content_bytes_per_row)) {
81+
return nullptr;
82+
}
83+
}
84+
}
85+
86+
auto palette {load_raw_palette(palette_path)};
87+
if (!palette.has_value()) {
88+
return nullptr;
89+
}
90+
image->set_palette (palette.value());
91+
92+
auto final_image {LV::Video::create (image->get_width(), image->get_height(), VISUAL_VIDEO_DEPTH_24BIT)};
93+
final_image->convert_depth(image);
94+
95+
return final_image;
96+
}
97+
98+
void test_png_load ()
99+
{
100+
{
101+
auto png_image {LV::Video::create_from_file ("../images/additive-colors-indexed8.png")};
102+
LV_TEST_ASSERT (png_image);
103+
104+
auto raw_image {load_raw_indexed_image ("../images/additive-colors-indexed8.raw",
105+
"../images/additive-colors-indexed8.raw.pal",
106+
png_image->get_width (),
107+
png_image->get_height (),
108+
VISUAL_VIDEO_DEPTH_24BIT)};
109+
110+
LV_TEST_ASSERT (png_image->has_same_content (raw_image));
111+
}
112+
113+
{
114+
auto png_image {LV::Video::create_from_file ("../images/additive-colors-rgb24.png")};
115+
LV_TEST_ASSERT (png_image);
116+
117+
auto raw_image {load_raw_image ("../images/additive-colors-rgb24.raw",
118+
png_image->get_width (),
119+
png_image->get_height (),
120+
VISUAL_VIDEO_DEPTH_24BIT)};
121+
122+
LV_TEST_ASSERT (png_image->has_same_content (raw_image));
123+
}
124+
125+
{
126+
auto png_image {LV::Video::create_from_file ("../images/additive-colors-argb32.png")};
127+
LV_TEST_ASSERT (png_image);
128+
129+
auto raw_image {load_raw_image ("../images/additive-colors-argb32.raw",
130+
png_image->get_width (),
131+
png_image->get_height (),
132+
VISUAL_VIDEO_DEPTH_32BIT)};
133+
134+
LV_TEST_ASSERT (png_image->has_same_content (raw_image));
135+
}
136+
}
137+
}
138+
139+
int main (int argc, char* argv[])
140+
{
141+
LV::System::init (argc, argv);
142+
143+
test_png_load ();
144+
145+
LV::System::destroy ();
146+
}

0 commit comments

Comments
 (0)