我正在使用 libtiff 编写一个加载和编写 tiff 图像的图像类。但是,使用它们非常困难,我不断收到错误,我的代码是:
TIFF *out = TIFFOpen(filename.c_str(),"w") ;
if (out)
{
uint32 imagelength, imagewidth;
uint8 * buf;
uint32 row, col, n;
uint16 config, nsamples;
imagewidth = dims[0] ;
imagelength = dims[1] ;
config = PLANARCONFIG_CONTIG ;
nsamples = cn ;
TIFFSetField(out, TIFFTAG_IMAGELENGTH, &imagelength);
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, &imagewidth);
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, &nsamples);
TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_LZW) ;
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8) ;
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, imagewidth*nsamples));
std::cout <<nsamples << std::endl ;
buf = new uint8 [imagewidth*nsamples] ;
for (row = 0; row < imagelength; row++){
for(col=0; col < imagewidth; col++){
for(n = 0 ; n < nsamples ; ++n)
{
Vec<T,cn>* temp = image_data[imagewidth*row+col] ;
buf[col*nsamples+n] = static_cast<uint8> ((*temp)[n]) ;
}
}
if (TIFFWriteScanline(out, buf, row) != 1 )
{
std::cout << "Unable to write a row." <<std::endl ;
break ;
}
}
_TIFFfree(buf);
TIFFClose(out);
} ...
错误信息是:
test_write.tiff: Integer overflow in TIFFScanlineSize.
test_write.tiff: Integer overflow in TIFFScanlineSize.
我的调用代码是这样的:
Image<unsigned char,3> testimg ; //uint8 is unsigned char
testimg.read_image("test.tiff") ;
testimg.write_image("test_write.tiff") ;
我可以编写 test_write.tiff,但我无法用任何图像浏览器打开它,并且文件大小与以前不同。
谢谢