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 comments; // All comments 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);

Part 1: Read the magic number and return enum The first two bytes of the file are magic number, which tell us how to read the rest of the file. The header file ppm.h defines a MagicNumber enum class that contains three entries. kRawbits indicates that the PPM file is using a binary format. This corresponds to magic number P6. kAscii indicates that the PPM file is using the ASCII text format. 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 1 char. 2. Read the second character with read. 3. Return the correct enum value based on the characters that you read. Part 2: Parse a PPM in text mode There are a few assumptions you can make to simplify this process. Comments in the PPM file are assumed to only start at the beginning of the line. Comments only exist after the magic number and before the width and height of the image. They cannot be put in the image data section. 1. Read the magic number again inside the parsing function. This serves two purposes: to advance the file cursor, and to verify that the correct function was called. 2. Create a new Image struct. 3. Read any comments that are present in the file. Comments are lines that begin with the # character. Append these comments to the comments field of the Image struct. 4. Heap allocate (new) enough memory for the pixels, relying on the width and height of the image as defined in the file. Sanity check these values against some maximum bound. Feel free to reject images that are too large and would result in allocating an unreasonable amount of memory. 5. Use a nested for loop to read three lines at time and create a Pixel struct. Copy this Pixel struct into the 2D pixels array 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 image struct out to a file. At this point, you should be able to run your program and it will read an ASCII PPM file and write out and identical PPM file.

Part 4: Implement FreeImage Our image structures contain heap-allocated memory to store the pixels. Implement the FreeImage function in such a way that it frees this memory.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!