Question
My unhappy paths for this function are not passing. Due to the grading software, I am unable to see what is being entered in so
My unhappy paths for this function are not passing. Due to the grading software, I am unable to see what is being entered in so I can't see what I'm missing in this code. Can someone fix my code and "REWRITE THE ENTIRE CODE" provided with the changes made, please.
Here is the information given for the function:
Here is my functions:
#include
#include
#include
#include
#include "functions.h"
using namespace std;
void initializeImage(Pixel image[][MAX_HEIGHT]) {
// iterate through columns
for (unsigned int col = 0; col < MAX_WIDTH; col++) {
// iterate through rows
for (unsigned int row = 0; row < MAX_HEIGHT; row++) {
// initialize pixel
image[col][row] = {0, 0, 0};
}
}
}
void loadImage(string filename, Pixel image[][MAX_HEIGHT], unsigned int& width, unsigned int& height) {
ifstream ifs(filename);
if (!ifs.is_open()) {
throw std::runtime_error("Failed to open " + filename);
}
string Type;
ifs >> Type;
if (toupper(Type.at(0)) != 'P' || Type.at(1) != '3') {
throw std::runtime_error("Invalid type " + Type);
}
ifs >> width;
if (ifs.fail() || width <= 0 || width > MAX_WIDTH) {
throw std::runtime_error("Invalid dimensions");
}
ifs >> height;
if (ifs.fail() || height <= 0 || height > MAX_HEIGHT) {
throw std::runtime_error("Invalid dimensions");
}
int colorMax = 0;
ifs >> colorMax;
if (ifs.fail() || colorMax != 255) {
throw std::runtime_error("Invalid color value");
}
for (unsigned int row = 0; row < height; ++row) {
for (unsigned int col = 0; col < width; ++col) {
ifs >> image[col][row].r;
if (ifs.fail() || image[col][row].r < 0 || image[col][row].r > 255) {
throw std::runtime_error("Invalid color value");
}
ifs >> image[col][row].g;
if (ifs.fail() || image[col][row].g < 0 || image[col][row].g > 255) {
throw std::runtime_error("Invalid color value");
}
ifs >> image[col][row].b;
if (ifs.fail() || image[col][row].b < 0 || image[col][row].b > 255) {
throw std::runtime_error("Invalid color value");
}
}
}
}
Error message:
loadImage (unhappy paths) (0/3)
Filename does not exist
Preamble type is invalid (i.e. not P3 or p3)
Preamble height is not an integer
Color value is negative
Not enough color values in ppm
Too many color values in ppm expected loadImage(filename, image, w, h) to throw std::runtime_error, but nothing thrown
[FAIL] some test(s) failed in main
loadImage() Description: opens and validates a PPM file while populating an array of pixels. Should read in the preamble and pixel values (you can use a file stream). Should validate values as they are read in. Should use stream states to ensure successful input. PPM files are in row major order. Parameters: O string filename Name of PPM file to read from. Pixel image [] [MAX_HEIGHT] 2D array of pixels to populate (MAX_WIDTH X MAX_HEIGHT). In column major order. unsigned int& width Width of image (should be updated). unsigned int& height Returns: void. Exceptions: Height of image (should be updated). If the file cannot be opened, throw a runtime_error exception with the description "Failed to open ". If the file type is not "P3" or "p3", throw a runtime_error exception with the description "Invalid type ". Both dimensions should be positive integers and less than the maximum's defined in functions.h. If the dimensions are invalid, throw a runtime_error exception with the description "Invalid dimensions". Each pixel should be 3 (red, green, and blue) non-negative integers less than 256. If there is an invalid pixel value, throw a runtime_error exception with the description "Invalid color value". If there are not enough pixel values, throw a runtime_error exception with the description "Invalid color value". If there are too many pixels, throw a runtime_error exception with the description "Too many values".
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