file won't close despite calling the function TiffClose(image)

This is a discussion on file won't close despite calling the function TiffClose(image) within the C Programming forums, part of the General Programming Boards category; I'm looping through a series of tiff images in a directory. I've determined that the image isn't closing like it's ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    file won't close despite calling the function TiffClose(image)

    I'm looping through a series of tiff images in a directory. I've determined that the image isn't closing like it's supposed to when I use TiffClose(image);

    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:

    File: 5251163
    .: Cannot read TIFF header.
    Could not open incoming image

    File: 5251187
    ..: Cannot read TIFF header.
    Could not open incoming image

    File: 5251211
    Image 2 is complete!!
    Get the closed image: 5283920

    File: 5251259
    TIFFOpen: img_000000001__000.tif: Cannot open.
    Could not open incoming image

    File: 5251307
    TIFFOpen: img_000000002__000.tif: Cannot open.
    Could not open incoming image

    File: 5251355
    TIFFOpen: img_000000003__000.tif: Cannot open.
    Could not open incoming image

    File: 5251403
    TIFFOpen: img_000000004__000.tif: Cannot open.
    Could not open incoming image

    File: 5251451
    TIFFOpen: img_000000005__000.tif: Cannot open.
    Could not open incoming image

    File: 5251499
    TIFFOpen: img_000000006__000.tif: Cannot open.
    Could not open incoming image

    File: 5251547
    TIFFOpen: img_000000007__000.tif: Cannot open.
    Could not open incoming image


    The first two files are expected to return an error (they are the ./ and ../ files). The third file opens like it's supposed to and completes. The last line in the third section should cause an error, since that is happening AFTER I've closed the file. I imagine, then, that the file isn't closing at all. I figure the fact that the image isn't closing properly is the reason why the other images are failing to open. Any ideas on how to fix?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    image is still a pointer. You can always print the value of a pointer. Whether what it points to is valid, who knows?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21