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 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_includes.h"
34 /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
39 /** @name Local static functions */
42 /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
45 * Reads a IHDR box - Image Header box
47 * @param p_image_header_data pointer to actual data (already read from file)
48 * @param jp2 the jpeg2000 file codec.
49 * @param p_image_header_size the size of the image header
50 * @param p_manager the user event manager.
52 * @return true if the image header is valid, false else.
54 static opj_bool opj_jp2_read_ihdr( opj_jp2_t *jp2,
55 OPJ_BYTE *p_image_header_data,
56 OPJ_UINT32 p_image_header_size,
57 opj_event_mgr_t * p_manager );
60 * Writes the Image Header box - Image Header box.
62 * @param jp2 jpeg2000 file codec.
63 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
65 * @return the data being copied.
67 static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
68 OPJ_UINT32 * p_nb_bytes_written );
71 * Writes the Bit per Component box.
73 * @param jp2 jpeg2000 file codec.
74 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
76 * @return the data being copied.
78 static OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
79 OPJ_UINT32 * p_nb_bytes_written );
82 * Reads a Bit per Component box.
84 * @param p_bpc_header_data pointer to actual data (already read from file)
85 * @param jp2 the jpeg2000 file codec.
86 * @param p_bpc_header_size the size of the bpc header
87 * @param p_manager the user event manager.
89 * @return true if the bpc header is valid, fale else.
91 static opj_bool opj_jp2_read_bpcc( opj_jp2_t *jp2,
92 OPJ_BYTE * p_bpc_header_data,
93 OPJ_UINT32 p_bpc_header_size,
94 opj_event_mgr_t * p_manager );
96 static opj_bool opj_jp2_read_cdef( opj_jp2_t * jp2,
97 OPJ_BYTE * p_cdef_header_data,
98 OPJ_UINT32 p_cdef_header_size,
99 opj_event_mgr_t * p_manager );
101 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color);
104 * Writes the Colour Specification box.
106 * @param jp2 jpeg2000 file codec.
107 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
109 * @return the data being copied.
111 static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
112 OPJ_UINT32 * p_nb_bytes_written );
115 * Writes a FTYP box - File type box
117 * @param cio the stream to write data to.
118 * @param jp2 the jpeg2000 file codec.
119 * @param p_manager the user event manager.
121 * @return true if writing was successful.
123 static opj_bool opj_jp2_write_ftyp( opj_jp2_t *jp2,
124 opj_stream_private_t *cio,
125 opj_event_mgr_t * p_manager );
128 * Reads a a FTYP box - File type box
130 * @param p_header_data the data contained in the FTYP box.
131 * @param jp2 the jpeg2000 file codec.
132 * @param p_header_size the size of the data contained in the FTYP box.
133 * @param p_manager the user event manager.
135 * @return true if the FTYP box is valid.
137 static opj_bool opj_jp2_read_ftyp( opj_jp2_t *jp2,
138 OPJ_BYTE * p_header_data,
139 OPJ_UINT32 p_header_size,
140 opj_event_mgr_t * p_manager );
142 opj_bool opj_jp2_skip_jp2c( opj_jp2_t *jp2,
143 opj_stream_private_t *cio,
144 opj_event_mgr_t * p_manager );
147 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
149 * @param p_header_data the data contained in the file header box.
150 * @param jp2 the jpeg2000 file codec.
151 * @param p_header_size the size of the data contained in the file header box.
152 * @param p_manager the user event manager.
154 * @return true if the JP2 Header box was successfully reconized.
156 static opj_bool opj_jp2_read_jp2h( opj_jp2_t *jp2,
157 OPJ_BYTE *p_header_data,
158 OPJ_UINT32 p_header_size,
159 opj_event_mgr_t * p_manager );
162 * Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
164 * @param cio the stream to write data to.
165 * @param jp2 the jpeg2000 file codec.
166 * @param p_manager user event manager.
168 * @return true if writing was successful.
170 static opj_bool opj_jp2_write_jp2c( opj_jp2_t *jp2,
171 opj_stream_private_t *cio,
172 opj_event_mgr_t * p_manager );
175 * Write index Finder box
176 * @param cio the stream to write to.
177 * @param jp2 the jpeg2000 file codec.
178 * @param p_manager user event manager.
180 static opj_bool opj_jpip_write_iptr( opj_jp2_t *jp2,
181 opj_stream_private_t *cio,
182 opj_event_mgr_t * p_manager );
185 * Write index Finder box
186 * @param cio the stream to write to.
187 * @param jp2 the jpeg2000 file codec.
188 * @param p_manager user event manager.
190 static opj_bool opj_jpip_write_cidx(opj_jp2_t *jp2,
191 opj_stream_private_t *cio,
192 opj_event_mgr_t * p_manager );
195 * Write file Index (superbox)
196 * @param cio the stream to write to.
197 * @param jp2 the jpeg2000 file codec.
198 * @param p_manager user event manager.
200 static opj_bool opj_jpip_write_fidx(opj_jp2_t *jp2,
201 opj_stream_private_t *cio,
202 opj_event_mgr_t * p_manager );
205 * Reads a jpeg2000 file signature box.
207 * @param p_header_data the data contained in the signature box.
208 * @param jp2 the jpeg2000 file codec.
209 * @param p_header_size the size of the data contained in the signature box.
210 * @param p_manager the user event manager.
212 * @return true if the file signature box is valid.
214 static opj_bool opj_jp2_read_jp(opj_jp2_t *jp2,
215 OPJ_BYTE * p_header_data,
216 OPJ_UINT32 p_header_size,
217 opj_event_mgr_t * p_manager);
220 * Writes a jpeg2000 file signature box.
222 * @param cio the stream to write data to.
223 * @param jp2 the jpeg2000 file codec.
224 * @param p_manager the user event manager.
226 * @return true if writing was successful.
228 static opj_bool opj_jp2_write_jp( opj_jp2_t *jp2,
229 opj_stream_private_t *cio,
230 opj_event_mgr_t * p_manager );
233 Apply collected palette data
234 @param color Collector for profile, cdef and pclr data
237 static void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color);
239 static void opj_jp2_free_pclr(opj_jp2_color_t *color);
242 * Collect palette data
244 * @param jp2 JP2 handle
245 * @param p_pclr_header_data FIXME DOC
246 * @param p_pclr_header_size FIXME DOC
249 * @return Returns true if successful, returns false otherwise
251 static opj_bool opj_jp2_read_pclr( opj_jp2_t *jp2,
252 OPJ_BYTE * p_pclr_header_data,
253 OPJ_UINT32 p_pclr_header_size,
254 opj_event_mgr_t * p_manager );
257 * Collect component mapping data
259 * @param jp2 JP2 handle
260 * @param p_cmap_header_data FIXME DOC
261 * @param p_cmap_header_size FIXME DOC
262 * @param p_manager FIXME DOC
264 * @return Returns true if successful, returns false otherwise
267 static opj_bool opj_jp2_read_cmap( opj_jp2_t * jp2,
268 OPJ_BYTE * p_cmap_header_data,
269 OPJ_UINT32 p_cmap_header_size,
270 opj_event_mgr_t * p_manager );
273 * Reads the Color Specification box.
275 * @param p_colr_header_data pointer to actual data (already read from file)
276 * @param jp2 the jpeg2000 file codec.
277 * @param p_colr_header_size the size of the color header
278 * @param p_manager the user event manager.
280 * @return true if the bpc header is valid, fale else.
282 static opj_bool opj_jp2_read_colr( opj_jp2_t *jp2,
283 OPJ_BYTE * p_colr_header_data,
284 OPJ_UINT32 p_colr_header_size,
285 opj_event_mgr_t * p_manager );
292 * Sets up the procedures to do on writing header after the codestream.
293 * Developpers wanting to extend the library can add their own writing procedures.
295 static void opj_jp2_setup_end_header_writing (opj_jp2_t *jp2);
298 * Sets up the procedures to do on reading header after the codestream.
299 * Developpers wanting to extend the library can add their own writing procedures.
301 static void opj_jp2_setup_end_header_reading (opj_jp2_t *jp2);
304 * Reads a jpeg2000 file header structure.
306 * @param jp2 the jpeg2000 file header structure.
307 * @param stream the stream to read data from.
308 * @param p_manager the user event manager.
310 * @return true if the box is valid.
312 static opj_bool opj_jp2_read_header_procedure( opj_jp2_t *jp2,
313 opj_stream_private_t *stream,
314 opj_event_mgr_t * p_manager );
317 * Excutes the given procedures on the given codec.
319 * @param p_procedure_list the list of procedures to execute
320 * @param jp2 the jpeg2000 file codec to execute the procedures on.
321 * @param stream the stream to execute the procedures on.
322 * @param p_manager the user manager.
324 * @return true if all the procedures were successfully executed.
326 static opj_bool opj_jp2_exec ( opj_jp2_t * jp2,
327 opj_procedure_list_t * p_procedure_list,
328 opj_stream_private_t *stream,
329 opj_event_mgr_t * p_manager );
332 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
334 * @param cio the input stream to read data from.
335 * @param box the box structure to fill.
336 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
337 * @param p_manager user event manager.
339 * @return true if the box is reconized, false otherwise
341 static opj_bool opj_jp2_read_boxhdr(opj_jp2_box_t *box,
342 OPJ_UINT32 * p_number_bytes_read,
343 opj_stream_private_t *cio,
344 opj_event_mgr_t * p_manager);
347 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
348 * are valid. Developpers wanting to extend the library can add their own validation procedures.
350 static void opj_jp2_setup_encoding_validation (opj_jp2_t *jp2);
353 * Sets up the procedures to do on writing header. Developpers wanting to extend the library can add their own writing procedures.
355 static void opj_jp2_setup_header_writing (opj_jp2_t *jp2);
357 opj_bool opj_jp2_default_validation ( opj_jp2_t * jp2,
358 opj_stream_private_t *cio,
359 opj_event_mgr_t * p_manager );
362 * Finds the image execution function related to the given box id.
364 * @param p_id the id of the handler to fetch.
366 * @return the given handler or NULL if it could not be found.
368 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id);
371 * Finds the execution function related to the given box id.
373 * @param p_id the id of the handler to fetch.
375 * @return the given handler or NULL if it could not be found.
377 static const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id );
379 const opj_jp2_header_handler_t jp2_header [] =
381 {JP2_JP,opj_jp2_read_jp},
382 {JP2_FTYP,opj_jp2_read_ftyp},
383 {JP2_JP2H,opj_jp2_read_jp2h}
386 const opj_jp2_header_handler_t jp2_img_header [] =
388 {JP2_IHDR,opj_jp2_read_ihdr},
389 {JP2_COLR,opj_jp2_read_colr},
390 {JP2_BPCC,opj_jp2_read_bpcc},
391 {JP2_PCLR,opj_jp2_read_pclr},
392 {JP2_CMAP,opj_jp2_read_cmap},
393 {JP2_CDEF,opj_jp2_read_cdef}
398 * Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
400 * @param box the box structure to fill.
401 * @param p_data the character string to read data from.
402 * @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
403 * @param p_box_max_size the maximum number of bytes in the box.
404 * @param p_manager FIXME DOC
406 * @return true if the box is reconized, false otherwise
408 static opj_bool opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
410 OPJ_UINT32 * p_number_bytes_read,
411 OPJ_UINT32 p_box_max_size,
412 opj_event_mgr_t * p_manager );
415 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
416 * are valid. Developpers wanting to extend the library can add their own validation procedures.
418 static void opj_jp2_setup_decoding_validation (opj_jp2_t *jp2);
421 * Sets up the procedures to do on reading header.
422 * Developpers wanting to extend the library can add their own writing procedures.
424 static void opj_jp2_setup_header_reading (opj_jp2_t *jp2);
426 /* ----------------------------------------------------------------------- */
428 opj_bool opj_jp2_read_boxhdr(opj_jp2_box_t *box,
429 OPJ_UINT32 * p_number_bytes_read,
430 opj_stream_private_t *cio,
431 opj_event_mgr_t * p_manager
434 /* read header from file */
435 OPJ_BYTE l_data_header [8];
440 assert(p_number_bytes_read != 00);
441 assert(p_manager != 00);
443 *p_number_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
444 if (*p_number_bytes_read != 8) {
448 /* process read data */
449 opj_read_bytes(l_data_header,&(box->length), 4);
450 opj_read_bytes(l_data_header+4,&(box->type), 4);
452 /* do we have a "special very large box ?" */
453 /* read then the XLBox */
454 if (box->length == 1) {
455 OPJ_UINT32 l_xl_part_size;
457 OPJ_UINT32 l_nb_bytes_read = opj_stream_read_data(cio,l_data_header,8,p_manager);
458 if (l_nb_bytes_read != 8) {
459 if (l_nb_bytes_read > 0) {
460 *p_number_bytes_read += l_nb_bytes_read;
466 opj_read_bytes(l_data_header,&l_xl_part_size, 4);
467 if (l_xl_part_size != 0) {
468 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
471 opj_read_bytes(l_data_header,&(box->length), 4);
477 static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
481 box.init_pos = cio_tell(cio);
483 cio_write(cio, JP2_URL, 4); /* DBTL */
484 cio_write(cio, 0, 1); /* VERS */
485 cio_write(cio, 0, 3); /* FLAG */
488 for (i = 0; i < strlen(Idx_file); i++) {
489 cio_write(cio, Idx_file[i], 1);
493 box.length = cio_tell(cio) - box.init_pos;
494 cio_seek(cio, box.init_pos);
495 cio_write(cio, box.length, 4); /* L */
496 cio_seek(cio, box.init_pos + box.length);
500 opj_bool opj_jp2_read_ihdr( opj_jp2_t *jp2,
501 OPJ_BYTE *p_image_header_data,
502 OPJ_UINT32 p_image_header_size,
503 opj_event_mgr_t * p_manager )
506 assert(p_image_header_data != 00);
508 assert(p_manager != 00);
510 if (p_image_header_size != 14) {
511 opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
515 opj_read_bytes(p_image_header_data,&(jp2->h),4); /* HEIGHT */
516 p_image_header_data += 4;
517 opj_read_bytes(p_image_header_data,&(jp2->w),4); /* WIDTH */
518 p_image_header_data += 4;
519 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */
520 p_image_header_data += 2;
522 /* allocate memory for components */
523 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
524 if (jp2->comps == 0) {
525 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle image header (ihdr)\n");
528 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t));
530 opj_read_bytes(p_image_header_data,&(jp2->bpc),1); /* BPC */
531 ++ p_image_header_data;
533 opj_read_bytes(p_image_header_data,&(jp2->C),1); /* C */
534 ++ p_image_header_data;
536 /* Should be equal to 7 cf. chapter about image header box of the norm */
538 opj_event_msg(p_manager, EVT_INFO, "JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n", jp2->C);
541 opj_read_bytes(p_image_header_data,&(jp2->UnkC),1); /* UnkC */
542 ++ p_image_header_data;
543 opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */
544 ++ p_image_header_data;
549 OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
550 OPJ_UINT32 * p_nb_bytes_written
553 OPJ_BYTE * l_ihdr_data,* l_current_ihdr_ptr;
557 assert(p_nb_bytes_written != 00);
559 /* default image header is 22 bytes wide */
560 l_ihdr_data = (OPJ_BYTE *) opj_malloc(22);
561 if (l_ihdr_data == 00) {
564 memset(l_ihdr_data,0,22);
566 l_current_ihdr_ptr = l_ihdr_data;
568 opj_write_bytes(l_current_ihdr_ptr,22,4); /* write box size */
569 l_current_ihdr_ptr+=4;
571 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR */
572 l_current_ihdr_ptr+=4;
574 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */
575 l_current_ihdr_ptr+=4;
577 opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
578 l_current_ihdr_ptr+=4;
580 opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
581 l_current_ihdr_ptr+=2;
583 opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
584 ++l_current_ihdr_ptr;
586 opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
587 ++l_current_ihdr_ptr;
589 opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC, 1); /* UnkC, colorspace unknown */
590 ++l_current_ihdr_ptr;
592 opj_write_bytes(l_current_ihdr_ptr, jp2->IPR, 1); /* IPR, no intellectual property */
593 ++l_current_ihdr_ptr;
595 *p_nb_bytes_written = 22;
600 OPJ_BYTE * opj_jp2_write_bpcc( opj_jp2_t *jp2,
601 OPJ_UINT32 * p_nb_bytes_written
605 /* room for 8 bytes for box and 1 byte for each component */
606 OPJ_INT32 l_bpcc_size = 8 + jp2->numcomps;
607 OPJ_BYTE * l_bpcc_data,* l_current_bpcc_ptr;
611 assert(p_nb_bytes_written != 00);
613 l_bpcc_data = (OPJ_BYTE *) opj_malloc(l_bpcc_size);
614 if (l_bpcc_data == 00) {
617 memset(l_bpcc_data,0,l_bpcc_size);
619 l_current_bpcc_ptr = l_bpcc_data;
621 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4); /* write box size */
622 l_current_bpcc_ptr += 4;
624 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4); /* BPCC */
625 l_current_bpcc_ptr += 4;
627 for (i = 0; i < jp2->numcomps; ++i) {
628 opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc, 1); /* write each component information */
629 ++l_current_bpcc_ptr;
632 *p_nb_bytes_written = l_bpcc_size;
637 opj_bool opj_jp2_read_bpcc( opj_jp2_t *jp2,
638 OPJ_BYTE * p_bpc_header_data,
639 OPJ_UINT32 p_bpc_header_size,
640 opj_event_mgr_t * p_manager
646 assert(p_bpc_header_data != 00);
648 assert(p_manager != 00);
651 if (jp2->bpc != 255 ){
652 opj_event_msg(p_manager, EVT_WARNING, "A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",jp2->bpc);
655 /* and length is relevant */
656 if (p_bpc_header_size != jp2->numcomps) {
657 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
661 /* read info for each component */
662 for (i = 0; i < jp2->numcomps; ++i) {
663 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
670 OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
671 OPJ_UINT32 * p_nb_bytes_written
674 /* room for 8 bytes for box 3 for common data and variable upon profile*/
675 OPJ_UINT32 l_colr_size = 11;
676 OPJ_BYTE * l_colr_data,* l_current_colr_ptr;
680 assert(p_nb_bytes_written != 00);
693 l_colr_data = (OPJ_BYTE *) opj_malloc(l_colr_size);
694 if (l_colr_data == 00) {
697 memset(l_colr_data,0,l_colr_size);
699 l_current_colr_ptr = l_colr_data;
701 opj_write_bytes(l_current_colr_ptr,l_colr_size,4); /* write box size */
702 l_current_colr_ptr += 4;
704 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4); /* BPCC */
705 l_current_colr_ptr += 4;
707 opj_write_bytes(l_current_colr_ptr, jp2->meth,1); /* METH */
708 ++l_current_colr_ptr;
710 opj_write_bytes(l_current_colr_ptr, jp2->precedence,1); /* PRECEDENCE */
711 ++l_current_colr_ptr;
713 opj_write_bytes(l_current_colr_ptr, jp2->approx,1); /* APPROX */
714 ++l_current_colr_ptr;
716 if (jp2->meth == 1) {
717 opj_write_bytes(l_current_colr_ptr, jp2->enumcs,4); /* EnumCS */
720 opj_write_bytes(l_current_colr_ptr, 0, 1); /* PROFILE (??) */
723 *p_nb_bytes_written = l_colr_size;
728 void opj_jp2_free_pclr(opj_jp2_color_t *color)
730 opj_free(color->jp2_pclr->channel_sign);
731 opj_free(color->jp2_pclr->channel_size);
732 opj_free(color->jp2_pclr->entries);
734 if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
736 opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
739 void opj_jp2_apply_pclr(opj_image_t *image, opj_jp2_color_t *color)
741 opj_image_comp_t *old_comps, *new_comps;
742 OPJ_BYTE *channel_size, *channel_sign;
744 opj_jp2_cmap_comp_t *cmap;
745 OPJ_INT32 *src, *dst;
747 OPJ_UINT16 i, nr_channels, cmp, pcol;
750 channel_size = color->jp2_pclr->channel_size;
751 channel_sign = color->jp2_pclr->channel_sign;
752 entries = color->jp2_pclr->entries;
753 cmap = color->jp2_pclr->cmap;
754 nr_channels = color->jp2_pclr->nr_channels;
756 old_comps = image->comps;
757 new_comps = (opj_image_comp_t*)
758 opj_malloc(nr_channels * sizeof(opj_image_comp_t));
760 for(i = 0; i < nr_channels; ++i) {
761 pcol = cmap[i].pcol; cmp = cmap[i].cmp;
763 new_comps[pcol] = old_comps[cmp];
766 if(cmap[i].mtyp == 0){
767 old_comps[cmp].data = NULL; continue;
770 /* Palette mapping: */
771 new_comps[pcol].data = (OPJ_INT32*)
772 opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
773 new_comps[pcol].prec = channel_size[i];
774 new_comps[pcol].sgnd = channel_sign[i];
777 top_k = color->jp2_pclr->nr_entries - 1;
779 for(i = 0; i < nr_channels; ++i) {
781 if(cmap[i].mtyp == 0) continue;
783 /* Palette mapping: */
784 cmp = cmap[i].cmp; pcol = cmap[i].pcol;
785 src = old_comps[cmp].data;
786 dst = new_comps[pcol].data;
787 max = new_comps[pcol].w * new_comps[pcol].h;
789 for(j = 0; j < max; ++j)
792 if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
795 dst[j] = entries[k * nr_channels + pcol];
799 max = image->numcomps;
800 for(i = 0; i < max; ++i) {
801 if(old_comps[i].data) opj_free(old_comps[i].data);
805 image->comps = new_comps;
806 image->numcomps = nr_channels;
808 opj_jp2_free_pclr(color);
812 opj_bool opj_jp2_read_pclr( opj_jp2_t *jp2,
813 OPJ_BYTE * p_pclr_header_data,
814 OPJ_UINT32 p_pclr_header_size,
815 opj_event_mgr_t * p_manager
818 opj_jp2_pclr_t *jp2_pclr;
819 OPJ_BYTE *channel_size, *channel_sign;
821 OPJ_UINT16 nr_entries,nr_channels;
826 assert(p_pclr_header_data != 00);
828 assert(p_manager != 00);
829 (void)p_pclr_header_size;
831 if(jp2->color.jp2_pclr)
834 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
835 p_pclr_header_data += 2;
836 nr_entries = (OPJ_UINT16) l_value;
838 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
839 ++p_pclr_header_data;
840 nr_channels = (OPJ_UINT16) l_value;
842 entries = (OPJ_UINT32*) opj_malloc(nr_channels * nr_entries * sizeof(OPJ_UINT32));
843 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
844 channel_sign = (OPJ_BYTE*) opj_malloc(nr_channels);
846 jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
847 jp2_pclr->channel_sign = channel_sign;
848 jp2_pclr->channel_size = channel_size;
849 jp2_pclr->entries = entries;
850 jp2_pclr->nr_entries = nr_entries;
851 jp2_pclr->nr_channels = (OPJ_BYTE) l_value;
852 jp2_pclr->cmap = NULL;
854 jp2->color.jp2_pclr = jp2_pclr;
856 for(i = 0; i < nr_channels; ++i) {
857 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* Bi */
858 ++p_pclr_header_data;
860 channel_size[i] = (l_value & 0x7f) + 1;
861 channel_sign[i] = (l_value & 0x80)? 1 : 0;
864 for(j = 0; j < nr_entries; ++j) {
865 for(i = 0; i < nr_channels; ++i) {
866 OPJ_INT32 bytes_to_read = (channel_size[i]+7)>>3;
868 opj_read_bytes(p_pclr_header_data, &l_value , bytes_to_read); /* Cji */
869 p_pclr_header_data += bytes_to_read;
870 *entries = (OPJ_UINT32) l_value;
878 opj_bool opj_jp2_read_cmap( opj_jp2_t * jp2,
879 OPJ_BYTE * p_cmap_header_data,
880 OPJ_UINT32 p_cmap_header_size,
881 opj_event_mgr_t * p_manager
884 opj_jp2_cmap_comp_t *cmap;
885 OPJ_BYTE i, nr_channels;
890 assert(p_cmap_header_data != 00);
891 assert(p_manager != 00);
892 (void)p_cmap_header_size;
894 /* Need nr_channels: */
895 if(jp2->color.jp2_pclr == NULL) {
896 opj_event_msg(p_manager, EVT_ERROR, "Need to read a PCLR box before the CMAP box.\n");
900 /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
901 * inside a JP2 Header box' :
903 if(jp2->color.jp2_pclr->cmap) {
904 opj_event_msg(p_manager, EVT_ERROR, "Only one CMAP box is allowed.\n");
908 nr_channels = jp2->color.jp2_pclr->nr_channels;
909 cmap = (opj_jp2_cmap_comp_t*) opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
911 for(i = 0; i < nr_channels; ++i) {
912 opj_read_bytes(p_cmap_header_data, &l_value, 2); /* CMP^i */
913 p_cmap_header_data +=2;
914 cmap[i].cmp = (OPJ_UINT16) l_value;
916 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* MTYP^i */
917 ++p_cmap_header_data;
918 cmap[i].mtyp = (OPJ_BYTE) l_value;
920 opj_read_bytes(p_cmap_header_data, &l_value, 1); /* PCOL^i */
921 ++p_cmap_header_data;
922 cmap[i].pcol = (OPJ_BYTE) l_value;
925 jp2->color.jp2_pclr->cmap = cmap;
930 void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
932 opj_jp2_cdef_info_t *info;
933 OPJ_UINT16 i, n, cn, asoc, acn;
935 info = color->jp2_cdef->info;
936 n = color->jp2_cdef->n;
938 for(i = 0; i < n; ++i)
940 /* WATCH: acn = asoc - 1 ! */
941 if((asoc = info[i].asoc) == 0) continue;
948 opj_image_comp_t saved;
950 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
951 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
952 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
954 info[i].asoc = cn + 1;
955 info[acn].asoc = info[acn].cn + 1;
959 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
961 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
963 }/* jp2_apply_cdef() */
965 opj_bool opj_jp2_read_cdef( opj_jp2_t * jp2,
966 OPJ_BYTE * p_cdef_header_data,
967 OPJ_UINT32 p_cdef_header_size,
968 opj_event_mgr_t * p_manager
971 opj_jp2_cdef_info_t *cdef_info;
977 assert(p_cdef_header_data != 00);
978 assert(p_manager != 00);
979 (void)p_cdef_header_size;
981 /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
982 * inside a JP2 Header box.'*/
983 if(jp2->color.jp2_cdef) return OPJ_FALSE;
985 opj_read_bytes(p_cdef_header_data,&l_value ,2); /* N */
986 p_cdef_header_data+= 2;
988 if ( (OPJ_UINT16)l_value == 0){ /* szukw000: FIXME */
989 opj_event_msg(p_manager, EVT_ERROR, "Number of channel description is equal to zero in CDEF box.\n");
993 cdef_info = (opj_jp2_cdef_info_t*) opj_malloc(l_value * sizeof(opj_jp2_cdef_info_t));
995 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
996 jp2->color.jp2_cdef->info = cdef_info;
997 jp2->color.jp2_cdef->n = (OPJ_UINT16) l_value;
999 for(i = 0; i < jp2->color.jp2_cdef->n; ++i) {
1000 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Cn^i */
1001 p_cdef_header_data +=2;
1002 cdef_info[i].cn = (OPJ_UINT16) l_value;
1004 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Typ^i */
1005 p_cdef_header_data +=2;
1006 cdef_info[i].typ = (OPJ_UINT16) l_value;
1008 opj_read_bytes(p_cdef_header_data, &l_value, 2); /* Asoc^i */
1009 p_cdef_header_data +=2;
1010 cdef_info[i].asoc = (OPJ_UINT16) l_value;
1016 opj_bool opj_jp2_read_colr( opj_jp2_t *jp2,
1017 OPJ_BYTE * p_colr_header_data,
1018 OPJ_UINT32 p_colr_header_size,
1019 opj_event_mgr_t * p_manager
1026 assert(p_colr_header_data != 00);
1027 assert(p_manager != 00);
1029 if (p_colr_header_size < 3) {
1030 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box (bad size)\n");
1034 /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
1035 * Specification boxes after the first.'
1037 if(jp2->color.jp2_has_colr) {
1038 opj_event_msg(p_manager, EVT_INFO, "A conforming JP2 reader shall ignore all Colour Specification boxes after the first, so we ignore this one.\n");
1039 p_colr_header_data += p_colr_header_size;
1043 opj_read_bytes(p_colr_header_data,&jp2->meth ,1); /* METH */
1044 ++p_colr_header_data;
1046 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECEDENCE */
1047 ++p_colr_header_data;
1049 opj_read_bytes(p_colr_header_data,&jp2->approx ,1); /* APPROX */
1050 ++p_colr_header_data;
1052 if (jp2->meth == 1) {
1053 if (p_colr_header_size != 7) {
1054 opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
1058 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4); /* EnumCS */
1060 else if (jp2->meth == 2) {
1062 OPJ_INT32 it_icc_value = 0;
1063 OPJ_INT32 icc_len = p_colr_header_size - 3;
1065 jp2->color.icc_profile_len = icc_len;
1066 jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_malloc(icc_len);
1068 memset(jp2->color.icc_profile_buf, 0, icc_len * sizeof(OPJ_BYTE));
1070 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value)
1072 opj_read_bytes(p_colr_header_data,&l_value,1); /* icc values */
1073 ++p_colr_header_data;
1074 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_value;
1079 opj_event_msg(p_manager, EVT_INFO, "COLR BOX meth value is not a regular value (%d), so we will skip the fields following the approx field.\n", jp2->meth);
1081 jp2->color.jp2_has_colr = 1;
1086 opj_bool opj_jp2_decode(opj_jp2_t *jp2,
1087 opj_stream_private_t *p_stream,
1088 opj_image_t* p_image,
1089 opj_event_mgr_t * p_manager)
1095 if( ! opj_j2k_decode(jp2->j2k, p_stream, p_image, p_manager) ) {
1096 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
1100 if (!jp2->ignore_pclr_cmap_cdef){
1102 /* Set Image Color Space */
1103 if (jp2->enumcs == 16)
1104 p_image->color_space = CLRSPC_SRGB;
1105 else if (jp2->enumcs == 17)
1106 p_image->color_space = CLRSPC_GRAY;
1107 else if (jp2->enumcs == 18)
1108 p_image->color_space = CLRSPC_SYCC;
1110 p_image->color_space = CLRSPC_UNKNOWN;
1112 /* Apply the color space if needed */
1113 if(jp2->color.jp2_cdef) {
1114 opj_jp2_apply_cdef(p_image, &(jp2->color));
1117 if(jp2->color.jp2_pclr) {
1118 /* Part 1, I.5.3.4: Either both or none : */
1119 if( !jp2->color.jp2_pclr->cmap)
1120 opj_jp2_free_pclr(&(jp2->color));
1122 opj_jp2_apply_pclr(p_image, &(jp2->color));
1125 if(jp2->color.icc_profile_buf) {
1126 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1127 p_image->icc_profile_len = jp2->color.icc_profile_len;
1128 jp2->color.icc_profile_buf = NULL;
1135 opj_bool opj_jp2_write_jp2h(opj_jp2_t *jp2,
1136 opj_stream_private_t *stream,
1137 opj_event_mgr_t * p_manager
1140 opj_jp2_img_header_writer_handler_t l_writers [3];
1141 opj_jp2_img_header_writer_handler_t * l_current_writer;
1143 OPJ_INT32 i, l_nb_pass;
1144 /* size of data for super box*/
1145 OPJ_INT32 l_jp2h_size = 8;
1146 opj_bool l_result = OPJ_TRUE;
1148 /* to store the data of the super box */
1149 OPJ_BYTE l_jp2h_data [8];
1152 assert(stream != 00);
1154 assert(p_manager != 00);
1156 memset(l_writers,0,sizeof(l_writers));
1158 if (jp2->bpc == 255) {
1160 l_writers[0].handler = opj_jp2_write_ihdr;
1161 l_writers[1].handler = opj_jp2_write_bpcc;
1162 l_writers[2].handler = opj_jp2_write_colr;
1166 l_writers[0].handler = opj_jp2_write_ihdr;
1167 l_writers[1].handler = opj_jp2_write_colr;
1170 /* write box header */
1171 /* write JP2H type */
1172 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1174 l_current_writer = l_writers;
1175 for (i=0;i<l_nb_pass;++i) {
1176 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_current_writer->m_size));
1177 if (l_current_writer->m_data == 00) {
1178 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to hold JP2 Header data\n");
1179 l_result = OPJ_FALSE;
1183 l_jp2h_size += l_current_writer->m_size;
1188 l_current_writer = l_writers;
1189 for (i=0;i<l_nb_pass;++i) {
1190 if (l_current_writer->m_data != 00) {
1191 opj_free(l_current_writer->m_data );
1199 /* write super box size */
1200 opj_write_bytes(l_jp2h_data,l_jp2h_size,4);
1202 /* write super box data on stream */
1203 if (opj_stream_write_data(stream,l_jp2h_data,8,p_manager) != 8) {
1204 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1205 l_result = OPJ_FALSE;
1209 l_current_writer = l_writers;
1210 for (i=0;i<l_nb_pass;++i) {
1211 if (opj_stream_write_data(stream,l_current_writer->m_data,l_current_writer->m_size,p_manager) != l_current_writer->m_size) {
1212 opj_event_msg(p_manager, EVT_ERROR, "Stream error while writing JP2 Header box\n");
1213 l_result = OPJ_FALSE;
1220 l_current_writer = l_writers;
1223 for (i=0;i<l_nb_pass;++i) {
1224 if (l_current_writer->m_data != 00) {
1225 opj_free(l_current_writer->m_data );
1233 opj_bool opj_jp2_write_ftyp(opj_jp2_t *jp2,
1234 opj_stream_private_t *cio,
1235 opj_event_mgr_t * p_manager )
1238 OPJ_UINT32 l_ftyp_size = 16 + 4 * jp2->numcl;
1239 OPJ_BYTE * l_ftyp_data, * l_current_data_ptr;
1245 assert(p_manager != 00);
1247 l_ftyp_data = (OPJ_BYTE *) opj_malloc(l_ftyp_size);
1249 if (l_ftyp_data == 00) {
1250 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle ftyp data\n");
1254 memset(l_ftyp_data,0,l_ftyp_size);
1256 l_current_data_ptr = l_ftyp_data;
1258 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */
1259 l_current_data_ptr += 4;
1261 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */
1262 l_current_data_ptr += 4;
1264 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */
1265 l_current_data_ptr += 4;
1267 opj_write_bytes(l_current_data_ptr, jp2->minversion,4); /* MinV */
1268 l_current_data_ptr += 4;
1270 for (i = 0; i < jp2->numcl; i++) {
1271 opj_write_bytes(l_current_data_ptr, jp2->cl[i],4); /* CL */
1274 l_result = (opj_stream_write_data(cio,l_ftyp_data,l_ftyp_size,p_manager) == l_ftyp_size);
1277 opj_event_msg(p_manager, EVT_ERROR, "Error while writing ftyp data to stream\n");
1280 opj_free(l_ftyp_data);
1285 opj_bool opj_jp2_write_jp2c(opj_jp2_t *jp2,
1286 opj_stream_private_t *cio,
1287 opj_event_mgr_t * p_manager )
1289 OPJ_OFF_T j2k_codestream_exit;
1290 OPJ_BYTE l_data_header [8];
1295 assert(p_manager != 00);
1296 assert(opj_stream_has_seek(cio));
1298 j2k_codestream_exit = opj_stream_tell(cio);
1299 opj_write_bytes(l_data_header,
1300 (OPJ_UINT32) (j2k_codestream_exit - jp2->j2k_codestream_offset),
1301 4); /* size of codestream */
1302 opj_write_bytes(l_data_header + 4,JP2_JP2C,4); /* JP2C */
1304 if (! opj_stream_seek(cio,jp2->j2k_codestream_offset,p_manager)) {
1305 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1309 if (opj_stream_write_data(cio,l_data_header,8,p_manager) != 8) {
1310 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1314 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
1315 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
1322 opj_bool opj_jp2_write_jp( opj_jp2_t *jp2,
1323 opj_stream_private_t *cio,
1324 opj_event_mgr_t * p_manager )
1326 /* 12 bytes will be read */
1327 OPJ_BYTE l_signature_data [12];
1332 assert(p_manager != 00);
1334 /* write box length */
1335 opj_write_bytes(l_signature_data,12,4);
1336 /* writes box type */
1337 opj_write_bytes(l_signature_data+4,JP2_JP,4);
1338 /* writes magic number*/
1339 opj_write_bytes(l_signature_data+8,0x0d0a870a,4);
1341 if (opj_stream_write_data(cio,l_signature_data,12,p_manager) != 12) {
1348 /* ----------------------------------------------------------------------- */
1349 /* JP2 decoder interface */
1350 /* ----------------------------------------------------------------------- */
1352 void opj_jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters)
1354 /* setup the J2K codec */
1355 opj_j2k_setup_decoder(jp2->j2k, parameters);
1357 /* further JP2 initializations go here */
1358 jp2->color.jp2_has_colr = 0;
1359 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
1362 /* ----------------------------------------------------------------------- */
1363 /* JP2 encoder interface */
1364 /* ----------------------------------------------------------------------- */
1366 void opj_jp2_setup_encoder( opj_jp2_t *jp2,
1367 opj_cparameters_t *parameters,
1369 opj_event_mgr_t * p_manager)
1372 OPJ_INT32 depth_0, sign;
1374 if(!jp2 || !parameters || !image)
1377 /* setup the J2K codec */
1378 /* ------------------- */
1380 /* Check if number of components respects standard */
1381 if (image->numcomps < 1 || image->numcomps > 16384) {
1382 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
1386 opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager );
1388 /* setup the JP2 codec */
1389 /* ------------------- */
1393 jp2->brand = JP2_JP2; /* BR */
1394 jp2->minversion = 0; /* MinV */
1396 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1397 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
1399 /* Image Header box */
1401 jp2->numcomps = image->numcomps; /* NC */
1402 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
1403 jp2->h = image->y1 - image->y0; /* HEIGHT */
1404 jp2->w = image->x1 - image->x0; /* WIDTH */
1406 depth_0 = image->comps[0].prec - 1;
1407 sign = image->comps[0].sgnd;
1408 jp2->bpc = depth_0 + (sign << 7);
1409 for (i = 1; i < image->numcomps; i++) {
1410 OPJ_INT32 depth = image->comps[i].prec - 1;
1411 sign = image->comps[i].sgnd;
1412 if (depth_0 != depth)
1415 jp2->C = 7; /* C : Always 7 */
1416 jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
1417 jp2->IPR = 0; /* IPR, no intellectual property */
1419 /* BitsPerComponent box */
1420 for (i = 0; i < image->numcomps; i++) {
1421 jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
1424 /* Colour Specification box */
1425 if ((image->numcomps == 1 || image->numcomps == 3) && (jp2->bpc != 255)) {
1426 jp2->meth = 1; /* METH: Enumerated colourspace */
1428 jp2->meth = 2; /* METH: Restricted ICC profile */
1430 if (jp2->meth == 1) {
1431 if (image->color_space == 1)
1432 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
1433 else if (image->color_space == 2)
1434 jp2->enumcs = 17; /* greyscale */
1435 else if (image->color_space == 3)
1436 jp2->enumcs = 18; /* YUV */
1438 jp2->enumcs = 0; /* PROFILE (??) */
1440 jp2->precedence = 0; /* PRECEDENCE */
1441 jp2->approx = 0; /* APPROX */
1443 jp2->jpip_on = parameters->jpip_on;
1446 opj_bool opj_jp2_encode(opj_jp2_t *jp2,
1447 opj_stream_private_t *stream,
1448 opj_event_mgr_t * p_manager)
1450 return opj_j2k_encode_v2(jp2->j2k, stream, p_manager);
1453 opj_bool opj_jp2_end_decompress(opj_jp2_t *jp2,
1454 opj_stream_private_t *cio,
1455 opj_event_mgr_t * p_manager
1461 assert(p_manager != 00);
1463 /* customization of the end encoding */
1464 opj_jp2_setup_end_header_reading(jp2);
1467 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,cio,p_manager)) {
1471 return opj_j2k_end_decompress(jp2->j2k, cio, p_manager);
1474 opj_bool opj_jp2_end_compress( opj_jp2_t *jp2,
1475 opj_stream_private_t *cio,
1476 opj_event_mgr_t * p_manager
1482 assert(p_manager != 00);
1484 /* customization of the end encoding */
1485 opj_jp2_setup_end_header_writing(jp2);
1487 if (! opj_j2k_end_compress(jp2->j2k,cio,p_manager)) {
1492 return opj_jp2_exec(jp2,jp2->m_procedure_list,cio,p_manager);
1495 void opj_jp2_setup_end_header_writing (opj_jp2_t *jp2)
1501 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_iptr );
1502 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2c );
1503 /* DEVELOPER CORNER, add your custom procedures */
1506 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_cidx );
1507 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_write_fidx );
1511 void opj_jp2_setup_end_header_reading (opj_jp2_t *jp2)
1515 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
1516 /* DEVELOPER CORNER, add your custom procedures */
1519 opj_bool opj_jp2_default_validation ( opj_jp2_t * jp2,
1520 opj_stream_private_t *cio,
1521 opj_event_mgr_t * p_manager
1524 opj_bool l_is_valid = OPJ_TRUE;
1530 assert(p_manager != 00);
1532 /* JPEG2000 codec validation */
1534 /* STATE checking */
1535 /* make sure the state is at 0 */
1536 l_is_valid &= (jp2->jp2_state == JP2_STATE_NONE);
1538 /* make sure not reading a jp2h ???? WEIRD */
1539 l_is_valid &= (jp2->jp2_img_state == JP2_IMG_STATE_NONE);
1541 /* POINTER validation */
1542 /* make sure a j2k codec is present */
1543 l_is_valid &= (jp2->j2k != 00);
1545 /* make sure a procedure list is present */
1546 l_is_valid &= (jp2->m_procedure_list != 00);
1548 /* make sure a validation list is present */
1549 l_is_valid &= (jp2->m_validation_list != 00);
1551 /* PARAMETER VALIDATION */
1552 /* number of components */
1553 l_is_valid &= (jp2->numcl > 0);
1555 l_is_valid &= (jp2->h > 0);
1557 l_is_valid &= (jp2->w > 0);
1559 for (i = 0; i < jp2->numcomps; ++i) {
1560 l_is_valid &= (jp2->comps[i].bpcc > 0);
1564 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3));
1566 /* stream validation */
1567 /* back and forth is needed */
1568 l_is_valid &= opj_stream_has_seek(cio);
1573 opj_bool opj_jp2_read_header_procedure( opj_jp2_t *jp2,
1574 opj_stream_private_t *stream,
1575 opj_event_mgr_t * p_manager
1579 OPJ_UINT32 l_nb_bytes_read;
1580 const opj_jp2_header_handler_t * l_current_handler;
1581 OPJ_UINT32 l_last_data_size = BOX_SIZE;
1582 OPJ_UINT32 l_current_data_size;
1583 OPJ_BYTE * l_current_data = 00;
1586 assert(stream != 00);
1588 assert(p_manager != 00);
1590 l_current_data = (OPJ_BYTE*)opj_malloc(l_last_data_size);
1592 if (l_current_data == 00) {
1593 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 file header\n");
1596 memset(l_current_data, 0 , l_last_data_size);
1598 while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) {
1599 /* is it the codestream box ? */
1600 if (box.type == JP2_JP2C) {
1601 if (jp2->jp2_state & JP2_STATE_HEADER) {
1602 jp2->jp2_state |= JP2_STATE_CODESTREAM;
1603 opj_free(l_current_data);
1607 opj_event_msg(p_manager, EVT_ERROR, "bad placed jpeg codestream\n");
1608 opj_free(l_current_data);
1612 else if (box.length == 0) {
1613 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
1614 opj_free(l_current_data);
1618 l_current_handler = opj_jp2_find_handler(box.type);
1619 l_current_data_size = box.length - l_nb_bytes_read;
1621 if (l_current_handler != 00) {
1622 if (l_current_data_size > l_last_data_size) {
1623 OPJ_BYTE* new_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_data_size);
1624 if (!l_current_data){
1625 opj_free(l_current_data);
1626 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle jpeg2000 box\n");
1629 l_current_data = new_current_data;
1630 l_last_data_size = l_current_data_size;
1633 l_nb_bytes_read = opj_stream_read_data(stream,l_current_data,l_current_data_size,p_manager);
1634 if (l_nb_bytes_read != l_current_data_size) {
1635 opj_event_msg(p_manager, EVT_ERROR, "Problem with reading JPEG2000 box, stream error\n");
1636 opj_free(l_current_data);
1640 if (! l_current_handler->handler(jp2,l_current_data,l_current_data_size,p_manager)) {
1641 opj_free(l_current_data);
1646 jp2->jp2_state |= JP2_STATE_UNKNOWN;
1647 if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
1648 opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
1649 opj_free(l_current_data);
1655 opj_free(l_current_data);
1661 * Excutes the given procedures on the given codec.
1663 * @param p_procedure_list the list of procedures to execute
1664 * @param jp2 the jpeg2000 file codec to execute the procedures on.
1665 * @param stream the stream to execute the procedures on.
1666 * @param p_manager the user manager.
1668 * @return true if all the procedures were successfully executed.
1670 static opj_bool opj_jp2_exec ( opj_jp2_t * jp2,
1671 opj_procedure_list_t * p_procedure_list,
1672 opj_stream_private_t *stream,
1673 opj_event_mgr_t * p_manager
1677 opj_bool (** l_procedure) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *) = 00;
1678 opj_bool l_result = OPJ_TRUE;
1679 OPJ_UINT32 l_nb_proc, i;
1682 assert(p_procedure_list != 00);
1684 assert(stream != 00);
1685 assert(p_manager != 00);
1687 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
1688 l_procedure = (opj_bool (**) (opj_jp2_t * jp2, opj_stream_private_t *, opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
1690 for (i=0;i<l_nb_proc;++i) {
1691 l_result = l_result && (*l_procedure) (jp2,stream,p_manager);
1695 /* and clear the procedure list at the end. */
1696 opj_procedure_list_clear(p_procedure_list);
1700 opj_bool opj_jp2_start_compress(opj_jp2_t *jp2,
1701 opj_stream_private_t *stream,
1702 opj_image_t * p_image,
1703 opj_event_mgr_t * p_manager
1708 assert(stream != 00);
1709 assert(p_manager != 00);
1711 /* customization of the validation */
1712 opj_jp2_setup_encoding_validation (jp2);
1714 /* validation of the parameters codec */
1715 if (! opj_jp2_exec(jp2,jp2->m_validation_list,stream,p_manager)) {
1719 /* customization of the encoding */
1720 opj_jp2_setup_header_writing(jp2);
1723 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,stream,p_manager)) {
1727 return opj_j2k_start_compress(jp2->j2k,stream,p_image,p_manager);
1730 const opj_jp2_header_handler_t * opj_jp2_find_handler (OPJ_UINT32 p_id)
1732 OPJ_UINT32 i, l_handler_size = sizeof(jp2_header) / sizeof(opj_jp2_header_handler_t);
1734 for (i=0;i<l_handler_size;++i) {
1735 if (jp2_header[i].id == p_id) {
1736 return &jp2_header[i];
1743 * Finds the image execution function related to the given box id.
1745 * @param p_id the id of the handler to fetch.
1747 * @return the given handler or 00 if it could not be found.
1749 static const opj_jp2_header_handler_t * opj_jp2_img_find_handler (OPJ_UINT32 p_id)
1751 OPJ_UINT32 i, l_handler_size = sizeof(jp2_img_header) / sizeof(opj_jp2_header_handler_t);
1752 for (i=0;i<l_handler_size;++i)
1754 if (jp2_img_header[i].id == p_id) {
1755 return &jp2_img_header[i];
1763 * Reads a jpeg2000 file signature box.
1765 * @param p_header_data the data contained in the signature box.
1766 * @param jp2 the jpeg2000 file codec.
1767 * @param p_header_size the size of the data contained in the signature box.
1768 * @param p_manager the user event manager.
1770 * @return true if the file signature box is valid.
1772 static opj_bool opj_jp2_read_jp(opj_jp2_t *jp2,
1773 OPJ_BYTE * p_header_data,
1774 OPJ_UINT32 p_header_size,
1775 opj_event_mgr_t * p_manager
1779 OPJ_UINT32 l_magic_number;
1782 assert(p_header_data != 00);
1784 assert(p_manager != 00);
1786 if (jp2->jp2_state != JP2_STATE_NONE) {
1787 opj_event_msg(p_manager, EVT_ERROR, "The signature box must be the first box in the file.\n");
1791 /* assure length of data is correct (4 -> magic number) */
1792 if (p_header_size != 4) {
1793 opj_event_msg(p_manager, EVT_ERROR, "Error with JP signature Box size\n");
1797 /* rearrange data */
1798 opj_read_bytes(p_header_data,&l_magic_number,4);
1799 if (l_magic_number != 0x0d0a870a ) {
1800 opj_event_msg(p_manager, EVT_ERROR, "Error with JP Signature : bad magic number\n");
1804 jp2->jp2_state |= JP2_STATE_SIGNATURE;
1810 * Reads a a FTYP box - File type box
1812 * @param p_header_data the data contained in the FTYP box.
1813 * @param jp2 the jpeg2000 file codec.
1814 * @param p_header_size the size of the data contained in the FTYP box.
1815 * @param p_manager the user event manager.
1817 * @return true if the FTYP box is valid.
1819 static opj_bool opj_jp2_read_ftyp( opj_jp2_t *jp2,
1820 OPJ_BYTE * p_header_data,
1821 OPJ_UINT32 p_header_size,
1822 opj_event_mgr_t * p_manager
1825 OPJ_UINT32 i, l_remaining_bytes;
1828 assert(p_header_data != 00);
1830 assert(p_manager != 00);
1832 if (jp2->jp2_state != JP2_STATE_SIGNATURE) {
1833 opj_event_msg(p_manager, EVT_ERROR, "The ftyp box must be the second box in the file.\n");
1837 /* assure length of data is correct */
1838 if (p_header_size < 8) {
1839 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
1843 opj_read_bytes(p_header_data,&jp2->brand,4); /* BR */
1846 opj_read_bytes(p_header_data,&jp2->minversion,4); /* MinV */
1849 l_remaining_bytes = p_header_size - 8;
1851 /* the number of remaining bytes should be a multiple of 4 */
1852 if ((l_remaining_bytes & 0x3) != 0) {
1853 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature Box size\n");
1858 jp2->numcl = l_remaining_bytes >> 2;
1860 jp2->cl = (OPJ_UINT32 *) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32));
1861 if (jp2->cl == 00) {
1862 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory with FTYP Box\n");
1865 memset(jp2->cl,0,jp2->numcl * sizeof(OPJ_UINT32));
1868 for (i = 0; i < jp2->numcl; ++i)
1870 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi */
1874 jp2->jp2_state |= JP2_STATE_FILE_TYPE;
1879 opj_bool opj_jp2_skip_jp2c( opj_jp2_t *jp2,
1880 opj_stream_private_t *stream,
1881 opj_event_mgr_t * p_manager )
1885 assert(stream != 00);
1886 assert(p_manager != 00);
1888 jp2->j2k_codestream_offset = opj_stream_tell(stream);
1890 if (opj_stream_skip(stream,8,p_manager) != 8) {
1897 static opj_bool opj_jpip_skip_iptr( opj_jp2_t *jp2,
1898 opj_stream_private_t *stream,
1899 opj_event_mgr_t * p_manager )
1903 assert(stream != 00);
1904 assert(p_manager != 00);
1906 jp2->jpip_iptr_offset = opj_stream_tell(stream);
1908 if (opj_stream_skip(stream,24,p_manager) != 24) {
1916 * Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
1918 * @param p_header_data the data contained in the file header box.
1919 * @param jp2 the jpeg2000 file codec.
1920 * @param p_header_size the size of the data contained in the file header box.
1921 * @param p_manager the user event manager.
1923 * @return true if the JP2 Header box was successfully reconized.
1925 static opj_bool opj_jp2_read_jp2h( opj_jp2_t *jp2,
1926 OPJ_BYTE *p_header_data,
1927 OPJ_UINT32 p_header_size,
1928 opj_event_mgr_t * p_manager
1931 OPJ_UINT32 l_box_size=0, l_current_data_size = 0;
1933 const opj_jp2_header_handler_t * l_current_handler;
1936 assert(p_header_data != 00);
1938 assert(p_manager != 00);
1940 /* make sure the box is well placed */
1941 if ((jp2->jp2_state & JP2_STATE_FILE_TYPE) != JP2_STATE_FILE_TYPE ) {
1942 opj_event_msg(p_manager, EVT_ERROR, "The box must be the first box in the file.\n");
1946 jp2->jp2_img_state = JP2_IMG_STATE_NONE;
1948 /* iterate while remaining data */
1949 while (p_header_size > 0) {
1951 if (! opj_jp2_read_boxhdr_char(&box,p_header_data,&l_box_size,p_header_size, p_manager)) {
1952 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box\n");
1956 if (box.length > p_header_size) {
1957 opj_event_msg(p_manager, EVT_ERROR, "Stream error while reading JP2 Header box: box length is inconsistent.\n");
1961 l_current_handler = opj_jp2_img_find_handler(box.type);
1962 l_current_data_size = box.length - l_box_size;
1963 p_header_data += l_box_size;
1965 if (l_current_handler != 00) {
1966 if (! l_current_handler->handler(jp2,p_header_data,l_current_data_size,p_manager)) {
1971 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN;
1974 p_header_data += l_current_data_size;
1975 p_header_size -= box.length;
1978 jp2->jp2_state |= JP2_STATE_HEADER;
1983 opj_bool opj_jp2_read_boxhdr_char( opj_jp2_box_t *box,
1985 OPJ_UINT32 * p_number_bytes_read,
1986 OPJ_UINT32 p_box_max_size,
1987 opj_event_mgr_t * p_manager
1993 assert(p_data != 00);
1995 assert(p_number_bytes_read != 00);
1996 assert(p_manager != 00);
1998 if (p_box_max_size < 8) {
1999 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of less than 8 bytes\n");
2003 /* process read data */
2004 opj_read_bytes(p_data, &l_value, 4);
2006 box->length = (OPJ_INT32)(l_value);
2008 opj_read_bytes(p_data, &l_value, 4);
2010 box->type = (OPJ_INT32)(l_value);
2012 *p_number_bytes_read = 8;
2014 /* do we have a "special very large box ?" */
2015 /* read then the XLBox */
2016 if (box->length == 1) {
2017 OPJ_UINT32 l_xl_part_size;
2019 if (p_box_max_size < 16) {
2020 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle XL box of less than 16 bytes\n");
2024 opj_read_bytes(p_data,&l_xl_part_size, 4);
2026 *p_number_bytes_read += 4;
2028 if (l_xl_part_size != 0) {
2029 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
2033 opj_read_bytes(p_data, &l_value, 4);
2034 *p_number_bytes_read += 4;
2035 box->length = (OPJ_INT32)(l_value);
2037 if (box->length == 0) {
2038 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2042 else if (box->length == 0) {
2043 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box of undefined sizes\n");
2050 opj_bool opj_jp2_read_header( opj_stream_private_t *p_stream,
2052 opj_image_t ** p_image,
2053 opj_event_mgr_t * p_manager
2058 assert(p_stream != 00);
2059 assert(p_manager != 00);
2061 /* customization of the validation */
2062 opj_jp2_setup_decoding_validation (jp2);
2064 /* customization of the encoding */
2065 opj_jp2_setup_header_reading(jp2);
2067 /* validation of the parameters codec */
2068 if (! opj_jp2_exec(jp2,jp2->m_validation_list,p_stream,p_manager)) {
2073 if (! opj_jp2_exec (jp2,jp2->m_procedure_list,p_stream,p_manager)) {
2077 return opj_j2k_read_header( p_stream,
2083 void opj_jp2_setup_encoding_validation (opj_jp2_t *jp2)
2088 opj_procedure_list_add_procedure(jp2->m_validation_list, (opj_procedure)opj_jp2_default_validation);
2089 /* DEVELOPER CORNER, add your custom validation procedure */
2092 void opj_jp2_setup_decoding_validation (opj_jp2_t *jp2)
2096 /* DEVELOPER CORNER, add your custom validation procedure */
2099 void opj_jp2_setup_header_writing (opj_jp2_t *jp2)
2104 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp );
2105 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_ftyp );
2106 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_write_jp2h );
2108 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jpip_skip_iptr );
2109 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_skip_jp2c );
2111 /* DEVELOPER CORNER, insert your custom procedures */
2115 void opj_jp2_setup_header_reading (opj_jp2_t *jp2)
2120 opj_procedure_list_add_procedure(jp2->m_procedure_list,(opj_procedure)opj_jp2_read_header_procedure );
2121 /* DEVELOPER CORNER, add your custom procedures */
2124 opj_bool opj_jp2_read_tile_header ( opj_jp2_t * p_jp2,
2125 OPJ_UINT32 * p_tile_index,
2126 OPJ_UINT32 * p_data_size,
2127 OPJ_INT32 * p_tile_x0,
2128 OPJ_INT32 * p_tile_y0,
2129 OPJ_INT32 * p_tile_x1,
2130 OPJ_INT32 * p_tile_y1,
2131 OPJ_UINT32 * p_nb_comps,
2133 opj_stream_private_t *p_stream,
2134 opj_event_mgr_t * p_manager
2137 return opj_j2k_read_tile_header(p_jp2->j2k,
2140 p_tile_x0, p_tile_y0,
2141 p_tile_x1, p_tile_y1,
2148 opj_bool opj_jp2_write_tile ( opj_jp2_t *p_jp2,
2149 OPJ_UINT32 p_tile_index,
2151 OPJ_UINT32 p_data_size,
2152 opj_stream_private_t *p_stream,
2153 opj_event_mgr_t * p_manager
2157 return opj_j2k_write_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2160 opj_bool opj_jp2_decode_tile ( opj_jp2_t * p_jp2,
2161 OPJ_UINT32 p_tile_index,
2163 OPJ_UINT32 p_data_size,
2164 opj_stream_private_t *p_stream,
2165 opj_event_mgr_t * p_manager
2168 return opj_j2k_decode_tile (p_jp2->j2k,p_tile_index,p_data,p_data_size,p_stream,p_manager);
2171 void opj_jp2_destroy(opj_jp2_t *jp2)
2174 /* destroy the J2K codec */
2175 opj_j2k_destroy(jp2->j2k);
2179 opj_free(jp2->comps);
2188 if (jp2->color.icc_profile_buf) {
2189 opj_free(jp2->color.icc_profile_buf);
2190 jp2->color.icc_profile_buf = 00;
2193 if (jp2->color.jp2_cdef) {
2194 if (jp2->color.jp2_cdef->info) {
2195 opj_free(jp2->color.jp2_cdef->info);
2196 jp2->color.jp2_cdef->info = NULL;
2199 opj_free(jp2->color.jp2_cdef);
2200 jp2->color.jp2_cdef = 00;
2203 if (jp2->color.jp2_pclr) {
2204 if (jp2->color.jp2_pclr->cmap) {
2205 opj_free(jp2->color.jp2_pclr->cmap);
2206 jp2->color.jp2_pclr->cmap = NULL;
2208 if (jp2->color.jp2_pclr->channel_sign) {
2209 opj_free(jp2->color.jp2_pclr->channel_sign);
2210 jp2->color.jp2_pclr->channel_sign = NULL;
2212 if (jp2->color.jp2_pclr->channel_size) {
2213 opj_free(jp2->color.jp2_pclr->channel_size);
2214 jp2->color.jp2_pclr->channel_size = NULL;
2216 if (jp2->color.jp2_pclr->entries) {
2217 opj_free(jp2->color.jp2_pclr->entries);
2218 jp2->color.jp2_pclr->entries = NULL;
2221 opj_free(jp2->color.jp2_pclr);
2222 jp2->color.jp2_pclr = 00;
2225 if (jp2->m_validation_list) {
2226 opj_procedure_list_destroy(jp2->m_validation_list);
2227 jp2->m_validation_list = 00;
2230 if (jp2->m_procedure_list) {
2231 opj_procedure_list_destroy(jp2->m_procedure_list);
2232 jp2->m_procedure_list = 00;
2239 opj_bool opj_jp2_set_decode_area( opj_jp2_t *p_jp2,
2240 opj_image_t* p_image,
2241 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
2242 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
2243 opj_event_mgr_t * p_manager
2246 return opj_j2k_set_decode_area(p_jp2->j2k, p_image, p_start_x, p_start_y, p_end_x, p_end_y, p_manager);
2249 opj_bool opj_jp2_get_tile( opj_jp2_t *p_jp2,
2250 opj_stream_private_t *p_stream,
2251 opj_image_t* p_image,
2252 opj_event_mgr_t * p_manager,
2253 OPJ_UINT32 tile_index
2259 opj_event_msg(p_manager, EVT_WARNING, "JP2 box which are after the codestream will not be read by this function.\n");
2261 if (! opj_j2k_get_tile(p_jp2->j2k, p_stream, p_image, p_manager, tile_index) ){
2262 opj_event_msg(p_manager, EVT_ERROR, "Failed to decode the codestream in the JP2 file\n");
2266 /* Set Image Color Space */
2267 if (p_jp2->enumcs == 16)
2268 p_image->color_space = CLRSPC_SRGB;
2269 else if (p_jp2->enumcs == 17)
2270 p_image->color_space = CLRSPC_GRAY;
2271 else if (p_jp2->enumcs == 18)
2272 p_image->color_space = CLRSPC_SYCC;
2274 p_image->color_space = CLRSPC_UNKNOWN;
2276 /* Apply the color space if needed */
2277 if(p_jp2->color.jp2_cdef) {
2278 opj_jp2_apply_cdef(p_image, &(p_jp2->color));
2281 if(p_jp2->color.jp2_pclr) {
2282 /* Part 1, I.5.3.4: Either both or none : */
2283 if( !p_jp2->color.jp2_pclr->cmap)
2284 opj_jp2_free_pclr(&(p_jp2->color));
2286 opj_jp2_apply_pclr(p_image, &(p_jp2->color));
2289 if(p_jp2->color.icc_profile_buf) {
2290 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
2291 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
2292 p_jp2->color.icc_profile_buf = NULL;
2298 /* ----------------------------------------------------------------------- */
2299 /* JP2 encoder interface */
2300 /* ----------------------------------------------------------------------- */
2302 opj_jp2_t* opj_jp2_create(opj_bool p_is_decoder)
2304 opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
2306 memset(jp2,0,sizeof(opj_jp2_t));
2308 /* create the J2K codec */
2309 if (! p_is_decoder) {
2310 jp2->j2k = opj_j2k_create_compress();
2313 jp2->j2k = opj_j2k_create_decompress();
2316 if (jp2->j2k == 00) {
2317 opj_jp2_destroy(jp2);
2321 /* Color structure */
2322 jp2->color.icc_profile_buf = NULL;
2323 jp2->color.icc_profile_len = 0;
2324 jp2->color.jp2_cdef = NULL;
2325 jp2->color.jp2_pclr = NULL;
2326 jp2->color.jp2_has_colr = 0;
2328 /* validation list creation */
2329 jp2->m_validation_list = opj_procedure_list_create();
2330 if (! jp2->m_validation_list) {
2331 opj_jp2_destroy(jp2);
2335 /* execution list creation */
2336 jp2->m_procedure_list = opj_procedure_list_create();
2337 if (! jp2->m_procedure_list) {
2338 opj_jp2_destroy(jp2);
2346 void jp2_dump(opj_jp2_t* p_jp2, OPJ_INT32 flag, FILE* out_stream)
2349 assert(p_jp2 != 00);
2351 j2k_dump(p_jp2->j2k,
2356 opj_codestream_index_t* jp2_get_cstr_index(opj_jp2_t* p_jp2)
2358 return j2k_get_cstr_index(p_jp2->j2k);
2361 opj_codestream_info_v2_t* jp2_get_cstr_info(opj_jp2_t* p_jp2)
2363 return j2k_get_cstr_info(p_jp2->j2k);
2366 opj_bool opj_jp2_set_decoded_resolution_factor(opj_jp2_t *p_jp2,
2367 OPJ_UINT32 res_factor,
2368 opj_event_mgr_t * p_manager)
2370 return opj_j2k_set_decoded_resolution_factor(p_jp2->j2k, res_factor, p_manager);
2375 static opj_bool opj_jpip_write_iptr(opj_jp2_t *jp2,
2376 opj_stream_private_t *cio,
2377 opj_event_mgr_t * p_manager )
2379 OPJ_OFF_T j2k_codestream_exit;
2380 OPJ_BYTE l_data_header [24];
2385 assert(p_manager != 00);
2386 assert(opj_stream_has_seek(cio));
2388 j2k_codestream_exit = opj_stream_tell(cio);
2389 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2390 opj_write_bytes(l_data_header + 4,JPIP_IPTR,4); /* IPTR */
2392 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
2393 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
2395 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2396 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2399 if (! opj_stream_seek(cio,jp2->jpip_iptr_offset,p_manager)) {
2400 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2404 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2405 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2409 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2410 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2417 static opj_bool opj_jpip_write_fidx(opj_jp2_t *jp2,
2418 opj_stream_private_t *cio,
2419 opj_event_mgr_t * p_manager )
2421 OPJ_OFF_T j2k_codestream_exit;
2422 OPJ_BYTE l_data_header [24];
2427 assert(p_manager != 00);
2428 assert(opj_stream_has_seek(cio));
2430 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2431 opj_write_bytes(l_data_header + 4,JPIP_FIDX,4); /* IPTR */
2432 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2433 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2435 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2436 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2440 j2k_codestream_exit = opj_stream_tell(cio);
2441 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2442 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2449 static opj_bool opj_jpip_write_cidx(opj_jp2_t *jp2,
2450 opj_stream_private_t *cio,
2451 opj_event_mgr_t * p_manager )
2453 OPJ_OFF_T j2k_codestream_exit;
2454 OPJ_BYTE l_data_header [24];
2459 assert(p_manager != 00);
2460 assert(opj_stream_has_seek(cio));
2462 j2k_codestream_exit = opj_stream_tell(cio);
2463 opj_write_bytes(l_data_header, 24, 4); /* size of iptr */
2464 opj_write_bytes(l_data_header + 4,JPIP_CIDX,4); /* IPTR */
2466 opj_write_bytes(l_data_header + 4 + 4, 0, 8); /* offset */
2467 opj_write_bytes(l_data_header + 8 + 8, 0, 8); /* length */
2469 opj_write_double(l_data_header + 4 + 4, 0); /* offset */
2470 opj_write_double(l_data_header + 8 + 8, 0); /* length */
2473 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2474 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2478 if (opj_stream_write_data(cio,l_data_header,24,p_manager) != 24) {
2479 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2483 j2k_codestream_exit = opj_stream_tell(cio);
2484 if (! opj_stream_seek(cio,j2k_codestream_exit,p_manager)) {
2485 opj_event_msg(p_manager, EVT_ERROR, "Failed to seek in the stream.\n");
2492 static void write_prxy_v2( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
2493 opj_event_mgr_t * p_manager )
2495 OPJ_BYTE l_data_header [8];
2499 lenp = cio_tell( cio);
2500 cio_skip( cio, 4); /* L [at the end] */
2501 cio_write( cio, JPIP_PRXY, 4); /* IPTR */
2503 lenp = opj_stream_tell(cio);
2504 opj_stream_skip(cio, 4, p_manager); /* L [at the end] */
2505 opj_write_bytes(l_data_header,JPIP_PRXY,4); /* IPTR */
2506 opj_stream_write_data(cio,l_data_header,4,p_manager);
2510 cio_write( cio, offset_jp2c, 8); /* OOFF */
2511 cio_write( cio, length_jp2c, 4); /* OBH part 1 */
2512 cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
2514 opj_write_bytes( l_data_header, offset_jp2c, 8); /* OOFF */
2515 opj_stream_write_data(cio,l_data_header,8,p_manager);
2516 opj_write_bytes( l_data_header, length_jp2c, 4); /* OBH part 1 */
2517 opj_write_bytes( l_data_header+4, JP2_JP2C, 4); /* OBH part 2 */
2518 opj_stream_write_data(cio,l_data_header,8,p_manager);
2522 cio_write( cio, 1,1); /* NI */
2524 opj_write_bytes( l_data_header, 1, 1);/* NI */
2525 opj_stream_write_data(cio,l_data_header,1,p_manager);
2529 cio_write( cio, offset_idx, 8); /* IOFF */
2530 cio_write( cio, length_idx, 4); /* IBH part 1 */
2531 cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
2533 opj_write_bytes( l_data_header, offset_idx, 8); /* IOFF */
2534 opj_stream_write_data(cio,l_data_header,8,p_manager);
2535 opj_write_bytes( l_data_header, length_idx, 4); /* IBH part 1 */
2536 opj_write_bytes( l_data_header+4, JPIP_CIDX, 4); /* IBH part 2 */
2537 opj_stream_write_data(cio,l_data_header,8,p_manager);
2541 len = cio_tell( cio)-lenp;
2542 cio_seek( cio, lenp);
2543 cio_write( cio, len, 4); /* L */
2544 cio_seek( cio, lenp+len);
2546 len = opj_stream_tell(cio)-lenp;
2547 opj_stream_skip(cio, lenp, p_manager);
2548 opj_write_bytes(l_data_header,len,4);/* L */
2549 opj_stream_write_data(cio,l_data_header,4,p_manager);
2550 opj_stream_seek(cio, lenp+len,p_manager);
2555 static int write_fidx_v2( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_stream_private_t *cio,
2556 opj_event_mgr_t * p_manager )
2558 OPJ_BYTE l_data_header [4];
2562 lenp = cio_tell( cio);
2563 cio_skip( cio, 4); /* L [at the end] */
2564 cio_write( cio, JPIP_FIDX, 4); /* IPTR */
2566 lenp = opj_stream_tell(cio);
2567 opj_stream_skip(cio, 4, p_manager);
2568 opj_write_bytes(l_data_header,JPIP_FIDX,4); /* FIDX */
2569 opj_stream_write_data(cio,l_data_header,4,p_manager);
2572 write_prxy_v2( offset_jp2c, length_jp2c, offset_idx, length_idx, cio,p_manager);
2575 len = cio_tell( cio)-lenp;
2576 cio_seek( cio, lenp);
2577 cio_write( cio, len, 4); /* L */
2578 cio_seek( cio, lenp+len);
2580 len = opj_stream_tell(cio)-lenp;
2581 opj_stream_skip(cio, lenp, p_manager);
2582 opj_write_bytes(l_data_header,len,4);/* L */
2583 opj_stream_write_data(cio,l_data_header,4,p_manager);
2584 opj_stream_seek(cio, lenp+len,p_manager);