【发布时间】:2020-03-20 05:01:43
【问题描述】:
我正在制作一个将 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;
}
【问题讨论】:
-
(toupper(type[0]) != 'p')将永远是true。所以(toupper(type[0]) != 'P') && (toupper(type[0]) != 'p')将测试true是否有任何不以'P'开头的文件。fin >> filewidth;或fin >> fileheight;未能触发您的非整数错误。 -
@DavidC.Rankin 我已经想到了,但事实并非如此。请参阅检查高度和宽度是否不是整数的 fin.fail() 的错误语句下的“1”?嵌套 for 循环中的 fin.fail() 检查 RGB 值是否是整数,在错误返回下使用“2”,所以我可以看到是哪个导致它。它是 2 - 嵌套 for 循环中的 fin.fail()。
-
哦,你为什么有
fin >> ws;? std::basic_istream::operator>> 将跳过整数值之间的空格。ws在哪里声明?如果是char或int- 你有你的答案。它正在读取您的 RGB 的一部分,您最终会缺少文件的颜色。 -
我从班级论坛上的某个人那里得知,我们需要“在读取颜色值并使用 std::ws 消耗空白之前放置 eof”以检查是否没有足够的颜色值.所以我补充说,试图按照她所说的去做。我认为当没有足够的值时,其余的空间是空白?当我的程序读取这些空格时,这就是为什么它会引发错误,即它读取了一个非整数值并退出。 @DavidC.Rankin
-
听起来你已经掌握了它。如果没有 cmets,您的逻辑就有效。考虑一次读取,例如
if (!(fin >> red >> green >> blue)) { /* handle error */ }这将大大简化您的逻辑,并可以帮助您缩小问题范围。 (或直接阅读image[i][j].r...)如果您有一个零或巨大的height和width怎么办。值得和鸭子聊天,见How to debug small programs
标签: c++ arrays whitespace ppm