迭代一维数组内矩形区域的算法(位图)

Sou*_*aws 9 c++ arrays algorithm bitmap

这是一个奇怪的问题,我很难写出一个标题.

我正在使用像素(位图,更具体地说),并且无法找出用于实际访问每个阵列单元的(简单)数学.

我的画布是[n16 x 16]像素,n总是1或更大.

这是一张基本n = 2画布的照片:

http://i.imgur.com/mabwQfJ.png

在此输入图像描述

我想要我的魔法算法是从0到495运行而不触及那个较浅的灰色区域然后从16到512(实际上是单元格511,我的坏)不触及暗灰色区域.

所以,0到15,跳过16到31,然后是32到47,等等.

并且对于n = 3:

http://i.imgur.com/TqJMWl6.png

在此输入图像描述

在这种情况下,它将是0-735跳过较浅的灰色区域,16-751跳过每侧的区域,32-767跳过较暗的灰色区域.

我尝试了什么:

这是我的代码的摘录,希望它有用并显示我已经尝试过的内容.这是确定'idxpos'价值的部分.

// Let's say length = 3 for now.
for (int character = 0; character < length; ++character)
{
    // in case you're wondering, it grabs 16x16 characters from an ASCII spritesheet
    charpos = (string[character] - ' ') * 16 * 16;

    // Runs through the spritesheet character map
    // this is a huge 16x1520 bitmap.
    for (int pixel = 0; pixel < 16 * 16; ++pixel)
    {
        // ignore this, just me messing around with pixel tinting
        r = (((CharMap[charpos + pixel] >> 0) & 0xFF) + 255 - u);
        g = (((CharMap[charpos + pixel] >> 8) & 0xFF) + 255 - v);
        b = (((CharMap[charpos + pixel] >> 16) & 0xFF) + 255 - w);
        newcolour = RGB(r, g, b);

        // THIS is the part I am stuck on:
        idxpos = pixel + (character * 16 * 16);

        bitmap[idxpos] = CharMap[charpos + j];
    }
}
Run Code Online (Sandbox Code Playgroud)

你可能会得到这个想法.这对我来说听起来很简单,但我无法理解.

哦,我对一些可以为我处理所有位图内容的神奇库感兴趣,我不能处理我可以使用的位置.

Fil*_*ski 2

如果我正确理解了您的问题,您想按照您提到的顺序访问它们。这是执行此操作的代码(给定您的n):

for(int i = 0; i < n; i++) //which section we are going through
{
  for(int row = 0; row < size; row++) //size = 16, better use on of your constants
  { 
    for(int col = 0; col < size; col++)
    {
      int pixelIndex = size * (row * n) + col + size * i; 
      /*the last one is an offset - it moves the 
      index to the right as far as we need.
      If you need two coordinates (as in (x,y))
      instead of one number, it is: */
      int x = row, y = col + size * i;
      doSomethingWithPixel(pixelIndex);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。