aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/openjpeg-libsl/libopenjpeg/jp2.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/openjpeg-libsl/libopenjpeg/jp2.c')
-rw-r--r--libraries/openjpeg-libsl/libopenjpeg/jp2.c700
1 files changed, 700 insertions, 0 deletions
diff --git a/libraries/openjpeg-libsl/libopenjpeg/jp2.c b/libraries/openjpeg-libsl/libopenjpeg/jp2.c
new file mode 100644
index 0000000..d102349
--- /dev/null
+++ b/libraries/openjpeg-libsl/libopenjpeg/jp2.c
@@ -0,0 +1,700 @@
1/*
2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "opj_includes.h"
33
34/** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
35/*@{*/
36
37/** @name Local static functions */
38/*@{*/
39
40/**
41Read box headers
42@param cinfo Codec context info
43@param cio Input stream
44@param box
45@return Returns true if successful, returns false otherwise
46*/
47static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
48/*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
49/**
50Read the IHDR box - Image Header box
51@param jp2 JP2 handle
52@param cio Input buffer stream
53@return Returns true if successful, returns false otherwise
54*/
55static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
56static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
57static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
58static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
59static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
60static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio);
61/**
62Write the FTYP box - File type box
63@param jp2 JP2 handle
64@param cio Output buffer stream
65*/
66static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
67/**
68Read the FTYP box - File type box
69@param jp2 JP2 handle
70@param cio Input buffer stream
71@return Returns true if successful, returns false otherwise
72*/
73static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
74static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index);
75static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
76static void jp2_write_jp(opj_cio_t *cio);
77/**
78Read the JP box - JPEG 2000 signature
79@param jp2 JP2 handle
80@param cio Input buffer stream
81@return Returns true if successful, returns false otherwise
82*/
83static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
84/**
85Decode the structure of a JP2 file
86@param jp2 JP2 handle
87@param cio Input buffer stream
88@return Returns true if successful, returns false otherwise
89*/
90static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio);
91
92/*@}*/
93
94/*@}*/
95
96/* ----------------------------------------------------------------------- */
97
98static bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
99 box->init_pos = cio_tell(cio);
100 box->length = cio_read(cio, 4);
101 box->type = cio_read(cio, 4);
102 if (box->length == 1) {
103 if (cio_read(cio, 4) != 0) {
104 opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
105 return false;
106 }
107 box->length = cio_read(cio, 4);
108 if (box->length == 0)
109 box->length = cio_numbytesleft(cio) + 12;
110 }
111 else if (box->length == 0) {
112 box->length = cio_numbytesleft(cio) + 8;
113 }
114
115 return true;
116}
117
118#if 0
119static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
120 unsigned int i;
121 opj_jp2_box_t box;
122
123 box.init_pos = cio_tell(cio);
124 cio_skip(cio, 4);
125 cio_write(cio, JP2_URL, 4); /* DBTL */
126 cio_write(cio, 0, 1); /* VERS */
127 cio_write(cio, 0, 3); /* FLAG */
128
129 if(Idx_file) {
130 for (i = 0; i < strlen(Idx_file); i++) {
131 cio_write(cio, Idx_file[i], 1);
132 }
133 }
134
135 box.length = cio_tell(cio) - box.init_pos;
136 cio_seek(cio, box.init_pos);
137 cio_write(cio, box.length, 4); /* L */
138 cio_seek(cio, box.init_pos + box.length);
139}
140#endif
141
142static bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
143 opj_jp2_box_t box;
144
145 opj_common_ptr cinfo = jp2->cinfo;
146
147 jp2_read_boxhdr(cinfo, cio, &box);
148 if (JP2_IHDR != box.type) {
149 opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
150 return false;
151 }
152
153 jp2->h = cio_read(cio, 4); /* HEIGHT */
154 jp2->w = cio_read(cio, 4); /* WIDTH */
155 jp2->numcomps = cio_read(cio, 2); /* NC */
156 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
157
158 jp2->bpc = cio_read(cio, 1); /* BPC */
159
160 jp2->C = cio_read(cio, 1); /* C */
161 jp2->UnkC = cio_read(cio, 1); /* UnkC */
162 jp2->IPR = cio_read(cio, 1); /* IPR */
163
164 if (cio_tell(cio) - box.init_pos != box.length) {
165 opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
166 return false;
167 }
168
169 return true;
170}
171
172static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
173 opj_jp2_box_t box;
174
175 box.init_pos = cio_tell(cio);
176 cio_skip(cio, 4);
177 cio_write(cio, JP2_IHDR, 4); /* IHDR */
178
179 cio_write(cio, jp2->h, 4); /* HEIGHT */
180 cio_write(cio, jp2->w, 4); /* WIDTH */
181 cio_write(cio, jp2->numcomps, 2); /* NC */
182
183 cio_write(cio, jp2->bpc, 1); /* BPC */
184
185 cio_write(cio, jp2->C, 1); /* C : Always 7 */
186 cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
187 cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
188
189 box.length = cio_tell(cio) - box.init_pos;
190 cio_seek(cio, box.init_pos);
191 cio_write(cio, box.length, 4); /* L */
192 cio_seek(cio, box.init_pos + box.length);
193}
194
195static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
196 unsigned int i;
197 opj_jp2_box_t box;
198
199 box.init_pos = cio_tell(cio);
200 cio_skip(cio, 4);
201 cio_write(cio, JP2_BPCC, 4); /* BPCC */
202
203 for (i = 0; i < jp2->numcomps; i++) {
204 cio_write(cio, jp2->comps[i].bpcc, 1);
205 }
206
207 box.length = cio_tell(cio) - box.init_pos;
208 cio_seek(cio, box.init_pos);
209 cio_write(cio, box.length, 4); /* L */
210 cio_seek(cio, box.init_pos + box.length);
211}
212
213
214static bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
215 unsigned int i;
216 opj_jp2_box_t box;
217
218 opj_common_ptr cinfo = jp2->cinfo;
219
220 jp2_read_boxhdr(cinfo, cio, &box);
221 if (JP2_BPCC != box.type) {
222 opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
223 return false;
224 }
225
226 for (i = 0; i < jp2->numcomps; i++) {
227 jp2->comps[i].bpcc = cio_read(cio, 1);
228 }
229
230 if (cio_tell(cio) - box.init_pos != box.length) {
231 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
232 return false;
233 }
234
235 return true;
236}
237
238static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
239 opj_jp2_box_t box;
240
241 box.init_pos = cio_tell(cio);
242 cio_skip(cio, 4);
243 cio_write(cio, JP2_COLR, 4); /* COLR */
244
245 cio_write(cio, jp2->meth, 1); /* METH */
246 cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
247 cio_write(cio, jp2->approx, 1); /* APPROX */
248
249 if (jp2->meth == 1) {
250 cio_write(cio, jp2->enumcs, 4); /* EnumCS */
251 } else {
252 cio_write(cio, 0, 1); /* PROFILE (??) */
253 }
254
255 box.length = cio_tell(cio) - box.init_pos;
256 cio_seek(cio, box.init_pos);
257 cio_write(cio, box.length, 4); /* L */
258 cio_seek(cio, box.init_pos + box.length);
259}
260
261static bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
262 opj_jp2_box_t box;
263 int skip_len;
264
265 opj_common_ptr cinfo = jp2->cinfo;
266
267 jp2_read_boxhdr(cinfo, cio, &box);
268 do {
269 if (JP2_COLR != box.type) {
270 cio_skip(cio, box.length - 8);
271 jp2_read_boxhdr(cinfo, cio, &box);
272 }
273 } while(JP2_COLR != box.type);
274
275 jp2->meth = cio_read(cio, 1); /* METH */
276 jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
277 jp2->approx = cio_read(cio, 1); /* APPROX */
278
279 if (jp2->meth == 1) {
280 jp2->enumcs = cio_read(cio, 4); /* EnumCS */
281 } else {
282 /* skip PROFILE */
283 skip_len = box.init_pos + box.length - cio_tell(cio);
284 if (skip_len < 0) {
285 opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H box size\n");
286 return false;
287 }
288 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
289 }
290
291 if (cio_tell(cio) - box.init_pos != box.length) {
292 opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
293 return false;
294 }
295 return true;
296}
297
298void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
299 opj_jp2_box_t box;
300
301 box.init_pos = cio_tell(cio);
302 cio_skip(cio, 4);
303 cio_write(cio, JP2_JP2H, 4); /* JP2H */
304
305 jp2_write_ihdr(jp2, cio);
306
307 if (jp2->bpc == 255) {
308 jp2_write_bpcc(jp2, cio);
309 }
310 jp2_write_colr(jp2, cio);
311
312 box.length = cio_tell(cio) - box.init_pos;
313 cio_seek(cio, box.init_pos);
314 cio_write(cio, box.length, 4); /* L */
315 cio_seek(cio, box.init_pos + box.length);
316}
317
318bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
319 opj_jp2_box_t box;
320 int skip_len;
321
322 opj_common_ptr cinfo = jp2->cinfo;
323
324 jp2_read_boxhdr(cinfo, cio, &box);
325 do {
326 if (JP2_JP2H != box.type) {
327 if (box.type == JP2_JP2C) {
328 opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
329 return false;
330 }
331 cio_skip(cio, box.length - 8);
332 jp2_read_boxhdr(cinfo, cio, &box);
333 }
334 } while(JP2_JP2H != box.type);
335
336 if (!jp2_read_ihdr(jp2, cio))
337 return false;
338
339 if (jp2->bpc == 255) {
340 if (!jp2_read_bpcc(jp2, cio))
341 return false;
342 }
343 if (!jp2_read_colr(jp2, cio))
344 return false;
345
346 skip_len = box.init_pos + box.length - cio_tell(cio);
347 if (skip_len < 0) {
348 opj_event_msg(cinfo, EVT_ERROR, "Error with JP2H Box\n");
349 return false;
350 }
351 cio_skip(cio, box.init_pos + box.length - cio_tell(cio));
352
353 return true;
354}
355
356static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
357 unsigned int i;
358 opj_jp2_box_t box;
359
360 box.init_pos = cio_tell(cio);
361 cio_skip(cio, 4);
362 cio_write(cio, JP2_FTYP, 4); /* FTYP */
363
364 cio_write(cio, jp2->brand, 4); /* BR */
365 cio_write(cio, jp2->minversion, 4); /* MinV */
366
367 for (i = 0; i < jp2->numcl; i++) {
368 cio_write(cio, jp2->cl[i], 4); /* CL */
369 }
370
371 box.length = cio_tell(cio) - box.init_pos;
372 cio_seek(cio, box.init_pos);
373 cio_write(cio, box.length, 4); /* L */
374 cio_seek(cio, box.init_pos + box.length);
375}
376
377static bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
378 int i;
379 opj_jp2_box_t box;
380
381 opj_common_ptr cinfo = jp2->cinfo;
382
383 jp2_read_boxhdr(cinfo, cio, &box);
384
385 if (JP2_FTYP != box.type) {
386 opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
387 return false;
388 }
389
390 jp2->brand = cio_read(cio, 4); /* BR */
391 jp2->minversion = cio_read(cio, 4); /* MinV */
392 jp2->numcl = (box.length - 16) / 4;
393 jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
394
395 for (i = 0; i < (int)jp2->numcl; i++) {
396 jp2->cl[i] = cio_read(cio, 4); /* CLi */
397 }
398
399 if (cio_tell(cio) - box.init_pos != box.length) {
400 opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
401 return false;
402 }
403
404 return true;
405}
406
407static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
408 unsigned int j2k_codestream_offset, j2k_codestream_length;
409 opj_jp2_box_t box;
410
411 opj_j2k_t *j2k = jp2->j2k;
412
413 box.init_pos = cio_tell(cio);
414 cio_skip(cio, 4);
415 cio_write(cio, JP2_JP2C, 4); /* JP2C */
416
417 /* J2K encoding */
418 j2k_codestream_offset = cio_tell(cio);
419 if(!j2k_encode(j2k, cio, image, index)) {
420 opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
421 return 0;
422 }
423 j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
424
425 jp2->j2k_codestream_offset = j2k_codestream_offset;
426 jp2->j2k_codestream_length = j2k_codestream_length;
427
428 box.length = 8 + jp2->j2k_codestream_length;
429 cio_seek(cio, box.init_pos);
430 cio_write(cio, box.length, 4); /* L */
431 cio_seek(cio, box.init_pos + box.length);
432
433 return box.length;
434}
435
436static bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
437 opj_jp2_box_t box;
438
439 opj_common_ptr cinfo = jp2->cinfo;
440
441 jp2_read_boxhdr(cinfo, cio, &box);
442 do {
443 if(JP2_JP2C != box.type) {
444 cio_skip(cio, box.length - 8);
445 jp2_read_boxhdr(cinfo, cio, &box);
446 }
447 } while(JP2_JP2C != box.type);
448
449 *j2k_codestream_offset = cio_tell(cio);
450 *j2k_codestream_length = box.length - 8;
451
452 return true;
453}
454
455static void jp2_write_jp(opj_cio_t *cio) {
456 opj_jp2_box_t box;
457
458 box.init_pos = cio_tell(cio);
459 cio_skip(cio, 4);
460 cio_write(cio, JP2_JP, 4); /* JP2 signature */
461 cio_write(cio, 0x0d0a870a, 4);
462
463 box.length = cio_tell(cio) - box.init_pos;
464 cio_seek(cio, box.init_pos);
465 cio_write(cio, box.length, 4); /* L */
466 cio_seek(cio, box.init_pos + box.length);
467}
468
469static bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
470 opj_jp2_box_t box;
471
472 opj_common_ptr cinfo = jp2->cinfo;
473
474 jp2_read_boxhdr(cinfo, cio, &box);
475 if (JP2_JP != box.type) {
476 opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
477 return false;
478 }
479 if (0x0d0a870a != cio_read(cio, 4)) {
480 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
481 return false;
482 }
483 if (cio_tell(cio) - box.init_pos != box.length) {
484 opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
485 return false;
486 }
487
488 return true;
489}
490
491
492static bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio) {
493 if (!jp2_read_jp(jp2, cio))
494 return false;
495 if (!jp2_read_ftyp(jp2, cio))
496 return false;
497 if (!jp2_read_jp2h(jp2, cio))
498 return false;
499 if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
500 return false;
501
502 return true;
503}
504
505/* ----------------------------------------------------------------------- */
506/* JP2 decoder interface */
507/* ----------------------------------------------------------------------- */
508
509opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
510 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
511 if(jp2) {
512 jp2->cinfo = cinfo;
513 /* create the J2K codec */
514 jp2->j2k = j2k_create_decompress(cinfo);
515 if(jp2->j2k == NULL) {
516 jp2_destroy_decompress(jp2);
517 return NULL;
518 }
519 }
520 return jp2;
521}
522
523void jp2_destroy_decompress(opj_jp2_t *jp2) {
524 if(jp2) {
525 /* destroy the J2K codec */
526 j2k_destroy_decompress(jp2->j2k);
527
528 if(jp2->comps) {
529 opj_free(jp2->comps);
530 }
531 if(jp2->cl) {
532 opj_free(jp2->cl);
533 }
534 opj_free(jp2);
535 }
536}
537
538void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
539 /* setup the J2K codec */
540 j2k_setup_decoder(jp2->j2k, parameters);
541 /* further JP2 initializations go here */
542}
543
544opj_image_t* jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio) {
545 opj_common_ptr cinfo;
546 opj_image_t *image = NULL;
547
548 if(!jp2 || !cio) {
549 return NULL;
550 }
551
552 cinfo = jp2->cinfo;
553
554 /* JP2 decoding */
555 if(!jp2_read_struct(jp2, cio)) {
556 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
557 return NULL;
558 }
559
560 /* J2K decoding */
561 image = j2k_decode(jp2->j2k, cio);
562 if(!image) {
563 opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
564 }
565
566 return image;
567}
568
569/* ----------------------------------------------------------------------- */
570/* JP2 encoder interface */
571/* ----------------------------------------------------------------------- */
572
573opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
574 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
575 if(jp2) {
576 jp2->cinfo = cinfo;
577 /* create the J2K codec */
578 jp2->j2k = j2k_create_compress(cinfo);
579 if(jp2->j2k == NULL) {
580 jp2_destroy_compress(jp2);
581 return NULL;
582 }
583 }
584 return jp2;
585}
586
587void jp2_destroy_compress(opj_jp2_t *jp2) {
588 if(jp2) {
589 /* destroy the J2K codec */
590 j2k_destroy_compress(jp2->j2k);
591
592 if(jp2->comps) {
593 opj_free(jp2->comps);
594 }
595 if(jp2->cl) {
596 opj_free(jp2->cl);
597 }
598 opj_free(jp2);
599 }
600}
601
602void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
603 int i;
604 int depth_0, sign;
605
606 if(!jp2 || !parameters || !image)
607 return;
608
609 /* setup the J2K codec */
610 /* ------------------- */
611
612 /* Check if number of components respects standard */
613 if (image->numcomps < 1 || image->numcomps > 16384) {
614 opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
615 return;
616 }
617
618 j2k_setup_encoder(jp2->j2k, parameters, image);
619
620 /* setup the JP2 codec */
621 /* ------------------- */
622
623 /* Profile box */
624
625 jp2->brand = JP2_JP2; /* BR */
626 jp2->minversion = 0; /* MinV */
627 jp2->numcl = 1;
628 jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
629 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
630
631 /* Image Header box */
632
633 jp2->numcomps = image->numcomps; /* NC */
634 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
635 jp2->h = image->y1 - image->y0; /* HEIGHT */
636 jp2->w = image->x1 - image->x0; /* WIDTH */
637 /* BPC */
638 depth_0 = image->comps[0].prec - 1;
639 sign = image->comps[0].sgnd;
640 jp2->bpc = depth_0 + (sign << 7);
641 for (i = 1; i < image->numcomps; i++) {
642 int depth = image->comps[i].prec - 1;
643 sign = image->comps[i].sgnd;
644 if (depth_0 != depth)
645 jp2->bpc = 255;
646 }
647 jp2->C = 7; /* C : Always 7 */
648 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
649 jp2->IPR = 0; /* IPR, no intellectual property */
650
651 /* BitsPerComponent box */
652
653 for (i = 0; i < image->numcomps; i++) {
654 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
655 }
656
657 /* Colour Specification box */
658
659 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
660 jp2->meth = 1; /* METH: Enumerated colourspace */
661 } else {
662 jp2->meth = 2; /* METH: Restricted ICC profile */
663 }
664 if (jp2->meth == 1) {
665 if (image->color_space == 1)
666 jp2->enumcs = 16; /* sRGB as defined by IEC 61966𣇻 */
667 else if (image->color_space == 2)
668 jp2->enumcs = 17; /* greyscale */
669 else if (image->color_space == 3)
670 jp2->enumcs = 18; /* YUV */
671 } else {
672 jp2->enumcs = 0; /* PROFILE (??) */
673 }
674 jp2->precedence = 0; /* PRECEDENCE */
675 jp2->approx = 0; /* APPROX */
676
677}
678
679bool jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, char *index) {
680
681 /* JP2 encoding */
682
683 /* JPEG 2000 Signature box */
684 jp2_write_jp(cio);
685 /* File Type box */
686 jp2_write_ftyp(jp2, cio);
687 /* JP2 Header box */
688 jp2_write_jp2h(jp2, cio);
689
690 /* J2K encoding */
691
692 if(!jp2_write_jp2c(jp2, cio, image, index)) {
693 opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
694 return false;
695 }
696
697 return true;
698}
699
700