usually works. need to figure out what causes it to not to sometimes.

This commit is contained in:
yggdrasil75
2026-01-26 20:02:01 -05:00
parent ed6b625826
commit c3916d146a
10 changed files with 743 additions and 200 deletions

View File

@@ -7,7 +7,7 @@
#include <string>
#include <algorithm>
#include <filesystem>
#include "../vectorlogic/vec3.hpp"
//#include "../vectorlogic/vec3.hpp"
#include "frame.hpp"
class BMPWriter {
@@ -51,40 +51,40 @@ private:
public:
// Save a 2D vector of Vec3ui8 (RGB) colors as BMP
// Vec3ui8 components: x = red, y = green, z = blue (values in range [0,1])
static bool saveBMP(const std::string& filename, const std::vector<std::vector<Vec3ui8>>& pixels) {
if (pixels.empty() || pixels[0].empty()) {
return false;
}
// static bool saveBMP(const std::string& filename, const std::vector<std::vector<Vec3ui8>>& pixels) {
// if (pixels.empty() || pixels[0].empty()) {
// return false;
// }
int height = static_cast<int>(pixels.size());
int width = static_cast<int>(pixels[0].size());
// int height = static_cast<int>(pixels.size());
// int width = static_cast<int>(pixels[0].size());
// Validate that all rows have the same width
for (const auto& row : pixels) {
if (row.size() != width) {
return false;
}
}
// // Validate that all rows have the same width
// for (const auto& row : pixels) {
// if (row.size() != width) {
// return false;
// }
// }
return saveBMP(filename, pixels, width, height);
}
// return saveBMP(filename, pixels, width, height);
// }
// Alternative interface with width/height and flat vector (row-major order)
static bool saveBMP(const std::string& filename, const std::vector<Vec3ui8>& pixels, int width, int height) {
if (pixels.size() != width * height) {
return false;
}
// // Alternative interface with width/height and flat vector (row-major order)
// static bool saveBMP(const std::string& filename, const std::vector<Vec3ui8>& pixels, int width, int height) {
// if (pixels.size() != width * height) {
// return false;
// }
// Convert to 2D vector format
std::vector<std::vector<Vec3ui8>> pixels2D(height, std::vector<Vec3ui8>(width));
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
pixels2D[y][x] = pixels[y * width + x];
}
}
// // Convert to 2D vector format
// std::vector<std::vector<Vec3ui8>> pixels2D(height, std::vector<Vec3ui8>(width));
// for (int y = 0; y < height; ++y) {
// for (int x = 0; x < width; ++x) {
// pixels2D[y][x] = pixels[y * width + x];
// }
// }
return saveBMP(filename, pixels2D, width, height);
}
// return saveBMP(filename, pixels2D, width, height);
// }
// Save from 1D vector of uint8_t pixels (BGR order: pixels[i]=b, pixels[i+1]=g, pixels[i+2]=r)
static bool saveBMP(const std::string& filename, const std::vector<uint8_t>& pixels, int width, int height) {
@@ -157,53 +157,53 @@ public:
}
private:
static bool saveBMP(const std::string& filename, const std::vector<std::vector<Vec3ui8>>& pixels, int width, int height) {
// Create directory if needed
if (!createDirectoryIfNeeded(filename)) {
return false;
}
// static bool saveBMP(const std::string& filename, const std::vector<std::vector<Vec3ui8>>& pixels, int width, int height) {
// // Create directory if needed
// if (!createDirectoryIfNeeded(filename)) {
// return false;
// }
BMPHeader header;
BMPInfoHeader infoHeader;
// BMPHeader header;
// BMPInfoHeader infoHeader;
int rowSize = (width * 3 + 3) & ~3; // 24-bit, padded to 4 bytes
int imageSize = rowSize * height;
// int rowSize = (width * 3 + 3) & ~3; // 24-bit, padded to 4 bytes
// int imageSize = rowSize * height;
header.fileSize = sizeof(BMPHeader) + sizeof(BMPInfoHeader) + imageSize;
infoHeader.width = width;
infoHeader.height = height;
infoHeader.imageSize = imageSize;
// header.fileSize = sizeof(BMPHeader) + sizeof(BMPInfoHeader) + imageSize;
// infoHeader.width = width;
// infoHeader.height = height;
// infoHeader.imageSize = imageSize;
std::ofstream file(filename, std::ios::binary);
if (!file) {
return false;
}
// std::ofstream file(filename, std::ios::binary);
// if (!file) {
// return false;
// }
file.write(reinterpret_cast<const char*>(&header), sizeof(header));
file.write(reinterpret_cast<const char*>(&infoHeader), sizeof(infoHeader));
// file.write(reinterpret_cast<const char*>(&header), sizeof(header));
// file.write(reinterpret_cast<const char*>(&infoHeader), sizeof(infoHeader));
// Write pixel data (BMP stores pixels bottom-to-top)
std::vector<uint8_t> row(rowSize, 0);
for (int y = height - 1; y >= 0; --y) {
for (int x = 0; x < width; ++x) {
const Vec3ui8& color = pixels[y][x];
// // Write pixel data (BMP stores pixels bottom-to-top)
// std::vector<uint8_t> row(rowSize, 0);
// for (int y = height - 1; y >= 0; --y) {
// for (int x = 0; x < width; ++x) {
// const Vec3ui8& color = pixels[y][x];
// Convert from [0,1] float to [0,255] uint8_t
uint8_t r = static_cast<uint8_t>(std::clamp(color.x * 255.0f, 0.0f, 255.0f));
uint8_t g = static_cast<uint8_t>(std::clamp(color.y * 255.0f, 0.0f, 255.0f));
uint8_t b = static_cast<uint8_t>(std::clamp(color.z * 255.0f, 0.0f, 255.0f));
// // Convert from [0,1] float to [0,255] uint8_t
// uint8_t r = static_cast<uint8_t>(std::clamp(color.x * 255.0f, 0.0f, 255.0f));
// uint8_t g = static_cast<uint8_t>(std::clamp(color.y * 255.0f, 0.0f, 255.0f));
// uint8_t b = static_cast<uint8_t>(std::clamp(color.z * 255.0f, 0.0f, 255.0f));
// BMP is BGR order
int pixelOffset = x * 3;
row[pixelOffset] = b;
row[pixelOffset + 1] = g;
row[pixelOffset + 2] = r;
}
file.write(reinterpret_cast<const char*>(row.data()), rowSize);
}
// // BMP is BGR order
// int pixelOffset = x * 3;
// row[pixelOffset] = b;
// row[pixelOffset + 1] = g;
// row[pixelOffset + 2] = r;
// }
// file.write(reinterpret_cast<const char*>(row.data()), rowSize);
// }
return true;
}
// return true;
// }
static std::vector<uint8_t> convertRGBAtoRGB(const std::vector<uint8_t>& rgba) {
std::vector<uint8_t> rgb;