less than 1 minute read

Categories:

Tags:

Even though 2D array is good for finding the surrounding index for the current index of investigation, a 1D array is fast most of the time(due to better cache locality and more โ€ฆ).

Height=4, Width=5

Given 2D index (i, j), its 1D index could be formulated as i + width*j

Example


for(int j=0; j<image.height; ++j)
{
  for(int i=0; i<iamge.width; ++i)
  {
    img.pixels[i+image.width*j].v[0] = std::clamp(image.pixels[i+image.width*j]*0.88f, 0.0f, 1.0f);
    img.pixels[i+image.width*j].v[1] = std::clamp(image.pixels[i+image.width*j]*0.88f, 0.0f, 1.0f);
    img.pixels[i+image.width*j].v[2] = std::clamp(image.pixels[i+image.width*j]*0.88f, 0.0f, 1.0f);
  }
}

Leave a comment