Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从RGB888数据生成的Tiff图像不正确_C_Tiff_Libtiff - Fatal编程技术网

C 从RGB888数据生成的Tiff图像不正确

C 从RGB888数据生成的Tiff图像不正确,c,tiff,libtiff,C,Tiff,Libtiff,我正在尝试将RGB888图像数据转换为TIFF图像。但代码会生成不正确的图像。 我正在从文本文件中读取RGB数据。这是输出图像 黑色区域不应该在那里,因为代码似乎正在将低RGB值设置为零。 我正在创建没有alpha通道的tiff图像。请帮助我理解这个问题 TIFF *out= TIFFOpen("new.tif", "w"); int sampleperpixel = 3; uint32 width=320; uint32 height=240; unsigned char image[wi

我正在尝试将RGB888图像数据转换为TIFF图像。但代码会生成不正确的图像。 我正在从文本文件中读取RGB数据。这是输出图像

黑色区域不应该在那里,因为代码似乎正在将低RGB值设置为零。 我正在创建没有alpha通道的tiff图像。请帮助我理解这个问题

TIFF *out= TIFFOpen("new.tif", "w");

int sampleperpixel = 3;
uint32 width=320;
uint32 height=240;
unsigned char image[width*height*sampleperpixel];
int pixval;
int count=0;


FILE *ptr=fopen("data.txt","r");
if (ptr!=NULL)
    {
      while(count<width*height)
        {fscanf(ptr,"%d",&pixval);            
        *(image+count)=(unsigned char)pixval;
        count++;}
    }
printf("%d\n",count);

TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);  // set the width of the image
TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);    // set the height of the image
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, sampleperpixel);   // set number of channels per pixel
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);    // set the size of the channels
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);    // set the origin of the image.
//   Some other essential fields to set that you do not have to understand for now.
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);

tsize_t linebytes = sampleperpixel * width;
unsigned char *buf = NULL; 
//if (TIFFScanlineSize(out)<linebytes)
//  buf =(unsigned char *)_TIFFmalloc(linebytes);
//else
    buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));




TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, width*sampleperpixel));

uint32 row ;
//Now writing image to the file one strip at a time
for (row = 0; row < height; row++)
{
    //memcpy(buf, &image[(height-row-1)*linebytes], linebytes);    // check the index here, and figure out why not using h*linebytes
    memcpy(buf, &image[(row)*linebytes], linebytes);        
if (TIFFWriteScanline(out, buf, row, 0) < 0)
    break;
}


TIFFClose(out);

if (buf)
 _TIFFfree(buf);
TIFF*out=TIFFOpen(“new.tif”、“w”);
int-sampleperpixel=3;
uint32宽度=320;
uint32高度=240;
无符号字符图像[宽度*高度*采样像素];
int-pixval;
整数计数=0;
文件*ptr=fopen(“data.txt”,“r”);
如果(ptr!=NULL)
{
虽然(count我在上找到了你的例子。因为你不是原作者,所以你应该经常发布一个链接来帮助他人,避免剽窃

除了您添加的部分外,该示例非常有效。若要将文件读入字节数组,请改用该示例(在您阅读了它的作用之后):

在原始代码中,您省略了
sampleperpixel

while(count<width*height*sampleperpixel) ...

while(count)您应该尝试一次执行一个步骤。首先,如果您尝试先用harcoded值填充所有扫描线,而不是从文本文件中读取数据,会发生什么情况?(例如,生成一个完全红色的tiff)您的TIFFTAG_ROWSPERSTRIP错误。需要将其设置为高度或高度的小数部分。其余部分看起来正常。发布实际文件,我可以更准确地告诉您哪里出了问题。@BitBank可能是您,请更具体地说明文件,因为这是完整的代码。除此之外,只有数据文件。谢谢TIFF文件,以便我可以检查它,而不是TIFF文件的PNG图像。---这是链接,我已上传到goolge drive
while(count<width*height*sampleperpixel) ...