8 Rawimage *readppm(Biobuf*, Rawimage*);
11 * fetch a non-comment character.
22 while((c = Bgetc(b)) != Beof && c != '\n')
30 * fetch a nonnegative decimal integer.
39 while((c = Bgetch(b)) != Beof && !isdigit(c))
47 } while((c = Bgetch(b)) != Beof && isdigit(c));
54 Bgetdecimalbit(Biobufhdr *b)
57 while((c = Bgetch(b)) != Beof && c != '0' && c != '1')
64 static int bitc, nbit;
77 return (bitc >> nbit) & 0x1;
89 readpixmap(int fd, int colorspace)
98 if(Binit(&b, fd, OREAD) < 0)
103 if((array = malloc(sizeof *array)) == nil)
105 if((array[0] = malloc(sizeof *array[0])) == nil)
107 memset(array[0], 0, sizeof *array[0]);
110 array[0]->chans[i] = nil;
112 e = "bad file format";
116 a = readppm(&b, array[0]);
133 errstr(buf, sizeof buf);
136 errstr(buf, sizeof buf);
141 typedef struct Pix Pix;
145 int (*fetch)(Biobufhdr*);
149 void (*flush)(Biobufhdr*);
153 { '1', 1, Bgetdecimalbit, 1, CY, 1, nil }, /* portable bitmap */
154 { '4', 1, Bgetbit, 1, CY, 1, Bflushbit }, /* raw portable bitmap */
155 { '2', 0, Bgetint, 1, CY, 0, nil }, /* portable greymap */
156 { '5', 0, Bgetc, 1, CY, 0, nil }, /* raw portable greymap */
157 { '3', 0, Bgetint, 3, CRGB, 0, nil }, /* portable pixmap */
158 { '6', 0, Bgetc, 3, CRGB, 0, nil }, /* raw portable pixmap */
163 readppm(Biobuf *b, Rawimage *a)
165 int i, ch, wid, ht, r, c;
166 int maxcol, nchan, invert;
167 int (*fetch)(Biobufhdr*);
173 e = "bad file format";
178 for(p=pix; p->magic; p++)
187 if(wid <= 0 || ht <= 0)
189 a->r = Rect(0,0,wid,ht);
199 for(i=0; i<p->nchan; i++)
200 if((rgb[i] = a->chans[i] = malloc(wid*ht)) == nil)
202 a->nchans = p->nchan;
204 a->chandesc = p->chandesc;
206 e = "error reading file";
211 for(r=0; r<ht; r++) {
212 for(c=0; c<wid; c++) {
213 for(i=0; i<nchan; i++) {
214 if((ch = (*fetch)(b)) < 0)
218 *rgb[i]++ = (ch * 255)/maxcol;
228 errstr(buf, sizeof buf);
231 errstr(buf, sizeof buf);