#include <tiffio.h>
#include <stdio.h>

int main(int argc, char *argv[]){

TIFF	*mytiff, *mynewtiff;
uint16  compression;
char    *buffer;

// Open the input tif file
if((mytiff = TIFFOpen("input.tif", "r")) == NULL){
  fprintf(stderr, "Could not open the tiff input.tif\n");
  exit(42);
  }

// Open the output tif file (this one for writing)
if((mynewtiff = TIFFOpen("output.tif", "w")) == NULL){
  fprintf(stderr, "Could not open the tiff output.tif\n");
  exit(42);
  }

// Get the value of the compression header field
if(TIFFGetField(mytiff, TIFFTAG_COMPRESSION, &compression) != 1){
  fprintf(stderr, "Something bad happened when I tried to get the compression\n");
  exit(42);
  }

// Change the value of the compression field if it makes sense to do so
if(compression == COMPRESSION_CCITTFAX3){
  fprintf(stdout, "Changing the compression to Group 4 FAX\n");
  compression = COMPRESSION_CCITTFAX4;
  } 

// Write the compresion value into the new tif
TIFFSetField(mynewtiff, TIFFTAG_COMPRESSION, compression);

// Move the data for the image across to the new tif
if((buffer = (char *) malloc(TIFFStripSize(mytiff))) == NULL){
  fprintf(stderr, "Could not get enough space for the uncompressed tif\n");
  exit(42);
  }

TIFFReadEncodedStrip(mytiff, 0, buffer, TIFFStripSize(mytiff));
TIFFWriteEncodedStrip(mynewtiff, 0, buffer, TIFFStripSize(mytiff));

// Close the tiff files
TIFFClose(mytiff);
TIFFClose(mynewtiff);
}



