VulkanGraphics/extern/Utils/private/utils.cpp
Ilya Shurupov dc88120f58 refactor
2024-11-26 12:58:54 +03:00

61 lines
No EOL
1.6 KiB
C++

#include "utils.hpp"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <iostream>
#include <chrono>
void writeImage(const tp::Buffer2D<tp::RGBA>& output, const char* name) {
using namespace tp;
// Save the data to a PNG file
struct urgb {
uint1 r, g, b, a;
};
Buffer2D<urgb> 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<std::chrono::milliseconds>(timeDelta).count();
return timeDelayMs;
}
std::vector<char> 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<char> buffer(fileSize);
file.seekg(0);
file.read(buffer.data(), (std::streamsize) fileSize);
file.close();
return buffer;
}