Added PPM writer
This commit is contained in:
@@ -9,4 +9,5 @@ target_compile_features(${target} PRIVATE cxx_std_23)
|
||||
target_link_libraries(${target}
|
||||
PRIVATE
|
||||
project_core
|
||||
project_image_writers
|
||||
)
|
||||
@@ -1,6 +1,27 @@
|
||||
#include "PpmImage.hpp"
|
||||
#include <cstdint>
|
||||
#include <print>
|
||||
#include <vector>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
std::println("Hello, GraphicsSandbox!");
|
||||
|
||||
// Generate PPM image
|
||||
constexpr size_t imageWidth = 256;
|
||||
constexpr size_t imageHeight = 256;
|
||||
std::vector<std::vector<std::uint8_t>> imageData;
|
||||
imageData.resize(imageWidth);
|
||||
for (auto &row : imageData) {
|
||||
row.resize(imageHeight);
|
||||
}
|
||||
|
||||
image_writers::PpmImage ppmImage(imageData);
|
||||
|
||||
if (ppmImage.writeToFile("Image.ppm")) {
|
||||
std::println("Wrote PPM image");
|
||||
} else {
|
||||
std::println("Failed to write PPM image");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+2
-1
@@ -1 +1,2 @@
|
||||
add_subdirectory(core)
|
||||
add_subdirectory(core)
|
||||
add_subdirectory(image_writers)
|
||||
@@ -0,0 +1,13 @@
|
||||
set(target project_image_writers)
|
||||
|
||||
add_library(${target}
|
||||
Image.hpp
|
||||
PpmImage.cpp
|
||||
PpmImage.hpp
|
||||
)
|
||||
|
||||
target_compile_features(${target} PRIVATE cxx_std_23)
|
||||
|
||||
target_include_directories(${target} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
# target_link_libraries(${target} PRIVATE)
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef IMAGE_HPP
|
||||
#define IMAGE_HPP
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
namespace image_writers {
|
||||
|
||||
class Image {
|
||||
public:
|
||||
virtual ~Image() = default;
|
||||
|
||||
virtual bool writeToFile(const std::filesystem::path &filePath) = 0;
|
||||
};
|
||||
|
||||
} // namespace image_writers
|
||||
|
||||
#endif // IMAGE_HPP
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "PpmImage.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <print>
|
||||
|
||||
image_writers::PpmImage::PpmImage(
|
||||
const std::vector<std::vector<std::uint8_t>> &imageData)
|
||||
: imageData_(imageData) {}
|
||||
|
||||
bool image_writers::PpmImage::writeToFile(
|
||||
const std::filesystem::path &filePath) {
|
||||
|
||||
const size_t height = imageData_[0].size();
|
||||
const size_t width = imageData_.size();
|
||||
|
||||
if (width == 0 || height == 0) {
|
||||
std::println("PpmImage::writeToFile() - Invalid image size");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::println("PpmImage::writeToFile() - Width {}, Height: {}", width, height);
|
||||
|
||||
std::ofstream outFile;
|
||||
outFile.open(filePath, std::ios_base::out | std::ios_base::binary);
|
||||
|
||||
if (outFile.is_open()) {
|
||||
// Create PPM header
|
||||
outFile << "P3\n"; // ASCII
|
||||
outFile << width << ' ' << height << '\n'; // Width and height
|
||||
outFile << "255\n"; // Max color
|
||||
|
||||
// Write image data
|
||||
for (size_t currHeight = 0; currHeight < height; ++currHeight) {
|
||||
for (size_t currWidth = 0; currWidth < width; ++currWidth) {
|
||||
// TODO: Currently overriding colors; make logic for colors
|
||||
const auto r = static_cast<double>(currWidth) / (width - 1);
|
||||
const auto g = static_cast<double>(currHeight) / (height - 1);
|
||||
const auto b = 0.0;
|
||||
|
||||
const auto ir = static_cast<std::uint8_t>(255.999 * r);
|
||||
const auto ig = static_cast<std::uint8_t>(255.999 * g);
|
||||
const auto ib = static_cast<std::uint8_t>(255.999 * b);
|
||||
|
||||
outFile << std::format("{} {} {}\n", ir, ig, ib);
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
outFile.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::println("PpmImage::writeToFile() - Failed to write image file");
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef PPM_IMAGE_HPP
|
||||
#define PPM_IMAGE_HPP
|
||||
|
||||
#include "Image.hpp"
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace image_writers {
|
||||
|
||||
class PpmImage : public Image {
|
||||
public:
|
||||
PpmImage(const std::vector<std::vector<std::uint8_t>> &imageData);
|
||||
~PpmImage() = default;
|
||||
|
||||
bool writeToFile(const std::filesystem::path &filePath) override;
|
||||
|
||||
private:
|
||||
const std::vector<std::vector<std::uint8_t>> &imageData_;
|
||||
};
|
||||
|
||||
} // namespace image_writers
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user