-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPPMLoader.cpp
More file actions
69 lines (57 loc) · 1.41 KB
/
PPMLoader.cpp
File metadata and controls
69 lines (57 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "PPMLoader.h"
#include <stdio.h>
#include <stdlib.h>
GLubyte *image;
PPMLoader::PPMLoader()
{
}
GLubyte* PPMLoader::loadPPM(char* file, int* width, int* height, int* max)
{
GLubyte* img;
FILE *fd;
int n, m;
int k, nm;
char c;
int i;
char b[100];
float s;
int red, green, blue;
/* first open file and check if it's an ASCII PPM (indicated by P3 at the start) */
fd = fopen(file, "r");
fscanf(fd,"%[^\n] ",b);
if(b[0]!='P'|| b[1] != '3')
{
printf("%s is not a PPM file!\n",file);
exit(0);
}
printf("%s is a PPM file\n", file);
fscanf(fd, "%c",&c);
/* next, skip past the comments - any line starting with #*/
while(c == '#')
{
fscanf(fd, "%[^\n] ", b);
printf("%s\n",b);
fscanf(fd, "%c",&c);
}
ungetc(c,fd);
/* now get the dimensions and max colour value from the image */
fscanf(fd, "%d %d %d", &n, &m, &k);
printf("%d rows %d columns max value= %d\n",n,m,k);
/* calculate number of pixels and allocate storage for this */
nm = n*m;
img = (GLubyte*)malloc(3*sizeof(GLuint)*nm);
s=255.0/k;
/* for every pixel, grab the read green and blue values, storing them in the image data array */
for(i=0;i<nm;i++)
{
fscanf(fd,"%d %d %d",&red, &green, &blue );
img[3*nm-3*i-3]=red*s;
img[3*nm-3*i-2]=green*s;
img[3*nm-3*i-1]=blue*s;
}
/* finally, set the "return parameters" (width, height, max) and return the image array */
*width = n;
*height = m;
*max = k;
return img;
}