Question: C PROGRAMMING Working with PPM files. I am struggling to understand. I have this to work with: HEADER FILE: struct pixel_t{ unsigned char r; unsigned
C PROGRAMMING
Working with PPM files. I am struggling to understand.
I have this to work with:
HEADER FILE:
struct pixel_t{ unsigned char r; unsigned char g; unsigned char b; };
struct header_t{ char* magicNum; int width; int height; int maxVal; }; /* pixel_t and header_t are defined before image_t so the compiler * will know what pixels and header are when we want to use them in image_t*/ struct image_t{ struct header_t header; struct pixel_t** pixels; };
/* function to read in header information from the ppm image */ void readHeader(FILE* input, struct image_t* image);
/*function to read in the pixel data from a ppm image */ void readPixels(FILE* input, struct image_t* image);
/* function to print out a P6 ppm image */ void printP6Image(FILE* output, struct image_t image);
/* function to print out a P3 ppm image */ void printP3Image(FILE* output, struct image_t image);
I need to fill out these functions but don't even know where to begin.
void readHeader(FILE* input, struct image_t* image){ // Step 1: Use malloc to allocate memory for image's header's magicNum. // It needs to hold 3 chars /* Accessing struct variables */ // In this function, the variable image is an image_t address(pointer) // so to access the header variable in the image struct, you need to // dereference image first like this (*image) then use the '.' operator // like this: (*image).header // This is so common, that a shorter way is to do this: image->header // The -> combines dereferencing the struct address, and the '.' operator // To then access the variables inside of image's header, you just use // the '.' operator // image->header.magicNum is the char pointer for magic num that belongs // to the header that belongs to the image variable in this function
// Step 2: use fscanf to read in the width, height, and magicNum and // store them into the the rest of image's header's variables // hint: the width is accessed like this: image->header.width
}
/* function to read in pixel information */ void readPixels(FILE* input, struct image_t* image){ /* because of how file pointers work, you don't have to re-read * the header information just to get to the right place in the file */
/* use a double for loop to fscanf in the r, g, and b values * from the input file into the image's pixel_t array */
}
/* function to print out a ppm image */
void printP6Image(FILE* output, struct image_t image){ /* Accessing the struct variables*/ /* use printf to print the header info in the correct format */
/* use a double for loop to print out the r,g,b values of the pixels */
} void printP3Image(FILE* output, struct image_t image){
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
