Skip to content

Commit fdad179

Browse files
committed
Core (Tests): Factor out raw image and raw palette loading functions into a common library.
1 parent 6f35b14 commit fdad179

File tree

4 files changed

+111
-85
lines changed

4 files changed

+111
-85
lines changed

libvisual/tests/video_test/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
LV_BUILD_TEST(video_check_test
22
SOURCES video_check_test.cpp
3+
LINK_LIBS video_test_common
34
)
45

56
LV_BUILD_TEST(video_blit_test
67
SOURCES video_blit_test.cpp
8+
LINK_LIBS video_test_common
79
)
810

911
LV_BUILD_TEST(video_load_test
1012
SOURCES video_load_test.cpp
13+
LINK_LIBS video_test_common
1114
)
1215

16+
ADD_LIBRARY(video_test_common STATIC common.cpp)
17+
TARGET_LINK_LIBRARIES(video_test_common PUBLIC libvisual)
18+
1319
IF(HAVE_SDL)
1420
LV_BUILD_TEST(video_scale_test
1521
SOURCES video_scale_test.cpp
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "common.hpp"
2+
#include <libvisual/libvisual.h>
3+
#include <filesystem>
4+
#include <fstream>
5+
#include <optional>
6+
7+
namespace fs = std::filesystem;
8+
9+
LV::VideoPtr load_raw_image (fs::path const& path, int width, int height, VisVideoDepth depth)
10+
{
11+
auto image {LV::Video::create (width, height, depth)};
12+
13+
std::size_t const content_bytes_per_row = image->get_width () * image->get_bpp ();
14+
15+
{
16+
std::ifstream input {path, std::ios::binary};
17+
if (!input) {
18+
return nullptr;
19+
}
20+
21+
for (int y = 0; y < image->get_height (); y++) {
22+
auto pixel_row_ptr = static_cast<char*> (image->get_pixel_ptr (0, y));
23+
if (!input.read (pixel_row_ptr, content_bytes_per_row)) {
24+
return nullptr;
25+
}
26+
}
27+
}
28+
29+
if constexpr (std::endian::native == std::endian::little) {
30+
auto byteswapped_image {LV::Video::create (width, height, depth)};
31+
byteswapped_image->flip_pixel_bytes (image);
32+
return byteswapped_image;
33+
} else {
34+
return image;
35+
}
36+
}
37+
38+
std::optional<LV::Palette> load_raw_palette (fs::path const& path)
39+
{
40+
std::vector<std::uint8_t> palette_buffer (256 * 3);
41+
{
42+
std::ifstream input {path, std::ios::binary};
43+
if (!input.read (reinterpret_cast<char*> (palette_buffer.data ()), palette_buffer.size ())) {
44+
return std::nullopt;
45+
}
46+
}
47+
48+
LV::Palette palette;
49+
palette.colors.reserve (256);
50+
for (unsigned int i = 0; i < 256; i++) {
51+
palette.colors.emplace_back (palette_buffer[i*3], palette_buffer[i*3+1], palette_buffer[i*3+2]);
52+
}
53+
54+
return palette;
55+
}
56+
57+
LV::VideoPtr load_raw_indexed_image (fs::path const& image_path,
58+
fs::path const& palette_path,
59+
int width,
60+
int height)
61+
{
62+
auto image {LV::Video::create (width, height, VISUAL_VIDEO_DEPTH_8BIT)};
63+
64+
std::size_t const content_bytes_per_row = image->get_width () * image->get_bpp ();
65+
66+
{
67+
std::ifstream input {image_path, std::ios::binary};
68+
if (!input) {
69+
return nullptr;
70+
}
71+
72+
for (int y = 0; y < image->get_height (); y++) {
73+
auto pixel_row_ptr = static_cast<char*> (image->get_pixel_ptr (0, y));
74+
if (!input.read (pixel_row_ptr, content_bytes_per_row)) {
75+
return nullptr;
76+
}
77+
}
78+
}
79+
80+
auto palette {load_raw_palette (palette_path)};
81+
if (!palette.has_value ()) {
82+
return nullptr;
83+
}
84+
image->set_palette (palette.value ());
85+
86+
return image;
87+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef LV_VIDEO_TEST_COMMON_HPP
2+
#define LV_VIDEO_TEST_COMMON_HPP
3+
4+
#include <libvisual/libvisual.h>
5+
#include <filesystem>
6+
#include <optional>
7+
8+
LV::VideoPtr load_raw_image (std::filesystem::path const& path, int width, int height, VisVideoDepth depth);
9+
10+
std::optional<LV::Palette> load_raw_palette (std::filesystem::path const& path);
11+
12+
LV::VideoPtr load_raw_indexed_image (std::filesystem::path const& image_path,
13+
std::filesystem::path const& palette_path,
14+
int width,
15+
int height);
16+
17+
#endif // LV_VIDEO_TEST_COMMON_HPP

libvisual/tests/video_test/video_load_test.cpp

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,9 @@
11
#include "test.h"
2+
#include "video_test/common.hpp"
23
#include <libvisual/libvisual.h>
3-
#include <bit>
4-
#include <filesystem>
5-
#include <fstream>
6-
#include <optional>
74

85
namespace
96
{
10-
namespace fs = std::filesystem;
11-
12-
LV::VideoPtr load_raw_image (fs::path const& path, int width, int height, VisVideoDepth depth)
13-
{
14-
auto image {LV::Video::create (width, height, depth)};
15-
16-
std::size_t const content_bytes_per_row = image->get_width () * image->get_bpp ();
17-
18-
{
19-
std::ifstream input {path, std::ios::binary};
20-
if (!input) {
21-
return nullptr;
22-
}
23-
24-
for (int y = 0; y < image->get_height (); y++) {
25-
auto pixel_row_ptr = static_cast<char*> (image->get_pixel_ptr (0, y));
26-
if (!input.read (pixel_row_ptr, content_bytes_per_row)) {
27-
return nullptr;
28-
}
29-
}
30-
}
31-
32-
if constexpr (std::endian::native == std::endian::little) {
33-
auto byteswapped_image {LV::Video::create (width, height, depth)};
34-
byteswapped_image->flip_pixel_bytes (image);
35-
return byteswapped_image;
36-
} else {
37-
return image;
38-
}
39-
}
40-
41-
std::optional<LV::Palette> load_raw_palette (fs::path const& path)
42-
{
43-
std::vector<std::uint8_t> palette_buffer (256 * 3);
44-
{
45-
std::ifstream input {path, std::ios::binary};
46-
if (!input.read (reinterpret_cast<char*> (palette_buffer.data ()), palette_buffer.size ())) {
47-
return std::nullopt;
48-
}
49-
}
50-
51-
LV::Palette palette;
52-
palette.colors.reserve (256);
53-
for (unsigned int i = 0; i < 256; i++) {
54-
palette.colors.emplace_back (palette_buffer[i*3], palette_buffer[i*3+1], palette_buffer[i*3+2]);
55-
}
56-
57-
return palette;
58-
}
59-
60-
LV::VideoPtr load_raw_indexed_image (fs::path const& image_path,
61-
fs::path const& palette_path,
62-
int width,
63-
int height)
64-
{
65-
auto image {LV::Video::create (width, height, VISUAL_VIDEO_DEPTH_8BIT)};
66-
67-
std::size_t const content_bytes_per_row = image->get_width () * image->get_bpp ();
68-
69-
{
70-
std::ifstream input {image_path, std::ios::binary};
71-
if (!input) {
72-
return nullptr;
73-
}
74-
75-
for (int y = 0; y < image->get_height (); y++) {
76-
auto pixel_row_ptr = static_cast<char*> (image->get_pixel_ptr (0, y));
77-
if (!input.read (pixel_row_ptr, content_bytes_per_row)) {
78-
return nullptr;
79-
}
80-
}
81-
}
82-
83-
auto palette {load_raw_palette (palette_path)};
84-
if (!palette.has_value ()) {
85-
return nullptr;
86-
}
87-
image->set_palette (palette.value ());
88-
89-
return image;
90-
}
917

928
void test_load_indexed8 ()
939
{

0 commit comments

Comments
 (0)