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