0

While trying to store the image data for a .ppm file I trip an error that says:

Unhandled exception at 0x01071712 in Map.exe: 0xC0000005: Access violation writing location 0x0bf13746.

I assume this means its writing to some part of memory that isn't part of the image array. However, I don't quite know how to remedy it. Any help is appreciated. I'm using C functions for this C++ program for simplicity. I'm not familiar enough with every little facet of the C++ IO suite to try anything with that. This program does involve usage of OpenGL, if that helps anything.

#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;
}
Halvor Holsten Strand
  • 18,479
  • 16
  • 70
  • 84

0 Answers0