about summary refs log tree commit diff
path: root/converter/other/jpeg2000/libjasper/jpc/jpc_cs.c
blob: 559f36cfce6a4647805be95ebd50db658b5a2d3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
/*
 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
 *   British Columbia.
 * Copyright (c) 2001-2002 Michael David Adams.
 * All rights reserved.
 */

/* __START_OF_JASPER_LICENSE__
 * 
 * JasPer Software License
 * 
 * IMAGE POWER JPEG-2000 PUBLIC LICENSE
 * ************************************
 * 
 * GRANT:
 * 
 * Permission is hereby granted, free of charge, to any person (the "User")
 * obtaining a copy of this software and associated documentation, to deal
 * in the JasPer Software without restriction, including without limitation
 * the right to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the JasPer Software (in source and binary forms),
 * and to permit persons to whom the JasPer Software is furnished to do so,
 * provided further that the License Conditions below are met.
 * 
 * License Conditions
 * ******************
 * 
 * A.  Redistributions of source code must retain the above copyright notice,
 * and this list of conditions, and the following disclaimer.
 * 
 * B.  Redistributions in binary form must reproduce the above copyright
 * notice, and this list of conditions, and the following disclaimer in
 * the documentation and/or other materials provided with the distribution.
 * 
 * C.  Neither the name of Image Power, Inc. nor any other contributor
 * (including, but not limited to, the University of British Columbia and
 * Michael David Adams) may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * 
 * D.  User agrees that it shall not commence any action against Image Power,
 * Inc., the University of British Columbia, Michael David Adams, or any
 * other contributors (collectively "Licensors") for infringement of any
 * intellectual property rights ("IPR") held by the User in respect of any
 * technology that User owns or has a right to license or sublicense and
 * which is an element required in order to claim compliance with ISO/IEC
 * 15444-1 (i.e., JPEG-2000 Part 1).  "IPR" means all intellectual property
 * rights worldwide arising under statutory or common law, and whether
 * or not perfected, including, without limitation, all (i) patents and
 * patent applications owned or licensable by User; (ii) rights associated
 * with works of authorship including copyrights, copyright applications,
 * copyright registrations, mask work rights, mask work applications,
 * mask work registrations; (iii) rights relating to the protection of
 * trade secrets and confidential information; (iv) any right analogous
 * to those set forth in subsections (i), (ii), or (iii) and any other
 * proprietary rights relating to intangible property (other than trademark,
 * trade dress, or service mark rights); and (v) divisions, continuations,
 * renewals, reissues and extensions of the foregoing (as and to the extent
 * applicable) now existing, hereafter filed, issued or acquired.
 * 
 * E.  If User commences an infringement action against any Licensor(s) then
 * such Licensor(s) shall have the right to terminate User's license and
 * all sublicenses that have been granted hereunder by User to other parties.
 * 
 * F.  This software is for use only in hardware or software products that
 * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1).  No license
 * or right to this Software is granted for products that do not comply
 * with ISO/IEC 15444-1.  The JPEG-2000 Part 1 standard can be purchased
 * from the ISO.
 * 
 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
 * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
 * THIS DISCLAIMER.  THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
 * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
 * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
 * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING.  THOSE INTENDING
 * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
 * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
 * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
 * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
 * IS WITH THE USER.  SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
 * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
 * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
 * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
 * REPAIR OR CORRECTION.  UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
 * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
 * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
 * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
 * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
 * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
 * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
 * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
 * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
 * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
 * OF SUCH DAMAGES.  THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
 * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
 * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
 * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
 * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
 * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
 * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
 * ("HIGH RISK ACTIVITIES").  LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
 * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.  USER WILL NOT
 * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
 * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
 * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
 * NOTICE SPECIFIED IN THIS SECTION.
 * 
 * __END_OF_JASPER_LICENSE__
 */

/*
 * JPEG-2000 Code Stream Library
 *
 * $Id$
 */

/******************************************************************************\
* Includes.
\******************************************************************************/

#include <stdlib.h>
#include <assert.h>
#include <ctype.h>

#include "jasper/jas_malloc.h"
#include "jasper/jas_debug.h"

#include "jpc_cs.h"

/******************************************************************************\
* Types.
\******************************************************************************/

/* Marker segment table entry. */
typedef struct {
    int id;
    const char *name;
    jpc_msops_t ops;
} jpc_mstabent_t;

/******************************************************************************\
* Local prototypes.
\******************************************************************************/

static jpc_mstabent_t *jpc_mstab_lookup(int id);

static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static void jpc_poc_destroyparms(jpc_ms_t *ms);

static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);

static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);

static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out);
static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out);

static void jpc_siz_destroyparms(jpc_ms_t *ms);
static void jpc_qcd_destroyparms(jpc_ms_t *ms);
static void jpc_qcc_destroyparms(jpc_ms_t *ms);
static void jpc_cod_destroyparms(jpc_ms_t *ms);
static void jpc_coc_destroyparms(jpc_ms_t *ms);
static void jpc_unk_destroyparms(jpc_ms_t *ms);
static void jpc_ppm_destroyparms(jpc_ms_t *ms);
static void jpc_ppt_destroyparms(jpc_ms_t *ms);
static void jpc_crg_destroyparms(jpc_ms_t *ms);
static void jpc_com_destroyparms(jpc_ms_t *ms);

static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms);
static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
  jas_stream_t *in, uint_fast16_t len);
static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
  jas_stream_t *out);
static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms);
static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
  jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms);
static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
  jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms);

/******************************************************************************\
* Global data.
\******************************************************************************/

