#include "header.h" /* ReadImageFile() takes as an argument a pointer to a file stream. It * allocates space for and reads the image into a two dimetional array and * returns a pointer to the array (**I). It assumes that the file is already * open and that the size parameters have already been read. */ /* Written by Justin Romberg, Waqas Akram, & Charles Gamiz, May 1997 */ int **ReadStartFile(FILE *sfile, int *sizef) { int **image; int i; /* allocate space for the image */ image = (int **)calloc(sizef[0], sizeof(int*)); for (i=0; i < sizef[0]; i++) { image[i] = (int*)calloc(sizef[1], sizeof(int)); } /* read in the image (the image is written in row order) */ for (i=0; i < sizef[0]; i++) { fread(image[i], sizeof(int), sizef[1], sfile); } return(image); } int **ReadImageFile(FILE *imagefile, int *sizeI) { int **Image; int i; /* allocate space for the image */ Image = (int **)calloc(sizeI[0], sizeof(int*)); for (i=0; i < sizeI[0]; i++) { Image[i] = (int*)calloc(sizeI[1], sizeof(int)); } /* read in the image (the image is written in row order) */ for (i=0; i < sizeI[0]; i++) { fread(Image[i], sizeof(int), sizeI[1], imagefile); } return(Image); } /* ReadGradientFile() takes a pointer to a 2D array of doubles. Assumes that the file is already open, but the size HAS NOT BEEN READ YET */ double **ReadGradientFile(FILE *gradfile) { double **Gradient; int dummy; int i; /* kill the size from the file */ fread(&dummy, sizeof(int), 1, gradfile); fread(&dummy, sizeof(int), 1, gradfile); /* allocate space for the image */ Gradient = (double **)calloc(sizeI[0], sizeof(double*)); for (i=0; i < sizeI[0]; i++) { Gradient[i] = (double*)calloc(sizeI[1], sizeof(double)); } /* read in the image (the image is written in row order) */ for (i=0; i < sizeI[0]; i++) { fread(Gradient[i], sizeof(double), sizeI[1], gradfile); } return(Gradient); } /* WriteImageFile() takes a 2D array and writes it out row wise to a file. It * also writes out the size of the image (first the number of rows and then the * number of columns. */ void WriteImageFile(char *filename, int **Image, int *sizeI) { int i; FILE *resultfile; resultfile = fopen(filename, "w"); /* write the number of rows and columns out */ fwrite(&sizeI[0], sizeof(int), 2, resultfile); /* write the image to the file row-wise */ for (i=0; i < sizeI[0]; i++) { fwrite(Image[i], sizeof(int), sizeI[1], resultfile); } fclose(resultfile); }