2
* Copyright (c) 1999-2000 Image Power, Inc. and the University of
4
* Copyright (c) 2001-2002 Michael David Adams.
8
/* __START_OF_JASPER_LICENSE__
10
* JasPer License Version 2.0
12
* Copyright (c) 2001-2006 Michael David Adams
13
* Copyright (c) 1999-2000 Image Power, Inc.
14
* Copyright (c) 1999-2000 The University of British Columbia
16
* All rights reserved.
18
* Permission is hereby granted, free of charge, to any person (the
19
* "User") obtaining a copy of this software and associated documentation
20
* files (the "Software"), to deal in the Software without restriction,
21
* including without limitation the rights to use, copy, modify, merge,
22
* publish, distribute, and/or sell copies of the Software, and to permit
23
* persons to whom the Software is furnished to do so, subject to the
24
* following conditions:
26
* 1. The above copyright notices and this permission notice (which
27
* includes the disclaimer below) shall be included in all copies or
28
* substantial portions of the Software.
30
* 2. The name of a copyright holder shall not be used to endorse or
31
* promote products derived from the Software without specific prior
34
* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35
* LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36
* THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37
* "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39
* PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
40
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41
* INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
45
* PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46
* THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47
* EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48
* BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49
* PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
50
* GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51
* ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
52
* IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53
* SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54
* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55
* SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56
* THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57
* PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58
* RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59
* EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
61
* __END_OF_JASPER_LICENSE__
65
* JPEG-2000 Code Stream Library
67
* $Id: jpc_cs.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $
73
/******************************************************************************\
75
\******************************************************************************/
77
#include "jasper/jas_image.h"
78
#include "jasper/jas_stream.h"
82
/******************************************************************************\
83
* Constants and Types.
84
\******************************************************************************/
86
/* The maximum number of resolution levels. */
87
#define JPC_MAXRLVLS 33
89
/* The maximum number of bands. */
90
#define JPC_MAXBANDS (3 * JPC_MAXRLVLS + 1)
92
/* The maximum number of layers. */
93
#define JPC_MAXLYRS 16384
95
/**************************************\
97
\**************************************/
100
* Code stream states.
104
#define JPC_CS_INIT 0
106
#define JPC_CS_MHDR 1
107
/* Tile-part header. */
108
#define JPC_CS_THDR 2
110
#define JPC_CS_MTLR 3
111
/* Tile-part data. */
112
#define JPC_CS_TDATA 4
115
* Unfortunately, the code stream syntax was not designed in such a way that
116
* any given marker segment can be correctly decoded without additional state
117
* derived from previously decoded marker segments.
118
* For example, a RGN/COC/QCC marker segment cannot be decoded unless the
119
* number of components is known.
123
* Code stream state information.
128
/* The number of components. */
129
uint_fast16_t numcomps;
133
/**************************************\
134
* SOT marker segment parameters.
135
\**************************************/
139
/* The tile number. */
140
uint_fast16_t tileno;
142
/* The combined length of the marker segment and its auxilary data
143
(i.e., packet data). */
146
/* The tile-part instance. */
149
/* The number of tile-parts. */
150
uint_fast8_t numparts;
154
/**************************************\
155
* SIZ marker segment parameters.
156
\**************************************/
158
/* Per component information. */
162
/* The precision of the samples. */
165
/* The signedness of the samples. */
168
/* The horizontal separation of samples with respect to the reference
172
/* The vertical separation of samples with respect to the reference
178
/* SIZ marker segment parameters. */
182
/* The code stream capabilities. */
185
/* The width of the image in units of the reference grid. */
188
/* The height of the image in units of the reference grid. */
189
uint_fast32_t height;
191
/* The horizontal offset from the origin of the reference grid to the
192
left side of the image area. */
195
/* The vertical offset from the origin of the reference grid to the
196
top side of the image area. */
199
/* The nominal width of a tile in units of the reference grid. */
200
uint_fast32_t tilewidth;
202
/* The nominal height of a tile in units of the reference grid. */
203
uint_fast32_t tileheight;
205
/* The horizontal offset from the origin of the reference grid to the
206
left side of the first tile. */
207
uint_fast32_t tilexoff;
209
/* The vertical offset from the origin of the reference grid to the
210
top side of the first tile. */
211
uint_fast32_t tileyoff;
213
/* The number of components. */
214
uint_fast16_t numcomps;
216
/* The per-component information. */
217
jpc_sizcomp_t *comps;
221
/**************************************\
222
* COD marker segment parameters.
223
\**************************************/
226
* Coding style constants.
229
/* Precincts may be used. */
230
#define JPC_COX_PRT 0x01
231
/* SOP marker segments may be used. */
232
#define JPC_COD_SOP 0x02
233
/* EPH marker segments may be used. */
234
#define JPC_COD_EPH 0x04
237
* Progression order constants.
240
/* Layer-resolution-component-precinct progressive
241
(i.e., progressive by fidelity). */
242
#define JPC_COD_LRCPPRG 0
243
/* Resolution-layer-component-precinct progressive
244
(i.e., progressive by resolution). */
245
#define JPC_COD_RLCPPRG 1
246
/* Resolution-precinct-component-layer progressive. */
247
#define JPC_COD_RPCLPRG 2
248
/* Precinct-component-resolution-layer progressive. */
249
#define JPC_COD_PCRLPRG 3
250
/* Component-position-resolution-layer progressive. */
251
#define JPC_COD_CPRLPRG 4
254
* Code block style constants.
257
#define JPC_COX_LAZY 0x01 /* Selective arithmetic coding bypass. */
258
#define JPC_COX_RESET 0x02 /* Reset context probabilities. */
259
#define JPC_COX_TERMALL 0x04 /* Terminate all coding passes. */
260
#define JPC_COX_VSC 0x08 /* Vertical stripe causal context formation. */
261
#define JPC_COX_PTERM 0x10 /* Predictable termination. */
262
#define JPC_COX_SEGSYM 0x20 /* Use segmentation symbols. */
264
/* Transform constants. */
265
#define JPC_COX_INS 0x00 /* Irreversible 9/7. */
266
#define JPC_COX_RFT 0x01 /* Reversible 5/3. */
268
/* Multicomponent transform constants. */
269
#define JPC_COD_NOMCT 0x00 /* No multicomponent transform. */
270
#define JPC_COD_MCT 0x01 /* Multicomponent transform. */
272
/* Get the code block size value from the code block size exponent. */
273
#define JPC_COX_CBLKSIZEEXPN(x) ((x) - 2)
274
/* Get the code block size exponent from the code block size value. */
275
#define JPC_COX_GETCBLKSIZEEXPN(x) ((x) + 2)
277
/* Per resolution-level information. */
281
/* The packet partition width. */
282
uint_fast8_t parwidthval;
284
/* The packet partition height. */
285
uint_fast8_t parheightval;
289
/* Per component information. */
293
/* The coding style. */
296
/* The number of decomposition levels. */
297
uint_fast8_t numdlvls;
299
/* The nominal code block width specifier. */
300
uint_fast8_t cblkwidthval;
302
/* The nominal code block height specifier. */
303
uint_fast8_t cblkheightval;
305
/* The style of coding passes. */
306
uint_fast8_t cblksty;
308
/* The QMFB employed. */
311
/* The number of resolution levels. */
314
/* The per-resolution-level information. */
315
jpc_coxrlvl_t rlvls[JPC_MAXRLVLS];
319
/* COD marker segment parameters. */
323
/* The general coding style. */
326
/* The progression order. */
329
/* The number of layers. */
330
uint_fast16_t numlyrs;
332
/* The multicomponent transform. */
333
uint_fast8_t mctrans;
335
/* Component-related parameters. */
336
jpc_coxcp_t compparms;
340
/* COC marker segment parameters. */
344
/* The component number. */
345
uint_fast16_t compno;
347
/* Component-related parameters. */
348
jpc_coxcp_t compparms;
352
/**************************************\
353
* RGN marker segment parameters.
354
\**************************************/
356
/* The maxshift ROI style. */
357
#define JPC_RGN_MAXSHIFT 0x00
361
/* The component to which the marker applies. */
362
uint_fast16_t compno;
367
/* The ROI shift value. */
368
uint_fast8_t roishift;
372
/**************************************\
373
* QCD/QCC marker segment parameters.
374
\**************************************/
377
* Quantization style constants.
380
#define JPC_QCX_NOQNT 0 /* No quantization. */
381
#define JPC_QCX_SIQNT 1 /* Scalar quantization, implicit. */
382
#define JPC_QCX_SEQNT 2 /* Scalar quantization, explicit. */
385
* Stepsize manipulation macros.
388
#define JPC_QCX_GETEXPN(x) ((x) >> 11)
389
#define JPC_QCX_GETMANT(x) ((x) & 0x07ff)
390
#define JPC_QCX_EXPN(x) (assert(!((x) & (~0x1f))), (((x) & 0x1f) << 11))
391
#define JPC_QCX_MANT(x) (assert(!((x) & (~0x7ff))), ((x) & 0x7ff))
393
/* Per component information. */
397
/* The quantization style. */
400
/* The number of step sizes. */
403
/* The step sizes. */
404
uint_fast16_t *stepsizes;
406
/* The number of guard bits. */
407
uint_fast8_t numguard;
411
/* QCC marker segment parameters. */
415
/* The component associated with this marker segment. */
416
uint_fast16_t compno;
418
/* The parameters. */
419
jpc_qcxcp_t compparms;
423
/* QCD marker segment parameters. */
427
/* The parameters. */
428
jpc_qcxcp_t compparms;
432
/**************************************\
433
* POD marker segment parameters.
434
\**************************************/
438
/* The progression order. */
441
/* The lower bound (inclusive) on the resolution level for the
442
progression order volume. */
443
uint_fast8_t rlvlnostart;
445
/* The upper bound (exclusive) on the resolution level for the
446
progression order volume. */
447
uint_fast8_t rlvlnoend;
449
/* The lower bound (inclusive) on the component for the progression
451
uint_fast16_t compnostart;
453
/* The upper bound (exclusive) on the component for the progression
455
uint_fast16_t compnoend;
457
/* The upper bound (exclusive) on the layer for the progression
459
uint_fast16_t lyrnoend;
463
/* An alias for the above type. */
464
typedef jpc_pocpchg_t jpc_pchg_t;
466
/* POC marker segment parameters. */
470
/* The number of progression order changes. */
473
/* The per-progression-order-change information. */
474
jpc_pocpchg_t *pchgs;
478
/**************************************\
479
* PPM/PPT marker segment parameters.
480
\**************************************/
482
/* PPM marker segment parameters. */
497
/* PPT marker segment parameters. */
512
/**************************************\
513
* COM marker segment parameters.
514
\**************************************/
520
#define JPC_COM_BIN 0x00
521
#define JPC_COM_LATIN 0x01
525
/* The registration ID. */
528
/* The length of the data in bytes. */
536
/**************************************\
537
* SOP marker segment parameters.
538
\**************************************/
542
/* The sequence number. */
547
/**************************************\
548
* CRG marker segment parameters.
549
\**************************************/
551
/* Per component information. */
555
/* The horizontal offset. */
558
/* The vertical offset. */
565
/* The number of components. */
568
/* Per component information. */
569
jpc_crgcomp_t *comps;
573
/**************************************\
574
* Marker segment parameters for unknown marker type.
575
\**************************************/
587
/**************************************\
588
* Generic marker segment parameters.
589
\**************************************/
592
int soc; /* unused */
594
int sod; /* unused */
595
int eoc; /* unused */
608
int eph; /* unused */
614
/**************************************\
616
\**************************************/
618
/* Marker segment IDs. */
620
/* The smallest valid marker value. */
621
#define JPC_MS_MIN 0xff00
623
/* The largest valid marker value. */
624
#define JPC_MS_MAX 0xffff
626
/* The minimum marker value that cannot occur within packet data. */
627
#define JPC_MS_INMIN 0xff80
628
/* The maximum marker value that cannot occur within packet data. */
629
#define JPC_MS_INMAX 0xffff
631
/* Delimiting marker segments. */
632
#define JPC_MS_SOC 0xff4f /* Start of code stream (SOC). */
633
#define JPC_MS_SOT 0xff90 /* Start of tile-part (SOT). */
634
#define JPC_MS_SOD 0xff93 /* Start of data (SOD). */
635
#define JPC_MS_EOC 0xffd9 /* End of code stream (EOC). */
637
/* Fixed information marker segments. */
638
#define JPC_MS_SIZ 0xff51 /* Image and tile size (SIZ). */
640
/* Functional marker segments. */
641
#define JPC_MS_COD 0xff52 /* Coding style default (COD). */
642
#define JPC_MS_COC 0xff53 /* Coding style component (COC). */
643
#define JPC_MS_RGN 0xff5e /* Region of interest (RGN). */
644
#define JPC_MS_QCD 0xff5c /* Quantization default (QCD). */
645
#define JPC_MS_QCC 0xff5d /* Quantization component (QCC). */
646
#define JPC_MS_POC 0xff5f /* Progression order default (POC). */
648
/* Pointer marker segments. */
649
#define JPC_MS_TLM 0xff55 /* Tile-part lengths, main header (TLM). */
650
#define JPC_MS_PLM 0xff57 /* Packet length, main header (PLM). */
651
#define JPC_MS_PLT 0xff58 /* Packet length, tile-part header (PLT). */
652
#define JPC_MS_PPM 0xff60 /* Packed packet headers, main header (PPM). */
653
#define JPC_MS_PPT 0xff61 /* Packet packet headers, tile-part header (PPT). */
655
/* In bit stream marker segments. */
656
#define JPC_MS_SOP 0xff91 /* Start of packet (SOP). */
657
#define JPC_MS_EPH 0xff92 /* End of packet header (EPH). */
659
/* Informational marker segments. */
660
#define JPC_MS_CRG 0xff63 /* Component registration (CRG). */
661
#define JPC_MS_COM 0xff64 /* Comment (COM). */
663
/* Forward declaration. */
666
/* Generic marker segment class. */
670
/* The type of marker segment. */
673
/* The length of the marker segment. */
676
/* The starting offset within the stream. */
679
/* The parameters of the marker segment. */
682
/* The marker segment operations. */
683
struct jpc_msops_s *ops;
687
/* Marker segment operations (which depend on the marker segment type). */
689
typedef struct jpc_msops_s {
691
/* Destroy the marker segment parameters. */
692
void (*destroyparms)(jpc_ms_t *ms);
694
/* Get the marker segment parameters from a stream. */
695
int (*getparms)(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);
697
/* Put the marker segment parameters to a stream. */
698
int (*putparms)(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);
700
/* Dump the marker segment parameters (for debugging). */
701
int (*dumpparms)(jpc_ms_t *ms, FILE *out);
705
/******************************************************************************\
707
\******************************************************************************/
709
/* Create a code-stream state object. */
710
jpc_cstate_t *jpc_cstate_create(void);
712
/* Destroy a code-stream state object. */
713
void jpc_cstate_destroy(jpc_cstate_t *cstate);
715
/* Create a marker segment. */
716
jpc_ms_t *jpc_ms_create(int type);
718
/* Destroy a marker segment. */
719
void jpc_ms_destroy(jpc_ms_t *ms);
721
/* Does a marker segment have parameters? */
722
#define JPC_MS_HASPARMS(x) \
723
(!((x) == JPC_MS_SOC || (x) == JPC_MS_SOD || (x) == JPC_MS_EOC || \
724
(x) == JPC_MS_EPH || ((x) >= 0xff30 && (x) <= 0xff3f)))
726
/* Get the marker segment type. */
727
#define jpc_ms_gettype(ms) \
730
/* Read a marker segment from a stream. */
731
jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate);
733
/* Write a marker segment to a stream. */
734
int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms);
736
/* Copy code stream data from one stream to another. */
737
int jpc_getdata(jas_stream_t *in, jas_stream_t *out, long n);
739
/* Copy code stream data from one stream to another. */
740
int jpc_putdata(jas_stream_t *out, jas_stream_t *in, long n);
742
/* Dump a marker segment (for debugging). */
743
void jpc_ms_dump(jpc_ms_t *ms, FILE *out);
745
/* Read a 8-bit unsigned integer from a stream. */
746
int jpc_getuint8(jas_stream_t *in, uint_fast8_t *val);
748
/* Read a 16-bit unsigned integer from a stream. */
749
int jpc_getuint16(jas_stream_t *in, uint_fast16_t *val);
751
/* Read a 32-bit unsigned integer from a stream. */
752
int jpc_getuint32(jas_stream_t *in, uint_fast32_t *val);
754
/* Write a 8-bit unsigned integer to a stream. */
755
int jpc_putuint8(jas_stream_t *out, uint_fast8_t val);
757
/* Write a 16-bit unsigned integer to a stream. */
758
int jpc_putuint16(jas_stream_t *out, uint_fast16_t val);
760
/* Write a 32-bit unsigned integer to a stream. */
761
int jpc_putuint32(jas_stream_t *out, uint_fast32_t val);