From 80c3555f87df37eee89648f9daaefb2e98a6a563 Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan.grewe@g-node.org>
Date: Fri, 27 Oct 2023 14:00:35 +0200
Subject: [PATCH] [writer] write raw data to file

---
 pylonrecorder.cpp |  2 +-
 writer.cpp        | 87 +++++++++++++++++++++++++++++------------------
 writer.h          |  3 +-
 3 files changed, 56 insertions(+), 36 deletions(-)

diff --git a/pylonrecorder.cpp b/pylonrecorder.cpp
index 20910f3..4de68ea 100644
--- a/pylonrecorder.cpp
+++ b/pylonrecorder.cpp
@@ -689,7 +689,7 @@ std::string PylonRecorder::createFilename() {
   QDateTime dt(QDateTime::currentDateTimeUtc());
   QDate date = dt.date();
   std::string base = (date.toString("yyyy.MM.dd") + "_").toStdString();
-  std::string extension = ".avi";
+  std::string extension = ".raw";
   QString idx = QString::number(movieCount);
   std::string fname = base + idx.toStdString() + extension;
   while (QFile::exists(QString::fromStdString(fname))) {
diff --git a/writer.cpp b/writer.cpp
index 5f1a1de..2a84929 100644
--- a/writer.cpp
+++ b/writer.cpp
@@ -1,6 +1,8 @@
 #include "writer.h"
 #include <chrono>
+#include <fstream>
 
+using namespace std::chrono;
 
 void Writer::setVideoSpecs(VideoSpecs specs) {
   videoSpecs = specs;
@@ -25,14 +27,22 @@ void Writer::writeMetadata(nix::Section &s){
 void Writer::run() {
   size_t count = 0;
   size_t chunksize = 256;
-
+  // size_t framecount = 0;
   if (specs_valid) {
       stop_request = false;
       stopNow = false;
-      Pylon::CVideoWriter videoWriter;
-      videoWriter.SetParameter(videoSpecs.width, videoSpecs.height, videoSpecs.pixelType,
-                               videoSpecs.fps, videoSpecs.quality);
-      videoWriter.Open(videoSpecs.filename.c_str());
+
+      std::ofstream myFile;
+      myFile.open(videoSpecs.filename, std::ios::out | std::ios::binary);
+      myFile.write((char*)&videoSpecs.width, 4);
+      myFile.write((char*)&videoSpecs.height, 4);
+      // Pylon::CVideoWriter videoWriter;
+      // videoWriter.SetParameter(videoSpecs.width, videoSpecs.height, videoSpecs.pixelType,
+                              //  videoSpecs.fps, videoSpecs.quality);
+      // std::cout << !Pylon::CVideoWriter::IsSupported() << std::endl;
+      //  videoWriter.SetParameter();
+      // videoWriter.Open(videoSpecs.filename.c_str());
+      // std::cerr << "Video writer is open!" << videoWriter.IsOpen() << std::endl;
       nix::File nix_file =nix::File::open(videoSpecs.filename + ".nix", nix::FileMode::Overwrite, "hdf5", nix::Compression::DeflateNormal);
       nix::Block b = nix_file.createBlock("Recording", "nix.recording");
       nix::Section s = nix_file.createSection("Recording", "nix.recording");
@@ -72,44 +82,53 @@ void Writer::run() {
       nix::NDSize offset(1, 0);
       nix::NDSize current_shape(initial_shape);
       nix::NDSize chunk_shape(1, chunksize);
-      MyImage img;
       while ((!stop_request || buffer->bufferLoad() > 0) && !stopNow) {
           if (buffer->bufferLoad() > 0 ) {
-              bool valid = buffer->pop(img);
-              if (valid) {
-                  Pylon::CPylonImage pyImage;
-                  try {
-                    pyImage.AttachUserBuffer(img.data(), videoSpecs.width * videoSpecs.height, videoSpecs.pixelType, videoSpecs.width, videoSpecs.height, 0, videoSpecs.orientation);
-                    videoWriter.Add( pyImage );
-                  } catch (const Pylon::GenericException &e) {
-                    std::cerr << "Writer::run: An exception occurred." << std::endl << e.GetDescription() << std::endl;
-                  }
-                  if (count < chunksize) {
-                      stamps_buffer[count] = nix::util::timeToStr(img.timestamp());
-                      ids_buffer[count] = img.index();
-                      count ++;
-                  } else {
-                      frametimes.setData(nix::DataType::String, stamps_buffer.data(), chunk_shape, offset);
-                      frameindices.setData(nix::DataType::Int64, ids_buffer.data(), chunk_shape, offset);
-                      current_shape += initial_shape;
-                      frametimes.dataExtent(current_shape);
-                      frameindices.dataExtent(current_shape);
-                      offset[0] += chunksize;
-                      count = 0;
-                   }
-                }
-            } else {
-              while (buffer->bufferLoad() < 1 && !stop_request) {
-                  msleep(10);
-                }
+            MyImage *img = buffer->pop();
+            if (img != nullptr) {
+              auto start = high_resolution_clock::now();
+              // framecount += 1;
+              myFile.write((char*)img->data(), img->size());
+
+              // Pylon::CPylonImage pyImage;
+              // try {
+              //     pyImage.AttachUserBuffer(img->data(), videoSpecs.width * videoSpecs.height, videoSpecs.pixelType, videoSpecs.width, videoSpecs.height, 0, videoSpecs.orientation);
+              //     // Test duration of writing...
+              //     videoWriter.Add( pyImage );
+              
+              // } catch (const Pylon::GenericException &e) {
+              //   std::cerr << "Writer::run: An exception occurred." << std::endl << e.GetDescription() << std::endl;
+              // }
+              auto stop = high_resolution_clock::now();
+              auto duration = duration_cast<microseconds>(stop - start);
+              std::cerr << "wrote binary to file " << duration.count() << std::endl;
+              if (count < chunksize) {
+                stamps_buffer[count] = nix::util::timeToStr(img->timestamp());
+                ids_buffer[count] = img->index();
+                count ++;
+              } else {
+                frametimes.setData(nix::DataType::String, stamps_buffer.data(), chunk_shape, offset);
+                frameindices.setData(nix::DataType::Int64, ids_buffer.data(), chunk_shape, offset);
+                current_shape += initial_shape;
+                frametimes.dataExtent(current_shape);
+                frameindices.dataExtent(current_shape);
+                offset[0] += chunksize;
+                count = 0;
+              }
+            }
+          } else {
+            while (buffer->bufferLoad() < 1 && !stop_request) {
+              msleep(10);
             }
+          }
         }
         if (count > 0) {
             chunk_shape[0] = count;
             frametimes.setData(nix::DataType::String, stamps_buffer.data(), chunk_shape, offset);
             frameindices.setData(nix::DataType::Int64, ids_buffer.data(), chunk_shape, offset);
         }
-        videoWriter.Close();
+        // videoWriter.Close();
+        myFile.close();
         nix_file.close();
     } else {
       std::cerr << "Got no video specifications, not writing!" << std::endl;
diff --git a/writer.h b/writer.h
index 8d1e299..161dad7 100644
--- a/writer.h
+++ b/writer.h
@@ -9,10 +9,11 @@
 #include "pylonwrapper.h"
 #include "imagebuffer.h"
 #include "projectsettings.h"
+#include <opencv2/opencv.hpp>
 
 struct VideoSpecs {
   std::string filename;
-  uint32_t width, height, quality = 95;
+  uint32_t width, height, quality = 10 ;
   int fps;
   double exposureTime;
   double detectorGain;