📄 jpc_dec.c
字号:
}
dec->image = 0;
dec->xstart = 0;
dec->ystart = 0;
dec->xend = 0;
dec->yend = 0;
dec->tilewidth = 0;
dec->tileheight = 0;
dec->tilexoff = 0;
dec->tileyoff = 0;
dec->numhtiles = 0;
dec->numvtiles = 0;
dec->numtiles = 0;
dec->tiles = 0;
dec->curtile = 0;
dec->numcomps = 0;
dec->in = in;
dec->cp = 0;
dec->maxlyrs = impopts->maxlyrs;
dec->maxpkts = impopts->maxpkts;
dec->numpkts = 0;
dec->ppmseqno = 0;
dec->state = 0;
dec->cmpts = 0;
dec->pkthdrstreams = 0;
dec->ppmstab = 0;
dec->curtileendoff = 0;
return dec;
}
static void jpc_dec_destroy(jpc_dec_t *dec)
{
if (dec->cstate) {
jpc_cstate_destroy(dec->cstate);
}
if (dec->pkthdrstreams) {
jpc_streamlist_destroy(dec->pkthdrstreams);
}
if (dec->image) {
jas_image_destroy(dec->image);
}
if (dec->cp) {
jpc_dec_cp_destroy(dec->cp);
}
if (dec->cmpts) {
jas_free(dec->cmpts);
}
if (dec->tiles) {
jas_free(dec->tiles);
}
jas_free(dec);
}
/******************************************************************************\
*
\******************************************************************************/
void jpc_seglist_insert(jpc_dec_seglist_t *list, jpc_dec_seg_t *ins, jpc_dec_seg_t *node)
{
jpc_dec_seg_t *prev;
jpc_dec_seg_t *next;
prev = ins;
node->prev = prev;
next = prev ? (prev->next) : 0;
node->prev = prev;
node->next = next;
if (prev) {
prev->next = node;
} else {
list->head = node;
}
if (next) {
next->prev = node;
} else {
list->tail = node;
}
}
void jpc_seglist_remove(jpc_dec_seglist_t *list, jpc_dec_seg_t *seg)
{
jpc_dec_seg_t *prev;
jpc_dec_seg_t *next;
prev = seg->prev;
next = seg->next;
if (prev) {
prev->next = next;
} else {
list->head = next;
}
if (next) {
next->prev = prev;
} else {
list->tail = prev;
}
seg->prev = 0;
seg->next = 0;
}
jpc_dec_seg_t *jpc_seg_alloc()
{
jpc_dec_seg_t *seg;
if (!(seg = jas_malloc(sizeof(jpc_dec_seg_t)))) {
return 0;
}
seg->prev = 0;
seg->next = 0;
seg->passno = -1;
seg->numpasses = 0;
seg->maxpasses = 0;
seg->type = JPC_SEG_INVALID;
seg->stream = 0;
seg->cnt = 0;
seg->complete = 0;
seg->lyrno = -1;
return seg;
}
void jpc_seg_destroy(jpc_dec_seg_t *seg)
{
if (seg->stream) {
jas_stream_close(seg->stream);
}
jas_free(seg);
}
static int jpc_dec_dump(jpc_dec_t *dec, FILE *out)
{
jpc_dec_tile_t *tile;
int tileno;
jpc_dec_tcomp_t *tcomp;
int compno;
jpc_dec_rlvl_t *rlvl;
int rlvlno;
jpc_dec_band_t *band;
int bandno;
jpc_dec_prc_t *prc;
int prcno;
jpc_dec_cblk_t *cblk;
int cblkno;
for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles;
++tileno, ++tile) {
for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps;
++compno, ++tcomp) {
for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno <
tcomp->numrlvls; ++rlvlno, ++rlvl) {
fprintf(out, "RESOLUTION LEVEL %d\n", rlvlno);
fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n",
rlvl->xstart, rlvl->ystart, rlvl->xend, rlvl->yend, rlvl->xend -
rlvl->xstart, rlvl->yend - rlvl->ystart);
for (bandno = 0, band = rlvl->bands;
bandno < rlvl->numbands; ++bandno, ++band) {
fprintf(out, "BAND %d\n", bandno);
fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n",
jas_seq2d_xstart(band->data), jas_seq2d_ystart(band->data), jas_seq2d_xend(band->data),
jas_seq2d_yend(band->data), jas_seq2d_xend(band->data) - jas_seq2d_xstart(band->data),
jas_seq2d_yend(band->data) - jas_seq2d_ystart(band->data));
for (prcno = 0, prc = band->prcs;
prcno < rlvl->numprcs; ++prcno,
++prc) {
fprintf(out, "CODE BLOCK GROUP %d\n", prcno);
fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n",
prc->xstart, prc->ystart, prc->xend, prc->yend, prc->xend -
prc->xstart, prc->yend - prc->ystart);
for (cblkno = 0, cblk =
prc->cblks; cblkno <
prc->numcblks; ++cblkno,
++cblk) {
fprintf(out, "CODE BLOCK %d\n", cblkno);
fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n",
jas_seq2d_xstart(cblk->data), jas_seq2d_ystart(cblk->data), jas_seq2d_xend(cblk->data),
jas_seq2d_yend(cblk->data), jas_seq2d_xend(cblk->data) - jas_seq2d_xstart(cblk->data),
jas_seq2d_yend(cblk->data) - jas_seq2d_ystart(cblk->data));
}
}
}
}
}
}
return 0;
}
jpc_streamlist_t *jpc_streamlist_create()
{
jpc_streamlist_t *streamlist;
int i;
if (!(streamlist = jas_malloc(sizeof(jpc_streamlist_t)))) {
return 0;
}
streamlist->numstreams = 0;
streamlist->maxstreams = 100;
if (!(streamlist->streams = jas_malloc(streamlist->maxstreams *
sizeof(jas_stream_t *)))) {
jas_free(streamlist);
return 0;
}
for (i = 0; i < streamlist->maxstreams; ++i) {
streamlist->streams[i] = 0;
}
return streamlist;
}
int jpc_streamlist_insert(jpc_streamlist_t *streamlist, int streamno,
jas_stream_t *stream)
{
jas_stream_t **newstreams;
int newmaxstreams;
int i;
/* Grow the array of streams if necessary. */
if (streamlist->numstreams >= streamlist->maxstreams) {
newmaxstreams = streamlist->maxstreams + 1024;
if (!(newstreams = jas_realloc(streamlist->streams,
(newmaxstreams + 1024) * sizeof(jas_stream_t *)))) {
return -1;
}
for (i = streamlist->numstreams; i < streamlist->maxstreams; ++i) {
streamlist->streams[i] = 0;
}
streamlist->maxstreams = newmaxstreams;
streamlist->streams = newstreams;
}
if (streamno != streamlist->numstreams) {
/* Can only handle insertion at start of list. */
return -1;
}
streamlist->streams[streamno] = stream;
++streamlist->numstreams;
return 0;
}
jas_stream_t *jpc_streamlist_remove(jpc_streamlist_t *streamlist, int streamno)
{
jas_stream_t *stream;
int i;
if (streamno >= streamlist->numstreams) {
abort();
}
stream = streamlist->streams[streamno];
for (i = streamno + 1; i < streamlist->numstreams; ++i) {
streamlist->streams[i - 1] = streamlist->streams[i];
}
--streamlist->numstreams;
return stream;
}
void jpc_streamlist_destroy(jpc_streamlist_t *streamlist)
{
int streamno;
if (streamlist->streams) {
for (streamno = 0; streamno < streamlist->numstreams;
++streamno) {
jas_stream_close(streamlist->streams[streamno]);
}
jas_free(streamlist->streams);
}
jas_free(streamlist);
}
jas_stream_t *jpc_streamlist_get(jpc_streamlist_t *streamlist, int streamno)
{
assert(streamno < streamlist->numstreams);
return streamlist->streams[streamno];
}
int jpc_streamlist_numstreams(jpc_streamlist_t *streamlist)
{
return streamlist->numstreams;
}
jpc_ppxstab_t *jpc_ppxstab_create()
{
jpc_ppxstab_t *tab;
if (!(tab = jas_malloc(sizeof(jpc_ppxstab_t)))) {
return 0;
}
tab->numents = 0;
tab->maxents = 0;
tab->ents = 0;
return tab;
}
void jpc_ppxstab_destroy(jpc_ppxstab_t *tab)
{
int i;
for (i = 0; i < tab->numents; ++i) {
jpc_ppxstabent_destroy(tab->ents[i]);
}
if (tab->ents) {
jas_free(tab->ents);
}
jas_free(tab);
}
int jpc_ppxstab_grow(jpc_ppxstab_t *tab, int maxents)
{
jpc_ppxstabent_t **newents;
if (tab->maxents < maxents) {
newents = (tab->ents) ? jas_realloc(tab->ents, maxents *
sizeof(jpc_ppxstabent_t *)) : jas_malloc(maxents * sizeof(jpc_ppxstabent_t *));
if (!newents) {
return -1;
}
tab->ents = newents;
tab->maxents = maxents;
}
return 0;
}
int jpc_ppxstab_insert(jpc_ppxstab_t *tab, jpc_ppxstabent_t *ent)
{
int inspt;
int i;
for (i = 0; i < tab->numents; ++i) {
if (tab->ents[i]->ind > ent->ind) {
break;
}
}
inspt = i;
if (tab->numents >= tab->maxents) {
if (jpc_ppxstab_grow(tab, tab->maxents + 128)) {
return -1;
}
}
for (i = tab->numents; i > inspt; --i) {
tab->ents[i] = tab->ents[i - 1];
}
tab->ents[i] = ent;
++tab->numents;
return 0;
}
jpc_streamlist_t *jpc_ppmstabtostreams(jpc_ppxstab_t *tab)
{
jpc_streamlist_t *streams;
uchar *dataptr;
uint_fast32_t datacnt;
uint_fast32_t tpcnt;
jpc_ppxstabent_t *ent;
int entno;
jas_stream_t *stream;
int n;
if (!(streams = jpc_streamlist_create())) {
goto error;
}
if (!tab->numents) {
return streams;
}
entno = 0;
ent = tab->ents[entno];
dataptr = ent->data;
datacnt = ent->len;
for (;;) {
/* Get the length of the packet header data for the current
tile-part. */
if (datacnt < 4) {
goto error;
}
if (!(stream = jas_stream_memopen(0, 0))) {
goto error;
}
if (jpc_streamlist_insert(streams, jpc_streamlist_numstreams(streams),
stream)) {
goto error;
}
tpcnt = (dataptr[0] << 24) | (dataptr[1] << 16) | (dataptr[2] << 8)
| dataptr[3];
datacnt -= 4;
dataptr += 4;
/* Get the packet header data for the current tile-part. */
while (tpcnt) {
if (!datacnt) {
if (++entno >= tab->numents) {
goto error;
}
ent = tab->ents[entno];
dataptr = ent->data;
datacnt = ent->len;
}
n = JAS_MIN(tpcnt, datacnt);
if (jas_stream_write(stream, dataptr, n) != n) {
goto error;
}
tpcnt -= n;
dataptr += n;
datacnt -= n;
}
jas_stream_rewind(stream);
if (!datacnt) {
if (++entno >= tab->numents) {
break;
}
ent = tab->ents[entno];
dataptr = ent->data;
datacnt = ent->len;
}
}
return streams;
error:
jpc_streamlist_destroy(streams);
return 0;
}
int jpc_pptstabwrite(jas_stream_t *out, jpc_ppxstab_t *tab)
{
int i;
jpc_ppxstabent_t *ent;
for (i = 0; i < tab->numents; ++i) {
ent = tab->ents[i];
if (jas_stream_write(out, ent->data, ent->len) != JAS_CAST(int, ent->len)) {
return -1;
}
}
return 0;
}
jpc_ppxstabent_t *jpc_ppxstabent_create()
{
jpc_ppxstabent_t *ent;
if (!(ent = jas_malloc(sizeof(jpc_ppxstabent_t)))) {
return 0;
}
ent->data = 0;
ent->len = 0;
ent->ind = 0;
return ent;
}
void jpc_ppxstabent_destroy(jpc_ppxstabent_t *ent)
{
if (ent->data) {
jas_free(ent->data);
}
jas_free(ent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -