  #pragma version(1)
  #pragma rs java_package_name(cz.vutbr.bpdataacquisition)
  #pragma rs_fp_relaxed

  // AVG
  uint width;
  uint height;
  static int pixelSum = 0;

  // YUV to RGB
  uint vPixelStride, vRowStride;
  rs_allocation ypsIn, vIn;

void __attribute__((kernel)) getRedFromYuv(uint32_t x, uint32_t y) {
    uint uvIndex =  vPixelStride * (x/2) + vRowStride*(y/2);
    uchar yps = rsGetElementAt_uchar(ypsIn, x, y);
    uchar v = rsGetElementAt_uchar(vIn, uvIndex);

    uchar red = clamp(yps + v * 1436 / 1024 - 179, 0, 255);
    rsAtomicAdd(&pixelSum, red);
}

float __attribute__((kernel)) getRedAvg(int x){
    return (float)pixelSum / (float)(width * height);
}

void reset(){
    pixelSum = 0;
}