📄 pnm_dec.c
字号:
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) { for (cmptno = 0; cmptno < hdr->numcmpts; ++cmptno) { if (fmt == PNM_FMT_BIN) { /* The sample data is in binary format. */ if (hdr->sgnd) { /* The sample data is signed. */ int_fast32_t sv; if (pnm_getsint(in, depth, &sv)) { if (!pnm_allowtrunc) { goto done; } sv = 0; } v = sv; } else { /* The sample data is unsigned. */ uint_fast32_t uv; if (pnm_getuint(in, depth, &uv)) { if (!pnm_allowtrunc) { goto done; } uv = 0; } v = uv; } } else { /* The sample data is in text format. */ if (hdr->sgnd) { /* The sample data is signed. */ int_fast32_t sv; if (pnm_getsintstr(in, &sv)) { if (!pnm_allowtrunc) { goto done; } sv = 0; } v = sv; } else { /* The sample data is unsigned. */ uint_fast32_t uv; if (pnm_getuintstr(in, &uv)) { if (!pnm_allowtrunc) { goto done; } uv = 0; } v = uv; } } jas_matrix_set(data[cmptno], 0, x, v); } } } for (cmptno = 0; cmptno < hdr->numcmpts; ++cmptno) { if (jas_image_writecmpt(image, cmptno, 0, y, hdr->width, 1, data[cmptno])) { goto done; } } } ret = 0;done: for (cmptno = 0; cmptno < hdr->numcmpts; ++cmptno) { if (data[cmptno]) { jas_matrix_destroy(data[cmptno]); } } return ret;}/******************************************************************************\* Miscellaneous functions.\******************************************************************************/static int pnm_getsint(jas_stream_t *in, int wordsize, int_fast32_t *val){ uint_fast32_t tmpval; if (pnm_getuint(in, wordsize, &tmpval)) { return -1; } if (val) { assert((tmpval & (1 << (wordsize - 1))) == 0); *val = tmpval; } return 0;}static int pnm_getuint(jas_stream_t *in, int wordsize, uint_fast32_t *val){ uint_fast32_t tmpval; int c; int n; tmpval = 0; n = (wordsize + 7) / 8; while (--n >= 0) { if ((c = jas_stream_getc(in)) == EOF) { return -1; } tmpval = (tmpval << 8) | c; } tmpval &= (((uint_fast64_t) 1) << wordsize) - 1; if (val) { *val = tmpval; } return 0;}static int pnm_getbitstr(jas_stream_t *in, int *val){ int c; int_fast32_t v; for (;;) { if ((c = pnm_getc(in)) == EOF) { return -1; } if (c == '#') { for (;;) { if ((c = pnm_getc(in)) == EOF) { return -1; } if (c == '\n') { break; } } } else if (c == '0' || c == '1') { v = c - '0'; break; } } if (val) { *val = v; } return 0;}static int pnm_getuintstr(jas_stream_t *in, uint_fast32_t *val){ uint_fast32_t v; int c; /* Discard any leading whitespace. */ do { if ((c = pnm_getc(in)) == EOF) { return -1; } } while (isspace(c)); /* Parse the number. */ v = 0; while (isdigit(c)) { v = 10 * v + c - '0'; if ((c = pnm_getc(in)) < 0) { return -1; } } /* The number must be followed by whitespace. */ if (!isspace(c)) { return -1; } if (val) { *val = v; } return 0;}static int pnm_getsintstr(jas_stream_t *in, int_fast32_t *val){ int c; int s; int_fast32_t v; /* Discard any leading whitespace. */ do { if ((c = pnm_getc(in)) == EOF) { return -1; } } while (isspace(c)); /* Get the number, allowing for a negative sign. */ s = 1; if (c == '-') { s = -1; if ((c = pnm_getc(in)) == EOF) { return -1; } } else if (c == '+') { if ((c = pnm_getc(in)) == EOF) { return -1; } } v = 0; while (isdigit(c)) { v = 10 * v + c - '0'; if ((c = pnm_getc(in)) < 0) { return -1; } } /* The number must be followed by whitespace. */ if (!isspace(c)) { return -1; } if (val) { *val = (s >= 0) ? v : (-v); } return 0;}static int pnm_getc(jas_stream_t *in){ int c; for (;;) { if ((c = jas_stream_getc(in)) == EOF) { return -1; } if (c != '#') { return c; } do { if ((c = jas_stream_getc(in)) == EOF) { return -1; } } while (c != '\n' && c != '\r'); }}static int pnm_getint16(jas_stream_t *in, int *val){ int v; int c; if ((c = jas_stream_getc(in)) == EOF) { return -1; } v = c & 0xff; if ((c = jas_stream_getc(in)) == EOF) { return -1; } v = (v << 8) | (c & 0xff); *val = v; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -