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

スタックオーバーフローで同様の問題が見つからないため、自分の問題を解決しただけだと思います。これは他の人に役立つかもしれません。

その理由は、TIFFSetField が元の変数の参照ではなく値を受け取るためです。

したがって、変更後は次のようにすべて正常に動作します。

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

imagej を使用して書き込まれたファイルを開くことからヒントを得ました。これは、ファイルのピクセルごとのサンプルが間違っていることを示しています。

于 2012-11-15T04:29:56.873 に答える