#include #include #include //Function prototypes //reads a 640x480 PPM file (specified by filename) and returns a pointer to the image data. It returns NULL on failure int *readPPMFile(char *fileName); //writes the 640x480 image represented by imageData to the file named by fileName in PPM format. Returns 1 on success, 0 on failure int writePPMFile(char *fileName, int *imageData); //blends the blue screen image1 with image2 and returns a pointer to the blended image. Returns NULL on failure int *blend(int *image1, int *image2); //Complete this function int writePPMFile(char *filename, int *imageData) { } //Complete this function int *blend(int *image1, int *image2) { } //DO NOT CHANGE ANYTHING BELOW //READ THE CODE CAREFULLY int main (int argc, char *args[]) { //DO NOT MODIFY THIS FUNCTION IN ANY WAY //all images are 640x480 int *image1; //pointer to blue screen image data int *image2; //pointer to second image data int *image3; //pointer to blended image data image1 = readPPMFile(args[1]); //read blue screen image file if (image1 == NULL) { printf("Problems reading file %s. Exiting...\n", args[1]); exit(1); } image2 = readPPMFile(args[2]); //read second image file if (image2 == NULL) { printf("Problems reading file %s. Exiting...\n", args[2]); exit(1); } image3 = blend(image1, image2); //blend the two images int x = writePPMFile(args[3], image3); //write the blended image to the third file if (x == 0) { printf("Problems writing file %s. Exiting...\n", args[3]); exit(1); } } //Open the specified ppm file and return a pointer to an array of ints representing the image data //return NULL on failure int *readPPMFile(char *fileName) { char c; char imageType[3]; int *imageData; //pointer to ppm image data read from file FILE *fp; //pointer to image file int i; int w, h, max; fp = fopen(fileName, "r"); //open file for reading. "r" for reading. Use "w" to open file for writing. if (fp == NULL) { printf("File cannot be opened. Check file name..\n"); return NULL; } //read magic number fscanf (fp, "%s", imageType); //Stop if image type is not P6 if (strcmp(imageType,"P6") != 0) { printf("Bad image format. Check image format and run again\n"); return NULL; } //Read and discard characters till you see a newline do { c = fgetc(fp); } while ( c != '\n'); c = fgetc(fp); //Read next character. This may be the # character if ( c == '#') { //file has a comment //Read and discard characters till you see a newline do { c = fgetc(fp); } while ( c != '\n'); } else {// file has no comment ungetc(c, fp); } fscanf(fp,"%d %d", &w, &h); fscanf(fp, "%d", &max); //Read and discard characters till you see a newline do { c = fgetc(fp); } while ( c != '\n'); //end of header processing imageData = malloc(3*w*h*sizeof(int)); //allocate enough space for all the pixels in the image if (imageData == NULL) { printf("Problems allocating memory..\n"); return NULL; } //read the image data into the imageData array //note how the pointer "imageData" can also be used as an array for(i=0; i < 3*w*h; i+=3) fscanf(fp, "%c%c%c", &imageData[i],&imageData[i+1],&imageData[i+2]); return imageData; }