【发布时间】:2016-06-30 14:28:08
【问题描述】:
我将图像数据存储在 unsigned char 数组中。数据的形式为RGB0RGB0/字节,即每个字节覆盖两个像素,在每个RGB之后填充一个0,使其与4的倍数对齐。为了进一步处理,我需要将每个颜色分量数据的 1 位转换为每个分量 8 位,即每种颜色 1 个字节。所以我正在做的是,对于每个字节我检查 MSB,如果它是1,我将一个字节设置为0xFF,否则我将它留给0。我写的代码如下:
void
convert_pixels(unsigned char *pixdata,
unsigned char *convertedpix,
int width,
int height)
{
int i,j,k, count=0;
unsigned int mask;
unsigned char temp;
for(i=0;i<height;i++)
{
count=0;
for(j=0;j<width;j++)
{
temp = *(pixdata+i*width+j);
for (mask = 0x80; mask != 0; mask >>= 1)
{
if ((temp & mask) && mask!=0x10 && mask!=0x01)
*(convertedpix+i*width*6+count)=0xFF;
count++;
}
}
}
}
它在执行时给出 SIGSEGV。 bt on gdb 给出:
(gdb) bt
#0 0x00000000004014b0 in convert_pixels (pixdata=0x7f008a0cf010 '\377' <repeats 200 times>...,
pixdata@entry=0x7f00967e2010 'w' <repeats 200 times>..., convertedpix=0x7f00967e2010 'w' <repeats 200 times>...,
convertedpix@entry=0x7f008a0cf010 '\377' <repeats 200 times>..., width=width@entry=4958, height=height@entry=7017) at image_convert.c:166
#1 0x0000000000401007 in main (argc=<optimized out>, argv=<optimized out>) at image_convert.c:355
数组convertedpix 分配的内存正好是6 乘以pixdata:
if(header.cupsBitsPerColor==1)
{
convertedpix = (unsigned char*)calloc(header.cupsWidth * header.cupsHeight*6,
sizeof(unsigned char));
convert_pixels(pixdata, convertedpix, header.cupsWidth,
header.cupsHeight);
}
【问题讨论】:
-
你确定
convertedpix指向的数组是pixdata指向的数组的6倍吗? -
@LPs 是的,我已将内存分配给
convertedpix,这正是6的pixdata倍
标签: c image-processing segmentation-fault