我在使用pnm文件時遇到了一些麻煩(這很明顯,否則我不會在這裏發佈XD)。事情是,我的老師要求我們用二進制模式開發一個簡單的pnm閱讀器,然後將其打印到屏幕上。我正在使用libEGL(框架可用here)。我的問題是,它只適用於這兩個圖像,並與其他任何失敗。PNM閱讀器:無法在二進制模式下讀取一些文件
隨着birch.pnm和checkers.pnm的工作,但cathedral.pnm,cotton.pnm和fish_tile.pnm它只是簡單的進入一個無限循環或拋出和錯誤。
的圖像是avaliable here
我的代碼如下:
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "engcomp_glib.h"
using namespace std;
struct RGB{
char red, green, blue;
};
int main(int argc, char* argv[]){
RGB **image;
RGB pixel;
//ifstream _file("..\\bin\\birch.pnm");
ifstream _file("..\\bin\\checkers.pnm");
//ifstream _file("..\\bin\\cotton.pnm");
//ifstream _file("..\\bin\\cathedral.pnm");
//ifstream _file("..\\bin\\fish_tile.pnm");
string type, maxColor;
int width, height;
if(_file){
_file >> type;
if(type != "P6")
cout << "Error! File type is not allowed." << endl;
_file >> width >> height >> maxColor;
_file.close();
egl_inicializar(width, height, true);
image = new RGB *[height];
for(int row = 0; row < height; row++)
image[row] = new RGB[width];
//Working 8D
//_file.open("..\\bin\\birch.pnm", ios::binary);
_file.open("..\\bin\\checkers.pnm", ios::binary);
//Not working D:<
//_file.open("..\\bin\\cathedral.pnm", ios::binary);
//_file.open("..\\bin\\fish_tile.pnm", ios::binary);
//_file.open("..\\bin\\cotton.pnm", ios::binary);
//imagem img; img.carregar("..\\bin\\birch.pnm");
_file.seekg(0, _file.end);
int size = _file.tellg();
int currentSize = 0, counter = 0;
char byte;
_file.seekg(0, _file.beg);
do{
_file.read(reinterpret_cast<char *> (&byte), sizeof(char));
if(byte == 10 || byte == 13)
counter++;
}while(counter < 3);
int rows = 0, columns = 0;
while(size != currentSize){
_file.read(reinterpret_cast<char *> (&pixel), sizeof(RGB));
if(rows < height && columns < width){
image[rows][columns] = pixel;
rows++;
}
else if(rows == height){
rows = 0;
columns++;
image[rows][columns] = pixel;
rows++;
}
//else if(columns >= width)
//currentSize = size;
currentSize = _file.tellg();
}
_file.close();
while(!key[SDLK_ESCAPE]){
for(int row = 0; row < height; row++)
for(int column = 0; column < width; column++)
//egl_pixel(row, column, image[row][column].red, image[row][column].green, image[row][column].blue);
egl_pixel(column, row, image[column][row].red, image[column][row].green, image[column][row].blue);
//img.desenha(0, 0);
egl_desenha_frame(false);
}
}
egl_finalizar();
return 0;
}
它沒有任何意義,因爲它適用於他們兩個,應該努力形成他們都 我睜開眼睛所有這些都在一個文本編輯器中,並且它們有標題,所以問題不在那裏。我究竟做錯了什麼?我的同事編寫了一個代碼,將像素存儲到大小爲[height * width]的數組中,幾乎可以讀取所有圖像,但可以讀取cathedral.pnm。
感謝您的耐心和幫助:)