Skip to content

Instantly share code, notes, and snippets.

@fernandoc1
Created May 4, 2018 20:50
Show Gist options
  • Save fernandoc1/8533276e2d171f7e5eddc19c9bdecd6d to your computer and use it in GitHub Desktop.
Save fernandoc1/8533276e2d171f7e5eddc19c9bdecd6d to your computer and use it in GitHub Desktop.
This code is to convert raw data buffer acquired from Flir camera to a TIFF image, using libtiff
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <tiffio.h>
int main(int argc, char** argv)
{
if(argc < 2)
{
std::cout << argv[0] << " <INPUT_FILE>" << std::endl;
return 1;
}
std::string filepath(argv[1]);
FILE* file = fopen(filepath.c_str(), "rb");
fseek(file, 0, SEEK_END);
long fsize = ftell(file);
fseek(file, 0, SEEK_SET);
uint8_t* buffer = (uint8_t*)malloc(fsize);
fread(buffer, fsize, 1, file);
fclose(file);
int imgWidth = 640;
int imgHeight = 512;
std::string outputFile = filepath + std::string(".tiff");
TIFF* tiff = TIFFOpen(outputFile.c_str(), "w");
TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, imgWidth);
TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, imgHeight);
TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 16);
for(int i = 0; i < imgHeight; i++)
{
TIFFWriteScanline(tiff, &buffer[i*imgWidth*2], i);
}
TIFFClose(tiff);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment