#ifndef SAMPLES_RISP4ML_COMMON_UTILS_H_
#define SAMPLES_RISP4ML_COMMON_UTILS_H_

#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "samples/risp4ml/common/constants.h"

// Return the RAW color channel index at position (x, y) for a given Bayer
// pattern
// The Bayer pattern code defines which color is top left in the quad:
// 0: +---+---+ 1: +---+---+ 2: +---+---+ 3: +---+---+
//    | R | Gr|    | Gr| R |    | Gb| B |    | B | Gb|
//    +---+---+    +---+---+    +---+---+    +---+---+
//    | Gb| B |    | B | Gb|    | R | Gr|    | Gr| R |
//    +---+---+    +---+---+    +---+---+    +---+---+
// pattern 0 is base pattern and other patterns are shifted versions of the
// base
BayerIndex GetBayerIndex(BayerPattern bayerType, uint16_t x, uint16_t y);

// Get the corresponding index of x in bayer images for when the index is out
// of bounds and mirrored across the boundary.
int BayerMirrorBoundary(int x, int size);

inline uint32_t Clamp(uint32_t value, uint32_t low, uint32_t high) {
  return value < low ? low : (value > high ? high : value);
}

inline uint16_t SubUnsignedZeroClamp(uint16_t lhs, uint16_t rhs) {
  return rhs < lhs ? lhs - rhs : 0;
}

// Count the number of consecutive zeros from LHS in N msbs of the number
// represented using BPP bits
inline int ClzMsb(int in, int BPP, int N) {
  int lz = 0;
  while (lz < N && (in & (1 << (BPP - lz - 1))) == 0) {
    ++lz;
  }
  return lz;
}

inline float Roundf(float x) {
  int d = x < 0 ? x - 0.5 : x + 0.5;
  return (float)d;
}

// This function converts floating point value `x` to fixed point with the
// specified `integer_bit`, `frac_bit`, and `is_signed` flag.
// TODO(alexkaplan): Detect overflow/underflow.
inline int FloatToFixedPoint(float x, int integer_bit, int frac_bit,
                             bool is_signed) {
  float output_as_float = Roundf(x * (1 << frac_bit));
  float min_value = 0;
  float max_value = (1 << (frac_bit + integer_bit)) - 1;

  if (is_signed) {
    min_value = -(1 << (frac_bit + integer_bit - 1));
    max_value = (1 << (frac_bit + integer_bit - 1)) - 1;
  }

  // Clamp to the allowed range.
  if (output_as_float < min_value) {
    return (int)min_value;
  } else if (output_as_float > max_value) {
    return (int)max_value;
  }
  return (int)output_as_float;
}

// Helper function for fixed point rounding of values.
inline int Round(int value, int right_shift) {
  int carry = right_shift == 0 ? 0 : (value >> (right_shift - 1)) & 1;
  return (value >> right_shift) + carry;
}

// Helper function for linearly interpolating 2 values. When weight equals 0,
// output = val0. When weight equals 1.0 (when represented in floating point),
// output = val1.
inline int Lerp(int val0, int val1, int weight, int weight_precision) {
  return val0 + Round((val1 - val0) * weight, weight_precision);
}

#endif  // SAMPLES_RISP4ML_COMMON_UTILS_H_
