在尝试存储 .ppm 文件的图像数据时,我遇到了一个错误,上面写着:
Map.exe 中 0x01071712 处的未处理异常:0xC0000005:访问冲突写入位置 0x0bf13746。
我认为这意味着它写入不属于图像数组的内存的某些部分。但是,我不太清楚如何补救。任何帮助表示赞赏。为了简单起见,我在这个 C++ 程序中使用了 C 函数。我对 C++ IO 套件的每一个小方面都不够熟悉,无法尝试任何东西。该程序确实涉及 OpenGL 的使用,如果这有帮助的话。
#include <stdio.h>
#include <gl/glut.h>
int x; //x and y determine window size based upon image size
int y;
GLubyte image[3 * 1401 * 1198];
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glDrawPixels(x, y, GL_RGB, GL_UNSIGNED_BYTE, image);
glFlush();
}
//void myFunc(int x, int y){
//}
int main(int argc, char **argv){
//boilerplate stuff
FILE *rawData;
char c;
char buffer[70];
int k;
int red;
int green;
int blue;
rawData = fopen("europe.ppm", "rb");
fscanf(rawData, "%[^\n]", buffer);
if(buffer[0] != 'P' || buffer[1] != '6'){
exit(0);
}
fscanf(rawData, "%c", &c);
while(c == '#'){
fscanf(rawData, "%[^\n] ", buffer);
printf("%s\n", buffer);
fscanf(rawData, "%c", &c);
}
ungetc(c, rawData);
fscanf(rawData, "%d %d %d", &x, &y, &k); //only reading resolution in because of ppm file format
float s = 255.0 / k;
if (k == 255) for(int i = 0; 1 < (x * y); i++){
fscanf(rawData, "%d %d %d", &red, &green, &blue);
image[3*x*y - (3*i)-3] = red;
image[3*x*y - (3*i)-2] = green;
image[3*x*y - (3*i)-1] = blue;
}
else for(int i = 0; i < (x*y); i++){
fscanf(rawData, "%d %d %d", &red, &green, &blue);
image[3*x*y - (3*i)-3] = red * s;
image[3*x*y - (3*i)-2] = green * s;
image[3*x*y - (3*i)-1] = blue * s;
}
fclose(rawData);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(x, y);
glutCreateWindow("Europe");
glutDisplayFunc(display);
//glutPassiveMotionFunc(myFunc);
glutMainLoop();
return 0;
}