gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:52:07 25 4
gpt4 key购买 nike

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

Attached the improper image黑色区域不应该在那里,似乎代码正在将低 RGB 值设置为零。我正在创建没有 Alpha channel 的 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);

最佳答案

我在this tutorial找到了你的例子。由于您不是原作者,因此您应该始终发布链接以帮助他人并避免抄袭。

除了您添加的部分之外,该示例效果很好。要将文件读入字节数组,请使用它(在阅读 fread 的作用之后):

fread(image, 1, sizeof image, ptr);

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

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

所以你只阅读了图像的三分之一。

关于c - 从 RGB888 数据生成的 Tiff 图像不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16377483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com