我正在尝试从任意像素阵列生成 TIFF/EP Profile 2 原始 (.dng) 图像。该像素阵列表示拜耳模式 (CFA)。
我研究了 TIFF/EP 文件规范,并通过使用 libtiff,包含了我认为生成完整 .dng 文件所必需的所有标签。但是,我无法使用 dcraw 转换创建的文件(dcraw 显示它无法解码文件)。
一个问题可能是由在 TIFF/EP 规范中声明为强制性但似乎并未在 libtiff 中实现的两个标签引起的:SensingMethod 和 TIFF/EPStandardID 标签。我真的必须包含它们吗(我已经看到忽略这些标签但仍然报告正常工作的示例代码),如果是这样,我如何手动将它们添加到 libtiff?此外,设置 SubIFD 标记会产生错误消息“断言失败:*pa<=0xFFFFFFFFUL,文件 tif_dirwrite.c,第 1869 行”
总而言之,我不认为我的问题仅仅是由于这三个标签,我相信有一些根本性的错误。也许你们中的某个人可以看看我的代码并提供一些线索?我不得不说 libtiff 的文档很差,所以我的代码受到了极少数示例代码之一的启发:elphel_dng.c。
非常感谢!费边
PS。我将生成的文件上传到Dropbox
C++
#include "tiffio.h"
#include <iostream>
using namespace std;
int main(void)
{
TIFF *tif = TIFFOpen("8bitRaw.dng", "w");
const int sampleperpixel = 1;
const int width = 4;
const int height = 4;
static const short bayerPatternDimensions[] = { 2,2 };
static const double cam_xyz[] = { 2.0413690, -0.5649464, -0.3446944, -0.9692660, 1.8760108, 0.0415560,0.0134474, -0.1183897, 1.0154096 }; //AdobeRGB
static const double neutral[] = { 1.0, 1.0, 1.0 };
long max_white = 255;
long sub_offset = 0;
int row, i;
float gamma = 80;
//Arbitrary Bayer pixel values:
unsigned char image[height][width*sampleperpixel] = {};
image[0][0] = 5; image[0][1] = 165; image[0][2] = 0; image[0][3] = 255;
image[1][0] = 0; image[1][1] = 21; image[1][2] = 0; image[1][3] = 10;
image[2][0] = 0; image[2][1] = 0; image[2][2] = 30; image[2][3] = 5;
image[3][0] = 21; image[3][1] = 120; image[3][2] = 1; image[3][3] = 254;
//Black Thumbnail pixel values:
unsigned char Thumbnail_RGB_Array[] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
//Linearization Table:
unsigned short curve[256];
for (i = 0; i < 256; i++)
curve[i] = 255 * pow(i / 255.0, 100.0 / gamma) + 0.5;
TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 1);
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, 4);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, 4);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(tif, TIFFTAG_XRESOLUTION, 75.0);
TIFFSetField(tif, TIFFTAG_YRESOLUTION, 75.0);
TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
TIFFSetField(tif, TIFFTAG_MAKE, "DummyMake");
TIFFSetField(tif, TIFFTAG_MODEL, "DummyModel");
TIFFSetField(tif, TIFFTAG_SOFTWARE, "DummySoftware");
TIFFSetField(tif, TIFFTAG_ORIGINALRAWFILENAME, 1, "DummyName.dng");
TIFFSetField(tif, TIFFTAG_UNIQUECAMERAMODEL, "DummyUniqueModel");
TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "DummyImageDescription");
TIFFSetField(tif, TIFFTAG_COPYRIGHT, "DummyCopyright");
TIFFSetField(tif, TIFFTAG_DATETIME, "2016:06:30 11:11:15");
TIFFSetField(tif, TIFFTAG_DNGVERSION, "\01\01\00\00");
TIFFSetField(tif, TIFFTAG_DNGBACKWARDVERSION, "\01\00\00\00");
TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, cam_xyz);
TIFFSetField(tif, TIFFTAG_ASSHOTNEUTRAL, 3, neutral);
TIFFSetField(tif, TIFFTAG_CALIBRATIONILLUMINANT1, 21);
//SensingMethodTag and TIFF/EPStandardID tag: Libtiff doesn't seem to know these:
//TIFFSetField(tif, 37399, 2);
//TIFFSetField(tif, 37398, "\01\01\00\00");
//Yields an error:
//TIFFSetField(tif, TIFFTAG_SUBIFD, 1, &sub_offset);
//Write a black 4x4 pixel dummy thumbnail:
for (row = 0; row < 4 ; row++)
TIFFWriteScanline(tif, Thumbnail_RGB_Array, row, 0);
TIFFWriteDirectory(tif);
//Now write main raw image:
TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 0);
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CFA);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_CFAREPEATPATTERNDIM, bayerPatternDimensions);
TIFFSetField(tif, TIFFTAG_CFAPATTERN, "\01\00\02\01");
TIFFSetField(tif, TIFFTAG_CFAPLANECOLOR, 3, "\00\01\02");
TIFFSetField(tif, TIFFTAG_LINEARIZATIONTABLE, 256, curve);
TIFFSetField(tif, TIFFTAG_WHITELEVEL, 1, &max_white);
TIFFWriteScanline(tif, image[0], 0, 0);
TIFFWriteScanline(tif, image[1], 1, 0);
TIFFWriteScanline(tif, image[2], 2, 0);
TIFFWriteScanline(tif, image[3], 3, 0);
TIFFClose(tif);
return 0;
}