/* Apply the BYTEIN algorithm. */ static void jpc_mqdec_bytein(jpc_mqdec_t *mqdec) { int c; unsigned char prevbuf; if (!mqdec->eof) { if ((c = jas_stream_getc(mqdec->in)) == EOF) { mqdec->eof = 1; c = 0xff; } prevbuf = mqdec->inbuffer; mqdec->inbuffer = c; if (prevbuf == 0xff) { if (c > 0x8f) { mqdec->creg += 0xff00; mqdec->ctreg = 8; } else { mqdec->creg += c << 9; mqdec->ctreg = 7; } } else { mqdec->creg += c << 8; mqdec->ctreg = 8; } } else { mqdec->creg += 0xff00; mqdec->ctreg = 8; } }
int iJP2ReadLine(jas_image_t *image, int row, int cmpno, T *data) { jas_image_cmpt_t *cmpt = image->cmpts_[cmpno]; if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * row) * cmpt->cps_, SEEK_SET) < 0) return 0; // this offset will convert from signed to unsigned int offset = 0; if (cmpt->sgnd_ && cmpt->prec_ > 1) offset = 1 << (cmpt->prec_-1); for (int j = 0; j < cmpt->width_; j++) { jas_seqent_t v = 0; for (int k = 0; k < cmpt->cps_; k++) { int c = jas_stream_getc(cmpt->stream_); if (c == EOF) return 0; v = (v << 8) | (c & 0xff); } v = iJP2Bits2Int(v, cmpt->prec_, cmpt->sgnd_); *data++ = (T)(v + offset); } return 1; }
int jas_stream_gobble(jas_stream_t *stream, int n) { int m; m = n; for (m = n; m > 0; --m) { if (jas_stream_getc(stream) == EOF) { return n - m; } } return n; }
static int jpg_copystreamtofile(FILE *out, jas_stream_t *in) { int c; while ((c = jas_stream_getc(in)) != EOF) { if (fputc(c, out) == EOF) { return -1; } } if (jas_stream_error(in)) { return -1; } return 0; }
int jas_stream_read(jas_stream_t *stream, void *buf, int cnt) { int n; int c; char *bufptr; bufptr = buf; n = 0; while (n < cnt) { if ((c = jas_stream_getc(stream)) == EOF) { return n; } *bufptr++ = c; ++n; } return n; }
void jpc_mqdec_init(jpc_mqdec_t *mqdec) { int c; mqdec->eof = 0; mqdec->creg = 0; /* Get the next byte from the input stream. */ if ((c = jas_stream_getc(mqdec->in)) == EOF) { /* We have encountered an I/O error or EOF. */ c = 0xff; mqdec->eof = 1; } mqdec->inbuffer = c; mqdec->creg += mqdec->inbuffer << 16; jpc_mqdec_bytein(mqdec); mqdec->creg <<= 7; mqdec->ctreg -= 7; mqdec->areg = 0x8000; }
char *jas_stream_gets(jas_stream_t *stream, char *buf, int bufsize) { int c; char *bufptr; assert(bufsize > 0); bufptr = buf; while (bufsize > 1) { if ((c = jas_stream_getc(stream)) == EOF) { break; } *bufptr++ = c; --bufsize; if (c == '\n') { break; } } *bufptr = '\0'; return buf; }
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) {