jas_image_t *pnm_decode(jas_stream_t *in, char *opts) { pnm_hdr_t hdr; jas_image_t *image; jas_image_cmptparm_t cmptparms[3]; jas_image_cmptparm_t *cmptparm; int i; if (opts) { jas_eprintf("warning: ignoring options\n"); } /* Read the file header. */ if (pnm_gethdr(in, &hdr)) { return 0; } /* Create an image of the correct size. */ for (i = 0, cmptparm = cmptparms; i < hdr.numcmpts; ++i, ++cmptparm) { cmptparm->tlx = 0; cmptparm->tly = 0; cmptparm->hstep = 1; cmptparm->vstep = 1; cmptparm->width = hdr.width; cmptparm->height = hdr.height; cmptparm->prec = pnm_maxvaltodepth(hdr.maxval); cmptparm->sgnd = hdr.sgnd; } if (!(image = jas_image_create(hdr.numcmpts, cmptparms, JAS_CLRSPC_UNKNOWN))) { return 0; } if (hdr.numcmpts == 3) { jas_image_setclrspc(image, JAS_CLRSPC_SRGB); jas_image_setcmpttype(image, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R)); jas_image_setcmpttype(image, 1, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G)); jas_image_setcmpttype(image, 2, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B)); } else { jas_image_setclrspc(image, JAS_CLRSPC_SGRAY); jas_image_setcmpttype(image, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y)); } /* Read image data from stream into image. */ if (pnm_getdata(in, &hdr, image)) { jas_image_destroy(image); return 0; } return image; }
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) {
jas_image_t *pnm_decode(jas_stream_t *in, const char *optstr) { pnm_hdr_t hdr; jas_image_t *image; jas_image_cmptparm_t cmptparms[3]; jas_image_cmptparm_t *cmptparm; int i; pnm_dec_importopts_t opts; size_t num_samples; image = 0; JAS_DBGLOG(10, ("pnm_decode(%p, \"%s\")\n", in, optstr ? optstr : "")); if (pnm_dec_parseopts(optstr, &opts)) { goto error; } /* Read the file header. */ if (pnm_gethdr(in, &hdr)) { goto error; } JAS_DBGLOG(10, ( "magic %lx; width %lu; height %ld; numcmpts %d; maxval %ld; sgnd %d\n", JAS_CAST(unsigned long, hdr.magic), JAS_CAST(long, hdr.width), JAS_CAST(long, hdr.height), hdr.numcmpts, JAS_CAST(long, hdr.maxval), hdr.sgnd) ); if (!jas_safe_size_mul3(hdr.width, hdr.height, hdr.numcmpts, &num_samples)) { jas_eprintf("image too large\n"); goto error; } if (opts.max_samples > 0 && num_samples > opts.max_samples) { jas_eprintf( "maximum number of samples would be exceeded (%zu > %zu)\n", num_samples, opts.max_samples); goto error; } /* Create an image of the correct size. */ for (i = 0, cmptparm = cmptparms; i < hdr.numcmpts; ++i, ++cmptparm) { cmptparm->tlx = 0; cmptparm->tly = 0; cmptparm->hstep = 1; cmptparm->vstep = 1; cmptparm->width = hdr.width; cmptparm->height = hdr.height; cmptparm->prec = pnm_maxvaltodepth(hdr.maxval); cmptparm->sgnd = hdr.sgnd; } if (!(image = jas_image_create(hdr.numcmpts, cmptparms, JAS_CLRSPC_UNKNOWN))) { goto error; } if (hdr.numcmpts == 3) { jas_image_setclrspc(image, JAS_CLRSPC_SRGB); jas_image_setcmpttype(image, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R)); jas_image_setcmpttype(image, 1, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G)); jas_image_setcmpttype(image, 2, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B)); } else { jas_image_setclrspc(image, JAS_CLRSPC_SGRAY); jas_image_setcmpttype(image, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y)); } /* Read image data from stream into image. */ if (pnm_getdata(in, &hdr, image, opts.allow_trunc)) { goto error; } return image; error: if (image) { jas_image_destroy(image); } return 0; }