2012-11-15 3 views
2

libtiff를 사용하여 tiff 이미지를로드하고 쓰는 이미지 클래스를 작성하고 있습니다.C++에서 libtiff로 쓰기 이미지에 문제가 발생했습니다. TIFFScanlineSize로 정수 오버플로가 발생했습니다.

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를 쓸 수는 있지만 이미지 브라우저를 사용하여 열 수는 없으며 파일 크기가 이전과 동일하지 않습니다.

덕분에

답변

5

나는 어쩌면이 다른 사람을 도움이 될 것입니다, 유래에 비슷한 문제를 찾을 수 없기 때문에 난 그냥 내 자신의 문제를 해결했다 생각합니다.

이유는 TIFFSetField가 원래의 변수의 참조가 아니라 값을 취하는 이유입니다. 다음과 같이

그래서 모든 변경 후 잘 작동 :

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

내가 작성한 파일을 열 수 ImageJ에를 사용하는 팁을 가지고, 파일은 픽셀 당 잘못된 샘플을 가지고 있음을 보여준다.