2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2002 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
12 * Copyright (c) 2001-2006 Michael David Adams
13 * Copyright (c) 1999-2000 Image Power, Inc.
14 * Copyright (c) 1999-2000 The University of British Columbia
16 * All rights reserved.
18 * Permission is hereby granted, free of charge, to any person (the
19 * "User") obtaining a copy of this software and associated documentation
20 * files (the "Software"), to deal in the Software without restriction,
21 * including without limitation the rights to use, copy, modify, merge,
22 * publish, distribute, and/or sell copies of the Software, and to permit
23 * persons to whom the Software is furnished to do so, subject to the
24 * following conditions:
26 * 1. The above copyright notices and this permission notice (which
27 * includes the disclaimer below) shall be included in all copies or
28 * substantial portions of the Software.
30 * 2. The name of a copyright holder shall not be used to endorse or
31 * promote products derived from the Software without specific prior
34 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35 * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36 * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37 * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39 * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
40 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
45 * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46 * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47 * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48 * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49 * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
50 * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51 * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
52 * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53 * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55 * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56 * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57 * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58 * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59 * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
61 * __END_OF_JASPER_LICENSE__
65 * JPEG-2000 Code Stream Library
67 * $Id: jpc_cs.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
70 /******************************************************************************\
72 \******************************************************************************/
78 #include "jasper/jas_malloc.h"
79 #include "jasper/jas_debug.h"
83 /******************************************************************************\
85 \******************************************************************************/
87 /* Marker segment table entry. */
94 /******************************************************************************\
96 \******************************************************************************/
98 static jpc_mstabent_t *jpc_mstab_lookup(int id);
100 static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out);
101 static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
102 static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
103 static void jpc_poc_destroyparms(jpc_ms_t *ms);
105 static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
106 static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
107 static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
108 static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
109 static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
110 static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
111 static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
112 static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
113 static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
114 static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
115 static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
116 static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
117 static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
119 static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
120 static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
121 static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
122 static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
123 static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
124 static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
125 static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
126 static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
127 static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
128 static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
129 static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
130 static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
131 static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
133 static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out);
134 static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out);
135 static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out);
136 static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out);
137 static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out);
138 static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out);
139 static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out);
140 static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out);
141 static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out);
142 static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out);
143 static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out);
144 static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out);
145 static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out);
147 static void jpc_siz_destroyparms(jpc_ms_t *ms);
148 static void jpc_qcd_destroyparms(jpc_ms_t *ms);
149 static void jpc_qcc_destroyparms(jpc_ms_t *ms);
150 static void jpc_cod_destroyparms(jpc_ms_t *ms);
151 static void jpc_coc_destroyparms(jpc_ms_t *ms);
152 static void jpc_unk_destroyparms(jpc_ms_t *ms);
153 static void jpc_ppm_destroyparms(jpc_ms_t *ms);
154 static void jpc_ppt_destroyparms(jpc_ms_t *ms);
155 static void jpc_crg_destroyparms(jpc_ms_t *ms);
156 static void jpc_com_destroyparms(jpc_ms_t *ms);
158 static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms);
159 static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
160 jas_stream_t *in, uint_fast16_t len);
161 static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
163 static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms);
164 static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
165 jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms);
166 static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
167 jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms);
169 /******************************************************************************\
171 \******************************************************************************/
173 static jpc_mstabent_t jpc_mstab[] = {
174 {JPC_MS_SOC, "SOC", {0, 0, 0, 0}},
175 {JPC_MS_SOT, "SOT", {0, jpc_sot_getparms, jpc_sot_putparms,
177 {JPC_MS_SOD, "SOD", {0, 0, 0, 0}},
178 {JPC_MS_EOC, "EOC", {0, 0, 0, 0}},
179 {JPC_MS_SIZ, "SIZ", {jpc_siz_destroyparms, jpc_siz_getparms,
180 jpc_siz_putparms, jpc_siz_dumpparms}},
181 {JPC_MS_COD, "COD", {jpc_cod_destroyparms, jpc_cod_getparms,
182 jpc_cod_putparms, jpc_cod_dumpparms}},
183 {JPC_MS_COC, "COC", {jpc_coc_destroyparms, jpc_coc_getparms,
184 jpc_coc_putparms, jpc_coc_dumpparms}},
185 {JPC_MS_RGN, "RGN", {0, jpc_rgn_getparms, jpc_rgn_putparms,
187 {JPC_MS_QCD, "QCD", {jpc_qcd_destroyparms, jpc_qcd_getparms,
188 jpc_qcd_putparms, jpc_qcd_dumpparms}},
189 {JPC_MS_QCC, "QCC", {jpc_qcc_destroyparms, jpc_qcc_getparms,
190 jpc_qcc_putparms, jpc_qcc_dumpparms}},
191 {JPC_MS_POC, "POC", {jpc_poc_destroyparms, jpc_poc_getparms,
192 jpc_poc_putparms, jpc_poc_dumpparms}},
193 {JPC_MS_TLM, "TLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}},
194 {JPC_MS_PLM, "PLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}},
195 {JPC_MS_PPM, "PPM", {jpc_ppm_destroyparms, jpc_ppm_getparms,
196 jpc_ppm_putparms, jpc_ppm_dumpparms}},
197 {JPC_MS_PPT, "PPT", {jpc_ppt_destroyparms, jpc_ppt_getparms,
198 jpc_ppt_putparms, jpc_ppt_dumpparms}},
199 {JPC_MS_SOP, "SOP", {0, jpc_sop_getparms, jpc_sop_putparms,
201 {JPC_MS_EPH, "EPH", {0, 0, 0, 0}},
202 {JPC_MS_CRG, "CRG", {0, jpc_crg_getparms, jpc_crg_putparms,
204 {JPC_MS_COM, "COM", {jpc_com_destroyparms, jpc_com_getparms,
205 jpc_com_putparms, jpc_com_dumpparms}},
206 {-1, "UNKNOWN", {jpc_unk_destroyparms, jpc_unk_getparms,
207 jpc_unk_putparms, jpc_unk_dumpparms}}
210 /******************************************************************************\
211 * Code stream manipulation functions.
212 \******************************************************************************/
214 /* Create a code stream state object. */
215 jpc_cstate_t *jpc_cstate_create()
217 jpc_cstate_t *cstate;
218 if (!(cstate = jas_malloc(sizeof(jpc_cstate_t)))) {
221 cstate->numcomps = 0;
225 /* Destroy a code stream state object. */
226 void jpc_cstate_destroy(jpc_cstate_t *cstate)
231 /* Read a marker segment from a stream. */
232 jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate)
235 jpc_mstabent_t *mstabent;
236 jas_stream_t *tmpstream;
238 if (!(ms = jpc_ms_create(0))) {
242 /* Get the marker type. */
243 if (jpc_getuint16(in, &ms->id) || ms->id < JPC_MS_MIN ||
244 ms->id > JPC_MS_MAX) {
249 mstabent = jpc_mstab_lookup(ms->id);
250 ms->ops = &mstabent->ops;
252 /* Get the marker segment length and parameters if present. */
253 /* Note: It is tacitly assumed that a marker segment cannot have
254 parameters unless it has a length field. That is, there cannot
255 be a parameters field without a length field and vice versa. */
256 if (JPC_MS_HASPARMS(ms->id)) {
257 /* Get the length of the marker segment. */
258 if (jpc_getuint16(in, &ms->len) || ms->len < 3) {
262 /* Calculate the length of the marker segment parameters. */
264 /* Create and prepare a temporary memory stream from which to
265 read the marker segment parameters. */
266 /* Note: This approach provides a simple way of ensuring that
267 we never read beyond the end of the marker segment (even if
268 the marker segment length is errantly set too small). */
269 if (!(tmpstream = jas_stream_memopen(0, 0))) {
273 if (jas_stream_copy(tmpstream, in, ms->len) ||
274 jas_stream_seek(tmpstream, 0, SEEK_SET) < 0) {
275 jas_stream_close(tmpstream);
279 /* Get the marker segment parameters. */
280 if ((*ms->ops->getparms)(ms, cstate, tmpstream)) {
283 jas_stream_close(tmpstream);
287 if (jas_getdbglevel() > 0) {
288 jpc_ms_dump(ms, stderr);
291 if (JAS_CAST(ulong, jas_stream_tell(tmpstream)) != ms->len) {
292 jas_eprintf("warning: trailing garbage in marker segment (%ld bytes)\n",
293 ms->len - jas_stream_tell(tmpstream));
296 /* Close the temporary stream. */
297 jas_stream_close(tmpstream);
300 /* There are no marker segment parameters. */
303 if (jas_getdbglevel() > 0) {
304 jpc_ms_dump(ms, stderr);
308 /* Update the code stream state information based on the type of
309 marker segment read. */
310 /* Note: This is a bit of a hack, but I'm not going to define another
311 type of virtual function for this one special case. */
312 if (ms->id == JPC_MS_SIZ) {
313 cstate->numcomps = ms->parms.siz.numcomps;
319 /* Write a marker segment to a stream. */
320 int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms)
322 jas_stream_t *tmpstream;
325 /* Output the marker segment type. */
326 if (jpc_putuint16(out, ms->id)) {
330 /* Output the marker segment length and parameters if necessary. */
331 if (ms->ops->putparms) {
332 /* Create a temporary stream in which to buffer the
334 if (!(tmpstream = jas_stream_memopen(0, 0))) {
337 if ((*ms->ops->putparms)(ms, cstate, tmpstream)) {
338 jas_stream_close(tmpstream);
341 /* Get the number of bytes of parameter data written. */
342 if ((len = jas_stream_tell(tmpstream)) < 0) {
343 jas_stream_close(tmpstream);
347 /* Write the marker segment length and parameter data to
348 the output stream. */
349 if (jas_stream_seek(tmpstream, 0, SEEK_SET) < 0 ||
350 jpc_putuint16(out, ms->len + 2) ||
351 jas_stream_copy(out, tmpstream, ms->len) < 0) {
352 jas_stream_close(tmpstream);
355 /* Close the temporary stream. */
356 jas_stream_close(tmpstream);
359 /* This is a bit of a hack, but I'm not going to define another
360 type of virtual function for this one special case. */
361 if (ms->id == JPC_MS_SIZ) {
362 cstate->numcomps = ms->parms.siz.numcomps;
365 if (jas_getdbglevel() > 0) {
366 jpc_ms_dump(ms, stderr);
372 /******************************************************************************\
373 * Marker segment operations.
374 \******************************************************************************/
376 /* Create a marker segment of the specified type. */
377 jpc_ms_t *jpc_ms_create(int type)
380 jpc_mstabent_t *mstabent;
382 if (!(ms = jas_malloc(sizeof(jpc_ms_t)))) {
387 mstabent = jpc_mstab_lookup(ms->id);
388 ms->ops = &mstabent->ops;
389 memset(&ms->parms, 0, sizeof(jpc_msparms_t));
393 /* Destroy a marker segment. */
394 void jpc_ms_destroy(jpc_ms_t *ms)
396 if (ms->ops && ms->ops->destroyparms) {
397 (*ms->ops->destroyparms)(ms);
402 /* Dump a marker segment to a stream for debugging. */
403 void jpc_ms_dump(jpc_ms_t *ms, FILE *out)
405 jpc_mstabent_t *mstabent;
406 mstabent = jpc_mstab_lookup(ms->id);
407 fprintf(out, "type = 0x%04x (%s);", ms->id, mstabent->name);
408 if (JPC_MS_HASPARMS(ms->id)) {
409 fprintf(out, " len = %d;", ms->len + 2);
410 if (ms->ops->dumpparms) {
411 (*ms->ops->dumpparms)(ms, out);
420 /******************************************************************************\
421 * SOT marker segment operations.
422 \******************************************************************************/
424 static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
426 jpc_sot_t *sot = &ms->parms.sot;
428 /* Eliminate compiler warning about unused variables. */
431 if (jpc_getuint16(in, &sot->tileno) ||
432 jpc_getuint32(in, &sot->len) ||
433 jpc_getuint8(in, &sot->partno) ||
434 jpc_getuint8(in, &sot->numparts)) {
437 if (jas_stream_eof(in)) {
443 static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
445 jpc_sot_t *sot = &ms->parms.sot;
447 /* Eliminate compiler warning about unused variables. */
450 if (jpc_putuint16(out, sot->tileno) ||
451 jpc_putuint32(out, sot->len) ||
452 jpc_putuint8(out, sot->partno) ||
453 jpc_putuint8(out, sot->numparts)) {
459 static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out)
461 jpc_sot_t *sot = &ms->parms.sot;
462 fprintf(out, "tileno = %d; len = %d; partno = %d; numparts = %d\n",
463 sot->tileno, sot->len, sot->partno, sot->numparts);
467 /******************************************************************************\
468 * SIZ marker segment operations.
469 \******************************************************************************/
471 static void jpc_siz_destroyparms(jpc_ms_t *ms)
473 jpc_siz_t *siz = &ms->parms.siz;
475 jas_free(siz->comps);
479 static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
482 jpc_siz_t *siz = &ms->parms.siz;
486 /* Eliminate compiler warning about unused variables. */
489 if (jpc_getuint16(in, &siz->caps) ||
490 jpc_getuint32(in, &siz->width) ||
491 jpc_getuint32(in, &siz->height) ||
492 jpc_getuint32(in, &siz->xoff) ||
493 jpc_getuint32(in, &siz->yoff) ||
494 jpc_getuint32(in, &siz->tilewidth) ||
495 jpc_getuint32(in, &siz->tileheight) ||
496 jpc_getuint32(in, &siz->tilexoff) ||
497 jpc_getuint32(in, &siz->tileyoff) ||
498 jpc_getuint16(in, &siz->numcomps)) {
501 if (!siz->width || !siz->height || !siz->tilewidth ||
502 !siz->tileheight || !siz->numcomps) {
505 if (!(siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t)))) {
508 for (i = 0; i < siz->numcomps; ++i) {
509 if (jpc_getuint8(in, &tmp) ||
510 jpc_getuint8(in, &siz->comps[i].hsamp) ||
511 jpc_getuint8(in, &siz->comps[i].vsamp)) {
512 jas_free(siz->comps);
515 siz->comps[i].sgnd = (tmp >> 7) & 1;
516 siz->comps[i].prec = (tmp & 0x7f) + 1;
518 if (jas_stream_eof(in)) {
519 jas_free(siz->comps);
525 static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
527 jpc_siz_t *siz = &ms->parms.siz;
530 /* Eliminate compiler warning about unused variables. */
533 assert(siz->width && siz->height && siz->tilewidth &&
534 siz->tileheight && siz->numcomps);
535 if (jpc_putuint16(out, siz->caps) ||
536 jpc_putuint32(out, siz->width) ||
537 jpc_putuint32(out, siz->height) ||
538 jpc_putuint32(out, siz->xoff) ||
539 jpc_putuint32(out, siz->yoff) ||
540 jpc_putuint32(out, siz->tilewidth) ||
541 jpc_putuint32(out, siz->tileheight) ||
542 jpc_putuint32(out, siz->tilexoff) ||
543 jpc_putuint32(out, siz->tileyoff) ||
544 jpc_putuint16(out, siz->numcomps)) {
547 for (i = 0; i < siz->numcomps; ++i) {
548 if (jpc_putuint8(out, ((siz->comps[i].sgnd & 1) << 7) |
549 ((siz->comps[i].prec - 1) & 0x7f)) ||
550 jpc_putuint8(out, siz->comps[i].hsamp) ||
551 jpc_putuint8(out, siz->comps[i].vsamp)) {
558 static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out)
560 jpc_siz_t *siz = &ms->parms.siz;
562 fprintf(out, "caps = 0x%02x;\n", siz->caps);
563 fprintf(out, "width = %d; height = %d; xoff = %d; yoff = %d;\n",
564 siz->width, siz->height, siz->xoff, siz->yoff);
565 fprintf(out, "tilewidth = %d; tileheight = %d; tilexoff = %d; "
566 "tileyoff = %d;\n", siz->tilewidth, siz->tileheight, siz->tilexoff,
568 for (i = 0; i < siz->numcomps; ++i) {
569 fprintf(out, "prec[%d] = %d; sgnd[%d] = %d; hsamp[%d] = %d; "
570 "vsamp[%d] = %d\n", i, siz->comps[i].prec, i,
571 siz->comps[i].sgnd, i, siz->comps[i].hsamp, i,
572 siz->comps[i].vsamp);
577 /******************************************************************************\
578 * COD marker segment operations.
579 \******************************************************************************/
581 static void jpc_cod_destroyparms(jpc_ms_t *ms)
583 jpc_cod_t *cod = &ms->parms.cod;
584 jpc_cox_destroycompparms(&cod->compparms);
587 static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
589 jpc_cod_t *cod = &ms->parms.cod;
590 if (jpc_getuint8(in, &cod->csty)) {
593 if (jpc_getuint8(in, &cod->prg) ||
594 jpc_getuint16(in, &cod->numlyrs) ||
595 jpc_getuint8(in, &cod->mctrans)) {
598 if (jpc_cox_getcompparms(ms, cstate, in,
599 (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) {
602 if (jas_stream_eof(in)) {
603 jpc_cod_destroyparms(ms);
609 static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
611 jpc_cod_t *cod = &ms->parms.cod;
612 assert(cod->numlyrs > 0 && cod->compparms.numdlvls <= 32);
613 assert(cod->compparms.numdlvls == cod->compparms.numrlvls - 1);
614 if (jpc_putuint8(out, cod->compparms.csty) ||
615 jpc_putuint8(out, cod->prg) ||
616 jpc_putuint16(out, cod->numlyrs) ||
617 jpc_putuint8(out, cod->mctrans)) {
620 if (jpc_cox_putcompparms(ms, cstate, out,
621 (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) {
627 static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out)
629 jpc_cod_t *cod = &ms->parms.cod;
631 fprintf(out, "csty = 0x%02x;\n", cod->compparms.csty);
632 fprintf(out, "numdlvls = %d; qmfbid = %d; mctrans = %d\n",
633 cod->compparms.numdlvls, cod->compparms.qmfbid, cod->mctrans);
634 fprintf(out, "prg = %d; numlyrs = %d;\n",
635 cod->prg, cod->numlyrs);
636 fprintf(out, "cblkwidthval = %d; cblkheightval = %d; "
637 "cblksty = 0x%02x;\n", cod->compparms.cblkwidthval, cod->compparms.cblkheightval,
638 cod->compparms.cblksty);
639 if (cod->csty & JPC_COX_PRT) {
640 for (i = 0; i < cod->compparms.numrlvls; ++i) {
641 jas_eprintf("prcwidth[%d] = %d, prcheight[%d] = %d\n",
642 i, cod->compparms.rlvls[i].parwidthval,
643 i, cod->compparms.rlvls[i].parheightval);
649 /******************************************************************************\
650 * COC marker segment operations.
651 \******************************************************************************/
653 static void jpc_coc_destroyparms(jpc_ms_t *ms)
655 jpc_coc_t *coc = &ms->parms.coc;
656 jpc_cox_destroycompparms(&coc->compparms);
659 static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
661 jpc_coc_t *coc = &ms->parms.coc;
663 if (cstate->numcomps <= 256) {
664 if (jpc_getuint8(in, &tmp)) {
669 if (jpc_getuint16(in, &coc->compno)) {
673 if (jpc_getuint8(in, &coc->compparms.csty)) {
676 if (jpc_cox_getcompparms(ms, cstate, in,
677 (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) {
680 if (jas_stream_eof(in)) {
686 static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
688 jpc_coc_t *coc = &ms->parms.coc;
689 assert(coc->compparms.numdlvls <= 32);
690 if (cstate->numcomps <= 256) {
691 if (jpc_putuint8(out, coc->compno)) {
695 if (jpc_putuint16(out, coc->compno)) {
699 if (jpc_putuint8(out, coc->compparms.csty)) {
702 if (jpc_cox_putcompparms(ms, cstate, out,
703 (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) {
709 static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out)
711 jpc_coc_t *coc = &ms->parms.coc;
712 fprintf(out, "compno = %d; csty = 0x%02x; numdlvls = %d;\n",
713 coc->compno, coc->compparms.csty, coc->compparms.numdlvls);
714 fprintf(out, "cblkwidthval = %d; cblkheightval = %d; "
715 "cblksty = 0x%02x; qmfbid = %d;\n", coc->compparms.cblkwidthval,
716 coc->compparms.cblkheightval, coc->compparms.cblksty, coc->compparms.qmfbid);
719 /******************************************************************************\
720 * COD/COC marker segment operation helper functions.
721 \******************************************************************************/
723 static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms)
725 /* Eliminate compiler warning about unused variables. */
729 static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
730 jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms)
735 /* Eliminate compiler warning about unused variables. */
739 if (jpc_getuint8(in, &compparms->numdlvls) ||
740 jpc_getuint8(in, &compparms->cblkwidthval) ||
741 jpc_getuint8(in, &compparms->cblkheightval) ||
742 jpc_getuint8(in, &compparms->cblksty) ||
743 jpc_getuint8(in, &compparms->qmfbid)) {
746 compparms->numrlvls = compparms->numdlvls + 1;
748 for (i = 0; i < compparms->numrlvls; ++i) {
749 if (jpc_getuint8(in, &tmp)) {
750 jpc_cox_destroycompparms(compparms);
753 compparms->rlvls[i].parwidthval = tmp & 0xf;
754 compparms->rlvls[i].parheightval = (tmp >> 4) & 0xf;
756 /* Sigh. This bit should be in the same field in both COC and COD mrk segs. */
757 compparms->csty |= JPC_COX_PRT;
760 if (jas_stream_eof(in)) {
761 jpc_cox_destroycompparms(compparms);
767 static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
768 jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms)
771 assert(compparms->numdlvls <= 32);
773 /* Eliminate compiler warning about unused variables. */
777 if (jpc_putuint8(out, compparms->numdlvls) ||
778 jpc_putuint8(out, compparms->cblkwidthval) ||
779 jpc_putuint8(out, compparms->cblkheightval) ||
780 jpc_putuint8(out, compparms->cblksty) ||
781 jpc_putuint8(out, compparms->qmfbid)) {
785 for (i = 0; i < compparms->numrlvls; ++i) {
786 if (jpc_putuint8(out,
787 ((compparms->rlvls[i].parheightval & 0xf) << 4) |
788 (compparms->rlvls[i].parwidthval & 0xf))) {
796 /******************************************************************************\
797 * RGN marker segment operations.
798 \******************************************************************************/
800 static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
802 jpc_rgn_t *rgn = &ms->parms.rgn;
804 if (cstate->numcomps <= 256) {
805 if (jpc_getuint8(in, &tmp)) {
810 if (jpc_getuint16(in, &rgn->compno)) {
814 if (jpc_getuint8(in, &rgn->roisty) ||
815 jpc_getuint8(in, &rgn->roishift)) {
821 static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
823 jpc_rgn_t *rgn = &ms->parms.rgn;
824 if (cstate->numcomps <= 256) {
825 if (jpc_putuint8(out, rgn->compno)) {
829 if (jpc_putuint16(out, rgn->compno)) {
833 if (jpc_putuint8(out, rgn->roisty) ||
834 jpc_putuint8(out, rgn->roishift)) {
840 static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out)
842 jpc_rgn_t *rgn = &ms->parms.rgn;
843 fprintf(out, "compno = %d; roisty = %d; roishift = %d\n",
844 rgn->compno, rgn->roisty, rgn->roishift);
848 /******************************************************************************\
849 * QCD marker segment operations.
850 \******************************************************************************/
852 static void jpc_qcd_destroyparms(jpc_ms_t *ms)
854 jpc_qcd_t *qcd = &ms->parms.qcd;
855 jpc_qcx_destroycompparms(&qcd->compparms);
858 static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
860 jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms;
861 return jpc_qcx_getcompparms(compparms, cstate, in, ms->len);
864 static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
866 jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms;
867 return jpc_qcx_putcompparms(compparms, cstate, out);
870 static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out)
872 jpc_qcd_t *qcd = &ms->parms.qcd;
874 fprintf(out, "qntsty = %d; numguard = %d; numstepsizes = %d\n",
875 (int) qcd->compparms.qntsty, qcd->compparms.numguard, qcd->compparms.numstepsizes);
876 for (i = 0; i < qcd->compparms.numstepsizes; ++i) {
877 fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n",
878 i, (unsigned) JPC_QCX_GETEXPN(qcd->compparms.stepsizes[i]),
879 i, (unsigned) JPC_QCX_GETMANT(qcd->compparms.stepsizes[i]));
884 /******************************************************************************\
885 * QCC marker segment operations.
886 \******************************************************************************/
888 static void jpc_qcc_destroyparms(jpc_ms_t *ms)
890 jpc_qcc_t *qcc = &ms->parms.qcc;
891 jpc_qcx_destroycompparms(&qcc->compparms);
894 static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
896 jpc_qcc_t *qcc = &ms->parms.qcc;
900 if (cstate->numcomps <= 256) {
901 jpc_getuint8(in, &tmp);
905 jpc_getuint16(in, &qcc->compno);
908 if (jpc_qcx_getcompparms(&qcc->compparms, cstate, in, len)) {
911 if (jas_stream_eof(in)) {
912 jpc_qcc_destroyparms(ms);
918 static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
920 jpc_qcc_t *qcc = &ms->parms.qcc;
921 if (cstate->numcomps <= 256) {
922 jpc_putuint8(out, qcc->compno);
924 jpc_putuint16(out, qcc->compno);
926 if (jpc_qcx_putcompparms(&qcc->compparms, cstate, out)) {
932 static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out)
934 jpc_qcc_t *qcc = &ms->parms.qcc;
936 fprintf(out, "compno = %d; qntsty = %d; numguard = %d; "
937 "numstepsizes = %d\n", qcc->compno, qcc->compparms.qntsty, qcc->compparms.numguard,
938 qcc->compparms.numstepsizes);
939 for (i = 0; i < qcc->compparms.numstepsizes; ++i) {
940 fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n",
941 i, (unsigned) JPC_QCX_GETEXPN(qcc->compparms.stepsizes[i]),
942 i, (unsigned) JPC_QCX_GETMANT(qcc->compparms.stepsizes[i]));
947 /******************************************************************************\
948 * QCD/QCC marker segment helper functions.
949 \******************************************************************************/
951 static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms)
953 if (compparms->stepsizes) {
954 jas_free(compparms->stepsizes);
958 static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
959 jas_stream_t *in, uint_fast16_t len)
965 /* Eliminate compiler warning about unused variables. */
969 jpc_getuint8(in, &tmp);
971 compparms->qntsty = tmp & 0x1f;
972 compparms->numguard = (tmp >> 5) & 7;
973 switch (compparms->qntsty) {
975 compparms->numstepsizes = 1;
978 compparms->numstepsizes = (len - n);
981 /* XXX - this is a hack */
982 compparms->numstepsizes = (len - n) / 2;
985 if (compparms->numstepsizes > 0) {
986 compparms->stepsizes = jas_malloc(compparms->numstepsizes *
987 sizeof(uint_fast16_t));
988 assert(compparms->stepsizes);
989 for (i = 0; i < compparms->numstepsizes; ++i) {
990 if (compparms->qntsty == JPC_QCX_NOQNT) {
991 jpc_getuint8(in, &tmp);
992 compparms->stepsizes[i] = JPC_QCX_EXPN(tmp >> 3);
994 jpc_getuint16(in, &compparms->stepsizes[i]);
998 compparms->stepsizes = 0;
1000 if (jas_stream_error(in) || jas_stream_eof(in)) {
1001 jpc_qcx_destroycompparms(compparms);
1007 static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
1012 /* Eliminate compiler warning about unused variables. */
1015 jpc_putuint8(out, ((compparms->numguard & 7) << 5) | compparms->qntsty);
1016 for (i = 0; i < compparms->numstepsizes; ++i) {
1017 if (compparms->qntsty == JPC_QCX_NOQNT) {
1018 jpc_putuint8(out, JPC_QCX_GETEXPN(
1019 compparms->stepsizes[i]) << 3);
1021 jpc_putuint16(out, compparms->stepsizes[i]);
1027 /******************************************************************************\
1028 * SOP marker segment operations.
1029 \******************************************************************************/
1031 static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
1033 jpc_sop_t *sop = &ms->parms.sop;
1035 /* Eliminate compiler warning about unused variable. */
1038 if (jpc_getuint16(in, &sop->seqno)) {
1044 static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
1046 jpc_sop_t *sop = &ms->parms.sop;
1048 /* Eliminate compiler warning about unused variable. */
1051 if (jpc_putuint16(out, sop->seqno)) {
1057 static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out)
1059 jpc_sop_t *sop = &ms->parms.sop;
1060 fprintf(out, "seqno = %d;\n", sop->seqno);
1064 /******************************************************************************\
1065 * PPM marker segment operations.
1066 \******************************************************************************/
1068 static void jpc_ppm_destroyparms(jpc_ms_t *ms)
1070 jpc_ppm_t *ppm = &ms->parms.ppm;
1072 jas_free(ppm->data);
1076 static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
1078 jpc_ppm_t *ppm = &ms->parms.ppm;
1080 /* Eliminate compiler warning about unused variables. */
1088 if (jpc_getuint8(in, &ppm->ind)) {
1092 ppm->len = ms->len - 1;
1094 if (!(ppm->data = jas_malloc(ppm->len * sizeof(unsigned char)))) {
1097 if (JAS_CAST(uint, jas_stream_read(in, ppm->data, ppm->len)) != ppm->len) {
1106 jpc_ppm_destroyparms(ms);
1110 static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
1112 jpc_ppm_t *ppm = &ms->parms.ppm;
1114 /* Eliminate compiler warning about unused variables. */
1117 if (JAS_CAST(uint, jas_stream_write(out, (char *) ppm->data, ppm->len)) != ppm->len) {
1123 static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out)
1125 jpc_ppm_t *ppm = &ms->parms.ppm;
1126 fprintf(out, "ind=%d; len = %d;\n", ppm->ind, ppm->len);
1128 fprintf(out, "data =\n");
1129 jas_memdump(out, ppm->data, ppm->len);
1134 /******************************************************************************\
1135 * PPT marker segment operations.
1136 \******************************************************************************/
1138 static void jpc_ppt_destroyparms(jpc_ms_t *ms)
1140 jpc_ppt_t *ppt = &ms->parms.ppt;
1142 jas_free(ppt->data);
1146 static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
1148 jpc_ppt_t *ppt = &ms->parms.ppt;
1150 /* Eliminate compiler warning about unused variables. */
1158 if (jpc_getuint8(in, &ppt->ind)) {
1161 ppt->len = ms->len - 1;
1163 if (!(ppt->data = jas_malloc(ppt->len * sizeof(unsigned char)))) {
1166 if (jas_stream_read(in, (char *) ppt->data, ppt->len) != JAS_CAST(int, ppt->len)) {
1175 jpc_ppt_destroyparms(ms);
1179 static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
1181 jpc_ppt_t *ppt = &ms->parms.ppt;
1183 /* Eliminate compiler warning about unused variable. */
1186 if (jpc_putuint8(out, ppt->ind)) {
1189 if (jas_stream_write(out, (char *) ppt->data, ppt->len) != JAS_CAST(int, ppt->len)) {
1195 static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out)
1197 jpc_ppt_t *ppt = &ms->parms.ppt;
1198 fprintf(out, "ind=%d; len = %d;\n", ppt->ind, ppt->len);
1200 fprintf(out, "data =\n");
1201 jas_memdump(out, ppt->data, ppt->len);
1206 /******************************************************************************\
1207 * POC marker segment operations.
1208 \******************************************************************************/
1210 static void jpc_poc_destroyparms(jpc_ms_t *ms)
1212 jpc_poc_t *poc = &ms->parms.poc;
1214 jas_free(poc->pchgs);
1218 static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
1220 jpc_poc_t *poc = &ms->parms.poc;
1221 jpc_pocpchg_t *pchg;
1224 poc->numpchgs = (cstate->numcomps > 256) ? (ms->len / 9) :
1226 if (!(poc->pchgs = jas_malloc(poc->numpchgs * sizeof(jpc_pocpchg_t)))) {
1229 for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno,
1231 if (jpc_getuint8(in, &pchg->rlvlnostart)) {
1234 if (cstate->numcomps > 256) {
1235 if (jpc_getuint16(in, &pchg->compnostart)) {
1239 if (jpc_getuint8(in, &tmp)) {
1242 pchg->compnostart = tmp;
1244 if (jpc_getuint16(in, &pchg->lyrnoend) ||
1245 jpc_getuint8(in, &pchg->rlvlnoend)) {
1248 if (cstate->numcomps > 256) {
1249 if (jpc_getuint16(in, &pchg->compnoend)) {
1253 if (jpc_getuint8(in, &tmp)) {
1256 pchg->compnoend = tmp;
1258 if (jpc_getuint8(in, &pchg->prgord)) {
1261 if (pchg->rlvlnostart > pchg->rlvlnoend ||
1262 pchg->compnostart > pchg->compnoend) {
1269 jpc_poc_destroyparms(ms);
1273 static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
1275 jpc_poc_t *poc = &ms->parms.poc;
1276 jpc_pocpchg_t *pchg;
1278 for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno,
1280 if (jpc_putuint8(out, pchg->rlvlnostart) ||
1281 ((cstate->numcomps > 256) ?
1282 jpc_putuint16(out, pchg->compnostart) :
1283 jpc_putuint8(out, pchg->compnostart)) ||
1284 jpc_putuint16(out, pchg->lyrnoend) ||
1285 jpc_putuint8(out, pchg->rlvlnoend) ||
1286 ((cstate->numcomps > 256) ?
1287 jpc_putuint16(out, pchg->compnoend) :
1288 jpc_putuint8(out, pchg->compnoend)) ||
1289 jpc_putuint8(out, pchg->prgord)) {
1296 static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out)
1298 jpc_poc_t *poc = &ms->parms.poc;
1299 jpc_pocpchg_t *pchg;
1301 for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs;
1303 fprintf(out, "po[%d] = %d; ", pchgno, pchg->prgord);
1304 fprintf(out, "cs[%d] = %d; ce[%d] = %d; ",
1305 pchgno, pchg->compnostart, pchgno, pchg->compnoend);
1306 fprintf(out, "rs[%d] = %d; re[%d] = %d; ",
1307 pchgno, pchg->rlvlnostart, pchgno, pchg->rlvlnoend);
1308 fprintf(out, "le[%d] = %d\n", pchgno, pchg->lyrnoend);
1313 /******************************************************************************\
1314 * CRG marker segment operations.
1315 \******************************************************************************/
1317 static void jpc_crg_destroyparms(jpc_ms_t *ms)
1319 jpc_crg_t *crg = &ms->parms.crg;
1321 jas_free(crg->comps);
1325 static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
1327 jpc_crg_t *crg = &ms->parms.crg;
1328 jpc_crgcomp_t *comp;
1329 uint_fast16_t compno;
1330 crg->numcomps = cstate->numcomps;
1331 if (!(crg->comps = jas_malloc(cstate->numcomps * sizeof(uint_fast16_t)))) {
1334 for (compno = 0, comp = crg->comps; compno < cstate->numcomps;
1336 if (jpc_getuint16(in, &comp->hoff) ||
1337 jpc_getuint16(in, &comp->voff)) {
1338 jpc_crg_destroyparms(ms);
1345 static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
1347 jpc_crg_t *crg = &ms->parms.crg;
1349 jpc_crgcomp_t *comp;
1351 /* Eliminate compiler warning about unused variables. */
1354 for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno,
1356 if (jpc_putuint16(out, comp->hoff) ||
1357 jpc_putuint16(out, comp->voff)) {
1364 static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out)
1366 jpc_crg_t *crg = &ms->parms.crg;
1368 jpc_crgcomp_t *comp;
1369 for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno,
1371 fprintf(out, "hoff[%d] = %d; voff[%d] = %d\n", compno,
1372 comp->hoff, compno, comp->voff);
1377 /******************************************************************************\
1378 * Operations for COM marker segment.
1379 \******************************************************************************/
1381 static void jpc_com_destroyparms(jpc_ms_t *ms)
1383 jpc_com_t *com = &ms->parms.com;
1385 jas_free(com->data);
1389 static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
1391 jpc_com_t *com = &ms->parms.com;
1393 /* Eliminate compiler warning about unused variables. */
1396 if (jpc_getuint16(in, &com->regid)) {
1399 com->len = ms->len - 2;
1401 if (!(com->data = jas_malloc(com->len))) {
1404 if (jas_stream_read(in, com->data, com->len) != JAS_CAST(int, com->len)) {
1413 static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
1415 jpc_com_t *com = &ms->parms.com;
1417 /* Eliminate compiler warning about unused variables. */
1420 if (jpc_putuint16(out, com->regid)) {
1423 if (jas_stream_write(out, com->data, com->len) != JAS_CAST(int, com->len)) {
1429 static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out)
1431 jpc_com_t *com = &ms->parms.com;
1434 fprintf(out, "regid = %d;\n", com->regid);
1436 for (i = 0; i < com->len; ++i) {
1437 if (!isprint(com->data[i])) {
1443 fprintf(out, "data = ");
1444 fwrite(com->data, sizeof(char), com->len, out);
1450 /******************************************************************************\
1451 * Operations for unknown types of marker segments.
1452 \******************************************************************************/
1454 static void jpc_unk_destroyparms(jpc_ms_t *ms)
1456 jpc_unk_t *unk = &ms->parms.unk;
1458 jas_free(unk->data);
1462 static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
1464 jpc_unk_t *unk = &ms->parms.unk;
1466 /* Eliminate compiler warning about unused variables. */
1470 if (!(unk->data = jas_malloc(ms->len * sizeof(unsigned char)))) {
1473 if (jas_stream_read(in, (char *) unk->data, ms->len) != JAS_CAST(int, ms->len)) {
1474 jas_free(unk->data);
1485 static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
1487 /* Eliminate compiler warning about unused variables. */
1492 /* If this function is called, we are trying to write an unsupported
1493 type of marker segment. Return with an error indication. */
1497 static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out)
1500 jpc_unk_t *unk = &ms->parms.unk;
1501 for (i = 0; i < unk->len; ++i) {
1502 fprintf(out, "%02x ", unk->data[i]);
1507 /******************************************************************************\
1508 * Primitive I/O operations.
1509 \******************************************************************************/
1511 int jpc_getuint8(jas_stream_t *in, uint_fast8_t *val)
1514 if ((c = jas_stream_getc(in)) == EOF) {
1523 int jpc_putuint8(jas_stream_t *out, uint_fast8_t val)
1525 if (jas_stream_putc(out, val & 0xff) == EOF) {
1531 int jpc_getuint16(jas_stream_t *in, uint_fast16_t *val)
1535 if ((c = jas_stream_getc(in)) == EOF) {
1539 if ((c = jas_stream_getc(in)) == EOF) {
1549 int jpc_putuint16(jas_stream_t *out, uint_fast16_t val)
1551 if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
1552 jas_stream_putc(out, val & 0xff) == EOF) {
1558 int jpc_getuint32(jas_stream_t *in, uint_fast32_t *val)
1562 if ((c = jas_stream_getc(in)) == EOF) {
1566 if ((c = jas_stream_getc(in)) == EOF) {
1570 if ((c = jas_stream_getc(in)) == EOF) {
1574 if ((c = jas_stream_getc(in)) == EOF) {
1584 int jpc_putuint32(jas_stream_t *out, uint_fast32_t val)
1586 if (jas_stream_putc(out, (val >> 24) & 0xff) == EOF ||
1587 jas_stream_putc(out, (val >> 16) & 0xff) == EOF ||
1588 jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
1589 jas_stream_putc(out, val & 0xff) == EOF) {
1595 /******************************************************************************\
1597 \******************************************************************************/
1599 static jpc_mstabent_t *jpc_mstab_lookup(int id)
1601 jpc_mstabent_t *mstabent;
1602 for (mstabent = jpc_mstab;; ++mstabent) {
1603 if (mstabent->id == id || mstabent->id < 0) {
1611 int jpc_validate(jas_stream_t *in)
1615 unsigned char buf[2];
1617 assert(JAS_STREAM_MAXPUTBACK >= 2);
1619 if ((n = jas_stream_read(in, (char *) buf, 2)) < 0) {
1622 for (i = n - 1; i >= 0; --i) {
1623 if (jas_stream_ungetc(in, buf[i]) == EOF) {
1630 if (buf[0] == (JPC_MS_SOC >> 8) && buf[1] == (JPC_MS_SOC & 0xff)) {
1636 int jpc_getdata(jas_stream_t *in, jas_stream_t *out, long len)
1638 return jas_stream_copy(out, in, len);
1641 int jpc_putdata(jas_stream_t *out, jas_stream_t *in, long len)
1643 return jas_stream_copy(out, in, len);