2014-01-10 90 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