2
* Copyright (c) 1999-2000 Image Power, Inc. and the University of
4
* Copyright (c) 2001-2003 Michael David Adams.
9
* Modified by Andrey Kiselev <dron@remotesensing.org> to handle UUID
13
/* __START_OF_JASPER_LICENSE__
15
* JasPer License Version 2.0
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
21
* All rights reserved.
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:
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.
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
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.
66
* __END_OF_JASPER_LICENSE__
72
* $Id: jp2_enc.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
75
/******************************************************************************\
77
\******************************************************************************/
80
#include "jasper/jas_malloc.h"
81
#include "jasper/jas_image.h"
82
#include "jasper/jas_stream.h"
83
#include "jasper/jas_cm.h"
84
#include "jasper/jas_icc.h"
87
static uint_fast32_t jp2_gettypeasoc(int colorspace, int ctype);
88
static int clrspctojp2(jas_clrspc_t clrspc);
90
/******************************************************************************\
92
\******************************************************************************/
94
int jp2_write_header(jas_image_t *image, jas_stream_t *out)
99
jas_stream_t *tmpstream;
103
uint_fast16_t cmptno;
105
jp2_cdefchan_t *cdefchanent;
108
uint_fast32_t typeasoc;
109
jas_iccprof_t *iccprof;
110
jas_stream_t *iccstream;
120
sgnd = jas_image_cmptsgnd(image, 0);
121
prec = jas_image_cmptprec(image, 0);
122
for (i = 1; i < jas_image_numcmpts(image); ++i) {
123
if (jas_image_cmptsgnd(image, i) != sgnd ||
124
jas_image_cmptprec(image, i) != prec) {
130
/* Output the signature box. */
132
if (!(box = jp2_box_create(JP2_BOX_JP))) {
135
box->data.jp.magic = JP2_JP_MAGIC;
136
if (jp2_box_put(box, out)) {
139
jp2_box_destroy(box);
142
/* Output the file type box. */
144
if (!(box = jp2_box_create(JP2_BOX_FTYP))) {
147
ftyp = &box->data.ftyp;
148
ftyp->majver = JP2_FTYP_MAJVER;
149
ftyp->minver = JP2_FTYP_MINVER;
150
ftyp->numcompatcodes = 1;
151
ftyp->compatcodes[0] = JP2_FTYP_COMPATCODE;
152
if (jp2_box_put(box, out)) {
155
jp2_box_destroy(box);
159
* Generate the data portion of the JP2 header box.
160
* We cannot simply output the header for this box
161
* since we do not yet know the correct value for the length
165
if (!(tmpstream = jas_stream_memopen(0, 0))) {
169
/* Generate image header box. */
171
if (!(box = jp2_box_create(JP2_BOX_IHDR))) {
174
ihdr = &box->data.ihdr;
175
ihdr->width = jas_image_width(image);
176
ihdr->height = jas_image_height(image);
177
ihdr->numcmpts = jas_image_numcmpts(image);
178
ihdr->bpc = allcmptssame ? JP2_SPTOBPC(jas_image_cmptsgnd(image, 0),
179
jas_image_cmptprec(image, 0)) : JP2_IHDR_BPCNULL;
180
ihdr->comptype = JP2_IHDR_COMPTYPE;
183
if (jp2_box_put(box, tmpstream)) {
186
jp2_box_destroy(box);
189
/* Generate bits per component box. */
192
if (!(box = jp2_box_create(JP2_BOX_BPCC))) {
195
bpcc = &box->data.bpcc;
196
bpcc->numcmpts = jas_image_numcmpts(image);
197
if (!(bpcc->bpcs = jas_alloc2(bpcc->numcmpts,
198
sizeof(uint_fast8_t)))) {
201
for (cmptno = 0; cmptno < bpcc->numcmpts; ++cmptno) {
202
bpcc->bpcs[cmptno] = JP2_SPTOBPC(jas_image_cmptsgnd(image,
203
cmptno), jas_image_cmptprec(image, cmptno));
205
if (jp2_box_put(box, tmpstream)) {
208
jp2_box_destroy(box);
212
/* Generate color specification box. */
214
if (!(box = jp2_box_create(JP2_BOX_COLR))) {
217
colr = &box->data.colr;
218
switch (jas_image_clrspc(image)) {
219
case JAS_CLRSPC_SRGB:
220
case JAS_CLRSPC_SYCBCR:
221
case JAS_CLRSPC_SGRAY:
222
colr->method = JP2_COLR_ENUM;
223
colr->csid = clrspctojp2(jas_image_clrspc(image));
224
colr->pri = JP2_COLR_PRI;
228
colr->method = JP2_COLR_ICC;
229
colr->pri = JP2_COLR_PRI;
231
iccprof = jas_iccprof_createfromcmprof(jas_image_cmprof(image));
233
iccstream = jas_stream_memopen(0, 0);
235
if (jas_iccprof_save(iccprof, iccstream))
237
if ((pos = jas_stream_tell(iccstream)) < 0)
240
colr->iccp = jas_malloc(pos);
242
jas_stream_rewind(iccstream);
243
if (jas_stream_read(iccstream, colr->iccp, colr->iccplen) != colr->iccplen)
245
jas_stream_close(iccstream);
246
jas_iccprof_destroy(iccprof);
249
if (jp2_box_put(box, tmpstream)) {
252
jp2_box_destroy(box);
256
switch (jas_clrspc_fam(jas_image_clrspc(image))) {
257
case JAS_CLRSPC_FAM_RGB:
258
if (jas_image_cmpttype(image, 0) ==
259
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R) &&
260
jas_image_cmpttype(image, 1) ==
261
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G) &&
262
jas_image_cmpttype(image, 2) ==
263
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B))
266
case JAS_CLRSPC_FAM_YCBCR:
267
if (jas_image_cmpttype(image, 0) ==
268
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_Y) &&
269
jas_image_cmpttype(image, 1) ==
270
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_CB) &&
271
jas_image_cmpttype(image, 2) ==
272
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_YCBCR_CR))
275
case JAS_CLRSPC_FAM_GRAY:
276
if (jas_image_cmpttype(image, 0) ==
277
JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y))
286
if (!(box = jp2_box_create(JP2_BOX_CDEF))) {
289
cdef = &box->data.cdef;
290
cdef->numchans = jas_image_numcmpts(image);
291
cdef->ents = jas_alloc2(cdef->numchans, sizeof(jp2_cdefchan_t));
292
for (i = 0; i < jas_image_numcmpts(image); ++i) {
293
cdefchanent = &cdef->ents[i];
294
cdefchanent->channo = i;
295
typeasoc = jp2_gettypeasoc(jas_image_clrspc(image), jas_image_cmpttype(image, i));
296
cdefchanent->type = typeasoc >> 16;
297
cdefchanent->assoc = typeasoc & 0x7fff;
299
if (jp2_box_put(box, tmpstream)) {
302
jp2_box_destroy(box);
306
/* Determine the total length of the JP2 header box. */
308
len = jas_stream_tell(tmpstream);
309
jas_stream_rewind(tmpstream);
312
* Output the JP2 header box and all of the boxes which it contains.
315
if (!(box = jp2_box_create(JP2_BOX_JP2H))) {
318
box->len = len + JP2_BOX_HDRLEN(false);
319
if (jp2_box_put(box, out)) {
322
jp2_box_destroy(box);
325
if (jas_stream_copy(out, tmpstream, len)) {
329
jas_stream_close(tmpstream);
338
jp2_box_destroy(box);
341
jas_stream_close(tmpstream);
346
int jp2_write_codestream(jas_image_t *image, jas_stream_t *out, char *optstr)
350
uint_fast32_t overhead;
353
* Output the contiguous code stream box.
356
if (!(box = jp2_box_create(JP2_BOX_JP2C))) {
360
if (jp2_box_put(box, out)) {
363
jp2_box_destroy(box);
366
/* Output the JPEG-2000 code stream. */
368
overhead = jas_stream_getrwcount(out);
369
sprintf(buf, "%s\n_jp2overhead=%lu\n", (optstr ? optstr : ""),
370
(unsigned long) overhead);
372
if (jpc_encode(image, out, buf)) {
382
jp2_box_destroy(box);
387
int jp2_encode(jas_image_t *image, jas_stream_t *out, char *optstr)
389
if (jp2_write_header(image, out) < 0)
391
if (jp2_write_codestream(image, out, optstr) < 0)
397
int jp2_encode_uuid(jas_image_t *image, jas_stream_t *out,
398
char *optstr, jp2_box_t *uuid)
400
if (jp2_write_header(image, out) < 0)
403
if (jp2_box_put(uuid, out))
406
if (jp2_write_codestream(image, out, optstr) < 0)
412
static uint_fast32_t jp2_gettypeasoc(int colorspace, int ctype)
417
if (ctype & JAS_IMAGE_CT_OPACITY) {
418
type = JP2_CDEF_TYPE_OPACITY;
419
asoc = JP2_CDEF_ASOC_ALL;
423
type = JP2_CDEF_TYPE_UNSPEC;
424
asoc = JP2_CDEF_ASOC_NONE;
425
switch (jas_clrspc_fam(colorspace)) {
426
case JAS_CLRSPC_FAM_RGB:
427
switch (JAS_IMAGE_CT_COLOR(ctype)) {
428
case JAS_IMAGE_CT_RGB_R:
429
type = JP2_CDEF_TYPE_COLOR;
430
asoc = JP2_CDEF_RGB_R;
432
case JAS_IMAGE_CT_RGB_G:
433
type = JP2_CDEF_TYPE_COLOR;
434
asoc = JP2_CDEF_RGB_G;
436
case JAS_IMAGE_CT_RGB_B:
437
type = JP2_CDEF_TYPE_COLOR;
438
asoc = JP2_CDEF_RGB_B;
442
case JAS_CLRSPC_FAM_YCBCR:
443
switch (JAS_IMAGE_CT_COLOR(ctype)) {
444
case JAS_IMAGE_CT_YCBCR_Y:
445
type = JP2_CDEF_TYPE_COLOR;
446
asoc = JP2_CDEF_YCBCR_Y;
448
case JAS_IMAGE_CT_YCBCR_CB:
449
type = JP2_CDEF_TYPE_COLOR;
450
asoc = JP2_CDEF_YCBCR_CB;
452
case JAS_IMAGE_CT_YCBCR_CR:
453
type = JP2_CDEF_TYPE_COLOR;
454
asoc = JP2_CDEF_YCBCR_CR;
458
case JAS_CLRSPC_FAM_GRAY:
459
type = JP2_CDEF_TYPE_COLOR;
460
asoc = JP2_CDEF_GRAY_Y;
465
return (type << 16) | asoc;
468
static int clrspctojp2(jas_clrspc_t clrspc)
471
case JAS_CLRSPC_SRGB:
472
return JP2_COLR_SRGB;
473
case JAS_CLRSPC_SYCBCR:
474
return JP2_COLR_SYCC;
475
case JAS_CLRSPC_SGRAY:
476
return JP2_COLR_SGRAY;