static int pnm_gethdr(jas_stream_t *in, pnm_hdr_t *hdr) { int_fast32_t maxval; int_fast32_t width; int_fast32_t height; if (pnm_getint16(in, &hdr->magic) || pnm_getsintstr(in, &width) || pnm_getsintstr(in, &height)) { return -1; } hdr->width = width; hdr->height = height; if (pnm_type(hdr->magic) != PNM_TYPE_PBM) { if (pnm_getsintstr(in, &maxval)) { return -1; } } else { maxval = 1; } if (maxval < 0) { hdr->maxval = -maxval; hdr->sgnd = true; } else { hdr->maxval = maxval; hdr->sgnd = false; } switch (pnm_type(hdr->magic)) { case PNM_TYPE_PBM: case PNM_TYPE_PGM: hdr->numcmpts = 1; break; case PNM_TYPE_PPM: hdr->numcmpts = 3; break; default: abort(); break; } return 0; }
static int pnm_getdata(jas_stream_t *in, pnm_hdr_t *hdr, jas_image_t *image) { int ret; #if 0 int numcmpts; #endif int cmptno; int fmt; jas_matrix_t *data[3]; int x; int y; int_fast64_t v; int depth; int type; int c; int n; ret = -1; #if 0 numcmpts = jas_image_numcmpts(image); #endif fmt = pnm_fmt(hdr->magic); type = pnm_type(hdr->magic); depth = pnm_maxvaltodepth(hdr->maxval); data[0] = 0; data[1] = 0; data[2] = 0; for (cmptno = 0; cmptno < hdr->numcmpts; ++cmptno) { if (!(data[cmptno] = jas_matrix_create(1, hdr->width))) { goto done; } } for (y = 0; y < hdr->height; ++y) { if (type == PNM_TYPE_PBM) { if (fmt == PNM_FMT_BIN) { for (x = 0; x < hdr->width;) { if ((c = jas_stream_getc(in)) == EOF) { goto done; } n = 8; while (n > 0 && x < hdr->width) { jas_matrix_set(data[0], 0, x, 1 - ((c >> 7) & 1)); c <<= 1; --n; ++x; } } } else { for (x = 0; x < hdr->width; ++x) { int uv; if (pnm_getbitstr(in, &uv)) { goto done; } jas_matrix_set(data[0], 0, x, 1 - uv); } } } else { for (x = 0; x < hdr->width; ++x) {
/** * Convert PNM image to pixel buffer * * @v image PNM image * @v pixbuf Pixel buffer to fill in * @ret rc Return status code */ static int pnm_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) { struct pnm_context pnm; int width; int height; int max; int rc; /* Initialise PNM context */ pnm.type = pnm_type ( image ); if ( ! pnm.type ) { rc = -ENOTSUP; goto err_type; } pnm.offset = sizeof ( struct pnm_signature ); pnm.ascii_len = PNM_ASCII_LEN; /* Extract width */ if ( ( width = pnm_ascii ( image, &pnm ) ) < 0 ) { rc = width; goto err_width; } /* Extract height */ if ( ( height = pnm_ascii ( image, &pnm ) ) < 0 ) { rc = height; goto err_height; } /* Extract maximum scalar value, if not predefined */ if ( pnm.type->flags & PNM_BITMAP ) { pnm.max = ( ( 1 << pnm.type->packing ) - 1 ); pnm.ascii_len = 1; } else { if ( ( max = pnm_ascii ( image, &pnm ) ) < 0 ) { rc = max; goto err_max; } pnm.max = max; } if ( pnm.max == 0 ) { DBGC ( image, "PNM %s has invalid maximum value 0\n", image->name ); rc = -EINVAL; goto err_max; } DBGC ( image, "PNM %s is type %c width %d height %d max %d\n", image->name, pnm.type->type, width, height, pnm.max ); /* Allocate pixel buffer */ *pixbuf = alloc_pixbuf ( width, height ); if ( ! *pixbuf ) { rc = -ENOMEM; goto err_alloc_pixbuf; } /* Extract pixel data */ if ( ( rc = pnm_data ( image, &pnm, *pixbuf ) ) != 0 ) goto err_data; return 0; err_data: pixbuf_put ( *pixbuf ); err_alloc_pixbuf: err_max: err_height: err_width: err_type: return rc; }