Getting error message on Raspberry Pi V2 Camera Colour Detection code

Hi, I’m using a Raspberry pi with a V2 camera and am running this code and getting the error message “ArrayIndexOutOfBoundsException: 64001”. I have ran this code on a mac computer and I do not get this error. It seems to only be happening when I run it on the Pi. Any suggestions/help


import gohai.glvideo.*;
GLCapture video;



GLCapture cap;
PImage bg_img;

color bgColor = color(0, 0, 0);

int rMargin = 1;
int rWidth = 320;
int rHeight = 200;

color input = color(0, 0, 0);
color background = color(255, 255, 255);
color current;
int bgTolerance = 5;

PImage myImage;

void setup() {
  size(320, 200, P2D);

  // start the webcam
  String[] devices = GLCapture.list();
  println("Devices:");
  printArray(devices);
  if (0 < devices.length) {
    String[] configs = GLCapture.configs(devices[0]);
    println("Configs:");
    printArray(configs);
  }
  video = new GLCapture(this);
  video.start();
  myImage = loadImage("ALL final.jpg");
}


void draw() {
  if (video.available()) {
    video.read();

    image(video, 0, 0);
    video.loadPixels();

    noFill();
    rect(rMargin, rMargin, rWidth, rHeight);

    int yCenter = (rWidth/2) + rMargin;
    int xCenter = (rWidth/2) + rMargin;
    // rectMode(CENTER);

    int rectCenterIndex = (width* yCenter) + xCenter;

    int r = 0, g = 0, b = 0;


    //CALCULATE AVG COLOR:
    int i;
    for (int x = 0; x <= width; x++) {
      for (int y = 0; y <= height; y++) {
        if (x >= rMargin && x <= rMargin + rWidth && y >= rMargin && y <= rMargin + rHeight) {

          i = (width*y) + x;
          color c = video.pixels[i];
          r += c>>16&0xFF;
          g += c>>8&0xFF;
          b += c&0xFF;
        }
      }
    }
    //divide by just the area sampled (x >= 50 && x <= 150 && y >= 50 && y <= 150 is a 100x100 px area) 
    int totalSampledPixels = rWidth * rHeight;

    r /= totalSampledPixels;
    g /= totalSampledPixels;
    b /= totalSampledPixels;

    if ((r<128) || (r>133)) {
      image(myImage, 0, 0);
    }

    if ((g<118) || (g>123)) {
      image(myImage, 0, 0);
    }

    if ((b<111) || (b>115)) {
      image(myImage, 0, 0);
    }

    println(r + " " + g + " " + b);
  }
}

i would use only

for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {

so for me the question is why it works on MAC