132001f49Smrg 232001f49Smrgtypedef struct { 332001f49Smrg size_t sizeX, sizeY; 432001f49Smrg GLubyte *data; 532001f49Smrg} PPMImage; 632001f49Smrg 732001f49Smrgstatic PPMImage *LoadPPM(const char *filename) 832001f49Smrg{ 932001f49Smrg char buff[16]; 1032001f49Smrg PPMImage *result; 1132001f49Smrg FILE *fp; 1232001f49Smrg int maxval, w, h; 1332001f49Smrg 1432001f49Smrg fp = fopen(filename, "rb"); 1532001f49Smrg if (!fp) 1632001f49Smrg { 1732001f49Smrg fprintf(stderr, "Unable to open file `%s'\n", filename); 1832001f49Smrg exit(1); 1932001f49Smrg } 2032001f49Smrg 2132001f49Smrg if (!fgets(buff, sizeof(buff), fp)) 2232001f49Smrg { 2332001f49Smrg perror(filename); 2432001f49Smrg exit(1); 2532001f49Smrg } 2632001f49Smrg 2732001f49Smrg if (buff[0] != 'P' || buff[1] != '6') 2832001f49Smrg { 2932001f49Smrg fprintf(stderr, "Invalid image format (must be `P6')\n"); 3032001f49Smrg exit(1); 3132001f49Smrg } 3232001f49Smrg 3332001f49Smrg result = (PPMImage *) malloc(sizeof(PPMImage)); 3432001f49Smrg if (!result) 3532001f49Smrg { 3632001f49Smrg fprintf(stderr, "Unable to allocate memory\n"); 3732001f49Smrg exit(1); 3832001f49Smrg } 3932001f49Smrg 4032001f49Smrg if (fscanf(fp, "%d %d", &w, &h) != 2) 4132001f49Smrg { 4232001f49Smrg fprintf(stderr, "Error loading image `%s'\n", filename); 4332001f49Smrg exit(1); 4432001f49Smrg } 4532001f49Smrg result->sizeX = w; 4632001f49Smrg result->sizeY = h; 4732001f49Smrg 4832001f49Smrg if (fscanf(fp, "%d", &maxval) != 1) 4932001f49Smrg { 5032001f49Smrg fprintf(stderr, "Error loading image `%s'\n", filename); 5132001f49Smrg exit(1); 5232001f49Smrg } 5332001f49Smrg 5432001f49Smrg while (fgetc(fp) != '\n') 5532001f49Smrg ; 5632001f49Smrg 5732001f49Smrg result->data = (GLubyte *) malloc(3 * result->sizeX * result->sizeY); 5832001f49Smrg if (!result) 5932001f49Smrg { 6032001f49Smrg fprintf(stderr, "Unable to allocate memory\n"); 6132001f49Smrg exit(1); 6232001f49Smrg } 6332001f49Smrg 6432001f49Smrg if (fread(result->data, 3 * result->sizeX, result->sizeY, fp) != result->sizeY) 6532001f49Smrg { 6632001f49Smrg fprintf(stderr, "Error loading image `%s'\n", filename); 6732001f49Smrg exit(1); 6832001f49Smrg } 6932001f49Smrg 7032001f49Smrg fclose(fp); 7132001f49Smrg 7232001f49Smrg return result; 7332001f49Smrg} 7432001f49Smrg 75