123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650 |
- /*
- * Copyright (c) 1999-2000 Image Power, Inc. and the University of
- * British Columbia.
- * Copyright (c) 2001-2002 Michael David Adams.
- * All rights reserved.
- */
- /* __START_OF_JASPER_LICENSE__
- *
- * JasPer License Version 2.0
- *
- * Copyright (c) 2001-2006 Michael David Adams
- * Copyright (c) 1999-2000 Image Power, Inc.
- * Copyright (c) 1999-2000 The University of British Columbia
- *
- * All rights reserved.
- *
- * Permission is hereby granted, free of charge, to any person (the
- * "User") obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge,
- * publish, distribute, and/or sell copies of the Software, and to permit
- * persons to whom the Software is furnished to do so, subject to the
- * following conditions:
- *
- * 1. The above copyright notices and this permission notice (which
- * includes the disclaimer below) shall be included in all copies or
- * substantial portions of the Software.
- *
- * 2. The name of a copyright holder shall not be used to endorse or
- * promote products derived from the Software without specific prior
- * written permission.
- *
- * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
- * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
- * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
- * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
- * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
- * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
- * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
- * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
- * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
- * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
- * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
- * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
- * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
- * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
- * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
- * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
- * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
- * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
- * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
- * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
- * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
- * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
- * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
- * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
- *
- * __END_OF_JASPER_LICENSE__
- */
- /*
- * JPEG-2000 Code Stream Library
- *
- * $Id: jpc_cs.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
- */
- /******************************************************************************\
- * Includes.
- \******************************************************************************/
- #include <stdlib.h>
- #include <assert.h>
- #include <ctype.h>
- #include "jasper/jas_malloc.h"
- #include "jasper/jas_debug.h"
- #include "jpc_cs.h"
- /******************************************************************************\
- * Types.
- \******************************************************************************/
- /* Marker segment table entry. */
- typedef struct {
- int id;
- char *name;
- jpc_msops_t ops;
- } jpc_mstabent_t;
- /******************************************************************************\
- * Local prototypes.
- \******************************************************************************/
- static jpc_mstabent_t *jpc_mstab_lookup(int id);
- static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static void jpc_poc_destroyparms(jpc_ms_t *ms);
- static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
- static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
- static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out);
- static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out);
- static void jpc_siz_destroyparms(jpc_ms_t *ms);
- static void jpc_qcd_destroyparms(jpc_ms_t *ms);
- static void jpc_qcc_destroyparms(jpc_ms_t *ms);
- static void jpc_cod_destroyparms(jpc_ms_t *ms);
- static void jpc_coc_destroyparms(jpc_ms_t *ms);
- static void jpc_unk_destroyparms(jpc_ms_t *ms);
- static void jpc_ppm_destroyparms(jpc_ms_t *ms);
- static void jpc_ppt_destroyparms(jpc_ms_t *ms);
- static void jpc_crg_destroyparms(jpc_ms_t *ms);
- static void jpc_com_destroyparms(jpc_ms_t *ms);
- static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms);
- static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
- jas_stream_t *in, uint_fast16_t len);
- static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
- jas_stream_t *out);
- static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms);
- static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
- jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms);
- static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
- jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms);
- /******************************************************************************\
- * Global data.
- \******************************************************************************/
- static jpc_mstabent_t jpc_mstab[] = {
- {JPC_MS_SOC, "SOC", {0, 0, 0, 0}},
- {JPC_MS_SOT, "SOT", {0, jpc_sot_getparms, jpc_sot_putparms,
- jpc_sot_dumpparms}},
- {JPC_MS_SOD, "SOD", {0, 0, 0, 0}},
- {JPC_MS_EOC, "EOC", {0, 0, 0, 0}},
- {JPC_MS_SIZ, "SIZ", {jpc_siz_destroyparms, jpc_siz_getparms,
- jpc_siz_putparms, jpc_siz_dumpparms}},
- {JPC_MS_COD, "COD", {jpc_cod_destroyparms, jpc_cod_getparms,
- jpc_cod_putparms, jpc_cod_dumpparms}},
- {JPC_MS_COC, "COC", {jpc_coc_destroyparms, jpc_coc_getparms,
- jpc_coc_putparms, jpc_coc_dumpparms}},
- {JPC_MS_RGN, "RGN", {0, jpc_rgn_getparms, jpc_rgn_putparms,
- jpc_rgn_dumpparms}},
- {JPC_MS_QCD, "QCD", {jpc_qcd_destroyparms, jpc_qcd_getparms,
- jpc_qcd_putparms, jpc_qcd_dumpparms}},
- {JPC_MS_QCC, "QCC", {jpc_qcc_destroyparms, jpc_qcc_getparms,
- jpc_qcc_putparms, jpc_qcc_dumpparms}},
- {JPC_MS_POC, "POC", {jpc_poc_destroyparms, jpc_poc_getparms,
- jpc_poc_putparms, jpc_poc_dumpparms}},
- {JPC_MS_TLM, "TLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}},
- {JPC_MS_PLM, "PLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}},
- {JPC_MS_PPM, "PPM", {jpc_ppm_destroyparms, jpc_ppm_getparms,
- jpc_ppm_putparms, jpc_ppm_dumpparms}},
- {JPC_MS_PPT, "PPT", {jpc_ppt_destroyparms, jpc_ppt_getparms,
- jpc_ppt_putparms, jpc_ppt_dumpparms}},
- {JPC_MS_SOP, "SOP", {0, jpc_sop_getparms, jpc_sop_putparms,
- jpc_sop_dumpparms}},
- {JPC_MS_EPH, "EPH", {0, 0, 0, 0}},
- {JPC_MS_CRG, "CRG", {0, jpc_crg_getparms, jpc_crg_putparms,
- jpc_crg_dumpparms}},
- {JPC_MS_COM, "COM", {jpc_com_destroyparms, jpc_com_getparms,
- jpc_com_putparms, jpc_com_dumpparms}},
- {-1, "UNKNOWN", {jpc_unk_destroyparms, jpc_unk_getparms,
- jpc_unk_putparms, jpc_unk_dumpparms}}
- };
- /******************************************************************************\
- * Code stream manipulation functions.
- \******************************************************************************/
- /* Create a code stream state object. */
- jpc_cstate_t *jpc_cstate_create()
- {
- jpc_cstate_t *cstate;
- if (!(cstate = jas_malloc(sizeof(jpc_cstate_t)))) {
- return 0;
- }
- cstate->numcomps = 0;
- return cstate;
- }
- /* Destroy a code stream state object. */
- void jpc_cstate_destroy(jpc_cstate_t *cstate)
- {
- jas_free(cstate);
- }
- /* Read a marker segment from a stream. */
- jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate)
- {
- jpc_ms_t *ms;
- jpc_mstabent_t *mstabent;
- jas_stream_t *tmpstream;
- if (!(ms = jpc_ms_create(0))) {
- return 0;
- }
- /* Get the marker type. */
- if (jpc_getuint16(in, &ms->id) || ms->id < JPC_MS_MIN /*|| ms->id > JPC_MS_MAX*/) {
- jpc_ms_destroy(ms);
- return 0;
- }
- mstabent = jpc_mstab_lookup(ms->id);
- ms->ops = &mstabent->ops;
- /* Get the marker segment length and parameters if present. */
- /* Note: It is tacitly assumed that a marker segment cannot have
- parameters unless it has a length field. That is, there cannot
- be a parameters field without a length field and vice versa. */
- if (JPC_MS_HASPARMS(ms->id)) {
- /* Get the length of the marker segment. */
- if (jpc_getuint16(in, &ms->len) || ms->len < 3) {
- jpc_ms_destroy(ms);
- return 0;
- }
- /* Calculate the length of the marker segment parameters. */
- ms->len -= 2;
- /* Create and prepare a temporary memory stream from which to
- read the marker segment parameters. */
- /* Note: This approach provides a simple way of ensuring that
- we never read beyond the end of the marker segment (even if
- the marker segment length is errantly set too small). */
- if (!(tmpstream = jas_stream_memopen(0, 0))) {
- jpc_ms_destroy(ms);
- return 0;
- }
- if (jas_stream_copy(tmpstream, in, ms->len) ||
- jas_stream_seek(tmpstream, 0, SEEK_SET) < 0) {
- jas_stream_close(tmpstream);
- jpc_ms_destroy(ms);
- return 0;
- }
- /* Get the marker segment parameters. */
- if ((*ms->ops->getparms)(ms, cstate, tmpstream)) {
- ms->ops = 0;
- jpc_ms_destroy(ms);
- jas_stream_close(tmpstream);
- return 0;
- }
- if (jas_getdbglevel() > 0) {
- jpc_ms_dump(ms, stderr);
- }
- if (JAS_CAST(ulong, jas_stream_tell(tmpstream)) != ms->len) {
- jas_eprintf("warning: trailing garbage in marker segment (%ld bytes)\n",
- ms->len - jas_stream_tell(tmpstream));
- }
- /* Close the temporary stream. */
- jas_stream_close(tmpstream);
- } else {
- /* There are no marker segment parameters. */
- ms->len = 0;
- if (jas_getdbglevel() > 0) {
- jpc_ms_dump(ms, stderr);
- }
- }
- /* Update the code stream state information based on the type of
- marker segment read. */
- /* Note: This is a bit of a hack, but I'm not going to define another
- type of virtual function for this one special case. */
- if (ms->id == JPC_MS_SIZ) {
- cstate->numcomps = ms->parms.siz.numcomps;
- }
- return ms;
- }
- /* Write a marker segment to a stream. */
- int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms)
- {
- jas_stream_t *tmpstream;
- int len;
- /* Output the marker segment type. */
- if (jpc_putuint16(out, ms->id)) {
- return -1;
- }
- /* Output the marker segment length and parameters if necessary. */
- if (ms->ops->putparms) {
- /* Create a temporary stream in which to buffer the
- parameter data. */
- if (!(tmpstream = jas_stream_memopen(0, 0))) {
- return -1;
- }
- if ((*ms->ops->putparms)(ms, cstate, tmpstream)) {
- jas_stream_close(tmpstream);
- return -1;
- }
- /* Get the number of bytes of parameter data written. */
- if ((len = jas_stream_tell(tmpstream)) < 0) {
- jas_stream_close(tmpstream);
- return -1;
- }
- ms->len = len;
- /* Write the marker segment length and parameter data to
- the output stream. */
- if (jas_stream_seek(tmpstream, 0, SEEK_SET) < 0 ||
- jpc_putuint16(out, ms->len + 2) ||
- jas_stream_copy(out, tmpstream, ms->len) < 0) {
- jas_stream_close(tmpstream);
- return -1;
- }
- /* Close the temporary stream. */
- jas_stream_close(tmpstream);
- }
- /* This is a bit of a hack, but I'm not going to define another
- type of virtual function for this one special case. */
- if (ms->id == JPC_MS_SIZ) {
- cstate->numcomps = ms->parms.siz.numcomps;
- }
- if (jas_getdbglevel() > 0) {
- jpc_ms_dump(ms, stderr);
- }
- return 0;
- }
- /******************************************************************************\
- * Marker segment operations.
- \******************************************************************************/
- /* Create a marker segment of the specified type. */
- jpc_ms_t *jpc_ms_create(int type)
- {
- jpc_ms_t *ms;
- jpc_mstabent_t *mstabent;
- if (!(ms = jas_malloc(sizeof(jpc_ms_t)))) {
- return 0;
- }
- ms->id = type;
- ms->len = 0;
- mstabent = jpc_mstab_lookup(ms->id);
- ms->ops = &mstabent->ops;
- memset(&ms->parms, 0, sizeof(jpc_msparms_t));
- return ms;
- }
- /* Destroy a marker segment. */
- void jpc_ms_destroy(jpc_ms_t *ms)
- {
- if (ms->ops && ms->ops->destroyparms) {
- (*ms->ops->destroyparms)(ms);
- }
- jas_free(ms);
- }
- /* Dump a marker segment to a stream for debugging. */
- void jpc_ms_dump(jpc_ms_t *ms, FILE *out)
- {
- jpc_mstabent_t *mstabent;
- mstabent = jpc_mstab_lookup(ms->id);
- fprintf(out, "type = 0x%04x (%s);", (unsigned)ms->id, mstabent->name);
- if (JPC_MS_HASPARMS(ms->id)) {
- fprintf(out, " len = %d;", (int)(ms->len + 2));
- if (ms->ops->dumpparms) {
- (*ms->ops->dumpparms)(ms, out);
- } else {
- fprintf(out, "\n");
- }
- } else {
- fprintf(out, "\n");
- }
- }
- /******************************************************************************\
- * SOT marker segment operations.
- \******************************************************************************/
- static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_sot_t *sot = &ms->parms.sot;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- if (jpc_getuint16(in, &sot->tileno) ||
- jpc_getuint32(in, &sot->len) ||
- jpc_getuint8(in, &sot->partno) ||
- jpc_getuint8(in, &sot->numparts)) {
- return -1;
- }
- if (jas_stream_eof(in)) {
- return -1;
- }
- return 0;
- }
- static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_sot_t *sot = &ms->parms.sot;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- if (jpc_putuint16(out, sot->tileno) ||
- jpc_putuint32(out, sot->len) ||
- jpc_putuint8(out, sot->partno) ||
- jpc_putuint8(out, sot->numparts)) {
- return -1;
- }
- return 0;
- }
- static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_sot_t *sot = &ms->parms.sot;
- fprintf(out, "tileno = %d; len = %d; partno = %d; numparts = %d\n",
- (int)sot->tileno, (int)sot->len, sot->partno, sot->numparts);
- return 0;
- }
- /******************************************************************************\
- * SIZ marker segment operations.
- \******************************************************************************/
- static void jpc_siz_destroyparms(jpc_ms_t *ms)
- {
- jpc_siz_t *siz = &ms->parms.siz;
- if (siz->comps) {
- jas_free(siz->comps);
- }
- }
- static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
- jas_stream_t *in)
- {
- jpc_siz_t *siz = &ms->parms.siz;
- unsigned int i;
- uint_fast8_t tmp;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- if (jpc_getuint16(in, &siz->caps) ||
- jpc_getuint32(in, &siz->width) ||
- jpc_getuint32(in, &siz->height) ||
- jpc_getuint32(in, &siz->xoff) ||
- jpc_getuint32(in, &siz->yoff) ||
- jpc_getuint32(in, &siz->tilewidth) ||
- jpc_getuint32(in, &siz->tileheight) ||
- jpc_getuint32(in, &siz->tilexoff) ||
- jpc_getuint32(in, &siz->tileyoff) ||
- jpc_getuint16(in, &siz->numcomps)) {
- return -1;
- }
- if (!siz->width || !siz->height || !siz->tilewidth ||
- !siz->tileheight || !siz->numcomps) {
- return -1;
- }
- if (!(siz->comps = jas_alloc2(siz->numcomps, sizeof(jpc_sizcomp_t)))) {
- return -1;
- }
- for (i = 0; i < siz->numcomps; ++i) {
- if (jpc_getuint8(in, &tmp) ||
- jpc_getuint8(in, &siz->comps[i].hsamp) ||
- jpc_getuint8(in, &siz->comps[i].vsamp)) {
- jas_free(siz->comps);
- return -1;
- }
- siz->comps[i].sgnd = (tmp >> 7) & 1;
- siz->comps[i].prec = (tmp & 0x7f) + 1;
- }
- if (jas_stream_eof(in)) {
- jas_free(siz->comps);
- return -1;
- }
- return 0;
- }
- static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_siz_t *siz = &ms->parms.siz;
- unsigned int i;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- assert(siz->width && siz->height && siz->tilewidth &&
- siz->tileheight && siz->numcomps);
- if (jpc_putuint16(out, siz->caps) ||
- jpc_putuint32(out, siz->width) ||
- jpc_putuint32(out, siz->height) ||
- jpc_putuint32(out, siz->xoff) ||
- jpc_putuint32(out, siz->yoff) ||
- jpc_putuint32(out, siz->tilewidth) ||
- jpc_putuint32(out, siz->tileheight) ||
- jpc_putuint32(out, siz->tilexoff) ||
- jpc_putuint32(out, siz->tileyoff) ||
- jpc_putuint16(out, siz->numcomps)) {
- return -1;
- }
- for (i = 0; i < siz->numcomps; ++i) {
- if (jpc_putuint8(out, ((siz->comps[i].sgnd & 1) << 7) |
- ((siz->comps[i].prec - 1) & 0x7f)) ||
- jpc_putuint8(out, siz->comps[i].hsamp) ||
- jpc_putuint8(out, siz->comps[i].vsamp)) {
- return -1;
- }
- }
- return 0;
- }
- static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_siz_t *siz = &ms->parms.siz;
- unsigned int i;
- fprintf(out, "caps = 0x%02x;\n", (unsigned)siz->caps);
- fprintf(out, "width = %d; height = %d; xoff = %d; yoff = %d;\n",
- (int)siz->width, (int)siz->height, (int)siz->xoff, (int)siz->yoff);
- fprintf(out, "tilewidth = %d; tileheight = %d; tilexoff = %d; "
- "tileyoff = %d;\n", (int)siz->tilewidth, (int)siz->tileheight, (int)siz->tilexoff,
- (int)siz->tileyoff);
- for (i = 0; i < siz->numcomps; ++i) {
- fprintf(out, "prec[%d] = %d; sgnd[%d] = %d; hsamp[%d] = %d; "
- "vsamp[%d] = %d\n", i, siz->comps[i].prec, i,
- siz->comps[i].sgnd, i, siz->comps[i].hsamp, i,
- siz->comps[i].vsamp);
- }
- return 0;
- }
- /******************************************************************************\
- * COD marker segment operations.
- \******************************************************************************/
- static void jpc_cod_destroyparms(jpc_ms_t *ms)
- {
- jpc_cod_t *cod = &ms->parms.cod;
- jpc_cox_destroycompparms(&cod->compparms);
- }
- static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_cod_t *cod = &ms->parms.cod;
- if (jpc_getuint8(in, &cod->csty)) {
- return -1;
- }
- if (jpc_getuint8(in, &cod->prg) ||
- jpc_getuint16(in, &cod->numlyrs) ||
- jpc_getuint8(in, &cod->mctrans)) {
- return -1;
- }
- if (jpc_cox_getcompparms(ms, cstate, in,
- (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) {
- return -1;
- }
- if (jas_stream_eof(in)) {
- jpc_cod_destroyparms(ms);
- return -1;
- }
- return 0;
- }
- static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_cod_t *cod = &ms->parms.cod;
- assert(cod->numlyrs > 0 && cod->compparms.numdlvls <= 32);
- assert(cod->compparms.numdlvls == cod->compparms.numrlvls - 1);
- if (jpc_putuint8(out, cod->compparms.csty) ||
- jpc_putuint8(out, cod->prg) ||
- jpc_putuint16(out, cod->numlyrs) ||
- jpc_putuint8(out, cod->mctrans)) {
- return -1;
- }
- if (jpc_cox_putcompparms(ms, cstate, out,
- (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) {
- return -1;
- }
- return 0;
- }
- static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_cod_t *cod = &ms->parms.cod;
- int i;
- fprintf(out, "csty = 0x%02x;\n", cod->compparms.csty);
- fprintf(out, "numdlvls = %d; qmfbid = %d; mctrans = %d\n",
- cod->compparms.numdlvls, cod->compparms.qmfbid, cod->mctrans);
- fprintf(out, "prg = %d; numlyrs = %d;\n",
- cod->prg, (int)cod->numlyrs);
- fprintf(out, "cblkwidthval = %d; cblkheightval = %d; "
- "cblksty = 0x%02x;\n", cod->compparms.cblkwidthval, cod->compparms.cblkheightval,
- cod->compparms.cblksty);
- if (cod->csty & JPC_COX_PRT) {
- for (i = 0; i < cod->compparms.numrlvls; ++i) {
- jas_eprintf("prcwidth[%d] = %d, prcheight[%d] = %d\n",
- i, cod->compparms.rlvls[i].parwidthval,
- i, cod->compparms.rlvls[i].parheightval);
- }
- }
- return 0;
- }
- /******************************************************************************\
- * COC marker segment operations.
- \******************************************************************************/
- static void jpc_coc_destroyparms(jpc_ms_t *ms)
- {
- jpc_coc_t *coc = &ms->parms.coc;
- jpc_cox_destroycompparms(&coc->compparms);
- }
- static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_coc_t *coc = &ms->parms.coc;
- uint_fast8_t tmp;
- if (cstate->numcomps <= 256) {
- if (jpc_getuint8(in, &tmp)) {
- return -1;
- }
- coc->compno = tmp;
- } else {
- if (jpc_getuint16(in, &coc->compno)) {
- return -1;
- }
- }
- if (jpc_getuint8(in, &coc->compparms.csty)) {
- return -1;
- }
- if (jpc_cox_getcompparms(ms, cstate, in,
- (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) {
- return -1;
- }
- if (jas_stream_eof(in)) {
- return -1;
- }
- return 0;
- }
- static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_coc_t *coc = &ms->parms.coc;
- assert(coc->compparms.numdlvls <= 32);
- if (cstate->numcomps <= 256) {
- if (jpc_putuint8(out, coc->compno)) {
- return -1;
- }
- } else {
- if (jpc_putuint16(out, coc->compno)) {
- return -1;
- }
- }
- if (jpc_putuint8(out, coc->compparms.csty)) {
- return -1;
- }
- if (jpc_cox_putcompparms(ms, cstate, out,
- (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) {
- return -1;
- }
- return 0;
- }
- static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_coc_t *coc = &ms->parms.coc;
- fprintf(out, "compno = %d; csty = 0x%02x; numdlvls = %d;\n",
- (int)coc->compno, coc->compparms.csty, coc->compparms.numdlvls);
- fprintf(out, "cblkwidthval = %d; cblkheightval = %d; "
- "cblksty = 0x%02x; qmfbid = %d;\n", coc->compparms.cblkwidthval,
- coc->compparms.cblkheightval, coc->compparms.cblksty, coc->compparms.qmfbid);
- return 0;
- }
- /******************************************************************************\
- * COD/COC marker segment operation helper functions.
- \******************************************************************************/
- static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms)
- {
- /* Eliminate compiler warning about unused variables. */
- compparms = 0;
- }
- static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
- jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms)
- {
- uint_fast8_t tmp;
- int i;
- /* Eliminate compiler warning about unused variables. */
- ms = 0;
- cstate = 0;
- if (jpc_getuint8(in, &compparms->numdlvls) ||
- jpc_getuint8(in, &compparms->cblkwidthval) ||
- jpc_getuint8(in, &compparms->cblkheightval) ||
- jpc_getuint8(in, &compparms->cblksty) ||
- jpc_getuint8(in, &compparms->qmfbid)) {
- return -1;
- }
- compparms->numrlvls = compparms->numdlvls + 1;
- if (compparms->numrlvls > JPC_MAXRLVLS) {
- jpc_cox_destroycompparms(compparms);
- return -1;
- }
- if (prtflag) {
- for (i = 0; i < compparms->numrlvls; ++i) {
- if (jpc_getuint8(in, &tmp)) {
- jpc_cox_destroycompparms(compparms);
- return -1;
- }
- compparms->rlvls[i].parwidthval = tmp & 0xf;
- compparms->rlvls[i].parheightval = (tmp >> 4) & 0xf;
- }
- /* Sigh. This bit should be in the same field in both COC and COD mrk segs. */
- compparms->csty |= JPC_COX_PRT;
- } else {
- }
- if (jas_stream_eof(in)) {
- jpc_cox_destroycompparms(compparms);
- return -1;
- }
- return 0;
- }
- static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
- jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms)
- {
- int i;
- assert(compparms->numdlvls <= 32);
- /* Eliminate compiler warning about unused variables. */
- ms = 0;
- cstate = 0;
- if (jpc_putuint8(out, compparms->numdlvls) ||
- jpc_putuint8(out, compparms->cblkwidthval) ||
- jpc_putuint8(out, compparms->cblkheightval) ||
- jpc_putuint8(out, compparms->cblksty) ||
- jpc_putuint8(out, compparms->qmfbid)) {
- return -1;
- }
- if (prtflag) {
- for (i = 0; i < compparms->numrlvls; ++i) {
- if (jpc_putuint8(out,
- ((compparms->rlvls[i].parheightval & 0xf) << 4) |
- (compparms->rlvls[i].parwidthval & 0xf))) {
- return -1;
- }
- }
- }
- return 0;
- }
- /******************************************************************************\
- * RGN marker segment operations.
- \******************************************************************************/
- static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_rgn_t *rgn = &ms->parms.rgn;
- uint_fast8_t tmp;
- if (cstate->numcomps <= 256) {
- if (jpc_getuint8(in, &tmp)) {
- return -1;
- }
- rgn->compno = tmp;
- } else {
- if (jpc_getuint16(in, &rgn->compno)) {
- return -1;
- }
- }
- if (jpc_getuint8(in, &rgn->roisty) ||
- jpc_getuint8(in, &rgn->roishift)) {
- return -1;
- }
- return 0;
- }
- static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_rgn_t *rgn = &ms->parms.rgn;
- if (cstate->numcomps <= 256) {
- if (jpc_putuint8(out, rgn->compno)) {
- return -1;
- }
- } else {
- if (jpc_putuint16(out, rgn->compno)) {
- return -1;
- }
- }
- if (jpc_putuint8(out, rgn->roisty) ||
- jpc_putuint8(out, rgn->roishift)) {
- return -1;
- }
- return 0;
- }
- static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_rgn_t *rgn = &ms->parms.rgn;
- fprintf(out, "compno = %d; roisty = %d; roishift = %d\n",
- (int)rgn->compno, rgn->roisty, rgn->roishift);
- return 0;
- }
- /******************************************************************************\
- * QCD marker segment operations.
- \******************************************************************************/
- static void jpc_qcd_destroyparms(jpc_ms_t *ms)
- {
- jpc_qcd_t *qcd = &ms->parms.qcd;
- jpc_qcx_destroycompparms(&qcd->compparms);
- }
- static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms;
- return jpc_qcx_getcompparms(compparms, cstate, in, ms->len);
- }
- static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms;
- return jpc_qcx_putcompparms(compparms, cstate, out);
- }
- static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_qcd_t *qcd = &ms->parms.qcd;
- int i;
- fprintf(out, "qntsty = %d; numguard = %d; numstepsizes = %d\n",
- (int) qcd->compparms.qntsty, qcd->compparms.numguard, qcd->compparms.numstepsizes);
- for (i = 0; i < qcd->compparms.numstepsizes; ++i) {
- fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n",
- i, (unsigned) JPC_QCX_GETEXPN(qcd->compparms.stepsizes[i]),
- i, (unsigned) JPC_QCX_GETMANT(qcd->compparms.stepsizes[i]));
- }
- return 0;
- }
- /******************************************************************************\
- * QCC marker segment operations.
- \******************************************************************************/
- static void jpc_qcc_destroyparms(jpc_ms_t *ms)
- {
- jpc_qcc_t *qcc = &ms->parms.qcc;
- jpc_qcx_destroycompparms(&qcc->compparms);
- }
- static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_qcc_t *qcc = &ms->parms.qcc;
- uint_fast8_t tmp;
- int len;
- len = ms->len;
- if (cstate->numcomps <= 256) {
- jpc_getuint8(in, &tmp);
- qcc->compno = tmp;
- --len;
- } else {
- jpc_getuint16(in, &qcc->compno);
- len -= 2;
- }
- if (jpc_qcx_getcompparms(&qcc->compparms, cstate, in, len)) {
- return -1;
- }
- if (jas_stream_eof(in)) {
- jpc_qcc_destroyparms(ms);
- return -1;
- }
- return 0;
- }
- static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_qcc_t *qcc = &ms->parms.qcc;
- if (cstate->numcomps <= 256) {
- jpc_putuint8(out, qcc->compno);
- } else {
- jpc_putuint16(out, qcc->compno);
- }
- if (jpc_qcx_putcompparms(&qcc->compparms, cstate, out)) {
- return -1;
- }
- return 0;
- }
- static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_qcc_t *qcc = &ms->parms.qcc;
- int i;
- fprintf(out, "compno = %d; qntsty = %d; numguard = %d; "
- "numstepsizes = %d\n", (int)qcc->compno, qcc->compparms.qntsty, qcc->compparms.numguard,
- qcc->compparms.numstepsizes);
- for (i = 0; i < qcc->compparms.numstepsizes; ++i) {
- fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n",
- i, (unsigned) JPC_QCX_GETEXPN(qcc->compparms.stepsizes[i]),
- i, (unsigned) JPC_QCX_GETMANT(qcc->compparms.stepsizes[i]));
- }
- return 0;
- }
- /******************************************************************************\
- * QCD/QCC marker segment helper functions.
- \******************************************************************************/
- static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms)
- {
- if (compparms->stepsizes) {
- jas_free(compparms->stepsizes);
- }
- }
- static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
- jas_stream_t *in, uint_fast16_t len)
- {
- uint_fast8_t tmp;
- int n;
- int i;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- n = 0;
- jpc_getuint8(in, &tmp);
- ++n;
- compparms->qntsty = tmp & 0x1f;
- compparms->numguard = (tmp >> 5) & 7;
- switch (compparms->qntsty) {
- case JPC_QCX_SIQNT:
- compparms->numstepsizes = 1;
- break;
- case JPC_QCX_NOQNT:
- compparms->numstepsizes = (len - n);
- break;
- case JPC_QCX_SEQNT:
- /* XXX - this is a hack */
- compparms->numstepsizes = (len - n) / 2;
- break;
- }
- if (compparms->numstepsizes > 3 * JPC_MAXRLVLS + 1) {
- jpc_qcx_destroycompparms(compparms);
- return -1;
- } else if (compparms->numstepsizes > 0) {
- compparms->stepsizes = jas_malloc(compparms->numstepsizes *
- sizeof(uint_fast16_t));
- assert(compparms->stepsizes);
- for (i = 0; i < compparms->numstepsizes; ++i) {
- if (compparms->qntsty == JPC_QCX_NOQNT) {
- jpc_getuint8(in, &tmp);
- compparms->stepsizes[i] = JPC_QCX_EXPN(tmp >> 3);
- } else {
- jpc_getuint16(in, &compparms->stepsizes[i]);
- }
- }
- } else {
- compparms->stepsizes = 0;
- }
- if (jas_stream_error(in) || jas_stream_eof(in)) {
- jpc_qcx_destroycompparms(compparms);
- return -1;
- }
- return 0;
- }
- static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
- jas_stream_t *out)
- {
- int i;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- jpc_putuint8(out, ((compparms->numguard & 7) << 5) | compparms->qntsty);
- for (i = 0; i < compparms->numstepsizes; ++i) {
- if (compparms->qntsty == JPC_QCX_NOQNT) {
- jpc_putuint8(out, JPC_QCX_GETEXPN(
- compparms->stepsizes[i]) << 3);
- } else {
- jpc_putuint16(out, compparms->stepsizes[i]);
- }
- }
- return 0;
- }
- /******************************************************************************\
- * SOP marker segment operations.
- \******************************************************************************/
- static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_sop_t *sop = &ms->parms.sop;
- /* Eliminate compiler warning about unused variable. */
- cstate = 0;
- if (jpc_getuint16(in, &sop->seqno)) {
- return -1;
- }
- return 0;
- }
- static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_sop_t *sop = &ms->parms.sop;
- /* Eliminate compiler warning about unused variable. */
- cstate = 0;
- if (jpc_putuint16(out, sop->seqno)) {
- return -1;
- }
- return 0;
- }
- static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_sop_t *sop = &ms->parms.sop;
- fprintf(out, "seqno = %d;\n", (int)sop->seqno);
- return 0;
- }
- /******************************************************************************\
- * PPM marker segment operations.
- \******************************************************************************/
- static void jpc_ppm_destroyparms(jpc_ms_t *ms)
- {
- jpc_ppm_t *ppm = &ms->parms.ppm;
- if (ppm->data) {
- jas_free(ppm->data);
- }
- }
- static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_ppm_t *ppm = &ms->parms.ppm;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- ppm->data = 0;
- if (ms->len < 1) {
- goto error;
- }
- if (jpc_getuint8(in, &ppm->ind)) {
- goto error;
- }
- ppm->len = ms->len - 1;
- if (ppm->len > 0) {
- if (!(ppm->data = jas_malloc(ppm->len))) {
- goto error;
- }
- if (JAS_CAST(uint, jas_stream_read(in, ppm->data, ppm->len)) != ppm->len) {
- goto error;
- }
- } else {
- ppm->data = 0;
- }
- return 0;
- error:
- jpc_ppm_destroyparms(ms);
- return -1;
- }
- static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_ppm_t *ppm = &ms->parms.ppm;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- if (JAS_CAST(uint, jas_stream_write(out, (char *) ppm->data, ppm->len)) != ppm->len) {
- return -1;
- }
- return 0;
- }
- static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_ppm_t *ppm = &ms->parms.ppm;
- fprintf(out, "ind=%d; len = %d;\n", ppm->ind, (int)ppm->len);
- if (ppm->len > 0) {
- fprintf(out, "data =\n");
- jas_memdump(out, ppm->data, ppm->len);
- }
- return 0;
- }
- /******************************************************************************\
- * PPT marker segment operations.
- \******************************************************************************/
- static void jpc_ppt_destroyparms(jpc_ms_t *ms)
- {
- jpc_ppt_t *ppt = &ms->parms.ppt;
- if (ppt->data) {
- jas_free(ppt->data);
- }
- }
- static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_ppt_t *ppt = &ms->parms.ppt;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- ppt->data = 0;
- if (ms->len < 1) {
- goto error;
- }
- if (jpc_getuint8(in, &ppt->ind)) {
- goto error;
- }
- ppt->len = ms->len - 1;
- if (ppt->len > 0) {
- if (!(ppt->data = jas_malloc(ppt->len))) {
- goto error;
- }
- if (jas_stream_read(in, (char *) ppt->data, ppt->len) != JAS_CAST(int, ppt->len)) {
- goto error;
- }
- } else {
- ppt->data = 0;
- }
- return 0;
- error:
- jpc_ppt_destroyparms(ms);
- return -1;
- }
- static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_ppt_t *ppt = &ms->parms.ppt;
- /* Eliminate compiler warning about unused variable. */
- cstate = 0;
- if (jpc_putuint8(out, ppt->ind)) {
- return -1;
- }
- if (jas_stream_write(out, (char *) ppt->data, ppt->len) != JAS_CAST(int, ppt->len)) {
- return -1;
- }
- return 0;
- }
- static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_ppt_t *ppt = &ms->parms.ppt;
- fprintf(out, "ind=%d; len = %d;\n", ppt->ind, (int)ppt->len);
- if (ppt->len > 0) {
- fprintf(out, "data =\n");
- jas_memdump(out, ppt->data, ppt->len);
- }
- return 0;
- }
- /******************************************************************************\
- * POC marker segment operations.
- \******************************************************************************/
- static void jpc_poc_destroyparms(jpc_ms_t *ms)
- {
- jpc_poc_t *poc = &ms->parms.poc;
- if (poc->pchgs) {
- jas_free(poc->pchgs);
- }
- }
- static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_poc_t *poc = &ms->parms.poc;
- jpc_pocpchg_t *pchg;
- int pchgno;
- uint_fast8_t tmp;
- poc->numpchgs = (cstate->numcomps > 256) ? (ms->len / 9) :
- (ms->len / 7);
- if (!(poc->pchgs = jas_alloc2(poc->numpchgs, sizeof(jpc_pocpchg_t)))) {
- goto error;
- }
- for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno,
- ++pchg) {
- if (jpc_getuint8(in, &pchg->rlvlnostart)) {
- goto error;
- }
- if (cstate->numcomps > 256) {
- if (jpc_getuint16(in, &pchg->compnostart)) {
- goto error;
- }
- } else {
- if (jpc_getuint8(in, &tmp)) {
- goto error;
- };
- pchg->compnostart = tmp;
- }
- if (jpc_getuint16(in, &pchg->lyrnoend) ||
- jpc_getuint8(in, &pchg->rlvlnoend)) {
- goto error;
- }
- if (cstate->numcomps > 256) {
- if (jpc_getuint16(in, &pchg->compnoend)) {
- goto error;
- }
- } else {
- if (jpc_getuint8(in, &tmp)) {
- goto error;
- }
- pchg->compnoend = tmp;
- }
- if (jpc_getuint8(in, &pchg->prgord)) {
- goto error;
- }
- if (pchg->rlvlnostart > pchg->rlvlnoend ||
- pchg->compnostart > pchg->compnoend) {
- goto error;
- }
- }
- return 0;
- error:
- jpc_poc_destroyparms(ms);
- return -1;
- }
- static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_poc_t *poc = &ms->parms.poc;
- jpc_pocpchg_t *pchg;
- int pchgno;
- for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno,
- ++pchg) {
- if (jpc_putuint8(out, pchg->rlvlnostart) ||
- ((cstate->numcomps > 256) ?
- jpc_putuint16(out, pchg->compnostart) :
- jpc_putuint8(out, pchg->compnostart)) ||
- jpc_putuint16(out, pchg->lyrnoend) ||
- jpc_putuint8(out, pchg->rlvlnoend) ||
- ((cstate->numcomps > 256) ?
- jpc_putuint16(out, pchg->compnoend) :
- jpc_putuint8(out, pchg->compnoend)) ||
- jpc_putuint8(out, pchg->prgord)) {
- return -1;
- }
- }
- return 0;
- }
- static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_poc_t *poc = &ms->parms.poc;
- jpc_pocpchg_t *pchg;
- int pchgno;
- for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs;
- ++pchgno, ++pchg) {
- fprintf(out, "po[%d] = %d; ", pchgno, pchg->prgord);
- fprintf(out, "cs[%d] = %d; ce[%d] = %d; ",
- pchgno, (int)pchg->compnostart, pchgno, (int)pchg->compnoend);
- fprintf(out, "rs[%d] = %d; re[%d] = %d; ",
- pchgno, pchg->rlvlnostart, pchgno, pchg->rlvlnoend);
- fprintf(out, "le[%d] = %d\n", pchgno, (int)pchg->lyrnoend);
- }
- return 0;
- }
- /******************************************************************************\
- * CRG marker segment operations.
- \******************************************************************************/
- static void jpc_crg_destroyparms(jpc_ms_t *ms)
- {
- jpc_crg_t *crg = &ms->parms.crg;
- if (crg->comps) {
- jas_free(crg->comps);
- }
- }
- static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_crg_t *crg = &ms->parms.crg;
- jpc_crgcomp_t *comp;
- uint_fast16_t compno;
- crg->numcomps = cstate->numcomps;
- if (!(crg->comps = jas_alloc2(cstate->numcomps, sizeof(jpc_crgcomp_t)))) {
- return -1;
- }
- for (compno = 0, comp = crg->comps; compno < cstate->numcomps;
- ++compno, ++comp) {
- if (jpc_getuint16(in, &comp->hoff) ||
- jpc_getuint16(in, &comp->voff)) {
- jpc_crg_destroyparms(ms);
- return -1;
- }
- }
- return 0;
- }
- static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_crg_t *crg = &ms->parms.crg;
- int compno;
- jpc_crgcomp_t *comp;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno,
- ++comp) {
- if (jpc_putuint16(out, comp->hoff) ||
- jpc_putuint16(out, comp->voff)) {
- return -1;
- }
- }
- return 0;
- }
- static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_crg_t *crg = &ms->parms.crg;
- int compno;
- jpc_crgcomp_t *comp;
- for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno,
- ++comp) {
- fprintf(out, "hoff[%d] = %d; voff[%d] = %d\n", compno,
- (int)comp->hoff, compno, (int)comp->voff);
- }
- return 0;
- }
- /******************************************************************************\
- * Operations for COM marker segment.
- \******************************************************************************/
- static void jpc_com_destroyparms(jpc_ms_t *ms)
- {
- jpc_com_t *com = &ms->parms.com;
- if (com->data) {
- jas_free(com->data);
- }
- }
- static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_com_t *com = &ms->parms.com;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- if (jpc_getuint16(in, &com->regid)) {
- return -1;
- }
- com->len = ms->len - 2;
- if (com->len > 0) {
- if (!(com->data = jas_malloc(com->len))) {
- return -1;
- }
- if (jas_stream_read(in, com->data, com->len) != JAS_CAST(int, com->len)) {
- return -1;
- }
- } else {
- com->data = 0;
- }
- return 0;
- }
- static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- jpc_com_t *com = &ms->parms.com;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- if (jpc_putuint16(out, com->regid)) {
- return -1;
- }
- if (jas_stream_write(out, com->data, com->len) != JAS_CAST(int, com->len)) {
- return -1;
- }
- return 0;
- }
- static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- jpc_com_t *com = &ms->parms.com;
- unsigned int i;
- int printable;
- fprintf(out, "regid = %d;\n", (int)com->regid);
- printable = 1;
- for (i = 0; i < com->len; ++i) {
- if (!isprint(com->data[i])) {
- printable = 0;
- break;
- }
- }
- if (printable) {
- fprintf(out, "data = ");
- fwrite(com->data, sizeof(char), com->len, out);
- fprintf(out, "\n");
- }
- return 0;
- }
- /******************************************************************************\
- * Operations for unknown types of marker segments.
- \******************************************************************************/
- static void jpc_unk_destroyparms(jpc_ms_t *ms)
- {
- jpc_unk_t *unk = &ms->parms.unk;
- if (unk->data) {
- jas_free(unk->data);
- }
- }
- static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
- {
- jpc_unk_t *unk = &ms->parms.unk;
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- if (ms->len > 0) {
- if (!(unk->data = jas_malloc(ms->len))) {
- return -1;
- }
- if (jas_stream_read(in, (char *) unk->data, ms->len) != JAS_CAST(int, ms->len)) {
- jas_free(unk->data);
- return -1;
- }
- unk->len = ms->len;
- } else {
- unk->data = 0;
- unk->len = 0;
- }
- return 0;
- }
- static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
- {
- /* Eliminate compiler warning about unused variables. */
- cstate = 0;
- ms = 0;
- out = 0;
- /* If this function is called, we are trying to write an unsupported
- type of marker segment. Return with an error indication. */
- return -1;
- }
- static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out)
- {
- unsigned int i;
- jpc_unk_t *unk = &ms->parms.unk;
- for (i = 0; i < unk->len; ++i) {
- fprintf(out, "%02x ", unk->data[i]);
- }
- return 0;
- }
- /******************************************************************************\
- * Primitive I/O operations.
- \******************************************************************************/
- int jpc_getuint8(jas_stream_t *in, uint_fast8_t *val)
- {
- int c;
- if ((c = jas_stream_getc(in)) == EOF) {
- return -1;
- }
- if (val) {
- *val = c;
- }
- return 0;
- }
- int jpc_putuint8(jas_stream_t *out, uint_fast8_t val)
- {
- if (jas_stream_putc(out, val & 0xff) == EOF) {
- return -1;
- }
- return 0;
- }
- int jpc_getuint16(jas_stream_t *in, uint_fast16_t *val)
- {
- uint_fast16_t v;
- int c;
- if ((c = jas_stream_getc(in)) == EOF) {
- return -1;
- }
- v = c;
- if ((c = jas_stream_getc(in)) == EOF) {
- return -1;
- }
- v = (v << 8) | c;
- if (val) {
- *val = v;
- }
- return 0;
- }
- int jpc_putuint16(jas_stream_t *out, uint_fast16_t val)
- {
- if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
- jas_stream_putc(out, val & 0xff) == EOF) {
- return -1;
- }
- return 0;
- }
- int jpc_getuint32(jas_stream_t *in, uint_fast32_t *val)
- {
- uint_fast32_t v;
- int c;
- if ((c = jas_stream_getc(in)) == EOF) {
- return -1;
- }
- v = c;
- if ((c = jas_stream_getc(in)) == EOF) {
- return -1;
- }
- v = (v << 8) | c;
- if ((c = jas_stream_getc(in)) == EOF) {
- return -1;
- }
- v = (v << 8) | c;
- if ((c = jas_stream_getc(in)) == EOF) {
- return -1;
- }
- v = (v << 8) | c;
- if (val) {
- *val = v;
- }
- return 0;
- }
- int jpc_putuint32(jas_stream_t *out, uint_fast32_t val)
- {
- if (jas_stream_putc(out, (val >> 24) & 0xff) == EOF ||
- jas_stream_putc(out, (val >> 16) & 0xff) == EOF ||
- jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
- jas_stream_putc(out, val & 0xff) == EOF) {
- return -1;
- }
- return 0;
- }
- /******************************************************************************\
- * Miscellany
- \******************************************************************************/
- static jpc_mstabent_t *jpc_mstab_lookup(int id)
- {
- jpc_mstabent_t *mstabent;
- for (mstabent = jpc_mstab;; ++mstabent) {
- if (mstabent->id == id || mstabent->id < 0) {
- return mstabent;
- }
- }
- assert(0);
- return 0;
- }
- int jpc_validate(jas_stream_t *in)
- {
- int n;
- int i;
- unsigned char buf[2];
- assert(JAS_STREAM_MAXPUTBACK >= 2);
- if ((n = jas_stream_read(in, (char *) buf, 2)) < 0) {
- return -1;
- }
- for (i = n - 1; i >= 0; --i) {
- if (jas_stream_ungetc(in, buf[i]) == EOF) {
- return -1;
- }
- }
- if (n < 2) {
- return -1;
- }
- if (buf[0] == (JPC_MS_SOC >> 8) && buf[1] == (JPC_MS_SOC & 0xff)) {
- return 0;
- }
- return -1;
- }
- int jpc_getdata(jas_stream_t *in, jas_stream_t *out, long len)
- {
- return jas_stream_copy(out, in, len);
- }
- int jpc_putdata(jas_stream_t *out, jas_stream_t *in, long len)
- {
- return jas_stream_copy(out, in, len);
- }
|