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) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9 * Copyright (c) 2006-2007, Parvatha Elangovan
10 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Copyright (c) 2011, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
41 /** @name Local static functions */
45 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
47 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 opj_bool j2k_read_header_procedure(
54 struct opj_stream_private *p_stream,
55 struct opj_event_mgr * p_manager);
58 * The default encoding validation procedure without any extension.
60 * @param p_j2k the jpeg2000 codec to validate.
61 * @param p_stream the input stream to validate.
62 * @param p_manager the user event manager.
64 * @return true if the parameters are correct.
66 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
67 opj_stream_private_t *p_stream,
68 opj_event_mgr_t * p_manager );
71 * The default decoding validation procedure without any extension.
73 * @param p_j2k the jpeg2000 codec to validate.
74 * @param p_stream the input stream to validate.
75 * @param p_manager the user event manager.
77 * @return true if the parameters are correct.
79 opj_bool j2k_decoding_validation (
81 opj_stream_private_t *p_stream,
82 opj_event_mgr_t * p_manager
86 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
87 * are valid. Developpers wanting to extend the library can add their own validation procedures.
89 static void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
92 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
93 * are valid. Developpers wanting to extend the library can add their own validation procedures.
95 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
98 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
99 * are valid. Developpers wanting to extend the library can add their own validation procedures.
101 static void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
104 * The mct encoding validation procedure.
106 * @param p_j2k the jpeg2000 codec to validate.
107 * @param p_stream the input stream to validate.
108 * @param p_manager the user event manager.
110 * @return true if the parameters are correct.
112 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
113 opj_stream_private_t *p_stream,
114 opj_event_mgr_t * p_manager );
117 * Builds the tcd decoder to use to decode tile.
119 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
120 opj_stream_private_t *p_stream,
121 opj_event_mgr_t * p_manager );
123 * Builds the tcd encoder to use to encode tile.
125 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
126 opj_stream_private_t *p_stream,
127 opj_event_mgr_t * p_manager );
130 * Creates a tile-coder decoder.
132 * @param p_stream the stream to write data to.
133 * @param p_j2k J2K codec.
134 * @param p_manager the user event manager.
136 static opj_bool j2k_create_tcd( opj_j2k_v2_t *p_j2k,
137 struct opj_stream_private *p_stream,
138 struct opj_event_mgr * p_manager );
141 * Excutes the given procedures on the given codec.
143 * @param p_procedure_list the list of procedures to execute
144 * @param p_j2k the jpeg2000 codec to execute the procedures on.
145 * @param p_stream the stream to execute the procedures on.
146 * @param p_manager the user manager.
148 * @return true if all the procedures were successfully executed.
150 static opj_bool j2k_exec (
151 opj_j2k_v2_t * p_j2k,
152 opj_procedure_list_t * p_procedure_list,
153 opj_stream_private_t *p_stream,
154 opj_event_mgr_t * p_manager
158 * Updates the rates of the tcp.
160 * @param p_stream the stream to write data to.
161 * @param p_j2k J2K codec.
162 * @param p_manager the user event manager.
164 static opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
165 struct opj_stream_private *p_stream,
166 struct opj_event_mgr * p_manager );
169 * Copies the decoding tile parameters onto all the tile parameters.
170 * Creates also the tile decoder.
172 opj_bool j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
173 opj_stream_private_t *p_stream,
174 opj_event_mgr_t * p_manager );
177 * Destroys the memory associated with the decoding of headers.
179 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
180 opj_stream_private_t *p_stream,
181 opj_event_mgr_t * p_manager );
184 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
185 * with the marker value.
186 * @param p_id Marker value to look up
188 * @return the handler associated with the id.
190 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
193 * Destroys a tile coding parameter structure.
195 * @param p_tcp the tile coding parameter to destroy.
197 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
200 * Destroys the data inside a tile coding parameter structure.
202 * @param p_tcp the tile coding parameter which contain data to destroy.
204 static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
207 * Destroys a coding parameter structure.
209 * @param p_cp the coding parameter to destroy.
211 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
215 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
217 * @param p_comp_no the component number to output.
218 * @param p_stream the stream to write data to.
219 * @param p_j2k J2K codec.
220 * @param p_manager the user event manager.
223 static opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
224 OPJ_UINT32 p_tile_no,
225 OPJ_UINT32 p_comp_no,
227 OPJ_UINT32 * p_header_size,
228 struct opj_event_mgr * p_manager );
231 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
233 * @param p_tile_no the tile index.
234 * @param p_comp_no the component being outputted.
235 * @param p_j2k the J2K codec.
237 * @return the number of bytes taken by the SPCod element.
239 static OPJ_UINT32 j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
240 OPJ_UINT32 p_tile_no,
241 OPJ_UINT32 p_comp_no );
244 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
245 * @param p_header_data the data contained in the COM box.
246 * @param p_j2k the jpeg2000 codec.
247 * @param p_header_size the size of the data contained in the COM marker.
248 * @param p_manager the user event manager.
250 static opj_bool j2k_read_SPCod_SPCoc(
253 OPJ_BYTE * p_header_data,
254 OPJ_UINT32 * p_header_size,
255 struct opj_event_mgr * p_manager
259 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
261 * @param p_tile_no the tile index.
262 * @param p_comp_no the component being outputted.
263 * @param p_j2k the J2K codec.
265 * @return the number of bytes taken by the SPCod element.
267 static OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
268 OPJ_UINT32 p_tile_no,
269 OPJ_UINT32 p_comp_no );
272 * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
274 * @param p_tile_no the tile to output.
275 * @param p_comp_no the component number to output.
276 * @param p_data the data buffer.
277 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
278 * @param p_j2k J2K codec.
279 * @param p_manager the user event manager.
282 static opj_bool j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
283 OPJ_UINT32 p_tile_no,
284 OPJ_UINT32 p_comp_no,
286 OPJ_UINT32 * p_header_size,
287 struct opj_event_mgr * p_manager
291 * Updates the Tile Length Marker.
293 static void j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
296 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
298 * @param p_tile_no the tile to output.
299 * @param p_comp_no the component number to output.
300 * @param p_data the data buffer.
301 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
302 * @param p_j2k J2K codec.
303 * @param p_manager the user event manager.
306 static opj_bool j2k_read_SQcd_SQcc(
309 OPJ_BYTE * p_header_data,
310 OPJ_UINT32 * p_header_size,
311 struct opj_event_mgr * p_manager
315 * Copies the tile component parameters of all the component from the first tile component.
317 * @param p_j2k the J2k codec.
319 static void j2k_copy_tile_component_parameters(
324 * Copies the tile quantization parameters of all the component from the first tile component.
326 * @param p_j2k the J2k codec.
328 static void j2k_copy_tile_quantization_parameters(
335 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
336 opj_stream_private_t *p_stream,
337 opj_event_mgr_t * p_manager);
340 static opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
341 OPJ_UINT32 p_tile_index,
342 opj_stream_private_t *p_stream,
343 opj_event_mgr_t * p_manager );
345 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
347 static void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
349 static opj_bool j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
351 OPJ_UINT32 p_data_size,
352 opj_stream_private_t *p_stream,
353 opj_event_mgr_t * p_manager );
356 * Sets up the procedures to do on writing header.
357 * Developers wanting to extend the library can add their own writing procedures.
359 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
361 static opj_bool j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k,
363 OPJ_UINT32 * p_data_written,
364 OPJ_UINT32 p_total_data_size,
365 opj_stream_private_t *p_stream,
366 struct opj_event_mgr * p_manager );
368 static opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
370 OPJ_UINT32 * p_data_written,
371 OPJ_UINT32 p_total_data_size,
372 opj_stream_private_t *p_stream,
373 struct opj_event_mgr * p_manager );
376 * Gets the offset of the header.
378 * @param p_stream the stream to write data to.
379 * @param p_j2k J2K codec.
380 * @param p_manager the user event manager.
382 static opj_bool j2k_get_end_header( opj_j2k_v2_t *p_j2k,
383 struct opj_stream_private *p_stream,
384 struct opj_event_mgr * p_manager );
387 * -----------------------------------------------------------------------
388 * -----------------------------------------------------------------------
389 * -----------------------------------------------------------------------
393 Write the SOC marker (Start Of Codestream)
394 @param j2k J2K handle
396 static void j2k_write_soc(opj_j2k_t *j2k);
399 * Writes the SOC marker (Start Of Codestream)
401 * @param p_stream the stream to write data to.
402 * @param p_j2k J2K codec.
403 * @param p_manager the user event manager.
405 static opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
406 struct opj_stream_private *p_stream,
407 struct opj_event_mgr * p_manager );
410 Read the SOC marker (Start of Codestream)
411 @param j2k J2K handle
413 static void j2k_read_soc(opj_j2k_t *j2k);
416 * Reads a SOC marker (Start of Codestream)
417 * @param p_header_data the data contained in the SOC box.
418 * @param jp2 the jpeg2000 file codec.
419 * @param p_header_size the size of the data contained in the SOC marker.
420 * @param p_manager the user event manager.
422 static opj_bool j2k_read_soc_v2(
424 struct opj_stream_private *p_stream,
425 struct opj_event_mgr * p_manager
429 Write the SIZ marker (image and tile size)
430 @param j2k J2K handle
432 static void j2k_write_siz(opj_j2k_t *j2k);
435 * Writes the SIZ marker (image and tile size)
437 * @param p_stream the stream to write data to.
438 * @param p_j2k J2K codec.
439 * @param p_manager the user event manager.
441 static opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
442 struct opj_stream_private *p_stream,
443 struct opj_event_mgr * p_manager );
446 Read the SIZ marker (image and tile size)
447 @param j2k J2K handle
449 static void j2k_read_siz(opj_j2k_t *j2k);
452 * Reads a SIZ marker (image and tile size)
453 * @param p_header_data the data contained in the SIZ box.
454 * @param jp2 the jpeg2000 file codec.
455 * @param p_header_size the size of the data contained in the SIZ marker.
456 * @param p_manager the user event manager.
458 static opj_bool j2k_read_siz_v2 (
460 OPJ_BYTE * p_header_data,
461 OPJ_UINT32 p_header_size,
462 struct opj_event_mgr * p_manager
466 Write the COM marker (comment)
467 @param j2k J2K handle
469 static void j2k_write_com(opj_j2k_t *j2k);
471 Read the COM marker (comment)
472 @param j2k J2K handle
474 static void j2k_read_com(opj_j2k_t *j2k);
476 * Reads a COM marker (comments)
477 * @param p_header_data the data contained in the COM box.
478 * @param jp2 the jpeg2000 file codec.
479 * @param p_header_size the size of the data contained in the COM marker.
480 * @param p_manager the user event manager.
482 static opj_bool j2k_read_com_v2 (
484 OPJ_BYTE * p_header_data,
485 OPJ_UINT32 p_header_size,
486 struct opj_event_mgr * p_manager
489 Write the value concerning the specified component in the marker COD and COC
490 @param j2k J2K handle
491 @param compno Number of the component concerned by the information written
493 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
495 Read the value concerning the specified component in the marker COD and COC
496 @param j2k J2K handle
497 @param compno Number of the component concerned by the information read
499 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
501 Write the COD marker (coding style default)
502 @param j2k J2K handle
504 static void j2k_write_cod(opj_j2k_t *j2k);
507 * Writes the COD marker (Coding style default)
509 * @param p_stream the stream to write data to.
510 * @param p_j2k J2K codec.
511 * @param p_manager the user event manager.
513 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
514 struct opj_stream_private *p_stream,
515 struct opj_event_mgr * p_manager );
518 Read the COD marker (coding style default)
519 @param j2k J2K handle
521 static void j2k_read_cod(opj_j2k_t *j2k);
524 * Reads a COD marker (Coding Styke defaults)
525 * @param p_header_data the data contained in the COD box.
526 * @param p_j2k the jpeg2000 codec.
527 * @param p_header_size the size of the data contained in the COD marker.
528 * @param p_manager the user event manager.
530 static opj_bool j2k_read_cod_v2 (
532 OPJ_BYTE * p_header_data,
533 OPJ_UINT32 p_header_size,
534 struct opj_event_mgr * p_manager
538 Write the COC marker (coding style component)
539 @param j2k J2K handle
540 @param compno Number of the component concerned by the information written
542 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
545 * Writes the COC marker (Coding style component)
547 * @param p_comp_number the index of the component to output.
548 * @param p_stream the stream to write data to.
549 * @param p_j2k J2K codec.
550 * @param p_manager the user event manager.
552 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
553 OPJ_UINT32 p_comp_number,
554 struct opj_stream_private *p_stream,
555 struct opj_event_mgr * p_manager
558 * Writes the COC marker (Coding style component)
560 * @param p_comp_no the index of the component to output.
561 * @param p_stream the stream to write data to.
562 * @param p_j2k J2K codec.
563 * @param p_manager the user event manager.
565 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
566 OPJ_UINT32 p_comp_no,
568 OPJ_UINT32 * p_data_written,
569 struct opj_event_mgr * p_manager );
572 * Gets the maximum size taken by a coc.
574 * @param p_j2k the jpeg2000 codec to use.
576 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
579 Read the COC marker (coding style component)
580 @param j2k J2K handle
582 static void j2k_read_coc(opj_j2k_t *j2k);
585 * Reads a COC marker (Coding Style Component)
586 * @param p_header_data the data contained in the COC box.
587 * @param p_j2k the jpeg2000 codec.
588 * @param p_header_size the size of the data contained in the COC marker.
589 * @param p_manager the user event manager.
591 static opj_bool j2k_read_coc_v2 (
593 OPJ_BYTE * p_header_data,
594 OPJ_UINT32 p_header_size,
595 struct opj_event_mgr * p_manager
599 Write the value concerning the specified component in the marker QCD and QCC
600 @param j2k J2K handle
601 @param compno Number of the component concerned by the information written
603 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
605 Read the value concerning the specified component in the marker QCD and QCC
606 @param j2k J2K handle
607 @param compno Number of the component concern by the information read
608 @param len Length of the information in the QCX part of the marker QCD/QCC
610 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
612 Write the QCD marker (quantization default)
613 @param j2k J2K handle
615 static void j2k_write_qcd(opj_j2k_t *j2k);
618 * Writes the QCD marker (quantization default)
620 * @param p_comp_number the index of the component to output.
621 * @param p_stream the stream to write data to.
622 * @param p_j2k J2K codec.
623 * @param p_manager the user event manager.
625 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
626 struct opj_stream_private *p_stream,
627 struct opj_event_mgr * p_manager );
630 Read the QCD marker (quantization default)
631 @param j2k J2K handle
633 static void j2k_read_qcd(opj_j2k_t *j2k);
636 * Reads a QCD marker (Quantization defaults)
637 * @param p_header_data the data contained in the QCD box.
638 * @param p_j2k the jpeg2000 codec.
639 * @param p_header_size the size of the data contained in the QCD marker.
640 * @param p_manager the user event manager.
642 static opj_bool j2k_read_qcd_v2 (
644 OPJ_BYTE * p_header_data,
645 OPJ_UINT32 p_header_size,
646 struct opj_event_mgr * p_manager
650 Write the QCC marker (quantization component)
651 @param j2k J2K handle
652 @param compno Number of the component concerned by the information written
654 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
657 * Writes the QCC marker (quantization component)
659 * @param p_comp_no the index of the component to output.
660 * @param p_stream the stream to write data to.
661 * @param p_j2k J2K codec.
662 * @param p_manager the user event manager.
664 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
665 OPJ_UINT32 p_comp_no,
666 struct opj_stream_private *p_stream,
667 struct opj_event_mgr * p_manager );
670 * Writes the QCC marker (quantization component)
672 * @param p_comp_no the index of the component to output.
673 * @param p_stream the stream to write data to.
674 * @param p_j2k J2K codec.
675 * @param p_manager the user event manager.
677 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
678 OPJ_UINT32 p_comp_no,
680 OPJ_UINT32 * p_data_written,
681 struct opj_event_mgr * p_manager );
684 * Gets the maximum size taken by a qcc.
686 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
689 Read the QCC marker (quantization component)
690 @param j2k J2K handle
692 static void j2k_read_qcc(opj_j2k_t *j2k);
694 * Reads a QCC marker (Quantization component)
695 * @param p_header_data the data contained in the QCC box.
696 * @param p_j2k the jpeg2000 codec.
697 * @param p_header_size the size of the data contained in the QCC marker.
698 * @param p_manager the user event manager.
700 static opj_bool j2k_read_qcc_v2(
702 OPJ_BYTE * p_header_data,
703 OPJ_UINT32 p_header_size,
704 struct opj_event_mgr * p_manager);
707 Write the POC marker (progression order change)
708 @param j2k J2K handle
710 static void j2k_write_poc(opj_j2k_t *j2k);
713 * Writes the POC marker (Progression Order Change)
715 * @param p_stream the stream to write data to.
716 * @param p_j2k J2K codec.
717 * @param p_manager the user event manager.
719 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
721 OPJ_UINT32 * p_data_written,
722 struct opj_event_mgr * p_manager );
725 * Gets the maximum size taken by the writting of a POC.
727 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
730 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
732 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
735 * Gets the maximum size taken by the headers of the SOT.
737 * @param p_j2k the jpeg2000 codec to use.
739 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
742 Read the POC marker (progression order change)
743 @param j2k J2K handle
745 static void j2k_read_poc(opj_j2k_t *j2k);
747 * Reads a POC marker (Progression Order Change)
749 * @param p_header_data the data contained in the POC box.
750 * @param p_j2k the jpeg2000 codec.
751 * @param p_header_size the size of the data contained in the POC marker.
752 * @param p_manager the user event manager.
754 static opj_bool j2k_read_poc_v2 (
756 OPJ_BYTE * p_header_data,
757 OPJ_UINT32 p_header_size,
758 struct opj_event_mgr * p_manager
762 Read the CRG marker (component registration)
763 @param j2k J2K handle
765 static void j2k_read_crg(opj_j2k_t *j2k);
767 * Reads a CRG marker (Component registration)
769 * @param p_header_data the data contained in the TLM box.
770 * @param p_j2k the jpeg2000 codec.
771 * @param p_header_size the size of the data contained in the TLM marker.
772 * @param p_manager the user event manager.
774 static opj_bool j2k_read_crg_v2 (
776 OPJ_BYTE * p_header_data,
777 OPJ_UINT32 p_header_size,
778 struct opj_event_mgr * p_manager
781 Read the TLM marker (tile-part lengths)
782 @param j2k J2K handle
784 static void j2k_read_tlm(opj_j2k_t *j2k);
786 * Reads a TLM marker (Tile Length Marker)
788 * @param p_header_data the data contained in the TLM box.
789 * @param p_j2k the jpeg2000 codec.
790 * @param p_header_size the size of the data contained in the TLM marker.
791 * @param p_manager the user event manager.
793 static opj_bool j2k_read_tlm_v2 (
795 OPJ_BYTE * p_header_data,
796 OPJ_UINT32 p_header_size,
797 struct opj_event_mgr * p_manager
801 * Writes the updated tlm.
803 * @param p_stream the stream to write data to.
804 * @param p_j2k J2K codec.
805 * @param p_manager the user event manager.
807 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
808 struct opj_stream_private *p_stream,
809 struct opj_event_mgr * p_manager );
812 Read the PLM marker (packet length, main header)
813 @param j2k J2K handle
815 static void j2k_read_plm(opj_j2k_t *j2k);
818 * Reads a PLM marker (Packet length, main header marker)
820 * @param p_header_data the data contained in the TLM box.
821 * @param p_j2k the jpeg2000 codec.
822 * @param p_header_size the size of the data contained in the TLM marker.
823 * @param p_manager the user event manager.
825 static opj_bool j2k_read_plm_v2 (
827 OPJ_BYTE * p_header_data,
828 OPJ_UINT32 p_header_size,
829 struct opj_event_mgr * p_manager
832 Read the PLT marker (packet length, tile-part header)
833 @param j2k J2K handle
835 static void j2k_read_plt(opj_j2k_t *j2k);
837 * Reads a PLT marker (Packet length, tile-part header)
839 * @param p_header_data the data contained in the PLT box.
840 * @param p_j2k the jpeg2000 codec.
841 * @param p_header_size the size of the data contained in the PLT marker.
842 * @param p_manager the user event manager.
844 static opj_bool j2k_read_plt_v2 (
846 OPJ_BYTE * p_header_data,
847 OPJ_UINT32 p_header_size,
848 struct opj_event_mgr * p_manager
851 Read the PPM marker (packet packet headers, main header)
852 @param j2k J2K handle
854 static void j2k_read_ppm(opj_j2k_t *j2k);
856 * Reads a PPM marker (Packed packet headers, main header)
858 * @param p_header_data the data contained in the POC box.
859 * @param p_j2k the jpeg2000 codec.
860 * @param p_header_size the size of the data contained in the POC marker.
861 * @param p_manager the user event manager.
863 static opj_bool j2k_read_ppm_v2 (
865 OPJ_BYTE * p_header_data,
866 OPJ_UINT32 p_header_size,
867 struct opj_event_mgr * p_manager
870 static opj_bool j2k_read_ppm_v3 (
872 OPJ_BYTE * p_header_data,
873 OPJ_UINT32 p_header_size,
874 struct opj_event_mgr * p_manager
878 Read the PPT marker (packet packet headers, tile-part header)
879 @param j2k J2K handle
881 static void j2k_read_ppt(opj_j2k_t *j2k);
883 * Reads a PPT marker (Packed packet headers, tile-part header)
885 * @param p_header_data the data contained in the PPT box.
886 * @param p_j2k the jpeg2000 codec.
887 * @param p_header_size the size of the data contained in the PPT marker.
888 * @param p_manager the user event manager.
890 static opj_bool j2k_read_ppt_v2 (
892 OPJ_BYTE * p_header_data,
893 OPJ_UINT32 p_header_size,
894 struct opj_event_mgr * p_manager
897 Write the TLM marker (Mainheader)
898 @param j2k J2K handle
900 static void j2k_write_tlm(opj_j2k_t *j2k);
902 Write the SOT marker (start of tile-part)
903 @param j2k J2K handle
905 static void j2k_write_sot(opj_j2k_t *j2k);
907 Read the SOT marker (start of tile-part)
908 @param j2k J2K handle
910 static void j2k_read_sot(opj_j2k_t *j2k);
913 * Writes the SOT marker (Start of tile-part)
915 * @param p_stream the stream to write data to.
916 * @param p_j2k J2K codec.
917 * @param p_manager the user event manager.
919 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
921 OPJ_UINT32 * p_data_written,
922 const struct opj_stream_private *p_stream,
923 struct opj_event_mgr * p_manager );
926 * Reads a PPT marker (Packed packet headers, tile-part header)
928 * @param p_header_data the data contained in the PPT box.
929 * @param p_j2k the jpeg2000 codec.
930 * @param p_header_size the size of the data contained in the PPT marker.
931 * @param p_manager the user event manager.
933 static opj_bool j2k_read_sot_v2 (
935 OPJ_BYTE * p_header_data,
936 OPJ_UINT32 p_header_size,
937 struct opj_event_mgr * p_manager
940 Write the SOD marker (start of data)
941 @param j2k J2K handle
942 @param tile_coder Pointer to a TCD handle
944 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
947 * Writes the SOD marker (Start of data)
949 * @param p_stream the stream to write data to.
950 * @param p_j2k J2K codec.
951 * @param p_manager the user event manager.
953 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
954 struct opj_tcd_v2 * p_tile_coder,
956 OPJ_UINT32 * p_data_written,
957 OPJ_UINT32 p_total_data_size,
958 const struct opj_stream_private *p_stream,
959 struct opj_event_mgr * p_manager );
962 Read the SOD marker (start of data)
963 @param j2k J2K handle
965 static void j2k_read_sod(opj_j2k_t *j2k);
968 * Reads a SOD marker (Start Of Data)
970 * @param p_header_data the data contained in the SOD box.
971 * @param p_j2k the jpeg2000 codec.
972 * @param p_header_size the size of the data contained in the SOD marker.
973 * @param p_manager the user event manager.
975 static opj_bool j2k_read_sod_v2 (
977 struct opj_stream_private *p_stream,
978 struct opj_event_mgr * p_manager
982 * Updates the Tile Length Marker.
984 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
986 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
987 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
989 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
990 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
994 Write the RGN marker (region-of-interest)
995 @param j2k J2K handle
996 @param compno Number of the component concerned by the information written
997 @param tileno Number of the tile concerned by the information written
999 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
1002 * Writes the RGN marker (Region Of Interest)
1004 * @param p_tile_no the tile to output
1005 * @param p_comp_no the component to output
1006 * @param p_stream the stream to write data to.
1007 * @param p_j2k J2K codec.
1008 * @param p_manager the user event manager.
1010 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
1011 OPJ_UINT32 p_tile_no,
1012 OPJ_UINT32 p_comp_no,
1013 struct opj_stream_private *p_stream,
1014 struct opj_event_mgr * p_manager );
1017 Read the RGN marker (region-of-interest)
1018 @param j2k J2K handle
1020 static void j2k_read_rgn(opj_j2k_t *j2k);
1023 * Reads a RGN marker (Region Of Interest)
1025 * @param p_header_data the data contained in the POC box.
1026 * @param p_j2k the jpeg2000 codec.
1027 * @param p_header_size the size of the data contained in the POC marker.
1028 * @param p_manager the user event manager.
1030 static opj_bool j2k_read_rgn_v2 (
1031 opj_j2k_v2_t *p_j2k,
1032 OPJ_BYTE * p_header_data,
1033 OPJ_UINT32 p_header_size,
1034 struct opj_event_mgr * p_manager
1038 Write the EOC marker (end of codestream)
1039 @param j2k J2K handle
1041 static void j2k_write_eoc(opj_j2k_t *j2k);
1043 Read the EOC marker (end of codestream)
1044 @param j2k J2K handle
1046 static void j2k_read_eoc(opj_j2k_t *j2k);
1049 * Reads a EOC marker (End Of Codestream)
1051 * @param p_header_data the data contained in the SOD box.
1052 * @param p_j2k the jpeg2000 codec.
1053 * @param p_header_size the size of the data contained in the SOD marker.
1054 * @param p_manager the user event manager.
1056 static opj_bool j2k_read_eoc_v2 (
1057 opj_j2k_v2_t *p_j2k,
1058 struct opj_stream_private *p_stream,
1059 struct opj_event_mgr * p_manager
1065 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1067 * @param p_stream the stream to write data to.
1068 * @param p_j2k J2K codec.
1069 * @param p_manager the user event manager.
1071 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1072 struct opj_stream_private *p_stream,
1073 struct opj_event_mgr * p_manager );
1078 * @param p_stream the stream to write data to.
1079 * @param p_j2k J2K codec.
1080 * @param p_manager the user event manager.
1082 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1083 struct opj_stream_private *p_stream,
1084 struct opj_event_mgr * p_manager );
1087 Read an unknown marker
1088 @param j2k J2K handle
1090 static void j2k_read_unk(opj_j2k_t *j2k);
1092 Add main header marker information
1093 @param cstr_info Codestream information structure
1094 @param type marker type
1095 @param pos byte offset of marker segment
1096 @param len length of marker segment
1098 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1100 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1102 Add tile header marker information
1103 @param tileno tile index number
1104 @param cstr_info Codestream information structure
1105 @param type marker type
1106 @param pos byte offset of marker segment
1107 @param len length of marker segment
1109 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1111 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
1114 * Reads an unknown marker
1116 * @param p_stream the stream object to read from.
1117 * @param p_j2k the jpeg2000 codec.
1118 * @param p_manager the user event manager.
1120 * @return true if the marker could be deduced.
1122 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1123 struct opj_stream_private *p_stream,
1124 OPJ_UINT32 *output_marker,
1125 struct opj_event_mgr * p_manager );
1128 * Writes the MCT marker (Multiple Component Transform)
1130 * @param p_stream the stream to write data to.
1131 * @param p_j2k J2K codec.
1132 * @param p_manager the user event manager.
1134 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1135 opj_mct_data_t * p_mct_record,
1136 struct opj_stream_private *p_stream,
1137 struct opj_event_mgr * p_manager );
1140 * Reads a MCT marker (Multiple Component Transform)
1142 * @param p_header_data the data contained in the MCT box.
1143 * @param p_j2k the jpeg2000 codec.
1144 * @param p_header_size the size of the data contained in the MCT marker.
1145 * @param p_manager the user event manager.
1147 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1148 OPJ_BYTE * p_header_data,
1149 OPJ_UINT32 p_header_size,
1150 struct opj_event_mgr * p_manager );
1153 * Writes the MCC marker (Multiple Component Collection)
1155 * @param p_stream the stream to write data to.
1156 * @param p_j2k J2K codec.
1157 * @param p_manager the user event manager.
1159 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1160 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1161 struct opj_stream_private *p_stream,
1162 struct opj_event_mgr * p_manager );
1166 * Reads a MCC marker (Multiple Component Collection)
1168 * @param p_header_data the data contained in the MCC box.
1169 * @param p_j2k the jpeg2000 codec.
1170 * @param p_header_size the size of the data contained in the MCC marker.
1171 * @param p_manager the user event manager.
1173 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1174 OPJ_BYTE * p_header_data,
1175 OPJ_UINT32 p_header_size,
1176 struct opj_event_mgr * p_manager );
1179 * Writes the MCO marker (Multiple component transformation ordering)
1181 * @param p_stream the stream to write data to.
1182 * @param p_j2k J2K codec.
1183 * @param p_manager the user event manager.
1185 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1186 struct opj_stream_private *p_stream,
1187 struct opj_event_mgr * p_manager );
1190 * Reads a MCO marker (Multiple Component Transform Ordering)
1192 * @param p_header_data the data contained in the MCO box.
1193 * @param p_j2k the jpeg2000 codec.
1194 * @param p_header_size the size of the data contained in the MCO marker.
1195 * @param p_manager the user event manager.
1197 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1198 OPJ_BYTE * p_header_data,
1199 OPJ_UINT32 p_header_size,
1200 struct opj_event_mgr * p_manager );
1202 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1204 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1205 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1206 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1207 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1209 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1210 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1211 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1212 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1214 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1215 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1216 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1217 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1221 * Ends the encoding, i.e. frees memory.
1223 * @param p_stream the stream to write data to.
1224 * @param p_j2k J2K codec.
1225 * @param p_manager the user event manager.
1227 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1228 struct opj_stream_private *p_stream,
1229 struct opj_event_mgr * p_manager );
1232 * Writes the CBD marker (Component bit depth definition)
1234 * @param p_stream the stream to write data to.
1235 * @param p_j2k J2K codec.
1236 * @param p_manager the user event manager.
1238 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1239 struct opj_stream_private *p_stream,
1240 struct opj_event_mgr * p_manager );
1243 * Reads a CBD marker (Component bit depth definition)
1244 * @param p_header_data the data contained in the CBD box.
1245 * @param p_j2k the jpeg2000 codec.
1246 * @param p_header_size the size of the data contained in the CBD marker.
1247 * @param p_manager the user event manager.
1249 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1250 OPJ_BYTE * p_header_data,
1251 OPJ_UINT32 p_header_size,
1252 struct opj_event_mgr * p_manager);
1255 * Writes the image components.
1257 * @param p_stream the stream to write data to.
1258 * @param p_j2k J2K codec.
1259 * @param p_manager the user event manager.
1261 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1262 struct opj_stream_private *p_stream,
1263 struct opj_event_mgr * p_manager );
1266 * Writes regions of interests.
1268 * @param p_stream the stream to write data to.
1269 * @param p_j2k J2K codec.
1270 * @param p_manager the user event manager.
1272 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1273 struct opj_stream_private *p_stream,
1274 struct opj_event_mgr * p_manager );
1279 * @param p_stream the stream to write data to.
1280 * @param p_j2k J2K codec.
1281 * @param p_manager the user event manager.
1283 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1284 struct opj_stream_private *p_stream,
1285 struct opj_event_mgr * p_manager );
1288 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1290 * @param cp the coding parameters.
1291 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1292 * @param tileno the given tile.
1294 * @return the number of tile parts.
1296 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1299 * Calculates the total number of tile parts needed by the encoder to
1300 * encode such an image. If not enough memory is available, then the function return false.
1302 * @param p_nb_tiles pointer that will hold the number of tile parts.
1303 * @param cp the coding parameters for the image.
1304 * @param image the image to encode.
1305 * @param p_j2k the p_j2k encoder.
1306 * @param p_manager the user event manager.
1308 * @return true if the function was successful, false else.
1310 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1312 OPJ_UINT32 * p_nb_tiles,
1314 opj_event_mgr_t * p_manager);
1316 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1318 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1320 static opj_codestream_index_t* j2k_create_cstr_index(void);
1326 /* ----------------------------------------------------------------------- */
1327 typedef struct j2k_prog_order{
1328 OPJ_PROG_ORDER enum_prog;
1332 j2k_prog_order_t j2k_prog_order_list[] = {
1338 {(OPJ_PROG_ORDER)-1, ""}
1346 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1354 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1356 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1358 j2k_read_int16_to_float,
1359 j2k_read_int32_to_float,
1360 j2k_read_float32_to_float,
1361 j2k_read_float64_to_float
1364 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1366 j2k_read_int16_to_int32,
1367 j2k_read_int32_to_int32,
1368 j2k_read_float32_to_int32,
1369 j2k_read_float64_to_int32
1372 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1374 j2k_write_float_to_int16,
1375 j2k_write_float_to_int32,
1376 j2k_write_float_to_float,
1377 j2k_write_float_to_float64
1380 typedef struct opj_dec_memory_marker_handler
1384 /** value of the state when the marker can appear */
1386 /** action linked to the marker */
1387 opj_bool (*handler) (
1388 opj_j2k_v2_t *p_j2k,
1389 OPJ_BYTE * p_header_data,
1390 OPJ_UINT32 p_header_size,
1391 struct opj_event_mgr * p_manager
1394 opj_dec_memory_marker_handler_t;
1396 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1399 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1400 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1401 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1402 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1403 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1404 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1405 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1406 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1407 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1408 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1409 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1410 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1411 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1413 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1414 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1415 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1416 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1417 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1418 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1420 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
1421 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1422 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1423 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1424 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1425 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1426 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1427 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
1428 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1429 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1430 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1431 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1432 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1434 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1435 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1436 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1437 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1438 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1439 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1441 #ifdef TODO_MS /* FIXME */
1442 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1443 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1444 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1445 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1447 #endif /* USE_JPWL */
1449 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1450 {J2K_MS_INSEC, 0, j2k_read_insec}
1451 #endif /* USE_JPSEC */
1452 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1457 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1459 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1460 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1464 for (i=0;i<p_nb_elem;++i) {
1465 opj_read_bytes(l_src_data,&l_temp,2);
1467 l_src_data+=sizeof(OPJ_INT16);
1469 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1473 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1475 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1476 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1480 for (i=0;i<p_nb_elem;++i) {
1481 opj_read_bytes(l_src_data,&l_temp,4);
1483 l_src_data+=sizeof(OPJ_INT32);
1485 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1489 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1491 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1492 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1496 for (i=0;i<p_nb_elem;++i) {
1497 opj_read_float(l_src_data,&l_temp);
1499 l_src_data+=sizeof(OPJ_FLOAT32);
1501 *(l_dest_data++) = l_temp;
1505 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1507 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1508 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1512 for (i=0;i<p_nb_elem;++i) {
1513 opj_read_double(l_src_data,&l_temp);
1515 l_src_data+=sizeof(OPJ_FLOAT64);
1517 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1521 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1523 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1524 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1528 for (i=0;i<p_nb_elem;++i) {
1529 opj_read_bytes(l_src_data,&l_temp,2);
1531 l_src_data+=sizeof(OPJ_INT16);
1533 *(l_dest_data++) = (OPJ_INT32) l_temp;
1537 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1539 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1540 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1544 for (i=0;i<p_nb_elem;++i) {
1545 opj_read_bytes(l_src_data,&l_temp,4);
1547 l_src_data+=sizeof(OPJ_INT32);
1549 *(l_dest_data++) = (OPJ_INT32) l_temp;
1553 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1555 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1556 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1560 for (i=0;i<p_nb_elem;++i) {
1561 opj_read_float(l_src_data,&l_temp);
1563 l_src_data+=sizeof(OPJ_FLOAT32);
1565 *(l_dest_data++) = (OPJ_INT32) l_temp;
1569 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1571 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1572 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1576 for (i=0;i<p_nb_elem;++i) {
1577 opj_read_double(l_src_data,&l_temp);
1579 l_src_data+=sizeof(OPJ_FLOAT64);
1581 *(l_dest_data++) = (OPJ_INT32) l_temp;
1585 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1587 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1588 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1592 for (i=0;i<p_nb_elem;++i) {
1593 l_temp = (OPJ_UINT32) *(l_src_data++);
1595 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1597 l_dest_data+=sizeof(OPJ_INT16);
1601 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1603 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1604 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1608 for (i=0;i<p_nb_elem;++i) {
1609 l_temp = (OPJ_UINT32) *(l_src_data++);
1611 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1613 l_dest_data+=sizeof(OPJ_INT32);
1617 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1619 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1620 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1624 for (i=0;i<p_nb_elem;++i) {
1625 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1627 opj_write_float(l_dest_data,l_temp);
1629 l_dest_data+=sizeof(OPJ_FLOAT32);
1633 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1635 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1636 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1640 for (i=0;i<p_nb_elem;++i) {
1641 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1643 opj_write_double(l_dest_data,l_temp);
1645 l_dest_data+=sizeof(OPJ_FLOAT64);
1651 * Converts an enum type progression order to string type.
1653 * @param prg_order the progression order to get.
1655 * @return the string representation of the given progression order.
1657 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1658 j2k_prog_order_t *po;
1659 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1660 if(po->enum_prog == prg_order){
1661 return po->str_prog;
1664 return po->str_prog;
1667 /* ----------------------------------------------------------------------- */
1668 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1671 int tpnum=1,tpend=0;
1672 opj_tcp_t *tcp = &cp->tcps[tileno];
1673 prog = j2k_convert_progression_order(tcp->prg);
1678 if( cp->tp_flag == prog[i] ){
1679 tpend=1;cp->tp_pos=i;
1683 tpnum= tpnum * tcp->pocs[pino].compE;
1686 tpnum= tpnum * tcp->pocs[pino].resE;
1689 tpnum= tpnum * tcp->pocs[pino].prcE;
1692 tpnum= tpnum * tcp->pocs[pino].layE;
1704 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1706 * @param cp the coding parameters.
1707 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1708 * @param tileno the given tile.
1710 * @return the number of tile parts.
1712 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1714 const OPJ_CHAR *prog = 00;
1716 OPJ_UINT32 tpnum = 1;
1717 opj_tcp_v2_t *tcp = 00;
1718 opj_poc_t * l_current_poc = 00;
1721 assert(tileno < (cp->tw * cp->th));
1722 assert(pino < (cp->tcps[tileno].numpocs + 1));
1724 /* get the given tile coding parameter */
1725 tcp = &cp->tcps[tileno];
1728 l_current_poc = &(tcp->pocs[pino]);
1729 assert(l_current_poc != 0);
1731 /* get the progression order as a character string */
1732 prog = j2k_convert_progression_order(tcp->prg);
1733 assert(strlen(prog) > 0);
1735 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1739 /* component wise */
1741 tpnum *= l_current_poc->compE;
1743 /* resolution wise */
1745 tpnum *= l_current_poc->resE;
1749 tpnum *= l_current_poc->prcE;
1753 tpnum *= l_current_poc->layE;
1756 /* whould we split here ? */
1757 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1758 cp->m_specific_param.m_enc.m_tp_pos=i;
1770 /** mem allocation for TLM marker*/
1771 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1772 int pino,tileno,totnum_tp=0;
1774 OPJ_ARG_NOT_USED(img_numcomp);
1776 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1777 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1778 int cur_totnum_tp = 0;
1779 opj_tcp_t *tcp = &cp->tcps[tileno];
1780 for(pino = 0; pino <= tcp->numpocs; pino++) {
1782 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1783 if(!pi) { return -1;}
1784 tp_num = j2k_get_num_tp(cp,pino,tileno);
1785 totnum_tp = totnum_tp + tp_num;
1786 cur_totnum_tp = cur_totnum_tp + tp_num;
1787 pi_destroy(pi, cp, tileno);
1789 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1791 if (j2k->cstr_info) {
1792 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1793 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1801 * Calculates the total number of tile parts needed by the encoder to
1802 * encode such an image. If not enough memory is available, then the function return false.
1804 * @param p_nb_tiles pointer that will hold the number of tile parts.
1805 * @param cp the coding parameters for the image.
1806 * @param image the image to encode.
1807 * @param p_j2k the p_j2k encoder.
1808 * @param p_manager the user event manager.
1810 * @return true if the function was successful, false else.
1812 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1814 OPJ_UINT32 * p_nb_tiles,
1816 opj_event_mgr_t * p_manager)
1818 OPJ_UINT32 pino,tileno;
1819 OPJ_UINT32 l_nb_tiles;
1823 assert(p_nb_tiles != 00);
1825 assert(image != 00);
1826 assert(p_j2k != 00);
1827 assert(p_manager != 00);
1829 l_nb_tiles = cp->tw * cp->th;
1834 /* TODO mergeV2: check this part which use cstr_info */
1835 /*if (p_j2k->cstr_info) {
1836 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1838 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1839 OPJ_UINT32 cur_totnum_tp = 0;
1841 pi_update_encoding_parameters(image,cp,tileno);
1843 for (pino = 0; pino <= tcp->numpocs; ++pino)
1845 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1847 *p_nb_tiles = *p_nb_tiles + tp_num;
1849 cur_totnum_tp += tp_num;
1852 tcp->m_nb_tile_parts = cur_totnum_tp;
1854 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1855 if (l_info_tile_ptr->tp == 00) {
1859 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1861 l_info_tile_ptr->num_tps = cur_totnum_tp;
1868 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1869 OPJ_UINT32 cur_totnum_tp = 0;
1871 pi_update_encoding_parameters(image,cp,tileno);
1873 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1874 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1876 *p_nb_tiles = *p_nb_tiles + tp_num;
1878 cur_totnum_tp += tp_num;
1880 tcp->m_nb_tile_parts = cur_totnum_tp;
1889 static void j2k_write_soc(opj_j2k_t *j2k) {
1890 opj_cio_t *cio = j2k->cio;
1891 cio_write(cio, J2K_MS_SOC, 2);
1894 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1899 /* update markers struct */
1900 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1901 #endif /* USE_JPWL */
1906 * Writes the SOC marker (Start Of Codestream)
1908 * @param p_stream the stream to write data to.
1909 * @param p_j2k J2K codec.
1910 * @param p_manager the user event manager.
1912 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
1913 struct opj_stream_private *p_stream,
1914 struct opj_event_mgr * p_manager )
1916 /* 2 bytes will be written */
1917 OPJ_BYTE * l_start_stream = 00;
1920 assert(p_stream != 00);
1921 assert(p_j2k != 00);
1922 assert(p_manager != 00);
1924 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1926 /* write SOC identifier */
1927 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1929 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1935 /* update markers struct */
1937 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1939 assert( 0 && "TODO" );
1940 #endif /* USE_JPWL */
1946 static void j2k_read_soc(opj_j2k_t *j2k) {
1947 j2k->state = J2K_STATE_MHSIZ;
1949 if (j2k->cstr_info) {
1950 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
1951 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
1956 * Reads a SOC marker (Start of Codestream)
1957 * @param p_header_data the data contained in the SOC box.
1958 * @param jp2 the jpeg2000 file codec.
1959 * @param p_header_size the size of the data contained in the SOC marker.
1960 * @param p_manager the user event manager.
1962 static opj_bool j2k_read_soc_v2( opj_j2k_v2_t *p_j2k,
1963 struct opj_stream_private *p_stream,
1964 struct opj_event_mgr * p_manager )
1966 OPJ_BYTE l_data [2];
1967 OPJ_UINT32 l_marker;
1970 assert(p_j2k != 00);
1971 assert(p_manager != 00);
1972 assert(p_stream != 00);
1974 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1978 opj_read_bytes(l_data,&l_marker,2);
1979 if (l_marker != J2K_MS_SOC) {
1983 /* Next marker should be a SIZ marker in the main header */
1984 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1986 /* FIXME move it in a index structure included in p_j2k*/
1987 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1989 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1991 /* Add the marker to the codestream index*/
1992 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1997 static void j2k_write_siz(opj_j2k_t *j2k) {
2001 opj_cio_t *cio = j2k->cio;
2002 opj_image_t *image = j2k->image;
2003 opj_cp_t *cp = j2k->cp;
2005 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2006 lenp = cio_tell(cio);
2008 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2009 cio_write(cio, image->x1, 4); /* Xsiz */
2010 cio_write(cio, image->y1, 4); /* Ysiz */
2011 cio_write(cio, image->x0, 4); /* X0siz */
2012 cio_write(cio, image->y0, 4); /* Y0siz */
2013 cio_write(cio, cp->tdx, 4); /* XTsiz */
2014 cio_write(cio, cp->tdy, 4); /* YTsiz */
2015 cio_write(cio, cp->tx0, 4); /* XT0siz */
2016 cio_write(cio, cp->ty0, 4); /* YT0siz */
2017 cio_write(cio, image->numcomps, 2); /* Csiz */
2018 for (i = 0; i < image->numcomps; i++) {
2019 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2020 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2021 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2023 len = cio_tell(cio) - lenp;
2024 cio_seek(cio, lenp);
2025 cio_write(cio, len, 2); /* Lsiz */
2026 cio_seek(cio, lenp + len);
2029 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2033 * Writes the SIZ marker (image and tile size)
2035 * @param p_stream the stream to write data to.
2036 * @param p_j2k J2K codec.
2037 * @param p_manager the user event manager.
2039 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2040 struct opj_stream_private *p_stream,
2041 struct opj_event_mgr * p_manager )
2044 OPJ_UINT32 l_size_len;
2045 OPJ_BYTE * l_current_ptr;
2046 opj_image_t * l_image = 00;
2047 opj_cp_v2_t *cp = 00;
2048 opj_image_comp_t * l_img_comp = 00;
2051 assert(p_stream != 00);
2052 assert(p_j2k != 00);
2053 assert(p_manager != 00);
2055 l_image = p_j2k->m_private_image;
2056 cp = &(p_j2k->m_cp);
2057 l_size_len = 40 + 3 * l_image->numcomps;
2058 l_img_comp = l_image->comps;
2060 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2062 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2063 = (OPJ_BYTE*)opj_realloc(
2064 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2066 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2070 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2073 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2075 /* write SOC identifier */
2076 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2079 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2082 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2085 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2088 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2091 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2094 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2097 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2100 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2103 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2106 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2109 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2112 for (i = 0; i < l_image->numcomps; ++i) {
2113 // TODO here with MCT ?
2114 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2117 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2120 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2126 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_size_len,p_manager) != l_size_len) {
2133 static void j2k_read_siz(opj_j2k_t *j2k) {
2136 opj_cio_t *cio = j2k->cio;
2137 opj_image_t *image = j2k->image;
2138 opj_cp_t *cp = j2k->cp;
2140 len = cio_read(cio, 2); /* Lsiz */
2141 cio_read(cio, 2); /* Rsiz (capabilities) */
2142 image->x1 = cio_read(cio, 4); /* Xsiz */
2143 image->y1 = cio_read(cio, 4); /* Ysiz */
2144 image->x0 = cio_read(cio, 4); /* X0siz */
2145 image->y0 = cio_read(cio, 4); /* Y0siz */
2146 cp->tdx = cio_read(cio, 4); /* XTsiz */
2147 cp->tdy = cio_read(cio, 4); /* YTsiz */
2148 cp->tx0 = cio_read(cio, 4); /* XT0siz */
2149 cp->ty0 = cio_read(cio, 4); /* YT0siz */
2151 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
2152 opj_event_msg(j2k->cinfo, EVT_ERROR,
2153 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
2154 image->x0,image->x1,image->y0,image->y1);
2158 image->numcomps = cio_read(cio, 2); /* Csiz */
2161 if (j2k->cp->correct) {
2162 /* if JPWL is on, we check whether TX errors have damaged
2163 too much the SIZ parameters */
2164 if (!(image->x1 * image->y1)) {
2165 opj_event_msg(j2k->cinfo, EVT_ERROR,
2166 "JPWL: bad image size (%d x %d)\n",
2167 image->x1, image->y1);
2168 if (!JPWL_ASSUME || JPWL_ASSUME) {
2169 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2173 if (image->numcomps != ((len - 38) / 3)) {
2174 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2175 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2176 image->numcomps, ((len - 38) / 3));
2178 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2181 /* we try to correct */
2182 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
2183 if (image->numcomps < ((len - 38) / 3)) {
2184 len = 38 + 3 * image->numcomps;
2185 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2188 image->numcomps = ((len - 38) / 3);
2189 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2194 /* update components number in the jpwl_exp_comps filed */
2195 cp->exp_comps = image->numcomps;
2197 #endif /* USE_JPWL */
2199 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
2200 for (i = 0; i < image->numcomps; i++) {
2202 tmp = cio_read(cio, 1); /* Ssiz_i */
2203 image->comps[i].prec = (tmp & 0x7f) + 1;
2204 image->comps[i].sgnd = tmp >> 7;
2205 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
2206 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
2209 if (j2k->cp->correct) {
2210 /* if JPWL is on, we check whether TX errors have damaged
2211 too much the SIZ parameters, again */
2212 if (!(image->comps[i].dx * image->comps[i].dy)) {
2213 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2214 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2215 i, i, image->comps[i].dx, image->comps[i].dy);
2217 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2220 /* we try to correct */
2221 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2222 if (!image->comps[i].dx) {
2223 image->comps[i].dx = 1;
2224 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2225 i, image->comps[i].dx);
2227 if (!image->comps[i].dy) {
2228 image->comps[i].dy = 1;
2229 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2230 i, image->comps[i].dy);
2235 #endif /* USE_JPWL */
2237 /* TODO: unused ? */
2238 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
2239 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
2241 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
2242 image->comps[i].factor = cp->reduce; /* reducing factor per component */
2245 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2246 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2249 if (j2k->cp->correct) {
2250 /* if JPWL is on, we check whether TX errors have damaged
2251 too much the SIZ parameters */
2252 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
2253 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2254 "JPWL: bad number of tiles (%d x %d)\n",
2257 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2260 /* we try to correct */
2261 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2264 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2267 if (cp->tw > cp->max_tiles) {
2269 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
2270 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2271 cp->max_tiles, cp->tw);
2275 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2278 if (cp->th > cp->max_tiles) {
2280 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2281 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2282 cp->max_tiles, cp->th);
2286 #endif /* USE_JPWL */
2288 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2289 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
2290 cp->tileno_size = 0;
2293 if (j2k->cp->correct) {
2295 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2296 "JPWL: could not alloc tcps field of cp\n");
2297 if (!JPWL_ASSUME || JPWL_ASSUME) {
2298 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2303 #endif /* USE_JPWL */
2305 for (i = 0; i < cp->tw * cp->th; i++) {
2306 cp->tcps[i].POC = 0;
2307 cp->tcps[i].numpocs = 0;
2308 cp->tcps[i].first = 1;
2311 /* Initialization for PPM marker */
2313 cp->ppm_data = NULL;
2314 cp->ppm_data_first = NULL;
2315 cp->ppm_previous = 0;
2318 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2319 for (i = 0; i < cp->tw * cp->th; i++) {
2320 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2322 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
2323 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
2324 j2k->state = J2K_STATE_MH;
2327 if (j2k->cstr_info) {
2328 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2329 cstr_info->image_w = image->x1 - image->x0;
2330 cstr_info->image_h = image->y1 - image->y0;
2331 cstr_info->numcomps = image->numcomps;
2332 cstr_info->tw = cp->tw;
2333 cstr_info->th = cp->th;
2334 cstr_info->tile_x = cp->tdx;
2335 cstr_info->tile_y = cp->tdy;
2336 cstr_info->tile_Ox = cp->tx0;
2337 cstr_info->tile_Oy = cp->ty0;
2338 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
2344 * Reads a SIZ marker (image and tile size)
2345 * @param p_header_data the data contained in the SIZ box.
2346 * @param jp2 the jpeg2000 file codec.
2347 * @param p_header_size the size of the data contained in the SIZ marker.
2348 * @param p_manager the user event manager.
2350 opj_bool j2k_read_siz_v2 (
2351 opj_j2k_v2_t *p_j2k,
2352 OPJ_BYTE * p_header_data,
2353 OPJ_UINT32 p_header_size,
2354 struct opj_event_mgr * p_manager
2357 OPJ_UINT32 l_size, i;
2358 OPJ_UINT32 l_nb_comp;
2359 OPJ_UINT32 l_nb_comp_remain;
2360 OPJ_UINT32 l_remaining_size;
2361 OPJ_UINT32 l_nb_tiles;
2363 opj_image_t *l_image = 00;
2364 opj_cp_v2_t *l_cp = 00;
2365 opj_image_comp_t * l_img_comp = 00;
2366 opj_tcp_v2_t * l_current_tile_param = 00;
2369 assert(p_j2k != 00);
2370 assert(p_manager != 00);
2371 assert(p_header_data != 00);
2373 l_image = p_j2k->m_private_image;
2374 l_cp = &(p_j2k->m_cp);
2376 /* minimum size == 39 - 3 (= minimum component parameter) */
2377 if (p_header_size < 36) {
2378 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2382 l_remaining_size = p_header_size - 36;
2383 l_nb_comp = l_remaining_size / 3;
2384 l_nb_comp_remain = l_remaining_size % 3;
2385 if (l_nb_comp_remain != 0){
2386 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2390 l_size = p_header_size + 2; /* Lsiz */
2392 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2394 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2395 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2397 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2399 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2401 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2403 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2405 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2407 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2409 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2411 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2414 l_image->numcomps = (OPJ_UINT16) l_tmp;
2416 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2420 if (l_image->numcomps != l_nb_comp) {
2421 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n", l_image->numcomps, l_nb_comp);
2426 if (l_cp->correct) {
2427 /* if JPWL is on, we check whether TX errors have damaged
2428 too much the SIZ parameters */
2429 if (!(l_image->x1 * l_image->y1)) {
2430 opj_event_msg_v2(p_manager, EVT_ERROR,
2431 "JPWL: bad image size (%d x %d)\n",
2432 l_image->x1, l_image->y1);
2433 if (!JPWL_ASSUME || JPWL_ASSUME) {
2434 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2439 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2440 if (l_image->numcomps != ((len - 38) / 3)) {
2441 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2442 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2443 l_image->numcomps, ((len - 38) / 3));
2445 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2448 */ /* we try to correct */
2449 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2450 if (l_image->numcomps < ((len - 38) / 3)) {
2451 len = 38 + 3 * l_image->numcomps;
2452 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2455 l_image->numcomps = ((len - 38) / 3);
2456 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2462 /* update components number in the jpwl_exp_comps filed */
2463 l_cp->exp_comps = l_image->numcomps;
2465 #endif /* USE_JPWL */
2467 /* Allocate the resulting image components */
2468 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2469 if (l_image->comps == 00){
2470 l_image->numcomps = 0;
2471 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2475 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2476 l_img_comp = l_image->comps;
2478 /* Read the component information */
2479 for (i = 0; i < l_image->numcomps; ++i){
2481 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2483 l_img_comp->prec = (tmp & 0x7f) + 1;
2484 l_img_comp->sgnd = tmp >> 7;
2485 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2487 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2488 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2490 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2493 if (l_cp->correct) {
2494 /* if JPWL is on, we check whether TX errors have damaged
2495 too much the SIZ parameters, again */
2496 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2497 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2498 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2499 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2501 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2504 /* we try to correct */
2505 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2506 if (!l_image->comps[i].dx) {
2507 l_image->comps[i].dx = 1;
2508 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2509 i, l_image->comps[i].dx);
2511 if (!l_image->comps[i].dy) {
2512 l_image->comps[i].dy = 1;
2513 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2514 i, l_image->comps[i].dy);
2518 #endif /* USE_JPWL */
2519 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2520 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2524 /* Compute the number of tiles */
2525 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2526 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2527 l_nb_tiles = l_cp->tw * l_cp->th;
2529 /* Define the tiles which will be decoded */
2530 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2531 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx;
2532 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy;
2533 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), l_cp->tdx);
2534 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), l_cp->tdy);
2537 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2538 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2539 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2540 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2544 if (l_cp->correct) {
2545 /* if JPWL is on, we check whether TX errors have damaged
2546 too much the SIZ parameters */
2547 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2548 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2549 "JPWL: bad number of tiles (%d x %d)\n",
2550 l_cp->tw, l_cp->th);
2552 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2555 /* we try to correct */
2556 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2559 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2562 if (l_cp->tw > l_cp->max_tiles) {
2564 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2565 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2566 l_cp->max_tiles, l_cp->tw);
2570 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2573 if (l_cp->th > l_cp->max_tiles) {
2575 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2576 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2577 l_cp->max_tiles, l_cp->th);
2581 #endif /* USE_JPWL */
2583 /* memory allocations */
2584 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2585 if (l_cp->tcps == 00) {
2586 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2589 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2592 if (l_cp->correct) {
2594 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2595 "JPWL: could not alloc tcps field of cp\n");
2596 if (!JPWL_ASSUME || JPWL_ASSUME) {
2597 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2602 #endif /* USE_JPWL */
2604 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2605 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2606 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2607 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2610 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2612 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2613 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2615 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2616 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2619 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2620 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2622 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2623 (opj_simple_mcc_decorrelation_data_t*)
2624 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2626 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2627 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2630 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records,0,J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2631 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2633 /* set up default dc level shift */
2634 for (i=0;i<l_image->numcomps;++i) {
2635 if (! l_image->comps[i].sgnd) {
2636 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2640 l_current_tile_param = l_cp->tcps;
2641 for (i = 0; i < l_nb_tiles; ++i) {
2642 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2643 if (l_current_tile_param->tccps == 00) {
2644 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2647 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2649 ++l_current_tile_param;
2652 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2653 opj_image_comp_header_update(l_image,l_cp);
2660 static void j2k_write_com(opj_j2k_t *j2k) {
2664 if(j2k->cp->comment) {
2665 opj_cio_t *cio = j2k->cio;
2666 char *comment = j2k->cp->comment;
2668 cio_write(cio, J2K_MS_COM, 2);
2669 lenp = cio_tell(cio);
2671 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2672 for (i = 0; i < strlen(comment); i++) {
2673 cio_write(cio, comment[i], 1);
2675 len = cio_tell(cio) - lenp;
2676 cio_seek(cio, lenp);
2677 cio_write(cio, len, 2);
2678 cio_seek(cio, lenp + len);
2682 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2687 static void j2k_read_com(opj_j2k_t *j2k) {
2690 opj_cio_t *cio = j2k->cio;
2692 len = cio_read(cio, 2);
2693 cio_skip(cio, len - 2);
2696 * Reads a COM marker (comments)
2697 * @param p_header_data the data contained in the COM box.
2698 * @param jp2 the jpeg2000 file codec.
2699 * @param p_header_size the size of the data contained in the COM marker.
2700 * @param p_manager the user event manager.
2702 opj_bool j2k_read_com_v2 (
2703 opj_j2k_v2_t *p_j2k,
2704 OPJ_BYTE * p_header_data,
2705 OPJ_UINT32 p_header_size,
2706 struct opj_event_mgr * p_manager
2710 assert(p_j2k != 00);
2711 assert(p_manager != 00);
2712 assert(p_header_data != 00);
2717 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2720 opj_cp_t *cp = j2k->cp;
2721 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2722 opj_tccp_t *tccp = &tcp->tccps[compno];
2723 opj_cio_t *cio = j2k->cio;
2725 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2726 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2727 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2728 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2729 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2731 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2732 for (i = 0; i < tccp->numresolutions; i++) {
2733 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2738 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2741 opj_cp_t *cp = j2k->cp;
2742 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2743 opj_tccp_t *tccp = &tcp->tccps[compno];
2744 opj_cio_t *cio = j2k->cio;
2746 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2748 /* If user wants to remove more resolutions than the codestream contains, return error*/
2749 if (cp->reduce >= tccp->numresolutions) {
2750 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2751 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2752 j2k->state |= J2K_STATE_ERR;
2755 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2756 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2757 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2758 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2759 if (tccp->csty & J2K_CP_CSTY_PRT) {
2760 for (i = 0; i < tccp->numresolutions; i++) {
2761 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2762 tccp->prcw[i] = tmp & 0xf;
2763 tccp->prch[i] = tmp >> 4;
2768 if(j2k->cstr_info && compno == 0) {
2769 for (i = 0; i < tccp->numresolutions; i++) {
2770 if (tccp->csty & J2K_CP_CSTY_PRT) {
2771 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2772 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2775 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2776 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2783 static void j2k_write_cod(opj_j2k_t *j2k) {
2784 opj_cp_t *cp = NULL;
2785 opj_tcp_t *tcp = NULL;
2788 opj_cio_t *cio = j2k->cio;
2790 cio_write(cio, J2K_MS_COD, 2); /* COD */
2792 lenp = cio_tell(cio);
2796 tcp = &cp->tcps[j2k->curtileno];
2798 cio_write(cio, tcp->csty, 1); /* Scod */
2799 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2800 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2801 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2803 j2k_write_cox(j2k, 0);
2804 len = cio_tell(cio) - lenp;
2805 cio_seek(cio, lenp);
2806 cio_write(cio, len, 2); /* Lcod */
2807 cio_seek(cio, lenp + len);
2810 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2815 * Writes the COD marker (Coding style default)
2817 * @param p_stream the stream to write data to.
2818 * @param p_j2k J2K codec.
2819 * @param p_manager the user event manager.
2821 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2822 struct opj_stream_private *p_stream,
2823 struct opj_event_mgr * p_manager )
2825 opj_cp_v2_t *l_cp = 00;
2826 opj_tcp_v2_t *l_tcp = 00;
2827 OPJ_UINT32 l_code_size,l_remaining_size;
2828 OPJ_BYTE * l_current_data = 00;
2831 assert(p_j2k != 00);
2832 assert(p_manager != 00);
2833 assert(p_stream != 00);
2835 l_cp = &(p_j2k->m_cp);
2836 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2837 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2838 l_remaining_size = l_code_size;
2840 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2841 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2842 = (OPJ_BYTE*)opj_realloc(
2843 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2846 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2850 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2853 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2855 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2856 l_current_data += 2;
2858 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2859 l_current_data += 2;
2861 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2864 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2867 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2870 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2873 l_remaining_size -= 9;
2875 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2876 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2880 if (l_remaining_size != 0) {
2881 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2885 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_code_size,p_manager) != l_code_size) {
2892 static void j2k_read_cod(opj_j2k_t *j2k) {
2895 opj_cio_t *cio = j2k->cio;
2896 opj_cp_t *cp = j2k->cp;
2897 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2898 opj_image_t *image = j2k->image;
2900 len = cio_read(cio, 2); /* Lcod */
2901 tcp->csty = cio_read(cio, 1); /* Scod */
2902 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
2903 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
2904 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
2906 pos = cio_tell(cio);
2907 for (i = 0; i < image->numcomps; i++) {
2908 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
2910 j2k_read_cox(j2k, i);
2914 if (j2k->cstr_info) {
2915 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2916 cstr_info->prog = tcp->prg;
2917 cstr_info->numlayers = tcp->numlayers;
2918 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
2919 for (i = 0; i < image->numcomps; i++) {
2920 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
2926 * Reads a COD marker (Coding Styke defaults)
2927 * @param p_header_data the data contained in the COD box.
2928 * @param p_j2k the jpeg2000 codec.
2929 * @param p_header_size the size of the data contained in the COD marker.
2930 * @param p_manager the user event manager.
2932 opj_bool j2k_read_cod_v2 (
2933 opj_j2k_v2_t *p_j2k,
2934 OPJ_BYTE * p_header_data,
2935 OPJ_UINT32 p_header_size,
2936 struct opj_event_mgr * p_manager
2942 opj_cp_v2_t *l_cp = 00;
2943 opj_tcp_v2_t *l_tcp = 00;
2944 opj_image_t *l_image = 00;
2947 assert(p_header_data != 00);
2948 assert(p_j2k != 00);
2949 assert(p_manager != 00);
2951 l_image = p_j2k->m_private_image;
2952 l_cp = &(p_j2k->m_cp);
2954 /* If we are in the first tile-part header of the current tile */
2955 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2956 &l_cp->tcps[p_j2k->m_current_tile_number] :
2957 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2959 /* Make sure room is sufficient */
2960 if (p_header_size < 5) {
2961 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2965 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2967 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2969 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2970 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2973 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2974 if (l_cp->m_specific_param.m_dec.m_layer) {
2975 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2978 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2981 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2985 for (i = 0; i < l_image->numcomps; ++i) {
2986 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2989 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2990 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2994 if (p_header_size != 0) {
2995 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2999 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
3000 j2k_copy_tile_component_parameters(p_j2k);
3003 #ifdef WIP_REMOVE_MSD
3004 if (p_j2k->cstr_info) {
3005 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
3006 p_j2k->cstr_info->prog = l_tcp->prg;
3007 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
3008 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
3009 for (i = 0; i < l_image->numcomps; ++i) {
3010 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
3018 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
3021 opj_cp_t *cp = j2k->cp;
3022 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3023 opj_image_t *image = j2k->image;
3024 opj_cio_t *cio = j2k->cio;
3026 cio_write(cio, J2K_MS_COC, 2); /* COC */
3027 lenp = cio_tell(cio);
3029 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3030 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
3031 j2k_write_cox(j2k, compno);
3032 len = cio_tell(cio) - lenp;
3033 cio_seek(cio, lenp);
3034 cio_write(cio, len, 2); /* Lcoc */
3035 cio_seek(cio, lenp + len);
3039 * Writes the COC marker (Coding style component)
3041 * @param p_comp_no the index of the component to output.
3042 * @param p_stream the stream to write data to.
3043 * @param p_j2k J2K codec.
3044 * @param p_manager the user event manager.
3046 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
3047 OPJ_UINT32 p_comp_no,
3048 struct opj_stream_private *p_stream,
3049 struct opj_event_mgr * p_manager )
3051 OPJ_UINT32 l_coc_size,l_remaining_size;
3052 OPJ_UINT32 l_comp_room;
3055 assert(p_j2k != 00);
3056 assert(p_manager != 00);
3057 assert(p_stream != 00);
3059 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3061 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3063 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3064 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3065 = (OPJ_BYTE*)opj_realloc(
3066 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3068 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3072 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3075 j2k_write_coc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);
3077 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_coc_size,p_manager) != l_coc_size) {
3085 * Writes the COC marker (Coding style component)
3087 * @param p_comp_no the index of the component to output.
3088 * @param p_stream the stream to write data to.
3089 * @param p_j2k J2K codec.
3090 * @param p_manager the user event manager.
3092 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3093 OPJ_UINT32 p_comp_no,
3095 OPJ_UINT32 * p_data_written,
3096 struct opj_event_mgr * p_manager )
3098 opj_cp_v2_t *l_cp = 00;
3099 opj_tcp_v2_t *l_tcp = 00;
3100 OPJ_UINT32 l_coc_size,l_remaining_size;
3101 OPJ_BYTE * l_current_data = 00;
3102 opj_image_t *l_image = 00;
3103 OPJ_UINT32 l_comp_room;
3106 assert(p_j2k != 00);
3107 assert(p_manager != 00);
3109 l_cp = &(p_j2k->m_cp);
3110 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3111 l_image = p_j2k->m_private_image;
3112 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3114 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3115 l_remaining_size = l_coc_size;
3117 l_current_data = p_data;
3119 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3120 l_current_data += 2;
3122 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3123 l_current_data += 2;
3125 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3126 l_current_data+=l_comp_room;
3128 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3131 l_remaining_size -= (5 + l_comp_room);
3132 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3133 * p_data_written = l_coc_size;
3137 * Gets the maximum size taken by a coc.
3139 * @param p_j2k the jpeg2000 codec to use.
3141 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3144 OPJ_UINT32 l_nb_comp;
3145 OPJ_UINT32 l_nb_tiles;
3146 OPJ_UINT32 l_max = 0;
3150 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3151 l_nb_comp = p_j2k->m_private_image->numcomps;
3153 for (i=0;i<l_nb_tiles;++i) {
3154 for (j=0;j<l_nb_comp;++j) {
3155 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3162 static void j2k_read_coc(opj_j2k_t *j2k) {
3165 opj_cp_t *cp = j2k->cp;
3166 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3167 opj_image_t *image = j2k->image;
3168 opj_cio_t *cio = j2k->cio;
3170 len = cio_read(cio, 2); /* Lcoc */
3171 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3172 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
3173 j2k_read_cox(j2k, compno);
3177 * Reads a COC marker (Coding Style Component)
3178 * @param p_header_data the data contained in the COC box.
3179 * @param p_j2k the jpeg2000 codec.
3180 * @param p_header_size the size of the data contained in the COC marker.
3181 * @param p_manager the user event manager.
3183 opj_bool j2k_read_coc_v2 (
3184 opj_j2k_v2_t *p_j2k,
3185 OPJ_BYTE * p_header_data,
3186 OPJ_UINT32 p_header_size,
3187 struct opj_event_mgr * p_manager
3190 opj_cp_v2_t *l_cp = NULL;
3191 opj_tcp_v2_t *l_tcp = NULL;
3192 opj_image_t *l_image = NULL;
3193 OPJ_UINT32 l_comp_room;
3194 OPJ_UINT32 l_comp_no;
3197 assert(p_header_data != 00);
3198 assert(p_j2k != 00);
3199 assert(p_manager != 00);
3201 l_cp = &(p_j2k->m_cp);
3202 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3203 &l_cp->tcps[p_j2k->m_current_tile_number] :
3204 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3205 l_image = p_j2k->m_private_image;
3207 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3209 /* make sure room is sufficient*/
3210 if (p_header_size < l_comp_room + 1) {
3211 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3214 p_header_size -= l_comp_room + 1;
3216 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3217 p_header_data += l_comp_room;
3218 if (l_comp_no >= l_image->numcomps) {
3219 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3223 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3226 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3227 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3231 if (p_header_size != 0) {
3232 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3238 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3239 int bandno, numbands;
3242 opj_cp_t *cp = j2k->cp;
3243 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3244 opj_tccp_t *tccp = &tcp->tccps[compno];
3245 opj_cio_t *cio = j2k->cio;
3247 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3248 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3250 for (bandno = 0; bandno < numbands; bandno++) {
3251 expn = tccp->stepsizes[bandno].expn;
3252 mant = tccp->stepsizes[bandno].mant;
3254 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3255 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3257 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3262 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3264 int bandno, numbands;
3266 opj_cp_t *cp = j2k->cp;
3267 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3268 opj_tccp_t *tccp = &tcp->tccps[compno];
3269 opj_cio_t *cio = j2k->cio;
3271 tmp = cio_read(cio, 1); /* Sqcx */
3272 tccp->qntsty = tmp & 0x1f;
3273 tccp->numgbits = tmp >> 5;
3274 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3275 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3278 if (j2k->cp->correct) {
3280 /* if JPWL is on, we check whether there are too many subbands */
3281 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3282 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3283 "JPWL: bad number of subbands in Sqcx (%d)\n",
3286 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3289 /* we try to correct */
3291 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3292 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3299 /* We check whether there are too many subbands */
3300 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3301 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3302 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3303 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3306 #endif /* USE_JPWL */
3308 for (bandno = 0; bandno < numbands; bandno++) {
3310 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3311 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3314 tmp = cio_read(cio, 2); /* SPqcx_i */
3318 if (bandno < J2K_MAXBANDS){
3319 tccp->stepsizes[bandno].expn = expn;
3320 tccp->stepsizes[bandno].mant = mant;
3324 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3325 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3326 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3327 tccp->stepsizes[bandno].expn =
3328 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3329 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3330 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3336 static void j2k_write_qcd(opj_j2k_t *j2k) {
3339 opj_cio_t *cio = j2k->cio;
3341 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3342 lenp = cio_tell(cio);
3344 j2k_write_qcx(j2k, 0);
3345 len = cio_tell(cio) - lenp;
3346 cio_seek(cio, lenp);
3347 cio_write(cio, len, 2); /* Lqcd */
3348 cio_seek(cio, lenp + len);
3351 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3355 * Writes the QCD marker (quantization default)
3357 * @param p_comp_number the index of the component to output.
3358 * @param p_stream the stream to write data to.
3359 * @param p_j2k J2K codec.
3360 * @param p_manager the user event manager.
3362 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3363 struct opj_stream_private *p_stream,
3364 struct opj_event_mgr * p_manager )
3366 opj_cp_v2_t *l_cp = 00;
3367 opj_tcp_v2_t *l_tcp = 00;
3368 OPJ_UINT32 l_qcd_size,l_remaining_size;
3369 OPJ_BYTE * l_current_data = 00;
3372 assert(p_j2k != 00);
3373 assert(p_manager != 00);
3374 assert(p_stream != 00);
3376 l_cp = &(p_j2k->m_cp);
3377 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3378 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3379 l_remaining_size = l_qcd_size;
3381 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3382 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3383 = (OPJ_BYTE*)opj_realloc(
3384 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3387 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3391 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3394 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3396 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3397 l_current_data += 2;
3399 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3400 l_current_data += 2;
3402 l_remaining_size -= 4;
3404 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3405 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3409 if (l_remaining_size != 0) {
3410 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3414 if (opj_stream_write_data(p_stream, p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcd_size,p_manager) != l_qcd_size) {
3422 static void j2k_read_qcd(opj_j2k_t *j2k) {
3425 opj_cio_t *cio = j2k->cio;
3426 opj_image_t *image = j2k->image;
3428 len = cio_read(cio, 2); /* Lqcd */
3429 pos = cio_tell(cio);
3430 for (i = 0; i < image->numcomps; i++) {
3432 j2k_read_qcx(j2k, i, len - 2);
3437 * Reads a QCD marker (Quantization defaults)
3438 * @param p_header_data the data contained in the QCD box.
3439 * @param p_j2k the jpeg2000 codec.
3440 * @param p_header_size the size of the data contained in the QCD marker.
3441 * @param p_manager the user event manager.
3443 opj_bool j2k_read_qcd_v2 (
3444 opj_j2k_v2_t *p_j2k,
3445 OPJ_BYTE * p_header_data,
3446 OPJ_UINT32 p_header_size,
3447 struct opj_event_mgr * p_manager
3451 assert(p_header_data != 00);
3452 assert(p_j2k != 00);
3453 assert(p_manager != 00);
3455 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3456 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3460 if (p_header_size != 0) {
3461 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3465 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3466 j2k_copy_tile_quantization_parameters(p_j2k);
3471 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3474 opj_cio_t *cio = j2k->cio;
3476 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3477 lenp = cio_tell(cio);
3479 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3480 j2k_write_qcx(j2k, compno);
3481 len = cio_tell(cio) - lenp;
3482 cio_seek(cio, lenp);
3483 cio_write(cio, len, 2); /* Lqcc */
3484 cio_seek(cio, lenp + len);
3488 * Writes the QCC marker (quantization component)
3490 * @param p_comp_no the index of the component to output.
3491 * @param p_stream the stream to write data to.
3492 * @param p_j2k J2K codec.
3493 * @param p_manager the user event manager.
3495 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3496 OPJ_UINT32 p_comp_no,
3497 struct opj_stream_private *p_stream,
3498 struct opj_event_mgr * p_manager )
3500 OPJ_UINT32 l_qcc_size,l_remaining_size;
3503 assert(p_j2k != 00);
3504 assert(p_manager != 00);
3505 assert(p_stream != 00);
3507 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3508 l_remaining_size = l_qcc_size;
3510 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3511 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3512 = (OPJ_BYTE*)opj_realloc(
3513 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3515 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3519 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3522 j2k_write_qcc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);
3524 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcc_size,p_manager) != l_qcc_size) {
3532 * Writes the QCC marker (quantization component)
3534 * @param p_comp_no the index of the component to output.
3535 * @param p_stream the stream to write data to.
3536 * @param p_j2k J2K codec.
3537 * @param p_manager the user event manager.
3539 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3540 OPJ_UINT32 p_comp_no,
3542 OPJ_UINT32 * p_data_written,
3543 struct opj_event_mgr * p_manager )
3545 OPJ_UINT32 l_qcc_size,l_remaining_size;
3546 OPJ_BYTE * l_current_data = 00;
3549 assert(p_j2k != 00);
3550 assert(p_manager != 00);
3552 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3553 l_remaining_size = l_qcc_size;
3555 l_current_data = p_data;
3557 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3558 l_current_data += 2;
3560 if (p_j2k->m_private_image->numcomps <= 256) {
3563 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3564 l_current_data += 2;
3566 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3569 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3570 l_remaining_size -= 6;
3573 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3574 l_current_data += 2;
3576 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3579 l_remaining_size -= 6;
3582 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3584 *p_data_written = l_qcc_size;
3588 * Gets the maximum size taken by a qcc.
3590 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3592 return j2k_get_max_coc_size(p_j2k);
3595 static void j2k_read_qcc(opj_j2k_t *j2k) {
3597 int numcomp = j2k->image->numcomps;
3598 opj_cio_t *cio = j2k->cio;
3600 len = cio_read(cio, 2); /* Lqcc */
3601 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3604 if (j2k->cp->correct) {
3606 static int backup_compno = 0;
3608 /* compno is negative or larger than the number of components!!! */
3609 if ((compno < 0) || (compno >= numcomp)) {
3610 opj_event_msg(j2k->cinfo, EVT_ERROR,
3611 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3614 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3617 /* we try to correct */
3618 compno = backup_compno % numcomp;
3619 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3620 "- setting component number to %d\n",
3624 /* keep your private count of tiles */
3627 #endif /* USE_JPWL */
3629 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3633 * Reads a QCC marker (Quantization component)
3634 * @param p_header_data the data contained in the QCC box.
3635 * @param p_j2k the jpeg2000 codec.
3636 * @param p_header_size the size of the data contained in the QCC marker.
3637 * @param p_manager the user event manager.
3639 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3640 OPJ_BYTE * p_header_data,
3641 OPJ_UINT32 p_header_size,
3642 struct opj_event_mgr * p_manager)
3644 OPJ_UINT32 l_num_comp,l_comp_no;
3647 assert(p_header_data != 00);
3648 assert(p_j2k != 00);
3649 assert(p_manager != 00);
3651 l_num_comp = p_j2k->m_private_image->numcomps;
3653 if (l_num_comp <= 256) {
3654 if (p_header_size < 1) {
3655 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3658 opj_read_bytes(p_header_data,&l_comp_no,1);
3663 if (p_header_size < 2) {
3664 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3667 opj_read_bytes(p_header_data,&l_comp_no,2);
3673 if (p_j2k->m_cp.correct) {
3675 static OPJ_UINT32 backup_compno = 0;
3677 /* compno is negative or larger than the number of components!!! */
3678 if ((l_comp_no < 0) || (l_comp_no >= l_num_comp)) {
3679 opj_event_msg_v2(p_manager, EVT_ERROR,
3680 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3681 l_comp_no, l_num_comp);
3683 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3686 /* we try to correct */
3687 l_comp_no = backup_compno % l_num_comp;
3688 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3689 "- setting component number to %d\n",
3693 /* keep your private count of tiles */
3696 #endif /* USE_JPWL */
3698 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3699 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3703 if (p_header_size != 0) {
3704 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3712 static void j2k_write_poc(opj_j2k_t *j2k) {
3713 int len, numpchgs, i;
3715 int numcomps = j2k->image->numcomps;
3717 opj_cp_t *cp = j2k->cp;
3718 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3719 opj_tccp_t *tccp = &tcp->tccps[0];
3720 opj_cio_t *cio = j2k->cio;
3722 numpchgs = 1 + tcp->numpocs;
3723 cio_write(cio, J2K_MS_POC, 2); /* POC */
3724 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3725 cio_write(cio, len, 2); /* Lpoc */
3726 for (i = 0; i < numpchgs; i++) {
3727 opj_poc_t *poc = &tcp->pocs[i];
3728 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3729 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3730 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3731 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3732 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3733 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3734 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3735 poc->compno1 = int_min(poc->compno1, numcomps);
3736 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3741 * Writes the POC marker (Progression Order Change)
3743 * @param p_stream the stream to write data to.
3744 * @param p_j2k J2K codec.
3745 * @param p_manager the user event manager.
3747 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3749 OPJ_UINT32 * p_data_written,
3750 struct opj_event_mgr * p_manager )
3753 OPJ_BYTE * l_current_data = 00;
3754 OPJ_UINT32 l_nb_comp;
3755 OPJ_UINT32 l_nb_poc;
3756 OPJ_UINT32 l_poc_size;
3757 opj_image_t *l_image = 00;
3758 opj_tcp_v2_t *l_tcp = 00;
3759 opj_tccp_t *l_tccp = 00;
3760 opj_poc_t *l_current_poc = 00;
3761 OPJ_UINT32 l_poc_room;
3764 assert(p_j2k != 00);
3765 assert(p_manager != 00);
3767 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3768 l_tccp = &l_tcp->tccps[0];
3769 l_image = p_j2k->m_private_image;
3770 l_nb_comp = l_image->numcomps;
3771 l_nb_poc = 1 + l_tcp->numpocs;
3773 if (l_nb_comp <= 256) {
3780 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3782 l_current_data = p_data;
3784 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3785 l_current_data += 2;
3787 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3788 l_current_data += 2;
3790 l_current_poc = l_tcp->pocs;
3791 for (i = 0; i < l_nb_poc; ++i) {
3792 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3795 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3796 l_current_data+=l_poc_room;
3798 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3801 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3804 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3805 l_current_data+=l_poc_room;
3807 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3810 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3811 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3812 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3813 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3818 *p_data_written = l_poc_size;
3822 * Gets the maximum size taken by the writing of a POC.
3824 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3826 opj_tcp_v2_t * l_tcp = 00;
3827 OPJ_UINT32 l_nb_tiles = 0;
3828 OPJ_UINT32 l_max_poc = 0;
3831 l_tcp = p_j2k->m_cp.tcps;
3832 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3834 for (i=0;i<l_nb_tiles;++i) {
3835 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3841 return 4 + 9 * l_max_poc;
3845 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3847 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3850 OPJ_UINT32 l_nb_tiles;
3851 OPJ_UINT32 l_max = 0;
3852 opj_tcp_v2_t * l_tcp = 00;
3854 l_tcp = p_j2k->m_cp.tcps;
3855 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3857 for (i=0;i<l_nb_tiles;++i) {
3858 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3868 * Gets the maximum size taken by the headers of the SOT.
3870 * @param p_j2k the jpeg2000 codec to use.
3872 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3874 OPJ_UINT32 l_nb_bytes = 0;
3875 OPJ_UINT32 l_nb_comps;
3876 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3878 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3879 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3881 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3882 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3883 l_nb_bytes += l_nb_comps * l_coc_bytes;
3885 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3886 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3889 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3891 /*** DEVELOPER CORNER, Add room for your headers ***/
3896 static void j2k_read_poc(opj_j2k_t *j2k) {
3897 int len, numpchgs, i, old_poc;
3899 int numcomps = j2k->image->numcomps;
3901 opj_cp_t *cp = j2k->cp;
3902 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3903 opj_cio_t *cio = j2k->cio;
3905 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
3907 len = cio_read(cio, 2); /* Lpoc */
3908 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
3910 for (i = old_poc; i < numpchgs + old_poc; i++) {
3912 poc = &tcp->pocs[i];
3913 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
3914 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
3915 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
3916 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
3917 poc->compno1 = int_min(
3918 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
3919 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
3922 tcp->numpocs = numpchgs + old_poc - 1;
3926 * Reads a POC marker (Progression Order Change)
3928 * @param p_header_data the data contained in the POC box.
3929 * @param p_j2k the jpeg2000 codec.
3930 * @param p_header_size the size of the data contained in the POC marker.
3931 * @param p_manager the user event manager.
3933 opj_bool j2k_read_poc_v2 (
3934 opj_j2k_v2_t *p_j2k,
3935 OPJ_BYTE * p_header_data,
3936 OPJ_UINT32 p_header_size,
3937 struct opj_event_mgr * p_manager)
3939 OPJ_UINT32 i, l_nb_comp, l_tmp;
3940 opj_image_t * l_image = 00;
3941 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3942 OPJ_UINT32 l_chunk_size, l_comp_room;
3944 opj_cp_v2_t *l_cp = 00;
3945 opj_tcp_v2_t *l_tcp = 00;
3946 opj_poc_t *l_current_poc = 00;
3949 assert(p_header_data != 00);
3950 assert(p_j2k != 00);
3951 assert(p_manager != 00);
3953 l_image = p_j2k->m_private_image;
3954 l_nb_comp = l_image->numcomps;
3955 if (l_nb_comp <= 256) {
3961 l_chunk_size = 5 + 2 * l_comp_room;
3962 l_current_poc_nb = p_header_size / l_chunk_size;
3963 l_current_poc_remaining = p_header_size % l_chunk_size;
3965 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3966 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3970 l_cp = &(p_j2k->m_cp);
3971 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3972 &l_cp->tcps[p_j2k->m_current_tile_number] :
3973 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3974 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3975 l_current_poc_nb += l_old_poc_nb;
3977 assert(l_current_poc_nb < 32);
3979 /* now poc is in use.*/
3982 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3983 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3984 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3986 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3987 p_header_data+=l_comp_room;
3988 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3990 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3992 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3993 p_header_data+=l_comp_room;
3994 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3996 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3997 /* make sure comp is in acceptable bounds */
3998 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4002 l_tcp->numpocs = l_current_poc_nb - 1;
4006 static void j2k_read_crg(opj_j2k_t *j2k) {
4007 int len, i, Xcrg_i, Ycrg_i;
4009 opj_cio_t *cio = j2k->cio;
4010 int numcomps = j2k->image->numcomps;
4012 len = cio_read(cio, 2); /* Lcrg */
4013 for (i = 0; i < numcomps; i++) {
4014 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
4015 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4020 * Reads a CRG marker (Component registration)
4022 * @param p_header_data the data contained in the TLM box.
4023 * @param p_j2k the jpeg2000 codec.
4024 * @param p_header_size the size of the data contained in the TLM marker.
4025 * @param p_manager the user event manager.
4027 opj_bool j2k_read_crg_v2 (
4028 opj_j2k_v2_t *p_j2k,
4029 OPJ_BYTE * p_header_data,
4030 OPJ_UINT32 p_header_size,
4031 struct opj_event_mgr * p_manager
4034 OPJ_UINT32 l_nb_comp;
4036 assert(p_header_data != 00);
4037 assert(p_j2k != 00);
4038 assert(p_manager != 00);
4040 l_nb_comp = p_j2k->m_private_image->numcomps;
4042 if (p_header_size != l_nb_comp *4) {
4043 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4046 /* Do not care of this at the moment since only local variables are set here */
4049 (i = 0; i < l_nb_comp; ++i)
4051 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4053 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4060 static void j2k_read_tlm(opj_j2k_t *j2k) {
4061 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4062 long int Ttlm_i, Ptlm_i;
4064 opj_cio_t *cio = j2k->cio;
4066 len = cio_read(cio, 2); /* Ltlm */
4067 Ztlm = cio_read(cio, 1); /* Ztlm */
4068 Stlm = cio_read(cio, 1); /* Stlm */
4069 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4070 SP = (Stlm >> 6) & 0x01;
4071 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4072 for (i = 0; i < tile_tlm; i++) {
4073 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4074 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4079 * Reads a TLM marker (Tile Length Marker)
4081 * @param p_header_data the data contained in the TLM box.
4082 * @param p_j2k the jpeg2000 codec.
4083 * @param p_header_size the size of the data contained in the TLM marker.
4084 * @param p_manager the user event manager.
4086 opj_bool j2k_read_tlm_v2 (
4087 opj_j2k_v2_t *p_j2k,
4088 OPJ_BYTE * p_header_data,
4089 OPJ_UINT32 p_header_size,
4090 struct opj_event_mgr * p_manager
4093 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4095 assert(p_header_data != 00);
4096 assert(p_j2k != 00);
4097 assert(p_manager != 00);
4099 if (p_header_size < 2) {
4100 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4105 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4107 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4110 l_ST = ((l_Stlm >> 4) & 0x3);
4111 l_SP = (l_Stlm >> 6) & 0x1;
4113 l_Ptlm_size = (l_SP + 1) * 2;
4114 l_quotient = l_Ptlm_size + l_ST;
4116 l_tot_num_tp = p_header_size / l_quotient;
4117 l_tot_num_tp_remaining = p_header_size % l_quotient;
4119 if (l_tot_num_tp_remaining != 0) {
4120 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4123 /* FIXME Do not care of this at the moment since only local variables are set here */
4126 (i = 0; i < l_tot_num_tp; ++i)
4128 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4129 p_header_data += l_ST;
4130 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4131 p_header_data += l_Ptlm_size;
4136 static void j2k_read_plm(opj_j2k_t *j2k) {
4137 int len, i, Zplm, Nplm, add, packet_len = 0;
4139 opj_cio_t *cio = j2k->cio;
4141 len = cio_read(cio, 2); /* Lplm */
4142 Zplm = cio_read(cio, 1); /* Zplm */
4145 Nplm = cio_read(cio, 4); /* Nplm */
4147 for (i = Nplm; i > 0; i--) {
4148 add = cio_read(cio, 1);
4150 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4151 if ((add & 0x80) == 0) {
4162 * Reads a PLM marker (Packet length, main header marker)
4164 * @param p_header_data the data contained in the TLM box.
4165 * @param p_j2k the jpeg2000 codec.
4166 * @param p_header_size the size of the data contained in the TLM marker.
4167 * @param p_manager the user event manager.
4169 opj_bool j2k_read_plm_v2 (
4170 opj_j2k_v2_t *p_j2k,
4171 OPJ_BYTE * p_header_data,
4172 OPJ_UINT32 p_header_size,
4173 struct opj_event_mgr * p_manager
4177 assert(p_header_data != 00);
4178 assert(p_j2k != 00);
4179 assert(p_manager != 00);
4181 if (p_header_size < 1) {
4182 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4185 /* Do not care of this at the moment since only local variables are set here */
4187 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4194 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4196 p_header_size -= (1+l_Nplm);
4200 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4204 (i = 0; i < l_Nplm; ++i)
4206 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4208 // take only the last seven bytes
4209 l_packet_len |= (l_tmp & 0x7f);
4217 // store packet length and proceed to next packet
4224 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4232 static void j2k_read_plt(opj_j2k_t *j2k) {
4233 int len, i, Zplt, packet_len = 0, add;
4235 opj_cio_t *cio = j2k->cio;
4237 len = cio_read(cio, 2); /* Lplt */
4238 Zplt = cio_read(cio, 1); /* Zplt */
4239 for (i = len - 3; i > 0; i--) {
4240 add = cio_read(cio, 1);
4241 packet_len = (packet_len << 7) + add; /* Iplt_i */
4242 if ((add & 0x80) == 0) {
4250 * Reads a PLT marker (Packet length, tile-part header)
4252 * @param p_header_data the data contained in the PLT box.
4253 * @param p_j2k the jpeg2000 codec.
4254 * @param p_header_size the size of the data contained in the PLT marker.
4255 * @param p_manager the user event manager.
4257 opj_bool j2k_read_plt_v2 (
4258 opj_j2k_v2_t *p_j2k,
4259 OPJ_BYTE * p_header_data,
4260 OPJ_UINT32 p_header_size,
4261 struct opj_event_mgr * p_manager
4264 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4267 assert(p_header_data != 00);
4268 assert(p_j2k != 00);
4269 assert(p_manager != 00);
4271 if (p_header_size < 1) {
4272 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4276 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4280 for (i = 0; i < p_header_size; ++i) {
4281 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4283 /* take only the last seven bytes */
4284 l_packet_len |= (l_tmp & 0x7f);
4289 /* store packet length and proceed to next packet */
4294 if (l_packet_len != 0) {
4295 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4302 static void j2k_read_ppm(opj_j2k_t *j2k) {
4303 int len, Z_ppm, i, j;
4306 opj_cp_t *cp = j2k->cp;
4307 opj_cio_t *cio = j2k->cio;
4309 len = cio_read(cio, 2);
4312 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4315 if (cp->ppm_previous == 0) {
4316 N_ppm = cio_read(cio, 4); /* N_ppm */
4319 N_ppm = cp->ppm_previous;
4322 if (Z_ppm == 0) { /* First PPM marker */
4323 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4324 cp->ppm_data_first = cp->ppm_data;
4325 cp->ppm_len = N_ppm;
4326 } else { /* NON-first PPM marker */
4327 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4330 /* this memory allocation check could be done even in non-JPWL cases */
4332 if (!cp->ppm_data) {
4333 opj_event_msg(j2k->cinfo, EVT_ERROR,
4334 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4336 if (!JPWL_ASSUME || JPWL_ASSUME) {
4337 opj_free(cp->ppm_data);
4338 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4345 cp->ppm_data_first = cp->ppm_data;
4346 cp->ppm_len = N_ppm + cp->ppm_store;
4348 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4349 cp->ppm_data[j] = cio_read(cio, 1);
4353 break; /* Case of non-finished packet header in present marker but finished in next one */
4355 cp->ppm_previous = i - 1;
4360 * Reads a PPM marker (Packed packet headers, main header)
4362 * @param p_header_data the data contained in the POC box.
4363 * @param p_j2k the jpeg2000 codec.
4364 * @param p_header_size the size of the data contained in the POC marker.
4365 * @param p_manager the user event manager.
4367 opj_bool j2k_read_ppm_v2 (
4368 opj_j2k_v2_t *p_j2k,
4369 OPJ_BYTE * p_header_data,
4370 OPJ_UINT32 p_header_size,
4371 struct opj_event_mgr * p_manager
4375 opj_cp_v2_t *l_cp = 00;
4376 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4379 assert(p_header_data != 00);
4380 assert(p_j2k != 00);
4381 assert(p_manager != 00);
4383 if (p_header_size < 1) {
4384 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4388 l_cp = &(p_j2k->m_cp);
4391 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4395 /* First PPM marker */
4397 if (p_header_size < 4) {
4398 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4402 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4406 /* First PPM marker: Initialization */
4407 l_cp->ppm_len = l_N_ppm;
4408 l_cp->ppm_data_size = 0;
4410 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4411 if (l_cp->ppm_buffer == 00) {
4412 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4415 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4417 l_cp->ppm_data = l_cp->ppm_buffer;
4421 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4422 if (p_header_size >= 4) {
4424 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4427 l_cp->ppm_len += l_N_ppm ;
4429 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4430 if (l_cp->ppm_buffer == 00) {
4431 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4434 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4436 l_cp->ppm_data = l_cp->ppm_buffer;
4443 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4445 if (l_remaining_data <= p_header_size) {
4446 /* we must store less information than available in the packet */
4447 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4448 l_cp->ppm_data_size = l_cp->ppm_len;
4449 p_header_size -= l_remaining_data;
4450 p_header_data += l_remaining_data;
4453 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4454 l_cp->ppm_data_size += p_header_size;
4455 p_header_data += p_header_size;
4467 * Reads a PPM marker (Packed packet headers, main header)
4469 * @param p_header_data the data contained in the POC box.
4470 * @param p_j2k the jpeg2000 codec.
4471 * @param p_header_size the size of the data contained in the POC marker.
4472 * @param p_manager the user event manager.
4474 opj_bool j2k_read_ppm_v3 (
4475 opj_j2k_v2_t *p_j2k,
4476 OPJ_BYTE * p_header_data,
4477 OPJ_UINT32 p_header_size,
4478 struct opj_event_mgr * p_manager
4481 opj_cp_v2_t *l_cp = 00;
4482 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4485 assert(p_header_data != 00);
4486 assert(p_j2k != 00);
4487 assert(p_manager != 00);
4489 /* Minimum size of PPM marker is equal to the size of Zppm element */
4490 if (p_header_size < 1) {
4491 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4495 l_cp = &(p_j2k->m_cp);
4498 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4502 /* First PPM marker */
4504 /* We need now at least the Nppm^0 element */
4505 if (p_header_size < 4) {
4506 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4510 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4514 /* First PPM marker: Initialization */
4515 l_cp->ppm_len = l_N_ppm;
4516 l_cp->ppm_data_read = 0;
4518 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4519 if (l_cp->ppm_data == 00) {
4520 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4523 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4525 l_cp->ppm_data_current = l_cp->ppm_data;
4527 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4530 if (p_header_size < 4) {
4531 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4535 /* Uncompleted Ippm series in the previous PPM marker?*/
4536 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4537 /* Get the place where add the remaining Ippm series*/
4538 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4539 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4542 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4546 /* Increase the size of ppm_data to add the new Ippm series*/
4547 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4549 /* Keep the position of the place where concatenate the new series*/
4550 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4551 l_cp->ppm_len += l_N_ppm;
4556 l_remaining_data = p_header_size;
4558 while (l_remaining_data >= l_N_ppm) {
4559 /* read a complete Ippm series*/
4560 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4561 p_header_size -= l_N_ppm;
4562 p_header_data += l_N_ppm;
4564 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4568 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4573 l_remaining_data = p_header_size;
4577 l_remaining_data = p_header_size;
4579 /* Next Ippm series is a complete series ?*/
4580 if (l_remaining_data > l_N_ppm) {
4581 /* Increase the size of ppm_data to add the new Ippm series*/
4582 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4584 /* Keep the position of the place where concatenate the new series */
4585 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4586 l_cp->ppm_len += l_N_ppm;
4591 /* Need to read an incomplete Ippm series*/
4592 if (l_remaining_data) {
4593 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4595 /* Keep the position of the place where concatenate the new series*/
4596 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4597 l_cp->ppm_len += l_N_ppm;
4599 /* Read incomplete Ippm series*/
4600 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4601 p_header_size -= l_remaining_data;
4602 p_header_data += l_remaining_data;
4604 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4609 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4610 if (p_header_size >= 4) {
4612 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4615 l_cp->ppm_len += l_N_ppm ;
4617 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4618 if (l_cp->ppm_buffer == 00) {
4619 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4622 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4624 l_cp->ppm_data = l_cp->ppm_buffer;
4631 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4633 if (l_remaining_data <= p_header_size) {
4634 /* we must store less information than available in the packet */
4635 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4636 l_cp->ppm_data_size = l_cp->ppm_len;
4637 p_header_size -= l_remaining_data;
4638 p_header_data += l_remaining_data;
4641 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4642 l_cp->ppm_data_size += p_header_size;
4643 p_header_data += p_header_size;
4652 static void j2k_read_ppt(opj_j2k_t *j2k) {
4653 int len, Z_ppt, i, j = 0;
4655 opj_cp_t *cp = j2k->cp;
4656 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4657 opj_cio_t *cio = j2k->cio;
4659 len = cio_read(cio, 2);
4660 Z_ppt = cio_read(cio, 1);
4662 if (Z_ppt == 0) { /* First PPT marker */
4663 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4664 tcp->ppt_data_first = tcp->ppt_data;
4666 tcp->ppt_len = len - 3;
4667 } else { /* NON-first PPT marker */
4668 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4669 tcp->ppt_data_first = tcp->ppt_data;
4670 tcp->ppt_len = len - 3 + tcp->ppt_store;
4673 for (i = len - 3; i > 0; i--) {
4674 tcp->ppt_data[j] = cio_read(cio, 1);
4681 * Reads a PPT marker (Packed packet headers, tile-part header)
4683 * @param p_header_data the data contained in the PPT box.
4684 * @param p_j2k the jpeg2000 codec.
4685 * @param p_header_size the size of the data contained in the PPT marker.
4686 * @param p_manager the user event manager.
4688 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4689 OPJ_BYTE * p_header_data,
4690 OPJ_UINT32 p_header_size,
4691 struct opj_event_mgr * p_manager )
4693 opj_cp_v2_t *l_cp = 00;
4694 opj_tcp_v2_t *l_tcp = 00;
4698 assert(p_header_data != 00);
4699 assert(p_j2k != 00);
4700 assert(p_manager != 00);
4702 /* We need to have the Z_ppt element at minimum */
4703 if (p_header_size < 1) {
4704 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4708 l_cp = &(p_j2k->m_cp);
4710 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
4714 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4717 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4721 /* Allocate buffer to read the packet header */
4723 /* First PPT marker */
4724 l_tcp->ppt_data_size = 0;
4725 l_tcp->ppt_len = p_header_size;
4727 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4728 if (l_tcp->ppt_buffer == 00) {
4729 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4732 l_tcp->ppt_data = l_tcp->ppt_buffer;
4734 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4737 l_tcp->ppt_len += p_header_size;
4739 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4740 if (l_tcp->ppt_buffer == 00) {
4741 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4744 l_tcp->ppt_data = l_tcp->ppt_buffer;
4746 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4749 /* Read packet header from buffer */
4750 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4752 l_tcp->ppt_data_size += p_header_size;
4757 static void j2k_write_tlm(opj_j2k_t *j2k){
4759 opj_cio_t *cio = j2k->cio;
4760 j2k->tlm_start = cio_tell(cio);
4761 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4762 lenp = 4 + (5*j2k->totnum_tp);
4763 cio_write(cio,lenp,2); /* Ltlm */
4764 cio_write(cio, 0,1); /* Ztlm=0*/
4765 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4766 cio_skip(cio,5*j2k->totnum_tp);
4769 static void j2k_write_sot(opj_j2k_t *j2k) {
4772 opj_cio_t *cio = j2k->cio;
4774 j2k->sot_start = cio_tell(cio);
4775 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4776 lenp = cio_tell(cio);
4777 cio_skip(cio, 2); /* Lsot (further) */
4778 cio_write(cio, j2k->curtileno, 2); /* Isot */
4779 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4780 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4781 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4782 len = cio_tell(cio) - lenp;
4783 cio_seek(cio, lenp);
4784 cio_write(cio, len, 2); /* Lsot */
4785 cio_seek(cio, lenp + len);
4789 /* update markers struct */
4790 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4791 #endif /* USE_JPWL */
4794 if( j2k->cstr_info && j2k->cur_tp_num==0){
4795 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4800 * Writes the SOT marker (Start of tile-part)
4802 * @param p_stream the stream to write data to.
4803 * @param p_j2k J2K codec.
4804 * @param p_manager the user event manager.
4806 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4808 OPJ_UINT32 * p_data_written,
4809 const struct opj_stream_private *p_stream,
4810 struct opj_event_mgr * p_manager )
4813 assert(p_j2k != 00);
4814 assert(p_manager != 00);
4815 assert(p_stream != 00);
4817 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4820 opj_write_bytes(p_data,10,2); /* Lsot */
4823 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4829 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4832 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4837 /* update markers struct */
4839 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4841 assert( 0 && "TODO" );
4842 #endif /* USE_JPWL */
4844 * p_data_written = 12;
4850 static void j2k_read_sot(opj_j2k_t *j2k) {
4851 int len, tileno, totlen, partno, numparts, i;
4852 opj_tcp_t *tcp = NULL;
4855 opj_cp_t *cp = j2k->cp;
4856 opj_cio_t *cio = j2k->cio;
4858 len = cio_read(cio, 2);
4859 tileno = cio_read(cio, 2);
4862 if (j2k->cp->correct) {
4864 static int backup_tileno = 0;
4866 /* tileno is negative or larger than the number of tiles!!! */
4867 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
4868 opj_event_msg(j2k->cinfo, EVT_ERROR,
4869 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4870 tileno, (cp->tw * cp->th));
4872 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4875 /* we try to correct */
4876 tileno = backup_tileno;
4877 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
4878 "- setting tile number to %d\n",
4882 /* keep your private count of tiles */
4885 #endif /* USE_JPWL */
4887 if (cp->tileno_size == 0) {
4888 cp->tileno[cp->tileno_size] = tileno;
4892 while (i < cp->tileno_size && status == 0) {
4893 status = cp->tileno[i] == tileno ? 1 : 0;
4897 cp->tileno[cp->tileno_size] = tileno;
4902 totlen = cio_read(cio, 4);
4905 if (j2k->cp->correct) {
4907 /* totlen is negative or larger than the bytes left!!! */
4908 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
4909 opj_event_msg(j2k->cinfo, EVT_ERROR,
4910 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4911 totlen, cio_numbytesleft(cio) + 8);
4913 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4916 /* we try to correct */
4918 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
4919 "- setting Psot to %d => assuming it is the last tile\n",
4924 #endif /* USE_JPWL */
4927 totlen = cio_numbytesleft(cio) + 8;
4929 partno = cio_read(cio, 1);
4930 numparts = cio_read(cio, 1);
4932 if (partno >= numparts) {
4933 opj_event_msg(j2k->cinfo, EVT_WARNING, "SOT marker inconsistency in tile %d: tile-part index greater (%d) than number of tile-parts (%d)\n", tileno, partno, numparts);
4934 numparts = partno+1;
4937 j2k->curtileno = tileno;
4938 j2k->cur_tp_num = partno;
4939 j2k->eot = cio_getbp(cio) - 12 + totlen;
4940 j2k->state = J2K_STATE_TPH;
4941 tcp = &cp->tcps[j2k->curtileno];
4944 if (j2k->cstr_info) {
4947 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
4948 j2k->cstr_info->tile[tileno].tileno = tileno;
4949 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
4950 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4952 j2k->cstr_info->tile[tileno].end_pos += totlen;
4954 j2k->cstr_info->tile[tileno].num_tps = numparts;
4956 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
4958 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, 10 * sizeof(opj_tp_info_t)); /* Fixme (10)*/
4959 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
4960 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4961 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4964 if (tcp->first == 1) {
4965 /* Initialization PPT */
4966 opj_tccp_t *tmp = tcp->tccps;
4967 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
4969 tcp->ppt_data = NULL;
4970 tcp->ppt_data_first = NULL;
4973 for (i = 0; i < j2k->image->numcomps; i++) {
4974 tcp->tccps[i] = j2k->default_tcp->tccps[i];
4976 cp->tcps[j2k->curtileno].first = 0;
4981 * Reads a PPT marker (Packed packet headers, tile-part header)
4983 * @param p_header_data the data contained in the PPT box.
4984 * @param p_j2k the jpeg2000 codec.
4985 * @param p_header_size the size of the data contained in the PPT marker.
4986 * @param p_manager the user event manager.
4988 opj_bool j2k_read_sot_v2 (
4989 opj_j2k_v2_t *p_j2k,
4990 OPJ_BYTE * p_header_data,
4991 OPJ_UINT32 p_header_size,
4992 struct opj_event_mgr * p_manager
4996 opj_cp_v2_t *l_cp = 00;
4997 opj_tcp_v2_t *l_tcp = 00;
4998 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4999 OPJ_UINT32 l_current_part;
5000 OPJ_UINT32 l_tile_x,l_tile_y;
5003 assert(p_header_data != 00);
5004 assert(p_j2k != 00);
5005 assert(p_manager != 00);
5007 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
5008 if (p_header_size != 8) {
5009 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
5013 l_cp = &(p_j2k->m_cp);
5014 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
5017 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5018 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
5019 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
5022 if (l_cp->correct) {
5024 int tileno = p_j2k->m_current_tile_number;
5025 static int backup_tileno = 0;
5027 /* tileno is negative or larger than the number of tiles!!! */
5028 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
5029 opj_event_msg_v2(p_manager, EVT_ERROR,
5030 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5031 tileno, (l_cp->tw * l_cp->th));
5033 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5036 /* we try to correct */
5037 tileno = backup_tileno;
5038 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5039 "- setting tile number to %d\n",
5043 /* keep your private count of tiles */
5046 #endif /* USE_JPWL */
5048 /* look for the tile in the list of already processed tile (in parts). */
5049 /* Optimization possible here with a more complex data structure and with the removing of tiles */
5050 /* since the time taken by this function can only grow at the time */
5052 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
5055 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
5056 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
5058 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5064 if (l_cp->correct) {
5066 /* totlen is negative or larger than the bytes left!!! */
5067 if ((l_tot_len < 0) || (l_tot_len > p_header_size ) ) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */
5068 opj_event_msg_v2(p_manager, EVT_ERROR,
5069 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5070 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5072 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5075 /* we try to correct */
5077 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5078 "- setting Psot to %d => assuming it is the last tile\n",
5082 #endif /* USE_JPWL */
5084 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5086 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5087 "we assuming it is the last tile-part of the codestream.\n");
5088 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5091 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5094 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5097 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5098 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5099 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5100 if (l_tcp->m_nb_tile_parts) {
5101 if (l_current_part >= l_tcp->m_nb_tile_parts){
5102 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5103 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5104 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5108 l_tcp->m_nb_tile_parts = l_num_parts;
5111 /* If know the number of tile part header we will check if we didn't read the last*/
5112 if (l_tcp->m_nb_tile_parts) {
5113 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5114 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5118 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5119 /* Keep the size of data to skip after this marker */
5120 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5123 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5124 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5127 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5129 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5130 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5131 p_j2k->m_specific_param.m_decoder.m_skip_data =
5132 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5133 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5134 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5135 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5138 p_j2k->m_specific_param.m_decoder.m_skip_data =
5139 (p_j2k->m_current_tile_number != p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5142 if (p_j2k->cstr_index)
5144 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5145 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5147 if (l_num_parts != 0){
5148 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5149 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5152 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5153 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5154 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5156 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5157 (opj_tp_index_t*)opj_realloc(
5158 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5159 l_num_parts* sizeof(opj_tp_index_t));
5162 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5164 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5165 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5166 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5167 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5168 sizeof(opj_tp_index_t));
5171 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5172 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5173 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5174 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5175 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5176 * sizeof(opj_tp_index_t));
5185 /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */
5186 /* if (p_j2k->cstr_info) {
5189 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5192 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5193 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5194 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5195 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5198 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5201 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5205 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5208 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5209 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5210 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5215 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5218 opj_tcp_t *tcp = NULL;
5219 opj_codestream_info_t *cstr_info = NULL;
5221 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5222 opj_cp_t *cp = j2k->cp;
5223 opj_cio_t *cio = j2k->cio;
5225 tcd->tp_num = j2k->tp_num ;
5226 tcd->cur_tp_num = j2k->cur_tp_num;
5228 cio_write(cio, J2K_MS_SOD, 2);
5230 if( j2k->cstr_info && j2k->cur_tp_num==0){
5231 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5234 if (j2k->curtileno == 0) {
5235 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5239 cstr_info = j2k->cstr_info;
5241 if (!j2k->cur_tp_num ) {
5242 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5243 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5246 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5247 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5251 /* update markers struct */
5252 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5253 #endif /* USE_JPWL */
5258 tcp = &cp->tcps[j2k->curtileno];
5259 for (layno = 0; layno < tcp->numlayers; layno++) {
5260 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5261 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5262 } else if (tcp->rates[layno]) {
5263 tcp->rates[layno]=1;
5266 if(j2k->cur_tp_num == 0){
5267 tcd->tcd_image->tiles->packno = 0;
5269 cstr_info->packno = 0;
5272 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5274 /* Writing Psot in SOT marker */
5275 totlen = cio_tell(cio) + l - j2k->sot_start;
5276 cio_seek(cio, j2k->sot_start + 6);
5277 cio_write(cio, totlen, 4);
5278 cio_seek(cio, j2k->sot_start + totlen);
5279 /* Writing Ttlm and Ptlm in TLM marker */
5281 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5282 cio_write(cio, j2k->curtileno, 1);
5283 cio_write(cio, totlen, 4);
5285 cio_seek(cio, j2k->sot_start + totlen);
5289 * Writes the SOD marker (Start of data)
5291 * @param p_stream the stream to write data to.
5292 * @param p_j2k J2K codec.
5293 * @param p_manager the user event manager.
5295 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5296 struct opj_tcd_v2 * p_tile_coder,
5298 OPJ_UINT32 * p_data_written,
5299 OPJ_UINT32 p_total_data_size,
5300 const struct opj_stream_private *p_stream,
5301 struct opj_event_mgr * p_manager )
5303 opj_tcp_v2_t *l_tcp = 00;
5304 opj_codestream_info_t *l_cstr_info = 00;
5305 opj_cp_v2_t *l_cp = 00;
5307 OPJ_UINT32 l_size_tile;
5308 OPJ_UINT32 l_remaining_data;
5311 assert(p_j2k != 00);
5312 assert(p_manager != 00);
5313 assert(p_stream != 00);
5315 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5318 /* make room for the EOF marker */
5319 l_remaining_data = p_total_data_size - 4;
5321 l_cp = &(p_j2k->m_cp);
5322 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5325 /* update tile coder */
5326 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5327 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5329 l_size_tile = l_cp->th * l_cp->tw;
5332 /* TODO mergeV2: check this part which use cstr_info */
5333 /*l_cstr_info = p_j2k->cstr_info;
5335 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5336 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5337 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5343 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5345 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5350 /* update markers struct */
5351 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5353 assert( 0 && "TODO" );
5354 #endif /* USE_JPWL */
5359 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5360 p_tile_coder->tcd_image->tiles->packno = 0;
5362 l_cstr_info->packno = 0;
5366 *p_data_written = 0;
5368 if (! tcd_encode_tile_v2(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) {
5369 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5373 *p_data_written += 2;
5379 static void j2k_read_sod(opj_j2k_t *j2k) {
5380 int len, truncate = 0, i;
5381 unsigned char *data = NULL, *data_ptr = NULL;
5383 opj_cio_t *cio = j2k->cio;
5384 int curtileno = j2k->curtileno;
5387 if (j2k->cstr_info) {
5388 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5389 cio_tell(cio) + j2k->pos_correction - 1;
5390 if (j2k->cur_tp_num == 0)
5391 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5392 j2k->cstr_info->packno = 0;
5395 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
5397 if (len == cio_numbytesleft(cio) + 1) {
5398 truncate = 1; /* Case of a truncate codestream */
5401 data = j2k->tile_data[curtileno];
5402 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
5404 data_ptr = data + j2k->tile_len[curtileno];
5405 for (i = 0; i < len; i++) {
5406 data_ptr[i] = cio_read(cio, 1);
5409 j2k->tile_len[curtileno] += len;
5410 j2k->tile_data[curtileno] = data;
5413 j2k->state = J2K_STATE_TPHSOT;
5415 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
5421 * Reads a SOD marker (Start Of Data)
5423 * @param p_header_data the data contained in the SOD box.
5424 * @param p_j2k the jpeg2000 codec.
5425 * @param p_header_size the size of the data contained in the SOD marker.
5426 * @param p_manager the user event manager.
5428 opj_bool j2k_read_sod_v2 (
5429 opj_j2k_v2_t *p_j2k,
5430 struct opj_stream_private *p_stream,
5431 struct opj_event_mgr * p_manager
5434 OPJ_UINT32 l_current_read_size;
5435 opj_codestream_index_t * l_cstr_index = 00;
5436 OPJ_BYTE ** l_current_data = 00;
5437 opj_tcp_v2_t * l_tcp = 00;
5438 OPJ_UINT32 * l_tile_len = 00;
5441 assert(p_j2k != 00);
5442 assert(p_manager != 00);
5443 assert(p_stream != 00);
5445 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5447 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5448 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5449 // but we are in the last tile part,
5450 // so its result will fit on OPJ_UINT32 unless we find
5451 // a file with a single tile part of more than 4 GB...*/
5452 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5455 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5457 l_current_data = &(l_tcp->m_data);
5458 l_tile_len = &l_tcp->m_data_size;
5460 if (! *l_current_data) {
5461 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5464 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5467 if (*l_current_data == 00) {
5468 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5474 l_cstr_index = p_j2k->cstr_index;
5476 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5478 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5479 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5481 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5482 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5484 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5488 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5490 /*l_cstr_index->packno = 0;*/
5493 l_current_read_size = opj_stream_read_data( p_stream,
5494 *l_current_data + *l_tile_len,
5495 p_j2k->m_specific_param.m_decoder.m_sot_length,
5498 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5499 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5502 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5505 *l_tile_len += l_current_read_size;
5511 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5512 opj_cp_t *cp = j2k->cp;
5513 opj_tcp_t *tcp = &cp->tcps[tileno];
5514 opj_cio_t *cio = j2k->cio;
5515 int numcomps = j2k->image->numcomps;
5517 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5518 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5519 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5520 cio_write(cio, 0, 1); /* Srgn */
5521 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5525 * Writes the RGN marker (Region Of Interest)
5527 * @param p_tile_no the tile to output
5528 * @param p_comp_no the component to output
5529 * @param p_stream the stream to write data to.
5530 * @param p_j2k J2K codec.
5531 * @param p_manager the user event manager.
5533 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5534 OPJ_UINT32 p_tile_no,
5535 OPJ_UINT32 p_comp_no,
5536 struct opj_stream_private *p_stream,
5537 struct opj_event_mgr * p_manager )
5539 OPJ_BYTE * l_current_data = 00;
5540 OPJ_UINT32 l_nb_comp;
5541 OPJ_UINT32 l_rgn_size;
5542 opj_image_t *l_image = 00;
5543 opj_cp_v2_t *l_cp = 00;
5544 opj_tcp_v2_t *l_tcp = 00;
5545 opj_tccp_t *l_tccp = 00;
5546 OPJ_UINT32 l_comp_room;
5549 assert(p_j2k != 00);
5550 assert(p_manager != 00);
5551 assert(p_stream != 00);
5553 l_cp = &(p_j2k->m_cp);
5554 l_tcp = &l_cp->tcps[p_tile_no];
5555 l_tccp = &l_tcp->tccps[p_comp_no];
5557 l_nb_comp = l_image->numcomps;
5559 if (l_nb_comp <= 256) {
5566 l_rgn_size = 6 + l_comp_room;
5568 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5570 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5571 l_current_data += 2;
5573 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5574 l_current_data += 2;
5576 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5577 l_current_data+=l_comp_room;
5579 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5582 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5585 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) {
5592 static void j2k_read_rgn(opj_j2k_t *j2k) {
5593 int len, compno, roisty;
5595 opj_cp_t *cp = j2k->cp;
5596 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5597 opj_cio_t *cio = j2k->cio;
5598 int numcomps = j2k->image->numcomps;
5600 len = cio_read(cio, 2); /* Lrgn */
5601 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
5602 roisty = cio_read(cio, 1); /* Srgn */
5605 if (j2k->cp->correct) {
5606 /* totlen is negative or larger than the bytes left!!! */
5607 if (compno >= numcomps) {
5608 opj_event_msg(j2k->cinfo, EVT_ERROR,
5609 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5611 if (!JPWL_ASSUME || JPWL_ASSUME) {
5612 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5617 #endif /* USE_JPWL */
5619 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
5622 static void j2k_write_eoc(opj_j2k_t *j2k) {
5623 opj_cio_t *cio = j2k->cio;
5624 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5625 cio_write(cio, J2K_MS_EOC, 2);
5629 /* update markers struct */
5630 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5631 #endif /* USE_JPWL */
5636 * Reads a RGN marker (Region Of Interest)
5638 * @param p_header_data the data contained in the POC box.
5639 * @param p_j2k the jpeg2000 codec.
5640 * @param p_header_size the size of the data contained in the POC marker.
5641 * @param p_manager the user event manager.
5643 opj_bool j2k_read_rgn_v2 (
5644 opj_j2k_v2_t *p_j2k,
5645 OPJ_BYTE * p_header_data,
5646 OPJ_UINT32 p_header_size,
5647 struct opj_event_mgr * p_manager
5650 OPJ_UINT32 l_nb_comp;
5651 opj_image_t * l_image = 00;
5653 opj_cp_v2_t *l_cp = 00;
5654 opj_tcp_v2_t *l_tcp = 00;
5655 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5658 assert(p_header_data != 00);
5659 assert(p_j2k != 00);
5660 assert(p_manager != 00);
5662 l_image = p_j2k->m_private_image;
5663 l_nb_comp = l_image->numcomps;
5665 if (l_nb_comp <= 256) {
5670 if (p_header_size != 2 + l_comp_room) {
5671 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5675 l_cp = &(p_j2k->m_cp);
5676 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5677 &l_cp->tcps[p_j2k->m_current_tile_number] :
5678 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5680 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5681 p_header_data+=l_comp_room;
5682 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5686 if (l_cp->correct) {
5687 /* totlen is negative or larger than the bytes left!!! */
5688 if (l_comp_room >= l_nb_comp) {
5689 opj_event_msg_v2(p_manager, EVT_ERROR,
5690 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5691 l_comp_room, l_nb_comp);
5692 if (!JPWL_ASSUME || JPWL_ASSUME) {
5693 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5698 #endif /* USE_JPWL */
5700 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5707 OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5709 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5712 OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5718 * Updates the rates of the tcp.
5720 * @param p_stream the stream to write data to.
5721 * @param p_j2k J2K codec.
5722 * @param p_manager the user event manager.
5724 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5725 struct opj_stream_private *p_stream,
5726 struct opj_event_mgr * p_manager )
5728 opj_cp_v2_t * l_cp = 00;
5729 opj_image_t * l_image = 00;
5730 opj_tcp_v2_t * l_tcp = 00;
5731 opj_image_comp_t * l_img_comp = 00;
5734 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5735 OPJ_FLOAT32 * l_rates = 0;
5736 OPJ_FLOAT32 l_sot_remove;
5737 OPJ_UINT32 l_bits_empty, l_size_pixel;
5738 OPJ_UINT32 l_tile_size = 0;
5739 OPJ_UINT32 l_last_res;
5740 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5743 assert(p_j2k != 00);
5744 assert(p_manager != 00);
5745 assert(p_stream != 00);
5748 l_cp = &(p_j2k->m_cp);
5749 l_image = p_j2k->m_private_image;
5752 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5753 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5754 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5756 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5757 l_tp_stride_func = get_tp_stride;
5760 l_tp_stride_func = get_default_stride;
5763 for (i=0;i<l_cp->th;++i) {
5764 for (j=0;j<l_cp->tw;++j) {
5765 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5767 /* 4 borders of the tile rescale on the image if necessary */
5768 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5769 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5770 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5771 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5773 l_rates = l_tcp->rates;
5775 /* Modification of the RATE >> */
5777 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5779 ((*l_rates) * l_bits_empty)
5787 for (k = 1; k < l_tcp->numlayers; ++k) {
5789 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5791 ((*l_rates) * l_bits_empty)
5807 for (i=0;i<l_cp->th;++i) {
5808 for (j=0;j<l_cp->tw;++j) {
5809 l_rates = l_tcp->rates;
5812 *l_rates -= l_sot_remove;
5814 if (*l_rates < 30) {
5821 l_last_res = l_tcp->numlayers - 1;
5823 for (k = 1; k < l_last_res; ++k) {
5826 *l_rates -= l_sot_remove;
5828 if (*l_rates < *(l_rates - 1) + 10) {
5829 *l_rates = (*(l_rates - 1)) + 20;
5837 *l_rates -= (l_sot_remove + 2.f);
5839 if (*l_rates < *(l_rates - 1) + 10) {
5840 *l_rates = (*(l_rates - 1)) + 20;
5848 l_img_comp = l_image->comps;
5851 for (i=0;i<l_image->numcomps;++i) {
5852 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5854 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5862 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5864 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5866 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5867 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5868 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5869 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5873 if (l_cp->m_specific_param.m_enc.m_cinema) {
5874 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5875 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5876 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5880 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5881 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5887 static void j2k_read_eoc(opj_j2k_t *j2k) {
5891 /* if packets should be decoded */
5892 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5893 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5894 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5895 for (i = 0; i < j2k->cp->tileno_size; i++) {
5896 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5897 tileno = j2k->cp->tileno[i];
5898 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5899 opj_free(j2k->tile_data[tileno]);
5900 j2k->tile_data[tileno] = NULL;
5901 tcd_free_decode_tile(tcd, i);
5902 if (success == OPJ_FALSE) {
5903 j2k->state |= J2K_STATE_ERR;
5907 tcd_free_decode(tcd);
5910 /* if packets should not be decoded */
5912 for (i = 0; i < j2k->cp->tileno_size; i++) {
5913 tileno = j2k->cp->tileno[i];
5914 opj_free(j2k->tile_data[tileno]);
5915 j2k->tile_data[tileno] = NULL;
5918 if (j2k->state & J2K_STATE_ERR)
5919 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5921 j2k->state = J2K_STATE_MT;
5925 * Reads a EOC marker (End Of Codestream)
5927 * @param p_header_data the data contained in the SOD box.
5928 * @param p_j2k the jpeg2000 codec.
5929 * @param p_header_size the size of the data contained in the SOD marker.
5930 * @param p_manager the user event manager.
5932 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5933 struct opj_stream_private *p_stream,
5934 struct opj_event_mgr * p_manager )
5937 opj_tcd_v2_t * l_tcd = 00;
5938 OPJ_UINT32 l_nb_tiles;
5939 opj_tcp_v2_t * l_tcp = 00;
5943 assert(p_j2k != 00);
5944 assert(p_manager != 00);
5945 assert(p_stream != 00);
5947 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5948 l_tcp = p_j2k->m_cp.tcps;
5950 l_tcd = tcd_create_v2(OPJ_TRUE);
5952 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5956 for (i = 0; i < l_nb_tiles; ++i) {
5957 if (l_tcp->m_data) {
5958 if (! tcd_init_decode_tile(l_tcd, i)) {
5959 tcd_destroy_v2(l_tcd);
5960 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5964 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5968 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5973 j2k_tcp_destroy(l_tcp);
5977 tcd_destroy_v2(l_tcd);
5982 * Gets the offset of the header.
5984 * @param p_stream the stream to write data to.
5985 * @param p_j2k J2K codec.
5986 * @param p_manager the user event manager.
5988 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5989 struct opj_stream_private *p_stream,
5990 struct opj_event_mgr * p_manager )
5993 assert(p_j2k != 00);
5994 assert(p_manager != 00);
5995 assert(p_stream != 00);
5997 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
6003 * Writes the MCT marker (Multiple Component Transform)
6005 * @param p_stream the stream to write data to.
6006 * @param p_j2k J2K codec.
6007 * @param p_manager the user event manager.
6009 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
6010 struct opj_stream_private *p_stream,
6011 struct opj_event_mgr * p_manager )
6014 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6015 opj_mct_data_t * l_mct_record;
6016 opj_tcp_v2_t * l_tcp;
6019 assert(p_j2k != 00);
6020 assert(p_stream != 00);
6021 assert(p_manager != 00);
6023 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
6027 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6028 l_mct_record = l_tcp->m_mct_records;
6030 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6032 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
6039 l_mcc_record = l_tcp->m_mcc_records;
6041 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6043 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6050 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6058 * Writes the image components.
6060 * @param p_stream the stream to write data to.
6061 * @param p_j2k J2K codec.
6062 * @param p_manager the user event manager.
6064 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6065 struct opj_stream_private *p_stream,
6066 struct opj_event_mgr * p_manager )
6071 assert(p_j2k != 00);
6072 assert(p_manager != 00);
6073 assert(p_stream != 00);
6075 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6077 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6081 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6090 * Writes regions of interests.
6092 * @param p_stream the stream to write data to.
6093 * @param p_j2k J2K codec.
6094 * @param p_manager the user event manager.
6096 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
6097 struct opj_stream_private *p_stream,
6098 struct opj_event_mgr * p_manager )
6101 const opj_tccp_t *l_tccp = 00;
6104 assert(p_j2k != 00);
6105 assert(p_manager != 00);
6106 assert(p_stream != 00);
6108 l_tccp = p_j2k->m_cp.tcps->tccps;
6110 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6111 if (l_tccp->roishift) {
6113 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6127 * @param p_stream the stream to write data to.
6128 * @param p_j2k J2K codec.
6129 * @param p_manager the user event manager.
6131 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6132 struct opj_stream_private *p_stream,
6133 struct opj_event_mgr * p_manager )
6135 opj_codestream_index_t * l_cstr_index = 00;
6138 assert(p_j2k != 00);
6139 assert(p_manager != 00);
6140 assert(p_stream != 00);
6142 l_cstr_index = p_j2k->cstr_index;
6144 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6146 /* The following adjustment is done to adjust the codestream size */
6147 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6148 /* the first bunch of bytes is not in the codestream */
6149 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6154 /* preparation of JPWL marker segments */
6158 /* encode according to JPWL */
6159 jpwl_encode(p_j2k, p_stream, image);
6163 assert( 0 && "TODO" );
6164 #endif /* USE_JPWL */
6169 typedef struct opj_dec_mstabent {
6172 /** value of the state when the marker can appear */
6174 /** action linked to the marker */
6175 void (*handler) (opj_j2k_t *j2k);
6176 } opj_dec_mstabent_t;
6178 opj_dec_mstabent_t j2k_dec_mstab[] = {
6179 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
6180 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
6181 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
6182 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6183 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
6184 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
6185 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6186 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6187 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6188 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6189 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6190 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6191 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6192 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6193 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6194 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6196 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6197 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6200 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6201 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6202 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6203 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6204 #endif /* USE_JPWL */
6206 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6207 {J2K_MS_INSEC, 0, j2k_read_insec},
6208 #endif /* USE_JPSEC */
6210 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6213 static void j2k_read_unk(opj_j2k_t *j2k) {
6214 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6217 if (j2k->cp->correct) {
6219 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6220 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6221 id = cio_read(j2k->cio, 2);
6222 opj_event_msg(j2k->cinfo, EVT_ERROR,
6223 "JPWL: really don't know this marker %x\n",
6226 opj_event_msg(j2k->cinfo, EVT_ERROR,
6227 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6230 /* OK, activate this at your own risk!!! */
6231 /* we look for the marker at the minimum hamming distance from this */
6232 while (j2k_dec_mstab[m].id) {
6234 /* 1's where they differ */
6235 tmp_id = j2k_dec_mstab[m].id ^ id;
6237 /* compute the hamming distance between our id and the current */
6239 for (i = 0; i < 16; i++) {
6240 if ((tmp_id >> i) & 0x0001) {
6245 /* if current distance is smaller, set the minimum */
6246 if (cur_dist < min_dist) {
6247 min_dist = cur_dist;
6248 min_id = j2k_dec_mstab[m].id;
6251 /* jump to the next marker */
6255 /* do we substitute the marker? */
6256 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6257 opj_event_msg(j2k->cinfo, EVT_ERROR,
6258 "- marker %x is at distance %d from the read %x\n",
6259 min_id, min_dist, id);
6260 opj_event_msg(j2k->cinfo, EVT_ERROR,
6261 "- trying to substitute in place and crossing fingers!\n");
6262 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6263 cio_write(j2k->cio, min_id, 2);
6266 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6271 #endif /* USE_JPWL */
6276 * Reads an unknown marker
6278 * @param p_stream the stream object to read from.
6279 * @param p_j2k the jpeg2000 codec.
6280 * @param p_manager the user event manager.
6282 * @return true if the marker could be deduced.
6284 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6285 struct opj_stream_private *p_stream,
6286 OPJ_UINT32 *output_marker,
6287 struct opj_event_mgr * p_manager
6290 OPJ_UINT32 l_unknown_marker;
6291 const opj_dec_memory_marker_handler_t * l_marker_handler;
6292 OPJ_UINT32 l_size_unk = 2;
6295 assert(p_j2k != 00);
6296 assert(p_manager != 00);
6297 assert(p_stream != 00);
6299 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6302 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6303 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6304 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6308 /* read 2 bytes as the new marker ID*/
6309 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6311 if (!(l_unknown_marker < 0xff00)) {
6313 /* Get the marker handler from the marker ID*/
6314 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6316 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6317 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6321 if (l_marker_handler->id != J2K_MS_UNK) {
6322 /* Add the marker to the codestream index*/
6323 if (l_marker_handler->id != J2K_MS_SOT)
6324 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6325 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6327 break; /* next marker is known and well located */
6335 *output_marker = l_marker_handler->id ;
6341 Read the lookup table containing all the marker, status and action
6342 @param id Marker value
6344 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6345 opj_dec_mstabent_t *e;
6346 for (e = j2k_dec_mstab; e->id != 0; e++) {
6355 * Writes the MCT marker (Multiple Component Transform)
6357 * @param p_stream the stream to write data to.
6358 * @param p_j2k J2K codec.
6359 * @param p_manager the user event manager.
6361 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6362 opj_mct_data_t * p_mct_record,
6363 struct opj_stream_private *p_stream,
6364 struct opj_event_mgr * p_manager )
6366 OPJ_UINT32 l_mct_size;
6367 OPJ_BYTE * l_current_data = 00;
6371 assert(p_j2k != 00);
6372 assert(p_manager != 00);
6373 assert(p_stream != 00);
6375 l_mct_size = 10 + p_mct_record->m_data_size;
6377 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6378 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6379 = (OPJ_BYTE*)opj_realloc(
6380 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6383 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6387 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6390 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6392 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6393 l_current_data += 2;
6395 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6396 l_current_data += 2;
6398 opj_write_bytes(l_current_data,0,2); /* Zmct */
6399 l_current_data += 2;
6401 /* only one marker atm */
6402 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6404 opj_write_bytes(l_current_data,l_tmp,2);
6405 l_current_data += 2;
6407 opj_write_bytes(l_current_data,0,2); /* Ymct */
6410 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6412 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,p_manager) != l_mct_size) {
6420 * Reads a MCT marker (Multiple Component Transform)
6422 * @param p_header_data the data contained in the MCT box.
6423 * @param p_j2k the jpeg2000 codec.
6424 * @param p_header_size the size of the data contained in the MCT marker.
6425 * @param p_manager the user event manager.
6427 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6428 OPJ_BYTE * p_header_data,
6429 OPJ_UINT32 p_header_size,
6430 struct opj_event_mgr * p_manager )
6433 opj_tcp_v2_t *l_tcp = 00;
6436 opj_mct_data_t * l_mct_data;
6439 assert(p_header_data != 00);
6440 assert(p_j2k != 00);
6442 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6443 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6444 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6446 if (p_header_size < 2) {
6447 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6452 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6455 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6459 if(p_header_size <= 6) {
6460 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6464 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6465 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6468 l_indix = l_tmp & 0xff;
6469 l_mct_data = l_tcp->m_mct_records;
6471 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6472 if (l_mct_data->m_index == l_indix) {
6479 if (i == l_tcp->m_nb_mct_records) {
6480 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6481 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6483 l_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(l_tcp->m_mct_records,l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
6484 if(! l_tcp->m_mct_records) {
6485 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6489 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6490 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6493 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6496 if (l_mct_data->m_data) {
6497 opj_free(l_mct_data->m_data);
6498 l_mct_data->m_data = 00;
6501 l_mct_data->m_index = l_indix;
6502 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6503 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6505 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6508 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6514 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6515 if (! l_mct_data->m_data) {
6516 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6519 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6521 l_mct_data->m_data_size = p_header_size;
6522 ++l_tcp->m_nb_mct_records;
6528 * Writes the MCC marker (Multiple Component Collection)
6530 * @param p_stream the stream to write data to.
6531 * @param p_j2k J2K codec.
6532 * @param p_manager the user event manager.
6534 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6535 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6536 struct opj_stream_private *p_stream,
6537 struct opj_event_mgr * p_manager )
6540 OPJ_UINT32 l_mcc_size;
6541 OPJ_BYTE * l_current_data = 00;
6542 OPJ_UINT32 l_nb_bytes_for_comp;
6547 assert(p_j2k != 00);
6548 assert(p_manager != 00);
6549 assert(p_stream != 00);
6551 if (p_mcc_record->m_nb_comps > 255 ) {
6552 l_nb_bytes_for_comp = 2;
6556 l_nb_bytes_for_comp = 1;
6560 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6561 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6563 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6564 = (OPJ_BYTE*)opj_realloc(
6565 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6567 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6571 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6574 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6576 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6577 l_current_data += 2;
6579 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6580 l_current_data += 2;
6583 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6584 l_current_data += 2;
6586 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6589 /* only one marker atm */
6590 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6593 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6596 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6599 opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps | l_mask,2); /* Nmcci number of input components involved and size for each component offset = 8 bits */
6602 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6603 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6604 l_current_data+=l_nb_bytes_for_comp;
6607 opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps|l_mask,2); /* Mmcci number of output components involved and size for each component offset = 8 bits */
6610 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6612 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6613 l_current_data+=l_nb_bytes_for_comp;
6616 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6618 if (p_mcc_record->m_decorrelation_array) {
6619 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6622 if (p_mcc_record->m_offset_array) {
6623 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6626 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6629 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,p_manager) != l_mcc_size) {
6637 * Reads a MCC marker (Multiple Component Collection)
6639 * @param p_header_data the data contained in the MCC box.
6640 * @param p_j2k the jpeg2000 codec.
6641 * @param p_header_size the size of the data contained in the MCC marker.
6642 * @param p_manager the user event manager.
6644 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6645 OPJ_BYTE * p_header_data,
6646 OPJ_UINT32 p_header_size,
6647 struct opj_event_mgr * p_manager )
6652 opj_tcp_v2_t * l_tcp;
6653 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6654 opj_mct_data_t * l_mct_data;
6655 OPJ_UINT32 l_nb_collections;
6656 OPJ_UINT32 l_nb_comps;
6657 OPJ_UINT32 l_nb_bytes_by_comp;
6661 assert(p_header_data != 00);
6662 assert(p_j2k != 00);
6663 assert(p_manager != 00);
6665 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6666 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6667 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6669 if (p_header_size < 2) {
6670 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6675 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6678 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6682 if (p_header_size < 7) {
6683 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6687 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6690 l_mcc_record = l_tcp->m_mcc_records;
6692 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6693 if (l_mcc_record->m_index == l_indix) {
6700 if (i == l_tcp->m_nb_mcc_records) {
6701 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6702 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6704 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6705 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6706 if (! l_tcp->m_mcc_records) {
6707 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6710 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6711 memset(l_mcc_record,0,(l_tcp->m_nb_max_mcc_records-l_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
6713 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6715 l_mcc_record->m_index = l_indix;
6717 /* only one marker atm */
6718 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6721 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6725 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6728 if (l_nb_collections > 1) {
6729 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6735 for (i=0;i<l_nb_collections;++i) {
6736 if (p_header_size < 3) {
6737 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6741 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6745 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6749 opj_read_bytes(p_header_data,&l_nb_comps,2);
6754 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6755 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6757 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6758 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6762 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6764 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6765 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6766 p_header_data+=l_nb_bytes_by_comp;
6769 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6774 opj_read_bytes(p_header_data,&l_nb_comps,2);
6777 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6778 l_nb_comps &= 0x7fff;
6780 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6781 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6785 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6786 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6790 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6792 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6793 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6794 p_header_data+=l_nb_bytes_by_comp;
6797 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6802 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6805 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6806 l_mcc_record->m_decorrelation_array = 00;
6807 l_mcc_record->m_offset_array = 00;
6809 l_indix = l_tmp & 0xff;
6811 l_mct_data = l_tcp->m_mct_records;
6812 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6813 if (l_mct_data->m_index == l_indix) {
6814 l_mcc_record->m_decorrelation_array = l_mct_data;
6820 if (l_mcc_record->m_decorrelation_array == 00) {
6821 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6826 l_indix = (l_tmp >> 8) & 0xff;
6828 l_mct_data = l_tcp->m_mct_records;
6829 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6830 if (l_mct_data->m_index == l_indix) {
6831 l_mcc_record->m_offset_array = l_mct_data;
6837 if (l_mcc_record->m_offset_array == 00) {
6838 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6844 if (p_header_size != 0) {
6845 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6849 ++l_tcp->m_nb_mcc_records;
6856 * Writes the MCO marker (Multiple component transformation ordering)
6858 * @param p_stream the stream to write data to.
6859 * @param p_j2k J2K codec.
6860 * @param p_manager the user event manager.
6862 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6863 struct opj_stream_private *p_stream,
6864 struct opj_event_mgr * p_manager
6867 OPJ_BYTE * l_current_data = 00;
6868 OPJ_UINT32 l_mco_size;
6869 opj_tcp_v2_t * l_tcp = 00;
6870 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6874 assert(p_j2k != 00);
6875 assert(p_manager != 00);
6876 assert(p_stream != 00);
6878 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6879 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6881 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6882 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6884 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6885 = (OPJ_BYTE*)opj_realloc(
6886 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6888 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6893 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6896 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6897 l_current_data += 2;
6899 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6900 l_current_data += 2;
6902 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6905 l_mcc_record = l_tcp->m_mcc_records;
6906 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6907 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6913 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,p_manager) != l_mco_size) {
6921 * Reads a MCO marker (Multiple Component Transform Ordering)
6923 * @param p_header_data the data contained in the MCO box.
6924 * @param p_j2k the jpeg2000 codec.
6925 * @param p_header_size the size of the data contained in the MCO marker.
6926 * @param p_manager the user event manager.
6928 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6929 OPJ_BYTE * p_header_data,
6930 OPJ_UINT32 p_header_size,
6931 struct opj_event_mgr * p_manager )
6933 OPJ_UINT32 l_tmp, i;
6934 OPJ_UINT32 l_nb_stages;
6935 opj_tcp_v2_t * l_tcp;
6936 opj_tccp_t * l_tccp;
6937 opj_image_t * l_image;
6938 opj_image_comp_t * l_img_comp;
6941 assert(p_header_data != 00);
6942 assert(p_j2k != 00);
6943 assert(p_manager != 00);
6945 l_image = p_j2k->m_private_image;
6946 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6947 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6948 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6950 if (p_header_size < 1) {
6951 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6955 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6958 if (l_nb_stages > 1) {
6959 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6963 if (p_header_size != l_nb_stages + 1) {
6964 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6968 l_tccp = l_tcp->tccps;
6969 l_img_comp = l_image->comps;
6971 for (i=0;i<l_image->numcomps;++i) {
6972 l_tccp->m_dc_level_shift = 0;
6976 if (l_tcp->m_mct_decoding_matrix) {
6977 opj_free(l_tcp->m_mct_decoding_matrix);
6978 l_tcp->m_mct_decoding_matrix = 00;
6981 for (i=0;i<l_nb_stages;++i) {
6982 opj_read_bytes(p_header_data,&l_tmp,1);
6985 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6993 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6996 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6997 opj_mct_data_t * l_deco_array, * l_offset_array;
6998 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6999 OPJ_UINT32 l_nb_elem;
7000 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
7001 opj_tccp_t * l_tccp;
7004 assert(p_tcp != 00);
7006 l_mcc_record = p_tcp->m_mcc_records;
7008 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
7009 if (l_mcc_record->m_index == p_index) {
7014 if (i==p_tcp->m_nb_mcc_records) {
7015 /** element discarded **/
7019 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
7020 /** do not support number of comps != image */
7024 l_deco_array = l_mcc_record->m_decorrelation_array;
7027 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
7028 if (l_deco_array->m_data_size != l_data_size) {
7032 l_nb_elem = p_image->numcomps * p_image->numcomps;
7033 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
7034 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7036 if (! p_tcp->m_mct_decoding_matrix ) {
7040 j2k_mct_read_functions_to_float[l_deco_array->m_element_type](l_deco_array->m_data,p_tcp->m_mct_decoding_matrix,l_nb_elem);
7043 l_offset_array = l_mcc_record->m_offset_array;
7045 if (l_offset_array) {
7046 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
7047 if (l_offset_array->m_data_size != l_data_size) {
7051 l_nb_elem = p_image->numcomps;
7052 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7053 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7055 if (! l_offset_data ) {
7059 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7061 l_tccp = p_tcp->tccps;
7062 l_current_offset_data = l_offset_data;
7064 for (i=0;i<p_image->numcomps;++i) {
7065 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7069 opj_free(l_offset_data);
7076 * Writes the CBD marker (Component bit depth definition)
7078 * @param p_stream the stream to write data to.
7079 * @param p_j2k J2K codec.
7080 * @param p_manager the user event manager.
7082 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7083 struct opj_stream_private *p_stream,
7084 struct opj_event_mgr * p_manager )
7087 OPJ_UINT32 l_cbd_size;
7088 OPJ_BYTE * l_current_data = 00;
7089 opj_image_t *l_image = 00;
7090 opj_image_comp_t * l_comp = 00;
7093 assert(p_j2k != 00);
7094 assert(p_manager != 00);
7095 assert(p_stream != 00);
7097 l_image = p_j2k->m_private_image;
7098 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7100 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7101 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7102 = (OPJ_BYTE*)opj_realloc(
7103 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7106 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7110 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7113 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7115 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7116 l_current_data += 2;
7118 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7119 l_current_data += 2;
7121 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7124 l_comp = l_image->comps;
7126 for (i=0;i<l_image->numcomps;++i) {
7127 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7133 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,p_manager) != l_cbd_size) {
7141 * Reads a CBD marker (Component bit depth definition)
7142 * @param p_header_data the data contained in the CBD box.
7143 * @param p_j2k the jpeg2000 codec.
7144 * @param p_header_size the size of the data contained in the CBD marker.
7145 * @param p_manager the user event manager.
7147 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7148 OPJ_BYTE * p_header_data,
7149 OPJ_UINT32 p_header_size,
7150 struct opj_event_mgr * p_manager)
7152 OPJ_UINT32 l_nb_comp,l_num_comp;
7153 OPJ_UINT32 l_comp_def;
7155 opj_image_comp_t * l_comp = 00;
7158 assert(p_header_data != 00);
7159 assert(p_j2k != 00);
7160 assert(p_manager != 00);
7162 l_num_comp = p_j2k->m_private_image->numcomps;
7164 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7165 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7169 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7172 if (l_nb_comp != l_num_comp) {
7173 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7177 l_comp = p_j2k->m_private_image->comps;
7178 for (i=0;i<l_num_comp;++i) {
7179 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7181 l_comp->sgnd = (l_comp_def>>7) & 1;
7182 l_comp->prec = (l_comp_def&0x7f) + 1;
7190 /* ----------------------------------------------------------------------- */
7191 /* J2K / JPT decoder interface */
7192 /* ----------------------------------------------------------------------- */
7194 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
7195 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7199 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
7200 if(!j2k->default_tcp) {
7206 j2k->tile_data = NULL;
7211 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7214 if(j2k->tile_len != NULL) {
7215 opj_free(j2k->tile_len);
7217 if(j2k->tile_data != NULL) {
7218 opj_free(j2k->tile_data);
7220 if(j2k->default_tcp != NULL) {
7221 opj_tcp_t *default_tcp = j2k->default_tcp;
7222 if(default_tcp->ppt_data_first != NULL) {
7223 opj_free(default_tcp->ppt_data_first);
7225 if(j2k->default_tcp->tccps != NULL) {
7226 opj_free(j2k->default_tcp->tccps);
7228 opj_free(j2k->default_tcp);
7230 if(j2k->cp != NULL) {
7231 opj_cp_t *cp = j2k->cp;
7232 if(cp->tcps != NULL) {
7233 for(i = 0; i < cp->tw * cp->th; i++) {
7234 if(cp->tcps[i].ppt_data_first != NULL) {
7235 opj_free(cp->tcps[i].ppt_data_first);
7237 if(cp->tcps[i].tccps != NULL) {
7238 opj_free(cp->tcps[i].tccps);
7243 if(cp->ppm_data_first != NULL) {
7244 opj_free(cp->ppm_data_first);
7246 if(cp->tileno != NULL) {
7247 opj_free(cp->tileno);
7249 if(cp->comment != NULL) {
7250 opj_free(cp->comment);
7258 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
7259 if(j2k && parameters) {
7260 /* create and initialize the coding parameters structure */
7261 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7262 cp->reduce = parameters->cp_reduce;
7263 cp->layer = parameters->cp_layer;
7264 cp->limit_decoding = parameters->cp_limit_decoding;
7267 cp->correct = parameters->jpwl_correct;
7268 cp->exp_comps = parameters->jpwl_exp_comps;
7269 cp->max_tiles = parameters->jpwl_max_tiles;
7270 #endif /* USE_JPWL */
7273 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
7278 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7280 if(j2k && parameters) {
7281 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7282 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7285 j2k->m_cp.correct = parameters->jpwl_correct;
7286 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7287 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7288 #endif /* USE_JPWL */
7292 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7293 opj_image_t *image = NULL;
7295 opj_common_ptr cinfo = j2k->cinfo;
7298 j2k->cstr_info = cstr_info;
7300 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
7302 /* create an empty image */
7303 image = opj_image_create0();
7306 j2k->state = J2K_STATE_MHSOC;
7309 opj_dec_mstabent_t *e;
7310 int id = cio_read(cio, 2);
7313 /* we try to honor JPWL correction power */
7314 if (j2k->cp->correct) {
7316 int orig_pos = cio_tell(cio);
7319 /* call the corrector */
7320 status = jpwl_correct(j2k);
7322 /* go back to where you were */
7323 cio_seek(cio, orig_pos - 2);
7325 /* re-read the marker */
7326 id = cio_read(cio, 2);
7328 /* check whether it begins with ff */
7329 if (id >> 8 != 0xff) {
7330 opj_event_msg(cinfo, EVT_ERROR,
7331 "JPWL: possible bad marker %x at %d\n",
7332 id, cio_tell(cio) - 2);
7334 opj_image_destroy(image);
7335 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
7338 /* we try to correct */
7340 cio_seek(cio, cio_tell(cio) - 2);
7341 cio_write(cio, id, 2);
7342 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
7343 "- setting marker to %x\n",
7348 #endif /* USE_JPWL */
7350 if (id >> 8 != 0xff) {
7351 opj_image_destroy(image);
7352 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7355 e = j2k_dec_mstab_lookup(id);
7356 /* Check if the marker is known*/
7357 if (!(j2k->state & e->states)) {
7358 opj_image_destroy(image);
7359 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7362 /* Check if the decoding is limited to the main header*/
7363 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
7364 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
7371 if (j2k->state & J2K_STATE_ERR)
7374 if (j2k->state == J2K_STATE_MT) {
7377 if (j2k->state == J2K_STATE_NEOC) {
7381 if (j2k->state == J2K_STATE_NEOC) {
7385 if (j2k->state != J2K_STATE_MT) {
7386 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7392 * Read a JPT-stream and decode file
7395 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7396 opj_image_t *image = NULL;
7397 opj_jpt_msg_header_t header;
7399 opj_common_ptr cinfo = j2k->cinfo;
7401 OPJ_ARG_NOT_USED(cstr_info);
7405 /* create an empty image */
7406 image = opj_image_create0();
7409 j2k->state = J2K_STATE_MHSOC;
7411 /* Initialize the header */
7412 jpt_init_msg_header(&header);
7413 /* Read the first header of the message */
7414 jpt_read_msg_header(cinfo, cio, &header);
7416 position = cio_tell(cio);
7417 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7418 opj_image_destroy(image);
7419 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7424 opj_dec_mstabent_t *e = NULL;
7427 if (!cio_numbytesleft(cio)) {
7431 /* data-bin read -> need to read a new header */
7432 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7433 jpt_read_msg_header(cinfo, cio, &header);
7434 position = cio_tell(cio);
7435 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7436 opj_image_destroy(image);
7437 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7442 id = cio_read(cio, 2);
7443 if (id >> 8 != 0xff) {
7444 opj_image_destroy(image);
7445 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7448 e = j2k_dec_mstab_lookup(id);
7449 if (!(j2k->state & e->states)) {
7450 opj_image_destroy(image);
7451 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7457 if (j2k->state == J2K_STATE_MT) {
7460 if (j2k->state == J2K_STATE_NEOC) {
7464 if (j2k->state == J2K_STATE_NEOC) {
7468 if (j2k->state != J2K_STATE_MT) {
7469 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7475 /* ----------------------------------------------------------------------- */
7476 /* J2K encoder interface */
7477 /* ----------------------------------------------------------------------- */
7479 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7480 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7487 opj_j2k_v2_t* j2k_create_compress_v2()
7489 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7494 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7496 l_j2k->m_is_decoder = 0;
7497 l_j2k->m_cp.m_is_decoder = 0;
7499 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7500 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7505 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7507 /* validation list creation*/
7508 l_j2k->m_validation_list = opj_procedure_list_create();
7509 if (! l_j2k->m_validation_list) {
7514 /* execution list creation*/
7515 l_j2k->m_procedure_list = opj_procedure_list_create();
7516 if (! l_j2k->m_procedure_list) {
7524 void j2k_destroy_compress(opj_j2k_t *j2k) {
7528 if(j2k->cp != NULL) {
7529 opj_cp_t *cp = j2k->cp;
7532 opj_free(cp->comment);
7535 opj_free(cp->matrice);
7537 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7538 opj_free(cp->tcps[tileno].tccps);
7547 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7548 int i, j, tileno, numpocs_tile;
7549 opj_cp_t *cp = NULL;
7551 if(!j2k || !parameters || ! image) {
7555 /* create and initialize the coding parameters structure */
7556 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7558 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7561 /* set default values for cp */
7566 copy user encoding parameters
7568 cp->cinema = parameters->cp_cinema;
7569 cp->max_comp_size = parameters->max_comp_size;
7570 cp->rsiz = parameters->cp_rsiz;
7571 cp->disto_alloc = parameters->cp_disto_alloc;
7572 cp->fixed_alloc = parameters->cp_fixed_alloc;
7573 cp->fixed_quality = parameters->cp_fixed_quality;
7575 /* mod fixed_quality */
7576 if(parameters->cp_matrice) {
7577 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7578 cp->matrice = (int *) opj_malloc(array_size);
7579 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7583 cp->tdx = parameters->cp_tdx;
7584 cp->tdy = parameters->cp_tdy;
7587 cp->tx0 = parameters->cp_tx0;
7588 cp->ty0 = parameters->cp_ty0;
7590 /* comment string */
7591 if(parameters->cp_comment) {
7592 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7594 strcpy(cp->comment, parameters->cp_comment);
7599 calculate other encoding parameters
7602 if (parameters->tile_size_on) {
7603 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7604 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7606 cp->tdx = image->x1 - cp->tx0;
7607 cp->tdy = image->y1 - cp->ty0;
7610 if(parameters->tp_on){
7611 cp->tp_flag = parameters->tp_flag;
7616 for(i=0;i<image->numcomps ;i++){
7617 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7623 calculate JPWL encoding parameters
7626 if (parameters->jpwl_epc_on) {
7630 cp->epc_on = OPJ_TRUE;
7631 cp->info_on = OPJ_FALSE; /* no informative technique */
7634 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7635 cp->epb_on = OPJ_TRUE;
7637 cp->hprot_MH = parameters->jpwl_hprot_MH;
7638 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7639 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7640 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7642 /* if tile specs are not specified, copy MH specs */
7643 if (cp->hprot_TPH[0] == -1) {
7644 cp->hprot_TPH_tileno[0] = 0;
7645 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7647 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7648 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7649 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7650 cp->pprot[i] = parameters->jpwl_pprot[i];
7654 /* set ESD writing */
7655 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7656 cp->esd_on = OPJ_TRUE;
7658 cp->sens_size = parameters->jpwl_sens_size;
7659 cp->sens_addr = parameters->jpwl_sens_addr;
7660 cp->sens_range = parameters->jpwl_sens_range;
7662 cp->sens_MH = parameters->jpwl_sens_MH;
7663 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7664 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7665 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7669 /* always set RED writing to false: we are at the encoder */
7670 cp->red_on = OPJ_FALSE;
7673 cp->epc_on = OPJ_FALSE;
7675 #endif /* USE_JPWL */
7678 /* initialize the mutiple tiles */
7679 /* ---------------------------- */
7680 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7682 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7683 opj_tcp_t *tcp = &cp->tcps[tileno];
7684 tcp->numlayers = parameters->tcp_numlayers;
7685 for (j = 0; j < tcp->numlayers; j++) {
7687 if (cp->fixed_quality) {
7688 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7690 tcp->rates[j] = parameters->tcp_rates[j];
7692 if (cp->fixed_quality) { /* add fixed_quality */
7693 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7695 tcp->rates[j] = parameters->tcp_rates[j];
7699 tcp->csty = parameters->csty;
7700 tcp->prg = parameters->prog_order;
7701 tcp->mct = parameters->tcp_mct;
7705 if (parameters->numpocs) {
7706 /* initialisation of POC */
7708 for (i = 0; i < parameters->numpocs; i++) {
7709 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7710 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7711 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7712 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7713 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7714 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7715 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7716 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7717 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7721 tcp->numpocs = numpocs_tile -1 ;
7726 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7728 for (i = 0; i < image->numcomps; i++) {
7729 opj_tccp_t *tccp = &tcp->tccps[i];
7730 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7731 tccp->numresolutions = parameters->numresolution;
7732 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7733 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7734 tccp->cblksty = parameters->mode;
7735 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7736 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7738 if (i == parameters->roi_compno) {
7739 tccp->roishift = parameters->roi_shift;
7744 if(parameters->cp_cinema)
7746 /*Precinct size for lowest frequency subband=128*/
7749 /*Precinct size at all other resolutions = 256*/
7750 for (j = 1; j < tccp->numresolutions; j++) {
7755 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7757 for (j = tccp->numresolutions - 1; j >= 0; j--) {
7758 if (p < parameters->res_spec) {
7760 if (parameters->prcw_init[p] < 1) {
7763 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7766 if (parameters->prch_init[p] < 1) {
7769 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7773 int res_spec = parameters->res_spec;
7774 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7775 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7777 if (size_prcw < 1) {
7780 tccp->prcw[j] = int_floorlog2(size_prcw);
7783 if (size_prch < 1) {
7786 tccp->prch[j] = int_floorlog2(size_prch);
7790 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7793 for (j = 0; j < tccp->numresolutions; j++) {
7800 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7805 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7807 opj_cp_t *cp = NULL;
7809 opj_tcd_t *tcd = NULL; /* TCD component */
7817 j2k->cstr_info = cstr_info;
7820 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7821 cstr_info->image_w = image->x1 - image->x0;
7822 cstr_info->image_h = image->y1 - image->y0;
7823 cstr_info->prog = (&cp->tcps[0])->prg;
7824 cstr_info->tw = cp->tw;
7825 cstr_info->th = cp->th;
7826 cstr_info->tile_x = cp->tdx; /* new version parser */
7827 cstr_info->tile_y = cp->tdy; /* new version parser */
7828 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7829 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7830 cstr_info->numcomps = image->numcomps;
7831 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7832 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7833 for (compno=0; compno < image->numcomps; compno++) {
7834 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7836 cstr_info->D_max = 0.0; /* ADD Marcela */
7837 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7838 cstr_info->maxmarknum = 100;
7839 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7840 cstr_info->marknum = 0;
7850 for (compno = 1; compno < image->numcomps; compno++) {
7851 j2k_write_coc(j2k, compno);
7852 j2k_write_qcc(j2k, compno);
7856 for (compno = 0; compno < image->numcomps; compno++) {
7857 opj_tcp_t *tcp = &cp->tcps[0];
7858 if (tcp->tccps[compno].roishift)
7859 j2k_write_rgn(j2k, compno, 0);
7861 if (cp->comment != NULL) {
7865 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7869 if (cp->cinema == CINEMA4K_24) {
7874 /* uncomment only for testing JPSEC marker writing */
7875 /* j2k_write_sec(j2k); */
7879 cstr_info->main_head_end = cio_tell(cio) - 1;
7882 /**** Main Header ENDS here ***/
7884 /* create the tile encoder */
7885 tcd = tcd_create(j2k->cinfo);
7887 /* encode each tile */
7888 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7892 int acc_pack_num = 0;
7896 opj_tcp_t *tcp = &cp->tcps[tileno];
7897 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7899 j2k->curtileno = tileno;
7900 j2k->cur_tp_num = 0;
7901 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7902 /* initialisation before tile encoding */
7904 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7906 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7911 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7912 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7913 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7914 cstr_info->tile[j2k->curtileno].marknum = 0;
7918 for(pino = 0; pino <= tcp->numpocs; pino++) {
7922 /*Get number of tile parts*/
7923 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7924 tcd->tp_pos = cp->tp_pos;
7926 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7927 j2k->tp_num = tilepartno;
7930 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7931 cio_tell(cio) + j2k->pos_correction;
7935 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7936 for (compno = 1; compno < image->numcomps; compno++) {
7937 j2k_write_coc(j2k, compno);
7938 j2k_write_qcc(j2k, compno);
7940 if (cp->tcps[tileno].numpocs) {
7947 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
7948 cio_tell(cio) + j2k->pos_correction + 1;
7951 j2k_write_sod(j2k, tcd);
7955 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
7956 cio_tell(cio) + j2k->pos_correction - 1;
7957 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
7959 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
7960 cstr_info->packno - acc_pack_num;
7961 acc_pack_num = cstr_info->packno;
7969 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
7974 if (tile->PPT) { // BAD PPT !!!
7977 PPT_file=fopen("PPT","rb");
7978 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
7979 for (i=0;i<tile->len_ppt;i++) {
7981 fread(&elmt, 1, 1, PPT_file);
7982 fwrite(&elmt,1,1,f);
7991 /* destroy the tile encoder */
7992 tcd_free_encode(tcd);
7995 opj_free(j2k->cur_totnum_tp);
8000 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8002 /* The following adjustment is done to adjust the codestream size */
8003 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8004 /* the first bunch of bytes is not in the codestream */
8005 cstr_info->codestream_size -= cstr_info->main_head_start;
8011 preparation of JPWL marker segments
8015 /* encode according to JPWL */
8016 jpwl_encode(j2k, cio, image);
8019 #endif /* USE_JPWL */
8024 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8026 assert(cstr_info != 00);
8028 /* expand the list? */
8029 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8030 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8031 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8034 /* add the marker */
8035 cstr_info->marker[cstr_info->marknum].type = type;
8036 cstr_info->marker[cstr_info->marknum].pos = pos;
8037 cstr_info->marker[cstr_info->marknum].len = len;
8038 cstr_info->marknum++;
8042 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8044 assert(cstr_index != 00);
8046 /* expand the list? */
8047 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8048 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8049 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8052 /* add the marker */
8053 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8054 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8055 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8056 cstr_index->marknum++;
8060 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8062 opj_marker_info_t *marker;
8064 assert(cstr_info != 00);
8066 /* expand the list? */
8067 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8068 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8069 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8072 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8074 /* add the marker */
8075 marker->type = type;
8078 cstr_info->tile[tileno].marknum++;
8081 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8083 assert(cstr_index != 00);
8084 assert(cstr_index->tile_index != 00);
8086 /* expand the list? */
8087 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8088 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8089 cstr_index->tile_index[tileno].marker =
8090 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8091 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8094 /* add the marker */
8095 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8096 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8097 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8098 cstr_index->tile_index[tileno].marknum++;
8100 if (type == J2K_MS_SOT) {
8101 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8103 if (cstr_index->tile_index[tileno].tp_index)
8104 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8111 * -----------------------------------------------------------------------
8112 * -----------------------------------------------------------------------
8113 * -----------------------------------------------------------------------
8117 * Ends the decompression procedures and possibiliy add data to be read after the
8120 opj_bool j2k_end_decompress(
8121 opj_j2k_v2_t *p_j2k,
8122 opj_stream_private_t *p_stream,
8123 opj_event_mgr_t * p_manager)
8129 * Reads a jpeg2000 codestream header structure.
8132 * @param p_stream the stream to read data from.
8133 * @param p_j2k the jpeg2000 codec.
8134 * @param p_manager the user event manager.
8136 * @return true if the box is valid.
8138 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8139 opj_j2k_v2_t* p_j2k,
8140 opj_image_t** p_image,
8141 struct opj_event_mgr* p_manager )
8144 assert(p_j2k != 00);
8145 assert(p_stream != 00);
8146 assert(p_manager != 00);
8148 /* create an empty image header */
8149 p_j2k->m_private_image = opj_image_create0();
8150 if (! p_j2k->m_private_image) {
8154 /* customization of the validation */
8155 j2k_setup_decoding_validation(p_j2k);
8157 /* validation of the parameters codec */
8158 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8159 opj_image_destroy(p_j2k->m_private_image);
8160 p_j2k->m_private_image = NULL;
8164 /* customization of the encoding */
8165 j2k_setup_header_reading(p_j2k);
8168 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8169 opj_image_destroy(p_j2k->m_private_image);
8170 p_j2k->m_private_image = NULL;
8174 *p_image = opj_image_create0();
8179 /* Copy codestream image information to the output image */
8180 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8187 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8189 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8192 assert(p_j2k != 00);
8194 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_read_header_procedure);
8196 /* DEVELOPER CORNER, add your custom procedures */
8197 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_copy_default_tcp_and_create_tcd);
8202 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8203 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8205 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8208 assert(p_j2k != 00);
8210 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_decoder);
8211 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_decoding_validation);
8212 /* DEVELOPER CORNER, add your custom validation procedure */
8218 * The mct encoding validation procedure.
8220 * @param p_j2k the jpeg2000 codec to validate.
8221 * @param p_stream the input stream to validate.
8222 * @param p_manager the user event manager.
8224 * @return true if the parameters are correct.
8226 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8227 opj_stream_private_t *p_stream,
8228 opj_event_mgr_t * p_manager )
8230 opj_bool l_is_valid = OPJ_TRUE;
8234 assert(p_j2k != 00);
8235 assert(p_stream != 00);
8236 assert(p_manager != 00);
8238 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8239 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8240 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8242 for (i=0;i<l_nb_tiles;++i) {
8243 if (l_tcp->mct == 2) {
8244 opj_tccp_t * l_tccp = l_tcp->tccps;
8245 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8247 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8248 l_is_valid &= ! (l_tccp->qmfbid & 1);
8260 * Builds the cp decoder parameters to use to decode tile.
8262 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8263 opj_stream_private_t *p_stream,
8264 opj_event_mgr_t * p_manager )
8266 /* add here initialization of cp
8267 copy paste of setup_decoder */
8272 * Builds the cp encoder parameters to use to encode tile.
8274 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8275 opj_stream_private_t *p_stream,
8276 opj_event_mgr_t * p_manager )
8278 /* add here initialization of cp
8279 copy paste of setup_encoder */
8284 * The default encoding validation procedure without any extension.
8286 * @param p_j2k the jpeg2000 codec to validate.
8287 * @param p_stream the input stream to validate.
8288 * @param p_manager the user event manager.
8290 * @return true if the parameters are correct.
8292 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8293 opj_stream_private_t *p_stream,
8294 opj_event_mgr_t * p_manager )
8296 opj_bool l_is_valid = OPJ_TRUE;
8299 assert(p_j2k != 00);
8300 assert(p_stream != 00);
8301 assert(p_manager != 00);
8303 /* STATE checking */
8304 /* make sure the state is at 0 */
8305 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8307 /* POINTER validation */
8308 /* make sure a p_j2k codec is present */
8309 l_is_valid &= (p_j2k->m_procedure_list != 00);
8310 /* make sure a validation list is present */
8311 l_is_valid &= (p_j2k->m_validation_list != 00);
8313 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8314 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8318 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8319 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8323 /* PARAMETER VALIDATION */
8328 * The default decoding validation procedure without any extension.
8330 * @param p_j2k the jpeg2000 codec to validate.
8331 * @param p_stream the input stream to validate.
8332 * @param p_manager the user event manager.
8334 * @return true if the parameters are correct.
8336 opj_bool j2k_decoding_validation (
8337 opj_j2k_v2_t *p_j2k,
8338 opj_stream_private_t *p_stream,
8339 opj_event_mgr_t * p_manager
8342 opj_bool l_is_valid = OPJ_TRUE;
8345 assert(p_j2k != 00);
8346 assert(p_stream != 00);
8347 assert(p_manager != 00);
8350 /* STATE checking */
8351 /* make sure the state is at 0 */
8353 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8355 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8357 /* POINTER validation */
8358 /* make sure a p_j2k codec is present */
8359 /* make sure a procedure list is present */
8360 l_is_valid &= (p_j2k->m_procedure_list != 00);
8361 /* make sure a validation list is present */
8362 l_is_valid &= (p_j2k->m_validation_list != 00);
8364 /* PARAMETER VALIDATION */
8368 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8369 struct opj_stream_private *p_stream,
8370 struct opj_event_mgr * p_manager)
8372 OPJ_UINT32 l_current_marker;
8373 OPJ_UINT32 l_marker_size;
8374 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8377 assert(p_stream != 00);
8378 assert(p_j2k != 00);
8379 assert(p_manager != 00);
8381 /* We enter in the main header */
8382 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8384 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8385 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
8386 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8390 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8391 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8392 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8396 /* Read 2 bytes as the new marker ID */
8397 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8399 /* Try to read until the SOT is detected */
8400 while (l_current_marker != J2K_MS_SOT) {
8402 /* Check if the current marker ID is valid */
8403 if (l_current_marker < 0xff00) {
8404 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8408 /* Get the marker handler from the marker ID */
8409 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8411 /* Manage case where marker is unknown */
8412 if (l_marker_handler->id == J2K_MS_UNK) {
8413 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8414 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8418 if (l_current_marker == J2K_MS_SOT)
8419 break; /* SOT marker is detected main header is completely read */
8420 else /* Get the marker handler from the marker ID */
8421 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8424 /* Check if the marker is known and if it is the right place to find it */
8425 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8426 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8430 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8431 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8432 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8436 /* read 2 bytes as the marker size */
8437 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8438 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8440 /* Check if the marker size is compatible with the header data size */
8441 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8442 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8443 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8444 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8447 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8450 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8451 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) {
8452 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8456 /* Read the marker segment with the correct marker handler */
8457 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8458 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8462 /* Add the marker to the codestream index*/
8463 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8464 l_marker_handler->id,
8465 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8466 l_marker_size + 4 );
8468 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8469 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8470 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8474 /* read 2 bytes as the new marker ID */
8475 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8478 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8480 /* Position of the last element if the main header */
8481 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8483 /* Next step: read a tile-part header */
8484 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8490 * Excutes the given procedures on the given codec.
8492 * @param p_procedure_list the list of procedures to execute
8493 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8494 * @param p_stream the stream to execute the procedures on.
8495 * @param p_manager the user manager.
8497 * @return true if all the procedures were successfully executed.
8499 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8500 opj_procedure_list_t * p_procedure_list,
8501 opj_stream_private_t *p_stream,
8502 opj_event_mgr_t * p_manager )
8504 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8505 opj_bool l_result = OPJ_TRUE;
8506 OPJ_UINT32 l_nb_proc, i;
8509 assert(p_procedure_list != 00);
8510 assert(p_j2k != 00);
8511 assert(p_stream != 00);
8512 assert(p_manager != 00);
8515 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8516 l_procedure = (opj_bool (**) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
8518 for (i=0;i<l_nb_proc;++i) {
8519 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8523 /* and clear the procedure list at the end.*/
8524 opj_procedure_list_clear(p_procedure_list);
8529 opj_bool j2k_copy_default_tcp_and_create_tcd
8531 opj_j2k_v2_t * p_j2k,
8532 opj_stream_private_t *p_stream,
8533 opj_event_mgr_t * p_manager
8536 opj_tcp_v2_t * l_tcp = 00;
8537 opj_tcp_v2_t * l_default_tcp = 00;
8538 OPJ_UINT32 l_nb_tiles;
8540 opj_tccp_t *l_current_tccp = 00;
8541 OPJ_UINT32 l_tccp_size;
8542 OPJ_UINT32 l_mct_size;
8543 opj_image_t * l_image;
8544 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8545 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8546 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8547 OPJ_UINT32 l_offset;
8550 assert(p_j2k != 00);
8551 assert(p_stream != 00);
8552 assert(p_manager != 00);
8554 l_image = p_j2k->m_private_image;
8555 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8556 l_tcp = p_j2k->m_cp.tcps;
8557 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8558 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8559 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8562 for (i=0; i<l_nb_tiles; ++i) {
8563 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8564 l_current_tccp = l_tcp->tccps;
8565 /*Copy default coding parameters into the current tile coding parameters*/
8566 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8567 /* Initialize some values of the current tile coding parameters*/
8569 l_tcp->ppt_data = 00;
8570 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8571 l_tcp->tccps = l_current_tccp;
8573 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8574 if (l_default_tcp->m_mct_decoding_matrix) {
8575 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8576 if (! l_tcp->m_mct_decoding_matrix ) {
8579 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8582 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8583 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8584 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8585 if (! l_tcp->m_mct_records) {
8588 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8590 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8591 l_src_mct_rec = l_default_tcp->m_mct_records;
8592 l_dest_mct_rec = l_tcp->m_mct_records;
8594 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8596 if (l_src_mct_rec->m_data) {
8598 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8599 if(! l_dest_mct_rec->m_data) {
8602 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8609 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8610 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8611 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8612 if (! l_tcp->m_mcc_records) {
8615 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8617 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8618 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8619 l_dest_mcc_rec = l_tcp->m_mcc_records;
8621 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8623 if (l_src_mcc_rec->m_decorrelation_array) {
8624 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8625 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8628 if (l_src_mcc_rec->m_offset_array) {
8629 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8630 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8637 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8638 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8640 /* Move to next tile cp*/
8644 /* Create the current tile decoder*/
8645 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8646 if (! p_j2k->m_tcd ) {
8650 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8651 tcd_destroy_v2(p_j2k->m_tcd);
8653 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8661 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8662 * with the marker value.
8663 * @param p_id Marker value to look up
8665 * @return the handler associated with the id.
8667 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8669 const opj_dec_memory_marker_handler_t *e;
8670 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8671 if (e->id == p_id) {
8672 break; /* we find a handler corresponding to the marker ID*/
8680 * Destroys a jpeg2000 codec.
8682 * @param p_j2k the jpeg20000 structure to destroy.
8684 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8690 if (p_j2k->m_is_decoder) {
8692 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8693 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8694 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8695 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8698 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8699 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8700 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8701 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8706 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8707 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8708 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8711 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8712 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8713 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8714 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8717 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8718 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8719 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8720 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8724 tcd_destroy_v2(p_j2k->m_tcd);
8726 j2k_cp_destroy(&(p_j2k->m_cp));
8727 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8729 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8730 p_j2k->m_procedure_list = 00;
8732 opj_procedure_list_destroy(p_j2k->m_validation_list);
8733 p_j2k->m_procedure_list = 00;
8735 j2k_destroy_cstr_index(p_j2k->cstr_index);
8736 p_j2k->cstr_index = NULL;
8738 opj_image_destroy(p_j2k->m_private_image);
8739 p_j2k->m_private_image = NULL;
8741 opj_image_destroy(p_j2k->m_output_image);
8742 p_j2k->m_output_image = NULL;
8747 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8751 if (p_cstr_ind->marker) {
8752 opj_free(p_cstr_ind->marker);
8753 p_cstr_ind->marker = NULL;
8756 if (p_cstr_ind->tile_index) {
8757 OPJ_UINT32 it_tile = 0;
8759 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8761 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8762 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8763 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8766 if(p_cstr_ind->tile_index[it_tile].tp_index){
8767 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8768 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8771 if(p_cstr_ind->tile_index[it_tile].marker){
8772 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8773 p_cstr_ind->tile_index[it_tile].marker = NULL;
8778 opj_free( p_cstr_ind->tile_index);
8779 p_cstr_ind->tile_index = NULL;
8782 opj_free(p_cstr_ind);
8789 * Destroys a tile coding parameter structure.
8791 * @param p_tcp the tile coding parameter to destroy.
8793 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8799 if (p_tcp->ppt_buffer != 00) {
8800 opj_free(p_tcp->ppt_buffer);
8801 p_tcp->ppt_buffer = 00;
8804 if (p_tcp->tccps != 00) {
8805 opj_free(p_tcp->tccps);
8809 if (p_tcp->m_mct_coding_matrix != 00) {
8810 opj_free(p_tcp->m_mct_coding_matrix);
8811 p_tcp->m_mct_coding_matrix = 00;
8814 if (p_tcp->m_mct_decoding_matrix != 00) {
8815 opj_free(p_tcp->m_mct_decoding_matrix);
8816 p_tcp->m_mct_decoding_matrix = 00;
8819 if (p_tcp->m_mcc_records) {
8820 opj_free(p_tcp->m_mcc_records);
8821 p_tcp->m_mcc_records = 00;
8822 p_tcp->m_nb_max_mcc_records = 0;
8823 p_tcp->m_nb_mcc_records = 0;
8826 if (p_tcp->m_mct_records) {
8827 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8830 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8831 if (l_mct_data->m_data) {
8832 opj_free(l_mct_data->m_data);
8833 l_mct_data->m_data = 00;
8839 opj_free(p_tcp->m_mct_records);
8840 p_tcp->m_mct_records = 00;
8843 if (p_tcp->mct_norms != 00) {
8844 opj_free(p_tcp->mct_norms);
8845 p_tcp->mct_norms = 00;
8848 j2k_tcp_data_destroy(p_tcp);
8853 * Destroys the data inside a tile coding parameter structure.
8855 * @param p_tcp the tile coding parameter which contain data to destroy.
8857 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
8859 if (p_tcp->m_data) {
8860 opj_free(p_tcp->m_data);
8861 p_tcp->m_data = NULL;
8862 p_tcp->m_data_size = 0;
8867 * Destroys a coding parameter structure.
8869 * @param p_cp the coding parameter to destroy.
8871 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
8873 OPJ_UINT32 l_nb_tiles;
8874 opj_tcp_v2_t * l_current_tile = 00;
8885 l_current_tile = p_cp->tcps;
8886 l_nb_tiles = p_cp->th * p_cp->tw;
8889 (i = 0; i < l_nb_tiles; ++i)
8891 j2k_tcp_destroy(l_current_tile);
8894 opj_free(p_cp->tcps);
8898 (p_cp->ppm_buffer != 00)
8900 opj_free(p_cp->ppm_buffer);
8901 p_cp->ppm_buffer = 00;
8904 (p_cp->comment != 00)
8906 opj_free(p_cp->comment);
8910 (! p_cp->m_is_decoder)
8913 (p_cp->m_specific_param.m_enc.m_matrice)
8915 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8916 p_cp->m_specific_param.m_enc.m_matrice = 00;
8924 * Reads a tile header.
8925 * @param p_j2k the jpeg2000 codec.
8926 * @param p_stream the stream to write data to.
8927 * @param p_manager the user event manager.
8929 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
8930 OPJ_UINT32 * p_tile_index,
8931 OPJ_UINT32 * p_data_size,
8932 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8933 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8934 OPJ_UINT32 * p_nb_comps,
8936 opj_stream_private_t *p_stream,
8937 opj_event_mgr_t * p_manager )
8939 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8940 OPJ_UINT32 l_marker_size;
8941 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8942 opj_tcp_v2_t * l_tcp = NULL;
8943 OPJ_UINT32 l_nb_tiles;
8946 assert(p_stream != 00);
8947 assert(p_j2k != 00);
8948 assert(p_manager != 00);
8950 /* Reach the End Of Codestream ?*/
8951 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
8952 l_current_marker = J2K_MS_EOC;
8954 /* We need to encounter a SOT marker (a new tile-part header) */
8955 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
8959 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
8960 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
8962 /* Try to read until the Start Of Data is detected */
8963 while (l_current_marker != J2K_MS_SOD) {
8965 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8966 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8967 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8971 /* Read 2 bytes from the buffer as the marker size */
8972 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8974 /* Why this condition? FIXME */
8975 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
8976 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8978 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8980 /* Get the marker handler from the marker ID */
8981 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8983 /* Check if the marker is known and if it is the right place to find it */
8984 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8985 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8988 /* FIXME manage case of unknown marker as in the main header ? */
8990 /* Check if the marker size is compatible with the header data size */
8991 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8992 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8993 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8994 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8997 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9000 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9001 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size) {
9002 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9006 /* Read the marker segment with the correct marker handler */
9007 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9008 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9012 /* Add the marker to the codestream index*/
9013 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9015 l_marker_handler->id,
9016 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9017 l_marker_size + 4 );
9019 /* Keep the position of the last SOT marker read */
9020 if ( l_marker_handler->id == J2K_MS_SOT ) {
9021 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9022 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9024 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9029 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9030 /* Skip the rest of the tile part header*/
9031 if (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) {
9032 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9035 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9038 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9039 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9040 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9043 /* Read 2 bytes from the buffer as the new marker ID */
9044 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9048 /* If we didn't skip data before, we need to read the SOD marker*/
9049 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9050 /* Try to read the SOD marker and skip data ? FIXME */
9051 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
9057 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9058 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9059 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9060 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9064 /* Read 2 bytes from buffer as the new marker ID */
9065 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9069 /* Indicate we will try to read a new tile-part header*/
9070 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9071 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9072 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9074 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9075 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9076 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9080 /* Read 2 bytes from buffer as the new marker ID */
9081 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9085 /* Current marker is the EOC marker ?*/
9086 if (l_current_marker == J2K_MS_EOC) {
9087 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9088 p_j2k->m_current_tile_number = 0;
9089 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9094 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9095 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9096 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9098 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9099 ++p_j2k->m_current_tile_number;
9103 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9104 *p_go_on = OPJ_FALSE;
9110 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9111 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9115 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9116 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9118 *p_tile_index = p_j2k->m_current_tile_number;
9119 *p_go_on = OPJ_TRUE;
9120 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9121 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9122 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9123 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9124 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9125 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9127 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9133 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9134 OPJ_UINT32 p_tile_index,
9136 OPJ_UINT32 p_data_size,
9137 opj_stream_private_t *p_stream,
9138 opj_event_mgr_t * p_manager )
9140 OPJ_UINT32 l_current_marker;
9141 OPJ_BYTE l_data [2];
9142 opj_tcp_v2_t * l_tcp;
9145 assert(p_stream != 00);
9146 assert(p_j2k != 00);
9147 assert(p_manager != 00);
9149 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9150 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9154 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9155 if (! l_tcp->m_data) {
9156 j2k_tcp_destroy(l_tcp);
9160 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9164 p_j2k->cstr_index) ) {
9165 j2k_tcp_destroy(l_tcp);
9166 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9170 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9174 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9175 * we destroy just the data which will be re-read in read_tile_header*/
9176 /*j2k_tcp_destroy(l_tcp);
9177 p_j2k->m_tcd->tcp = 0;*/
9178 j2k_tcp_data_destroy(l_tcp);
9180 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9181 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9183 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9184 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9185 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9189 opj_read_bytes(l_data,&l_current_marker,2);
9191 if (l_current_marker == J2K_MS_EOC) {
9192 p_j2k->m_current_tile_number = 0;
9193 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9195 else if (l_current_marker != J2K_MS_SOT)
9197 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9206 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9208 OPJ_UINT32 i,j,k = 0;
9209 OPJ_UINT32 l_width_src,l_height_src;
9210 OPJ_UINT32 l_width_dest,l_height_dest;
9211 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9212 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9213 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9214 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9215 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9217 opj_image_comp_t * l_img_comp_src = 00;
9218 opj_image_comp_t * l_img_comp_dest = 00;
9220 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9221 opj_image_t * l_image_src = 00;
9222 OPJ_UINT32 l_size_comp, l_remaining;
9223 OPJ_INT32 * l_dest_ptr;
9224 opj_tcd_resolution_v2_t* l_res= 00;
9226 l_tilec = p_tcd->tcd_image->tiles->comps;
9227 l_image_src = p_tcd->image;
9228 l_img_comp_src = l_image_src->comps;
9230 l_img_comp_dest = p_output_image->comps;
9232 for (i=0; i<l_image_src->numcomps; i++) {
9234 /* Allocate output component buffer if necessary */
9235 if (!l_img_comp_dest->data) {
9237 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9238 if (! l_img_comp_dest->data) {
9243 /* Copy info from decoded comp image to output image */
9244 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9247 /* Compute the precision of the output buffer */
9248 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9249 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9250 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9256 if (l_size_comp == 3) {
9261 /* Current tile component size*/
9263 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9264 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9267 l_width_src = (l_res->x1 - l_res->x0);
9268 l_height_src = (l_res->y1 - l_res->y0);
9270 /* Border of the current output component*/
9271 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9272 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9273 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9274 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9277 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9278 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9282 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9283 * of the input buffer (decoded tile component) which will be move
9284 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9285 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9286 * by this input area.
9288 if ( l_x0_dest < l_res->x0 ) {
9289 l_start_x_dest = l_res->x0 - l_x0_dest;
9290 l_offset_x0_src = 0;
9292 if ( l_x1_dest >= l_res->x1 ) {
9293 l_width_dest = l_width_src;
9294 l_offset_x1_src = 0;
9297 l_width_dest = l_x1_dest - l_res->x0 ;
9298 l_offset_x1_src = l_width_src - l_width_dest;
9302 l_start_x_dest = 0 ;
9303 l_offset_x0_src = l_x0_dest - l_res->x0;
9305 if ( l_x1_dest >= l_res->x1 ) {
9306 l_width_dest = l_width_src - l_offset_x0_src;
9307 l_offset_x1_src = 0;
9310 l_width_dest = l_img_comp_dest->w ;
9311 l_offset_x1_src = l_res->x1 - l_x1_dest;
9315 if ( l_y0_dest < l_res->y0 ) {
9316 l_start_y_dest = l_res->y0 - l_y0_dest;
9317 l_offset_y0_src = 0;
9319 if ( l_y1_dest >= l_res->y1 ) {
9320 l_height_dest = l_height_src;
9321 l_offset_y1_src = 0;
9324 l_height_dest = l_y1_dest - l_res->y0 ;
9325 l_offset_y1_src = l_height_src - l_height_dest;
9329 l_start_y_dest = 0 ;
9330 l_offset_y0_src = l_y0_dest - l_res->y0;
9332 if ( l_y1_dest >= l_res->y1 ) {
9333 l_height_dest = l_height_src - l_offset_y0_src;
9334 l_offset_y1_src = 0;
9337 l_height_dest = l_img_comp_dest->h ;
9338 l_offset_y1_src = l_res->y1 - l_y1_dest;
9342 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9347 /* Compute the input buffer offset */
9348 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9349 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9350 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9352 /* Compute the output buffer offset */
9353 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9354 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9356 /* Move the output buffer to the first place where we will write*/
9357 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9360 fprintf(stdout, "COMPO[%d]:\n",i);
9361 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9362 "\t tile offset:%d, %d, %d, %d\n"
9363 "\t buffer offset: %d; %d, %d\n",
9364 l_res->x0, l_res->y0, l_width_src, l_height_src,
9365 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9366 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9368 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9369 "\t start offset: %d, line offset= %d\n",
9370 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9374 switch (l_size_comp) {
9377 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9378 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9380 if (l_img_comp_src->sgnd) {
9381 for (j = 0 ; j < l_height_dest ; ++j) {
9382 for ( k = 0 ; k < l_width_dest ; ++k) {
9383 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9386 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9387 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9391 for ( j = 0 ; j < l_height_dest ; ++j ) {
9392 for ( k = 0 ; k < l_width_dest ; ++k) {
9393 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9396 l_dest_ptr+= l_line_offset_dest;
9397 l_src_ptr += l_line_offset_src;
9401 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9402 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9407 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9408 l_src_ptr += l_start_offset_src;
9410 if (l_img_comp_src->sgnd) {
9411 for (j=0;j<l_height_dest;++j) {
9412 for (k=0;k<l_width_dest;++k) {
9413 *(l_dest_ptr++) = *(l_src_ptr++);
9416 l_dest_ptr+= l_line_offset_dest;
9417 l_src_ptr += l_line_offset_src ;
9421 for (j=0;j<l_height_dest;++j) {
9422 for (k=0;k<l_width_dest;++k) {
9423 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9426 l_dest_ptr+= l_line_offset_dest;
9427 l_src_ptr += l_line_offset_src ;
9431 l_src_ptr += l_end_offset_src;
9432 p_data = (OPJ_BYTE*) l_src_ptr;
9437 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9438 l_src_ptr += l_start_offset_src;
9440 for (j=0;j<l_height_dest;++j) {
9441 for (k=0;k<l_width_dest;++k) {
9442 *(l_dest_ptr++) = (*(l_src_ptr++));
9445 l_dest_ptr+= l_line_offset_dest;
9446 l_src_ptr += l_line_offset_src ;
9449 l_src_ptr += l_end_offset_src;
9450 p_data = (OPJ_BYTE*) l_src_ptr;
9464 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9466 * @param p_j2k the jpeg2000 codec.
9467 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9468 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9469 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9470 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9471 * @param p_manager the user event manager
9473 * @return true if the area could be set.
9475 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9476 opj_image_t* p_image,
9477 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9478 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9479 struct opj_event_mgr * p_manager )
9481 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9482 opj_image_t * l_image = p_j2k->m_private_image;
9485 OPJ_INT32 l_comp_x1, l_comp_y1;
9486 opj_image_comp_t* l_img_comp = NULL;
9488 /* Check if we are read the main header */
9489 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9490 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9494 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9495 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9497 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9498 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9499 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9500 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9506 /* Check if the positions provided by the user are correct */
9509 if (p_start_x > l_image->x1 ) {
9510 opj_event_msg_v2(p_manager, EVT_ERROR,
9511 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9512 p_start_x, l_image->x1);
9515 else if (p_start_x < l_image->x0){
9516 opj_event_msg_v2(p_manager, EVT_WARNING,
9517 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9518 p_start_x, l_image->x0);
9519 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9520 p_image->x0 = l_image->x0;
9523 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9524 p_image->x0 = p_start_x;
9528 if (p_start_y > l_image->y1){
9529 opj_event_msg_v2(p_manager, EVT_ERROR,
9530 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9531 p_start_y, l_image->y1);
9534 else if (p_start_y < l_image->y0){
9535 opj_event_msg_v2(p_manager, EVT_WARNING,
9536 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9537 p_start_y, l_image->y0);
9538 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9539 p_image->y0 = l_image->y0;
9542 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9543 p_image->y0 = p_start_y;
9547 if (p_end_x < l_image->x0) {
9548 opj_event_msg_v2(p_manager, EVT_ERROR,
9549 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9550 p_end_x, l_image->x0);
9553 else if (p_end_x > l_image->x1) {
9554 opj_event_msg_v2(p_manager, EVT_WARNING,
9555 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9556 p_end_x, l_image->x1);
9557 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9558 p_image->x1 = l_image->x1;
9561 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9562 p_image->x1 = p_end_x;
9566 if (p_end_y < l_image->y0) {
9567 opj_event_msg_v2(p_manager, EVT_ERROR,
9568 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9569 p_end_y, l_image->y0);
9572 if (p_end_y > l_image->y1){
9573 opj_event_msg_v2(p_manager, EVT_WARNING,
9574 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9575 p_end_y, l_image->y1);
9576 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9577 p_image->y1 = l_image->y1;
9580 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9581 p_image->y1 = p_end_y;
9585 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9587 l_img_comp = p_image->comps;
9588 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9592 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9593 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9594 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9595 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9597 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9598 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9600 opj_event_msg_v2(p_manager, EVT_ERROR,
9601 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9605 l_img_comp->w = l_w;
9607 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9608 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9610 opj_event_msg_v2(p_manager, EVT_ERROR,
9611 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9615 l_img_comp->h = l_h;
9620 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9621 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9628 /* ----------------------------------------------------------------------- */
9629 /* J2K / JPT decoder interface */
9630 /* ----------------------------------------------------------------------- */
9632 * Creates a J2K decompression structure.
9634 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9636 opj_j2k_v2_t* j2k_create_decompress_v2()
9638 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9642 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9644 l_j2k->m_is_decoder = 1;
9645 l_j2k->m_cp.m_is_decoder = 1;
9647 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9648 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9652 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9654 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9655 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9660 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9662 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9664 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9666 /* codestream index creation */
9667 l_j2k->cstr_index = j2k_create_cstr_index();
9669 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9670 if (!l_j2k->cstr_index){
9675 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9678 /* validation list creation */
9679 l_j2k->m_validation_list = opj_procedure_list_create();
9680 if (! l_j2k->m_validation_list) {
9685 /* execution list creation */
9686 l_j2k->m_procedure_list = opj_procedure_list_create();
9687 if (! l_j2k->m_procedure_list) {
9696 opj_codestream_index_t* j2k_create_cstr_index(void)
9698 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9699 opj_calloc(1,sizeof(opj_codestream_index_t));
9703 cstr_index->maxmarknum = 100;
9704 cstr_index->marknum = 0;
9705 cstr_index->marker = (opj_marker_info_t*)
9706 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9707 if (!cstr_index-> marker)
9710 cstr_index->tile_index = NULL;
9717 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9719 * @param p_tile_no the tile index.
9720 * @param p_comp_no the component being outputted.
9721 * @param p_j2k the J2K codec.
9723 * @return the number of bytes taken by the SPCod element.
9725 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9726 OPJ_UINT32 p_tile_no,
9727 OPJ_UINT32 p_comp_no )
9729 opj_cp_v2_t *l_cp = 00;
9730 opj_tcp_v2_t *l_tcp = 00;
9731 opj_tccp_t *l_tccp = 00;
9734 assert(p_j2k != 00);
9736 l_cp = &(p_j2k->m_cp);
9737 l_tcp = &l_cp->tcps[p_tile_no];
9738 l_tccp = &l_tcp->tccps[p_comp_no];
9740 /* preconditions again */
9741 assert(p_tile_no < (l_cp->tw * l_cp->th));
9742 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9744 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9745 return 5 + l_tccp->numresolutions;
9753 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9755 * @param p_comp_no the component number to output.
9756 * @param p_stream the stream to write data to.
9757 * @param p_j2k J2K codec.
9758 * @param p_manager the user event manager.
9761 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9762 OPJ_UINT32 p_tile_no,
9763 OPJ_UINT32 p_comp_no,
9765 OPJ_UINT32 * p_header_size,
9766 struct opj_event_mgr * p_manager )
9769 opj_cp_v2_t *l_cp = 00;
9770 opj_tcp_v2_t *l_tcp = 00;
9771 opj_tccp_t *l_tccp = 00;
9774 assert(p_j2k != 00);
9775 assert(p_header_size != 00);
9776 assert(p_manager != 00);
9777 assert(p_data != 00);
9779 l_cp = &(p_j2k->m_cp);
9780 l_tcp = &l_cp->tcps[p_tile_no];
9781 l_tccp = &l_tcp->tccps[p_comp_no];
9783 /* preconditions again */
9784 assert(p_tile_no < (l_cp->tw * l_cp->th));
9785 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9787 if (*p_header_size < 5) {
9788 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9792 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9795 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9798 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9801 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9804 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9807 *p_header_size = *p_header_size - 5;
9809 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9811 if (*p_header_size < l_tccp->numresolutions) {
9812 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9816 for (i = 0; i < l_tccp->numresolutions; ++i) {
9817 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9821 *p_header_size = *p_header_size - l_tccp->numresolutions;
9828 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9829 * @param p_header_data the data contained in the COM box.
9830 * @param p_j2k the jpeg2000 codec.
9831 * @param p_header_size the size of the data contained in the COM marker.
9832 * @param p_manager the user event manager.
9834 opj_bool j2k_read_SPCod_SPCoc(
9835 opj_j2k_v2_t *p_j2k,
9837 OPJ_BYTE * p_header_data,
9838 OPJ_UINT32 * p_header_size,
9839 struct opj_event_mgr * p_manager
9842 OPJ_UINT32 i, l_tmp;
9843 opj_cp_v2_t *l_cp = NULL;
9844 opj_tcp_v2_t *l_tcp = NULL;
9845 opj_tccp_t *l_tccp = NULL;
9846 OPJ_BYTE * l_current_ptr = NULL;
9849 assert(p_j2k != 00);
9850 assert(p_manager != 00);
9851 assert(p_header_data != 00);
9853 l_cp = &(p_j2k->m_cp);
9854 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9855 &l_cp->tcps[p_j2k->m_current_tile_number] :
9856 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9858 /* precondition again */
9859 assert(compno < p_j2k->m_private_image->numcomps);
9861 l_tccp = &l_tcp->tccps[compno];
9862 l_current_ptr = p_header_data;
9864 /* make sure room is sufficient */
9865 if (*p_header_size < 5) {
9866 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9870 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
9871 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
9874 /* If user wants to remove more resolutions than the codestream contains, return error */
9875 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9876 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
9877 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
9878 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9882 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
9886 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
9890 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
9893 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
9896 *p_header_size = *p_header_size - 5;
9898 /* use custom precinct size ? */
9899 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9900 if (*p_header_size < l_tccp->numresolutions) {
9901 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9905 for (i = 0; i < l_tccp->numresolutions; ++i) {
9906 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
9908 l_tccp->prcw[i] = l_tmp & 0xf;
9909 l_tccp->prch[i] = l_tmp >> 4;
9912 *p_header_size = *p_header_size - l_tccp->numresolutions;
9915 /* set default size for the precinct width and height */
9916 for (i = 0; i < l_tccp->numresolutions; ++i) {
9917 l_tccp->prcw[i] = 15;
9918 l_tccp->prch[i] = 15;
9922 #ifdef WIP_REMOVE_MSD
9924 if (p_j2k->cstr_info && compno == 0) {
9925 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9927 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
9928 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
9929 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
9930 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
9931 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
9934 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
9935 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
9944 * Copies the tile component parameters of all the component from the first tile component.
9946 * @param p_j2k the J2k codec.
9948 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
9952 opj_cp_v2_t *l_cp = NULL;
9953 opj_tcp_v2_t *l_tcp = NULL;
9954 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
9955 OPJ_UINT32 l_prc_size;
9958 assert(p_j2k != 00);
9960 l_cp = &(p_j2k->m_cp);
9961 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
9962 &l_cp->tcps[p_j2k->m_current_tile_number] :
9963 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9965 l_ref_tccp = &l_tcp->tccps[0];
9966 l_copied_tccp = l_ref_tccp + 1;
9967 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
9969 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
9970 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9971 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9972 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9973 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9974 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9975 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
9976 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
9982 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
9984 * @param p_tile_no the tile index.
9985 * @param p_comp_no the component being outputted.
9986 * @param p_j2k the J2K codec.
9988 * @return the number of bytes taken by the SPCod element.
9990 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
9991 OPJ_UINT32 p_tile_no,
9992 OPJ_UINT32 p_comp_no )
9994 OPJ_UINT32 l_num_bands;
9996 opj_cp_v2_t *l_cp = 00;
9997 opj_tcp_v2_t *l_tcp = 00;
9998 opj_tccp_t *l_tccp = 00;
10000 /* preconditions */
10001 assert(p_j2k != 00);
10003 l_cp = &(p_j2k->m_cp);
10004 l_tcp = &l_cp->tcps[p_tile_no];
10005 l_tccp = &l_tcp->tccps[p_comp_no];
10007 /* preconditions again */
10008 assert(p_tile_no < l_cp->tw * l_cp->th);
10009 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10011 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10013 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10014 return 1 + l_num_bands;
10017 return 1 + 2*l_num_bands;
10022 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10024 * @param p_tile_no the tile to output.
10025 * @param p_comp_no the component number to output.
10026 * @param p_data the data buffer.
10027 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10028 * @param p_j2k J2K codec.
10029 * @param p_manager the user event manager.
10032 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10033 OPJ_UINT32 p_tile_no,
10034 OPJ_UINT32 p_comp_no,
10036 OPJ_UINT32 * p_header_size,
10037 struct opj_event_mgr * p_manager )
10039 OPJ_UINT32 l_header_size;
10040 OPJ_UINT32 l_band_no, l_num_bands;
10041 OPJ_UINT32 l_expn,l_mant;
10043 opj_cp_v2_t *l_cp = 00;
10044 opj_tcp_v2_t *l_tcp = 00;
10045 opj_tccp_t *l_tccp = 00;
10047 /* preconditions */
10048 assert(p_j2k != 00);
10049 assert(p_header_size != 00);
10050 assert(p_manager != 00);
10051 assert(p_data != 00);
10053 l_cp = &(p_j2k->m_cp);
10054 l_tcp = &l_cp->tcps[p_tile_no];
10055 l_tccp = &l_tcp->tccps[p_comp_no];
10057 /* preconditions again */
10058 assert(p_tile_no < l_cp->tw * l_cp->th);
10059 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10061 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10063 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10064 l_header_size = 1 + l_num_bands;
10066 if (*p_header_size < l_header_size) {
10067 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10071 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10074 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10075 l_expn = l_tccp->stepsizes[l_band_no].expn;
10076 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10081 l_header_size = 1 + 2*l_num_bands;
10083 if (*p_header_size < l_header_size) {
10084 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10088 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10091 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10092 l_expn = l_tccp->stepsizes[l_band_no].expn;
10093 l_mant = l_tccp->stepsizes[l_band_no].mant;
10095 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10100 *p_header_size = *p_header_size - l_header_size;
10106 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10108 * @param p_comp_no the component being targeted.
10109 * @param p_header_data the data contained in the COM box.
10110 * @param p_j2k the jpeg2000 codec.
10111 * @param p_header_size the size of the data contained in the COM marker.
10112 * @param p_manager the user event manager.
10114 opj_bool j2k_read_SQcd_SQcc(
10115 opj_j2k_v2_t *p_j2k,
10116 OPJ_UINT32 p_comp_no,
10117 OPJ_BYTE* p_header_data,
10118 OPJ_UINT32 * p_header_size,
10119 struct opj_event_mgr * p_manager
10123 OPJ_UINT32 l_band_no;
10124 opj_cp_v2_t *l_cp = 00;
10125 opj_tcp_v2_t *l_tcp = 00;
10126 opj_tccp_t *l_tccp = 00;
10127 OPJ_BYTE * l_current_ptr = 00;
10128 OPJ_UINT32 l_tmp, l_num_band;
10131 assert(p_j2k != 00);
10132 assert(p_manager != 00);
10133 assert(p_header_data != 00);
10135 l_cp = &(p_j2k->m_cp);
10136 /* come from tile part header or main header ?*/
10137 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10138 &l_cp->tcps[p_j2k->m_current_tile_number] :
10139 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10141 /* precondition again*/
10142 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10144 l_tccp = &l_tcp->tccps[p_comp_no];
10145 l_current_ptr = p_header_data;
10147 if (*p_header_size < 1) {
10148 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10151 *p_header_size -= 1;
10153 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10156 l_tccp->qntsty = l_tmp & 0x1f;
10157 l_tccp->numgbits = l_tmp >> 5;
10158 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10162 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10164 (*p_header_size) / 2;
10166 if( l_num_band > J2K_MAXBANDS ) {
10167 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10168 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10169 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10170 /*return OPJ_FALSE;*/
10175 if (l_cp->correct) {
10177 /* if JPWL is on, we check whether there are too many subbands */
10178 if ((l_num_band < 0) || (l_num_band >= J2K_MAXBANDS)) {
10179 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10180 "JPWL: bad number of subbands in Sqcx (%d)\n",
10182 if (!JPWL_ASSUME) {
10183 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10186 /* we try to correct */
10188 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10189 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10194 #endif /* USE_JPWL */
10196 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10197 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10198 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10200 if (l_band_no < J2K_MAXBANDS){
10201 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10202 l_tccp->stepsizes[l_band_no].mant = 0;
10205 *p_header_size = *p_header_size - l_num_band;
10208 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10209 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10211 if (l_band_no < J2K_MAXBANDS){
10212 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10213 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10216 *p_header_size = *p_header_size - 2*l_num_band;
10219 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10220 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10221 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10222 l_tccp->stepsizes[l_band_no].expn =
10223 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10224 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10225 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10233 * Copies the tile component parameters of all the component from the first tile component.
10235 * @param p_j2k the J2k codec.
10237 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10240 opj_cp_v2_t *l_cp = NULL;
10241 opj_tcp_v2_t *l_tcp = NULL;
10242 opj_tccp_t *l_ref_tccp = NULL;
10243 opj_tccp_t *l_copied_tccp = NULL;
10246 /* preconditions */
10247 assert(p_j2k != 00);
10249 l_cp = &(p_j2k->m_cp);
10250 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10251 &l_cp->tcps[p_j2k->m_current_tile_number] :
10252 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10254 l_ref_tccp = &l_tcp->tccps[0];
10255 l_copied_tccp = l_ref_tccp + 1;
10256 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10258 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10259 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10260 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10261 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10267 * Dump some elements from the J2K decompression structure .
10269 *@param p_j2k the jpeg2000 codec.
10270 *@param flag flag to describe what elments are dump.
10271 *@param out_stream output stream where dump the elements.
10274 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10276 /* Check if the flag is compatible with j2k file*/
10277 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10278 fprintf(out_stream, "Wrong flag\n");
10282 /* Dump the image_header */
10283 if (flag & OPJ_IMG_INFO){
10284 if (p_j2k->m_private_image)
10285 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10288 /* Dump the codestream info from main header */
10289 if (flag & OPJ_J2K_MH_INFO){
10290 j2k_dump_MH_info(p_j2k, out_stream);
10294 /* Dump the codestream info of the current tile */
10295 if (flag & OPJ_J2K_TH_INFO){
10299 /* Dump the codestream index from main header */
10300 if (flag & OPJ_J2K_MH_IND){
10301 j2k_dump_MH_index(p_j2k, out_stream);
10304 /* Dump the codestream index of the current tile */
10305 if (flag & OPJ_J2K_TH_IND){
10312 * Dump index elements of the codestream extract from the main header.
10314 *@param p_j2k the jpeg2000 codec.
10315 *@param out_stream output stream where dump the elements.
10318 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10320 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10321 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10323 fprintf(out_stream, "Codestream index from main header: {\n");
10325 fprintf(out_stream, "\t Main header start position=%" OPJ_OFF_F "d\n"
10326 "\t Main header end position=%" OPJ_OFF_F "d\n",
10327 cstr_index->main_head_start, cstr_index->main_head_end);
10329 fprintf(out_stream, "\t Marker list: {\n");
10331 if (cstr_index->marker){
10332 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10333 fprintf(out_stream, "\t\t type=%#x, pos=%" OPJ_OFF_F "d, len=%d\n",
10334 cstr_index->marker[it_marker].type,
10335 cstr_index->marker[it_marker].pos,
10336 cstr_index->marker[it_marker].len );
10340 fprintf(out_stream, "\t }\n");
10343 if (cstr_index->tile_index){
10344 fprintf(out_stream, "\t Tile index: {\n");
10346 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10347 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10349 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10351 if (cstr_index->tile_index[it_tile].tp_index){
10352 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10353 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" OPJ_OFF_F "d, end_header=%" OPJ_OFF_F "d, end_pos=%" OPJ_OFF_F "d.\n",
10355 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10356 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10357 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10361 if (cstr_index->tile_index[it_tile].marker){
10362 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10363 fprintf(out_stream, "\t\t type=%#x, pos=%" OPJ_OFF_F "d, len=%d\n",
10364 cstr_index->tile_index[it_tile].marker[it_marker].type,
10365 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10366 cstr_index->tile_index[it_tile].marker[it_marker].len );
10370 fprintf(out_stream,"\t }\n");
10373 fprintf(out_stream,"}\n");
10378 * Dump info elements of the codestream extract from the main header.
10380 *@param p_j2k the jpeg2000 codec.
10381 *@param out_stream output stream where dump the elements.
10384 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10386 opj_tcp_v2_t * l_default_tile=NULL;
10388 fprintf(out_stream, "Codestream info from main header: {\n");
10390 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10391 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10392 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10394 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10395 if (l_default_tile)
10398 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10400 fprintf(out_stream, "\t default tile {\n");
10401 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10402 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10403 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10404 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10406 for (compno = 0; compno < numcomps; compno++) {
10407 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10408 OPJ_INT32 resno, bandno, numbands;
10411 fprintf(out_stream, "\t\t comp %d {\n", compno);
10412 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10413 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10414 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10415 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10416 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10417 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10419 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10420 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10421 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10423 fprintf(out_stream, "\n");
10425 /* quantization style*/
10426 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10427 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10428 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10429 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10430 for (bandno = 0; bandno < numbands; bandno++) {
10431 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10432 l_tccp->stepsizes[bandno].expn);
10434 fprintf(out_stream, "\n");
10437 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10439 fprintf(out_stream, "\t\t }\n");
10440 } /*end of component of default tile*/
10441 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10445 fprintf(out_stream, "}\n");
10450 * Dump an image header structure.
10452 *@param img_header the image header to dump.
10453 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10454 *@param out_stream output stream where dump the elements.
10456 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10460 if (dev_dump_flag){
10461 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10465 fprintf(out_stream, "Image info {\n");
10466 tab[0] = '\t';tab[1] = '\0';
10469 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10470 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10471 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10473 if (img_header->comps){
10475 for (compno = 0; compno < img_header->numcomps; compno++) {
10476 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10477 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10478 fprintf(out_stream,"%s}\n",tab);
10482 fprintf(out_stream, "}\n");
10486 * Dump a component image header structure.
10488 *@param comp_header the component image header to dump.
10489 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10490 *@param out_stream output stream where dump the elements.
10492 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10496 if (dev_dump_flag){
10497 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10500 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10503 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10504 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10505 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10508 fprintf(out_stream, "}\n");
10513 * Get the codestream info from a JPEG2000 codec.
10515 *@param p_j2k the component image header to dump.
10517 *@return the codestream information extract from the jpg2000 codec
10519 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10522 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10523 opj_tcp_v2_t *l_default_tile;
10524 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10526 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10528 cstr_info->tx0 = p_j2k->m_cp.tx0;
10529 cstr_info->ty0 = p_j2k->m_cp.ty0;
10530 cstr_info->tdx = p_j2k->m_cp.tdx;
10531 cstr_info->tdy = p_j2k->m_cp.tdy;
10532 cstr_info->tw = p_j2k->m_cp.tw;
10533 cstr_info->th = p_j2k->m_cp.th;
10535 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10537 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10539 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10540 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10541 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10542 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10544 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10546 for (compno = 0; compno < numcomps; compno++) {
10547 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10548 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10549 OPJ_INT32 bandno, numbands;
10552 l_tccp_info->csty = l_tccp->csty;
10553 l_tccp_info->numresolutions = l_tccp->numresolutions;
10554 l_tccp_info->cblkw = l_tccp->cblkw;
10555 l_tccp_info->cblkh = l_tccp->cblkh;
10556 l_tccp_info->cblksty = l_tccp->cblksty;
10557 l_tccp_info->qmfbid = l_tccp->qmfbid;
10558 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10560 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10561 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10564 /* quantization style*/
10565 l_tccp_info->qntsty = l_tccp->qntsty;
10566 l_tccp_info->numgbits = l_tccp->numgbits;
10568 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10569 if (numbands < J2K_MAXBANDS) {
10570 for (bandno = 0; bandno < numbands; bandno++) {
10571 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10572 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10577 l_tccp_info->roishift = l_tccp->roishift;
10585 * Get the codestream index from a JPEG2000 codec.
10587 *@param p_j2k the component image header to dump.
10589 *@return the codestream index extract from the jpg2000 codec
10591 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10593 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10594 opj_calloc(1,sizeof(opj_codestream_index_t));
10598 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10599 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10600 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10602 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10603 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10604 if (!l_cstr_index->marker){
10605 opj_free( l_cstr_index);
10609 if (p_j2k->cstr_index->marker)
10610 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10612 opj_free(l_cstr_index->marker);
10613 l_cstr_index->marker = NULL;
10616 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10617 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10618 if (!l_cstr_index->tile_index){
10619 opj_free( l_cstr_index->marker);
10620 opj_free( l_cstr_index);
10624 if (!p_j2k->cstr_index->tile_index){
10625 opj_free(l_cstr_index->tile_index);
10626 l_cstr_index->tile_index = NULL;
10629 OPJ_UINT32 it_tile = 0;
10630 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10633 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10635 l_cstr_index->tile_index[it_tile].marker =
10636 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10638 if (!l_cstr_index->tile_index[it_tile].marker) {
10639 OPJ_UINT32 it_tile_free;
10641 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10642 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10645 opj_free( l_cstr_index->tile_index);
10646 opj_free( l_cstr_index->marker);
10647 opj_free( l_cstr_index);
10651 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10652 memcpy( l_cstr_index->tile_index[it_tile].marker,
10653 p_j2k->cstr_index->tile_index[it_tile].marker,
10654 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10656 opj_free(l_cstr_index->tile_index[it_tile].marker);
10657 l_cstr_index->tile_index[it_tile].marker = NULL;
10660 /* Tile part index*/
10661 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10663 l_cstr_index->tile_index[it_tile].tp_index =
10664 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10666 if(!l_cstr_index->tile_index[it_tile].tp_index){
10667 OPJ_UINT32 it_tile_free;
10669 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10670 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10671 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10674 opj_free( l_cstr_index->tile_index);
10675 opj_free( l_cstr_index->marker);
10676 opj_free( l_cstr_index);
10680 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10681 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10682 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10683 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10686 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10687 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10690 /* Packet index (NOT USED)*/
10691 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10692 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10697 return l_cstr_index;
10700 opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10702 OPJ_UINT32 it_tile=0;
10704 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10705 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10706 if (!p_j2k->cstr_index->tile_index)
10709 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10710 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10711 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10712 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10713 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10714 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10724 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10725 opj_stream_private_t *p_stream,
10726 opj_event_mgr_t * p_manager)
10728 opj_bool l_go_on = OPJ_TRUE;
10729 OPJ_UINT32 l_current_tile_no;
10730 OPJ_UINT32 l_data_size,l_max_data_size;
10731 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10732 OPJ_UINT32 l_nb_comps;
10733 OPJ_BYTE * l_current_data;
10735 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10736 if (! l_current_data) {
10739 l_max_data_size = 1000;
10741 /*Allocate and initialize some elements of codestrem index*/
10742 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
10743 opj_free(l_current_data);
10748 if (! j2k_read_tile_header( p_j2k,
10749 &l_current_tile_no,
10751 &l_tile_x0, &l_tile_y0,
10752 &l_tile_x1, &l_tile_y1,
10757 opj_free(l_current_data);
10765 if (l_data_size > l_max_data_size) {
10766 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10767 if (! l_current_data) {
10768 opj_free(l_current_data);
10772 l_max_data_size = l_data_size;
10775 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10776 opj_free(l_current_data);
10779 opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
10781 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10782 opj_free(l_current_data);
10785 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10789 opj_free(l_current_data);
10795 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10797 void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10800 assert(p_j2k != 00);
10802 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_tiles);
10803 /* DEVELOPER CORNER, add your custom procedures */
10808 * Read and decode one tile.
10810 opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10811 opj_stream_private_t *p_stream,
10812 opj_event_mgr_t * p_manager)
10814 opj_bool l_go_on = OPJ_TRUE;
10815 OPJ_UINT32 l_current_tile_no;
10816 OPJ_UINT32 l_tile_no_to_dec;
10817 OPJ_UINT32 l_data_size,l_max_data_size;
10818 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10819 OPJ_UINT32 l_nb_comps;
10820 OPJ_BYTE * l_current_data;
10822 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10823 if (! l_current_data) {
10826 l_max_data_size = 1000;
10828 /*Allocate and initialize some elements of codestrem index if not already done*/
10829 if( !p_j2k->cstr_index->tile_index)
10831 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
10832 opj_free(l_current_data);
10836 /* Move into the codestream to the first SOT used to decode the desired tile */
10837 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10838 if (p_j2k->cstr_index->tile_index)
10839 if(p_j2k->cstr_index->tile_index->tp_index)
10841 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10842 /* the index for this tile has not been built,
10843 * so move to the last SOT read */
10844 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
10845 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10850 if (opj_stream_read_seek(p_stream, p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos+2, p_manager)) {
10851 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10855 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10856 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
10857 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10861 if (! j2k_read_tile_header( p_j2k,
10862 &l_current_tile_no,
10864 &l_tile_x0, &l_tile_y0,
10865 &l_tile_x1, &l_tile_y1,
10870 opj_free(l_current_data);
10879 if (l_data_size > l_max_data_size) {
10880 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10881 if (! l_current_data) {
10882 opj_free(l_current_data);
10886 l_max_data_size = l_data_size;
10891 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10892 opj_free(l_current_data);
10895 opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
10897 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10898 opj_free(l_current_data);
10901 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
10903 if(l_current_tile_no == l_tile_no_to_dec)
10905 /* move into the codestream to the the first SOT (FIXME or not move?)*/
10906 if (opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) {
10907 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10913 opj_event_msg_v2(p_manager, EVT_WARNING, "Tile read, decode and updated is not the desired (%d vs %d).\n", l_current_tile_no, l_tile_no_to_dec);
10918 opj_free(l_current_data);
10925 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10927 void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
10930 assert(p_j2k != 00);
10932 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_one_tile);
10933 /* DEVELOPER CORNER, add your custom procedures */
10939 * Decodes the tiles of the stream.
10941 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
10942 opj_stream_private_t * p_stream,
10943 opj_image_t * p_image,
10944 opj_event_mgr_t * p_manager)
10951 p_j2k->m_output_image = opj_image_create0();
10952 if (! (p_j2k->m_output_image)) {
10955 opj_copy_image_header(p_image, p_j2k->m_output_image);
10957 /* customization of the decoding */
10958 j2k_setup_decoding(p_j2k);
10960 /* Decode the codestream */
10961 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10962 opj_image_destroy(p_j2k->m_private_image);
10963 p_j2k->m_private_image = NULL;
10967 /* Move data and copy one information from codec to output image*/
10968 for (compno = 0; compno < p_image->numcomps; compno++) {
10969 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10970 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10971 p_j2k->m_output_image->comps[compno].data = NULL;
10979 * Get the decoded tile.
10981 * @param p_j2k the jpeg2000 codestream codec.
10982 * @param p_stream input_stream
10983 * @param p_image output image. .
10984 * @param p_manager the user event manager
10985 * @param tile_index index of the tile we want decode
10987 * @return true if succeed.
10989 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
10990 opj_stream_private_t *p_stream,
10991 opj_image_t* p_image,
10992 struct opj_event_mgr * p_manager,
10993 OPJ_UINT32 tile_index )
10996 OPJ_UINT32 l_tile_x, l_tile_y;
10997 opj_image_comp_t* l_img_comp;
11000 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11004 if ( (tile_index < 0) && (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11005 opj_event_msg_v2(p_manager, EVT_ERROR, "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1);
11009 /* Compute the dimension of the desired tile*/
11010 l_tile_x = tile_index % p_j2k->m_cp.tw;
11011 l_tile_y = tile_index / p_j2k->m_cp.tw;
11013 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11014 if (p_image->x0 < p_j2k->m_private_image->x0)
11015 p_image->x0 = p_j2k->m_private_image->x0;
11016 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11017 if (p_image->x1 > p_j2k->m_private_image->x1)
11018 p_image->x1 = p_j2k->m_private_image->x1;
11020 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11021 if (p_image->y0 < p_j2k->m_private_image->y0)
11022 p_image->y0 = p_j2k->m_private_image->y0;
11023 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11024 if (p_image->y1 > p_j2k->m_private_image->y1)
11025 p_image->y1 = p_j2k->m_private_image->y1;
11027 l_img_comp = p_image->comps;
11028 for (compno=0; compno < p_image->numcomps; ++compno)
11030 OPJ_INT32 l_comp_x1, l_comp_y1;
11032 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11034 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11035 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11036 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11037 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11039 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11040 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11045 /* Destroy the previous output image*/
11046 if (p_j2k->m_output_image)
11047 opj_image_destroy(p_j2k->m_output_image);
11049 /* Create the ouput image from the information previously computed*/
11050 p_j2k->m_output_image = opj_image_create0();
11051 if (! (p_j2k->m_output_image)) {
11054 opj_copy_image_header(p_image, p_j2k->m_output_image);
11056 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11058 /* customization of the decoding */
11059 j2k_setup_decoding_tile(p_j2k);
11061 /* Decode the codestream */
11062 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11063 opj_image_destroy(p_j2k->m_private_image);
11064 p_j2k->m_private_image = NULL;
11068 /* Move data and copy one information from codec to output image*/
11069 for (compno = 0; compno < p_image->numcomps; compno++) {
11070 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11072 if (p_image->comps[compno].data)
11073 opj_free(p_image->comps[compno].data);
11075 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11077 p_j2k->m_output_image->comps[compno].data = NULL;
11083 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11085 OPJ_UINT32 it_comp;
11087 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11089 if (p_j2k->m_private_image) {
11090 if (p_j2k->m_private_image->comps) {
11091 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11092 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11093 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11094 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11095 if ( res_factor >= max_res){
11096 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11099 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11112 * Encodes all the tiles in a row.
11114 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11115 opj_stream_private_t *p_stream,
11116 opj_event_mgr_t * p_manager )
11119 OPJ_UINT32 l_nb_tiles;
11120 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11121 OPJ_BYTE * l_current_data;
11123 /* preconditions */
11124 assert(p_j2k != 00);
11125 assert(p_stream != 00);
11126 assert(p_manager != 00);
11128 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11129 if (! l_current_data) {
11132 l_max_tile_size = 1000;
11134 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11135 for (i=0;i<l_nb_tiles;++i) {
11136 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11137 opj_free(l_current_data);
11141 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11142 if (l_current_tile_size > l_max_tile_size) {
11143 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11144 if (! l_current_data) {
11147 l_max_tile_size = l_current_tile_size;
11150 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11152 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11157 opj_free(l_current_data);
11162 * Ends the compression procedures and possibility add data to be read after the
11165 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11166 opj_stream_private_t *p_stream,
11167 struct opj_event_mgr * p_manager)
11169 /* customization of the encoding */
11170 j2k_setup_end_compress(p_j2k);
11172 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11182 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11184 * @param p_j2k the jpeg2000 codec.
11185 * @param p_stream the stream object.
11186 * @param p_manager the user event manager.
11188 * @return true if the codec is valid.
11190 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11191 opj_stream_private_t *p_stream,
11192 opj_image_t * p_image,
11193 opj_event_mgr_t * p_manager)
11196 assert(p_j2k != 00);
11197 assert(p_stream != 00);
11198 assert(p_manager != 00);
11200 p_j2k->m_private_image = p_image;
11202 /* customization of the validation */
11203 j2k_setup_encoding_validation (p_j2k);
11205 /* validation of the parameters codec */
11206 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11210 /* customization of the encoding */
11211 j2k_setup_header_writting(p_j2k);
11214 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11224 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11225 OPJ_UINT32 p_tile_index,
11226 opj_stream_private_t *p_stream,
11227 opj_event_mgr_t * p_manager )
11229 if (p_tile_index != p_j2k->m_current_tile_number) {
11230 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11234 opj_event_msg_v2(p_manager, EVT_INFO, "tile number %d / %d\n", p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th);
11236 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11237 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11238 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11240 /* initialisation before tile encoding */
11241 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11251 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11253 OPJ_UINT32 i,j,k = 0;
11254 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11255 opj_image_comp_t * l_img_comp = 00;
11256 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11257 opj_image_t * l_image = 00;
11258 OPJ_UINT32 l_size_comp, l_remaining;
11259 OPJ_INT32 * l_src_ptr;
11260 l_tilec = p_tcd->tcd_image->tiles->comps;
11261 l_image = p_tcd->image;
11262 l_img_comp = l_image->comps;
11264 for (i=0;i<p_tcd->image->numcomps;++i) {
11265 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11266 l_remaining = l_img_comp->prec & 7; /* (%8) */
11271 if (l_size_comp == 3) {
11275 l_width = (l_tilec->x1 - l_tilec->x0);
11276 l_height = (l_tilec->y1 - l_tilec->y0);
11277 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11278 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11279 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11280 l_stride = l_image_width - l_width;
11281 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11283 switch (l_size_comp) {
11286 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11287 if (l_img_comp->sgnd) {
11288 for (j=0;j<l_height;++j) {
11289 for (k=0;k<l_width;++k) {
11290 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11294 l_src_ptr += l_stride;
11298 for (j=0;j<l_height;++j) {
11299 for (k=0;k<l_width;++k) {
11300 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11304 l_src_ptr += l_stride;
11308 p_data = (OPJ_BYTE*) l_dest_ptr;
11313 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11314 if (l_img_comp->sgnd) {
11315 for (j=0;j<l_height;++j) {
11316 for (k=0;k<l_width;++k) {
11317 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11319 l_src_ptr += l_stride;
11323 for (j=0;j<l_height;++j) {
11324 for (k=0;k<l_width;++k) {
11325 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11327 l_src_ptr += l_stride;
11331 p_data = (OPJ_BYTE*) l_dest_ptr;
11336 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11337 for (j=0;j<l_height;++j) {
11338 for (k=0;k<l_width;++k) {
11339 *(l_dest_ptr++) = *(l_src_ptr++);
11341 l_src_ptr += l_stride;
11344 p_data = (OPJ_BYTE*) l_dest_ptr;
11357 * @param p_j2k the jpeg2000 codec.
11358 * @param p_stream the stream to write data to.
11359 * @param p_manager the user event manager.
11361 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11363 OPJ_UINT32 p_data_size,
11364 opj_stream_private_t *p_stream,
11365 opj_event_mgr_t * p_manager )
11367 opj_tcd_v2_t * l_tcd = 00;
11368 opj_cp_v2_t * l_cp = 00;
11369 opj_tcp_v2_t * l_tcp = 00;
11370 OPJ_UINT32 l_nb_bytes_written;
11371 OPJ_BYTE * l_current_data = 00;
11372 OPJ_UINT32 l_tile_size = 0;
11373 OPJ_UINT32 l_available_data;
11375 /* preconditions */
11376 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11378 l_tcd = p_j2k->m_tcd;
11379 l_cp = &(p_j2k->m_cp);
11380 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11382 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11383 l_available_data = l_tile_size;
11384 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11386 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11387 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismtach between tile data and sent data." );
11391 l_nb_bytes_written = 0;
11392 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11395 l_current_data += l_nb_bytes_written;
11396 l_available_data -= l_nb_bytes_written;
11398 l_nb_bytes_written = 0;
11399 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11403 l_available_data -= l_nb_bytes_written;
11404 l_nb_bytes_written = l_tile_size - l_available_data;
11406 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11410 ++p_j2k->m_current_tile_number;
11417 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11418 * are valid. Developers wanting to extend the library can add their own validation procedures.
11420 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11422 /* preconditions */
11423 assert(p_j2k != 00);
11425 /* DEVELOPER CORNER, insert your custom procedures */
11426 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_eoc );
11428 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11429 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_updated_tlm);
11432 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_epc );
11433 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_end_encoding );
11434 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_destroy_header_memory);
11438 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11439 * are valid. Developers wanting to extend the library can add their own validation procedures.
11441 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11443 /* preconditions */
11444 assert(p_j2k != 00);
11446 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_encoder);
11447 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_encoding_validation);
11449 /* DEVELOPER CORNER, add your custom validation procedure */
11450 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_mct_validation);
11455 * Sets up the procedures to do on writing header.
11456 * Developers wanting to extend the library can add their own writing procedures.
11458 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11460 /* preconditions */
11461 assert(p_j2k != 00);
11463 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_init_info );
11464 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_soc_v2 );
11465 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_siz_v2 );
11466 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_cod_v2 );
11467 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_qcd_v2 );
11470 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11471 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_image_components );
11472 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_tlm );
11474 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11475 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_poc );
11479 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_regions);
11481 if (p_j2k->m_cp.comment != 00) {
11482 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_com);
11485 /* DEVELOPER CORNER, insert your custom procedures */
11486 if (p_j2k->m_cp.rsiz & MCT) {
11487 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_write_mct_data_group );
11489 /* End of Developer Corner */
11491 if (p_j2k->cstr_index) {
11492 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_get_end_header );
11495 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_create_tcd);
11496 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_update_rates);
11500 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11502 OPJ_UINT32 * p_data_written,
11503 OPJ_UINT32 p_total_data_size,
11504 opj_stream_private_t *p_stream,
11505 struct opj_event_mgr * p_manager )
11508 OPJ_UINT32 l_nb_bytes_written = 0;
11509 OPJ_UINT32 l_current_nb_bytes_written;
11510 OPJ_BYTE * l_begin_data = 00;
11512 opj_tcp_v2_t *l_tcp = 00;
11513 opj_tcd_v2_t * l_tcd = 00;
11514 opj_cp_v2_t * l_cp = 00;
11516 l_tcd = p_j2k->m_tcd;
11517 l_cp = &(p_j2k->m_cp);
11518 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11520 l_tcd->cur_pino = 0;
11522 /*Get number of tile parts*/
11523 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11528 l_current_nb_bytes_written = 0;
11529 l_begin_data = p_data;
11530 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11535 l_nb_bytes_written += l_current_nb_bytes_written;
11536 p_data += l_current_nb_bytes_written;
11537 p_total_data_size -= l_current_nb_bytes_written;
11539 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11540 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11541 l_current_nb_bytes_written = 0;
11542 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11543 l_nb_bytes_written += l_current_nb_bytes_written;
11544 p_data += l_current_nb_bytes_written;
11545 p_total_data_size -= l_current_nb_bytes_written;
11547 l_current_nb_bytes_written = 0;
11548 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11549 l_nb_bytes_written += l_current_nb_bytes_written;
11550 p_data += l_current_nb_bytes_written;
11551 p_total_data_size -= l_current_nb_bytes_written;
11554 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11555 l_current_nb_bytes_written = 0;
11556 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11557 l_nb_bytes_written += l_current_nb_bytes_written;
11558 p_data += l_current_nb_bytes_written;
11559 p_total_data_size -= l_current_nb_bytes_written;
11563 l_current_nb_bytes_written = 0;
11564 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11568 l_nb_bytes_written += l_current_nb_bytes_written;
11569 * p_data_written = l_nb_bytes_written;
11571 /* Writing Psot in SOT marker */
11572 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11574 if (l_cp->m_specific_param.m_enc.m_cinema){
11575 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11581 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11583 OPJ_UINT32 * p_data_written,
11584 OPJ_UINT32 p_total_data_size,
11585 opj_stream_private_t *p_stream,
11586 struct opj_event_mgr * p_manager
11589 OPJ_UINT32 tilepartno=0;
11590 OPJ_UINT32 l_nb_bytes_written = 0;
11591 OPJ_UINT32 l_current_nb_bytes_written;
11592 OPJ_UINT32 l_part_tile_size;
11593 OPJ_UINT32 tot_num_tp;
11596 OPJ_BYTE * l_begin_data;
11597 opj_tcp_v2_t *l_tcp = 00;
11598 opj_tcd_v2_t * l_tcd = 00;
11599 opj_cp_v2_t * l_cp = 00;
11602 l_tcd = p_j2k->m_tcd;
11603 l_cp = &(p_j2k->m_cp);
11604 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11606 /*Get number of tile parts*/
11607 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11609 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11610 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11611 l_current_nb_bytes_written = 0;
11612 l_part_tile_size = 0;
11613 l_begin_data = p_data;
11615 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11619 l_nb_bytes_written += l_current_nb_bytes_written;
11620 p_data += l_current_nb_bytes_written;
11621 p_total_data_size -= l_current_nb_bytes_written;
11622 l_part_tile_size += l_nb_bytes_written;
11624 l_current_nb_bytes_written = 0;
11625 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11629 p_data += l_current_nb_bytes_written;
11630 l_nb_bytes_written += l_current_nb_bytes_written;
11631 p_total_data_size -= l_current_nb_bytes_written;
11632 l_part_tile_size += l_nb_bytes_written;
11634 /* Writing Psot in SOT marker */
11635 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11637 if (l_cp->m_specific_param.m_enc.m_cinema) {
11638 j2k_update_tlm(p_j2k,l_part_tile_size);
11641 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11644 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11645 l_tcd->cur_pino = pino;
11647 /*Get number of tile parts*/
11648 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11649 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11650 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11651 l_current_nb_bytes_written = 0;
11652 l_part_tile_size = 0;
11653 l_begin_data = p_data;
11655 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11659 l_nb_bytes_written += l_current_nb_bytes_written;
11660 p_data += l_current_nb_bytes_written;
11661 p_total_data_size -= l_current_nb_bytes_written;
11662 l_part_tile_size += l_current_nb_bytes_written;
11664 l_current_nb_bytes_written = 0;
11666 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11670 l_nb_bytes_written += l_current_nb_bytes_written;
11671 p_data += l_current_nb_bytes_written;
11672 p_total_data_size -= l_current_nb_bytes_written;
11673 l_part_tile_size += l_current_nb_bytes_written;
11675 /* Writing Psot in SOT marker */
11676 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11678 if (l_cp->m_specific_param.m_enc.m_cinema) {
11679 j2k_update_tlm(p_j2k,l_part_tile_size);
11682 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11686 *p_data_written = l_nb_bytes_written;
11692 * Writes the updated tlm.
11694 * @param p_stream the stream to write data to.
11695 * @param p_j2k J2K codec.
11696 * @param p_manager the user event manager.
11698 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11699 struct opj_stream_private *p_stream,
11700 struct opj_event_mgr * p_manager )
11702 OPJ_UINT32 l_tlm_size;
11703 OPJ_SIZE_T l_tlm_position, l_current_position;
11705 /* preconditions */
11706 assert(p_j2k != 00);
11707 assert(p_manager != 00);
11708 assert(p_stream != 00);
11710 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11711 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11712 l_current_position = opj_stream_tell(p_stream);
11714 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11718 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer,l_tlm_size,p_manager) != l_tlm_size) {
11722 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11731 * Ends the encoding, i.e. frees memory.
11733 * @param p_stream the stream to write data to.
11734 * @param p_j2k J2K codec.
11735 * @param p_manager the user event manager.
11737 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11738 struct opj_stream_private *p_stream,
11739 struct opj_event_mgr * p_manager )
11741 /* preconditions */
11742 assert(p_j2k != 00);
11743 assert(p_manager != 00);
11744 assert(p_stream != 00);
11746 tcd_destroy_v2(p_j2k->m_tcd);
11749 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11750 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11751 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11752 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11755 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11756 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11757 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11760 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11766 * Destroys the memory associated with the decoding of headers.
11768 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
11769 opj_stream_private_t *p_stream,
11770 opj_event_mgr_t * p_manager )
11772 /* preconditions */
11773 assert(p_j2k != 00);
11774 assert(p_stream != 00);
11775 assert(p_manager != 00);
11777 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11778 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11779 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11782 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11791 * @param p_stream the stream to write data to.
11792 * @param p_j2k J2K codec.
11793 * @param p_manager the user event manager.
11795 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11796 struct opj_stream_private *p_stream,
11797 struct opj_event_mgr * p_manager )
11799 opj_codestream_info_t * l_cstr_info = 00;
11801 /* preconditions */
11802 assert(p_j2k != 00);
11803 assert(p_manager != 00);
11804 assert(p_stream != 00);
11806 /* TODO mergeV2: check this part which use cstr_info */
11807 /*l_cstr_info = p_j2k->cstr_info;
11811 l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t));
11813 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
11814 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
11816 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
11818 l_cstr_info->tw = p_j2k->m_cp.tw;
11819 l_cstr_info->th = p_j2k->m_cp.th;
11821 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
11822 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
11823 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
11824 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
11826 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
11828 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
11830 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
11832 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
11833 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
11836 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
11838 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
11840 /*l_cstr_info->maxmarknum = 100;
11841 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
11842 l_cstr_info->marknum = 0;
11845 return j2k_calculate_tp_v2(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager);
11849 * Creates a tile-coder decoder.
11851 * @param p_stream the stream to write data to.
11852 * @param p_j2k J2K codec.
11853 * @param p_manager the user event manager.
11855 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
11856 struct opj_stream_private *p_stream,
11857 struct opj_event_mgr * p_manager )
11859 /* preconditions */
11860 assert(p_j2k != 00);
11861 assert(p_manager != 00);
11862 assert(p_stream != 00);
11864 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
11866 if (! p_j2k->m_tcd) {
11867 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
11871 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
11872 tcd_destroy_v2(p_j2k->m_tcd);
11883 * @param p_j2k the jpeg2000 codec.
11884 * @param p_stream the stream to write data to.
11885 * @param p_manager the user event manager.
11887 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
11888 OPJ_UINT32 p_tile_index,
11890 OPJ_UINT32 p_data_size,
11891 opj_stream_private_t *p_stream,
11892 opj_event_mgr_t * p_manager )
11894 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
11898 return j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager);