Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function that reads PPM format images. A PPM image has an ASCII header f

ID: 3862237 • Letter: W

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 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 <cstdio>
#include <cstring>
#include "readWritePPM.h"

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 <cstdio>
#include <cstring>
#include "readWritePPM.h"

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

Explanation / Answer

#include <cstdio>
#include <cstring>
#include "readWritePPM.h"

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 <cstdio>
#include <cstring>
#include "readWritePPM.h"

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote