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
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)

View File

@@ -50,6 +50,7 @@ namespace OpenVulkano::AR
size_t archiveSize = 2_GiB;
bool downsampleColor = true;
bool highResFramesInSeparateArchive = true;
bool saveHighResFrames = true;
bool asyncRecording = true;
};
@@ -81,7 +82,7 @@ namespace OpenVulkano::AR
void Write(ArFrame* frame, bool highRes = false);
void WriteMetadata(ArFrame* frame, MultiPartArchiveWriter* metaWriter);
void WriteColorImage(ArFrame* arFrame, MultiPartArchiveWriter* colorWriter, bool highRes) const;
void WriteColorImage(ArFrame* arFrame, MultiPartArchiveWriter* colorWriter, const std::filesystem::path* path, bool highRes) const;
void WriteDepthImage(ArFrame *arFrame, MultiPartArchiveWriter* depthWriter, MultiPartArchiveWriter* confWriter);
public:
@@ -93,6 +94,8 @@ namespace OpenVulkano::AR
void SaveHighResolution(const std::shared_ptr<ArFrame>& frame);
void SaveToFile(const std::shared_ptr<ArFrame>& frame, const std::filesystem::path& path, bool downsample = false);
/**
* Starts the recording of the owning AR session
*/
@@ -185,5 +188,7 @@ namespace OpenVulkano::AR
size_t GetArchivePartMaxFileSize() const { return m_settings.archiveSize; }
const RecordingSettings& GetRecordingSettings() const { return m_settings; }
void SetRecordHighResImages(bool recHighRes = true) { m_settings.saveHighResFrames = recHighRes; }
};
}

View File

@@ -98,6 +98,11 @@ namespace OpenVulkano::AR::ArKit
m_colorImage.format = ArImagePlanar::Format::NV12;
m_colorImage.luminescenceOrColor.resolution = GetSize(arKitFrame.capturedImage);
m_colorImage.uv.resolution = m_colorImage.luminescenceOrColor.resolution / 2u;
#ifdef DEBUG
assert(m_colorImage.uv.resolution.x == GetSize(arKitFrame.capturedImage, 1).x);
auto format = CVPixelBufferGetPixelFormatType(arKitFrame.capturedImage);
assert(format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange);
#endif
m_depthImage.format = ArDepthFormat::METER_FP32;
m_depthImage.depth.resolution = GetDepthSize(arKitFrame);