/* cs545 F97/98, file add_header.c - add a header to a digital image */	/* this program is useful if you have a digital image that you want to put into cs545 format *//* Written by Norman Wittels *//* 09Apr97	first version *//* 30Apr97	Updated to handle 24-bit rgb color */#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <math.h>#include <limits.h>#include <time.h>#include <float.h>#define FILE_NAME_LENGTH 51 /* length of a file name */#define MIN(a,b) ((a)<(b) ? (a) : (b)) /* return the minimum value */typedef struct digital_image	{	unsigned int cols; /* digital image size */	unsigned int rows;	unsigned int colors; /* 1 for gray scale; 3 for color */	unsigned char *image; /* the image array */	struct digital_image *next; /* for linked list */	} digital_image; /* newly defined type *//* prototypes */int write_digital_image(digital_image *im,FILE *f);void write_user_specified_digital_image_file(digital_image *);int main()	{	FILE *f; /* the file */	digital_image the_image;	char file_name[FILE_NAME_LENGTH];	int cols, rows, colors;	long int size;		printf("Please enter image width and height (pixels) and number of colors (1 or 3)\n");	scanf("%d %d %d", &cols, &rows, &colors);	printf("What is the name of the input raw data file?: ");	scanf("%s",file_name); /* read file name */	if ((f = fopen(file_name,"rb"))==NULL) /* could not open file */		{		printf("Could not open input file\n");		return 0;		} /* end if(fopen()) */	the_image.cols = cols; the_image.rows = rows; the_image.colors = colors;	size = (long int) cols * (long int) rows * (long int) colors;	if ((the_image.image = malloc(size)) == NULL)		{		printf("could not allocate memory for digital image\n");		return 0;		} /* end if(the_image.image) */			if (fread(the_image.image, 1, size, f) != size)		{		printf("Difficulty reading the image. Are you sure the size is correct?\n");		free(the_image.image);		return 0;		} /* end if(fread()) */	fclose(f);	write_user_specified_digital_image_file(&the_image);	printf("goodbye\n");	} /* end main() *//*** write a digital image into a file ***/int write_digital_image(digital_image *im,FILE *f)	{	unsigned char header[6];	unsigned int size = im->rows * im->cols * im->colors; /* image bytes */	if (im == NULL || f == NULL) return 1; /* error indicator */	header[0] = MIN((im->cols / 256),255); /* high order bits */ 	header[1] = im->cols % 256; /* low order bits */	header[2] = MIN((im->rows / 256),255); /* high order bits */	header[3] = im->rows % 256; /* low order bits */	header[4] = MIN((im->colors / 256),255); /* high order bits */	header[5] = im->colors % 256; /* low order bits */	if (fwrite(header,1,6,f) != 6) return 1; /* write the header */	if (fwrite(im->image,1, size,f) != size) return 1; /* write the image data */	return 0; /* happy return value */	} /* end write_digital_image() *//*** query user and write digital image file ***/void write_user_specified_digital_image_file(digital_image *im)	{	FILE *F; /* the file */	char file_name[FILE_NAME_LENGTH];	printf("What is the name of the digital image output file?: ");	scanf("%s",file_name); /* read output file name */	if ((F = fopen(file_name,"wb"))==NULL) /* open output file */		{		printf("Could not open output file\n");		return;		} /* end if() */	if (write_digital_image(im,F)) printf("Could not write output file\n");	return;	} /* end write_user_specified_digital_image_file() *//* Copyright 1997, Norman Wittels */