static jpc_mstabent_t jpc_mstab[] = {
    {JPC_MS_SOC, "SOC", {0, 0, 0, 0}},
    {JPC_MS_SOT, "SOT", {0, jpc_sot_getparms, jpc_sot_putparms,
      jpc_sot_dumpparms}},
    {JPC_MS_SOD, "SOD", {0, 0, 0, 0}},
    {JPC_MS_EOC, "EOC", {0, 0, 0, 0}},
    {JPC_MS_SIZ, "SIZ", {jpc_siz_destroyparms, jpc_siz_getparms,
      jpc_siz_putparms, jpc_siz_dumpparms}},
    {JPC_MS_COD, "COD", {jpc_cod_destroyparms, jpc_cod_getparms,
      jpc_cod_putparms, jpc_cod_dumpparms}},
    {JPC_MS_COC, "COC", {jpc_coc_destroyparms, jpc_coc_getparms,
      jpc_coc_putparms, jpc_coc_dumpparms}},
    {JPC_MS_RGN, "RGN", {0, jpc_rgn_getparms, jpc_rgn_putparms,
      jpc_rgn_dumpparms}},
    {JPC_MS_QCD, "QCD", {jpc_qcd_destroyparms, jpc_qcd_getparms,
      jpc_qcd_putparms, jpc_qcd_dumpparms}},
    {JPC_MS_QCC, "QCC", {jpc_qcc_destroyparms, jpc_qcc_getparms,
      jpc_qcc_putparms, jpc_qcc_dumpparms}},
    {JPC_MS_POC, "POC", {jpc_poc_destroyparms, jpc_poc_getparms,
      jpc_poc_putparms, jpc_poc_dumpparms}},
    {JPC_MS_TLM, "TLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}},
    {JPC_MS_PLM, "PLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}},
    {JPC_MS_PPM, "PPM", {jpc_ppm_destroyparms, jpc_ppm_getparms,
      jpc_ppm_putparms, jpc_ppm_dumpparms}},
    {JPC_MS_PPT, "PPT", {jpc_ppt_destroyparms, jpc_ppt_getparms,
      jpc_ppt_putparms, jpc_ppt_dumpparms}},
    {JPC_MS_SOP, "SOP", {0, jpc_sop_getparms, jpc_sop_putparms,
      jpc_sop_dumpparms}},
    {JPC_MS_EPH, "EPH", {0, 0, 0, 0}},
    {JPC_MS_CRG, "CRG", {0, jpc_crg_getparms, jpc_crg_putparms,
      jpc_crg_dumpparms}},
    {JPC_MS_COM, "COM", {jpc_com_destroyparms, jpc_com_getparms,
      jpc_com_putparms, jpc_com_dumpparms}},
    {-1, "UNKNOWN",  {jpc_unk_destroyparms, jpc_unk_getparms,
      jpc_unk_putparms, jpc_unk_dumpparms}}
};

/******************************************************************************\
* Code stream manipulation functions.
\******************************************************************************/

/* Create a code stream state object. */
jpc_cstate_t *jpc_cstate_create()
{
    jpc_cstate_t *cstate;
    if (!(cstate = jas_malloc(sizeof(jpc_cstate_t)))) {
        return 0;
    }
    cstate->numcomps = 0;
    return cstate;
}

/* Destroy a code stream state object. */
void jpc_cstate_destroy(jpc_cstate_t *cstate)
{
    jas_free(cstate);
}

/* Read a marker segment from a stream. */
jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate)
{
    jpc_ms_t *ms;
    jpc_mstabent_t *mstabent;
    jas_stream_t *tmpstream;

    if (!(ms = jpc_ms_create(0))) {
        return 0;
    }

    /* Get the marker type. */
    if (jpc_getuint16(in, &ms->id) || ms->id < JPC_MS_MIN ||
      ms->id > JPC_MS_MAX) {
        jpc_ms_destroy(ms);
        return 0;
    }

    mstabent = jpc_mstab_lookup(ms->id);
    ms->ops = &mstabent->ops;

    /* Get the marker segment length and parameters if present. */
    /* Note: It is tacitly assumed that a marker segment cannot have
      parameters unless it has a length field.  That is, there cannot
      be a parameters field without a length field and vice versa. */
    if (JPC_MS_HASPARMS(ms->id)) {
        /* Get the length of the marker segment. */
        if (jpc_getuint16(in, &ms->len) || ms->len < 3) {
            jpc_ms_destroy(ms);
            return 0;
        }
        /* Calculate the length of the marker segment parameters. */
        ms->len -= 2;
        /* Create and prepare a temporary memory stream from which to
          read the marker segment parameters. */
        /* Note: This approach provides a simple way of ensuring that
          we never read beyond the end of the marker segment (even if
          the marker segment length is errantly set too small). */
        if (!(tmpstream = jas_stream_memopen(0, 0))) {
            jpc_ms_destroy(ms);
            return 0;
        }
        if (jas_stream_copy(tmpstream, in, ms->len) ||
          jas_stream_seek(tmpstream, 0, SEEK_SET) < 0) {
            jas_stream_close(tmpstream);
            jpc_ms_destroy(ms);
            return 0;
        }
        /* Get the marker segment parameters. */
        if ((*ms->ops->getparms)(ms, cstate, tmpstream)) {
            ms->ops = 0;
            jpc_ms_destroy(ms);
            jas_stream_close(tmpstream);
            return 0;
        }

        if (jas_getdbglevel() > 0) {
            jpc_ms_dump(ms, stderr);
        }

        if (jas_stream_tell(tmpstream) != ms->len) {
            fprintf(stderr,
              "warning: trailing garbage in marker segment (%ld bytes)\n",
              ms->len - jas_stream_tell(tmpstream));
        }

        /* Close the temporary stream. */
        jas_stream_close(tmpstream);

    } else {
        /* There are no marker segment parameters. */
        ms->len = 0;

        if (jas_getdbglevel() > 0) {
            jpc_ms_dump(ms, stderr);
        }
    }

    /* Update the code stream state information based on the type of
      marker segment read. */
    /* Note: This is a bit of a hack, but I'm not going to define another
      type of virtual function for this one special case. */
    if (ms->id == JPC_MS_SIZ) {
        cstate->numcomps = ms->parms.siz.numcomps;
    }

    return ms;
}

/* Write a marker segment to a stream. */
int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms)
{
    jas_stream_t *tmpstream;
    int len;

    /* Output the marker segment type. */
    if (jpc_putuint16(out, ms->id)) {
        return -1;
    }

    /* Output the marker segment length and parameters if necessary. */
    if (ms->ops->putparms) {
        /* Create a temporary stream in which to buffer the
          parameter data. */
        if (!(tmpstream = jas_stream_memopen(0, 0))) {
            return -1;
        }
        if ((*ms->ops->putparms)(ms, cstate, tmpstream)) {
            jas_stream_close(tmpstream);
            return -1;
        }
        /* Get the number of bytes of parameter data written. */
        if ((len = jas_stream_tell(tmpstream)) < 0) {
            jas_stream_close(tmpstream);
            return -1;
        }
        ms->len = len;
        /* Write the marker segment length and parameter data to
          the output stream. */
        if (jas_stream_seek(tmpstream, 0, SEEK_SET) < 0 ||
          jpc_putuint16(out, ms->len + 2) ||
          jas_stream_copy(out, tmpstream, ms->len) < 0) {
            jas_stream_close(tmpstream);
            return -1;
        }
        /* Close the temporary stream. */
        jas_stream_close(tmpstream);
    }

    /* This is a bit of a hack, but I'm not going to define another
      type of virtual function for this one special case. */
    if (ms->id == JPC_MS_SIZ) {
        cstate->numcomps = ms->parms.siz.numcomps;
    }

    if (jas_getdbglevel() > 0) {
        jpc_ms_dump(ms, stderr);
    }

    return 0;
}

