From c90b9ed4ff888a79f57f9c47f3e0b940a944e4cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20=C3=9Cbelh=C3=B6r?= <tim.uebelhoer@irt.rwt-aachen.de>
Date: Sun, 3 Mar 2019 17:26:33 +0100
Subject: [PATCH] templated rasterizer image. fixed not implemented bug.

---
 .../scigl_render/render/depth_simulator.hpp   |  3 +-
 include/scigl_render/render/rasterizer.hpp    | 34 ++++++++++++++-----
 src/buffer/frame_buffer.cpp                   |  2 +-
 src/render/rasterizer.cpp                     | 22 ------------
 4 files changed, 29 insertions(+), 32 deletions(-)

diff --git a/include/scigl_render/render/depth_simulator.hpp b/include/scigl_render/render/depth_simulator.hpp
index bf2ec32..2ca2d1f 100644
--- a/include/scigl_render/render/depth_simulator.hpp
+++ b/include/scigl_render/render/depth_simulator.hpp
@@ -29,7 +29,8 @@ public:
   static const GLenum FORMAT = GL_RED;
   static const GLenum TYPE = GL_FLOAT;
   static const GLint INTERNAL_FORMAT = GL_R32F;
-  static const size_t PIXEL_SIZE = sizeof(GLfloat);
+  static const size_t CHANNELS = 1;
+  static const size_t PIXEL_SIZE = CHANNELS * sizeof(GLfloat);
 
 private:
   // where and how to render
diff --git a/include/scigl_render/render/rasterizer.hpp b/include/scigl_render/render/rasterizer.hpp
index b41dac2..5b37151 100644
--- a/include/scigl_render/render/rasterizer.hpp
+++ b/include/scigl_render/render/rasterizer.hpp
@@ -12,8 +12,9 @@ class Rasterizer
 {
 public:
   /**
-   * Defines a header that is compatible to the OpenCV Mat_<float> constructor.
+   * Defines a header that is compatible to the OpenCV Mat_<T> constructor.
    */
+  template <typename T>
   struct ImageHeader
   {
     /** height of the image */
@@ -21,7 +22,7 @@ public:
     /** width of the image */
     size_t columns;
     /** points to the first pixel of the image in the texture */
-    void *data;
+    T *data;
     /** size in bytes of one row in the image */
     size_t step;
   };
@@ -66,19 +67,36 @@ public:
    * \param row of the view
    * \param column of the view
    * \param data pointer to the full texture
-   * \param pixel_size size of one pixel in bytes (sizeof(type) * n_channels)
+   * \param channels number of channels in the image
    */
-  ImageHeader get_image(size_t row, size_t column, void *data,
-                             size_t pixel_size) const;
+  template <typename T>
+  ImageHeader<T> get_image(size_t row, size_t column, void *data,
+                           size_t channels) const
+  {
+    auto xy = view_xy(row, column);
+    // all the rows above and the columns in front
+    ImageHeader<T> header;
+    header.data = static_cast<T *>(data) +
+                  (xy.second * texture_width + xy.first) * channels;
+    header.columns = width;
+    header.rows = height;
+    header.step = texture_width * channels * sizeof(T);
+    return header;
+  }
 
   /**
    * Create metadata to read one view as image from the texture of all views.
    * \param view get the image for this view
    * \param data pointer to the full texture
-   * \param pixel_size size of one pixel in bytes (sizeof(type) * n_channels)
+   * \param channels number of channels in the image
    */
-  ImageHeader get_image(size_t view, void *data,
-                             size_t pixel_size) const;
+  template <typename T>
+  ImageHeader<T> get_image(size_t view, void *data,
+                           size_t channels) const
+  {
+    auto pos = view_position(view);
+    return get_image<T>(pos.first, pos.second, data, channels);
+  }
 
   size_t get_views_per_row() const;
   size_t get_views_per_column() const;
diff --git a/src/buffer/frame_buffer.cpp b/src/buffer/frame_buffer.cpp
index 897c940..a4d634b 100644
--- a/src/buffer/frame_buffer.cpp
+++ b/src/buffer/frame_buffer.cpp
@@ -56,7 +56,7 @@ void FrameBuffer::clear(float color, float depth, int stencil) const
   deactivate();
 }
 
-static int get_max_size()
+int FrameBuffer::get_max_size()
 {
   int max_rbo_size;
   glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &max_rbo_size);
diff --git a/src/render/rasterizer.cpp b/src/render/rasterizer.cpp
index 7721f63..18156a5 100644
--- a/src/render/rasterizer.cpp
+++ b/src/render/rasterizer.cpp
@@ -92,28 +92,6 @@ void Rasterizer::set_view_height(size_t height)
   texture_height = views_per_column * height;
 }
 
-Rasterizer::ImageHeader Rasterizer::get_image(
-    size_t row, size_t column, void *data, size_t pixel_size) const
-{
-  auto xy = view_xy(row, column);
-  // all the rows above and the columns in front
-  ImageHeader header;
-  // pixel size is in bytes so do arithmetics in bytes
-  header.data = static_cast<uint8_t *>(data) +
-                (xy.second * texture_width + xy.first) * pixel_size;
-  header.columns = width;
-  header.rows = height;
-  header.step = texture_width * pixel_size;
-  return header;
-}
-
-Rasterizer::ImageHeader Rasterizer::get_image(
-    size_t view, void *data, size_t pixel_size) const
-{
-  auto pos = view_position(view);
-  return get_image(pos.first, pos.second, data, pixel_size);
-}
-
 std::pair<size_t, size_t> Rasterizer::view_position(size_t view) const
 {
   if (view > views_per_row * views_per_column)
-- 
GitLab