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.
864 static opj_bool j2k_read_ppm_v2 (
866 OPJ_BYTE * p_header_data,
867 OPJ_UINT32 p_header_size,
868 struct opj_event_mgr * p_manager
872 static opj_bool j2k_read_ppm_v3 (
874 OPJ_BYTE * p_header_data,
875 OPJ_UINT32 p_header_size,
876 struct opj_event_mgr * p_manager
880 Read the PPT marker (packet packet headers, tile-part header)
881 @param j2k J2K handle
883 static void j2k_read_ppt(opj_j2k_t *j2k);
885 * Reads a PPT marker (Packed packet headers, tile-part header)
887 * @param p_header_data the data contained in the PPT box.
888 * @param p_j2k the jpeg2000 codec.
889 * @param p_header_size the size of the data contained in the PPT marker.
890 * @param p_manager the user event manager.
892 static opj_bool j2k_read_ppt_v2 (
894 OPJ_BYTE * p_header_data,
895 OPJ_UINT32 p_header_size,
896 struct opj_event_mgr * p_manager
899 Write the TLM marker (Mainheader)
900 @param j2k J2K handle
902 static void j2k_write_tlm(opj_j2k_t *j2k);
904 Write the SOT marker (start of tile-part)
905 @param j2k J2K handle
907 static void j2k_write_sot(opj_j2k_t *j2k);
909 Read the SOT marker (start of tile-part)
910 @param j2k J2K handle
912 static void j2k_read_sot(opj_j2k_t *j2k);
915 * Writes the SOT marker (Start of tile-part)
917 * @param p_stream the stream to write data to.
918 * @param p_j2k J2K codec.
919 * @param p_manager the user event manager.
921 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
923 OPJ_UINT32 * p_data_written,
924 const struct opj_stream_private *p_stream,
925 struct opj_event_mgr * p_manager );
928 * Reads a PPT marker (Packed packet headers, tile-part header)
930 * @param p_header_data the data contained in the PPT box.
931 * @param p_j2k the jpeg2000 codec.
932 * @param p_header_size the size of the data contained in the PPT marker.
933 * @param p_manager the user event manager.
935 static opj_bool j2k_read_sot_v2 (
937 OPJ_BYTE * p_header_data,
938 OPJ_UINT32 p_header_size,
939 struct opj_event_mgr * p_manager
942 Write the SOD marker (start of data)
943 @param j2k J2K handle
944 @param tile_coder Pointer to a TCD handle
946 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
949 * Writes the SOD marker (Start of data)
951 * @param p_stream the stream to write data to.
952 * @param p_j2k J2K codec.
953 * @param p_manager the user event manager.
955 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
956 struct opj_tcd_v2 * p_tile_coder,
958 OPJ_UINT32 * p_data_written,
959 OPJ_UINT32 p_total_data_size,
960 const struct opj_stream_private *p_stream,
961 struct opj_event_mgr * p_manager );
964 Read the SOD marker (start of data)
965 @param j2k J2K handle
967 static void j2k_read_sod(opj_j2k_t *j2k);
970 * Reads a SOD marker (Start Of Data)
972 * @param p_header_data the data contained in the SOD box.
973 * @param p_j2k the jpeg2000 codec.
974 * @param p_header_size the size of the data contained in the SOD marker.
975 * @param p_manager the user event manager.
977 static opj_bool j2k_read_sod_v2 (
979 struct opj_stream_private *p_stream,
980 struct opj_event_mgr * p_manager
984 * Updates the Tile Length Marker.
986 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
988 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
989 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
991 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
992 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
996 Write the RGN marker (region-of-interest)
997 @param j2k J2K handle
998 @param compno Number of the component concerned by the information written
999 @param tileno Number of the tile concerned by the information written
1001 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
1004 * Writes the RGN marker (Region Of Interest)
1006 * @param p_tile_no the tile to output
1007 * @param p_comp_no the component to output
1008 * @param p_stream the stream to write data to.
1009 * @param p_j2k J2K codec.
1010 * @param p_manager the user event manager.
1012 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
1013 OPJ_UINT32 p_tile_no,
1014 OPJ_UINT32 p_comp_no,
1015 struct opj_stream_private *p_stream,
1016 struct opj_event_mgr * p_manager );
1019 Read the RGN marker (region-of-interest)
1020 @param j2k J2K handle
1022 static void j2k_read_rgn(opj_j2k_t *j2k);
1025 * Reads a RGN marker (Region Of Interest)
1027 * @param p_header_data the data contained in the POC box.
1028 * @param p_j2k the jpeg2000 codec.
1029 * @param p_header_size the size of the data contained in the POC marker.
1030 * @param p_manager the user event manager.
1032 static opj_bool j2k_read_rgn_v2 (
1033 opj_j2k_v2_t *p_j2k,
1034 OPJ_BYTE * p_header_data,
1035 OPJ_UINT32 p_header_size,
1036 struct opj_event_mgr * p_manager
1040 Write the EOC marker (end of codestream)
1041 @param j2k J2K handle
1043 static void j2k_write_eoc(opj_j2k_t *j2k);
1045 Read the EOC marker (end of codestream)
1046 @param j2k J2K handle
1048 static void j2k_read_eoc(opj_j2k_t *j2k);
1051 * Reads a EOC marker (End Of Codestream)
1053 * @param p_header_data the data contained in the SOD box.
1054 * @param p_j2k the jpeg2000 codec.
1055 * @param p_header_size the size of the data contained in the SOD marker.
1056 * @param p_manager the user event manager.
1059 static opj_bool j2k_read_eoc_v2 (
1060 opj_j2k_v2_t *p_j2k,
1061 struct opj_stream_private *p_stream,
1062 struct opj_event_mgr * p_manager
1069 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1071 * @param p_stream the stream to write data to.
1072 * @param p_j2k J2K codec.
1073 * @param p_manager the user event manager.
1075 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1076 struct opj_stream_private *p_stream,
1077 struct opj_event_mgr * p_manager );
1082 * @param p_stream the stream to write data to.
1083 * @param p_j2k J2K codec.
1084 * @param p_manager the user event manager.
1086 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1087 struct opj_stream_private *p_stream,
1088 struct opj_event_mgr * p_manager );
1091 Read an unknown marker
1092 @param j2k J2K handle
1094 static void j2k_read_unk(opj_j2k_t *j2k);
1096 Add main header marker information
1097 @param cstr_info Codestream information structure
1098 @param type marker type
1099 @param pos byte offset of marker segment
1100 @param len length of marker segment
1102 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1104 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1106 Add tile header marker information
1107 @param tileno tile index number
1108 @param cstr_info Codestream information structure
1109 @param type marker type
1110 @param pos byte offset of marker segment
1111 @param len length of marker segment
1113 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1115 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);
1118 * Reads an unknown marker
1120 * @param p_stream the stream object to read from.
1121 * @param p_j2k the jpeg2000 codec.
1122 * @param p_manager the user event manager.
1124 * @return true if the marker could be deduced.
1126 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1127 struct opj_stream_private *p_stream,
1128 OPJ_UINT32 *output_marker,
1129 struct opj_event_mgr * p_manager );
1132 * Writes the MCT marker (Multiple Component Transform)
1134 * @param p_stream the stream to write data to.
1135 * @param p_j2k J2K codec.
1136 * @param p_manager the user event manager.
1138 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1139 opj_mct_data_t * p_mct_record,
1140 struct opj_stream_private *p_stream,
1141 struct opj_event_mgr * p_manager );
1144 * Reads a MCT marker (Multiple Component Transform)
1146 * @param p_header_data the data contained in the MCT box.
1147 * @param p_j2k the jpeg2000 codec.
1148 * @param p_header_size the size of the data contained in the MCT marker.
1149 * @param p_manager the user event manager.
1151 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1152 OPJ_BYTE * p_header_data,
1153 OPJ_UINT32 p_header_size,
1154 struct opj_event_mgr * p_manager );
1157 * Writes the MCC marker (Multiple Component Collection)
1159 * @param p_stream the stream to write data to.
1160 * @param p_j2k J2K codec.
1161 * @param p_manager the user event manager.
1163 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1164 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1165 struct opj_stream_private *p_stream,
1166 struct opj_event_mgr * p_manager );
1170 * Reads a MCC marker (Multiple Component Collection)
1172 * @param p_header_data the data contained in the MCC box.
1173 * @param p_j2k the jpeg2000 codec.
1174 * @param p_header_size the size of the data contained in the MCC marker.
1175 * @param p_manager the user event manager.
1177 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1178 OPJ_BYTE * p_header_data,
1179 OPJ_UINT32 p_header_size,
1180 struct opj_event_mgr * p_manager );
1183 * Writes the MCO marker (Multiple component transformation ordering)
1185 * @param p_stream the stream to write data to.
1186 * @param p_j2k J2K codec.
1187 * @param p_manager the user event manager.
1189 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1190 struct opj_stream_private *p_stream,
1191 struct opj_event_mgr * p_manager );
1194 * Reads a MCO marker (Multiple Component Transform Ordering)
1196 * @param p_header_data the data contained in the MCO box.
1197 * @param p_j2k the jpeg2000 codec.
1198 * @param p_header_size the size of the data contained in the MCO marker.
1199 * @param p_manager the user event manager.
1201 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1202 OPJ_BYTE * p_header_data,
1203 OPJ_UINT32 p_header_size,
1204 struct opj_event_mgr * p_manager );
1206 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1208 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1209 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1210 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1211 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1213 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1214 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1215 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1216 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1218 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1219 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1220 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1221 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1225 * Ends the encoding, i.e. frees memory.
1227 * @param p_stream the stream to write data to.
1228 * @param p_j2k J2K codec.
1229 * @param p_manager the user event manager.
1231 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1232 struct opj_stream_private *p_stream,
1233 struct opj_event_mgr * p_manager );
1236 * Writes the CBD marker (Component bit depth definition)
1238 * @param p_stream the stream to write data to.
1239 * @param p_j2k J2K codec.
1240 * @param p_manager the user event manager.
1242 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1243 struct opj_stream_private *p_stream,
1244 struct opj_event_mgr * p_manager );
1247 * Reads a CBD marker (Component bit depth definition)
1248 * @param p_header_data the data contained in the CBD box.
1249 * @param p_j2k the jpeg2000 codec.
1250 * @param p_header_size the size of the data contained in the CBD marker.
1251 * @param p_manager the user event manager.
1253 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1254 OPJ_BYTE * p_header_data,
1255 OPJ_UINT32 p_header_size,
1256 struct opj_event_mgr * p_manager);
1259 * Writes the image components.
1261 * @param p_stream the stream to write data to.
1262 * @param p_j2k J2K codec.
1263 * @param p_manager the user event manager.
1265 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1266 struct opj_stream_private *p_stream,
1267 struct opj_event_mgr * p_manager );
1270 * Writes regions of interests.
1272 * @param p_stream the stream to write data to.
1273 * @param p_j2k J2K codec.
1274 * @param p_manager the user event manager.
1276 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1277 struct opj_stream_private *p_stream,
1278 struct opj_event_mgr * p_manager );
1283 * @param p_stream the stream to write data to.
1284 * @param p_j2k J2K codec.
1285 * @param p_manager the user event manager.
1287 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1288 struct opj_stream_private *p_stream,
1289 struct opj_event_mgr * p_manager );
1292 * Checks the progression order changes values. Tells of the poc given as input are valid.
1293 * A nice message is outputted at errors.
1295 * @param p_pocs the progression order changes.
1296 * @param p_nb_pocs the number of progression order changes.
1297 * @param p_nb_resolutions the number of resolutions.
1298 * @param numcomps the number of components
1299 * @param numlayers the number of layers.
1301 * @return true if the pocs are valid.
1303 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1304 OPJ_UINT32 p_nb_pocs,
1305 OPJ_UINT32 p_nb_resolutions,
1306 OPJ_UINT32 numcomps,
1307 OPJ_UINT32 numlayers,
1308 opj_event_mgr_t * p_manager);
1311 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1313 * @param cp the coding parameters.
1314 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1315 * @param tileno the given tile.
1317 * @return the number of tile parts.
1319 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1321 /** mem allocation for TLM marker*/
1322 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1325 * Calculates the total number of tile parts needed by the encoder to
1326 * encode such an image. If not enough memory is available, then the function return false.
1328 * @param p_nb_tiles pointer that will hold the number of tile parts.
1329 * @param cp the coding parameters for the image.
1330 * @param image the image to encode.
1331 * @param p_j2k the p_j2k encoder.
1332 * @param p_manager the user event manager.
1334 * @return true if the function was successful, false else.
1336 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1338 OPJ_UINT32 * p_nb_tiles,
1340 opj_event_mgr_t * p_manager);
1342 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1344 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1346 static opj_codestream_index_t* j2k_create_cstr_index(void);
1352 /* ----------------------------------------------------------------------- */
1353 typedef struct j2k_prog_order{
1354 OPJ_PROG_ORDER enum_prog;
1358 j2k_prog_order_t j2k_prog_order_list[] = {
1364 {(OPJ_PROG_ORDER)-1, ""}
1372 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1380 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1382 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1384 j2k_read_int16_to_float,
1385 j2k_read_int32_to_float,
1386 j2k_read_float32_to_float,
1387 j2k_read_float64_to_float
1390 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1392 j2k_read_int16_to_int32,
1393 j2k_read_int32_to_int32,
1394 j2k_read_float32_to_int32,
1395 j2k_read_float64_to_int32
1398 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1400 j2k_write_float_to_int16,
1401 j2k_write_float_to_int32,
1402 j2k_write_float_to_float,
1403 j2k_write_float_to_float64
1406 typedef struct opj_dec_memory_marker_handler
1410 /** value of the state when the marker can appear */
1412 /** action linked to the marker */
1413 opj_bool (*handler) (
1414 opj_j2k_v2_t *p_j2k,
1415 OPJ_BYTE * p_header_data,
1416 OPJ_UINT32 p_header_size,
1417 struct opj_event_mgr * p_manager
1420 opj_dec_memory_marker_handler_t;
1422 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1425 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1426 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1427 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1428 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1429 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1430 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1431 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1432 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1433 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1434 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1435 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1436 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1437 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1439 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1440 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1441 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1442 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1443 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1444 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1446 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
1447 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1448 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1449 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1450 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1451 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1452 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1453 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
1454 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1455 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1456 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1457 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1458 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1460 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1461 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1462 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1463 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1464 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1465 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1467 #ifdef TODO_MS /* FIXME */
1468 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1469 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1470 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1471 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1473 #endif /* USE_JPWL */
1475 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1476 {J2K_MS_INSEC, 0, j2k_read_insec}
1477 #endif /* USE_JPSEC */
1478 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1483 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1485 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1486 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1490 for (i=0;i<p_nb_elem;++i) {
1491 opj_read_bytes(l_src_data,&l_temp,2);
1493 l_src_data+=sizeof(OPJ_INT16);
1495 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1499 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1501 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1502 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1506 for (i=0;i<p_nb_elem;++i) {
1507 opj_read_bytes(l_src_data,&l_temp,4);
1509 l_src_data+=sizeof(OPJ_INT32);
1511 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1515 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1517 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1518 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1522 for (i=0;i<p_nb_elem;++i) {
1523 opj_read_float(l_src_data,&l_temp);
1525 l_src_data+=sizeof(OPJ_FLOAT32);
1527 *(l_dest_data++) = l_temp;
1531 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1533 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1534 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1538 for (i=0;i<p_nb_elem;++i) {
1539 opj_read_double(l_src_data,&l_temp);
1541 l_src_data+=sizeof(OPJ_FLOAT64);
1543 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1547 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1549 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1550 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1554 for (i=0;i<p_nb_elem;++i) {
1555 opj_read_bytes(l_src_data,&l_temp,2);
1557 l_src_data+=sizeof(OPJ_INT16);
1559 *(l_dest_data++) = (OPJ_INT32) l_temp;
1563 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1565 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1566 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1570 for (i=0;i<p_nb_elem;++i) {
1571 opj_read_bytes(l_src_data,&l_temp,4);
1573 l_src_data+=sizeof(OPJ_INT32);
1575 *(l_dest_data++) = (OPJ_INT32) l_temp;
1579 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1581 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1582 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1586 for (i=0;i<p_nb_elem;++i) {
1587 opj_read_float(l_src_data,&l_temp);
1589 l_src_data+=sizeof(OPJ_FLOAT32);
1591 *(l_dest_data++) = (OPJ_INT32) l_temp;
1595 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1597 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1598 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1602 for (i=0;i<p_nb_elem;++i) {
1603 opj_read_double(l_src_data,&l_temp);
1605 l_src_data+=sizeof(OPJ_FLOAT64);
1607 *(l_dest_data++) = (OPJ_INT32) l_temp;
1611 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1613 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1614 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1618 for (i=0;i<p_nb_elem;++i) {
1619 l_temp = (OPJ_UINT32) *(l_src_data++);
1621 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1623 l_dest_data+=sizeof(OPJ_INT16);
1627 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1629 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1630 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1634 for (i=0;i<p_nb_elem;++i) {
1635 l_temp = (OPJ_UINT32) *(l_src_data++);
1637 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1639 l_dest_data+=sizeof(OPJ_INT32);
1643 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1645 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1646 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1650 for (i=0;i<p_nb_elem;++i) {
1651 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1653 opj_write_float(l_dest_data,l_temp);
1655 l_dest_data+=sizeof(OPJ_FLOAT32);
1659 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1661 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1662 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1666 for (i=0;i<p_nb_elem;++i) {
1667 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1669 opj_write_double(l_dest_data,l_temp);
1671 l_dest_data+=sizeof(OPJ_FLOAT64);
1677 * Converts an enum type progression order to string type.
1679 * @param prg_order the progression order to get.
1681 * @return the string representation of the given progression order.
1683 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1684 j2k_prog_order_t *po;
1685 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1686 if(po->enum_prog == prg_order){
1687 return po->str_prog;
1690 return po->str_prog;
1694 * Checks the progression order changes values. Tells if the poc given as input are valid.
1696 * @param p_pocs the progression order changes.
1697 * @param p_nb_pocs the number of progression order changes.
1698 * @param p_nb_resolutions the number of resolutions.
1699 * @param numcomps the number of components
1700 * @param numlayers the number of layers.
1701 * @param p_manager the user event manager.
1703 * @return true if the pocs are valid.
1705 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1706 OPJ_UINT32 p_nb_pocs,
1707 OPJ_UINT32 p_nb_resolutions,
1708 OPJ_UINT32 p_num_comps,
1709 OPJ_UINT32 p_num_layers,
1710 opj_event_mgr_t * p_manager)
1712 OPJ_UINT32* packet_array;
1713 OPJ_UINT32 index , resno, compno, layno;
1715 OPJ_UINT32 step_c = 1;
1716 OPJ_UINT32 step_r = p_num_comps * step_c;
1717 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1718 opj_bool loss = OPJ_FALSE;
1719 OPJ_UINT32 layno0 = 0;
1721 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1722 if (packet_array == 00) {
1723 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1726 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1728 if (p_nb_pocs == 0) {
1732 index = step_r * p_pocs->resno0;
1733 // take each resolution for each poc
1734 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1736 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1738 // take each comp of each resolution for each poc
1739 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1740 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1742 // and finally take each layer of each res of ...
1743 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1744 //index = step_r * resno + step_c * compno + step_l * layno;
1745 packet_array[comp_index] = 1;
1746 comp_index += step_l;
1749 res_index += step_c;
1756 // iterate through all the pocs
1757 for (i = 1; i < p_nb_pocs ; ++i) {
1758 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1760 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1761 index = step_r * p_pocs->resno0;
1763 // take each resolution for each poc
1764 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1765 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1767 // take each comp of each resolution for each poc
1768 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1769 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1771 // and finally take each layer of each res of ...
1772 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1773 //index = step_r * resno + step_c * compno + step_l * layno;
1774 packet_array[comp_index] = 1;
1775 comp_index += step_l;
1778 res_index += step_c;
1788 for (layno = 0; layno < p_num_layers ; ++layno) {
1789 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1790 for (compno = 0; compno < p_num_comps; ++compno) {
1791 loss |= (packet_array[index]!=1);
1792 //index = step_r * resno + step_c * compno + step_l * layno;
1799 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1802 opj_free(packet_array);
1807 /* ----------------------------------------------------------------------- */
1808 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1811 int tpnum=1,tpend=0;
1812 opj_tcp_t *tcp = &cp->tcps[tileno];
1813 prog = j2k_convert_progression_order(tcp->prg);
1818 if( cp->tp_flag == prog[i] ){
1819 tpend=1;cp->tp_pos=i;
1823 tpnum= tpnum * tcp->pocs[pino].compE;
1826 tpnum= tpnum * tcp->pocs[pino].resE;
1829 tpnum= tpnum * tcp->pocs[pino].prcE;
1832 tpnum= tpnum * tcp->pocs[pino].layE;
1844 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1846 * @param cp the coding parameters.
1847 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1848 * @param tileno the given tile.
1850 * @return the number of tile parts.
1852 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1854 const OPJ_CHAR *prog = 00;
1856 OPJ_UINT32 tpnum = 1;
1857 opj_tcp_v2_t *tcp = 00;
1858 opj_poc_t * l_current_poc = 00;
1861 assert(tileno < (cp->tw * cp->th));
1862 assert(pino < (cp->tcps[tileno].numpocs + 1));
1864 /* get the given tile coding parameter */
1865 tcp = &cp->tcps[tileno];
1868 l_current_poc = &(tcp->pocs[pino]);
1869 assert(l_current_poc != 0);
1871 /* get the progression order as a character string */
1872 prog = j2k_convert_progression_order(tcp->prg);
1873 assert(strlen(prog) > 0);
1875 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1879 /* component wise */
1881 tpnum *= l_current_poc->compE;
1883 /* resolution wise */
1885 tpnum *= l_current_poc->resE;
1889 tpnum *= l_current_poc->prcE;
1893 tpnum *= l_current_poc->layE;
1896 /* whould we split here ? */
1897 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1898 cp->m_specific_param.m_enc.m_tp_pos=i;
1910 /** mem allocation for TLM marker*/
1911 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1912 int pino,tileno,totnum_tp=0;
1914 OPJ_ARG_NOT_USED(img_numcomp);
1916 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1917 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1918 int cur_totnum_tp = 0;
1919 opj_tcp_t *tcp = &cp->tcps[tileno];
1920 for(pino = 0; pino <= tcp->numpocs; pino++) {
1922 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1923 if(!pi) { return -1;}
1924 tp_num = j2k_get_num_tp(cp,pino,tileno);
1925 totnum_tp = totnum_tp + tp_num;
1926 cur_totnum_tp = cur_totnum_tp + tp_num;
1927 pi_destroy(pi, cp, tileno);
1929 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1931 if (j2k->cstr_info) {
1932 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1933 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1941 * Calculates the total number of tile parts needed by the encoder to
1942 * encode such an image. If not enough memory is available, then the function return false.
1944 * @param p_nb_tiles pointer that will hold the number of tile parts.
1945 * @param cp the coding parameters for the image.
1946 * @param image the image to encode.
1947 * @param p_j2k the p_j2k encoder.
1948 * @param p_manager the user event manager.
1950 * @return true if the function was successful, false else.
1952 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1954 OPJ_UINT32 * p_nb_tiles,
1956 opj_event_mgr_t * p_manager)
1958 OPJ_UINT32 pino,tileno;
1959 OPJ_UINT32 l_nb_tiles;
1963 assert(p_nb_tiles != 00);
1965 assert(image != 00);
1966 assert(p_j2k != 00);
1967 assert(p_manager != 00);
1969 l_nb_tiles = cp->tw * cp->th;
1974 /* TODO mergeV2: check this part which use cstr_info */
1975 /*if (p_j2k->cstr_info) {
1976 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1978 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1979 OPJ_UINT32 cur_totnum_tp = 0;
1981 pi_update_encoding_parameters(image,cp,tileno);
1983 for (pino = 0; pino <= tcp->numpocs; ++pino)
1985 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1987 *p_nb_tiles = *p_nb_tiles + tp_num;
1989 cur_totnum_tp += tp_num;
1992 tcp->m_nb_tile_parts = cur_totnum_tp;
1994 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1995 if (l_info_tile_ptr->tp == 00) {
1999 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
2001 l_info_tile_ptr->num_tps = cur_totnum_tp;
2008 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2009 OPJ_UINT32 cur_totnum_tp = 0;
2011 pi_update_encoding_parameters(image,cp,tileno);
2013 for (pino = 0; pino <= tcp->numpocs; ++pino) {
2014 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2016 *p_nb_tiles = *p_nb_tiles + tp_num;
2018 cur_totnum_tp += tp_num;
2020 tcp->m_nb_tile_parts = cur_totnum_tp;
2029 static void j2k_write_soc(opj_j2k_t *j2k) {
2030 opj_cio_t *cio = j2k->cio;
2031 cio_write(cio, J2K_MS_SOC, 2);
2034 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2039 /* update markers struct */
2040 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2041 #endif /* USE_JPWL */
2046 * Writes the SOC marker (Start Of Codestream)
2048 * @param p_stream the stream to write data to.
2049 * @param p_j2k J2K codec.
2050 * @param p_manager the user event manager.
2052 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2053 struct opj_stream_private *p_stream,
2054 struct opj_event_mgr * p_manager )
2056 /* 2 bytes will be written */
2057 OPJ_BYTE * l_start_stream = 00;
2060 assert(p_stream != 00);
2061 assert(p_j2k != 00);
2062 assert(p_manager != 00);
2064 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2066 /* write SOC identifier */
2067 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2069 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2075 /* update markers struct */
2077 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2079 assert( 0 && "TODO" );
2080 #endif /* USE_JPWL */
2086 static void j2k_read_soc(opj_j2k_t *j2k) {
2087 j2k->state = J2K_STATE_MHSIZ;
2089 if (j2k->cstr_info) {
2090 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
2091 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
2096 * Reads a SOC marker (Start of Codestream)
2097 * @param p_header_data the data contained in the SOC box.
2098 * @param jp2 the jpeg2000 file codec.
2099 * @param p_header_size the size of the data contained in the SOC marker.
2100 * @param p_manager the user event manager.
2102 static opj_bool j2k_read_soc_v2( opj_j2k_v2_t *p_j2k,
2103 struct opj_stream_private *p_stream,
2104 struct opj_event_mgr * p_manager )
2106 OPJ_BYTE l_data [2];
2107 OPJ_UINT32 l_marker;
2110 assert(p_j2k != 00);
2111 assert(p_manager != 00);
2112 assert(p_stream != 00);
2114 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2118 opj_read_bytes(l_data,&l_marker,2);
2119 if (l_marker != J2K_MS_SOC) {
2123 /* Next marker should be a SIZ marker in the main header */
2124 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2126 /* FIXME move it in a index structure included in p_j2k*/
2127 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2129 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2131 /* Add the marker to the codestream index*/
2132 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2137 static void j2k_write_siz(opj_j2k_t *j2k) {
2141 opj_cio_t *cio = j2k->cio;
2142 opj_image_t *image = j2k->image;
2143 opj_cp_t *cp = j2k->cp;
2145 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2146 lenp = cio_tell(cio);
2148 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2149 cio_write(cio, image->x1, 4); /* Xsiz */
2150 cio_write(cio, image->y1, 4); /* Ysiz */
2151 cio_write(cio, image->x0, 4); /* X0siz */
2152 cio_write(cio, image->y0, 4); /* Y0siz */
2153 cio_write(cio, cp->tdx, 4); /* XTsiz */
2154 cio_write(cio, cp->tdy, 4); /* YTsiz */
2155 cio_write(cio, cp->tx0, 4); /* XT0siz */
2156 cio_write(cio, cp->ty0, 4); /* YT0siz */
2157 cio_write(cio, image->numcomps, 2); /* Csiz */
2158 for (i = 0; i < image->numcomps; i++) {
2159 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2160 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2161 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2163 len = cio_tell(cio) - lenp;
2164 cio_seek(cio, lenp);
2165 cio_write(cio, len, 2); /* Lsiz */
2166 cio_seek(cio, lenp + len);
2169 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2173 * Writes the SIZ marker (image and tile size)
2175 * @param p_stream the stream to write data to.
2176 * @param p_j2k J2K codec.
2177 * @param p_manager the user event manager.
2179 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2180 struct opj_stream_private *p_stream,
2181 struct opj_event_mgr * p_manager )
2184 OPJ_UINT32 l_size_len;
2185 OPJ_BYTE * l_current_ptr;
2186 opj_image_t * l_image = 00;
2187 opj_cp_v2_t *cp = 00;
2188 opj_image_comp_t * l_img_comp = 00;
2191 assert(p_stream != 00);
2192 assert(p_j2k != 00);
2193 assert(p_manager != 00);
2195 l_image = p_j2k->m_private_image;
2196 cp = &(p_j2k->m_cp);
2197 l_size_len = 40 + 3 * l_image->numcomps;
2198 l_img_comp = l_image->comps;
2200 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2202 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2203 = (OPJ_BYTE*)opj_realloc(
2204 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2206 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2210 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2213 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2215 /* write SOC identifier */
2216 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2219 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2222 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2225 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2228 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2231 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2234 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2237 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2240 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2243 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2246 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2249 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2252 for (i = 0; i < l_image->numcomps; ++i) {
2253 /* TODO here with MCT ? */
2254 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2257 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2260 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2266 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) {
2273 static void j2k_read_siz(opj_j2k_t *j2k) {
2277 opj_cio_t *cio = j2k->cio;
2278 opj_image_t *image = j2k->image;
2279 opj_cp_t *cp = j2k->cp;
2281 len = cio_read(cio, 2); /* Lsiz */
2282 cio_read(cio, 2); /* Rsiz (capabilities) */
2283 image->x1 = cio_read(cio, 4); /* Xsiz */
2284 image->y1 = cio_read(cio, 4); /* Ysiz */
2285 image->x0 = cio_read(cio, 4); /* X0siz */
2286 image->y0 = cio_read(cio, 4); /* Y0siz */
2287 cp->tdx = cio_read(cio, 4); /* XTsiz */
2288 cp->tdy = cio_read(cio, 4); /* YTsiz */
2289 cp->tx0 = cio_read(cio, 4); /* XT0siz */
2290 cp->ty0 = cio_read(cio, 4); /* YT0siz */
2292 /* the following code triggers: */
2293 /* warning: comparison of unsigned expression < 0 is always false */
2295 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
2296 opj_event_msg(j2k->cinfo, EVT_ERROR,
2297 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
2298 image->x0,image->x1,image->y0,image->y1);
2303 image->numcomps = cio_read(cio, 2); /* Csiz */
2306 if (j2k->cp->correct) {
2307 /* if JPWL is on, we check whether TX errors have damaged
2308 too much the SIZ parameters */
2309 if (!(image->x1 * image->y1)) {
2310 opj_event_msg(j2k->cinfo, EVT_ERROR,
2311 "JPWL: bad image size (%d x %d)\n",
2312 image->x1, image->y1);
2313 if (!JPWL_ASSUME || JPWL_ASSUME) {
2314 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2318 assert( len >= 38 );
2319 if (image->numcomps != (OPJ_UINT32)((len - 38) / 3)) {
2320 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2321 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2322 image->numcomps, ((len - 38) / 3));
2324 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2327 /* we try to correct */
2328 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
2329 if (image->numcomps < (OPJ_UINT32)((len - 38) / 3)) {
2330 len = 38 + 3 * image->numcomps;
2331 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2334 image->numcomps = ((len - 38) / 3);
2335 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2340 /* update components number in the jpwl_exp_comps filed */
2341 cp->exp_comps = image->numcomps;
2343 #endif /* USE_JPWL */
2345 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
2346 for (i = 0; i < image->numcomps; i++) {
2348 tmp = cio_read(cio, 1); /* Ssiz_i */
2349 image->comps[i].prec = (tmp & 0x7f) + 1;
2350 image->comps[i].sgnd = tmp >> 7;
2351 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
2352 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
2355 if (j2k->cp->correct) {
2356 /* if JPWL is on, we check whether TX errors have damaged
2357 too much the SIZ parameters, again */
2358 if (!(image->comps[i].dx * image->comps[i].dy)) {
2359 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2360 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2361 i, i, image->comps[i].dx, image->comps[i].dy);
2363 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2366 /* we try to correct */
2367 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2368 if (!image->comps[i].dx) {
2369 image->comps[i].dx = 1;
2370 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2371 i, image->comps[i].dx);
2373 if (!image->comps[i].dy) {
2374 image->comps[i].dy = 1;
2375 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2376 i, image->comps[i].dy);
2381 #endif /* USE_JPWL */
2383 /* TODO: unused ? */
2384 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
2385 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
2387 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
2388 image->comps[i].factor = cp->reduce; /* reducing factor per component */
2391 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2392 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2395 if (j2k->cp->correct) {
2396 /* if JPWL is on, we check whether TX errors have damaged
2397 too much the SIZ parameters */
2398 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
2399 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2400 "JPWL: bad number of tiles (%d x %d)\n",
2403 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2406 /* we try to correct */
2407 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2410 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2413 if (cp->tw > cp->max_tiles) {
2415 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
2416 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2417 cp->max_tiles, cp->tw);
2421 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2424 if (cp->th > cp->max_tiles) {
2426 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2427 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2428 cp->max_tiles, cp->th);
2432 #endif /* USE_JPWL */
2434 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2435 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
2436 cp->tileno_size = 0;
2439 if (j2k->cp->correct) {
2441 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2442 "JPWL: could not alloc tcps field of cp\n");
2443 if (!JPWL_ASSUME || JPWL_ASSUME) {
2444 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2449 #endif /* USE_JPWL */
2451 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2452 cp->tcps[i].POC = 0;
2453 cp->tcps[i].numpocs = 0;
2454 cp->tcps[i].first = 1;
2457 /* Initialization for PPM marker */
2459 cp->ppm_data = NULL;
2460 cp->ppm_data_first = NULL;
2461 cp->ppm_previous = 0;
2464 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2465 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2466 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2468 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
2469 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
2470 j2k->state = J2K_STATE_MH;
2473 if (j2k->cstr_info) {
2474 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2475 cstr_info->image_w = image->x1 - image->x0;
2476 cstr_info->image_h = image->y1 - image->y0;
2477 cstr_info->numcomps = image->numcomps;
2478 cstr_info->tw = cp->tw;
2479 cstr_info->th = cp->th;
2480 cstr_info->tile_x = cp->tdx;
2481 cstr_info->tile_y = cp->tdy;
2482 cstr_info->tile_Ox = cp->tx0;
2483 cstr_info->tile_Oy = cp->ty0;
2484 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
2490 * Reads a SIZ marker (image and tile size)
2491 * @param p_header_data the data contained in the SIZ box.
2492 * @param jp2 the jpeg2000 file codec.
2493 * @param p_header_size the size of the data contained in the SIZ marker.
2494 * @param p_manager the user event manager.
2496 opj_bool j2k_read_siz_v2 (
2497 opj_j2k_v2_t *p_j2k,
2498 OPJ_BYTE * p_header_data,
2499 OPJ_UINT32 p_header_size,
2500 struct opj_event_mgr * p_manager
2503 OPJ_UINT32 l_size, i;
2504 OPJ_UINT32 l_nb_comp;
2505 OPJ_UINT32 l_nb_comp_remain;
2506 OPJ_UINT32 l_remaining_size;
2507 OPJ_UINT32 l_nb_tiles;
2509 opj_image_t *l_image = 00;
2510 opj_cp_v2_t *l_cp = 00;
2511 opj_image_comp_t * l_img_comp = 00;
2512 opj_tcp_v2_t * l_current_tile_param = 00;
2515 assert(p_j2k != 00);
2516 assert(p_manager != 00);
2517 assert(p_header_data != 00);
2519 l_image = p_j2k->m_private_image;
2520 l_cp = &(p_j2k->m_cp);
2522 /* minimum size == 39 - 3 (= minimum component parameter) */
2523 if (p_header_size < 36) {
2524 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2528 l_remaining_size = p_header_size - 36;
2529 l_nb_comp = l_remaining_size / 3;
2530 l_nb_comp_remain = l_remaining_size % 3;
2531 if (l_nb_comp_remain != 0){
2532 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2536 l_size = p_header_size + 2; /* Lsiz */
2538 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2540 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2541 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2543 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2545 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2547 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2549 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2551 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2553 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2555 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2557 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2560 l_image->numcomps = (OPJ_UINT16) l_tmp;
2562 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2566 if (l_image->numcomps != l_nb_comp) {
2567 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);
2572 if (l_cp->correct) {
2573 /* if JPWL is on, we check whether TX errors have damaged
2574 too much the SIZ parameters */
2575 if (!(l_image->x1 * l_image->y1)) {
2576 opj_event_msg_v2(p_manager, EVT_ERROR,
2577 "JPWL: bad image size (%d x %d)\n",
2578 l_image->x1, l_image->y1);
2579 if (!JPWL_ASSUME || JPWL_ASSUME) {
2580 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2585 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2586 if (l_image->numcomps != ((len - 38) / 3)) {
2587 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2588 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2589 l_image->numcomps, ((len - 38) / 3));
2591 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2594 */ /* we try to correct */
2595 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2596 if (l_image->numcomps < ((len - 38) / 3)) {
2597 len = 38 + 3 * l_image->numcomps;
2598 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2601 l_image->numcomps = ((len - 38) / 3);
2602 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2608 /* update components number in the jpwl_exp_comps filed */
2609 l_cp->exp_comps = l_image->numcomps;
2611 #endif /* USE_JPWL */
2613 /* Allocate the resulting image components */
2614 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2615 if (l_image->comps == 00){
2616 l_image->numcomps = 0;
2617 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2621 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2622 l_img_comp = l_image->comps;
2624 /* Read the component information */
2625 for (i = 0; i < l_image->numcomps; ++i){
2627 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2629 l_img_comp->prec = (tmp & 0x7f) + 1;
2630 l_img_comp->sgnd = tmp >> 7;
2631 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2633 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2634 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2636 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2639 if (l_cp->correct) {
2640 /* if JPWL is on, we check whether TX errors have damaged
2641 too much the SIZ parameters, again */
2642 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2643 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2644 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2645 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2647 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2650 /* we try to correct */
2651 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2652 if (!l_image->comps[i].dx) {
2653 l_image->comps[i].dx = 1;
2654 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2655 i, l_image->comps[i].dx);
2657 if (!l_image->comps[i].dy) {
2658 l_image->comps[i].dy = 1;
2659 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2660 i, l_image->comps[i].dy);
2664 #endif /* USE_JPWL */
2665 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2666 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2670 /* Compute the number of tiles */
2671 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2672 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2673 l_nb_tiles = l_cp->tw * l_cp->th;
2675 /* Define the tiles which will be decoded */
2676 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2677 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;
2678 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;
2679 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);
2680 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);
2683 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2684 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2685 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2686 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2690 if (l_cp->correct) {
2691 /* if JPWL is on, we check whether TX errors have damaged
2692 too much the SIZ parameters */
2693 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2694 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2695 "JPWL: bad number of tiles (%d x %d)\n",
2696 l_cp->tw, l_cp->th);
2698 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2701 /* we try to correct */
2702 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2705 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2708 if (l_cp->tw > l_cp->max_tiles) {
2710 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2711 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2712 l_cp->max_tiles, l_cp->tw);
2716 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2719 if (l_cp->th > l_cp->max_tiles) {
2721 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2722 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2723 l_cp->max_tiles, l_cp->th);
2727 #endif /* USE_JPWL */
2729 /* memory allocations */
2730 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2731 if (l_cp->tcps == 00) {
2732 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2735 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2738 if (l_cp->correct) {
2740 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2741 "JPWL: could not alloc tcps field of cp\n");
2742 if (!JPWL_ASSUME || JPWL_ASSUME) {
2743 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2748 #endif /* USE_JPWL */
2750 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2751 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2752 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2753 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2756 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2758 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2759 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2761 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2762 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2765 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));
2766 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2768 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2769 (opj_simple_mcc_decorrelation_data_t*)
2770 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2772 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2773 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2776 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));
2777 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2779 /* set up default dc level shift */
2780 for (i=0;i<l_image->numcomps;++i) {
2781 if (! l_image->comps[i].sgnd) {
2782 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2786 l_current_tile_param = l_cp->tcps;
2787 for (i = 0; i < l_nb_tiles; ++i) {
2788 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2789 if (l_current_tile_param->tccps == 00) {
2790 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2793 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2795 ++l_current_tile_param;
2798 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2799 opj_image_comp_header_update(l_image,l_cp);
2806 static void j2k_write_com(opj_j2k_t *j2k) {
2810 if(j2k->cp->comment) {
2811 opj_cio_t *cio = j2k->cio;
2812 char *comment = j2k->cp->comment;
2814 cio_write(cio, J2K_MS_COM, 2);
2815 lenp = cio_tell(cio);
2817 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2818 for (i = 0; i < strlen(comment); i++) {
2819 cio_write(cio, comment[i], 1);
2821 len = cio_tell(cio) - lenp;
2822 cio_seek(cio, lenp);
2823 cio_write(cio, len, 2);
2824 cio_seek(cio, lenp + len);
2828 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2833 static void j2k_read_com(opj_j2k_t *j2k) {
2836 opj_cio_t *cio = j2k->cio;
2838 len = cio_read(cio, 2);
2839 cio_skip(cio, len - 2);
2842 * Reads a COM marker (comments)
2843 * @param p_header_data the data contained in the COM box.
2844 * @param jp2 the jpeg2000 file codec.
2845 * @param p_header_size the size of the data contained in the COM marker.
2846 * @param p_manager the user event manager.
2848 opj_bool j2k_read_com_v2 (
2849 opj_j2k_v2_t *p_j2k,
2850 OPJ_BYTE * p_header_data,
2851 OPJ_UINT32 p_header_size,
2852 struct opj_event_mgr * p_manager
2856 assert(p_j2k != 00);
2857 assert(p_manager != 00);
2858 assert(p_header_data != 00);
2859 (void)p_header_size;
2864 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2867 opj_cp_t *cp = j2k->cp;
2868 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2869 opj_tccp_t *tccp = &tcp->tccps[compno];
2870 opj_cio_t *cio = j2k->cio;
2872 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2873 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2874 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2875 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2876 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2878 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2879 for (i = 0; i < tccp->numresolutions; i++) {
2880 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2885 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2888 opj_cp_t *cp = j2k->cp;
2889 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2890 opj_tccp_t *tccp = &tcp->tccps[compno];
2891 opj_cio_t *cio = j2k->cio;
2893 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2895 /* If user wants to remove more resolutions than the codestream contains, return error*/
2896 assert(cp->reduce >= 0);
2897 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2898 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2899 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2900 j2k->state |= J2K_STATE_ERR;
2903 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2904 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2905 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2906 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2907 if (tccp->csty & J2K_CP_CSTY_PRT) {
2908 for (i = 0; i < tccp->numresolutions; i++) {
2909 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2910 tccp->prcw[i] = tmp & 0xf;
2911 tccp->prch[i] = tmp >> 4;
2916 if(j2k->cstr_info && compno == 0) {
2917 for (i = 0; i < tccp->numresolutions; i++) {
2918 if (tccp->csty & J2K_CP_CSTY_PRT) {
2919 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2920 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2923 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2924 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2931 static void j2k_write_cod(opj_j2k_t *j2k) {
2932 opj_cp_t *cp = NULL;
2933 opj_tcp_t *tcp = NULL;
2936 opj_cio_t *cio = j2k->cio;
2938 cio_write(cio, J2K_MS_COD, 2); /* COD */
2940 lenp = cio_tell(cio);
2944 tcp = &cp->tcps[j2k->curtileno];
2946 cio_write(cio, tcp->csty, 1); /* Scod */
2947 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2948 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2949 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2951 j2k_write_cox(j2k, 0);
2952 len = cio_tell(cio) - lenp;
2953 cio_seek(cio, lenp);
2954 cio_write(cio, len, 2); /* Lcod */
2955 cio_seek(cio, lenp + len);
2958 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2963 * Writes the COD marker (Coding style default)
2965 * @param p_stream the stream to write data to.
2966 * @param p_j2k J2K codec.
2967 * @param p_manager the user event manager.
2969 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2970 struct opj_stream_private *p_stream,
2971 struct opj_event_mgr * p_manager )
2973 opj_cp_v2_t *l_cp = 00;
2974 opj_tcp_v2_t *l_tcp = 00;
2975 OPJ_UINT32 l_code_size,l_remaining_size;
2976 OPJ_BYTE * l_current_data = 00;
2979 assert(p_j2k != 00);
2980 assert(p_manager != 00);
2981 assert(p_stream != 00);
2983 l_cp = &(p_j2k->m_cp);
2984 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2985 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2986 l_remaining_size = l_code_size;
2988 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2989 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2990 = (OPJ_BYTE*)opj_realloc(
2991 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2994 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2998 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
3001 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3003 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
3004 l_current_data += 2;
3006 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
3007 l_current_data += 2;
3009 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
3012 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
3015 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
3018 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
3021 l_remaining_size -= 9;
3023 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3024 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3028 if (l_remaining_size != 0) {
3029 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3033 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) {
3040 static void j2k_read_cod(opj_j2k_t *j2k) {
3044 opj_cio_t *cio = j2k->cio;
3045 opj_cp_t *cp = j2k->cp;
3046 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3047 opj_image_t *image = j2k->image;
3049 len = cio_read(cio, 2); /* Lcod */
3050 tcp->csty = cio_read(cio, 1); /* Scod */
3051 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
3052 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
3053 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
3055 pos = cio_tell(cio);
3056 for (i = 0; i < image->numcomps; i++) {
3057 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
3059 j2k_read_cox(j2k, i);
3063 if (j2k->cstr_info) {
3064 opj_codestream_info_t *cstr_info = j2k->cstr_info;
3065 cstr_info->prog = tcp->prg;
3066 cstr_info->numlayers = tcp->numlayers;
3067 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
3068 for (i = 0; i < image->numcomps; i++) {
3069 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
3075 * Reads a COD marker (Coding Styke defaults)
3076 * @param p_header_data the data contained in the COD box.
3077 * @param p_j2k the jpeg2000 codec.
3078 * @param p_header_size the size of the data contained in the COD marker.
3079 * @param p_manager the user event manager.
3081 opj_bool j2k_read_cod_v2 (
3082 opj_j2k_v2_t *p_j2k,
3083 OPJ_BYTE * p_header_data,
3084 OPJ_UINT32 p_header_size,
3085 struct opj_event_mgr * p_manager
3091 opj_cp_v2_t *l_cp = 00;
3092 opj_tcp_v2_t *l_tcp = 00;
3093 opj_image_t *l_image = 00;
3096 assert(p_header_data != 00);
3097 assert(p_j2k != 00);
3098 assert(p_manager != 00);
3100 l_image = p_j2k->m_private_image;
3101 l_cp = &(p_j2k->m_cp);
3103 /* If we are in the first tile-part header of the current tile */
3104 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3105 &l_cp->tcps[p_j2k->m_current_tile_number] :
3106 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3108 /* Make sure room is sufficient */
3109 if (p_header_size < 5) {
3110 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3114 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
3116 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
3118 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
3119 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
3122 /* If user didn't set a number layer to decode take the max specify in the codestream. */
3123 if (l_cp->m_specific_param.m_dec.m_layer) {
3124 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
3127 l_tcp->num_layers_to_decode = l_tcp->numlayers;
3130 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
3134 for (i = 0; i < l_image->numcomps; ++i) {
3135 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
3138 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3139 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3143 if (p_header_size != 0) {
3144 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3148 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
3149 j2k_copy_tile_component_parameters(p_j2k);
3152 #ifdef WIP_REMOVE_MSD
3153 if (p_j2k->cstr_info) {
3154 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
3155 p_j2k->cstr_info->prog = l_tcp->prg;
3156 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
3157 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
3158 for (i = 0; i < l_image->numcomps; ++i) {
3159 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
3167 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
3170 opj_cp_t *cp = j2k->cp;
3171 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3172 opj_image_t *image = j2k->image;
3173 opj_cio_t *cio = j2k->cio;
3175 cio_write(cio, J2K_MS_COC, 2); /* COC */
3176 lenp = cio_tell(cio);
3178 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3179 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
3180 j2k_write_cox(j2k, compno);
3181 len = cio_tell(cio) - lenp;
3182 cio_seek(cio, lenp);
3183 cio_write(cio, len, 2); /* Lcoc */
3184 cio_seek(cio, lenp + len);
3188 * Writes the COC marker (Coding style component)
3190 * @param p_comp_no the index of the component to output.
3191 * @param p_stream the stream to write data to.
3192 * @param p_j2k J2K codec.
3193 * @param p_manager the user event manager.
3195 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
3196 OPJ_UINT32 p_comp_no,
3197 struct opj_stream_private *p_stream,
3198 struct opj_event_mgr * p_manager )
3200 OPJ_UINT32 l_coc_size,l_remaining_size;
3201 OPJ_UINT32 l_comp_room;
3204 assert(p_j2k != 00);
3205 assert(p_manager != 00);
3206 assert(p_stream != 00);
3208 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3210 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3212 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3213 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3214 = (OPJ_BYTE*)opj_realloc(
3215 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3217 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3221 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3224 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);
3226 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) {
3234 * Writes the COC marker (Coding style component)
3236 * @param p_comp_no the index of the component to output.
3237 * @param p_stream the stream to write data to.
3238 * @param p_j2k J2K codec.
3239 * @param p_manager the user event manager.
3241 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3242 OPJ_UINT32 p_comp_no,
3244 OPJ_UINT32 * p_data_written,
3245 struct opj_event_mgr * p_manager )
3247 opj_cp_v2_t *l_cp = 00;
3248 opj_tcp_v2_t *l_tcp = 00;
3249 OPJ_UINT32 l_coc_size,l_remaining_size;
3250 OPJ_BYTE * l_current_data = 00;
3251 opj_image_t *l_image = 00;
3252 OPJ_UINT32 l_comp_room;
3255 assert(p_j2k != 00);
3256 assert(p_manager != 00);
3258 l_cp = &(p_j2k->m_cp);
3259 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3260 l_image = p_j2k->m_private_image;
3261 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3263 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3264 l_remaining_size = l_coc_size;
3266 l_current_data = p_data;
3268 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3269 l_current_data += 2;
3271 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3272 l_current_data += 2;
3274 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3275 l_current_data+=l_comp_room;
3277 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3280 l_remaining_size -= (5 + l_comp_room);
3281 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3282 * p_data_written = l_coc_size;
3286 * Gets the maximum size taken by a coc.
3288 * @param p_j2k the jpeg2000 codec to use.
3290 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3293 OPJ_UINT32 l_nb_comp;
3294 OPJ_UINT32 l_nb_tiles;
3295 OPJ_UINT32 l_max = 0;
3299 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3300 l_nb_comp = p_j2k->m_private_image->numcomps;
3302 for (i=0;i<l_nb_tiles;++i) {
3303 for (j=0;j<l_nb_comp;++j) {
3304 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3311 static void j2k_read_coc(opj_j2k_t *j2k) {
3314 opj_cp_t *cp = j2k->cp;
3315 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3316 opj_image_t *image = j2k->image;
3317 opj_cio_t *cio = j2k->cio;
3319 len = cio_read(cio, 2); /* Lcoc */
3320 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3321 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
3322 j2k_read_cox(j2k, compno);
3326 * Reads a COC marker (Coding Style Component)
3327 * @param p_header_data the data contained in the COC box.
3328 * @param p_j2k the jpeg2000 codec.
3329 * @param p_header_size the size of the data contained in the COC marker.
3330 * @param p_manager the user event manager.
3332 opj_bool j2k_read_coc_v2 (
3333 opj_j2k_v2_t *p_j2k,
3334 OPJ_BYTE * p_header_data,
3335 OPJ_UINT32 p_header_size,
3336 struct opj_event_mgr * p_manager
3339 opj_cp_v2_t *l_cp = NULL;
3340 opj_tcp_v2_t *l_tcp = NULL;
3341 opj_image_t *l_image = NULL;
3342 OPJ_UINT32 l_comp_room;
3343 OPJ_UINT32 l_comp_no;
3346 assert(p_header_data != 00);
3347 assert(p_j2k != 00);
3348 assert(p_manager != 00);
3350 l_cp = &(p_j2k->m_cp);
3351 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3352 &l_cp->tcps[p_j2k->m_current_tile_number] :
3353 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3354 l_image = p_j2k->m_private_image;
3356 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3358 /* make sure room is sufficient*/
3359 if (p_header_size < l_comp_room + 1) {
3360 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3363 p_header_size -= l_comp_room + 1;
3365 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3366 p_header_data += l_comp_room;
3367 if (l_comp_no >= l_image->numcomps) {
3368 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3372 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3375 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3376 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3380 if (p_header_size != 0) {
3381 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3387 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3388 int bandno, numbands;
3391 opj_cp_t *cp = j2k->cp;
3392 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3393 opj_tccp_t *tccp = &tcp->tccps[compno];
3394 opj_cio_t *cio = j2k->cio;
3396 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3397 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3399 for (bandno = 0; bandno < numbands; bandno++) {
3400 expn = tccp->stepsizes[bandno].expn;
3401 mant = tccp->stepsizes[bandno].mant;
3403 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3404 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3406 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3411 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3413 int bandno, numbands;
3415 opj_cp_t *cp = j2k->cp;
3416 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3417 opj_tccp_t *tccp = &tcp->tccps[compno];
3418 opj_cio_t *cio = j2k->cio;
3420 tmp = cio_read(cio, 1); /* Sqcx */
3421 tccp->qntsty = tmp & 0x1f;
3422 tccp->numgbits = tmp >> 5;
3423 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3424 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3427 if (j2k->cp->correct) {
3429 /* if JPWL is on, we check whether there are too many subbands */
3430 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3431 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3432 "JPWL: bad number of subbands in Sqcx (%d)\n",
3435 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3438 /* we try to correct */
3440 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3441 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3448 /* We check whether there are too many subbands */
3449 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3450 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3451 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3452 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3455 #endif /* USE_JPWL */
3457 for (bandno = 0; bandno < numbands; bandno++) {
3459 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3460 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3463 tmp = cio_read(cio, 2); /* SPqcx_i */
3467 if (bandno < J2K_MAXBANDS){
3468 tccp->stepsizes[bandno].expn = expn;
3469 tccp->stepsizes[bandno].mant = mant;
3473 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3474 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3475 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3476 tccp->stepsizes[bandno].expn =
3477 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3478 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3479 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3485 static void j2k_write_qcd(opj_j2k_t *j2k) {
3488 opj_cio_t *cio = j2k->cio;
3490 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3491 lenp = cio_tell(cio);
3493 j2k_write_qcx(j2k, 0);
3494 len = cio_tell(cio) - lenp;
3495 cio_seek(cio, lenp);
3496 cio_write(cio, len, 2); /* Lqcd */
3497 cio_seek(cio, lenp + len);
3500 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3504 * Writes the QCD marker (quantization default)
3506 * @param p_comp_number the index of the component to output.
3507 * @param p_stream the stream to write data to.
3508 * @param p_j2k J2K codec.
3509 * @param p_manager the user event manager.
3511 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3512 struct opj_stream_private *p_stream,
3513 struct opj_event_mgr * p_manager )
3515 opj_cp_v2_t *l_cp = 00;
3516 opj_tcp_v2_t *l_tcp = 00;
3517 OPJ_UINT32 l_qcd_size,l_remaining_size;
3518 OPJ_BYTE * l_current_data = 00;
3521 assert(p_j2k != 00);
3522 assert(p_manager != 00);
3523 assert(p_stream != 00);
3525 l_cp = &(p_j2k->m_cp);
3526 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3527 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3528 l_remaining_size = l_qcd_size;
3530 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3531 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3532 = (OPJ_BYTE*)opj_realloc(
3533 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3536 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3540 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3543 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3545 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3546 l_current_data += 2;
3548 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3549 l_current_data += 2;
3551 l_remaining_size -= 4;
3553 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3554 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3558 if (l_remaining_size != 0) {
3559 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3563 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) {
3571 static void j2k_read_qcd(opj_j2k_t *j2k) {
3575 opj_cio_t *cio = j2k->cio;
3576 opj_image_t *image = j2k->image;
3578 len = cio_read(cio, 2); /* Lqcd */
3579 pos = cio_tell(cio);
3580 for (i = 0; i < image->numcomps; i++) {
3582 j2k_read_qcx(j2k, i, len - 2);
3587 * Reads a QCD marker (Quantization defaults)
3588 * @param p_header_data the data contained in the QCD box.
3589 * @param p_j2k the jpeg2000 codec.
3590 * @param p_header_size the size of the data contained in the QCD marker.
3591 * @param p_manager the user event manager.
3593 opj_bool j2k_read_qcd_v2 (
3594 opj_j2k_v2_t *p_j2k,
3595 OPJ_BYTE * p_header_data,
3596 OPJ_UINT32 p_header_size,
3597 struct opj_event_mgr * p_manager
3601 assert(p_header_data != 00);
3602 assert(p_j2k != 00);
3603 assert(p_manager != 00);
3605 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3606 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3610 if (p_header_size != 0) {
3611 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3615 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3616 j2k_copy_tile_quantization_parameters(p_j2k);
3621 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3624 opj_cio_t *cio = j2k->cio;
3626 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3627 lenp = cio_tell(cio);
3629 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3630 j2k_write_qcx(j2k, compno);
3631 len = cio_tell(cio) - lenp;
3632 cio_seek(cio, lenp);
3633 cio_write(cio, len, 2); /* Lqcc */
3634 cio_seek(cio, lenp + len);
3638 * Writes the QCC marker (quantization component)
3640 * @param p_comp_no the index of the component to output.
3641 * @param p_stream the stream to write data to.
3642 * @param p_j2k J2K codec.
3643 * @param p_manager the user event manager.
3645 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3646 OPJ_UINT32 p_comp_no,
3647 struct opj_stream_private *p_stream,
3648 struct opj_event_mgr * p_manager )
3650 OPJ_UINT32 l_qcc_size,l_remaining_size;
3653 assert(p_j2k != 00);
3654 assert(p_manager != 00);
3655 assert(p_stream != 00);
3657 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3658 l_remaining_size = l_qcc_size;
3660 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3661 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3662 = (OPJ_BYTE*)opj_realloc(
3663 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3665 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3669 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3672 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);
3674 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) {
3682 * Writes the QCC marker (quantization component)
3684 * @param p_comp_no the index of the component to output.
3685 * @param p_stream the stream to write data to.
3686 * @param p_j2k J2K codec.
3687 * @param p_manager the user event manager.
3689 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3690 OPJ_UINT32 p_comp_no,
3692 OPJ_UINT32 * p_data_written,
3693 struct opj_event_mgr * p_manager )
3695 OPJ_UINT32 l_qcc_size,l_remaining_size;
3696 OPJ_BYTE * l_current_data = 00;
3699 assert(p_j2k != 00);
3700 assert(p_manager != 00);
3702 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3703 l_remaining_size = l_qcc_size;
3705 l_current_data = p_data;
3707 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3708 l_current_data += 2;
3710 if (p_j2k->m_private_image->numcomps <= 256) {
3713 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3714 l_current_data += 2;
3716 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3719 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3720 l_remaining_size -= 6;
3723 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3724 l_current_data += 2;
3726 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3729 l_remaining_size -= 6;
3732 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3734 *p_data_written = l_qcc_size;
3738 * Gets the maximum size taken by a qcc.
3740 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3742 return j2k_get_max_coc_size(p_j2k);
3745 static void j2k_read_qcc(opj_j2k_t *j2k) {
3747 int numcomp = j2k->image->numcomps;
3748 opj_cio_t *cio = j2k->cio;
3750 len = cio_read(cio, 2); /* Lqcc */
3751 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3754 if (j2k->cp->correct) {
3756 static int backup_compno = 0;
3758 /* compno is negative or larger than the number of components!!! */
3759 if ((compno < 0) || (compno >= numcomp)) {
3760 opj_event_msg(j2k->cinfo, EVT_ERROR,
3761 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3764 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3767 /* we try to correct */
3768 compno = backup_compno % numcomp;
3769 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3770 "- setting component number to %d\n",
3774 /* keep your private count of tiles */
3777 #endif /* USE_JPWL */
3779 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3783 * Reads a QCC marker (Quantization component)
3784 * @param p_header_data the data contained in the QCC box.
3785 * @param p_j2k the jpeg2000 codec.
3786 * @param p_header_size the size of the data contained in the QCC marker.
3787 * @param p_manager the user event manager.
3789 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3790 OPJ_BYTE * p_header_data,
3791 OPJ_UINT32 p_header_size,
3792 struct opj_event_mgr * p_manager)
3794 OPJ_UINT32 l_num_comp,l_comp_no;
3797 assert(p_header_data != 00);
3798 assert(p_j2k != 00);
3799 assert(p_manager != 00);
3801 l_num_comp = p_j2k->m_private_image->numcomps;
3803 if (l_num_comp <= 256) {
3804 if (p_header_size < 1) {
3805 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3808 opj_read_bytes(p_header_data,&l_comp_no,1);
3813 if (p_header_size < 2) {
3814 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3817 opj_read_bytes(p_header_data,&l_comp_no,2);
3823 if (p_j2k->m_cp.correct) {
3825 static OPJ_UINT32 backup_compno = 0;
3827 /* compno is negative or larger than the number of components!!! */
3828 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3829 opj_event_msg_v2(p_manager, EVT_ERROR,
3830 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3831 l_comp_no, l_num_comp);
3833 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3836 /* we try to correct */
3837 l_comp_no = backup_compno % l_num_comp;
3838 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3839 "- setting component number to %d\n",
3843 /* keep your private count of tiles */
3846 #endif /* USE_JPWL */
3848 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3849 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3853 if (p_header_size != 0) {
3854 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3862 static void j2k_write_poc(opj_j2k_t *j2k) {
3863 int len, numpchgs, i;
3865 int numcomps = j2k->image->numcomps;
3867 opj_cp_t *cp = j2k->cp;
3868 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3869 opj_tccp_t *tccp = &tcp->tccps[0];
3870 opj_cio_t *cio = j2k->cio;
3872 numpchgs = 1 + tcp->numpocs;
3873 cio_write(cio, J2K_MS_POC, 2); /* POC */
3874 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3875 cio_write(cio, len, 2); /* Lpoc */
3876 for (i = 0; i < numpchgs; i++) {
3877 opj_poc_t *poc = &tcp->pocs[i];
3878 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3879 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3880 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3881 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3882 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3883 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3884 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3885 poc->compno1 = int_min(poc->compno1, numcomps);
3886 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3891 * Writes the POC marker (Progression Order Change)
3893 * @param p_stream the stream to write data to.
3894 * @param p_j2k J2K codec.
3895 * @param p_manager the user event manager.
3897 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3899 OPJ_UINT32 * p_data_written,
3900 struct opj_event_mgr * p_manager )
3903 OPJ_BYTE * l_current_data = 00;
3904 OPJ_UINT32 l_nb_comp;
3905 OPJ_UINT32 l_nb_poc;
3906 OPJ_UINT32 l_poc_size;
3907 opj_image_t *l_image = 00;
3908 opj_tcp_v2_t *l_tcp = 00;
3909 opj_tccp_t *l_tccp = 00;
3910 opj_poc_t *l_current_poc = 00;
3911 OPJ_UINT32 l_poc_room;
3914 assert(p_j2k != 00);
3915 assert(p_manager != 00);
3917 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3918 l_tccp = &l_tcp->tccps[0];
3919 l_image = p_j2k->m_private_image;
3920 l_nb_comp = l_image->numcomps;
3921 l_nb_poc = 1 + l_tcp->numpocs;
3923 if (l_nb_comp <= 256) {
3930 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3932 l_current_data = p_data;
3934 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3935 l_current_data += 2;
3937 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3938 l_current_data += 2;
3940 l_current_poc = l_tcp->pocs;
3941 for (i = 0; i < l_nb_poc; ++i) {
3942 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3945 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3946 l_current_data+=l_poc_room;
3948 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3951 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3954 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3955 l_current_data+=l_poc_room;
3957 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3960 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3961 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3962 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3963 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3968 *p_data_written = l_poc_size;
3972 * Gets the maximum size taken by the writing of a POC.
3974 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3976 opj_tcp_v2_t * l_tcp = 00;
3977 OPJ_UINT32 l_nb_tiles = 0;
3978 OPJ_UINT32 l_max_poc = 0;
3981 l_tcp = p_j2k->m_cp.tcps;
3982 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3984 for (i=0;i<l_nb_tiles;++i) {
3985 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3991 return 4 + 9 * l_max_poc;
3995 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3997 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
4000 OPJ_UINT32 l_nb_tiles;
4001 OPJ_UINT32 l_max = 0;
4002 opj_tcp_v2_t * l_tcp = 00;
4004 l_tcp = p_j2k->m_cp.tcps;
4005 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
4007 for (i=0;i<l_nb_tiles;++i) {
4008 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
4018 * Gets the maximum size taken by the headers of the SOT.
4020 * @param p_j2k the jpeg2000 codec to use.
4022 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
4024 OPJ_UINT32 l_nb_bytes = 0;
4025 OPJ_UINT32 l_nb_comps;
4026 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
4028 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
4029 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
4031 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
4032 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
4033 l_nb_bytes += l_nb_comps * l_coc_bytes;
4035 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
4036 l_nb_bytes += l_nb_comps * l_qcc_bytes;
4039 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
4041 /*** DEVELOPER CORNER, Add room for your headers ***/
4046 static void j2k_read_poc(opj_j2k_t *j2k) {
4047 int len, numpchgs, i, old_poc;
4049 int numcomps = j2k->image->numcomps;
4051 opj_cp_t *cp = j2k->cp;
4052 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
4053 opj_cio_t *cio = j2k->cio;
4055 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
4057 len = cio_read(cio, 2); /* Lpoc */
4058 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
4060 for (i = old_poc; i < numpchgs + old_poc; i++) {
4062 poc = &tcp->pocs[i];
4063 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
4064 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
4065 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
4066 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
4067 poc->compno1 = int_min(
4068 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
4069 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
4072 tcp->numpocs = numpchgs + old_poc - 1;
4076 * Reads a POC marker (Progression Order Change)
4078 * @param p_header_data the data contained in the POC box.
4079 * @param p_j2k the jpeg2000 codec.
4080 * @param p_header_size the size of the data contained in the POC marker.
4081 * @param p_manager the user event manager.
4083 opj_bool j2k_read_poc_v2 (
4084 opj_j2k_v2_t *p_j2k,
4085 OPJ_BYTE * p_header_data,
4086 OPJ_UINT32 p_header_size,
4087 struct opj_event_mgr * p_manager)
4089 OPJ_UINT32 i, l_nb_comp, l_tmp;
4090 opj_image_t * l_image = 00;
4091 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
4092 OPJ_UINT32 l_chunk_size, l_comp_room;
4094 opj_cp_v2_t *l_cp = 00;
4095 opj_tcp_v2_t *l_tcp = 00;
4096 opj_poc_t *l_current_poc = 00;
4099 assert(p_header_data != 00);
4100 assert(p_j2k != 00);
4101 assert(p_manager != 00);
4103 l_image = p_j2k->m_private_image;
4104 l_nb_comp = l_image->numcomps;
4105 if (l_nb_comp <= 256) {
4111 l_chunk_size = 5 + 2 * l_comp_room;
4112 l_current_poc_nb = p_header_size / l_chunk_size;
4113 l_current_poc_remaining = p_header_size % l_chunk_size;
4115 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
4116 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
4120 l_cp = &(p_j2k->m_cp);
4121 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4122 &l_cp->tcps[p_j2k->m_current_tile_number] :
4123 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4124 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
4125 l_current_poc_nb += l_old_poc_nb;
4127 assert(l_current_poc_nb < 32);
4129 /* now poc is in use.*/
4132 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
4133 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
4134 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
4136 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
4137 p_header_data+=l_comp_room;
4138 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
4140 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
4142 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
4143 p_header_data+=l_comp_room;
4144 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
4146 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4147 /* make sure comp is in acceptable bounds */
4148 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4152 l_tcp->numpocs = l_current_poc_nb - 1;
4156 static void j2k_read_crg(opj_j2k_t *j2k) {
4157 int len, i, Xcrg_i, Ycrg_i;
4159 opj_cio_t *cio = j2k->cio;
4160 int numcomps = j2k->image->numcomps;
4162 len = cio_read(cio, 2); /* Lcrg */
4163 for (i = 0; i < numcomps; i++) {
4164 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
4165 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4170 * Reads a CRG marker (Component registration)
4172 * @param p_header_data the data contained in the TLM box.
4173 * @param p_j2k the jpeg2000 codec.
4174 * @param p_header_size the size of the data contained in the TLM marker.
4175 * @param p_manager the user event manager.
4177 opj_bool j2k_read_crg_v2 (
4178 opj_j2k_v2_t *p_j2k,
4179 OPJ_BYTE * p_header_data,
4180 OPJ_UINT32 p_header_size,
4181 struct opj_event_mgr * p_manager
4184 OPJ_UINT32 l_nb_comp;
4186 assert(p_header_data != 00);
4187 assert(p_j2k != 00);
4188 assert(p_manager != 00);
4190 l_nb_comp = p_j2k->m_private_image->numcomps;
4192 if (p_header_size != l_nb_comp *4) {
4193 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4196 /* Do not care of this at the moment since only local variables are set here */
4199 (i = 0; i < l_nb_comp; ++i)
4201 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4203 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4210 static void j2k_read_tlm(opj_j2k_t *j2k) {
4211 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4212 long int Ttlm_i, Ptlm_i;
4214 opj_cio_t *cio = j2k->cio;
4216 len = cio_read(cio, 2); /* Ltlm */
4217 Ztlm = cio_read(cio, 1); /* Ztlm */
4218 Stlm = cio_read(cio, 1); /* Stlm */
4219 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4220 SP = (Stlm >> 6) & 0x01;
4221 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4222 for (i = 0; i < tile_tlm; i++) {
4223 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4224 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4229 * Reads a TLM marker (Tile Length Marker)
4231 * @param p_header_data the data contained in the TLM box.
4232 * @param p_j2k the jpeg2000 codec.
4233 * @param p_header_size the size of the data contained in the TLM marker.
4234 * @param p_manager the user event manager.
4236 opj_bool j2k_read_tlm_v2 (
4237 opj_j2k_v2_t *p_j2k,
4238 OPJ_BYTE * p_header_data,
4239 OPJ_UINT32 p_header_size,
4240 struct opj_event_mgr * p_manager
4243 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4245 assert(p_header_data != 00);
4246 assert(p_j2k != 00);
4247 assert(p_manager != 00);
4249 if (p_header_size < 2) {
4250 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4255 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4257 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4260 l_ST = ((l_Stlm >> 4) & 0x3);
4261 l_SP = (l_Stlm >> 6) & 0x1;
4263 l_Ptlm_size = (l_SP + 1) * 2;
4264 l_quotient = l_Ptlm_size + l_ST;
4266 l_tot_num_tp = p_header_size / l_quotient;
4267 l_tot_num_tp_remaining = p_header_size % l_quotient;
4269 if (l_tot_num_tp_remaining != 0) {
4270 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4273 /* FIXME Do not care of this at the moment since only local variables are set here */
4276 (i = 0; i < l_tot_num_tp; ++i)
4278 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4279 p_header_data += l_ST;
4280 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4281 p_header_data += l_Ptlm_size;
4286 static void j2k_read_plm(opj_j2k_t *j2k) {
4287 int len, i, Zplm, Nplm, add, packet_len = 0;
4289 opj_cio_t *cio = j2k->cio;
4291 len = cio_read(cio, 2); /* Lplm */
4292 Zplm = cio_read(cio, 1); /* Zplm */
4295 Nplm = cio_read(cio, 4); /* Nplm */
4297 for (i = Nplm; i > 0; i--) {
4298 add = cio_read(cio, 1);
4300 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4301 if ((add & 0x80) == 0) {
4312 * Reads a PLM marker (Packet length, main header marker)
4314 * @param p_header_data the data contained in the TLM box.
4315 * @param p_j2k the jpeg2000 codec.
4316 * @param p_header_size the size of the data contained in the TLM marker.
4317 * @param p_manager the user event manager.
4319 opj_bool j2k_read_plm_v2 (
4320 opj_j2k_v2_t *p_j2k,
4321 OPJ_BYTE * p_header_data,
4322 OPJ_UINT32 p_header_size,
4323 struct opj_event_mgr * p_manager
4327 assert(p_header_data != 00);
4328 assert(p_j2k != 00);
4329 assert(p_manager != 00);
4331 if (p_header_size < 1) {
4332 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4335 /* Do not care of this at the moment since only local variables are set here */
4337 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4344 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4346 p_header_size -= (1+l_Nplm);
4350 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4354 (i = 0; i < l_Nplm; ++i)
4356 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4358 // take only the last seven bytes
4359 l_packet_len |= (l_tmp & 0x7f);
4367 // store packet length and proceed to next packet
4374 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4382 static void j2k_read_plt(opj_j2k_t *j2k) {
4383 int len, i, Zplt, packet_len = 0, add;
4385 opj_cio_t *cio = j2k->cio;
4387 len = cio_read(cio, 2); /* Lplt */
4388 Zplt = cio_read(cio, 1); /* Zplt */
4389 for (i = len - 3; i > 0; i--) {
4390 add = cio_read(cio, 1);
4391 packet_len = (packet_len << 7) + add; /* Iplt_i */
4392 if ((add & 0x80) == 0) {
4400 * Reads a PLT marker (Packet length, tile-part header)
4402 * @param p_header_data the data contained in the PLT box.
4403 * @param p_j2k the jpeg2000 codec.
4404 * @param p_header_size the size of the data contained in the PLT marker.
4405 * @param p_manager the user event manager.
4407 opj_bool j2k_read_plt_v2 (
4408 opj_j2k_v2_t *p_j2k,
4409 OPJ_BYTE * p_header_data,
4410 OPJ_UINT32 p_header_size,
4411 struct opj_event_mgr * p_manager
4414 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4417 assert(p_header_data != 00);
4418 assert(p_j2k != 00);
4419 assert(p_manager != 00);
4421 if (p_header_size < 1) {
4422 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4426 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4430 for (i = 0; i < p_header_size; ++i) {
4431 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4433 /* take only the last seven bytes */
4434 l_packet_len |= (l_tmp & 0x7f);
4439 /* store packet length and proceed to next packet */
4444 if (l_packet_len != 0) {
4445 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4452 static void j2k_read_ppm(opj_j2k_t *j2k) {
4453 int len, Z_ppm, i, j;
4456 opj_cp_t *cp = j2k->cp;
4457 opj_cio_t *cio = j2k->cio;
4459 len = cio_read(cio, 2);
4462 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4465 if (cp->ppm_previous == 0) {
4466 N_ppm = cio_read(cio, 4); /* N_ppm */
4469 N_ppm = cp->ppm_previous;
4472 if (Z_ppm == 0) { /* First PPM marker */
4473 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4474 cp->ppm_data_first = cp->ppm_data;
4475 cp->ppm_len = N_ppm;
4476 } else { /* NON-first PPM marker */
4477 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4480 /* this memory allocation check could be done even in non-JPWL cases */
4482 if (!cp->ppm_data) {
4483 opj_event_msg(j2k->cinfo, EVT_ERROR,
4484 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4486 if (!JPWL_ASSUME || JPWL_ASSUME) {
4487 opj_free(cp->ppm_data);
4488 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4495 cp->ppm_data_first = cp->ppm_data;
4496 cp->ppm_len = N_ppm + cp->ppm_store;
4498 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4499 cp->ppm_data[j] = cio_read(cio, 1);
4503 break; /* Case of non-finished packet header in present marker but finished in next one */
4505 cp->ppm_previous = i - 1;
4510 * Reads a PPM marker (Packed packet headers, main header)
4512 * @param p_header_data the data contained in the POC box.
4513 * @param p_j2k the jpeg2000 codec.
4514 * @param p_header_size the size of the data contained in the POC marker.
4515 * @param p_manager the user event manager.
4518 opj_bool j2k_read_ppm_v2 (
4519 opj_j2k_v2_t *p_j2k,
4520 OPJ_BYTE * p_header_data,
4521 OPJ_UINT32 p_header_size,
4522 struct opj_event_mgr * p_manager
4526 opj_cp_v2_t *l_cp = 00;
4527 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4530 assert(p_header_data != 00);
4531 assert(p_j2k != 00);
4532 assert(p_manager != 00);
4534 if (p_header_size < 1) {
4535 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4539 l_cp = &(p_j2k->m_cp);
4542 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4546 /* First PPM marker */
4548 if (p_header_size < 4) {
4549 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4553 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4557 /* First PPM marker: Initialization */
4558 l_cp->ppm_len = l_N_ppm;
4559 l_cp->ppm_data_size = 0;
4561 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4562 if (l_cp->ppm_buffer == 00) {
4563 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4566 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4568 l_cp->ppm_data = l_cp->ppm_buffer;
4572 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4573 if (p_header_size >= 4) {
4575 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4578 l_cp->ppm_len += l_N_ppm ;
4580 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4581 if (l_cp->ppm_buffer == 00) {
4582 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4585 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4587 l_cp->ppm_data = l_cp->ppm_buffer;
4594 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4596 if (l_remaining_data <= p_header_size) {
4597 /* we must store less information than available in the packet */
4598 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4599 l_cp->ppm_data_size = l_cp->ppm_len;
4600 p_header_size -= l_remaining_data;
4601 p_header_data += l_remaining_data;
4604 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4605 l_cp->ppm_data_size += p_header_size;
4606 p_header_data += p_header_size;
4619 * Reads a PPM marker (Packed packet headers, main header)
4621 * @param p_header_data the data contained in the POC box.
4622 * @param p_j2k the jpeg2000 codec.
4623 * @param p_header_size the size of the data contained in the POC marker.
4624 * @param p_manager the user event manager.
4626 opj_bool j2k_read_ppm_v3 (
4627 opj_j2k_v2_t *p_j2k,
4628 OPJ_BYTE * p_header_data,
4629 OPJ_UINT32 p_header_size,
4630 struct opj_event_mgr * p_manager
4633 opj_cp_v2_t *l_cp = 00;
4634 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4637 assert(p_header_data != 00);
4638 assert(p_j2k != 00);
4639 assert(p_manager != 00);
4641 /* Minimum size of PPM marker is equal to the size of Zppm element */
4642 if (p_header_size < 1) {
4643 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4647 l_cp = &(p_j2k->m_cp);
4650 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4654 /* First PPM marker */
4656 /* We need now at least the Nppm^0 element */
4657 if (p_header_size < 4) {
4658 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4662 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4666 /* First PPM marker: Initialization */
4667 l_cp->ppm_len = l_N_ppm;
4668 l_cp->ppm_data_read = 0;
4670 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4671 if (l_cp->ppm_data == 00) {
4672 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4675 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4677 l_cp->ppm_data_current = l_cp->ppm_data;
4679 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4682 if (p_header_size < 4) {
4683 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4687 /* Uncompleted Ippm series in the previous PPM marker?*/
4688 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4689 /* Get the place where add the remaining Ippm series*/
4690 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4691 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4694 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4698 /* Increase the size of ppm_data to add the new Ippm series*/
4699 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4701 /* Keep the position of the place where concatenate the new series*/
4702 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4703 l_cp->ppm_len += l_N_ppm;
4708 l_remaining_data = p_header_size;
4710 while (l_remaining_data >= l_N_ppm) {
4711 /* read a complete Ippm series*/
4712 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4713 p_header_size -= l_N_ppm;
4714 p_header_data += l_N_ppm;
4716 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4720 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4725 l_remaining_data = p_header_size;
4729 l_remaining_data = p_header_size;
4731 /* Next Ippm series is a complete series ?*/
4732 if (l_remaining_data > l_N_ppm) {
4733 /* Increase the size of ppm_data to add the new Ippm series*/
4734 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4736 /* Keep the position of the place where concatenate the new series */
4737 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4738 l_cp->ppm_len += l_N_ppm;
4743 /* Need to read an incomplete Ippm series*/
4744 if (l_remaining_data) {
4745 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4747 /* Keep the position of the place where concatenate the new series*/
4748 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4749 l_cp->ppm_len += l_N_ppm;
4751 /* Read incomplete Ippm series*/
4752 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4753 p_header_size -= l_remaining_data;
4754 p_header_data += l_remaining_data;
4756 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4761 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4762 if (p_header_size >= 4) {
4764 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4767 l_cp->ppm_len += l_N_ppm ;
4769 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4770 if (l_cp->ppm_buffer == 00) {
4771 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4774 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4776 l_cp->ppm_data = l_cp->ppm_buffer;
4783 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4785 if (l_remaining_data <= p_header_size) {
4786 /* we must store less information than available in the packet */
4787 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4788 l_cp->ppm_data_size = l_cp->ppm_len;
4789 p_header_size -= l_remaining_data;
4790 p_header_data += l_remaining_data;
4793 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4794 l_cp->ppm_data_size += p_header_size;
4795 p_header_data += p_header_size;
4804 static void j2k_read_ppt(opj_j2k_t *j2k) {
4805 int len, Z_ppt, i, j = 0;
4807 opj_cp_t *cp = j2k->cp;
4808 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4809 opj_cio_t *cio = j2k->cio;
4811 len = cio_read(cio, 2);
4812 Z_ppt = cio_read(cio, 1);
4814 if (Z_ppt == 0) { /* First PPT marker */
4815 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4816 tcp->ppt_data_first = tcp->ppt_data;
4818 tcp->ppt_len = len - 3;
4819 } else { /* NON-first PPT marker */
4820 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4821 tcp->ppt_data_first = tcp->ppt_data;
4822 tcp->ppt_len = len - 3 + tcp->ppt_store;
4825 for (i = len - 3; i > 0; i--) {
4826 tcp->ppt_data[j] = cio_read(cio, 1);
4833 * Reads a PPT marker (Packed packet headers, tile-part header)
4835 * @param p_header_data the data contained in the PPT box.
4836 * @param p_j2k the jpeg2000 codec.
4837 * @param p_header_size the size of the data contained in the PPT marker.
4838 * @param p_manager the user event manager.
4840 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4841 OPJ_BYTE * p_header_data,
4842 OPJ_UINT32 p_header_size,
4843 struct opj_event_mgr * p_manager )
4845 opj_cp_v2_t *l_cp = 00;
4846 opj_tcp_v2_t *l_tcp = 00;
4850 assert(p_header_data != 00);
4851 assert(p_j2k != 00);
4852 assert(p_manager != 00);
4854 /* We need to have the Z_ppt element at minimum */
4855 if (p_header_size < 1) {
4856 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4860 l_cp = &(p_j2k->m_cp);
4862 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");
4866 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4869 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4873 /* Allocate buffer to read the packet header */
4875 /* First PPT marker */
4876 l_tcp->ppt_data_size = 0;
4877 l_tcp->ppt_len = p_header_size;
4879 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4880 if (l_tcp->ppt_buffer == 00) {
4881 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4884 l_tcp->ppt_data = l_tcp->ppt_buffer;
4886 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4889 l_tcp->ppt_len += p_header_size;
4891 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4892 if (l_tcp->ppt_buffer == 00) {
4893 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4896 l_tcp->ppt_data = l_tcp->ppt_buffer;
4898 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4901 /* Read packet header from buffer */
4902 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4904 l_tcp->ppt_data_size += p_header_size;
4909 static void j2k_write_tlm(opj_j2k_t *j2k){
4911 opj_cio_t *cio = j2k->cio;
4912 j2k->tlm_start = cio_tell(cio);
4913 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4914 lenp = 4 + (5*j2k->totnum_tp);
4915 cio_write(cio,lenp,2); /* Ltlm */
4916 cio_write(cio, 0,1); /* Ztlm=0*/
4917 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4918 cio_skip(cio,5*j2k->totnum_tp);
4921 static void j2k_write_sot(opj_j2k_t *j2k) {
4924 opj_cio_t *cio = j2k->cio;
4926 j2k->sot_start = cio_tell(cio);
4927 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4928 lenp = cio_tell(cio);
4929 cio_skip(cio, 2); /* Lsot (further) */
4930 cio_write(cio, j2k->curtileno, 2); /* Isot */
4931 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4932 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4933 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4934 len = cio_tell(cio) - lenp;
4935 cio_seek(cio, lenp);
4936 cio_write(cio, len, 2); /* Lsot */
4937 cio_seek(cio, lenp + len);
4941 /* update markers struct */
4942 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4943 #endif /* USE_JPWL */
4946 if( j2k->cstr_info && j2k->cur_tp_num==0){
4947 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4952 * Writes the SOT marker (Start of tile-part)
4954 * @param p_stream the stream to write data to.
4955 * @param p_j2k J2K codec.
4956 * @param p_manager the user event manager.
4958 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4960 OPJ_UINT32 * p_data_written,
4961 const struct opj_stream_private *p_stream,
4962 struct opj_event_mgr * p_manager )
4965 assert(p_j2k != 00);
4966 assert(p_manager != 00);
4967 assert(p_stream != 00);
4969 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4972 opj_write_bytes(p_data,10,2); /* Lsot */
4975 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4981 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4984 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4989 /* update markers struct */
4991 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4993 assert( 0 && "TODO" );
4994 #endif /* USE_JPWL */
4996 * p_data_written = 12;
5002 static void j2k_read_sot(opj_j2k_t *j2k) {
5003 int len, tileno, totlen, partno, numparts;
5005 opj_tcp_t *tcp = NULL;
5008 opj_cp_t *cp = j2k->cp;
5009 opj_cio_t *cio = j2k->cio;
5011 len = cio_read(cio, 2);
5012 tileno = cio_read(cio, 2);
5015 if (j2k->cp->correct) {
5017 static int backup_tileno = 0;
5019 /* tileno is negative or larger than the number of tiles!!! */
5020 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
5021 opj_event_msg(j2k->cinfo, EVT_ERROR,
5022 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5023 tileno, (cp->tw * cp->th));
5025 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5028 /* we try to correct */
5029 tileno = backup_tileno;
5030 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
5031 "- setting tile number to %d\n",
5035 /* keep your private count of tiles */
5038 #endif /* USE_JPWL */
5040 if (cp->tileno_size == 0) {
5041 cp->tileno[cp->tileno_size] = tileno;
5045 assert(cp->tileno_size >= 0);
5046 while (i < (OPJ_UINT32)cp->tileno_size && status == 0) {
5047 status = cp->tileno[i] == tileno ? 1 : 0;
5051 cp->tileno[cp->tileno_size] = tileno;
5056 totlen = cio_read(cio, 4);
5059 if (j2k->cp->correct) {
5061 /* totlen is negative or larger than the bytes left!!! */
5062 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
5063 opj_event_msg(j2k->cinfo, EVT_ERROR,
5064 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5065 totlen, cio_numbytesleft(cio) + 8);
5067 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5070 /* we try to correct */
5072 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
5073 "- setting Psot to %d => assuming it is the last tile\n",
5078 #endif /* USE_JPWL */
5081 totlen = cio_numbytesleft(cio) + 8;
5083 partno = cio_read(cio, 1);
5084 numparts = cio_read(cio, 1);
5086 if (partno >= numparts) {
5087 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);
5088 numparts = partno+1;
5091 j2k->curtileno = tileno;
5092 j2k->cur_tp_num = partno;
5093 j2k->eot = cio_getbp(cio) - 12 + totlen;
5094 j2k->state = J2K_STATE_TPH;
5095 tcp = &cp->tcps[j2k->curtileno];
5098 if (j2k->cstr_info) {
5101 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
5102 j2k->cstr_info->tile[tileno].tileno = tileno;
5103 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
5104 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5106 j2k->cstr_info->tile[tileno].end_pos += totlen;
5108 j2k->cstr_info->tile[tileno].num_tps = numparts;
5110 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
5112 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)*/
5113 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
5114 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5115 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5118 if (tcp->first == 1) {
5119 /* Initialization PPT */
5120 opj_tccp_t *tmp = tcp->tccps;
5121 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
5123 tcp->ppt_data = NULL;
5124 tcp->ppt_data_first = NULL;
5127 for (i = 0; i < j2k->image->numcomps; i++) {
5128 tcp->tccps[i] = j2k->default_tcp->tccps[i];
5130 cp->tcps[j2k->curtileno].first = 0;
5135 * Reads a PPT marker (Packed packet headers, tile-part header)
5137 * @param p_header_data the data contained in the PPT box.
5138 * @param p_j2k the jpeg2000 codec.
5139 * @param p_header_size the size of the data contained in the PPT marker.
5140 * @param p_manager the user event manager.
5142 opj_bool j2k_read_sot_v2 (
5143 opj_j2k_v2_t *p_j2k,
5144 OPJ_BYTE * p_header_data,
5145 OPJ_UINT32 p_header_size,
5146 struct opj_event_mgr * p_manager
5150 opj_cp_v2_t *l_cp = 00;
5151 opj_tcp_v2_t *l_tcp = 00;
5152 OPJ_UINT32 l_tot_len, l_num_parts = 0;
5153 OPJ_UINT32 l_current_part;
5154 OPJ_UINT32 l_tile_x,l_tile_y;
5157 assert(p_header_data != 00);
5158 assert(p_j2k != 00);
5159 assert(p_manager != 00);
5161 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
5162 if (p_header_size != 8) {
5163 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
5167 l_cp = &(p_j2k->m_cp);
5168 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
5171 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5172 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
5173 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
5176 if (l_cp->correct) {
5178 int tileno = p_j2k->m_current_tile_number;
5179 static int backup_tileno = 0;
5181 /* tileno is negative or larger than the number of tiles!!! */
5182 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
5183 opj_event_msg_v2(p_manager, EVT_ERROR,
5184 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5185 tileno, (l_cp->tw * l_cp->th));
5187 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5190 /* we try to correct */
5191 tileno = backup_tileno;
5192 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5193 "- setting tile number to %d\n",
5197 /* keep your private count of tiles */
5200 #endif /* USE_JPWL */
5202 /* look for the tile in the list of already processed tile (in parts). */
5203 /* Optimization possible here with a more complex data structure and with the removing of tiles */
5204 /* since the time taken by this function can only grow at the time */
5206 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
5209 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
5210 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
5212 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5218 if (l_cp->correct) {
5220 /* totlen is negative or larger than the bytes left!!! */
5221 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))) { */
5222 opj_event_msg_v2(p_manager, EVT_ERROR,
5223 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5224 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5226 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5229 /* we try to correct */
5231 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5232 "- setting Psot to %d => assuming it is the last tile\n",
5236 #endif /* USE_JPWL */
5238 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5240 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5241 "we assuming it is the last tile-part of the codestream.\n");
5242 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5245 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5248 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5251 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5252 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5253 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5254 if (l_tcp->m_nb_tile_parts) {
5255 if (l_current_part >= l_tcp->m_nb_tile_parts){
5256 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5257 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5258 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5262 l_tcp->m_nb_tile_parts = l_num_parts;
5265 /* If know the number of tile part header we will check if we didn't read the last*/
5266 if (l_tcp->m_nb_tile_parts) {
5267 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5268 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5272 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5273 /* Keep the size of data to skip after this marker */
5274 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5277 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5278 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5281 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5283 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5284 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5285 p_j2k->m_specific_param.m_decoder.m_skip_data =
5286 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5287 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5288 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5289 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5292 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5293 p_j2k->m_specific_param.m_decoder.m_skip_data =
5294 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5298 if (p_j2k->cstr_index)
5300 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5301 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5303 if (l_num_parts != 0){
5304 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5305 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5308 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5309 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5310 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5312 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5313 (opj_tp_index_t*)opj_realloc(
5314 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5315 l_num_parts* sizeof(opj_tp_index_t));
5318 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5320 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5321 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5322 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5323 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5324 sizeof(opj_tp_index_t));
5327 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5328 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5329 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5330 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5331 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5332 * sizeof(opj_tp_index_t));
5341 /* 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 */
5342 /* if (p_j2k->cstr_info) {
5345 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5348 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5349 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5350 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5351 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5354 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5357 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5361 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5364 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5365 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5366 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5371 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5374 opj_tcp_t *tcp = NULL;
5375 opj_codestream_info_t *cstr_info = NULL;
5377 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5378 opj_cp_t *cp = j2k->cp;
5379 opj_cio_t *cio = j2k->cio;
5381 tcd->tp_num = j2k->tp_num ;
5382 tcd->cur_tp_num = j2k->cur_tp_num;
5384 cio_write(cio, J2K_MS_SOD, 2);
5386 if( j2k->cstr_info && j2k->cur_tp_num==0){
5387 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5390 if (j2k->curtileno == 0) {
5391 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5395 cstr_info = j2k->cstr_info;
5397 if (!j2k->cur_tp_num ) {
5398 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5399 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5402 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5403 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5407 /* update markers struct */
5408 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5409 #endif /* USE_JPWL */
5414 tcp = &cp->tcps[j2k->curtileno];
5415 for (layno = 0; layno < tcp->numlayers; layno++) {
5416 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5417 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5418 } else if (tcp->rates[layno]) {
5419 tcp->rates[layno]=1;
5422 if(j2k->cur_tp_num == 0){
5423 tcd->tcd_image->tiles->packno = 0;
5425 cstr_info->packno = 0;
5428 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5430 /* Writing Psot in SOT marker */
5431 totlen = cio_tell(cio) + l - j2k->sot_start;
5432 cio_seek(cio, j2k->sot_start + 6);
5433 cio_write(cio, totlen, 4);
5434 cio_seek(cio, j2k->sot_start + totlen);
5435 /* Writing Ttlm and Ptlm in TLM marker */
5437 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5438 cio_write(cio, j2k->curtileno, 1);
5439 cio_write(cio, totlen, 4);
5441 cio_seek(cio, j2k->sot_start + totlen);
5445 * Writes the SOD marker (Start of data)
5447 * @param p_stream the stream to write data to.
5448 * @param p_j2k J2K codec.
5449 * @param p_manager the user event manager.
5451 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5452 struct opj_tcd_v2 * p_tile_coder,
5454 OPJ_UINT32 * p_data_written,
5455 OPJ_UINT32 p_total_data_size,
5456 const struct opj_stream_private *p_stream,
5457 struct opj_event_mgr * p_manager )
5459 opj_tcp_v2_t *l_tcp = 00;
5460 opj_codestream_info_t *l_cstr_info = 00;
5461 opj_cp_v2_t *l_cp = 00;
5463 OPJ_UINT32 l_size_tile;
5464 OPJ_UINT32 l_remaining_data;
5467 assert(p_j2k != 00);
5468 assert(p_manager != 00);
5469 assert(p_stream != 00);
5471 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5474 /* make room for the EOF marker */
5475 l_remaining_data = p_total_data_size - 4;
5477 l_cp = &(p_j2k->m_cp);
5478 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5481 /* update tile coder */
5482 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5483 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5485 l_size_tile = l_cp->th * l_cp->tw;
5488 /* TODO mergeV2: check this part which use cstr_info */
5489 /*l_cstr_info = p_j2k->cstr_info;
5491 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5492 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5493 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5499 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5501 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5506 /* update markers struct */
5507 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5509 assert( 0 && "TODO" );
5510 #endif /* USE_JPWL */
5515 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5516 p_tile_coder->tcd_image->tiles->packno = 0;
5518 l_cstr_info->packno = 0;
5522 *p_data_written = 0;
5524 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)) {
5525 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5529 *p_data_written += 2;
5535 static void j2k_read_sod(opj_j2k_t *j2k) {
5536 int len, truncate = 0, i;
5537 unsigned char *data = NULL, *data_ptr = NULL;
5539 opj_cio_t *cio = j2k->cio;
5540 int curtileno = j2k->curtileno;
5543 if (j2k->cstr_info) {
5544 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5545 cio_tell(cio) + j2k->pos_correction - 1;
5546 if (j2k->cur_tp_num == 0)
5547 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5548 j2k->cstr_info->packno = 0;
5551 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
5553 if (len == cio_numbytesleft(cio) + 1) {
5554 truncate = 1; /* Case of a truncate codestream */
5557 data = j2k->tile_data[curtileno];
5558 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
5560 data_ptr = data + j2k->tile_len[curtileno];
5561 for (i = 0; i < len; i++) {
5562 data_ptr[i] = cio_read(cio, 1);
5565 j2k->tile_len[curtileno] += len;
5566 j2k->tile_data[curtileno] = data;
5569 j2k->state = J2K_STATE_TPHSOT;
5571 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
5577 * Reads a SOD marker (Start Of Data)
5579 * @param p_header_data the data contained in the SOD box.
5580 * @param p_j2k the jpeg2000 codec.
5581 * @param p_header_size the size of the data contained in the SOD marker.
5582 * @param p_manager the user event manager.
5584 opj_bool j2k_read_sod_v2 (
5585 opj_j2k_v2_t *p_j2k,
5586 struct opj_stream_private *p_stream,
5587 struct opj_event_mgr * p_manager
5590 OPJ_UINT32 l_current_read_size;
5591 opj_codestream_index_t * l_cstr_index = 00;
5592 OPJ_BYTE ** l_current_data = 00;
5593 opj_tcp_v2_t * l_tcp = 00;
5594 OPJ_UINT32 * l_tile_len = 00;
5597 assert(p_j2k != 00);
5598 assert(p_manager != 00);
5599 assert(p_stream != 00);
5601 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5603 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5604 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5605 // but we are in the last tile part,
5606 // so its result will fit on OPJ_UINT32 unless we find
5607 // a file with a single tile part of more than 4 GB...*/
5608 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5611 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5613 l_current_data = &(l_tcp->m_data);
5614 l_tile_len = &l_tcp->m_data_size;
5616 if (! *l_current_data) {
5617 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5620 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5623 if (*l_current_data == 00) {
5624 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5630 l_cstr_index = p_j2k->cstr_index;
5632 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5634 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5635 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5637 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5638 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5640 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5644 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5646 /*l_cstr_index->packno = 0;*/
5649 l_current_read_size = opj_stream_read_data( p_stream,
5650 *l_current_data + *l_tile_len,
5651 p_j2k->m_specific_param.m_decoder.m_sot_length,
5654 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5655 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5658 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5661 *l_tile_len += l_current_read_size;
5667 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5668 opj_cp_t *cp = j2k->cp;
5669 opj_tcp_t *tcp = &cp->tcps[tileno];
5670 opj_cio_t *cio = j2k->cio;
5671 int numcomps = j2k->image->numcomps;
5673 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5674 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5675 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5676 cio_write(cio, 0, 1); /* Srgn */
5677 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5681 * Writes the RGN marker (Region Of Interest)
5683 * @param p_tile_no the tile to output
5684 * @param p_comp_no the component to output
5685 * @param p_stream the stream to write data to.
5686 * @param p_j2k J2K codec.
5687 * @param p_manager the user event manager.
5689 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5690 OPJ_UINT32 p_tile_no,
5691 OPJ_UINT32 p_comp_no,
5692 struct opj_stream_private *p_stream,
5693 struct opj_event_mgr * p_manager )
5695 OPJ_BYTE * l_current_data = 00;
5696 OPJ_UINT32 l_nb_comp;
5697 OPJ_UINT32 l_rgn_size;
5698 opj_image_t *l_image = 00;
5699 opj_cp_v2_t *l_cp = 00;
5700 opj_tcp_v2_t *l_tcp = 00;
5701 opj_tccp_t *l_tccp = 00;
5702 OPJ_UINT32 l_comp_room;
5705 assert(p_j2k != 00);
5706 assert(p_manager != 00);
5707 assert(p_stream != 00);
5709 l_cp = &(p_j2k->m_cp);
5710 l_tcp = &l_cp->tcps[p_tile_no];
5711 l_tccp = &l_tcp->tccps[p_comp_no];
5713 l_nb_comp = l_image->numcomps;
5715 if (l_nb_comp <= 256) {
5722 l_rgn_size = 6 + l_comp_room;
5724 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5726 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5727 l_current_data += 2;
5729 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5730 l_current_data += 2;
5732 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5733 l_current_data+=l_comp_room;
5735 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5738 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5741 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) {
5748 static void j2k_read_rgn(opj_j2k_t *j2k) {
5749 int len, compno, roisty;
5751 opj_cp_t *cp = j2k->cp;
5752 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5753 opj_cio_t *cio = j2k->cio;
5754 int numcomps = j2k->image->numcomps;
5756 len = cio_read(cio, 2); /* Lrgn */
5757 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
5758 roisty = cio_read(cio, 1); /* Srgn */
5761 if (j2k->cp->correct) {
5762 /* totlen is negative or larger than the bytes left!!! */
5763 if (compno >= numcomps) {
5764 opj_event_msg(j2k->cinfo, EVT_ERROR,
5765 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5767 if (!JPWL_ASSUME || JPWL_ASSUME) {
5768 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5773 #endif /* USE_JPWL */
5775 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
5778 static void j2k_write_eoc(opj_j2k_t *j2k) {
5779 opj_cio_t *cio = j2k->cio;
5780 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5781 cio_write(cio, J2K_MS_EOC, 2);
5785 /* update markers struct */
5786 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5787 #endif /* USE_JPWL */
5792 * Reads a RGN marker (Region Of Interest)
5794 * @param p_header_data the data contained in the POC box.
5795 * @param p_j2k the jpeg2000 codec.
5796 * @param p_header_size the size of the data contained in the POC marker.
5797 * @param p_manager the user event manager.
5799 opj_bool j2k_read_rgn_v2 (
5800 opj_j2k_v2_t *p_j2k,
5801 OPJ_BYTE * p_header_data,
5802 OPJ_UINT32 p_header_size,
5803 struct opj_event_mgr * p_manager
5806 OPJ_UINT32 l_nb_comp;
5807 opj_image_t * l_image = 00;
5809 opj_cp_v2_t *l_cp = 00;
5810 opj_tcp_v2_t *l_tcp = 00;
5811 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5814 assert(p_header_data != 00);
5815 assert(p_j2k != 00);
5816 assert(p_manager != 00);
5818 l_image = p_j2k->m_private_image;
5819 l_nb_comp = l_image->numcomps;
5821 if (l_nb_comp <= 256) {
5826 if (p_header_size != 2 + l_comp_room) {
5827 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5831 l_cp = &(p_j2k->m_cp);
5832 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5833 &l_cp->tcps[p_j2k->m_current_tile_number] :
5834 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5836 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5837 p_header_data+=l_comp_room;
5838 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5842 if (l_cp->correct) {
5843 /* totlen is negative or larger than the bytes left!!! */
5844 if (l_comp_room >= l_nb_comp) {
5845 opj_event_msg_v2(p_manager, EVT_ERROR,
5846 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5847 l_comp_room, l_nb_comp);
5848 if (!JPWL_ASSUME || JPWL_ASSUME) {
5849 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5854 #endif /* USE_JPWL */
5856 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5863 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5865 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5868 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5875 * Updates the rates of the tcp.
5877 * @param p_stream the stream to write data to.
5878 * @param p_j2k J2K codec.
5879 * @param p_manager the user event manager.
5881 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5882 struct opj_stream_private *p_stream,
5883 struct opj_event_mgr * p_manager )
5885 opj_cp_v2_t * l_cp = 00;
5886 opj_image_t * l_image = 00;
5887 opj_tcp_v2_t * l_tcp = 00;
5888 opj_image_comp_t * l_img_comp = 00;
5891 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5892 OPJ_FLOAT32 * l_rates = 0;
5893 OPJ_FLOAT32 l_sot_remove;
5894 OPJ_UINT32 l_bits_empty, l_size_pixel;
5895 OPJ_UINT32 l_tile_size = 0;
5896 OPJ_UINT32 l_last_res;
5897 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5900 assert(p_j2k != 00);
5901 assert(p_manager != 00);
5902 assert(p_stream != 00);
5905 l_cp = &(p_j2k->m_cp);
5906 l_image = p_j2k->m_private_image;
5909 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5910 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5911 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5913 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5914 l_tp_stride_func = get_tp_stride;
5917 l_tp_stride_func = get_default_stride;
5920 for (i=0;i<l_cp->th;++i) {
5921 for (j=0;j<l_cp->tw;++j) {
5922 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5924 /* 4 borders of the tile rescale on the image if necessary */
5925 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5926 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5927 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5928 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5930 l_rates = l_tcp->rates;
5932 /* Modification of the RATE >> */
5934 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5936 ((*l_rates) * l_bits_empty)
5944 for (k = 1; k < l_tcp->numlayers; ++k) {
5946 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5948 ((*l_rates) * l_bits_empty)
5964 for (i=0;i<l_cp->th;++i) {
5965 for (j=0;j<l_cp->tw;++j) {
5966 l_rates = l_tcp->rates;
5969 *l_rates -= l_sot_remove;
5971 if (*l_rates < 30) {
5978 l_last_res = l_tcp->numlayers - 1;
5980 for (k = 1; k < l_last_res; ++k) {
5983 *l_rates -= l_sot_remove;
5985 if (*l_rates < *(l_rates - 1) + 10) {
5986 *l_rates = (*(l_rates - 1)) + 20;
5994 *l_rates -= (l_sot_remove + 2.f);
5996 if (*l_rates < *(l_rates - 1) + 10) {
5997 *l_rates = (*(l_rates - 1)) + 20;
6005 l_img_comp = l_image->comps;
6008 for (i=0;i<l_image->numcomps;++i) {
6009 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
6011 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
6019 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
6021 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
6023 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
6024 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
6025 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
6026 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
6030 if (l_cp->m_specific_param.m_enc.m_cinema) {
6031 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
6032 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
6033 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6037 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
6038 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
6044 static void j2k_read_eoc(opj_j2k_t *j2k) {
6048 /* if packets should be decoded */
6049 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
6050 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
6051 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
6052 for (i = 0; i < j2k->cp->tileno_size; i++) {
6053 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
6054 tileno = j2k->cp->tileno[i];
6055 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
6056 opj_free(j2k->tile_data[tileno]);
6057 j2k->tile_data[tileno] = NULL;
6058 tcd_free_decode_tile(tcd, i);
6059 if (success == OPJ_FALSE) {
6060 j2k->state |= J2K_STATE_ERR;
6064 tcd_free_decode(tcd);
6067 /* if packets should not be decoded */
6069 for (i = 0; i < j2k->cp->tileno_size; i++) {
6070 tileno = j2k->cp->tileno[i];
6071 opj_free(j2k->tile_data[tileno]);
6072 j2k->tile_data[tileno] = NULL;
6075 if (j2k->state & J2K_STATE_ERR)
6076 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
6078 j2k->state = J2K_STATE_MT;
6082 * Reads a EOC marker (End Of Codestream)
6084 * @param p_header_data the data contained in the SOD box.
6085 * @param p_j2k the jpeg2000 codec.
6086 * @param p_header_size the size of the data contained in the SOD marker.
6087 * @param p_manager the user event manager.
6090 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
6091 struct opj_stream_private *p_stream,
6092 struct opj_event_mgr * p_manager )
6095 opj_tcd_v2_t * l_tcd = 00;
6096 OPJ_UINT32 l_nb_tiles;
6097 opj_tcp_v2_t * l_tcp = 00;
6101 assert(p_j2k != 00);
6102 assert(p_manager != 00);
6103 assert(p_stream != 00);
6105 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6106 l_tcp = p_j2k->m_cp.tcps;
6108 l_tcd = tcd_create_v2(OPJ_TRUE);
6110 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6114 for (i = 0; i < l_nb_tiles; ++i) {
6115 if (l_tcp->m_data) {
6116 if (! tcd_init_decode_tile(l_tcd, i)) {
6117 tcd_destroy_v2(l_tcd);
6118 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6122 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
6126 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
6131 j2k_tcp_destroy(l_tcp);
6135 tcd_destroy_v2(l_tcd);
6141 * Gets the offset of the header.
6143 * @param p_stream the stream to write data to.
6144 * @param p_j2k J2K codec.
6145 * @param p_manager the user event manager.
6147 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
6148 struct opj_stream_private *p_stream,
6149 struct opj_event_mgr * p_manager )
6152 assert(p_j2k != 00);
6153 assert(p_manager != 00);
6154 assert(p_stream != 00);
6156 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
6162 * Writes the MCT marker (Multiple Component Transform)
6164 * @param p_stream the stream to write data to.
6165 * @param p_j2k J2K codec.
6166 * @param p_manager the user event manager.
6168 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
6169 struct opj_stream_private *p_stream,
6170 struct opj_event_mgr * p_manager )
6173 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6174 opj_mct_data_t * l_mct_record;
6175 opj_tcp_v2_t * l_tcp;
6178 assert(p_j2k != 00);
6179 assert(p_stream != 00);
6180 assert(p_manager != 00);
6182 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
6186 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6187 l_mct_record = l_tcp->m_mct_records;
6189 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6191 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
6198 l_mcc_record = l_tcp->m_mcc_records;
6200 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6202 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6209 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6217 * Writes the image components.
6219 * @param p_stream the stream to write data to.
6220 * @param p_j2k J2K codec.
6221 * @param p_manager the user event manager.
6223 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6224 struct opj_stream_private *p_stream,
6225 struct opj_event_mgr * p_manager )
6230 assert(p_j2k != 00);
6231 assert(p_manager != 00);
6232 assert(p_stream != 00);
6234 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6236 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6240 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6249 * Writes regions of interests.
6251 * @param p_stream the stream to write data to.
6252 * @param p_j2k J2K codec.
6253 * @param p_manager the user event manager.
6255 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
6256 struct opj_stream_private *p_stream,
6257 struct opj_event_mgr * p_manager )
6260 const opj_tccp_t *l_tccp = 00;
6263 assert(p_j2k != 00);
6264 assert(p_manager != 00);
6265 assert(p_stream != 00);
6267 l_tccp = p_j2k->m_cp.tcps->tccps;
6269 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6270 if (l_tccp->roishift) {
6272 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6286 * @param p_stream the stream to write data to.
6287 * @param p_j2k J2K codec.
6288 * @param p_manager the user event manager.
6290 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6291 struct opj_stream_private *p_stream,
6292 struct opj_event_mgr * p_manager )
6294 opj_codestream_index_t * l_cstr_index = 00;
6297 assert(p_j2k != 00);
6298 assert(p_manager != 00);
6299 assert(p_stream != 00);
6301 l_cstr_index = p_j2k->cstr_index;
6303 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6305 /* The following adjustment is done to adjust the codestream size */
6306 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6307 /* the first bunch of bytes is not in the codestream */
6308 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6313 /* preparation of JPWL marker segments */
6317 /* encode according to JPWL */
6318 jpwl_encode(p_j2k, p_stream, image);
6322 assert( 0 && "TODO" );
6323 #endif /* USE_JPWL */
6328 typedef struct opj_dec_mstabent {
6331 /** value of the state when the marker can appear */
6333 /** action linked to the marker */
6334 void (*handler) (opj_j2k_t *j2k);
6335 } opj_dec_mstabent_t;
6337 opj_dec_mstabent_t j2k_dec_mstab[] = {
6338 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
6339 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
6340 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
6341 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6342 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
6343 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
6344 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6345 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6346 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6347 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6348 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6349 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6350 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6351 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6352 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6353 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6355 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6356 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6359 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6360 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6361 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6362 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6363 #endif /* USE_JPWL */
6365 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6366 {J2K_MS_INSEC, 0, j2k_read_insec},
6367 #endif /* USE_JPSEC */
6369 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6372 static void j2k_read_unk(opj_j2k_t *j2k) {
6373 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6376 if (j2k->cp->correct) {
6378 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6379 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6380 id = cio_read(j2k->cio, 2);
6381 opj_event_msg(j2k->cinfo, EVT_ERROR,
6382 "JPWL: really don't know this marker %x\n",
6385 opj_event_msg(j2k->cinfo, EVT_ERROR,
6386 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6389 /* OK, activate this at your own risk!!! */
6390 /* we look for the marker at the minimum hamming distance from this */
6391 while (j2k_dec_mstab[m].id) {
6393 /* 1's where they differ */
6394 tmp_id = j2k_dec_mstab[m].id ^ id;
6396 /* compute the hamming distance between our id and the current */
6398 for (i = 0; i < 16; i++) {
6399 if ((tmp_id >> i) & 0x0001) {
6404 /* if current distance is smaller, set the minimum */
6405 if (cur_dist < min_dist) {
6406 min_dist = cur_dist;
6407 min_id = j2k_dec_mstab[m].id;
6410 /* jump to the next marker */
6414 /* do we substitute the marker? */
6415 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6416 opj_event_msg(j2k->cinfo, EVT_ERROR,
6417 "- marker %x is at distance %d from the read %x\n",
6418 min_id, min_dist, id);
6419 opj_event_msg(j2k->cinfo, EVT_ERROR,
6420 "- trying to substitute in place and crossing fingers!\n");
6421 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6422 cio_write(j2k->cio, min_id, 2);
6425 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6430 #endif /* USE_JPWL */
6435 * Reads an unknown marker
6437 * @param p_stream the stream object to read from.
6438 * @param p_j2k the jpeg2000 codec.
6439 * @param p_manager the user event manager.
6441 * @return true if the marker could be deduced.
6443 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6444 struct opj_stream_private *p_stream,
6445 OPJ_UINT32 *output_marker,
6446 struct opj_event_mgr * p_manager
6449 OPJ_UINT32 l_unknown_marker;
6450 const opj_dec_memory_marker_handler_t * l_marker_handler;
6451 OPJ_UINT32 l_size_unk = 2;
6454 assert(p_j2k != 00);
6455 assert(p_manager != 00);
6456 assert(p_stream != 00);
6458 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6461 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6462 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6463 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6467 /* read 2 bytes as the new marker ID*/
6468 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6470 if (!(l_unknown_marker < 0xff00)) {
6472 /* Get the marker handler from the marker ID*/
6473 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6475 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6476 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6480 if (l_marker_handler->id != J2K_MS_UNK) {
6481 /* Add the marker to the codestream index*/
6482 if (l_marker_handler->id != J2K_MS_SOT)
6483 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6484 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6486 break; /* next marker is known and well located */
6494 *output_marker = l_marker_handler->id ;
6500 Read the lookup table containing all the marker, status and action
6501 @param id Marker value
6503 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6504 opj_dec_mstabent_t *e;
6505 for (e = j2k_dec_mstab; e->id != 0; e++) {
6514 * Writes the MCT marker (Multiple Component Transform)
6516 * @param p_stream the stream to write data to.
6517 * @param p_j2k J2K codec.
6518 * @param p_manager the user event manager.
6520 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6521 opj_mct_data_t * p_mct_record,
6522 struct opj_stream_private *p_stream,
6523 struct opj_event_mgr * p_manager )
6525 OPJ_UINT32 l_mct_size;
6526 OPJ_BYTE * l_current_data = 00;
6530 assert(p_j2k != 00);
6531 assert(p_manager != 00);
6532 assert(p_stream != 00);
6534 l_mct_size = 10 + p_mct_record->m_data_size;
6536 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6537 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6538 = (OPJ_BYTE*)opj_realloc(
6539 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6542 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6546 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6549 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6551 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6552 l_current_data += 2;
6554 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6555 l_current_data += 2;
6557 opj_write_bytes(l_current_data,0,2); /* Zmct */
6558 l_current_data += 2;
6560 /* only one marker atm */
6561 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6563 opj_write_bytes(l_current_data,l_tmp,2);
6564 l_current_data += 2;
6566 opj_write_bytes(l_current_data,0,2); /* Ymct */
6569 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6571 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) {
6579 * Reads a MCT marker (Multiple Component Transform)
6581 * @param p_header_data the data contained in the MCT box.
6582 * @param p_j2k the jpeg2000 codec.
6583 * @param p_header_size the size of the data contained in the MCT marker.
6584 * @param p_manager the user event manager.
6586 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6587 OPJ_BYTE * p_header_data,
6588 OPJ_UINT32 p_header_size,
6589 struct opj_event_mgr * p_manager )
6592 opj_tcp_v2_t *l_tcp = 00;
6595 opj_mct_data_t * l_mct_data;
6598 assert(p_header_data != 00);
6599 assert(p_j2k != 00);
6601 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6602 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6603 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6605 if (p_header_size < 2) {
6606 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6611 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6614 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6618 if(p_header_size <= 6) {
6619 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6623 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6624 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6627 l_indix = l_tmp & 0xff;
6628 l_mct_data = l_tcp->m_mct_records;
6630 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6631 if (l_mct_data->m_index == l_indix) {
6638 if (i == l_tcp->m_nb_mct_records) {
6639 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6640 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6642 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));
6643 if(! l_tcp->m_mct_records) {
6644 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6648 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6649 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6652 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6655 if (l_mct_data->m_data) {
6656 opj_free(l_mct_data->m_data);
6657 l_mct_data->m_data = 00;
6660 l_mct_data->m_index = l_indix;
6661 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6662 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6664 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6667 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6673 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6674 if (! l_mct_data->m_data) {
6675 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6678 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6680 l_mct_data->m_data_size = p_header_size;
6681 ++l_tcp->m_nb_mct_records;
6687 * Writes the MCC marker (Multiple Component Collection)
6689 * @param p_stream the stream to write data to.
6690 * @param p_j2k J2K codec.
6691 * @param p_manager the user event manager.
6693 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6694 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6695 struct opj_stream_private *p_stream,
6696 struct opj_event_mgr * p_manager )
6699 OPJ_UINT32 l_mcc_size;
6700 OPJ_BYTE * l_current_data = 00;
6701 OPJ_UINT32 l_nb_bytes_for_comp;
6706 assert(p_j2k != 00);
6707 assert(p_manager != 00);
6708 assert(p_stream != 00);
6710 if (p_mcc_record->m_nb_comps > 255 ) {
6711 l_nb_bytes_for_comp = 2;
6715 l_nb_bytes_for_comp = 1;
6719 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6720 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6722 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6723 = (OPJ_BYTE*)opj_realloc(
6724 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6726 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6730 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6733 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6735 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6736 l_current_data += 2;
6738 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6739 l_current_data += 2;
6742 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6743 l_current_data += 2;
6745 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6748 /* only one marker atm */
6749 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6752 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6755 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6758 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 */
6761 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6762 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6763 l_current_data+=l_nb_bytes_for_comp;
6766 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 */
6769 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6771 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6772 l_current_data+=l_nb_bytes_for_comp;
6775 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6777 if (p_mcc_record->m_decorrelation_array) {
6778 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6781 if (p_mcc_record->m_offset_array) {
6782 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6785 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6788 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) {
6796 * Reads a MCC marker (Multiple Component Collection)
6798 * @param p_header_data the data contained in the MCC box.
6799 * @param p_j2k the jpeg2000 codec.
6800 * @param p_header_size the size of the data contained in the MCC marker.
6801 * @param p_manager the user event manager.
6803 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6804 OPJ_BYTE * p_header_data,
6805 OPJ_UINT32 p_header_size,
6806 struct opj_event_mgr * p_manager )
6811 opj_tcp_v2_t * l_tcp;
6812 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6813 opj_mct_data_t * l_mct_data;
6814 OPJ_UINT32 l_nb_collections;
6815 OPJ_UINT32 l_nb_comps;
6816 OPJ_UINT32 l_nb_bytes_by_comp;
6820 assert(p_header_data != 00);
6821 assert(p_j2k != 00);
6822 assert(p_manager != 00);
6824 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6825 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6826 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6828 if (p_header_size < 2) {
6829 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6834 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6837 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6841 if (p_header_size < 7) {
6842 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6846 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6849 l_mcc_record = l_tcp->m_mcc_records;
6851 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6852 if (l_mcc_record->m_index == l_indix) {
6859 if (i == l_tcp->m_nb_mcc_records) {
6860 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6861 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6863 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6864 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6865 if (! l_tcp->m_mcc_records) {
6866 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6869 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6870 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));
6872 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6874 l_mcc_record->m_index = l_indix;
6876 /* only one marker atm */
6877 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6880 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6884 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6887 if (l_nb_collections > 1) {
6888 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6894 for (i=0;i<l_nb_collections;++i) {
6895 if (p_header_size < 3) {
6896 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6900 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6904 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6908 opj_read_bytes(p_header_data,&l_nb_comps,2);
6913 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6914 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6916 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6917 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6921 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6923 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6924 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6925 p_header_data+=l_nb_bytes_by_comp;
6928 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6933 opj_read_bytes(p_header_data,&l_nb_comps,2);
6936 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6937 l_nb_comps &= 0x7fff;
6939 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6940 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6944 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6945 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6949 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6951 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6952 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6953 p_header_data+=l_nb_bytes_by_comp;
6956 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6961 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6964 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6965 l_mcc_record->m_decorrelation_array = 00;
6966 l_mcc_record->m_offset_array = 00;
6968 l_indix = l_tmp & 0xff;
6970 l_mct_data = l_tcp->m_mct_records;
6971 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6972 if (l_mct_data->m_index == l_indix) {
6973 l_mcc_record->m_decorrelation_array = l_mct_data;
6979 if (l_mcc_record->m_decorrelation_array == 00) {
6980 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6985 l_indix = (l_tmp >> 8) & 0xff;
6987 l_mct_data = l_tcp->m_mct_records;
6988 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6989 if (l_mct_data->m_index == l_indix) {
6990 l_mcc_record->m_offset_array = l_mct_data;
6996 if (l_mcc_record->m_offset_array == 00) {
6997 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7003 if (p_header_size != 0) {
7004 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7008 ++l_tcp->m_nb_mcc_records;
7015 * Writes the MCO marker (Multiple component transformation ordering)
7017 * @param p_stream the stream to write data to.
7018 * @param p_j2k J2K codec.
7019 * @param p_manager the user event manager.
7021 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
7022 struct opj_stream_private *p_stream,
7023 struct opj_event_mgr * p_manager
7026 OPJ_BYTE * l_current_data = 00;
7027 OPJ_UINT32 l_mco_size;
7028 opj_tcp_v2_t * l_tcp = 00;
7029 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7033 assert(p_j2k != 00);
7034 assert(p_manager != 00);
7035 assert(p_stream != 00);
7037 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
7038 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7040 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
7041 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7043 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7044 = (OPJ_BYTE*)opj_realloc(
7045 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7047 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
7052 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
7055 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
7056 l_current_data += 2;
7058 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
7059 l_current_data += 2;
7061 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
7064 l_mcc_record = l_tcp->m_mcc_records;
7065 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
7066 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
7072 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) {
7080 * Reads a MCO marker (Multiple Component Transform Ordering)
7082 * @param p_header_data the data contained in the MCO box.
7083 * @param p_j2k the jpeg2000 codec.
7084 * @param p_header_size the size of the data contained in the MCO marker.
7085 * @param p_manager the user event manager.
7087 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
7088 OPJ_BYTE * p_header_data,
7089 OPJ_UINT32 p_header_size,
7090 struct opj_event_mgr * p_manager )
7092 OPJ_UINT32 l_tmp, i;
7093 OPJ_UINT32 l_nb_stages;
7094 opj_tcp_v2_t * l_tcp;
7095 opj_tccp_t * l_tccp;
7096 opj_image_t * l_image;
7097 opj_image_comp_t * l_img_comp;
7100 assert(p_header_data != 00);
7101 assert(p_j2k != 00);
7102 assert(p_manager != 00);
7104 l_image = p_j2k->m_private_image;
7105 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7106 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7107 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7109 if (p_header_size < 1) {
7110 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
7114 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
7117 if (l_nb_stages > 1) {
7118 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
7122 if (p_header_size != l_nb_stages + 1) {
7123 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
7127 l_tccp = l_tcp->tccps;
7128 l_img_comp = l_image->comps;
7130 for (i=0;i<l_image->numcomps;++i) {
7131 l_tccp->m_dc_level_shift = 0;
7135 if (l_tcp->m_mct_decoding_matrix) {
7136 opj_free(l_tcp->m_mct_decoding_matrix);
7137 l_tcp->m_mct_decoding_matrix = 00;
7140 for (i=0;i<l_nb_stages;++i) {
7141 opj_read_bytes(p_header_data,&l_tmp,1);
7144 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
7152 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
7155 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7156 opj_mct_data_t * l_deco_array, * l_offset_array;
7157 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
7158 OPJ_UINT32 l_nb_elem;
7159 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
7160 opj_tccp_t * l_tccp;
7163 assert(p_tcp != 00);
7165 l_mcc_record = p_tcp->m_mcc_records;
7167 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
7168 if (l_mcc_record->m_index == p_index) {
7173 if (i==p_tcp->m_nb_mcc_records) {
7174 /** element discarded **/
7178 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
7179 /** do not support number of comps != image */
7183 l_deco_array = l_mcc_record->m_decorrelation_array;
7186 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
7187 if (l_deco_array->m_data_size != l_data_size) {
7191 l_nb_elem = p_image->numcomps * p_image->numcomps;
7192 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
7193 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7195 if (! p_tcp->m_mct_decoding_matrix ) {
7199 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);
7202 l_offset_array = l_mcc_record->m_offset_array;
7204 if (l_offset_array) {
7205 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
7206 if (l_offset_array->m_data_size != l_data_size) {
7210 l_nb_elem = p_image->numcomps;
7211 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7212 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7214 if (! l_offset_data ) {
7218 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7220 l_tccp = p_tcp->tccps;
7221 l_current_offset_data = l_offset_data;
7223 for (i=0;i<p_image->numcomps;++i) {
7224 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7228 opj_free(l_offset_data);
7235 * Writes the CBD marker (Component bit depth definition)
7237 * @param p_stream the stream to write data to.
7238 * @param p_j2k J2K codec.
7239 * @param p_manager the user event manager.
7241 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7242 struct opj_stream_private *p_stream,
7243 struct opj_event_mgr * p_manager )
7246 OPJ_UINT32 l_cbd_size;
7247 OPJ_BYTE * l_current_data = 00;
7248 opj_image_t *l_image = 00;
7249 opj_image_comp_t * l_comp = 00;
7252 assert(p_j2k != 00);
7253 assert(p_manager != 00);
7254 assert(p_stream != 00);
7256 l_image = p_j2k->m_private_image;
7257 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7259 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7260 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7261 = (OPJ_BYTE*)opj_realloc(
7262 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7265 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7269 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7272 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7274 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7275 l_current_data += 2;
7277 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7278 l_current_data += 2;
7280 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7283 l_comp = l_image->comps;
7285 for (i=0;i<l_image->numcomps;++i) {
7286 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7292 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) {
7300 * Reads a CBD marker (Component bit depth definition)
7301 * @param p_header_data the data contained in the CBD box.
7302 * @param p_j2k the jpeg2000 codec.
7303 * @param p_header_size the size of the data contained in the CBD marker.
7304 * @param p_manager the user event manager.
7306 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7307 OPJ_BYTE * p_header_data,
7308 OPJ_UINT32 p_header_size,
7309 struct opj_event_mgr * p_manager)
7311 OPJ_UINT32 l_nb_comp,l_num_comp;
7312 OPJ_UINT32 l_comp_def;
7314 opj_image_comp_t * l_comp = 00;
7317 assert(p_header_data != 00);
7318 assert(p_j2k != 00);
7319 assert(p_manager != 00);
7321 l_num_comp = p_j2k->m_private_image->numcomps;
7323 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7324 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7328 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7331 if (l_nb_comp != l_num_comp) {
7332 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7336 l_comp = p_j2k->m_private_image->comps;
7337 for (i=0;i<l_num_comp;++i) {
7338 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7340 l_comp->sgnd = (l_comp_def>>7) & 1;
7341 l_comp->prec = (l_comp_def&0x7f) + 1;
7349 /* ----------------------------------------------------------------------- */
7350 /* J2K / JPT decoder interface */
7351 /* ----------------------------------------------------------------------- */
7353 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
7354 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7358 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
7359 if(!j2k->default_tcp) {
7365 j2k->tile_data = NULL;
7370 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7373 if(j2k->tile_len != NULL) {
7374 opj_free(j2k->tile_len);
7376 if(j2k->tile_data != NULL) {
7377 opj_free(j2k->tile_data);
7379 if(j2k->default_tcp != NULL) {
7380 opj_tcp_t *default_tcp = j2k->default_tcp;
7381 if(default_tcp->ppt_data_first != NULL) {
7382 opj_free(default_tcp->ppt_data_first);
7384 if(j2k->default_tcp->tccps != NULL) {
7385 opj_free(j2k->default_tcp->tccps);
7387 opj_free(j2k->default_tcp);
7389 if(j2k->cp != NULL) {
7390 opj_cp_t *cp = j2k->cp;
7391 if(cp->tcps != NULL) {
7392 for(i = 0; i < cp->tw * cp->th; i++) {
7393 if(cp->tcps[i].ppt_data_first != NULL) {
7394 opj_free(cp->tcps[i].ppt_data_first);
7396 if(cp->tcps[i].tccps != NULL) {
7397 opj_free(cp->tcps[i].tccps);
7402 if(cp->ppm_data_first != NULL) {
7403 opj_free(cp->ppm_data_first);
7405 if(cp->tileno != NULL) {
7406 opj_free(cp->tileno);
7408 if(cp->comment != NULL) {
7409 opj_free(cp->comment);
7417 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
7418 if(j2k && parameters) {
7419 /* create and initialize the coding parameters structure */
7420 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7421 cp->reduce = parameters->cp_reduce;
7422 cp->layer = parameters->cp_layer;
7423 cp->limit_decoding = parameters->cp_limit_decoding;
7426 cp->correct = parameters->jpwl_correct;
7427 cp->exp_comps = parameters->jpwl_exp_comps;
7428 cp->max_tiles = parameters->jpwl_max_tiles;
7429 #endif /* USE_JPWL */
7432 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
7437 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7439 if(j2k && parameters) {
7440 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7441 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7444 j2k->m_cp.correct = parameters->jpwl_correct;
7445 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7446 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7447 #endif /* USE_JPWL */
7451 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7452 opj_image_t *image = NULL;
7454 opj_common_ptr cinfo = j2k->cinfo;
7457 j2k->cstr_info = cstr_info;
7459 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
7461 /* create an empty image */
7462 image = opj_image_create0();
7465 j2k->state = J2K_STATE_MHSOC;
7468 opj_dec_mstabent_t *e;
7469 int id = cio_read(cio, 2);
7472 /* we try to honor JPWL correction power */
7473 if (j2k->cp->correct) {
7475 int orig_pos = cio_tell(cio);
7478 /* call the corrector */
7479 status = jpwl_correct(j2k);
7481 /* go back to where you were */
7482 cio_seek(cio, orig_pos - 2);
7484 /* re-read the marker */
7485 id = cio_read(cio, 2);
7487 /* check whether it begins with ff */
7488 if (id >> 8 != 0xff) {
7489 opj_event_msg(cinfo, EVT_ERROR,
7490 "JPWL: possible bad marker %x at %d\n",
7491 id, cio_tell(cio) - 2);
7493 opj_image_destroy(image);
7494 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
7497 /* we try to correct */
7499 cio_seek(cio, cio_tell(cio) - 2);
7500 cio_write(cio, id, 2);
7501 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
7502 "- setting marker to %x\n",
7507 #endif /* USE_JPWL */
7509 if (id >> 8 != 0xff) {
7510 opj_image_destroy(image);
7511 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7514 e = j2k_dec_mstab_lookup(id);
7515 /* Check if the marker is known*/
7516 if (!(j2k->state & e->states)) {
7517 opj_image_destroy(image);
7518 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7521 /* Check if the decoding is limited to the main header*/
7522 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
7523 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
7530 if (j2k->state & J2K_STATE_ERR)
7533 if (j2k->state == J2K_STATE_MT) {
7536 if (j2k->state == J2K_STATE_NEOC) {
7540 if (j2k->state == J2K_STATE_NEOC) {
7544 if (j2k->state != J2K_STATE_MT) {
7545 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7551 * Read a JPT-stream and decode file
7554 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7555 opj_image_t *image = NULL;
7556 opj_jpt_msg_header_t header;
7558 opj_common_ptr cinfo = j2k->cinfo;
7560 OPJ_ARG_NOT_USED(cstr_info);
7564 /* create an empty image */
7565 image = opj_image_create0();
7568 j2k->state = J2K_STATE_MHSOC;
7570 /* Initialize the header */
7571 jpt_init_msg_header(&header);
7572 /* Read the first header of the message */
7573 jpt_read_msg_header(cinfo, cio, &header);
7575 position = cio_tell(cio);
7576 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7577 opj_image_destroy(image);
7578 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7583 opj_dec_mstabent_t *e = NULL;
7586 if (!cio_numbytesleft(cio)) {
7590 /* data-bin read -> need to read a new header */
7591 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7592 jpt_read_msg_header(cinfo, cio, &header);
7593 position = cio_tell(cio);
7594 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7595 opj_image_destroy(image);
7596 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7601 id = cio_read(cio, 2);
7602 if (id >> 8 != 0xff) {
7603 opj_image_destroy(image);
7604 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7607 e = j2k_dec_mstab_lookup(id);
7608 if (!(j2k->state & e->states)) {
7609 opj_image_destroy(image);
7610 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7616 if (j2k->state == J2K_STATE_MT) {
7619 if (j2k->state == J2K_STATE_NEOC) {
7623 if (j2k->state == J2K_STATE_NEOC) {
7627 if (j2k->state != J2K_STATE_MT) {
7628 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7634 /* ----------------------------------------------------------------------- */
7635 /* J2K encoder interface */
7636 /* ----------------------------------------------------------------------- */
7638 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7639 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7646 opj_j2k_v2_t* j2k_create_compress_v2(void)
7648 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7653 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7655 l_j2k->m_is_decoder = 0;
7656 l_j2k->m_cp.m_is_decoder = 0;
7658 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7659 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7664 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7666 /* validation list creation*/
7667 l_j2k->m_validation_list = opj_procedure_list_create();
7668 if (! l_j2k->m_validation_list) {
7673 /* execution list creation*/
7674 l_j2k->m_procedure_list = opj_procedure_list_create();
7675 if (! l_j2k->m_procedure_list) {
7683 void j2k_destroy_compress(opj_j2k_t *j2k) {
7687 if(j2k->cp != NULL) {
7688 opj_cp_t *cp = j2k->cp;
7691 opj_free(cp->comment);
7694 opj_free(cp->matrice);
7696 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7697 opj_free(cp->tcps[tileno].tccps);
7706 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7708 int tileno, numpocs_tile;
7709 opj_cp_t *cp = NULL;
7711 if(!j2k || !parameters || ! image) {
7715 /* create and initialize the coding parameters structure */
7716 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7718 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7721 /* set default values for cp */
7726 copy user encoding parameters
7728 cp->cinema = parameters->cp_cinema;
7729 cp->max_comp_size = parameters->max_comp_size;
7730 cp->rsiz = parameters->cp_rsiz;
7731 cp->disto_alloc = parameters->cp_disto_alloc;
7732 cp->fixed_alloc = parameters->cp_fixed_alloc;
7733 cp->fixed_quality = parameters->cp_fixed_quality;
7735 /* mod fixed_quality */
7736 if(parameters->cp_matrice) {
7737 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7738 cp->matrice = (int *) opj_malloc(array_size);
7739 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7743 cp->tdx = parameters->cp_tdx;
7744 cp->tdy = parameters->cp_tdy;
7747 cp->tx0 = parameters->cp_tx0;
7748 cp->ty0 = parameters->cp_ty0;
7750 /* comment string */
7751 if(parameters->cp_comment) {
7752 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7754 strcpy(cp->comment, parameters->cp_comment);
7759 calculate other encoding parameters
7762 if (parameters->tile_size_on) {
7763 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7764 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7766 cp->tdx = image->x1 - cp->tx0;
7767 cp->tdy = image->y1 - cp->ty0;
7770 if(parameters->tp_on){
7771 cp->tp_flag = parameters->tp_flag;
7776 for(i=0;i<image->numcomps ;i++){
7777 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7783 calculate JPWL encoding parameters
7786 if (parameters->jpwl_epc_on) {
7790 cp->epc_on = OPJ_TRUE;
7791 cp->info_on = OPJ_FALSE; /* no informative technique */
7794 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7795 cp->epb_on = OPJ_TRUE;
7797 cp->hprot_MH = parameters->jpwl_hprot_MH;
7798 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7799 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7800 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7802 /* if tile specs are not specified, copy MH specs */
7803 if (cp->hprot_TPH[0] == -1) {
7804 cp->hprot_TPH_tileno[0] = 0;
7805 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7807 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7808 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7809 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7810 cp->pprot[i] = parameters->jpwl_pprot[i];
7814 /* set ESD writing */
7815 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7816 cp->esd_on = OPJ_TRUE;
7818 cp->sens_size = parameters->jpwl_sens_size;
7819 cp->sens_addr = parameters->jpwl_sens_addr;
7820 cp->sens_range = parameters->jpwl_sens_range;
7822 cp->sens_MH = parameters->jpwl_sens_MH;
7823 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7824 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7825 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7829 /* always set RED writing to false: we are at the encoder */
7830 cp->red_on = OPJ_FALSE;
7833 cp->epc_on = OPJ_FALSE;
7835 #endif /* USE_JPWL */
7838 /* initialize the mutiple tiles */
7839 /* ---------------------------- */
7840 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7842 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7843 opj_tcp_t *tcp = &cp->tcps[tileno];
7844 tcp->numlayers = parameters->tcp_numlayers;
7845 assert ( tcp->numlayers >= 0 );
7846 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7848 if (cp->fixed_quality) {
7849 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7851 tcp->rates[j] = parameters->tcp_rates[j];
7853 if (cp->fixed_quality) { /* add fixed_quality */
7854 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7856 tcp->rates[j] = parameters->tcp_rates[j];
7860 tcp->csty = parameters->csty;
7861 tcp->prg = parameters->prog_order;
7862 tcp->mct = parameters->tcp_mct;
7866 if (parameters->numpocs) {
7867 /* initialisation of POC */
7869 assert( parameters->numpocs >= 0 );
7870 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7871 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7872 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7873 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7874 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7875 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7876 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7877 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7878 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7879 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7883 tcp->numpocs = numpocs_tile -1 ;
7888 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7890 for (i = 0; i < image->numcomps; i++) {
7891 opj_tccp_t *tccp = &tcp->tccps[i];
7892 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7893 tccp->numresolutions = parameters->numresolution;
7894 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7895 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7896 tccp->cblksty = parameters->mode;
7897 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7898 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7900 assert(parameters->roi_compno >= 0);
7901 if (i == (OPJ_UINT32)parameters->roi_compno) {
7902 tccp->roishift = parameters->roi_shift;
7907 if(parameters->cp_cinema)
7909 /*Precinct size for lowest frequency subband=128*/
7912 /*Precinct size at all other resolutions = 256*/
7913 for (j = 1; j < tccp->numresolutions; j++) {
7918 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7920 assert(tccp->numresolutions > 0);
7921 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7922 if (p < parameters->res_spec) {
7924 if (parameters->prcw_init[p] < 1) {
7927 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7930 if (parameters->prch_init[p] < 1) {
7933 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7937 int res_spec = parameters->res_spec;
7938 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7939 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7941 if (size_prcw < 1) {
7944 tccp->prcw[j] = int_floorlog2(size_prcw);
7947 if (size_prch < 1) {
7950 tccp->prch[j] = int_floorlog2(size_prch);
7954 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7957 for (j = 0; j < tccp->numresolutions; j++) {
7964 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7969 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7970 opj_cparameters_t *parameters,
7972 struct opj_event_mgr * p_manager)
7974 OPJ_UINT32 i, j, tileno, numpocs_tile;
7975 opj_cp_v2_t *cp = 00;
7978 if(!p_j2k || !parameters || ! image) {
7982 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7983 cp = &(p_j2k->m_cp);
7985 /* set default values for cp */
7990 copy user encoding parameters
7992 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7993 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7994 cp->rsiz = parameters->cp_rsiz;
7995 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7996 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7997 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7999 /* mod fixed_quality */
8000 if (parameters->cp_matrice) {
8001 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
8002 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
8003 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
8007 cp->tdx = parameters->cp_tdx;
8008 cp->tdy = parameters->cp_tdy;
8011 cp->tx0 = parameters->cp_tx0;
8012 cp->ty0 = parameters->cp_ty0;
8014 /* comment string */
8015 if(parameters->cp_comment) {
8016 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
8018 strcpy(cp->comment, parameters->cp_comment);
8023 calculate other encoding parameters
8026 if (parameters->tile_size_on) {
8027 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
8028 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8030 cp->tdx = image->x1 - cp->tx0;
8031 cp->tdy = image->y1 - cp->ty0;
8034 if (parameters->tp_on) {
8035 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
8036 cp->m_specific_param.m_enc.m_tp_on = 1;
8041 calculate JPWL encoding parameters
8044 if (parameters->jpwl_epc_on) {
8048 cp->epc_on = OPJ_TRUE;
8049 cp->info_on = OPJ_FALSE; /* no informative technique */
8052 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8053 cp->epb_on = OPJ_TRUE;
8055 cp->hprot_MH = parameters->jpwl_hprot_MH;
8056 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8057 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8058 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8060 /* if tile specs are not specified, copy MH specs */
8061 if (cp->hprot_TPH[0] == -1) {
8062 cp->hprot_TPH_tileno[0] = 0;
8063 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8065 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8066 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8067 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8068 cp->pprot[i] = parameters->jpwl_pprot[i];
8072 /* set ESD writing */
8073 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8074 cp->esd_on = OPJ_TRUE;
8076 cp->sens_size = parameters->jpwl_sens_size;
8077 cp->sens_addr = parameters->jpwl_sens_addr;
8078 cp->sens_range = parameters->jpwl_sens_range;
8080 cp->sens_MH = parameters->jpwl_sens_MH;
8081 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8082 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8083 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8087 /* always set RED writing to false: we are at the encoder */
8088 cp->red_on = OPJ_FALSE;
8091 cp->epc_on = OPJ_FALSE;
8093 #endif /* USE_JPWL */
8096 /* initialize the mutiple tiles */
8097 /* ---------------------------- */
8098 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
8099 if (parameters->numpocs) {
8100 /* initialisation of POC */
8101 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
8105 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8106 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
8107 tcp->numlayers = parameters->tcp_numlayers;
8109 for (j = 0; j < tcp->numlayers; j++) {
8110 if(cp->m_specific_param.m_enc.m_cinema){
8111 if (cp->m_specific_param.m_enc.m_fixed_quality) {
8112 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8114 tcp->rates[j] = parameters->tcp_rates[j];
8116 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
8117 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8119 tcp->rates[j] = parameters->tcp_rates[j];
8124 tcp->csty = parameters->csty;
8125 tcp->prg = parameters->prog_order;
8126 tcp->mct = parameters->tcp_mct;
8131 if (parameters->numpocs) {
8132 /* initialisation of POC */
8135 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
8136 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8137 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8139 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
8140 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
8141 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
8142 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
8143 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
8144 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
8145 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
8151 tcp->numpocs = numpocs_tile -1 ;
8156 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8158 if (parameters->mct_data) {
8160 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
8163 /* TODO MSD : merge v2 add invert.c or used a external lib ?
8164 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
8165 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8166 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
8169 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8170 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
8171 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
8173 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8174 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
8176 tcp->mct_norms = (OPJ_FLOAT64*)
8177 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
8179 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
8182 for (i = 0; i < image->numcomps; i++) {
8183 opj_tccp_t *tccp = &tcp->tccps[i];
8184 tccp->m_dc_level_shift = l_dc_shift[i];
8187 j2k_setup_mct_encoding(tcp,image);
8191 for (i = 0; i < image->numcomps; i++) {
8192 opj_tccp_t *tccp = &tcp->tccps[i];
8193 opj_image_comp_t * l_comp = &(image->comps[i]);
8195 if (! l_comp->sgnd) {
8196 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
8201 for (i = 0; i < image->numcomps; i++) {
8202 opj_tccp_t *tccp = &tcp->tccps[i];
8204 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
8205 tccp->numresolutions = parameters->numresolution;
8206 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8207 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8208 tccp->cblksty = parameters->mode;
8209 tccp->qmfbid = parameters->irreversible ? 0 : 1;
8210 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8213 if (i == parameters->roi_compno) {
8214 tccp->roishift = parameters->roi_shift;
8219 if(parameters->cp_cinema) {
8220 //Precinct size for lowest frequency subband=128
8223 //Precinct size at all other resolutions = 256
8224 for (j = 1; j < tccp->numresolutions; j++) {
8229 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8231 for (j = tccp->numresolutions - 1; j >= 0; j--) {
8232 if (p < parameters->res_spec) {
8234 if (parameters->prcw_init[p] < 1) {
8237 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
8240 if (parameters->prch_init[p] < 1) {
8243 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
8247 int res_spec = parameters->res_spec;
8248 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8249 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8251 if (size_prcw < 1) {
8254 tccp->prcw[j] = int_floorlog2(size_prcw);
8257 if (size_prch < 1) {
8260 tccp->prch[j] = int_floorlog2(size_prch);
8264 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
8267 for (j = 0; j < tccp->numresolutions; j++) {
8274 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8278 if (parameters->mct_data) {
8279 opj_free(parameters->mct_data);
8280 parameters->mct_data = 00;
8285 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
8288 opj_cp_t *cp = NULL;
8290 opj_tcd_t *tcd = NULL; /* TCD component */
8298 j2k->cstr_info = cstr_info;
8301 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
8302 cstr_info->image_w = image->x1 - image->x0;
8303 cstr_info->image_h = image->y1 - image->y0;
8304 cstr_info->prog = (&cp->tcps[0])->prg;
8305 cstr_info->tw = cp->tw;
8306 cstr_info->th = cp->th;
8307 cstr_info->tile_x = cp->tdx; /* new version parser */
8308 cstr_info->tile_y = cp->tdy; /* new version parser */
8309 cstr_info->tile_Ox = cp->tx0; /* new version parser */
8310 cstr_info->tile_Oy = cp->ty0; /* new version parser */
8311 cstr_info->numcomps = image->numcomps;
8312 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
8313 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
8314 for (compno=0; compno < image->numcomps; compno++) {
8315 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
8317 cstr_info->D_max = 0.0; /* ADD Marcela */
8318 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
8319 cstr_info->maxmarknum = 100;
8320 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
8321 cstr_info->marknum = 0;
8331 for (compno = 1; compno < image->numcomps; compno++) {
8332 j2k_write_coc(j2k, compno);
8333 j2k_write_qcc(j2k, compno);
8337 for (compno = 0; compno < image->numcomps; compno++) {
8338 opj_tcp_t *tcp = &cp->tcps[0];
8339 if (tcp->tccps[compno].roishift)
8340 j2k_write_rgn(j2k, compno, 0);
8342 if (cp->comment != NULL) {
8346 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
8350 if (cp->cinema == CINEMA4K_24) {
8355 /* uncomment only for testing JPSEC marker writing */
8356 /* j2k_write_sec(j2k); */
8360 cstr_info->main_head_end = cio_tell(cio) - 1;
8363 /**** Main Header ENDS here ***/
8365 /* create the tile encoder */
8366 tcd = tcd_create(j2k->cinfo);
8368 /* encode each tile */
8369 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8373 int acc_pack_num = 0;
8377 opj_tcp_t *tcp = &cp->tcps[tileno];
8378 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8380 j2k->curtileno = tileno;
8381 j2k->cur_tp_num = 0;
8382 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8383 /* initialisation before tile encoding */
8385 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8387 tcd_init_encode(tcd, image, cp, j2k->curtileno);
8392 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8393 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8394 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8395 cstr_info->tile[j2k->curtileno].marknum = 0;
8399 for(pino = 0; pino <= tcp->numpocs; pino++) {
8403 /*Get number of tile parts*/
8404 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8405 tcd->tp_pos = cp->tp_pos;
8407 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8408 j2k->tp_num = tilepartno;
8411 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8412 cio_tell(cio) + j2k->pos_correction;
8416 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8417 for (compno = 1; compno < image->numcomps; compno++) {
8418 j2k_write_coc(j2k, compno);
8419 j2k_write_qcc(j2k, compno);
8421 if (cp->tcps[tileno].numpocs) {
8428 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8429 cio_tell(cio) + j2k->pos_correction + 1;
8432 j2k_write_sod(j2k, tcd);
8436 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8437 cio_tell(cio) + j2k->pos_correction - 1;
8438 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8440 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8441 cstr_info->packno - acc_pack_num;
8442 acc_pack_num = cstr_info->packno;
8450 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8455 if (tile->PPT) { // BAD PPT !!!
8458 PPT_file=fopen("PPT","rb");
8459 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8460 for (i=0;i<tile->len_ppt;i++) {
8462 fread(&elmt, 1, 1, PPT_file);
8463 fwrite(&elmt,1,1,f);
8472 /* destroy the tile encoder */
8473 tcd_free_encode(tcd);
8476 opj_free(j2k->cur_totnum_tp);
8481 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8483 /* The following adjustment is done to adjust the codestream size */
8484 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8485 /* the first bunch of bytes is not in the codestream */
8486 cstr_info->codestream_size -= cstr_info->main_head_start;
8492 preparation of JPWL marker segments
8496 /* encode according to JPWL */
8497 jpwl_encode(j2k, cio, image);
8500 #endif /* USE_JPWL */
8505 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8507 assert(cstr_info != 00);
8509 /* expand the list? */
8510 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8511 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8512 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8515 /* add the marker */
8516 cstr_info->marker[cstr_info->marknum].type = type;
8517 cstr_info->marker[cstr_info->marknum].pos = pos;
8518 cstr_info->marker[cstr_info->marknum].len = len;
8519 cstr_info->marknum++;
8523 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8525 assert(cstr_index != 00);
8527 /* expand the list? */
8528 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8529 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8530 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8533 /* add the marker */
8534 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8535 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8536 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8537 cstr_index->marknum++;
8541 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8543 opj_marker_info_t *marker;
8545 assert(cstr_info != 00);
8547 /* expand the list? */
8548 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8549 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8550 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8553 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8555 /* add the marker */
8556 marker->type = type;
8559 cstr_info->tile[tileno].marknum++;
8562 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)
8564 assert(cstr_index != 00);
8565 assert(cstr_index->tile_index != 00);
8567 /* expand the list? */
8568 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8569 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8570 cstr_index->tile_index[tileno].marker =
8571 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8572 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8575 /* add the marker */
8576 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8577 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8578 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8579 cstr_index->tile_index[tileno].marknum++;
8581 if (type == J2K_MS_SOT) {
8582 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8584 if (cstr_index->tile_index[tileno].tp_index)
8585 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8592 * -----------------------------------------------------------------------
8593 * -----------------------------------------------------------------------
8594 * -----------------------------------------------------------------------
8598 * Ends the decompression procedures and possibiliy add data to be read after the
8601 opj_bool j2k_end_decompress(
8602 opj_j2k_v2_t *p_j2k,
8603 opj_stream_private_t *p_stream,
8604 opj_event_mgr_t * p_manager)
8613 * Reads a jpeg2000 codestream header structure.
8616 * @param p_stream the stream to read data from.
8617 * @param p_j2k the jpeg2000 codec.
8618 * @param p_manager the user event manager.
8620 * @return true if the box is valid.
8622 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8623 opj_j2k_v2_t* p_j2k,
8624 opj_image_t** p_image,
8625 struct opj_event_mgr* p_manager )
8628 assert(p_j2k != 00);
8629 assert(p_stream != 00);
8630 assert(p_manager != 00);
8632 /* create an empty image header */
8633 p_j2k->m_private_image = opj_image_create0();
8634 if (! p_j2k->m_private_image) {
8638 /* customization of the validation */
8639 j2k_setup_decoding_validation(p_j2k);
8641 /* validation of the parameters codec */
8642 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8643 opj_image_destroy(p_j2k->m_private_image);
8644 p_j2k->m_private_image = NULL;
8648 /* customization of the encoding */
8649 j2k_setup_header_reading(p_j2k);
8652 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8653 opj_image_destroy(p_j2k->m_private_image);
8654 p_j2k->m_private_image = NULL;
8658 *p_image = opj_image_create0();
8663 /* Copy codestream image information to the output image */
8664 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8671 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8673 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8676 assert(p_j2k != 00);
8678 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8680 /* DEVELOPER CORNER, add your custom procedures */
8681 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8686 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8687 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8689 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8692 assert(p_j2k != 00);
8694 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8695 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8696 /* DEVELOPER CORNER, add your custom validation procedure */
8702 * The mct encoding validation procedure.
8704 * @param p_j2k the jpeg2000 codec to validate.
8705 * @param p_stream the input stream to validate.
8706 * @param p_manager the user event manager.
8708 * @return true if the parameters are correct.
8710 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8711 opj_stream_private_t *p_stream,
8712 opj_event_mgr_t * p_manager )
8714 opj_bool l_is_valid = OPJ_TRUE;
8718 assert(p_j2k != 00);
8719 assert(p_stream != 00);
8720 assert(p_manager != 00);
8722 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8723 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8724 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8726 for (i=0;i<l_nb_tiles;++i) {
8727 if (l_tcp->mct == 2) {
8728 opj_tccp_t * l_tccp = l_tcp->tccps;
8729 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8731 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8732 l_is_valid &= ! (l_tccp->qmfbid & 1);
8743 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8746 OPJ_UINT32 l_indix = 1;
8747 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8748 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8749 OPJ_UINT32 l_mct_size,l_nb_elem;
8750 OPJ_FLOAT32 * l_data, * l_current_data;
8751 opj_tccp_t * l_tccp;
8754 assert(p_tcp != 00);
8756 if (p_tcp->mct != 2) {
8760 if (p_tcp->m_mct_decoding_matrix) {
8761 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8762 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8764 p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
8765 if (! p_tcp->m_mct_records) {
8768 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8770 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8772 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8774 if (l_mct_deco_data->m_data) {
8775 opj_free(l_mct_deco_data->m_data);
8776 l_mct_deco_data->m_data = 00;
8779 l_mct_deco_data->m_index = l_indix++;
8780 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8781 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8782 l_nb_elem = p_image->numcomps * p_image->numcomps;
8783 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8784 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8786 if (! l_mct_deco_data->m_data) {
8790 j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem);
8792 l_mct_deco_data->m_data_size = l_mct_size;
8793 ++p_tcp->m_nb_mct_records;
8796 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8797 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8798 p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
8800 if (! p_tcp->m_mct_records) {
8804 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8805 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8807 if (l_mct_deco_data) {
8808 l_mct_deco_data = l_mct_offset_data - 1;
8812 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8814 if (l_mct_offset_data->m_data) {
8815 opj_free(l_mct_offset_data->m_data);
8816 l_mct_offset_data->m_data = 00;
8819 l_mct_offset_data->m_index = l_indix++;
8820 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8821 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8822 l_nb_elem = p_image->numcomps;
8823 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8824 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8826 if (! l_mct_offset_data->m_data) {
8830 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8832 opj_free(l_mct_offset_data->m_data);
8833 l_mct_offset_data->m_data = 00;
8837 l_tccp = p_tcp->tccps;
8838 l_current_data = l_data;
8840 for (i=0;i<l_nb_elem;++i) {
8841 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8845 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8849 l_mct_offset_data->m_data_size = l_mct_size;
8851 ++p_tcp->m_nb_mct_records;
8853 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8854 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8855 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8856 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8858 if (! p_tcp->m_mcc_records) {
8861 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8862 memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
8866 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8867 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8868 l_mcc_data->m_is_irreversible = 1;
8869 l_mcc_data->m_nb_comps = p_image->numcomps;
8870 l_mcc_data->m_index = l_indix++;
8871 l_mcc_data->m_offset_array = l_mct_offset_data;
8872 ++p_tcp->m_nb_mcc_records;
8878 * Builds the cp decoder parameters to use to decode tile.
8880 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8881 opj_stream_private_t *p_stream,
8882 opj_event_mgr_t * p_manager )
8884 /* add here initialization of cp
8885 copy paste of setup_decoder */
8893 * Builds the cp encoder parameters to use to encode tile.
8895 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8896 opj_stream_private_t *p_stream,
8897 opj_event_mgr_t * p_manager )
8899 /* add here initialization of cp
8900 copy paste of setup_encoder */
8908 * The default encoding validation procedure without any extension.
8910 * @param p_j2k the jpeg2000 codec to validate.
8911 * @param p_stream the input stream to validate.
8912 * @param p_manager the user event manager.
8914 * @return true if the parameters are correct.
8916 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8917 opj_stream_private_t *p_stream,
8918 opj_event_mgr_t * p_manager )
8920 opj_bool l_is_valid = OPJ_TRUE;
8923 assert(p_j2k != 00);
8924 assert(p_stream != 00);
8925 assert(p_manager != 00);
8927 /* STATE checking */
8928 /* make sure the state is at 0 */
8929 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8931 /* POINTER validation */
8932 /* make sure a p_j2k codec is present */
8933 l_is_valid &= (p_j2k->m_procedure_list != 00);
8934 /* make sure a validation list is present */
8935 l_is_valid &= (p_j2k->m_validation_list != 00);
8937 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8938 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8942 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8943 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8947 /* PARAMETER VALIDATION */
8952 * The default decoding validation procedure without any extension.
8954 * @param p_j2k the jpeg2000 codec to validate.
8955 * @param p_stream the input stream to validate.
8956 * @param p_manager the user event manager.
8958 * @return true if the parameters are correct.
8960 opj_bool j2k_decoding_validation (
8961 opj_j2k_v2_t *p_j2k,
8962 opj_stream_private_t *p_stream,
8963 opj_event_mgr_t * p_manager
8966 opj_bool l_is_valid = OPJ_TRUE;
8969 assert(p_j2k != 00);
8970 assert(p_stream != 00);
8971 assert(p_manager != 00);
8974 /* STATE checking */
8975 /* make sure the state is at 0 */
8977 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8979 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8981 /* POINTER validation */
8982 /* make sure a p_j2k codec is present */
8983 /* make sure a procedure list is present */
8984 l_is_valid &= (p_j2k->m_procedure_list != 00);
8985 /* make sure a validation list is present */
8986 l_is_valid &= (p_j2k->m_validation_list != 00);
8988 /* PARAMETER VALIDATION */
8992 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8993 struct opj_stream_private *p_stream,
8994 struct opj_event_mgr * p_manager)
8996 OPJ_UINT32 l_current_marker;
8997 OPJ_UINT32 l_marker_size;
8998 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9001 assert(p_stream != 00);
9002 assert(p_j2k != 00);
9003 assert(p_manager != 00);
9005 /* We enter in the main header */
9006 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
9008 /* Try to read the SOC marker, the codestream must begin with SOC marker */
9009 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
9010 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
9014 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9015 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9016 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9020 /* Read 2 bytes as the new marker ID */
9021 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9023 /* Try to read until the SOT is detected */
9024 while (l_current_marker != J2K_MS_SOT) {
9026 /* Check if the current marker ID is valid */
9027 if (l_current_marker < 0xff00) {
9028 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
9032 /* Get the marker handler from the marker ID */
9033 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9035 /* Manage case where marker is unknown */
9036 if (l_marker_handler->id == J2K_MS_UNK) {
9037 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
9038 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
9042 if (l_current_marker == J2K_MS_SOT)
9043 break; /* SOT marker is detected main header is completely read */
9044 else /* Get the marker handler from the marker ID */
9045 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9048 /* Check if the marker is known and if it is the right place to find it */
9049 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9050 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9054 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9055 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9056 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9060 /* read 2 bytes as the marker size */
9061 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9062 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9064 /* Check if the marker size is compatible with the header data size */
9065 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9066 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9067 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9068 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9071 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9074 /* Try to read the rest of the marker segment 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,l_marker_size,p_manager) != l_marker_size) {
9076 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9080 /* Read the marker segment with the correct marker handler */
9081 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9082 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
9086 /* Add the marker to the codestream index*/
9087 j2k_add_mhmarker_v2(p_j2k->cstr_index,
9088 l_marker_handler->id,
9089 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9090 l_marker_size + 4 );
9092 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9093 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9094 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9098 /* read 2 bytes as the new marker ID */
9099 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9102 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
9104 /* Position of the last element if the main header */
9105 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
9107 /* Next step: read a tile-part header */
9108 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9114 * Excutes the given procedures on the given codec.
9116 * @param p_procedure_list the list of procedures to execute
9117 * @param p_j2k the jpeg2000 codec to execute the procedures on.
9118 * @param p_stream the stream to execute the procedures on.
9119 * @param p_manager the user manager.
9121 * @return true if all the procedures were successfully executed.
9123 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
9124 opj_procedure_list_t * p_procedure_list,
9125 opj_stream_private_t *p_stream,
9126 opj_event_mgr_t * p_manager )
9128 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
9129 opj_bool l_result = OPJ_TRUE;
9130 OPJ_UINT32 l_nb_proc, i;
9133 assert(p_procedure_list != 00);
9134 assert(p_j2k != 00);
9135 assert(p_stream != 00);
9136 assert(p_manager != 00);
9139 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
9140 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);
9142 for (i=0;i<l_nb_proc;++i) {
9143 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
9147 /* and clear the procedure list at the end.*/
9148 opj_procedure_list_clear(p_procedure_list);
9153 opj_bool j2k_copy_default_tcp_and_create_tcd
9155 opj_j2k_v2_t * p_j2k,
9156 opj_stream_private_t *p_stream,
9157 opj_event_mgr_t * p_manager
9160 opj_tcp_v2_t * l_tcp = 00;
9161 opj_tcp_v2_t * l_default_tcp = 00;
9162 OPJ_UINT32 l_nb_tiles;
9164 opj_tccp_t *l_current_tccp = 00;
9165 OPJ_UINT32 l_tccp_size;
9166 OPJ_UINT32 l_mct_size;
9167 opj_image_t * l_image;
9168 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
9169 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
9170 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
9171 OPJ_UINT32 l_offset;
9174 assert(p_j2k != 00);
9175 assert(p_stream != 00);
9176 assert(p_manager != 00);
9178 l_image = p_j2k->m_private_image;
9179 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9180 l_tcp = p_j2k->m_cp.tcps;
9181 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
9182 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9183 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
9186 for (i=0; i<l_nb_tiles; ++i) {
9187 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
9188 l_current_tccp = l_tcp->tccps;
9189 /*Copy default coding parameters into the current tile coding parameters*/
9190 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
9191 /* Initialize some values of the current tile coding parameters*/
9193 l_tcp->ppt_data = 00;
9194 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
9195 l_tcp->tccps = l_current_tccp;
9197 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
9198 if (l_default_tcp->m_mct_decoding_matrix) {
9199 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
9200 if (! l_tcp->m_mct_decoding_matrix ) {
9203 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
9206 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
9207 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
9208 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
9209 if (! l_tcp->m_mct_records) {
9212 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
9214 /* Copy the mct record data from dflt_tile_cp to the current tile*/
9215 l_src_mct_rec = l_default_tcp->m_mct_records;
9216 l_dest_mct_rec = l_tcp->m_mct_records;
9218 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
9220 if (l_src_mct_rec->m_data) {
9222 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
9223 if(! l_dest_mct_rec->m_data) {
9226 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
9233 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
9234 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
9235 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
9236 if (! l_tcp->m_mcc_records) {
9239 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
9241 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
9242 l_src_mcc_rec = l_default_tcp->m_mcc_records;
9243 l_dest_mcc_rec = l_tcp->m_mcc_records;
9245 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
9247 if (l_src_mcc_rec->m_decorrelation_array) {
9248 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
9249 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
9252 if (l_src_mcc_rec->m_offset_array) {
9253 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
9254 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
9261 /* Copy all the dflt_tile_compo_cp to the current tile cp */
9262 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
9264 /* Move to next tile cp*/
9268 /* Create the current tile decoder*/
9269 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
9270 if (! p_j2k->m_tcd ) {
9274 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
9275 tcd_destroy_v2(p_j2k->m_tcd);
9277 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9285 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
9286 * with the marker value.
9287 * @param p_id Marker value to look up
9289 * @return the handler associated with the id.
9291 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
9293 const opj_dec_memory_marker_handler_t *e;
9294 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
9295 if (e->id == p_id) {
9296 break; /* we find a handler corresponding to the marker ID*/
9304 * Destroys a jpeg2000 codec.
9306 * @param p_j2k the jpeg20000 structure to destroy.
9308 void j2k_destroy (opj_j2k_v2_t *p_j2k)
9314 if (p_j2k->m_is_decoder) {
9316 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
9317 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9318 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9319 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
9322 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
9323 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
9324 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
9325 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
9330 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
9331 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9332 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
9335 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
9336 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
9337 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
9338 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
9341 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
9342 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
9343 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
9344 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
9348 tcd_destroy_v2(p_j2k->m_tcd);
9350 j2k_cp_destroy(&(p_j2k->m_cp));
9351 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
9353 opj_procedure_list_destroy(p_j2k->m_procedure_list);
9354 p_j2k->m_procedure_list = 00;
9356 opj_procedure_list_destroy(p_j2k->m_validation_list);
9357 p_j2k->m_procedure_list = 00;
9359 j2k_destroy_cstr_index(p_j2k->cstr_index);
9360 p_j2k->cstr_index = NULL;
9362 opj_image_destroy(p_j2k->m_private_image);
9363 p_j2k->m_private_image = NULL;
9365 opj_image_destroy(p_j2k->m_output_image);
9366 p_j2k->m_output_image = NULL;
9371 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9375 if (p_cstr_ind->marker) {
9376 opj_free(p_cstr_ind->marker);
9377 p_cstr_ind->marker = NULL;
9380 if (p_cstr_ind->tile_index) {
9381 OPJ_UINT32 it_tile = 0;
9383 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9385 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9386 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9387 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9390 if(p_cstr_ind->tile_index[it_tile].tp_index){
9391 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9392 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9395 if(p_cstr_ind->tile_index[it_tile].marker){
9396 opj_free(p_cstr_ind->tile_index[it_tile].marker);
9397 p_cstr_ind->tile_index[it_tile].marker = NULL;
9402 opj_free( p_cstr_ind->tile_index);
9403 p_cstr_ind->tile_index = NULL;
9406 opj_free(p_cstr_ind);
9413 * Destroys a tile coding parameter structure.
9415 * @param p_tcp the tile coding parameter to destroy.
9417 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9423 if (p_tcp->ppt_buffer != 00) {
9424 opj_free(p_tcp->ppt_buffer);
9425 p_tcp->ppt_buffer = 00;
9428 if (p_tcp->tccps != 00) {
9429 opj_free(p_tcp->tccps);
9433 if (p_tcp->m_mct_coding_matrix != 00) {
9434 opj_free(p_tcp->m_mct_coding_matrix);
9435 p_tcp->m_mct_coding_matrix = 00;
9438 if (p_tcp->m_mct_decoding_matrix != 00) {
9439 opj_free(p_tcp->m_mct_decoding_matrix);
9440 p_tcp->m_mct_decoding_matrix = 00;
9443 if (p_tcp->m_mcc_records) {
9444 opj_free(p_tcp->m_mcc_records);
9445 p_tcp->m_mcc_records = 00;
9446 p_tcp->m_nb_max_mcc_records = 0;
9447 p_tcp->m_nb_mcc_records = 0;
9450 if (p_tcp->m_mct_records) {
9451 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9454 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9455 if (l_mct_data->m_data) {
9456 opj_free(l_mct_data->m_data);
9457 l_mct_data->m_data = 00;
9463 opj_free(p_tcp->m_mct_records);
9464 p_tcp->m_mct_records = 00;
9467 if (p_tcp->mct_norms != 00) {
9468 opj_free(p_tcp->mct_norms);
9469 p_tcp->mct_norms = 00;
9472 j2k_tcp_data_destroy(p_tcp);
9477 * Destroys the data inside a tile coding parameter structure.
9479 * @param p_tcp the tile coding parameter which contain data to destroy.
9481 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9483 if (p_tcp->m_data) {
9484 opj_free(p_tcp->m_data);
9485 p_tcp->m_data = NULL;
9486 p_tcp->m_data_size = 0;
9491 * Destroys a coding parameter structure.
9493 * @param p_cp the coding parameter to destroy.
9495 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9497 OPJ_UINT32 l_nb_tiles;
9498 opj_tcp_v2_t * l_current_tile = 00;
9509 l_current_tile = p_cp->tcps;
9510 l_nb_tiles = p_cp->th * p_cp->tw;
9513 (i = 0; i < l_nb_tiles; ++i)
9515 j2k_tcp_destroy(l_current_tile);
9518 opj_free(p_cp->tcps);
9522 (p_cp->ppm_buffer != 00)
9524 opj_free(p_cp->ppm_buffer);
9525 p_cp->ppm_buffer = 00;
9528 (p_cp->comment != 00)
9530 opj_free(p_cp->comment);
9534 (! p_cp->m_is_decoder)
9537 (p_cp->m_specific_param.m_enc.m_matrice)
9539 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9540 p_cp->m_specific_param.m_enc.m_matrice = 00;
9548 * Reads a tile header.
9549 * @param p_j2k the jpeg2000 codec.
9550 * @param p_stream the stream to write data to.
9551 * @param p_manager the user event manager.
9553 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9554 OPJ_UINT32 * p_tile_index,
9555 OPJ_UINT32 * p_data_size,
9556 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9557 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9558 OPJ_UINT32 * p_nb_comps,
9560 opj_stream_private_t *p_stream,
9561 opj_event_mgr_t * p_manager )
9563 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9564 OPJ_UINT32 l_marker_size;
9565 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9566 opj_tcp_v2_t * l_tcp = NULL;
9567 OPJ_UINT32 l_nb_tiles;
9570 assert(p_stream != 00);
9571 assert(p_j2k != 00);
9572 assert(p_manager != 00);
9574 /* Reach the End Of Codestream ?*/
9575 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9576 l_current_marker = J2K_MS_EOC;
9578 /* We need to encounter a SOT marker (a new tile-part header) */
9579 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9583 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9584 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9586 /* Try to read until the Start Of Data is detected */
9587 while (l_current_marker != J2K_MS_SOD) {
9589 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9590 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9591 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9595 /* Read 2 bytes from the buffer as the marker size */
9596 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9598 /* Why this condition? FIXME */
9599 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9600 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9602 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9604 /* Get the marker handler from the marker ID */
9605 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9607 /* Check if the marker is known and if it is the right place to find it */
9608 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9609 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9612 /* FIXME manage case of unknown marker as in the main header ? */
9614 /* Check if the marker size is compatible with the header data size */
9615 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9616 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9617 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9618 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9621 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9624 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9625 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) {
9626 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9630 /* Read the marker segment with the correct marker handler */
9631 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9632 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9636 /* Add the marker to the codestream index*/
9637 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9639 l_marker_handler->id,
9640 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9641 l_marker_size + 4 );
9643 /* Keep the position of the last SOT marker read */
9644 if ( l_marker_handler->id == J2K_MS_SOT ) {
9645 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9646 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9648 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9653 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9654 /* Skip the rest of the tile part header*/
9655 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) {
9656 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9659 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9662 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9663 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9664 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9667 /* Read 2 bytes from the buffer as the new marker ID */
9668 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9672 /* If we didn't skip data before, we need to read the SOD marker*/
9673 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9674 /* Try to read the SOD marker and skip data ? FIXME */
9675 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
9681 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9682 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9683 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9684 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9688 /* Read 2 bytes from buffer as the new marker ID */
9689 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9693 /* Indicate we will try to read a new tile-part header*/
9694 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9695 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9696 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9698 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9699 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9700 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9704 /* Read 2 bytes from buffer as the new marker ID */
9705 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9709 /* Current marker is the EOC marker ?*/
9710 if (l_current_marker == J2K_MS_EOC) {
9711 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9712 p_j2k->m_current_tile_number = 0;
9713 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9718 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9719 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9720 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9722 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9723 ++p_j2k->m_current_tile_number;
9727 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9728 *p_go_on = OPJ_FALSE;
9734 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9735 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9739 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9740 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9742 *p_tile_index = p_j2k->m_current_tile_number;
9743 *p_go_on = OPJ_TRUE;
9744 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9745 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9746 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9747 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9748 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9749 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9751 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9757 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9758 OPJ_UINT32 p_tile_index,
9760 OPJ_UINT32 p_data_size,
9761 opj_stream_private_t *p_stream,
9762 opj_event_mgr_t * p_manager )
9764 OPJ_UINT32 l_current_marker;
9765 OPJ_BYTE l_data [2];
9766 opj_tcp_v2_t * l_tcp;
9769 assert(p_stream != 00);
9770 assert(p_j2k != 00);
9771 assert(p_manager != 00);
9773 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9774 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9778 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9779 if (! l_tcp->m_data) {
9780 j2k_tcp_destroy(l_tcp);
9784 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9788 p_j2k->cstr_index) ) {
9789 j2k_tcp_destroy(l_tcp);
9790 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9794 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9798 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9799 * we destroy just the data which will be re-read in read_tile_header*/
9800 /*j2k_tcp_destroy(l_tcp);
9801 p_j2k->m_tcd->tcp = 0;*/
9802 j2k_tcp_data_destroy(l_tcp);
9804 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9805 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9807 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9808 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9809 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9813 opj_read_bytes(l_data,&l_current_marker,2);
9815 if (l_current_marker == J2K_MS_EOC) {
9816 p_j2k->m_current_tile_number = 0;
9817 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9819 else if (l_current_marker != J2K_MS_SOT)
9821 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9830 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9832 OPJ_UINT32 i,j,k = 0;
9833 OPJ_UINT32 l_width_src,l_height_src;
9834 OPJ_UINT32 l_width_dest,l_height_dest;
9835 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9836 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9837 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9838 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9839 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9841 opj_image_comp_t * l_img_comp_src = 00;
9842 opj_image_comp_t * l_img_comp_dest = 00;
9844 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9845 opj_image_t * l_image_src = 00;
9846 OPJ_UINT32 l_size_comp, l_remaining;
9847 OPJ_INT32 * l_dest_ptr;
9848 opj_tcd_resolution_v2_t* l_res= 00;
9850 l_tilec = p_tcd->tcd_image->tiles->comps;
9851 l_image_src = p_tcd->image;
9852 l_img_comp_src = l_image_src->comps;
9854 l_img_comp_dest = p_output_image->comps;
9856 for (i=0; i<l_image_src->numcomps; i++) {
9858 /* Allocate output component buffer if necessary */
9859 if (!l_img_comp_dest->data) {
9861 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9862 if (! l_img_comp_dest->data) {
9867 /* Copy info from decoded comp image to output image */
9868 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9871 /* Compute the precision of the output buffer */
9872 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9873 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9874 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9880 if (l_size_comp == 3) {
9885 /* Current tile component size*/
9887 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9888 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9891 l_width_src = (l_res->x1 - l_res->x0);
9892 l_height_src = (l_res->y1 - l_res->y0);
9894 /* Border of the current output component*/
9895 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9896 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9897 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9898 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9901 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9902 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9906 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9907 * of the input buffer (decoded tile component) which will be move
9908 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9909 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9910 * by this input area.
9912 assert( l_res->x0 >= 0);
9913 assert( l_res->x1 >= 0);
9914 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9915 l_start_x_dest = l_res->x0 - l_x0_dest;
9916 l_offset_x0_src = 0;
9918 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9919 l_width_dest = l_width_src;
9920 l_offset_x1_src = 0;
9923 l_width_dest = l_x1_dest - l_res->x0 ;
9924 l_offset_x1_src = l_width_src - l_width_dest;
9928 l_start_x_dest = 0 ;
9929 l_offset_x0_src = l_x0_dest - l_res->x0;
9931 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9932 l_width_dest = l_width_src - l_offset_x0_src;
9933 l_offset_x1_src = 0;
9936 l_width_dest = l_img_comp_dest->w ;
9937 l_offset_x1_src = l_res->x1 - l_x1_dest;
9941 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9942 l_start_y_dest = l_res->y0 - l_y0_dest;
9943 l_offset_y0_src = 0;
9945 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9946 l_height_dest = l_height_src;
9947 l_offset_y1_src = 0;
9950 l_height_dest = l_y1_dest - l_res->y0 ;
9951 l_offset_y1_src = l_height_src - l_height_dest;
9955 l_start_y_dest = 0 ;
9956 l_offset_y0_src = l_y0_dest - l_res->y0;
9958 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9959 l_height_dest = l_height_src - l_offset_y0_src;
9960 l_offset_y1_src = 0;
9963 l_height_dest = l_img_comp_dest->h ;
9964 l_offset_y1_src = l_res->y1 - l_y1_dest;
9968 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9973 /* Compute the input buffer offset */
9974 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9975 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9976 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9978 /* Compute the output buffer offset */
9979 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9980 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9982 /* Move the output buffer to the first place where we will write*/
9983 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9986 fprintf(stdout, "COMPO[%d]:\n",i);
9987 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9988 "\t tile offset:%d, %d, %d, %d\n"
9989 "\t buffer offset: %d; %d, %d\n",
9990 l_res->x0, l_res->y0, l_width_src, l_height_src,
9991 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9992 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9994 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9995 "\t start offset: %d, line offset= %d\n",
9996 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
10000 switch (l_size_comp) {
10003 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
10004 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
10006 if (l_img_comp_src->sgnd) {
10007 for (j = 0 ; j < l_height_dest ; ++j) {
10008 for ( k = 0 ; k < l_width_dest ; ++k) {
10009 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
10012 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
10013 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
10017 for ( j = 0 ; j < l_height_dest ; ++j ) {
10018 for ( k = 0 ; k < l_width_dest ; ++k) {
10019 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
10022 l_dest_ptr+= l_line_offset_dest;
10023 l_src_ptr += l_line_offset_src;
10027 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
10028 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
10033 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
10034 l_src_ptr += l_start_offset_src;
10036 if (l_img_comp_src->sgnd) {
10037 for (j=0;j<l_height_dest;++j) {
10038 for (k=0;k<l_width_dest;++k) {
10039 *(l_dest_ptr++) = *(l_src_ptr++);
10042 l_dest_ptr+= l_line_offset_dest;
10043 l_src_ptr += l_line_offset_src ;
10047 for (j=0;j<l_height_dest;++j) {
10048 for (k=0;k<l_width_dest;++k) {
10049 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10052 l_dest_ptr+= l_line_offset_dest;
10053 l_src_ptr += l_line_offset_src ;
10057 l_src_ptr += l_end_offset_src;
10058 p_data = (OPJ_BYTE*) l_src_ptr;
10063 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
10064 l_src_ptr += l_start_offset_src;
10066 for (j=0;j<l_height_dest;++j) {
10067 for (k=0;k<l_width_dest;++k) {
10068 *(l_dest_ptr++) = (*(l_src_ptr++));
10071 l_dest_ptr+= l_line_offset_dest;
10072 l_src_ptr += l_line_offset_src ;
10075 l_src_ptr += l_end_offset_src;
10076 p_data = (OPJ_BYTE*) l_src_ptr;
10090 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
10092 * @param p_j2k the jpeg2000 codec.
10093 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
10094 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
10095 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
10096 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
10097 * @param p_manager the user event manager
10099 * @return true if the area could be set.
10101 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
10102 opj_image_t* p_image,
10103 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
10104 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
10105 struct opj_event_mgr * p_manager )
10107 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
10108 opj_image_t * l_image = p_j2k->m_private_image;
10110 OPJ_UINT32 it_comp;
10111 OPJ_INT32 l_comp_x1, l_comp_y1;
10112 opj_image_comp_t* l_img_comp = NULL;
10114 /* Check if we are read the main header */
10115 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
10116 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
10120 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
10121 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
10123 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10124 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10125 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10126 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10132 /* Check if the positions provided by the user are correct */
10135 assert(p_start_x > 0 );
10136 assert(p_start_y > 0 );
10138 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
10139 opj_event_msg_v2(p_manager, EVT_ERROR,
10140 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
10141 p_start_x, l_image->x1);
10144 else if ((OPJ_UINT32)p_start_x < l_image->x0){
10145 opj_event_msg_v2(p_manager, EVT_WARNING,
10146 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
10147 p_start_x, l_image->x0);
10148 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10149 p_image->x0 = l_image->x0;
10152 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
10153 p_image->x0 = p_start_x;
10157 if ((OPJ_UINT32)p_start_y > l_image->y1){
10158 opj_event_msg_v2(p_manager, EVT_ERROR,
10159 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
10160 p_start_y, l_image->y1);
10163 else if ((OPJ_UINT32)p_start_y < l_image->y0){
10164 opj_event_msg_v2(p_manager, EVT_WARNING,
10165 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
10166 p_start_y, l_image->y0);
10167 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10168 p_image->y0 = l_image->y0;
10171 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
10172 p_image->y0 = p_start_y;
10176 assert((OPJ_UINT32)p_end_x > 0);
10177 assert((OPJ_UINT32)p_end_y > 0);
10178 if ((OPJ_UINT32)p_end_x < l_image->x0) {
10179 opj_event_msg_v2(p_manager, EVT_ERROR,
10180 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
10181 p_end_x, l_image->x0);
10184 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
10185 opj_event_msg_v2(p_manager, EVT_WARNING,
10186 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
10187 p_end_x, l_image->x1);
10188 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10189 p_image->x1 = l_image->x1;
10192 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
10193 p_image->x1 = p_end_x;
10197 if ((OPJ_UINT32)p_end_y < l_image->y0) {
10198 opj_event_msg_v2(p_manager, EVT_ERROR,
10199 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
10200 p_end_y, l_image->y0);
10203 if ((OPJ_UINT32)p_end_y > l_image->y1){
10204 opj_event_msg_v2(p_manager, EVT_WARNING,
10205 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
10206 p_end_y, l_image->y1);
10207 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10208 p_image->y1 = l_image->y1;
10211 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
10212 p_image->y1 = p_end_y;
10216 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
10218 l_img_comp = p_image->comps;
10219 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
10223 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10224 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10225 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10226 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10228 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
10229 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10231 opj_event_msg_v2(p_manager, EVT_ERROR,
10232 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
10236 l_img_comp->w = l_w;
10238 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
10239 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10241 opj_event_msg_v2(p_manager, EVT_ERROR,
10242 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
10246 l_img_comp->h = l_h;
10251 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
10252 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
10259 /* ----------------------------------------------------------------------- */
10260 /* J2K / JPT decoder interface */
10261 /* ----------------------------------------------------------------------- */
10263 * Creates a J2K decompression structure.
10265 * @return a handle to a J2K decompressor if successful, NULL otherwise.
10267 opj_j2k_v2_t* j2k_create_decompress_v2(void)
10269 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
10273 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
10275 l_j2k->m_is_decoder = 1;
10276 l_j2k->m_cp.m_is_decoder = 1;
10278 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
10279 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
10280 j2k_destroy(l_j2k);
10283 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
10285 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
10286 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
10287 j2k_destroy(l_j2k);
10291 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
10293 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
10295 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
10297 /* codestream index creation */
10298 l_j2k->cstr_index = j2k_create_cstr_index();
10300 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
10301 if (!l_j2k->cstr_index){
10302 j2k_destroy(l_j2k);
10306 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
10309 /* validation list creation */
10310 l_j2k->m_validation_list = opj_procedure_list_create();
10311 if (! l_j2k->m_validation_list) {
10312 j2k_destroy(l_j2k);
10316 /* execution list creation */
10317 l_j2k->m_procedure_list = opj_procedure_list_create();
10318 if (! l_j2k->m_procedure_list) {
10319 j2k_destroy(l_j2k);
10327 opj_codestream_index_t* j2k_create_cstr_index(void)
10329 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
10330 opj_calloc(1,sizeof(opj_codestream_index_t));
10334 cstr_index->maxmarknum = 100;
10335 cstr_index->marknum = 0;
10336 cstr_index->marker = (opj_marker_info_t*)
10337 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
10338 if (!cstr_index-> marker)
10341 cstr_index->tile_index = NULL;
10348 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
10350 * @param p_tile_no the tile index.
10351 * @param p_comp_no the component being outputted.
10352 * @param p_j2k the J2K codec.
10354 * @return the number of bytes taken by the SPCod element.
10356 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
10357 OPJ_UINT32 p_tile_no,
10358 OPJ_UINT32 p_comp_no )
10360 opj_cp_v2_t *l_cp = 00;
10361 opj_tcp_v2_t *l_tcp = 00;
10362 opj_tccp_t *l_tccp = 00;
10364 /* preconditions */
10365 assert(p_j2k != 00);
10367 l_cp = &(p_j2k->m_cp);
10368 l_tcp = &l_cp->tcps[p_tile_no];
10369 l_tccp = &l_tcp->tccps[p_comp_no];
10371 /* preconditions again */
10372 assert(p_tile_no < (l_cp->tw * l_cp->th));
10373 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10375 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10376 return 5 + l_tccp->numresolutions;
10384 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10386 * @param p_comp_no the component number to output.
10387 * @param p_stream the stream to write data to.
10388 * @param p_j2k J2K codec.
10389 * @param p_manager the user event manager.
10392 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10393 OPJ_UINT32 p_tile_no,
10394 OPJ_UINT32 p_comp_no,
10396 OPJ_UINT32 * p_header_size,
10397 struct opj_event_mgr * p_manager )
10400 opj_cp_v2_t *l_cp = 00;
10401 opj_tcp_v2_t *l_tcp = 00;
10402 opj_tccp_t *l_tccp = 00;
10404 /* preconditions */
10405 assert(p_j2k != 00);
10406 assert(p_header_size != 00);
10407 assert(p_manager != 00);
10408 assert(p_data != 00);
10410 l_cp = &(p_j2k->m_cp);
10411 l_tcp = &l_cp->tcps[p_tile_no];
10412 l_tccp = &l_tcp->tccps[p_comp_no];
10414 /* preconditions again */
10415 assert(p_tile_no < (l_cp->tw * l_cp->th));
10416 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10418 if (*p_header_size < 5) {
10419 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10423 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
10426 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
10429 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
10432 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
10435 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
10438 *p_header_size = *p_header_size - 5;
10440 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10442 if (*p_header_size < l_tccp->numresolutions) {
10443 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10447 for (i = 0; i < l_tccp->numresolutions; ++i) {
10448 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
10452 *p_header_size = *p_header_size - l_tccp->numresolutions;
10459 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10460 * @param p_header_data the data contained in the COM box.
10461 * @param p_j2k the jpeg2000 codec.
10462 * @param p_header_size the size of the data contained in the COM marker.
10463 * @param p_manager the user event manager.
10465 opj_bool j2k_read_SPCod_SPCoc(
10466 opj_j2k_v2_t *p_j2k,
10468 OPJ_BYTE * p_header_data,
10469 OPJ_UINT32 * p_header_size,
10470 struct opj_event_mgr * p_manager
10473 OPJ_UINT32 i, l_tmp;
10474 opj_cp_v2_t *l_cp = NULL;
10475 opj_tcp_v2_t *l_tcp = NULL;
10476 opj_tccp_t *l_tccp = NULL;
10477 OPJ_BYTE * l_current_ptr = NULL;
10479 /* preconditions */
10480 assert(p_j2k != 00);
10481 assert(p_manager != 00);
10482 assert(p_header_data != 00);
10484 l_cp = &(p_j2k->m_cp);
10485 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10486 &l_cp->tcps[p_j2k->m_current_tile_number] :
10487 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10489 /* precondition again */
10490 assert(compno < p_j2k->m_private_image->numcomps);
10492 l_tccp = &l_tcp->tccps[compno];
10493 l_current_ptr = p_header_data;
10495 /* make sure room is sufficient */
10496 if (*p_header_size < 5) {
10497 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10501 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10502 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10505 /* If user wants to remove more resolutions than the codestream contains, return error */
10506 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10507 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10508 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10509 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10513 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10515 l_tccp->cblkw += 2;
10517 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10519 l_tccp->cblkh += 2;
10521 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10524 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10527 *p_header_size = *p_header_size - 5;
10529 /* use custom precinct size ? */
10530 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10531 if (*p_header_size < l_tccp->numresolutions) {
10532 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10536 for (i = 0; i < l_tccp->numresolutions; ++i) {
10537 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10539 l_tccp->prcw[i] = l_tmp & 0xf;
10540 l_tccp->prch[i] = l_tmp >> 4;
10543 *p_header_size = *p_header_size - l_tccp->numresolutions;
10546 /* set default size for the precinct width and height */
10547 for (i = 0; i < l_tccp->numresolutions; ++i) {
10548 l_tccp->prcw[i] = 15;
10549 l_tccp->prch[i] = 15;
10553 #ifdef WIP_REMOVE_MSD
10555 if (p_j2k->cstr_info && compno == 0) {
10556 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10558 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10559 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10560 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10561 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10562 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10565 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10566 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10575 * Copies the tile component parameters of all the component from the first tile component.
10577 * @param p_j2k the J2k codec.
10579 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10583 opj_cp_v2_t *l_cp = NULL;
10584 opj_tcp_v2_t *l_tcp = NULL;
10585 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10586 OPJ_UINT32 l_prc_size;
10588 /* preconditions */
10589 assert(p_j2k != 00);
10591 l_cp = &(p_j2k->m_cp);
10592 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10593 &l_cp->tcps[p_j2k->m_current_tile_number] :
10594 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10596 l_ref_tccp = &l_tcp->tccps[0];
10597 l_copied_tccp = l_ref_tccp + 1;
10598 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10600 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10601 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10602 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10603 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10604 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10605 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10606 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10607 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10613 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10615 * @param p_tile_no the tile index.
10616 * @param p_comp_no the component being outputted.
10617 * @param p_j2k the J2K codec.
10619 * @return the number of bytes taken by the SPCod element.
10621 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10622 OPJ_UINT32 p_tile_no,
10623 OPJ_UINT32 p_comp_no )
10625 OPJ_UINT32 l_num_bands;
10627 opj_cp_v2_t *l_cp = 00;
10628 opj_tcp_v2_t *l_tcp = 00;
10629 opj_tccp_t *l_tccp = 00;
10631 /* preconditions */
10632 assert(p_j2k != 00);
10634 l_cp = &(p_j2k->m_cp);
10635 l_tcp = &l_cp->tcps[p_tile_no];
10636 l_tccp = &l_tcp->tccps[p_comp_no];
10638 /* preconditions again */
10639 assert(p_tile_no < l_cp->tw * l_cp->th);
10640 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10642 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10644 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10645 return 1 + l_num_bands;
10648 return 1 + 2*l_num_bands;
10653 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10655 * @param p_tile_no the tile to output.
10656 * @param p_comp_no the component number to output.
10657 * @param p_data the data buffer.
10658 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10659 * @param p_j2k J2K codec.
10660 * @param p_manager the user event manager.
10663 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10664 OPJ_UINT32 p_tile_no,
10665 OPJ_UINT32 p_comp_no,
10667 OPJ_UINT32 * p_header_size,
10668 struct opj_event_mgr * p_manager )
10670 OPJ_UINT32 l_header_size;
10671 OPJ_UINT32 l_band_no, l_num_bands;
10672 OPJ_UINT32 l_expn,l_mant;
10674 opj_cp_v2_t *l_cp = 00;
10675 opj_tcp_v2_t *l_tcp = 00;
10676 opj_tccp_t *l_tccp = 00;
10678 /* preconditions */
10679 assert(p_j2k != 00);
10680 assert(p_header_size != 00);
10681 assert(p_manager != 00);
10682 assert(p_data != 00);
10684 l_cp = &(p_j2k->m_cp);
10685 l_tcp = &l_cp->tcps[p_tile_no];
10686 l_tccp = &l_tcp->tccps[p_comp_no];
10688 /* preconditions again */
10689 assert(p_tile_no < l_cp->tw * l_cp->th);
10690 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10692 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10694 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10695 l_header_size = 1 + l_num_bands;
10697 if (*p_header_size < l_header_size) {
10698 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10702 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10705 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10706 l_expn = l_tccp->stepsizes[l_band_no].expn;
10707 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10712 l_header_size = 1 + 2*l_num_bands;
10714 if (*p_header_size < l_header_size) {
10715 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10719 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10722 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10723 l_expn = l_tccp->stepsizes[l_band_no].expn;
10724 l_mant = l_tccp->stepsizes[l_band_no].mant;
10726 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10731 *p_header_size = *p_header_size - l_header_size;
10737 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10739 * @param p_comp_no the component being targeted.
10740 * @param p_header_data the data contained in the COM box.
10741 * @param p_j2k the jpeg2000 codec.
10742 * @param p_header_size the size of the data contained in the COM marker.
10743 * @param p_manager the user event manager.
10745 opj_bool j2k_read_SQcd_SQcc(
10746 opj_j2k_v2_t *p_j2k,
10747 OPJ_UINT32 p_comp_no,
10748 OPJ_BYTE* p_header_data,
10749 OPJ_UINT32 * p_header_size,
10750 struct opj_event_mgr * p_manager
10754 OPJ_UINT32 l_band_no;
10755 opj_cp_v2_t *l_cp = 00;
10756 opj_tcp_v2_t *l_tcp = 00;
10757 opj_tccp_t *l_tccp = 00;
10758 OPJ_BYTE * l_current_ptr = 00;
10759 OPJ_UINT32 l_tmp, l_num_band;
10762 assert(p_j2k != 00);
10763 assert(p_manager != 00);
10764 assert(p_header_data != 00);
10766 l_cp = &(p_j2k->m_cp);
10767 /* come from tile part header or main header ?*/
10768 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10769 &l_cp->tcps[p_j2k->m_current_tile_number] :
10770 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10772 /* precondition again*/
10773 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10775 l_tccp = &l_tcp->tccps[p_comp_no];
10776 l_current_ptr = p_header_data;
10778 if (*p_header_size < 1) {
10779 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10782 *p_header_size -= 1;
10784 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10787 l_tccp->qntsty = l_tmp & 0x1f;
10788 l_tccp->numgbits = l_tmp >> 5;
10789 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10793 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10795 (*p_header_size) / 2;
10797 if( l_num_band > J2K_MAXBANDS ) {
10798 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10799 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10800 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10801 /*return OPJ_FALSE;*/
10806 if (l_cp->correct) {
10808 /* if JPWL is on, we check whether there are too many subbands */
10809 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10810 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10811 "JPWL: bad number of subbands in Sqcx (%d)\n",
10813 if (!JPWL_ASSUME) {
10814 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10817 /* we try to correct */
10819 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10820 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10825 #endif /* USE_JPWL */
10827 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10828 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10829 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10831 if (l_band_no < J2K_MAXBANDS){
10832 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10833 l_tccp->stepsizes[l_band_no].mant = 0;
10836 *p_header_size = *p_header_size - l_num_band;
10839 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10840 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10842 if (l_band_no < J2K_MAXBANDS){
10843 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10844 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10847 *p_header_size = *p_header_size - 2*l_num_band;
10850 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10851 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10852 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10853 l_tccp->stepsizes[l_band_no].expn =
10854 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10855 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10856 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10864 * Copies the tile component parameters of all the component from the first tile component.
10866 * @param p_j2k the J2k codec.
10868 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10871 opj_cp_v2_t *l_cp = NULL;
10872 opj_tcp_v2_t *l_tcp = NULL;
10873 opj_tccp_t *l_ref_tccp = NULL;
10874 opj_tccp_t *l_copied_tccp = NULL;
10877 /* preconditions */
10878 assert(p_j2k != 00);
10880 l_cp = &(p_j2k->m_cp);
10881 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10882 &l_cp->tcps[p_j2k->m_current_tile_number] :
10883 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10885 l_ref_tccp = &l_tcp->tccps[0];
10886 l_copied_tccp = l_ref_tccp + 1;
10887 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10889 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10890 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10891 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10892 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10898 * Dump some elements from the J2K decompression structure .
10900 *@param p_j2k the jpeg2000 codec.
10901 *@param flag flag to describe what elments are dump.
10902 *@param out_stream output stream where dump the elements.
10905 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10907 /* Check if the flag is compatible with j2k file*/
10908 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10909 fprintf(out_stream, "Wrong flag\n");
10913 /* Dump the image_header */
10914 if (flag & OPJ_IMG_INFO){
10915 if (p_j2k->m_private_image)
10916 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10919 /* Dump the codestream info from main header */
10920 if (flag & OPJ_J2K_MH_INFO){
10921 j2k_dump_MH_info(p_j2k, out_stream);
10925 /* Dump the codestream info of the current tile */
10926 if (flag & OPJ_J2K_TH_INFO){
10930 /* Dump the codestream index from main header */
10931 if (flag & OPJ_J2K_MH_IND){
10932 j2k_dump_MH_index(p_j2k, out_stream);
10935 /* Dump the codestream index of the current tile */
10936 if (flag & OPJ_J2K_TH_IND){
10943 * Dump index elements of the codestream extract from the main header.
10945 *@param p_j2k the jpeg2000 codec.
10946 *@param out_stream output stream where dump the elements.
10949 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10951 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10952 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10954 fprintf(out_stream, "Codestream index from main header: {\n");
10956 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10957 "\t Main header end position=%" PRIi64 "\n",
10958 cstr_index->main_head_start, cstr_index->main_head_end);
10960 fprintf(out_stream, "\t Marker list: {\n");
10962 if (cstr_index->marker){
10963 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10964 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10965 cstr_index->marker[it_marker].type,
10966 cstr_index->marker[it_marker].pos,
10967 cstr_index->marker[it_marker].len );
10971 fprintf(out_stream, "\t }\n");
10974 if (cstr_index->tile_index){
10975 fprintf(out_stream, "\t Tile index: {\n");
10977 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10978 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10980 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10982 if (cstr_index->tile_index[it_tile].tp_index){
10983 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10984 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10986 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10987 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10988 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10992 if (cstr_index->tile_index[it_tile].marker){
10993 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10994 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10995 cstr_index->tile_index[it_tile].marker[it_marker].type,
10996 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10997 cstr_index->tile_index[it_tile].marker[it_marker].len );
11001 fprintf(out_stream,"\t }\n");
11004 fprintf(out_stream,"}\n");
11009 * Dump info elements of the codestream extract from the main header.
11011 *@param p_j2k the jpeg2000 codec.
11012 *@param out_stream output stream where dump the elements.
11015 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11017 opj_tcp_v2_t * l_default_tile=NULL;
11019 fprintf(out_stream, "Codestream info from main header: {\n");
11021 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
11022 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
11023 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
11025 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11026 if (l_default_tile)
11029 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
11031 fprintf(out_stream, "\t default tile {\n");
11032 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
11033 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
11034 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
11035 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
11037 for (compno = 0; compno < numcomps; compno++) {
11038 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11040 OPJ_INT32 bandno, numbands;
11043 fprintf(out_stream, "\t\t comp %d {\n", compno);
11044 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
11045 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
11046 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
11047 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
11048 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
11049 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
11051 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
11052 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
11053 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
11055 fprintf(out_stream, "\n");
11057 /* quantization style*/
11058 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
11059 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
11060 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
11061 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11062 for (bandno = 0; bandno < numbands; bandno++) {
11063 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
11064 l_tccp->stepsizes[bandno].expn);
11066 fprintf(out_stream, "\n");
11069 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
11071 fprintf(out_stream, "\t\t }\n");
11072 } /*end of component of default tile*/
11073 fprintf(out_stream, "\t }\n"); /*end of default tile*/
11077 fprintf(out_stream, "}\n");
11082 * Dump an image header structure.
11084 *@param img_header the image header to dump.
11085 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11086 *@param out_stream output stream where dump the elements.
11088 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
11092 if (dev_dump_flag){
11093 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
11097 fprintf(out_stream, "Image info {\n");
11098 tab[0] = '\t';tab[1] = '\0';
11101 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
11102 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
11103 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
11105 if (img_header->comps){
11107 for (compno = 0; compno < img_header->numcomps; compno++) {
11108 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
11109 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
11110 fprintf(out_stream,"%s}\n",tab);
11114 fprintf(out_stream, "}\n");
11118 * Dump a component image header structure.
11120 *@param comp_header the component image header to dump.
11121 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11122 *@param out_stream output stream where dump the elements.
11124 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
11128 if (dev_dump_flag){
11129 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
11132 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
11135 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
11136 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
11137 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
11140 fprintf(out_stream, "}\n");
11145 * Get the codestream info from a JPEG2000 codec.
11147 *@param p_j2k the component image header to dump.
11149 *@return the codestream information extract from the jpg2000 codec
11151 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
11154 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
11155 opj_tcp_v2_t *l_default_tile;
11156 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
11158 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
11160 cstr_info->tx0 = p_j2k->m_cp.tx0;
11161 cstr_info->ty0 = p_j2k->m_cp.ty0;
11162 cstr_info->tdx = p_j2k->m_cp.tdx;
11163 cstr_info->tdy = p_j2k->m_cp.tdy;
11164 cstr_info->tw = p_j2k->m_cp.tw;
11165 cstr_info->th = p_j2k->m_cp.th;
11167 cstr_info->tile_info = NULL; /* Not fill from the main header*/
11169 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11171 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
11172 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
11173 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
11174 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
11176 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
11178 for (compno = 0; compno < numcomps; compno++) {
11179 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11180 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
11181 OPJ_INT32 bandno, numbands;
11184 l_tccp_info->csty = l_tccp->csty;
11185 l_tccp_info->numresolutions = l_tccp->numresolutions;
11186 l_tccp_info->cblkw = l_tccp->cblkw;
11187 l_tccp_info->cblkh = l_tccp->cblkh;
11188 l_tccp_info->cblksty = l_tccp->cblksty;
11189 l_tccp_info->qmfbid = l_tccp->qmfbid;
11190 if (l_tccp->numresolutions < J2K_MAXRLVLS)
11192 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
11193 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
11196 /* quantization style*/
11197 l_tccp_info->qntsty = l_tccp->qntsty;
11198 l_tccp_info->numgbits = l_tccp->numgbits;
11200 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11201 if (numbands < J2K_MAXBANDS) {
11202 for (bandno = 0; bandno < numbands; bandno++) {
11203 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
11204 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
11209 l_tccp_info->roishift = l_tccp->roishift;
11217 * Get the codestream index from a JPEG2000 codec.
11219 *@param p_j2k the component image header to dump.
11221 *@return the codestream index extract from the jpg2000 codec
11223 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
11225 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
11226 opj_calloc(1,sizeof(opj_codestream_index_t));
11230 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
11231 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
11232 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
11234 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
11235 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
11236 if (!l_cstr_index->marker){
11237 opj_free( l_cstr_index);
11241 if (p_j2k->cstr_index->marker)
11242 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
11244 opj_free(l_cstr_index->marker);
11245 l_cstr_index->marker = NULL;
11248 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
11249 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
11250 if (!l_cstr_index->tile_index){
11251 opj_free( l_cstr_index->marker);
11252 opj_free( l_cstr_index);
11256 if (!p_j2k->cstr_index->tile_index){
11257 opj_free(l_cstr_index->tile_index);
11258 l_cstr_index->tile_index = NULL;
11261 OPJ_UINT32 it_tile = 0;
11262 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
11265 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
11267 l_cstr_index->tile_index[it_tile].marker =
11268 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
11270 if (!l_cstr_index->tile_index[it_tile].marker) {
11271 OPJ_UINT32 it_tile_free;
11273 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11274 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11277 opj_free( l_cstr_index->tile_index);
11278 opj_free( l_cstr_index->marker);
11279 opj_free( l_cstr_index);
11283 if (p_j2k->cstr_index->tile_index[it_tile].marker)
11284 memcpy( l_cstr_index->tile_index[it_tile].marker,
11285 p_j2k->cstr_index->tile_index[it_tile].marker,
11286 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
11288 opj_free(l_cstr_index->tile_index[it_tile].marker);
11289 l_cstr_index->tile_index[it_tile].marker = NULL;
11292 /* Tile part index*/
11293 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
11295 l_cstr_index->tile_index[it_tile].tp_index =
11296 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
11298 if(!l_cstr_index->tile_index[it_tile].tp_index){
11299 OPJ_UINT32 it_tile_free;
11301 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11302 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11303 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
11306 opj_free( l_cstr_index->tile_index);
11307 opj_free( l_cstr_index->marker);
11308 opj_free( l_cstr_index);
11312 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
11313 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
11314 p_j2k->cstr_index->tile_index[it_tile].tp_index,
11315 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
11318 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
11319 l_cstr_index->tile_index[it_tile].tp_index = NULL;
11322 /* Packet index (NOT USED)*/
11323 l_cstr_index->tile_index[it_tile].nb_packet = 0;
11324 l_cstr_index->tile_index[it_tile].packet_index = NULL;
11329 return l_cstr_index;
11332 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
11334 OPJ_UINT32 it_tile=0;
11336 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
11337 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
11338 if (!p_j2k->cstr_index->tile_index)
11341 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
11342 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
11343 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
11344 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
11345 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
11346 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11356 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
11357 opj_stream_private_t *p_stream,
11358 opj_event_mgr_t * p_manager)
11360 opj_bool l_go_on = OPJ_TRUE;
11361 OPJ_UINT32 l_current_tile_no;
11362 OPJ_UINT32 l_data_size,l_max_data_size;
11363 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11364 OPJ_UINT32 l_nb_comps;
11365 OPJ_BYTE * l_current_data;
11367 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11368 if (! l_current_data) {
11371 l_max_data_size = 1000;
11373 /*Allocate and initialize some elements of codestrem index*/
11374 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11375 opj_free(l_current_data);
11380 if (! j2k_read_tile_header( p_j2k,
11381 &l_current_tile_no,
11383 &l_tile_x0, &l_tile_y0,
11384 &l_tile_x1, &l_tile_y1,
11389 opj_free(l_current_data);
11397 if (l_data_size > l_max_data_size) {
11398 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11399 if (! l_current_data) {
11400 opj_free(l_current_data);
11404 l_max_data_size = l_data_size;
11407 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11408 opj_free(l_current_data);
11411 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);
11413 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11414 opj_free(l_current_data);
11417 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11421 opj_free(l_current_data);
11427 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11429 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11432 assert(p_j2k != 00);
11434 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11435 /* DEVELOPER CORNER, add your custom procedures */
11440 * Read and decode one tile.
11442 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
11443 opj_stream_private_t *p_stream,
11444 opj_event_mgr_t * p_manager)
11446 opj_bool l_go_on = OPJ_TRUE;
11447 OPJ_UINT32 l_current_tile_no;
11448 OPJ_UINT32 l_tile_no_to_dec;
11449 OPJ_UINT32 l_data_size,l_max_data_size;
11450 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11451 OPJ_UINT32 l_nb_comps;
11452 OPJ_BYTE * l_current_data;
11454 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11455 if (! l_current_data) {
11458 l_max_data_size = 1000;
11460 /*Allocate and initialize some elements of codestrem index if not already done*/
11461 if( !p_j2k->cstr_index->tile_index)
11463 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11464 opj_free(l_current_data);
11468 /* Move into the codestream to the first SOT used to decode the desired tile */
11469 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11470 if (p_j2k->cstr_index->tile_index)
11471 if(p_j2k->cstr_index->tile_index->tp_index)
11473 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11474 /* the index for this tile has not been built,
11475 * so move to the last SOT read */
11476 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11477 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11482 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)) {
11483 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11487 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11488 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11489 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11493 if (! j2k_read_tile_header( p_j2k,
11494 &l_current_tile_no,
11496 &l_tile_x0, &l_tile_y0,
11497 &l_tile_x1, &l_tile_y1,
11502 opj_free(l_current_data);
11511 if (l_data_size > l_max_data_size) {
11512 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11513 if (! l_current_data) {
11514 opj_free(l_current_data);
11518 l_max_data_size = l_data_size;
11523 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11524 opj_free(l_current_data);
11527 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);
11529 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11530 opj_free(l_current_data);
11533 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11535 if(l_current_tile_no == l_tile_no_to_dec)
11537 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11538 if (opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) {
11539 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11545 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);
11550 opj_free(l_current_data);
11557 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11559 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11562 assert(p_j2k != 00);
11564 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11565 /* DEVELOPER CORNER, add your custom procedures */
11571 * Decodes the tiles of the stream.
11573 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
11574 opj_stream_private_t * p_stream,
11575 opj_image_t * p_image,
11576 opj_event_mgr_t * p_manager)
11583 p_j2k->m_output_image = opj_image_create0();
11584 if (! (p_j2k->m_output_image)) {
11587 opj_copy_image_header(p_image, p_j2k->m_output_image);
11589 /* customization of the decoding */
11590 j2k_setup_decoding(p_j2k);
11592 /* Decode the codestream */
11593 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11594 opj_image_destroy(p_j2k->m_private_image);
11595 p_j2k->m_private_image = NULL;
11599 /* Move data and copy one information from codec to output image*/
11600 for (compno = 0; compno < p_image->numcomps; compno++) {
11601 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11602 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11603 p_j2k->m_output_image->comps[compno].data = NULL;
11611 * Get the decoded tile.
11613 * @param p_j2k the jpeg2000 codestream codec.
11614 * @param p_stream input_stream
11615 * @param p_image output image. .
11616 * @param p_manager the user event manager
11617 * @param tile_index index of the tile we want decode
11619 * @return true if succeed.
11621 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11622 opj_stream_private_t *p_stream,
11623 opj_image_t* p_image,
11624 struct opj_event_mgr * p_manager,
11625 OPJ_UINT32 tile_index )
11628 OPJ_UINT32 l_tile_x, l_tile_y;
11629 opj_image_comp_t* l_img_comp;
11632 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11636 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11637 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);
11641 /* Compute the dimension of the desired tile*/
11642 l_tile_x = tile_index % p_j2k->m_cp.tw;
11643 l_tile_y = tile_index / p_j2k->m_cp.tw;
11645 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11646 if (p_image->x0 < p_j2k->m_private_image->x0)
11647 p_image->x0 = p_j2k->m_private_image->x0;
11648 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11649 if (p_image->x1 > p_j2k->m_private_image->x1)
11650 p_image->x1 = p_j2k->m_private_image->x1;
11652 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11653 if (p_image->y0 < p_j2k->m_private_image->y0)
11654 p_image->y0 = p_j2k->m_private_image->y0;
11655 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11656 if (p_image->y1 > p_j2k->m_private_image->y1)
11657 p_image->y1 = p_j2k->m_private_image->y1;
11659 l_img_comp = p_image->comps;
11660 for (compno=0; compno < p_image->numcomps; ++compno)
11662 OPJ_INT32 l_comp_x1, l_comp_y1;
11664 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11666 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11667 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11668 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11669 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11671 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11672 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11677 /* Destroy the previous output image*/
11678 if (p_j2k->m_output_image)
11679 opj_image_destroy(p_j2k->m_output_image);
11681 /* Create the ouput image from the information previously computed*/
11682 p_j2k->m_output_image = opj_image_create0();
11683 if (! (p_j2k->m_output_image)) {
11686 opj_copy_image_header(p_image, p_j2k->m_output_image);
11688 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11690 /* customization of the decoding */
11691 j2k_setup_decoding_tile(p_j2k);
11693 /* Decode the codestream */
11694 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11695 opj_image_destroy(p_j2k->m_private_image);
11696 p_j2k->m_private_image = NULL;
11700 /* Move data and copy one information from codec to output image*/
11701 for (compno = 0; compno < p_image->numcomps; compno++) {
11702 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11704 if (p_image->comps[compno].data)
11705 opj_free(p_image->comps[compno].data);
11707 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11709 p_j2k->m_output_image->comps[compno].data = NULL;
11715 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11717 OPJ_UINT32 it_comp;
11719 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11721 if (p_j2k->m_private_image) {
11722 if (p_j2k->m_private_image->comps) {
11723 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11724 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11725 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11726 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11727 if ( res_factor >= max_res){
11728 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11731 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11744 * Encodes all the tiles in a row.
11746 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11747 opj_stream_private_t *p_stream,
11748 opj_event_mgr_t * p_manager )
11751 OPJ_UINT32 l_nb_tiles;
11752 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11753 OPJ_BYTE * l_current_data;
11755 /* preconditions */
11756 assert(p_j2k != 00);
11757 assert(p_stream != 00);
11758 assert(p_manager != 00);
11760 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11761 if (! l_current_data) {
11764 l_max_tile_size = 1000;
11766 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11767 for (i=0;i<l_nb_tiles;++i) {
11768 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11769 opj_free(l_current_data);
11773 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11774 if (l_current_tile_size > l_max_tile_size) {
11775 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11776 if (! l_current_data) {
11779 l_max_tile_size = l_current_tile_size;
11782 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11784 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11789 opj_free(l_current_data);
11794 * Ends the compression procedures and possibility add data to be read after the
11797 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11798 opj_stream_private_t *p_stream,
11799 struct opj_event_mgr * p_manager)
11801 /* customization of the encoding */
11802 j2k_setup_end_compress(p_j2k);
11804 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11814 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11816 * @param p_j2k the jpeg2000 codec.
11817 * @param p_stream the stream object.
11818 * @param p_manager the user event manager.
11820 * @return true if the codec is valid.
11822 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11823 opj_stream_private_t *p_stream,
11824 opj_image_t * p_image,
11825 opj_event_mgr_t * p_manager)
11827 /* preconditions */
11828 assert(p_j2k != 00);
11829 assert(p_stream != 00);
11830 assert(p_manager != 00);
11832 p_j2k->m_private_image = p_image;
11834 /* customization of the validation */
11835 j2k_setup_encoding_validation (p_j2k);
11837 /* validation of the parameters codec */
11838 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11842 /* customization of the encoding */
11843 j2k_setup_header_writting(p_j2k);
11846 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11856 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11857 OPJ_UINT32 p_tile_index,
11858 opj_stream_private_t *p_stream,
11859 opj_event_mgr_t * p_manager )
11862 if (p_tile_index != p_j2k->m_current_tile_number) {
11863 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11867 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);
11869 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11870 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11871 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11873 /* initialisation before tile encoding */
11874 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11884 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11886 OPJ_UINT32 i,j,k = 0;
11887 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11888 opj_image_comp_t * l_img_comp = 00;
11889 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11890 opj_image_t * l_image = 00;
11891 OPJ_UINT32 l_size_comp, l_remaining;
11892 OPJ_INT32 * l_src_ptr;
11893 l_tilec = p_tcd->tcd_image->tiles->comps;
11894 l_image = p_tcd->image;
11895 l_img_comp = l_image->comps;
11897 for (i=0;i<p_tcd->image->numcomps;++i) {
11898 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11899 l_remaining = l_img_comp->prec & 7; /* (%8) */
11904 if (l_size_comp == 3) {
11908 l_width = (l_tilec->x1 - l_tilec->x0);
11909 l_height = (l_tilec->y1 - l_tilec->y0);
11910 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11911 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11912 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11913 l_stride = l_image_width - l_width;
11914 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11916 switch (l_size_comp) {
11919 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11920 if (l_img_comp->sgnd) {
11921 for (j=0;j<l_height;++j) {
11922 for (k=0;k<l_width;++k) {
11923 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11927 l_src_ptr += l_stride;
11931 for (j=0;j<l_height;++j) {
11932 for (k=0;k<l_width;++k) {
11933 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11937 l_src_ptr += l_stride;
11941 p_data = (OPJ_BYTE*) l_dest_ptr;
11946 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11947 if (l_img_comp->sgnd) {
11948 for (j=0;j<l_height;++j) {
11949 for (k=0;k<l_width;++k) {
11950 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11952 l_src_ptr += l_stride;
11956 for (j=0;j<l_height;++j) {
11957 for (k=0;k<l_width;++k) {
11958 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11960 l_src_ptr += l_stride;
11964 p_data = (OPJ_BYTE*) l_dest_ptr;
11969 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11970 for (j=0;j<l_height;++j) {
11971 for (k=0;k<l_width;++k) {
11972 *(l_dest_ptr++) = *(l_src_ptr++);
11974 l_src_ptr += l_stride;
11977 p_data = (OPJ_BYTE*) l_dest_ptr;
11990 * @param p_j2k the jpeg2000 codec.
11991 * @param p_stream the stream to write data to.
11992 * @param p_manager the user event manager.
11994 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11996 OPJ_UINT32 p_data_size,
11997 opj_stream_private_t *p_stream,
11998 opj_event_mgr_t * p_manager )
12000 opj_tcd_v2_t * l_tcd = 00;
12001 opj_cp_v2_t * l_cp = 00;
12002 opj_tcp_v2_t * l_tcp = 00;
12003 OPJ_UINT32 l_nb_bytes_written;
12004 OPJ_BYTE * l_current_data = 00;
12005 OPJ_UINT32 l_tile_size = 0;
12006 OPJ_UINT32 l_available_data;
12008 /* preconditions */
12009 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12011 l_tcd = p_j2k->m_tcd;
12012 l_cp = &(p_j2k->m_cp);
12013 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12015 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
12016 l_available_data = l_tile_size;
12017 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
12019 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
12020 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismtach between tile data and sent data." );
12024 l_nb_bytes_written = 0;
12025 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12028 l_current_data += l_nb_bytes_written;
12029 l_available_data -= l_nb_bytes_written;
12031 l_nb_bytes_written = 0;
12032 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12036 l_available_data -= l_nb_bytes_written;
12037 l_nb_bytes_written = l_tile_size - l_available_data;
12039 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) {
12043 ++p_j2k->m_current_tile_number;
12050 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12051 * are valid. Developers wanting to extend the library can add their own validation procedures.
12053 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
12055 /* preconditions */
12056 assert(p_j2k != 00);
12058 /* DEVELOPER CORNER, insert your custom procedures */
12059 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc );
12061 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12062 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
12065 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
12066 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
12067 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
12071 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12072 * are valid. Developers wanting to extend the library can add their own validation procedures.
12074 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
12076 /* preconditions */
12077 assert(p_j2k != 00);
12079 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
12080 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
12082 /* DEVELOPER CORNER, add your custom validation procedure */
12083 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
12088 * Sets up the procedures to do on writing header.
12089 * Developers wanting to extend the library can add their own writing procedures.
12091 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
12093 /* preconditions */
12094 assert(p_j2k != 00);
12096 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
12097 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
12098 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
12099 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
12100 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
12103 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12104 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
12105 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm );
12107 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
12108 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc );
12112 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
12114 if (p_j2k->m_cp.comment != 00) {
12115 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com);
12118 /* DEVELOPER CORNER, insert your custom procedures */
12119 if (p_j2k->m_cp.rsiz & MCT) {
12120 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
12122 /* End of Developer Corner */
12124 if (p_j2k->cstr_index) {
12125 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
12128 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
12129 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
12133 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
12135 OPJ_UINT32 * p_data_written,
12136 OPJ_UINT32 p_total_data_size,
12137 opj_stream_private_t *p_stream,
12138 struct opj_event_mgr * p_manager )
12141 OPJ_UINT32 l_nb_bytes_written = 0;
12142 OPJ_UINT32 l_current_nb_bytes_written;
12143 OPJ_BYTE * l_begin_data = 00;
12145 opj_tcp_v2_t *l_tcp = 00;
12146 opj_tcd_v2_t * l_tcd = 00;
12147 opj_cp_v2_t * l_cp = 00;
12149 l_tcd = p_j2k->m_tcd;
12150 l_cp = &(p_j2k->m_cp);
12151 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12153 l_tcd->cur_pino = 0;
12155 /*Get number of tile parts*/
12156 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12161 l_current_nb_bytes_written = 0;
12162 l_begin_data = p_data;
12163 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
12168 l_nb_bytes_written += l_current_nb_bytes_written;
12169 p_data += l_current_nb_bytes_written;
12170 p_total_data_size -= l_current_nb_bytes_written;
12172 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
12173 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
12174 l_current_nb_bytes_written = 0;
12175 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12176 l_nb_bytes_written += l_current_nb_bytes_written;
12177 p_data += l_current_nb_bytes_written;
12178 p_total_data_size -= l_current_nb_bytes_written;
12180 l_current_nb_bytes_written = 0;
12181 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12182 l_nb_bytes_written += l_current_nb_bytes_written;
12183 p_data += l_current_nb_bytes_written;
12184 p_total_data_size -= l_current_nb_bytes_written;
12187 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
12188 l_current_nb_bytes_written = 0;
12189 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
12190 l_nb_bytes_written += l_current_nb_bytes_written;
12191 p_data += l_current_nb_bytes_written;
12192 p_total_data_size -= l_current_nb_bytes_written;
12196 l_current_nb_bytes_written = 0;
12197 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12201 l_nb_bytes_written += l_current_nb_bytes_written;
12202 * p_data_written = l_nb_bytes_written;
12204 /* Writing Psot in SOT marker */
12205 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
12207 if (l_cp->m_specific_param.m_enc.m_cinema){
12208 j2k_update_tlm(p_j2k,l_nb_bytes_written);
12214 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
12216 OPJ_UINT32 * p_data_written,
12217 OPJ_UINT32 p_total_data_size,
12218 opj_stream_private_t *p_stream,
12219 struct opj_event_mgr * p_manager
12222 OPJ_UINT32 tilepartno=0;
12223 OPJ_UINT32 l_nb_bytes_written = 0;
12224 OPJ_UINT32 l_current_nb_bytes_written;
12225 OPJ_UINT32 l_part_tile_size;
12226 OPJ_UINT32 tot_num_tp;
12229 OPJ_BYTE * l_begin_data;
12230 opj_tcp_v2_t *l_tcp = 00;
12231 opj_tcd_v2_t * l_tcd = 00;
12232 opj_cp_v2_t * l_cp = 00;
12235 l_tcd = p_j2k->m_tcd;
12236 l_cp = &(p_j2k->m_cp);
12237 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12239 /*Get number of tile parts*/
12240 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
12242 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
12243 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12244 l_current_nb_bytes_written = 0;
12245 l_part_tile_size = 0;
12246 l_begin_data = p_data;
12248 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12252 l_nb_bytes_written += l_current_nb_bytes_written;
12253 p_data += l_current_nb_bytes_written;
12254 p_total_data_size -= l_current_nb_bytes_written;
12255 l_part_tile_size += l_nb_bytes_written;
12257 l_current_nb_bytes_written = 0;
12258 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12262 p_data += l_current_nb_bytes_written;
12263 l_nb_bytes_written += l_current_nb_bytes_written;
12264 p_total_data_size -= l_current_nb_bytes_written;
12265 l_part_tile_size += l_nb_bytes_written;
12267 /* Writing Psot in SOT marker */
12268 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12270 if (l_cp->m_specific_param.m_enc.m_cinema) {
12271 j2k_update_tlm(p_j2k,l_part_tile_size);
12274 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12277 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
12278 l_tcd->cur_pino = pino;
12280 /*Get number of tile parts*/
12281 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
12282 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
12283 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12284 l_current_nb_bytes_written = 0;
12285 l_part_tile_size = 0;
12286 l_begin_data = p_data;
12288 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12292 l_nb_bytes_written += l_current_nb_bytes_written;
12293 p_data += l_current_nb_bytes_written;
12294 p_total_data_size -= l_current_nb_bytes_written;
12295 l_part_tile_size += l_current_nb_bytes_written;
12297 l_current_nb_bytes_written = 0;
12299 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12303 l_nb_bytes_written += l_current_nb_bytes_written;
12304 p_data += l_current_nb_bytes_written;
12305 p_total_data_size -= l_current_nb_bytes_written;
12306 l_part_tile_size += l_current_nb_bytes_written;
12308 /* Writing Psot in SOT marker */
12309 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12311 if (l_cp->m_specific_param.m_enc.m_cinema) {
12312 j2k_update_tlm(p_j2k,l_part_tile_size);
12315 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12319 *p_data_written = l_nb_bytes_written;
12325 * Writes the updated tlm.
12327 * @param p_stream the stream to write data to.
12328 * @param p_j2k J2K codec.
12329 * @param p_manager the user event manager.
12331 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
12332 struct opj_stream_private *p_stream,
12333 struct opj_event_mgr * p_manager )
12335 OPJ_UINT32 l_tlm_size;
12336 OPJ_SIZE_T l_tlm_position, l_current_position;
12338 /* preconditions */
12339 assert(p_j2k != 00);
12340 assert(p_manager != 00);
12341 assert(p_stream != 00);
12343 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12344 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12345 l_current_position = opj_stream_tell(p_stream);
12347 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12351 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) {
12355 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12364 * Ends the encoding, i.e. frees memory.
12366 * @param p_stream the stream to write data to.
12367 * @param p_j2k J2K codec.
12368 * @param p_manager the user event manager.
12370 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
12371 struct opj_stream_private *p_stream,
12372 struct opj_event_mgr * p_manager )
12374 /* preconditions */
12375 assert(p_j2k != 00);
12376 assert(p_manager != 00);
12377 assert(p_stream != 00);
12379 tcd_destroy_v2(p_j2k->m_tcd);
12382 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12383 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12384 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12385 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12388 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12389 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12390 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12393 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12399 * Destroys the memory associated with the decoding of headers.
12401 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12402 opj_stream_private_t *p_stream,
12403 opj_event_mgr_t * p_manager )
12405 /* preconditions */
12406 assert(p_j2k != 00);
12407 assert(p_stream != 00);
12408 assert(p_manager != 00);
12410 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12411 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12412 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12415 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12424 * @param p_stream the stream to write data to.
12425 * @param p_j2k J2K codec.
12426 * @param p_manager the user event manager.
12428 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12429 struct opj_stream_private *p_stream,
12430 struct opj_event_mgr * p_manager )
12432 opj_codestream_info_t * l_cstr_info = 00;
12434 /* preconditions */
12435 assert(p_j2k != 00);
12436 assert(p_manager != 00);
12437 assert(p_stream != 00);
12440 /* TODO mergeV2: check this part which use cstr_info */
12441 /*l_cstr_info = p_j2k->cstr_info;
12445 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));
12447 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12448 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12450 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12452 l_cstr_info->tw = p_j2k->m_cp.tw;
12453 l_cstr_info->th = p_j2k->m_cp.th;
12455 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12456 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12457 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12458 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12460 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12462 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12464 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12466 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12467 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12470 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12472 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12474 /*l_cstr_info->maxmarknum = 100;
12475 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12476 l_cstr_info->marknum = 0;
12479 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);
12483 * Creates a tile-coder decoder.
12485 * @param p_stream the stream to write data to.
12486 * @param p_j2k J2K codec.
12487 * @param p_manager the user event manager.
12489 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12490 struct opj_stream_private *p_stream,
12491 struct opj_event_mgr * p_manager )
12493 /* preconditions */
12494 assert(p_j2k != 00);
12495 assert(p_manager != 00);
12496 assert(p_stream != 00);
12498 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12500 if (! p_j2k->m_tcd) {
12501 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12505 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12506 tcd_destroy_v2(p_j2k->m_tcd);
12517 * @param p_j2k the jpeg2000 codec.
12518 * @param p_stream the stream to write data to.
12519 * @param p_manager the user event manager.
12521 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12522 OPJ_UINT32 p_tile_index,
12524 OPJ_UINT32 p_data_size,
12525 opj_stream_private_t *p_stream,
12526 opj_event_mgr_t * p_manager )
12528 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12532 return j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager);