Code:
#include <stdlib.h>
#include <stdio.h>
#include <tiffio.h>
#include <dirent.h>
int main(int argc, char** argv)
{
TIFF *image;
FILE *output;
DIR *dir;
struct dirent *entry;
uint16 photo, bps, spp, fillorder;
uint32 height, width, rps;
tsize_t stripSize;
unsigned long imageOffset, result;
int stripMax, stripCount;
char *buffer, tempbyte;
unsigned long bufferSize, count;
dir = opendir(argv[1]);
if (!dir)
{
fprintf(stderr, "There was a problem opening the directory\n");
exit(42);
}
int m = 0;
while ((entry = readdir(dir)) && (m<10))
{
fprintf(stdout, "\nFile: %d\n", entry->d_name);
// Open the TIFF image
if((image = TIFFOpen(entry->d_name, "r")) == NULL)
fprintf(stderr, "Could not open incoming image\n");
else
{
// Check that it is of a type that we support
result = TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bps);
if(result == 0)
{
fprintf(stderr, "Either undefined or unsupported number of bits per sample\n");
fprintf(stdout, "Number of bits: %d\n", TIFFTAG_BITSPERSAMPLE);
//exit(42);
}
if((TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp) == 0) || (spp != 1))
{
fprintf(stderr, "Either undefined or unsupported number of samples per pixel\n");
//exit(42);
}
if(TIFFGetField(image, TIFFTAG_ROWSPERSTRIP, &rps) == 0)
{
fprintf(stderr, "Either undefined or unsupported number of rows per strip\n");
//exit(42);
}
// Read in the possibly multiple strips
stripSize = TIFFStripSize (image);
stripMax = TIFFNumberOfStrips (image);
imageOffset = 0;
bufferSize = stripMax * stripSize;
if((buffer = (char *) malloc(bufferSize)) == NULL)
{
fprintf(stderr, "Could not allocate enough memory for the uncompressed image\n");
exit(42);
}
for (stripCount = 0; stripCount < stripMax; stripCount++)
{
if((result = TIFFReadEncodedStrip (image, stripCount, buffer + imageOffset, stripSize)) == -1)
{
fprintf(stderr, "Read error on input strip number %d\n", stripCount);
exit(42);
}
imageOffset += result;
}
// Deal with photometric interpretations
if(TIFFGetField(image, TIFFTAG_PHOTOMETRIC, &photo) == 0)
{
fprintf(stderr, "Image has an undefined photometric interpretation\n");
exit(42);
}
/*
if(photo != PHOTOMETRIC_MINISWHITE)
{
// Flip bits
printf("Fixing the photometric interpretation\n");
for(count = 0; count < bufferSize; count++)
buffer[count] = ~buffer[count];
}
*/
// Do whatever it is we do with the buffer -- we dump it in hex
if(TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width) == 0)
{
fprintf(stderr, "Image does not define its width\n");
exit(42);
}
else
if(TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height) == 0)
{
fprintf(stderr, "Image does not define its height\n");
exit(42);
}
int matrixA[width][height];
for(count = 1; count < width*height; count++)
{
int i = (count) % (width);
int j = (count) / (width);
matrixA[i][j] = buffer[count];
}
output = fopen("output.txt", "w");
if(!output)
{
printf("Oh ........!\n");
exit(0);
}
for(count = 0; count < width*height; count++)
{
int i = (count) % (width);
int j = (count) / (width);
fprintf(output, "%d", matrixA[i][j]);
if(i == width-1)
fprintf(output, "\n");
else
fprintf(output, " ");
}
fprintf(stdout, "Image %d is complete!!\n", m);
TIFFClose(image);
fprintf(stdout, "Get the closed image: %d\n", image);
}
m++;
}
}
Here's the output: