2014-01-10 89 views
1

我試圖以tiff文件格式保存圖像。我已經使用Libraw來讀取來自相機的原始數據,它給了我無符號的短數據。我對數據做了一些操作,我想將結果保存爲Tiff文件格式的16位灰度(1通道)圖像。但結果只是一張空白圖片。即使我使用保留原始拜爾圖像的緩衝區,它也不會正確保存。這是我用於保存的代碼:將16位單通道圖像寫入Tiff

// Open the TIFF file 
if((output_image = TIFFOpen("image.tiff", "w")) == NULL){ 
     std::cerr << "Unable to write tif file: " << "image.tiff" << std::endl; 
} 

TIFFSetField(output_image, TIFFTAG_IMAGEWIDTH, width()); 
TIFFSetField(output_image, TIFFTAG_IMAGELENGTH, height()); 
TIFFSetField(output_image, TIFFTAG_SAMPLESPERPIXEL, 1); 
TIFFSetField(output_image, TIFFTAG_BITSPERSAMPLE, 16); 
TIFFSetField(output_image, TIFFTAG_ROWSPERSTRIP, 1); 
TIFFSetField(output_image, TIFFTAG_ORIENTATION, (int)ORIENTATION_TOPLEFT); 
TIFFSetField(output_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); 
TIFFSetField(output_image, TIFFTAG_COMPRESSION, COMPRESSION_NONE); 
TIFFSetField(output_image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); 


// Write the information to the file 

tsize_t image_s; 
if((image_s = TIFFWriteEncodedStrip(output_image, 0, &m_data_cropped[0], width()*height())) == -1) 
{ 
     std::cerr << "Unable to write tif file: " << "image.tif" << std::endl; 
} 
else 
{ 
     std::cout << "Image is saved! size is : " << image_s << std::endl; 
} 

TIFFWriteDirectory(output_image); 
TIFFClose(output_image); 

回答

2

看起來您在代碼中有兩個問題。

  1. 您正在試圖通過一個調用整個圖像寫入TIFFWriteEncodedStrip,但在同一時間設置TIFFTAG_ROWSPERSTRIP1(你應該在這樣的情況下,將其設置爲height())。

  2. 您將錯誤的值傳遞給TIFFWriteEncodedStrip。最後一個參數是條帶的字節長度,並且您明確通過像素爲的長度爲

我不知道,如果&m_data_cropped[0]參數指向整個圖像的第一個字節,所以你可能要檢查這個參數的正確性了。

+0

謝謝,問題與您提到的完全相同! – user3178756