MCPcopy Create free account
hub / github.com/VirtualGL/virtualgl / ppm_load

Function ppm_load

util/bmp.c:102–170  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

100
101
102static int ppm_load(int *fd, unsigned char **buf, int *width, int align,
103 int *height, PF *pf, enum BMPORN orientation, int ascii)
104{
105 FILE *file = NULL; int ret = 0, scaleFactor, dstPitch;
106 unsigned char *tempbuf = NULL; char temps[255], temps2[255];
107 int numRead = 0, totalRead = 0, pixel[3], i, j;
108
109 if((file = fdopen(*fd, "r")) == NULL) THROW(strerror(errno));
110
111 do
112 {
113 if(!fgets(temps, 255, file)) THROW("Read error");
114 if(strlen(temps) == 0 || temps[0] == '\n') continue;
115 if(sscanf(temps, "%s", temps2) == 1 && temps2[1] == '#') continue;
116 switch(totalRead)
117 {
118 case 0:
119 if((numRead = sscanf(temps, "%d %d %d", width, height,
120 &scaleFactor)) == EOF)
121 THROW("Read error");
122 break;
123 case 1:
124 if((numRead = sscanf(temps, "%d %d", height, &scaleFactor)) == EOF)
125 THROW("Read error");
126 break;
127 case 2:
128 if((numRead = sscanf(temps, "%d", &scaleFactor)) == EOF)
129 THROW("Read error");
130 break;
131 }
132 totalRead += numRead;
133 } while(totalRead < 3);
134 if((*width) < 1 || (*height) < 1 || scaleFactor < 1)
135 THROW("Corrupt PPM header");
136
137 dstPitch = (((*width) * pf->size) + (align - 1)) & (~(align - 1));
138 if((*buf = (unsigned char *)malloc(dstPitch * (*height))) == NULL)
139 THROW("Memory allocation error");
140 if(ascii)
141 {
142 for(j = 0; j < *height; j++)
143 {
144 for(i = 0; i < *width; i++)
145 {
146 if(fscanf(file, "%d%d%d", &pixel[0], &pixel[1], &pixel[2]) != 3)
147 THROW("Read error");
148 pf->setRGB(&(*buf)[j * dstPitch + i * pf->size],
149 pixel[0] * 255 / scaleFactor, pixel[1] * 255 / scaleFactor,
150 pixel[2] * 255 / scaleFactor);
151 }
152 }
153 }
154 else
155 {
156 if(scaleFactor != 255)
157 THROW("Binary PPMs must have 8-bit components");
158 if((tempbuf = (unsigned char *)malloc((*width) * (*height) * 3)) == NULL)
159 THROW("Memory allocation error");

Callers 1

bmp_loadFunction · 0.85

Calls 2

pixelConvertFunction · 0.85
pf_getFunction · 0.85

Tested by

no test coverage detected