Read and Write TIFF file in c using libtiff

80 Views Asked by At

This is the code that I wrote. It'll read from tiff file and then I'm trying to write to a different tiff file but getting black image in output.tiff. I've attached the input file. I'm adding all the necessary header in output file

// Include necessary header files
#include <stdio.h>
#include <stdlib.h>
#include <tiffio.h>

// Main function
int main() {
    // Open the input and output TIFF files
    TIFF* tif_in = TIFFOpen("image.tiff", "rb");
    TIFF* tif_out = TIFFOpen("output.tiff", "w+");
    
    // Define a color map
    unsigned long cmap[2] = {0, 65535};
    
    // Check if input TIFF file opened successfully
    if (tif_in) {
        // Variables to store image properties
        uint32 width, height;
        uint16 bits_per_sample, samples_per_pixel;
        uint32 strip_size, tile_width, tile_height;
        uint8* strip_data;
        uint32 number_of_strips;
        uint16 compression_method;
        uint64* strip_offsets;
        uint64* strip_byte_counts;
        
        // Retrieve image properties from the input TIFF file
        TIFFGetField(tif_in, TIFFTAG_IMAGEWIDTH, &width);
        TIFFGetField(tif_in, TIFFTAG_IMAGELENGTH, &height);
        TIFFGetField(tif_in, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
        TIFFGetField(tif_in, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
        TIFFGetField(tif_in, TIFFTAG_COMPRESSION, &compression_method);
        TIFFGetField(tif_in, TIFFTAG_STRIPOFFSETS, &strip_offsets);
        TIFFGetField(tif_in, TIFFTAG_STRIPBYTECOUNTS, &strip_byte_counts);
        
        // Set image properties for the output TIFF file
        TIFFSetField(tif_out, TIFFTAG_IMAGEWIDTH, width);
        TIFFSetField(tif_out, TIFFTAG_IMAGELENGTH, height);
        TIFFSetField(tif_out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
        TIFFSetField(tif_out, TIFFTAG_COMPRESSION, compression_method);
        TIFFSetField(tif_out, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
        TIFFSetField(tif_out, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel);
        TIFFSetField(tif_out, TIFFTAG_STRIPOFFSETS, strip_offsets);
        TIFFSetField(tif_out, TIFFTAG_STRIPBYTECOUNTS, strip_byte_counts);
        TIFFSetField(tif_out, TIFFTAG_COLORMAP, cmap, cmap, cmap);
        
        // Calculate strip size and allocate memory for strip data
        strip_size = TIFFStripSize(tif_in);
        strip_data = (uint8*)malloc(strip_size);
        // Get the total number of strips in the image
        number_of_strips = TIFFNumberOfStrips(tif_in);
        
        // Print image properties
        printf("Strip size is %d\n", strip_size);
        printf("Number of strips are %d\n", number_of_strips);
        printf("Compression method is %d\n", compression_method);
        printf("Width: %d, Height: %d\n", width, height);
        
        // Read and write each strip from the input to the output TIFF file
        for (uint32 strip = 0; strip < number_of_strips; strip++) {
            TIFFReadEncodedStrip(tif_in, strip, strip_data, strip_size);
            TIFFWriteEncodedStrip(tif_out, strip, strip_data, strip_size);
        }
        
        // Close TIFF files and free allocated memory
        TIFFClose(tif_in);
        TIFFClose(tif_out);
        free(strip_data);
    } else {
        // Print error message if the input TIFF file failed to open
        printf("Failed to open the TIFF file.\n");
    }
    
    return 0;
}

I want to read from input file and write to different file. Any help will be appreciated.

Input file: input file

0

There are 0 best solutions below