/******************************************************************************\
* Marker segment operations.
\******************************************************************************/

/* Create a marker segment of the specified type. */
jpc_ms_t *jpc_ms_create(int type)
{
    jpc_ms_t *ms;
    jpc_mstabent_t *mstabent;

    if (!(ms = jas_malloc(sizeof(jpc_ms_t)))) {
        return 0;
    }
    ms->id = type;
    ms->len = 0;
    mstabent = jpc_mstab_lookup(ms->id);
    ms->ops = &mstabent->ops;
    memset(&ms->parms, 0, sizeof(jpc_msparms_t));
    return ms;
}

/* Destroy a marker segment. */
void jpc_ms_destroy(jpc_ms_t *ms)
{
    if (ms->ops && ms->ops->destroyparms) {
        (*ms->ops->destroyparms)(ms);
    }
    jas_free(ms);
}

/* Dump a marker segment to a stream for debugging. */
void jpc_ms_dump(jpc_ms_t *ms, FILE *out)
{
    jpc_mstabent_t *mstabent;
    mstabent = jpc_mstab_lookup(ms->id);
    fprintf(out, "type = 0x%04x (%s);",
            (unsigned int)ms->id, mstabent->name);
    if (JPC_MS_HASPARMS(ms->id)) {
        fprintf(out, " len = %d;", (int)ms->len + 2);
        if (ms->ops->dumpparms) {
            (*ms->ops->dumpparms)(ms, out);
        } else {
            fprintf(out, "\n");
        }
    } else {
        fprintf(out, "\n");
    }
}

/******************************************************************************\
* SOT marker segment operations.
\******************************************************************************/

static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_sot_t *sot = &ms->parms.sot;
    if (jpc_getuint16(in, &sot->tileno) ||
      jpc_getuint32(in, &sot->len) ||
      jpc_getuint8(in, &sot->partno) ||
      jpc_getuint8(in, &sot->numparts)) {
        return -1;
    }
    if (jas_stream_eof(in)) {
        return -1;
    }
    return 0;
}

static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_sot_t *sot = &ms->parms.sot;
    if (jpc_putuint16(out, sot->tileno) ||
      jpc_putuint32(out, sot->len) ||
      jpc_putuint8(out, sot->partno) ||
      jpc_putuint8(out, sot->numparts)) {
        return -1;
    }
    return 0;
}

static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_sot_t *sot = &ms->parms.sot;
    fprintf(out, "tileno = %d; len = %d; partno = %d; numparts = %d\n",
            (int)sot->tileno, (int)sot->len, sot->partno, sot->numparts);
    return 0;
}

/******************************************************************************\
* SIZ marker segment operations.
\******************************************************************************/

static void jpc_siz_destroyparms(jpc_ms_t *ms)
{
    jpc_siz_t *siz = &ms->parms.siz;
    if (siz->comps) {
        jas_free(siz->comps);
    }
}

static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_siz_t *siz = &ms->parms.siz;
    int i;
    uint_fast8_t tmp;
    if (jpc_getuint16(in, &siz->caps) ||
      jpc_getuint32(in, &siz->width) ||
      jpc_getuint32(in, &siz->height) ||
      jpc_getuint32(in, &siz->xoff) ||
      jpc_getuint32(in, &siz->yoff) ||
      jpc_getuint32(in, &siz->tilewidth) ||
      jpc_getuint32(in, &siz->tileheight) ||
      jpc_getuint32(in, &siz->tilexoff) ||
      jpc_getuint32(in, &siz->tileyoff) ||
      jpc_getuint16(in, &siz->numcomps)) {
        return -1;
    }
    if (!siz->width || !siz->height || !siz->tilewidth ||
      !siz->tileheight || !siz->numcomps) {
        return -1;
    }
    if (!(siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t)))) {
        return -1;
    }
    for (i = 0; i < siz->numcomps; ++i) {
        if (jpc_getuint8(in, &tmp) ||
          jpc_getuint8(in, &siz->comps[i].hsamp) ||
          jpc_getuint8(in, &siz->comps[i].vsamp)) {
            jas_free(siz->comps);
            return -1;
        }
        siz->comps[i].sgnd = (tmp >> 7) & 1;
        siz->comps[i].prec = (tmp & 0x7f) + 1;
    }
    if (jas_stream_eof(in)) {
        jas_free(siz->comps);
        return -1;
    }
    return 0;
}

