use*_*756 3 c++ file-io 16-bit grayscale libtiff
我正在尝试以 tiff 文件格式保存图像。我使用 libraw 从相机读取原始数据,它给了我未签名的短数据。我对数据做了一些操作,我想将结果保存为 Tiff 文件格式的 16 位灰度(1 通道)图像。但结果只是一个空白图像。即使我使用保留原始拜耳图像的缓冲区,它也不会正确保存。这是我用于保存的代码:
// 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);
Run Code Online (Sandbox Code Playgroud)
看起来您的代码中有两个问题。
您正在尝试通过一次调用写入整个图像,TIFFWriteEncodedStrip但同时设置TIFFTAG_ROWSPERSTRIP为1(height()在这种情况下您应该将其设置为)。
您将错误的值传递给TIFFWriteEncodedStrip. 最后一个参数是以字节为单位的条带长度,您显然是以像素为单位传递的长度。
我不确定该&m_data_cropped[0]参数是否指向整个图像的第一个字节,因此您可能还想检查此参数的正确性。