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 /*
9  * Modified by Andrey Kiselev <dron@remotesensing.org> to handle UUID
10  * box properly.
11  */
12 
13 /* __START_OF_JASPER_LICENSE__
14  *
15  * JasPer License Version 2.0
16  *
17  * Copyright (c) 2001-2006 Michael David Adams
18  * Copyright (c) 1999-2000 Image Power, Inc.
19  * Copyright (c) 1999-2000 The University of British Columbia
20  *
21  * All rights reserved.
22  *
23  * Permission is hereby granted, free of charge, to any person (the
24  * "User") obtaining a copy of this software and associated documentation
25  * files (the "Software"), to deal in the Software without restriction,
26  * including without limitation the rights to use, copy, modify, merge,
27  * publish, distribute, and/or sell copies of the Software, and to permit
28  * persons to whom the Software is furnished to do so, subject to the
29  * following conditions:
30  *
31  * 1.  The above copyright notices and this permission notice (which
32  * includes the disclaimer below) shall be included in all copies or
33  * substantial portions of the Software.
34  *
35  * 2.  The name of a copyright holder shall not be used to endorse or
36  * promote products derived from the Software without specific prior
37  * written permission.
38  *
39  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
40  * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
41  * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
42  * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
43  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
44  * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
45  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
46  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
47  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
48  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
49  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
50  * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
51  * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
52  * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
53  * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
54  * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
55  * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
56  * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
57  * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
58  * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
59  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
60  * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
61  * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
62  * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
63  * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
64  * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
65  *
66  * __END_OF_JASPER_LICENSE__
67  */
68 
69 /*
70  * JP2 Library
71  *
72  * $Id: jp2_cod.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $
73  */
74 
75 #ifndef JP2_COD_H
76 #define JP2_COD_H
77 
78 /******************************************************************************\
79 * Includes.
80 \******************************************************************************/
81 
82 #include "jasper/jas_types.h"
83 
84 /******************************************************************************\
85 * Macros.
86 \******************************************************************************/
87 
88 #define	JP2_SPTOBPC(s, p) \
89     ((((p) - 1) & 0x7f) | (((s) & 1) << 7))
90 
91 /******************************************************************************\
92 * Box class.
93 \******************************************************************************/
94 
95 #define	JP2_BOX_HDRLEN(ext) ((ext) ? 16 : 8)
96 
97 /* Box types. */
98 #define	JP2_BOX_JP		0x6a502020	/* Signature */
99 #define JP2_BOX_FTYP	0x66747970	/* File Type */
100 #define	JP2_BOX_JP2H	0x6a703268	/* JP2 Header */
101 #define	JP2_BOX_IHDR	0x69686472	/* Image Header */
102 #define	JP2_BOX_BPCC	0x62706363	/* Bits Per Component */
103 #define	JP2_BOX_COLR	0x636f6c72	/* Color Specification */
104 #define	JP2_BOX_PCLR	0x70636c72	/* Palette */
105 #define	JP2_BOX_CMAP	0x636d6170	/* Component Mapping */
106 #define	JP2_BOX_CDEF	0x63646566	/* Channel Definition */
107 #define	JP2_BOX_RES		0x72657320	/* Resolution */
108 #define	JP2_BOX_RESC	0x72657363	/* Capture Resolution */
109 #define	JP2_BOX_RESD	0x72657364	/* Default Display Resolution */
110 #define	JP2_BOX_JP2C	0x6a703263	/* Contiguous Code Stream */
111 #define	JP2_BOX_JP2I	0x6a703269	/* Intellectual Property */
112 #define	JP2_BOX_XML		0x786d6c20	/* XML */
113 #define	JP2_BOX_UUID	0x75756964	/* UUID */
114 #define	JP2_BOX_UINF	0x75696e66	/* UUID Info */
115 #define	JP2_BOX_ULST	0x75637374	/* UUID List */
116 #define	JP2_BOX_URL		0x75726c20	/* URL */
117 
118 #define	JP2_BOX_SUPER	0x01
119 #define	JP2_BOX_NODATA	0x02
120 
121 /* JP box data. */
122 
123 #define	JP2_JP_MAGIC	0x0d0a870a
124 #define	JP2_JP_LEN		12
125 
126 typedef struct {
127     uint_fast32_t magic;
128 } jp2_jp_t;
129 
130 /* FTYP box data. */
131 
132 #define	JP2_FTYP_MAXCOMPATCODES	32
133 #define	JP2_FTYP_MAJVER		0x6a703220
134 #define	JP2_FTYP_MINVER		0
135 #define	JP2_FTYP_COMPATCODE		JP2_FTYP_MAJVER
136 
137 typedef struct {
138     uint_fast32_t majver;
139     uint_fast32_t minver;
140     uint_fast32_t numcompatcodes;
141     uint_fast32_t compatcodes[JP2_FTYP_MAXCOMPATCODES];
142 } jp2_ftyp_t;
143 
144 /* IHDR box data. */
145 
146 #define	JP2_IHDR_COMPTYPE	7
147 #define	JP2_IHDR_BPCNULL	255
148 
149 typedef struct {
150     uint_fast32_t width;
151     uint_fast32_t height;
152     uint_fast16_t numcmpts;
153     uint_fast8_t bpc;
154     uint_fast8_t comptype;
155     uint_fast8_t csunk;
156     uint_fast8_t ipr;
157 } jp2_ihdr_t;
158 
159 /* BPCC box data. */
160 
161 typedef struct {
162     uint_fast16_t numcmpts;
163     uint_fast8_t *bpcs;
164 } jp2_bpcc_t;
165 
166 /* COLR box data. */
167 
168 #define	JP2_COLR_ENUM	1
169 #define	JP2_COLR_ICC	2
170 #define	JP2_COLR_PRI	0
171 
172 #define	JP2_COLR_SRGB	16
173 #define	JP2_COLR_SGRAY	17
174 #define	JP2_COLR_SYCC	18
175 
176 typedef struct {
177     uint_fast8_t method;
178     uint_fast8_t pri;
179     uint_fast8_t approx;
180     uint_fast32_t csid;
181     uint_fast8_t *iccp;
182     int iccplen;
183     /* XXX - Someday we ought to add ICC profile data here. */
184 } jp2_colr_t;
185 
186 /* PCLR box data. */
187 
188 typedef struct {
189     uint_fast16_t numlutents;
190     uint_fast8_t numchans;
191     int_fast32_t *lutdata;
192     uint_fast8_t *bpc;
193 } jp2_pclr_t;
194 
195 /* CDEF box per-channel data. */
196 
197 #define JP2_CDEF_RGB_R	1
198 #define JP2_CDEF_RGB_G	2
199 #define JP2_CDEF_RGB_B	3
200 
201 #define JP2_CDEF_YCBCR_Y	1
202 #define JP2_CDEF_YCBCR_CB	2
203 #define JP2_CDEF_YCBCR_CR	3
204 
205 #define	JP2_CDEF_GRAY_Y	1
206 
207 #define	JP2_CDEF_TYPE_COLOR	0
208 #define	JP2_CDEF_TYPE_OPACITY	1
209 #define	JP2_CDEF_TYPE_UNSPEC	65535
210 #define	JP2_CDEF_ASOC_ALL	0
211 #define	JP2_CDEF_ASOC_NONE	65535
212 
213 typedef struct {
214     uint_fast16_t channo;
215     uint_fast16_t type;
216     uint_fast16_t assoc;
217 } jp2_cdefchan_t;
218 
219 /* CDEF box data. */
220 
221 typedef struct {
222     uint_fast16_t numchans;
223     jp2_cdefchan_t *ents;
224 } jp2_cdef_t;
225 
226 typedef struct {
227     uint_fast16_t cmptno;
228     uint_fast8_t map;
229     uint_fast8_t pcol;
230 } jp2_cmapent_t;
231 
232 typedef struct {
233     uint_fast16_t numchans;
234     jp2_cmapent_t *ents;
235 } jp2_cmap_t;
236 
237 typedef struct {
238     uint_fast32_t datalen;
239     uint_fast8_t uuid[16];
240     uint_fast8_t *data;
241 } jp2_uuid_t;
242 
243 #define	JP2_CMAP_DIRECT		0
244 #define	JP2_CMAP_PALETTE	1
245 
246 /* Generic box. */
247 
248 struct jp2_boxops_s;
249 typedef struct {
250 
251     struct jp2_boxops_s *ops;
252     struct jp2_boxinfo_s *info;
253 
254     uint_fast32_t type;
255 
256     /* The length of the box including the (variable-length) header. */
257     uint_fast32_t len;
258 
259     /* The length of the box data. */
260     uint_fast32_t datalen;
261 
262     union {
263         jp2_jp_t jp;
264         jp2_ftyp_t ftyp;
265         jp2_ihdr_t ihdr;
266         jp2_bpcc_t bpcc;
267         jp2_colr_t colr;
268         jp2_pclr_t pclr;
269         jp2_cdef_t cdef;
270         jp2_cmap_t cmap;
271         jp2_uuid_t uuid;
272     } data;
273 
274 } jp2_box_t;
275 
276 typedef struct jp2_boxops_s {
277     void (*init)(jp2_box_t *box);
278     void (*destroy)(jp2_box_t *box);
279     int (*getdata)(jp2_box_t *box, jas_stream_t *in);
280     int (*putdata)(jp2_box_t *box, jas_stream_t *out);
281     void (*dumpdata)(jp2_box_t *box, FILE *out);
282 } jp2_boxops_t;
283 
284 /******************************************************************************\
285 *
286 \******************************************************************************/
287 
288 typedef struct jp2_boxinfo_s {
289     int type;
290     char *name;
291     int flags;
292     jp2_boxops_t ops;
293 } jp2_boxinfo_t;
294 
295 /******************************************************************************\
296 * Box class.
297 \******************************************************************************/
298 
299 jp2_box_t *jp2_box_create(int type);
300 void jp2_box_destroy(jp2_box_t *box);
301 jp2_box_t *jp2_box_get(jas_stream_t *in);
302 int jp2_box_put(jp2_box_t *box, jas_stream_t *out);
303 
304 #define JP2_DTYPETOBPC(dtype) \
305   ((JAS_IMAGE_CDT_GETSGND(dtype) << 7) | (JAS_IMAGE_CDT_GETPREC(dtype) - 1))
306 #define	JP2_BPCTODTYPE(bpc) \
307   (JAS_IMAGE_CDT_SETSGND(bpc >> 7) | JAS_IMAGE_CDT_SETPREC((bpc & 0x7f) + 1))
308 
309 #define ICC_CS_RGB	0x52474220
310 #define ICC_CS_YCBCR	0x59436272
311 #define ICC_CS_GRAY	0x47524159
312 
313 jp2_cdefchan_t *jp2_cdef_lookup(jp2_cdef_t *cdef, int channo);
314 
315 
316 #endif
317