static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_siz_t *siz = &ms->parms.siz;
    int i;
    assert(siz->width && siz->height && siz->tilewidth &&
      siz->tileheight && siz->numcomps);
    if (jpc_putuint16(out, siz->caps) ||
      jpc_putuint32(out, siz->width) ||
      jpc_putuint32(out, siz->height) ||
      jpc_putuint32(out, siz->xoff) ||
      jpc_putuint32(out, siz->yoff) ||
      jpc_putuint32(out, siz->tilewidth) ||
      jpc_putuint32(out, siz->tileheight) ||
      jpc_putuint32(out, siz->tilexoff) ||
      jpc_putuint32(out, siz->tileyoff) ||
      jpc_putuint16(out, siz->numcomps)) {
        return -1;
    }
    for (i = 0; i < siz->numcomps; ++i) {
        if (jpc_putuint8(out, ((siz->comps[i].sgnd & 1) << 7) |
          ((siz->comps[i].prec - 1) & 0x7f)) ||
          jpc_putuint8(out, siz->comps[i].hsamp) ||
          jpc_putuint8(out, siz->comps[i].vsamp)) {
            return -1;
        }
    }
    return 0;
}

static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_siz_t *siz = &ms->parms.siz;
    int i;
    fprintf(out, "caps = 0x%02x;\n", (unsigned int)siz->caps);
    fprintf(out, "width = %d; height = %d; xoff = %d; yoff = %d;\n",
            (int)siz->width, (int)siz->height, (int)siz->xoff, (int)siz->yoff);
    fprintf(out, "tilewidth = %d; tileheight = %d; tilexoff = %d; "
            "tileyoff = %d;\n",
            (int)siz->tilewidth, (int)siz->tileheight,
            (int)siz->tilexoff, (int)siz->tileyoff);
    for (i = 0; i < siz->numcomps; ++i) {
        fprintf(out, "prec[%d] = %d; sgnd[%d] = %d; hsamp[%d] = %d; "
                "vsamp[%d] = %d\n", i, siz->comps[i].prec, i,
                siz->comps[i].sgnd, i, siz->comps[i].hsamp, i,
                siz->comps[i].vsamp);
    }
    return 0;
}

/*****************************************************************************\
* COD marker segment operations.
\*****************************************************************************/

static void jpc_cod_destroyparms(jpc_ms_t *ms)
{
    jpc_cod_t *cod = &ms->parms.cod;
    jpc_cox_destroycompparms(&cod->compparms);
}

static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_cod_t *cod = &ms->parms.cod;
    if (jpc_getuint8(in, &cod->csty)) {
        return -1;
    }
    if (jpc_getuint8(in, &cod->prg) ||
      jpc_getuint16(in, &cod->numlyrs) ||
      jpc_getuint8(in, &cod->mctrans)) {
        return -1;
    }
    if (jpc_cox_getcompparms(ms, cstate, in,
      (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) {
        return -1;
    }
    if (jas_stream_eof(in)) {
        jpc_cod_destroyparms(ms);
        return -1;
    }
    return 0;
}

static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_cod_t *cod = &ms->parms.cod;
    assert(cod->numlyrs > 0 && cod->compparms.numdlvls <= 32);
    assert(cod->compparms.numdlvls == cod->compparms.numrlvls - 1);
    if (jpc_putuint8(out, cod->compparms.csty) ||
      jpc_putuint8(out, cod->prg) ||
      jpc_putuint16(out, cod->numlyrs) ||
      jpc_putuint8(out, cod->mctrans)) {
        return -1;
    }
    if (jpc_cox_putcompparms(ms, cstate, out,
      (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) {
        return -1;
    }
    return 0;
}

static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_cod_t *cod = &ms->parms.cod;
    int i;
    fprintf(out, "csty = 0x%02x;\n", cod->compparms.csty);
    fprintf(out, "numdlvls = %d; qmfbid = %d; mctrans = %d\n",
            cod->compparms.numdlvls, cod->compparms.qmfbid, cod->mctrans);
    fprintf(out, "prg = %d; numlyrs = %d;\n",
            cod->prg, (int)cod->numlyrs);
    fprintf(out, "cblkwidthval = %d; cblkheightval = %d; "
            "cblksty = 0x%02x;\n", cod->compparms.cblkwidthval, cod->compparms.cblkheightval,
            cod->compparms.cblksty);
    if (cod->csty & JPC_COX_PRT) {
        for (i = 0; i < cod->compparms.numrlvls; ++i) {
            fprintf(stderr, "prcwidth[%d] = %d, prcheight[%d] = %d\n",
                    i, cod->compparms.rlvls[i].parwidthval,
                    i, cod->compparms.rlvls[i].parheightval);
        }
    }
    return 0;
}

/*****************************************************************************\
* COC marker segment operations.
\*****************************************************************************/

static void jpc_coc_destroyparms(jpc_ms_t *ms)
{
    jpc_coc_t *coc = &ms->parms.coc;
    jpc_cox_destroycompparms(&coc->compparms);
}

static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_coc_t *coc = &ms->parms.coc;
    uint_fast8_t tmp;
    if (cstate->numcomps <= 256) {
        if (jpc_getuint8(in, &tmp)) {
            return -1;
        }
        coc->compno = tmp;
    } else {
        if (jpc_getuint16(in, &coc->compno)) {
            return -1;
        }
    }
    if (jpc_getuint8(in, &coc->compparms.csty)) {
        return -1;
    }
    if (jpc_cox_getcompparms(ms, cstate, in,
      (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) {
        return -1;
    }
    if (jas_stream_eof(in)) {
        return -1;
    }
    return 0;
}

static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_coc_t *coc = &ms->parms.coc;
    assert(coc->compparms.numdlvls <= 32);
    if (cstate->numcomps <= 256) {
        if (jpc_putuint8(out, coc->compno)) {
            return -1;
        }
    } else {
        if (jpc_putuint16(out, coc->compno)) {
            return -1;
        }
    }
    if (jpc_putuint8(out, coc->compparms.csty)) {
        return -1;
    }
    if (jpc_cox_putcompparms(ms, cstate, out,
      (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) {
        return -1;
    }
    return 0;
}

static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_coc_t *coc = &ms->parms.coc;
    fprintf(out, "compno = %d; csty = 0x%02x; numdlvls = %d;\n",
            (int)coc->compno, coc->compparms.csty, coc->compparms.numdlvls);
    fprintf(out, "cblkwidthval = %d; cblkheightval = %d; "
            "cblksty = 0x%02x; qmfbid = %d;\n", coc->compparms.cblkwidthval,
            coc->compparms.cblkheightval, coc->compparms.cblksty,
            coc->compparms.qmfbid);
    return 0;
}
/******************************************************************************\
* COD/COC marker segment operation helper functions.
\******************************************************************************/

static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms)
{
}

static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
  jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms)
{
    uint_fast8_t tmp;
    int i;
    if (jpc_getuint8(in, &compparms->numdlvls) ||
      jpc_getuint8(in, &compparms->cblkwidthval) ||
      jpc_getuint8(in, &compparms->cblkheightval) ||
      jpc_getuint8(in, &compparms->cblksty) ||
      jpc_getuint8(in, &compparms->qmfbid)) {
        return -1;
    }
    compparms->numrlvls = compparms->numdlvls + 1;
    if (prtflag) {
        for (i = 0; i < compparms->numrlvls; ++i) {
            if (jpc_getuint8(in, &tmp)) {
                jpc_cox_destroycompparms(compparms);
                return -1;
            }
            compparms->rlvls[i].parwidthval = tmp & 0xf;
            compparms->rlvls[i].parheightval = (tmp >> 4) & 0xf;
        }
/* Sigh.  This bit should be in the same field in both COC and COD mrk segs. */
compparms->csty |= JPC_COX_PRT;
    } else {
    }
    if (jas_stream_eof(in)) {
        jpc_cox_destroycompparms(compparms);
        return -1;
    }
    return 0;
}

static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,
  jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms)
{
    int i;
    assert(compparms->numdlvls <= 32);
    if (jpc_putuint8(out, compparms->numdlvls) ||
      jpc_putuint8(out, compparms->cblkwidthval) ||
      jpc_putuint8(out, compparms->cblkheightval) ||
      jpc_putuint8(out, compparms->cblksty) ||
      jpc_putuint8(out, compparms->qmfbid)) {
        return -1;
    }
    if (prtflag) {
        for (i = 0; i < compparms->numrlvls; ++i) {
            if (jpc_putuint8(out,
              ((compparms->rlvls[i].parheightval & 0xf) << 4) |
              (compparms->rlvls[i].parwidthval & 0xf))) {
                return -1;
            }
        }
    }
    return 0;
}

/******************************************************************************\
* RGN marker segment operations.
\******************************************************************************/

static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_rgn_t *rgn = &ms->parms.rgn;
    uint_fast8_t tmp;
    if (cstate->numcomps <= 256) {
        if (jpc_getuint8(in, &tmp)) {
            return -1;
        }
        rgn->compno = tmp;
    } else {
        if (jpc_getuint16(in, &rgn->compno)) {
            return -1;
        }
    }
    if (jpc_getuint8(in, &rgn->roisty) ||
      jpc_getuint8(in, &rgn->roishift)) {
        return -1;
    }
    return 0;
}

static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_rgn_t *rgn = &ms->parms.rgn;
    if (cstate->numcomps <= 256) {
        if (jpc_putuint8(out, rgn->compno)) {
            return -1;
        }
    } else {
        if (jpc_putuint16(out, rgn->compno)) {
            return -1;
        }
    }
    if (jpc_putuint8(out, rgn->roisty) ||
      jpc_putuint8(out, rgn->roishift)) {
        return -1;
    }
    return 0;
}

static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_rgn_t *rgn = &ms->parms.rgn;
    fprintf(out, "compno = %d; roisty = %d; roishift = %d\n",
            (int)rgn->compno, rgn->roisty, rgn->roishift);
    return 0;
}

/******************************************************************************\
* QCD marker segment operations.
\******************************************************************************/

static void jpc_qcd_destroyparms(jpc_ms_t *ms)
{
    jpc_qcd_t *qcd = &ms->parms.qcd;
    jpc_qcx_destroycompparms(&qcd->compparms);
}

static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms;
    return jpc_qcx_getcompparms(compparms, cstate, in, ms->len);
}

static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms;
    return jpc_qcx_putcompparms(compparms, cstate, out);
}

static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_qcd_t *qcd = &ms->parms.qcd;
    int i;
    fprintf(out, "qntsty = %d; numguard = %d; numstepsizes = %d\n",
      (int) qcd->compparms.qntsty, qcd->compparms.numguard, qcd->compparms.numstepsizes);
    for (i = 0; i < qcd->compparms.numstepsizes; ++i) {
        fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n",
          i, (unsigned) JPC_QCX_GETEXPN(qcd->compparms.stepsizes[i]),
          i, (unsigned) JPC_QCX_GETMANT(qcd->compparms.stepsizes[i]));
    }
    return 0;
}

/******************************************************************************\
* QCC marker segment operations.
\******************************************************************************/

static void jpc_qcc_destroyparms(jpc_ms_t *ms)
{
    jpc_qcc_t *qcc = &ms->parms.qcc;
    jpc_qcx_destroycompparms(&qcc->compparms);
}

static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_qcc_t *qcc = &ms->parms.qcc;
    uint_fast8_t tmp;
    int len;
    len = ms->len;
    if (cstate->numcomps <= 256) {
        jpc_getuint8(in, &tmp);
        qcc->compno = tmp;
        --len;
    } else {
        jpc_getuint16(in, &qcc->compno);
        len -= 2;
    }
    if (jpc_qcx_getcompparms(&qcc->compparms, cstate, in, len)) {
        return -1;
    }
    if (jas_stream_eof(in)) {
        jpc_qcc_destroyparms(ms);
        return -1;
    }
    return 0;
}

static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_qcc_t *qcc = &ms->parms.qcc;
    if (cstate->numcomps <= 256) {
        jpc_putuint8(out, qcc->compno);
    } else {
        jpc_putuint16(out, qcc->compno);
    }
    if (jpc_qcx_putcompparms(&qcc->compparms, cstate, out)) {
        return -1;
    }
    return 0;
}

static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_qcc_t *qcc = &ms->parms.qcc;
    int i;
    fprintf(out, "compno = %d; qntsty = %d; numguard = %d; "
            "numstepsizes = %d\n",
            (int)qcc->compno, qcc->compparms.qntsty, qcc->compparms.numguard,
            qcc->compparms.numstepsizes);
    for (i = 0; i < qcc->compparms.numstepsizes; ++i) {
        fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n",
          i, (unsigned) JPC_QCX_GETEXPN(qcc->compparms.stepsizes[i]),
          i, (unsigned) JPC_QCX_GETMANT(qcc->compparms.stepsizes[i]));
    }
    return 0;
}

/*****************************************************************************\
* QCD/QCC marker segment helper functions.
\*****************************************************************************/

static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms)
{
    if (compparms->stepsizes) {
        jas_free(compparms->stepsizes);
    }
}

static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
  jas_stream_t *in, uint_fast16_t len)
{
    uint_fast8_t tmp;
    int n;
    int i;
    n = 0;
    jpc_getuint8(in, &tmp);
    ++n;
    compparms->qntsty = tmp & 0x1f;
    compparms->numguard = (tmp >> 5) & 7;
    switch (compparms->qntsty) {
    case JPC_QCX_SIQNT:
        compparms->numstepsizes = 1;
        break;
    case JPC_QCX_NOQNT:
        compparms->numstepsizes = (len - n);
        break;
    case JPC_QCX_SEQNT:
        /* XXX - this is a hack */
        compparms->numstepsizes = (len - n) / 2;
        break;
    }
if (compparms->numstepsizes > 0) {
    compparms->stepsizes = jas_malloc(compparms->numstepsizes *
      sizeof(uint_fast32_t));
    assert(compparms->stepsizes);
    for (i = 0; i < compparms->numstepsizes; ++i) {
        if (compparms->qntsty == JPC_QCX_NOQNT) {
            jpc_getuint8(in, &tmp);
            compparms->stepsizes[i] = JPC_QCX_EXPN(tmp >> 3);
        } else {
            jpc_getuint16(in, &compparms->stepsizes[i]);
        }
    }
} else {
    compparms->stepsizes = 0;
}
    if (jas_stream_error(in) || jas_stream_eof(in)) {
        jpc_qcx_destroycompparms(compparms);
        return -1;
    }
    return 0;
}

static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,
  jas_stream_t *out)
{
    int i;
    jpc_putuint8(out, ((compparms->numguard & 7) << 5) | compparms->qntsty);
    for (i = 0; i < compparms->numstepsizes; ++i) {
        if (compparms->qntsty == JPC_QCX_NOQNT) {
            jpc_putuint8(out, JPC_QCX_GETEXPN(
              compparms->stepsizes[i]) << 3);
        } else {
            jpc_putuint16(out, compparms->stepsizes[i]);
        }
    }
    return 0;
}

/******************************************************************************\
* SOP marker segment operations.
\******************************************************************************/

static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_sop_t *sop = &ms->parms.sop;
    if (jpc_getuint16(in, &sop->seqno)) {
        return -1;
    }
    return 0;
}

static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_sop_t *sop = &ms->parms.sop;
    if (jpc_putuint16(out, sop->seqno)) {
        return -1;
    }
    return 0;
}

static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_sop_t *sop = &ms->parms.sop;
    fprintf(out, "seqno = %d;\n", (int)sop->seqno);
    return 0;
}

/******************************************************************************\
* PPM marker segment operations.
\******************************************************************************/

static void jpc_ppm_destroyparms(jpc_ms_t *ms)
{
    jpc_ppm_t *ppm = &ms->parms.ppm;
    if (ppm->data) {
        jas_free(ppm->data);
    }
}

static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_ppm_t *ppm = &ms->parms.ppm;

    ppm->data = 0;

    if (ms->len < 1) {
        goto error;
    }
    if (jpc_getuint8(in, &ppm->ind)) {
        goto error;
    }

    ppm->len = ms->len - 1;
    if (ppm->len > 0) {
        if (!(ppm->data = jas_malloc(ppm->len * sizeof(unsigned char)))) {
            goto error;
        }
        if (jas_stream_read(in, ppm->data, ppm->len) != ppm->len) {
            goto error;
        }
    } else {
        ppm->data = 0;
    }
    return 0;

error:
    jpc_ppm_destroyparms(ms);
    return -1;
}

static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_ppm_t *ppm = &ms->parms.ppm;
    if (jas_stream_write(out, (char *) ppm->data, ppm->len) != ppm->len) {
        return -1;
    }
    return 0;
}

static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_ppm_t *ppm = &ms->parms.ppm;
    fprintf(out, "ind=%d; len = %d;\n", ppm->ind, (int)ppm->len);
    if (ppm->len > 0) {
        fprintf(out, "data =\n");
        jas_memdump(out, ppm->data, ppm->len);
    }
    return 0;
}

/******************************************************************************\
* PPT marker segment operations.
\******************************************************************************/

static void jpc_ppt_destroyparms(jpc_ms_t *ms)
{
    jpc_ppt_t *ppt = &ms->parms.ppt;
    if (ppt->data) {
        jas_free(ppt->data);
    }
}

static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_ppt_t *ppt = &ms->parms.ppt;
    ppt->data = 0;

    if (ms->len < 1) {
        goto error;
    }
    if (jpc_getuint8(in, &ppt->ind)) {
        goto error;
    }
    ppt->len = ms->len - 1;
    if (ppt->len > 0) {
        if (!(ppt->data = jas_malloc(ppt->len * sizeof(unsigned char)))) {
            goto error;
        }
        if (jas_stream_read(in, (char *) ppt->data, ppt->len) != ppt->len) {
            goto error;
        }
    } else {
        ppt->data = 0;
    }
    return 0;

error:
    jpc_ppt_destroyparms(ms);
    return -1;
}

static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_ppt_t *ppt = &ms->parms.ppt;
    if (jpc_putuint8(out, ppt->ind)) {
        return -1;
    }
    if (jas_stream_write(out, (char *) ppt->data, ppt->len) != ppt->len) {
        return -1;
    }
    return 0;
}

static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_ppt_t *ppt = &ms->parms.ppt;
    fprintf(out, "ind=%d; len = %d;\n", ppt->ind, (int)ppt->len);
    if (ppt->len > 0) {
        fprintf(out, "data =\n");
        jas_memdump(out, ppt->data, ppt->len);
    }
    return 0;
}

/*****************************************************************************\
* POC marker segment operations.
\*****************************************************************************/

static void jpc_poc_destroyparms(jpc_ms_t *ms)
{
    jpc_poc_t *poc = &ms->parms.poc;
    if (poc->pchgs) {
        jas_free(poc->pchgs);
    }
}

static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_poc_t *poc = &ms->parms.poc;
    jpc_pocpchg_t *pchg;
    int pchgno;
    uint_fast8_t tmp;
    poc->numpchgs = (cstate->numcomps > 256) ? (ms->len / 9) :
      (ms->len / 7);
    if (!(poc->pchgs = jas_malloc(poc->numpchgs * sizeof(jpc_pocpchg_t)))) {
        goto error;
    }
    for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno,
      ++pchg) {
        if (jpc_getuint8(in, &pchg->rlvlnostart)) {
            goto error;
        }
        if (cstate->numcomps > 256) {
            if (jpc_getuint16(in, &pchg->compnostart)) {
                goto error;
            }
        } else {
            if (jpc_getuint8(in, &tmp)) {
                goto error;
            };
            pchg->compnostart = tmp;
        }
        if (jpc_getuint16(in, &pchg->lyrnoend) ||
          jpc_getuint8(in, &pchg->rlvlnoend)) {
            goto error;
        }
        if (cstate->numcomps > 256) {
            if (jpc_getuint16(in, &pchg->compnoend)) {
                goto error;
            }
        } else {
            if (jpc_getuint8(in, &tmp)) {
                goto error;
            }
            pchg->compnoend = tmp;
        }
        if (jpc_getuint8(in, &pchg->prgord)) {
            goto error;
        }
        if (pchg->rlvlnostart > pchg->rlvlnoend ||
          pchg->compnostart > pchg->compnoend) {
            goto error;
        }
    }
    return 0;

error:
    jpc_poc_destroyparms(ms);
    return -1;
}

static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_poc_t *poc = &ms->parms.poc;
    jpc_pocpchg_t *pchg;
    int pchgno;
    for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno,
      ++pchg) {
        if (jpc_putuint8(out, pchg->rlvlnostart) ||
          ((cstate->numcomps > 256) ?
          jpc_putuint16(out, pchg->compnostart) :
          jpc_putuint8(out, pchg->compnostart)) ||
          jpc_putuint16(out, pchg->lyrnoend) ||
          jpc_putuint8(out, pchg->rlvlnoend) ||
          ((cstate->numcomps > 256) ?
          jpc_putuint16(out, pchg->compnoend) :
          jpc_putuint8(out, pchg->compnoend)) ||
          jpc_putuint8(out, pchg->prgord)) {
            return -1;
        }
    }
    return 0;
}

static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_poc_t *poc = &ms->parms.poc;
    jpc_pocpchg_t *pchg;
    int pchgno;
    for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs;
      ++pchgno, ++pchg) {
        fprintf(out, "po[%d] = %d; ", pchgno, pchg->prgord);
        fprintf(out, "cs[%d] = %d; ce[%d] = %d; ",
                pchgno, (int)pchg->compnostart, pchgno, (int)pchg->compnoend);
        fprintf(out, "rs[%d] = %d; re[%d] = %d; ",
                pchgno, pchg->rlvlnostart, pchgno, pchg->rlvlnoend);
        fprintf(out, "le[%d] = %d\n", pchgno, (int)pchg->lyrnoend);
    }
    return 0;
}

/******************************************************************************\
* CRG marker segment operations.
\******************************************************************************/

static void jpc_crg_destroyparms(jpc_ms_t *ms)
{
    jpc_crg_t *crg = &ms->parms.crg;
    if (crg->comps) {
        jas_free(crg->comps);
    }
}

static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_crg_t *crg = &ms->parms.crg;
    jpc_crgcomp_t *comp;
    uint_fast16_t compno;
    crg->numcomps = cstate->numcomps;
    if (!(crg->comps = jas_malloc(cstate->numcomps * sizeof(uint_fast16_t)))) {
        return -1;
    }
    for (compno = 0, comp = crg->comps; compno < cstate->numcomps;
      ++compno, ++comp) {
        if (jpc_getuint16(in, &comp->hoff) ||
          jpc_getuint16(in, &comp->voff)) {
            jpc_crg_destroyparms(ms);
            return -1;
        }
    }
    return 0;
}

static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_crg_t *crg = &ms->parms.crg;
    int compno;
    jpc_crgcomp_t *comp;
    for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno,
      ++comp) {
        if (jpc_putuint16(out, comp->hoff) ||
          jpc_putuint16(out, comp->voff)) {
            return -1;
        }
    }
    return 0;
}

static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_crg_t *crg = &ms->parms.crg;
    int compno;
    jpc_crgcomp_t *comp;
    for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno,
      ++comp) {
        fprintf(out, "hoff[%d] = %d; voff[%d] = %d\n",
                compno, (int)comp->hoff, compno, (int)comp->voff);
    }
    return 0;
}

/******************************************************************************\
* Operations for COM marker segment.
\******************************************************************************/

static void jpc_com_destroyparms(jpc_ms_t *ms)
{
    jpc_com_t *com = &ms->parms.com;
    if (com->data) {
        jas_free(com->data);
    }
}

static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_com_t *com = &ms->parms.com;
    if (jpc_getuint16(in, &com->regid)) {
        return -1;
    }
    com->len = ms->len - 2;
    if (com->len > 0) {
        if (!(com->data = jas_malloc(com->len))) {
            return -1;
        }
        if (jas_stream_read(in, com->data, com->len) != com->len) {
            return -1;
        }
    } else {
        com->data = 0;
    }
    return 0;
}

static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    jpc_com_t *com = &ms->parms.com;
    if (jpc_putuint16(out, com->regid)) {
        return -1;
    }
    if (jas_stream_write(out, com->data, com->len) != com->len) {
        return -1;
    }
    return 0;
}

static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out)
{
    jpc_com_t *com = &ms->parms.com;
    int i;
    int printable;
    fprintf(out, "regid = %d;\n", (int)com->regid);
    printable = 1;
    for (i = 0, printable = 1; i < com->len && printable; ++i) {
        if (!isprint(com->data[i]))
            printable = 0;
    }
    if (printable) {
        fprintf(out, "data = ");
        fwrite(com->data, sizeof(char), com->len, out);
        fprintf(out, "\n");
    }
    return 0;
}

/*****************************************************************************\
* Operations for unknown types of marker segments.
\*****************************************************************************/

static void jpc_unk_destroyparms(jpc_ms_t *ms)
{
    jpc_unk_t *unk = &ms->parms.unk;
    if (unk->data) {
        jas_free(unk->data);
    }
}

static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in)
{
    jpc_unk_t *unk = &ms->parms.unk;

    if (ms->len > 0) {
        if (!(unk->data = jas_malloc(ms->len * sizeof(unsigned char)))) {
            return -1;
        }
        if (jas_stream_read(in, (char *) unk->data, ms->len) != ms->len) {
            jas_free(unk->data);
            return -1;
        }
        unk->len = ms->len;
    } else {
        unk->data = 0;
        unk->len = 0;
    }
    return 0;
}

static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out)
{
    return -1;
}

static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out)
{
    int i;
    jpc_unk_t *unk = &ms->parms.unk;
    for (i = 0; i < unk->len; ++i) {
        fprintf(out, "%02x ", unk->data[i]);
    }
    return 0;
}

/******************************************************************************\
* Primitive I/O operations.
\******************************************************************************/

int jpc_getuint8(jas_stream_t *in, uint_fast8_t *val)
{
    int c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    if (val) {
        *val = c;
    }
    return 0;
}

int jpc_putuint8(jas_stream_t *out, uint_fast8_t val)
{
    if (jas_stream_putc(out, val & 0xff) == EOF) {
        return -1;
    }
    return 0;
}

int jpc_getuint16(jas_stream_t *in, uint_fast16_t *val)
{
    uint_fast16_t v;
    int c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = (v << 8) | c;
    if (val) {
        *val = v;
    }
    return 0;
}

int jpc_putuint16(jas_stream_t *out, uint_fast16_t val)
{
    if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
      jas_stream_putc(out, val & 0xff) == EOF) {
        return -1;
    }
    return 0;
}

int jpc_getuint32(jas_stream_t *in, uint_fast32_t *val)
{
    uint_fast32_t v;
    int c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = (v << 8) | c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = (v << 8) | c;
    if ((c = jas_stream_getc(in)) == EOF) {
        return -1;
    }
    v = (v << 8) | c;
    if (val) {
        *val = v;
    }
    return 0;
}

int jpc_putuint32(jas_stream_t *out, uint_fast32_t val)
{
    if (jas_stream_putc(out, (val >> 24) & 0xff) == EOF ||
      jas_stream_putc(out, (val >> 16) & 0xff) == EOF ||
      jas_stream_putc(out, (val >> 8) & 0xff) == EOF ||
      jas_stream_putc(out, val & 0xff) == EOF) {
        return -1;
    }
    return 0;
}

/******************************************************************************\
* Miscellany
\******************************************************************************/

static jpc_mstabent_t *jpc_mstab_lookup(int id)
{
    jpc_mstabent_t *mstabent;
    for (mstabent = jpc_mstab;; ++mstabent) {
        if (mstabent->id == id || mstabent->id < 0) {
            return mstabent;
        }
    }
    assert(0);
    return 0;
}

int jpc_validate(jas_stream_t *in)
{
    int n;
    int i;
    unsigned char buf[2];

    assert(JAS_STREAM_MAXPUTBACK >= 2);

    if ((n = jas_stream_read(in, (char *) buf, 2)) < 0) {
        return -1;
    }
    for (i = n - 1; i >= 0; --i) {
        if (jas_stream_ungetc(in, buf[i]) == EOF) {
            return -1;
        }
    }
    if (n < 2) {
        return -1;
    }
    if (buf[0] == (JPC_MS_SOC >> 8) && buf[1] == (JPC_MS_SOC & 0xff)) {
        return 0;
    }
    return -1;
}

int jpc_getdata(jas_stream_t *in, jas_stream_t *out, long len)
{
    return jas_stream_copy(out, in, len);
}

int jpc_putdata(jas_stream_t *out, jas_stream_t *in, long len)
{
    return jas_stream_copy(out, in, len);
}