jpc_cs.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2002 Michael David Adams.
  5. * All rights reserved.
  6. */
  7. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * files (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, and/or sell copies of the Software, and to permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * JPEG-2000 Code Stream Library
  64. *
  65. * $Id: jpc_cs.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  66. */
  67. /******************************************************************************\
  68. * Includes.
  69. \******************************************************************************/
  70. #include <stdlib.h>
  71. #include <assert.h>
  72. #include <ctype.h>
  73. #include "jasper/jas_malloc.h"
  74. #include "jasper/jas_debug.h"
  75. #include "jpc_cs.h"
  76. /******************************************************************************\
  77. * Types.
  78. \******************************************************************************/
  79. /* Marker segment table entry. */
  80. typedef struct {
  81. int id;
  82. char *name;
  83. jpc_msops_t ops;
  84. } jpc_mstabent_t;
  85. /******************************************************************************\
  86. * Local prototypes.
  87. \******************************************************************************/
  88. static jpc_mstabent_t *jpc_mstab_lookup(int id);
  89. static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out);
  90. static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  91. static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  92. static void jpc_poc_destroyparms(jpc_ms_t *ms);
  93. static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  94. static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  95. static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  96. static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  97. static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  98. static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  99. static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  100. static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  101. static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  102. static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  103. static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  104. static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  105. static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
  106. static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  107. static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  108. static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  109. static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  110. static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  111. static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  112. static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  113. static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  114. static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  115. static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  116. static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  117. static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  118. static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
  119. static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out);
  120. static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out);
  121. static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out);
  122. static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out);
  123. static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out);
  124. static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out);
  125. static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out);
  126. static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out);
  127. static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out);
  128. static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out);
  129. static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out);
  130. static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out);
  131. static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out);
  132. static void jpc_siz_destroyparms(jpc_ms_t *ms);
  133. static void jpc_qcd_destroyparms(jpc_ms_t *ms);
  134. static void jpc_qcc_destroyparms(jpc_ms_t *ms);
  135. static void jpc_cod_destroyparms(jpc_ms_t *ms);
  136. static void jpc_coc_destroyparms(jpc_ms_t *ms);
  137. static void jpc_unk_destroyparms(jpc_ms_t *ms);
  138. static void jpc_ppm_destroyparms(jpc_ms_t *ms);
  139. static void jpc_ppt_destroyparms(jpc_ms_t *ms);
  140. static void jpc_crg_destroyparms(jpc_ms_t *ms);
  141. static void jpc_com_destroyparms(jpc_ms_t *ms);
  142. static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms);
  143. static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
  144. jas_stream_t *in, uint_fast16_t len);
  145. static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
  146. jas_stream_t *out);
  147. static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms);
  148. static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
  149. jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms);
  150. static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
  151. jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms);
  152. /******************************************************************************\
  153. * Global data.
  154. \******************************************************************************/
  155. static jpc_mstabent_t jpc_mstab[] = {
  156. {JPC_MS_SOC, "SOC", {0, 0, 0, 0}},
  157. {JPC_MS_SOT, "SOT", {0, jpc_sot_getparms, jpc_sot_putparms,
  158. jpc_sot_dumpparms}},
  159. {JPC_MS_SOD, "SOD", {0, 0, 0, 0}},
  160. {JPC_MS_EOC, "EOC", {0, 0, 0, 0}},
  161. {JPC_MS_SIZ, "SIZ", {jpc_siz_destroyparms, jpc_siz_getparms,
  162. jpc_siz_putparms, jpc_siz_dumpparms}},
  163. {JPC_MS_COD, "COD", {jpc_cod_destroyparms, jpc_cod_getparms,
  164. jpc_cod_putparms, jpc_cod_dumpparms}},
  165. {JPC_MS_COC, "COC", {jpc_coc_destroyparms, jpc_coc_getparms,
  166. jpc_coc_putparms, jpc_coc_dumpparms}},
  167. {JPC_MS_RGN, "RGN", {0, jpc_rgn_getparms, jpc_rgn_putparms,
  168. jpc_rgn_dumpparms}},
  169. {JPC_MS_QCD, "QCD", {jpc_qcd_destroyparms, jpc_qcd_getparms,
  170. jpc_qcd_putparms, jpc_qcd_dumpparms}},
  171. {JPC_MS_QCC, "QCC", {jpc_qcc_destroyparms, jpc_qcc_getparms,
  172. jpc_qcc_putparms, jpc_qcc_dumpparms}},
  173. {JPC_MS_POC, "POC", {jpc_poc_destroyparms, jpc_poc_getparms,
  174. jpc_poc_putparms, jpc_poc_dumpparms}},
  175. {JPC_MS_TLM, "TLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}},
  176. {JPC_MS_PLM, "PLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}},
  177. {JPC_MS_PPM, "PPM", {jpc_ppm_destroyparms, jpc_ppm_getparms,
  178. jpc_ppm_putparms, jpc_ppm_dumpparms}},
  179. {JPC_MS_PPT, "PPT", {jpc_ppt_destroyparms, jpc_ppt_getparms,
  180. jpc_ppt_putparms, jpc_ppt_dumpparms}},
  181. {JPC_MS_SOP, "SOP", {0, jpc_sop_getparms, jpc_sop_putparms,
  182. jpc_sop_dumpparms}},
  183. {JPC_MS_EPH, "EPH", {0, 0, 0, 0}},
  184. {JPC_MS_CRG, "CRG", {0, jpc_crg_getparms, jpc_crg_putparms,
  185. jpc_crg_dumpparms}},
  186. {JPC_MS_COM, "COM", {jpc_com_destroyparms, jpc_com_getparms,
  187. jpc_com_putparms, jpc_com_dumpparms}},
  188. {-1, "UNKNOWN", {jpc_unk_destroyparms, jpc_unk_getparms,
  189. jpc_unk_putparms, jpc_unk_dumpparms}}
  190. };
  191. /******************************************************************************\
  192. * Code stream manipulation functions.
  193. \******************************************************************************/
  194. /* Create a code stream state object. */
  195. jpc_cstate_t *jpc_cstate_create()
  196. {
  197. jpc_cstate_t *cstate;
  198. if (!(cstate = jas_malloc(sizeof(jpc_cstate_t)))) {
  199. return 0;
  200. }
  201. cstate->numcomps = 0;
  202. return cstate;
  203. }
  204. /* Destroy a code stream state object. */
  205. void jpc_cstate_destroy(jpc_cstate_t *cstate)
  206. {
  207. jas_free(cstate);
  208. }
  209. /* Read a marker segment from a stream. */
  210. jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate)
  211. {
  212. jpc_ms_t *ms;
  213. jpc_mstabent_t *mstabent;
  214. jas_stream_t *tmpstream;
  215. if (!(ms = jpc_ms_create(0))) {
  216. return 0;
  217. }
  218. /* Get the marker type. */
  219. if (jpc_getuint16(in, &ms->id) || ms->id < JPC_MS_MIN /*|| ms->id > JPC_MS_MAX*/) {
  220. jpc_ms_destroy(ms);
  221. return 0;
  222. }
  223. mstabent = jpc_mstab_lookup(ms->id);
  224. ms->ops = &mstabent->ops;
  225. /* Get the marker segment length and parameters if present. */
  226. /* Note: It is tacitly assumed that a marker segment cannot have
  227. parameters unless it has a length field. That is, there cannot
  228. be a parameters field without a length field and vice versa. */
  229. if (JPC_MS_HASPARMS(ms->id)) {
  230. /* Get the length of the marker segment. */
  231. if (jpc_getuint16(in, &ms->len) || ms->len < 3) {
  232. jpc_ms_destroy(ms);
  233. return 0;
  234. }
  235. /* Calculate the length of the marker segment parameters. */
  236. ms->len -= 2;
  237. /* Create and prepare a temporary memory stream from which to
  238. read the marker segment parameters. */
  239. /* Note: This approach provides a simple way of ensuring that
  240. we never read beyond the end of the marker segment (even if
  241. the marker segment length is errantly set too small). */
  242. if (!(tmpstream = jas_stream_memopen(0, 0))) {
  243. jpc_ms_destroy(ms);
  244. return 0;
  245. }
  246. if (jas_stream_copy(tmpstream, in, ms->len) ||
  247. jas_stream_seek(tmpstream, 0, SEEK_SET) < 0) {
  248. jas_stream_close(tmpstream);
  249. jpc_ms_destroy(ms);
  250. return 0;
  251. }
  252. /* Get the marker segment parameters. */
  253. if ((*ms->ops->getparms)(ms, cstate, tmpstream)) {
  254. ms->ops = 0;
  255. jpc_ms_destroy(ms);
  256. jas_stream_close(tmpstream);
  257. return 0;
  258. }
  259. if (jas_getdbglevel() > 0) {
  260. jpc_ms_dump(ms, stderr);
  261. }
  262. if (JAS_CAST(ulong, jas_stream_tell(tmpstream)) != ms->len) {
  263. jas_eprintf("warning: trailing garbage in marker segment (%ld bytes)\n",
  264. ms->len - jas_stream_tell(tmpstream));
  265. }
  266. /* Close the temporary stream. */
  267. jas_stream_close(tmpstream);
  268. } else {
  269. /* There are no marker segment parameters. */
  270. ms->len = 0;
  271. if (jas_getdbglevel() > 0) {
  272. jpc_ms_dump(ms, stderr);
  273. }
  274. }
  275. /* Update the code stream state information based on the type of
  276. marker segment read. */
  277. /* Note: This is a bit of a hack, but I'm not going to define another
  278. type of virtual function for this one special case. */
  279. if (ms->id == JPC_MS_SIZ) {
  280. cstate->numcomps = ms->parms.siz.numcomps;
  281. }
  282. return ms;
  283. }
  284. /* Write a marker segment to a stream. */
  285. int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms)
  286. {
  287. jas_stream_t *tmpstream;
  288. int len;
  289. /* Output the marker segment type. */
  290. if (jpc_putuint16(out, ms->id)) {
  291. return -1;
  292. }
  293. /* Output the marker segment length and parameters if necessary. */
  294. if (ms->ops->putparms) {
  295. /* Create a temporary stream in which to buffer the
  296. parameter data. */
  297. if (!(tmpstream = jas_stream_memopen(0, 0))) {
  298. return -1;
  299. }
  300. if ((*ms->ops->putparms)(ms, cstate, tmpstream)) {
  301. jas_stream_close(tmpstream);
  302. return -1;
  303. }
  304. /* Get the number of bytes of parameter data written. */
  305. if ((len = jas_stream_tell(tmpstream)) < 0) {
  306. jas_stream_close(tmpstream);
  307. return -1;
  308. }
  309. ms->len = len;
  310. /* Write the marker segment length and parameter data to
  311. the output stream. */
  312. if (jas_stream_seek(tmpstream, 0, SEEK_SET) < 0 ||
  313. jpc_putuint16(out, ms->len + 2) ||
  314. jas_stream_copy(out, tmpstream, ms->len) < 0) {
  315. jas_stream_close(tmpstream);
  316. return -1;
  317. }
  318. /* Close the temporary stream. */
  319. jas_stream_close(tmpstream);
  320. }
  321. /* This is a bit of a hack, but I'm not going to define another
  322. type of virtual function for this one special case. */
  323. if (ms->id == JPC_MS_SIZ) {
  324. cstate->numcomps = ms->parms.siz.numcomps;
  325. }
  326. if (jas_getdbglevel() > 0) {
  327. jpc_ms_dump(ms, stderr);
  328. }
  329. return 0;
  330. }
  331. /******************************************************************************\
  332. * Marker segment operations.
  333. \******************************************************************************/
  334. /* Create a marker segment of the specified type. */
  335. jpc_ms_t *jpc_ms_create(int type)
  336. {
  337. jpc_ms_t *ms;
  338. jpc_mstabent_t *mstabent;
  339. if (!(ms = jas_malloc(sizeof(jpc_ms_t)))) {
  340. return 0;
  341. }
  342. ms->id = type;
  343. ms->len = 0;
  344. mstabent = jpc_mstab_lookup(ms->id);
  345. ms->ops = &mstabent->ops;
  346. memset(&ms->parms, 0, sizeof(jpc_msparms_t));
  347. return ms;
  348. }
  349. /* Destroy a marker segment. */
  350. void jpc_ms_destroy(jpc_ms_t *ms)
  351. {
  352. if (ms->ops && ms->ops->destroyparms) {
  353. (*ms->ops->destroyparms)(ms);
  354. }
  355. jas_free(ms);
  356. }
  357. /* Dump a marker segment to a stream for debugging. */
  358. void jpc_ms_dump(jpc_ms_t *ms, FILE *out)
  359. {
  360. jpc_mstabent_t *mstabent;
  361. mstabent = jpc_mstab_lookup(ms->id);
  362. fprintf(out, "type = 0x%04x (%s);", (unsigned)ms->id, mstabent->name);
  363. if (JPC_MS_HASPARMS(ms->id)) {
  364. fprintf(out, " len = %d;", (int)(ms->len + 2));
  365. if (ms->ops->dumpparms) {
  366. (*ms->ops->dumpparms)(ms, out);
  367. } else {
  368. fprintf(out, "\n");
  369. }
  370. } else {
  371. fprintf(out, "\n");
  372. }
  373. }
  374. /******************************************************************************\
  375. * SOT marker segment operations.
  376. \******************************************************************************/
  377. static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  378. {
  379. jpc_sot_t *sot = &ms->parms.sot;
  380. /* Eliminate compiler warning about unused variables. */
  381. cstate = 0;
  382. if (jpc_getuint16(in, &sot->tileno) ||
  383. jpc_getuint32(in, &sot->len) ||
  384. jpc_getuint8(in, &sot->partno) ||
  385. jpc_getuint8(in, &sot->numparts)) {
  386. return -1;
  387. }
  388. if (jas_stream_eof(in)) {
  389. return -1;
  390. }
  391. return 0;
  392. }
  393. static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  394. {
  395. jpc_sot_t *sot = &ms->parms.sot;
  396. /* Eliminate compiler warning about unused variables. */
  397. cstate = 0;
  398. if (jpc_putuint16(out, sot->tileno) ||
  399. jpc_putuint32(out, sot->len) ||
  400. jpc_putuint8(out, sot->partno) ||
  401. jpc_putuint8(out, sot->numparts)) {
  402. return -1;
  403. }
  404. return 0;
  405. }
  406. static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out)
  407. {
  408. jpc_sot_t *sot = &ms->parms.sot;
  409. fprintf(out, "tileno = %d; len = %d; partno = %d; numparts = %d\n",
  410. (int)sot->tileno, (int)sot->len, sot->partno, sot->numparts);
  411. return 0;
  412. }
  413. /******************************************************************************\
  414. * SIZ marker segment operations.
  415. \******************************************************************************/
  416. static void jpc_siz_destroyparms(jpc_ms_t *ms)
  417. {
  418. jpc_siz_t *siz = &ms->parms.siz;
  419. if (siz->comps) {
  420. jas_free(siz->comps);
  421. }
  422. }
  423. static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
  424. jas_stream_t *in)
  425. {
  426. jpc_siz_t *siz = &ms->parms.siz;
  427. unsigned int i;
  428. uint_fast8_t tmp;
  429. /* Eliminate compiler warning about unused variables. */
  430. cstate = 0;
  431. if (jpc_getuint16(in, &siz->caps) ||
  432. jpc_getuint32(in, &siz->width) ||
  433. jpc_getuint32(in, &siz->height) ||
  434. jpc_getuint32(in, &siz->xoff) ||
  435. jpc_getuint32(in, &siz->yoff) ||
  436. jpc_getuint32(in, &siz->tilewidth) ||
  437. jpc_getuint32(in, &siz->tileheight) ||
  438. jpc_getuint32(in, &siz->tilexoff) ||
  439. jpc_getuint32(in, &siz->tileyoff) ||
  440. jpc_getuint16(in, &siz->numcomps)) {
  441. return -1;
  442. }
  443. if (!siz->width || !siz->height || !siz->tilewidth ||
  444. !siz->tileheight || !siz->numcomps) {
  445. return -1;
  446. }
  447. if (!(siz->comps = jas_alloc2(siz->numcomps, sizeof(jpc_sizcomp_t)))) {
  448. return -1;
  449. }
  450. for (i = 0; i < siz->numcomps; ++i) {
  451. if (jpc_getuint8(in, &tmp) ||
  452. jpc_getuint8(in, &siz->comps[i].hsamp) ||
  453. jpc_getuint8(in, &siz->comps[i].vsamp)) {
  454. jas_free(siz->comps);
  455. return -1;
  456. }
  457. siz->comps[i].sgnd = (tmp >> 7) & 1;
  458. siz->comps[i].prec = (tmp & 0x7f) + 1;
  459. }
  460. if (jas_stream_eof(in)) {
  461. jas_free(siz->comps);
  462. return -1;
  463. }
  464. return 0;
  465. }
  466. static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  467. {
  468. jpc_siz_t *siz = &ms->parms.siz;
  469. unsigned int i;
  470. /* Eliminate compiler warning about unused variables. */
  471. cstate = 0;
  472. assert(siz->width && siz->height && siz->tilewidth &&
  473. siz->tileheight && siz->numcomps);
  474. if (jpc_putuint16(out, siz->caps) ||
  475. jpc_putuint32(out, siz->width) ||
  476. jpc_putuint32(out, siz->height) ||
  477. jpc_putuint32(out, siz->xoff) ||
  478. jpc_putuint32(out, siz->yoff) ||
  479. jpc_putuint32(out, siz->tilewidth) ||
  480. jpc_putuint32(out, siz->tileheight) ||
  481. jpc_putuint32(out, siz->tilexoff) ||
  482. jpc_putuint32(out, siz->tileyoff) ||
  483. jpc_putuint16(out, siz->numcomps)) {
  484. return -1;
  485. }
  486. for (i = 0; i < siz->numcomps; ++i) {
  487. if (jpc_putuint8(out, ((siz->comps[i].sgnd & 1) << 7) |
  488. ((siz->comps[i].prec - 1) & 0x7f)) ||
  489. jpc_putuint8(out, siz->comps[i].hsamp) ||
  490. jpc_putuint8(out, siz->comps[i].vsamp)) {
  491. return -1;
  492. }
  493. }
  494. return 0;
  495. }
  496. static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out)
  497. {
  498. jpc_siz_t *siz = &ms->parms.siz;
  499. unsigned int i;
  500. fprintf(out, "caps = 0x%02x;\n", (unsigned)siz->caps);
  501. fprintf(out, "width = %d; height = %d; xoff = %d; yoff = %d;\n",
  502. (int)siz->width, (int)siz->height, (int)siz->xoff, (int)siz->yoff);
  503. fprintf(out, "tilewidth = %d; tileheight = %d; tilexoff = %d; "
  504. "tileyoff = %d;\n", (int)siz->tilewidth, (int)siz->tileheight, (int)siz->tilexoff,
  505. (int)siz->tileyoff);
  506. for (i = 0; i < siz->numcomps; ++i) {
  507. fprintf(out, "prec[%d] = %d; sgnd[%d] = %d; hsamp[%d] = %d; "
  508. "vsamp[%d] = %d\n", i, siz->comps[i].prec, i,
  509. siz->comps[i].sgnd, i, siz->comps[i].hsamp, i,
  510. siz->comps[i].vsamp);
  511. }
  512. return 0;
  513. }
  514. /******************************************************************************\
  515. * COD marker segment operations.
  516. \******************************************************************************/
  517. static void jpc_cod_destroyparms(jpc_ms_t *ms)
  518. {
  519. jpc_cod_t *cod = &ms->parms.cod;
  520. jpc_cox_destroycompparms(&cod->compparms);
  521. }
  522. static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  523. {
  524. jpc_cod_t *cod = &ms->parms.cod;
  525. if (jpc_getuint8(in, &cod->csty)) {
  526. return -1;
  527. }
  528. if (jpc_getuint8(in, &cod->prg) ||
  529. jpc_getuint16(in, &cod->numlyrs) ||
  530. jpc_getuint8(in, &cod->mctrans)) {
  531. return -1;
  532. }
  533. if (jpc_cox_getcompparms(ms, cstate, in,
  534. (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) {
  535. return -1;
  536. }
  537. if (jas_stream_eof(in)) {
  538. jpc_cod_destroyparms(ms);
  539. return -1;
  540. }
  541. return 0;
  542. }
  543. static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  544. {
  545. jpc_cod_t *cod = &ms->parms.cod;
  546. assert(cod->numlyrs > 0 && cod->compparms.numdlvls <= 32);
  547. assert(cod->compparms.numdlvls == cod->compparms.numrlvls - 1);
  548. if (jpc_putuint8(out, cod->compparms.csty) ||
  549. jpc_putuint8(out, cod->prg) ||
  550. jpc_putuint16(out, cod->numlyrs) ||
  551. jpc_putuint8(out, cod->mctrans)) {
  552. return -1;
  553. }
  554. if (jpc_cox_putcompparms(ms, cstate, out,
  555. (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) {
  556. return -1;
  557. }
  558. return 0;
  559. }
  560. static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out)
  561. {
  562. jpc_cod_t *cod = &ms->parms.cod;
  563. int i;
  564. fprintf(out, "csty = 0x%02x;\n", cod->compparms.csty);
  565. fprintf(out, "numdlvls = %d; qmfbid = %d; mctrans = %d\n",
  566. cod->compparms.numdlvls, cod->compparms.qmfbid, cod->mctrans);
  567. fprintf(out, "prg = %d; numlyrs = %d;\n",
  568. cod->prg, (int)cod->numlyrs);
  569. fprintf(out, "cblkwidthval = %d; cblkheightval = %d; "
  570. "cblksty = 0x%02x;\n", cod->compparms.cblkwidthval, cod->compparms.cblkheightval,
  571. cod->compparms.cblksty);
  572. if (cod->csty & JPC_COX_PRT) {
  573. for (i = 0; i < cod->compparms.numrlvls; ++i) {
  574. jas_eprintf("prcwidth[%d] = %d, prcheight[%d] = %d\n",
  575. i, cod->compparms.rlvls[i].parwidthval,
  576. i, cod->compparms.rlvls[i].parheightval);
  577. }
  578. }
  579. return 0;
  580. }
  581. /******************************************************************************\
  582. * COC marker segment operations.
  583. \******************************************************************************/
  584. static void jpc_coc_destroyparms(jpc_ms_t *ms)
  585. {
  586. jpc_coc_t *coc = &ms->parms.coc;
  587. jpc_cox_destroycompparms(&coc->compparms);
  588. }
  589. static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  590. {
  591. jpc_coc_t *coc = &ms->parms.coc;
  592. uint_fast8_t tmp;
  593. if (cstate->numcomps <= 256) {
  594. if (jpc_getuint8(in, &tmp)) {
  595. return -1;
  596. }
  597. coc->compno = tmp;
  598. } else {
  599. if (jpc_getuint16(in, &coc->compno)) {
  600. return -1;
  601. }
  602. }
  603. if (jpc_getuint8(in, &coc->compparms.csty)) {
  604. return -1;
  605. }
  606. if (jpc_cox_getcompparms(ms, cstate, in,
  607. (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) {
  608. return -1;
  609. }
  610. if (jas_stream_eof(in)) {
  611. return -1;
  612. }
  613. return 0;
  614. }
  615. static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  616. {
  617. jpc_coc_t *coc = &ms->parms.coc;
  618. assert(coc->compparms.numdlvls <= 32);
  619. if (cstate->numcomps <= 256) {
  620. if (jpc_putuint8(out, coc->compno)) {
  621. return -1;
  622. }
  623. } else {
  624. if (jpc_putuint16(out, coc->compno)) {
  625. return -1;
  626. }
  627. }
  628. if (jpc_putuint8(out, coc->compparms.csty)) {
  629. return -1;
  630. }
  631. if (jpc_cox_putcompparms(ms, cstate, out,
  632. (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) {
  633. return -1;
  634. }
  635. return 0;
  636. }
  637. static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out)
  638. {
  639. jpc_coc_t *coc = &ms->parms.coc;
  640. fprintf(out, "compno = %d; csty = 0x%02x; numdlvls = %d;\n",
  641. (int)coc->compno, coc->compparms.csty, coc->compparms.numdlvls);
  642. fprintf(out, "cblkwidthval = %d; cblkheightval = %d; "
  643. "cblksty = 0x%02x; qmfbid = %d;\n", coc->compparms.cblkwidthval,
  644. coc->compparms.cblkheightval, coc->compparms.cblksty, coc->compparms.qmfbid);
  645. return 0;
  646. }
  647. /******************************************************************************\
  648. * COD/COC marker segment operation helper functions.
  649. \******************************************************************************/
  650. static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms)
  651. {
  652. /* Eliminate compiler warning about unused variables. */
  653. compparms = 0;
  654. }
  655. static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
  656. jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms)
  657. {
  658. uint_fast8_t tmp;
  659. int i;
  660. /* Eliminate compiler warning about unused variables. */
  661. ms = 0;
  662. cstate = 0;
  663. if (jpc_getuint8(in, &compparms->numdlvls) ||
  664. jpc_getuint8(in, &compparms->cblkwidthval) ||
  665. jpc_getuint8(in, &compparms->cblkheightval) ||
  666. jpc_getuint8(in, &compparms->cblksty) ||
  667. jpc_getuint8(in, &compparms->qmfbid)) {
  668. return -1;
  669. }
  670. compparms->numrlvls = compparms->numdlvls + 1;
  671. if (compparms->numrlvls > JPC_MAXRLVLS) {
  672. jpc_cox_destroycompparms(compparms);
  673. return -1;
  674. }
  675. if (prtflag) {
  676. for (i = 0; i < compparms->numrlvls; ++i) {
  677. if (jpc_getuint8(in, &tmp)) {
  678. jpc_cox_destroycompparms(compparms);
  679. return -1;
  680. }
  681. compparms->rlvls[i].parwidthval = tmp & 0xf;
  682. compparms->rlvls[i].parheightval = (tmp >> 4) & 0xf;
  683. }
  684. /* Sigh. This bit should be in the same field in both COC and COD mrk segs. */
  685. compparms->csty |= JPC_COX_PRT;
  686. } else {
  687. }
  688. if (jas_stream_eof(in)) {
  689. jpc_cox_destroycompparms(compparms);
  690. return -1;
  691. }
  692. return 0;
  693. }
  694. static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
  695. jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms)
  696. {
  697. int i;
  698. assert(compparms->numdlvls <= 32);
  699. /* Eliminate compiler warning about unused variables. */
  700. ms = 0;
  701. cstate = 0;
  702. if (jpc_putuint8(out, compparms->numdlvls) ||
  703. jpc_putuint8(out, compparms->cblkwidthval) ||
  704. jpc_putuint8(out, compparms->cblkheightval) ||
  705. jpc_putuint8(out, compparms->cblksty) ||
  706. jpc_putuint8(out, compparms->qmfbid)) {
  707. return -1;
  708. }
  709. if (prtflag) {
  710. for (i = 0; i < compparms->numrlvls; ++i) {
  711. if (jpc_putuint8(out,
  712. ((compparms->rlvls[i].parheightval & 0xf) << 4) |
  713. (compparms->rlvls[i].parwidthval & 0xf))) {
  714. return -1;
  715. }
  716. }
  717. }
  718. return 0;
  719. }
  720. /******************************************************************************\
  721. * RGN marker segment operations.
  722. \******************************************************************************/
  723. static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  724. {
  725. jpc_rgn_t *rgn = &ms->parms.rgn;
  726. uint_fast8_t tmp;
  727. if (cstate->numcomps <= 256) {
  728. if (jpc_getuint8(in, &tmp)) {
  729. return -1;
  730. }
  731. rgn->compno = tmp;
  732. } else {
  733. if (jpc_getuint16(in, &rgn->compno)) {
  734. return -1;
  735. }
  736. }
  737. if (jpc_getuint8(in, &rgn->roisty) ||
  738. jpc_getuint8(in, &rgn->roishift)) {
  739. return -1;
  740. }
  741. return 0;
  742. }
  743. static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  744. {
  745. jpc_rgn_t *rgn = &ms->parms.rgn;
  746. if (cstate->numcomps <= 256) {
  747. if (jpc_putuint8(out, rgn->compno)) {
  748. return -1;
  749. }
  750. } else {
  751. if (jpc_putuint16(out, rgn->compno)) {
  752. return -1;
  753. }
  754. }
  755. if (jpc_putuint8(out, rgn->roisty) ||
  756. jpc_putuint8(out, rgn->roishift)) {
  757. return -1;
  758. }
  759. return 0;
  760. }
  761. static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out)
  762. {
  763. jpc_rgn_t *rgn = &ms->parms.rgn;
  764. fprintf(out, "compno = %d; roisty = %d; roishift = %d\n",
  765. (int)rgn->compno, rgn->roisty, rgn->roishift);
  766. return 0;
  767. }
  768. /******************************************************************************\
  769. * QCD marker segment operations.
  770. \******************************************************************************/
  771. static void jpc_qcd_destroyparms(jpc_ms_t *ms)
  772. {
  773. jpc_qcd_t *qcd = &ms->parms.qcd;
  774. jpc_qcx_destroycompparms(&qcd->compparms);
  775. }
  776. static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  777. {
  778. jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms;
  779. return jpc_qcx_getcompparms(compparms, cstate, in, ms->len);
  780. }
  781. static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  782. {
  783. jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms;
  784. return jpc_qcx_putcompparms(compparms, cstate, out);
  785. }
  786. static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out)
  787. {
  788. jpc_qcd_t *qcd = &ms->parms.qcd;
  789. int i;
  790. fprintf(out, "qntsty = %d; numguard = %d; numstepsizes = %d\n",
  791. (int) qcd->compparms.qntsty, qcd->compparms.numguard, qcd->compparms.numstepsizes);
  792. for (i = 0; i < qcd->compparms.numstepsizes; ++i) {
  793. fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n",
  794. i, (unsigned) JPC_QCX_GETEXPN(qcd->compparms.stepsizes[i]),
  795. i, (unsigned) JPC_QCX_GETMANT(qcd->compparms.stepsizes[i]));
  796. }
  797. return 0;
  798. }
  799. /******************************************************************************\
  800. * QCC marker segment operations.
  801. \******************************************************************************/
  802. static void jpc_qcc_destroyparms(jpc_ms_t *ms)
  803. {
  804. jpc_qcc_t *qcc = &ms->parms.qcc;
  805. jpc_qcx_destroycompparms(&qcc->compparms);
  806. }
  807. static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  808. {
  809. jpc_qcc_t *qcc = &ms->parms.qcc;
  810. uint_fast8_t tmp;
  811. int len;
  812. len = ms->len;
  813. if (cstate->numcomps <= 256) {
  814. jpc_getuint8(in, &tmp);
  815. qcc->compno = tmp;
  816. --len;
  817. } else {
  818. jpc_getuint16(in, &qcc->compno);
  819. len -= 2;
  820. }
  821. if (jpc_qcx_getcompparms(&qcc->compparms, cstate, in, len)) {
  822. return -1;
  823. }
  824. if (jas_stream_eof(in)) {
  825. jpc_qcc_destroyparms(ms);
  826. return -1;
  827. }
  828. return 0;
  829. }
  830. static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  831. {
  832. jpc_qcc_t *qcc = &ms->parms.qcc;
  833. if (cstate->numcomps <= 256) {
  834. jpc_putuint8(out, qcc->compno);
  835. } else {
  836. jpc_putuint16(out, qcc->compno);
  837. }
  838. if (jpc_qcx_putcompparms(&qcc->compparms, cstate, out)) {
  839. return -1;
  840. }
  841. return 0;
  842. }
  843. static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out)
  844. {
  845. jpc_qcc_t *qcc = &ms->parms.qcc;
  846. int i;
  847. fprintf(out, "compno = %d; qntsty = %d; numguard = %d; "
  848. "numstepsizes = %d\n", (int)qcc->compno, qcc->compparms.qntsty, qcc->compparms.numguard,
  849. qcc->compparms.numstepsizes);
  850. for (i = 0; i < qcc->compparms.numstepsizes; ++i) {
  851. fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n",
  852. i, (unsigned) JPC_QCX_GETEXPN(qcc->compparms.stepsizes[i]),
  853. i, (unsigned) JPC_QCX_GETMANT(qcc->compparms.stepsizes[i]));
  854. }
  855. return 0;
  856. }
  857. /******************************************************************************\
  858. * QCD/QCC marker segment helper functions.
  859. \******************************************************************************/
  860. static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms)
  861. {
  862. if (compparms->stepsizes) {
  863. jas_free(compparms->stepsizes);
  864. }
  865. }
  866. static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
  867. jas_stream_t *in, uint_fast16_t len)
  868. {
  869. uint_fast8_t tmp;
  870. int n;
  871. int i;
  872. /* Eliminate compiler warning about unused variables. */
  873. cstate = 0;
  874. n = 0;
  875. jpc_getuint8(in, &tmp);
  876. ++n;
  877. compparms->qntsty = tmp & 0x1f;
  878. compparms->numguard = (tmp >> 5) & 7;
  879. switch (compparms->qntsty) {
  880. case JPC_QCX_SIQNT:
  881. compparms->numstepsizes = 1;
  882. break;
  883. case JPC_QCX_NOQNT:
  884. compparms->numstepsizes = (len - n);
  885. break;
  886. case JPC_QCX_SEQNT:
  887. /* XXX - this is a hack */
  888. compparms->numstepsizes = (len - n) / 2;
  889. break;
  890. }
  891. if (compparms->numstepsizes > 3 * JPC_MAXRLVLS + 1) {
  892. jpc_qcx_destroycompparms(compparms);
  893. return -1;
  894. } else if (compparms->numstepsizes > 0) {
  895. compparms->stepsizes = jas_malloc(compparms->numstepsizes *
  896. sizeof(uint_fast16_t));
  897. assert(compparms->stepsizes);
  898. for (i = 0; i < compparms->numstepsizes; ++i) {
  899. if (compparms->qntsty == JPC_QCX_NOQNT) {
  900. jpc_getuint8(in, &tmp);
  901. compparms->stepsizes[i] = JPC_QCX_EXPN(tmp >> 3);
  902. } else {
  903. jpc_getuint16(in, &compparms->stepsizes[i]);
  904. }
  905. }
  906. } else {
  907. compparms->stepsizes = 0;
  908. }
  909. if (jas_stream_error(in) || jas_stream_eof(in)) {
  910. jpc_qcx_destroycompparms(compparms);
  911. return -1;
  912. }
  913. return 0;
  914. }
  915. static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
  916. jas_stream_t *out)
  917. {
  918. int i;
  919. /* Eliminate compiler warning about unused variables. */
  920. cstate = 0;
  921. jpc_putuint8(out, ((compparms->numguard & 7) << 5) | compparms->qntsty);
  922. for (i = 0; i < compparms->numstepsizes; ++i) {
  923. if (compparms->qntsty == JPC_QCX_NOQNT) {
  924. jpc_putuint8(out, JPC_QCX_GETEXPN(
  925. compparms->stepsizes[i]) << 3);
  926. } else {
  927. jpc_putuint16(out, compparms->stepsizes[i]);
  928. }
  929. }
  930. return 0;
  931. }
  932. /******************************************************************************\
  933. * SOP marker segment operations.
  934. \******************************************************************************/
  935. static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  936. {
  937. jpc_sop_t *sop = &ms->parms.sop;
  938. /* Eliminate compiler warning about unused variable. */
  939. cstate = 0;
  940. if (jpc_getuint16(in, &sop->seqno)) {
  941. return -1;
  942. }
  943. return 0;
  944. }
  945. static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  946. {
  947. jpc_sop_t *sop = &ms->parms.sop;
  948. /* Eliminate compiler warning about unused variable. */
  949. cstate = 0;
  950. if (jpc_putuint16(out, sop->seqno)) {
  951. return -1;
  952. }
  953. return 0;
  954. }
  955. static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out)
  956. {
  957. jpc_sop_t *sop = &ms->parms.sop;
  958. fprintf(out, "seqno = %d;\n", (int)sop->seqno);
  959. return 0;
  960. }
  961. /******************************************************************************\
  962. * PPM marker segment operations.
  963. \******************************************************************************/
  964. static void jpc_ppm_destroyparms(jpc_ms_t *ms)
  965. {
  966. jpc_ppm_t *ppm = &ms->parms.ppm;
  967. if (ppm->data) {
  968. jas_free(ppm->data);
  969. }
  970. }
  971. static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  972. {
  973. jpc_ppm_t *ppm = &ms->parms.ppm;
  974. /* Eliminate compiler warning about unused variables. */
  975. cstate = 0;
  976. ppm->data = 0;
  977. if (ms->len < 1) {
  978. goto error;
  979. }
  980. if (jpc_getuint8(in, &ppm->ind)) {
  981. goto error;
  982. }
  983. ppm->len = ms->len - 1;
  984. if (ppm->len > 0) {
  985. if (!(ppm->data = jas_malloc(ppm->len))) {
  986. goto error;
  987. }
  988. if (JAS_CAST(uint, jas_stream_read(in, ppm->data, ppm->len)) != ppm->len) {
  989. goto error;
  990. }
  991. } else {
  992. ppm->data = 0;
  993. }
  994. return 0;
  995. error:
  996. jpc_ppm_destroyparms(ms);
  997. return -1;
  998. }
  999. static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  1000. {
  1001. jpc_ppm_t *ppm = &ms->parms.ppm;
  1002. /* Eliminate compiler warning about unused variables. */
  1003. cstate = 0;
  1004. if (JAS_CAST(uint, jas_stream_write(out, (char *) ppm->data, ppm->len)) != ppm->len) {
  1005. return -1;
  1006. }
  1007. return 0;
  1008. }
  1009. static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out)
  1010. {
  1011. jpc_ppm_t *ppm = &ms->parms.ppm;
  1012. fprintf(out, "ind=%d; len = %d;\n", ppm->ind, (int)ppm->len);
  1013. if (ppm->len > 0) {
  1014. fprintf(out, "data =\n");
  1015. jas_memdump(out, ppm->data, ppm->len);
  1016. }
  1017. return 0;
  1018. }
  1019. /******************************************************************************\
  1020. * PPT marker segment operations.
  1021. \******************************************************************************/
  1022. static void jpc_ppt_destroyparms(jpc_ms_t *ms)
  1023. {
  1024. jpc_ppt_t *ppt = &ms->parms.ppt;
  1025. if (ppt->data) {
  1026. jas_free(ppt->data);
  1027. }
  1028. }
  1029. static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  1030. {
  1031. jpc_ppt_t *ppt = &ms->parms.ppt;
  1032. /* Eliminate compiler warning about unused variables. */
  1033. cstate = 0;
  1034. ppt->data = 0;
  1035. if (ms->len < 1) {
  1036. goto error;
  1037. }
  1038. if (jpc_getuint8(in, &ppt->ind)) {
  1039. goto error;
  1040. }
  1041. ppt->len = ms->len - 1;
  1042. if (ppt->len > 0) {
  1043. if (!(ppt->data = jas_malloc(ppt->len))) {
  1044. goto error;
  1045. }
  1046. if (jas_stream_read(in, (char *) ppt->data, ppt->len) != JAS_CAST(int, ppt->len)) {
  1047. goto error;
  1048. }
  1049. } else {
  1050. ppt->data = 0;
  1051. }
  1052. return 0;
  1053. error:
  1054. jpc_ppt_destroyparms(ms);
  1055. return -1;
  1056. }
  1057. static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  1058. {
  1059. jpc_ppt_t *ppt = &ms->parms.ppt;
  1060. /* Eliminate compiler warning about unused variable. */
  1061. cstate = 0;
  1062. if (jpc_putuint8(out, ppt->ind)) {
  1063. return -1;
  1064. }
  1065. if (jas_stream_write(out, (char *) ppt->data, ppt->len) != JAS_CAST(int, ppt->len)) {
  1066. return -1;
  1067. }
  1068. return 0;
  1069. }
  1070. static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out)
  1071. {
  1072. jpc_ppt_t *ppt = &ms->parms.ppt;
  1073. fprintf(out, "ind=%d; len = %d;\n", ppt->ind, (int)ppt->len);
  1074. if (ppt->len > 0) {
  1075. fprintf(out, "data =\n");
  1076. jas_memdump(out, ppt->data, ppt->len);
  1077. }
  1078. return 0;
  1079. }
  1080. /******************************************************************************\
  1081. * POC marker segment operations.
  1082. \******************************************************************************/
  1083. static void jpc_poc_destroyparms(jpc_ms_t *ms)
  1084. {
  1085. jpc_poc_t *poc = &ms->parms.poc;
  1086. if (poc->pchgs) {
  1087. jas_free(poc->pchgs);
  1088. }
  1089. }
  1090. static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  1091. {
  1092. jpc_poc_t *poc = &ms->parms.poc;
  1093. jpc_pocpchg_t *pchg;
  1094. int pchgno;
  1095. uint_fast8_t tmp;
  1096. poc->numpchgs = (cstate->numcomps > 256) ? (ms->len / 9) :
  1097. (ms->len / 7);
  1098. if (!(poc->pchgs = jas_alloc2(poc->numpchgs, sizeof(jpc_pocpchg_t)))) {
  1099. goto error;
  1100. }
  1101. for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno,
  1102. ++pchg) {
  1103. if (jpc_getuint8(in, &pchg->rlvlnostart)) {
  1104. goto error;
  1105. }
  1106. if (cstate->numcomps > 256) {
  1107. if (jpc_getuint16(in, &pchg->compnostart)) {
  1108. goto error;
  1109. }
  1110. } else {
  1111. if (jpc_getuint8(in, &tmp)) {
  1112. goto error;
  1113. };
  1114. pchg->compnostart = tmp;
  1115. }
  1116. if (jpc_getuint16(in, &pchg->lyrnoend) ||
  1117. jpc_getuint8(in, &pchg->rlvlnoend)) {
  1118. goto error;
  1119. }
  1120. if (cstate->numcomps > 256) {
  1121. if (jpc_getuint16(in, &pchg->compnoend)) {
  1122. goto error;
  1123. }
  1124. } else {
  1125. if (jpc_getuint8(in, &tmp)) {
  1126. goto error;
  1127. }
  1128. pchg->compnoend = tmp;
  1129. }
  1130. if (jpc_getuint8(in, &pchg->prgord)) {
  1131. goto error;
  1132. }
  1133. if (pchg->rlvlnostart > pchg->rlvlnoend ||
  1134. pchg->compnostart > pchg->compnoend) {
  1135. goto error;
  1136. }
  1137. }
  1138. return 0;
  1139. error:
  1140. jpc_poc_destroyparms(ms);
  1141. return -1;
  1142. }
  1143. static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  1144. {
  1145. jpc_poc_t *poc = &ms->parms.poc;
  1146. jpc_pocpchg_t *pchg;
  1147. int pchgno;
  1148. for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno,
  1149. ++pchg) {
  1150. if (jpc_putuint8(out, pchg->rlvlnostart) ||
  1151. ((cstate->numcomps > 256) ?
  1152. jpc_putuint16(out, pchg->compnostart) :
  1153. jpc_putuint8(out, pchg->compnostart)) ||
  1154. jpc_putuint16(out, pchg->lyrnoend) ||
  1155. jpc_putuint8(out, pchg->rlvlnoend) ||
  1156. ((cstate->numcomps > 256) ?
  1157. jpc_putuint16(out, pchg->compnoend) :
  1158. jpc_putuint8(out, pchg->compnoend)) ||
  1159. jpc_putuint8(out, pchg->prgord)) {
  1160. return -1;
  1161. }
  1162. }
  1163. return 0;
  1164. }
  1165. static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out)
  1166. {
  1167. jpc_poc_t *poc = &ms->parms.poc;
  1168. jpc_pocpchg_t *pchg;
  1169. int pchgno;
  1170. for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs;
  1171. ++pchgno, ++pchg) {
  1172. fprintf(out, "po[%d] = %d; ", pchgno, pchg->prgord);
  1173. fprintf(out, "cs[%d] = %d; ce[%d] = %d; ",
  1174. pchgno, (int)pchg->compnostart, pchgno, (int)pchg->compnoend);
  1175. fprintf(out, "rs[%d] = %d; re[%d] = %d; ",
  1176. pchgno, pchg->rlvlnostart, pchgno, pchg->rlvlnoend);
  1177. fprintf(out, "le[%d] = %d\n", pchgno, (int)pchg->lyrnoend);
  1178. }
  1179. return 0;
  1180. }
  1181. /******************************************************************************\
  1182. * CRG marker segment operations.
  1183. \******************************************************************************/
  1184. static void jpc_crg_destroyparms(jpc_ms_t *ms)
  1185. {
  1186. jpc_crg_t *crg = &ms->parms.crg;
  1187. if (crg->comps) {
  1188. jas_free(crg->comps);
  1189. }
  1190. }
  1191. static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  1192. {
  1193. jpc_crg_t *crg = &ms->parms.crg;
  1194. jpc_crgcomp_t *comp;
  1195. uint_fast16_t compno;
  1196. crg->numcomps = cstate->numcomps;
  1197. if (!(crg->comps = jas_alloc2(cstate->numcomps, sizeof(jpc_crgcomp_t)))) {
  1198. return -1;
  1199. }
  1200. for (compno = 0, comp = crg->comps; compno < cstate->numcomps;
  1201. ++compno, ++comp) {
  1202. if (jpc_getuint16(in, &comp->hoff) ||
  1203. jpc_getuint16(in, &comp->voff)) {
  1204. jpc_crg_destroyparms(ms);
  1205. return -1;
  1206. }
  1207. }
  1208. return 0;
  1209. }
  1210. static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  1211. {
  1212. jpc_crg_t *crg = &ms->parms.crg;
  1213. int compno;
  1214. jpc_crgcomp_t *comp;
  1215. /* Eliminate compiler warning about unused variables. */
  1216. cstate = 0;
  1217. for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno,
  1218. ++comp) {
  1219. if (jpc_putuint16(out, comp->hoff) ||
  1220. jpc_putuint16(out, comp->voff)) {
  1221. return -1;
  1222. }
  1223. }
  1224. return 0;
  1225. }
  1226. static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out)
  1227. {
  1228. jpc_crg_t *crg = &ms->parms.crg;
  1229. int compno;
  1230. jpc_crgcomp_t *comp;
  1231. for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno,
  1232. ++comp) {
  1233. fprintf(out, "hoff[%d] = %d; voff[%d] = %d\n", compno,
  1234. (int)comp->hoff, compno, (int)comp->voff);
  1235. }
  1236. return 0;
  1237. }
  1238. /******************************************************************************\
  1239. * Operations for COM marker segment.
  1240. \******************************************************************************/
  1241. static void jpc_com_destroyparms(jpc_ms_t *ms)
  1242. {
  1243. jpc_com_t *com = &ms->parms.com;
  1244. if (com->data) {
  1245. jas_free(com->data);
  1246. }
  1247. }
  1248. static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  1249. {
  1250. jpc_com_t *com = &ms->parms.com;
  1251. /* Eliminate compiler warning about unused variables. */
  1252. cstate = 0;
  1253. if (jpc_getuint16(in, &com->regid)) {
  1254. return -1;
  1255. }
  1256. com->len = ms->len - 2;
  1257. if (com->len > 0) {
  1258. if (!(com->data = jas_malloc(com->len))) {
  1259. return -1;
  1260. }
  1261. if (jas_stream_read(in, com->data, com->len) != JAS_CAST(int, com->len)) {
  1262. return -1;
  1263. }
  1264. } else {
  1265. com->data = 0;
  1266. }
  1267. return 0;
  1268. }
  1269. static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  1270. {
  1271. jpc_com_t *com = &ms->parms.com;
  1272. /* Eliminate compiler warning about unused variables. */
  1273. cstate = 0;
  1274. if (jpc_putuint16(out, com->regid)) {
  1275. return -1;
  1276. }
  1277. if (jas_stream_write(out, com->data, com->len) != JAS_CAST(int, com->len)) {
  1278. return -1;
  1279. }
  1280. return 0;
  1281. }
  1282. static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out)
  1283. {
  1284. jpc_com_t *com = &ms->parms.com;
  1285. unsigned int i;
  1286. int printable;
  1287. fprintf(out, "regid = %d;\n", (int)com->regid);
  1288. printable = 1;
  1289. for (i = 0; i < com->len; ++i) {
  1290. if (!isprint(com->data[i])) {
  1291. printable = 0;
  1292. break;
  1293. }
  1294. }
  1295. if (printable) {
  1296. fprintf(out, "data = ");
  1297. fwrite(com->data, sizeof(char), com->len, out);
  1298. fprintf(out, "\n");
  1299. }
  1300. return 0;
  1301. }
  1302. /******************************************************************************\
  1303. * Operations for unknown types of marker segments.
  1304. \******************************************************************************/
  1305. static void jpc_unk_destroyparms(jpc_ms_t *ms)
  1306. {
  1307. jpc_unk_t *unk = &ms->parms.unk;
  1308. if (unk->data) {
  1309. jas_free(unk->data);
  1310. }
  1311. }
  1312. static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
  1313. {
  1314. jpc_unk_t *unk = &ms->parms.unk;
  1315. /* Eliminate compiler warning about unused variables. */
  1316. cstate = 0;
  1317. if (ms->len > 0) {
  1318. if (!(unk->data = jas_malloc(ms->len))) {
  1319. return -1;
  1320. }
  1321. if (jas_stream_read(in, (char *) unk->data, ms->len) != JAS_CAST(int, ms->len)) {
  1322. jas_free(unk->data);
  1323. return -1;
  1324. }
  1325. unk->len = ms->len;
  1326. } else {
  1327. unk->data = 0;
  1328. unk->len = 0;
  1329. }
  1330. return 0;
  1331. }
  1332. static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
  1333. {
  1334. /* Eliminate compiler warning about unused variables. */
  1335. cstate = 0;
  1336. ms = 0;
  1337. out = 0;
  1338. /* If this function is called, we are trying to write an unsupported
  1339. type of marker segment. Return with an error indication. */
  1340. return -1;
  1341. }
  1342. static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out)
  1343. {
  1344. unsigned int i;
  1345. jpc_unk_t *unk = &ms->parms.unk;
  1346. for (i = 0; i < unk->len; ++i) {
  1347. fprintf(out, "%02x ", unk->data[i]);
  1348. }
  1349. return 0;
  1350. }
  1351. /******************************************************************************\
  1352. * Primitive I/O operations.
  1353. \******************************************************************************/
  1354. int jpc_getuint8(jas_stream_t *in, uint_fast8_t *val)
  1355. {
  1356. int c;
  1357. if ((c = jas_stream_getc(in)) == EOF) {
  1358. return -1;
  1359. }
  1360. if (val) {
  1361. *val = c;
  1362. }
  1363. return 0;
  1364. }
  1365. int jpc_putuint8(jas_stream_t *out, uint_fast8_t val)
  1366. {
  1367. if (jas_stream_putc(out, val & 0xff) == EOF) {
  1368. return -1;
  1369. }
  1370. return 0;
  1371. }
  1372. int jpc_getuint16(jas_stream_t *in, uint_fast16_t *val)
  1373. {
  1374. uint_fast16_t v;
  1375. int c;
  1376. if ((c = jas_stream_getc(in)) == EOF) {
  1377. return -1;
  1378. }
  1379. v = c;
  1380. if ((c = jas_stream_getc(in)) == EOF) {
  1381. return -1;
  1382. }
  1383. v = (v << 8) | c;
  1384. if (val) {
  1385. *val = v;
  1386. }
  1387. return 0;
  1388. }
  1389. int jpc_putuint16(jas_stream_t *out, uint_fast16_t val)
  1390. {
  1391. if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
  1392. jas_stream_putc(out, val & 0xff) == EOF) {
  1393. return -1;
  1394. }
  1395. return 0;
  1396. }
  1397. int jpc_getuint32(jas_stream_t *in, uint_fast32_t *val)
  1398. {
  1399. uint_fast32_t v;
  1400. int c;
  1401. if ((c = jas_stream_getc(in)) == EOF) {
  1402. return -1;
  1403. }
  1404. v = c;
  1405. if ((c = jas_stream_getc(in)) == EOF) {
  1406. return -1;
  1407. }
  1408. v = (v << 8) | c;
  1409. if ((c = jas_stream_getc(in)) == EOF) {
  1410. return -1;
  1411. }
  1412. v = (v << 8) | c;
  1413. if ((c = jas_stream_getc(in)) == EOF) {
  1414. return -1;
  1415. }
  1416. v = (v << 8) | c;
  1417. if (val) {
  1418. *val = v;
  1419. }
  1420. return 0;
  1421. }
  1422. int jpc_putuint32(jas_stream_t *out, uint_fast32_t val)
  1423. {
  1424. if (jas_stream_putc(out, (val >> 24) & 0xff) == EOF ||
  1425. jas_stream_putc(out, (val >> 16) & 0xff) == EOF ||
  1426. jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
  1427. jas_stream_putc(out, val & 0xff) == EOF) {
  1428. return -1;
  1429. }
  1430. return 0;
  1431. }
  1432. /******************************************************************************\
  1433. * Miscellany
  1434. \******************************************************************************/
  1435. static jpc_mstabent_t *jpc_mstab_lookup(int id)
  1436. {
  1437. jpc_mstabent_t *mstabent;
  1438. for (mstabent = jpc_mstab;; ++mstabent) {
  1439. if (mstabent->id == id || mstabent->id < 0) {
  1440. return mstabent;
  1441. }
  1442. }
  1443. assert(0);
  1444. return 0;
  1445. }
  1446. int jpc_validate(jas_stream_t *in)
  1447. {
  1448. int n;
  1449. int i;
  1450. unsigned char buf[2];
  1451. assert(JAS_STREAM_MAXPUTBACK >= 2);
  1452. if ((n = jas_stream_read(in, (char *) buf, 2)) < 0) {
  1453. return -1;
  1454. }
  1455. for (i = n - 1; i >= 0; --i) {
  1456. if (jas_stream_ungetc(in, buf[i]) == EOF) {
  1457. return -1;
  1458. }
  1459. }
  1460. if (n < 2) {
  1461. return -1;
  1462. }
  1463. if (buf[0] == (JPC_MS_SOC >> 8) && buf[1] == (JPC_MS_SOC & 0xff)) {
  1464. return 0;
  1465. }
  1466. return -1;
  1467. }
  1468. int jpc_getdata(jas_stream_t *in, jas_stream_t *out, long len)
  1469. {
  1470. return jas_stream_copy(out, in, len);
  1471. }
  1472. int jpc_putdata(jas_stream_t *out, jas_stream_t *in, long len)
  1473. {
  1474. return jas_stream_copy(out, in, len);
  1475. }