Iāve hacked up a bayer2tiff, here it is:
#include <stdio.h>
#include <stdlib.h>
#include <tiffio.h>
int main(int argc, char * argv[])
{
char * img;
if (argc < 5) {
printf("bayer2tiff: Opens a file containing raw bayer data and saves it to a\n");
printf("16-bit integer TIFF with absolutely no processing. The CFAPattern\n");
printf("EXIF tag is loaded with \"RGGB\" to facilitate subsequent demosaicing.\n");
printf("\nUsage: $ bayer2tiff width height rawfile.bayer (whatevernameyouwant).tif\n\n");
return 1;
}
printf("Versions:\n\ttiff=%s\n", TIFFLIB_VERSION_STR);
//opens the bayer file and slurps it into memory
FILE *f = fopen(argv[3], "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char *bayerdata = (char *) malloc(fsize);
fread(bayerdata, 1, fsize, f);
fclose(f);
//raw_image is an array of 16-bit integers of size width*height:
int width = atoi(argv[1]);
int height = atoi(argv[2]);
unsigned short *rawimage = (unsigned short *) bayerdata;
//Use libtiff to write the TIFF image file:
TIFF* tif = TIFFOpen(argv[4], "w");
if (tif) {
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
//set the relevant CFA metadata:
static const short bayerPatternDimensions[] = { 2, 2 };
TIFFSetField (tif, TIFFTAG_CFAREPEATPATTERNDIM, bayerPatternDimensions);
TIFFSetField (tif, TIFFTAG_CFAPATTERN, "\00\01\01\02"); //RGGB
//about the simplest TIFF format to write, one big strip:
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, height);
if (rawimage)
TIFFWriteEncodedStrip(tif, 0, rawimage, width*height*2);
else printf("bayer2tiff has a problem...\n");
(void) TIFFClose(tif);
}
else {
printf("TIFF file save failed...\n");
return 1;
}
}
All I know is that it compiles. It assumes raw.bayer contains unsigned shorts. It also assumes the CFA pattern is āRGGBā. It can be compiled with this command line:
g++ -o bayer2tiff bayer2tiff.cpp -ltiff
Once I can get a TIFF produced from this file with a raw.bayer input, Iāll see what modifications I need for rawproc, my hack software , to compel it to demosaicā¦