Question
Write a function that reads PPM format images. A PPM image has an ASCII header followed by binary pixel data. The header looks something like
Write a function that reads PPM format images. A PPM image has an ASCII header followed by binary pixel data. The header looks something like this:
P6
650 652
255
P6 is a string that indicates this is a PPM image. Most file formats start with an identifying string like this. It is called a "magic number". The next two fields are the width and height of the image. The last field gives the maximum pixel value. You can expect it will always be 255. At the end of the header is a and then the binary pixel data. The image is in color, so there are three bytes (red, green, blue) for every pixel. You should store the pixel data in a two-dimensional array ints. Each int should store the red, green, and blue bytes from on pixel. Your final program should have one function that reads a PPM image and returns a two-dimensional array of ints and the other should take a two-dimensional array of ints as an argument and write a PPM image. Your program should read an image and then write it back out to another file. Use pointer casting to turn an int into an array of four bytes. Leave one of the bytes unused. Use a separate array of bytes for I/O and copy bytes between that array and your two-dimensional array. Don't forget to delete any data you allocate on the heap.
Below are my main.cc and readWritePPM.cc files. It keeps giving me these errors. Fix this code so it does what the instruction says to do.
main.cc
#include
int main() {
//char fileName[50] = "binary_pixels.txt"; char fileName[50] = "test.ppm"; int width = 0; // width of the image int height = 0; // heigt of the image
// read the PPM file and store its contents inside an array and return the pointer to that array to pixelArray unsigned char* pixelArray = readPPM(fileName, &width, &height);
// new line printf(" ");
// write the PPM format images writePPM("binaryPixel.txt", width, height, pixelArray);
// delete memory on heap delete [] pixelArray; pixelArray = NULL;
} // end of main
readWritePPM.cc
#include
unsigned char* readPPM(const char* fileName, int* width, int* height) {
// open the file to read just the header reading FILE* fp = fopen(fileName, "rb"); // does the file exist? if (fp == NULL) { //??? } // read width and height int read = fscanf(fp, "P6 %d %d 255 ", width, height);
// if fscanf did not read width and height if (read != 2) { //??? } printf("Reading PPM file ");
// Use the "Numerical Recipies trick" for storing the two-dimensional array unsigned char** pixels = new char*[height]; *pixels = new char[(*width)*(*height)*4];
for(int i = 1; i < height; ++i) { pixels[i] = array[i-1] + width; } // end of for loop
//unsigned char* pixels = new unsigned char[(*width)*(*height)*3];
// read binary data fread(pixels, sizeof(unsigned char), (*width)*(*height)*4, fp);
// close file fclose(fp);
// return the array return pixels; } // end of readPPM
void writePPM(const char* fileName, int width, int height, unsigned char* pixels) {
// open file FILE* fp = fopen(fileName, "w");
// write to a fle printf("Writing PPM data to a file ");
fwrite(pixels, sizeof(unsigned char), width*height*4, fp);
// close file fclose(fp);
} // end of writePPM
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started