Skip to content

Commit 3ddc571

Browse files
Added raw pointer overload for tc::img::image_save method.
1 parent 093a238 commit 3ddc571

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

image/include/teiacare/image/image_io.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ auto image_load_from_memory(
6060
uint8_t* memory_data,
6161
std::size_t memory_data_size) -> std::tuple<std::vector<uint8_t>, int, int, int>;
6262

63+
/*!
64+
* \brief Save image data to a file.
65+
* \param output_path Path where the image file should be saved
66+
* \param image_data Pointer to the image pixel data buffer
67+
* \param width Width of the image in pixels
68+
* \param height Height of the image in pixels
69+
* \param channels Number of color channels (e.g., 3 for RGB, 4 for RGBA)
70+
*/
71+
void image_save(
72+
const std::filesystem::path& image_path,
73+
const uint8_t* image_data_ptr,
74+
int width,
75+
int height,
76+
int channels);
77+
6378
/*!
6479
* \brief Save image data to a file.
6580
* \param output_path Path where the image file should be saved

image/src/image_io.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,26 @@ auto image_load_from_memory(uint8_t* memory_data, std::size_t memory_data_size)
7777
return create_image_data(image_data, width, height, channels);
7878
}
7979

80-
void image_save(const std::filesystem::path& image_path, const std::vector<uint8_t>& image_data, int width, int height, int channels)
80+
void image_save(const std::filesystem::path& image_path, const uint8_t* image_data_ptr, int width, int height, int channels)
8181
{
8282
bool ok = false;
8383
const std::string image_ext = (image_path.has_extension() ? image_path.extension().string() : "png");
8484

8585
if (image_ext == ".png")
8686
{
87-
ok = stbi_write_png(image_path.string().c_str(), width, height, channels, image_data.data(), width * channels) != 0;
87+
ok = stbi_write_png(image_path.string().c_str(), width, height, channels, image_data_ptr, width * channels) != 0;
8888
}
8989
else if (image_ext == ".jpg" || image_ext == ".jpeg")
9090
{
91-
ok = stbi_write_jpg(image_path.string().c_str(), width, height, channels, image_data.data(), /*quality*/ 100) != 0;
91+
ok = stbi_write_jpg(image_path.string().c_str(), width, height, channels, image_data_ptr, /*quality*/ 100) != 0;
9292
}
9393
else if (image_ext == ".bmp")
9494
{
95-
ok = stbi_write_bmp(image_path.string().c_str(), width, height, channels, image_data.data()) != 0;
95+
ok = stbi_write_bmp(image_path.string().c_str(), width, height, channels, image_data_ptr) != 0;
9696
}
9797
else if (image_ext == ".tga")
9898
{
99-
ok = stbi_write_tga(image_path.string().c_str(), width, height, channels, image_data.data()) != 0;
99+
ok = stbi_write_tga(image_path.string().c_str(), width, height, channels, image_data_ptr) != 0;
100100
}
101101
else
102102
{
@@ -108,4 +108,9 @@ void image_save(const std::filesystem::path& image_path, const std::vector<uint8
108108
}
109109
}
110110

111+
void image_save(const std::filesystem::path& image_path, const std::vector<uint8_t>& image_data, int width, int height, int channels)
112+
{
113+
return image_save(image_path, image_data.data(), width, height, channels);
114+
}
115+
111116
}

image/tests/test_image_io.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ TEST_F(image_io_test, image_save_png)
230230
EXPECT_GT(std::filesystem::file_size(output_file), 0);
231231
}
232232

233+
// Test tc::img::image_save with raw pointer version
234+
TEST_F(image_io_test, image_save_png_raw_pointer)
235+
{
236+
int width = 4, height = 3, channels = 3;
237+
auto image_data = createUniformImageData(width, height, channels, 128);
238+
auto output_file = temp_dir_ / "test_ptr.png";
239+
240+
EXPECT_NO_THROW(tc::img::image_save(output_file, image_data.data(), width, height, channels));
241+
EXPECT_TRUE(std::filesystem::exists(output_file));
242+
EXPECT_GT(std::filesystem::file_size(output_file), 0);
243+
}
244+
233245
// Test tc::img::image_save with JPEG format
234246
TEST_F(image_io_test, image_save_jpeg)
235247
{

0 commit comments

Comments
 (0)