RISP4ML Clean-ups

changes:
- removed assert
- added more explanations on algorithms in downscale_rvv.c
- renamed macros to all capitals

Change-Id: Ia91d10bb23a4704b575dcced3707ea9d9f6cfc03
diff --git a/samples/risp4ml/common/constants.h b/samples/risp4ml/common/constants.h
index 336de71..f3e2ebd 100644
--- a/samples/risp4ml/common/constants.h
+++ b/samples/risp4ml/common/constants.h
@@ -56,7 +56,17 @@
   kBggr = 3,
 } BayerPattern;
 
-#define kNumBayerPatterns 4
+#ifndef NUM_BAYER_PATTERNS
+#define NUM_BAYER_PATTERNS 4
+#endif
+
+#ifndef BAYER_COLOR_CHANNELS
+#define BAYER_COLOR_CHANNELS 4
+#endif
+
+#ifndef RGB_COLOR_CHANNELS
+#define RGB_COLOR_CHANNELS 3
+#endif
 
 // TODO(alexkaplan): Add a way to update this based on the image
 // or to make sure the BayerType corresponds to the loaded image
diff --git a/samples/risp4ml/isp_stages/blc.h b/samples/risp4ml/isp_stages/blc.h
index 159ce80..f77ae6a 100644
--- a/samples/risp4ml/isp_stages/blc.h
+++ b/samples/risp4ml/isp_stages/blc.h
@@ -25,7 +25,7 @@
 
 typedef struct {
   bool enable;
-  pixel_type_t offsets[kNumBayerPatterns];
+  pixel_type_t offsets[NUM_BAYER_PATTERNS];
 } BlcParams;
 
 void set_blc_params(BlcParams* params);
diff --git a/samples/risp4ml/isp_stages/demosaic.c b/samples/risp4ml/isp_stages/demosaic.c
index 231b330..4cbd8c9 100644
--- a/samples/risp4ml/isp_stages/demosaic.c
+++ b/samples/risp4ml/isp_stages/demosaic.c
@@ -14,13 +14,9 @@
  * limitations under the License.
  */
 
-#include <assert.h>
-
 #include "samples/risp4ml/common/utils.h"
 #include "samples/risp4ml/isp_stages/demosaic.h"
 
-#define kRgbColorChannels 3
-
 static DemosaicParams demosaic_params = {.enable = true};
 
 void set_demosaic_params(DemosaicParams* params) { demosaic_params = *params; }
@@ -33,8 +29,8 @@
   uint16_t height = input->height;
   uint16_t width = input->width;
 
-  const pixel_type_t* line_buffers[kRgbColorChannels];
-  int x_offset[kRgbColorChannels];
+  const pixel_type_t* line_buffers[RGB_COLOR_CHANNELS];
+  int x_offset[RGB_COLOR_CHANNELS];
 
   for (uint16_t y = 0; y < height; ++y) {
     line_buffers[0] = (y) ? image_row(input, 0, y - 1) : image_row(input, 0, 1);
@@ -43,7 +39,7 @@
                                        : image_row(input, 0, height - 2);
 
     for (uint16_t x = 0; x < width; ++x) {
-      for (uint16_t c = 0; c < kRgbColorChannels; ++c) {
+      for (uint16_t c = 0; c < RGB_COLOR_CHANNELS; ++c) {
         x_offset[c] = BayerMirrorBoundary(x - 1 + c, width);
       }
 
@@ -86,7 +82,7 @@
           *image_pixel(output, 2, y, x) = line_buffers[1][x_offset[1]];
         }; break;
         default: {
-          assert(0 && "Unexpected channel index");
+          break;
         }
       }
     }
diff --git a/samples/risp4ml/isp_stages/demosaic_rvv.c b/samples/risp4ml/isp_stages/demosaic_rvv.c
index 2ed9d25..0feab1a 100644
--- a/samples/risp4ml/isp_stages/demosaic_rvv.c
+++ b/samples/risp4ml/isp_stages/demosaic_rvv.c
@@ -19,8 +19,6 @@
 #include "samples/risp4ml/common/utils.h"
 #include "samples/risp4ml/isp_stages/demosaic.h"
 
