我正在编写一个简单的PGM文件阅读器,用于基本的CV想法,我有一个奇怪的问题 . 我的方法似乎适用于对称文件(例如255 x 255),但是当我尝试读取非对称文件(300 x 246)时,我得到了一些奇怪的输入 . 一个文件读取到某个点,然后将ESCAPE字符(ASCII 27)转储到图像的其余部分(见下文),而其他文件则不会读取 . 我认为这可能是一些有缺陷的逻辑或内存问题 . 任何帮助,将不胜感激 .
// Process files of binary type (P5)
else if(holdString[1] == '5') {
// Assign fileType value
fileType = 5;
// Read in comments and discard
getline(fileIN, holdString);
// Read in image Width value
fileIN >> width;
// Read in image Height value
fileIN >> height;
// Read in Maximum Grayscale Value
fileIN >> max;
// Determine byte size if Maximum value is over 256 (1 byte)
if(max < 256) {
// Collection variable for bytes
char readChar;
// Assign image dynamic memory
*image = new int*[height];
for(int index = 0; index < height; index++) {
(*image)[index] = new int[width];
}
// Read in 1 byte at a time
for(int row = 0; row < height; row++) {
for(int column = 0; column < width; column++) {
fileIN.get(readChar);
(*image)[row][column] = (int) readChar;
}
}
// Close the file
fileIN.close();
} else {
// Assign image dynamic memory
// Read in 2 bytes at a time
// Close the file
}
}
1 回答
稍微修改一下,并提出了至少大部分解决方案 . 使用.read()函数,我能够绘制整个文件,然后将其逐个读入int数组 . 我保留了动态内存,因为我想绘制不同大小的文件,但我确实更加注意它是如何读入数组的,所以谢谢你的建议,Mark . 编辑似乎适用于宽度达1000像素或高达1000像素的文件,这对于我正在使用的文件来说很好 . 之后,它会扭曲,但我仍然会考虑不读取文件 .