Question: In c++ please, im struggling to implement these functions. use reinterpret_cast use these functions and structs. enum class MagicNumber { kRawbits, kAscii, kFail }; struct
In c++ please, im struggling to implement these functions.
use reinterpret_cast
use these functions and structs.
enum class MagicNumber { kRawbits, kAscii, kFail };
struct Pixel {
int red;
int green;
int blue;
};
struct Image {
MagicNumber magic; // PPM format
std::vector
int width; // image width
int height; // image height
int max_color; // maximum color value
Pixel **pixels; // Acts as 2D array of pixels
};
// Reads the magic number of PPM file.
MagicNumber ReadMagicNum(std::ifstream &ifs);
// Parses ASCII PPM file.
Image ParseAsciiImage(std::ifstream &ifs);
// Writes the image struct to file in ASCII format.
void WriteAscii (const Image &img, std::ofstream &ifs);
void FreeImage(const Image &img);
-in the main function, argc needs to be 3, for the ppm file, andan output file
Part 1: Read the magic number and return enum
The first two bytes of the file are “magic number,” which tell ushow to read the rest of the file.
The header file ppm.h defines a MagicNumber enum class thatcontains three entries.
• kRawbits indicates that the PPM file is using a binaryformat. This corresponds to magic number P6.
• kAscii indicates that the PPM file is using the ASCII textformat. P3
• kFail indicates an unknown file type.
Implement the ReadMagicNum function
Dont assume that the file pointer is at the beginning of the file.Use seekg to seek to the beginning of the file.
1. Read the first character by using read to read 1char.
2. Read the second character with read.
3. Return the correct enum value based on the charactersthat you read.
Part 2: Parse a PPM in text mode
There are a few assumptions you can make to simplify thisprocess.
• Comments in the PPM file are assumed to only start at thebeginning of the line.
• Comments only exist after the magic number and before thewidth and height of the image. They cannot be put in the image datasection.
1. Read the magic number again inside the parsing function.This serves two purposes: to advance the file cursor, and to verifythat the correct function was called.
2. Create a new Image struct.
3. Read any comments that are present in the file. Commentsare lines that begin with the # character. Append these comments tothe comments field of the Image struct.
4. Heap allocate (new) enough memory for the pixels, relyingon the width and height of the image as defined in the file. Sanitycheck these values against some maximum bound. Feel free to rejectimages that are too large and would result in allocating anunreasonable amount of memory.
5. Use a nested for loop to read three lines at time andcreate a Pixel struct. Copy this Pixel struct into the 2D pixelsarray of the image.
6. Return the final image from the function.
Part 3: Write a PPM in text mode
Implement the WriteAsciiI function. Write every field of the imagestruct out to a file.
At this point, you should be able to run your program and it willread an ASCII PPM file and write out and identical PPM file.
Part 4: Implement FreeImage
Our image structures contain heap-allocated memory to store thepixels. Implement the FreeImage function in such a way that itfrees this memory.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