-#define kRgbColorChannels 3
-
 static DemosaicParams demosaic_params = {.enable = true};
 
 void set_demosaic_params(DemosaicParams* params) { demosaic_params = *params; }
@@ -31,11 +29,11 @@
     return;
   }
 
-  const pixel_type_t* line_in[kRgbColorChannels];
-  pixel_type_t* line_out[kRgbColorChannels];
-  int x_offset[kRgbColorChannels];
-  const uint16_t boundary[kRgbColorChannels] = {0, input->width - 2,
-                                                input->width - 1};
+  const pixel_type_t* line_in[RGB_COLOR_CHANNELS];
+  pixel_type_t* line_out[RGB_COLOR_CHANNELS];
+  int x_offset[RGB_COLOR_CHANNELS];
+  const uint16_t boundary[RGB_COLOR_CHANNELS] = {0, input->width - 2,
+                                                 input->width - 1};
 
   size_t vl;
   // auxiliary variables
@@ -61,7 +59,7 @@
     // x at boundary
     for (uint8_t i = 0; i < 3; ++i) {
       uint16_t x = boundary[i];
-      for (uint16_t c = 0; c < kRgbColorChannels; ++c) {
+      for (uint16_t c = 0; c < RGB_COLOR_CHANNELS; ++c) {
         x_offset[c] = BayerMirrorBoundary(x - 1 + c, input->width);
       }
 
diff --git a/samples/risp4ml/isp_stages/dg.h b/samples/risp4ml/isp_stages/dg.h
index db7e295..bb87264 100644
--- a/samples/risp4ml/isp_stages/dg.h
+++ b/samples/risp4ml/isp_stages/dg.h
@@ -25,7 +25,7 @@
 
 typedef struct {
   bool enable;
-  pixel_type_t gains[kNumBayerPatterns];
+  pixel_type_t gains[NUM_BAYER_PATTERNS];
 } DgParams;
 
 void set_dg_params(DgParams* params);
diff --git a/samples/risp4ml/isp_stages/downscale.c b/samples/risp4ml/isp_stages/downscale.c
index a503a1a..e323cbb 100644
--- a/samples/risp4ml/isp_stages/downscale.c
+++ b/samples/risp4ml/isp_stages/downscale.c
@@ -14,8 +14,6 @@
  * limitations under the License.
  */
 
-#include <assert.h>
-
 #include "samples/risp4ml/common/utils.h"
 #include "samples/risp4ml/isp_stages/downscale.h"
 
@@ -52,7 +50,6 @@
 // Resamples image using bilinear interpolation.
 // 'output' is modified by this function to store the output image.
 void downscale_process(Image* input, ImageU8* output) {
-  assert(input->num_channels == output->num_channels);
   if (!params.enable) {
     return;
   }
diff --git a/samples/risp4ml/isp_stages/downscale_rvv.c b/samples/risp4ml/isp_stages/downscale_rvv.c
index 454bd6c..70c2877 100644
--- a/samples/risp4ml/isp_stages/downscale_rvv.c
+++ b/samples/risp4ml/isp_stages/downscale_rvv.c
@@ -51,11 +51,11 @@
   size_t n = output->height * output->width;
   // auxiliary variables
   vuint32m8_t vx, vy, vz, vid, vp, vq;
-  // neiboring x & y coordinates
+  // neighboring x & y coordinates
   vuint32m8_t vx_l, vy_l, vx_h, vy_h;
   // weights of neighbors
   vuint32m8_t vx_weight, vy_weight, vx_weight_1minus, vy_weight_1minus;
-  // neighboring data points
+  // values of neighboring data points
   vuint32m8_t va, vb, vc, vd;
   vuint32m8_t vo;      // 32bit output
   vuint16m4_t vo_16b;  // 16bit output
@@ -73,7 +73,9 @@
       vy = vdivu(vid, output->width, vl);
       vx = vremu(vid, output->width, vl);
 
-      // find neighbors
+      // find 4 neighboring points
+      // four neighboring coordinates are
+      // [y_l, x_l], [y_l, x_h], [y_h, x_l], [y_h, x_h]
       vx_l = vmul(vx, input_w_1, vl);
       vx_l = vdivu(vx_l, w_1, vl);
       vx_h = vadd(vx_l, 1, vl);
@@ -84,7 +86,7 @@
       vy_h = vadd(vy_l, 1, vl);
       vy_h = vminu(vy_h, input_h_1, vl);  // clamp
 
-      // load a, b, c, d
+      // load values of four neighboring points: A, B C, D
       vz = vmul(vy_l, input_width, vl);
       vz = vadd(vz, vx_l, vl);
       vz = vsll(vz, 1, vl);  // *2
@@ -109,7 +111,7 @@
       vo_16b = vluxei32(in, vz, vl);
       vd = vwaddu_vx(vo_16b, 0, vl);
 
-      // weights
+      // compute weights of four neighboring points: wx, wy, 1-wx, 1-wy
       vp = vmul(vx, input_w_1, vl);
       vq = vmul(vx_l, w_1, vl);
       vp = vssubu(vp, vq, vl);
@@ -125,7 +127,9 @@
       vx_weight_1minus = vrsub(vx_weight, params.scale_fixed_one, vl);
       vy_weight_1minus = vrsub(vy_weight, params.scale_fixed_one, vl);
 
-      // resized
+      // Bilinear Interpolation Formular:
+      // out = A*(1-wx)*(1-wy) + B*wx*(1-wy)
+      //     + C*(1-wx)*wy + D*wx*wy
       vo = vmul(va, vx_weight_1minus, vl);
       vo = vsrl(vo, params.scale_precision, vl);
       vo = vmul(vo, vy_weight_1minus, vl);
@@ -145,7 +149,7 @@
       vp = vmul(vp, vy_weight, vl);
       vo = vadd(vo, vp, vl);
 
-      // bit shift
+      // bit shift from 32bits to 8bits
       vo_16b = vnsrl(vo, params.scale_precision, vl);
       vo_8b = vnsrl(vo_16b, kRawPipelineBpp - kPipeOutputBpp, vl);
 
diff --git a/samples/risp4ml/isp_stages/gamma.c b/samples/risp4ml/isp_stages/gamma.c
index b1dd975..396dc62 100644
--- a/samples/risp4ml/isp_stages/gamma.c
+++ b/samples/risp4ml/isp_stages/gamma.c
@@ -17,8 +17,6 @@
 #include "samples/risp4ml/common/utils.h"
 #include "samples/risp4ml/isp_stages/gamma.h"
 
-#define kRgbColorChannels 3
-
 static const uint16_t kRgbPipelineBpp = 16;
 static const uint32_t kRgbPipelineMaxVal = (1 << kRgbPipelineBpp) - 1;
 
@@ -46,15 +44,15 @@
 void gamma_process(Image* img) {
   if (!gamma_params.enable) return;
 
-  pixel_type_t* line[kRgbColorChannels];
+  pixel_type_t* line[RGB_COLOR_CHANNELS];
 
   for (uint16_t y = 0; y < img->height; ++y) {
-    for (uint16_t c = 0; c < kRgbColorChannels; ++c) {
+    for (uint16_t c = 0; c < RGB_COLOR_CHANNELS; ++c) {
       line[c] = image_row(img, c, y);
     }
 
     for (uint16_t x = 0; x < img->width; ++x) {
-      for (uint16_t c = 0; c < kRgbColorChannels; ++c) {
+      for (uint16_t c = 0; c < RGB_COLOR_CHANNELS; ++c) {
         pixel_type_t pixel_val =
             (pixel_type_t)Clamp(line[c][x], 0, kRgbPipelineMaxVal);
 
diff --git a/samples/risp4ml/isp_stages/gamma.h b/samples/risp4ml/isp_stages/gamma.h
index 24b42cd..f6da623 100644
--- a/samples/risp4ml/isp_stages/gamma.h
+++ b/samples/risp4ml/isp_stages/gamma.h
@@ -24,14 +24,14 @@
 #endif  // __cplusplus
 
 #ifndef ISP_WITH_RVV
-#define kGammaNumberPoints 81
+#define GAMMA_NUMBER_POINTS 81
 #else
-#define kGammaNumberPoints 2049
+#define GAMMA_NUMBER_POINTS 2049
 #endif
 
 typedef struct {
   bool enable;
-  pixel_type_t lut[kGammaNumberPoints];
+  pixel_type_t lut[GAMMA_NUMBER_POINTS];
 } GammaParams;
 
 void set_gamma_params(GammaParams* params);
diff --git a/samples/risp4ml/isp_stages/gamma_rvv.c b/samples/risp4ml/isp_stages/gamma_rvv.c
index a0f5503..c15b92c 100644
--- a/samples/risp4ml/isp_stages/gamma_rvv.c
+++ b/samples/risp4ml/isp_stages/gamma_rvv.c
@@ -18,8 +18,6 @@
 #include "samples/risp4ml/common/utils.h"
 #include "samples/risp4ml/isp_stages/gamma.h"
 
-#define kRgbColorChannels 3
-
 static const uint16_t kRgbPipelineBpp = 16;
 static const uint16_t kRgbPipelineMaxVal = (1 << kRgbPipelineBpp) - 1;
 static const uint16_t kGammaShiftBits = 5;
diff --git a/samples/risp4ml/isp_stages/gamma_rvv_test.cc b/samples/risp4ml/isp_stages/gamma_rvv_test.cc
index e379e6d..061ac8f 100644
--- a/samples/risp4ml/isp_stages/gamma_rvv_test.cc
+++ b/samples/risp4ml/isp_stages/gamma_rvv_test.cc
@@ -49,7 +49,7 @@
     for (int n = 0; n <= kRgbPipelineMaxVal; n += kGammaSpacing) {
       params->lut[n / kGammaSpacing] = n;
     }
-    params->lut[kGammaNumberPoints - 1] = kRgbPipelineMaxVal;
+    params->lut[GAMMA_NUMBER_POINTS - 1] = kRgbPipelineMaxVal;
   }
   float sRgb_gamma(float in_) {
     return (in_ < 0.0031308f) ? 12.92f * in_
@@ -64,7 +64,7 @@
           (1 << kRgbPipelineBpp) *
           sRgb_gamma((float)n / (1 << kRgbPipelineBpp));
     }
-    params->lut[kGammaNumberPoints - 1] = kRgbPipelineMaxVal;
+    params->lut[GAMMA_NUMBER_POINTS - 1] = kRgbPipelineMaxVal;
   }
 
   Image* in_;
diff --git a/samples/risp4ml/isp_stages/wbg.c b/samples/risp4ml/isp_stages/wbg.c
index 8598278..4eed39b 100644
--- a/samples/risp4ml/isp_stages/wbg.c
+++ b/samples/risp4ml/isp_stages/wbg.c
@@ -14,12 +14,9 @@
  * limitations under the License.
  */
 
-#include <assert.h>
-
 #include "samples/risp4ml/common/utils.h"
 #include "samples/risp4ml/isp_stages/wbg.h"
 
-#define kBayerColorChannels 4
 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
 
 static const uint16_t kWbgFractional = kRawPipelineFraction;
@@ -64,7 +61,7 @@
           num_of_blues++;
         }; break;
         default: {
-          assert(0 && "Unexpected channel index");
+          break;
         }
       }
     }
diff --git a/samples/risp4ml/isp_stages/wbg.h b/samples/risp4ml/isp_stages/wbg.h
index 7c9f14d..adcbd6f 100644
--- a/samples/risp4ml/isp_stages/wbg.h
+++ b/samples/risp4ml/isp_stages/wbg.h
@@ -26,7 +26,7 @@
 typedef struct {
   bool enable;
   bool fixed;
-  uint32_t gains[kNumBayerPatterns];
+  uint32_t gains[NUM_BAYER_PATTERNS];
 } WbgParams;
 
 void set_wbg_params(WbgParams* params);
diff --git a/samples/risp4ml/isp_stages/wbg_test.cc b/samples/risp4ml/isp_stages/wbg_test.cc
index 14da4d8..e0d126f 100644
--- a/samples/risp4ml/isp_stages/wbg_test.cc
+++ b/samples/risp4ml/isp_stages/wbg_test.cc
@@ -17,7 +17,6 @@
 #include "samples/risp4ml/common/test_utils.h"
 #include "samples/risp4ml/isp_stages/wbg.h"
 
-static constexpr uint16_t kBayerColorChannels = 4;
 static constexpr uint16_t kWbgFractional = kRawPipelineFraction;
 static constexpr uint16_t kFrameWidth = 4;
 static constexpr uint16_t kFrameHeight = 4;