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);
472 * Writes the COM marker (comment)
474 * @param p_stream the stream to write data to.
475 * @param p_j2k J2K codec.
476 * @param p_manager the user event manager.
478 static opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
479 struct opj_stream_private *p_stream,
480 struct opj_event_mgr * p_manager );
483 Read the COM marker (comment)
484 @param j2k J2K handle
486 static void j2k_read_com(opj_j2k_t *j2k);
488 * Reads a COM marker (comments)
489 * @param p_header_data the data contained in the COM box.
490 * @param jp2 the jpeg2000 file codec.
491 * @param p_header_size the size of the data contained in the COM marker.
492 * @param p_manager the user event manager.
494 static opj_bool j2k_read_com_v2 (
496 OPJ_BYTE * p_header_data,
497 OPJ_UINT32 p_header_size,
498 struct opj_event_mgr * p_manager
501 Write the value concerning the specified component in the marker COD and COC
502 @param j2k J2K handle
503 @param compno Number of the component concerned by the information written
505 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
507 Read the value concerning the specified component in the marker COD and COC
508 @param j2k J2K handle
509 @param compno Number of the component concerned by the information read
511 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
513 Write the COD marker (coding style default)
514 @param j2k J2K handle
516 static void j2k_write_cod(opj_j2k_t *j2k);
519 * Writes the COD marker (Coding style default)
521 * @param p_stream the stream to write data to.
522 * @param p_j2k J2K codec.
523 * @param p_manager the user event manager.
525 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
526 struct opj_stream_private *p_stream,
527 struct opj_event_mgr * p_manager );
530 Read the COD marker (coding style default)
531 @param j2k J2K handle
533 static void j2k_read_cod(opj_j2k_t *j2k);
536 * Reads a COD marker (Coding Styke defaults)
537 * @param p_header_data the data contained in the COD box.
538 * @param p_j2k the jpeg2000 codec.
539 * @param p_header_size the size of the data contained in the COD marker.
540 * @param p_manager the user event manager.
542 static opj_bool j2k_read_cod_v2 (
544 OPJ_BYTE * p_header_data,
545 OPJ_UINT32 p_header_size,
546 struct opj_event_mgr * p_manager
550 Write the COC marker (coding style component)
551 @param j2k J2K handle
552 @param compno Number of the component concerned by the information written
554 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
557 * Writes the COC marker (Coding style component)
559 * @param p_comp_number the index of the component to output.
560 * @param p_stream the stream to write data to.
561 * @param p_j2k J2K codec.
562 * @param p_manager the user event manager.
564 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
565 OPJ_UINT32 p_comp_number,
566 struct opj_stream_private *p_stream,
567 struct opj_event_mgr * p_manager
570 * Writes the COC marker (Coding style component)
572 * @param p_comp_no the index of the component to output.
573 * @param p_stream the stream to write data to.
574 * @param p_j2k J2K codec.
575 * @param p_manager the user event manager.
577 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
578 OPJ_UINT32 p_comp_no,
580 OPJ_UINT32 * p_data_written,
581 struct opj_event_mgr * p_manager );
584 * Gets the maximum size taken by a coc.
586 * @param p_j2k the jpeg2000 codec to use.
588 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
591 Read the COC marker (coding style component)
592 @param j2k J2K handle
594 static void j2k_read_coc(opj_j2k_t *j2k);
597 * Reads a COC marker (Coding Style Component)
598 * @param p_header_data the data contained in the COC box.
599 * @param p_j2k the jpeg2000 codec.
600 * @param p_header_size the size of the data contained in the COC marker.
601 * @param p_manager the user event manager.
603 static opj_bool j2k_read_coc_v2 (
605 OPJ_BYTE * p_header_data,
606 OPJ_UINT32 p_header_size,
607 struct opj_event_mgr * p_manager
611 Write the value concerning the specified component in the marker QCD and QCC
612 @param j2k J2K handle
613 @param compno Number of the component concerned by the information written
615 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
617 Read the value concerning the specified component in the marker QCD and QCC
618 @param j2k J2K handle
619 @param compno Number of the component concern by the information read
620 @param len Length of the information in the QCX part of the marker QCD/QCC
622 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
624 Write the QCD marker (quantization default)
625 @param j2k J2K handle
627 static void j2k_write_qcd(opj_j2k_t *j2k);
630 * Writes the QCD marker (quantization default)
632 * @param p_comp_number the index of the component to output.
633 * @param p_stream the stream to write data to.
634 * @param p_j2k J2K codec.
635 * @param p_manager the user event manager.
637 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
638 struct opj_stream_private *p_stream,
639 struct opj_event_mgr * p_manager );
642 Read the QCD marker (quantization default)
643 @param j2k J2K handle
645 static void j2k_read_qcd(opj_j2k_t *j2k);
648 * Reads a QCD marker (Quantization defaults)
649 * @param p_header_data the data contained in the QCD box.
650 * @param p_j2k the jpeg2000 codec.
651 * @param p_header_size the size of the data contained in the QCD marker.
652 * @param p_manager the user event manager.
654 static opj_bool j2k_read_qcd_v2 (
656 OPJ_BYTE * p_header_data,
657 OPJ_UINT32 p_header_size,
658 struct opj_event_mgr * p_manager
662 Write the QCC marker (quantization component)
663 @param j2k J2K handle
664 @param compno Number of the component concerned by the information written
666 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
669 * Writes the QCC marker (quantization component)
671 * @param p_comp_no the index of the component to output.
672 * @param p_stream the stream to write data to.
673 * @param p_j2k J2K codec.
674 * @param p_manager the user event manager.
676 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
677 OPJ_UINT32 p_comp_no,
678 struct opj_stream_private *p_stream,
679 struct opj_event_mgr * p_manager );
682 * Writes the QCC marker (quantization component)
684 * @param p_comp_no the index of the component to output.
685 * @param p_stream the stream to write data to.
686 * @param p_j2k J2K codec.
687 * @param p_manager the user event manager.
689 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
690 OPJ_UINT32 p_comp_no,
692 OPJ_UINT32 * p_data_written,
693 struct opj_event_mgr * p_manager );
696 * Gets the maximum size taken by a qcc.
698 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
701 Read the QCC marker (quantization component)
702 @param j2k J2K handle
704 static void j2k_read_qcc(opj_j2k_t *j2k);
706 * Reads a QCC marker (Quantization component)
707 * @param p_header_data the data contained in the QCC box.
708 * @param p_j2k the jpeg2000 codec.
709 * @param p_header_size the size of the data contained in the QCC marker.
710 * @param p_manager the user event manager.
712 static opj_bool j2k_read_qcc_v2(
714 OPJ_BYTE * p_header_data,
715 OPJ_UINT32 p_header_size,
716 struct opj_event_mgr * p_manager);
719 Write the POC marker (progression order change)
720 @param j2k J2K handle
722 static void j2k_write_poc(opj_j2k_t *j2k);
725 * Writes the POC marker (Progression Order Change)
727 * @param p_stream the stream to write data to.
728 * @param p_j2k J2K codec.
729 * @param p_manager the user event manager.
731 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
732 struct opj_stream_private *p_stream,
733 struct opj_event_mgr * p_manager );
736 * Writes the POC marker (Progression Order Change)
738 * @param p_stream the stream to write data to.
739 * @param p_j2k J2K codec.
740 * @param p_manager the user event manager.
742 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
744 OPJ_UINT32 * p_data_written,
745 struct opj_event_mgr * p_manager );
748 * Gets the maximum size taken by the writting of a POC.
750 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
753 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
755 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
758 * Gets the maximum size taken by the headers of the SOT.
760 * @param p_j2k the jpeg2000 codec to use.
762 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
765 Read the POC marker (progression order change)
766 @param j2k J2K handle
768 static void j2k_read_poc(opj_j2k_t *j2k);
770 * Reads a POC marker (Progression Order Change)
772 * @param p_header_data the data contained in the POC box.
773 * @param p_j2k the jpeg2000 codec.
774 * @param p_header_size the size of the data contained in the POC marker.
775 * @param p_manager the user event manager.
777 static opj_bool j2k_read_poc_v2 (
779 OPJ_BYTE * p_header_data,
780 OPJ_UINT32 p_header_size,
781 struct opj_event_mgr * p_manager
785 Read the CRG marker (component registration)
786 @param j2k J2K handle
788 static void j2k_read_crg(opj_j2k_t *j2k);
790 * Reads a CRG marker (Component registration)
792 * @param p_header_data the data contained in the TLM box.
793 * @param p_j2k the jpeg2000 codec.
794 * @param p_header_size the size of the data contained in the TLM marker.
795 * @param p_manager the user event manager.
797 static opj_bool j2k_read_crg_v2 (
799 OPJ_BYTE * p_header_data,
800 OPJ_UINT32 p_header_size,
801 struct opj_event_mgr * p_manager
804 Read the TLM marker (tile-part lengths)
805 @param j2k J2K handle
807 static void j2k_read_tlm(opj_j2k_t *j2k);
809 * Reads a TLM marker (Tile Length Marker)
811 * @param p_header_data the data contained in the TLM box.
812 * @param p_j2k the jpeg2000 codec.
813 * @param p_header_size the size of the data contained in the TLM marker.
814 * @param p_manager the user event manager.
816 static opj_bool j2k_read_tlm_v2 (
818 OPJ_BYTE * p_header_data,
819 OPJ_UINT32 p_header_size,
820 struct opj_event_mgr * p_manager
824 * Writes the updated tlm.
826 * @param p_stream the stream to write data to.
827 * @param p_j2k J2K codec.
828 * @param p_manager the user event manager.
830 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
831 struct opj_stream_private *p_stream,
832 struct opj_event_mgr * p_manager );
835 Read the PLM marker (packet length, main header)
836 @param j2k J2K handle
838 static void j2k_read_plm(opj_j2k_t *j2k);
841 * Reads a PLM marker (Packet length, main header marker)
843 * @param p_header_data the data contained in the TLM box.
844 * @param p_j2k the jpeg2000 codec.
845 * @param p_header_size the size of the data contained in the TLM marker.
846 * @param p_manager the user event manager.
848 static opj_bool j2k_read_plm_v2 (
850 OPJ_BYTE * p_header_data,
851 OPJ_UINT32 p_header_size,
852 struct opj_event_mgr * p_manager
855 Read the PLT marker (packet length, tile-part header)
856 @param j2k J2K handle
858 static void j2k_read_plt(opj_j2k_t *j2k);
860 * Reads a PLT marker (Packet length, tile-part header)
862 * @param p_header_data the data contained in the PLT box.
863 * @param p_j2k the jpeg2000 codec.
864 * @param p_header_size the size of the data contained in the PLT marker.
865 * @param p_manager the user event manager.
867 static opj_bool j2k_read_plt_v2 (
869 OPJ_BYTE * p_header_data,
870 OPJ_UINT32 p_header_size,
871 struct opj_event_mgr * p_manager
874 Read the PPM marker (packet packet headers, main header)
875 @param j2k J2K handle
877 static void j2k_read_ppm(opj_j2k_t *j2k);
879 * Reads a PPM marker (Packed packet headers, main header)
881 * @param p_header_data the data contained in the POC box.
882 * @param p_j2k the jpeg2000 codec.
883 * @param p_header_size the size of the data contained in the POC marker.
884 * @param p_manager the user event manager.
887 static opj_bool j2k_read_ppm_v2 (
889 OPJ_BYTE * p_header_data,
890 OPJ_UINT32 p_header_size,
891 struct opj_event_mgr * p_manager
895 static opj_bool j2k_read_ppm_v3 (
897 OPJ_BYTE * p_header_data,
898 OPJ_UINT32 p_header_size,
899 struct opj_event_mgr * p_manager
903 Read the PPT marker (packet packet headers, tile-part header)
904 @param j2k J2K handle
906 static void j2k_read_ppt(opj_j2k_t *j2k);
908 * Reads a PPT marker (Packed packet headers, tile-part header)
910 * @param p_header_data the data contained in the PPT box.
911 * @param p_j2k the jpeg2000 codec.
912 * @param p_header_size the size of the data contained in the PPT marker.
913 * @param p_manager the user event manager.
915 static opj_bool j2k_read_ppt_v2 (
917 OPJ_BYTE * p_header_data,
918 OPJ_UINT32 p_header_size,
919 struct opj_event_mgr * p_manager
922 Write the TLM marker (Mainheader)
923 @param j2k J2K handle
925 static void j2k_write_tlm(opj_j2k_t *j2k);
928 * Writes the TLM marker (Tile Length Marker)
930 * @param p_stream the stream to write data to.
931 * @param p_j2k J2K codec.
932 * @param p_manager the user event manager.
934 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
935 struct opj_stream_private *p_stream,
936 struct opj_event_mgr * p_manager );
939 Write the SOT marker (start of tile-part)
940 @param j2k J2K handle
942 static void j2k_write_sot(opj_j2k_t *j2k);
944 Read the SOT marker (start of tile-part)
945 @param j2k J2K handle
947 static void j2k_read_sot(opj_j2k_t *j2k);
950 * Writes the SOT marker (Start of tile-part)
952 * @param p_stream the stream to write data to.
953 * @param p_j2k J2K codec.
954 * @param p_manager the user event manager.
956 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
958 OPJ_UINT32 * p_data_written,
959 const struct opj_stream_private *p_stream,
960 struct opj_event_mgr * p_manager );
963 * Reads a PPT marker (Packed packet headers, tile-part header)
965 * @param p_header_data the data contained in the PPT box.
966 * @param p_j2k the jpeg2000 codec.
967 * @param p_header_size the size of the data contained in the PPT marker.
968 * @param p_manager the user event manager.
970 static opj_bool j2k_read_sot_v2 (
972 OPJ_BYTE * p_header_data,
973 OPJ_UINT32 p_header_size,
974 struct opj_event_mgr * p_manager
977 Write the SOD marker (start of data)
978 @param j2k J2K handle
979 @param tile_coder Pointer to a TCD handle
981 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
984 * Writes the SOD marker (Start of data)
986 * @param p_stream the stream to write data to.
987 * @param p_j2k J2K codec.
988 * @param p_manager the user event manager.
990 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
991 struct opj_tcd_v2 * p_tile_coder,
993 OPJ_UINT32 * p_data_written,
994 OPJ_UINT32 p_total_data_size,
995 const struct opj_stream_private *p_stream,
996 struct opj_event_mgr * p_manager );
999 Read the SOD marker (start of data)
1000 @param j2k J2K handle
1002 static void j2k_read_sod(opj_j2k_t *j2k);
1005 * Reads a SOD marker (Start Of Data)
1007 * @param p_header_data the data contained in the SOD box.
1008 * @param p_j2k the jpeg2000 codec.
1009 * @param p_header_size the size of the data contained in the SOD marker.
1010 * @param p_manager the user event manager.
1012 static opj_bool j2k_read_sod_v2 (
1013 opj_j2k_v2_t *p_j2k,
1014 struct opj_stream_private *p_stream,
1015 struct opj_event_mgr * p_manager
1019 * Updates the Tile Length Marker.
1021 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
1023 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
1024 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
1026 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
1027 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
1031 Write the RGN marker (region-of-interest)
1032 @param j2k J2K handle
1033 @param compno Number of the component concerned by the information written
1034 @param tileno Number of the tile concerned by the information written
1036 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
1039 * Writes the RGN marker (Region Of Interest)
1041 * @param p_tile_no the tile to output
1042 * @param p_comp_no the component to output
1043 * @param p_stream the stream to write data to.
1044 * @param p_j2k J2K codec.
1045 * @param p_manager the user event manager.
1047 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
1048 OPJ_UINT32 p_tile_no,
1049 OPJ_UINT32 p_comp_no,
1050 struct opj_stream_private *p_stream,
1051 struct opj_event_mgr * p_manager );
1054 Read the RGN marker (region-of-interest)
1055 @param j2k J2K handle
1057 static void j2k_read_rgn(opj_j2k_t *j2k);
1060 * Reads a RGN marker (Region Of Interest)
1062 * @param p_header_data the data contained in the POC box.
1063 * @param p_j2k the jpeg2000 codec.
1064 * @param p_header_size the size of the data contained in the POC marker.
1065 * @param p_manager the user event manager.
1067 static opj_bool j2k_read_rgn_v2 (
1068 opj_j2k_v2_t *p_j2k,
1069 OPJ_BYTE * p_header_data,
1070 OPJ_UINT32 p_header_size,
1071 struct opj_event_mgr * p_manager
1075 * Writes the EOC marker (End of Codestream)
1077 * @param p_stream the stream to write data to.
1078 * @param p_j2k J2K codec.
1079 * @param p_manager the user event manager.
1081 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
1082 struct opj_stream_private *p_stream,
1083 struct opj_event_mgr * p_manager );
1086 Write the EOC marker (end of codestream)
1087 @param j2k J2K handle
1089 static void j2k_write_eoc(opj_j2k_t *j2k);
1091 Read the EOC marker (end of codestream)
1092 @param j2k J2K handle
1094 static void j2k_read_eoc(opj_j2k_t *j2k);
1097 * Reads a EOC marker (End Of Codestream)
1099 * @param p_header_data the data contained in the SOD box.
1100 * @param p_j2k the jpeg2000 codec.
1101 * @param p_header_size the size of the data contained in the SOD marker.
1102 * @param p_manager the user event manager.
1105 static opj_bool j2k_read_eoc_v2 (
1106 opj_j2k_v2_t *p_j2k,
1107 struct opj_stream_private *p_stream,
1108 struct opj_event_mgr * p_manager
1115 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1117 * @param p_stream the stream to write data to.
1118 * @param p_j2k J2K codec.
1119 * @param p_manager the user event manager.
1121 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1122 struct opj_stream_private *p_stream,
1123 struct opj_event_mgr * p_manager );
1128 * @param p_stream the stream to write data to.
1129 * @param p_j2k J2K codec.
1130 * @param p_manager the user event manager.
1132 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1133 struct opj_stream_private *p_stream,
1134 struct opj_event_mgr * p_manager );
1137 Read an unknown marker
1138 @param j2k J2K handle
1140 static void j2k_read_unk(opj_j2k_t *j2k);
1142 Add main header marker information
1143 @param cstr_info Codestream information structure
1144 @param type marker type
1145 @param pos byte offset of marker segment
1146 @param len length of marker segment
1148 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1150 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1152 Add tile header marker information
1153 @param tileno tile index number
1154 @param cstr_info Codestream information structure
1155 @param type marker type
1156 @param pos byte offset of marker segment
1157 @param len length of marker segment
1159 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1161 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);
1164 * Reads an unknown marker
1166 * @param p_stream the stream object to read from.
1167 * @param p_j2k the jpeg2000 codec.
1168 * @param p_manager the user event manager.
1170 * @return true if the marker could be deduced.
1172 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1173 struct opj_stream_private *p_stream,
1174 OPJ_UINT32 *output_marker,
1175 struct opj_event_mgr * p_manager );
1178 * Writes the MCT marker (Multiple Component Transform)
1180 * @param p_stream the stream to write data to.
1181 * @param p_j2k J2K codec.
1182 * @param p_manager the user event manager.
1184 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1185 opj_mct_data_t * p_mct_record,
1186 struct opj_stream_private *p_stream,
1187 struct opj_event_mgr * p_manager );
1190 * Reads a MCT marker (Multiple Component Transform)
1192 * @param p_header_data the data contained in the MCT box.
1193 * @param p_j2k the jpeg2000 codec.
1194 * @param p_header_size the size of the data contained in the MCT marker.
1195 * @param p_manager the user event manager.
1197 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1198 OPJ_BYTE * p_header_data,
1199 OPJ_UINT32 p_header_size,
1200 struct opj_event_mgr * p_manager );
1203 * Writes the MCC marker (Multiple Component Collection)
1205 * @param p_stream the stream to write data to.
1206 * @param p_j2k J2K codec.
1207 * @param p_manager the user event manager.
1209 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1210 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1211 struct opj_stream_private *p_stream,
1212 struct opj_event_mgr * p_manager );
1216 * Reads a MCC marker (Multiple Component Collection)
1218 * @param p_header_data the data contained in the MCC box.
1219 * @param p_j2k the jpeg2000 codec.
1220 * @param p_header_size the size of the data contained in the MCC marker.
1221 * @param p_manager the user event manager.
1223 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1224 OPJ_BYTE * p_header_data,
1225 OPJ_UINT32 p_header_size,
1226 struct opj_event_mgr * p_manager );
1229 * Writes the MCO marker (Multiple component transformation ordering)
1231 * @param p_stream the stream to write data to.
1232 * @param p_j2k J2K codec.
1233 * @param p_manager the user event manager.
1235 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1236 struct opj_stream_private *p_stream,
1237 struct opj_event_mgr * p_manager );
1240 * Reads a MCO marker (Multiple Component Transform Ordering)
1242 * @param p_header_data the data contained in the MCO box.
1243 * @param p_j2k the jpeg2000 codec.
1244 * @param p_header_size the size of the data contained in the MCO marker.
1245 * @param p_manager the user event manager.
1247 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1248 OPJ_BYTE * p_header_data,
1249 OPJ_UINT32 p_header_size,
1250 struct opj_event_mgr * p_manager );
1252 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1254 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1255 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1256 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1257 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1259 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1260 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1261 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1262 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1264 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1265 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1266 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1267 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1271 * Ends the encoding, i.e. frees memory.
1273 * @param p_stream the stream to write data to.
1274 * @param p_j2k J2K codec.
1275 * @param p_manager the user event manager.
1277 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1278 struct opj_stream_private *p_stream,
1279 struct opj_event_mgr * p_manager );
1282 * Writes the CBD marker (Component bit depth definition)
1284 * @param p_stream the stream to write data to.
1285 * @param p_j2k J2K codec.
1286 * @param p_manager the user event manager.
1288 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1289 struct opj_stream_private *p_stream,
1290 struct opj_event_mgr * p_manager );
1293 * Reads a CBD marker (Component bit depth definition)
1294 * @param p_header_data the data contained in the CBD box.
1295 * @param p_j2k the jpeg2000 codec.
1296 * @param p_header_size the size of the data contained in the CBD marker.
1297 * @param p_manager the user event manager.
1299 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1300 OPJ_BYTE * p_header_data,
1301 OPJ_UINT32 p_header_size,
1302 struct opj_event_mgr * p_manager);
1305 * Writes the image components.
1307 * @param p_stream the stream to write data to.
1308 * @param p_j2k J2K codec.
1309 * @param p_manager the user event manager.
1311 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1312 struct opj_stream_private *p_stream,
1313 struct opj_event_mgr * p_manager );
1316 * Writes regions of interests.
1318 * @param p_stream the stream to write data to.
1319 * @param p_j2k J2K codec.
1320 * @param p_manager the user event manager.
1322 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1323 struct opj_stream_private *p_stream,
1324 struct opj_event_mgr * p_manager );
1329 * @param p_stream the stream to write data to.
1330 * @param p_j2k J2K codec.
1331 * @param p_manager the user event manager.
1333 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1334 struct opj_stream_private *p_stream,
1335 struct opj_event_mgr * p_manager );
1338 * Checks the progression order changes values. Tells of the poc given as input are valid.
1339 * A nice message is outputted at errors.
1341 * @param p_pocs the progression order changes.
1342 * @param p_nb_pocs the number of progression order changes.
1343 * @param p_nb_resolutions the number of resolutions.
1344 * @param numcomps the number of components
1345 * @param numlayers the number of layers.
1347 * @return true if the pocs are valid.
1349 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1350 OPJ_UINT32 p_nb_pocs,
1351 OPJ_UINT32 p_nb_resolutions,
1352 OPJ_UINT32 numcomps,
1353 OPJ_UINT32 numlayers,
1354 opj_event_mgr_t * p_manager);
1357 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1359 * @param cp the coding parameters.
1360 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1361 * @param tileno the given tile.
1363 * @return the number of tile parts.
1365 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1367 /** mem allocation for TLM marker*/
1368 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1371 * Calculates the total number of tile parts needed by the encoder to
1372 * encode such an image. If not enough memory is available, then the function return false.
1374 * @param p_nb_tiles pointer that will hold the number of tile parts.
1375 * @param cp the coding parameters for the image.
1376 * @param image the image to encode.
1377 * @param p_j2k the p_j2k encoder.
1378 * @param p_manager the user event manager.
1380 * @return true if the function was successful, false else.
1382 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1384 OPJ_UINT32 * p_nb_tiles,
1386 opj_event_mgr_t * p_manager);
1388 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1390 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1392 static opj_codestream_index_t* j2k_create_cstr_index(void);
1398 /* ----------------------------------------------------------------------- */
1399 typedef struct j2k_prog_order{
1400 OPJ_PROG_ORDER enum_prog;
1404 j2k_prog_order_t j2k_prog_order_list[] = {
1410 {(OPJ_PROG_ORDER)-1, ""}
1418 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1426 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1428 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1430 j2k_read_int16_to_float,
1431 j2k_read_int32_to_float,
1432 j2k_read_float32_to_float,
1433 j2k_read_float64_to_float
1436 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1438 j2k_read_int16_to_int32,
1439 j2k_read_int32_to_int32,
1440 j2k_read_float32_to_int32,
1441 j2k_read_float64_to_int32
1444 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1446 j2k_write_float_to_int16,
1447 j2k_write_float_to_int32,
1448 j2k_write_float_to_float,
1449 j2k_write_float_to_float64
1452 typedef struct opj_dec_memory_marker_handler
1456 /** value of the state when the marker can appear */
1458 /** action linked to the marker */
1459 opj_bool (*handler) (
1460 opj_j2k_v2_t *p_j2k,
1461 OPJ_BYTE * p_header_data,
1462 OPJ_UINT32 p_header_size,
1463 struct opj_event_mgr * p_manager
1466 opj_dec_memory_marker_handler_t;
1468 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1471 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1472 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1473 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1474 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1475 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1476 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1477 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1478 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1479 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1480 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1481 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1482 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1483 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1485 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1486 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1487 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1488 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1489 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1490 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1492 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
1493 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1494 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1495 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1496 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1497 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1498 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1499 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
1500 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1501 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1502 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1503 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1504 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1506 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1507 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1508 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1509 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1510 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1511 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1513 #ifdef TODO_MS /* FIXME */
1514 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1515 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1516 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1517 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1519 #endif /* USE_JPWL */
1521 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1522 {J2K_MS_INSEC, 0, j2k_read_insec}
1523 #endif /* USE_JPSEC */
1524 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1529 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1531 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1532 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1536 for (i=0;i<p_nb_elem;++i) {
1537 opj_read_bytes(l_src_data,&l_temp,2);
1539 l_src_data+=sizeof(OPJ_INT16);
1541 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1545 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1547 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1548 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1552 for (i=0;i<p_nb_elem;++i) {
1553 opj_read_bytes(l_src_data,&l_temp,4);
1555 l_src_data+=sizeof(OPJ_INT32);
1557 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1561 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1563 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1564 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1568 for (i=0;i<p_nb_elem;++i) {
1569 opj_read_float(l_src_data,&l_temp);
1571 l_src_data+=sizeof(OPJ_FLOAT32);
1573 *(l_dest_data++) = l_temp;
1577 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1579 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1580 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1584 for (i=0;i<p_nb_elem;++i) {
1585 opj_read_double(l_src_data,&l_temp);
1587 l_src_data+=sizeof(OPJ_FLOAT64);
1589 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1593 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1595 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1596 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1600 for (i=0;i<p_nb_elem;++i) {
1601 opj_read_bytes(l_src_data,&l_temp,2);
1603 l_src_data+=sizeof(OPJ_INT16);
1605 *(l_dest_data++) = (OPJ_INT32) l_temp;
1609 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1611 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1612 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1616 for (i=0;i<p_nb_elem;++i) {
1617 opj_read_bytes(l_src_data,&l_temp,4);
1619 l_src_data+=sizeof(OPJ_INT32);
1621 *(l_dest_data++) = (OPJ_INT32) l_temp;
1625 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1627 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1628 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1632 for (i=0;i<p_nb_elem;++i) {
1633 opj_read_float(l_src_data,&l_temp);
1635 l_src_data+=sizeof(OPJ_FLOAT32);
1637 *(l_dest_data++) = (OPJ_INT32) l_temp;
1641 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1643 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1644 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1648 for (i=0;i<p_nb_elem;++i) {
1649 opj_read_double(l_src_data,&l_temp);
1651 l_src_data+=sizeof(OPJ_FLOAT64);
1653 *(l_dest_data++) = (OPJ_INT32) l_temp;
1657 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1659 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1660 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1664 for (i=0;i<p_nb_elem;++i) {
1665 l_temp = (OPJ_UINT32) *(l_src_data++);
1667 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1669 l_dest_data+=sizeof(OPJ_INT16);
1673 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1675 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1676 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1680 for (i=0;i<p_nb_elem;++i) {
1681 l_temp = (OPJ_UINT32) *(l_src_data++);
1683 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1685 l_dest_data+=sizeof(OPJ_INT32);
1689 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1691 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1692 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1696 for (i=0;i<p_nb_elem;++i) {
1697 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1699 opj_write_float(l_dest_data,l_temp);
1701 l_dest_data+=sizeof(OPJ_FLOAT32);
1705 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1707 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1708 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1712 for (i=0;i<p_nb_elem;++i) {
1713 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1715 opj_write_double(l_dest_data,l_temp);
1717 l_dest_data+=sizeof(OPJ_FLOAT64);
1723 * Converts an enum type progression order to string type.
1725 * @param prg_order the progression order to get.
1727 * @return the string representation of the given progression order.
1729 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1730 j2k_prog_order_t *po;
1731 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1732 if(po->enum_prog == prg_order){
1733 return po->str_prog;
1736 return po->str_prog;
1740 * Checks the progression order changes values. Tells if the poc given as input are valid.
1742 * @param p_pocs the progression order changes.
1743 * @param p_nb_pocs the number of progression order changes.
1744 * @param p_nb_resolutions the number of resolutions.
1745 * @param numcomps the number of components
1746 * @param numlayers the number of layers.
1747 * @param p_manager the user event manager.
1749 * @return true if the pocs are valid.
1751 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1752 OPJ_UINT32 p_nb_pocs,
1753 OPJ_UINT32 p_nb_resolutions,
1754 OPJ_UINT32 p_num_comps,
1755 OPJ_UINT32 p_num_layers,
1756 opj_event_mgr_t * p_manager)
1758 OPJ_UINT32* packet_array;
1759 OPJ_UINT32 index , resno, compno, layno;
1761 OPJ_UINT32 step_c = 1;
1762 OPJ_UINT32 step_r = p_num_comps * step_c;
1763 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1764 opj_bool loss = OPJ_FALSE;
1765 OPJ_UINT32 layno0 = 0;
1767 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1768 if (packet_array == 00) {
1769 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1772 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1774 if (p_nb_pocs == 0) {
1778 index = step_r * p_pocs->resno0;
1779 // take each resolution for each poc
1780 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1782 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1784 // take each comp of each resolution for each poc
1785 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1786 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1788 // and finally take each layer of each res of ...
1789 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1790 //index = step_r * resno + step_c * compno + step_l * layno;
1791 packet_array[comp_index] = 1;
1792 comp_index += step_l;
1795 res_index += step_c;
1802 // iterate through all the pocs
1803 for (i = 1; i < p_nb_pocs ; ++i) {
1804 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1806 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1807 index = step_r * p_pocs->resno0;
1809 // take each resolution for each poc
1810 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1811 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1813 // take each comp of each resolution for each poc
1814 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1815 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1817 // and finally take each layer of each res of ...
1818 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1819 //index = step_r * resno + step_c * compno + step_l * layno;
1820 packet_array[comp_index] = 1;
1821 comp_index += step_l;
1824 res_index += step_c;
1834 for (layno = 0; layno < p_num_layers ; ++layno) {
1835 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1836 for (compno = 0; compno < p_num_comps; ++compno) {
1837 loss |= (packet_array[index]!=1);
1838 //index = step_r * resno + step_c * compno + step_l * layno;
1845 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1848 opj_free(packet_array);
1853 /* ----------------------------------------------------------------------- */
1854 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1857 int tpnum=1,tpend=0;
1858 opj_tcp_t *tcp = &cp->tcps[tileno];
1859 prog = j2k_convert_progression_order(tcp->prg);
1864 if( cp->tp_flag == prog[i] ){
1865 tpend=1;cp->tp_pos=i;
1869 tpnum= tpnum * tcp->pocs[pino].compE;
1872 tpnum= tpnum * tcp->pocs[pino].resE;
1875 tpnum= tpnum * tcp->pocs[pino].prcE;
1878 tpnum= tpnum * tcp->pocs[pino].layE;
1890 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1892 * @param cp the coding parameters.
1893 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1894 * @param tileno the given tile.
1896 * @return the number of tile parts.
1898 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1900 const OPJ_CHAR *prog = 00;
1902 OPJ_UINT32 tpnum = 1;
1903 opj_tcp_v2_t *tcp = 00;
1904 opj_poc_t * l_current_poc = 00;
1907 assert(tileno < (cp->tw * cp->th));
1908 assert(pino < (cp->tcps[tileno].numpocs + 1));
1910 /* get the given tile coding parameter */
1911 tcp = &cp->tcps[tileno];
1914 l_current_poc = &(tcp->pocs[pino]);
1915 assert(l_current_poc != 0);
1917 /* get the progression order as a character string */
1918 prog = j2k_convert_progression_order(tcp->prg);
1919 assert(strlen(prog) > 0);
1921 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1925 /* component wise */
1927 tpnum *= l_current_poc->compE;
1929 /* resolution wise */
1931 tpnum *= l_current_poc->resE;
1935 tpnum *= l_current_poc->prcE;
1939 tpnum *= l_current_poc->layE;
1942 /* whould we split here ? */
1943 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1944 cp->m_specific_param.m_enc.m_tp_pos=i;
1956 /** mem allocation for TLM marker*/
1957 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1958 int pino,tileno,totnum_tp=0;
1960 OPJ_ARG_NOT_USED(img_numcomp);
1962 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1963 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1964 int cur_totnum_tp = 0;
1965 opj_tcp_t *tcp = &cp->tcps[tileno];
1966 for(pino = 0; pino <= tcp->numpocs; pino++) {
1968 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1969 if(!pi) { return -1;}
1970 tp_num = j2k_get_num_tp(cp,pino,tileno);
1971 totnum_tp = totnum_tp + tp_num;
1972 cur_totnum_tp = cur_totnum_tp + tp_num;
1973 pi_destroy(pi, cp, tileno);
1975 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1977 if (j2k->cstr_info) {
1978 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1979 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1987 * Calculates the total number of tile parts needed by the encoder to
1988 * encode such an image. If not enough memory is available, then the function return false.
1990 * @param p_nb_tiles pointer that will hold the number of tile parts.
1991 * @param cp the coding parameters for the image.
1992 * @param image the image to encode.
1993 * @param p_j2k the p_j2k encoder.
1994 * @param p_manager the user event manager.
1996 * @return true if the function was successful, false else.
1998 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
2000 OPJ_UINT32 * p_nb_tiles,
2002 opj_event_mgr_t * p_manager)
2004 OPJ_UINT32 pino,tileno;
2005 OPJ_UINT32 l_nb_tiles;
2009 assert(p_nb_tiles != 00);
2011 assert(image != 00);
2012 assert(p_j2k != 00);
2013 assert(p_manager != 00);
2015 l_nb_tiles = cp->tw * cp->th;
2020 /* TODO mergeV2: check this part which use cstr_info */
2021 /*if (p_j2k->cstr_info) {
2022 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
2024 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2025 OPJ_UINT32 cur_totnum_tp = 0;
2027 pi_update_encoding_parameters(image,cp,tileno);
2029 for (pino = 0; pino <= tcp->numpocs; ++pino)
2031 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2033 *p_nb_tiles = *p_nb_tiles + tp_num;
2035 cur_totnum_tp += tp_num;
2038 tcp->m_nb_tile_parts = cur_totnum_tp;
2040 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
2041 if (l_info_tile_ptr->tp == 00) {
2045 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
2047 l_info_tile_ptr->num_tps = cur_totnum_tp;
2054 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2055 OPJ_UINT32 cur_totnum_tp = 0;
2057 pi_update_encoding_parameters(image,cp,tileno);
2059 for (pino = 0; pino <= tcp->numpocs; ++pino) {
2060 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2062 *p_nb_tiles = *p_nb_tiles + tp_num;
2064 cur_totnum_tp += tp_num;
2066 tcp->m_nb_tile_parts = cur_totnum_tp;
2075 static void j2k_write_soc(opj_j2k_t *j2k) {
2076 opj_cio_t *cio = j2k->cio;
2077 cio_write(cio, J2K_MS_SOC, 2);
2080 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2085 /* update markers struct */
2086 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2087 #endif /* USE_JPWL */
2092 * Writes the SOC marker (Start Of Codestream)
2094 * @param p_stream the stream to write data to.
2095 * @param p_j2k J2K codec.
2096 * @param p_manager the user event manager.
2098 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2099 struct opj_stream_private *p_stream,
2100 struct opj_event_mgr * p_manager )
2102 /* 2 bytes will be written */
2103 OPJ_BYTE * l_start_stream = 00;
2106 assert(p_stream != 00);
2107 assert(p_j2k != 00);
2108 assert(p_manager != 00);
2110 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2112 /* write SOC identifier */
2113 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2115 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2121 /* update markers struct */
2123 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2125 assert( 0 && "TODO" );
2126 #endif /* USE_JPWL */
2132 static void j2k_read_soc(opj_j2k_t *j2k) {
2133 j2k->state = J2K_STATE_MHSIZ;
2135 if (j2k->cstr_info) {
2136 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
2137 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
2142 * Reads a SOC marker (Start of Codestream)
2143 * @param p_header_data the data contained in the SOC box.
2144 * @param jp2 the jpeg2000 file codec.
2145 * @param p_header_size the size of the data contained in the SOC marker.
2146 * @param p_manager the user event manager.
2148 static opj_bool j2k_read_soc_v2( opj_j2k_v2_t *p_j2k,
2149 struct opj_stream_private *p_stream,
2150 struct opj_event_mgr * p_manager )
2152 OPJ_BYTE l_data [2];
2153 OPJ_UINT32 l_marker;
2156 assert(p_j2k != 00);
2157 assert(p_manager != 00);
2158 assert(p_stream != 00);
2160 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2164 opj_read_bytes(l_data,&l_marker,2);
2165 if (l_marker != J2K_MS_SOC) {
2169 /* Next marker should be a SIZ marker in the main header */
2170 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2172 /* FIXME move it in a index structure included in p_j2k*/
2173 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2175 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2177 /* Add the marker to the codestream index*/
2178 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2183 static void j2k_write_siz(opj_j2k_t *j2k) {
2187 opj_cio_t *cio = j2k->cio;
2188 opj_image_t *image = j2k->image;
2189 opj_cp_t *cp = j2k->cp;
2191 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2192 lenp = cio_tell(cio);
2194 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2195 cio_write(cio, image->x1, 4); /* Xsiz */
2196 cio_write(cio, image->y1, 4); /* Ysiz */
2197 cio_write(cio, image->x0, 4); /* X0siz */
2198 cio_write(cio, image->y0, 4); /* Y0siz */
2199 cio_write(cio, cp->tdx, 4); /* XTsiz */
2200 cio_write(cio, cp->tdy, 4); /* YTsiz */
2201 cio_write(cio, cp->tx0, 4); /* XT0siz */
2202 cio_write(cio, cp->ty0, 4); /* YT0siz */
2203 cio_write(cio, image->numcomps, 2); /* Csiz */
2204 for (i = 0; i < image->numcomps; i++) {
2205 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2206 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2207 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2209 len = cio_tell(cio) - lenp;
2210 cio_seek(cio, lenp);
2211 cio_write(cio, len, 2); /* Lsiz */
2212 cio_seek(cio, lenp + len);
2215 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2219 * Writes the SIZ marker (image and tile size)
2221 * @param p_stream the stream to write data to.
2222 * @param p_j2k J2K codec.
2223 * @param p_manager the user event manager.
2225 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2226 struct opj_stream_private *p_stream,
2227 struct opj_event_mgr * p_manager )
2230 OPJ_UINT32 l_size_len;
2231 OPJ_BYTE * l_current_ptr;
2232 opj_image_t * l_image = 00;
2233 opj_cp_v2_t *cp = 00;
2234 opj_image_comp_t * l_img_comp = 00;
2237 assert(p_stream != 00);
2238 assert(p_j2k != 00);
2239 assert(p_manager != 00);
2241 l_image = p_j2k->m_private_image;
2242 cp = &(p_j2k->m_cp);
2243 l_size_len = 40 + 3 * l_image->numcomps;
2244 l_img_comp = l_image->comps;
2246 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2248 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2249 = (OPJ_BYTE*)opj_realloc(
2250 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2252 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2256 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2259 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2261 /* write SOC identifier */
2262 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2265 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2268 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2271 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2274 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2277 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2280 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2283 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2286 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2289 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2292 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2295 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2298 for (i = 0; i < l_image->numcomps; ++i) {
2299 /* TODO here with MCT ? */
2300 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2303 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2306 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2312 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) {
2319 static void j2k_read_siz(opj_j2k_t *j2k) {
2323 opj_cio_t *cio = j2k->cio;
2324 opj_image_t *image = j2k->image;
2325 opj_cp_t *cp = j2k->cp;
2327 len = cio_read(cio, 2); /* Lsiz */
2328 cio_read(cio, 2); /* Rsiz (capabilities) */
2329 image->x1 = cio_read(cio, 4); /* Xsiz */
2330 image->y1 = cio_read(cio, 4); /* Ysiz */
2331 image->x0 = cio_read(cio, 4); /* X0siz */
2332 image->y0 = cio_read(cio, 4); /* Y0siz */
2333 cp->tdx = cio_read(cio, 4); /* XTsiz */
2334 cp->tdy = cio_read(cio, 4); /* YTsiz */
2335 cp->tx0 = cio_read(cio, 4); /* XT0siz */
2336 cp->ty0 = cio_read(cio, 4); /* YT0siz */
2338 /* the following code triggers: */
2339 /* warning: comparison of unsigned expression < 0 is always false */
2341 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
2342 opj_event_msg(j2k->cinfo, EVT_ERROR,
2343 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
2344 image->x0,image->x1,image->y0,image->y1);
2349 image->numcomps = cio_read(cio, 2); /* Csiz */
2352 if (j2k->cp->correct) {
2353 /* if JPWL is on, we check whether TX errors have damaged
2354 too much the SIZ parameters */
2355 if (!(image->x1 * image->y1)) {
2356 opj_event_msg(j2k->cinfo, EVT_ERROR,
2357 "JPWL: bad image size (%d x %d)\n",
2358 image->x1, image->y1);
2359 if (!JPWL_ASSUME || JPWL_ASSUME) {
2360 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2364 assert( len >= 38 );
2365 if (image->numcomps != (OPJ_UINT32)((len - 38) / 3)) {
2366 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2367 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2368 image->numcomps, ((len - 38) / 3));
2370 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2373 /* we try to correct */
2374 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
2375 if (image->numcomps < (OPJ_UINT32)((len - 38) / 3)) {
2376 len = 38 + 3 * image->numcomps;
2377 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2380 image->numcomps = ((len - 38) / 3);
2381 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2386 /* update components number in the jpwl_exp_comps filed */
2387 cp->exp_comps = image->numcomps;
2389 #endif /* USE_JPWL */
2391 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
2392 for (i = 0; i < image->numcomps; i++) {
2394 tmp = cio_read(cio, 1); /* Ssiz_i */
2395 image->comps[i].prec = (tmp & 0x7f) + 1;
2396 image->comps[i].sgnd = tmp >> 7;
2397 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
2398 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
2401 if (j2k->cp->correct) {
2402 /* if JPWL is on, we check whether TX errors have damaged
2403 too much the SIZ parameters, again */
2404 if (!(image->comps[i].dx * image->comps[i].dy)) {
2405 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2406 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2407 i, i, image->comps[i].dx, image->comps[i].dy);
2409 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2412 /* we try to correct */
2413 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2414 if (!image->comps[i].dx) {
2415 image->comps[i].dx = 1;
2416 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2417 i, image->comps[i].dx);
2419 if (!image->comps[i].dy) {
2420 image->comps[i].dy = 1;
2421 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2422 i, image->comps[i].dy);
2427 #endif /* USE_JPWL */
2429 /* TODO: unused ? */
2430 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
2431 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
2433 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
2434 image->comps[i].factor = cp->reduce; /* reducing factor per component */
2437 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2438 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2441 if (j2k->cp->correct) {
2442 /* if JPWL is on, we check whether TX errors have damaged
2443 too much the SIZ parameters */
2444 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
2445 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2446 "JPWL: bad number of tiles (%d x %d)\n",
2449 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2452 /* we try to correct */
2453 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2456 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2459 if (cp->tw > cp->max_tiles) {
2461 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
2462 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2463 cp->max_tiles, cp->tw);
2467 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2470 if (cp->th > cp->max_tiles) {
2472 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2473 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2474 cp->max_tiles, cp->th);
2478 #endif /* USE_JPWL */
2480 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2481 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
2482 cp->tileno_size = 0;
2485 if (j2k->cp->correct) {
2487 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2488 "JPWL: could not alloc tcps field of cp\n");
2489 if (!JPWL_ASSUME || JPWL_ASSUME) {
2490 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2495 #endif /* USE_JPWL */
2497 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2498 cp->tcps[i].POC = 0;
2499 cp->tcps[i].numpocs = 0;
2500 cp->tcps[i].first = 1;
2503 /* Initialization for PPM marker */
2505 cp->ppm_data = NULL;
2506 cp->ppm_data_first = NULL;
2507 cp->ppm_previous = 0;
2510 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2511 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2512 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2514 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
2515 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
2516 j2k->state = J2K_STATE_MH;
2519 if (j2k->cstr_info) {
2520 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2521 cstr_info->image_w = image->x1 - image->x0;
2522 cstr_info->image_h = image->y1 - image->y0;
2523 cstr_info->numcomps = image->numcomps;
2524 cstr_info->tw = cp->tw;
2525 cstr_info->th = cp->th;
2526 cstr_info->tile_x = cp->tdx;
2527 cstr_info->tile_y = cp->tdy;
2528 cstr_info->tile_Ox = cp->tx0;
2529 cstr_info->tile_Oy = cp->ty0;
2530 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
2536 * Reads a SIZ marker (image and tile size)
2537 * @param p_header_data the data contained in the SIZ box.
2538 * @param jp2 the jpeg2000 file codec.
2539 * @param p_header_size the size of the data contained in the SIZ marker.
2540 * @param p_manager the user event manager.
2542 opj_bool j2k_read_siz_v2 (
2543 opj_j2k_v2_t *p_j2k,
2544 OPJ_BYTE * p_header_data,
2545 OPJ_UINT32 p_header_size,
2546 struct opj_event_mgr * p_manager
2549 OPJ_UINT32 l_size, i;
2550 OPJ_UINT32 l_nb_comp;
2551 OPJ_UINT32 l_nb_comp_remain;
2552 OPJ_UINT32 l_remaining_size;
2553 OPJ_UINT32 l_nb_tiles;
2555 opj_image_t *l_image = 00;
2556 opj_cp_v2_t *l_cp = 00;
2557 opj_image_comp_t * l_img_comp = 00;
2558 opj_tcp_v2_t * l_current_tile_param = 00;
2561 assert(p_j2k != 00);
2562 assert(p_manager != 00);
2563 assert(p_header_data != 00);
2565 l_image = p_j2k->m_private_image;
2566 l_cp = &(p_j2k->m_cp);
2568 /* minimum size == 39 - 3 (= minimum component parameter) */
2569 if (p_header_size < 36) {
2570 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2574 l_remaining_size = p_header_size - 36;
2575 l_nb_comp = l_remaining_size / 3;
2576 l_nb_comp_remain = l_remaining_size % 3;
2577 if (l_nb_comp_remain != 0){
2578 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2582 l_size = p_header_size + 2; /* Lsiz */
2584 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2586 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2587 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2589 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2591 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2593 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2595 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2597 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2599 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2601 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2603 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2606 l_image->numcomps = (OPJ_UINT16) l_tmp;
2608 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2612 if (l_image->numcomps != l_nb_comp) {
2613 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);
2618 if (l_cp->correct) {
2619 /* if JPWL is on, we check whether TX errors have damaged
2620 too much the SIZ parameters */
2621 if (!(l_image->x1 * l_image->y1)) {
2622 opj_event_msg_v2(p_manager, EVT_ERROR,
2623 "JPWL: bad image size (%d x %d)\n",
2624 l_image->x1, l_image->y1);
2625 if (!JPWL_ASSUME || JPWL_ASSUME) {
2626 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2631 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2632 if (l_image->numcomps != ((len - 38) / 3)) {
2633 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2634 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2635 l_image->numcomps, ((len - 38) / 3));
2637 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2640 */ /* we try to correct */
2641 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2642 if (l_image->numcomps < ((len - 38) / 3)) {
2643 len = 38 + 3 * l_image->numcomps;
2644 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2647 l_image->numcomps = ((len - 38) / 3);
2648 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2654 /* update components number in the jpwl_exp_comps filed */
2655 l_cp->exp_comps = l_image->numcomps;
2657 #endif /* USE_JPWL */
2659 /* Allocate the resulting image components */
2660 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2661 if (l_image->comps == 00){
2662 l_image->numcomps = 0;
2663 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2667 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2668 l_img_comp = l_image->comps;
2670 /* Read the component information */
2671 for (i = 0; i < l_image->numcomps; ++i){
2673 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2675 l_img_comp->prec = (tmp & 0x7f) + 1;
2676 l_img_comp->sgnd = tmp >> 7;
2677 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2679 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2680 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2682 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2685 if (l_cp->correct) {
2686 /* if JPWL is on, we check whether TX errors have damaged
2687 too much the SIZ parameters, again */
2688 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2689 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2690 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2691 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2693 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2696 /* we try to correct */
2697 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2698 if (!l_image->comps[i].dx) {
2699 l_image->comps[i].dx = 1;
2700 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2701 i, l_image->comps[i].dx);
2703 if (!l_image->comps[i].dy) {
2704 l_image->comps[i].dy = 1;
2705 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2706 i, l_image->comps[i].dy);
2710 #endif /* USE_JPWL */
2711 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2712 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2716 /* Compute the number of tiles */
2717 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2718 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2719 l_nb_tiles = l_cp->tw * l_cp->th;
2721 /* Define the tiles which will be decoded */
2722 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2723 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;
2724 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;
2725 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);
2726 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);
2729 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2730 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2731 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2732 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2736 if (l_cp->correct) {
2737 /* if JPWL is on, we check whether TX errors have damaged
2738 too much the SIZ parameters */
2739 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2740 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2741 "JPWL: bad number of tiles (%d x %d)\n",
2742 l_cp->tw, l_cp->th);
2744 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2747 /* we try to correct */
2748 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2751 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2754 if (l_cp->tw > l_cp->max_tiles) {
2756 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2757 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2758 l_cp->max_tiles, l_cp->tw);
2762 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2765 if (l_cp->th > l_cp->max_tiles) {
2767 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2768 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2769 l_cp->max_tiles, l_cp->th);
2773 #endif /* USE_JPWL */
2775 /* memory allocations */
2776 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2777 if (l_cp->tcps == 00) {
2778 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2781 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2784 if (l_cp->correct) {
2786 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2787 "JPWL: could not alloc tcps field of cp\n");
2788 if (!JPWL_ASSUME || JPWL_ASSUME) {
2789 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2794 #endif /* USE_JPWL */
2796 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2797 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2798 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2799 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2802 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2804 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2805 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2807 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2808 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2811 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));
2812 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2814 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2815 (opj_simple_mcc_decorrelation_data_t*)
2816 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2818 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2819 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2822 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));
2823 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2825 /* set up default dc level shift */
2826 for (i=0;i<l_image->numcomps;++i) {
2827 if (! l_image->comps[i].sgnd) {
2828 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2832 l_current_tile_param = l_cp->tcps;
2833 for (i = 0; i < l_nb_tiles; ++i) {
2834 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2835 if (l_current_tile_param->tccps == 00) {
2836 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2839 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2841 ++l_current_tile_param;
2844 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2845 opj_image_comp_header_update(l_image,l_cp);
2852 static void j2k_write_com(opj_j2k_t *j2k) {
2856 if(j2k->cp->comment) {
2857 opj_cio_t *cio = j2k->cio;
2858 char *comment = j2k->cp->comment;
2860 cio_write(cio, J2K_MS_COM, 2);
2861 lenp = cio_tell(cio);
2863 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2864 for (i = 0; i < strlen(comment); i++) {
2865 cio_write(cio, comment[i], 1);
2867 len = cio_tell(cio) - lenp;
2868 cio_seek(cio, lenp);
2869 cio_write(cio, len, 2);
2870 cio_seek(cio, lenp + len);
2874 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2880 * Writes the COM marker (comment)
2882 * @param p_stream the stream to write data to.
2883 * @param p_j2k J2K codec.
2884 * @param p_manager the user event manager.
2886 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2887 struct opj_stream_private *p_stream,
2888 struct opj_event_mgr * p_manager )
2890 OPJ_UINT32 l_comment_size;
2891 OPJ_UINT32 l_total_com_size;
2892 const OPJ_CHAR *l_comment;
2893 OPJ_BYTE * l_current_ptr = 00;
2896 assert(p_j2k != 00);
2897 assert(p_stream != 00);
2898 assert(p_manager != 00);
2900 l_comment = p_j2k->m_cp.comment;
2901 l_comment_size = strlen(l_comment);
2902 l_total_com_size = l_comment_size + 6;
2904 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2905 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2906 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2909 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2913 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2916 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2918 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2921 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2924 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2927 memcpy( l_current_ptr,l_comment,l_comment_size);
2929 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) {
2936 static void j2k_read_com(opj_j2k_t *j2k) {
2939 opj_cio_t *cio = j2k->cio;
2941 len = cio_read(cio, 2);
2942 cio_skip(cio, len - 2);
2945 * Reads a COM marker (comments)
2946 * @param p_header_data the data contained in the COM box.
2947 * @param jp2 the jpeg2000 file codec.
2948 * @param p_header_size the size of the data contained in the COM marker.
2949 * @param p_manager the user event manager.
2951 opj_bool j2k_read_com_v2 (
2952 opj_j2k_v2_t *p_j2k,
2953 OPJ_BYTE * p_header_data,
2954 OPJ_UINT32 p_header_size,
2955 struct opj_event_mgr * p_manager
2959 assert(p_j2k != 00);
2960 assert(p_manager != 00);
2961 assert(p_header_data != 00);
2962 (void)p_header_size;
2967 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2970 opj_cp_t *cp = j2k->cp;
2971 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2972 opj_tccp_t *tccp = &tcp->tccps[compno];
2973 opj_cio_t *cio = j2k->cio;
2975 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2976 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2977 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2978 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2979 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2981 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2982 for (i = 0; i < tccp->numresolutions; i++) {
2983 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2988 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2991 opj_cp_t *cp = j2k->cp;
2992 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2993 opj_tccp_t *tccp = &tcp->tccps[compno];
2994 opj_cio_t *cio = j2k->cio;
2996 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2998 /* If user wants to remove more resolutions than the codestream contains, return error*/
2999 assert(cp->reduce >= 0);
3000 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
3001 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
3002 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
3003 j2k->state |= J2K_STATE_ERR;
3006 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
3007 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
3008 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
3009 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
3010 if (tccp->csty & J2K_CP_CSTY_PRT) {
3011 for (i = 0; i < tccp->numresolutions; i++) {
3012 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
3013 tccp->prcw[i] = tmp & 0xf;
3014 tccp->prch[i] = tmp >> 4;
3019 if(j2k->cstr_info && compno == 0) {
3020 for (i = 0; i < tccp->numresolutions; i++) {
3021 if (tccp->csty & J2K_CP_CSTY_PRT) {
3022 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
3023 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
3026 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
3027 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
3034 static void j2k_write_cod(opj_j2k_t *j2k) {
3035 opj_cp_t *cp = NULL;
3036 opj_tcp_t *tcp = NULL;
3039 opj_cio_t *cio = j2k->cio;
3041 cio_write(cio, J2K_MS_COD, 2); /* COD */
3043 lenp = cio_tell(cio);
3047 tcp = &cp->tcps[j2k->curtileno];
3049 cio_write(cio, tcp->csty, 1); /* Scod */
3050 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
3051 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
3052 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
3054 j2k_write_cox(j2k, 0);
3055 len = cio_tell(cio) - lenp;
3056 cio_seek(cio, lenp);
3057 cio_write(cio, len, 2); /* Lcod */
3058 cio_seek(cio, lenp + len);
3061 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
3066 * Writes the COD marker (Coding style default)
3068 * @param p_stream the stream to write data to.
3069 * @param p_j2k J2K codec.
3070 * @param p_manager the user event manager.
3072 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
3073 struct opj_stream_private *p_stream,
3074 struct opj_event_mgr * p_manager )
3076 opj_cp_v2_t *l_cp = 00;
3077 opj_tcp_v2_t *l_tcp = 00;
3078 OPJ_UINT32 l_code_size,l_remaining_size;
3079 OPJ_BYTE * l_current_data = 00;
3082 assert(p_j2k != 00);
3083 assert(p_manager != 00);
3084 assert(p_stream != 00);
3086 l_cp = &(p_j2k->m_cp);
3087 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3088 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
3089 l_remaining_size = l_code_size;
3091 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3092 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3093 = (OPJ_BYTE*)opj_realloc(
3094 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3097 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3101 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
3104 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3106 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
3107 l_current_data += 2;
3109 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
3110 l_current_data += 2;
3112 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
3115 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
3118 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
3121 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
3124 l_remaining_size -= 9;
3126 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3127 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3131 if (l_remaining_size != 0) {
3132 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3136 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) {
3143 static void j2k_read_cod(opj_j2k_t *j2k) {
3147 opj_cio_t *cio = j2k->cio;
3148 opj_cp_t *cp = j2k->cp;
3149 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3150 opj_image_t *image = j2k->image;
3152 len = cio_read(cio, 2); /* Lcod */
3153 tcp->csty = cio_read(cio, 1); /* Scod */
3154 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
3155 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
3156 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
3158 pos = cio_tell(cio);
3159 for (i = 0; i < image->numcomps; i++) {
3160 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
3162 j2k_read_cox(j2k, i);
3166 if (j2k->cstr_info) {
3167 opj_codestream_info_t *cstr_info = j2k->cstr_info;
3168 cstr_info->prog = tcp->prg;
3169 cstr_info->numlayers = tcp->numlayers;
3170 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
3171 for (i = 0; i < image->numcomps; i++) {
3172 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
3178 * Reads a COD marker (Coding Styke defaults)
3179 * @param p_header_data the data contained in the COD box.
3180 * @param p_j2k the jpeg2000 codec.
3181 * @param p_header_size the size of the data contained in the COD marker.
3182 * @param p_manager the user event manager.
3184 opj_bool j2k_read_cod_v2 (
3185 opj_j2k_v2_t *p_j2k,
3186 OPJ_BYTE * p_header_data,
3187 OPJ_UINT32 p_header_size,
3188 struct opj_event_mgr * p_manager
3194 opj_cp_v2_t *l_cp = 00;
3195 opj_tcp_v2_t *l_tcp = 00;
3196 opj_image_t *l_image = 00;
3199 assert(p_header_data != 00);
3200 assert(p_j2k != 00);
3201 assert(p_manager != 00);
3203 l_image = p_j2k->m_private_image;
3204 l_cp = &(p_j2k->m_cp);
3206 /* If we are in the first tile-part header of the current tile */
3207 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3208 &l_cp->tcps[p_j2k->m_current_tile_number] :
3209 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3211 /* Make sure room is sufficient */
3212 if (p_header_size < 5) {
3213 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3217 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
3219 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
3221 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
3222 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
3225 /* If user didn't set a number layer to decode take the max specify in the codestream. */
3226 if (l_cp->m_specific_param.m_dec.m_layer) {
3227 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
3230 l_tcp->num_layers_to_decode = l_tcp->numlayers;
3233 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
3237 for (i = 0; i < l_image->numcomps; ++i) {
3238 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
3241 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3242 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3246 if (p_header_size != 0) {
3247 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3251 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
3252 j2k_copy_tile_component_parameters(p_j2k);
3255 #ifdef WIP_REMOVE_MSD
3256 if (p_j2k->cstr_info) {
3257 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
3258 p_j2k->cstr_info->prog = l_tcp->prg;
3259 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
3260 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
3261 for (i = 0; i < l_image->numcomps; ++i) {
3262 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
3270 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
3273 opj_cp_t *cp = j2k->cp;
3274 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3275 opj_image_t *image = j2k->image;
3276 opj_cio_t *cio = j2k->cio;
3278 cio_write(cio, J2K_MS_COC, 2); /* COC */
3279 lenp = cio_tell(cio);
3281 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3282 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
3283 j2k_write_cox(j2k, compno);
3284 len = cio_tell(cio) - lenp;
3285 cio_seek(cio, lenp);
3286 cio_write(cio, len, 2); /* Lcoc */
3287 cio_seek(cio, lenp + len);
3291 * Writes the COC marker (Coding style component)
3293 * @param p_comp_no the index of the component to output.
3294 * @param p_stream the stream to write data to.
3295 * @param p_j2k J2K codec.
3296 * @param p_manager the user event manager.
3298 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
3299 OPJ_UINT32 p_comp_no,
3300 struct opj_stream_private *p_stream,
3301 struct opj_event_mgr * p_manager )
3303 OPJ_UINT32 l_coc_size,l_remaining_size;
3304 OPJ_UINT32 l_comp_room;
3307 assert(p_j2k != 00);
3308 assert(p_manager != 00);
3309 assert(p_stream != 00);
3311 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3313 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3315 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3316 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3317 = (OPJ_BYTE*)opj_realloc(
3318 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3320 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3324 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3327 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);
3329 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) {
3337 * Writes the COC marker (Coding style component)
3339 * @param p_comp_no the index of the component to output.
3340 * @param p_stream the stream to write data to.
3341 * @param p_j2k J2K codec.
3342 * @param p_manager the user event manager.
3344 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3345 OPJ_UINT32 p_comp_no,
3347 OPJ_UINT32 * p_data_written,
3348 struct opj_event_mgr * p_manager )
3350 opj_cp_v2_t *l_cp = 00;
3351 opj_tcp_v2_t *l_tcp = 00;
3352 OPJ_UINT32 l_coc_size,l_remaining_size;
3353 OPJ_BYTE * l_current_data = 00;
3354 opj_image_t *l_image = 00;
3355 OPJ_UINT32 l_comp_room;
3358 assert(p_j2k != 00);
3359 assert(p_manager != 00);
3361 l_cp = &(p_j2k->m_cp);
3362 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3363 l_image = p_j2k->m_private_image;
3364 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3366 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3367 l_remaining_size = l_coc_size;
3369 l_current_data = p_data;
3371 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3372 l_current_data += 2;
3374 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3375 l_current_data += 2;
3377 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3378 l_current_data+=l_comp_room;
3380 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3383 l_remaining_size -= (5 + l_comp_room);
3384 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3385 * p_data_written = l_coc_size;
3389 * Gets the maximum size taken by a coc.
3391 * @param p_j2k the jpeg2000 codec to use.
3393 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3396 OPJ_UINT32 l_nb_comp;
3397 OPJ_UINT32 l_nb_tiles;
3398 OPJ_UINT32 l_max = 0;
3402 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3403 l_nb_comp = p_j2k->m_private_image->numcomps;
3405 for (i=0;i<l_nb_tiles;++i) {
3406 for (j=0;j<l_nb_comp;++j) {
3407 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3414 static void j2k_read_coc(opj_j2k_t *j2k) {
3417 opj_cp_t *cp = j2k->cp;
3418 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3419 opj_image_t *image = j2k->image;
3420 opj_cio_t *cio = j2k->cio;
3422 len = cio_read(cio, 2); /* Lcoc */
3423 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3424 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
3425 j2k_read_cox(j2k, compno);
3429 * Reads a COC marker (Coding Style Component)
3430 * @param p_header_data the data contained in the COC box.
3431 * @param p_j2k the jpeg2000 codec.
3432 * @param p_header_size the size of the data contained in the COC marker.
3433 * @param p_manager the user event manager.
3435 opj_bool j2k_read_coc_v2 (
3436 opj_j2k_v2_t *p_j2k,
3437 OPJ_BYTE * p_header_data,
3438 OPJ_UINT32 p_header_size,
3439 struct opj_event_mgr * p_manager
3442 opj_cp_v2_t *l_cp = NULL;
3443 opj_tcp_v2_t *l_tcp = NULL;
3444 opj_image_t *l_image = NULL;
3445 OPJ_UINT32 l_comp_room;
3446 OPJ_UINT32 l_comp_no;
3449 assert(p_header_data != 00);
3450 assert(p_j2k != 00);
3451 assert(p_manager != 00);
3453 l_cp = &(p_j2k->m_cp);
3454 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3455 &l_cp->tcps[p_j2k->m_current_tile_number] :
3456 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3457 l_image = p_j2k->m_private_image;
3459 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3461 /* make sure room is sufficient*/
3462 if (p_header_size < l_comp_room + 1) {
3463 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3466 p_header_size -= l_comp_room + 1;
3468 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3469 p_header_data += l_comp_room;
3470 if (l_comp_no >= l_image->numcomps) {
3471 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3475 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3478 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3479 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3483 if (p_header_size != 0) {
3484 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3490 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3491 int bandno, numbands;
3494 opj_cp_t *cp = j2k->cp;
3495 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3496 opj_tccp_t *tccp = &tcp->tccps[compno];
3497 opj_cio_t *cio = j2k->cio;
3499 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3500 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3502 for (bandno = 0; bandno < numbands; bandno++) {
3503 expn = tccp->stepsizes[bandno].expn;
3504 mant = tccp->stepsizes[bandno].mant;
3506 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3507 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3509 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3514 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3516 int bandno, numbands;
3518 opj_cp_t *cp = j2k->cp;
3519 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3520 opj_tccp_t *tccp = &tcp->tccps[compno];
3521 opj_cio_t *cio = j2k->cio;
3523 tmp = cio_read(cio, 1); /* Sqcx */
3524 tccp->qntsty = tmp & 0x1f;
3525 tccp->numgbits = tmp >> 5;
3526 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3527 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3530 if (j2k->cp->correct) {
3532 /* if JPWL is on, we check whether there are too many subbands */
3533 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3534 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3535 "JPWL: bad number of subbands in Sqcx (%d)\n",
3538 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3541 /* we try to correct */
3543 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3544 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3551 /* We check whether there are too many subbands */
3552 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3553 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3554 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3555 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3558 #endif /* USE_JPWL */
3560 for (bandno = 0; bandno < numbands; bandno++) {
3562 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3563 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3566 tmp = cio_read(cio, 2); /* SPqcx_i */
3570 if (bandno < J2K_MAXBANDS){
3571 tccp->stepsizes[bandno].expn = expn;
3572 tccp->stepsizes[bandno].mant = mant;
3576 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3577 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3578 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3579 tccp->stepsizes[bandno].expn =
3580 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3581 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3582 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3588 static void j2k_write_qcd(opj_j2k_t *j2k) {
3591 opj_cio_t *cio = j2k->cio;
3593 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3594 lenp = cio_tell(cio);
3596 j2k_write_qcx(j2k, 0);
3597 len = cio_tell(cio) - lenp;
3598 cio_seek(cio, lenp);
3599 cio_write(cio, len, 2); /* Lqcd */
3600 cio_seek(cio, lenp + len);
3603 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3607 * Writes the QCD marker (quantization default)
3609 * @param p_comp_number the index of the component to output.
3610 * @param p_stream the stream to write data to.
3611 * @param p_j2k J2K codec.
3612 * @param p_manager the user event manager.
3614 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3615 struct opj_stream_private *p_stream,
3616 struct opj_event_mgr * p_manager )
3618 opj_cp_v2_t *l_cp = 00;
3619 opj_tcp_v2_t *l_tcp = 00;
3620 OPJ_UINT32 l_qcd_size,l_remaining_size;
3621 OPJ_BYTE * l_current_data = 00;
3624 assert(p_j2k != 00);
3625 assert(p_manager != 00);
3626 assert(p_stream != 00);
3628 l_cp = &(p_j2k->m_cp);
3629 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3630 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3631 l_remaining_size = l_qcd_size;
3633 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3634 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3635 = (OPJ_BYTE*)opj_realloc(
3636 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3639 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3643 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3646 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3648 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3649 l_current_data += 2;
3651 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3652 l_current_data += 2;
3654 l_remaining_size -= 4;
3656 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3657 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3661 if (l_remaining_size != 0) {
3662 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3666 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) {
3674 static void j2k_read_qcd(opj_j2k_t *j2k) {
3678 opj_cio_t *cio = j2k->cio;
3679 opj_image_t *image = j2k->image;
3681 len = cio_read(cio, 2); /* Lqcd */
3682 pos = cio_tell(cio);
3683 for (i = 0; i < image->numcomps; i++) {
3685 j2k_read_qcx(j2k, i, len - 2);
3690 * Reads a QCD marker (Quantization defaults)
3691 * @param p_header_data the data contained in the QCD box.
3692 * @param p_j2k the jpeg2000 codec.
3693 * @param p_header_size the size of the data contained in the QCD marker.
3694 * @param p_manager the user event manager.
3696 opj_bool j2k_read_qcd_v2 (
3697 opj_j2k_v2_t *p_j2k,
3698 OPJ_BYTE * p_header_data,
3699 OPJ_UINT32 p_header_size,
3700 struct opj_event_mgr * p_manager
3704 assert(p_header_data != 00);
3705 assert(p_j2k != 00);
3706 assert(p_manager != 00);
3708 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3709 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3713 if (p_header_size != 0) {
3714 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3718 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3719 j2k_copy_tile_quantization_parameters(p_j2k);
3724 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3727 opj_cio_t *cio = j2k->cio;
3729 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3730 lenp = cio_tell(cio);
3732 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3733 j2k_write_qcx(j2k, compno);
3734 len = cio_tell(cio) - lenp;
3735 cio_seek(cio, lenp);
3736 cio_write(cio, len, 2); /* Lqcc */
3737 cio_seek(cio, lenp + len);
3741 * Writes the QCC marker (quantization component)
3743 * @param p_comp_no the index of the component to output.
3744 * @param p_stream the stream to write data to.
3745 * @param p_j2k J2K codec.
3746 * @param p_manager the user event manager.
3748 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3749 OPJ_UINT32 p_comp_no,
3750 struct opj_stream_private *p_stream,
3751 struct opj_event_mgr * p_manager )
3753 OPJ_UINT32 l_qcc_size,l_remaining_size;
3756 assert(p_j2k != 00);
3757 assert(p_manager != 00);
3758 assert(p_stream != 00);
3760 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3761 l_remaining_size = l_qcc_size;
3763 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3764 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3765 = (OPJ_BYTE*)opj_realloc(
3766 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3768 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3772 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3775 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);
3777 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) {
3785 * Writes the QCC marker (quantization component)
3787 * @param p_comp_no the index of the component to output.
3788 * @param p_stream the stream to write data to.
3789 * @param p_j2k J2K codec.
3790 * @param p_manager the user event manager.
3792 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3793 OPJ_UINT32 p_comp_no,
3795 OPJ_UINT32 * p_data_written,
3796 struct opj_event_mgr * p_manager )
3798 OPJ_UINT32 l_qcc_size,l_remaining_size;
3799 OPJ_BYTE * l_current_data = 00;
3802 assert(p_j2k != 00);
3803 assert(p_manager != 00);
3805 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3806 l_remaining_size = l_qcc_size;
3808 l_current_data = p_data;
3810 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3811 l_current_data += 2;
3813 if (p_j2k->m_private_image->numcomps <= 256) {
3816 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3817 l_current_data += 2;
3819 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3822 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3823 l_remaining_size -= 6;
3826 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3827 l_current_data += 2;
3829 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3832 l_remaining_size -= 6;
3835 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3837 *p_data_written = l_qcc_size;
3841 * Gets the maximum size taken by a qcc.
3843 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3845 return j2k_get_max_coc_size(p_j2k);
3848 static void j2k_read_qcc(opj_j2k_t *j2k) {
3850 int numcomp = j2k->image->numcomps;
3851 opj_cio_t *cio = j2k->cio;
3853 len = cio_read(cio, 2); /* Lqcc */
3854 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3857 if (j2k->cp->correct) {
3859 static int backup_compno = 0;
3861 /* compno is negative or larger than the number of components!!! */
3862 if ((compno < 0) || (compno >= numcomp)) {
3863 opj_event_msg(j2k->cinfo, EVT_ERROR,
3864 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3867 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3870 /* we try to correct */
3871 compno = backup_compno % numcomp;
3872 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3873 "- setting component number to %d\n",
3877 /* keep your private count of tiles */
3880 #endif /* USE_JPWL */
3882 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3886 * Reads a QCC marker (Quantization component)
3887 * @param p_header_data the data contained in the QCC box.
3888 * @param p_j2k the jpeg2000 codec.
3889 * @param p_header_size the size of the data contained in the QCC marker.
3890 * @param p_manager the user event manager.
3892 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3893 OPJ_BYTE * p_header_data,
3894 OPJ_UINT32 p_header_size,
3895 struct opj_event_mgr * p_manager)
3897 OPJ_UINT32 l_num_comp,l_comp_no;
3900 assert(p_header_data != 00);
3901 assert(p_j2k != 00);
3902 assert(p_manager != 00);
3904 l_num_comp = p_j2k->m_private_image->numcomps;
3906 if (l_num_comp <= 256) {
3907 if (p_header_size < 1) {
3908 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3911 opj_read_bytes(p_header_data,&l_comp_no,1);
3916 if (p_header_size < 2) {
3917 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3920 opj_read_bytes(p_header_data,&l_comp_no,2);
3926 if (p_j2k->m_cp.correct) {
3928 static OPJ_UINT32 backup_compno = 0;
3930 /* compno is negative or larger than the number of components!!! */
3931 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3932 opj_event_msg_v2(p_manager, EVT_ERROR,
3933 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3934 l_comp_no, l_num_comp);
3936 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3939 /* we try to correct */
3940 l_comp_no = backup_compno % l_num_comp;
3941 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3942 "- setting component number to %d\n",
3946 /* keep your private count of tiles */
3949 #endif /* USE_JPWL */
3951 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3952 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3956 if (p_header_size != 0) {
3957 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3965 static void j2k_write_poc(opj_j2k_t *j2k) {
3966 int len, numpchgs, i;
3968 int numcomps = j2k->image->numcomps;
3970 opj_cp_t *cp = j2k->cp;
3971 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3972 opj_tccp_t *tccp = &tcp->tccps[0];
3973 opj_cio_t *cio = j2k->cio;
3975 numpchgs = 1 + tcp->numpocs;
3976 cio_write(cio, J2K_MS_POC, 2); /* POC */
3977 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3978 cio_write(cio, len, 2); /* Lpoc */
3979 for (i = 0; i < numpchgs; i++) {
3980 opj_poc_t *poc = &tcp->pocs[i];
3981 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3982 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3983 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3984 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3985 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3986 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3987 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3988 poc->compno1 = int_min(poc->compno1, numcomps);
3989 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3994 * Writes the POC marker (Progression Order Change)
3996 * @param p_stream the stream to write data to.
3997 * @param p_j2k J2K codec.
3998 * @param p_manager the user event manager.
4000 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
4001 struct opj_stream_private *p_stream,
4002 struct opj_event_mgr * p_manager )
4004 OPJ_UINT32 l_nb_comp;
4005 OPJ_UINT32 l_nb_poc;
4006 OPJ_UINT32 l_poc_size;
4007 OPJ_UINT32 l_written_size = 0;
4008 opj_tcp_v2_t *l_tcp = 00;
4009 opj_tccp_t *l_tccp = 00;
4010 OPJ_UINT32 l_poc_room;
4013 assert(p_j2k != 00);
4014 assert(p_manager != 00);
4015 assert(p_stream != 00);
4017 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4018 l_tccp = &l_tcp->tccps[0];
4019 l_nb_comp = p_j2k->m_private_image->numcomps;
4020 l_nb_poc = 1 + l_tcp->numpocs;
4022 if (l_nb_comp <= 256) {
4028 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4030 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4031 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4032 = (OPJ_BYTE*)opj_realloc(
4033 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4036 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4040 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
4043 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
4045 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) {
4054 * Writes the POC marker (Progression Order Change)
4056 * @param p_stream the stream to write data to.
4057 * @param p_j2k J2K codec.
4058 * @param p_manager the user event manager.
4060 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
4062 OPJ_UINT32 * p_data_written,
4063 struct opj_event_mgr * p_manager )
4066 OPJ_BYTE * l_current_data = 00;
4067 OPJ_UINT32 l_nb_comp;
4068 OPJ_UINT32 l_nb_poc;
4069 OPJ_UINT32 l_poc_size;
4070 opj_image_t *l_image = 00;
4071 opj_tcp_v2_t *l_tcp = 00;
4072 opj_tccp_t *l_tccp = 00;
4073 opj_poc_t *l_current_poc = 00;
4074 OPJ_UINT32 l_poc_room;
4077 assert(p_j2k != 00);
4078 assert(p_manager != 00);
4080 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4081 l_tccp = &l_tcp->tccps[0];
4082 l_image = p_j2k->m_private_image;
4083 l_nb_comp = l_image->numcomps;
4084 l_nb_poc = 1 + l_tcp->numpocs;
4086 if (l_nb_comp <= 256) {
4093 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4095 l_current_data = p_data;
4097 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
4098 l_current_data += 2;
4100 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
4101 l_current_data += 2;
4103 l_current_poc = l_tcp->pocs;
4104 for (i = 0; i < l_nb_poc; ++i) {
4105 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
4108 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
4109 l_current_data+=l_poc_room;
4111 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
4114 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
4117 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
4118 l_current_data+=l_poc_room;
4120 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
4123 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
4124 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
4125 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
4126 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
4131 *p_data_written = l_poc_size;
4135 * Gets the maximum size taken by the writing of a POC.
4137 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
4139 opj_tcp_v2_t * l_tcp = 00;
4140 OPJ_UINT32 l_nb_tiles = 0;
4141 OPJ_UINT32 l_max_poc = 0;
4144 l_tcp = p_j2k->m_cp.tcps;
4145 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4147 for (i=0;i<l_nb_tiles;++i) {
4148 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
4154 return 4 + 9 * l_max_poc;
4158 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
4160 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
4163 OPJ_UINT32 l_nb_tiles;
4164 OPJ_UINT32 l_max = 0;
4165 opj_tcp_v2_t * l_tcp = 00;
4167 l_tcp = p_j2k->m_cp.tcps;
4168 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
4170 for (i=0;i<l_nb_tiles;++i) {
4171 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
4181 * Gets the maximum size taken by the headers of the SOT.
4183 * @param p_j2k the jpeg2000 codec to use.
4185 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
4187 OPJ_UINT32 l_nb_bytes = 0;
4188 OPJ_UINT32 l_nb_comps;
4189 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
4191 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
4192 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
4194 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
4195 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
4196 l_nb_bytes += l_nb_comps * l_coc_bytes;
4198 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
4199 l_nb_bytes += l_nb_comps * l_qcc_bytes;
4202 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
4204 /*** DEVELOPER CORNER, Add room for your headers ***/
4209 static void j2k_read_poc(opj_j2k_t *j2k) {
4210 int len, numpchgs, i, old_poc;
4212 int numcomps = j2k->image->numcomps;
4214 opj_cp_t *cp = j2k->cp;
4215 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
4216 opj_cio_t *cio = j2k->cio;
4218 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
4220 len = cio_read(cio, 2); /* Lpoc */
4221 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
4223 for (i = old_poc; i < numpchgs + old_poc; i++) {
4225 poc = &tcp->pocs[i];
4226 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
4227 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
4228 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
4229 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
4230 poc->compno1 = int_min(
4231 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
4232 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
4235 tcp->numpocs = numpchgs + old_poc - 1;
4239 * Reads a POC marker (Progression Order Change)
4241 * @param p_header_data the data contained in the POC box.
4242 * @param p_j2k the jpeg2000 codec.
4243 * @param p_header_size the size of the data contained in the POC marker.
4244 * @param p_manager the user event manager.
4246 opj_bool j2k_read_poc_v2 (
4247 opj_j2k_v2_t *p_j2k,
4248 OPJ_BYTE * p_header_data,
4249 OPJ_UINT32 p_header_size,
4250 struct opj_event_mgr * p_manager)
4252 OPJ_UINT32 i, l_nb_comp, l_tmp;
4253 opj_image_t * l_image = 00;
4254 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
4255 OPJ_UINT32 l_chunk_size, l_comp_room;
4257 opj_cp_v2_t *l_cp = 00;
4258 opj_tcp_v2_t *l_tcp = 00;
4259 opj_poc_t *l_current_poc = 00;
4262 assert(p_header_data != 00);
4263 assert(p_j2k != 00);
4264 assert(p_manager != 00);
4266 l_image = p_j2k->m_private_image;
4267 l_nb_comp = l_image->numcomps;
4268 if (l_nb_comp <= 256) {
4274 l_chunk_size = 5 + 2 * l_comp_room;
4275 l_current_poc_nb = p_header_size / l_chunk_size;
4276 l_current_poc_remaining = p_header_size % l_chunk_size;
4278 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
4279 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
4283 l_cp = &(p_j2k->m_cp);
4284 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4285 &l_cp->tcps[p_j2k->m_current_tile_number] :
4286 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4287 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
4288 l_current_poc_nb += l_old_poc_nb;
4290 assert(l_current_poc_nb < 32);
4292 /* now poc is in use.*/
4295 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
4296 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
4297 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
4299 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
4300 p_header_data+=l_comp_room;
4301 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
4303 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
4305 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
4306 p_header_data+=l_comp_room;
4307 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
4309 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4310 /* make sure comp is in acceptable bounds */
4311 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4315 l_tcp->numpocs = l_current_poc_nb - 1;
4319 static void j2k_read_crg(opj_j2k_t *j2k) {
4320 int len, i, Xcrg_i, Ycrg_i;
4322 opj_cio_t *cio = j2k->cio;
4323 int numcomps = j2k->image->numcomps;
4325 len = cio_read(cio, 2); /* Lcrg */
4326 for (i = 0; i < numcomps; i++) {
4327 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
4328 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4333 * Reads a CRG marker (Component registration)
4335 * @param p_header_data the data contained in the TLM box.
4336 * @param p_j2k the jpeg2000 codec.
4337 * @param p_header_size the size of the data contained in the TLM marker.
4338 * @param p_manager the user event manager.
4340 opj_bool j2k_read_crg_v2 (
4341 opj_j2k_v2_t *p_j2k,
4342 OPJ_BYTE * p_header_data,
4343 OPJ_UINT32 p_header_size,
4344 struct opj_event_mgr * p_manager
4347 OPJ_UINT32 l_nb_comp;
4349 assert(p_header_data != 00);
4350 assert(p_j2k != 00);
4351 assert(p_manager != 00);
4353 l_nb_comp = p_j2k->m_private_image->numcomps;
4355 if (p_header_size != l_nb_comp *4) {
4356 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4359 /* Do not care of this at the moment since only local variables are set here */
4362 (i = 0; i < l_nb_comp; ++i)
4364 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4366 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4373 static void j2k_read_tlm(opj_j2k_t *j2k) {
4374 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4375 long int Ttlm_i, Ptlm_i;
4377 opj_cio_t *cio = j2k->cio;
4379 len = cio_read(cio, 2); /* Ltlm */
4380 Ztlm = cio_read(cio, 1); /* Ztlm */
4381 Stlm = cio_read(cio, 1); /* Stlm */
4382 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4383 SP = (Stlm >> 6) & 0x01;
4384 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4385 for (i = 0; i < tile_tlm; i++) {
4386 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4387 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4392 * Reads a TLM marker (Tile Length Marker)
4394 * @param p_header_data the data contained in the TLM box.
4395 * @param p_j2k the jpeg2000 codec.
4396 * @param p_header_size the size of the data contained in the TLM marker.
4397 * @param p_manager the user event manager.
4399 opj_bool j2k_read_tlm_v2 (
4400 opj_j2k_v2_t *p_j2k,
4401 OPJ_BYTE * p_header_data,
4402 OPJ_UINT32 p_header_size,
4403 struct opj_event_mgr * p_manager
4406 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4408 assert(p_header_data != 00);
4409 assert(p_j2k != 00);
4410 assert(p_manager != 00);
4412 if (p_header_size < 2) {
4413 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4418 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4420 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4423 l_ST = ((l_Stlm >> 4) & 0x3);
4424 l_SP = (l_Stlm >> 6) & 0x1;
4426 l_Ptlm_size = (l_SP + 1) * 2;
4427 l_quotient = l_Ptlm_size + l_ST;
4429 l_tot_num_tp = p_header_size / l_quotient;
4430 l_tot_num_tp_remaining = p_header_size % l_quotient;
4432 if (l_tot_num_tp_remaining != 0) {
4433 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4436 /* FIXME Do not care of this at the moment since only local variables are set here */
4439 (i = 0; i < l_tot_num_tp; ++i)
4441 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4442 p_header_data += l_ST;
4443 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4444 p_header_data += l_Ptlm_size;
4449 static void j2k_read_plm(opj_j2k_t *j2k) {
4450 int len, i, Zplm, Nplm, add, packet_len = 0;
4452 opj_cio_t *cio = j2k->cio;
4454 len = cio_read(cio, 2); /* Lplm */
4455 Zplm = cio_read(cio, 1); /* Zplm */
4458 Nplm = cio_read(cio, 4); /* Nplm */
4460 for (i = Nplm; i > 0; i--) {
4461 add = cio_read(cio, 1);
4463 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4464 if ((add & 0x80) == 0) {
4475 * Reads a PLM marker (Packet length, main header marker)
4477 * @param p_header_data the data contained in the TLM box.
4478 * @param p_j2k the jpeg2000 codec.
4479 * @param p_header_size the size of the data contained in the TLM marker.
4480 * @param p_manager the user event manager.
4482 opj_bool j2k_read_plm_v2 (
4483 opj_j2k_v2_t *p_j2k,
4484 OPJ_BYTE * p_header_data,
4485 OPJ_UINT32 p_header_size,
4486 struct opj_event_mgr * p_manager
4490 assert(p_header_data != 00);
4491 assert(p_j2k != 00);
4492 assert(p_manager != 00);
4494 if (p_header_size < 1) {
4495 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4498 /* Do not care of this at the moment since only local variables are set here */
4500 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4507 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4509 p_header_size -= (1+l_Nplm);
4513 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4517 (i = 0; i < l_Nplm; ++i)
4519 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4521 // take only the last seven bytes
4522 l_packet_len |= (l_tmp & 0x7f);
4530 // store packet length and proceed to next packet
4537 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4545 static void j2k_read_plt(opj_j2k_t *j2k) {
4546 int len, i, Zplt, packet_len = 0, add;
4548 opj_cio_t *cio = j2k->cio;
4550 len = cio_read(cio, 2); /* Lplt */
4551 Zplt = cio_read(cio, 1); /* Zplt */
4552 for (i = len - 3; i > 0; i--) {
4553 add = cio_read(cio, 1);
4554 packet_len = (packet_len << 7) + add; /* Iplt_i */
4555 if ((add & 0x80) == 0) {
4563 * Reads a PLT marker (Packet length, tile-part header)
4565 * @param p_header_data the data contained in the PLT box.
4566 * @param p_j2k the jpeg2000 codec.
4567 * @param p_header_size the size of the data contained in the PLT marker.
4568 * @param p_manager the user event manager.
4570 opj_bool j2k_read_plt_v2 (
4571 opj_j2k_v2_t *p_j2k,
4572 OPJ_BYTE * p_header_data,
4573 OPJ_UINT32 p_header_size,
4574 struct opj_event_mgr * p_manager
4577 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4580 assert(p_header_data != 00);
4581 assert(p_j2k != 00);
4582 assert(p_manager != 00);
4584 if (p_header_size < 1) {
4585 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4589 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4593 for (i = 0; i < p_header_size; ++i) {
4594 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4596 /* take only the last seven bytes */
4597 l_packet_len |= (l_tmp & 0x7f);
4602 /* store packet length and proceed to next packet */
4607 if (l_packet_len != 0) {
4608 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4615 static void j2k_read_ppm(opj_j2k_t *j2k) {
4616 int len, Z_ppm, i, j;
4619 opj_cp_t *cp = j2k->cp;
4620 opj_cio_t *cio = j2k->cio;
4622 len = cio_read(cio, 2);
4625 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4628 if (cp->ppm_previous == 0) {
4629 N_ppm = cio_read(cio, 4); /* N_ppm */
4632 N_ppm = cp->ppm_previous;
4635 if (Z_ppm == 0) { /* First PPM marker */
4636 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4637 cp->ppm_data_first = cp->ppm_data;
4638 cp->ppm_len = N_ppm;
4639 } else { /* NON-first PPM marker */
4640 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4643 /* this memory allocation check could be done even in non-JPWL cases */
4645 if (!cp->ppm_data) {
4646 opj_event_msg(j2k->cinfo, EVT_ERROR,
4647 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4649 if (!JPWL_ASSUME || JPWL_ASSUME) {
4650 opj_free(cp->ppm_data);
4651 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4658 cp->ppm_data_first = cp->ppm_data;
4659 cp->ppm_len = N_ppm + cp->ppm_store;
4661 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4662 cp->ppm_data[j] = cio_read(cio, 1);
4666 break; /* Case of non-finished packet header in present marker but finished in next one */
4668 cp->ppm_previous = i - 1;
4673 * Reads a PPM marker (Packed packet headers, main header)
4675 * @param p_header_data the data contained in the POC box.
4676 * @param p_j2k the jpeg2000 codec.
4677 * @param p_header_size the size of the data contained in the POC marker.
4678 * @param p_manager the user event manager.
4681 opj_bool j2k_read_ppm_v2 (
4682 opj_j2k_v2_t *p_j2k,
4683 OPJ_BYTE * p_header_data,
4684 OPJ_UINT32 p_header_size,
4685 struct opj_event_mgr * p_manager
4689 opj_cp_v2_t *l_cp = 00;
4690 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4693 assert(p_header_data != 00);
4694 assert(p_j2k != 00);
4695 assert(p_manager != 00);
4697 if (p_header_size < 1) {
4698 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4702 l_cp = &(p_j2k->m_cp);
4705 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4709 /* First PPM marker */
4711 if (p_header_size < 4) {
4712 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4716 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4720 /* First PPM marker: Initialization */
4721 l_cp->ppm_len = l_N_ppm;
4722 l_cp->ppm_data_size = 0;
4724 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4725 if (l_cp->ppm_buffer == 00) {
4726 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4729 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4731 l_cp->ppm_data = l_cp->ppm_buffer;
4735 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4736 if (p_header_size >= 4) {
4738 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4741 l_cp->ppm_len += l_N_ppm ;
4743 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4744 if (l_cp->ppm_buffer == 00) {
4745 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4748 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4750 l_cp->ppm_data = l_cp->ppm_buffer;
4757 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4759 if (l_remaining_data <= p_header_size) {
4760 /* we must store less information than available in the packet */
4761 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4762 l_cp->ppm_data_size = l_cp->ppm_len;
4763 p_header_size -= l_remaining_data;
4764 p_header_data += l_remaining_data;
4767 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4768 l_cp->ppm_data_size += p_header_size;
4769 p_header_data += p_header_size;
4782 * Reads a PPM marker (Packed packet headers, main header)
4784 * @param p_header_data the data contained in the POC box.
4785 * @param p_j2k the jpeg2000 codec.
4786 * @param p_header_size the size of the data contained in the POC marker.
4787 * @param p_manager the user event manager.
4789 opj_bool j2k_read_ppm_v3 (
4790 opj_j2k_v2_t *p_j2k,
4791 OPJ_BYTE * p_header_data,
4792 OPJ_UINT32 p_header_size,
4793 struct opj_event_mgr * p_manager
4796 opj_cp_v2_t *l_cp = 00;
4797 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4800 assert(p_header_data != 00);
4801 assert(p_j2k != 00);
4802 assert(p_manager != 00);
4804 /* Minimum size of PPM marker is equal to the size of Zppm element */
4805 if (p_header_size < 1) {
4806 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4810 l_cp = &(p_j2k->m_cp);
4813 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4817 /* First PPM marker */
4819 /* We need now at least the Nppm^0 element */
4820 if (p_header_size < 4) {
4821 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4825 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4829 /* First PPM marker: Initialization */
4830 l_cp->ppm_len = l_N_ppm;
4831 l_cp->ppm_data_read = 0;
4833 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4834 if (l_cp->ppm_data == 00) {
4835 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4838 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4840 l_cp->ppm_data_current = l_cp->ppm_data;
4842 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4845 if (p_header_size < 4) {
4846 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4850 /* Uncompleted Ippm series in the previous PPM marker?*/
4851 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4852 /* Get the place where add the remaining Ippm series*/
4853 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4854 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4857 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4861 /* Increase the size of ppm_data to add the new Ippm series*/
4862 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4864 /* Keep the position of the place where concatenate the new series*/
4865 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4866 l_cp->ppm_len += l_N_ppm;
4871 l_remaining_data = p_header_size;
4873 while (l_remaining_data >= l_N_ppm) {
4874 /* read a complete Ippm series*/
4875 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4876 p_header_size -= l_N_ppm;
4877 p_header_data += l_N_ppm;
4879 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4883 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4888 l_remaining_data = p_header_size;
4892 l_remaining_data = p_header_size;
4894 /* Next Ippm series is a complete series ?*/
4895 if (l_remaining_data > l_N_ppm) {
4896 /* Increase the size of ppm_data to add the new Ippm series*/
4897 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4899 /* Keep the position of the place where concatenate the new series */
4900 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4901 l_cp->ppm_len += l_N_ppm;
4906 /* Need to read an incomplete Ippm series*/
4907 if (l_remaining_data) {
4908 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4910 /* Keep the position of the place where concatenate the new series*/
4911 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4912 l_cp->ppm_len += l_N_ppm;
4914 /* Read incomplete Ippm series*/
4915 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4916 p_header_size -= l_remaining_data;
4917 p_header_data += l_remaining_data;
4919 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4924 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4925 if (p_header_size >= 4) {
4927 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4930 l_cp->ppm_len += l_N_ppm ;
4932 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4933 if (l_cp->ppm_buffer == 00) {
4934 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4937 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4939 l_cp->ppm_data = l_cp->ppm_buffer;
4946 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4948 if (l_remaining_data <= p_header_size) {
4949 /* we must store less information than available in the packet */
4950 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4951 l_cp->ppm_data_size = l_cp->ppm_len;
4952 p_header_size -= l_remaining_data;
4953 p_header_data += l_remaining_data;
4956 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4957 l_cp->ppm_data_size += p_header_size;
4958 p_header_data += p_header_size;
4967 static void j2k_read_ppt(opj_j2k_t *j2k) {
4968 int len, Z_ppt, i, j = 0;
4970 opj_cp_t *cp = j2k->cp;
4971 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4972 opj_cio_t *cio = j2k->cio;
4974 len = cio_read(cio, 2);
4975 Z_ppt = cio_read(cio, 1);
4977 if (Z_ppt == 0) { /* First PPT marker */
4978 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4979 tcp->ppt_data_first = tcp->ppt_data;
4981 tcp->ppt_len = len - 3;
4982 } else { /* NON-first PPT marker */
4983 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4984 tcp->ppt_data_first = tcp->ppt_data;
4985 tcp->ppt_len = len - 3 + tcp->ppt_store;
4988 for (i = len - 3; i > 0; i--) {
4989 tcp->ppt_data[j] = cio_read(cio, 1);
4996 * Reads a PPT marker (Packed packet headers, tile-part header)
4998 * @param p_header_data the data contained in the PPT box.
4999 * @param p_j2k the jpeg2000 codec.
5000 * @param p_header_size the size of the data contained in the PPT marker.
5001 * @param p_manager the user event manager.
5003 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
5004 OPJ_BYTE * p_header_data,
5005 OPJ_UINT32 p_header_size,
5006 struct opj_event_mgr * p_manager )
5008 opj_cp_v2_t *l_cp = 00;
5009 opj_tcp_v2_t *l_tcp = 00;
5013 assert(p_header_data != 00);
5014 assert(p_j2k != 00);
5015 assert(p_manager != 00);
5017 /* We need to have the Z_ppt element at minimum */
5018 if (p_header_size < 1) {
5019 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
5023 l_cp = &(p_j2k->m_cp);
5025 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");
5029 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
5032 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
5036 /* Allocate buffer to read the packet header */
5038 /* First PPT marker */
5039 l_tcp->ppt_data_size = 0;
5040 l_tcp->ppt_len = p_header_size;
5042 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
5043 if (l_tcp->ppt_buffer == 00) {
5044 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5047 l_tcp->ppt_data = l_tcp->ppt_buffer;
5049 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
5052 l_tcp->ppt_len += p_header_size;
5054 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
5055 if (l_tcp->ppt_buffer == 00) {
5056 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5059 l_tcp->ppt_data = l_tcp->ppt_buffer;
5061 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
5064 /* Read packet header from buffer */
5065 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
5067 l_tcp->ppt_data_size += p_header_size;
5073 * Writes the TLM marker (Tile Length Marker)
5075 * @param p_stream the stream to write data to.
5076 * @param p_j2k J2K codec.
5077 * @param p_manager the user event manager.
5079 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
5080 struct opj_stream_private *p_stream,
5081 struct opj_event_mgr * p_manager )
5083 OPJ_BYTE * l_current_data = 00;
5084 OPJ_UINT32 l_tlm_size;
5087 assert(p_j2k != 00);
5088 assert(p_manager != 00);
5089 assert(p_stream != 00);
5091 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5093 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5094 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5095 = (OPJ_BYTE*)opj_realloc(
5096 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5099 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5103 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
5106 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5108 /* change the way data is written to avoid seeking if possible */
5110 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
5112 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
5113 l_current_data += 2;
5115 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
5116 l_current_data += 2;
5118 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
5121 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5124 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
5125 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) {
5132 static void j2k_write_tlm(opj_j2k_t *j2k){
5134 opj_cio_t *cio = j2k->cio;
5135 j2k->tlm_start = cio_tell(cio);
5136 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
5137 lenp = 4 + (5*j2k->totnum_tp);
5138 cio_write(cio,lenp,2); /* Ltlm */
5139 cio_write(cio, 0,1); /* Ztlm=0*/
5140 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5141 cio_skip(cio,5*j2k->totnum_tp);
5144 static void j2k_write_sot(opj_j2k_t *j2k) {
5147 opj_cio_t *cio = j2k->cio;
5149 j2k->sot_start = cio_tell(cio);
5150 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
5151 lenp = cio_tell(cio);
5152 cio_skip(cio, 2); /* Lsot (further) */
5153 cio_write(cio, j2k->curtileno, 2); /* Isot */
5154 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
5155 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
5156 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
5157 len = cio_tell(cio) - lenp;
5158 cio_seek(cio, lenp);
5159 cio_write(cio, len, 2); /* Lsot */
5160 cio_seek(cio, lenp + len);
5164 /* update markers struct */
5165 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
5166 #endif /* USE_JPWL */
5169 if( j2k->cstr_info && j2k->cur_tp_num==0){
5170 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
5175 * Writes the SOT marker (Start of tile-part)
5177 * @param p_stream the stream to write data to.
5178 * @param p_j2k J2K codec.
5179 * @param p_manager the user event manager.
5181 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
5183 OPJ_UINT32 * p_data_written,
5184 const struct opj_stream_private *p_stream,
5185 struct opj_event_mgr * p_manager )
5188 assert(p_j2k != 00);
5189 assert(p_manager != 00);
5190 assert(p_stream != 00);
5192 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
5195 opj_write_bytes(p_data,10,2); /* Lsot */
5198 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
5204 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
5207 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
5212 /* update markers struct */
5214 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
5216 assert( 0 && "TODO" );
5217 #endif /* USE_JPWL */
5219 * p_data_written = 12;
5225 static void j2k_read_sot(opj_j2k_t *j2k) {
5226 int len, tileno, totlen, partno, numparts;
5228 opj_tcp_t *tcp = NULL;
5231 opj_cp_t *cp = j2k->cp;
5232 opj_cio_t *cio = j2k->cio;
5234 len = cio_read(cio, 2);
5235 tileno = cio_read(cio, 2);
5238 if (j2k->cp->correct) {
5240 static int backup_tileno = 0;
5242 /* tileno is negative or larger than the number of tiles!!! */
5243 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
5244 opj_event_msg(j2k->cinfo, EVT_ERROR,
5245 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5246 tileno, (cp->tw * cp->th));
5248 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5251 /* we try to correct */
5252 tileno = backup_tileno;
5253 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
5254 "- setting tile number to %d\n",
5258 /* keep your private count of tiles */
5262 #endif /* USE_JPWL */
5264 /* tileno is negative or larger than the number of tiles!!! */
5265 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
5266 opj_event_msg(j2k->cinfo, EVT_ERROR,
5267 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5268 tileno, (cp->tw * cp->th));
5273 if (cp->tileno_size == 0) {
5274 cp->tileno[cp->tileno_size] = tileno;
5278 assert(cp->tileno_size >= 0);
5279 while (i < (OPJ_UINT32)cp->tileno_size && status == 0) {
5280 status = cp->tileno[i] == tileno ? 1 : 0;
5284 cp->tileno[cp->tileno_size] = tileno;
5289 totlen = cio_read(cio, 4);
5292 if (j2k->cp->correct) {
5294 /* totlen is negative or larger than the bytes left!!! */
5295 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
5296 opj_event_msg(j2k->cinfo, EVT_ERROR,
5297 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5298 totlen, cio_numbytesleft(cio) + 8);
5300 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5303 /* we try to correct */
5305 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
5306 "- setting Psot to %d => assuming it is the last tile\n",
5312 #endif /* USE_JPWL */
5314 /* totlen is negative or larger than the bytes left!!! */
5315 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
5316 opj_event_msg(j2k->cinfo, EVT_ERROR,
5317 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5318 totlen, cio_numbytesleft(cio) + 8);
5324 totlen = cio_numbytesleft(cio) + 8;
5326 partno = cio_read(cio, 1);
5327 numparts = cio_read(cio, 1);
5329 if (partno >= numparts) {
5330 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);
5331 numparts = partno+1;
5334 j2k->curtileno = tileno;
5335 j2k->cur_tp_num = partno;
5336 j2k->eot = cio_getbp(cio) - 12 + totlen;
5337 j2k->state = J2K_STATE_TPH;
5338 tcp = &cp->tcps[j2k->curtileno];
5341 if (j2k->cstr_info) {
5344 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
5345 j2k->cstr_info->tile[tileno].tileno = tileno;
5346 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
5347 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5349 j2k->cstr_info->tile[tileno].end_pos += totlen;
5351 j2k->cstr_info->tile[tileno].num_tps = numparts;
5353 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
5355 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)*/
5356 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
5357 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5358 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5361 if (tcp->first == 1) {
5362 /* Initialization PPT */
5363 opj_tccp_t *tmp = tcp->tccps;
5364 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
5366 tcp->ppt_data = NULL;
5367 tcp->ppt_data_first = NULL;
5370 for (i = 0; i < j2k->image->numcomps; i++) {
5371 tcp->tccps[i] = j2k->default_tcp->tccps[i];
5373 cp->tcps[j2k->curtileno].first = 0;
5378 * Reads a PPT marker (Packed packet headers, tile-part header)
5380 * @param p_header_data the data contained in the PPT box.
5381 * @param p_j2k the jpeg2000 codec.
5382 * @param p_header_size the size of the data contained in the PPT marker.
5383 * @param p_manager the user event manager.
5385 opj_bool j2k_read_sot_v2 (
5386 opj_j2k_v2_t *p_j2k,
5387 OPJ_BYTE * p_header_data,
5388 OPJ_UINT32 p_header_size,
5389 struct opj_event_mgr * p_manager
5393 opj_cp_v2_t *l_cp = 00;
5394 opj_tcp_v2_t *l_tcp = 00;
5395 OPJ_UINT32 l_tot_len, l_num_parts = 0;
5396 OPJ_UINT32 l_current_part;
5397 OPJ_UINT32 l_tile_x,l_tile_y;
5400 assert(p_header_data != 00);
5401 assert(p_j2k != 00);
5402 assert(p_manager != 00);
5404 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
5405 if (p_header_size != 8) {
5406 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
5410 l_cp = &(p_j2k->m_cp);
5411 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
5414 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5415 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
5416 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
5419 if (l_cp->correct) {
5421 int tileno = p_j2k->m_current_tile_number;
5422 static int backup_tileno = 0;
5424 /* tileno is negative or larger than the number of tiles!!! */
5425 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
5426 opj_event_msg_v2(p_manager, EVT_ERROR,
5427 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5428 tileno, (l_cp->tw * l_cp->th));
5430 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5433 /* we try to correct */
5434 tileno = backup_tileno;
5435 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5436 "- setting tile number to %d\n",
5440 /* keep your private count of tiles */
5443 #endif /* USE_JPWL */
5445 /* look for the tile in the list of already processed tile (in parts). */
5446 /* Optimization possible here with a more complex data structure and with the removing of tiles */
5447 /* since the time taken by this function can only grow at the time */
5449 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
5452 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
5453 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
5455 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5461 if (l_cp->correct) {
5463 /* totlen is negative or larger than the bytes left!!! */
5464 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))) { */
5465 opj_event_msg_v2(p_manager, EVT_ERROR,
5466 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5467 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5469 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5472 /* we try to correct */
5474 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5475 "- setting Psot to %d => assuming it is the last tile\n",
5479 #endif /* USE_JPWL */
5481 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5483 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5484 "we assuming it is the last tile-part of the codestream.\n");
5485 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5488 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5491 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5494 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5495 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5496 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5497 if (l_tcp->m_nb_tile_parts) {
5498 if (l_current_part >= l_tcp->m_nb_tile_parts){
5499 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5500 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5501 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5505 l_tcp->m_nb_tile_parts = l_num_parts;
5508 /* If know the number of tile part header we will check if we didn't read the last*/
5509 if (l_tcp->m_nb_tile_parts) {
5510 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5511 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5515 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5516 /* Keep the size of data to skip after this marker */
5517 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5520 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5521 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5524 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5526 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5527 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5528 p_j2k->m_specific_param.m_decoder.m_skip_data =
5529 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5530 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5531 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5532 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5535 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5536 p_j2k->m_specific_param.m_decoder.m_skip_data =
5537 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5541 if (p_j2k->cstr_index)
5543 assert(p_j2k->cstr_index->tile_index != 00);
5544 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5545 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5547 if (l_num_parts != 0){
5548 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5549 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5552 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5553 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5554 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5556 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5557 (opj_tp_index_t*)opj_realloc(
5558 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5559 l_num_parts* sizeof(opj_tp_index_t));
5562 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5564 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5565 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5566 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5567 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5568 sizeof(opj_tp_index_t));
5571 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5572 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5573 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5574 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5575 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5576 * sizeof(opj_tp_index_t));
5585 /* 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 */
5586 /* if (p_j2k->cstr_info) {
5589 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5592 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5593 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5594 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5595 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5598 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5601 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5605 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5608 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5609 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5610 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5615 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5618 opj_tcp_t *tcp = NULL;
5619 opj_codestream_info_t *cstr_info = NULL;
5621 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5622 opj_cp_t *cp = j2k->cp;
5623 opj_cio_t *cio = j2k->cio;
5625 tcd->tp_num = j2k->tp_num ;
5626 tcd->cur_tp_num = j2k->cur_tp_num;
5628 cio_write(cio, J2K_MS_SOD, 2);
5630 if( j2k->cstr_info && j2k->cur_tp_num==0){
5631 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5634 if (j2k->curtileno == 0) {
5635 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5639 cstr_info = j2k->cstr_info;
5641 if (!j2k->cur_tp_num ) {
5642 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5643 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5646 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5647 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5651 /* update markers struct */
5652 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5653 #endif /* USE_JPWL */
5658 tcp = &cp->tcps[j2k->curtileno];
5659 for (layno = 0; layno < tcp->numlayers; layno++) {
5660 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5661 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5662 } else if (tcp->rates[layno]) {
5663 tcp->rates[layno]=1;
5666 if(j2k->cur_tp_num == 0){
5667 tcd->tcd_image->tiles->packno = 0;
5669 cstr_info->packno = 0;
5672 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5674 /* Writing Psot in SOT marker */
5675 totlen = cio_tell(cio) + l - j2k->sot_start;
5676 cio_seek(cio, j2k->sot_start + 6);
5677 cio_write(cio, totlen, 4);
5678 cio_seek(cio, j2k->sot_start + totlen);
5679 /* Writing Ttlm and Ptlm in TLM marker */
5681 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5682 cio_write(cio, j2k->curtileno, 1);
5683 cio_write(cio, totlen, 4);
5685 cio_seek(cio, j2k->sot_start + totlen);
5689 * Writes the SOD marker (Start of data)
5691 * @param p_stream the stream to write data to.
5692 * @param p_j2k J2K codec.
5693 * @param p_manager the user event manager.
5695 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5696 struct opj_tcd_v2 * p_tile_coder,
5698 OPJ_UINT32 * p_data_written,
5699 OPJ_UINT32 p_total_data_size,
5700 const struct opj_stream_private *p_stream,
5701 struct opj_event_mgr * p_manager )
5703 opj_tcp_v2_t *l_tcp = 00;
5704 opj_codestream_info_t *l_cstr_info = 00;
5705 opj_cp_v2_t *l_cp = 00;
5707 OPJ_UINT32 l_size_tile;
5708 OPJ_UINT32 l_remaining_data;
5711 assert(p_j2k != 00);
5712 assert(p_manager != 00);
5713 assert(p_stream != 00);
5715 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5718 /* make room for the EOF marker */
5719 l_remaining_data = p_total_data_size - 4;
5721 l_cp = &(p_j2k->m_cp);
5722 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5725 /* update tile coder */
5726 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5727 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5729 l_size_tile = l_cp->th * l_cp->tw;
5732 /* TODO mergeV2: check this part which use cstr_info */
5733 /*l_cstr_info = p_j2k->cstr_info;
5735 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5736 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5737 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5743 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5745 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5750 /* update markers struct */
5751 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5753 assert( 0 && "TODO" );
5754 #endif /* USE_JPWL */
5759 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5760 p_tile_coder->tcd_image->tiles->packno = 0;
5762 l_cstr_info->packno = 0;
5766 *p_data_written = 0;
5768 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)) {
5769 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5773 *p_data_written += 2;
5779 static void j2k_read_sod(opj_j2k_t *j2k) {
5780 int len, truncate = 0, i;
5781 unsigned char *data = NULL, *data_ptr = NULL;
5783 opj_cio_t *cio = j2k->cio;
5784 int curtileno = j2k->curtileno;
5787 if (j2k->cstr_info) {
5788 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5789 cio_tell(cio) + j2k->pos_correction - 1;
5790 if (j2k->cur_tp_num == 0)
5791 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5792 j2k->cstr_info->packno = 0;
5795 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
5797 if (len == cio_numbytesleft(cio) + 1) {
5798 truncate = 1; /* Case of a truncate codestream */
5801 data = j2k->tile_data[curtileno];
5802 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
5804 data_ptr = data + j2k->tile_len[curtileno];
5805 for (i = 0; i < len; i++) {
5806 data_ptr[i] = cio_read(cio, 1);
5809 j2k->tile_len[curtileno] += len;
5810 j2k->tile_data[curtileno] = data;
5813 j2k->state = J2K_STATE_TPHSOT;
5815 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
5821 * Reads a SOD marker (Start Of Data)
5823 * @param p_header_data the data contained in the SOD box.
5824 * @param p_j2k the jpeg2000 codec.
5825 * @param p_header_size the size of the data contained in the SOD marker.
5826 * @param p_manager the user event manager.
5828 opj_bool j2k_read_sod_v2 (
5829 opj_j2k_v2_t *p_j2k,
5830 struct opj_stream_private *p_stream,
5831 struct opj_event_mgr * p_manager
5834 OPJ_UINT32 l_current_read_size;
5835 opj_codestream_index_t * l_cstr_index = 00;
5836 OPJ_BYTE ** l_current_data = 00;
5837 opj_tcp_v2_t * l_tcp = 00;
5838 OPJ_UINT32 * l_tile_len = 00;
5841 assert(p_j2k != 00);
5842 assert(p_manager != 00);
5843 assert(p_stream != 00);
5845 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5847 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5848 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5849 // but we are in the last tile part,
5850 // so its result will fit on OPJ_UINT32 unless we find
5851 // a file with a single tile part of more than 4 GB...*/
5852 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5855 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5857 l_current_data = &(l_tcp->m_data);
5858 l_tile_len = &l_tcp->m_data_size;
5860 if (! *l_current_data) {
5861 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5864 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5867 if (*l_current_data == 00) {
5868 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5874 l_cstr_index = p_j2k->cstr_index;
5876 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5878 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5879 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5881 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5882 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5884 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5888 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5890 /*l_cstr_index->packno = 0;*/
5893 l_current_read_size = opj_stream_read_data( p_stream,
5894 *l_current_data + *l_tile_len,
5895 p_j2k->m_specific_param.m_decoder.m_sot_length,
5898 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5899 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5902 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5905 *l_tile_len += l_current_read_size;
5911 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5912 opj_cp_t *cp = j2k->cp;
5913 opj_tcp_t *tcp = &cp->tcps[tileno];
5914 opj_cio_t *cio = j2k->cio;
5915 int numcomps = j2k->image->numcomps;
5917 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5918 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5919 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5920 cio_write(cio, 0, 1); /* Srgn */
5921 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5925 * Writes the RGN marker (Region Of Interest)
5927 * @param p_tile_no the tile to output
5928 * @param p_comp_no the component to output
5929 * @param p_stream the stream to write data to.
5930 * @param p_j2k J2K codec.
5931 * @param p_manager the user event manager.
5933 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5934 OPJ_UINT32 p_tile_no,
5935 OPJ_UINT32 p_comp_no,
5936 struct opj_stream_private *p_stream,
5937 struct opj_event_mgr * p_manager )
5939 OPJ_BYTE * l_current_data = 00;
5940 OPJ_UINT32 l_nb_comp;
5941 OPJ_UINT32 l_rgn_size;
5942 opj_image_t *l_image = 00;
5943 opj_cp_v2_t *l_cp = 00;
5944 opj_tcp_v2_t *l_tcp = 00;
5945 opj_tccp_t *l_tccp = 00;
5946 OPJ_UINT32 l_comp_room;
5949 assert(p_j2k != 00);
5950 assert(p_manager != 00);
5951 assert(p_stream != 00);
5953 l_cp = &(p_j2k->m_cp);
5954 l_tcp = &l_cp->tcps[p_tile_no];
5955 l_tccp = &l_tcp->tccps[p_comp_no];
5957 l_nb_comp = l_image->numcomps;
5959 if (l_nb_comp <= 256) {
5966 l_rgn_size = 6 + l_comp_room;
5968 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5970 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5971 l_current_data += 2;
5973 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5974 l_current_data += 2;
5976 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5977 l_current_data+=l_comp_room;
5979 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5982 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5985 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) {
5992 static void j2k_read_rgn(opj_j2k_t *j2k) {
5993 int len, compno, roisty;
5995 opj_cp_t *cp = j2k->cp;
5996 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5997 opj_cio_t *cio = j2k->cio;
5998 int numcomps = j2k->image->numcomps;
6000 len = cio_read(cio, 2); /* Lrgn */
6001 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
6002 roisty = cio_read(cio, 1); /* Srgn */
6005 if (j2k->cp->correct) {
6006 /* totlen is negative or larger than the bytes left!!! */
6007 if (compno >= numcomps) {
6008 opj_event_msg(j2k->cinfo, EVT_ERROR,
6009 "JPWL: bad component number in RGN (%d when there are only %d)\n",
6011 if (!JPWL_ASSUME || JPWL_ASSUME) {
6012 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
6017 #endif /* USE_JPWL */
6019 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
6022 static void j2k_write_eoc(opj_j2k_t *j2k) {
6023 opj_cio_t *cio = j2k->cio;
6024 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
6025 cio_write(cio, J2K_MS_EOC, 2);
6029 /* update markers struct */
6030 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
6031 #endif /* USE_JPWL */
6036 * Writes the EOC marker (End of Codestream)
6038 * @param p_stream the stream to write data to.
6039 * @param p_j2k J2K codec.
6040 * @param p_manager the user event manager.
6042 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
6043 struct opj_stream_private *p_stream,
6044 struct opj_event_mgr * p_manager )
6047 assert(p_j2k != 00);
6048 assert(p_manager != 00);
6049 assert(p_stream != 00);
6051 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
6056 /* update markers struct */
6058 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
6060 #endif /* USE_JPWL */
6062 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
6066 if ( ! opj_stream_flush(p_stream,p_manager) ) {
6075 * Reads a RGN marker (Region Of Interest)
6077 * @param p_header_data the data contained in the POC box.
6078 * @param p_j2k the jpeg2000 codec.
6079 * @param p_header_size the size of the data contained in the POC marker.
6080 * @param p_manager the user event manager.
6082 opj_bool j2k_read_rgn_v2 (
6083 opj_j2k_v2_t *p_j2k,
6084 OPJ_BYTE * p_header_data,
6085 OPJ_UINT32 p_header_size,
6086 struct opj_event_mgr * p_manager
6089 OPJ_UINT32 l_nb_comp;
6090 opj_image_t * l_image = 00;
6092 opj_cp_v2_t *l_cp = 00;
6093 opj_tcp_v2_t *l_tcp = 00;
6094 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
6097 assert(p_header_data != 00);
6098 assert(p_j2k != 00);
6099 assert(p_manager != 00);
6101 l_image = p_j2k->m_private_image;
6102 l_nb_comp = l_image->numcomps;
6104 if (l_nb_comp <= 256) {
6109 if (p_header_size != 2 + l_comp_room) {
6110 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
6114 l_cp = &(p_j2k->m_cp);
6115 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
6116 &l_cp->tcps[p_j2k->m_current_tile_number] :
6117 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6119 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
6120 p_header_data+=l_comp_room;
6121 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
6125 if (l_cp->correct) {
6126 /* totlen is negative or larger than the bytes left!!! */
6127 if (l_comp_room >= l_nb_comp) {
6128 opj_event_msg_v2(p_manager, EVT_ERROR,
6129 "JPWL: bad component number in RGN (%d when there are only %d)\n",
6130 l_comp_room, l_nb_comp);
6131 if (!JPWL_ASSUME || JPWL_ASSUME) {
6132 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
6137 #endif /* USE_JPWL */
6139 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
6146 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
6148 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
6151 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
6158 * Updates the rates of the tcp.
6160 * @param p_stream the stream to write data to.
6161 * @param p_j2k J2K codec.
6162 * @param p_manager the user event manager.
6164 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
6165 struct opj_stream_private *p_stream,
6166 struct opj_event_mgr * p_manager )
6168 opj_cp_v2_t * l_cp = 00;
6169 opj_image_t * l_image = 00;
6170 opj_tcp_v2_t * l_tcp = 00;
6171 opj_image_comp_t * l_img_comp = 00;
6174 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
6175 OPJ_FLOAT32 * l_rates = 0;
6176 OPJ_FLOAT32 l_sot_remove;
6177 OPJ_UINT32 l_bits_empty, l_size_pixel;
6178 OPJ_UINT32 l_tile_size = 0;
6179 OPJ_UINT32 l_last_res;
6180 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
6183 assert(p_j2k != 00);
6184 assert(p_manager != 00);
6185 assert(p_stream != 00);
6188 l_cp = &(p_j2k->m_cp);
6189 l_image = p_j2k->m_private_image;
6192 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
6193 l_size_pixel = l_image->numcomps * l_image->comps->prec;
6194 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
6196 if (l_cp->m_specific_param.m_enc.m_tp_on) {
6197 l_tp_stride_func = get_tp_stride;
6200 l_tp_stride_func = get_default_stride;
6203 for (i=0;i<l_cp->th;++i) {
6204 for (j=0;j<l_cp->tw;++j) {
6205 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
6207 /* 4 borders of the tile rescale on the image if necessary */
6208 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
6209 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
6210 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
6211 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
6213 l_rates = l_tcp->rates;
6215 /* Modification of the RATE >> */
6217 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6219 ((*l_rates) * l_bits_empty)
6227 for (k = 1; k < l_tcp->numlayers; ++k) {
6229 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6231 ((*l_rates) * l_bits_empty)
6247 for (i=0;i<l_cp->th;++i) {
6248 for (j=0;j<l_cp->tw;++j) {
6249 l_rates = l_tcp->rates;
6252 *l_rates -= l_sot_remove;
6254 if (*l_rates < 30) {
6261 l_last_res = l_tcp->numlayers - 1;
6263 for (k = 1; k < l_last_res; ++k) {
6266 *l_rates -= l_sot_remove;
6268 if (*l_rates < *(l_rates - 1) + 10) {
6269 *l_rates = (*(l_rates - 1)) + 20;
6277 *l_rates -= (l_sot_remove + 2.f);
6279 if (*l_rates < *(l_rates - 1) + 10) {
6280 *l_rates = (*(l_rates - 1)) + 20;
6288 l_img_comp = l_image->comps;
6291 for (i=0;i<l_image->numcomps;++i) {
6292 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
6294 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
6302 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
6304 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
6306 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
6307 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
6308 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
6309 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
6313 if (l_cp->m_specific_param.m_enc.m_cinema) {
6314 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
6315 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
6316 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6320 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
6321 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
6327 static void j2k_read_eoc(opj_j2k_t *j2k) {
6329 opj_bool success = OPJ_FALSE;
6331 /* if packets should be decoded */
6332 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
6333 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
6334 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
6335 for (i = 0; i < j2k->cp->tileno_size; i++) {
6336 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
6337 if (j2k->cp->tileno[i] != -1)
6339 tileno = j2k->cp->tileno[i];
6340 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
6341 opj_free(j2k->tile_data[tileno]);
6342 j2k->tile_data[tileno] = NULL;
6343 tcd_free_decode_tile(tcd, i);
6346 success = OPJ_FALSE;
6347 if (success == OPJ_FALSE) {
6348 j2k->state |= J2K_STATE_ERR;
6352 tcd_free_decode(tcd);
6355 /* if packets should not be decoded */
6357 for (i = 0; i < j2k->cp->tileno_size; i++) {
6358 tileno = j2k->cp->tileno[i];
6359 opj_free(j2k->tile_data[tileno]);
6360 j2k->tile_data[tileno] = NULL;
6363 if (j2k->state & J2K_STATE_ERR)
6364 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
6366 j2k->state = J2K_STATE_MT;
6370 * Reads a EOC marker (End Of Codestream)
6372 * @param p_header_data the data contained in the SOD box.
6373 * @param p_j2k the jpeg2000 codec.
6374 * @param p_header_size the size of the data contained in the SOD marker.
6375 * @param p_manager the user event manager.
6378 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
6379 struct opj_stream_private *p_stream,
6380 struct opj_event_mgr * p_manager )
6383 opj_tcd_v2_t * l_tcd = 00;
6384 OPJ_UINT32 l_nb_tiles;
6385 opj_tcp_v2_t * l_tcp = 00;
6389 assert(p_j2k != 00);
6390 assert(p_manager != 00);
6391 assert(p_stream != 00);
6393 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6394 l_tcp = p_j2k->m_cp.tcps;
6396 l_tcd = tcd_create_v2(OPJ_TRUE);
6398 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6402 for (i = 0; i < l_nb_tiles; ++i) {
6403 if (l_tcp->m_data) {
6404 if (! tcd_init_decode_tile(l_tcd, i)) {
6405 tcd_destroy_v2(l_tcd);
6406 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6410 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
6414 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
6419 j2k_tcp_destroy(l_tcp);
6423 tcd_destroy_v2(l_tcd);
6429 * Gets the offset of the header.
6431 * @param p_stream the stream to write data to.
6432 * @param p_j2k J2K codec.
6433 * @param p_manager the user event manager.
6435 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
6436 struct opj_stream_private *p_stream,
6437 struct opj_event_mgr * p_manager )
6440 assert(p_j2k != 00);
6441 assert(p_manager != 00);
6442 assert(p_stream != 00);
6444 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
6450 * Writes the MCT marker (Multiple Component Transform)
6452 * @param p_stream the stream to write data to.
6453 * @param p_j2k J2K codec.
6454 * @param p_manager the user event manager.
6456 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
6457 struct opj_stream_private *p_stream,
6458 struct opj_event_mgr * p_manager )
6461 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6462 opj_mct_data_t * l_mct_record;
6463 opj_tcp_v2_t * l_tcp;
6466 assert(p_j2k != 00);
6467 assert(p_stream != 00);
6468 assert(p_manager != 00);
6470 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
6474 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6475 l_mct_record = l_tcp->m_mct_records;
6477 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6479 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
6486 l_mcc_record = l_tcp->m_mcc_records;
6488 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6490 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6497 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6505 * Writes the image components.
6507 * @param p_stream the stream to write data to.
6508 * @param p_j2k J2K codec.
6509 * @param p_manager the user event manager.
6511 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6512 struct opj_stream_private *p_stream,
6513 struct opj_event_mgr * p_manager )
6518 assert(p_j2k != 00);
6519 assert(p_manager != 00);
6520 assert(p_stream != 00);
6522 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6524 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6528 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6537 * Writes regions of interests.
6539 * @param p_stream the stream to write data to.
6540 * @param p_j2k J2K codec.
6541 * @param p_manager the user event manager.
6543 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
6544 struct opj_stream_private *p_stream,
6545 struct opj_event_mgr * p_manager )
6548 const opj_tccp_t *l_tccp = 00;
6551 assert(p_j2k != 00);
6552 assert(p_manager != 00);
6553 assert(p_stream != 00);
6555 l_tccp = p_j2k->m_cp.tcps->tccps;
6557 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6558 if (l_tccp->roishift) {
6560 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6574 * @param p_stream the stream to write data to.
6575 * @param p_j2k J2K codec.
6576 * @param p_manager the user event manager.
6578 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6579 struct opj_stream_private *p_stream,
6580 struct opj_event_mgr * p_manager )
6582 opj_codestream_index_t * l_cstr_index = 00;
6585 assert(p_j2k != 00);
6586 assert(p_manager != 00);
6587 assert(p_stream != 00);
6589 l_cstr_index = p_j2k->cstr_index;
6591 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6593 /* The following adjustment is done to adjust the codestream size */
6594 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6595 /* the first bunch of bytes is not in the codestream */
6596 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6601 /* preparation of JPWL marker segments */
6605 /* encode according to JPWL */
6606 jpwl_encode(p_j2k, p_stream, image);
6610 assert( 0 && "TODO" );
6611 #endif /* USE_JPWL */
6616 typedef struct opj_dec_mstabent {
6619 /** value of the state when the marker can appear */
6621 /** action linked to the marker */
6622 void (*handler) (opj_j2k_t *j2k);
6623 } opj_dec_mstabent_t;
6625 opj_dec_mstabent_t j2k_dec_mstab[] = {
6626 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
6627 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
6628 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
6629 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6630 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
6631 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
6632 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6633 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6634 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6635 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6636 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6637 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6638 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6639 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6640 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6641 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6643 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6644 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6647 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6648 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6649 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6650 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6651 #endif /* USE_JPWL */
6653 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6654 {J2K_MS_INSEC, 0, j2k_read_insec},
6655 #endif /* USE_JPSEC */
6657 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6660 static void j2k_read_unk(opj_j2k_t *j2k) {
6661 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6664 if (j2k->cp->correct) {
6666 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6667 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6668 id = cio_read(j2k->cio, 2);
6669 opj_event_msg(j2k->cinfo, EVT_ERROR,
6670 "JPWL: really don't know this marker %x\n",
6673 opj_event_msg(j2k->cinfo, EVT_ERROR,
6674 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6677 /* OK, activate this at your own risk!!! */
6678 /* we look for the marker at the minimum hamming distance from this */
6679 while (j2k_dec_mstab[m].id) {
6681 /* 1's where they differ */
6682 tmp_id = j2k_dec_mstab[m].id ^ id;
6684 /* compute the hamming distance between our id and the current */
6686 for (i = 0; i < 16; i++) {
6687 if ((tmp_id >> i) & 0x0001) {
6692 /* if current distance is smaller, set the minimum */
6693 if (cur_dist < min_dist) {
6694 min_dist = cur_dist;
6695 min_id = j2k_dec_mstab[m].id;
6698 /* jump to the next marker */
6702 /* do we substitute the marker? */
6703 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6704 opj_event_msg(j2k->cinfo, EVT_ERROR,
6705 "- marker %x is at distance %d from the read %x\n",
6706 min_id, min_dist, id);
6707 opj_event_msg(j2k->cinfo, EVT_ERROR,
6708 "- trying to substitute in place and crossing fingers!\n");
6709 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6710 cio_write(j2k->cio, min_id, 2);
6713 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6718 #endif /* USE_JPWL */
6723 * Reads an unknown marker
6725 * @param p_stream the stream object to read from.
6726 * @param p_j2k the jpeg2000 codec.
6727 * @param p_manager the user event manager.
6729 * @return true if the marker could be deduced.
6731 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6732 struct opj_stream_private *p_stream,
6733 OPJ_UINT32 *output_marker,
6734 struct opj_event_mgr * p_manager
6737 OPJ_UINT32 l_unknown_marker;
6738 const opj_dec_memory_marker_handler_t * l_marker_handler;
6739 OPJ_UINT32 l_size_unk = 2;
6742 assert(p_j2k != 00);
6743 assert(p_manager != 00);
6744 assert(p_stream != 00);
6746 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6749 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6750 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6751 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6755 /* read 2 bytes as the new marker ID*/
6756 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6758 if (!(l_unknown_marker < 0xff00)) {
6760 /* Get the marker handler from the marker ID*/
6761 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6763 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6764 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6768 if (l_marker_handler->id != J2K_MS_UNK) {
6769 /* Add the marker to the codestream index*/
6770 if (l_marker_handler->id != J2K_MS_SOT)
6771 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6772 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6774 break; /* next marker is known and well located */
6782 *output_marker = l_marker_handler->id ;
6788 Read the lookup table containing all the marker, status and action
6789 @param id Marker value
6791 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6792 opj_dec_mstabent_t *e;
6793 for (e = j2k_dec_mstab; e->id != 0; e++) {
6802 * Writes the MCT marker (Multiple Component Transform)
6804 * @param p_stream the stream to write data to.
6805 * @param p_j2k J2K codec.
6806 * @param p_manager the user event manager.
6808 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6809 opj_mct_data_t * p_mct_record,
6810 struct opj_stream_private *p_stream,
6811 struct opj_event_mgr * p_manager )
6813 OPJ_UINT32 l_mct_size;
6814 OPJ_BYTE * l_current_data = 00;
6818 assert(p_j2k != 00);
6819 assert(p_manager != 00);
6820 assert(p_stream != 00);
6822 l_mct_size = 10 + p_mct_record->m_data_size;
6824 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6825 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6826 = (OPJ_BYTE*)opj_realloc(
6827 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6830 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6834 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6837 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6839 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6840 l_current_data += 2;
6842 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6843 l_current_data += 2;
6845 opj_write_bytes(l_current_data,0,2); /* Zmct */
6846 l_current_data += 2;
6848 /* only one marker atm */
6849 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6851 opj_write_bytes(l_current_data,l_tmp,2);
6852 l_current_data += 2;
6854 opj_write_bytes(l_current_data,0,2); /* Ymct */
6857 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6859 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) {
6867 * Reads a MCT marker (Multiple Component Transform)
6869 * @param p_header_data the data contained in the MCT box.
6870 * @param p_j2k the jpeg2000 codec.
6871 * @param p_header_size the size of the data contained in the MCT marker.
6872 * @param p_manager the user event manager.
6874 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6875 OPJ_BYTE * p_header_data,
6876 OPJ_UINT32 p_header_size,
6877 struct opj_event_mgr * p_manager )
6880 opj_tcp_v2_t *l_tcp = 00;
6883 opj_mct_data_t * l_mct_data;
6886 assert(p_header_data != 00);
6887 assert(p_j2k != 00);
6889 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6890 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6891 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6893 if (p_header_size < 2) {
6894 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6899 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6902 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6906 if(p_header_size <= 6) {
6907 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6911 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6912 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6915 l_indix = l_tmp & 0xff;
6916 l_mct_data = l_tcp->m_mct_records;
6918 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6919 if (l_mct_data->m_index == l_indix) {
6926 if (i == l_tcp->m_nb_mct_records) {
6927 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6928 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6930 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));
6931 if(! l_tcp->m_mct_records) {
6932 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6936 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6937 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6940 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6943 if (l_mct_data->m_data) {
6944 opj_free(l_mct_data->m_data);
6945 l_mct_data->m_data = 00;
6948 l_mct_data->m_index = l_indix;
6949 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6950 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6952 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6955 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6961 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6962 if (! l_mct_data->m_data) {
6963 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6966 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6968 l_mct_data->m_data_size = p_header_size;
6969 ++l_tcp->m_nb_mct_records;
6975 * Writes the MCC marker (Multiple Component Collection)
6977 * @param p_stream the stream to write data to.
6978 * @param p_j2k J2K codec.
6979 * @param p_manager the user event manager.
6981 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6982 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6983 struct opj_stream_private *p_stream,
6984 struct opj_event_mgr * p_manager )
6987 OPJ_UINT32 l_mcc_size;
6988 OPJ_BYTE * l_current_data = 00;
6989 OPJ_UINT32 l_nb_bytes_for_comp;
6994 assert(p_j2k != 00);
6995 assert(p_manager != 00);
6996 assert(p_stream != 00);
6998 if (p_mcc_record->m_nb_comps > 255 ) {
6999 l_nb_bytes_for_comp = 2;
7003 l_nb_bytes_for_comp = 1;
7007 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
7008 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
7010 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7011 = (OPJ_BYTE*)opj_realloc(
7012 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7014 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7018 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
7021 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7023 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
7024 l_current_data += 2;
7026 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
7027 l_current_data += 2;
7030 opj_write_bytes(l_current_data,0,2); /* Zmcc */
7031 l_current_data += 2;
7033 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
7036 /* only one marker atm */
7037 opj_write_bytes(l_current_data,0,2); /* Ymcc */
7040 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
7043 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
7046 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 */
7049 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
7050 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
7051 l_current_data+=l_nb_bytes_for_comp;
7054 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 */
7057 for (i=0;i<p_mcc_record->m_nb_comps;++i)
7059 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
7060 l_current_data+=l_nb_bytes_for_comp;
7063 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
7065 if (p_mcc_record->m_decorrelation_array) {
7066 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
7069 if (p_mcc_record->m_offset_array) {
7070 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
7073 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
7076 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) {
7084 * Reads a MCC marker (Multiple Component Collection)
7086 * @param p_header_data the data contained in the MCC box.
7087 * @param p_j2k the jpeg2000 codec.
7088 * @param p_header_size the size of the data contained in the MCC marker.
7089 * @param p_manager the user event manager.
7091 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
7092 OPJ_BYTE * p_header_data,
7093 OPJ_UINT32 p_header_size,
7094 struct opj_event_mgr * p_manager )
7099 opj_tcp_v2_t * l_tcp;
7100 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7101 opj_mct_data_t * l_mct_data;
7102 OPJ_UINT32 l_nb_collections;
7103 OPJ_UINT32 l_nb_comps;
7104 OPJ_UINT32 l_nb_bytes_by_comp;
7108 assert(p_header_data != 00);
7109 assert(p_j2k != 00);
7110 assert(p_manager != 00);
7112 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7113 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7114 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7116 if (p_header_size < 2) {
7117 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7122 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
7125 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
7129 if (p_header_size < 7) {
7130 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7134 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
7137 l_mcc_record = l_tcp->m_mcc_records;
7139 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
7140 if (l_mcc_record->m_index == l_indix) {
7147 if (i == l_tcp->m_nb_mcc_records) {
7148 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
7149 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
7151 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7152 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7153 if (! l_tcp->m_mcc_records) {
7154 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7157 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
7158 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));
7160 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
7162 l_mcc_record->m_index = l_indix;
7164 /* only one marker atm */
7165 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
7168 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
7172 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
7175 if (l_nb_collections > 1) {
7176 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
7182 for (i=0;i<l_nb_collections;++i) {
7183 if (p_header_size < 3) {
7184 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7188 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
7192 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
7196 opj_read_bytes(p_header_data,&l_nb_comps,2);
7201 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7202 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
7204 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
7205 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7209 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
7211 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7212 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
7213 p_header_data+=l_nb_bytes_by_comp;
7216 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7221 opj_read_bytes(p_header_data,&l_nb_comps,2);
7224 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7225 l_nb_comps &= 0x7fff;
7227 if (l_nb_comps != l_mcc_record->m_nb_comps) {
7228 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
7232 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
7233 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7237 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
7239 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7240 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
7241 p_header_data+=l_nb_bytes_by_comp;
7244 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7249 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
7252 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
7253 l_mcc_record->m_decorrelation_array = 00;
7254 l_mcc_record->m_offset_array = 00;
7256 l_indix = l_tmp & 0xff;
7258 l_mct_data = l_tcp->m_mct_records;
7259 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7260 if (l_mct_data->m_index == l_indix) {
7261 l_mcc_record->m_decorrelation_array = l_mct_data;
7267 if (l_mcc_record->m_decorrelation_array == 00) {
7268 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7273 l_indix = (l_tmp >> 8) & 0xff;
7275 l_mct_data = l_tcp->m_mct_records;
7276 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7277 if (l_mct_data->m_index == l_indix) {
7278 l_mcc_record->m_offset_array = l_mct_data;
7284 if (l_mcc_record->m_offset_array == 00) {
7285 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7291 if (p_header_size != 0) {
7292 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7296 ++l_tcp->m_nb_mcc_records;
7303 * Writes the MCO marker (Multiple component transformation ordering)
7305 * @param p_stream the stream to write data to.
7306 * @param p_j2k J2K codec.
7307 * @param p_manager the user event manager.
7309 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
7310 struct opj_stream_private *p_stream,
7311 struct opj_event_mgr * p_manager
7314 OPJ_BYTE * l_current_data = 00;
7315 OPJ_UINT32 l_mco_size;
7316 opj_tcp_v2_t * l_tcp = 00;
7317 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7321 assert(p_j2k != 00);
7322 assert(p_manager != 00);
7323 assert(p_stream != 00);
7325 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
7326 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7328 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
7329 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7331 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7332 = (OPJ_BYTE*)opj_realloc(
7333 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7335 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
7340 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
7343 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
7344 l_current_data += 2;
7346 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
7347 l_current_data += 2;
7349 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
7352 l_mcc_record = l_tcp->m_mcc_records;
7353 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
7354 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
7360 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) {
7368 * Reads a MCO marker (Multiple Component Transform Ordering)
7370 * @param p_header_data the data contained in the MCO box.
7371 * @param p_j2k the jpeg2000 codec.
7372 * @param p_header_size the size of the data contained in the MCO marker.
7373 * @param p_manager the user event manager.
7375 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
7376 OPJ_BYTE * p_header_data,
7377 OPJ_UINT32 p_header_size,
7378 struct opj_event_mgr * p_manager )
7380 OPJ_UINT32 l_tmp, i;
7381 OPJ_UINT32 l_nb_stages;
7382 opj_tcp_v2_t * l_tcp;
7383 opj_tccp_t * l_tccp;
7384 opj_image_t * l_image;
7385 opj_image_comp_t * l_img_comp;
7388 assert(p_header_data != 00);
7389 assert(p_j2k != 00);
7390 assert(p_manager != 00);
7392 l_image = p_j2k->m_private_image;
7393 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7394 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7395 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7397 if (p_header_size < 1) {
7398 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
7402 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
7405 if (l_nb_stages > 1) {
7406 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
7410 if (p_header_size != l_nb_stages + 1) {
7411 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
7415 l_tccp = l_tcp->tccps;
7416 l_img_comp = l_image->comps;
7418 for (i=0;i<l_image->numcomps;++i) {
7419 l_tccp->m_dc_level_shift = 0;
7423 if (l_tcp->m_mct_decoding_matrix) {
7424 opj_free(l_tcp->m_mct_decoding_matrix);
7425 l_tcp->m_mct_decoding_matrix = 00;
7428 for (i=0;i<l_nb_stages;++i) {
7429 opj_read_bytes(p_header_data,&l_tmp,1);
7432 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
7440 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
7443 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7444 opj_mct_data_t * l_deco_array, * l_offset_array;
7445 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
7446 OPJ_UINT32 l_nb_elem;
7447 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
7448 opj_tccp_t * l_tccp;
7451 assert(p_tcp != 00);
7453 l_mcc_record = p_tcp->m_mcc_records;
7455 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
7456 if (l_mcc_record->m_index == p_index) {
7461 if (i==p_tcp->m_nb_mcc_records) {
7462 /** element discarded **/
7466 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
7467 /** do not support number of comps != image */
7471 l_deco_array = l_mcc_record->m_decorrelation_array;
7474 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
7475 if (l_deco_array->m_data_size != l_data_size) {
7479 l_nb_elem = p_image->numcomps * p_image->numcomps;
7480 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
7481 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7483 if (! p_tcp->m_mct_decoding_matrix ) {
7487 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);
7490 l_offset_array = l_mcc_record->m_offset_array;
7492 if (l_offset_array) {
7493 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
7494 if (l_offset_array->m_data_size != l_data_size) {
7498 l_nb_elem = p_image->numcomps;
7499 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7500 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7502 if (! l_offset_data ) {
7506 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7508 l_tccp = p_tcp->tccps;
7509 l_current_offset_data = l_offset_data;
7511 for (i=0;i<p_image->numcomps;++i) {
7512 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7516 opj_free(l_offset_data);
7523 * Writes the CBD marker (Component bit depth definition)
7525 * @param p_stream the stream to write data to.
7526 * @param p_j2k J2K codec.
7527 * @param p_manager the user event manager.
7529 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7530 struct opj_stream_private *p_stream,
7531 struct opj_event_mgr * p_manager )
7534 OPJ_UINT32 l_cbd_size;
7535 OPJ_BYTE * l_current_data = 00;
7536 opj_image_t *l_image = 00;
7537 opj_image_comp_t * l_comp = 00;
7540 assert(p_j2k != 00);
7541 assert(p_manager != 00);
7542 assert(p_stream != 00);
7544 l_image = p_j2k->m_private_image;
7545 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7547 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7548 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7549 = (OPJ_BYTE*)opj_realloc(
7550 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7553 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7557 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7560 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7562 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7563 l_current_data += 2;
7565 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7566 l_current_data += 2;
7568 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7571 l_comp = l_image->comps;
7573 for (i=0;i<l_image->numcomps;++i) {
7574 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7580 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) {
7588 * Reads a CBD marker (Component bit depth definition)
7589 * @param p_header_data the data contained in the CBD box.
7590 * @param p_j2k the jpeg2000 codec.
7591 * @param p_header_size the size of the data contained in the CBD marker.
7592 * @param p_manager the user event manager.
7594 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7595 OPJ_BYTE * p_header_data,
7596 OPJ_UINT32 p_header_size,
7597 struct opj_event_mgr * p_manager)
7599 OPJ_UINT32 l_nb_comp,l_num_comp;
7600 OPJ_UINT32 l_comp_def;
7602 opj_image_comp_t * l_comp = 00;
7605 assert(p_header_data != 00);
7606 assert(p_j2k != 00);
7607 assert(p_manager != 00);
7609 l_num_comp = p_j2k->m_private_image->numcomps;
7611 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7612 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7616 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7619 if (l_nb_comp != l_num_comp) {
7620 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7624 l_comp = p_j2k->m_private_image->comps;
7625 for (i=0;i<l_num_comp;++i) {
7626 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7628 l_comp->sgnd = (l_comp_def>>7) & 1;
7629 l_comp->prec = (l_comp_def&0x7f) + 1;
7637 /* ----------------------------------------------------------------------- */
7638 /* J2K / JPT decoder interface */
7639 /* ----------------------------------------------------------------------- */
7641 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
7642 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7646 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
7647 if(!j2k->default_tcp) {
7653 j2k->tile_data = NULL;
7658 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7661 if(j2k->tile_len != NULL) {
7662 opj_free(j2k->tile_len);
7664 if(j2k->tile_data != NULL) {
7665 opj_free(j2k->tile_data);
7667 if(j2k->default_tcp != NULL) {
7668 opj_tcp_t *default_tcp = j2k->default_tcp;
7669 if(default_tcp->ppt_data_first != NULL) {
7670 opj_free(default_tcp->ppt_data_first);
7672 if(j2k->default_tcp->tccps != NULL) {
7673 opj_free(j2k->default_tcp->tccps);
7675 opj_free(j2k->default_tcp);
7677 if(j2k->cp != NULL) {
7678 opj_cp_t *cp = j2k->cp;
7679 if(cp->tcps != NULL) {
7680 for(i = 0; i < cp->tw * cp->th; i++) {
7681 if(cp->tcps[i].ppt_data_first != NULL) {
7682 opj_free(cp->tcps[i].ppt_data_first);
7684 if(cp->tcps[i].tccps != NULL) {
7685 opj_free(cp->tcps[i].tccps);
7690 if(cp->ppm_data_first != NULL) {
7691 opj_free(cp->ppm_data_first);
7693 if(cp->tileno != NULL) {
7694 opj_free(cp->tileno);
7696 if(cp->comment != NULL) {
7697 opj_free(cp->comment);
7705 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
7706 if(j2k && parameters) {
7707 /* create and initialize the coding parameters structure */
7708 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7709 cp->reduce = parameters->cp_reduce;
7710 cp->layer = parameters->cp_layer;
7711 cp->limit_decoding = parameters->cp_limit_decoding;
7714 cp->correct = parameters->jpwl_correct;
7715 cp->exp_comps = parameters->jpwl_exp_comps;
7716 cp->max_tiles = parameters->jpwl_max_tiles;
7717 #endif /* USE_JPWL */
7720 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
7725 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7727 if(j2k && parameters) {
7728 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7729 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7732 j2k->m_cp.correct = parameters->jpwl_correct;
7733 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7734 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7735 #endif /* USE_JPWL */
7739 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7740 opj_image_t *image = NULL;
7742 opj_common_ptr cinfo = j2k->cinfo;
7745 j2k->cstr_info = cstr_info;
7747 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
7749 /* create an empty image */
7750 image = opj_image_create0();
7753 j2k->state = J2K_STATE_MHSOC;
7756 opj_dec_mstabent_t *e;
7757 int id = cio_read(cio, 2);
7760 /* we try to honor JPWL correction power */
7761 if (j2k->cp->correct) {
7763 int orig_pos = cio_tell(cio);
7766 /* call the corrector */
7767 status = jpwl_correct(j2k);
7769 /* go back to where you were */
7770 cio_seek(cio, orig_pos - 2);
7772 /* re-read the marker */
7773 id = cio_read(cio, 2);
7775 /* check whether it begins with ff */
7776 if (id >> 8 != 0xff) {
7777 opj_event_msg(cinfo, EVT_ERROR,
7778 "JPWL: possible bad marker %x at %d\n",
7779 id, cio_tell(cio) - 2);
7781 opj_image_destroy(image);
7782 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
7785 /* we try to correct */
7787 cio_seek(cio, cio_tell(cio) - 2);
7788 cio_write(cio, id, 2);
7789 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
7790 "- setting marker to %x\n",
7795 #endif /* USE_JPWL */
7797 if (id >> 8 != 0xff) {
7798 opj_image_destroy(image);
7799 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7802 e = j2k_dec_mstab_lookup(id);
7803 /* Check if the marker is known*/
7804 if (!(j2k->state & e->states)) {
7805 opj_image_destroy(image);
7806 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7809 /* Check if the decoding is limited to the main header*/
7810 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
7811 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
7818 if (j2k->state & J2K_STATE_ERR)
7821 if (j2k->state == J2K_STATE_MT) {
7824 if (j2k->state == J2K_STATE_NEOC) {
7828 if (j2k->state == J2K_STATE_NEOC) {
7832 if (j2k->state != J2K_STATE_MT) {
7833 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7839 * Read a JPT-stream and decode file
7842 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7843 opj_image_t *image = NULL;
7844 opj_jpt_msg_header_t header;
7846 opj_common_ptr cinfo = j2k->cinfo;
7848 OPJ_ARG_NOT_USED(cstr_info);
7852 /* create an empty image */
7853 image = opj_image_create0();
7856 j2k->state = J2K_STATE_MHSOC;
7858 /* Initialize the header */
7859 jpt_init_msg_header(&header);
7860 /* Read the first header of the message */
7861 jpt_read_msg_header(cinfo, cio, &header);
7863 position = cio_tell(cio);
7864 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7865 opj_image_destroy(image);
7866 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7871 opj_dec_mstabent_t *e = NULL;
7874 if (!cio_numbytesleft(cio)) {
7878 /* data-bin read -> need to read a new header */
7879 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7880 jpt_read_msg_header(cinfo, cio, &header);
7881 position = cio_tell(cio);
7882 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7883 opj_image_destroy(image);
7884 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7889 id = cio_read(cio, 2);
7890 if (id >> 8 != 0xff) {
7891 opj_image_destroy(image);
7892 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7895 e = j2k_dec_mstab_lookup(id);
7896 if (!(j2k->state & e->states)) {
7897 opj_image_destroy(image);
7898 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7904 if (j2k->state == J2K_STATE_MT) {
7907 if (j2k->state == J2K_STATE_NEOC) {
7911 if (j2k->state == J2K_STATE_NEOC) {
7915 if (j2k->state != J2K_STATE_MT) {
7916 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7922 /* ----------------------------------------------------------------------- */
7923 /* J2K encoder interface */
7924 /* ----------------------------------------------------------------------- */
7926 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7927 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7934 opj_j2k_v2_t* j2k_create_compress_v2(void)
7936 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7941 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7943 l_j2k->m_is_decoder = 0;
7944 l_j2k->m_cp.m_is_decoder = 0;
7946 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7947 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7952 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7954 /* validation list creation*/
7955 l_j2k->m_validation_list = opj_procedure_list_create();
7956 if (! l_j2k->m_validation_list) {
7961 /* execution list creation*/
7962 l_j2k->m_procedure_list = opj_procedure_list_create();
7963 if (! l_j2k->m_procedure_list) {
7971 void j2k_destroy_compress(opj_j2k_t *j2k) {
7975 if(j2k->cp != NULL) {
7976 opj_cp_t *cp = j2k->cp;
7979 opj_free(cp->comment);
7982 opj_free(cp->matrice);
7984 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7985 opj_free(cp->tcps[tileno].tccps);
7994 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7996 int tileno, numpocs_tile;
7997 opj_cp_t *cp = NULL;
7999 if(!j2k || !parameters || ! image) {
8003 /* create and initialize the coding parameters structure */
8004 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
8006 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
8009 /* set default values for cp */
8014 copy user encoding parameters
8016 cp->cinema = parameters->cp_cinema;
8017 cp->max_comp_size = parameters->max_comp_size;
8018 cp->rsiz = parameters->cp_rsiz;
8019 cp->disto_alloc = parameters->cp_disto_alloc;
8020 cp->fixed_alloc = parameters->cp_fixed_alloc;
8021 cp->fixed_quality = parameters->cp_fixed_quality;
8023 /* mod fixed_quality */
8024 if(parameters->cp_matrice) {
8025 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
8026 cp->matrice = (int *) opj_malloc(array_size);
8027 memcpy(cp->matrice, parameters->cp_matrice, array_size);
8031 cp->tdx = parameters->cp_tdx;
8032 cp->tdy = parameters->cp_tdy;
8035 cp->tx0 = parameters->cp_tx0;
8036 cp->ty0 = parameters->cp_ty0;
8038 /* comment string */
8039 if(parameters->cp_comment) {
8040 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
8042 strcpy(cp->comment, parameters->cp_comment);
8047 calculate other encoding parameters
8050 if (parameters->tile_size_on) {
8051 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
8052 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8054 cp->tdx = image->x1 - cp->tx0;
8055 cp->tdy = image->y1 - cp->ty0;
8058 if(parameters->tp_on){
8059 cp->tp_flag = parameters->tp_flag;
8064 for(i=0;i<image->numcomps ;i++){
8065 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
8071 calculate JPWL encoding parameters
8074 if (parameters->jpwl_epc_on) {
8078 cp->epc_on = OPJ_TRUE;
8079 cp->info_on = OPJ_FALSE; /* no informative technique */
8082 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8083 cp->epb_on = OPJ_TRUE;
8085 cp->hprot_MH = parameters->jpwl_hprot_MH;
8086 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8087 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8088 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8090 /* if tile specs are not specified, copy MH specs */
8091 if (cp->hprot_TPH[0] == -1) {
8092 cp->hprot_TPH_tileno[0] = 0;
8093 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8095 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8096 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8097 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8098 cp->pprot[i] = parameters->jpwl_pprot[i];
8102 /* set ESD writing */
8103 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8104 cp->esd_on = OPJ_TRUE;
8106 cp->sens_size = parameters->jpwl_sens_size;
8107 cp->sens_addr = parameters->jpwl_sens_addr;
8108 cp->sens_range = parameters->jpwl_sens_range;
8110 cp->sens_MH = parameters->jpwl_sens_MH;
8111 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8112 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8113 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8117 /* always set RED writing to false: we are at the encoder */
8118 cp->red_on = OPJ_FALSE;
8121 cp->epc_on = OPJ_FALSE;
8123 #endif /* USE_JPWL */
8126 /* initialize the mutiple tiles */
8127 /* ---------------------------- */
8128 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
8130 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8131 opj_tcp_t *tcp = &cp->tcps[tileno];
8132 tcp->numlayers = parameters->tcp_numlayers;
8133 assert ( tcp->numlayers >= 0 );
8134 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
8136 if (cp->fixed_quality) {
8137 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8139 tcp->rates[j] = parameters->tcp_rates[j];
8141 if (cp->fixed_quality) { /* add fixed_quality */
8142 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8144 tcp->rates[j] = parameters->tcp_rates[j];
8148 tcp->csty = parameters->csty;
8149 tcp->prg = parameters->prog_order;
8150 tcp->mct = parameters->tcp_mct;
8154 if (parameters->numpocs) {
8155 /* initialisation of POC */
8157 assert( parameters->numpocs >= 0 );
8158 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
8159 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8160 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8161 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
8162 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
8163 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
8164 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
8165 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
8166 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
8167 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
8171 tcp->numpocs = numpocs_tile -1 ;
8176 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8178 for (i = 0; i < image->numcomps; i++) {
8179 opj_tccp_t *tccp = &tcp->tccps[i];
8180 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
8181 tccp->numresolutions = parameters->numresolution;
8182 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8183 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8184 tccp->cblksty = parameters->mode;
8185 tccp->qmfbid = parameters->irreversible ? 0 : 1;
8186 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8188 assert(parameters->roi_compno >= 0);
8189 if (i == (OPJ_UINT32)parameters->roi_compno) {
8190 tccp->roishift = parameters->roi_shift;
8195 if(parameters->cp_cinema)
8197 /*Precinct size for lowest frequency subband=128*/
8200 /*Precinct size at all other resolutions = 256*/
8201 for (j = 1; j < tccp->numresolutions; j++) {
8206 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8208 assert(tccp->numresolutions > 0);
8209 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
8210 if (p < parameters->res_spec) {
8212 if (parameters->prcw_init[p] < 1) {
8215 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
8218 if (parameters->prch_init[p] < 1) {
8221 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
8225 int res_spec = parameters->res_spec;
8226 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8227 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8229 if (size_prcw < 1) {
8232 tccp->prcw[j] = int_floorlog2(size_prcw);
8235 if (size_prch < 1) {
8238 tccp->prch[j] = int_floorlog2(size_prch);
8242 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
8245 for (j = 0; j < tccp->numresolutions; j++) {
8252 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8257 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
8258 opj_cparameters_t *parameters,
8260 struct opj_event_mgr * p_manager)
8262 OPJ_UINT32 i, j, tileno, numpocs_tile;
8263 opj_cp_v2_t *cp = 00;
8266 if(!p_j2k || !parameters || ! image) {
8270 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
8271 cp = &(p_j2k->m_cp);
8273 /* set default values for cp */
8278 copy user encoding parameters
8280 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
8281 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
8282 cp->rsiz = parameters->cp_rsiz;
8283 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
8284 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
8285 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
8287 /* mod fixed_quality */
8288 if (parameters->cp_matrice) {
8289 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
8290 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
8291 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
8295 cp->tdx = parameters->cp_tdx;
8296 cp->tdy = parameters->cp_tdy;
8299 cp->tx0 = parameters->cp_tx0;
8300 cp->ty0 = parameters->cp_ty0;
8302 /* comment string */
8303 if(parameters->cp_comment) {
8304 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
8306 strcpy(cp->comment, parameters->cp_comment);
8311 calculate other encoding parameters
8314 if (parameters->tile_size_on) {
8315 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
8316 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8318 cp->tdx = image->x1 - cp->tx0;
8319 cp->tdy = image->y1 - cp->ty0;
8322 if (parameters->tp_on) {
8323 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
8324 cp->m_specific_param.m_enc.m_tp_on = 1;
8329 calculate JPWL encoding parameters
8332 if (parameters->jpwl_epc_on) {
8336 cp->epc_on = OPJ_TRUE;
8337 cp->info_on = OPJ_FALSE; /* no informative technique */
8340 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8341 cp->epb_on = OPJ_TRUE;
8343 cp->hprot_MH = parameters->jpwl_hprot_MH;
8344 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8345 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8346 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8348 /* if tile specs are not specified, copy MH specs */
8349 if (cp->hprot_TPH[0] == -1) {
8350 cp->hprot_TPH_tileno[0] = 0;
8351 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8353 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8354 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8355 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8356 cp->pprot[i] = parameters->jpwl_pprot[i];
8360 /* set ESD writing */
8361 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8362 cp->esd_on = OPJ_TRUE;
8364 cp->sens_size = parameters->jpwl_sens_size;
8365 cp->sens_addr = parameters->jpwl_sens_addr;
8366 cp->sens_range = parameters->jpwl_sens_range;
8368 cp->sens_MH = parameters->jpwl_sens_MH;
8369 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8370 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8371 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8375 /* always set RED writing to false: we are at the encoder */
8376 cp->red_on = OPJ_FALSE;
8379 cp->epc_on = OPJ_FALSE;
8381 #endif /* USE_JPWL */
8384 /* initialize the mutiple tiles */
8385 /* ---------------------------- */
8386 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
8387 if (parameters->numpocs) {
8388 /* initialisation of POC */
8389 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
8393 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8394 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
8395 tcp->numlayers = parameters->tcp_numlayers;
8397 for (j = 0; j < tcp->numlayers; j++) {
8398 if(cp->m_specific_param.m_enc.m_cinema){
8399 if (cp->m_specific_param.m_enc.m_fixed_quality) {
8400 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8402 tcp->rates[j] = parameters->tcp_rates[j];
8404 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
8405 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8407 tcp->rates[j] = parameters->tcp_rates[j];
8412 tcp->csty = parameters->csty;
8413 tcp->prg = parameters->prog_order;
8414 tcp->mct = parameters->tcp_mct;
8419 if (parameters->numpocs) {
8420 /* initialisation of POC */
8423 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
8424 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8425 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8427 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
8428 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
8429 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
8430 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
8431 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
8432 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
8433 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
8439 tcp->numpocs = numpocs_tile -1 ;
8444 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8446 if (parameters->mct_data) {
8448 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
8451 /* TODO MSD : merge v2 add invert.c or used a external lib ?
8452 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
8453 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8454 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
8457 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8458 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
8459 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
8461 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8462 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
8464 tcp->mct_norms = (OPJ_FLOAT64*)
8465 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
8467 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
8470 for (i = 0; i < image->numcomps; i++) {
8471 opj_tccp_t *tccp = &tcp->tccps[i];
8472 tccp->m_dc_level_shift = l_dc_shift[i];
8475 j2k_setup_mct_encoding(tcp,image);
8479 for (i = 0; i < image->numcomps; i++) {
8480 opj_tccp_t *tccp = &tcp->tccps[i];
8481 opj_image_comp_t * l_comp = &(image->comps[i]);
8483 if (! l_comp->sgnd) {
8484 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
8489 for (i = 0; i < image->numcomps; i++) {
8490 opj_tccp_t *tccp = &tcp->tccps[i];
8492 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
8493 tccp->numresolutions = parameters->numresolution;
8494 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8495 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8496 tccp->cblksty = parameters->mode;
8497 tccp->qmfbid = parameters->irreversible ? 0 : 1;
8498 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8501 if (i == parameters->roi_compno) {
8502 tccp->roishift = parameters->roi_shift;
8507 if(parameters->cp_cinema) {
8508 //Precinct size for lowest frequency subband=128
8511 //Precinct size at all other resolutions = 256
8512 for (j = 1; j < tccp->numresolutions; j++) {
8517 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8518 OPJ_INT32 p = 0, it_res;
8519 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
8520 if (p < parameters->res_spec) {
8522 if (parameters->prcw_init[p] < 1) {
8523 tccp->prcw[it_res] = 1;
8525 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
8528 if (parameters->prch_init[p] < 1) {
8529 tccp->prch[it_res] = 1;
8531 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
8535 int res_spec = parameters->res_spec;
8536 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8537 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8539 if (size_prcw < 1) {
8540 tccp->prcw[it_res] = 1;
8542 tccp->prcw[it_res] = int_floorlog2(size_prcw);
8545 if (size_prch < 1) {
8546 tccp->prch[it_res] = 1;
8548 tccp->prch[it_res] = int_floorlog2(size_prch);
8552 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
8555 for (j = 0; j < tccp->numresolutions; j++) {
8562 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8566 if (parameters->mct_data) {
8567 opj_free(parameters->mct_data);
8568 parameters->mct_data = 00;
8573 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
8576 opj_cp_t *cp = NULL;
8578 opj_tcd_t *tcd = NULL; /* TCD component */
8586 j2k->cstr_info = cstr_info;
8589 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
8590 cstr_info->image_w = image->x1 - image->x0;
8591 cstr_info->image_h = image->y1 - image->y0;
8592 cstr_info->prog = (&cp->tcps[0])->prg;
8593 cstr_info->tw = cp->tw;
8594 cstr_info->th = cp->th;
8595 cstr_info->tile_x = cp->tdx; /* new version parser */
8596 cstr_info->tile_y = cp->tdy; /* new version parser */
8597 cstr_info->tile_Ox = cp->tx0; /* new version parser */
8598 cstr_info->tile_Oy = cp->ty0; /* new version parser */
8599 cstr_info->numcomps = image->numcomps;
8600 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
8601 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
8602 for (compno=0; compno < image->numcomps; compno++) {
8603 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
8605 cstr_info->D_max = 0.0; /* ADD Marcela */
8606 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
8607 cstr_info->maxmarknum = 100;
8608 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
8609 cstr_info->marknum = 0;
8619 for (compno = 1; compno < image->numcomps; compno++) {
8620 j2k_write_coc(j2k, compno);
8621 j2k_write_qcc(j2k, compno);
8625 for (compno = 0; compno < image->numcomps; compno++) {
8626 opj_tcp_t *tcp = &cp->tcps[0];
8627 if (tcp->tccps[compno].roishift)
8628 j2k_write_rgn(j2k, compno, 0);
8630 if (cp->comment != NULL) {
8634 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
8638 if (cp->cinema == CINEMA4K_24) {
8643 /* uncomment only for testing JPSEC marker writing */
8644 /* j2k_write_sec(j2k); */
8648 cstr_info->main_head_end = cio_tell(cio) - 1;
8651 /**** Main Header ENDS here ***/
8653 /* create the tile encoder */
8654 tcd = tcd_create(j2k->cinfo);
8656 /* encode each tile */
8657 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8661 int acc_pack_num = 0;
8665 opj_tcp_t *tcp = &cp->tcps[tileno];
8666 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8668 j2k->curtileno = tileno;
8669 j2k->cur_tp_num = 0;
8670 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8671 /* initialisation before tile encoding */
8673 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8675 tcd_init_encode(tcd, image, cp, j2k->curtileno);
8680 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8681 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8682 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8683 cstr_info->tile[j2k->curtileno].marknum = 0;
8687 for(pino = 0; pino <= tcp->numpocs; pino++) {
8691 /*Get number of tile parts*/
8692 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8693 tcd->tp_pos = cp->tp_pos;
8695 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8696 j2k->tp_num = tilepartno;
8699 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8700 cio_tell(cio) + j2k->pos_correction;
8704 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8705 for (compno = 1; compno < image->numcomps; compno++) {
8706 j2k_write_coc(j2k, compno);
8707 j2k_write_qcc(j2k, compno);
8709 if (cp->tcps[tileno].numpocs) {
8716 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8717 cio_tell(cio) + j2k->pos_correction + 1;
8720 j2k_write_sod(j2k, tcd);
8724 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8725 cio_tell(cio) + j2k->pos_correction - 1;
8726 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8728 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8729 cstr_info->packno - acc_pack_num;
8730 acc_pack_num = cstr_info->packno;
8738 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8743 if (tile->PPT) { // BAD PPT !!!
8746 PPT_file=fopen("PPT","rb");
8747 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8748 for (i=0;i<tile->len_ppt;i++) {
8750 fread(&elmt, 1, 1, PPT_file);
8751 fwrite(&elmt,1,1,f);
8760 /* destroy the tile encoder */
8761 tcd_free_encode(tcd);
8764 opj_free(j2k->cur_totnum_tp);
8769 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8771 /* The following adjustment is done to adjust the codestream size */
8772 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8773 /* the first bunch of bytes is not in the codestream */
8774 cstr_info->codestream_size -= cstr_info->main_head_start;
8780 preparation of JPWL marker segments
8784 /* encode according to JPWL */
8785 jpwl_encode(j2k, cio, image);
8788 #endif /* USE_JPWL */
8793 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8795 assert(cstr_info != 00);
8797 /* expand the list? */
8798 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8799 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8800 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8803 /* add the marker */
8804 cstr_info->marker[cstr_info->marknum].type = type;
8805 cstr_info->marker[cstr_info->marknum].pos = pos;
8806 cstr_info->marker[cstr_info->marknum].len = len;
8807 cstr_info->marknum++;
8811 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8813 assert(cstr_index != 00);
8815 /* expand the list? */
8816 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8817 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8818 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8821 /* add the marker */
8822 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8823 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8824 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8825 cstr_index->marknum++;
8829 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8831 opj_marker_info_t *marker;
8833 assert(cstr_info != 00);
8835 /* expand the list? */
8836 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8837 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8838 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8841 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8843 /* add the marker */
8844 marker->type = type;
8847 cstr_info->tile[tileno].marknum++;
8850 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)
8852 assert(cstr_index != 00);
8853 assert(cstr_index->tile_index != 00);
8855 /* expand the list? */
8856 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8857 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8858 cstr_index->tile_index[tileno].marker =
8859 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8860 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8863 /* add the marker */
8864 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8865 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8866 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8867 cstr_index->tile_index[tileno].marknum++;
8869 if (type == J2K_MS_SOT) {
8870 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8872 if (cstr_index->tile_index[tileno].tp_index)
8873 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8880 * -----------------------------------------------------------------------
8881 * -----------------------------------------------------------------------
8882 * -----------------------------------------------------------------------
8886 * Ends the decompression procedures and possibiliy add data to be read after the
8889 opj_bool j2k_end_decompress(
8890 opj_j2k_v2_t *p_j2k,
8891 opj_stream_private_t *p_stream,
8892 opj_event_mgr_t * p_manager)
8901 * Reads a jpeg2000 codestream header structure.
8904 * @param p_stream the stream to read data from.
8905 * @param p_j2k the jpeg2000 codec.
8906 * @param p_manager the user event manager.
8908 * @return true if the box is valid.
8910 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8911 opj_j2k_v2_t* p_j2k,
8912 opj_image_t** p_image,
8913 struct opj_event_mgr* p_manager )
8916 assert(p_j2k != 00);
8917 assert(p_stream != 00);
8918 assert(p_manager != 00);
8920 /* create an empty image header */
8921 p_j2k->m_private_image = opj_image_create0();
8922 if (! p_j2k->m_private_image) {
8926 /* customization of the validation */
8927 j2k_setup_decoding_validation(p_j2k);
8929 /* validation of the parameters codec */
8930 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8931 opj_image_destroy(p_j2k->m_private_image);
8932 p_j2k->m_private_image = NULL;
8936 /* customization of the encoding */
8937 j2k_setup_header_reading(p_j2k);
8940 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8941 opj_image_destroy(p_j2k->m_private_image);
8942 p_j2k->m_private_image = NULL;
8946 *p_image = opj_image_create0();
8951 /* Copy codestream image information to the output image */
8952 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8959 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8961 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8964 assert(p_j2k != 00);
8966 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8968 /* DEVELOPER CORNER, add your custom procedures */
8969 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8974 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8975 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8977 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8980 assert(p_j2k != 00);
8982 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8983 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8984 /* DEVELOPER CORNER, add your custom validation procedure */
8990 * The mct encoding validation procedure.
8992 * @param p_j2k the jpeg2000 codec to validate.
8993 * @param p_stream the input stream to validate.
8994 * @param p_manager the user event manager.
8996 * @return true if the parameters are correct.
8998 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8999 opj_stream_private_t *p_stream,
9000 opj_event_mgr_t * p_manager )
9002 opj_bool l_is_valid = OPJ_TRUE;
9006 assert(p_j2k != 00);
9007 assert(p_stream != 00);
9008 assert(p_manager != 00);
9010 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
9011 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9012 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
9014 for (i=0;i<l_nb_tiles;++i) {
9015 if (l_tcp->mct == 2) {
9016 opj_tccp_t * l_tccp = l_tcp->tccps;
9017 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
9019 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
9020 l_is_valid &= ! (l_tccp->qmfbid & 1);
9031 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
9034 OPJ_UINT32 l_indix = 1;
9035 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
9036 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
9037 OPJ_UINT32 l_mct_size,l_nb_elem;
9038 OPJ_FLOAT32 * l_data, * l_current_data;
9039 opj_tccp_t * l_tccp;
9042 assert(p_tcp != 00);
9044 if (p_tcp->mct != 2) {
9048 if (p_tcp->m_mct_decoding_matrix) {
9049 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
9050 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
9052 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));
9053 if (! p_tcp->m_mct_records) {
9056 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9058 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
9060 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9062 if (l_mct_deco_data->m_data) {
9063 opj_free(l_mct_deco_data->m_data);
9064 l_mct_deco_data->m_data = 00;
9067 l_mct_deco_data->m_index = l_indix++;
9068 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
9069 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
9070 l_nb_elem = p_image->numcomps * p_image->numcomps;
9071 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
9072 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
9074 if (! l_mct_deco_data->m_data) {
9078 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);
9080 l_mct_deco_data->m_data_size = l_mct_size;
9081 ++p_tcp->m_nb_mct_records;
9084 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
9085 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
9086 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));
9088 if (! p_tcp->m_mct_records) {
9092 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9093 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
9095 if (l_mct_deco_data) {
9096 l_mct_deco_data = l_mct_offset_data - 1;
9100 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9102 if (l_mct_offset_data->m_data) {
9103 opj_free(l_mct_offset_data->m_data);
9104 l_mct_offset_data->m_data = 00;
9107 l_mct_offset_data->m_index = l_indix++;
9108 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
9109 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
9110 l_nb_elem = p_image->numcomps;
9111 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
9112 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
9114 if (! l_mct_offset_data->m_data) {
9118 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
9120 opj_free(l_mct_offset_data->m_data);
9121 l_mct_offset_data->m_data = 00;
9125 l_tccp = p_tcp->tccps;
9126 l_current_data = l_data;
9128 for (i=0;i<l_nb_elem;++i) {
9129 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
9133 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
9137 l_mct_offset_data->m_data_size = l_mct_size;
9139 ++p_tcp->m_nb_mct_records;
9141 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
9142 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
9143 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
9144 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
9146 if (! p_tcp->m_mcc_records) {
9149 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
9150 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));
9154 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
9155 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
9156 l_mcc_data->m_is_irreversible = 1;
9157 l_mcc_data->m_nb_comps = p_image->numcomps;
9158 l_mcc_data->m_index = l_indix++;
9159 l_mcc_data->m_offset_array = l_mct_offset_data;
9160 ++p_tcp->m_nb_mcc_records;
9166 * Builds the cp decoder parameters to use to decode tile.
9168 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
9169 opj_stream_private_t *p_stream,
9170 opj_event_mgr_t * p_manager )
9172 /* add here initialization of cp
9173 copy paste of setup_decoder */
9181 * Builds the cp encoder parameters to use to encode tile.
9183 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
9184 opj_stream_private_t *p_stream,
9185 opj_event_mgr_t * p_manager )
9187 /* add here initialization of cp
9188 copy paste of setup_encoder */
9196 * The default encoding validation procedure without any extension.
9198 * @param p_j2k the jpeg2000 codec to validate.
9199 * @param p_stream the input stream to validate.
9200 * @param p_manager the user event manager.
9202 * @return true if the parameters are correct.
9204 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
9205 opj_stream_private_t *p_stream,
9206 opj_event_mgr_t * p_manager )
9208 opj_bool l_is_valid = OPJ_TRUE;
9211 assert(p_j2k != 00);
9212 assert(p_stream != 00);
9213 assert(p_manager != 00);
9215 /* STATE checking */
9216 /* make sure the state is at 0 */
9217 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
9219 /* POINTER validation */
9220 /* make sure a p_j2k codec is present */
9221 l_is_valid &= (p_j2k->m_procedure_list != 00);
9222 /* make sure a validation list is present */
9223 l_is_valid &= (p_j2k->m_validation_list != 00);
9225 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
9226 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
9230 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
9231 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
9235 /* PARAMETER VALIDATION */
9240 * The default decoding validation procedure without any extension.
9242 * @param p_j2k the jpeg2000 codec to validate.
9243 * @param p_stream the input stream to validate.
9244 * @param p_manager the user event manager.
9246 * @return true if the parameters are correct.
9248 opj_bool j2k_decoding_validation (
9249 opj_j2k_v2_t *p_j2k,
9250 opj_stream_private_t *p_stream,
9251 opj_event_mgr_t * p_manager
9254 opj_bool l_is_valid = OPJ_TRUE;
9257 assert(p_j2k != 00);
9258 assert(p_stream != 00);
9259 assert(p_manager != 00);
9262 /* STATE checking */
9263 /* make sure the state is at 0 */
9265 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
9267 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
9269 /* POINTER validation */
9270 /* make sure a p_j2k codec is present */
9271 /* make sure a procedure list is present */
9272 l_is_valid &= (p_j2k->m_procedure_list != 00);
9273 /* make sure a validation list is present */
9274 l_is_valid &= (p_j2k->m_validation_list != 00);
9276 /* PARAMETER VALIDATION */
9280 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
9281 struct opj_stream_private *p_stream,
9282 struct opj_event_mgr * p_manager)
9284 OPJ_UINT32 l_current_marker;
9285 OPJ_UINT32 l_marker_size;
9286 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9289 assert(p_stream != 00);
9290 assert(p_j2k != 00);
9291 assert(p_manager != 00);
9293 /* We enter in the main header */
9294 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
9296 /* Try to read the SOC marker, the codestream must begin with SOC marker */
9297 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
9298 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
9302 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9303 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9304 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9308 /* Read 2 bytes as the new marker ID */
9309 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9311 /* Try to read until the SOT is detected */
9312 while (l_current_marker != J2K_MS_SOT) {
9314 /* Check if the current marker ID is valid */
9315 if (l_current_marker < 0xff00) {
9316 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
9320 /* Get the marker handler from the marker ID */
9321 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9323 /* Manage case where marker is unknown */
9324 if (l_marker_handler->id == J2K_MS_UNK) {
9325 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
9326 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
9330 if (l_current_marker == J2K_MS_SOT)
9331 break; /* SOT marker is detected main header is completely read */
9332 else /* Get the marker handler from the marker ID */
9333 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9336 /* Check if the marker is known and if it is the right place to find it */
9337 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9338 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9342 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9343 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9344 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9348 /* read 2 bytes as the marker size */
9349 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9350 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9352 /* Check if the marker size is compatible with the header data size */
9353 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9354 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9355 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9356 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9359 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9362 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9363 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) {
9364 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9368 /* Read the marker segment with the correct marker handler */
9369 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9370 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
9374 /* Add the marker to the codestream index*/
9375 j2k_add_mhmarker_v2(p_j2k->cstr_index,
9376 l_marker_handler->id,
9377 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9378 l_marker_size + 4 );
9380 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9381 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9382 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9386 /* read 2 bytes as the new marker ID */
9387 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9390 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
9392 /* Position of the last element if the main header */
9393 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
9395 /* Next step: read a tile-part header */
9396 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9402 * Excutes the given procedures on the given codec.
9404 * @param p_procedure_list the list of procedures to execute
9405 * @param p_j2k the jpeg2000 codec to execute the procedures on.
9406 * @param p_stream the stream to execute the procedures on.
9407 * @param p_manager the user manager.
9409 * @return true if all the procedures were successfully executed.
9411 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
9412 opj_procedure_list_t * p_procedure_list,
9413 opj_stream_private_t *p_stream,
9414 opj_event_mgr_t * p_manager )
9416 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
9417 opj_bool l_result = OPJ_TRUE;
9418 OPJ_UINT32 l_nb_proc, i;
9421 assert(p_procedure_list != 00);
9422 assert(p_j2k != 00);
9423 assert(p_stream != 00);
9424 assert(p_manager != 00);
9427 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
9428 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);
9430 for (i=0;i<l_nb_proc;++i) {
9431 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
9435 /* and clear the procedure list at the end.*/
9436 opj_procedure_list_clear(p_procedure_list);
9441 opj_bool j2k_copy_default_tcp_and_create_tcd
9443 opj_j2k_v2_t * p_j2k,
9444 opj_stream_private_t *p_stream,
9445 opj_event_mgr_t * p_manager
9448 opj_tcp_v2_t * l_tcp = 00;
9449 opj_tcp_v2_t * l_default_tcp = 00;
9450 OPJ_UINT32 l_nb_tiles;
9452 opj_tccp_t *l_current_tccp = 00;
9453 OPJ_UINT32 l_tccp_size;
9454 OPJ_UINT32 l_mct_size;
9455 opj_image_t * l_image;
9456 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
9457 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
9458 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
9459 OPJ_UINT32 l_offset;
9462 assert(p_j2k != 00);
9463 assert(p_stream != 00);
9464 assert(p_manager != 00);
9466 l_image = p_j2k->m_private_image;
9467 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9468 l_tcp = p_j2k->m_cp.tcps;
9469 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
9470 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9471 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
9474 for (i=0; i<l_nb_tiles; ++i) {
9475 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
9476 l_current_tccp = l_tcp->tccps;
9477 /*Copy default coding parameters into the current tile coding parameters*/
9478 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
9479 /* Initialize some values of the current tile coding parameters*/
9481 l_tcp->ppt_data = 00;
9482 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
9483 l_tcp->tccps = l_current_tccp;
9485 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
9486 if (l_default_tcp->m_mct_decoding_matrix) {
9487 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
9488 if (! l_tcp->m_mct_decoding_matrix ) {
9491 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
9494 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
9495 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
9496 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
9497 if (! l_tcp->m_mct_records) {
9500 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
9502 /* Copy the mct record data from dflt_tile_cp to the current tile*/
9503 l_src_mct_rec = l_default_tcp->m_mct_records;
9504 l_dest_mct_rec = l_tcp->m_mct_records;
9506 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
9508 if (l_src_mct_rec->m_data) {
9510 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
9511 if(! l_dest_mct_rec->m_data) {
9514 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
9521 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
9522 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
9523 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
9524 if (! l_tcp->m_mcc_records) {
9527 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
9529 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
9530 l_src_mcc_rec = l_default_tcp->m_mcc_records;
9531 l_dest_mcc_rec = l_tcp->m_mcc_records;
9533 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
9535 if (l_src_mcc_rec->m_decorrelation_array) {
9536 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
9537 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
9540 if (l_src_mcc_rec->m_offset_array) {
9541 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
9542 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
9549 /* Copy all the dflt_tile_compo_cp to the current tile cp */
9550 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
9552 /* Move to next tile cp*/
9556 /* Create the current tile decoder*/
9557 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
9558 if (! p_j2k->m_tcd ) {
9562 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
9563 tcd_destroy_v2(p_j2k->m_tcd);
9565 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9573 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
9574 * with the marker value.
9575 * @param p_id Marker value to look up
9577 * @return the handler associated with the id.
9579 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
9581 const opj_dec_memory_marker_handler_t *e;
9582 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
9583 if (e->id == p_id) {
9584 break; /* we find a handler corresponding to the marker ID*/
9592 * Destroys a jpeg2000 codec.
9594 * @param p_j2k the jpeg20000 structure to destroy.
9596 void j2k_destroy (opj_j2k_v2_t *p_j2k)
9602 if (p_j2k->m_is_decoder) {
9604 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
9605 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9606 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9607 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
9610 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
9611 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
9612 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
9613 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
9618 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
9619 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9620 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
9623 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
9624 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
9625 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
9626 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
9629 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
9630 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
9631 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
9632 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
9636 tcd_destroy_v2(p_j2k->m_tcd);
9638 j2k_cp_destroy(&(p_j2k->m_cp));
9639 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
9641 opj_procedure_list_destroy(p_j2k->m_procedure_list);
9642 p_j2k->m_procedure_list = 00;
9644 opj_procedure_list_destroy(p_j2k->m_validation_list);
9645 p_j2k->m_procedure_list = 00;
9647 j2k_destroy_cstr_index(p_j2k->cstr_index);
9648 p_j2k->cstr_index = NULL;
9650 opj_image_destroy(p_j2k->m_private_image);
9651 p_j2k->m_private_image = NULL;
9653 opj_image_destroy(p_j2k->m_output_image);
9654 p_j2k->m_output_image = NULL;
9659 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9663 if (p_cstr_ind->marker) {
9664 opj_free(p_cstr_ind->marker);
9665 p_cstr_ind->marker = NULL;
9668 if (p_cstr_ind->tile_index) {
9669 OPJ_UINT32 it_tile = 0;
9671 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9673 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9674 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9675 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9678 if(p_cstr_ind->tile_index[it_tile].tp_index){
9679 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9680 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9683 if(p_cstr_ind->tile_index[it_tile].marker){
9684 opj_free(p_cstr_ind->tile_index[it_tile].marker);
9685 p_cstr_ind->tile_index[it_tile].marker = NULL;
9690 opj_free( p_cstr_ind->tile_index);
9691 p_cstr_ind->tile_index = NULL;
9694 opj_free(p_cstr_ind);
9701 * Destroys a tile coding parameter structure.
9703 * @param p_tcp the tile coding parameter to destroy.
9705 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9711 if (p_tcp->ppt_buffer != 00) {
9712 opj_free(p_tcp->ppt_buffer);
9713 p_tcp->ppt_buffer = 00;
9716 if (p_tcp->tccps != 00) {
9717 opj_free(p_tcp->tccps);
9721 if (p_tcp->m_mct_coding_matrix != 00) {
9722 opj_free(p_tcp->m_mct_coding_matrix);
9723 p_tcp->m_mct_coding_matrix = 00;
9726 if (p_tcp->m_mct_decoding_matrix != 00) {
9727 opj_free(p_tcp->m_mct_decoding_matrix);
9728 p_tcp->m_mct_decoding_matrix = 00;
9731 if (p_tcp->m_mcc_records) {
9732 opj_free(p_tcp->m_mcc_records);
9733 p_tcp->m_mcc_records = 00;
9734 p_tcp->m_nb_max_mcc_records = 0;
9735 p_tcp->m_nb_mcc_records = 0;
9738 if (p_tcp->m_mct_records) {
9739 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9742 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9743 if (l_mct_data->m_data) {
9744 opj_free(l_mct_data->m_data);
9745 l_mct_data->m_data = 00;
9751 opj_free(p_tcp->m_mct_records);
9752 p_tcp->m_mct_records = 00;
9755 if (p_tcp->mct_norms != 00) {
9756 opj_free(p_tcp->mct_norms);
9757 p_tcp->mct_norms = 00;
9760 j2k_tcp_data_destroy(p_tcp);
9765 * Destroys the data inside a tile coding parameter structure.
9767 * @param p_tcp the tile coding parameter which contain data to destroy.
9769 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9771 if (p_tcp->m_data) {
9772 opj_free(p_tcp->m_data);
9773 p_tcp->m_data = NULL;
9774 p_tcp->m_data_size = 0;
9779 * Destroys a coding parameter structure.
9781 * @param p_cp the coding parameter to destroy.
9783 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9785 OPJ_UINT32 l_nb_tiles;
9786 opj_tcp_v2_t * l_current_tile = 00;
9797 l_current_tile = p_cp->tcps;
9798 l_nb_tiles = p_cp->th * p_cp->tw;
9801 (i = 0; i < l_nb_tiles; ++i)
9803 j2k_tcp_destroy(l_current_tile);
9806 opj_free(p_cp->tcps);
9810 (p_cp->ppm_buffer != 00)
9812 opj_free(p_cp->ppm_buffer);
9813 p_cp->ppm_buffer = 00;
9816 (p_cp->comment != 00)
9818 opj_free(p_cp->comment);
9822 (! p_cp->m_is_decoder)
9825 (p_cp->m_specific_param.m_enc.m_matrice)
9827 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9828 p_cp->m_specific_param.m_enc.m_matrice = 00;
9836 * Reads a tile header.
9837 * @param p_j2k the jpeg2000 codec.
9838 * @param p_stream the stream to write data to.
9839 * @param p_manager the user event manager.
9841 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9842 OPJ_UINT32 * p_tile_index,
9843 OPJ_UINT32 * p_data_size,
9844 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9845 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9846 OPJ_UINT32 * p_nb_comps,
9848 opj_stream_private_t *p_stream,
9849 opj_event_mgr_t * p_manager )
9851 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9852 OPJ_UINT32 l_marker_size;
9853 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9854 opj_tcp_v2_t * l_tcp = NULL;
9855 OPJ_UINT32 l_nb_tiles;
9858 assert(p_stream != 00);
9859 assert(p_j2k != 00);
9860 assert(p_manager != 00);
9862 /* Reach the End Of Codestream ?*/
9863 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9864 l_current_marker = J2K_MS_EOC;
9866 /* We need to encounter a SOT marker (a new tile-part header) */
9867 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9871 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9872 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9874 /* Try to read until the Start Of Data is detected */
9875 while (l_current_marker != J2K_MS_SOD) {
9877 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9878 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9879 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9883 /* Read 2 bytes from the buffer as the marker size */
9884 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9886 /* Why this condition? FIXME */
9887 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9888 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9890 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9892 /* Get the marker handler from the marker ID */
9893 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9895 /* Check if the marker is known and if it is the right place to find it */
9896 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9897 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9900 /* FIXME manage case of unknown marker as in the main header ? */
9902 /* Check if the marker size is compatible with the header data size */
9903 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9904 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9905 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9906 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9909 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9912 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9913 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) {
9914 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9918 /* Read the marker segment with the correct marker handler */
9919 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9920 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9924 /* Add the marker to the codestream index*/
9925 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9927 l_marker_handler->id,
9928 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9929 l_marker_size + 4 );
9931 /* Keep the position of the last SOT marker read */
9932 if ( l_marker_handler->id == J2K_MS_SOT ) {
9933 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9934 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9936 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9941 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9942 /* Skip the rest of the tile part header*/
9943 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) {
9944 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9947 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9950 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9951 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9952 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9955 /* Read 2 bytes from the buffer as the new marker ID */
9956 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9960 /* If we didn't skip data before, we need to read the SOD marker*/
9961 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9962 /* Try to read the SOD marker and skip data ? FIXME */
9963 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
9969 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9970 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9971 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9972 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9976 /* Read 2 bytes from buffer as the new marker ID */
9977 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9981 /* Indicate we will try to read a new tile-part header*/
9982 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9983 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9984 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9986 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9987 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9988 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9992 /* Read 2 bytes from buffer as the new marker ID */
9993 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9997 /* Current marker is the EOC marker ?*/
9998 if (l_current_marker == J2K_MS_EOC) {
9999 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
10000 p_j2k->m_current_tile_number = 0;
10001 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
10006 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
10007 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
10008 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10010 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
10011 ++p_j2k->m_current_tile_number;
10015 if (p_j2k->m_current_tile_number == l_nb_tiles) {
10016 *p_go_on = OPJ_FALSE;
10022 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
10023 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
10027 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
10028 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
10030 *p_tile_index = p_j2k->m_current_tile_number;
10031 *p_go_on = OPJ_TRUE;
10032 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
10033 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
10034 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
10035 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
10036 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
10037 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
10039 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
10045 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
10046 OPJ_UINT32 p_tile_index,
10048 OPJ_UINT32 p_data_size,
10049 opj_stream_private_t *p_stream,
10050 opj_event_mgr_t * p_manager )
10052 OPJ_UINT32 l_current_marker;
10053 OPJ_BYTE l_data [2];
10054 opj_tcp_v2_t * l_tcp;
10056 /* preconditions */
10057 assert(p_stream != 00);
10058 assert(p_j2k != 00);
10059 assert(p_manager != 00);
10061 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
10062 || (p_tile_index != p_j2k->m_current_tile_number) ) {
10066 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
10067 if (! l_tcp->m_data) {
10068 j2k_tcp_destroy(l_tcp);
10072 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
10074 l_tcp->m_data_size,
10076 p_j2k->cstr_index) ) {
10077 j2k_tcp_destroy(l_tcp);
10078 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
10082 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
10086 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
10087 * we destroy just the data which will be re-read in read_tile_header*/
10088 /*j2k_tcp_destroy(l_tcp);
10089 p_j2k->m_tcd->tcp = 0;*/
10090 j2k_tcp_data_destroy(l_tcp);
10092 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
10093 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
10095 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
10096 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
10097 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
10101 opj_read_bytes(l_data,&l_current_marker,2);
10103 if (l_current_marker == J2K_MS_EOC) {
10104 p_j2k->m_current_tile_number = 0;
10105 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
10107 else if (l_current_marker != J2K_MS_SOT)
10109 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
10118 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
10120 OPJ_UINT32 i,j,k = 0;
10121 OPJ_UINT32 l_width_src,l_height_src;
10122 OPJ_UINT32 l_width_dest,l_height_dest;
10123 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
10124 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
10125 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
10126 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
10127 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
10129 opj_image_comp_t * l_img_comp_src = 00;
10130 opj_image_comp_t * l_img_comp_dest = 00;
10132 opj_tcd_tilecomp_v2_t * l_tilec = 00;
10133 opj_image_t * l_image_src = 00;
10134 OPJ_UINT32 l_size_comp, l_remaining;
10135 OPJ_INT32 * l_dest_ptr;
10136 opj_tcd_resolution_v2_t* l_res= 00;
10138 l_tilec = p_tcd->tcd_image->tiles->comps;
10139 l_image_src = p_tcd->image;
10140 l_img_comp_src = l_image_src->comps;
10142 l_img_comp_dest = p_output_image->comps;
10144 for (i=0; i<l_image_src->numcomps; i++) {
10146 /* Allocate output component buffer if necessary */
10147 if (!l_img_comp_dest->data) {
10149 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
10150 if (! l_img_comp_dest->data) {
10155 /* Copy info from decoded comp image to output image */
10156 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
10159 /* Compute the precision of the output buffer */
10160 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
10161 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
10162 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
10168 if (l_size_comp == 3) {
10173 /* Current tile component size*/
10175 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
10176 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
10179 l_width_src = (l_res->x1 - l_res->x0);
10180 l_height_src = (l_res->y1 - l_res->y0);
10182 /* Border of the current output component*/
10183 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
10184 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
10185 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
10186 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
10189 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
10190 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
10194 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
10195 * of the input buffer (decoded tile component) which will be move
10196 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
10197 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
10198 * by this input area.
10200 assert( l_res->x0 >= 0);
10201 assert( l_res->x1 >= 0);
10202 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
10203 l_start_x_dest = l_res->x0 - l_x0_dest;
10204 l_offset_x0_src = 0;
10206 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
10207 l_width_dest = l_width_src;
10208 l_offset_x1_src = 0;
10211 l_width_dest = l_x1_dest - l_res->x0 ;
10212 l_offset_x1_src = l_width_src - l_width_dest;
10216 l_start_x_dest = 0 ;
10217 l_offset_x0_src = l_x0_dest - l_res->x0;
10219 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
10220 l_width_dest = l_width_src - l_offset_x0_src;
10221 l_offset_x1_src = 0;
10224 l_width_dest = l_img_comp_dest->w ;
10225 l_offset_x1_src = l_res->x1 - l_x1_dest;
10229 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
10230 l_start_y_dest = l_res->y0 - l_y0_dest;
10231 l_offset_y0_src = 0;
10233 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
10234 l_height_dest = l_height_src;
10235 l_offset_y1_src = 0;
10238 l_height_dest = l_y1_dest - l_res->y0 ;
10239 l_offset_y1_src = l_height_src - l_height_dest;
10243 l_start_y_dest = 0 ;
10244 l_offset_y0_src = l_y0_dest - l_res->y0;
10246 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
10247 l_height_dest = l_height_src - l_offset_y0_src;
10248 l_offset_y1_src = 0;
10251 l_height_dest = l_img_comp_dest->h ;
10252 l_offset_y1_src = l_res->y1 - l_y1_dest;
10256 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
10261 /* Compute the input buffer offset */
10262 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
10263 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
10264 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
10266 /* Compute the output buffer offset */
10267 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
10268 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
10270 /* Move the output buffer to the first place where we will write*/
10271 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
10274 fprintf(stdout, "COMPO[%d]:\n",i);
10275 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
10276 "\t tile offset:%d, %d, %d, %d\n"
10277 "\t buffer offset: %d; %d, %d\n",
10278 l_res->x0, l_res->y0, l_width_src, l_height_src,
10279 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
10280 l_start_offset_src, l_line_offset_src, l_end_offset_src);
10282 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
10283 "\t start offset: %d, line offset= %d\n",
10284 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
10288 switch (l_size_comp) {
10291 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
10292 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
10294 if (l_img_comp_src->sgnd) {
10295 for (j = 0 ; j < l_height_dest ; ++j) {
10296 for ( k = 0 ; k < l_width_dest ; ++k) {
10297 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
10300 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
10301 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
10305 for ( j = 0 ; j < l_height_dest ; ++j ) {
10306 for ( k = 0 ; k < l_width_dest ; ++k) {
10307 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
10310 l_dest_ptr+= l_line_offset_dest;
10311 l_src_ptr += l_line_offset_src;
10315 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
10316 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
10321 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
10322 l_src_ptr += l_start_offset_src;
10324 if (l_img_comp_src->sgnd) {
10325 for (j=0;j<l_height_dest;++j) {
10326 for (k=0;k<l_width_dest;++k) {
10327 *(l_dest_ptr++) = *(l_src_ptr++);
10330 l_dest_ptr+= l_line_offset_dest;
10331 l_src_ptr += l_line_offset_src ;
10335 for (j=0;j<l_height_dest;++j) {
10336 for (k=0;k<l_width_dest;++k) {
10337 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10340 l_dest_ptr+= l_line_offset_dest;
10341 l_src_ptr += l_line_offset_src ;
10345 l_src_ptr += l_end_offset_src;
10346 p_data = (OPJ_BYTE*) l_src_ptr;
10351 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
10352 l_src_ptr += l_start_offset_src;
10354 for (j=0;j<l_height_dest;++j) {
10355 for (k=0;k<l_width_dest;++k) {
10356 *(l_dest_ptr++) = (*(l_src_ptr++));
10359 l_dest_ptr+= l_line_offset_dest;
10360 l_src_ptr += l_line_offset_src ;
10363 l_src_ptr += l_end_offset_src;
10364 p_data = (OPJ_BYTE*) l_src_ptr;
10378 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
10380 * @param p_j2k the jpeg2000 codec.
10381 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
10382 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
10383 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
10384 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
10385 * @param p_manager the user event manager
10387 * @return true if the area could be set.
10389 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
10390 opj_image_t* p_image,
10391 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
10392 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
10393 struct opj_event_mgr * p_manager )
10395 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
10396 opj_image_t * l_image = p_j2k->m_private_image;
10398 OPJ_UINT32 it_comp;
10399 OPJ_INT32 l_comp_x1, l_comp_y1;
10400 opj_image_comp_t* l_img_comp = NULL;
10402 /* Check if we are read the main header */
10403 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
10404 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
10408 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
10409 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
10411 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10412 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10413 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10414 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10420 /* Check if the positions provided by the user are correct */
10423 assert(p_start_x >= 0 );
10424 assert(p_start_y >= 0 );
10426 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
10427 opj_event_msg_v2(p_manager, EVT_ERROR,
10428 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
10429 p_start_x, l_image->x1);
10432 else if ((OPJ_UINT32)p_start_x < l_image->x0){
10433 opj_event_msg_v2(p_manager, EVT_WARNING,
10434 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
10435 p_start_x, l_image->x0);
10436 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10437 p_image->x0 = l_image->x0;
10440 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
10441 p_image->x0 = p_start_x;
10445 if ((OPJ_UINT32)p_start_y > l_image->y1){
10446 opj_event_msg_v2(p_manager, EVT_ERROR,
10447 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
10448 p_start_y, l_image->y1);
10451 else if ((OPJ_UINT32)p_start_y < l_image->y0){
10452 opj_event_msg_v2(p_manager, EVT_WARNING,
10453 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
10454 p_start_y, l_image->y0);
10455 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10456 p_image->y0 = l_image->y0;
10459 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
10460 p_image->y0 = p_start_y;
10464 assert((OPJ_UINT32)p_end_x > 0);
10465 assert((OPJ_UINT32)p_end_y > 0);
10466 if ((OPJ_UINT32)p_end_x < l_image->x0) {
10467 opj_event_msg_v2(p_manager, EVT_ERROR,
10468 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
10469 p_end_x, l_image->x0);
10472 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
10473 opj_event_msg_v2(p_manager, EVT_WARNING,
10474 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
10475 p_end_x, l_image->x1);
10476 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10477 p_image->x1 = l_image->x1;
10480 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
10481 p_image->x1 = p_end_x;
10485 if ((OPJ_UINT32)p_end_y < l_image->y0) {
10486 opj_event_msg_v2(p_manager, EVT_ERROR,
10487 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
10488 p_end_y, l_image->y0);
10491 if ((OPJ_UINT32)p_end_y > l_image->y1){
10492 opj_event_msg_v2(p_manager, EVT_WARNING,
10493 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
10494 p_end_y, l_image->y1);
10495 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10496 p_image->y1 = l_image->y1;
10499 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
10500 p_image->y1 = p_end_y;
10504 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
10506 l_img_comp = p_image->comps;
10507 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
10511 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10512 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10513 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10514 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10516 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
10517 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10519 opj_event_msg_v2(p_manager, EVT_ERROR,
10520 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
10524 l_img_comp->w = l_w;
10526 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
10527 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10529 opj_event_msg_v2(p_manager, EVT_ERROR,
10530 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
10534 l_img_comp->h = l_h;
10539 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
10540 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
10547 /* ----------------------------------------------------------------------- */
10548 /* J2K / JPT decoder interface */
10549 /* ----------------------------------------------------------------------- */
10551 * Creates a J2K decompression structure.
10553 * @return a handle to a J2K decompressor if successful, NULL otherwise.
10555 opj_j2k_v2_t* j2k_create_decompress_v2(void)
10557 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
10561 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
10563 l_j2k->m_is_decoder = 1;
10564 l_j2k->m_cp.m_is_decoder = 1;
10566 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
10567 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
10568 j2k_destroy(l_j2k);
10571 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
10573 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
10574 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
10575 j2k_destroy(l_j2k);
10579 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
10581 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
10583 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
10585 /* codestream index creation */
10586 l_j2k->cstr_index = j2k_create_cstr_index();
10588 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
10589 if (!l_j2k->cstr_index){
10590 j2k_destroy(l_j2k);
10594 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
10597 /* validation list creation */
10598 l_j2k->m_validation_list = opj_procedure_list_create();
10599 if (! l_j2k->m_validation_list) {
10600 j2k_destroy(l_j2k);
10604 /* execution list creation */
10605 l_j2k->m_procedure_list = opj_procedure_list_create();
10606 if (! l_j2k->m_procedure_list) {
10607 j2k_destroy(l_j2k);
10615 opj_codestream_index_t* j2k_create_cstr_index(void)
10617 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
10618 opj_calloc(1,sizeof(opj_codestream_index_t));
10622 cstr_index->maxmarknum = 100;
10623 cstr_index->marknum = 0;
10624 cstr_index->marker = (opj_marker_info_t*)
10625 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
10626 if (!cstr_index-> marker)
10629 cstr_index->tile_index = NULL;
10636 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
10638 * @param p_tile_no the tile index.
10639 * @param p_comp_no the component being outputted.
10640 * @param p_j2k the J2K codec.
10642 * @return the number of bytes taken by the SPCod element.
10644 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
10645 OPJ_UINT32 p_tile_no,
10646 OPJ_UINT32 p_comp_no )
10648 opj_cp_v2_t *l_cp = 00;
10649 opj_tcp_v2_t *l_tcp = 00;
10650 opj_tccp_t *l_tccp = 00;
10652 /* preconditions */
10653 assert(p_j2k != 00);
10655 l_cp = &(p_j2k->m_cp);
10656 l_tcp = &l_cp->tcps[p_tile_no];
10657 l_tccp = &l_tcp->tccps[p_comp_no];
10659 /* preconditions again */
10660 assert(p_tile_no < (l_cp->tw * l_cp->th));
10661 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10663 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10664 return 5 + l_tccp->numresolutions;
10672 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10674 * @param p_comp_no the component number to output.
10675 * @param p_stream the stream to write data to.
10676 * @param p_j2k J2K codec.
10677 * @param p_manager the user event manager.
10680 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10681 OPJ_UINT32 p_tile_no,
10682 OPJ_UINT32 p_comp_no,
10684 OPJ_UINT32 * p_header_size,
10685 struct opj_event_mgr * p_manager )
10688 opj_cp_v2_t *l_cp = 00;
10689 opj_tcp_v2_t *l_tcp = 00;
10690 opj_tccp_t *l_tccp = 00;
10692 /* preconditions */
10693 assert(p_j2k != 00);
10694 assert(p_header_size != 00);
10695 assert(p_manager != 00);
10696 assert(p_data != 00);
10698 l_cp = &(p_j2k->m_cp);
10699 l_tcp = &l_cp->tcps[p_tile_no];
10700 l_tccp = &l_tcp->tccps[p_comp_no];
10702 /* preconditions again */
10703 assert(p_tile_no < (l_cp->tw * l_cp->th));
10704 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10706 if (*p_header_size < 5) {
10707 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10711 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
10714 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
10717 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
10720 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
10723 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
10726 *p_header_size = *p_header_size - 5;
10728 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10730 if (*p_header_size < l_tccp->numresolutions) {
10731 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10735 for (i = 0; i < l_tccp->numresolutions; ++i) {
10736 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
10740 *p_header_size = *p_header_size - l_tccp->numresolutions;
10747 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10748 * @param p_header_data the data contained in the COM box.
10749 * @param p_j2k the jpeg2000 codec.
10750 * @param p_header_size the size of the data contained in the COM marker.
10751 * @param p_manager the user event manager.
10753 opj_bool j2k_read_SPCod_SPCoc(
10754 opj_j2k_v2_t *p_j2k,
10756 OPJ_BYTE * p_header_data,
10757 OPJ_UINT32 * p_header_size,
10758 struct opj_event_mgr * p_manager
10761 OPJ_UINT32 i, l_tmp;
10762 opj_cp_v2_t *l_cp = NULL;
10763 opj_tcp_v2_t *l_tcp = NULL;
10764 opj_tccp_t *l_tccp = NULL;
10765 OPJ_BYTE * l_current_ptr = NULL;
10767 /* preconditions */
10768 assert(p_j2k != 00);
10769 assert(p_manager != 00);
10770 assert(p_header_data != 00);
10772 l_cp = &(p_j2k->m_cp);
10773 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10774 &l_cp->tcps[p_j2k->m_current_tile_number] :
10775 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10777 /* precondition again */
10778 assert(compno < p_j2k->m_private_image->numcomps);
10780 l_tccp = &l_tcp->tccps[compno];
10781 l_current_ptr = p_header_data;
10783 /* make sure room is sufficient */
10784 if (*p_header_size < 5) {
10785 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10789 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10790 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10793 /* If user wants to remove more resolutions than the codestream contains, return error */
10794 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10795 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10796 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10797 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10801 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10803 l_tccp->cblkw += 2;
10805 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10807 l_tccp->cblkh += 2;
10809 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10812 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10815 *p_header_size = *p_header_size - 5;
10817 /* use custom precinct size ? */
10818 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10819 if (*p_header_size < l_tccp->numresolutions) {
10820 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10824 for (i = 0; i < l_tccp->numresolutions; ++i) {
10825 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10827 l_tccp->prcw[i] = l_tmp & 0xf;
10828 l_tccp->prch[i] = l_tmp >> 4;
10831 *p_header_size = *p_header_size - l_tccp->numresolutions;
10834 /* set default size for the precinct width and height */
10835 for (i = 0; i < l_tccp->numresolutions; ++i) {
10836 l_tccp->prcw[i] = 15;
10837 l_tccp->prch[i] = 15;
10841 #ifdef WIP_REMOVE_MSD
10843 if (p_j2k->cstr_info && compno == 0) {
10844 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10846 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10847 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10848 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10849 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10850 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10853 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10854 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10863 * Copies the tile component parameters of all the component from the first tile component.
10865 * @param p_j2k the J2k codec.
10867 void j2k_copy_tile_component_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, *l_copied_tccp = NULL;
10874 OPJ_UINT32 l_prc_size;
10876 /* preconditions */
10877 assert(p_j2k != 00);
10879 l_cp = &(p_j2k->m_cp);
10880 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10881 &l_cp->tcps[p_j2k->m_current_tile_number] :
10882 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10884 l_ref_tccp = &l_tcp->tccps[0];
10885 l_copied_tccp = l_ref_tccp + 1;
10886 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10888 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10889 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10890 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10891 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10892 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10893 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10894 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10895 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10901 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10903 * @param p_tile_no the tile index.
10904 * @param p_comp_no the component being outputted.
10905 * @param p_j2k the J2K codec.
10907 * @return the number of bytes taken by the SPCod element.
10909 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10910 OPJ_UINT32 p_tile_no,
10911 OPJ_UINT32 p_comp_no )
10913 OPJ_UINT32 l_num_bands;
10915 opj_cp_v2_t *l_cp = 00;
10916 opj_tcp_v2_t *l_tcp = 00;
10917 opj_tccp_t *l_tccp = 00;
10919 /* preconditions */
10920 assert(p_j2k != 00);
10922 l_cp = &(p_j2k->m_cp);
10923 l_tcp = &l_cp->tcps[p_tile_no];
10924 l_tccp = &l_tcp->tccps[p_comp_no];
10926 /* preconditions again */
10927 assert(p_tile_no < l_cp->tw * l_cp->th);
10928 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10930 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10932 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10933 return 1 + l_num_bands;
10936 return 1 + 2*l_num_bands;
10941 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10943 * @param p_tile_no the tile to output.
10944 * @param p_comp_no the component number to output.
10945 * @param p_data the data buffer.
10946 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10947 * @param p_j2k J2K codec.
10948 * @param p_manager the user event manager.
10951 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10952 OPJ_UINT32 p_tile_no,
10953 OPJ_UINT32 p_comp_no,
10955 OPJ_UINT32 * p_header_size,
10956 struct opj_event_mgr * p_manager )
10958 OPJ_UINT32 l_header_size;
10959 OPJ_UINT32 l_band_no, l_num_bands;
10960 OPJ_UINT32 l_expn,l_mant;
10962 opj_cp_v2_t *l_cp = 00;
10963 opj_tcp_v2_t *l_tcp = 00;
10964 opj_tccp_t *l_tccp = 00;
10966 /* preconditions */
10967 assert(p_j2k != 00);
10968 assert(p_header_size != 00);
10969 assert(p_manager != 00);
10970 assert(p_data != 00);
10972 l_cp = &(p_j2k->m_cp);
10973 l_tcp = &l_cp->tcps[p_tile_no];
10974 l_tccp = &l_tcp->tccps[p_comp_no];
10976 /* preconditions again */
10977 assert(p_tile_no < l_cp->tw * l_cp->th);
10978 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10980 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10982 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10983 l_header_size = 1 + l_num_bands;
10985 if (*p_header_size < l_header_size) {
10986 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10990 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10993 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10994 l_expn = l_tccp->stepsizes[l_band_no].expn;
10995 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
11000 l_header_size = 1 + 2*l_num_bands;
11002 if (*p_header_size < l_header_size) {
11003 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
11007 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
11010 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
11011 l_expn = l_tccp->stepsizes[l_band_no].expn;
11012 l_mant = l_tccp->stepsizes[l_band_no].mant;
11014 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
11019 *p_header_size = *p_header_size - l_header_size;
11025 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
11027 * @param p_comp_no the component being targeted.
11028 * @param p_header_data the data contained in the COM box.
11029 * @param p_j2k the jpeg2000 codec.
11030 * @param p_header_size the size of the data contained in the COM marker.
11031 * @param p_manager the user event manager.
11033 opj_bool j2k_read_SQcd_SQcc(
11034 opj_j2k_v2_t *p_j2k,
11035 OPJ_UINT32 p_comp_no,
11036 OPJ_BYTE* p_header_data,
11037 OPJ_UINT32 * p_header_size,
11038 struct opj_event_mgr * p_manager
11042 OPJ_UINT32 l_band_no;
11043 opj_cp_v2_t *l_cp = 00;
11044 opj_tcp_v2_t *l_tcp = 00;
11045 opj_tccp_t *l_tccp = 00;
11046 OPJ_BYTE * l_current_ptr = 00;
11047 OPJ_UINT32 l_tmp, l_num_band;
11050 assert(p_j2k != 00);
11051 assert(p_manager != 00);
11052 assert(p_header_data != 00);
11054 l_cp = &(p_j2k->m_cp);
11055 /* come from tile part header or main header ?*/
11056 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
11057 &l_cp->tcps[p_j2k->m_current_tile_number] :
11058 p_j2k->m_specific_param.m_decoder.m_default_tcp;
11060 /* precondition again*/
11061 assert(p_comp_no < p_j2k->m_private_image->numcomps);
11063 l_tccp = &l_tcp->tccps[p_comp_no];
11064 l_current_ptr = p_header_data;
11066 if (*p_header_size < 1) {
11067 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
11070 *p_header_size -= 1;
11072 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
11075 l_tccp->qntsty = l_tmp & 0x1f;
11076 l_tccp->numgbits = l_tmp >> 5;
11077 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
11081 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
11083 (*p_header_size) / 2;
11085 if( l_num_band > J2K_MAXBANDS ) {
11086 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
11087 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
11088 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
11089 /*return OPJ_FALSE;*/
11094 if (l_cp->correct) {
11096 /* if JPWL is on, we check whether there are too many subbands */
11097 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
11098 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
11099 "JPWL: bad number of subbands in Sqcx (%d)\n",
11101 if (!JPWL_ASSUME) {
11102 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
11105 /* we try to correct */
11107 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
11108 "- setting number of bands to %d => HYPOTHESIS!!!\n",
11113 #endif /* USE_JPWL */
11115 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
11116 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
11117 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
11119 if (l_band_no < J2K_MAXBANDS){
11120 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
11121 l_tccp->stepsizes[l_band_no].mant = 0;
11124 *p_header_size = *p_header_size - l_num_band;
11127 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
11128 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
11130 if (l_band_no < J2K_MAXBANDS){
11131 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
11132 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
11135 *p_header_size = *p_header_size - 2*l_num_band;
11138 /* Add Antonin : if scalar_derived -> compute other stepsizes */
11139 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
11140 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
11141 l_tccp->stepsizes[l_band_no].expn =
11142 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
11143 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
11144 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
11152 * Copies the tile component parameters of all the component from the first tile component.
11154 * @param p_j2k the J2k codec.
11156 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
11159 opj_cp_v2_t *l_cp = NULL;
11160 opj_tcp_v2_t *l_tcp = NULL;
11161 opj_tccp_t *l_ref_tccp = NULL;
11162 opj_tccp_t *l_copied_tccp = NULL;
11165 /* preconditions */
11166 assert(p_j2k != 00);
11168 l_cp = &(p_j2k->m_cp);
11169 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
11170 &l_cp->tcps[p_j2k->m_current_tile_number] :
11171 p_j2k->m_specific_param.m_decoder.m_default_tcp;
11173 l_ref_tccp = &l_tcp->tccps[0];
11174 l_copied_tccp = l_ref_tccp + 1;
11175 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
11177 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
11178 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
11179 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
11180 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
11186 * Dump some elements from the J2K decompression structure .
11188 *@param p_j2k the jpeg2000 codec.
11189 *@param flag flag to describe what elments are dump.
11190 *@param out_stream output stream where dump the elements.
11193 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
11195 /* Check if the flag is compatible with j2k file*/
11196 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
11197 fprintf(out_stream, "Wrong flag\n");
11201 /* Dump the image_header */
11202 if (flag & OPJ_IMG_INFO){
11203 if (p_j2k->m_private_image)
11204 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
11207 /* Dump the codestream info from main header */
11208 if (flag & OPJ_J2K_MH_INFO){
11209 j2k_dump_MH_info(p_j2k, out_stream);
11213 /* Dump the codestream info of the current tile */
11214 if (flag & OPJ_J2K_TH_INFO){
11218 /* Dump the codestream index from main header */
11219 if (flag & OPJ_J2K_MH_IND){
11220 j2k_dump_MH_index(p_j2k, out_stream);
11223 /* Dump the codestream index of the current tile */
11224 if (flag & OPJ_J2K_TH_IND){
11231 * Dump index elements of the codestream extract from the main header.
11233 *@param p_j2k the jpeg2000 codec.
11234 *@param out_stream output stream where dump the elements.
11237 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11239 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
11240 OPJ_UINT32 it_marker, it_tile, it_tile_part;
11242 fprintf(out_stream, "Codestream index from main header: {\n");
11244 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
11245 "\t Main header end position=%" PRIi64 "\n",
11246 cstr_index->main_head_start, cstr_index->main_head_end);
11248 fprintf(out_stream, "\t Marker list: {\n");
11250 if (cstr_index->marker){
11251 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
11252 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
11253 cstr_index->marker[it_marker].type,
11254 cstr_index->marker[it_marker].pos,
11255 cstr_index->marker[it_marker].len );
11259 fprintf(out_stream, "\t }\n");
11262 if (cstr_index->tile_index){
11263 fprintf(out_stream, "\t Tile index: {\n");
11265 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
11266 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
11268 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
11270 if (cstr_index->tile_index[it_tile].tp_index){
11271 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
11272 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
11274 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
11275 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
11276 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
11280 if (cstr_index->tile_index[it_tile].marker){
11281 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
11282 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
11283 cstr_index->tile_index[it_tile].marker[it_marker].type,
11284 cstr_index->tile_index[it_tile].marker[it_marker].pos,
11285 cstr_index->tile_index[it_tile].marker[it_marker].len );
11289 fprintf(out_stream,"\t }\n");
11292 fprintf(out_stream,"}\n");
11297 * Dump info elements of the codestream extract from the main header.
11299 *@param p_j2k the jpeg2000 codec.
11300 *@param out_stream output stream where dump the elements.
11303 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11305 opj_tcp_v2_t * l_default_tile=NULL;
11307 fprintf(out_stream, "Codestream info from main header: {\n");
11309 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
11310 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
11311 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
11313 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11314 if (l_default_tile)
11317 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
11319 fprintf(out_stream, "\t default tile {\n");
11320 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
11321 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
11322 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
11323 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
11325 for (compno = 0; compno < numcomps; compno++) {
11326 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11328 OPJ_INT32 bandno, numbands;
11331 fprintf(out_stream, "\t\t comp %d {\n", compno);
11332 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
11333 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
11334 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
11335 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
11336 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
11337 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
11339 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
11340 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
11341 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
11343 fprintf(out_stream, "\n");
11345 /* quantization style*/
11346 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
11347 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
11348 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
11349 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11350 for (bandno = 0; bandno < numbands; bandno++) {
11351 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
11352 l_tccp->stepsizes[bandno].expn);
11354 fprintf(out_stream, "\n");
11357 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
11359 fprintf(out_stream, "\t\t }\n");
11360 } /*end of component of default tile*/
11361 fprintf(out_stream, "\t }\n"); /*end of default tile*/
11365 fprintf(out_stream, "}\n");
11370 * Dump an image header structure.
11372 *@param img_header the image header to dump.
11373 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11374 *@param out_stream output stream where dump the elements.
11376 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
11380 if (dev_dump_flag){
11381 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
11385 fprintf(out_stream, "Image info {\n");
11386 tab[0] = '\t';tab[1] = '\0';
11389 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
11390 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
11391 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
11393 if (img_header->comps){
11395 for (compno = 0; compno < img_header->numcomps; compno++) {
11396 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
11397 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
11398 fprintf(out_stream,"%s}\n",tab);
11402 fprintf(out_stream, "}\n");
11406 * Dump a component image header structure.
11408 *@param comp_header the component image header to dump.
11409 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11410 *@param out_stream output stream where dump the elements.
11412 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
11416 if (dev_dump_flag){
11417 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
11420 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
11423 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
11424 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
11425 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
11428 fprintf(out_stream, "}\n");
11433 * Get the codestream info from a JPEG2000 codec.
11435 *@param p_j2k the component image header to dump.
11437 *@return the codestream information extract from the jpg2000 codec
11439 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
11442 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
11443 opj_tcp_v2_t *l_default_tile;
11444 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
11446 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
11448 cstr_info->tx0 = p_j2k->m_cp.tx0;
11449 cstr_info->ty0 = p_j2k->m_cp.ty0;
11450 cstr_info->tdx = p_j2k->m_cp.tdx;
11451 cstr_info->tdy = p_j2k->m_cp.tdy;
11452 cstr_info->tw = p_j2k->m_cp.tw;
11453 cstr_info->th = p_j2k->m_cp.th;
11455 cstr_info->tile_info = NULL; /* Not fill from the main header*/
11457 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11459 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
11460 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
11461 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
11462 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
11464 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
11466 for (compno = 0; compno < numcomps; compno++) {
11467 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11468 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
11469 OPJ_INT32 bandno, numbands;
11472 l_tccp_info->csty = l_tccp->csty;
11473 l_tccp_info->numresolutions = l_tccp->numresolutions;
11474 l_tccp_info->cblkw = l_tccp->cblkw;
11475 l_tccp_info->cblkh = l_tccp->cblkh;
11476 l_tccp_info->cblksty = l_tccp->cblksty;
11477 l_tccp_info->qmfbid = l_tccp->qmfbid;
11478 if (l_tccp->numresolutions < J2K_MAXRLVLS)
11480 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
11481 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
11484 /* quantization style*/
11485 l_tccp_info->qntsty = l_tccp->qntsty;
11486 l_tccp_info->numgbits = l_tccp->numgbits;
11488 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11489 if (numbands < J2K_MAXBANDS) {
11490 for (bandno = 0; bandno < numbands; bandno++) {
11491 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
11492 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
11497 l_tccp_info->roishift = l_tccp->roishift;
11505 * Get the codestream index from a JPEG2000 codec.
11507 *@param p_j2k the component image header to dump.
11509 *@return the codestream index extract from the jpg2000 codec
11511 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
11513 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
11514 opj_calloc(1,sizeof(opj_codestream_index_t));
11518 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
11519 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
11520 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
11522 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
11523 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
11524 if (!l_cstr_index->marker){
11525 opj_free( l_cstr_index);
11529 if (p_j2k->cstr_index->marker)
11530 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
11532 opj_free(l_cstr_index->marker);
11533 l_cstr_index->marker = NULL;
11536 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
11537 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
11538 if (!l_cstr_index->tile_index){
11539 opj_free( l_cstr_index->marker);
11540 opj_free( l_cstr_index);
11544 if (!p_j2k->cstr_index->tile_index){
11545 opj_free(l_cstr_index->tile_index);
11546 l_cstr_index->tile_index = NULL;
11549 OPJ_UINT32 it_tile = 0;
11550 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
11553 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
11555 l_cstr_index->tile_index[it_tile].marker =
11556 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
11558 if (!l_cstr_index->tile_index[it_tile].marker) {
11559 OPJ_UINT32 it_tile_free;
11561 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11562 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11565 opj_free( l_cstr_index->tile_index);
11566 opj_free( l_cstr_index->marker);
11567 opj_free( l_cstr_index);
11571 if (p_j2k->cstr_index->tile_index[it_tile].marker)
11572 memcpy( l_cstr_index->tile_index[it_tile].marker,
11573 p_j2k->cstr_index->tile_index[it_tile].marker,
11574 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
11576 opj_free(l_cstr_index->tile_index[it_tile].marker);
11577 l_cstr_index->tile_index[it_tile].marker = NULL;
11580 /* Tile part index*/
11581 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
11583 l_cstr_index->tile_index[it_tile].tp_index =
11584 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
11586 if(!l_cstr_index->tile_index[it_tile].tp_index){
11587 OPJ_UINT32 it_tile_free;
11589 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11590 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11591 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
11594 opj_free( l_cstr_index->tile_index);
11595 opj_free( l_cstr_index->marker);
11596 opj_free( l_cstr_index);
11600 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
11601 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
11602 p_j2k->cstr_index->tile_index[it_tile].tp_index,
11603 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
11606 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
11607 l_cstr_index->tile_index[it_tile].tp_index = NULL;
11610 /* Packet index (NOT USED)*/
11611 l_cstr_index->tile_index[it_tile].nb_packet = 0;
11612 l_cstr_index->tile_index[it_tile].packet_index = NULL;
11617 return l_cstr_index;
11620 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
11622 OPJ_UINT32 it_tile=0;
11624 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
11625 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
11626 if (!p_j2k->cstr_index->tile_index)
11629 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
11630 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
11631 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
11632 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
11633 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
11634 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11644 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
11645 opj_stream_private_t *p_stream,
11646 opj_event_mgr_t * p_manager)
11648 opj_bool l_go_on = OPJ_TRUE;
11649 OPJ_UINT32 l_current_tile_no;
11650 OPJ_UINT32 l_data_size,l_max_data_size;
11651 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11652 OPJ_UINT32 l_nb_comps;
11653 OPJ_BYTE * l_current_data;
11655 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11656 if (! l_current_data) {
11659 l_max_data_size = 1000;
11661 /*Allocate and initialize some elements of codestrem index*/
11662 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11663 opj_free(l_current_data);
11668 if (! j2k_read_tile_header( p_j2k,
11669 &l_current_tile_no,
11671 &l_tile_x0, &l_tile_y0,
11672 &l_tile_x1, &l_tile_y1,
11677 opj_free(l_current_data);
11685 if (l_data_size > l_max_data_size) {
11686 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11687 if (! l_current_data) {
11688 opj_free(l_current_data);
11692 l_max_data_size = l_data_size;
11695 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11696 opj_free(l_current_data);
11699 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);
11701 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11702 opj_free(l_current_data);
11705 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11709 opj_free(l_current_data);
11715 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11717 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11720 assert(p_j2k != 00);
11722 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11723 /* DEVELOPER CORNER, add your custom procedures */
11728 * Read and decode one tile.
11730 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
11731 opj_stream_private_t *p_stream,
11732 opj_event_mgr_t * p_manager)
11734 opj_bool l_go_on = OPJ_TRUE;
11735 OPJ_UINT32 l_current_tile_no;
11736 OPJ_UINT32 l_tile_no_to_dec;
11737 OPJ_UINT32 l_data_size,l_max_data_size;
11738 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11739 OPJ_UINT32 l_nb_comps;
11740 OPJ_BYTE * l_current_data;
11742 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11743 if (! l_current_data) {
11746 l_max_data_size = 1000;
11748 /*Allocate and initialize some elements of codestrem index if not already done*/
11749 if( !p_j2k->cstr_index->tile_index)
11751 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11752 opj_free(l_current_data);
11756 /* Move into the codestream to the first SOT used to decode the desired tile */
11757 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11758 if (p_j2k->cstr_index->tile_index)
11759 if(p_j2k->cstr_index->tile_index->tp_index)
11761 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11762 /* the index for this tile has not been built,
11763 * so move to the last SOT read */
11764 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11765 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11770 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)) {
11771 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11775 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11776 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11777 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11781 if (! j2k_read_tile_header( p_j2k,
11782 &l_current_tile_no,
11784 &l_tile_x0, &l_tile_y0,
11785 &l_tile_x1, &l_tile_y1,
11790 opj_free(l_current_data);
11799 if (l_data_size > l_max_data_size) {
11800 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11801 if (! l_current_data) {
11802 opj_free(l_current_data);
11806 l_max_data_size = l_data_size;
11811 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11812 opj_free(l_current_data);
11815 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);
11817 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11818 opj_free(l_current_data);
11821 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11823 if(l_current_tile_no == l_tile_no_to_dec)
11825 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11826 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11827 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11833 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);
11838 opj_free(l_current_data);
11845 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11847 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11850 assert(p_j2k != 00);
11852 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11853 /* DEVELOPER CORNER, add your custom procedures */
11859 * Decodes the tiles of the stream.
11861 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
11862 opj_stream_private_t * p_stream,
11863 opj_image_t * p_image,
11864 opj_event_mgr_t * p_manager)
11871 p_j2k->m_output_image = opj_image_create0();
11872 if (! (p_j2k->m_output_image)) {
11875 opj_copy_image_header(p_image, p_j2k->m_output_image);
11877 /* customization of the decoding */
11878 j2k_setup_decoding(p_j2k);
11880 /* Decode the codestream */
11881 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11882 opj_image_destroy(p_j2k->m_private_image);
11883 p_j2k->m_private_image = NULL;
11887 /* Move data and copy one information from codec to output image*/
11888 for (compno = 0; compno < p_image->numcomps; compno++) {
11889 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11890 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11891 p_j2k->m_output_image->comps[compno].data = NULL;
11899 * Get the decoded tile.
11901 * @param p_j2k the jpeg2000 codestream codec.
11902 * @param p_stream input_stream
11903 * @param p_image output image. .
11904 * @param p_manager the user event manager
11905 * @param tile_index index of the tile we want decode
11907 * @return true if succeed.
11909 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11910 opj_stream_private_t *p_stream,
11911 opj_image_t* p_image,
11912 struct opj_event_mgr * p_manager,
11913 OPJ_UINT32 tile_index )
11916 OPJ_UINT32 l_tile_x, l_tile_y;
11917 opj_image_comp_t* l_img_comp;
11920 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11924 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11925 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);
11929 /* Compute the dimension of the desired tile*/
11930 l_tile_x = tile_index % p_j2k->m_cp.tw;
11931 l_tile_y = tile_index / p_j2k->m_cp.tw;
11933 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11934 if (p_image->x0 < p_j2k->m_private_image->x0)
11935 p_image->x0 = p_j2k->m_private_image->x0;
11936 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11937 if (p_image->x1 > p_j2k->m_private_image->x1)
11938 p_image->x1 = p_j2k->m_private_image->x1;
11940 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11941 if (p_image->y0 < p_j2k->m_private_image->y0)
11942 p_image->y0 = p_j2k->m_private_image->y0;
11943 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11944 if (p_image->y1 > p_j2k->m_private_image->y1)
11945 p_image->y1 = p_j2k->m_private_image->y1;
11947 l_img_comp = p_image->comps;
11948 for (compno=0; compno < p_image->numcomps; ++compno)
11950 OPJ_INT32 l_comp_x1, l_comp_y1;
11952 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11954 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11955 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11956 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11957 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11959 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11960 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11965 /* Destroy the previous output image*/
11966 if (p_j2k->m_output_image)
11967 opj_image_destroy(p_j2k->m_output_image);
11969 /* Create the ouput image from the information previously computed*/
11970 p_j2k->m_output_image = opj_image_create0();
11971 if (! (p_j2k->m_output_image)) {
11974 opj_copy_image_header(p_image, p_j2k->m_output_image);
11976 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11978 /* customization of the decoding */
11979 j2k_setup_decoding_tile(p_j2k);
11981 /* Decode the codestream */
11982 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11983 opj_image_destroy(p_j2k->m_private_image);
11984 p_j2k->m_private_image = NULL;
11988 /* Move data and copy one information from codec to output image*/
11989 for (compno = 0; compno < p_image->numcomps; compno++) {
11990 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11992 if (p_image->comps[compno].data)
11993 opj_free(p_image->comps[compno].data);
11995 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11997 p_j2k->m_output_image->comps[compno].data = NULL;
12003 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
12005 OPJ_UINT32 it_comp;
12007 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
12009 if (p_j2k->m_private_image) {
12010 if (p_j2k->m_private_image->comps) {
12011 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
12012 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
12013 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
12014 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
12015 if ( res_factor >= max_res){
12016 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
12019 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
12032 * Encodes all the tiles in a row.
12034 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
12035 opj_stream_private_t *p_stream,
12036 opj_event_mgr_t * p_manager )
12039 OPJ_UINT32 l_nb_tiles;
12040 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
12041 OPJ_BYTE * l_current_data;
12043 /* preconditions */
12044 assert(p_j2k != 00);
12045 assert(p_stream != 00);
12046 assert(p_manager != 00);
12048 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
12049 if (! l_current_data) {
12052 l_max_tile_size = 1000;
12054 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
12055 for (i=0;i<l_nb_tiles;++i) {
12056 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
12057 opj_free(l_current_data);
12061 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
12062 if (l_current_tile_size > l_max_tile_size) {
12063 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
12064 if (! l_current_data) {
12067 l_max_tile_size = l_current_tile_size;
12070 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
12072 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
12077 opj_free(l_current_data);
12082 * Ends the compression procedures and possibility add data to be read after the
12085 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
12086 opj_stream_private_t *p_stream,
12087 struct opj_event_mgr * p_manager)
12089 /* customization of the encoding */
12090 j2k_setup_end_compress(p_j2k);
12092 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
12102 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
12104 * @param p_j2k the jpeg2000 codec.
12105 * @param p_stream the stream object.
12106 * @param p_manager the user event manager.
12108 * @return true if the codec is valid.
12110 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
12111 opj_stream_private_t *p_stream,
12112 opj_image_t * p_image,
12113 opj_event_mgr_t * p_manager)
12115 /* preconditions */
12116 assert(p_j2k != 00);
12117 assert(p_stream != 00);
12118 assert(p_manager != 00);
12120 p_j2k->m_private_image = opj_image_create0();
12121 opj_copy_image_header(p_image, p_j2k->m_private_image);
12123 // TODO_MSD: Find a better way
12124 if (p_image->comps) {
12125 OPJ_UINT32 it_comp;
12126 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
12127 if (p_image->comps[it_comp].data) {
12128 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
12129 p_image->comps[it_comp].data = NULL;
12135 /* customization of the validation */
12136 j2k_setup_encoding_validation (p_j2k);
12138 /* validation of the parameters codec */
12139 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
12143 /* customization of the encoding */
12144 j2k_setup_header_writting(p_j2k);
12147 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
12157 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
12158 OPJ_UINT32 p_tile_index,
12159 opj_stream_private_t *p_stream,
12160 opj_event_mgr_t * p_manager )
12163 if (p_tile_index != p_j2k->m_current_tile_number) {
12164 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
12168 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);
12170 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
12171 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
12172 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12174 /* initialisation before tile encoding */
12175 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
12185 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
12187 OPJ_UINT32 i,j,k = 0;
12188 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
12189 opj_image_comp_t * l_img_comp = 00;
12190 opj_tcd_tilecomp_v2_t * l_tilec = 00;
12191 opj_image_t * l_image = 00;
12192 OPJ_UINT32 l_size_comp, l_remaining;
12193 OPJ_INT32 * l_src_ptr;
12194 l_tilec = p_tcd->tcd_image->tiles->comps;
12195 l_image = p_tcd->image;
12196 l_img_comp = l_image->comps;
12198 for (i=0;i<p_tcd->image->numcomps;++i) {
12199 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
12200 l_remaining = l_img_comp->prec & 7; /* (%8) */
12205 if (l_size_comp == 3) {
12209 l_width = (l_tilec->x1 - l_tilec->x0);
12210 l_height = (l_tilec->y1 - l_tilec->y0);
12211 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
12212 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
12213 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
12214 l_stride = l_image_width - l_width;
12215 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
12217 switch (l_size_comp) {
12220 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
12221 if (l_img_comp->sgnd) {
12222 for (j=0;j<l_height;++j) {
12223 for (k=0;k<l_width;++k) {
12224 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
12228 l_src_ptr += l_stride;
12232 for (j=0;j<l_height;++j) {
12233 for (k=0;k<l_width;++k) {
12234 *(l_dest_ptr) = (*l_src_ptr)&0xff;
12238 l_src_ptr += l_stride;
12242 p_data = (OPJ_BYTE*) l_dest_ptr;
12247 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
12248 if (l_img_comp->sgnd) {
12249 for (j=0;j<l_height;++j) {
12250 for (k=0;k<l_width;++k) {
12251 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
12253 l_src_ptr += l_stride;
12257 for (j=0;j<l_height;++j) {
12258 for (k=0;k<l_width;++k) {
12259 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
12261 l_src_ptr += l_stride;
12265 p_data = (OPJ_BYTE*) l_dest_ptr;
12270 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
12271 for (j=0;j<l_height;++j) {
12272 for (k=0;k<l_width;++k) {
12273 *(l_dest_ptr++) = *(l_src_ptr++);
12275 l_src_ptr += l_stride;
12278 p_data = (OPJ_BYTE*) l_dest_ptr;
12291 * @param p_j2k the jpeg2000 codec.
12292 * @param p_stream the stream to write data to.
12293 * @param p_manager the user event manager.
12295 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
12297 OPJ_UINT32 p_data_size,
12298 opj_stream_private_t *p_stream,
12299 opj_event_mgr_t * p_manager )
12301 opj_tcd_v2_t * l_tcd = 00;
12302 opj_cp_v2_t * l_cp = 00;
12303 opj_tcp_v2_t * l_tcp = 00;
12304 OPJ_UINT32 l_nb_bytes_written;
12305 OPJ_BYTE * l_current_data = 00;
12306 OPJ_UINT32 l_tile_size = 0;
12307 OPJ_UINT32 l_available_data;
12309 /* preconditions */
12310 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12312 l_tcd = p_j2k->m_tcd;
12313 l_cp = &(p_j2k->m_cp);
12314 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12316 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
12317 l_available_data = l_tile_size;
12318 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
12320 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
12321 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
12325 l_nb_bytes_written = 0;
12326 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12329 l_current_data += l_nb_bytes_written;
12330 l_available_data -= l_nb_bytes_written;
12332 l_nb_bytes_written = 0;
12333 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12337 l_available_data -= l_nb_bytes_written;
12338 l_nb_bytes_written = l_tile_size - l_available_data;
12340 if ( opj_stream_write_data( p_stream,
12341 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
12342 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
12346 ++p_j2k->m_current_tile_number;
12353 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12354 * are valid. Developers wanting to extend the library can add their own validation procedures.
12356 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
12358 /* preconditions */
12359 assert(p_j2k != 00);
12361 /* DEVELOPER CORNER, insert your custom procedures */
12362 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
12364 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12365 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
12368 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
12369 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
12370 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
12374 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12375 * are valid. Developers wanting to extend the library can add their own validation procedures.
12377 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
12379 /* preconditions */
12380 assert(p_j2k != 00);
12382 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
12383 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
12385 /* DEVELOPER CORNER, add your custom validation procedure */
12386 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
12391 * Sets up the procedures to do on writing header.
12392 * Developers wanting to extend the library can add their own writing procedures.
12394 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
12396 /* preconditions */
12397 assert(p_j2k != 00);
12399 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
12400 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
12401 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
12402 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
12403 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
12406 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12407 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
12408 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
12410 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
12411 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
12415 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
12417 if (p_j2k->m_cp.comment != 00) {
12418 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
12421 /* DEVELOPER CORNER, insert your custom procedures */
12422 if (p_j2k->m_cp.rsiz & MCT) {
12423 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
12425 /* End of Developer Corner */
12427 if (p_j2k->cstr_index) {
12428 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
12431 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
12432 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
12436 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
12438 OPJ_UINT32 * p_data_written,
12439 OPJ_UINT32 p_total_data_size,
12440 opj_stream_private_t *p_stream,
12441 struct opj_event_mgr * p_manager )
12444 OPJ_UINT32 l_nb_bytes_written = 0;
12445 OPJ_UINT32 l_current_nb_bytes_written;
12446 OPJ_BYTE * l_begin_data = 00;
12448 opj_tcp_v2_t *l_tcp = 00;
12449 opj_tcd_v2_t * l_tcd = 00;
12450 opj_cp_v2_t * l_cp = 00;
12452 l_tcd = p_j2k->m_tcd;
12453 l_cp = &(p_j2k->m_cp);
12454 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12456 l_tcd->cur_pino = 0;
12458 /*Get number of tile parts*/
12459 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12464 l_current_nb_bytes_written = 0;
12465 l_begin_data = p_data;
12466 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
12471 l_nb_bytes_written += l_current_nb_bytes_written;
12472 p_data += l_current_nb_bytes_written;
12473 p_total_data_size -= l_current_nb_bytes_written;
12475 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
12476 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
12477 l_current_nb_bytes_written = 0;
12478 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12479 l_nb_bytes_written += l_current_nb_bytes_written;
12480 p_data += l_current_nb_bytes_written;
12481 p_total_data_size -= l_current_nb_bytes_written;
12483 l_current_nb_bytes_written = 0;
12484 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12485 l_nb_bytes_written += l_current_nb_bytes_written;
12486 p_data += l_current_nb_bytes_written;
12487 p_total_data_size -= l_current_nb_bytes_written;
12490 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
12491 l_current_nb_bytes_written = 0;
12492 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
12493 l_nb_bytes_written += l_current_nb_bytes_written;
12494 p_data += l_current_nb_bytes_written;
12495 p_total_data_size -= l_current_nb_bytes_written;
12499 l_current_nb_bytes_written = 0;
12500 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12504 l_nb_bytes_written += l_current_nb_bytes_written;
12505 * p_data_written = l_nb_bytes_written;
12507 /* Writing Psot in SOT marker */
12508 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
12510 if (l_cp->m_specific_param.m_enc.m_cinema){
12511 j2k_update_tlm(p_j2k,l_nb_bytes_written);
12517 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
12519 OPJ_UINT32 * p_data_written,
12520 OPJ_UINT32 p_total_data_size,
12521 opj_stream_private_t *p_stream,
12522 struct opj_event_mgr * p_manager
12525 OPJ_UINT32 tilepartno=0;
12526 OPJ_UINT32 l_nb_bytes_written = 0;
12527 OPJ_UINT32 l_current_nb_bytes_written;
12528 OPJ_UINT32 l_part_tile_size;
12529 OPJ_UINT32 tot_num_tp;
12532 OPJ_BYTE * l_begin_data;
12533 opj_tcp_v2_t *l_tcp = 00;
12534 opj_tcd_v2_t * l_tcd = 00;
12535 opj_cp_v2_t * l_cp = 00;
12538 l_tcd = p_j2k->m_tcd;
12539 l_cp = &(p_j2k->m_cp);
12540 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12542 /*Get number of tile parts*/
12543 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
12545 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
12546 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12547 l_current_nb_bytes_written = 0;
12548 l_part_tile_size = 0;
12549 l_begin_data = p_data;
12551 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12555 l_nb_bytes_written += l_current_nb_bytes_written;
12556 p_data += l_current_nb_bytes_written;
12557 p_total_data_size -= l_current_nb_bytes_written;
12558 l_part_tile_size += l_nb_bytes_written;
12560 l_current_nb_bytes_written = 0;
12561 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12565 p_data += l_current_nb_bytes_written;
12566 l_nb_bytes_written += l_current_nb_bytes_written;
12567 p_total_data_size -= l_current_nb_bytes_written;
12568 l_part_tile_size += l_nb_bytes_written;
12570 /* Writing Psot in SOT marker */
12571 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12573 if (l_cp->m_specific_param.m_enc.m_cinema) {
12574 j2k_update_tlm(p_j2k,l_part_tile_size);
12577 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12580 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
12581 l_tcd->cur_pino = pino;
12583 /*Get number of tile parts*/
12584 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
12585 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
12586 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12587 l_current_nb_bytes_written = 0;
12588 l_part_tile_size = 0;
12589 l_begin_data = p_data;
12591 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12595 l_nb_bytes_written += l_current_nb_bytes_written;
12596 p_data += l_current_nb_bytes_written;
12597 p_total_data_size -= l_current_nb_bytes_written;
12598 l_part_tile_size += l_current_nb_bytes_written;
12600 l_current_nb_bytes_written = 0;
12602 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12606 l_nb_bytes_written += l_current_nb_bytes_written;
12607 p_data += l_current_nb_bytes_written;
12608 p_total_data_size -= l_current_nb_bytes_written;
12609 l_part_tile_size += l_current_nb_bytes_written;
12611 /* Writing Psot in SOT marker */
12612 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12614 if (l_cp->m_specific_param.m_enc.m_cinema) {
12615 j2k_update_tlm(p_j2k,l_part_tile_size);
12618 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12622 *p_data_written = l_nb_bytes_written;
12628 * Writes the updated tlm.
12630 * @param p_stream the stream to write data to.
12631 * @param p_j2k J2K codec.
12632 * @param p_manager the user event manager.
12634 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
12635 struct opj_stream_private *p_stream,
12636 struct opj_event_mgr * p_manager )
12638 OPJ_UINT32 l_tlm_size;
12639 OPJ_SIZE_T l_tlm_position, l_current_position;
12641 /* preconditions */
12642 assert(p_j2k != 00);
12643 assert(p_manager != 00);
12644 assert(p_stream != 00);
12646 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12647 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12648 l_current_position = opj_stream_tell(p_stream);
12650 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12654 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) {
12658 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12667 * Ends the encoding, i.e. frees memory.
12669 * @param p_stream the stream to write data to.
12670 * @param p_j2k J2K codec.
12671 * @param p_manager the user event manager.
12673 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
12674 struct opj_stream_private *p_stream,
12675 struct opj_event_mgr * p_manager )
12677 /* preconditions */
12678 assert(p_j2k != 00);
12679 assert(p_manager != 00);
12680 assert(p_stream != 00);
12682 tcd_destroy_v2(p_j2k->m_tcd);
12685 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12686 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12687 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12688 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12691 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12692 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12693 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12696 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12702 * Destroys the memory associated with the decoding of headers.
12704 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12705 opj_stream_private_t *p_stream,
12706 opj_event_mgr_t * p_manager )
12708 /* preconditions */
12709 assert(p_j2k != 00);
12710 assert(p_stream != 00);
12711 assert(p_manager != 00);
12713 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12714 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12715 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12718 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12727 * @param p_stream the stream to write data to.
12728 * @param p_j2k J2K codec.
12729 * @param p_manager the user event manager.
12731 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12732 struct opj_stream_private *p_stream,
12733 struct opj_event_mgr * p_manager )
12735 opj_codestream_info_t * l_cstr_info = 00;
12737 /* preconditions */
12738 assert(p_j2k != 00);
12739 assert(p_manager != 00);
12740 assert(p_stream != 00);
12743 /* TODO mergeV2: check this part which use cstr_info */
12744 /*l_cstr_info = p_j2k->cstr_info;
12748 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));
12750 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12751 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12753 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12755 l_cstr_info->tw = p_j2k->m_cp.tw;
12756 l_cstr_info->th = p_j2k->m_cp.th;
12758 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12759 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12760 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12761 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12763 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12765 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12767 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12769 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12770 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12773 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12775 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12777 /*l_cstr_info->maxmarknum = 100;
12778 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12779 l_cstr_info->marknum = 0;
12782 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);
12786 * Creates a tile-coder decoder.
12788 * @param p_stream the stream to write data to.
12789 * @param p_j2k J2K codec.
12790 * @param p_manager the user event manager.
12792 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12793 struct opj_stream_private *p_stream,
12794 struct opj_event_mgr * p_manager )
12796 /* preconditions */
12797 assert(p_j2k != 00);
12798 assert(p_manager != 00);
12799 assert(p_stream != 00);
12801 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12803 if (! p_j2k->m_tcd) {
12804 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12808 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12809 tcd_destroy_v2(p_j2k->m_tcd);
12820 * @param p_j2k the jpeg2000 codec.
12821 * @param p_stream the stream to write data to.
12822 * @param p_manager the user event manager.
12824 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12825 OPJ_UINT32 p_tile_index,
12827 OPJ_UINT32 p_data_size,
12828 opj_stream_private_t *p_stream,
12829 opj_event_mgr_t * p_manager )
12831 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12832 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12836 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12837 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);