0
我試圖將RGB888圖像數據轉換爲TIFF圖像。但是代碼會生成不正確的圖像。 我讀從文本file.Here的RGB數據是輸出圖像從RGB888數據生成不正確的TIFF圖像
黑色區域不應該在那裏似乎代碼零使得低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) – Jem 2013-05-04 19:14:33
您的TIFFTAG_ROWSPERSTRIP錯誤。它需要設置爲HEIGHT或HEIGHT的小數部分。其餘的看起來不錯。發佈實際文件,我可以更準確地告訴你哪裏出了問題。 – BitBank 2013-05-05 14:38:05
@BitBank可以請你更具體的文件,因爲這是完整的代碼。除此之外,只有數據文件。謝謝 – vid09 2013-05-05 16:04:15