#include "utils.hpp" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" #include #include void writeImage(const tp::Buffer2D& output, const char* name) { using namespace tp; // Save the data to a PNG file struct urgb { uint1 r, g, b, a; }; Buffer2D converted; converted.reserve(output.size()); for (Index i = 0; i < output.size().x; i++) { for (Index j = 0; j < output.size().y; j++) { converted.get({ i, j }).r = uint1(output.get({ i, j }).r * 255); converted.get({ i, j }).g = uint1(output.get({ i, j }).g * 255); converted.get({ i, j }).b = uint1(output.get({ i, j }).b * 255); converted.get({ i, j }).a = uint1(output.get({ i, j }).a * 255); } } if (stbi_write_png(name, converted.size().x, converted.size().y, 4, converted.getBuff(), converted.size().x * 4) != 0) { // Image saved successfully printf("Image saved successfully.\n"); } else { printf("Error saving the image.\n"); } } TimeMs timeDeltaMs(const TimePoint& point) { auto timeDelta = std::chrono::system_clock::now() - point; auto timeDelayMs = std::chrono::duration_cast(timeDelta).count(); return timeDelayMs; } std::vector readFile(const std::string &fileName) { std::ifstream file(fileName, std::ios::ate | std::ios::binary); if (!file.is_open()) { throw std::runtime_error("cannot open file"); } size_t fileSize = (size_t) file.tellg(); std::vector buffer(fileSize); file.seekg(0); file.read(buffer.data(), (std::streamsize) fileSize); file.close(); return buffer; }