From 087cd3f22124e8fc09b113958ecb144cc3ba3e07 Mon Sep 17 00:00:00 2001
From: Matthias Vogelgesang <matthias.vogelgesang@kit.edu>
Date: Tue, 20 Jun 2017 09:44:03 +0200
Subject: Allow IPECAMERA_WIDTH to be set during configure

---
 README.md          |  5 +++++
 src/CMakeLists.txt |  2 ++
 src/config.h.in    |  1 +
 src/ufodecode.c    | 11 ++++-------
 4 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/README.md b/README.md
index 43050ee..da3b21b 100644
--- a/README.md
+++ b/README.md
@@ -5,6 +5,11 @@ library to decode frames from the UFO camera developed at IPE/KIT. It is able to
 decode frames produced with firmware versions 4, 5 and 5 with 12-bit support.
 The library is a dependency of pcilib to decode frames on-the-fly.
 
+To set the number of pixels in x-direction other than the default of 5120
+pixels, you have to pass that number in the configuration step, i.e.
+
+    $ cmake -DIPECAMERA_WIDTH=2048 <src-dir>
+
 This package also contains a stand-alone offline decoder called `ipedec` to
 decode raw frames acquired with the `pcitool` program. More information is
 available by calling
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 85feff4..f0edde7 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -31,6 +31,8 @@ include_directories(
     ${CMAKE_CURRENT_BINARY_DIR}
 )
 
+set(IPECAMERA_WIDTH "5120" CACHE STRING "Sensor width in pixels (default: 5120)")
+
 configure_file("config.h.in"
                "${CMAKE_CURRENT_BINARY_DIR}/config.h")
 
diff --git a/src/config.h.in b/src/config.h.in
index 37a1399..766377c 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -1,2 +1,3 @@
 #cmakedefine DEBUG
 #cmakedefine HAVE_SSE
+#define IPECAMERA_WIDTH     ${IPECAMERA_WIDTH}
diff --git a/src/ufodecode.c b/src/ufodecode.c
index a3c07ec..ea189d6 100644
--- a/src/ufodecode.c
+++ b/src/ufodecode.c
@@ -13,9 +13,6 @@
 #define IPECAMERA_NUM_ROWS              1088
 #define IPECAMERA_NUM_CHANNELS          16      /**< Number of channels per row */
 #define IPECAMERA_PIXELS_PER_CHANNEL    128     /**< Number of pixels per channel */
-#define IPECAMERA_WIDTH (IPECAMERA_NUM_CHANNELS * IPECAMERA_PIXELS_PER_CHANNEL) /**< Total pixel width of row */
-
-#define IPECAMERA_WIDTH_20MP            5120
 
 #define IPECAMERA_MODE_16_CHAN_IO	0
 #define IPECAMERA_MODE_4_CHAN_IO	2
@@ -233,7 +230,7 @@ ufo_decode_frame_channels_v6 (UfoDecoder *decoder, uint16_t *pixel_buffer, uint3
 {
     size_t base = 0;
     size_t index = 0;
-    const size_t space = 640;
+    const size_t space = IPECAMERA_PIXELS_PER_CHANNEL;
 
 #ifdef HAVE_SSE
     const __m64 mask_fff = _mm_set_pi32 (0xfff, 0xfff);
@@ -245,7 +242,7 @@ ufo_decode_frame_channels_v6 (UfoDecoder *decoder, uint16_t *pixel_buffer, uint3
         const size_t pixel_number = (raw[base + 1] >> 16) & 0xfff;
 
         base += 2;
-        index = row_number * IPECAMERA_WIDTH_20MP + pixel_number;
+        index = row_number * IPECAMERA_WIDTH + pixel_number;
 
 #ifdef HAVE_SSE
         const __m64 src1 = _mm_set_pi32 (raw[base], raw[base + 3]);
@@ -254,7 +251,7 @@ ufo_decode_frame_channels_v6 (UfoDecoder *decoder, uint16_t *pixel_buffer, uint3
 
 #define store(i) \
         pixel_buffer[index + i * space] = ((uint32_t *) &mm_r)[1]; \
-        pixel_buffer[index + IPECAMERA_WIDTH_20MP + i * space] = ((uint32_t *) &mm_r)[0];
+        pixel_buffer[index + IPECAMERA_WIDTH + i * space] = ((uint32_t *) &mm_r)[0];
 
         mm_r = _mm_srli_pi32 (src1, 20);
         store(0);
@@ -291,7 +288,7 @@ ufo_decode_frame_channels_v6 (UfoDecoder *decoder, uint16_t *pixel_buffer, uint3
         pixel_buffer[index + 6 * space] = (raw[base + 2] >> 12) & 0xfff;
         pixel_buffer[index + 7 * space] = raw[base + 2] & 0xfff;
 
-        index += IPECAMERA_WIDTH_20MP;
+        index += 8 * space;
         pixel_buffer[index + 0 * space] = (raw[base + 3] >> 20);
         pixel_buffer[index + 1 * space] = (raw[base + 3] >> 8) & 0xfff;
         pixel_buffer[index + 2 * space] = ((raw[base + 3] << 4) & 0xfff) | (raw[base + 4] >> 28);
-- 
cgit v1.2.3