/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "risp4ml/common/utils.h"

// int GetBayerIndex(int x, int y) {
//   // The Bayer pattern code defines which color is top left in the quad:
//   // +---+---+
//   // | R | Gr|
//   // +---+---+
//   // | Gb| B |
//   // +---+---+
//   return ((x & 1) + 2 * (y & 1));
// }

BayerIndex GetBayerIndex(BayerPattern bayerType, uint16_t x, uint16_t y) {
  // 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

  // Patterns 1 and 3 shift in the x
  uint16_t x_shift = (uint16_t)(bayerType == kGrbg || bayerType == kBggr);
  // Patterns 2 and 3 shift in the y
  uint16_t y_shift = (uint16_t)(bayerType == kGbrg || bayerType == kBggr);
  return (BayerIndex)(((x + x_shift) & 1) + 2 * ((y + y_shift) & 1));
}

int BayerMirrorBoundary(int x, int size) {
  if (x < 0)
    return (-x + 2 * (-x & 0x1) - 2);
  else if (x < size)
    return x;
  else
    return 2 * size - x - 2 * ((x - size + 1) & 0x1);
}
