C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-27-2009, 12:18 PM   #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?
caggles is offline   Reply With Quote
Old 05-27-2009, 12:46 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
image is still a pointer. You can always print the value of a pointer. Whether what it points to is valid, who knows?
tabstop is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:02 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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