本文介绍了C ++表示1D数组中的3D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个1D数组中为每个3D坐标以像素阵列存储aFloat的字节值:



float aFloat = 1.0;
unsigned char * pixelsArray = new unsigned char [HEIGHT * WIDTH * 3];

for(int i = 0; i {
for(int j = 0; j< WIDTH; j ++)

for(int k = 0; k {
pixelsArray [?] = aFloat;
}
}
}

?我认为如果我没有错误,它还需要 + sizeof(float)在索引的某个地方。

解决方案

您的内线需要:

  pixelsArray [(i * WIDTH + j )* 3 + k] =(unsigned char)(255.0 * aFloat); 

这应该会给你一个全白的图像。



确保你的目标实际上是每个像素三个字节,而不是四个(alpha通道或填充);如果是四,您只需要将上面的 3 更改为 4


I want to store the byte value of aFloat in pixelsArray for each 3D coordinate, in a 1D array:

float aFloat = 1.0;
unsigned char* pixelsArray = new unsigned char[HEIGHT*WIDTH*3];

for (int i = 0; i < HEIGHT; i++)
{
   for (int j = 0; j < WIDTH; j++)
   {
      for (int k = 0; k < 3; k++)
      {
         pixelsArray[?] = aFloat;
      }
   }
}

What would go in the ?? I think it also needs to have + sizeof(float) somewhere in the index if I'm not mistaken.

解决方案

Your inside line needs to be:

pixelsArray[(i * WIDTH + j) * 3 + k] = (unsigned char)(255.0 * aFloat);

This should give you an all-white image.

Make sure your target is really three bytes per pixel and not four (alpha channel or padding); if it is four, you'll just need to change the 3 above to a 4.

这篇关于C ++表示1D数组中的3D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:33