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 Software License
11  *
12  * IMAGE POWER JPEG-2000 PUBLIC LICENSE
13  * ************************************
14  *
15  * GRANT:
16  *
17  * Permission is hereby granted, free of charge, to any person (the "User")
18  * obtaining a copy of this software and associated documentation, to deal
19  * in the JasPer Software without restriction, including without limitation
20  * the right to use, copy, modify, merge, publish, distribute, sublicense,
21  * and/or sell copies of the JasPer Software (in source and binary forms),
22  * and to permit persons to whom the JasPer Software is furnished to do so,
23  * provided further that the License Conditions below are met.
24  *
25  * License Conditions
26  * ******************
27  *
28  * A.  Redistributions of source code must retain the above copyright notice,
29  * and this list of conditions, and the following disclaimer.
30  *
31  * B.  Redistributions in binary form must reproduce the above copyright
32  * notice, and this list of conditions, and the following disclaimer in
33  * the documentation and/or other materials provided with the distribution.
34  *
35  * C.  Neither the name of Image Power, Inc. nor any other contributor
36  * (including, but not limited to, the University of British Columbia and
37  * Michael David Adams) may be used to endorse or promote products derived
38  * from this software without specific prior written permission.
39  *
40  * D.  User agrees that it shall not commence any action against Image Power,
41  * Inc., the University of British Columbia, Michael David Adams, or any
42  * other contributors (collectively "Licensors") for infringement of any
43  * intellectual property rights ("IPR") held by the User in respect of any
44  * technology that User owns or has a right to license or sublicense and
45  * which is an element required in order to claim compliance with ISO/IEC
46  * 15444-1 (i.e., JPEG-2000 Part 1).  "IPR" means all intellectual property
47  * rights worldwide arising under statutory or common law, and whether
48  * or not perfected, including, without limitation, all (i) patents and
49  * patent applications owned or licensable by User; (ii) rights associated
50  * with works of authorship including copyrights, copyright applications,
51  * copyright registrations, mask work rights, mask work applications,
52  * mask work registrations; (iii) rights relating to the protection of
53  * trade secrets and confidential information; (iv) any right analogous
54  * to those set forth in subsections (i), (ii), or (iii) and any other
55  * proprietary rights relating to intangible property (other than trademark,
56  * trade dress, or service mark rights); and (v) divisions, continuations,
57  * renewals, reissues and extensions of the foregoing (as and to the extent
58  * applicable) now existing, hereafter filed, issued or acquired.
59  *
60  * E.  If User commences an infringement action against any Licensor(s) then
61  * such Licensor(s) shall have the right to terminate User's license and
62  * all sublicenses that have been granted hereunder by User to other parties.
63  *
64  * F.  This software is for use only in hardware or software products that
65  * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1).  No license
66  * or right to this Software is granted for products that do not comply
67  * with ISO/IEC 15444-1.  The JPEG-2000 Part 1 standard can be purchased
68  * from the ISO.
69  *
70  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
71  * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
72  * THIS DISCLAIMER.  THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
73  * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
74  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
75  * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
76  * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING.  THOSE INTENDING
77  * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
78  * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
79  * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
80  * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
81  * IS WITH THE USER.  SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
82  * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
83  * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
84  * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
85  * REPAIR OR CORRECTION.  UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
86  * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
87  * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
88  * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
89  * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
90  * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
91  * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
92  * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
93  * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
94  * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
95  * OF SUCH DAMAGES.  THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
96  * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
97  * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
98  * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
99  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
100  * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
101  * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
102  * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
103  * ("HIGH RISK ACTIVITIES").  LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
104  * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.  USER WILL NOT
105  * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
106  * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
107  * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
108  * NOTICE SPECIFIED IN THIS SECTION.
109  *
110  * __END_OF_JASPER_LICENSE__
111  */
112 
113 /*
114  * JP2 Library
115  *
116  * $Id$
117  */
118 
119 /******************************************************************************\
120 * Includes.
121 \******************************************************************************/
122 
123 #include "jasper/jas_malloc.h"
124 #include "jasper/jas_image.h"
125 #include "jasper/jas_stream.h"
126 
127 #include "jp2_cod.h"
128 
129 static uint_fast32_t jp2_gettypeasoc(int colorspace, int ctype);
130 
131 /******************************************************************************\
132 * Functions.
133 \******************************************************************************/
134 
jp2_encode(jas_image_t * image,jas_stream_t * out,char * optstr)135 int jp2_encode(jas_image_t *image, jas_stream_t *out, char *optstr)
136 {
137 	jp2_box_t *box;
138 	jp2_ftyp_t *ftyp;
139 	jp2_ihdr_t *ihdr;
140 	jas_stream_t *tmpstream;
141 	int allcmptssame;
142 	jp2_bpcc_t *bpcc;
143 	long len;
144 	uint_fast16_t cmptno;
145 	jp2_colr_t *colr;
146 	char buf[4096];
147 	uint_fast32_t overhead;
148 	jp2_cdefchan_t *cdefchanent;
149 	jp2_cdef_t *cdef;
150 	int i;
151 	uint_fast32_t typeasoc;
152 
153 	box = 0;
154 	tmpstream = 0;
155 
156 	/* Output the signature box. */
157 
158 	if (!(box = jp2_box_create(JP2_BOX_JP))) {
159 		goto error;
160 	}
161 	box->data.jp.magic = JP2_JP_MAGIC;
162 	if (jp2_box_put(box, out)) {
163 		goto error;
164 	}
165 	jp2_box_destroy(box);
166 	box = 0;
167 
168 	/* Output the file type box. */
169 
170 	if (!(box = jp2_box_create(JP2_BOX_FTYP))) {
171 		goto error;
172 	}
173 	ftyp = &box->data.ftyp;
174 	ftyp->majver = JP2_FTYP_MAJVER;
175 	ftyp->minver = JP2_FTYP_MINVER;
176 	ftyp->numcompatcodes = 1;
177 	ftyp->compatcodes[0] = JP2_FTYP_COMPATCODE;
178 	if (jp2_box_put(box, out)) {
179 		goto error;
180 	}
181 	jp2_box_destroy(box);
182 	box = 0;
183 
184 	/*
185 	 * Generate the data portion of the JP2 header box.
186 	 * We cannot simply output the header for this box
187 	 * since we do not yet know the correct value for the length
188 	 * field.
189 	 */
190 
191 	if (!(tmpstream = jas_stream_memopen(0, 0))) {
192 		goto error;
193 	}
194 
195 	/* Generate image header box. */
196 
197 	if (!(box = jp2_box_create(JP2_BOX_IHDR))) {
198 		goto error;
199 	}
200 	ihdr = &box->data.ihdr;
201 	ihdr->width = jas_image_width(image);
202 	ihdr->height = jas_image_height(image);
203 	ihdr->numcmpts = jas_image_numcmpts(image);
204 	allcmptssame = 0;
205 	ihdr->bpc = allcmptssame ? JP2_SPTOBPC(jas_image_cmptsgnd(image, 0),
206 	  jas_image_cmptprec(image, 0)) : JP2_IHDR_BPCNULL;
207 	ihdr->comptype = JP2_IHDR_COMPTYPE;
208 	ihdr->csunk = 0;
209 	ihdr->ipr = 0;
210 	if (jp2_box_put(box, tmpstream)) {
211 		goto error;
212 	}
213 	jp2_box_destroy(box);
214 	box = 0;
215 
216 	/* Generate bits per component box. */
217 
218 	if (!allcmptssame) {
219 		if (!(box = jp2_box_create(JP2_BOX_BPCC))) {
220 			goto error;
221 		}
222 		bpcc = &box->data.bpcc;
223 		bpcc->numcmpts = jas_image_numcmpts(image);
224 		if (!(bpcc->bpcs = jas_malloc(bpcc->numcmpts *
225 		  sizeof(uint_fast8_t)))) {
226 			goto error;
227 		}
228 		for (cmptno = 0; cmptno < bpcc->numcmpts; ++cmptno) {
229 			bpcc->bpcs[cmptno] = JP2_SPTOBPC(jas_image_cmptsgnd(image,
230 			  cmptno), jas_image_cmptprec(image, cmptno));
231 		}
232 		if (jp2_box_put(box, tmpstream)) {
233 			goto error;
234 		}
235 		jp2_box_destroy(box);
236 		box = 0;
237 	}
238 
239 	/* Generate color specification box. */
240 
241 	if (!(box = jp2_box_create(JP2_BOX_COLR))) {
242 		goto error;
243 	}
244 	colr = &box->data.colr;
245 	colr->method = JP2_COLR_ENUM;
246 	colr->pri = JP2_COLR_PRI;
247 	colr->approx = 0;
248 	colr->csid = (jas_image_colorspace(image) == JAS_IMAGE_CS_RGB) ? JP2_COLR_SRGB :
249 	  JP2_COLR_SGRAY;
250 	if (jp2_box_put(box, tmpstream)) {
251 		goto error;
252 	}
253 	jp2_box_destroy(box);
254 	box = 0;
255 
256 	if (!(jas_image_colorspace(image) == JAS_IMAGE_CS_RGB &&
257 	  jas_image_numcmpts(image) == 3 &&
258 	  jas_image_getcmptbytype(image, 0) ==
259 	  JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R) &&
260 	  jas_image_getcmptbytype(image, 1) ==
261 	  JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G) &&
262 	  jas_image_getcmptbytype(image, 2) ==
263 	  JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B)) &&
264 	  !(jas_image_colorspace(image) == JAS_IMAGE_CS_YCBCR &&
265 	  jas_image_numcmpts(image) != 3 &&
266 	  jas_image_getcmptbytype(image, 0) ==
267 	  JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_Y) &&
268 	  jas_image_getcmptbytype(image, 1) ==
269 	  JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CB) &&
270 	  jas_image_getcmptbytype(image, 2) ==
271 	  JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_YCBCR_CR)) &&
272 	  !(jas_image_colorspace(image) == JAS_IMAGE_CS_GRAY &&
273 	  jas_image_numcmpts(image) == 1 &&
274 	  jas_image_getcmptbytype(image, 0) ==
275 	  JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y))) {
276 
277 		if (!(box = jp2_box_create(JP2_BOX_CDEF))) {
278 			goto error;
279 		}
280 		cdef = &box->data.cdef;
281 		cdef->numchans = jas_image_numcmpts(image);
282 		cdef->ents = jas_malloc(cdef->numchans * sizeof(jp2_cdefchan_t));
283 		for (i = 0; i < jas_image_numcmpts(image); ++i) {
284 			cdefchanent = &cdef->ents[i];
285 			cdefchanent->channo = i;
286 			typeasoc = jp2_gettypeasoc(jas_image_colorspace(image), jas_image_cmpttype(image, i));
287 			cdefchanent->type = typeasoc >> 16;
288 			cdefchanent->assoc = typeasoc & 0x7fff;
289 		}
290 		jp2_box_destroy(box);
291 		box = 0;
292 	}
293 
294 	/* Determine the total length of the JP2 header box. */
295 
296 	len = jas_stream_tell(tmpstream);
297 	jas_stream_rewind(tmpstream);
298 
299 	/*
300 	 * Output the JP2 header box and all of the boxes which it contains.
301 	 */
302 
303 	if (!(box = jp2_box_create(JP2_BOX_JP2H))) {
304 		goto error;
305 	}
306 	box->len = len + JP2_BOX_HDRLEN;
307 	if (jp2_box_put(box, out)) {
308 		goto error;
309 	}
310 	jp2_box_destroy(box);
311 	box = 0;
312 
313 	if (jas_stream_copy(out, tmpstream, len)) {
314 		goto error;
315 	}
316 
317 	jas_stream_close(tmpstream);
318 	tmpstream = 0;
319 
320 	/*
321 	 * Output the contiguous code stream box.
322 	 */
323 
324 	if (!(box = jp2_box_create(JP2_BOX_JP2C))) {
325 		goto error;
326 	}
327 	box->len = 0;
328 	if (jp2_box_put(box, out)) {
329 		goto error;
330 	}
331 	jp2_box_destroy(box);
332 	box = 0;
333 
334 	/* Output the JPEG-2000 code stream. */
335 
336 	overhead = jas_stream_getrwcount(out);
337 	sprintf(buf, "%s\n_jp2overhead=%lu\n", (optstr ? optstr : ""),
338 	  (unsigned long) overhead);
339 
340 	if (jpc_encode(image, out, buf)) {
341 		goto error;
342 	}
343 
344 	return 0;
345 	abort();
346 
347 error:
348 
349 	if (box) {
350 		jp2_box_destroy(box);
351 	}
352 	if (tmpstream) {
353 		jas_stream_close(tmpstream);
354 	}
355 	return -1;
356 }
357 
358 
jp2_gettypeasoc(int colorspace,int ctype)359 static uint_fast32_t jp2_gettypeasoc(int colorspace, int ctype)
360 {
361 	int type;
362 	int asoc;
363 
364 	if (ctype & JAS_IMAGE_CT_OPACITY) {
365 		type = JP2_CDEF_TYPE_OPACITY;
366 		asoc = JP2_CDEF_ASOC_ALL;
367 		goto done;
368 	}
369 
370 	type = JP2_CDEF_TYPE_UNSPEC;
371 	asoc = JP2_CDEF_ASOC_NONE;
372 	switch (colorspace) {
373 	case JAS_IMAGE_CS_RGB:
374 		switch (JAS_IMAGE_CT_COLOR(ctype)) {
375 		case JAS_IMAGE_CT_RGB_R:
376 			type = JP2_CDEF_TYPE_COLOR;
377 			asoc = JP2_CDEF_RGB_R;
378 			break;
379 		case JAS_IMAGE_CT_RGB_G:
380 			type = JP2_CDEF_TYPE_COLOR;
381 			asoc = JP2_CDEF_RGB_G;
382 			break;
383 		case JAS_IMAGE_CT_RGB_B:
384 			type = JP2_CDEF_TYPE_COLOR;
385 			asoc = JP2_CDEF_RGB_B;
386 			break;
387 		}
388 		break;
389 	case JAS_IMAGE_CS_YCBCR:
390 		switch (JAS_IMAGE_CT_COLOR(ctype)) {
391 		case JAS_IMAGE_CT_YCBCR_Y:
392 			type = JP2_CDEF_TYPE_COLOR;
393 			asoc = JP2_CDEF_YCBCR_Y;
394 			break;
395 		case JAS_IMAGE_CT_YCBCR_CB:
396 			type = JP2_CDEF_TYPE_COLOR;
397 			asoc = JP2_CDEF_YCBCR_CB;
398 			break;
399 		case JAS_IMAGE_CT_YCBCR_CR:
400 			type = JP2_CDEF_TYPE_COLOR;
401 			asoc = JP2_CDEF_YCBCR_CR;
402 			break;
403 		}
404 		break;
405 	case JAS_IMAGE_CS_GRAY:
406 		type = JP2_CDEF_TYPE_COLOR;
407 		asoc = JP2_CDEF_GRAY_Y;
408 		break;
409 	}
410 
411 done:
412 	return (type << 16) | asoc;
413 }
414 
415