我正在制作一个将ppm图像加载到二维数组中的函数,其中数组中的每个项目都是一个保存红色,绿色和蓝色值的结构。除其他外,函数需要检查给定图像的特定高度和宽度尺寸,文件是否具有足够的颜色值来填充数组。此错误检查的单元测试失败并退出,并显示标记非整数值的错误。
我该如何解决? (我正在尝试在嵌套的for循环的开头检查颜色值是否不足)
bool loadImage(string filename, Pixel** image, int width, int height) {
// checks if file can open
ifstream fin(filename);
if (!fin.is_open()) {
cout << "Error: failed to open input file - " << filename << endl;
return false;
}
// checking if p3
char type[3];
fin >> type; // should be p3
if (((toupper(type[0]) != 'P') && (toupper(type[0]) != 'p')) || (type[1] != '3')) {
cout << "Error: type is " << type << " instead of P3" << endl;
return false;
}
// input value does not match value in file (entered width and height not same as that of image)
int filewidth = 0;
int fileheight = 0;
fin >> filewidth;
fin >> fileheight;
if (fin.fail()) {
cout << "Error: read non-integer value" << endl;
cout << "1" << endl;
return false;
}
if (filewidth != width) {
cout << "Error: input width (" << width << ") does not match value in file (" << filewidth << ")" << endl;
return false;
}
if (fileheight != height) {
cout << "Error: input height (" << height << ") does not match value in file (" << fileheight << ")" << endl;
return false;
}
// get max color from preamble
int maxColor = 0;
fin >> maxColor;
if (maxColor != 255) {
cout << "Error: file is not using RGB color values" << endl;
return false;
}
// get RGB color values
int red=0;
int green=0;
int blue=0;
for (int i = 0; i < height; i++) { // switched height and width and i++ and j++ to ++i and ++j
for (int j = 0; j < width; j++) {
if (fin.eof()) {
cout << "Error: not enough color values" << endl;
return false;
}
fin >> ws;
fin >> red;
if (fin.eof()) {
cout << "Error: not enough color values" << endl;
return false;
}
fin >> ws;
fin >> green;
if (fin.eof()) {
cout << "Error: not enough color values" << endl;
return false;
}
fin >> ws;
fin >> blue;
if (fin.eof() && i < (height -1) && j < (width -1)) { // reads eof and hasn't reached full span of file
cout << "Error: not enough color values" << endl;
return false;
}
// checking valid color range
if (red < 0 || red > 255) {
cout << "Error: invalid color value " << red << endl;
return false;
}
if (green < 0 || green > 255) {
cout << "Error: invalid color value " << green << endl;
return false;
}
if (blue < 0 || blue > 255) {
cout << "Error: invalid color value " << blue << endl;
return false;
}
// non integer values check
if (fin.fail()) {
cout << "Error: read non-integer value" << endl;
return false;
}
image[j][i].r = red;
image[j][i].g = green;
image[j][i].b = blue;
}
}
// too many color values
fin >> red;
if (!fin.eof()) {
cout << "Error: too many color values" << endl;
return false;
}
return true;
}
答案 0 :(得分:0)
我知道了。我如何检查ppm图像中是否没有足够的颜色是一个问题。我需要考虑一个事实,就是我退出时会出现以下错误:只要没有足够的颜色值,就会检查非整数值。
下面只是我更改的嵌套for循环开头的一段。
int red=0;
int green=0;
int blue=0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (fin.eof()) {
cout << "Error: not enough color values" << endl;
return false;
}
fin >> ws;
fin >> red;
if (fin.eof() && fin.fail()) { // reads eof and hasn't reached full span of file
cout << "Error: not enough color values" << endl;
return false;
}
fin >> ws;
fin >> green;
if (fin.eof() && fin.fail()) { // reads eof and hasn't reached full span of file
cout << "Error: not enough color values" << endl;
return false;
}
fin >> ws;
fin >> blue;
if (fin.eof() && fin.fail()) { // reads eof and hasn't reached full span of file
cout << "Error: not enough color values" << endl;
return false;
}