Move the sources to trunk
[opencv] / otherlibs / _graphics / src / libjasper / jp2_cod.h
1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  *   British Columbia.
4  * Copyright (c) 2001-2002 Michael David Adams.
5  * All rights reserved.
6  */
7
8 /* __START_OF_JASPER_LICENSE__
9  * 
10  * JasPer License Version 2.0
11  * 
12  * Copyright (c) 1999-2000 Image Power, Inc.
13  * Copyright (c) 1999-2000 The University of British Columbia
14  * Copyright (c) 2001-2003 Michael David Adams
15  * 
16  * All rights reserved.
17  * 
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:
25  * 
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.
29  * 
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
32  * written permission.
33  * 
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.
60  * 
61  * __END_OF_JASPER_LICENSE__
62  */
63
64 /*
65  * JP2 Library
66  *
67  * $Id: jp2_cod.h,v 1.1 2007/01/15 16:09:24 vp153 Exp $
68  */
69
70 #ifndef JP2_COD_H
71 #define JP2_COD_H
72
73 /******************************************************************************\
74 * Includes.
75 \******************************************************************************/
76
77 #include "jasper/jas_types.h"
78
79 /******************************************************************************\
80 * Macros.
81 \******************************************************************************/
82
83 #define JP2_SPTOBPC(s, p) \
84         ((((p) - 1) & 0x7f) | (((s) & 1) << 7))
85
86 /******************************************************************************\
87 * Box class.
88 \******************************************************************************/
89
90 #define JP2_BOX_HDRLEN  8
91
92 /* Box types. */
93 #define JP2_BOX_JP              0x6a502020      /* Signature */
94 #define JP2_BOX_FTYP    0x66747970      /* File Type */
95 #define JP2_BOX_JP2H    0x6a703268      /* JP2 Header */
96 #define JP2_BOX_IHDR    0x69686472      /* Image Header */
97 #define JP2_BOX_BPCC    0x62706363      /* Bits Per Component */
98 #define JP2_BOX_COLR    0x636f6c72      /* Color Specification */
99 #define JP2_BOX_PCLR    0x70636c72      /* Palette */
100 #define JP2_BOX_CMAP    0x636d6170      /* Component Mapping */
101 #define JP2_BOX_CDEF    0x63646566      /* Channel Definition */
102 #define JP2_BOX_RES             0x72657320      /* Resolution */
103 #define JP2_BOX_RESC    0x72657363      /* Capture Resolution */
104 #define JP2_BOX_RESD    0x72657364      /* Default Display Resolution */
105 #define JP2_BOX_JP2C    0x6a703263      /* Contiguous Code Stream */
106 #define JP2_BOX_JP2I    0x6a703269      /* Intellectual Property */
107 #define JP2_BOX_XML             0x786d6c20      /* XML */
108 #define JP2_BOX_UUID    0x75756964      /* UUID */
109 #define JP2_BOX_UINF    0x75696e66      /* UUID Info */
110 #define JP2_BOX_ULST    0x75637374      /* UUID List */
111 #define JP2_BOX_URL             0x75726c20      /* URL */
112
113 #define JP2_BOX_SUPER   0x01
114 #define JP2_BOX_NODATA  0x02
115
116 /* JP box data. */
117
118 #define JP2_JP_MAGIC    0x0d0a870a
119 #define JP2_JP_LEN              12
120
121 typedef struct {
122         uint_fast32_t magic;
123 } jp2_jp_t;
124
125 /* FTYP box data. */
126
127 #define JP2_FTYP_MAXCOMPATCODES 32
128 #define JP2_FTYP_MAJVER         0x6a703220
129 #define JP2_FTYP_MINVER         0
130 #define JP2_FTYP_COMPATCODE             JP2_FTYP_MAJVER
131
132 typedef struct {
133         uint_fast32_t majver;
134         uint_fast32_t minver;
135         uint_fast32_t numcompatcodes;
136         uint_fast32_t compatcodes[JP2_FTYP_MAXCOMPATCODES];
137 } jp2_ftyp_t;
138
139 /* IHDR box data. */
140
141 #define JP2_IHDR_COMPTYPE       7
142 #define JP2_IHDR_BPCNULL        255
143
144 typedef struct {
145         uint_fast32_t width;
146         uint_fast32_t height;
147         uint_fast16_t numcmpts;
148         uint_fast8_t bpc;
149         uint_fast8_t comptype;
150         uint_fast8_t csunk;
151         uint_fast8_t ipr;
152 } jp2_ihdr_t;
153
154 /* BPCC box data. */
155
156 typedef struct {
157         uint_fast16_t numcmpts;
158         uint_fast8_t *bpcs;
159 } jp2_bpcc_t;
160
161 /* COLR box data. */
162
163 #define JP2_COLR_ENUM   1
164 #define JP2_COLR_ICC    2
165 #define JP2_COLR_PRI    0
166
167 #define JP2_COLR_SRGB   16
168 #define JP2_COLR_SGRAY  17
169 #define JP2_COLR_SYCC   18
170
171 typedef struct {
172         uint_fast8_t method;
173         uint_fast8_t pri;
174         uint_fast8_t approx;
175         uint_fast32_t csid;
176         uint_fast8_t *iccp;
177         int iccplen;
178         /* XXX - Someday we ought to add ICC profile data here. */
179 } jp2_colr_t;
180
181 /* PCLR box data. */
182
183 typedef struct {
184         uint_fast16_t numlutents;
185         uint_fast8_t numchans;
186         int_fast32_t *lutdata;
187         uint_fast8_t *bpc;
188 } jp2_pclr_t;
189
190 /* CDEF box per-channel data. */
191
192 #define JP2_CDEF_RGB_R  1
193 #define JP2_CDEF_RGB_G  2
194 #define JP2_CDEF_RGB_B  3
195
196 #define JP2_CDEF_YCBCR_Y        1
197 #define JP2_CDEF_YCBCR_CB       2
198 #define JP2_CDEF_YCBCR_CR       3
199
200 #define JP2_CDEF_GRAY_Y 1
201
202 #define JP2_CDEF_TYPE_COLOR     0
203 #define JP2_CDEF_TYPE_OPACITY   1
204 #define JP2_CDEF_TYPE_UNSPEC    65535
205 #define JP2_CDEF_ASOC_ALL       0
206 #define JP2_CDEF_ASOC_NONE      65535
207
208 typedef struct {
209         uint_fast16_t channo;
210         uint_fast16_t type;
211         uint_fast16_t assoc;
212 } jp2_cdefchan_t;
213
214 /* CDEF box data. */
215
216 typedef struct {
217         uint_fast16_t numchans;
218         jp2_cdefchan_t *ents;
219 } jp2_cdef_t;
220
221 typedef struct {
222         uint_fast16_t cmptno;
223         uint_fast8_t map;
224         uint_fast8_t pcol;
225 } jp2_cmapent_t;
226
227 typedef struct {
228         uint_fast16_t numchans;
229         jp2_cmapent_t *ents;
230 } jp2_cmap_t;
231
232 #define JP2_CMAP_DIRECT         0
233 #define JP2_CMAP_PALETTE        1
234
235 /* Generic box. */
236
237 struct jp2_boxops_s;
238 typedef struct {
239
240         struct jp2_boxops_s *ops;
241         struct jp2_boxinfo_s *info;
242
243         uint_fast32_t type;
244         uint_fast32_t len;
245
246         union {
247                 jp2_jp_t jp;
248                 jp2_ftyp_t ftyp;
249                 jp2_ihdr_t ihdr;
250                 jp2_bpcc_t bpcc;
251                 jp2_colr_t colr;
252                 jp2_pclr_t pclr;
253                 jp2_cdef_t cdef;
254                 jp2_cmap_t cmap;
255         } data;
256
257 } jp2_box_t;
258
259 typedef struct jp2_boxops_s {
260         void (*init)(jp2_box_t *box);
261         void (*destroy)(jp2_box_t *box);
262         int (*getdata)(jp2_box_t *box, jas_stream_t *in);
263         int (*putdata)(jp2_box_t *box, jas_stream_t *out);
264         void (*dumpdata)(jp2_box_t *box, FILE *out);
265 } jp2_boxops_t;
266
267 /******************************************************************************\
268 *
269 \******************************************************************************/
270
271 typedef struct jp2_boxinfo_s {
272         int type;
273         char *name;
274         int flags;
275         jp2_boxops_t ops;
276 } jp2_boxinfo_t;
277
278 /******************************************************************************\
279 * Box class.
280 \******************************************************************************/
281
282 jp2_box_t *jp2_box_create(int type);
283 void jp2_box_destroy(jp2_box_t *box);
284 jp2_box_t *jp2_box_get(jas_stream_t *in);
285 int jp2_box_put(jp2_box_t *box, jas_stream_t *out);
286
287 #define JP2_DTYPETOBPC(dtype) \
288   ((JAS_IMAGE_CDT_GETSGND(dtype) << 7) | (JAS_IMAGE_CDT_GETPREC(dtype) - 1))
289 #define JP2_BPCTODTYPE(bpc) \
290   (JAS_IMAGE_CDT_SETSGND(bpc >> 7) | JAS_IMAGE_CDT_SETPREC((bpc & 0x7f) + 1))
291
292 #define ICC_CS_RGB      0x52474220
293 #define ICC_CS_YCBCR    0x59436272
294 #define ICC_CS_GRAY     0x47524159
295
296 jp2_cdefchan_t *jp2_cdef_lookup(jp2_cdef_t *cdef, int channo);
297
298
299 #endif