我试图将图像的RGB像素分别映射到R,G,B的2D阵列。
当读取图像时,像素以{r1,g1,b1,r2,g2,b2 ......}的形式存储在1D阵列中。
数组的长度为3*height*width
。 2D阵列的宽度为X高度
for(i = 0; i < length; i++) { // length = 3*height*width
image[i][2] = getc(f); // blue pixel
image[i][1] = getc(f); // green pixel
image[i][0] = getc(f); // red pixel
img[count] = (unsigned char)image[i][0];
count += 1;
img[count] = (unsigned char)image[i][1];
count += 1;
img[count] = (unsigned char)image[i][2];
count += 1;
printf("pixel %d : [%d,%d,%d]\n", i+1, image[i][0], image[i][1], image[i][2]);
}
RGB值位于img[]
。 2d数组是红色[] [],绿色[] []和蓝色[] []。
请帮忙!
答案 0 :(得分:2)
据我了解,您正在尝试重建色域。只需改变你的功能:
unsigned char * imgptr = img;
for( int y = 0; y < height; y++ ) {
for( int x = 0; x < width; x++ ) {
red[y][x] = *imgptr++;
green[y][x] = *imgptr++;
blue[y][x] = *imgptr++;
}
}
动态创建数组:
unsigned char** CreateColourPlane( int width, int height )
{
int i;
unsigned char ** rows;
const size_t indexSize = height * sizeof(unsigned char*);
const size_t dataSize = width * height * sizeof(unsigned char);
// Allocate memory for row index and data segment. Note, if using C compiler
// do not cast the return value from malloc.
rows = (unsigned char**) malloc( indexSize + dataSize );
if( rows == NULL ) return NULL;
// Index rows. Data segment begins after row index array.
rows[0] = (unsigned char*)rows + height;
for( i = 1; i < height; i++ ) {
rows[i] = rows[i-1] + width;
}
return rows;
}
然后:
unsigned char ** red = CreateColourPlane( width, height );
unsigned char ** green = CreateColourPlane( width, height );
unsigned char ** blue = CreateColourPlane( width, height );
你可以轻松地释放它们,但如果你包装了分配器函数,它总是需要包装自由函数:
void DeleteColourPlane( unsigned char** p )
{
free(p);
}