虽然您已经接受了另一个答案,但我想在这里保存我的努力结果,以防其他人遇到类似问题。根据原始问题,此代码在 Mac 或 Linux 上运行。
////////////////////////////////////////////////////////////////////////////////
// raw2tif.c
// Mark Setchell
//
// Reads a RAW file from a MAPIR NDVI camera and converts it to a TIF
//
// Usage:
// raw2tif RAWfile result.tif
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "tiffio.h"
#define WIDTH 4608
#define HEIGHT 3456
#define RAW_BITSPERSAMPLE 8
#define RAW_SAMPLESPERPIXEL 2
#define TIF_BITSPERSAMPLE 8
#define TIF_SAMPLESPERPIXEL 3
int main(int argc,char* argv[])
{
char ifilename[128];
char ofilename[128];
unsigned char inrow[WIDTH*RAW_BITSPERSAMPLE*RAW_SAMPLESPERPIXEL/8];
FILE* in;
// Check input and output file names supplied
if(argc!=3){
fprintf(stderr,"Usage: raw2tif RAWfile TIFfile");
exit(EXIT_FAILURE);
}
// Pick up arguments - infile (RAW) and outfile (TIF)
strcpy(ifilename,argv[1]);
strcpy(ofilename,argv[2]);
// Open input file
in=fopen(ifilename,"rb");
if(in==NULL){
fprintf(stderr,"ERROR: Unable to open RAW input file");
exit(EXIT_FAILURE);
}
// Open output file
TIFF *tif= TIFFOpen(ofilename, "w");
if(tif==NULL){
fprintf(stderr,"ERROR: Unable to open output file");
exit(EXIT_FAILURE);
}
// Set baseline tags
TIFFSetField(tif,TIFFTAG_IMAGEWIDTH,WIDTH);
TIFFSetField(tif,TIFFTAG_IMAGELENGTH,HEIGHT);
TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,TIF_BITSPERSAMPLE);
TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,TIF_SAMPLESPERPIXEL);
TIFFSetField(tif,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
TIFFSetField(tif,TIFFTAG_COMPRESSION,COMPRESSION_LZW);
TIFFSetField(tif,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_MINISBLACK);
uint32 rowsPerStrip;
rowsPerStrip = HEIGHT;
rowsPerStrip = TIFFDefaultStripSize(tif, rowsPerStrip);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsPerStrip);
TIFFSetupStrips(tif);
// Line buffer
int scanlineSize = TIFFScanlineSize(tif);
unsigned char* scanline = (unsigned char*) _TIFFmalloc(scanlineSize);
// Iterate over rows of RAW file writing rows of TIF file
for(int row=0;row<HEIGHT;row++)
{
if(fread(inrow,WIDTH*RAW_BITSPERSAMPLE*RAW_SAMPLESPERPIXEL/8,1,in)!=1){
fprintf(stderr,"ERROR: Error reading input file at row %d\n",row);
exit(EXIT_FAILURE);
}
// Following few lines need correcting when input file format is known
unsigned char* ip=inrow;
unsigned char* op=scanline;
for(int col=0;col<WIDTH;col++){
*op++=*ip++; // Write RAW red to TIF red
*op++=0; // Set TIF green to 0
*op++=*ip++; // Write RAW NIR to TIF blue
}
if(TIFFWriteScanline(tif,scanline,row,0) != 1){
fprintf(stderr,"ERROR: Error writing output file at row %d\n",row);
exit(EXIT_FAILURE);
}
}
// Clean up
_TIFFfree(scanline);
TIFFClose(tif);
}
一个合适的Makefile是:
CC=clang -Wall -pedantic
TIF_INC=-I/usr/include -I/usr/local/include
TIF_LIB=-L/usr/lib -L/usr/local/lib -ltiff
raw2tif: raw2tif.c
$(CC) raw2tif.c $(TIF_INC) $(TIF_LIB) -o raw2tif