Debian Codegraph @

converter/other/jpeg2000/libjasper/jpc/jpc_cs.c

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