From 03d31033554a1c29f21f69232d373ac3bc130e5e Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sat, 14 Mar 2020 14:57:49 +0200
Subject: [PATCH] Update example to write depth map

---
 SDK/C/examples/image_write/main.cpp | 47 +++++++++++++++--------------
 SDK/C/src/streams.cpp               |  1 +
 2 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/SDK/C/examples/image_write/main.cpp b/SDK/C/examples/image_write/main.cpp
index da340f4d7..882feb48e 100644
--- a/SDK/C/examples/image_write/main.cpp
+++ b/SDK/C/examples/image_write/main.cpp
@@ -3,35 +3,38 @@
 #define LOGURU_REPLACE_GLOG 1
 #include <loguru.hpp>
 
+static void ftlCheck(ftlError_t err) {
+	if (err != FTLERROR_OK) {
+		LOG(ERROR) << "FTL Stream Error: " << err;
+		exit(-1);
+	}
+}
+
 int main(int argc, char **argv) {
 	ftlStream_t s = ftlCreateWriteStream("./out.ftl");
-	if (!s) {
-		LOG(ERROR) << "Create stream error: " << ftlGetLastStreamError(s);
-		return -1;
-	}
+	if (!s) ftlCheck(ftlGetLastStreamError(s));
 
-	// Single red test image
-	cv::Mat test_image(720, 1280, CV_8UC4, cv::Scalar(0,0,255,255));
+	// Two test images, red and green
+	cv::Mat test_image1(720, 1280, CV_8UC4, cv::Scalar(0,0,255,255));
+	cv::Mat test_image2(720, 1280, CV_8UC4, cv::Scalar(0,255,0,255));
 
-	ftlError_t err;
+	// Two test depth maps
+	cv::Mat test_depth1(720, 1280, CV_32F, cv::Scalar(3.0f));
+	cv::Mat test_depth2(720, 1280, CV_32F, cv::Scalar(2.0f));
 
-	err = ftlIntrinsicsWriteLeft(s, 0, 1280, 720, 300.0f, -1280.0f/2.0f, -720.0f/2.0f, 0.1f, 0.1f, 8.0f);
-	if (err != FTLERROR_OK) {
-		LOG(ERROR) << "Intrinsics write error: " << err;
-		return -1;
-	}
+	// Write red image
+	ftlCheck(ftlIntrinsicsWriteLeft(s, 0, 1280, 720, 300.0f, -1280.0f/2.0f, -720.0f/2.0f, 0.1f, 0.1f, 8.0f));
+	ftlCheck(ftlImageWrite(s, 0, FTLCHANNEL_Colour, FTLIMAGE_BGRA, test_image1.step, test_image1.data));
 
-	err = ftlImageWrite(s, 0, FTLCHANNEL_Colour, FTLIMAGE_BGRA, test_image.step, test_image.data);
-	if (err != FTLERROR_OK) {
-		LOG(ERROR) << "Image write error: " << err;
-		return -1;
-	}
+	// Write green image
+	ftlCheck(ftlIntrinsicsWriteLeft(s, 1, 1280, 720, 300.0f, -1280.0f/2.0f, -720.0f/2.0f, 0.1f, 0.1f, 8.0f));
+	ftlCheck(ftlImageWrite(s, 1, FTLCHANNEL_Colour, FTLIMAGE_BGRA, test_image2.step, test_image2.data));
 
-	err = ftlDestroyStream(s);
-	if (err != FTLERROR_OK) {
-		LOG(ERROR) << "Destroy stream error: " << err;
-		return -1;
-	}
+	// Write depth images
+	ftlCheck(ftlImageWrite(s, 0, FTLCHANNEL_Depth, FTLIMAGE_FLOAT, test_depth1.step, test_depth1.data));
+	ftlCheck(ftlImageWrite(s, 1, FTLCHANNEL_Depth, FTLIMAGE_FLOAT, test_depth2.step, test_depth2.data));
+
+	ftlCheck(ftlDestroyStream(s));
 
 	return 0;
 }
diff --git a/SDK/C/src/streams.cpp b/SDK/C/src/streams.cpp
index 256bbc9f4..41bf3b668 100644
--- a/SDK/C/src/streams.cpp
+++ b/SDK/C/src/streams.cpp
@@ -122,6 +122,7 @@ ftlError_t ftlImageWrite(
 		if (img.empty()) return FTLERROR_STREAM_NO_DATA;
 
 		ftl::codecs::Channels<0> channels;
+		if (stream->stream->size() > static_cast<unsigned int>(stream->video_fs.id)) channels = stream->stream->selected(stream->video_fs.id);
 		channels += static_cast<ftl::codecs::Channel>(channel);
 		stream->stream->select(stream->video_fs.id, channels, true);
 
-- 
GitLab