11 pnm.c - implements reading and writing ppm/pnm/pbm files, uses io layer.
15 io_glue *ig = io_new_fd( fd );
16 i_img *im = i_readpnm_wiol(ig, -1); // no limit on how much is read
18 io_glue *ig = io_new_fd( fd );
19 return_code = i_writepnm_wiol(im, ig);
23 pnm.c implements the basic functions to read and write portable
24 anymap files. It uses the iolayer and needs either a seekable source
25 or an entire memory mapped buffer.
27 =head1 FUNCTION REFERENCE
29 Some of these functions are internal.
38 #define misspace(x) (x==' ' || x=='\n' || x=='\r' || x=='\t' || x=='\f' || x=='\v')
39 #define misnumber(x) (x <= '9' && x>='0')
41 static char *typenames[]={"ascii pbm", "ascii pgm", "ascii ppm", "binary pbm", "binary pgm", "binary ppm"};
44 * Type to encapsulate the local buffer
45 * management skipping over in a file
57 void init_buf(mbuf *mb, io_glue *ig) {
68 Fetches a character and advances in stream by one character.
69 Returns a pointer to the byte or NULL on failure (internal).
80 if (mb->cp == mb->len) {
82 mb->len = ig->readcb(ig, mb->buf, BSIZ);
84 mm_log((1, "i_readpnm: read error\n"));
88 mm_log((1, "i_readpnm: end of file\n"));
92 return &mb->buf[mb->cp++];
99 Fetches a character but does NOT advance. Returns a pointer to
100 the byte or NULL on failure (internal).
110 io_glue *ig = mb->ig;
111 if (mb->cp == mb->len) {
113 mb->len = ig->readcb(ig, mb->buf, BSIZ);
115 mm_log((1, "i_readpnm: read error\n"));
119 mm_log((1, "i_readpnm: end of file\n"));
123 return &mb->buf[mb->cp];
130 =item skip_spaces(mb)
132 Advances in stream until it is positioned at a
133 non white space character. (internal)
142 skip_spaces(mbuf *mb) {
144 while( (cp = gpeek(mb)) && misspace(*cp) ) if ( !gnext(mb) ) break;
151 =item skip_spaces(mb)
153 Advances in stream over whitespace and a comment if one is found. (internal)
162 skip_comment(mbuf *mb) {
165 if (!skip_spaces(mb)) return 0;
167 if (!(cp = gpeek(mb))) return 0;
169 while( (cp = gpeek(mb)) && (*cp != '\n' && *cp != '\r') ) {
170 if ( !gnext(mb) ) break;
182 Fetches the next number from stream and stores in i, returns true
183 on success else false.
186 i - integer to store result in
193 gnum(mbuf *mb, int *i) {
197 if (!skip_spaces(mb)) return 0;
199 while( (cp = gpeek(mb)) && misnumber(*cp) ) {
200 *i = *i*10+(*cp-'0');
208 =item i_readpnm_wiol(ig, length)
210 Retrieve an image and stores in the iolayer object. Returns NULL on fatal error.
213 length - maximum length to read from data source, before closing it -1
221 i_readpnm_wiol(io_glue *ig, int length) {
225 int width, height, maxval, channels, pcount;
234 mm_log((1,"i_readpnm(ig %p, length %d)\n", ig, length));
246 mm_log((1, "Hack is exiting\n"));
250 io_glue_commit_types(ig);
255 if (!cp || *cp != 'P') {
256 mm_log((1, "i_readpnm: Could not read header of file\n"));
260 if ( !(cp = gnext(&buf)) ) {
261 mm_log((1, "i_readpnm: Could not read header of file\n"));
267 if (type < 1 || type > 6) {
268 mm_log((1, "i_readpnm: Not a pnm file\n"));
272 if ( !(cp = gnext(&buf)) ) {
273 mm_log((1, "i_readpnm: Could not read header of file\n"));
277 if ( !misspace(*cp) ) {
278 mm_log((1, "i_readpnm: Not a pnm file\n"));
282 mm_log((1, "i_readpnm: image is a %s\n", typenames[type-1] ));
285 /* Read sizes and such */
287 if (!skip_comment(&buf)) {
288 mm_log((1, "i_readpnm: error reading before width\n"));
292 if (!gnum(&buf, &width)) {
293 mm_log((1, "i_readpnm: error reading width\n"));
297 if (!skip_comment(&buf)) {
298 mm_log((1, "i_readpnm: error reading before height\n"));
302 if (!gnum(&buf, &height)) {
303 mm_log((1, "i_readpnm: error reading height\n"));
307 if (!(type == 1 || type == 4)) {
308 if (!skip_comment(&buf)) {
309 mm_log((1, "i_readpnm: error reading before maxval\n"));
313 if (!gnum(&buf, &maxval)) {
314 mm_log((1, "i_readpnm: error reading maxval\n"));
319 if (!(cp = gnext(&buf)) || !misspace(*cp)) {
320 mm_log((1, "i_readpnm: garbage in header\n"));
324 channels = (type == 3 || type == 6) ? 3:1;
325 pcount = width*height*channels;
327 mm_log((1, "i_readpnm: (%d x %d), channels = %d, maxval = %d\n", width, height, channels, maxval));
329 im = i_img_empty_ch(NULL, width, height, channels);
332 case 1: /* Ascii types */
335 mult = type == 1 ? 255 : 1;
336 for(y=0;y<height;y++) for(x=0; x<width; x++) {
337 for(ch=0; ch<channels; ch++) {
339 if (gnum(&buf, &t)) val.channel[ch] = t;
341 mm_log((1,"i_readpnm: gnum() returned false in data\n"));
345 i_ppix(im, x, y, &val);
349 case 4: /* binary pbm */
350 for(y=0;y<height;y++) for(x=0; x<width; x+=8) {
351 if ( (uc = gnext(&buf)) ) {
353 int pc = width-x < 8 ? width-x : 8;
354 /* mm_log((1,"i_readpnm: y=%d x=%d pc=%d\n", y, x, pc)); */
355 for(xt = 0; xt<pc; xt++) {
356 val.channel[0] = (*uc & (128>>xt)) ? 0 : 255;
357 i_ppix(im, x+xt, y, &val);
360 mm_log((1,"i_readpnm: gnext() returned false in data\n"));
366 case 5: /* binary pgm */
367 case 6: /* binary ppm */
368 for(y=0;y<height;y++) for(x=0; x<width; x++) {
369 for(ch=0; ch<channels; ch++) {
370 if ( (uc = gnext(&buf)) ) val.channel[ch] = *uc;
372 mm_log((1,"i_readpnm: gnext() returned false in data\n"));
376 i_ppix(im, x, y, &val);
380 mm_log((1, "type %s [P%d] unsupported\n", typenames[type-1], type));
387 i_writeppm(i_img *im,int fd) {
391 mm_log((1,"i_writeppm(im* 0x%x,fd %d)\n",im,fd));
392 if (im->channels!=3) {
393 mm_log((1,"i_writeppm: ppm is 3 channel only (current image is %d)\n",im->channels));
397 sprintf(header,"P6\n#CREATOR: Imager\n%d %d\n255\n",im->xsize,im->ysize);
399 if (mywrite(fd,header,strlen(header))<0) {
400 mm_log((1,"i_writeppm: unable to write ppm header.\n"));
404 rc=mywrite(fd,im->data,im->bytes);
406 mm_log((1,"i_writeppm: unable to write ppm data.\n"));