2

我正在使用 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,但我无法用任何图像浏览器打开它,并且文件大小与以前不同。

谢谢

4

1 回答 1

6

我想我刚刚解决了自己的问题,因为我在 stackoverflow 上找不到类似的问题,也许这会对其他人有所帮助。

原因是 TIFFSetField 接受值而不是原始变量的引用。

因此,更改后一切正常,如下所示:

TIFFSetField(out, TIFFTAG_IMAGELENGTH, imagelength);
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, imagewidth);
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, nsamples);

我从使用 imagej 打开写入的文件中得到了提示,它表明该文件每个像素的样本错误。

于 2012-11-15T04:29:56.873 回答