【问题标题】:Issue on write image with libtiff in C++, Integer overflow in TIFFScanlineSize在 C++ 中使用 libtiff 写入图像时出现问题,TIFFScanlineSize 中的整数溢出
【发布时间】:2012-11-15 04:17:06
【问题描述】:

我正在使用 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 可以写了,但是用任何图片浏览器都打不开,而且文件大小和以前不一样了。

谢谢

【问题讨论】:

    标签: c++ templates libtiff


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 我犯了和你一样的错误,这对我有帮助!我相信当您复制粘贴阅读代码并将TIFFGetFieldTIFFSetField 交换但忘记从参数中删除&amp; 时会发生这种情况。 :-) 其他函数(例如 TIFFScanlineSize)然后返回无效值,因为这些字段首先是无效的,并且您保存的 TIFF 文件变得无效。
    猜你喜欢
    • 1970-01-01
    • 2010-11-21
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 2013-08-15
    相关资源
    最近更新 更多