Difference between loadPixels and get()

As the title implies just after a bit of info.

Looking through some examples I came across the histogram image sketch which makes use of get to retrieve pixel values.

for (int i = 0; i < img.width; i++) {
  for (int j = 0; j < img.height; j++) {
    int bright = int(brightness(get(i, j)));
    hist[bright]++; 
  }
}

what is the difference between that and

for (int i = 0; i < img.width; i++) {
  for (int j = 0; j < img.height; j++) {
    int bright = int(brightness(pixels[i+ j*img.width]));
    hist[bright]++; 
  }
}

other than the fact that pixels require loadPixels.

Just have a look at get(): :eyeglasses:

As we can see for ourselves get() does pretty much the same as we’d do by directly accessing pixels[], but w/ more checks; therefore slightly slower. :snail:

Method get() doesn’t invoke loadPixels() either! :scream:

So we should assume it’s needed as much as using pixels[] on our own. :face_with_hand_over_mouth:

For best contiguous memory speed access the outer loop gotta be height & the inner 1 be width: :racing_car:

for (int h = img.height, y = 0; y < h; ++y)
  for (int w = img.width, x = 0; x < w; ++x) {
4 Likes