Add support for saving color images directly to file

This commit is contained in:
Georg Hagen
2024-07-04 09:07:31 +02:00
parent 5b6fde2aaf
commit 4af5dbd596
3 changed files with 34 additions and 10 deletions

View File

@@ -54,7 +54,7 @@ namespace OpenVulkano::AR
ArRecorder::~ArRecorder() = default;
void ArRecorder::WriteColorImage(ArFrame* arFrame, MultiPartArchiveWriter* colorWriter, bool highRes) const
void ArRecorder::WriteColorImage(ArFrame* arFrame, MultiPartArchiveWriter* colorWriter, const std::filesystem::path* path, bool highRes) const
{
BlockProfiler profile("Save AR Frame - Image");
std::string fileName = GetFileName(arFrame->GetFrameId(), "jpg");
@@ -95,10 +95,19 @@ namespace OpenVulkano::AR
uint8_t* outBuffer = nullptr;
unsigned long size = 0;
if (tjCompressFromYUVPlanes(handle, buffers, resX, nullptr, resY, TJSAMP_420, &outBuffer, &size, 95, TJFLAG_FASTDCT))
if (tjCompressFromYUVPlanes(handle, buffers, resX, nullptr, resY, TJSAMP_420, &outBuffer, &size, 95, TJFLAG_FASTDCT)) [[unlikely]]
Logger::AR->error("Failed to create JPEG! {}", tjGetErrorStr());
else
colorWriter->AddFile(fileName.c_str(), outBuffer, size);
else [[likely]]
{
if (colorWriter) [[likely]]
colorWriter->AddFile(fileName.c_str(), outBuffer, size);
if (path) [[unlikely]]
{
std::ofstream outFile(*path, std::ios::binary);
outFile.write(reinterpret_cast<const char*>(outBuffer), size);
outFile.close();
}
}
tjFree(outBuffer);
#endif
}
@@ -151,7 +160,7 @@ namespace OpenVulkano::AR
bool useHighResWriter = highRes && m_settings.highResFramesInSeparateArchive;
BlockProfiler profile("Save AR Frame");
WriteMetadata(frame, useHighResWriter ? m_highResWriter.get() : m_metadataWriter.get());
WriteColorImage(frame, useHighResWriter ? m_highResWriter.get() : m_colorWriter.get(), highRes);
WriteColorImage(frame, useHighResWriter ? m_highResWriter.get() : m_colorWriter.get(), nullptr, highRes);
WriteDepthImage(frame,
useHighResWriter ? m_highResWriter.get() : m_depthWriter.get(),
useHighResWriter ? m_highResWriter.get() : m_confidenceWriter.get());
@@ -230,7 +239,7 @@ namespace OpenVulkano::AR
void ArRecorder::SaveHighResolution(const std::shared_ptr<ArFrame>& frame)
{
if (!m_recording) return;
if (!m_recording || !m_settings.saveHighResFrames) return;
if (m_settings.asyncRecording)
{
m_asyncProcessor.Queue(frame, true);
@@ -238,6 +247,11 @@ namespace OpenVulkano::AR
else Write(frame.get(), true);
}
void ArRecorder::SaveToFile(const std::shared_ptr<ArFrame>& frame, const std::filesystem::path& path, bool downsample)
{
WriteColorImage(frame.get(), nullptr, &path, !downsample);
}
//region AsyncProcessor
ArRecorder::AsyncProcessor::AsyncProcessor(ArRecorder* recorder)
: recorder(recorder), processingThread(&ArRecorder::AsyncProcessor::Handler, this)