Thread: Reading color tiff images?

  1. #1
    Registered User
    Join Date
    Nov 2003

    Reading color tiff images?


    I'm currently working on some code that will need to read color tiff image files. While I have no problems working with gray scale images I have not had any luck getting color to work. At the moment all I am simply trying to do is read a color tiff image (which filename is "input.tif") and output the values of individual samples (pixel elements RGB) to the screen. (I'm intentionally not printing all image pixels to the screen at the moment.) However, what my code currently does is output partially correct results. It seems to display the first few (~10) samples fine to the screen, but then starts skipping samples. Do you guys have any ideas what may be causing that? What may be wrong with my code (listed below)?
    Any suggestions/pointers would be greatly appreciated.

    Thank you.

    This is my code (hopefully it doesn't lose all formatting):
    #include <stdio.h>
    #include <iostream.h>
    #include <tiffio.h>
    void print_error (char *err_message);
    int main()
    double red_mean;				// mean red value of fed image
        uint32 r; 						// row index
        uint32 c; 						// column index
        uint32 rows;					// number of rows in image
        uint32 columns;					// number of columns in image
        uint16 BitsPerSample;			// normally 8 for grayscale image
        uint16 SamplesPerPixel;			// normally 1 for grayscale image
        uint16 PhotoMetric; 			// normally 1 for grayscale image
        unsigned char *in_image;		// pointer for input image array
        unsigned char *out_image;		// pointer for output image array
        TIFF *in_filep; 				// handle for input image file
        TIFF *out_filep;	 			// handle for output image file
        int	check_io;					// status of I/O operation
        // Open input image file
        in_filep = TIFFOpen("input.tif", "r");
        if (in_filep == NULL)
    	print_error ("Could not open input file!");
        // Open output image file
        out_filep = TIFFOpen ("output.tif", "w"); 
        if (out_filep == NULL)
    	print_error ("Could not open output file!");
        // Determine the size of the input image
        TIFFGetField(in_filep, TIFFTAG_IMAGELENGTH, &rows);
        TIFFGetField(in_filep, TIFFTAG_IMAGEWIDTH, &columns);
        TIFFGetField(in_filep, TIFFTAG_BITSPERSAMPLE, &BitsPerSample);
        TIFFGetField(in_filep, TIFFTAG_SAMPLESPERPIXEL, &SamplesPerPixel);
        TIFFGetField(in_filep, TIFFTAG_PHOTOMETRIC, &PhotoMetric);
    	//TIFFGetField(in_filep, TIFFTAG_PHOTOMETRIC, &PlanarConfig);
        // The following statements are helpful in debugging
        printf ("rows = %ld\n", rows);
        printf ("columns = %ld\n", columns);
        printf ("BitsPerSample = %d\n", BitsPerSample);
        printf ("SamplesPerPixel = %d\n", SamplesPerPixel);
        printf ("PhotoMetric = %d\n", PhotoMetric);
        // Specify TIFF header fields for output image
        TIFFSetField(out_filep, TIFFTAG_IMAGELENGTH, rows);
        TIFFSetField(out_filep, TIFFTAG_IMAGEWIDTH, columns);
        TIFFSetField(out_filep, TIFFTAG_BITSPERSAMPLE, BitsPerSample);
        TIFFSetField(out_filep, TIFFTAG_SAMPLESPERPIXEL, SamplesPerPixel);
     	TIFFSetField(out_filep, TIFFTAG_PLANARCONFIG, 1);
        TIFFSetField(out_filep, TIFFTAG_PHOTOMETRIC, PhotoMetric);
        // Allocate memory to hold image arrays
        in_image = (unsigned char *) _TIFFmalloc(rows*columns);
        if (in_image == NULL) 
    	print_error ("Could not allocate memory!");
        out_image = (unsigned char *) _TIFFmalloc(rows*columns);
        if (out_image == NULL) 
    	print_error ("Could not allocate memory!");
        // Read image pixel values from file, row by row 
    	for (r = 0; r < rows; r++)
            check_io = TIFFReadScanline(in_filep, &in_image[r*columns], r, 1);
            if (check_io != 1)
    	    print_error ("Could not read image from file!");
    	// Output the first several samples (pixel elements) to screen
    	red_mean = 0;
        for (r = 0; r < 3; r++)
    		for (c = 0; c < 24; c++)
    		//for (c = 0; c <= (columns*3); (c=c+3))
    		    cout << "r = " << r << " c= " << c << " Intensity = " << (int) in_image[r*columns+c] << endl;
        // Write new image to file, row by row 
        for (r = 0; r < rows; r++)
            check_io = TIFFWriteScanline(out_filep, &out_image[r*columns], r, 1);
            if (check_io != 1)
    	    print_error ("Could not read image from file!");
        // Deallocate the image memory, close file streams, and exit
        exit (0);
    Function name:   print_error
        Print an error message, and exit the program.
    Input parameters:
        err_message -- pointer to string to be printed
    Returned value:
    void print_error (char *err_message)
        fprintf(stderr, "Error:  %s\n", err_message);
        exit (1);
    Last edited by compz; 11-20-2003 at 10:40 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 10:36 PM
  2. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 05:41 PM
  3. reading images
    By hkl01 in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 09:25 PM
  4. reading file word by word
    By 98holb in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 04:49 PM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM