1121986Sjhb/*
2121986Sjhb * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3121986Sjhb *   British Columbia.
4121986Sjhb * Copyright (c) 2001-2002 Michael David Adams.
5121986Sjhb * All rights reserved.
6121986Sjhb */
7121986Sjhb
8121986Sjhb/* __START_OF_JASPER_LICENSE__
9121986Sjhb *
10121986Sjhb * JasPer Software License
11121986Sjhb *
12121986Sjhb * IMAGE POWER JPEG-2000 PUBLIC LICENSE
13121986Sjhb * ************************************
14121986Sjhb *
15121986Sjhb * GRANT:
16121986Sjhb *
17121986Sjhb * Permission is hereby granted, free of charge, to any person (the "User")
18121986Sjhb * obtaining a copy of this software and associated documentation, to deal
19121986Sjhb * in the JasPer Software without restriction, including without limitation
20121986Sjhb * the right to use, copy, modify, merge, publish, distribute, sublicense,
21121986Sjhb * and/or sell copies of the JasPer Software (in source and binary forms),
22121986Sjhb * and to permit persons to whom the JasPer Software is furnished to do so,
23121986Sjhb * provided further that the License Conditions below are met.
24121986Sjhb *
25121986Sjhb * License Conditions
26121986Sjhb * ******************
27121986Sjhb *
28121986Sjhb * A.  Redistributions of source code must retain the above copyright notice,
29121986Sjhb * and this list of conditions, and the following disclaimer.
3026168Sfsmp *
3150477Speter * B.  Redistributions in binary form must reproduce the above copyright
3226168Sfsmp * notice, and this list of conditions, and the following disclaimer in
3326168Sfsmp * the documentation and/or other materials provided with the distribution.
34121986Sjhb *
35121986Sjhb * C.  Neither the name of Image Power, Inc. nor any other contributor
36121986Sjhb * (including, but not limited to, the University of British Columbia and
37121986Sjhb * Michael David Adams) may be used to endorse or promote products derived
3828043Sfsmp * from this software without specific prior written permission.
39121986Sjhb *
40121986Sjhb * D.  User agrees that it shall not commence any action against Image Power,
41121986Sjhb * Inc., the University of British Columbia, Michael David Adams, or any
4226168Sfsmp * other contributors (collectively "Licensors") for infringement of any
43121986Sjhb * intellectual property rights ("IPR") held by the User in respect of any
4426168Sfsmp * technology that User owns or has a right to license or sublicense and
4566716Sjhb * which is an element required in order to claim compliance with ISO/IEC
46121986Sjhb * 15444-1 (i.e., JPEG-2000 Part 1).  "IPR" means all intellectual property
4766716Sjhb * rights worldwide arising under statutory or common law, and whether
4866716Sjhb * or not perfected, including, without limitation, all (i) patents and
4966716Sjhb * patent applications owned or licensable by User; (ii) rights associated
5066716Sjhb * with works of authorship including copyrights, copyright applications,
5193264Sdillon * copyright registrations, mask work rights, mask work applications,
5266716Sjhb * mask work registrations; (iii) rights relating to the protection of
5366716Sjhb * trade secrets and confidential information; (iv) any right analogous
5466716Sjhb * to those set forth in subsections (i), (ii), or (iii) and any other
5528442Sfsmp * proprietary rights relating to intangible property (other than trademark,
5666716Sjhb * trade dress, or service mark rights); and (v) divisions, continuations,
5766716Sjhb * renewals, reissues and extensions of the foregoing (as and to the extent
5866716Sjhb * applicable) now existing, hereafter filed, issued or acquired.
5966716Sjhb *
6066716Sjhb * E.  If User commences an infringement action against any Licensor(s) then
6166716Sjhb * such Licensor(s) shall have the right to terminate User's license and
6266716Sjhb * all sublicenses that have been granted hereunder by User to other parties.
6328487Sfsmp *
64121986Sjhb * F.  This software is for use only in hardware or software products that
65121986Sjhb * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1).  No license
66121986Sjhb * or right to this Software is granted for products that do not comply
67121986Sjhb * with ISO/IEC 15444-1.  The JPEG-2000 Part 1 standard can be purchased
68121986Sjhb * from the ISO.
6928487Sfsmp *
70121986Sjhb * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
7193264Sdillon * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
7293264Sdillon * THIS DISCLAIMER.  THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
7393264Sdillon * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
74125405Sjhb * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
75125405Sjhb * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
76128328Sjhb * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING.  THOSE INTENDING
77128328Sjhb * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
78125405Sjhb * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
79128328Sjhb * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
80129620Sbde * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
81121986Sjhb * IS WITH THE USER.  SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
82121986Sjhb * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
83121986Sjhb * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
84121986Sjhb * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
85121986Sjhb * REPAIR OR CORRECTION.  UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
8693264Sdillon * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
87121986Sjhb * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
88121986Sjhb * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
89121986Sjhb * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
9026168Sfsmp * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
91121986Sjhb * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
92121986Sjhb * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
93121986Sjhb * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
9426168Sfsmp * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
9527251Sfsmp * OF SUCH DAMAGES.  THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
9627352Sfsmp * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
9727352Sfsmp * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
9827352Sfsmp * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
9927352Sfsmp * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
10027352Sfsmp * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
10127352Sfsmp * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
10227352Sfsmp * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
10327352Sfsmp * ("HIGH RISK ACTIVITIES").  LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
104115907Sjhb * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.  USER WILL NOT
10527352Sfsmp * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
10627352Sfsmp * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
10727352Sfsmp * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
10827352Sfsmp * NOTICE SPECIFIED IN THIS SECTION.
10927352Sfsmp *
110122620Sjhb * __END_OF_JASPER_LICENSE__
111122620Sjhb */
112122620Sjhb
113122620Sjhb/*
114122620Sjhb * JPEG-2000 Code Stream Library
115122690Sjhb *
116122690Sjhb * $Id: jpc_cs.h 14449 2005-10-20 12:15:56Z stippi $
117121986Sjhb */
118141538Sjhb
119141538Sjhb#ifndef JPC_CS_H
120141538Sjhb#define JPC_CS_H
121141538Sjhb
122141538Sjhb/******************************************************************************\
123141538Sjhb* Includes.
124141538Sjhb\******************************************************************************/
125141538Sjhb
126141538Sjhb#include "jasper/jas_image.h"
127141538Sjhb#include "jasper/jas_stream.h"
128141538Sjhb
129141538Sjhb#include "jpc_cod.h"
130141538Sjhb
131141538Sjhb/******************************************************************************\
132141538Sjhb* Constants and Types.
133141538Sjhb\******************************************************************************/
134141538Sjhb
135141538Sjhb/* The maximum number of resolution levels. */
136141538Sjhb#define	JPC_MAXRLVLS	33
137141538Sjhb
138141538Sjhb/* The maximum number of bands. */
139141538Sjhb#define	JPC_MAXBANDS	(3 * JPC_MAXRLVLS + 1)
140141538Sjhb
141141538Sjhb/* The maximum number of layers. */
142115907Sjhb#define JPC_MAXLYRS	16384
14327352Sfsmp
14499862Speter/**************************************\
14527251Sfsmp* Code stream.
14626168Sfsmp\**************************************/
14726168Sfsmp
148115907Sjhb/*
14926168Sfsmp * Code stream states.
15099862Speter */
15199862Speter
152128328Sjhb/* Initial. */
153125405Sjhb#define	JPC_CS_INIT	0
15427251Sfsmp/* Main header. */
15546129Sluoqi#define	JPC_CS_MHDR	1
15699862Speter/* Tile-part header. */
157128328Sjhb#define	JPC_CS_THDR	2
15869971Sjake/* Main trailer. */
15946129Sluoqi#define	JPC_CS_MTLR	3
16099862Speter/* Tile-part data. */
16127251Sfsmp#define	JPC_CS_TDATA	4
16227251Sfsmp
16326168Sfsmp/*
16426168Sfsmp * Unfortunately, the code stream syntax was not designed in such a way that
16527251Sfsmp * any given marker segment can be correctly decoded without additional state
166121986Sjhb * derived from previously decoded marker segments.
167121986Sjhb * For example, a RGN/COC/QCC marker segment cannot be decoded unless the
16827251Sfsmp * number of components is known.
16999862Speter */
17099862Speter
17199862Speter/*
17299862Speter * Code stream state information.
17326168Sfsmp */
17426168Sfsmp
17526168Sfsmptypedef struct {
17627007Sfsmp
17799862Speter	/* The number of components. */
17899862Speter	int numcomps;
17999862Speter
18099862Speter} jpc_cstate_t;
181115907Sjhb
18299862Speter/**************************************\
18399862Speter* SOT marker segment parameters.
18499862Speter\**************************************/
185128328Sjhb
186125405Sjhbtypedef struct {
18799862Speter
18899862Speter	/* The tile number. */
18999862Speter	uint_fast16_t tileno;
190128328Sjhb
19199862Speter	/* The combined length of the marker segment and its auxilary data
19299862Speter	  (i.e., packet data). */
19399862Speter	uint_fast32_t len;
19499862Speter
19599862Speter	/* The tile-part instance. */
19699862Speter	uint_fast8_t partno;
19799862Speter
19899862Speter	/* The number of tile-parts. */
199121986Sjhb	uint_fast8_t numparts;
200121986Sjhb
20199862Speter} jpc_sot_t;
20299862Speter
20399862Speter/**************************************\
20499862Speter* SIZ marker segment parameters.
20599862Speter\**************************************/
20699862Speter
20799862Speter/* Per component information. */
20899862Speter
20999862Spetertypedef struct {
21099862Speter
21199862Speter	/* The precision of the samples. */
21299862Speter	uint_fast8_t prec;
21399862Speter
214115907Sjhb	/* The signedness of the samples. */
21599862Speter	uint_fast8_t sgnd;
21699862Speter
21799862Speter	/* The horizontal separation of samples with respect to the reference
21899862Speter	  grid. */
219128328Sjhb	uint_fast8_t hsamp;
220125405Sjhb
22199862Speter	/* The vertical separation of samples with respect to the reference
22299862Speter	  grid. */
22399862Speter	uint_fast8_t vsamp;
224128328Sjhb
22599862Speter} jpc_sizcomp_t;
22699862Speter
22799862Speter/* SIZ marker segment parameters. */
22899862Speter
22999862Spetertypedef struct {
23099862Speter
23199862Speter	/* The code stream capabilities. */
23299862Speter	uint_fast16_t caps;
23399862Speter
234102329Speter	/* The width of the image in units of the reference grid. */
23599862Speter	uint_fast32_t width;
23699862Speter
237121986Sjhb	/* The height of the image in units of the reference grid. */
238121986Sjhb	uint_fast32_t height;
23999862Speter
24099862Speter	/* The horizontal offset from the origin of the reference grid to the
24199862Speter	  left side of the image area. */
24299862Speter	uint_fast32_t xoff;
24399862Speter
24499862Speter	/* The vertical offset from the origin of the reference grid to the
24599862Speter	  top side of the image area. */
24699862Speter	uint_fast32_t yoff;
24799862Speter
24899862Speter	/* The nominal width of a tile in units of the reference grid. */
249110296Sjake	uint_fast32_t tilewidth;
25076078Sjhb
25131638Sfsmp	/* The nominal height of a tile in units of the reference grid. */
25231638Sfsmp	uint_fast32_t tileheight;
25331638Sfsmp
254138528Sups	/* The horizontal offset from the origin of the reference grid to the
255138528Sups	  left side of the first tile. */
256125405Sjhb	uint_fast32_t tilexoff;
257125405Sjhb
258128328Sjhb	/* The vertical offset from the origin of the reference grid to the
259128328Sjhb	  top side of the first tile. */
260125405Sjhb	uint_fast32_t tileyoff;
261128328Sjhb
26231638Sfsmp	/* The number of components. */
263121986Sjhb	uint_fast16_t numcomps;
264121986Sjhb
265138528Sups	/* The per-component information. */
266129620Sbde	jpc_sizcomp_t *comps;
26793264Sdillon
268110296Sjake} jpc_siz_t;
269138528Sups
270110296Sjake/**************************************\
27176078Sjhb* COD marker segment parameters.
27276078Sjhb\**************************************/
27331638Sfsmp
27431638Sfsmp/*
27527007Sfsmp * Coding style constants.
27627007Sfsmp */
27727007Sfsmp
27827007Sfsmp/* Precincts may be used. */
27927007Sfsmp#define	JPC_COX_PRT	0x01
28027007Sfsmp/* SOP marker segments may be used. */
28127007Sfsmp#define	JPC_COD_SOP	0x02
28227007Sfsmp/* EPH marker segments may be used. */
283115907Sjhb#define	JPC_COD_EPH	0x04
28436135Stegge
28536135Stegge/*
28627007Sfsmp * Progression order constants.
28736135Stegge */
28836135Stegge
28927007Sfsmp/* Layer-resolution-component-precinct progressive
290121133Sbde  (i.e., progressive by fidelity). */
29146129Sluoqi#define	JPC_COD_LRCPPRG	0
29227007Sfsmp/* Resolution-layer-component-precinct progressive
29327007Sfsmp  (i.e., progressive by resolution). */
294128328Sjhb#define	JPC_COD_RLCPPRG	1
295128328Sjhb/* Resolution-precinct-component-layer progressive. */
29646129Sluoqi#define	JPC_COD_RPCLPRG	2
297128328Sjhb/* Precinct-component-resolution-layer progressive. */
29827007Sfsmp#define	JPC_COD_PCRLPRG	3
299121986Sjhb/* Component-position-resolution-layer progressive. */
300121986Sjhb#define	JPC_COD_CPRLPRG	4
30136135Stegge
30269971Sjake/*
30336135Stegge * Code block style constants.
30436135Stegge */
30536135Stegge
30636135Stegge#define	JPC_COX_LAZY	0x01 /* Selective arithmetic coding bypass. */
30736135Stegge#define	JPC_COX_RESET	0x02 /* Reset context probabilities. */
30836135Stegge#define	JPC_COX_TERMALL	0x04 /* Terminate all coding passes. */
30969971Sjake#define	JPC_COX_VSC		0x08 /* Vertical stripe causal context formation. */
31027251Sfsmp#define	JPC_COX_PTERM	0x10 /* Predictable termination. */
31127007Sfsmp#define	JPC_COX_SEGSYM	0x20 /* Use segmentation symbols. */
31276078Sjhb
31327007Sfsmp/* Transform constants. */
31476078Sjhb#define	JPC_COX_INS	0x00 /* Irreversible 9/7. */
31527007Sfsmp#define	JPC_COX_RFT	0x01 /* Reversible 5/3. */
31627007Sfsmp
31727007Sfsmp/* Multicomponent transform constants. */
31876078Sjhb#define	JPC_COD_NOMCT	0x00 /* No multicomponent transform. */
31936135Stegge#define	JPC_COD_MCT		0x01 /* Multicomponent transform. */
32076078Sjhb
32127007Sfsmp/* Get the code block size value from the code block size exponent. */
32236135Stegge#define	JPC_COX_CBLKSIZEEXPN(x)		((x) - 2)
32336135Stegge/* Get the code block size exponent from the code block size value. */
32427007Sfsmp#define	JPC_COX_GETCBLKSIZEEXPN(x)	((x) + 2)
32536135Stegge
32636135Stegge/* Per resolution-level information. */
32736135Stegge
32836135Steggetypedef struct {
32936135Stegge
33060303Sobrien	/* The packet partition width. */
33136135Stegge	uint_fast8_t parwidthval;
33246129Sluoqi
333121133Sbde	/* The packet partition height. */
33427007Sfsmp	uint_fast8_t parheightval;
33536135Stegge
33636135Stegge} jpc_coxrlvl_t;
33727007Sfsmp
33836135Stegge/* Per component information. */
33936135Stegge
34027251Sfsmptypedef struct {
34127007Sfsmp
34248924Smsmith	/* The coding style. */
34348924Smsmith	uint_fast8_t csty;
34448924Smsmith
34548924Smsmith	/* The number of decomposition levels. */
34648924Smsmith	uint_fast8_t numdlvls;
34748924Smsmith
34848924Smsmith	/* The nominal code block width specifier. */
349115907Sjhb	uint_fast8_t cblkwidthval;
350125405Sjhb
351125405Sjhb	/* The nominal code block height specifier. */
352128328Sjhb	uint_fast8_t cblkheightval;
353128328Sjhb
354125405Sjhb	/* The style of coding passes. */
355128328Sjhb	uint_fast8_t cblksty;
35648924Smsmith
35773011Sjake	/* The QMFB employed. */
35848924Smsmith	uint_fast8_t qmfbid;
359121986Sjhb
360121986Sjhb	/* The number of resolution levels. */
36148924Smsmith	int numrlvls;
36248924Smsmith
36348924Smsmith	/* The per-resolution-level information. */
364112993Speter	jpc_coxrlvl_t rlvls[JPC_MAXRLVLS];
365112993Speter
366112993Speter} jpc_coxcp_t;
367112993Speter
368112993Speter/* COD marker segment parameters. */
369115907Sjhb
370125405Sjhbtypedef struct {
371125405Sjhb
372128328Sjhb	/* The general coding style. */
373128328Sjhb	uint_fast8_t csty;
374125405Sjhb
375128328Sjhb	/* The progression order. */
376112993Speter	uint_fast8_t prg;
377112993Speter
378121986Sjhb	/* The number of layers. */
379121986Sjhb	uint_fast16_t numlyrs;
380121986Sjhb
381112993Speter	/* The multicomponent transform. */
382112993Speter	uint_fast8_t mctrans;
383115907Sjhb
384	/* Component-related parameters. */
385	jpc_coxcp_t compparms;
386
387} jpc_cod_t;
388
389/* COC marker segment parameters. */
390
391typedef struct {
392
393	/* The component number. */
394	uint_fast16_t compno;
395
396	/* Component-related parameters. */
397	jpc_coxcp_t compparms;
398
399} jpc_coc_t;
400
401/**************************************\
402* RGN marker segment parameters.
403\**************************************/
404
405/* The maxshift ROI style. */
406#define	JPC_RGN_MAXSHIFT	0x00
407
408typedef struct {
409
410	/* The component to which the marker applies. */
411	uint_fast16_t compno;
412
413	/* The ROI style. */
414	uint_fast8_t roisty;
415
416	/* The ROI shift value. */
417	uint_fast8_t roishift;
418
419} jpc_rgn_t;
420
421/**************************************\
422* QCD/QCC marker segment parameters.
423\**************************************/
424
425/*
426 * Quantization style constants.
427 */
428
429#define	JPC_QCX_NOQNT	0 /* No quantization. */
430#define	JPC_QCX_SIQNT	1 /* Scalar quantization, implicit. */
431#define	JPC_QCX_SEQNT	2 /* Scalar quantization, explicit. */
432
433/*
434 * Stepsize manipulation macros.
435 */
436
437#define	JPC_QCX_GETEXPN(x)	((x) >> 11)
438#define	JPC_QCX_GETMANT(x)	((x) & 0x07ff)
439#define	JPC_QCX_EXPN(x)		(assert(!((x) & (~0x1f))), (((x) & 0x1f) << 11))
440#define	JPC_QCX_MANT(x)		(assert(!((x) & (~0x7ff))), ((x) & 0x7ff))
441
442/* Per component information. */
443
444typedef struct {
445
446	/* The quantization style. */
447	uint_fast8_t qntsty;
448
449	/* The number of step sizes. */
450	int numstepsizes;
451
452	/* The step sizes. */
453	uint_fast16_t *stepsizes;
454
455	/* The number of guard bits. */
456	uint_fast8_t numguard;
457
458} jpc_qcxcp_t;
459
460/* QCC marker segment parameters. */
461
462typedef struct {
463
464	/* The component associated with this marker segment. */
465	uint_fast16_t compno;
466
467	/* The parameters. */
468	jpc_qcxcp_t compparms;
469
470} jpc_qcc_t;
471
472/* QCD marker segment parameters. */
473
474typedef struct {
475
476	/* The parameters. */
477	jpc_qcxcp_t compparms;
478
479} jpc_qcd_t;
480
481/**************************************\
482* POD marker segment parameters.
483\**************************************/
484
485typedef struct {
486
487	/* The progression order. */
488	uint_fast8_t prgord;
489
490	/* The lower bound (inclusive) on the resolution level for the
491	  progression order volume. */
492	uint_fast8_t rlvlnostart;
493
494	/* The upper bound (exclusive) on the resolution level for the
495	  progression order volume. */
496	uint_fast8_t rlvlnoend;
497
498	/* The lower bound (inclusive) on the component for the progression
499	  order volume. */
500	uint_fast16_t compnostart;
501
502	/* The upper bound (exclusive) on the component for the progression
503	  order volume. */
504	uint_fast16_t compnoend;
505
506	/* The upper bound (exclusive) on the layer for the progression
507	  order volume. */
508	uint_fast16_t lyrnoend;
509
510} jpc_pocpchg_t;
511
512/* An alias for the above type. */
513typedef jpc_pocpchg_t jpc_pchg_t;
514
515/* POC marker segment parameters. */
516
517typedef struct {
518
519	/* The number of progression order changes. */
520	int numpchgs;
521
522	/* The per-progression-order-change information. */
523	jpc_pocpchg_t *pchgs;
524
525} jpc_poc_t;
526
527/**************************************\
528* PPM/PPT marker segment parameters.
529\**************************************/
530
531/* PPM marker segment parameters. */
532
533typedef struct {
534
535	/* The index. */
536	uint_fast8_t ind;
537
538	/* The length. */
539	uint_fast16_t len;
540
541	/* The data. */
542	jpr_uchar_t *data;
543
544} jpc_ppm_t;
545
546/* PPT marker segment parameters. */
547
548typedef struct {
549
550	/* The index. */
551	uint_fast8_t ind;
552
553	/* The length. */
554	uint_fast32_t len;
555
556	/* The data. */
557	unsigned char *data;
558
559} jpc_ppt_t;
560
561/**************************************\
562* COM marker segment parameters.
563\**************************************/
564
565/*
566 * Registration IDs.
567 */
568
569#define	JPC_COM_BIN		0x00
570#define	JPC_COM_LATIN	0x01
571
572typedef struct {
573
574	/* The registration ID. */
575	uint_fast16_t regid;
576
577	/* The length of the data in bytes. */
578	uint_fast16_t len;
579
580	/* The data. */
581	jpr_uchar_t *data;
582
583} jpc_com_t;
584
585/**************************************\
586* SOP marker segment parameters.
587\**************************************/
588
589typedef struct {
590
591	/* The sequence number. */
592	uint_fast16_t seqno;
593
594} jpc_sop_t;
595
596/**************************************\
597* CRG marker segment parameters.
598\**************************************/
599
600/* Per component information. */
601
602typedef struct {
603
604	/* The horizontal offset. */
605	uint_fast16_t hoff;
606
607	/* The vertical offset. */
608	uint_fast16_t voff;
609
610} jpc_crgcomp_t;
611
612typedef struct {
613
614	/* The number of components. */
615	int numcomps;
616
617	/* Per component information. */
618	jpc_crgcomp_t *comps;
619
620} jpc_crg_t;
621
622/**************************************\
623* Marker segment parameters for unknown marker type.
624\**************************************/
625
626typedef struct {
627
628	/* The data. */
629	jpr_uchar_t *data;
630
631	/* The length. */
632	uint_fast16_t len;
633
634} jpc_unk_t;
635
636/**************************************\
637* Generic marker segment parameters.
638\**************************************/
639
640typedef union {
641	int soc;	/* unused */
642	jpc_sot_t sot;
643	int sod;	/* unused */
644	int eoc;	/* unused */
645	jpc_siz_t siz;
646	jpc_cod_t cod;
647	jpc_coc_t coc;
648	jpc_rgn_t rgn;
649	jpc_qcd_t qcd;
650	jpc_qcc_t qcc;
651	jpc_poc_t poc;
652	/* jpc_plm_t plm; */
653	/* jpc_plt_t plt; */
654	jpc_ppm_t ppm;
655	jpc_ppt_t ppt;
656	jpc_sop_t sop;
657	int eph;	/* unused */
658	jpc_com_t com;
659	jpc_crg_t crg;
660	jpc_unk_t unk;
661} jpc_msparms_t;
662
663/**************************************\
664* Marker segment.
665\**************************************/
666
667/* Marker segment IDs. */
668
669/* The smallest valid marker value. */
670#define	JPC_MS_MIN	0xff00
671
672/* The largest valid marker value. */
673#define	JPC_MS_MAX	0xffff
674
675/* The minimum marker value that cannot occur within packet data. */
676#define	JPC_MS_INMIN	0xff80
677/* The maximum marker value that cannot occur within packet data. */
678#define	JPC_MS_INMAX	0xffff
679
680/* Delimiting marker segments. */
681#define	JPC_MS_SOC	0xff4f /* Start of code stream (SOC). */
682#define	JPC_MS_SOT	0xff90 /* Start of tile-part (SOT). */
683#define	JPC_MS_SOD	0xff93 /* Start of data (SOD). */
684#define	JPC_MS_EOC	0xffd9 /* End of code stream (EOC). */
685
686/* Fixed information marker segments. */
687#define	JPC_MS_SIZ	0xff51 /* Image and tile size (SIZ). */
688
689/* Functional marker segments. */
690#define	JPC_MS_COD	0xff52 /* Coding style default (COD). */
691#define JPC_MS_COC	0xff53 /* Coding style component (COC). */
692#define	JPC_MS_RGN	0xff5e /* Region of interest (RGN). */
693#define JPC_MS_QCD	0xff5c /* Quantization default (QCD). */
694#define JPC_MS_QCC	0xff5d /* Quantization component (QCC). */
695#define JPC_MS_POC	0xff5f /* Progression order default (POC). */
696
697/* Pointer marker segments. */
698#define	JPC_MS_TLM	0xff55 /* Tile-part lengths, main header (TLM). */
699#define	JPC_MS_PLM	0xff57 /* Packet length, main header (PLM). */
700#define	JPC_MS_PLT	0xff58 /* Packet length, tile-part header (PLT). */
701#define	JPC_MS_PPM	0xff60 /* Packed packet headers, main header (PPM). */
702#define	JPC_MS_PPT	0xff61 /* Packet packet headers, tile-part header (PPT). */
703
704/* In bit stream marker segments. */
705#define	JPC_MS_SOP	0xff91	/* Start of packet (SOP). */
706#define	JPC_MS_EPH	0xff92	/* End of packet header (EPH). */
707
708/* Informational marker segments. */
709#define	JPC_MS_CRG	0xff63 /* Component registration (CRG). */
710#define JPC_MS_COM	0xff64 /* Comment (COM). */
711
712/* Forward declaration. */
713struct jpc_msops_s;
714
715/* Generic marker segment class. */
716
717typedef struct {
718
719	/* The type of marker segment. */
720	uint_fast16_t id;
721
722	/* The length of the marker segment. */
723	uint_fast16_t len;
724
725	/* The starting offset within the stream. */
726	uint_fast32_t off;
727
728	/* The parameters of the marker segment. */
729	jpc_msparms_t parms;
730
731	/* The marker segment operations. */
732	struct jpc_msops_s *ops;
733
734} jpc_ms_t;
735
736/* Marker segment operations (which depend on the marker segment type). */
737
738typedef struct jpc_msops_s {
739
740	/* Destroy the marker segment parameters. */
741	void (*destroyparms)(jpc_ms_t *ms);
742
743	/* Get the marker segment parameters from a stream. */
744	int (*getparms)(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
745
746	/* Put the marker segment parameters to a stream. */
747	int (*putparms)(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
748
749	/* Dump the marker segment parameters (for debugging). */
750	int (*dumpparms)(jpc_ms_t *ms, FILE *out);
751
752} jpc_msops_t;
753
754/******************************************************************************\
755* Macros/Functions.
756\******************************************************************************/
757
758/* Create a code-stream state object. */
759jpc_cstate_t *jpc_cstate_create();
760
761/* Destroy a code-stream state object. */
762void jpc_cstate_destroy(jpc_cstate_t *cstate);
763
764/* Create a marker segment. */
765jpc_ms_t *jpc_ms_create(int type);
766
767/* Destroy a marker segment. */
768void jpc_ms_destroy(jpc_ms_t *ms);
769
770/* Does a marker segment have parameters? */
771#define	JPC_MS_HASPARMS(x) \
772	(!((x) == JPC_MS_SOC || (x) == JPC_MS_SOD || (x) == JPC_MS_EOC || \
773	  (x) == JPC_MS_EPH || ((x) >= 0xff30 && (x) <= 0xff3f)))
774
775/* Get the marker segment type. */
776#define	jpc_ms_gettype(ms) \
777	((ms)->id)
778
779/* Read a marker segment from a stream. */
780jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate);
781
782/* Write a marker segment to a stream. */
783int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms);
784
785/* Copy code stream data from one stream to another. */
786int jpc_getdata(jas_stream_t *in, jas_stream_t *out, long n);
787
788/* Copy code stream data from one stream to another. */
789int jpc_putdata(jas_stream_t *out, jas_stream_t *in, long n);
790
791/* Dump a marker segment (for debugging). */
792void jpc_ms_dump(jpc_ms_t *ms, FILE *out);
793
794/* Read a 8-bit unsigned integer from a stream. */
795int jpc_getuint8(jas_stream_t *in, uint_fast8_t *val);
796
797/* Read a 16-bit unsigned integer from a stream. */
798int jpc_getuint16(jas_stream_t *in, uint_fast16_t *val);
799
800/* Read a 32-bit unsigned integer from a stream. */
801int jpc_getuint32(jas_stream_t *in, uint_fast32_t *val);
802
803/* Write a 8-bit unsigned integer to a stream. */
804int jpc_putuint8(jas_stream_t *out, uint_fast8_t val);
805
806/* Write a 16-bit unsigned integer to a stream. */
807int jpc_putuint16(jas_stream_t *out, uint_fast16_t val);
808
809/* Write a 32-bit unsigned integer to a stream. */
810int jpc_putuint32(jas_stream_t *out, uint_fast32_t val);
811
812#endif
813