spatial grid is annoying. also added frame storage
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <chrono>
|
||||
#include "frame.hpp"
|
||||
#include "video.hpp"
|
||||
|
||||
class AVIWriter {
|
||||
private:
|
||||
@@ -110,7 +112,73 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to convert frame to RGB format
|
||||
static std::vector<uint8_t> frameToRGB(const frame& frm) {
|
||||
if (frm.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
size_t width = frm.width();
|
||||
size_t height = frm.height();
|
||||
std::vector<uint8_t> rgbData(width * height * 3);
|
||||
|
||||
// Check if frame already has RGB channels
|
||||
bool hasR = frm.has_channel('R') || frm.has_channel('r');
|
||||
bool hasG = frm.has_channel('G') || frm.has_channel('g');
|
||||
bool hasB = frm.has_channel('B') || frm.has_channel('b');
|
||||
|
||||
if (hasR && hasG && hasB) {
|
||||
// Frame has RGB channels - extract them
|
||||
std::vector<uint8_t> rChannel = frm.has_channel('R') ?
|
||||
frm.get_channel_data('R') : frm.get_channel_data('r');
|
||||
std::vector<uint8_t> gChannel = frm.has_channel('G') ?
|
||||
frm.get_channel_data('G') : frm.get_channel_data('g');
|
||||
std::vector<uint8_t> bChannel = frm.has_channel('B') ?
|
||||
frm.get_channel_data('B') : frm.get_channel_data('b');
|
||||
|
||||
// Convert to BGR format (required by AVI)
|
||||
for (size_t i = 0; i < width * height; ++i) {
|
||||
rgbData[i * 3 + 0] = bChannel[i]; // Blue
|
||||
rgbData[i * 3 + 1] = gChannel[i]; // Green
|
||||
rgbData[i * 3 + 2] = rChannel[i]; // Red
|
||||
}
|
||||
} else if (frm.channels_count() == 1) {
|
||||
// Grayscale frame - convert to RGB
|
||||
std::vector<uint8_t> grayChannel = frm.get_channel_data(frm.channels()[0]);
|
||||
|
||||
for (size_t i = 0; i < width * height; ++i) {
|
||||
uint8_t gray = grayChannel[i];
|
||||
rgbData[i * 3 + 0] = gray; // Blue
|
||||
rgbData[i * 3 + 1] = gray; // Green
|
||||
rgbData[i * 3 + 2] = gray; // Red
|
||||
}
|
||||
} else if (frm.channels_count() == 3) {
|
||||
// Assume the 3 channels are RGB (even if not named)
|
||||
// Convert to BGR format
|
||||
for (size_t y = 0; y < height; ++y) {
|
||||
for (size_t x = 0; x < width; ++x) {
|
||||
rgbData[(y * width + x) * 3 + 0] = frm.at(y, x, size_t(2)); // Blue
|
||||
rgbData[(y * width + x) * 3 + 1] = frm.at(y, x, size_t(1)); // Green
|
||||
rgbData[(y * width + x) * 3 + 2] = frm.at(y, x, size_t(0)); // Red
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Unsupported format - use first channel as grayscale
|
||||
std::vector<uint8_t> firstChannel = frm.get_channel_data(frm.channels()[0]);
|
||||
|
||||
for (size_t i = 0; i < width * height; ++i) {
|
||||
uint8_t gray = firstChannel[i];
|
||||
rgbData[i * 3 + 0] = gray; // Blue
|
||||
rgbData[i * 3 + 1] = gray; // Green
|
||||
rgbData[i * 3 + 2] = gray; // Red
|
||||
}
|
||||
}
|
||||
|
||||
return rgbData;
|
||||
}
|
||||
|
||||
public:
|
||||
// Original method for vector of raw frame data
|
||||
static bool saveAVI(const std::string& filename,
|
||||
const std::vector<std::vector<uint8_t>>& frames,
|
||||
int width, int height, float fps = 30.0f) {
|
||||
@@ -120,7 +188,7 @@ public:
|
||||
|
||||
// std::cout << "1" << "width: " << width <<
|
||||
// "height: " << height << "frame count: " << fps << std::endl;
|
||||
|
||||
|
||||
// Validate frame sizes
|
||||
size_t expectedFrameSize = width * height * 3;
|
||||
for (const auto& frame : frames) {
|
||||
@@ -304,6 +372,37 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
// New overload for frame objects
|
||||
static bool saveAVI(const std::string& filename,
|
||||
const std::vector<frame>& frames,
|
||||
float fps = 30.0f) {
|
||||
if (frames.empty() || fps <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validate that all frames have the same dimensions
|
||||
int width = static_cast<int>(frames[0].width());
|
||||
int height = static_cast<int>(frames[0].height());
|
||||
|
||||
for (const auto& frm : frames) {
|
||||
if (frm.width() != static_cast<size_t>(width) ||
|
||||
frm.height() != static_cast<size_t>(height)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert frames to RGB format
|
||||
std::vector<std::vector<uint8_t>> rgbFrames;
|
||||
rgbFrames.reserve(frames.size());
|
||||
|
||||
for (const auto& frm : frames) {
|
||||
rgbFrames.push_back(frameToRGB(frm));
|
||||
}
|
||||
|
||||
// Use the existing implementation
|
||||
return saveAVI(filename, rgbFrames, width, height, fps);
|
||||
}
|
||||
|
||||
// Convenience function to save from individual frame files
|
||||
static bool saveAVIFromFrames(const std::string& filename,
|
||||
const std::vector<std::string>& frameFiles,
|
||||
|
||||
Reference in New Issue
Block a user