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"
37 #ifdef HAVE_INTTYPES_H
38 #include <inttypes.h> /* PRIi64 */
41 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
44 /** @name Local static functions */
48 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
50 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
53 * The read header procedure.
55 opj_bool j2k_read_header_procedure(
57 struct opj_stream_private *p_stream,
58 struct opj_event_mgr * p_manager);
61 * The default encoding validation procedure without any extension.
63 * @param p_j2k the jpeg2000 codec to validate.
64 * @param p_stream the input stream to validate.
65 * @param p_manager the user event manager.
67 * @return true if the parameters are correct.
69 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
70 opj_stream_private_t *p_stream,
71 opj_event_mgr_t * p_manager );
74 * The default decoding validation procedure without any extension.
76 * @param p_j2k the jpeg2000 codec to validate.
77 * @param p_stream the input stream to validate.
78 * @param p_manager the user event manager.
80 * @return true if the parameters are correct.
82 opj_bool j2k_decoding_validation (
84 opj_stream_private_t *p_stream,
85 opj_event_mgr_t * p_manager
89 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
90 * are valid. Developpers wanting to extend the library can add their own validation procedures.
92 static void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
95 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
96 * are valid. Developpers wanting to extend the library can add their own validation procedures.
98 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
101 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
102 * are valid. Developpers wanting to extend the library can add their own validation procedures.
104 static void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
107 * The mct encoding validation procedure.
109 * @param p_j2k the jpeg2000 codec to validate.
110 * @param p_stream the input stream to validate.
111 * @param p_manager the user event manager.
113 * @return true if the parameters are correct.
115 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
116 opj_stream_private_t *p_stream,
117 opj_event_mgr_t * p_manager );
120 * Builds the tcd decoder to use to decode tile.
122 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
123 opj_stream_private_t *p_stream,
124 opj_event_mgr_t * p_manager );
126 * Builds the tcd encoder to use to encode tile.
128 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
129 opj_stream_private_t *p_stream,
130 opj_event_mgr_t * p_manager );
133 * Creates a tile-coder decoder.
135 * @param p_stream the stream to write data to.
136 * @param p_j2k J2K codec.
137 * @param p_manager the user event manager.
139 static opj_bool j2k_create_tcd( opj_j2k_v2_t *p_j2k,
140 struct opj_stream_private *p_stream,
141 struct opj_event_mgr * p_manager );
144 * Excutes the given procedures on the given codec.
146 * @param p_procedure_list the list of procedures to execute
147 * @param p_j2k the jpeg2000 codec to execute the procedures on.
148 * @param p_stream the stream to execute the procedures on.
149 * @param p_manager the user manager.
151 * @return true if all the procedures were successfully executed.
153 static opj_bool j2k_exec (
154 opj_j2k_v2_t * p_j2k,
155 opj_procedure_list_t * p_procedure_list,
156 opj_stream_private_t *p_stream,
157 opj_event_mgr_t * p_manager
161 * Updates the rates of the tcp.
163 * @param p_stream the stream to write data to.
164 * @param p_j2k J2K codec.
165 * @param p_manager the user event manager.
167 static opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
168 struct opj_stream_private *p_stream,
169 struct opj_event_mgr * p_manager );
172 * Copies the decoding tile parameters onto all the tile parameters.
173 * Creates also the tile decoder.
175 opj_bool j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
176 opj_stream_private_t *p_stream,
177 opj_event_mgr_t * p_manager );
180 * Destroys the memory associated with the decoding of headers.
182 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
183 opj_stream_private_t *p_stream,
184 opj_event_mgr_t * p_manager );
187 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
188 * with the marker value.
189 * @param p_id Marker value to look up
191 * @return the handler associated with the id.
193 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
196 * Destroys a tile coding parameter structure.
198 * @param p_tcp the tile coding parameter to destroy.
200 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
203 * Destroys the data inside a tile coding parameter structure.
205 * @param p_tcp the tile coding parameter which contain data to destroy.
207 static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
210 * Destroys a coding parameter structure.
212 * @param p_cp the coding parameter to destroy.
214 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
218 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
220 * @param p_comp_no the component number to output.
221 * @param p_stream the stream to write data to.
222 * @param p_j2k J2K codec.
223 * @param p_manager the user event manager.
226 static opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
227 OPJ_UINT32 p_tile_no,
228 OPJ_UINT32 p_comp_no,
230 OPJ_UINT32 * p_header_size,
231 struct opj_event_mgr * p_manager );
234 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
236 * @param p_tile_no the tile index.
237 * @param p_comp_no the component being outputted.
238 * @param p_j2k the J2K codec.
240 * @return the number of bytes taken by the SPCod element.
242 static OPJ_UINT32 j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
243 OPJ_UINT32 p_tile_no,
244 OPJ_UINT32 p_comp_no );
247 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
248 * @param p_header_data the data contained in the COM box.
249 * @param p_j2k the jpeg2000 codec.
250 * @param p_header_size the size of the data contained in the COM marker.
251 * @param p_manager the user event manager.
253 static opj_bool j2k_read_SPCod_SPCoc(
256 OPJ_BYTE * p_header_data,
257 OPJ_UINT32 * p_header_size,
258 struct opj_event_mgr * p_manager
262 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
264 * @param p_tile_no the tile index.
265 * @param p_comp_no the component being outputted.
266 * @param p_j2k the J2K codec.
268 * @return the number of bytes taken by the SPCod element.
270 static OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
271 OPJ_UINT32 p_tile_no,
272 OPJ_UINT32 p_comp_no );
275 * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
277 * @param p_tile_no the tile to output.
278 * @param p_comp_no the component number to output.
279 * @param p_data the data buffer.
280 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
281 * @param p_j2k J2K codec.
282 * @param p_manager the user event manager.
285 static opj_bool j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
286 OPJ_UINT32 p_tile_no,
287 OPJ_UINT32 p_comp_no,
289 OPJ_UINT32 * p_header_size,
290 struct opj_event_mgr * p_manager
294 * Updates the Tile Length Marker.
296 static void j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
299 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
301 * @param p_tile_no the tile to output.
302 * @param p_comp_no the component number to output.
303 * @param p_data the data buffer.
304 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
305 * @param p_j2k J2K codec.
306 * @param p_manager the user event manager.
309 static opj_bool j2k_read_SQcd_SQcc(
312 OPJ_BYTE * p_header_data,
313 OPJ_UINT32 * p_header_size,
314 struct opj_event_mgr * p_manager
318 * Copies the tile component parameters of all the component from the first tile component.
320 * @param p_j2k the J2k codec.
322 static void j2k_copy_tile_component_parameters(
327 * Copies the tile quantization parameters of all the component from the first tile component.
329 * @param p_j2k the J2k codec.
331 static void j2k_copy_tile_quantization_parameters(
338 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
339 opj_stream_private_t *p_stream,
340 opj_event_mgr_t * p_manager);
343 static opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
344 OPJ_UINT32 p_tile_index,
345 opj_stream_private_t *p_stream,
346 opj_event_mgr_t * p_manager );
348 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
350 static void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
352 static opj_bool j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
354 OPJ_UINT32 p_data_size,
355 opj_stream_private_t *p_stream,
356 opj_event_mgr_t * p_manager );
359 * Sets up the procedures to do on writing header.
360 * Developers wanting to extend the library can add their own writing procedures.
362 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
364 static opj_bool j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k,
366 OPJ_UINT32 * p_data_written,
367 OPJ_UINT32 p_total_data_size,
368 opj_stream_private_t *p_stream,
369 struct opj_event_mgr * p_manager );
371 static opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
373 OPJ_UINT32 * p_data_written,
374 OPJ_UINT32 p_total_data_size,
375 opj_stream_private_t *p_stream,
376 struct opj_event_mgr * p_manager );
379 * Gets the offset of the header.
381 * @param p_stream the stream to write data to.
382 * @param p_j2k J2K codec.
383 * @param p_manager the user event manager.
385 static opj_bool j2k_get_end_header( opj_j2k_v2_t *p_j2k,
386 struct opj_stream_private *p_stream,
387 struct opj_event_mgr * p_manager );
390 * -----------------------------------------------------------------------
391 * -----------------------------------------------------------------------
392 * -----------------------------------------------------------------------
396 Write the SOC marker (Start Of Codestream)
397 @param j2k J2K handle
399 static void j2k_write_soc(opj_j2k_t *j2k);
402 * Writes the SOC marker (Start Of Codestream)
404 * @param p_stream the stream to write data to.
405 * @param p_j2k J2K codec.
406 * @param p_manager the user event manager.
408 static opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
409 struct opj_stream_private *p_stream,
410 struct opj_event_mgr * p_manager );
413 Read the SOC marker (Start of Codestream)
414 @param j2k J2K handle
416 static void j2k_read_soc(opj_j2k_t *j2k);
419 * Reads a SOC marker (Start of Codestream)
420 * @param p_header_data the data contained in the SOC box.
421 * @param jp2 the jpeg2000 file codec.
422 * @param p_header_size the size of the data contained in the SOC marker.
423 * @param p_manager the user event manager.
425 static opj_bool j2k_read_soc_v2(
427 struct opj_stream_private *p_stream,
428 struct opj_event_mgr * p_manager
432 Write the SIZ marker (image and tile size)
433 @param j2k J2K handle
435 static void j2k_write_siz(opj_j2k_t *j2k);
438 * Writes the SIZ marker (image and tile size)
440 * @param p_stream the stream to write data to.
441 * @param p_j2k J2K codec.
442 * @param p_manager the user event manager.
444 static opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
445 struct opj_stream_private *p_stream,
446 struct opj_event_mgr * p_manager );
449 Read the SIZ marker (image and tile size)
450 @param j2k J2K handle
452 static void j2k_read_siz(opj_j2k_t *j2k);
455 * Reads a SIZ marker (image and tile size)
456 * @param p_header_data the data contained in the SIZ box.
457 * @param jp2 the jpeg2000 file codec.
458 * @param p_header_size the size of the data contained in the SIZ marker.
459 * @param p_manager the user event manager.
461 static opj_bool j2k_read_siz_v2 (
463 OPJ_BYTE * p_header_data,
464 OPJ_UINT32 p_header_size,
465 struct opj_event_mgr * p_manager
469 Write the COM marker (comment)
470 @param j2k J2K handle
472 static void j2k_write_com(opj_j2k_t *j2k);
474 Read the COM marker (comment)
475 @param j2k J2K handle
477 static void j2k_read_com(opj_j2k_t *j2k);
479 * Reads a COM marker (comments)
480 * @param p_header_data the data contained in the COM box.
481 * @param jp2 the jpeg2000 file codec.
482 * @param p_header_size the size of the data contained in the COM marker.
483 * @param p_manager the user event manager.
485 static opj_bool j2k_read_com_v2 (
487 OPJ_BYTE * p_header_data,
488 OPJ_UINT32 p_header_size,
489 struct opj_event_mgr * p_manager
492 Write the value concerning the specified component in the marker COD and COC
493 @param j2k J2K handle
494 @param compno Number of the component concerned by the information written
496 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
498 Read the value concerning the specified component in the marker COD and COC
499 @param j2k J2K handle
500 @param compno Number of the component concerned by the information read
502 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
504 Write the COD marker (coding style default)
505 @param j2k J2K handle
507 static void j2k_write_cod(opj_j2k_t *j2k);
510 * Writes the COD marker (Coding style default)
512 * @param p_stream the stream to write data to.
513 * @param p_j2k J2K codec.
514 * @param p_manager the user event manager.
516 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
517 struct opj_stream_private *p_stream,
518 struct opj_event_mgr * p_manager );
521 Read the COD marker (coding style default)
522 @param j2k J2K handle
524 static void j2k_read_cod(opj_j2k_t *j2k);
527 * Reads a COD marker (Coding Styke defaults)
528 * @param p_header_data the data contained in the COD box.
529 * @param p_j2k the jpeg2000 codec.
530 * @param p_header_size the size of the data contained in the COD marker.
531 * @param p_manager the user event manager.
533 static opj_bool j2k_read_cod_v2 (
535 OPJ_BYTE * p_header_data,
536 OPJ_UINT32 p_header_size,
537 struct opj_event_mgr * p_manager
541 Write the COC marker (coding style component)
542 @param j2k J2K handle
543 @param compno Number of the component concerned by the information written
545 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
548 * Writes the COC marker (Coding style component)
550 * @param p_comp_number the index of the component to output.
551 * @param p_stream the stream to write data to.
552 * @param p_j2k J2K codec.
553 * @param p_manager the user event manager.
555 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
556 OPJ_UINT32 p_comp_number,
557 struct opj_stream_private *p_stream,
558 struct opj_event_mgr * p_manager
561 * Writes the COC marker (Coding style component)
563 * @param p_comp_no the index of the component to output.
564 * @param p_stream the stream to write data to.
565 * @param p_j2k J2K codec.
566 * @param p_manager the user event manager.
568 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
569 OPJ_UINT32 p_comp_no,
571 OPJ_UINT32 * p_data_written,
572 struct opj_event_mgr * p_manager );
575 * Gets the maximum size taken by a coc.
577 * @param p_j2k the jpeg2000 codec to use.
579 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
582 Read the COC marker (coding style component)
583 @param j2k J2K handle
585 static void j2k_read_coc(opj_j2k_t *j2k);
588 * Reads a COC marker (Coding Style Component)
589 * @param p_header_data the data contained in the COC box.
590 * @param p_j2k the jpeg2000 codec.
591 * @param p_header_size the size of the data contained in the COC marker.
592 * @param p_manager the user event manager.
594 static opj_bool j2k_read_coc_v2 (
596 OPJ_BYTE * p_header_data,
597 OPJ_UINT32 p_header_size,
598 struct opj_event_mgr * p_manager
602 Write the value concerning the specified component in the marker QCD and QCC
603 @param j2k J2K handle
604 @param compno Number of the component concerned by the information written
606 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
608 Read the value concerning the specified component in the marker QCD and QCC
609 @param j2k J2K handle
610 @param compno Number of the component concern by the information read
611 @param len Length of the information in the QCX part of the marker QCD/QCC
613 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
615 Write the QCD marker (quantization default)
616 @param j2k J2K handle
618 static void j2k_write_qcd(opj_j2k_t *j2k);
621 * Writes the QCD marker (quantization default)
623 * @param p_comp_number the index of the component to output.
624 * @param p_stream the stream to write data to.
625 * @param p_j2k J2K codec.
626 * @param p_manager the user event manager.
628 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
629 struct opj_stream_private *p_stream,
630 struct opj_event_mgr * p_manager );
633 Read the QCD marker (quantization default)
634 @param j2k J2K handle
636 static void j2k_read_qcd(opj_j2k_t *j2k);
639 * Reads a QCD marker (Quantization defaults)
640 * @param p_header_data the data contained in the QCD box.
641 * @param p_j2k the jpeg2000 codec.
642 * @param p_header_size the size of the data contained in the QCD marker.
643 * @param p_manager the user event manager.
645 static opj_bool j2k_read_qcd_v2 (
647 OPJ_BYTE * p_header_data,
648 OPJ_UINT32 p_header_size,
649 struct opj_event_mgr * p_manager
653 Write the QCC marker (quantization component)
654 @param j2k J2K handle
655 @param compno Number of the component concerned by the information written
657 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
660 * Writes the QCC marker (quantization component)
662 * @param p_comp_no the index of the component to output.
663 * @param p_stream the stream to write data to.
664 * @param p_j2k J2K codec.
665 * @param p_manager the user event manager.
667 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
668 OPJ_UINT32 p_comp_no,
669 struct opj_stream_private *p_stream,
670 struct opj_event_mgr * p_manager );
673 * Writes the QCC marker (quantization component)
675 * @param p_comp_no the index of the component to output.
676 * @param p_stream the stream to write data to.
677 * @param p_j2k J2K codec.
678 * @param p_manager the user event manager.
680 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
681 OPJ_UINT32 p_comp_no,
683 OPJ_UINT32 * p_data_written,
684 struct opj_event_mgr * p_manager );
687 * Gets the maximum size taken by a qcc.
689 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
692 Read the QCC marker (quantization component)
693 @param j2k J2K handle
695 static void j2k_read_qcc(opj_j2k_t *j2k);
697 * Reads a QCC marker (Quantization component)
698 * @param p_header_data the data contained in the QCC box.
699 * @param p_j2k the jpeg2000 codec.
700 * @param p_header_size the size of the data contained in the QCC marker.
701 * @param p_manager the user event manager.
703 static opj_bool j2k_read_qcc_v2(
705 OPJ_BYTE * p_header_data,
706 OPJ_UINT32 p_header_size,
707 struct opj_event_mgr * p_manager);
710 Write the POC marker (progression order change)
711 @param j2k J2K handle
713 static void j2k_write_poc(opj_j2k_t *j2k);
716 * Writes the POC marker (Progression Order Change)
718 * @param p_stream the stream to write data to.
719 * @param p_j2k J2K codec.
720 * @param p_manager the user event manager.
722 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
724 OPJ_UINT32 * p_data_written,
725 struct opj_event_mgr * p_manager );
728 * Gets the maximum size taken by the writting of a POC.
730 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
733 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
735 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
738 * Gets the maximum size taken by the headers of the SOT.
740 * @param p_j2k the jpeg2000 codec to use.
742 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
745 Read the POC marker (progression order change)
746 @param j2k J2K handle
748 static void j2k_read_poc(opj_j2k_t *j2k);
750 * Reads a POC marker (Progression Order Change)
752 * @param p_header_data the data contained in the POC box.
753 * @param p_j2k the jpeg2000 codec.
754 * @param p_header_size the size of the data contained in the POC marker.
755 * @param p_manager the user event manager.
757 static opj_bool j2k_read_poc_v2 (
759 OPJ_BYTE * p_header_data,
760 OPJ_UINT32 p_header_size,
761 struct opj_event_mgr * p_manager
765 Read the CRG marker (component registration)
766 @param j2k J2K handle
768 static void j2k_read_crg(opj_j2k_t *j2k);
770 * Reads a CRG marker (Component registration)
772 * @param p_header_data the data contained in the TLM box.
773 * @param p_j2k the jpeg2000 codec.
774 * @param p_header_size the size of the data contained in the TLM marker.
775 * @param p_manager the user event manager.
777 static opj_bool j2k_read_crg_v2 (
779 OPJ_BYTE * p_header_data,
780 OPJ_UINT32 p_header_size,
781 struct opj_event_mgr * p_manager
784 Read the TLM marker (tile-part lengths)
785 @param j2k J2K handle
787 static void j2k_read_tlm(opj_j2k_t *j2k);
789 * Reads a TLM marker (Tile Length Marker)
791 * @param p_header_data the data contained in the TLM box.
792 * @param p_j2k the jpeg2000 codec.
793 * @param p_header_size the size of the data contained in the TLM marker.
794 * @param p_manager the user event manager.
796 static opj_bool j2k_read_tlm_v2 (
798 OPJ_BYTE * p_header_data,
799 OPJ_UINT32 p_header_size,
800 struct opj_event_mgr * p_manager
804 * Writes the updated tlm.
806 * @param p_stream the stream to write data to.
807 * @param p_j2k J2K codec.
808 * @param p_manager the user event manager.
810 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
811 struct opj_stream_private *p_stream,
812 struct opj_event_mgr * p_manager );
815 Read the PLM marker (packet length, main header)
816 @param j2k J2K handle
818 static void j2k_read_plm(opj_j2k_t *j2k);
821 * Reads a PLM marker (Packet length, main header marker)
823 * @param p_header_data the data contained in the TLM box.
824 * @param p_j2k the jpeg2000 codec.
825 * @param p_header_size the size of the data contained in the TLM marker.
826 * @param p_manager the user event manager.
828 static opj_bool j2k_read_plm_v2 (
830 OPJ_BYTE * p_header_data,
831 OPJ_UINT32 p_header_size,
832 struct opj_event_mgr * p_manager
835 Read the PLT marker (packet length, tile-part header)
836 @param j2k J2K handle
838 static void j2k_read_plt(opj_j2k_t *j2k);
840 * Reads a PLT marker (Packet length, tile-part header)
842 * @param p_header_data the data contained in the PLT box.
843 * @param p_j2k the jpeg2000 codec.
844 * @param p_header_size the size of the data contained in the PLT marker.
845 * @param p_manager the user event manager.
847 static opj_bool j2k_read_plt_v2 (
849 OPJ_BYTE * p_header_data,
850 OPJ_UINT32 p_header_size,
851 struct opj_event_mgr * p_manager
854 Read the PPM marker (packet packet headers, main header)
855 @param j2k J2K handle
857 static void j2k_read_ppm(opj_j2k_t *j2k);
859 * Reads a PPM marker (Packed packet headers, main header)
861 * @param p_header_data the data contained in the POC box.
862 * @param p_j2k the jpeg2000 codec.
863 * @param p_header_size the size of the data contained in the POC marker.
864 * @param p_manager the user event manager.
867 static opj_bool j2k_read_ppm_v2 (
869 OPJ_BYTE * p_header_data,
870 OPJ_UINT32 p_header_size,
871 struct opj_event_mgr * p_manager
875 static opj_bool j2k_read_ppm_v3 (
877 OPJ_BYTE * p_header_data,
878 OPJ_UINT32 p_header_size,
879 struct opj_event_mgr * p_manager
883 Read the PPT marker (packet packet headers, tile-part header)
884 @param j2k J2K handle
886 static void j2k_read_ppt(opj_j2k_t *j2k);
888 * Reads a PPT marker (Packed packet headers, tile-part header)
890 * @param p_header_data the data contained in the PPT box.
891 * @param p_j2k the jpeg2000 codec.
892 * @param p_header_size the size of the data contained in the PPT marker.
893 * @param p_manager the user event manager.
895 static opj_bool j2k_read_ppt_v2 (
897 OPJ_BYTE * p_header_data,
898 OPJ_UINT32 p_header_size,
899 struct opj_event_mgr * p_manager
902 Write the TLM marker (Mainheader)
903 @param j2k J2K handle
905 static void j2k_write_tlm(opj_j2k_t *j2k);
907 Write the SOT marker (start of tile-part)
908 @param j2k J2K handle
910 static void j2k_write_sot(opj_j2k_t *j2k);
912 Read the SOT marker (start of tile-part)
913 @param j2k J2K handle
915 static void j2k_read_sot(opj_j2k_t *j2k);
918 * Writes the SOT marker (Start of tile-part)
920 * @param p_stream the stream to write data to.
921 * @param p_j2k J2K codec.
922 * @param p_manager the user event manager.
924 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
926 OPJ_UINT32 * p_data_written,
927 const struct opj_stream_private *p_stream,
928 struct opj_event_mgr * p_manager );
931 * Reads a PPT marker (Packed packet headers, tile-part header)
933 * @param p_header_data the data contained in the PPT box.
934 * @param p_j2k the jpeg2000 codec.
935 * @param p_header_size the size of the data contained in the PPT marker.
936 * @param p_manager the user event manager.
938 static opj_bool j2k_read_sot_v2 (
940 OPJ_BYTE * p_header_data,
941 OPJ_UINT32 p_header_size,
942 struct opj_event_mgr * p_manager
945 Write the SOD marker (start of data)
946 @param j2k J2K handle
947 @param tile_coder Pointer to a TCD handle
949 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
952 * Writes the SOD marker (Start of data)
954 * @param p_stream the stream to write data to.
955 * @param p_j2k J2K codec.
956 * @param p_manager the user event manager.
958 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
959 struct opj_tcd_v2 * p_tile_coder,
961 OPJ_UINT32 * p_data_written,
962 OPJ_UINT32 p_total_data_size,
963 const struct opj_stream_private *p_stream,
964 struct opj_event_mgr * p_manager );
967 Read the SOD marker (start of data)
968 @param j2k J2K handle
970 static void j2k_read_sod(opj_j2k_t *j2k);
973 * Reads a SOD marker (Start Of Data)
975 * @param p_header_data the data contained in the SOD box.
976 * @param p_j2k the jpeg2000 codec.
977 * @param p_header_size the size of the data contained in the SOD marker.
978 * @param p_manager the user event manager.
980 static opj_bool j2k_read_sod_v2 (
982 struct opj_stream_private *p_stream,
983 struct opj_event_mgr * p_manager
987 * Updates the Tile Length Marker.
989 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
991 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
992 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
994 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
995 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
999 Write the RGN marker (region-of-interest)
1000 @param j2k J2K handle
1001 @param compno Number of the component concerned by the information written
1002 @param tileno Number of the tile concerned by the information written
1004 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
1007 * Writes the RGN marker (Region Of Interest)
1009 * @param p_tile_no the tile to output
1010 * @param p_comp_no the component to output
1011 * @param p_stream the stream to write data to.
1012 * @param p_j2k J2K codec.
1013 * @param p_manager the user event manager.
1015 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
1016 OPJ_UINT32 p_tile_no,
1017 OPJ_UINT32 p_comp_no,
1018 struct opj_stream_private *p_stream,
1019 struct opj_event_mgr * p_manager );
1022 Read the RGN marker (region-of-interest)
1023 @param j2k J2K handle
1025 static void j2k_read_rgn(opj_j2k_t *j2k);
1028 * Reads a RGN marker (Region Of Interest)
1030 * @param p_header_data the data contained in the POC box.
1031 * @param p_j2k the jpeg2000 codec.
1032 * @param p_header_size the size of the data contained in the POC marker.
1033 * @param p_manager the user event manager.
1035 static opj_bool j2k_read_rgn_v2 (
1036 opj_j2k_v2_t *p_j2k,
1037 OPJ_BYTE * p_header_data,
1038 OPJ_UINT32 p_header_size,
1039 struct opj_event_mgr * p_manager
1043 Write the EOC marker (end of codestream)
1044 @param j2k J2K handle
1046 static void j2k_write_eoc(opj_j2k_t *j2k);
1048 Read the EOC marker (end of codestream)
1049 @param j2k J2K handle
1051 static void j2k_read_eoc(opj_j2k_t *j2k);
1054 * Reads a EOC marker (End Of Codestream)
1056 * @param p_header_data the data contained in the SOD box.
1057 * @param p_j2k the jpeg2000 codec.
1058 * @param p_header_size the size of the data contained in the SOD marker.
1059 * @param p_manager the user event manager.
1062 static opj_bool j2k_read_eoc_v2 (
1063 opj_j2k_v2_t *p_j2k,
1064 struct opj_stream_private *p_stream,
1065 struct opj_event_mgr * p_manager
1072 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1074 * @param p_stream the stream to write data to.
1075 * @param p_j2k J2K codec.
1076 * @param p_manager the user event manager.
1078 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1079 struct opj_stream_private *p_stream,
1080 struct opj_event_mgr * p_manager );
1085 * @param p_stream the stream to write data to.
1086 * @param p_j2k J2K codec.
1087 * @param p_manager the user event manager.
1089 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1090 struct opj_stream_private *p_stream,
1091 struct opj_event_mgr * p_manager );
1094 Read an unknown marker
1095 @param j2k J2K handle
1097 static void j2k_read_unk(opj_j2k_t *j2k);
1099 Add main header marker information
1100 @param cstr_info Codestream information structure
1101 @param type marker type
1102 @param pos byte offset of marker segment
1103 @param len length of marker segment
1105 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1107 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1109 Add tile header marker information
1110 @param tileno tile index number
1111 @param cstr_info Codestream information structure
1112 @param type marker type
1113 @param pos byte offset of marker segment
1114 @param len length of marker segment
1116 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1118 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);
1121 * Reads an unknown marker
1123 * @param p_stream the stream object to read from.
1124 * @param p_j2k the jpeg2000 codec.
1125 * @param p_manager the user event manager.
1127 * @return true if the marker could be deduced.
1129 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1130 struct opj_stream_private *p_stream,
1131 OPJ_UINT32 *output_marker,
1132 struct opj_event_mgr * p_manager );
1135 * Writes the MCT marker (Multiple Component Transform)
1137 * @param p_stream the stream to write data to.
1138 * @param p_j2k J2K codec.
1139 * @param p_manager the user event manager.
1141 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1142 opj_mct_data_t * p_mct_record,
1143 struct opj_stream_private *p_stream,
1144 struct opj_event_mgr * p_manager );
1147 * Reads a MCT marker (Multiple Component Transform)
1149 * @param p_header_data the data contained in the MCT box.
1150 * @param p_j2k the jpeg2000 codec.
1151 * @param p_header_size the size of the data contained in the MCT marker.
1152 * @param p_manager the user event manager.
1154 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1155 OPJ_BYTE * p_header_data,
1156 OPJ_UINT32 p_header_size,
1157 struct opj_event_mgr * p_manager );
1160 * Writes the MCC marker (Multiple Component Collection)
1162 * @param p_stream the stream to write data to.
1163 * @param p_j2k J2K codec.
1164 * @param p_manager the user event manager.
1166 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1167 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1168 struct opj_stream_private *p_stream,
1169 struct opj_event_mgr * p_manager );
1173 * Reads a MCC marker (Multiple Component Collection)
1175 * @param p_header_data the data contained in the MCC box.
1176 * @param p_j2k the jpeg2000 codec.
1177 * @param p_header_size the size of the data contained in the MCC marker.
1178 * @param p_manager the user event manager.
1180 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1181 OPJ_BYTE * p_header_data,
1182 OPJ_UINT32 p_header_size,
1183 struct opj_event_mgr * p_manager );
1186 * Writes the MCO marker (Multiple component transformation ordering)
1188 * @param p_stream the stream to write data to.
1189 * @param p_j2k J2K codec.
1190 * @param p_manager the user event manager.
1192 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1193 struct opj_stream_private *p_stream,
1194 struct opj_event_mgr * p_manager );
1197 * Reads a MCO marker (Multiple Component Transform Ordering)
1199 * @param p_header_data the data contained in the MCO box.
1200 * @param p_j2k the jpeg2000 codec.
1201 * @param p_header_size the size of the data contained in the MCO marker.
1202 * @param p_manager the user event manager.
1204 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1205 OPJ_BYTE * p_header_data,
1206 OPJ_UINT32 p_header_size,
1207 struct opj_event_mgr * p_manager );
1209 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1211 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1212 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1213 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1214 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1216 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1217 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1218 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1219 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1221 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1222 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1223 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1224 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1228 * Ends the encoding, i.e. frees memory.
1230 * @param p_stream the stream to write data to.
1231 * @param p_j2k J2K codec.
1232 * @param p_manager the user event manager.
1234 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1235 struct opj_stream_private *p_stream,
1236 struct opj_event_mgr * p_manager );
1239 * Writes the CBD marker (Component bit depth definition)
1241 * @param p_stream the stream to write data to.
1242 * @param p_j2k J2K codec.
1243 * @param p_manager the user event manager.
1245 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1246 struct opj_stream_private *p_stream,
1247 struct opj_event_mgr * p_manager );
1250 * Reads a CBD marker (Component bit depth definition)
1251 * @param p_header_data the data contained in the CBD box.
1252 * @param p_j2k the jpeg2000 codec.
1253 * @param p_header_size the size of the data contained in the CBD marker.
1254 * @param p_manager the user event manager.
1256 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1257 OPJ_BYTE * p_header_data,
1258 OPJ_UINT32 p_header_size,
1259 struct opj_event_mgr * p_manager);
1262 * Writes the image components.
1264 * @param p_stream the stream to write data to.
1265 * @param p_j2k J2K codec.
1266 * @param p_manager the user event manager.
1268 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1269 struct opj_stream_private *p_stream,
1270 struct opj_event_mgr * p_manager );
1273 * Writes regions of interests.
1275 * @param p_stream the stream to write data to.
1276 * @param p_j2k J2K codec.
1277 * @param p_manager the user event manager.
1279 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1280 struct opj_stream_private *p_stream,
1281 struct opj_event_mgr * p_manager );
1286 * @param p_stream the stream to write data to.
1287 * @param p_j2k J2K codec.
1288 * @param p_manager the user event manager.
1290 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1291 struct opj_stream_private *p_stream,
1292 struct opj_event_mgr * p_manager );
1295 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1297 * @param cp the coding parameters.
1298 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1299 * @param tileno the given tile.
1301 * @return the number of tile parts.
1303 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1305 /** mem allocation for TLM marker*/
1306 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1309 * Calculates the total number of tile parts needed by the encoder to
1310 * encode such an image. If not enough memory is available, then the function return false.
1312 * @param p_nb_tiles pointer that will hold the number of tile parts.
1313 * @param cp the coding parameters for the image.
1314 * @param image the image to encode.
1315 * @param p_j2k the p_j2k encoder.
1316 * @param p_manager the user event manager.
1318 * @return true if the function was successful, false else.
1320 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1322 OPJ_UINT32 * p_nb_tiles,
1324 opj_event_mgr_t * p_manager);
1326 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1328 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1330 static opj_codestream_index_t* j2k_create_cstr_index(void);
1336 /* ----------------------------------------------------------------------- */
1337 typedef struct j2k_prog_order{
1338 OPJ_PROG_ORDER enum_prog;
1342 j2k_prog_order_t j2k_prog_order_list[] = {
1348 {(OPJ_PROG_ORDER)-1, ""}
1356 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1364 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1366 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1368 j2k_read_int16_to_float,
1369 j2k_read_int32_to_float,
1370 j2k_read_float32_to_float,
1371 j2k_read_float64_to_float
1374 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1376 j2k_read_int16_to_int32,
1377 j2k_read_int32_to_int32,
1378 j2k_read_float32_to_int32,
1379 j2k_read_float64_to_int32
1382 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1384 j2k_write_float_to_int16,
1385 j2k_write_float_to_int32,
1386 j2k_write_float_to_float,
1387 j2k_write_float_to_float64
1390 typedef struct opj_dec_memory_marker_handler
1394 /** value of the state when the marker can appear */
1396 /** action linked to the marker */
1397 opj_bool (*handler) (
1398 opj_j2k_v2_t *p_j2k,
1399 OPJ_BYTE * p_header_data,
1400 OPJ_UINT32 p_header_size,
1401 struct opj_event_mgr * p_manager
1404 opj_dec_memory_marker_handler_t;
1406 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1409 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1410 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1411 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1412 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1413 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1414 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1415 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1416 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1417 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1418 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1419 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1420 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1421 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1423 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1424 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1425 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1426 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1427 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1428 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1430 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
1431 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1432 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1433 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1434 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1435 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1436 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1437 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
1438 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1439 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1440 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1441 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1442 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1444 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1445 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1446 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1447 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1448 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1449 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1451 #ifdef TODO_MS /* FIXME */
1452 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1453 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1454 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1455 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1457 #endif /* USE_JPWL */
1459 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1460 {J2K_MS_INSEC, 0, j2k_read_insec}
1461 #endif /* USE_JPSEC */
1462 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1467 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1469 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1470 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1474 for (i=0;i<p_nb_elem;++i) {
1475 opj_read_bytes(l_src_data,&l_temp,2);
1477 l_src_data+=sizeof(OPJ_INT16);
1479 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1483 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1485 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1486 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1490 for (i=0;i<p_nb_elem;++i) {
1491 opj_read_bytes(l_src_data,&l_temp,4);
1493 l_src_data+=sizeof(OPJ_INT32);
1495 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1499 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1501 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1502 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1506 for (i=0;i<p_nb_elem;++i) {
1507 opj_read_float(l_src_data,&l_temp);
1509 l_src_data+=sizeof(OPJ_FLOAT32);
1511 *(l_dest_data++) = l_temp;
1515 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1517 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1518 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1522 for (i=0;i<p_nb_elem;++i) {
1523 opj_read_double(l_src_data,&l_temp);
1525 l_src_data+=sizeof(OPJ_FLOAT64);
1527 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1531 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1533 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1534 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1538 for (i=0;i<p_nb_elem;++i) {
1539 opj_read_bytes(l_src_data,&l_temp,2);
1541 l_src_data+=sizeof(OPJ_INT16);
1543 *(l_dest_data++) = (OPJ_INT32) l_temp;
1547 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1549 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1550 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1554 for (i=0;i<p_nb_elem;++i) {
1555 opj_read_bytes(l_src_data,&l_temp,4);
1557 l_src_data+=sizeof(OPJ_INT32);
1559 *(l_dest_data++) = (OPJ_INT32) l_temp;
1563 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1565 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1566 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1570 for (i=0;i<p_nb_elem;++i) {
1571 opj_read_float(l_src_data,&l_temp);
1573 l_src_data+=sizeof(OPJ_FLOAT32);
1575 *(l_dest_data++) = (OPJ_INT32) l_temp;
1579 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1581 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1582 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1586 for (i=0;i<p_nb_elem;++i) {
1587 opj_read_double(l_src_data,&l_temp);
1589 l_src_data+=sizeof(OPJ_FLOAT64);
1591 *(l_dest_data++) = (OPJ_INT32) l_temp;
1595 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1597 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1598 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1602 for (i=0;i<p_nb_elem;++i) {
1603 l_temp = (OPJ_UINT32) *(l_src_data++);
1605 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1607 l_dest_data+=sizeof(OPJ_INT16);
1611 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1613 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1614 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1618 for (i=0;i<p_nb_elem;++i) {
1619 l_temp = (OPJ_UINT32) *(l_src_data++);
1621 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1623 l_dest_data+=sizeof(OPJ_INT32);
1627 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1629 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1630 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1634 for (i=0;i<p_nb_elem;++i) {
1635 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1637 opj_write_float(l_dest_data,l_temp);
1639 l_dest_data+=sizeof(OPJ_FLOAT32);
1643 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1645 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1646 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1650 for (i=0;i<p_nb_elem;++i) {
1651 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1653 opj_write_double(l_dest_data,l_temp);
1655 l_dest_data+=sizeof(OPJ_FLOAT64);
1661 * Converts an enum type progression order to string type.
1663 * @param prg_order the progression order to get.
1665 * @return the string representation of the given progression order.
1667 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1668 j2k_prog_order_t *po;
1669 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1670 if(po->enum_prog == prg_order){
1671 return po->str_prog;
1674 return po->str_prog;
1677 /* ----------------------------------------------------------------------- */
1678 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1681 int tpnum=1,tpend=0;
1682 opj_tcp_t *tcp = &cp->tcps[tileno];
1683 prog = j2k_convert_progression_order(tcp->prg);
1688 if( cp->tp_flag == prog[i] ){
1689 tpend=1;cp->tp_pos=i;
1693 tpnum= tpnum * tcp->pocs[pino].compE;
1696 tpnum= tpnum * tcp->pocs[pino].resE;
1699 tpnum= tpnum * tcp->pocs[pino].prcE;
1702 tpnum= tpnum * tcp->pocs[pino].layE;
1714 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1716 * @param cp the coding parameters.
1717 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1718 * @param tileno the given tile.
1720 * @return the number of tile parts.
1722 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1724 const OPJ_CHAR *prog = 00;
1726 OPJ_UINT32 tpnum = 1;
1727 opj_tcp_v2_t *tcp = 00;
1728 opj_poc_t * l_current_poc = 00;
1731 assert(tileno < (cp->tw * cp->th));
1732 assert(pino < (cp->tcps[tileno].numpocs + 1));
1734 /* get the given tile coding parameter */
1735 tcp = &cp->tcps[tileno];
1738 l_current_poc = &(tcp->pocs[pino]);
1739 assert(l_current_poc != 0);
1741 /* get the progression order as a character string */
1742 prog = j2k_convert_progression_order(tcp->prg);
1743 assert(strlen(prog) > 0);
1745 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1749 /* component wise */
1751 tpnum *= l_current_poc->compE;
1753 /* resolution wise */
1755 tpnum *= l_current_poc->resE;
1759 tpnum *= l_current_poc->prcE;
1763 tpnum *= l_current_poc->layE;
1766 /* whould we split here ? */
1767 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1768 cp->m_specific_param.m_enc.m_tp_pos=i;
1780 /** mem allocation for TLM marker*/
1781 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1782 int pino,tileno,totnum_tp=0;
1784 OPJ_ARG_NOT_USED(img_numcomp);
1786 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1787 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1788 int cur_totnum_tp = 0;
1789 opj_tcp_t *tcp = &cp->tcps[tileno];
1790 for(pino = 0; pino <= tcp->numpocs; pino++) {
1792 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1793 if(!pi) { return -1;}
1794 tp_num = j2k_get_num_tp(cp,pino,tileno);
1795 totnum_tp = totnum_tp + tp_num;
1796 cur_totnum_tp = cur_totnum_tp + tp_num;
1797 pi_destroy(pi, cp, tileno);
1799 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1801 if (j2k->cstr_info) {
1802 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1803 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1811 * Calculates the total number of tile parts needed by the encoder to
1812 * encode such an image. If not enough memory is available, then the function return false.
1814 * @param p_nb_tiles pointer that will hold the number of tile parts.
1815 * @param cp the coding parameters for the image.
1816 * @param image the image to encode.
1817 * @param p_j2k the p_j2k encoder.
1818 * @param p_manager the user event manager.
1820 * @return true if the function was successful, false else.
1822 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1824 OPJ_UINT32 * p_nb_tiles,
1826 opj_event_mgr_t * p_manager)
1828 OPJ_UINT32 pino,tileno;
1829 OPJ_UINT32 l_nb_tiles;
1833 assert(p_nb_tiles != 00);
1835 assert(image != 00);
1836 assert(p_j2k != 00);
1837 assert(p_manager != 00);
1839 l_nb_tiles = cp->tw * cp->th;
1844 /* TODO mergeV2: check this part which use cstr_info */
1845 /*if (p_j2k->cstr_info) {
1846 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1848 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1849 OPJ_UINT32 cur_totnum_tp = 0;
1851 pi_update_encoding_parameters(image,cp,tileno);
1853 for (pino = 0; pino <= tcp->numpocs; ++pino)
1855 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1857 *p_nb_tiles = *p_nb_tiles + tp_num;
1859 cur_totnum_tp += tp_num;
1862 tcp->m_nb_tile_parts = cur_totnum_tp;
1864 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1865 if (l_info_tile_ptr->tp == 00) {
1869 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1871 l_info_tile_ptr->num_tps = cur_totnum_tp;
1878 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1879 OPJ_UINT32 cur_totnum_tp = 0;
1881 pi_update_encoding_parameters(image,cp,tileno);
1883 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1884 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1886 *p_nb_tiles = *p_nb_tiles + tp_num;
1888 cur_totnum_tp += tp_num;
1890 tcp->m_nb_tile_parts = cur_totnum_tp;
1899 static void j2k_write_soc(opj_j2k_t *j2k) {
1900 opj_cio_t *cio = j2k->cio;
1901 cio_write(cio, J2K_MS_SOC, 2);
1904 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1909 /* update markers struct */
1910 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1911 #endif /* USE_JPWL */
1916 * Writes the SOC marker (Start Of Codestream)
1918 * @param p_stream the stream to write data to.
1919 * @param p_j2k J2K codec.
1920 * @param p_manager the user event manager.
1922 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
1923 struct opj_stream_private *p_stream,
1924 struct opj_event_mgr * p_manager )
1926 /* 2 bytes will be written */
1927 OPJ_BYTE * l_start_stream = 00;
1930 assert(p_stream != 00);
1931 assert(p_j2k != 00);
1932 assert(p_manager != 00);
1934 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1936 /* write SOC identifier */
1937 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1939 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1945 /* update markers struct */
1947 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1949 assert( 0 && "TODO" );
1950 #endif /* USE_JPWL */
1956 static void j2k_read_soc(opj_j2k_t *j2k) {
1957 j2k->state = J2K_STATE_MHSIZ;
1959 if (j2k->cstr_info) {
1960 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
1961 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
1966 * Reads a SOC marker (Start of Codestream)
1967 * @param p_header_data the data contained in the SOC box.
1968 * @param jp2 the jpeg2000 file codec.
1969 * @param p_header_size the size of the data contained in the SOC marker.
1970 * @param p_manager the user event manager.
1972 static opj_bool j2k_read_soc_v2( opj_j2k_v2_t *p_j2k,
1973 struct opj_stream_private *p_stream,
1974 struct opj_event_mgr * p_manager )
1976 OPJ_BYTE l_data [2];
1977 OPJ_UINT32 l_marker;
1980 assert(p_j2k != 00);
1981 assert(p_manager != 00);
1982 assert(p_stream != 00);
1984 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1988 opj_read_bytes(l_data,&l_marker,2);
1989 if (l_marker != J2K_MS_SOC) {
1993 /* Next marker should be a SIZ marker in the main header */
1994 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1996 /* FIXME move it in a index structure included in p_j2k*/
1997 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1999 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2001 /* Add the marker to the codestream index*/
2002 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2007 static void j2k_write_siz(opj_j2k_t *j2k) {
2011 opj_cio_t *cio = j2k->cio;
2012 opj_image_t *image = j2k->image;
2013 opj_cp_t *cp = j2k->cp;
2015 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2016 lenp = cio_tell(cio);
2018 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2019 cio_write(cio, image->x1, 4); /* Xsiz */
2020 cio_write(cio, image->y1, 4); /* Ysiz */
2021 cio_write(cio, image->x0, 4); /* X0siz */
2022 cio_write(cio, image->y0, 4); /* Y0siz */
2023 cio_write(cio, cp->tdx, 4); /* XTsiz */
2024 cio_write(cio, cp->tdy, 4); /* YTsiz */
2025 cio_write(cio, cp->tx0, 4); /* XT0siz */
2026 cio_write(cio, cp->ty0, 4); /* YT0siz */
2027 cio_write(cio, image->numcomps, 2); /* Csiz */
2028 for (i = 0; i < image->numcomps; i++) {
2029 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2030 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2031 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2033 len = cio_tell(cio) - lenp;
2034 cio_seek(cio, lenp);
2035 cio_write(cio, len, 2); /* Lsiz */
2036 cio_seek(cio, lenp + len);
2039 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2043 * Writes the SIZ marker (image and tile size)
2045 * @param p_stream the stream to write data to.
2046 * @param p_j2k J2K codec.
2047 * @param p_manager the user event manager.
2049 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2050 struct opj_stream_private *p_stream,
2051 struct opj_event_mgr * p_manager )
2054 OPJ_UINT32 l_size_len;
2055 OPJ_BYTE * l_current_ptr;
2056 opj_image_t * l_image = 00;
2057 opj_cp_v2_t *cp = 00;
2058 opj_image_comp_t * l_img_comp = 00;
2061 assert(p_stream != 00);
2062 assert(p_j2k != 00);
2063 assert(p_manager != 00);
2065 l_image = p_j2k->m_private_image;
2066 cp = &(p_j2k->m_cp);
2067 l_size_len = 40 + 3 * l_image->numcomps;
2068 l_img_comp = l_image->comps;
2070 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2072 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2073 = (OPJ_BYTE*)opj_realloc(
2074 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2076 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2080 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2083 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2085 /* write SOC identifier */
2086 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2089 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2092 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2095 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2098 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2101 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2104 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2107 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2110 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2113 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2116 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2119 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2122 for (i = 0; i < l_image->numcomps; ++i) {
2123 /* TODO here with MCT ? */
2124 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2127 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2130 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2136 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) {
2143 static void j2k_read_siz(opj_j2k_t *j2k) {
2147 opj_cio_t *cio = j2k->cio;
2148 opj_image_t *image = j2k->image;
2149 opj_cp_t *cp = j2k->cp;
2151 len = cio_read(cio, 2); /* Lsiz */
2152 cio_read(cio, 2); /* Rsiz (capabilities) */
2153 image->x1 = cio_read(cio, 4); /* Xsiz */
2154 image->y1 = cio_read(cio, 4); /* Ysiz */
2155 image->x0 = cio_read(cio, 4); /* X0siz */
2156 image->y0 = cio_read(cio, 4); /* Y0siz */
2157 cp->tdx = cio_read(cio, 4); /* XTsiz */
2158 cp->tdy = cio_read(cio, 4); /* YTsiz */
2159 cp->tx0 = cio_read(cio, 4); /* XT0siz */
2160 cp->ty0 = cio_read(cio, 4); /* YT0siz */
2162 /* the following code triggers: */
2163 /* warning: comparison of unsigned expression < 0 is always false */
2165 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
2166 opj_event_msg(j2k->cinfo, EVT_ERROR,
2167 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
2168 image->x0,image->x1,image->y0,image->y1);
2173 image->numcomps = cio_read(cio, 2); /* Csiz */
2176 if (j2k->cp->correct) {
2177 /* if JPWL is on, we check whether TX errors have damaged
2178 too much the SIZ parameters */
2179 if (!(image->x1 * image->y1)) {
2180 opj_event_msg(j2k->cinfo, EVT_ERROR,
2181 "JPWL: bad image size (%d x %d)\n",
2182 image->x1, image->y1);
2183 if (!JPWL_ASSUME || JPWL_ASSUME) {
2184 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2188 assert( len >= 38 );
2189 if (image->numcomps != (OPJ_UINT32)((len - 38) / 3)) {
2190 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2191 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2192 image->numcomps, ((len - 38) / 3));
2194 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2197 /* we try to correct */
2198 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
2199 if (image->numcomps < (OPJ_UINT32)((len - 38) / 3)) {
2200 len = 38 + 3 * image->numcomps;
2201 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2204 image->numcomps = ((len - 38) / 3);
2205 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2210 /* update components number in the jpwl_exp_comps filed */
2211 cp->exp_comps = image->numcomps;
2213 #endif /* USE_JPWL */
2215 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
2216 for (i = 0; i < image->numcomps; i++) {
2218 tmp = cio_read(cio, 1); /* Ssiz_i */
2219 image->comps[i].prec = (tmp & 0x7f) + 1;
2220 image->comps[i].sgnd = tmp >> 7;
2221 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
2222 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
2225 if (j2k->cp->correct) {
2226 /* if JPWL is on, we check whether TX errors have damaged
2227 too much the SIZ parameters, again */
2228 if (!(image->comps[i].dx * image->comps[i].dy)) {
2229 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2230 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2231 i, i, image->comps[i].dx, image->comps[i].dy);
2233 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2236 /* we try to correct */
2237 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2238 if (!image->comps[i].dx) {
2239 image->comps[i].dx = 1;
2240 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2241 i, image->comps[i].dx);
2243 if (!image->comps[i].dy) {
2244 image->comps[i].dy = 1;
2245 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2246 i, image->comps[i].dy);
2251 #endif /* USE_JPWL */
2253 /* TODO: unused ? */
2254 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
2255 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
2257 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
2258 image->comps[i].factor = cp->reduce; /* reducing factor per component */
2261 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2262 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2265 if (j2k->cp->correct) {
2266 /* if JPWL is on, we check whether TX errors have damaged
2267 too much the SIZ parameters */
2268 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
2269 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2270 "JPWL: bad number of tiles (%d x %d)\n",
2273 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2276 /* we try to correct */
2277 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2280 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2283 if (cp->tw > cp->max_tiles) {
2285 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
2286 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2287 cp->max_tiles, cp->tw);
2291 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2294 if (cp->th > cp->max_tiles) {
2296 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2297 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2298 cp->max_tiles, cp->th);
2302 #endif /* USE_JPWL */
2304 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2305 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
2306 cp->tileno_size = 0;
2309 if (j2k->cp->correct) {
2311 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2312 "JPWL: could not alloc tcps field of cp\n");
2313 if (!JPWL_ASSUME || JPWL_ASSUME) {
2314 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2319 #endif /* USE_JPWL */
2321 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2322 cp->tcps[i].POC = 0;
2323 cp->tcps[i].numpocs = 0;
2324 cp->tcps[i].first = 1;
2327 /* Initialization for PPM marker */
2329 cp->ppm_data = NULL;
2330 cp->ppm_data_first = NULL;
2331 cp->ppm_previous = 0;
2334 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2335 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2336 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2338 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
2339 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
2340 j2k->state = J2K_STATE_MH;
2343 if (j2k->cstr_info) {
2344 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2345 cstr_info->image_w = image->x1 - image->x0;
2346 cstr_info->image_h = image->y1 - image->y0;
2347 cstr_info->numcomps = image->numcomps;
2348 cstr_info->tw = cp->tw;
2349 cstr_info->th = cp->th;
2350 cstr_info->tile_x = cp->tdx;
2351 cstr_info->tile_y = cp->tdy;
2352 cstr_info->tile_Ox = cp->tx0;
2353 cstr_info->tile_Oy = cp->ty0;
2354 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
2360 * Reads a SIZ marker (image and tile size)
2361 * @param p_header_data the data contained in the SIZ box.
2362 * @param jp2 the jpeg2000 file codec.
2363 * @param p_header_size the size of the data contained in the SIZ marker.
2364 * @param p_manager the user event manager.
2366 opj_bool j2k_read_siz_v2 (
2367 opj_j2k_v2_t *p_j2k,
2368 OPJ_BYTE * p_header_data,
2369 OPJ_UINT32 p_header_size,
2370 struct opj_event_mgr * p_manager
2373 OPJ_UINT32 l_size, i;
2374 OPJ_UINT32 l_nb_comp;
2375 OPJ_UINT32 l_nb_comp_remain;
2376 OPJ_UINT32 l_remaining_size;
2377 OPJ_UINT32 l_nb_tiles;
2379 opj_image_t *l_image = 00;
2380 opj_cp_v2_t *l_cp = 00;
2381 opj_image_comp_t * l_img_comp = 00;
2382 opj_tcp_v2_t * l_current_tile_param = 00;
2385 assert(p_j2k != 00);
2386 assert(p_manager != 00);
2387 assert(p_header_data != 00);
2389 l_image = p_j2k->m_private_image;
2390 l_cp = &(p_j2k->m_cp);
2392 /* minimum size == 39 - 3 (= minimum component parameter) */
2393 if (p_header_size < 36) {
2394 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2398 l_remaining_size = p_header_size - 36;
2399 l_nb_comp = l_remaining_size / 3;
2400 l_nb_comp_remain = l_remaining_size % 3;
2401 if (l_nb_comp_remain != 0){
2402 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2406 l_size = p_header_size + 2; /* Lsiz */
2408 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2410 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2411 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2413 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2415 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2417 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2419 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2421 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2423 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2425 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2427 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2430 l_image->numcomps = (OPJ_UINT16) l_tmp;
2432 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2436 if (l_image->numcomps != l_nb_comp) {
2437 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);
2442 if (l_cp->correct) {
2443 /* if JPWL is on, we check whether TX errors have damaged
2444 too much the SIZ parameters */
2445 if (!(l_image->x1 * l_image->y1)) {
2446 opj_event_msg_v2(p_manager, EVT_ERROR,
2447 "JPWL: bad image size (%d x %d)\n",
2448 l_image->x1, l_image->y1);
2449 if (!JPWL_ASSUME || JPWL_ASSUME) {
2450 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2455 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2456 if (l_image->numcomps != ((len - 38) / 3)) {
2457 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2458 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2459 l_image->numcomps, ((len - 38) / 3));
2461 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2464 */ /* we try to correct */
2465 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2466 if (l_image->numcomps < ((len - 38) / 3)) {
2467 len = 38 + 3 * l_image->numcomps;
2468 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2471 l_image->numcomps = ((len - 38) / 3);
2472 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2478 /* update components number in the jpwl_exp_comps filed */
2479 l_cp->exp_comps = l_image->numcomps;
2481 #endif /* USE_JPWL */
2483 /* Allocate the resulting image components */
2484 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2485 if (l_image->comps == 00){
2486 l_image->numcomps = 0;
2487 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2491 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2492 l_img_comp = l_image->comps;
2494 /* Read the component information */
2495 for (i = 0; i < l_image->numcomps; ++i){
2497 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2499 l_img_comp->prec = (tmp & 0x7f) + 1;
2500 l_img_comp->sgnd = tmp >> 7;
2501 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2503 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2504 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2506 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2509 if (l_cp->correct) {
2510 /* if JPWL is on, we check whether TX errors have damaged
2511 too much the SIZ parameters, again */
2512 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2513 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2514 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2515 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2517 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2520 /* we try to correct */
2521 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2522 if (!l_image->comps[i].dx) {
2523 l_image->comps[i].dx = 1;
2524 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2525 i, l_image->comps[i].dx);
2527 if (!l_image->comps[i].dy) {
2528 l_image->comps[i].dy = 1;
2529 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2530 i, l_image->comps[i].dy);
2534 #endif /* USE_JPWL */
2535 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2536 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2540 /* Compute the number of tiles */
2541 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2542 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2543 l_nb_tiles = l_cp->tw * l_cp->th;
2545 /* Define the tiles which will be decoded */
2546 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2547 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;
2548 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;
2549 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);
2550 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);
2553 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2554 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2555 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2556 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2560 if (l_cp->correct) {
2561 /* if JPWL is on, we check whether TX errors have damaged
2562 too much the SIZ parameters */
2563 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2564 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2565 "JPWL: bad number of tiles (%d x %d)\n",
2566 l_cp->tw, l_cp->th);
2568 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2571 /* we try to correct */
2572 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2575 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2578 if (l_cp->tw > l_cp->max_tiles) {
2580 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2581 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2582 l_cp->max_tiles, l_cp->tw);
2586 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2589 if (l_cp->th > l_cp->max_tiles) {
2591 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2592 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2593 l_cp->max_tiles, l_cp->th);
2597 #endif /* USE_JPWL */
2599 /* memory allocations */
2600 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2601 if (l_cp->tcps == 00) {
2602 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2605 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2608 if (l_cp->correct) {
2610 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2611 "JPWL: could not alloc tcps field of cp\n");
2612 if (!JPWL_ASSUME || JPWL_ASSUME) {
2613 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2618 #endif /* USE_JPWL */
2620 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2621 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2622 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2623 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2626 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2628 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2629 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2631 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2632 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2635 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));
2636 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2638 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2639 (opj_simple_mcc_decorrelation_data_t*)
2640 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2642 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2643 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2646 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));
2647 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2649 /* set up default dc level shift */
2650 for (i=0;i<l_image->numcomps;++i) {
2651 if (! l_image->comps[i].sgnd) {
2652 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2656 l_current_tile_param = l_cp->tcps;
2657 for (i = 0; i < l_nb_tiles; ++i) {
2658 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2659 if (l_current_tile_param->tccps == 00) {
2660 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2663 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2665 ++l_current_tile_param;
2668 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2669 opj_image_comp_header_update(l_image,l_cp);
2676 static void j2k_write_com(opj_j2k_t *j2k) {
2680 if(j2k->cp->comment) {
2681 opj_cio_t *cio = j2k->cio;
2682 char *comment = j2k->cp->comment;
2684 cio_write(cio, J2K_MS_COM, 2);
2685 lenp = cio_tell(cio);
2687 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2688 for (i = 0; i < strlen(comment); i++) {
2689 cio_write(cio, comment[i], 1);
2691 len = cio_tell(cio) - lenp;
2692 cio_seek(cio, lenp);
2693 cio_write(cio, len, 2);
2694 cio_seek(cio, lenp + len);
2698 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2703 static void j2k_read_com(opj_j2k_t *j2k) {
2706 opj_cio_t *cio = j2k->cio;
2708 len = cio_read(cio, 2);
2709 cio_skip(cio, len - 2);
2712 * Reads a COM marker (comments)
2713 * @param p_header_data the data contained in the COM box.
2714 * @param jp2 the jpeg2000 file codec.
2715 * @param p_header_size the size of the data contained in the COM marker.
2716 * @param p_manager the user event manager.
2718 opj_bool j2k_read_com_v2 (
2719 opj_j2k_v2_t *p_j2k,
2720 OPJ_BYTE * p_header_data,
2721 OPJ_UINT32 p_header_size,
2722 struct opj_event_mgr * p_manager
2726 assert(p_j2k != 00);
2727 assert(p_manager != 00);
2728 assert(p_header_data != 00);
2729 (void)p_header_size;
2734 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2737 opj_cp_t *cp = j2k->cp;
2738 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2739 opj_tccp_t *tccp = &tcp->tccps[compno];
2740 opj_cio_t *cio = j2k->cio;
2742 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2743 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2744 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2745 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2746 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2748 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2749 for (i = 0; i < tccp->numresolutions; i++) {
2750 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2755 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2758 opj_cp_t *cp = j2k->cp;
2759 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2760 opj_tccp_t *tccp = &tcp->tccps[compno];
2761 opj_cio_t *cio = j2k->cio;
2763 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2765 /* If user wants to remove more resolutions than the codestream contains, return error*/
2766 assert(cp->reduce >= 0);
2767 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2768 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2769 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2770 j2k->state |= J2K_STATE_ERR;
2773 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2774 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2775 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2776 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2777 if (tccp->csty & J2K_CP_CSTY_PRT) {
2778 for (i = 0; i < tccp->numresolutions; i++) {
2779 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2780 tccp->prcw[i] = tmp & 0xf;
2781 tccp->prch[i] = tmp >> 4;
2786 if(j2k->cstr_info && compno == 0) {
2787 for (i = 0; i < tccp->numresolutions; i++) {
2788 if (tccp->csty & J2K_CP_CSTY_PRT) {
2789 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2790 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2793 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2794 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2801 static void j2k_write_cod(opj_j2k_t *j2k) {
2802 opj_cp_t *cp = NULL;
2803 opj_tcp_t *tcp = NULL;
2806 opj_cio_t *cio = j2k->cio;
2808 cio_write(cio, J2K_MS_COD, 2); /* COD */
2810 lenp = cio_tell(cio);
2814 tcp = &cp->tcps[j2k->curtileno];
2816 cio_write(cio, tcp->csty, 1); /* Scod */
2817 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2818 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2819 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2821 j2k_write_cox(j2k, 0);
2822 len = cio_tell(cio) - lenp;
2823 cio_seek(cio, lenp);
2824 cio_write(cio, len, 2); /* Lcod */
2825 cio_seek(cio, lenp + len);
2828 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2833 * Writes the COD marker (Coding style default)
2835 * @param p_stream the stream to write data to.
2836 * @param p_j2k J2K codec.
2837 * @param p_manager the user event manager.
2839 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2840 struct opj_stream_private *p_stream,
2841 struct opj_event_mgr * p_manager )
2843 opj_cp_v2_t *l_cp = 00;
2844 opj_tcp_v2_t *l_tcp = 00;
2845 OPJ_UINT32 l_code_size,l_remaining_size;
2846 OPJ_BYTE * l_current_data = 00;
2849 assert(p_j2k != 00);
2850 assert(p_manager != 00);
2851 assert(p_stream != 00);
2853 l_cp = &(p_j2k->m_cp);
2854 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2855 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2856 l_remaining_size = l_code_size;
2858 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2859 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2860 = (OPJ_BYTE*)opj_realloc(
2861 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2864 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2868 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2871 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2873 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2874 l_current_data += 2;
2876 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2877 l_current_data += 2;
2879 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2882 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2885 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2888 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2891 l_remaining_size -= 9;
2893 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2894 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2898 if (l_remaining_size != 0) {
2899 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2903 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) {
2910 static void j2k_read_cod(opj_j2k_t *j2k) {
2914 opj_cio_t *cio = j2k->cio;
2915 opj_cp_t *cp = j2k->cp;
2916 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2917 opj_image_t *image = j2k->image;
2919 len = cio_read(cio, 2); /* Lcod */
2920 tcp->csty = cio_read(cio, 1); /* Scod */
2921 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
2922 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
2923 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
2925 pos = cio_tell(cio);
2926 for (i = 0; i < image->numcomps; i++) {
2927 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
2929 j2k_read_cox(j2k, i);
2933 if (j2k->cstr_info) {
2934 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2935 cstr_info->prog = tcp->prg;
2936 cstr_info->numlayers = tcp->numlayers;
2937 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
2938 for (i = 0; i < image->numcomps; i++) {
2939 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
2945 * Reads a COD marker (Coding Styke defaults)
2946 * @param p_header_data the data contained in the COD box.
2947 * @param p_j2k the jpeg2000 codec.
2948 * @param p_header_size the size of the data contained in the COD marker.
2949 * @param p_manager the user event manager.
2951 opj_bool j2k_read_cod_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
2961 opj_cp_v2_t *l_cp = 00;
2962 opj_tcp_v2_t *l_tcp = 00;
2963 opj_image_t *l_image = 00;
2966 assert(p_header_data != 00);
2967 assert(p_j2k != 00);
2968 assert(p_manager != 00);
2970 l_image = p_j2k->m_private_image;
2971 l_cp = &(p_j2k->m_cp);
2973 /* If we are in the first tile-part header of the current tile */
2974 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2975 &l_cp->tcps[p_j2k->m_current_tile_number] :
2976 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2978 /* Make sure room is sufficient */
2979 if (p_header_size < 5) {
2980 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2984 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2986 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2988 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2989 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2992 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2993 if (l_cp->m_specific_param.m_dec.m_layer) {
2994 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2997 l_tcp->num_layers_to_decode = l_tcp->numlayers;
3000 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
3004 for (i = 0; i < l_image->numcomps; ++i) {
3005 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
3008 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3009 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3013 if (p_header_size != 0) {
3014 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3018 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
3019 j2k_copy_tile_component_parameters(p_j2k);
3022 #ifdef WIP_REMOVE_MSD
3023 if (p_j2k->cstr_info) {
3024 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
3025 p_j2k->cstr_info->prog = l_tcp->prg;
3026 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
3027 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
3028 for (i = 0; i < l_image->numcomps; ++i) {
3029 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
3037 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
3040 opj_cp_t *cp = j2k->cp;
3041 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3042 opj_image_t *image = j2k->image;
3043 opj_cio_t *cio = j2k->cio;
3045 cio_write(cio, J2K_MS_COC, 2); /* COC */
3046 lenp = cio_tell(cio);
3048 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3049 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
3050 j2k_write_cox(j2k, compno);
3051 len = cio_tell(cio) - lenp;
3052 cio_seek(cio, lenp);
3053 cio_write(cio, len, 2); /* Lcoc */
3054 cio_seek(cio, lenp + len);
3058 * Writes the COC marker (Coding style component)
3060 * @param p_comp_no the index of the component to output.
3061 * @param p_stream the stream to write data to.
3062 * @param p_j2k J2K codec.
3063 * @param p_manager the user event manager.
3065 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
3066 OPJ_UINT32 p_comp_no,
3067 struct opj_stream_private *p_stream,
3068 struct opj_event_mgr * p_manager )
3070 OPJ_UINT32 l_coc_size,l_remaining_size;
3071 OPJ_UINT32 l_comp_room;
3074 assert(p_j2k != 00);
3075 assert(p_manager != 00);
3076 assert(p_stream != 00);
3078 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3080 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3082 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3083 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3084 = (OPJ_BYTE*)opj_realloc(
3085 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3087 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3091 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3094 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);
3096 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) {
3104 * Writes the COC marker (Coding style component)
3106 * @param p_comp_no the index of the component to output.
3107 * @param p_stream the stream to write data to.
3108 * @param p_j2k J2K codec.
3109 * @param p_manager the user event manager.
3111 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3112 OPJ_UINT32 p_comp_no,
3114 OPJ_UINT32 * p_data_written,
3115 struct opj_event_mgr * p_manager )
3117 opj_cp_v2_t *l_cp = 00;
3118 opj_tcp_v2_t *l_tcp = 00;
3119 OPJ_UINT32 l_coc_size,l_remaining_size;
3120 OPJ_BYTE * l_current_data = 00;
3121 opj_image_t *l_image = 00;
3122 OPJ_UINT32 l_comp_room;
3125 assert(p_j2k != 00);
3126 assert(p_manager != 00);
3128 l_cp = &(p_j2k->m_cp);
3129 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3130 l_image = p_j2k->m_private_image;
3131 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3133 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3134 l_remaining_size = l_coc_size;
3136 l_current_data = p_data;
3138 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3139 l_current_data += 2;
3141 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3142 l_current_data += 2;
3144 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3145 l_current_data+=l_comp_room;
3147 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3150 l_remaining_size -= (5 + l_comp_room);
3151 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3152 * p_data_written = l_coc_size;
3156 * Gets the maximum size taken by a coc.
3158 * @param p_j2k the jpeg2000 codec to use.
3160 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3163 OPJ_UINT32 l_nb_comp;
3164 OPJ_UINT32 l_nb_tiles;
3165 OPJ_UINT32 l_max = 0;
3169 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3170 l_nb_comp = p_j2k->m_private_image->numcomps;
3172 for (i=0;i<l_nb_tiles;++i) {
3173 for (j=0;j<l_nb_comp;++j) {
3174 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3181 static void j2k_read_coc(opj_j2k_t *j2k) {
3184 opj_cp_t *cp = j2k->cp;
3185 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3186 opj_image_t *image = j2k->image;
3187 opj_cio_t *cio = j2k->cio;
3189 len = cio_read(cio, 2); /* Lcoc */
3190 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3191 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
3192 j2k_read_cox(j2k, compno);
3196 * Reads a COC marker (Coding Style Component)
3197 * @param p_header_data the data contained in the COC box.
3198 * @param p_j2k the jpeg2000 codec.
3199 * @param p_header_size the size of the data contained in the COC marker.
3200 * @param p_manager the user event manager.
3202 opj_bool j2k_read_coc_v2 (
3203 opj_j2k_v2_t *p_j2k,
3204 OPJ_BYTE * p_header_data,
3205 OPJ_UINT32 p_header_size,
3206 struct opj_event_mgr * p_manager
3209 opj_cp_v2_t *l_cp = NULL;
3210 opj_tcp_v2_t *l_tcp = NULL;
3211 opj_image_t *l_image = NULL;
3212 OPJ_UINT32 l_comp_room;
3213 OPJ_UINT32 l_comp_no;
3216 assert(p_header_data != 00);
3217 assert(p_j2k != 00);
3218 assert(p_manager != 00);
3220 l_cp = &(p_j2k->m_cp);
3221 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3222 &l_cp->tcps[p_j2k->m_current_tile_number] :
3223 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3224 l_image = p_j2k->m_private_image;
3226 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3228 /* make sure room is sufficient*/
3229 if (p_header_size < l_comp_room + 1) {
3230 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3233 p_header_size -= l_comp_room + 1;
3235 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3236 p_header_data += l_comp_room;
3237 if (l_comp_no >= l_image->numcomps) {
3238 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3242 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3245 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3246 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3250 if (p_header_size != 0) {
3251 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3257 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3258 int bandno, numbands;
3261 opj_cp_t *cp = j2k->cp;
3262 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3263 opj_tccp_t *tccp = &tcp->tccps[compno];
3264 opj_cio_t *cio = j2k->cio;
3266 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3267 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3269 for (bandno = 0; bandno < numbands; bandno++) {
3270 expn = tccp->stepsizes[bandno].expn;
3271 mant = tccp->stepsizes[bandno].mant;
3273 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3274 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3276 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3281 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3283 int bandno, numbands;
3285 opj_cp_t *cp = j2k->cp;
3286 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3287 opj_tccp_t *tccp = &tcp->tccps[compno];
3288 opj_cio_t *cio = j2k->cio;
3290 tmp = cio_read(cio, 1); /* Sqcx */
3291 tccp->qntsty = tmp & 0x1f;
3292 tccp->numgbits = tmp >> 5;
3293 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3294 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3297 if (j2k->cp->correct) {
3299 /* if JPWL is on, we check whether there are too many subbands */
3300 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3301 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3302 "JPWL: bad number of subbands in Sqcx (%d)\n",
3305 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3308 /* we try to correct */
3310 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3311 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3318 /* We check whether there are too many subbands */
3319 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3320 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3321 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3322 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3325 #endif /* USE_JPWL */
3327 for (bandno = 0; bandno < numbands; bandno++) {
3329 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3330 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3333 tmp = cio_read(cio, 2); /* SPqcx_i */
3337 if (bandno < J2K_MAXBANDS){
3338 tccp->stepsizes[bandno].expn = expn;
3339 tccp->stepsizes[bandno].mant = mant;
3343 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3344 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3345 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3346 tccp->stepsizes[bandno].expn =
3347 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3348 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3349 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3355 static void j2k_write_qcd(opj_j2k_t *j2k) {
3358 opj_cio_t *cio = j2k->cio;
3360 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3361 lenp = cio_tell(cio);
3363 j2k_write_qcx(j2k, 0);
3364 len = cio_tell(cio) - lenp;
3365 cio_seek(cio, lenp);
3366 cio_write(cio, len, 2); /* Lqcd */
3367 cio_seek(cio, lenp + len);
3370 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3374 * Writes the QCD marker (quantization default)
3376 * @param p_comp_number the index of the component to output.
3377 * @param p_stream the stream to write data to.
3378 * @param p_j2k J2K codec.
3379 * @param p_manager the user event manager.
3381 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3382 struct opj_stream_private *p_stream,
3383 struct opj_event_mgr * p_manager )
3385 opj_cp_v2_t *l_cp = 00;
3386 opj_tcp_v2_t *l_tcp = 00;
3387 OPJ_UINT32 l_qcd_size,l_remaining_size;
3388 OPJ_BYTE * l_current_data = 00;
3391 assert(p_j2k != 00);
3392 assert(p_manager != 00);
3393 assert(p_stream != 00);
3395 l_cp = &(p_j2k->m_cp);
3396 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3397 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3398 l_remaining_size = l_qcd_size;
3400 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3401 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3402 = (OPJ_BYTE*)opj_realloc(
3403 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3406 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3410 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3413 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3415 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3416 l_current_data += 2;
3418 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3419 l_current_data += 2;
3421 l_remaining_size -= 4;
3423 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3424 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3428 if (l_remaining_size != 0) {
3429 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3433 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) {
3441 static void j2k_read_qcd(opj_j2k_t *j2k) {
3445 opj_cio_t *cio = j2k->cio;
3446 opj_image_t *image = j2k->image;
3448 len = cio_read(cio, 2); /* Lqcd */
3449 pos = cio_tell(cio);
3450 for (i = 0; i < image->numcomps; i++) {
3452 j2k_read_qcx(j2k, i, len - 2);
3457 * Reads a QCD marker (Quantization defaults)
3458 * @param p_header_data the data contained in the QCD box.
3459 * @param p_j2k the jpeg2000 codec.
3460 * @param p_header_size the size of the data contained in the QCD marker.
3461 * @param p_manager the user event manager.
3463 opj_bool j2k_read_qcd_v2 (
3464 opj_j2k_v2_t *p_j2k,
3465 OPJ_BYTE * p_header_data,
3466 OPJ_UINT32 p_header_size,
3467 struct opj_event_mgr * p_manager
3471 assert(p_header_data != 00);
3472 assert(p_j2k != 00);
3473 assert(p_manager != 00);
3475 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3476 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3480 if (p_header_size != 0) {
3481 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3485 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3486 j2k_copy_tile_quantization_parameters(p_j2k);
3491 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3494 opj_cio_t *cio = j2k->cio;
3496 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3497 lenp = cio_tell(cio);
3499 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3500 j2k_write_qcx(j2k, compno);
3501 len = cio_tell(cio) - lenp;
3502 cio_seek(cio, lenp);
3503 cio_write(cio, len, 2); /* Lqcc */
3504 cio_seek(cio, lenp + len);
3508 * Writes the QCC marker (quantization component)
3510 * @param p_comp_no the index of the component to output.
3511 * @param p_stream the stream to write data to.
3512 * @param p_j2k J2K codec.
3513 * @param p_manager the user event manager.
3515 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3516 OPJ_UINT32 p_comp_no,
3517 struct opj_stream_private *p_stream,
3518 struct opj_event_mgr * p_manager )
3520 OPJ_UINT32 l_qcc_size,l_remaining_size;
3523 assert(p_j2k != 00);
3524 assert(p_manager != 00);
3525 assert(p_stream != 00);
3527 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3528 l_remaining_size = l_qcc_size;
3530 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3531 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3532 = (OPJ_BYTE*)opj_realloc(
3533 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3535 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3539 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3542 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);
3544 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) {
3552 * Writes the QCC marker (quantization component)
3554 * @param p_comp_no the index of the component to output.
3555 * @param p_stream the stream to write data to.
3556 * @param p_j2k J2K codec.
3557 * @param p_manager the user event manager.
3559 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3560 OPJ_UINT32 p_comp_no,
3562 OPJ_UINT32 * p_data_written,
3563 struct opj_event_mgr * p_manager )
3565 OPJ_UINT32 l_qcc_size,l_remaining_size;
3566 OPJ_BYTE * l_current_data = 00;
3569 assert(p_j2k != 00);
3570 assert(p_manager != 00);
3572 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3573 l_remaining_size = l_qcc_size;
3575 l_current_data = p_data;
3577 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3578 l_current_data += 2;
3580 if (p_j2k->m_private_image->numcomps <= 256) {
3583 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3584 l_current_data += 2;
3586 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3589 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3590 l_remaining_size -= 6;
3593 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3594 l_current_data += 2;
3596 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3599 l_remaining_size -= 6;
3602 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3604 *p_data_written = l_qcc_size;
3608 * Gets the maximum size taken by a qcc.
3610 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3612 return j2k_get_max_coc_size(p_j2k);
3615 static void j2k_read_qcc(opj_j2k_t *j2k) {
3617 int numcomp = j2k->image->numcomps;
3618 opj_cio_t *cio = j2k->cio;
3620 len = cio_read(cio, 2); /* Lqcc */
3621 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3624 if (j2k->cp->correct) {
3626 static int backup_compno = 0;
3628 /* compno is negative or larger than the number of components!!! */
3629 if ((compno < 0) || (compno >= numcomp)) {
3630 opj_event_msg(j2k->cinfo, EVT_ERROR,
3631 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3634 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3637 /* we try to correct */
3638 compno = backup_compno % numcomp;
3639 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3640 "- setting component number to %d\n",
3644 /* keep your private count of tiles */
3647 #endif /* USE_JPWL */
3649 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3653 * Reads a QCC marker (Quantization component)
3654 * @param p_header_data the data contained in the QCC box.
3655 * @param p_j2k the jpeg2000 codec.
3656 * @param p_header_size the size of the data contained in the QCC marker.
3657 * @param p_manager the user event manager.
3659 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3660 OPJ_BYTE * p_header_data,
3661 OPJ_UINT32 p_header_size,
3662 struct opj_event_mgr * p_manager)
3664 OPJ_UINT32 l_num_comp,l_comp_no;
3667 assert(p_header_data != 00);
3668 assert(p_j2k != 00);
3669 assert(p_manager != 00);
3671 l_num_comp = p_j2k->m_private_image->numcomps;
3673 if (l_num_comp <= 256) {
3674 if (p_header_size < 1) {
3675 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3678 opj_read_bytes(p_header_data,&l_comp_no,1);
3683 if (p_header_size < 2) {
3684 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3687 opj_read_bytes(p_header_data,&l_comp_no,2);
3693 if (p_j2k->m_cp.correct) {
3695 static OPJ_UINT32 backup_compno = 0;
3697 /* compno is negative or larger than the number of components!!! */
3698 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3699 opj_event_msg_v2(p_manager, EVT_ERROR,
3700 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3701 l_comp_no, l_num_comp);
3703 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3706 /* we try to correct */
3707 l_comp_no = backup_compno % l_num_comp;
3708 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3709 "- setting component number to %d\n",
3713 /* keep your private count of tiles */
3716 #endif /* USE_JPWL */
3718 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3719 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3723 if (p_header_size != 0) {
3724 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3732 static void j2k_write_poc(opj_j2k_t *j2k) {
3733 int len, numpchgs, i;
3735 int numcomps = j2k->image->numcomps;
3737 opj_cp_t *cp = j2k->cp;
3738 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3739 opj_tccp_t *tccp = &tcp->tccps[0];
3740 opj_cio_t *cio = j2k->cio;
3742 numpchgs = 1 + tcp->numpocs;
3743 cio_write(cio, J2K_MS_POC, 2); /* POC */
3744 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3745 cio_write(cio, len, 2); /* Lpoc */
3746 for (i = 0; i < numpchgs; i++) {
3747 opj_poc_t *poc = &tcp->pocs[i];
3748 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3749 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3750 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3751 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3752 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3753 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3754 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3755 poc->compno1 = int_min(poc->compno1, numcomps);
3756 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3761 * Writes the POC marker (Progression Order Change)
3763 * @param p_stream the stream to write data to.
3764 * @param p_j2k J2K codec.
3765 * @param p_manager the user event manager.
3767 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3769 OPJ_UINT32 * p_data_written,
3770 struct opj_event_mgr * p_manager )
3773 OPJ_BYTE * l_current_data = 00;
3774 OPJ_UINT32 l_nb_comp;
3775 OPJ_UINT32 l_nb_poc;
3776 OPJ_UINT32 l_poc_size;
3777 opj_image_t *l_image = 00;
3778 opj_tcp_v2_t *l_tcp = 00;
3779 opj_tccp_t *l_tccp = 00;
3780 opj_poc_t *l_current_poc = 00;
3781 OPJ_UINT32 l_poc_room;
3784 assert(p_j2k != 00);
3785 assert(p_manager != 00);
3787 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3788 l_tccp = &l_tcp->tccps[0];
3789 l_image = p_j2k->m_private_image;
3790 l_nb_comp = l_image->numcomps;
3791 l_nb_poc = 1 + l_tcp->numpocs;
3793 if (l_nb_comp <= 256) {
3800 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3802 l_current_data = p_data;
3804 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3805 l_current_data += 2;
3807 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3808 l_current_data += 2;
3810 l_current_poc = l_tcp->pocs;
3811 for (i = 0; i < l_nb_poc; ++i) {
3812 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3815 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3816 l_current_data+=l_poc_room;
3818 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3821 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3824 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3825 l_current_data+=l_poc_room;
3827 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3830 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3831 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3832 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3833 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3838 *p_data_written = l_poc_size;
3842 * Gets the maximum size taken by the writing of a POC.
3844 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3846 opj_tcp_v2_t * l_tcp = 00;
3847 OPJ_UINT32 l_nb_tiles = 0;
3848 OPJ_UINT32 l_max_poc = 0;
3851 l_tcp = p_j2k->m_cp.tcps;
3852 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3854 for (i=0;i<l_nb_tiles;++i) {
3855 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3861 return 4 + 9 * l_max_poc;
3865 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3867 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3870 OPJ_UINT32 l_nb_tiles;
3871 OPJ_UINT32 l_max = 0;
3872 opj_tcp_v2_t * l_tcp = 00;
3874 l_tcp = p_j2k->m_cp.tcps;
3875 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3877 for (i=0;i<l_nb_tiles;++i) {
3878 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3888 * Gets the maximum size taken by the headers of the SOT.
3890 * @param p_j2k the jpeg2000 codec to use.
3892 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3894 OPJ_UINT32 l_nb_bytes = 0;
3895 OPJ_UINT32 l_nb_comps;
3896 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3898 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3899 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3901 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3902 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3903 l_nb_bytes += l_nb_comps * l_coc_bytes;
3905 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3906 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3909 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3911 /*** DEVELOPER CORNER, Add room for your headers ***/
3916 static void j2k_read_poc(opj_j2k_t *j2k) {
3917 int len, numpchgs, i, old_poc;
3919 int numcomps = j2k->image->numcomps;
3921 opj_cp_t *cp = j2k->cp;
3922 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3923 opj_cio_t *cio = j2k->cio;
3925 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
3927 len = cio_read(cio, 2); /* Lpoc */
3928 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
3930 for (i = old_poc; i < numpchgs + old_poc; i++) {
3932 poc = &tcp->pocs[i];
3933 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
3934 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
3935 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
3936 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
3937 poc->compno1 = int_min(
3938 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
3939 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
3942 tcp->numpocs = numpchgs + old_poc - 1;
3946 * Reads a POC marker (Progression Order Change)
3948 * @param p_header_data the data contained in the POC box.
3949 * @param p_j2k the jpeg2000 codec.
3950 * @param p_header_size the size of the data contained in the POC marker.
3951 * @param p_manager the user event manager.
3953 opj_bool j2k_read_poc_v2 (
3954 opj_j2k_v2_t *p_j2k,
3955 OPJ_BYTE * p_header_data,
3956 OPJ_UINT32 p_header_size,
3957 struct opj_event_mgr * p_manager)
3959 OPJ_UINT32 i, l_nb_comp, l_tmp;
3960 opj_image_t * l_image = 00;
3961 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3962 OPJ_UINT32 l_chunk_size, l_comp_room;
3964 opj_cp_v2_t *l_cp = 00;
3965 opj_tcp_v2_t *l_tcp = 00;
3966 opj_poc_t *l_current_poc = 00;
3969 assert(p_header_data != 00);
3970 assert(p_j2k != 00);
3971 assert(p_manager != 00);
3973 l_image = p_j2k->m_private_image;
3974 l_nb_comp = l_image->numcomps;
3975 if (l_nb_comp <= 256) {
3981 l_chunk_size = 5 + 2 * l_comp_room;
3982 l_current_poc_nb = p_header_size / l_chunk_size;
3983 l_current_poc_remaining = p_header_size % l_chunk_size;
3985 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3986 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3990 l_cp = &(p_j2k->m_cp);
3991 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3992 &l_cp->tcps[p_j2k->m_current_tile_number] :
3993 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3994 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3995 l_current_poc_nb += l_old_poc_nb;
3997 assert(l_current_poc_nb < 32);
3999 /* now poc is in use.*/
4002 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
4003 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
4004 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
4006 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
4007 p_header_data+=l_comp_room;
4008 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
4010 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
4012 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
4013 p_header_data+=l_comp_room;
4014 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
4016 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4017 /* make sure comp is in acceptable bounds */
4018 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4022 l_tcp->numpocs = l_current_poc_nb - 1;
4026 static void j2k_read_crg(opj_j2k_t *j2k) {
4027 int len, i, Xcrg_i, Ycrg_i;
4029 opj_cio_t *cio = j2k->cio;
4030 int numcomps = j2k->image->numcomps;
4032 len = cio_read(cio, 2); /* Lcrg */
4033 for (i = 0; i < numcomps; i++) {
4034 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
4035 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4040 * Reads a CRG marker (Component registration)
4042 * @param p_header_data the data contained in the TLM box.
4043 * @param p_j2k the jpeg2000 codec.
4044 * @param p_header_size the size of the data contained in the TLM marker.
4045 * @param p_manager the user event manager.
4047 opj_bool j2k_read_crg_v2 (
4048 opj_j2k_v2_t *p_j2k,
4049 OPJ_BYTE * p_header_data,
4050 OPJ_UINT32 p_header_size,
4051 struct opj_event_mgr * p_manager
4054 OPJ_UINT32 l_nb_comp;
4056 assert(p_header_data != 00);
4057 assert(p_j2k != 00);
4058 assert(p_manager != 00);
4060 l_nb_comp = p_j2k->m_private_image->numcomps;
4062 if (p_header_size != l_nb_comp *4) {
4063 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4066 /* Do not care of this at the moment since only local variables are set here */
4069 (i = 0; i < l_nb_comp; ++i)
4071 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4073 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4080 static void j2k_read_tlm(opj_j2k_t *j2k) {
4081 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4082 long int Ttlm_i, Ptlm_i;
4084 opj_cio_t *cio = j2k->cio;
4086 len = cio_read(cio, 2); /* Ltlm */
4087 Ztlm = cio_read(cio, 1); /* Ztlm */
4088 Stlm = cio_read(cio, 1); /* Stlm */
4089 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4090 SP = (Stlm >> 6) & 0x01;
4091 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4092 for (i = 0; i < tile_tlm; i++) {
4093 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4094 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4099 * Reads a TLM marker (Tile Length Marker)
4101 * @param p_header_data the data contained in the TLM box.
4102 * @param p_j2k the jpeg2000 codec.
4103 * @param p_header_size the size of the data contained in the TLM marker.
4104 * @param p_manager the user event manager.
4106 opj_bool j2k_read_tlm_v2 (
4107 opj_j2k_v2_t *p_j2k,
4108 OPJ_BYTE * p_header_data,
4109 OPJ_UINT32 p_header_size,
4110 struct opj_event_mgr * p_manager
4113 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4115 assert(p_header_data != 00);
4116 assert(p_j2k != 00);
4117 assert(p_manager != 00);
4119 if (p_header_size < 2) {
4120 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4125 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4127 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4130 l_ST = ((l_Stlm >> 4) & 0x3);
4131 l_SP = (l_Stlm >> 6) & 0x1;
4133 l_Ptlm_size = (l_SP + 1) * 2;
4134 l_quotient = l_Ptlm_size + l_ST;
4136 l_tot_num_tp = p_header_size / l_quotient;
4137 l_tot_num_tp_remaining = p_header_size % l_quotient;
4139 if (l_tot_num_tp_remaining != 0) {
4140 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4143 /* FIXME Do not care of this at the moment since only local variables are set here */
4146 (i = 0; i < l_tot_num_tp; ++i)
4148 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4149 p_header_data += l_ST;
4150 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4151 p_header_data += l_Ptlm_size;
4156 static void j2k_read_plm(opj_j2k_t *j2k) {
4157 int len, i, Zplm, Nplm, add, packet_len = 0;
4159 opj_cio_t *cio = j2k->cio;
4161 len = cio_read(cio, 2); /* Lplm */
4162 Zplm = cio_read(cio, 1); /* Zplm */
4165 Nplm = cio_read(cio, 4); /* Nplm */
4167 for (i = Nplm; i > 0; i--) {
4168 add = cio_read(cio, 1);
4170 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4171 if ((add & 0x80) == 0) {
4182 * Reads a PLM marker (Packet length, main header marker)
4184 * @param p_header_data the data contained in the TLM box.
4185 * @param p_j2k the jpeg2000 codec.
4186 * @param p_header_size the size of the data contained in the TLM marker.
4187 * @param p_manager the user event manager.
4189 opj_bool j2k_read_plm_v2 (
4190 opj_j2k_v2_t *p_j2k,
4191 OPJ_BYTE * p_header_data,
4192 OPJ_UINT32 p_header_size,
4193 struct opj_event_mgr * p_manager
4197 assert(p_header_data != 00);
4198 assert(p_j2k != 00);
4199 assert(p_manager != 00);
4201 if (p_header_size < 1) {
4202 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4205 /* Do not care of this at the moment since only local variables are set here */
4207 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4214 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4216 p_header_size -= (1+l_Nplm);
4220 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4224 (i = 0; i < l_Nplm; ++i)
4226 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4228 // take only the last seven bytes
4229 l_packet_len |= (l_tmp & 0x7f);
4237 // store packet length and proceed to next packet
4244 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4252 static void j2k_read_plt(opj_j2k_t *j2k) {
4253 int len, i, Zplt, packet_len = 0, add;
4255 opj_cio_t *cio = j2k->cio;
4257 len = cio_read(cio, 2); /* Lplt */
4258 Zplt = cio_read(cio, 1); /* Zplt */
4259 for (i = len - 3; i > 0; i--) {
4260 add = cio_read(cio, 1);
4261 packet_len = (packet_len << 7) + add; /* Iplt_i */
4262 if ((add & 0x80) == 0) {
4270 * Reads a PLT marker (Packet length, tile-part header)
4272 * @param p_header_data the data contained in the PLT box.
4273 * @param p_j2k the jpeg2000 codec.
4274 * @param p_header_size the size of the data contained in the PLT marker.
4275 * @param p_manager the user event manager.
4277 opj_bool j2k_read_plt_v2 (
4278 opj_j2k_v2_t *p_j2k,
4279 OPJ_BYTE * p_header_data,
4280 OPJ_UINT32 p_header_size,
4281 struct opj_event_mgr * p_manager
4284 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4287 assert(p_header_data != 00);
4288 assert(p_j2k != 00);
4289 assert(p_manager != 00);
4291 if (p_header_size < 1) {
4292 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4296 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4300 for (i = 0; i < p_header_size; ++i) {
4301 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4303 /* take only the last seven bytes */
4304 l_packet_len |= (l_tmp & 0x7f);
4309 /* store packet length and proceed to next packet */
4314 if (l_packet_len != 0) {
4315 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4322 static void j2k_read_ppm(opj_j2k_t *j2k) {
4323 int len, Z_ppm, i, j;
4326 opj_cp_t *cp = j2k->cp;
4327 opj_cio_t *cio = j2k->cio;
4329 len = cio_read(cio, 2);
4332 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4335 if (cp->ppm_previous == 0) {
4336 N_ppm = cio_read(cio, 4); /* N_ppm */
4339 N_ppm = cp->ppm_previous;
4342 if (Z_ppm == 0) { /* First PPM marker */
4343 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4344 cp->ppm_data_first = cp->ppm_data;
4345 cp->ppm_len = N_ppm;
4346 } else { /* NON-first PPM marker */
4347 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4350 /* this memory allocation check could be done even in non-JPWL cases */
4352 if (!cp->ppm_data) {
4353 opj_event_msg(j2k->cinfo, EVT_ERROR,
4354 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4356 if (!JPWL_ASSUME || JPWL_ASSUME) {
4357 opj_free(cp->ppm_data);
4358 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4365 cp->ppm_data_first = cp->ppm_data;
4366 cp->ppm_len = N_ppm + cp->ppm_store;
4368 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4369 cp->ppm_data[j] = cio_read(cio, 1);
4373 break; /* Case of non-finished packet header in present marker but finished in next one */
4375 cp->ppm_previous = i - 1;
4380 * Reads a PPM marker (Packed packet headers, main header)
4382 * @param p_header_data the data contained in the POC box.
4383 * @param p_j2k the jpeg2000 codec.
4384 * @param p_header_size the size of the data contained in the POC marker.
4385 * @param p_manager the user event manager.
4388 opj_bool j2k_read_ppm_v2 (
4389 opj_j2k_v2_t *p_j2k,
4390 OPJ_BYTE * p_header_data,
4391 OPJ_UINT32 p_header_size,
4392 struct opj_event_mgr * p_manager
4396 opj_cp_v2_t *l_cp = 00;
4397 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4400 assert(p_header_data != 00);
4401 assert(p_j2k != 00);
4402 assert(p_manager != 00);
4404 if (p_header_size < 1) {
4405 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4409 l_cp = &(p_j2k->m_cp);
4412 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4416 /* First PPM marker */
4418 if (p_header_size < 4) {
4419 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4423 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4427 /* First PPM marker: Initialization */
4428 l_cp->ppm_len = l_N_ppm;
4429 l_cp->ppm_data_size = 0;
4431 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4432 if (l_cp->ppm_buffer == 00) {
4433 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4436 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4438 l_cp->ppm_data = l_cp->ppm_buffer;
4442 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4443 if (p_header_size >= 4) {
4445 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4448 l_cp->ppm_len += l_N_ppm ;
4450 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4451 if (l_cp->ppm_buffer == 00) {
4452 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4455 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4457 l_cp->ppm_data = l_cp->ppm_buffer;
4464 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4466 if (l_remaining_data <= p_header_size) {
4467 /* we must store less information than available in the packet */
4468 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4469 l_cp->ppm_data_size = l_cp->ppm_len;
4470 p_header_size -= l_remaining_data;
4471 p_header_data += l_remaining_data;
4474 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4475 l_cp->ppm_data_size += p_header_size;
4476 p_header_data += p_header_size;
4489 * Reads a PPM marker (Packed packet headers, main header)
4491 * @param p_header_data the data contained in the POC box.
4492 * @param p_j2k the jpeg2000 codec.
4493 * @param p_header_size the size of the data contained in the POC marker.
4494 * @param p_manager the user event manager.
4496 opj_bool j2k_read_ppm_v3 (
4497 opj_j2k_v2_t *p_j2k,
4498 OPJ_BYTE * p_header_data,
4499 OPJ_UINT32 p_header_size,
4500 struct opj_event_mgr * p_manager
4503 opj_cp_v2_t *l_cp = 00;
4504 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4507 assert(p_header_data != 00);
4508 assert(p_j2k != 00);
4509 assert(p_manager != 00);
4511 /* Minimum size of PPM marker is equal to the size of Zppm element */
4512 if (p_header_size < 1) {
4513 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4517 l_cp = &(p_j2k->m_cp);
4520 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4524 /* First PPM marker */
4526 /* We need now at least the Nppm^0 element */
4527 if (p_header_size < 4) {
4528 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4532 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4536 /* First PPM marker: Initialization */
4537 l_cp->ppm_len = l_N_ppm;
4538 l_cp->ppm_data_read = 0;
4540 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4541 if (l_cp->ppm_data == 00) {
4542 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4545 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4547 l_cp->ppm_data_current = l_cp->ppm_data;
4549 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4552 if (p_header_size < 4) {
4553 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4557 /* Uncompleted Ippm series in the previous PPM marker?*/
4558 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4559 /* Get the place where add the remaining Ippm series*/
4560 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4561 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4564 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4568 /* Increase the size of ppm_data to add the new Ippm series*/
4569 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4571 /* Keep the position of the place where concatenate the new series*/
4572 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4573 l_cp->ppm_len += l_N_ppm;
4578 l_remaining_data = p_header_size;
4580 while (l_remaining_data >= l_N_ppm) {
4581 /* read a complete Ippm series*/
4582 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4583 p_header_size -= l_N_ppm;
4584 p_header_data += l_N_ppm;
4586 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4590 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4595 l_remaining_data = p_header_size;
4599 l_remaining_data = p_header_size;
4601 /* Next Ippm series is a complete series ?*/
4602 if (l_remaining_data > l_N_ppm) {
4603 /* Increase the size of ppm_data to add the new Ippm series*/
4604 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4606 /* Keep the position of the place where concatenate the new series */
4607 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4608 l_cp->ppm_len += l_N_ppm;
4613 /* Need to read an incomplete Ippm series*/
4614 if (l_remaining_data) {
4615 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4617 /* Keep the position of the place where concatenate the new series*/
4618 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4619 l_cp->ppm_len += l_N_ppm;
4621 /* Read incomplete Ippm series*/
4622 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4623 p_header_size -= l_remaining_data;
4624 p_header_data += l_remaining_data;
4626 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4631 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4632 if (p_header_size >= 4) {
4634 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4637 l_cp->ppm_len += l_N_ppm ;
4639 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4640 if (l_cp->ppm_buffer == 00) {
4641 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4644 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4646 l_cp->ppm_data = l_cp->ppm_buffer;
4653 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4655 if (l_remaining_data <= p_header_size) {
4656 /* we must store less information than available in the packet */
4657 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4658 l_cp->ppm_data_size = l_cp->ppm_len;
4659 p_header_size -= l_remaining_data;
4660 p_header_data += l_remaining_data;
4663 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4664 l_cp->ppm_data_size += p_header_size;
4665 p_header_data += p_header_size;
4674 static void j2k_read_ppt(opj_j2k_t *j2k) {
4675 int len, Z_ppt, i, j = 0;
4677 opj_cp_t *cp = j2k->cp;
4678 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4679 opj_cio_t *cio = j2k->cio;
4681 len = cio_read(cio, 2);
4682 Z_ppt = cio_read(cio, 1);
4684 if (Z_ppt == 0) { /* First PPT marker */
4685 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4686 tcp->ppt_data_first = tcp->ppt_data;
4688 tcp->ppt_len = len - 3;
4689 } else { /* NON-first PPT marker */
4690 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4691 tcp->ppt_data_first = tcp->ppt_data;
4692 tcp->ppt_len = len - 3 + tcp->ppt_store;
4695 for (i = len - 3; i > 0; i--) {
4696 tcp->ppt_data[j] = cio_read(cio, 1);
4703 * Reads a PPT marker (Packed packet headers, tile-part header)
4705 * @param p_header_data the data contained in the PPT box.
4706 * @param p_j2k the jpeg2000 codec.
4707 * @param p_header_size the size of the data contained in the PPT marker.
4708 * @param p_manager the user event manager.
4710 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4711 OPJ_BYTE * p_header_data,
4712 OPJ_UINT32 p_header_size,
4713 struct opj_event_mgr * p_manager )
4715 opj_cp_v2_t *l_cp = 00;
4716 opj_tcp_v2_t *l_tcp = 00;
4720 assert(p_header_data != 00);
4721 assert(p_j2k != 00);
4722 assert(p_manager != 00);
4724 /* We need to have the Z_ppt element at minimum */
4725 if (p_header_size < 1) {
4726 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4730 l_cp = &(p_j2k->m_cp);
4732 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");
4736 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4739 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4743 /* Allocate buffer to read the packet header */
4745 /* First PPT marker */
4746 l_tcp->ppt_data_size = 0;
4747 l_tcp->ppt_len = p_header_size;
4749 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4750 if (l_tcp->ppt_buffer == 00) {
4751 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4754 l_tcp->ppt_data = l_tcp->ppt_buffer;
4756 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4759 l_tcp->ppt_len += p_header_size;
4761 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4762 if (l_tcp->ppt_buffer == 00) {
4763 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4766 l_tcp->ppt_data = l_tcp->ppt_buffer;
4768 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4771 /* Read packet header from buffer */
4772 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4774 l_tcp->ppt_data_size += p_header_size;
4779 static void j2k_write_tlm(opj_j2k_t *j2k){
4781 opj_cio_t *cio = j2k->cio;
4782 j2k->tlm_start = cio_tell(cio);
4783 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4784 lenp = 4 + (5*j2k->totnum_tp);
4785 cio_write(cio,lenp,2); /* Ltlm */
4786 cio_write(cio, 0,1); /* Ztlm=0*/
4787 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4788 cio_skip(cio,5*j2k->totnum_tp);
4791 static void j2k_write_sot(opj_j2k_t *j2k) {
4794 opj_cio_t *cio = j2k->cio;
4796 j2k->sot_start = cio_tell(cio);
4797 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4798 lenp = cio_tell(cio);
4799 cio_skip(cio, 2); /* Lsot (further) */
4800 cio_write(cio, j2k->curtileno, 2); /* Isot */
4801 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4802 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4803 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4804 len = cio_tell(cio) - lenp;
4805 cio_seek(cio, lenp);
4806 cio_write(cio, len, 2); /* Lsot */
4807 cio_seek(cio, lenp + len);
4811 /* update markers struct */
4812 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4813 #endif /* USE_JPWL */
4816 if( j2k->cstr_info && j2k->cur_tp_num==0){
4817 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4822 * Writes the SOT marker (Start of tile-part)
4824 * @param p_stream the stream to write data to.
4825 * @param p_j2k J2K codec.
4826 * @param p_manager the user event manager.
4828 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4830 OPJ_UINT32 * p_data_written,
4831 const struct opj_stream_private *p_stream,
4832 struct opj_event_mgr * p_manager )
4835 assert(p_j2k != 00);
4836 assert(p_manager != 00);
4837 assert(p_stream != 00);
4839 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4842 opj_write_bytes(p_data,10,2); /* Lsot */
4845 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4851 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4854 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4859 /* update markers struct */
4861 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4863 assert( 0 && "TODO" );
4864 #endif /* USE_JPWL */
4866 * p_data_written = 12;
4872 static void j2k_read_sot(opj_j2k_t *j2k) {
4873 int len, tileno, totlen, partno, numparts;
4875 opj_tcp_t *tcp = NULL;
4878 opj_cp_t *cp = j2k->cp;
4879 opj_cio_t *cio = j2k->cio;
4881 len = cio_read(cio, 2);
4882 tileno = cio_read(cio, 2);
4885 if (j2k->cp->correct) {
4887 static int backup_tileno = 0;
4889 /* tileno is negative or larger than the number of tiles!!! */
4890 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
4891 opj_event_msg(j2k->cinfo, EVT_ERROR,
4892 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4893 tileno, (cp->tw * cp->th));
4895 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4898 /* we try to correct */
4899 tileno = backup_tileno;
4900 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
4901 "- setting tile number to %d\n",
4905 /* keep your private count of tiles */
4908 #endif /* USE_JPWL */
4910 if (cp->tileno_size == 0) {
4911 cp->tileno[cp->tileno_size] = tileno;
4915 assert(cp->tileno_size >= 0);
4916 while (i < (OPJ_UINT32)cp->tileno_size && status == 0) {
4917 status = cp->tileno[i] == tileno ? 1 : 0;
4921 cp->tileno[cp->tileno_size] = tileno;
4926 totlen = cio_read(cio, 4);
4929 if (j2k->cp->correct) {
4931 /* totlen is negative or larger than the bytes left!!! */
4932 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
4933 opj_event_msg(j2k->cinfo, EVT_ERROR,
4934 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4935 totlen, cio_numbytesleft(cio) + 8);
4937 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4940 /* we try to correct */
4942 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
4943 "- setting Psot to %d => assuming it is the last tile\n",
4948 #endif /* USE_JPWL */
4951 totlen = cio_numbytesleft(cio) + 8;
4953 partno = cio_read(cio, 1);
4954 numparts = cio_read(cio, 1);
4956 if (partno >= numparts) {
4957 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);
4958 numparts = partno+1;
4961 j2k->curtileno = tileno;
4962 j2k->cur_tp_num = partno;
4963 j2k->eot = cio_getbp(cio) - 12 + totlen;
4964 j2k->state = J2K_STATE_TPH;
4965 tcp = &cp->tcps[j2k->curtileno];
4968 if (j2k->cstr_info) {
4971 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
4972 j2k->cstr_info->tile[tileno].tileno = tileno;
4973 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
4974 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4976 j2k->cstr_info->tile[tileno].end_pos += totlen;
4978 j2k->cstr_info->tile[tileno].num_tps = numparts;
4980 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
4982 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)*/
4983 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
4984 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4985 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4988 if (tcp->first == 1) {
4989 /* Initialization PPT */
4990 opj_tccp_t *tmp = tcp->tccps;
4991 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
4993 tcp->ppt_data = NULL;
4994 tcp->ppt_data_first = NULL;
4997 for (i = 0; i < j2k->image->numcomps; i++) {
4998 tcp->tccps[i] = j2k->default_tcp->tccps[i];
5000 cp->tcps[j2k->curtileno].first = 0;
5005 * Reads a PPT marker (Packed packet headers, tile-part header)
5007 * @param p_header_data the data contained in the PPT box.
5008 * @param p_j2k the jpeg2000 codec.
5009 * @param p_header_size the size of the data contained in the PPT marker.
5010 * @param p_manager the user event manager.
5012 opj_bool j2k_read_sot_v2 (
5013 opj_j2k_v2_t *p_j2k,
5014 OPJ_BYTE * p_header_data,
5015 OPJ_UINT32 p_header_size,
5016 struct opj_event_mgr * p_manager
5020 opj_cp_v2_t *l_cp = 00;
5021 opj_tcp_v2_t *l_tcp = 00;
5022 OPJ_UINT32 l_tot_len, l_num_parts = 0;
5023 OPJ_UINT32 l_current_part;
5024 OPJ_UINT32 l_tile_x,l_tile_y;
5027 assert(p_header_data != 00);
5028 assert(p_j2k != 00);
5029 assert(p_manager != 00);
5031 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
5032 if (p_header_size != 8) {
5033 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
5037 l_cp = &(p_j2k->m_cp);
5038 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
5041 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5042 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
5043 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
5046 if (l_cp->correct) {
5048 int tileno = p_j2k->m_current_tile_number;
5049 static int backup_tileno = 0;
5051 /* tileno is negative or larger than the number of tiles!!! */
5052 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
5053 opj_event_msg_v2(p_manager, EVT_ERROR,
5054 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5055 tileno, (l_cp->tw * l_cp->th));
5057 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5060 /* we try to correct */
5061 tileno = backup_tileno;
5062 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5063 "- setting tile number to %d\n",
5067 /* keep your private count of tiles */
5070 #endif /* USE_JPWL */
5072 /* look for the tile in the list of already processed tile (in parts). */
5073 /* Optimization possible here with a more complex data structure and with the removing of tiles */
5074 /* since the time taken by this function can only grow at the time */
5076 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
5079 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
5080 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
5082 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5088 if (l_cp->correct) {
5090 /* totlen is negative or larger than the bytes left!!! */
5091 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))) { */
5092 opj_event_msg_v2(p_manager, EVT_ERROR,
5093 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5094 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5096 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5099 /* we try to correct */
5101 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5102 "- setting Psot to %d => assuming it is the last tile\n",
5106 #endif /* USE_JPWL */
5108 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5110 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5111 "we assuming it is the last tile-part of the codestream.\n");
5112 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5115 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5118 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5121 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5122 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5123 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5124 if (l_tcp->m_nb_tile_parts) {
5125 if (l_current_part >= l_tcp->m_nb_tile_parts){
5126 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5127 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5128 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5132 l_tcp->m_nb_tile_parts = l_num_parts;
5135 /* If know the number of tile part header we will check if we didn't read the last*/
5136 if (l_tcp->m_nb_tile_parts) {
5137 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5138 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5142 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5143 /* Keep the size of data to skip after this marker */
5144 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5147 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5148 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5151 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5153 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5154 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5155 p_j2k->m_specific_param.m_decoder.m_skip_data =
5156 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5157 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5158 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5159 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5162 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5163 p_j2k->m_specific_param.m_decoder.m_skip_data =
5164 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5168 if (p_j2k->cstr_index)
5170 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5171 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5173 if (l_num_parts != 0){
5174 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5175 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5178 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5179 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5180 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5182 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5183 (opj_tp_index_t*)opj_realloc(
5184 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5185 l_num_parts* sizeof(opj_tp_index_t));
5188 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5190 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5191 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5192 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5193 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5194 sizeof(opj_tp_index_t));
5197 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5198 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5199 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5200 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5201 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5202 * sizeof(opj_tp_index_t));
5211 /* 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 */
5212 /* if (p_j2k->cstr_info) {
5215 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5218 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5219 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5220 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5221 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5224 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5227 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5231 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5234 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5235 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5236 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5241 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5244 opj_tcp_t *tcp = NULL;
5245 opj_codestream_info_t *cstr_info = NULL;
5247 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5248 opj_cp_t *cp = j2k->cp;
5249 opj_cio_t *cio = j2k->cio;
5251 tcd->tp_num = j2k->tp_num ;
5252 tcd->cur_tp_num = j2k->cur_tp_num;
5254 cio_write(cio, J2K_MS_SOD, 2);
5256 if( j2k->cstr_info && j2k->cur_tp_num==0){
5257 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5260 if (j2k->curtileno == 0) {
5261 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5265 cstr_info = j2k->cstr_info;
5267 if (!j2k->cur_tp_num ) {
5268 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5269 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5272 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5273 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5277 /* update markers struct */
5278 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5279 #endif /* USE_JPWL */
5284 tcp = &cp->tcps[j2k->curtileno];
5285 for (layno = 0; layno < tcp->numlayers; layno++) {
5286 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5287 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5288 } else if (tcp->rates[layno]) {
5289 tcp->rates[layno]=1;
5292 if(j2k->cur_tp_num == 0){
5293 tcd->tcd_image->tiles->packno = 0;
5295 cstr_info->packno = 0;
5298 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5300 /* Writing Psot in SOT marker */
5301 totlen = cio_tell(cio) + l - j2k->sot_start;
5302 cio_seek(cio, j2k->sot_start + 6);
5303 cio_write(cio, totlen, 4);
5304 cio_seek(cio, j2k->sot_start + totlen);
5305 /* Writing Ttlm and Ptlm in TLM marker */
5307 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5308 cio_write(cio, j2k->curtileno, 1);
5309 cio_write(cio, totlen, 4);
5311 cio_seek(cio, j2k->sot_start + totlen);
5315 * Writes the SOD marker (Start of data)
5317 * @param p_stream the stream to write data to.
5318 * @param p_j2k J2K codec.
5319 * @param p_manager the user event manager.
5321 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5322 struct opj_tcd_v2 * p_tile_coder,
5324 OPJ_UINT32 * p_data_written,
5325 OPJ_UINT32 p_total_data_size,
5326 const struct opj_stream_private *p_stream,
5327 struct opj_event_mgr * p_manager )
5329 opj_tcp_v2_t *l_tcp = 00;
5330 opj_codestream_info_t *l_cstr_info = 00;
5331 opj_cp_v2_t *l_cp = 00;
5333 OPJ_UINT32 l_size_tile;
5334 OPJ_UINT32 l_remaining_data;
5337 assert(p_j2k != 00);
5338 assert(p_manager != 00);
5339 assert(p_stream != 00);
5341 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5344 /* make room for the EOF marker */
5345 l_remaining_data = p_total_data_size - 4;
5347 l_cp = &(p_j2k->m_cp);
5348 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5351 /* update tile coder */
5352 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5353 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5355 l_size_tile = l_cp->th * l_cp->tw;
5358 /* TODO mergeV2: check this part which use cstr_info */
5359 /*l_cstr_info = p_j2k->cstr_info;
5361 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5362 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5363 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5369 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5371 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5376 /* update markers struct */
5377 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5379 assert( 0 && "TODO" );
5380 #endif /* USE_JPWL */
5385 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5386 p_tile_coder->tcd_image->tiles->packno = 0;
5388 l_cstr_info->packno = 0;
5392 *p_data_written = 0;
5394 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)) {
5395 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5399 *p_data_written += 2;
5405 static void j2k_read_sod(opj_j2k_t *j2k) {
5406 int len, truncate = 0, i;
5407 unsigned char *data = NULL, *data_ptr = NULL;
5409 opj_cio_t *cio = j2k->cio;
5410 int curtileno = j2k->curtileno;
5413 if (j2k->cstr_info) {
5414 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5415 cio_tell(cio) + j2k->pos_correction - 1;
5416 if (j2k->cur_tp_num == 0)
5417 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5418 j2k->cstr_info->packno = 0;
5421 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
5423 if (len == cio_numbytesleft(cio) + 1) {
5424 truncate = 1; /* Case of a truncate codestream */
5427 data = j2k->tile_data[curtileno];
5428 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
5430 data_ptr = data + j2k->tile_len[curtileno];
5431 for (i = 0; i < len; i++) {
5432 data_ptr[i] = cio_read(cio, 1);
5435 j2k->tile_len[curtileno] += len;
5436 j2k->tile_data[curtileno] = data;
5439 j2k->state = J2K_STATE_TPHSOT;
5441 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
5447 * Reads a SOD marker (Start Of Data)
5449 * @param p_header_data the data contained in the SOD box.
5450 * @param p_j2k the jpeg2000 codec.
5451 * @param p_header_size the size of the data contained in the SOD marker.
5452 * @param p_manager the user event manager.
5454 opj_bool j2k_read_sod_v2 (
5455 opj_j2k_v2_t *p_j2k,
5456 struct opj_stream_private *p_stream,
5457 struct opj_event_mgr * p_manager
5460 OPJ_UINT32 l_current_read_size;
5461 opj_codestream_index_t * l_cstr_index = 00;
5462 OPJ_BYTE ** l_current_data = 00;
5463 opj_tcp_v2_t * l_tcp = 00;
5464 OPJ_UINT32 * l_tile_len = 00;
5467 assert(p_j2k != 00);
5468 assert(p_manager != 00);
5469 assert(p_stream != 00);
5471 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5473 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5474 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5475 // but we are in the last tile part,
5476 // so its result will fit on OPJ_UINT32 unless we find
5477 // a file with a single tile part of more than 4 GB...*/
5478 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5481 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5483 l_current_data = &(l_tcp->m_data);
5484 l_tile_len = &l_tcp->m_data_size;
5486 if (! *l_current_data) {
5487 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5490 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5493 if (*l_current_data == 00) {
5494 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5500 l_cstr_index = p_j2k->cstr_index;
5502 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5504 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5505 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5507 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5508 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5510 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5514 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5516 /*l_cstr_index->packno = 0;*/
5519 l_current_read_size = opj_stream_read_data( p_stream,
5520 *l_current_data + *l_tile_len,
5521 p_j2k->m_specific_param.m_decoder.m_sot_length,
5524 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5525 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5528 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5531 *l_tile_len += l_current_read_size;
5537 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5538 opj_cp_t *cp = j2k->cp;
5539 opj_tcp_t *tcp = &cp->tcps[tileno];
5540 opj_cio_t *cio = j2k->cio;
5541 int numcomps = j2k->image->numcomps;
5543 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5544 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5545 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5546 cio_write(cio, 0, 1); /* Srgn */
5547 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5551 * Writes the RGN marker (Region Of Interest)
5553 * @param p_tile_no the tile to output
5554 * @param p_comp_no the component to output
5555 * @param p_stream the stream to write data to.
5556 * @param p_j2k J2K codec.
5557 * @param p_manager the user event manager.
5559 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5560 OPJ_UINT32 p_tile_no,
5561 OPJ_UINT32 p_comp_no,
5562 struct opj_stream_private *p_stream,
5563 struct opj_event_mgr * p_manager )
5565 OPJ_BYTE * l_current_data = 00;
5566 OPJ_UINT32 l_nb_comp;
5567 OPJ_UINT32 l_rgn_size;
5568 opj_image_t *l_image = 00;
5569 opj_cp_v2_t *l_cp = 00;
5570 opj_tcp_v2_t *l_tcp = 00;
5571 opj_tccp_t *l_tccp = 00;
5572 OPJ_UINT32 l_comp_room;
5575 assert(p_j2k != 00);
5576 assert(p_manager != 00);
5577 assert(p_stream != 00);
5579 l_cp = &(p_j2k->m_cp);
5580 l_tcp = &l_cp->tcps[p_tile_no];
5581 l_tccp = &l_tcp->tccps[p_comp_no];
5583 l_nb_comp = l_image->numcomps;
5585 if (l_nb_comp <= 256) {
5592 l_rgn_size = 6 + l_comp_room;
5594 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5596 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5597 l_current_data += 2;
5599 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5600 l_current_data += 2;
5602 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5603 l_current_data+=l_comp_room;
5605 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5608 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5611 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) {
5618 static void j2k_read_rgn(opj_j2k_t *j2k) {
5619 int len, compno, roisty;
5621 opj_cp_t *cp = j2k->cp;
5622 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5623 opj_cio_t *cio = j2k->cio;
5624 int numcomps = j2k->image->numcomps;
5626 len = cio_read(cio, 2); /* Lrgn */
5627 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
5628 roisty = cio_read(cio, 1); /* Srgn */
5631 if (j2k->cp->correct) {
5632 /* totlen is negative or larger than the bytes left!!! */
5633 if (compno >= numcomps) {
5634 opj_event_msg(j2k->cinfo, EVT_ERROR,
5635 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5637 if (!JPWL_ASSUME || JPWL_ASSUME) {
5638 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5643 #endif /* USE_JPWL */
5645 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
5648 static void j2k_write_eoc(opj_j2k_t *j2k) {
5649 opj_cio_t *cio = j2k->cio;
5650 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5651 cio_write(cio, J2K_MS_EOC, 2);
5655 /* update markers struct */
5656 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5657 #endif /* USE_JPWL */
5662 * Reads a RGN marker (Region Of Interest)
5664 * @param p_header_data the data contained in the POC box.
5665 * @param p_j2k the jpeg2000 codec.
5666 * @param p_header_size the size of the data contained in the POC marker.
5667 * @param p_manager the user event manager.
5669 opj_bool j2k_read_rgn_v2 (
5670 opj_j2k_v2_t *p_j2k,
5671 OPJ_BYTE * p_header_data,
5672 OPJ_UINT32 p_header_size,
5673 struct opj_event_mgr * p_manager
5676 OPJ_UINT32 l_nb_comp;
5677 opj_image_t * l_image = 00;
5679 opj_cp_v2_t *l_cp = 00;
5680 opj_tcp_v2_t *l_tcp = 00;
5681 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5684 assert(p_header_data != 00);
5685 assert(p_j2k != 00);
5686 assert(p_manager != 00);
5688 l_image = p_j2k->m_private_image;
5689 l_nb_comp = l_image->numcomps;
5691 if (l_nb_comp <= 256) {
5696 if (p_header_size != 2 + l_comp_room) {
5697 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5701 l_cp = &(p_j2k->m_cp);
5702 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5703 &l_cp->tcps[p_j2k->m_current_tile_number] :
5704 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5706 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5707 p_header_data+=l_comp_room;
5708 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5712 if (l_cp->correct) {
5713 /* totlen is negative or larger than the bytes left!!! */
5714 if (l_comp_room >= l_nb_comp) {
5715 opj_event_msg_v2(p_manager, EVT_ERROR,
5716 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5717 l_comp_room, l_nb_comp);
5718 if (!JPWL_ASSUME || JPWL_ASSUME) {
5719 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5724 #endif /* USE_JPWL */
5726 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5733 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5735 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5738 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5745 * Updates the rates of the tcp.
5747 * @param p_stream the stream to write data to.
5748 * @param p_j2k J2K codec.
5749 * @param p_manager the user event manager.
5751 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5752 struct opj_stream_private *p_stream,
5753 struct opj_event_mgr * p_manager )
5755 opj_cp_v2_t * l_cp = 00;
5756 opj_image_t * l_image = 00;
5757 opj_tcp_v2_t * l_tcp = 00;
5758 opj_image_comp_t * l_img_comp = 00;
5761 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5762 OPJ_FLOAT32 * l_rates = 0;
5763 OPJ_FLOAT32 l_sot_remove;
5764 OPJ_UINT32 l_bits_empty, l_size_pixel;
5765 OPJ_UINT32 l_tile_size = 0;
5766 OPJ_UINT32 l_last_res;
5767 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5770 assert(p_j2k != 00);
5771 assert(p_manager != 00);
5772 assert(p_stream != 00);
5775 l_cp = &(p_j2k->m_cp);
5776 l_image = p_j2k->m_private_image;
5779 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5780 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5781 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5783 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5784 l_tp_stride_func = get_tp_stride;
5787 l_tp_stride_func = get_default_stride;
5790 for (i=0;i<l_cp->th;++i) {
5791 for (j=0;j<l_cp->tw;++j) {
5792 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5794 /* 4 borders of the tile rescale on the image if necessary */
5795 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5796 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5797 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5798 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5800 l_rates = l_tcp->rates;
5802 /* Modification of the RATE >> */
5804 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5806 ((*l_rates) * l_bits_empty)
5814 for (k = 1; k < l_tcp->numlayers; ++k) {
5816 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5818 ((*l_rates) * l_bits_empty)
5834 for (i=0;i<l_cp->th;++i) {
5835 for (j=0;j<l_cp->tw;++j) {
5836 l_rates = l_tcp->rates;
5839 *l_rates -= l_sot_remove;
5841 if (*l_rates < 30) {
5848 l_last_res = l_tcp->numlayers - 1;
5850 for (k = 1; k < l_last_res; ++k) {
5853 *l_rates -= l_sot_remove;
5855 if (*l_rates < *(l_rates - 1) + 10) {
5856 *l_rates = (*(l_rates - 1)) + 20;
5864 *l_rates -= (l_sot_remove + 2.f);
5866 if (*l_rates < *(l_rates - 1) + 10) {
5867 *l_rates = (*(l_rates - 1)) + 20;
5875 l_img_comp = l_image->comps;
5878 for (i=0;i<l_image->numcomps;++i) {
5879 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5881 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5889 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5891 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5893 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5894 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5895 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5896 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5900 if (l_cp->m_specific_param.m_enc.m_cinema) {
5901 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5902 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5903 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5907 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5908 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5914 static void j2k_read_eoc(opj_j2k_t *j2k) {
5918 /* if packets should be decoded */
5919 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5920 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5921 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5922 for (i = 0; i < j2k->cp->tileno_size; i++) {
5923 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5924 tileno = j2k->cp->tileno[i];
5925 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5926 opj_free(j2k->tile_data[tileno]);
5927 j2k->tile_data[tileno] = NULL;
5928 tcd_free_decode_tile(tcd, i);
5929 if (success == OPJ_FALSE) {
5930 j2k->state |= J2K_STATE_ERR;
5934 tcd_free_decode(tcd);
5937 /* if packets should not be decoded */
5939 for (i = 0; i < j2k->cp->tileno_size; i++) {
5940 tileno = j2k->cp->tileno[i];
5941 opj_free(j2k->tile_data[tileno]);
5942 j2k->tile_data[tileno] = NULL;
5945 if (j2k->state & J2K_STATE_ERR)
5946 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5948 j2k->state = J2K_STATE_MT;
5952 * Reads a EOC marker (End Of Codestream)
5954 * @param p_header_data the data contained in the SOD box.
5955 * @param p_j2k the jpeg2000 codec.
5956 * @param p_header_size the size of the data contained in the SOD marker.
5957 * @param p_manager the user event manager.
5960 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5961 struct opj_stream_private *p_stream,
5962 struct opj_event_mgr * p_manager )
5965 opj_tcd_v2_t * l_tcd = 00;
5966 OPJ_UINT32 l_nb_tiles;
5967 opj_tcp_v2_t * l_tcp = 00;
5971 assert(p_j2k != 00);
5972 assert(p_manager != 00);
5973 assert(p_stream != 00);
5975 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5976 l_tcp = p_j2k->m_cp.tcps;
5978 l_tcd = tcd_create_v2(OPJ_TRUE);
5980 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5984 for (i = 0; i < l_nb_tiles; ++i) {
5985 if (l_tcp->m_data) {
5986 if (! tcd_init_decode_tile(l_tcd, i)) {
5987 tcd_destroy_v2(l_tcd);
5988 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5992 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5996 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
6001 j2k_tcp_destroy(l_tcp);
6005 tcd_destroy_v2(l_tcd);
6011 * Gets the offset of the header.
6013 * @param p_stream the stream to write data to.
6014 * @param p_j2k J2K codec.
6015 * @param p_manager the user event manager.
6017 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
6018 struct opj_stream_private *p_stream,
6019 struct opj_event_mgr * p_manager )
6022 assert(p_j2k != 00);
6023 assert(p_manager != 00);
6024 assert(p_stream != 00);
6026 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
6032 * Writes the MCT marker (Multiple Component Transform)
6034 * @param p_stream the stream to write data to.
6035 * @param p_j2k J2K codec.
6036 * @param p_manager the user event manager.
6038 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
6039 struct opj_stream_private *p_stream,
6040 struct opj_event_mgr * p_manager )
6043 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6044 opj_mct_data_t * l_mct_record;
6045 opj_tcp_v2_t * l_tcp;
6048 assert(p_j2k != 00);
6049 assert(p_stream != 00);
6050 assert(p_manager != 00);
6052 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
6056 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6057 l_mct_record = l_tcp->m_mct_records;
6059 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6061 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
6068 l_mcc_record = l_tcp->m_mcc_records;
6070 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6072 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6079 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6087 * Writes the image components.
6089 * @param p_stream the stream to write data to.
6090 * @param p_j2k J2K codec.
6091 * @param p_manager the user event manager.
6093 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6094 struct opj_stream_private *p_stream,
6095 struct opj_event_mgr * p_manager )
6100 assert(p_j2k != 00);
6101 assert(p_manager != 00);
6102 assert(p_stream != 00);
6104 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6106 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6110 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6119 * Writes regions of interests.
6121 * @param p_stream the stream to write data to.
6122 * @param p_j2k J2K codec.
6123 * @param p_manager the user event manager.
6125 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
6126 struct opj_stream_private *p_stream,
6127 struct opj_event_mgr * p_manager )
6130 const opj_tccp_t *l_tccp = 00;
6133 assert(p_j2k != 00);
6134 assert(p_manager != 00);
6135 assert(p_stream != 00);
6137 l_tccp = p_j2k->m_cp.tcps->tccps;
6139 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6140 if (l_tccp->roishift) {
6142 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6156 * @param p_stream the stream to write data to.
6157 * @param p_j2k J2K codec.
6158 * @param p_manager the user event manager.
6160 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6161 struct opj_stream_private *p_stream,
6162 struct opj_event_mgr * p_manager )
6164 opj_codestream_index_t * l_cstr_index = 00;
6167 assert(p_j2k != 00);
6168 assert(p_manager != 00);
6169 assert(p_stream != 00);
6171 l_cstr_index = p_j2k->cstr_index;
6173 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6175 /* The following adjustment is done to adjust the codestream size */
6176 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6177 /* the first bunch of bytes is not in the codestream */
6178 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6183 /* preparation of JPWL marker segments */
6187 /* encode according to JPWL */
6188 jpwl_encode(p_j2k, p_stream, image);
6192 assert( 0 && "TODO" );
6193 #endif /* USE_JPWL */
6198 typedef struct opj_dec_mstabent {
6201 /** value of the state when the marker can appear */
6203 /** action linked to the marker */
6204 void (*handler) (opj_j2k_t *j2k);
6205 } opj_dec_mstabent_t;
6207 opj_dec_mstabent_t j2k_dec_mstab[] = {
6208 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
6209 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
6210 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
6211 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6212 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
6213 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
6214 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6215 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6216 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6217 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6218 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6219 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6220 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6221 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6222 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6223 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6225 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6226 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6229 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6230 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6231 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6232 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6233 #endif /* USE_JPWL */
6235 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6236 {J2K_MS_INSEC, 0, j2k_read_insec},
6237 #endif /* USE_JPSEC */
6239 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6242 static void j2k_read_unk(opj_j2k_t *j2k) {
6243 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6246 if (j2k->cp->correct) {
6248 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6249 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6250 id = cio_read(j2k->cio, 2);
6251 opj_event_msg(j2k->cinfo, EVT_ERROR,
6252 "JPWL: really don't know this marker %x\n",
6255 opj_event_msg(j2k->cinfo, EVT_ERROR,
6256 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6259 /* OK, activate this at your own risk!!! */
6260 /* we look for the marker at the minimum hamming distance from this */
6261 while (j2k_dec_mstab[m].id) {
6263 /* 1's where they differ */
6264 tmp_id = j2k_dec_mstab[m].id ^ id;
6266 /* compute the hamming distance between our id and the current */
6268 for (i = 0; i < 16; i++) {
6269 if ((tmp_id >> i) & 0x0001) {
6274 /* if current distance is smaller, set the minimum */
6275 if (cur_dist < min_dist) {
6276 min_dist = cur_dist;
6277 min_id = j2k_dec_mstab[m].id;
6280 /* jump to the next marker */
6284 /* do we substitute the marker? */
6285 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6286 opj_event_msg(j2k->cinfo, EVT_ERROR,
6287 "- marker %x is at distance %d from the read %x\n",
6288 min_id, min_dist, id);
6289 opj_event_msg(j2k->cinfo, EVT_ERROR,
6290 "- trying to substitute in place and crossing fingers!\n");
6291 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6292 cio_write(j2k->cio, min_id, 2);
6295 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6300 #endif /* USE_JPWL */
6305 * Reads an unknown marker
6307 * @param p_stream the stream object to read from.
6308 * @param p_j2k the jpeg2000 codec.
6309 * @param p_manager the user event manager.
6311 * @return true if the marker could be deduced.
6313 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6314 struct opj_stream_private *p_stream,
6315 OPJ_UINT32 *output_marker,
6316 struct opj_event_mgr * p_manager
6319 OPJ_UINT32 l_unknown_marker;
6320 const opj_dec_memory_marker_handler_t * l_marker_handler;
6321 OPJ_UINT32 l_size_unk = 2;
6324 assert(p_j2k != 00);
6325 assert(p_manager != 00);
6326 assert(p_stream != 00);
6328 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6331 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6332 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6333 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6337 /* read 2 bytes as the new marker ID*/
6338 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6340 if (!(l_unknown_marker < 0xff00)) {
6342 /* Get the marker handler from the marker ID*/
6343 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6345 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6346 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6350 if (l_marker_handler->id != J2K_MS_UNK) {
6351 /* Add the marker to the codestream index*/
6352 if (l_marker_handler->id != J2K_MS_SOT)
6353 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6354 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6356 break; /* next marker is known and well located */
6364 *output_marker = l_marker_handler->id ;
6370 Read the lookup table containing all the marker, status and action
6371 @param id Marker value
6373 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6374 opj_dec_mstabent_t *e;
6375 for (e = j2k_dec_mstab; e->id != 0; e++) {
6384 * Writes the MCT marker (Multiple Component Transform)
6386 * @param p_stream the stream to write data to.
6387 * @param p_j2k J2K codec.
6388 * @param p_manager the user event manager.
6390 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6391 opj_mct_data_t * p_mct_record,
6392 struct opj_stream_private *p_stream,
6393 struct opj_event_mgr * p_manager )
6395 OPJ_UINT32 l_mct_size;
6396 OPJ_BYTE * l_current_data = 00;
6400 assert(p_j2k != 00);
6401 assert(p_manager != 00);
6402 assert(p_stream != 00);
6404 l_mct_size = 10 + p_mct_record->m_data_size;
6406 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6407 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6408 = (OPJ_BYTE*)opj_realloc(
6409 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6412 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6416 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6419 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6421 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6422 l_current_data += 2;
6424 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6425 l_current_data += 2;
6427 opj_write_bytes(l_current_data,0,2); /* Zmct */
6428 l_current_data += 2;
6430 /* only one marker atm */
6431 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6433 opj_write_bytes(l_current_data,l_tmp,2);
6434 l_current_data += 2;
6436 opj_write_bytes(l_current_data,0,2); /* Ymct */
6439 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6441 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) {
6449 * Reads a MCT marker (Multiple Component Transform)
6451 * @param p_header_data the data contained in the MCT box.
6452 * @param p_j2k the jpeg2000 codec.
6453 * @param p_header_size the size of the data contained in the MCT marker.
6454 * @param p_manager the user event manager.
6456 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6457 OPJ_BYTE * p_header_data,
6458 OPJ_UINT32 p_header_size,
6459 struct opj_event_mgr * p_manager )
6462 opj_tcp_v2_t *l_tcp = 00;
6465 opj_mct_data_t * l_mct_data;
6468 assert(p_header_data != 00);
6469 assert(p_j2k != 00);
6471 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6472 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6473 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6475 if (p_header_size < 2) {
6476 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6481 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6484 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6488 if(p_header_size <= 6) {
6489 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6493 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6494 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6497 l_indix = l_tmp & 0xff;
6498 l_mct_data = l_tcp->m_mct_records;
6500 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6501 if (l_mct_data->m_index == l_indix) {
6508 if (i == l_tcp->m_nb_mct_records) {
6509 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6510 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6512 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));
6513 if(! l_tcp->m_mct_records) {
6514 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6518 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6519 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6522 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6525 if (l_mct_data->m_data) {
6526 opj_free(l_mct_data->m_data);
6527 l_mct_data->m_data = 00;
6530 l_mct_data->m_index = l_indix;
6531 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6532 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6534 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6537 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6543 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6544 if (! l_mct_data->m_data) {
6545 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6548 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6550 l_mct_data->m_data_size = p_header_size;
6551 ++l_tcp->m_nb_mct_records;
6557 * Writes the MCC marker (Multiple Component Collection)
6559 * @param p_stream the stream to write data to.
6560 * @param p_j2k J2K codec.
6561 * @param p_manager the user event manager.
6563 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6564 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6565 struct opj_stream_private *p_stream,
6566 struct opj_event_mgr * p_manager )
6569 OPJ_UINT32 l_mcc_size;
6570 OPJ_BYTE * l_current_data = 00;
6571 OPJ_UINT32 l_nb_bytes_for_comp;
6576 assert(p_j2k != 00);
6577 assert(p_manager != 00);
6578 assert(p_stream != 00);
6580 if (p_mcc_record->m_nb_comps > 255 ) {
6581 l_nb_bytes_for_comp = 2;
6585 l_nb_bytes_for_comp = 1;
6589 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6590 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6592 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6593 = (OPJ_BYTE*)opj_realloc(
6594 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6596 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6600 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6603 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6605 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6606 l_current_data += 2;
6608 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6609 l_current_data += 2;
6612 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6613 l_current_data += 2;
6615 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6618 /* only one marker atm */
6619 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6622 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6625 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6628 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 */
6631 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6632 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6633 l_current_data+=l_nb_bytes_for_comp;
6636 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 */
6639 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6641 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6642 l_current_data+=l_nb_bytes_for_comp;
6645 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6647 if (p_mcc_record->m_decorrelation_array) {
6648 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6651 if (p_mcc_record->m_offset_array) {
6652 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6655 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6658 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) {
6666 * Reads a MCC marker (Multiple Component Collection)
6668 * @param p_header_data the data contained in the MCC box.
6669 * @param p_j2k the jpeg2000 codec.
6670 * @param p_header_size the size of the data contained in the MCC marker.
6671 * @param p_manager the user event manager.
6673 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6674 OPJ_BYTE * p_header_data,
6675 OPJ_UINT32 p_header_size,
6676 struct opj_event_mgr * p_manager )
6681 opj_tcp_v2_t * l_tcp;
6682 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6683 opj_mct_data_t * l_mct_data;
6684 OPJ_UINT32 l_nb_collections;
6685 OPJ_UINT32 l_nb_comps;
6686 OPJ_UINT32 l_nb_bytes_by_comp;
6690 assert(p_header_data != 00);
6691 assert(p_j2k != 00);
6692 assert(p_manager != 00);
6694 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6695 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6696 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6698 if (p_header_size < 2) {
6699 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6704 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6707 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6711 if (p_header_size < 7) {
6712 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6716 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6719 l_mcc_record = l_tcp->m_mcc_records;
6721 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6722 if (l_mcc_record->m_index == l_indix) {
6729 if (i == l_tcp->m_nb_mcc_records) {
6730 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6731 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6733 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6734 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6735 if (! l_tcp->m_mcc_records) {
6736 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6739 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6740 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));
6742 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6744 l_mcc_record->m_index = l_indix;
6746 /* only one marker atm */
6747 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6750 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6754 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6757 if (l_nb_collections > 1) {
6758 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6764 for (i=0;i<l_nb_collections;++i) {
6765 if (p_header_size < 3) {
6766 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6770 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6774 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6778 opj_read_bytes(p_header_data,&l_nb_comps,2);
6783 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6784 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6786 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6787 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6791 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6793 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6794 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6795 p_header_data+=l_nb_bytes_by_comp;
6798 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6803 opj_read_bytes(p_header_data,&l_nb_comps,2);
6806 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6807 l_nb_comps &= 0x7fff;
6809 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6810 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6814 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6815 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6819 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6821 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6822 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6823 p_header_data+=l_nb_bytes_by_comp;
6826 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6831 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6834 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6835 l_mcc_record->m_decorrelation_array = 00;
6836 l_mcc_record->m_offset_array = 00;
6838 l_indix = l_tmp & 0xff;
6840 l_mct_data = l_tcp->m_mct_records;
6841 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6842 if (l_mct_data->m_index == l_indix) {
6843 l_mcc_record->m_decorrelation_array = l_mct_data;
6849 if (l_mcc_record->m_decorrelation_array == 00) {
6850 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6855 l_indix = (l_tmp >> 8) & 0xff;
6857 l_mct_data = l_tcp->m_mct_records;
6858 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6859 if (l_mct_data->m_index == l_indix) {
6860 l_mcc_record->m_offset_array = l_mct_data;
6866 if (l_mcc_record->m_offset_array == 00) {
6867 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6873 if (p_header_size != 0) {
6874 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6878 ++l_tcp->m_nb_mcc_records;
6885 * Writes the MCO marker (Multiple component transformation ordering)
6887 * @param p_stream the stream to write data to.
6888 * @param p_j2k J2K codec.
6889 * @param p_manager the user event manager.
6891 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6892 struct opj_stream_private *p_stream,
6893 struct opj_event_mgr * p_manager
6896 OPJ_BYTE * l_current_data = 00;
6897 OPJ_UINT32 l_mco_size;
6898 opj_tcp_v2_t * l_tcp = 00;
6899 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6903 assert(p_j2k != 00);
6904 assert(p_manager != 00);
6905 assert(p_stream != 00);
6907 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6908 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6910 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6911 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6913 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6914 = (OPJ_BYTE*)opj_realloc(
6915 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6917 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6922 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6925 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6926 l_current_data += 2;
6928 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6929 l_current_data += 2;
6931 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6934 l_mcc_record = l_tcp->m_mcc_records;
6935 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6936 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6942 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) {
6950 * Reads a MCO marker (Multiple Component Transform Ordering)
6952 * @param p_header_data the data contained in the MCO box.
6953 * @param p_j2k the jpeg2000 codec.
6954 * @param p_header_size the size of the data contained in the MCO marker.
6955 * @param p_manager the user event manager.
6957 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6958 OPJ_BYTE * p_header_data,
6959 OPJ_UINT32 p_header_size,
6960 struct opj_event_mgr * p_manager )
6962 OPJ_UINT32 l_tmp, i;
6963 OPJ_UINT32 l_nb_stages;
6964 opj_tcp_v2_t * l_tcp;
6965 opj_tccp_t * l_tccp;
6966 opj_image_t * l_image;
6967 opj_image_comp_t * l_img_comp;
6970 assert(p_header_data != 00);
6971 assert(p_j2k != 00);
6972 assert(p_manager != 00);
6974 l_image = p_j2k->m_private_image;
6975 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6976 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6977 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6979 if (p_header_size < 1) {
6980 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6984 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6987 if (l_nb_stages > 1) {
6988 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6992 if (p_header_size != l_nb_stages + 1) {
6993 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6997 l_tccp = l_tcp->tccps;
6998 l_img_comp = l_image->comps;
7000 for (i=0;i<l_image->numcomps;++i) {
7001 l_tccp->m_dc_level_shift = 0;
7005 if (l_tcp->m_mct_decoding_matrix) {
7006 opj_free(l_tcp->m_mct_decoding_matrix);
7007 l_tcp->m_mct_decoding_matrix = 00;
7010 for (i=0;i<l_nb_stages;++i) {
7011 opj_read_bytes(p_header_data,&l_tmp,1);
7014 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
7022 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
7025 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7026 opj_mct_data_t * l_deco_array, * l_offset_array;
7027 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
7028 OPJ_UINT32 l_nb_elem;
7029 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
7030 opj_tccp_t * l_tccp;
7033 assert(p_tcp != 00);
7035 l_mcc_record = p_tcp->m_mcc_records;
7037 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
7038 if (l_mcc_record->m_index == p_index) {
7043 if (i==p_tcp->m_nb_mcc_records) {
7044 /** element discarded **/
7048 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
7049 /** do not support number of comps != image */
7053 l_deco_array = l_mcc_record->m_decorrelation_array;
7056 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
7057 if (l_deco_array->m_data_size != l_data_size) {
7061 l_nb_elem = p_image->numcomps * p_image->numcomps;
7062 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
7063 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7065 if (! p_tcp->m_mct_decoding_matrix ) {
7069 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);
7072 l_offset_array = l_mcc_record->m_offset_array;
7074 if (l_offset_array) {
7075 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
7076 if (l_offset_array->m_data_size != l_data_size) {
7080 l_nb_elem = p_image->numcomps;
7081 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7082 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7084 if (! l_offset_data ) {
7088 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7090 l_tccp = p_tcp->tccps;
7091 l_current_offset_data = l_offset_data;
7093 for (i=0;i<p_image->numcomps;++i) {
7094 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7098 opj_free(l_offset_data);
7105 * Writes the CBD marker (Component bit depth definition)
7107 * @param p_stream the stream to write data to.
7108 * @param p_j2k J2K codec.
7109 * @param p_manager the user event manager.
7111 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7112 struct opj_stream_private *p_stream,
7113 struct opj_event_mgr * p_manager )
7116 OPJ_UINT32 l_cbd_size;
7117 OPJ_BYTE * l_current_data = 00;
7118 opj_image_t *l_image = 00;
7119 opj_image_comp_t * l_comp = 00;
7122 assert(p_j2k != 00);
7123 assert(p_manager != 00);
7124 assert(p_stream != 00);
7126 l_image = p_j2k->m_private_image;
7127 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7129 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7130 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7131 = (OPJ_BYTE*)opj_realloc(
7132 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7135 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7139 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7142 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7144 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7145 l_current_data += 2;
7147 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7148 l_current_data += 2;
7150 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7153 l_comp = l_image->comps;
7155 for (i=0;i<l_image->numcomps;++i) {
7156 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7162 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) {
7170 * Reads a CBD marker (Component bit depth definition)
7171 * @param p_header_data the data contained in the CBD box.
7172 * @param p_j2k the jpeg2000 codec.
7173 * @param p_header_size the size of the data contained in the CBD marker.
7174 * @param p_manager the user event manager.
7176 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7177 OPJ_BYTE * p_header_data,
7178 OPJ_UINT32 p_header_size,
7179 struct opj_event_mgr * p_manager)
7181 OPJ_UINT32 l_nb_comp,l_num_comp;
7182 OPJ_UINT32 l_comp_def;
7184 opj_image_comp_t * l_comp = 00;
7187 assert(p_header_data != 00);
7188 assert(p_j2k != 00);
7189 assert(p_manager != 00);
7191 l_num_comp = p_j2k->m_private_image->numcomps;
7193 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7194 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7198 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7201 if (l_nb_comp != l_num_comp) {
7202 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7206 l_comp = p_j2k->m_private_image->comps;
7207 for (i=0;i<l_num_comp;++i) {
7208 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7210 l_comp->sgnd = (l_comp_def>>7) & 1;
7211 l_comp->prec = (l_comp_def&0x7f) + 1;
7219 /* ----------------------------------------------------------------------- */
7220 /* J2K / JPT decoder interface */
7221 /* ----------------------------------------------------------------------- */
7223 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
7224 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7228 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
7229 if(!j2k->default_tcp) {
7235 j2k->tile_data = NULL;
7240 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7243 if(j2k->tile_len != NULL) {
7244 opj_free(j2k->tile_len);
7246 if(j2k->tile_data != NULL) {
7247 opj_free(j2k->tile_data);
7249 if(j2k->default_tcp != NULL) {
7250 opj_tcp_t *default_tcp = j2k->default_tcp;
7251 if(default_tcp->ppt_data_first != NULL) {
7252 opj_free(default_tcp->ppt_data_first);
7254 if(j2k->default_tcp->tccps != NULL) {
7255 opj_free(j2k->default_tcp->tccps);
7257 opj_free(j2k->default_tcp);
7259 if(j2k->cp != NULL) {
7260 opj_cp_t *cp = j2k->cp;
7261 if(cp->tcps != NULL) {
7262 for(i = 0; i < cp->tw * cp->th; i++) {
7263 if(cp->tcps[i].ppt_data_first != NULL) {
7264 opj_free(cp->tcps[i].ppt_data_first);
7266 if(cp->tcps[i].tccps != NULL) {
7267 opj_free(cp->tcps[i].tccps);
7272 if(cp->ppm_data_first != NULL) {
7273 opj_free(cp->ppm_data_first);
7275 if(cp->tileno != NULL) {
7276 opj_free(cp->tileno);
7278 if(cp->comment != NULL) {
7279 opj_free(cp->comment);
7287 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
7288 if(j2k && parameters) {
7289 /* create and initialize the coding parameters structure */
7290 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7291 cp->reduce = parameters->cp_reduce;
7292 cp->layer = parameters->cp_layer;
7293 cp->limit_decoding = parameters->cp_limit_decoding;
7296 cp->correct = parameters->jpwl_correct;
7297 cp->exp_comps = parameters->jpwl_exp_comps;
7298 cp->max_tiles = parameters->jpwl_max_tiles;
7299 #endif /* USE_JPWL */
7302 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
7307 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7309 if(j2k && parameters) {
7310 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7311 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7314 j2k->m_cp.correct = parameters->jpwl_correct;
7315 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7316 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7317 #endif /* USE_JPWL */
7321 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7322 opj_image_t *image = NULL;
7324 opj_common_ptr cinfo = j2k->cinfo;
7327 j2k->cstr_info = cstr_info;
7329 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
7331 /* create an empty image */
7332 image = opj_image_create0();
7335 j2k->state = J2K_STATE_MHSOC;
7338 opj_dec_mstabent_t *e;
7339 int id = cio_read(cio, 2);
7342 /* we try to honor JPWL correction power */
7343 if (j2k->cp->correct) {
7345 int orig_pos = cio_tell(cio);
7348 /* call the corrector */
7349 status = jpwl_correct(j2k);
7351 /* go back to where you were */
7352 cio_seek(cio, orig_pos - 2);
7354 /* re-read the marker */
7355 id = cio_read(cio, 2);
7357 /* check whether it begins with ff */
7358 if (id >> 8 != 0xff) {
7359 opj_event_msg(cinfo, EVT_ERROR,
7360 "JPWL: possible bad marker %x at %d\n",
7361 id, cio_tell(cio) - 2);
7363 opj_image_destroy(image);
7364 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
7367 /* we try to correct */
7369 cio_seek(cio, cio_tell(cio) - 2);
7370 cio_write(cio, id, 2);
7371 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
7372 "- setting marker to %x\n",
7377 #endif /* USE_JPWL */
7379 if (id >> 8 != 0xff) {
7380 opj_image_destroy(image);
7381 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7384 e = j2k_dec_mstab_lookup(id);
7385 /* Check if the marker is known*/
7386 if (!(j2k->state & e->states)) {
7387 opj_image_destroy(image);
7388 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7391 /* Check if the decoding is limited to the main header*/
7392 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
7393 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
7400 if (j2k->state & J2K_STATE_ERR)
7403 if (j2k->state == J2K_STATE_MT) {
7406 if (j2k->state == J2K_STATE_NEOC) {
7410 if (j2k->state == J2K_STATE_NEOC) {
7414 if (j2k->state != J2K_STATE_MT) {
7415 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7421 * Read a JPT-stream and decode file
7424 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7425 opj_image_t *image = NULL;
7426 opj_jpt_msg_header_t header;
7428 opj_common_ptr cinfo = j2k->cinfo;
7430 OPJ_ARG_NOT_USED(cstr_info);
7434 /* create an empty image */
7435 image = opj_image_create0();
7438 j2k->state = J2K_STATE_MHSOC;
7440 /* Initialize the header */
7441 jpt_init_msg_header(&header);
7442 /* Read the first header of the message */
7443 jpt_read_msg_header(cinfo, cio, &header);
7445 position = cio_tell(cio);
7446 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7447 opj_image_destroy(image);
7448 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7453 opj_dec_mstabent_t *e = NULL;
7456 if (!cio_numbytesleft(cio)) {
7460 /* data-bin read -> need to read a new header */
7461 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7462 jpt_read_msg_header(cinfo, cio, &header);
7463 position = cio_tell(cio);
7464 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7465 opj_image_destroy(image);
7466 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7471 id = cio_read(cio, 2);
7472 if (id >> 8 != 0xff) {
7473 opj_image_destroy(image);
7474 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7477 e = j2k_dec_mstab_lookup(id);
7478 if (!(j2k->state & e->states)) {
7479 opj_image_destroy(image);
7480 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7486 if (j2k->state == J2K_STATE_MT) {
7489 if (j2k->state == J2K_STATE_NEOC) {
7493 if (j2k->state == J2K_STATE_NEOC) {
7497 if (j2k->state != J2K_STATE_MT) {
7498 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7504 /* ----------------------------------------------------------------------- */
7505 /* J2K encoder interface */
7506 /* ----------------------------------------------------------------------- */
7508 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7509 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7516 opj_j2k_v2_t* j2k_create_compress_v2(void)
7518 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7523 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7525 l_j2k->m_is_decoder = 0;
7526 l_j2k->m_cp.m_is_decoder = 0;
7528 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7529 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7534 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7536 /* validation list creation*/
7537 l_j2k->m_validation_list = opj_procedure_list_create();
7538 if (! l_j2k->m_validation_list) {
7543 /* execution list creation*/
7544 l_j2k->m_procedure_list = opj_procedure_list_create();
7545 if (! l_j2k->m_procedure_list) {
7553 void j2k_destroy_compress(opj_j2k_t *j2k) {
7557 if(j2k->cp != NULL) {
7558 opj_cp_t *cp = j2k->cp;
7561 opj_free(cp->comment);
7564 opj_free(cp->matrice);
7566 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7567 opj_free(cp->tcps[tileno].tccps);
7576 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7578 int tileno, numpocs_tile;
7579 opj_cp_t *cp = NULL;
7581 if(!j2k || !parameters || ! image) {
7585 /* create and initialize the coding parameters structure */
7586 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7588 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7591 /* set default values for cp */
7596 copy user encoding parameters
7598 cp->cinema = parameters->cp_cinema;
7599 cp->max_comp_size = parameters->max_comp_size;
7600 cp->rsiz = parameters->cp_rsiz;
7601 cp->disto_alloc = parameters->cp_disto_alloc;
7602 cp->fixed_alloc = parameters->cp_fixed_alloc;
7603 cp->fixed_quality = parameters->cp_fixed_quality;
7605 /* mod fixed_quality */
7606 if(parameters->cp_matrice) {
7607 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7608 cp->matrice = (int *) opj_malloc(array_size);
7609 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7613 cp->tdx = parameters->cp_tdx;
7614 cp->tdy = parameters->cp_tdy;
7617 cp->tx0 = parameters->cp_tx0;
7618 cp->ty0 = parameters->cp_ty0;
7620 /* comment string */
7621 if(parameters->cp_comment) {
7622 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7624 strcpy(cp->comment, parameters->cp_comment);
7629 calculate other encoding parameters
7632 if (parameters->tile_size_on) {
7633 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7634 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7636 cp->tdx = image->x1 - cp->tx0;
7637 cp->tdy = image->y1 - cp->ty0;
7640 if(parameters->tp_on){
7641 cp->tp_flag = parameters->tp_flag;
7646 for(i=0;i<image->numcomps ;i++){
7647 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7653 calculate JPWL encoding parameters
7656 if (parameters->jpwl_epc_on) {
7660 cp->epc_on = OPJ_TRUE;
7661 cp->info_on = OPJ_FALSE; /* no informative technique */
7664 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7665 cp->epb_on = OPJ_TRUE;
7667 cp->hprot_MH = parameters->jpwl_hprot_MH;
7668 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7669 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7670 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7672 /* if tile specs are not specified, copy MH specs */
7673 if (cp->hprot_TPH[0] == -1) {
7674 cp->hprot_TPH_tileno[0] = 0;
7675 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7677 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7678 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7679 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7680 cp->pprot[i] = parameters->jpwl_pprot[i];
7684 /* set ESD writing */
7685 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7686 cp->esd_on = OPJ_TRUE;
7688 cp->sens_size = parameters->jpwl_sens_size;
7689 cp->sens_addr = parameters->jpwl_sens_addr;
7690 cp->sens_range = parameters->jpwl_sens_range;
7692 cp->sens_MH = parameters->jpwl_sens_MH;
7693 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7694 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7695 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7699 /* always set RED writing to false: we are at the encoder */
7700 cp->red_on = OPJ_FALSE;
7703 cp->epc_on = OPJ_FALSE;
7705 #endif /* USE_JPWL */
7708 /* initialize the mutiple tiles */
7709 /* ---------------------------- */
7710 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7712 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7713 opj_tcp_t *tcp = &cp->tcps[tileno];
7714 tcp->numlayers = parameters->tcp_numlayers;
7715 assert ( tcp->numlayers >= 0 );
7716 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7718 if (cp->fixed_quality) {
7719 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7721 tcp->rates[j] = parameters->tcp_rates[j];
7723 if (cp->fixed_quality) { /* add fixed_quality */
7724 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7726 tcp->rates[j] = parameters->tcp_rates[j];
7730 tcp->csty = parameters->csty;
7731 tcp->prg = parameters->prog_order;
7732 tcp->mct = parameters->tcp_mct;
7736 if (parameters->numpocs) {
7737 /* initialisation of POC */
7739 assert( parameters->numpocs >= 0 );
7740 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7741 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7742 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7743 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7744 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7745 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7746 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7747 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7748 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7749 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7753 tcp->numpocs = numpocs_tile -1 ;
7758 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7760 for (i = 0; i < image->numcomps; i++) {
7761 opj_tccp_t *tccp = &tcp->tccps[i];
7762 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7763 tccp->numresolutions = parameters->numresolution;
7764 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7765 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7766 tccp->cblksty = parameters->mode;
7767 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7768 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7770 assert(parameters->roi_compno >= 0);
7771 if (i == (OPJ_UINT32)parameters->roi_compno) {
7772 tccp->roishift = parameters->roi_shift;
7777 if(parameters->cp_cinema)
7779 /*Precinct size for lowest frequency subband=128*/
7782 /*Precinct size at all other resolutions = 256*/
7783 for (j = 1; j < tccp->numresolutions; j++) {
7788 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7790 assert(tccp->numresolutions > 0);
7791 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7792 if (p < parameters->res_spec) {
7794 if (parameters->prcw_init[p] < 1) {
7797 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7800 if (parameters->prch_init[p] < 1) {
7803 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7807 int res_spec = parameters->res_spec;
7808 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7809 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7811 if (size_prcw < 1) {
7814 tccp->prcw[j] = int_floorlog2(size_prcw);
7817 if (size_prch < 1) {
7820 tccp->prch[j] = int_floorlog2(size_prch);
7824 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7827 for (j = 0; j < tccp->numresolutions; j++) {
7834 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7839 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7842 opj_cp_t *cp = NULL;
7844 opj_tcd_t *tcd = NULL; /* TCD component */
7852 j2k->cstr_info = cstr_info;
7855 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7856 cstr_info->image_w = image->x1 - image->x0;
7857 cstr_info->image_h = image->y1 - image->y0;
7858 cstr_info->prog = (&cp->tcps[0])->prg;
7859 cstr_info->tw = cp->tw;
7860 cstr_info->th = cp->th;
7861 cstr_info->tile_x = cp->tdx; /* new version parser */
7862 cstr_info->tile_y = cp->tdy; /* new version parser */
7863 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7864 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7865 cstr_info->numcomps = image->numcomps;
7866 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7867 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7868 for (compno=0; compno < image->numcomps; compno++) {
7869 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7871 cstr_info->D_max = 0.0; /* ADD Marcela */
7872 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7873 cstr_info->maxmarknum = 100;
7874 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7875 cstr_info->marknum = 0;
7885 for (compno = 1; compno < image->numcomps; compno++) {
7886 j2k_write_coc(j2k, compno);
7887 j2k_write_qcc(j2k, compno);
7891 for (compno = 0; compno < image->numcomps; compno++) {
7892 opj_tcp_t *tcp = &cp->tcps[0];
7893 if (tcp->tccps[compno].roishift)
7894 j2k_write_rgn(j2k, compno, 0);
7896 if (cp->comment != NULL) {
7900 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7904 if (cp->cinema == CINEMA4K_24) {
7909 /* uncomment only for testing JPSEC marker writing */
7910 /* j2k_write_sec(j2k); */
7914 cstr_info->main_head_end = cio_tell(cio) - 1;
7917 /**** Main Header ENDS here ***/
7919 /* create the tile encoder */
7920 tcd = tcd_create(j2k->cinfo);
7922 /* encode each tile */
7923 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7927 int acc_pack_num = 0;
7931 opj_tcp_t *tcp = &cp->tcps[tileno];
7932 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7934 j2k->curtileno = tileno;
7935 j2k->cur_tp_num = 0;
7936 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7937 /* initialisation before tile encoding */
7939 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7941 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7946 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7947 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7948 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7949 cstr_info->tile[j2k->curtileno].marknum = 0;
7953 for(pino = 0; pino <= tcp->numpocs; pino++) {
7957 /*Get number of tile parts*/
7958 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7959 tcd->tp_pos = cp->tp_pos;
7961 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7962 j2k->tp_num = tilepartno;
7965 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7966 cio_tell(cio) + j2k->pos_correction;
7970 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7971 for (compno = 1; compno < image->numcomps; compno++) {
7972 j2k_write_coc(j2k, compno);
7973 j2k_write_qcc(j2k, compno);
7975 if (cp->tcps[tileno].numpocs) {
7982 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
7983 cio_tell(cio) + j2k->pos_correction + 1;
7986 j2k_write_sod(j2k, tcd);
7990 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
7991 cio_tell(cio) + j2k->pos_correction - 1;
7992 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
7994 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
7995 cstr_info->packno - acc_pack_num;
7996 acc_pack_num = cstr_info->packno;
8004 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8009 if (tile->PPT) { // BAD PPT !!!
8012 PPT_file=fopen("PPT","rb");
8013 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8014 for (i=0;i<tile->len_ppt;i++) {
8016 fread(&elmt, 1, 1, PPT_file);
8017 fwrite(&elmt,1,1,f);
8026 /* destroy the tile encoder */
8027 tcd_free_encode(tcd);
8030 opj_free(j2k->cur_totnum_tp);
8035 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8037 /* The following adjustment is done to adjust the codestream size */
8038 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8039 /* the first bunch of bytes is not in the codestream */
8040 cstr_info->codestream_size -= cstr_info->main_head_start;
8046 preparation of JPWL marker segments
8050 /* encode according to JPWL */
8051 jpwl_encode(j2k, cio, image);
8054 #endif /* USE_JPWL */
8059 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8061 assert(cstr_info != 00);
8063 /* expand the list? */
8064 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8065 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8066 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8069 /* add the marker */
8070 cstr_info->marker[cstr_info->marknum].type = type;
8071 cstr_info->marker[cstr_info->marknum].pos = pos;
8072 cstr_info->marker[cstr_info->marknum].len = len;
8073 cstr_info->marknum++;
8077 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8079 assert(cstr_index != 00);
8081 /* expand the list? */
8082 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8083 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8084 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8087 /* add the marker */
8088 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8089 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8090 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8091 cstr_index->marknum++;
8095 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8097 opj_marker_info_t *marker;
8099 assert(cstr_info != 00);
8101 /* expand the list? */
8102 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8103 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8104 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8107 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8109 /* add the marker */
8110 marker->type = type;
8113 cstr_info->tile[tileno].marknum++;
8116 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)
8118 assert(cstr_index != 00);
8119 assert(cstr_index->tile_index != 00);
8121 /* expand the list? */
8122 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8123 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8124 cstr_index->tile_index[tileno].marker =
8125 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8126 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8129 /* add the marker */
8130 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8131 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8132 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8133 cstr_index->tile_index[tileno].marknum++;
8135 if (type == J2K_MS_SOT) {
8136 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8138 if (cstr_index->tile_index[tileno].tp_index)
8139 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8146 * -----------------------------------------------------------------------
8147 * -----------------------------------------------------------------------
8148 * -----------------------------------------------------------------------
8152 * Ends the decompression procedures and possibiliy add data to be read after the
8155 opj_bool j2k_end_decompress(
8156 opj_j2k_v2_t *p_j2k,
8157 opj_stream_private_t *p_stream,
8158 opj_event_mgr_t * p_manager)
8167 * Reads a jpeg2000 codestream header structure.
8170 * @param p_stream the stream to read data from.
8171 * @param p_j2k the jpeg2000 codec.
8172 * @param p_manager the user event manager.
8174 * @return true if the box is valid.
8176 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8177 opj_j2k_v2_t* p_j2k,
8178 opj_image_t** p_image,
8179 struct opj_event_mgr* p_manager )
8182 assert(p_j2k != 00);
8183 assert(p_stream != 00);
8184 assert(p_manager != 00);
8186 /* create an empty image header */
8187 p_j2k->m_private_image = opj_image_create0();
8188 if (! p_j2k->m_private_image) {
8192 /* customization of the validation */
8193 j2k_setup_decoding_validation(p_j2k);
8195 /* validation of the parameters codec */
8196 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8197 opj_image_destroy(p_j2k->m_private_image);
8198 p_j2k->m_private_image = NULL;
8202 /* customization of the encoding */
8203 j2k_setup_header_reading(p_j2k);
8206 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8207 opj_image_destroy(p_j2k->m_private_image);
8208 p_j2k->m_private_image = NULL;
8212 *p_image = opj_image_create0();
8217 /* Copy codestream image information to the output image */
8218 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8225 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8227 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8230 assert(p_j2k != 00);
8232 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8234 /* DEVELOPER CORNER, add your custom procedures */
8235 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8240 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8241 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8243 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8246 assert(p_j2k != 00);
8248 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8249 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8250 /* DEVELOPER CORNER, add your custom validation procedure */
8256 * The mct encoding validation procedure.
8258 * @param p_j2k the jpeg2000 codec to validate.
8259 * @param p_stream the input stream to validate.
8260 * @param p_manager the user event manager.
8262 * @return true if the parameters are correct.
8264 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8265 opj_stream_private_t *p_stream,
8266 opj_event_mgr_t * p_manager )
8268 opj_bool l_is_valid = OPJ_TRUE;
8272 assert(p_j2k != 00);
8273 assert(p_stream != 00);
8274 assert(p_manager != 00);
8276 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8277 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8278 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8280 for (i=0;i<l_nb_tiles;++i) {
8281 if (l_tcp->mct == 2) {
8282 opj_tccp_t * l_tccp = l_tcp->tccps;
8283 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8285 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8286 l_is_valid &= ! (l_tccp->qmfbid & 1);
8298 * Builds the cp decoder parameters to use to decode tile.
8300 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8301 opj_stream_private_t *p_stream,
8302 opj_event_mgr_t * p_manager )
8304 /* add here initialization of cp
8305 copy paste of setup_decoder */
8313 * Builds the cp encoder parameters to use to encode tile.
8315 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8316 opj_stream_private_t *p_stream,
8317 opj_event_mgr_t * p_manager )
8319 /* add here initialization of cp
8320 copy paste of setup_encoder */
8328 * The default encoding validation procedure without any extension.
8330 * @param p_j2k the jpeg2000 codec to validate.
8331 * @param p_stream the input stream to validate.
8332 * @param p_manager the user event manager.
8334 * @return true if the parameters are correct.
8336 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8337 opj_stream_private_t *p_stream,
8338 opj_event_mgr_t * p_manager )
8340 opj_bool l_is_valid = OPJ_TRUE;
8343 assert(p_j2k != 00);
8344 assert(p_stream != 00);
8345 assert(p_manager != 00);
8347 /* STATE checking */
8348 /* make sure the state is at 0 */
8349 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8351 /* POINTER validation */
8352 /* make sure a p_j2k codec is present */
8353 l_is_valid &= (p_j2k->m_procedure_list != 00);
8354 /* make sure a validation list is present */
8355 l_is_valid &= (p_j2k->m_validation_list != 00);
8357 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8358 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8362 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8363 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8367 /* PARAMETER VALIDATION */
8372 * The default decoding validation procedure without any extension.
8374 * @param p_j2k the jpeg2000 codec to validate.
8375 * @param p_stream the input stream to validate.
8376 * @param p_manager the user event manager.
8378 * @return true if the parameters are correct.
8380 opj_bool j2k_decoding_validation (
8381 opj_j2k_v2_t *p_j2k,
8382 opj_stream_private_t *p_stream,
8383 opj_event_mgr_t * p_manager
8386 opj_bool l_is_valid = OPJ_TRUE;
8389 assert(p_j2k != 00);
8390 assert(p_stream != 00);
8391 assert(p_manager != 00);
8394 /* STATE checking */
8395 /* make sure the state is at 0 */
8397 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8399 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8401 /* POINTER validation */
8402 /* make sure a p_j2k codec is present */
8403 /* make sure a procedure list is present */
8404 l_is_valid &= (p_j2k->m_procedure_list != 00);
8405 /* make sure a validation list is present */
8406 l_is_valid &= (p_j2k->m_validation_list != 00);
8408 /* PARAMETER VALIDATION */
8412 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8413 struct opj_stream_private *p_stream,
8414 struct opj_event_mgr * p_manager)
8416 OPJ_UINT32 l_current_marker;
8417 OPJ_UINT32 l_marker_size;
8418 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8421 assert(p_stream != 00);
8422 assert(p_j2k != 00);
8423 assert(p_manager != 00);
8425 /* We enter in the main header */
8426 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8428 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8429 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
8430 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8434 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8435 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8436 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8440 /* Read 2 bytes as the new marker ID */
8441 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8443 /* Try to read until the SOT is detected */
8444 while (l_current_marker != J2K_MS_SOT) {
8446 /* Check if the current marker ID is valid */
8447 if (l_current_marker < 0xff00) {
8448 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8452 /* Get the marker handler from the marker ID */
8453 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8455 /* Manage case where marker is unknown */
8456 if (l_marker_handler->id == J2K_MS_UNK) {
8457 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8458 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8462 if (l_current_marker == J2K_MS_SOT)
8463 break; /* SOT marker is detected main header is completely read */
8464 else /* Get the marker handler from the marker ID */
8465 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8468 /* Check if the marker is known and if it is the right place to find it */
8469 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8470 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8474 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8475 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8476 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8480 /* read 2 bytes as the marker size */
8481 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8482 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8484 /* Check if the marker size is compatible with the header data size */
8485 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8486 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8487 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8488 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8491 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8494 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8495 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) {
8496 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8500 /* Read the marker segment with the correct marker handler */
8501 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8502 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8506 /* Add the marker to the codestream index*/
8507 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8508 l_marker_handler->id,
8509 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8510 l_marker_size + 4 );
8512 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8513 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8514 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8518 /* read 2 bytes as the new marker ID */
8519 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8522 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8524 /* Position of the last element if the main header */
8525 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8527 /* Next step: read a tile-part header */
8528 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8534 * Excutes the given procedures on the given codec.
8536 * @param p_procedure_list the list of procedures to execute
8537 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8538 * @param p_stream the stream to execute the procedures on.
8539 * @param p_manager the user manager.
8541 * @return true if all the procedures were successfully executed.
8543 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8544 opj_procedure_list_t * p_procedure_list,
8545 opj_stream_private_t *p_stream,
8546 opj_event_mgr_t * p_manager )
8548 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8549 opj_bool l_result = OPJ_TRUE;
8550 OPJ_UINT32 l_nb_proc, i;
8553 assert(p_procedure_list != 00);
8554 assert(p_j2k != 00);
8555 assert(p_stream != 00);
8556 assert(p_manager != 00);
8559 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8560 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);
8562 for (i=0;i<l_nb_proc;++i) {
8563 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8567 /* and clear the procedure list at the end.*/
8568 opj_procedure_list_clear(p_procedure_list);
8573 opj_bool j2k_copy_default_tcp_and_create_tcd
8575 opj_j2k_v2_t * p_j2k,
8576 opj_stream_private_t *p_stream,
8577 opj_event_mgr_t * p_manager
8580 opj_tcp_v2_t * l_tcp = 00;
8581 opj_tcp_v2_t * l_default_tcp = 00;
8582 OPJ_UINT32 l_nb_tiles;
8584 opj_tccp_t *l_current_tccp = 00;
8585 OPJ_UINT32 l_tccp_size;
8586 OPJ_UINT32 l_mct_size;
8587 opj_image_t * l_image;
8588 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8589 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8590 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8591 OPJ_UINT32 l_offset;
8594 assert(p_j2k != 00);
8595 assert(p_stream != 00);
8596 assert(p_manager != 00);
8598 l_image = p_j2k->m_private_image;
8599 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8600 l_tcp = p_j2k->m_cp.tcps;
8601 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8602 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8603 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8606 for (i=0; i<l_nb_tiles; ++i) {
8607 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8608 l_current_tccp = l_tcp->tccps;
8609 /*Copy default coding parameters into the current tile coding parameters*/
8610 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8611 /* Initialize some values of the current tile coding parameters*/
8613 l_tcp->ppt_data = 00;
8614 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8615 l_tcp->tccps = l_current_tccp;
8617 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8618 if (l_default_tcp->m_mct_decoding_matrix) {
8619 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8620 if (! l_tcp->m_mct_decoding_matrix ) {
8623 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8626 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8627 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8628 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8629 if (! l_tcp->m_mct_records) {
8632 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8634 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8635 l_src_mct_rec = l_default_tcp->m_mct_records;
8636 l_dest_mct_rec = l_tcp->m_mct_records;
8638 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8640 if (l_src_mct_rec->m_data) {
8642 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8643 if(! l_dest_mct_rec->m_data) {
8646 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8653 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8654 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8655 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8656 if (! l_tcp->m_mcc_records) {
8659 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8661 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8662 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8663 l_dest_mcc_rec = l_tcp->m_mcc_records;
8665 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8667 if (l_src_mcc_rec->m_decorrelation_array) {
8668 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8669 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8672 if (l_src_mcc_rec->m_offset_array) {
8673 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8674 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8681 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8682 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8684 /* Move to next tile cp*/
8688 /* Create the current tile decoder*/
8689 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8690 if (! p_j2k->m_tcd ) {
8694 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8695 tcd_destroy_v2(p_j2k->m_tcd);
8697 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8705 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8706 * with the marker value.
8707 * @param p_id Marker value to look up
8709 * @return the handler associated with the id.
8711 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8713 const opj_dec_memory_marker_handler_t *e;
8714 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8715 if (e->id == p_id) {
8716 break; /* we find a handler corresponding to the marker ID*/
8724 * Destroys a jpeg2000 codec.
8726 * @param p_j2k the jpeg20000 structure to destroy.
8728 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8734 if (p_j2k->m_is_decoder) {
8736 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8737 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8738 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8739 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8742 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8743 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8744 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8745 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8750 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8751 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8752 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8755 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8756 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8757 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8758 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8761 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8762 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8763 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8764 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8768 tcd_destroy_v2(p_j2k->m_tcd);
8770 j2k_cp_destroy(&(p_j2k->m_cp));
8771 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8773 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8774 p_j2k->m_procedure_list = 00;
8776 opj_procedure_list_destroy(p_j2k->m_validation_list);
8777 p_j2k->m_procedure_list = 00;
8779 j2k_destroy_cstr_index(p_j2k->cstr_index);
8780 p_j2k->cstr_index = NULL;
8782 opj_image_destroy(p_j2k->m_private_image);
8783 p_j2k->m_private_image = NULL;
8785 opj_image_destroy(p_j2k->m_output_image);
8786 p_j2k->m_output_image = NULL;
8791 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8795 if (p_cstr_ind->marker) {
8796 opj_free(p_cstr_ind->marker);
8797 p_cstr_ind->marker = NULL;
8800 if (p_cstr_ind->tile_index) {
8801 OPJ_UINT32 it_tile = 0;
8803 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8805 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8806 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8807 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8810 if(p_cstr_ind->tile_index[it_tile].tp_index){
8811 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8812 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8815 if(p_cstr_ind->tile_index[it_tile].marker){
8816 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8817 p_cstr_ind->tile_index[it_tile].marker = NULL;
8822 opj_free( p_cstr_ind->tile_index);
8823 p_cstr_ind->tile_index = NULL;
8826 opj_free(p_cstr_ind);
8833 * Destroys a tile coding parameter structure.
8835 * @param p_tcp the tile coding parameter to destroy.
8837 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8843 if (p_tcp->ppt_buffer != 00) {
8844 opj_free(p_tcp->ppt_buffer);
8845 p_tcp->ppt_buffer = 00;
8848 if (p_tcp->tccps != 00) {
8849 opj_free(p_tcp->tccps);
8853 if (p_tcp->m_mct_coding_matrix != 00) {
8854 opj_free(p_tcp->m_mct_coding_matrix);
8855 p_tcp->m_mct_coding_matrix = 00;
8858 if (p_tcp->m_mct_decoding_matrix != 00) {
8859 opj_free(p_tcp->m_mct_decoding_matrix);
8860 p_tcp->m_mct_decoding_matrix = 00;
8863 if (p_tcp->m_mcc_records) {
8864 opj_free(p_tcp->m_mcc_records);
8865 p_tcp->m_mcc_records = 00;
8866 p_tcp->m_nb_max_mcc_records = 0;
8867 p_tcp->m_nb_mcc_records = 0;
8870 if (p_tcp->m_mct_records) {
8871 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8874 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8875 if (l_mct_data->m_data) {
8876 opj_free(l_mct_data->m_data);
8877 l_mct_data->m_data = 00;
8883 opj_free(p_tcp->m_mct_records);
8884 p_tcp->m_mct_records = 00;
8887 if (p_tcp->mct_norms != 00) {
8888 opj_free(p_tcp->mct_norms);
8889 p_tcp->mct_norms = 00;
8892 j2k_tcp_data_destroy(p_tcp);
8897 * Destroys the data inside a tile coding parameter structure.
8899 * @param p_tcp the tile coding parameter which contain data to destroy.
8901 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
8903 if (p_tcp->m_data) {
8904 opj_free(p_tcp->m_data);
8905 p_tcp->m_data = NULL;
8906 p_tcp->m_data_size = 0;
8911 * Destroys a coding parameter structure.
8913 * @param p_cp the coding parameter to destroy.
8915 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
8917 OPJ_UINT32 l_nb_tiles;
8918 opj_tcp_v2_t * l_current_tile = 00;
8929 l_current_tile = p_cp->tcps;
8930 l_nb_tiles = p_cp->th * p_cp->tw;
8933 (i = 0; i < l_nb_tiles; ++i)
8935 j2k_tcp_destroy(l_current_tile);
8938 opj_free(p_cp->tcps);
8942 (p_cp->ppm_buffer != 00)
8944 opj_free(p_cp->ppm_buffer);
8945 p_cp->ppm_buffer = 00;
8948 (p_cp->comment != 00)
8950 opj_free(p_cp->comment);
8954 (! p_cp->m_is_decoder)
8957 (p_cp->m_specific_param.m_enc.m_matrice)
8959 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8960 p_cp->m_specific_param.m_enc.m_matrice = 00;
8968 * Reads a tile header.
8969 * @param p_j2k the jpeg2000 codec.
8970 * @param p_stream the stream to write data to.
8971 * @param p_manager the user event manager.
8973 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
8974 OPJ_UINT32 * p_tile_index,
8975 OPJ_UINT32 * p_data_size,
8976 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8977 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8978 OPJ_UINT32 * p_nb_comps,
8980 opj_stream_private_t *p_stream,
8981 opj_event_mgr_t * p_manager )
8983 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8984 OPJ_UINT32 l_marker_size;
8985 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8986 opj_tcp_v2_t * l_tcp = NULL;
8987 OPJ_UINT32 l_nb_tiles;
8990 assert(p_stream != 00);
8991 assert(p_j2k != 00);
8992 assert(p_manager != 00);
8994 /* Reach the End Of Codestream ?*/
8995 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
8996 l_current_marker = J2K_MS_EOC;
8998 /* We need to encounter a SOT marker (a new tile-part header) */
8999 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9003 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9004 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9006 /* Try to read until the Start Of Data is detected */
9007 while (l_current_marker != J2K_MS_SOD) {
9009 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9010 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9011 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9015 /* Read 2 bytes from the buffer as the marker size */
9016 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9018 /* Why this condition? FIXME */
9019 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9020 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9022 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9024 /* Get the marker handler from the marker ID */
9025 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9027 /* Check if the marker is known and if it is the right place to find it */
9028 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9029 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9032 /* FIXME manage case of unknown marker as in the main header ? */
9034 /* Check if the marker size is compatible with the header data size */
9035 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9036 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9037 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9038 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9041 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9044 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9045 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) {
9046 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9050 /* Read the marker segment with the correct marker handler */
9051 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9052 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9056 /* Add the marker to the codestream index*/
9057 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9059 l_marker_handler->id,
9060 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9061 l_marker_size + 4 );
9063 /* Keep the position of the last SOT marker read */
9064 if ( l_marker_handler->id == J2K_MS_SOT ) {
9065 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9066 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9068 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9073 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9074 /* Skip the rest of the tile part header*/
9075 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) {
9076 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9079 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9082 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9083 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9084 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9087 /* Read 2 bytes from the buffer as the new marker ID */
9088 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9092 /* If we didn't skip data before, we need to read the SOD marker*/
9093 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9094 /* Try to read the SOD marker and skip data ? FIXME */
9095 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
9101 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9102 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9103 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9104 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9108 /* Read 2 bytes from buffer as the new marker ID */
9109 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9113 /* Indicate we will try to read a new tile-part header*/
9114 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9115 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9116 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9118 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9119 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9120 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9124 /* Read 2 bytes from buffer as the new marker ID */
9125 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9129 /* Current marker is the EOC marker ?*/
9130 if (l_current_marker == J2K_MS_EOC) {
9131 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9132 p_j2k->m_current_tile_number = 0;
9133 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9138 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9139 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9140 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9142 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9143 ++p_j2k->m_current_tile_number;
9147 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9148 *p_go_on = OPJ_FALSE;
9154 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9155 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9159 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9160 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9162 *p_tile_index = p_j2k->m_current_tile_number;
9163 *p_go_on = OPJ_TRUE;
9164 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9165 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9166 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9167 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9168 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9169 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9171 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9177 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9178 OPJ_UINT32 p_tile_index,
9180 OPJ_UINT32 p_data_size,
9181 opj_stream_private_t *p_stream,
9182 opj_event_mgr_t * p_manager )
9184 OPJ_UINT32 l_current_marker;
9185 OPJ_BYTE l_data [2];
9186 opj_tcp_v2_t * l_tcp;
9189 assert(p_stream != 00);
9190 assert(p_j2k != 00);
9191 assert(p_manager != 00);
9193 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9194 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9198 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9199 if (! l_tcp->m_data) {
9200 j2k_tcp_destroy(l_tcp);
9204 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9208 p_j2k->cstr_index) ) {
9209 j2k_tcp_destroy(l_tcp);
9210 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9214 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9218 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9219 * we destroy just the data which will be re-read in read_tile_header*/
9220 /*j2k_tcp_destroy(l_tcp);
9221 p_j2k->m_tcd->tcp = 0;*/
9222 j2k_tcp_data_destroy(l_tcp);
9224 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9225 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9227 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9228 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9229 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9233 opj_read_bytes(l_data,&l_current_marker,2);
9235 if (l_current_marker == J2K_MS_EOC) {
9236 p_j2k->m_current_tile_number = 0;
9237 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9239 else if (l_current_marker != J2K_MS_SOT)
9241 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9250 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9252 OPJ_UINT32 i,j,k = 0;
9253 OPJ_UINT32 l_width_src,l_height_src;
9254 OPJ_UINT32 l_width_dest,l_height_dest;
9255 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9256 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9257 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9258 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9259 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9261 opj_image_comp_t * l_img_comp_src = 00;
9262 opj_image_comp_t * l_img_comp_dest = 00;
9264 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9265 opj_image_t * l_image_src = 00;
9266 OPJ_UINT32 l_size_comp, l_remaining;
9267 OPJ_INT32 * l_dest_ptr;
9268 opj_tcd_resolution_v2_t* l_res= 00;
9270 l_tilec = p_tcd->tcd_image->tiles->comps;
9271 l_image_src = p_tcd->image;
9272 l_img_comp_src = l_image_src->comps;
9274 l_img_comp_dest = p_output_image->comps;
9276 for (i=0; i<l_image_src->numcomps; i++) {
9278 /* Allocate output component buffer if necessary */
9279 if (!l_img_comp_dest->data) {
9281 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9282 if (! l_img_comp_dest->data) {
9287 /* Copy info from decoded comp image to output image */
9288 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9291 /* Compute the precision of the output buffer */
9292 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9293 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9294 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9300 if (l_size_comp == 3) {
9305 /* Current tile component size*/
9307 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9308 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9311 l_width_src = (l_res->x1 - l_res->x0);
9312 l_height_src = (l_res->y1 - l_res->y0);
9314 /* Border of the current output component*/
9315 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9316 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9317 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9318 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9321 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9322 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9326 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9327 * of the input buffer (decoded tile component) which will be move
9328 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9329 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9330 * by this input area.
9332 assert( l_res->x0 >= 0);
9333 assert( l_res->x1 >= 0);
9334 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9335 l_start_x_dest = l_res->x0 - l_x0_dest;
9336 l_offset_x0_src = 0;
9338 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9339 l_width_dest = l_width_src;
9340 l_offset_x1_src = 0;
9343 l_width_dest = l_x1_dest - l_res->x0 ;
9344 l_offset_x1_src = l_width_src - l_width_dest;
9348 l_start_x_dest = 0 ;
9349 l_offset_x0_src = l_x0_dest - l_res->x0;
9351 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9352 l_width_dest = l_width_src - l_offset_x0_src;
9353 l_offset_x1_src = 0;
9356 l_width_dest = l_img_comp_dest->w ;
9357 l_offset_x1_src = l_res->x1 - l_x1_dest;
9361 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9362 l_start_y_dest = l_res->y0 - l_y0_dest;
9363 l_offset_y0_src = 0;
9365 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9366 l_height_dest = l_height_src;
9367 l_offset_y1_src = 0;
9370 l_height_dest = l_y1_dest - l_res->y0 ;
9371 l_offset_y1_src = l_height_src - l_height_dest;
9375 l_start_y_dest = 0 ;
9376 l_offset_y0_src = l_y0_dest - l_res->y0;
9378 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9379 l_height_dest = l_height_src - l_offset_y0_src;
9380 l_offset_y1_src = 0;
9383 l_height_dest = l_img_comp_dest->h ;
9384 l_offset_y1_src = l_res->y1 - l_y1_dest;
9388 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9393 /* Compute the input buffer offset */
9394 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9395 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9396 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9398 /* Compute the output buffer offset */
9399 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9400 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9402 /* Move the output buffer to the first place where we will write*/
9403 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9406 fprintf(stdout, "COMPO[%d]:\n",i);
9407 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9408 "\t tile offset:%d, %d, %d, %d\n"
9409 "\t buffer offset: %d; %d, %d\n",
9410 l_res->x0, l_res->y0, l_width_src, l_height_src,
9411 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9412 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9414 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9415 "\t start offset: %d, line offset= %d\n",
9416 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9420 switch (l_size_comp) {
9423 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9424 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9426 if (l_img_comp_src->sgnd) {
9427 for (j = 0 ; j < l_height_dest ; ++j) {
9428 for ( k = 0 ; k < l_width_dest ; ++k) {
9429 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9432 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9433 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9437 for ( j = 0 ; j < l_height_dest ; ++j ) {
9438 for ( k = 0 ; k < l_width_dest ; ++k) {
9439 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9442 l_dest_ptr+= l_line_offset_dest;
9443 l_src_ptr += l_line_offset_src;
9447 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9448 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9453 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9454 l_src_ptr += l_start_offset_src;
9456 if (l_img_comp_src->sgnd) {
9457 for (j=0;j<l_height_dest;++j) {
9458 for (k=0;k<l_width_dest;++k) {
9459 *(l_dest_ptr++) = *(l_src_ptr++);
9462 l_dest_ptr+= l_line_offset_dest;
9463 l_src_ptr += l_line_offset_src ;
9467 for (j=0;j<l_height_dest;++j) {
9468 for (k=0;k<l_width_dest;++k) {
9469 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9472 l_dest_ptr+= l_line_offset_dest;
9473 l_src_ptr += l_line_offset_src ;
9477 l_src_ptr += l_end_offset_src;
9478 p_data = (OPJ_BYTE*) l_src_ptr;
9483 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9484 l_src_ptr += l_start_offset_src;
9486 for (j=0;j<l_height_dest;++j) {
9487 for (k=0;k<l_width_dest;++k) {
9488 *(l_dest_ptr++) = (*(l_src_ptr++));
9491 l_dest_ptr+= l_line_offset_dest;
9492 l_src_ptr += l_line_offset_src ;
9495 l_src_ptr += l_end_offset_src;
9496 p_data = (OPJ_BYTE*) l_src_ptr;
9510 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9512 * @param p_j2k the jpeg2000 codec.
9513 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9514 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9515 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9516 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9517 * @param p_manager the user event manager
9519 * @return true if the area could be set.
9521 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9522 opj_image_t* p_image,
9523 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9524 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9525 struct opj_event_mgr * p_manager )
9527 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9528 opj_image_t * l_image = p_j2k->m_private_image;
9531 OPJ_INT32 l_comp_x1, l_comp_y1;
9532 opj_image_comp_t* l_img_comp = NULL;
9534 /* Check if we are read the main header */
9535 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9536 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9540 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9541 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9543 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9544 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9545 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9546 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9552 /* Check if the positions provided by the user are correct */
9555 assert(p_start_x > 0 );
9556 assert(p_start_y > 0 );
9558 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9559 opj_event_msg_v2(p_manager, EVT_ERROR,
9560 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9561 p_start_x, l_image->x1);
9564 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9565 opj_event_msg_v2(p_manager, EVT_WARNING,
9566 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9567 p_start_x, l_image->x0);
9568 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9569 p_image->x0 = l_image->x0;
9572 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9573 p_image->x0 = p_start_x;
9577 if ((OPJ_UINT32)p_start_y > l_image->y1){
9578 opj_event_msg_v2(p_manager, EVT_ERROR,
9579 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9580 p_start_y, l_image->y1);
9583 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9584 opj_event_msg_v2(p_manager, EVT_WARNING,
9585 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9586 p_start_y, l_image->y0);
9587 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9588 p_image->y0 = l_image->y0;
9591 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9592 p_image->y0 = p_start_y;
9596 assert((OPJ_UINT32)p_end_x > 0);
9597 assert((OPJ_UINT32)p_end_y > 0);
9598 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9599 opj_event_msg_v2(p_manager, EVT_ERROR,
9600 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9601 p_end_x, l_image->x0);
9604 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9605 opj_event_msg_v2(p_manager, EVT_WARNING,
9606 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9607 p_end_x, l_image->x1);
9608 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9609 p_image->x1 = l_image->x1;
9612 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9613 p_image->x1 = p_end_x;
9617 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9618 opj_event_msg_v2(p_manager, EVT_ERROR,
9619 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9620 p_end_y, l_image->y0);
9623 if ((OPJ_UINT32)p_end_y > l_image->y1){
9624 opj_event_msg_v2(p_manager, EVT_WARNING,
9625 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9626 p_end_y, l_image->y1);
9627 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9628 p_image->y1 = l_image->y1;
9631 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9632 p_image->y1 = p_end_y;
9636 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9638 l_img_comp = p_image->comps;
9639 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9643 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9644 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9645 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9646 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9648 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9649 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9651 opj_event_msg_v2(p_manager, EVT_ERROR,
9652 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9656 l_img_comp->w = l_w;
9658 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9659 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9661 opj_event_msg_v2(p_manager, EVT_ERROR,
9662 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9666 l_img_comp->h = l_h;
9671 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9672 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9679 /* ----------------------------------------------------------------------- */
9680 /* J2K / JPT decoder interface */
9681 /* ----------------------------------------------------------------------- */
9683 * Creates a J2K decompression structure.
9685 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9687 opj_j2k_v2_t* j2k_create_decompress_v2(void)
9689 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9693 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9695 l_j2k->m_is_decoder = 1;
9696 l_j2k->m_cp.m_is_decoder = 1;
9698 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9699 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9703 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9705 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9706 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9711 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9713 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9715 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9717 /* codestream index creation */
9718 l_j2k->cstr_index = j2k_create_cstr_index();
9720 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9721 if (!l_j2k->cstr_index){
9726 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9729 /* validation list creation */
9730 l_j2k->m_validation_list = opj_procedure_list_create();
9731 if (! l_j2k->m_validation_list) {
9736 /* execution list creation */
9737 l_j2k->m_procedure_list = opj_procedure_list_create();
9738 if (! l_j2k->m_procedure_list) {
9747 opj_codestream_index_t* j2k_create_cstr_index(void)
9749 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9750 opj_calloc(1,sizeof(opj_codestream_index_t));
9754 cstr_index->maxmarknum = 100;
9755 cstr_index->marknum = 0;
9756 cstr_index->marker = (opj_marker_info_t*)
9757 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9758 if (!cstr_index-> marker)
9761 cstr_index->tile_index = NULL;
9768 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9770 * @param p_tile_no the tile index.
9771 * @param p_comp_no the component being outputted.
9772 * @param p_j2k the J2K codec.
9774 * @return the number of bytes taken by the SPCod element.
9776 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9777 OPJ_UINT32 p_tile_no,
9778 OPJ_UINT32 p_comp_no )
9780 opj_cp_v2_t *l_cp = 00;
9781 opj_tcp_v2_t *l_tcp = 00;
9782 opj_tccp_t *l_tccp = 00;
9785 assert(p_j2k != 00);
9787 l_cp = &(p_j2k->m_cp);
9788 l_tcp = &l_cp->tcps[p_tile_no];
9789 l_tccp = &l_tcp->tccps[p_comp_no];
9791 /* preconditions again */
9792 assert(p_tile_no < (l_cp->tw * l_cp->th));
9793 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9795 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9796 return 5 + l_tccp->numresolutions;
9804 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9806 * @param p_comp_no the component number to output.
9807 * @param p_stream the stream to write data to.
9808 * @param p_j2k J2K codec.
9809 * @param p_manager the user event manager.
9812 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9813 OPJ_UINT32 p_tile_no,
9814 OPJ_UINT32 p_comp_no,
9816 OPJ_UINT32 * p_header_size,
9817 struct opj_event_mgr * p_manager )
9820 opj_cp_v2_t *l_cp = 00;
9821 opj_tcp_v2_t *l_tcp = 00;
9822 opj_tccp_t *l_tccp = 00;
9825 assert(p_j2k != 00);
9826 assert(p_header_size != 00);
9827 assert(p_manager != 00);
9828 assert(p_data != 00);
9830 l_cp = &(p_j2k->m_cp);
9831 l_tcp = &l_cp->tcps[p_tile_no];
9832 l_tccp = &l_tcp->tccps[p_comp_no];
9834 /* preconditions again */
9835 assert(p_tile_no < (l_cp->tw * l_cp->th));
9836 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9838 if (*p_header_size < 5) {
9839 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9843 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9846 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9849 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9852 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9855 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9858 *p_header_size = *p_header_size - 5;
9860 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9862 if (*p_header_size < l_tccp->numresolutions) {
9863 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9867 for (i = 0; i < l_tccp->numresolutions; ++i) {
9868 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9872 *p_header_size = *p_header_size - l_tccp->numresolutions;
9879 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9880 * @param p_header_data the data contained in the COM box.
9881 * @param p_j2k the jpeg2000 codec.
9882 * @param p_header_size the size of the data contained in the COM marker.
9883 * @param p_manager the user event manager.
9885 opj_bool j2k_read_SPCod_SPCoc(
9886 opj_j2k_v2_t *p_j2k,
9888 OPJ_BYTE * p_header_data,
9889 OPJ_UINT32 * p_header_size,
9890 struct opj_event_mgr * p_manager
9893 OPJ_UINT32 i, l_tmp;
9894 opj_cp_v2_t *l_cp = NULL;
9895 opj_tcp_v2_t *l_tcp = NULL;
9896 opj_tccp_t *l_tccp = NULL;
9897 OPJ_BYTE * l_current_ptr = NULL;
9900 assert(p_j2k != 00);
9901 assert(p_manager != 00);
9902 assert(p_header_data != 00);
9904 l_cp = &(p_j2k->m_cp);
9905 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9906 &l_cp->tcps[p_j2k->m_current_tile_number] :
9907 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9909 /* precondition again */
9910 assert(compno < p_j2k->m_private_image->numcomps);
9912 l_tccp = &l_tcp->tccps[compno];
9913 l_current_ptr = p_header_data;
9915 /* make sure room is sufficient */
9916 if (*p_header_size < 5) {
9917 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9921 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
9922 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
9925 /* If user wants to remove more resolutions than the codestream contains, return error */
9926 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9927 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
9928 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
9929 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9933 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
9937 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
9941 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
9944 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
9947 *p_header_size = *p_header_size - 5;
9949 /* use custom precinct size ? */
9950 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9951 if (*p_header_size < l_tccp->numresolutions) {
9952 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9956 for (i = 0; i < l_tccp->numresolutions; ++i) {
9957 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
9959 l_tccp->prcw[i] = l_tmp & 0xf;
9960 l_tccp->prch[i] = l_tmp >> 4;
9963 *p_header_size = *p_header_size - l_tccp->numresolutions;
9966 /* set default size for the precinct width and height */
9967 for (i = 0; i < l_tccp->numresolutions; ++i) {
9968 l_tccp->prcw[i] = 15;
9969 l_tccp->prch[i] = 15;
9973 #ifdef WIP_REMOVE_MSD
9975 if (p_j2k->cstr_info && compno == 0) {
9976 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9978 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
9979 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
9980 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
9981 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
9982 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
9985 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
9986 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
9995 * Copies the tile component parameters of all the component from the first tile component.
9997 * @param p_j2k the J2k codec.
9999 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10003 opj_cp_v2_t *l_cp = NULL;
10004 opj_tcp_v2_t *l_tcp = NULL;
10005 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10006 OPJ_UINT32 l_prc_size;
10008 /* preconditions */
10009 assert(p_j2k != 00);
10011 l_cp = &(p_j2k->m_cp);
10012 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10013 &l_cp->tcps[p_j2k->m_current_tile_number] :
10014 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10016 l_ref_tccp = &l_tcp->tccps[0];
10017 l_copied_tccp = l_ref_tccp + 1;
10018 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10020 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10021 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10022 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10023 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10024 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10025 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10026 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10027 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10033 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10035 * @param p_tile_no the tile index.
10036 * @param p_comp_no the component being outputted.
10037 * @param p_j2k the J2K codec.
10039 * @return the number of bytes taken by the SPCod element.
10041 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10042 OPJ_UINT32 p_tile_no,
10043 OPJ_UINT32 p_comp_no )
10045 OPJ_UINT32 l_num_bands;
10047 opj_cp_v2_t *l_cp = 00;
10048 opj_tcp_v2_t *l_tcp = 00;
10049 opj_tccp_t *l_tccp = 00;
10051 /* preconditions */
10052 assert(p_j2k != 00);
10054 l_cp = &(p_j2k->m_cp);
10055 l_tcp = &l_cp->tcps[p_tile_no];
10056 l_tccp = &l_tcp->tccps[p_comp_no];
10058 /* preconditions again */
10059 assert(p_tile_no < l_cp->tw * l_cp->th);
10060 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10062 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10064 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10065 return 1 + l_num_bands;
10068 return 1 + 2*l_num_bands;
10073 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10075 * @param p_tile_no the tile to output.
10076 * @param p_comp_no the component number to output.
10077 * @param p_data the data buffer.
10078 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10079 * @param p_j2k J2K codec.
10080 * @param p_manager the user event manager.
10083 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10084 OPJ_UINT32 p_tile_no,
10085 OPJ_UINT32 p_comp_no,
10087 OPJ_UINT32 * p_header_size,
10088 struct opj_event_mgr * p_manager )
10090 OPJ_UINT32 l_header_size;
10091 OPJ_UINT32 l_band_no, l_num_bands;
10092 OPJ_UINT32 l_expn,l_mant;
10094 opj_cp_v2_t *l_cp = 00;
10095 opj_tcp_v2_t *l_tcp = 00;
10096 opj_tccp_t *l_tccp = 00;
10098 /* preconditions */
10099 assert(p_j2k != 00);
10100 assert(p_header_size != 00);
10101 assert(p_manager != 00);
10102 assert(p_data != 00);
10104 l_cp = &(p_j2k->m_cp);
10105 l_tcp = &l_cp->tcps[p_tile_no];
10106 l_tccp = &l_tcp->tccps[p_comp_no];
10108 /* preconditions again */
10109 assert(p_tile_no < l_cp->tw * l_cp->th);
10110 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10112 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10114 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10115 l_header_size = 1 + l_num_bands;
10117 if (*p_header_size < l_header_size) {
10118 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10122 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10125 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10126 l_expn = l_tccp->stepsizes[l_band_no].expn;
10127 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10132 l_header_size = 1 + 2*l_num_bands;
10134 if (*p_header_size < l_header_size) {
10135 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10139 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10142 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10143 l_expn = l_tccp->stepsizes[l_band_no].expn;
10144 l_mant = l_tccp->stepsizes[l_band_no].mant;
10146 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10151 *p_header_size = *p_header_size - l_header_size;
10157 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10159 * @param p_comp_no the component being targeted.
10160 * @param p_header_data the data contained in the COM box.
10161 * @param p_j2k the jpeg2000 codec.
10162 * @param p_header_size the size of the data contained in the COM marker.
10163 * @param p_manager the user event manager.
10165 opj_bool j2k_read_SQcd_SQcc(
10166 opj_j2k_v2_t *p_j2k,
10167 OPJ_UINT32 p_comp_no,
10168 OPJ_BYTE* p_header_data,
10169 OPJ_UINT32 * p_header_size,
10170 struct opj_event_mgr * p_manager
10174 OPJ_UINT32 l_band_no;
10175 opj_cp_v2_t *l_cp = 00;
10176 opj_tcp_v2_t *l_tcp = 00;
10177 opj_tccp_t *l_tccp = 00;
10178 OPJ_BYTE * l_current_ptr = 00;
10179 OPJ_UINT32 l_tmp, l_num_band;
10182 assert(p_j2k != 00);
10183 assert(p_manager != 00);
10184 assert(p_header_data != 00);
10186 l_cp = &(p_j2k->m_cp);
10187 /* come from tile part header or main header ?*/
10188 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10189 &l_cp->tcps[p_j2k->m_current_tile_number] :
10190 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10192 /* precondition again*/
10193 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10195 l_tccp = &l_tcp->tccps[p_comp_no];
10196 l_current_ptr = p_header_data;
10198 if (*p_header_size < 1) {
10199 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10202 *p_header_size -= 1;
10204 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10207 l_tccp->qntsty = l_tmp & 0x1f;
10208 l_tccp->numgbits = l_tmp >> 5;
10209 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10213 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10215 (*p_header_size) / 2;
10217 if( l_num_band > J2K_MAXBANDS ) {
10218 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10219 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10220 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10221 /*return OPJ_FALSE;*/
10226 if (l_cp->correct) {
10228 /* if JPWL is on, we check whether there are too many subbands */
10229 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10230 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10231 "JPWL: bad number of subbands in Sqcx (%d)\n",
10233 if (!JPWL_ASSUME) {
10234 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10237 /* we try to correct */
10239 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10240 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10245 #endif /* USE_JPWL */
10247 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10248 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10249 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10251 if (l_band_no < J2K_MAXBANDS){
10252 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10253 l_tccp->stepsizes[l_band_no].mant = 0;
10256 *p_header_size = *p_header_size - l_num_band;
10259 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10260 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10262 if (l_band_no < J2K_MAXBANDS){
10263 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10264 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10267 *p_header_size = *p_header_size - 2*l_num_band;
10270 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10271 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10272 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10273 l_tccp->stepsizes[l_band_no].expn =
10274 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10275 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10276 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10284 * Copies the tile component parameters of all the component from the first tile component.
10286 * @param p_j2k the J2k codec.
10288 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10291 opj_cp_v2_t *l_cp = NULL;
10292 opj_tcp_v2_t *l_tcp = NULL;
10293 opj_tccp_t *l_ref_tccp = NULL;
10294 opj_tccp_t *l_copied_tccp = NULL;
10297 /* preconditions */
10298 assert(p_j2k != 00);
10300 l_cp = &(p_j2k->m_cp);
10301 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10302 &l_cp->tcps[p_j2k->m_current_tile_number] :
10303 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10305 l_ref_tccp = &l_tcp->tccps[0];
10306 l_copied_tccp = l_ref_tccp + 1;
10307 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10309 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10310 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10311 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10312 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10318 * Dump some elements from the J2K decompression structure .
10320 *@param p_j2k the jpeg2000 codec.
10321 *@param flag flag to describe what elments are dump.
10322 *@param out_stream output stream where dump the elements.
10325 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10327 /* Check if the flag is compatible with j2k file*/
10328 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10329 fprintf(out_stream, "Wrong flag\n");
10333 /* Dump the image_header */
10334 if (flag & OPJ_IMG_INFO){
10335 if (p_j2k->m_private_image)
10336 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10339 /* Dump the codestream info from main header */
10340 if (flag & OPJ_J2K_MH_INFO){
10341 j2k_dump_MH_info(p_j2k, out_stream);
10345 /* Dump the codestream info of the current tile */
10346 if (flag & OPJ_J2K_TH_INFO){
10350 /* Dump the codestream index from main header */
10351 if (flag & OPJ_J2K_MH_IND){
10352 j2k_dump_MH_index(p_j2k, out_stream);
10355 /* Dump the codestream index of the current tile */
10356 if (flag & OPJ_J2K_TH_IND){
10363 * Dump index elements of the codestream extract from the main header.
10365 *@param p_j2k the jpeg2000 codec.
10366 *@param out_stream output stream where dump the elements.
10369 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10371 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10372 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10374 fprintf(out_stream, "Codestream index from main header: {\n");
10376 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10377 "\t Main header end position=%" PRIi64 "\n",
10378 cstr_index->main_head_start, cstr_index->main_head_end);
10380 fprintf(out_stream, "\t Marker list: {\n");
10382 if (cstr_index->marker){
10383 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10384 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10385 cstr_index->marker[it_marker].type,
10386 cstr_index->marker[it_marker].pos,
10387 cstr_index->marker[it_marker].len );
10391 fprintf(out_stream, "\t }\n");
10394 if (cstr_index->tile_index){
10395 fprintf(out_stream, "\t Tile index: {\n");
10397 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10398 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10400 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10402 if (cstr_index->tile_index[it_tile].tp_index){
10403 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10404 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10406 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10407 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10408 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10412 if (cstr_index->tile_index[it_tile].marker){
10413 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10414 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10415 cstr_index->tile_index[it_tile].marker[it_marker].type,
10416 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10417 cstr_index->tile_index[it_tile].marker[it_marker].len );
10421 fprintf(out_stream,"\t }\n");
10424 fprintf(out_stream,"}\n");
10429 * Dump info elements of the codestream extract from the main header.
10431 *@param p_j2k the jpeg2000 codec.
10432 *@param out_stream output stream where dump the elements.
10435 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10437 opj_tcp_v2_t * l_default_tile=NULL;
10439 fprintf(out_stream, "Codestream info from main header: {\n");
10441 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10442 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10443 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10445 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10446 if (l_default_tile)
10449 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10451 fprintf(out_stream, "\t default tile {\n");
10452 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10453 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10454 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10455 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10457 for (compno = 0; compno < numcomps; compno++) {
10458 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10460 OPJ_INT32 bandno, numbands;
10463 fprintf(out_stream, "\t\t comp %d {\n", compno);
10464 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10465 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10466 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10467 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10468 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10469 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10471 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10472 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10473 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10475 fprintf(out_stream, "\n");
10477 /* quantization style*/
10478 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10479 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10480 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10481 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10482 for (bandno = 0; bandno < numbands; bandno++) {
10483 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10484 l_tccp->stepsizes[bandno].expn);
10486 fprintf(out_stream, "\n");
10489 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10491 fprintf(out_stream, "\t\t }\n");
10492 } /*end of component of default tile*/
10493 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10497 fprintf(out_stream, "}\n");
10502 * Dump an image header structure.
10504 *@param img_header the image header to dump.
10505 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10506 *@param out_stream output stream where dump the elements.
10508 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10512 if (dev_dump_flag){
10513 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10517 fprintf(out_stream, "Image info {\n");
10518 tab[0] = '\t';tab[1] = '\0';
10521 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10522 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10523 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10525 if (img_header->comps){
10527 for (compno = 0; compno < img_header->numcomps; compno++) {
10528 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10529 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10530 fprintf(out_stream,"%s}\n",tab);
10534 fprintf(out_stream, "}\n");
10538 * Dump a component image header structure.
10540 *@param comp_header the component image header to dump.
10541 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10542 *@param out_stream output stream where dump the elements.
10544 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10548 if (dev_dump_flag){
10549 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10552 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10555 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10556 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10557 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10560 fprintf(out_stream, "}\n");
10565 * Get the codestream info from a JPEG2000 codec.
10567 *@param p_j2k the component image header to dump.
10569 *@return the codestream information extract from the jpg2000 codec
10571 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10574 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10575 opj_tcp_v2_t *l_default_tile;
10576 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10578 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10580 cstr_info->tx0 = p_j2k->m_cp.tx0;
10581 cstr_info->ty0 = p_j2k->m_cp.ty0;
10582 cstr_info->tdx = p_j2k->m_cp.tdx;
10583 cstr_info->tdy = p_j2k->m_cp.tdy;
10584 cstr_info->tw = p_j2k->m_cp.tw;
10585 cstr_info->th = p_j2k->m_cp.th;
10587 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10589 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10591 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10592 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10593 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10594 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10596 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10598 for (compno = 0; compno < numcomps; compno++) {
10599 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10600 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10601 OPJ_INT32 bandno, numbands;
10604 l_tccp_info->csty = l_tccp->csty;
10605 l_tccp_info->numresolutions = l_tccp->numresolutions;
10606 l_tccp_info->cblkw = l_tccp->cblkw;
10607 l_tccp_info->cblkh = l_tccp->cblkh;
10608 l_tccp_info->cblksty = l_tccp->cblksty;
10609 l_tccp_info->qmfbid = l_tccp->qmfbid;
10610 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10612 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10613 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10616 /* quantization style*/
10617 l_tccp_info->qntsty = l_tccp->qntsty;
10618 l_tccp_info->numgbits = l_tccp->numgbits;
10620 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10621 if (numbands < J2K_MAXBANDS) {
10622 for (bandno = 0; bandno < numbands; bandno++) {
10623 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10624 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10629 l_tccp_info->roishift = l_tccp->roishift;
10637 * Get the codestream index from a JPEG2000 codec.
10639 *@param p_j2k the component image header to dump.
10641 *@return the codestream index extract from the jpg2000 codec
10643 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10645 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10646 opj_calloc(1,sizeof(opj_codestream_index_t));
10650 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10651 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10652 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10654 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10655 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10656 if (!l_cstr_index->marker){
10657 opj_free( l_cstr_index);
10661 if (p_j2k->cstr_index->marker)
10662 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10664 opj_free(l_cstr_index->marker);
10665 l_cstr_index->marker = NULL;
10668 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10669 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10670 if (!l_cstr_index->tile_index){
10671 opj_free( l_cstr_index->marker);
10672 opj_free( l_cstr_index);
10676 if (!p_j2k->cstr_index->tile_index){
10677 opj_free(l_cstr_index->tile_index);
10678 l_cstr_index->tile_index = NULL;
10681 OPJ_UINT32 it_tile = 0;
10682 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10685 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10687 l_cstr_index->tile_index[it_tile].marker =
10688 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10690 if (!l_cstr_index->tile_index[it_tile].marker) {
10691 OPJ_UINT32 it_tile_free;
10693 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10694 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10697 opj_free( l_cstr_index->tile_index);
10698 opj_free( l_cstr_index->marker);
10699 opj_free( l_cstr_index);
10703 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10704 memcpy( l_cstr_index->tile_index[it_tile].marker,
10705 p_j2k->cstr_index->tile_index[it_tile].marker,
10706 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10708 opj_free(l_cstr_index->tile_index[it_tile].marker);
10709 l_cstr_index->tile_index[it_tile].marker = NULL;
10712 /* Tile part index*/
10713 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10715 l_cstr_index->tile_index[it_tile].tp_index =
10716 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10718 if(!l_cstr_index->tile_index[it_tile].tp_index){
10719 OPJ_UINT32 it_tile_free;
10721 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10722 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10723 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10726 opj_free( l_cstr_index->tile_index);
10727 opj_free( l_cstr_index->marker);
10728 opj_free( l_cstr_index);
10732 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10733 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10734 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10735 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10738 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10739 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10742 /* Packet index (NOT USED)*/
10743 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10744 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10749 return l_cstr_index;
10752 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10754 OPJ_UINT32 it_tile=0;
10756 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10757 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10758 if (!p_j2k->cstr_index->tile_index)
10761 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10762 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10763 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10764 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10765 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10766 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10776 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10777 opj_stream_private_t *p_stream,
10778 opj_event_mgr_t * p_manager)
10780 opj_bool l_go_on = OPJ_TRUE;
10781 OPJ_UINT32 l_current_tile_no;
10782 OPJ_UINT32 l_data_size,l_max_data_size;
10783 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10784 OPJ_UINT32 l_nb_comps;
10785 OPJ_BYTE * l_current_data;
10787 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10788 if (! l_current_data) {
10791 l_max_data_size = 1000;
10793 /*Allocate and initialize some elements of codestrem index*/
10794 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
10795 opj_free(l_current_data);
10800 if (! j2k_read_tile_header( p_j2k,
10801 &l_current_tile_no,
10803 &l_tile_x0, &l_tile_y0,
10804 &l_tile_x1, &l_tile_y1,
10809 opj_free(l_current_data);
10817 if (l_data_size > l_max_data_size) {
10818 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10819 if (! l_current_data) {
10820 opj_free(l_current_data);
10824 l_max_data_size = l_data_size;
10827 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10828 opj_free(l_current_data);
10831 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);
10833 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10834 opj_free(l_current_data);
10837 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10841 opj_free(l_current_data);
10847 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10849 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10852 assert(p_j2k != 00);
10854 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
10855 /* DEVELOPER CORNER, add your custom procedures */
10860 * Read and decode one tile.
10862 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10863 opj_stream_private_t *p_stream,
10864 opj_event_mgr_t * p_manager)
10866 opj_bool l_go_on = OPJ_TRUE;
10867 OPJ_UINT32 l_current_tile_no;
10868 OPJ_UINT32 l_tile_no_to_dec;
10869 OPJ_UINT32 l_data_size,l_max_data_size;
10870 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10871 OPJ_UINT32 l_nb_comps;
10872 OPJ_BYTE * l_current_data;
10874 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10875 if (! l_current_data) {
10878 l_max_data_size = 1000;
10880 /*Allocate and initialize some elements of codestrem index if not already done*/
10881 if( !p_j2k->cstr_index->tile_index)
10883 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
10884 opj_free(l_current_data);
10888 /* Move into the codestream to the first SOT used to decode the desired tile */
10889 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10890 if (p_j2k->cstr_index->tile_index)
10891 if(p_j2k->cstr_index->tile_index->tp_index)
10893 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10894 /* the index for this tile has not been built,
10895 * so move to the last SOT read */
10896 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
10897 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10902 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)) {
10903 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10907 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10908 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
10909 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10913 if (! j2k_read_tile_header( p_j2k,
10914 &l_current_tile_no,
10916 &l_tile_x0, &l_tile_y0,
10917 &l_tile_x1, &l_tile_y1,
10922 opj_free(l_current_data);
10931 if (l_data_size > l_max_data_size) {
10932 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10933 if (! l_current_data) {
10934 opj_free(l_current_data);
10938 l_max_data_size = l_data_size;
10943 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10944 opj_free(l_current_data);
10947 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);
10949 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10950 opj_free(l_current_data);
10953 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
10955 if(l_current_tile_no == l_tile_no_to_dec)
10957 /* move into the codestream to the the first SOT (FIXME or not move?)*/
10958 if (opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) {
10959 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10965 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);
10970 opj_free(l_current_data);
10977 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10979 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
10982 assert(p_j2k != 00);
10984 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
10985 /* DEVELOPER CORNER, add your custom procedures */
10991 * Decodes the tiles of the stream.
10993 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
10994 opj_stream_private_t * p_stream,
10995 opj_image_t * p_image,
10996 opj_event_mgr_t * p_manager)
11003 p_j2k->m_output_image = opj_image_create0();
11004 if (! (p_j2k->m_output_image)) {
11007 opj_copy_image_header(p_image, p_j2k->m_output_image);
11009 /* customization of the decoding */
11010 j2k_setup_decoding(p_j2k);
11012 /* Decode the codestream */
11013 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11014 opj_image_destroy(p_j2k->m_private_image);
11015 p_j2k->m_private_image = NULL;
11019 /* Move data and copy one information from codec to output image*/
11020 for (compno = 0; compno < p_image->numcomps; compno++) {
11021 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11022 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11023 p_j2k->m_output_image->comps[compno].data = NULL;
11031 * Get the decoded tile.
11033 * @param p_j2k the jpeg2000 codestream codec.
11034 * @param p_stream input_stream
11035 * @param p_image output image. .
11036 * @param p_manager the user event manager
11037 * @param tile_index index of the tile we want decode
11039 * @return true if succeed.
11041 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11042 opj_stream_private_t *p_stream,
11043 opj_image_t* p_image,
11044 struct opj_event_mgr * p_manager,
11045 OPJ_UINT32 tile_index )
11048 OPJ_UINT32 l_tile_x, l_tile_y;
11049 opj_image_comp_t* l_img_comp;
11052 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11056 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11057 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);
11061 /* Compute the dimension of the desired tile*/
11062 l_tile_x = tile_index % p_j2k->m_cp.tw;
11063 l_tile_y = tile_index / p_j2k->m_cp.tw;
11065 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11066 if (p_image->x0 < p_j2k->m_private_image->x0)
11067 p_image->x0 = p_j2k->m_private_image->x0;
11068 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11069 if (p_image->x1 > p_j2k->m_private_image->x1)
11070 p_image->x1 = p_j2k->m_private_image->x1;
11072 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11073 if (p_image->y0 < p_j2k->m_private_image->y0)
11074 p_image->y0 = p_j2k->m_private_image->y0;
11075 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11076 if (p_image->y1 > p_j2k->m_private_image->y1)
11077 p_image->y1 = p_j2k->m_private_image->y1;
11079 l_img_comp = p_image->comps;
11080 for (compno=0; compno < p_image->numcomps; ++compno)
11082 OPJ_INT32 l_comp_x1, l_comp_y1;
11084 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11086 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11087 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11088 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11089 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11091 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11092 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11097 /* Destroy the previous output image*/
11098 if (p_j2k->m_output_image)
11099 opj_image_destroy(p_j2k->m_output_image);
11101 /* Create the ouput image from the information previously computed*/
11102 p_j2k->m_output_image = opj_image_create0();
11103 if (! (p_j2k->m_output_image)) {
11106 opj_copy_image_header(p_image, p_j2k->m_output_image);
11108 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11110 /* customization of the decoding */
11111 j2k_setup_decoding_tile(p_j2k);
11113 /* Decode the codestream */
11114 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11115 opj_image_destroy(p_j2k->m_private_image);
11116 p_j2k->m_private_image = NULL;
11120 /* Move data and copy one information from codec to output image*/
11121 for (compno = 0; compno < p_image->numcomps; compno++) {
11122 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11124 if (p_image->comps[compno].data)
11125 opj_free(p_image->comps[compno].data);
11127 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11129 p_j2k->m_output_image->comps[compno].data = NULL;
11135 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11137 OPJ_UINT32 it_comp;
11139 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11141 if (p_j2k->m_private_image) {
11142 if (p_j2k->m_private_image->comps) {
11143 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11144 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11145 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11146 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11147 if ( res_factor >= max_res){
11148 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11151 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11164 * Encodes all the tiles in a row.
11166 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11167 opj_stream_private_t *p_stream,
11168 opj_event_mgr_t * p_manager )
11171 OPJ_UINT32 l_nb_tiles;
11172 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11173 OPJ_BYTE * l_current_data;
11175 /* preconditions */
11176 assert(p_j2k != 00);
11177 assert(p_stream != 00);
11178 assert(p_manager != 00);
11180 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11181 if (! l_current_data) {
11184 l_max_tile_size = 1000;
11186 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11187 for (i=0;i<l_nb_tiles;++i) {
11188 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11189 opj_free(l_current_data);
11193 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11194 if (l_current_tile_size > l_max_tile_size) {
11195 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11196 if (! l_current_data) {
11199 l_max_tile_size = l_current_tile_size;
11202 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11204 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11209 opj_free(l_current_data);
11214 * Ends the compression procedures and possibility add data to be read after the
11217 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11218 opj_stream_private_t *p_stream,
11219 struct opj_event_mgr * p_manager)
11221 /* customization of the encoding */
11222 j2k_setup_end_compress(p_j2k);
11224 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11234 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11236 * @param p_j2k the jpeg2000 codec.
11237 * @param p_stream the stream object.
11238 * @param p_manager the user event manager.
11240 * @return true if the codec is valid.
11242 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11243 opj_stream_private_t *p_stream,
11244 opj_image_t * p_image,
11245 opj_event_mgr_t * p_manager)
11247 /* preconditions */
11248 assert(p_j2k != 00);
11249 assert(p_stream != 00);
11250 assert(p_manager != 00);
11252 p_j2k->m_private_image = p_image;
11254 /* customization of the validation */
11255 j2k_setup_encoding_validation (p_j2k);
11257 /* validation of the parameters codec */
11258 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11262 /* customization of the encoding */
11263 j2k_setup_header_writting(p_j2k);
11266 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11276 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11277 OPJ_UINT32 p_tile_index,
11278 opj_stream_private_t *p_stream,
11279 opj_event_mgr_t * p_manager )
11282 if (p_tile_index != p_j2k->m_current_tile_number) {
11283 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11287 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);
11289 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11290 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11291 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11293 /* initialisation before tile encoding */
11294 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11304 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11306 OPJ_UINT32 i,j,k = 0;
11307 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11308 opj_image_comp_t * l_img_comp = 00;
11309 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11310 opj_image_t * l_image = 00;
11311 OPJ_UINT32 l_size_comp, l_remaining;
11312 OPJ_INT32 * l_src_ptr;
11313 l_tilec = p_tcd->tcd_image->tiles->comps;
11314 l_image = p_tcd->image;
11315 l_img_comp = l_image->comps;
11317 for (i=0;i<p_tcd->image->numcomps;++i) {
11318 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11319 l_remaining = l_img_comp->prec & 7; /* (%8) */
11324 if (l_size_comp == 3) {
11328 l_width = (l_tilec->x1 - l_tilec->x0);
11329 l_height = (l_tilec->y1 - l_tilec->y0);
11330 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11331 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11332 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11333 l_stride = l_image_width - l_width;
11334 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11336 switch (l_size_comp) {
11339 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11340 if (l_img_comp->sgnd) {
11341 for (j=0;j<l_height;++j) {
11342 for (k=0;k<l_width;++k) {
11343 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11347 l_src_ptr += l_stride;
11351 for (j=0;j<l_height;++j) {
11352 for (k=0;k<l_width;++k) {
11353 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11357 l_src_ptr += l_stride;
11361 p_data = (OPJ_BYTE*) l_dest_ptr;
11366 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11367 if (l_img_comp->sgnd) {
11368 for (j=0;j<l_height;++j) {
11369 for (k=0;k<l_width;++k) {
11370 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11372 l_src_ptr += l_stride;
11376 for (j=0;j<l_height;++j) {
11377 for (k=0;k<l_width;++k) {
11378 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11380 l_src_ptr += l_stride;
11384 p_data = (OPJ_BYTE*) l_dest_ptr;
11389 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11390 for (j=0;j<l_height;++j) {
11391 for (k=0;k<l_width;++k) {
11392 *(l_dest_ptr++) = *(l_src_ptr++);
11394 l_src_ptr += l_stride;
11397 p_data = (OPJ_BYTE*) l_dest_ptr;
11410 * @param p_j2k the jpeg2000 codec.
11411 * @param p_stream the stream to write data to.
11412 * @param p_manager the user event manager.
11414 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11416 OPJ_UINT32 p_data_size,
11417 opj_stream_private_t *p_stream,
11418 opj_event_mgr_t * p_manager )
11420 opj_tcd_v2_t * l_tcd = 00;
11421 opj_cp_v2_t * l_cp = 00;
11422 opj_tcp_v2_t * l_tcp = 00;
11423 OPJ_UINT32 l_nb_bytes_written;
11424 OPJ_BYTE * l_current_data = 00;
11425 OPJ_UINT32 l_tile_size = 0;
11426 OPJ_UINT32 l_available_data;
11428 /* preconditions */
11429 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11431 l_tcd = p_j2k->m_tcd;
11432 l_cp = &(p_j2k->m_cp);
11433 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11435 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11436 l_available_data = l_tile_size;
11437 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11439 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11440 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismtach between tile data and sent data." );
11444 l_nb_bytes_written = 0;
11445 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11448 l_current_data += l_nb_bytes_written;
11449 l_available_data -= l_nb_bytes_written;
11451 l_nb_bytes_written = 0;
11452 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11456 l_available_data -= l_nb_bytes_written;
11457 l_nb_bytes_written = l_tile_size - l_available_data;
11459 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11463 ++p_j2k->m_current_tile_number;
11470 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11471 * are valid. Developers wanting to extend the library can add their own validation procedures.
11473 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11475 /* preconditions */
11476 assert(p_j2k != 00);
11478 /* DEVELOPER CORNER, insert your custom procedures */
11479 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc );
11481 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11482 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11485 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11486 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11487 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11491 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11492 * are valid. Developers wanting to extend the library can add their own validation procedures.
11494 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11496 /* preconditions */
11497 assert(p_j2k != 00);
11499 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11500 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11502 /* DEVELOPER CORNER, add your custom validation procedure */
11503 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11508 * Sets up the procedures to do on writing header.
11509 * Developers wanting to extend the library can add their own writing procedures.
11511 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11513 /* preconditions */
11514 assert(p_j2k != 00);
11516 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11517 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11518 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11519 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11520 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11523 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11524 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11525 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm );
11527 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11528 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc );
11532 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11534 if (p_j2k->m_cp.comment != 00) {
11535 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com);
11538 /* DEVELOPER CORNER, insert your custom procedures */
11539 if (p_j2k->m_cp.rsiz & MCT) {
11540 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11542 /* End of Developer Corner */
11544 if (p_j2k->cstr_index) {
11545 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11548 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11549 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11553 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11555 OPJ_UINT32 * p_data_written,
11556 OPJ_UINT32 p_total_data_size,
11557 opj_stream_private_t *p_stream,
11558 struct opj_event_mgr * p_manager )
11561 OPJ_UINT32 l_nb_bytes_written = 0;
11562 OPJ_UINT32 l_current_nb_bytes_written;
11563 OPJ_BYTE * l_begin_data = 00;
11565 opj_tcp_v2_t *l_tcp = 00;
11566 opj_tcd_v2_t * l_tcd = 00;
11567 opj_cp_v2_t * l_cp = 00;
11569 l_tcd = p_j2k->m_tcd;
11570 l_cp = &(p_j2k->m_cp);
11571 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11573 l_tcd->cur_pino = 0;
11575 /*Get number of tile parts*/
11576 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11581 l_current_nb_bytes_written = 0;
11582 l_begin_data = p_data;
11583 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11588 l_nb_bytes_written += l_current_nb_bytes_written;
11589 p_data += l_current_nb_bytes_written;
11590 p_total_data_size -= l_current_nb_bytes_written;
11592 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11593 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11594 l_current_nb_bytes_written = 0;
11595 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11596 l_nb_bytes_written += l_current_nb_bytes_written;
11597 p_data += l_current_nb_bytes_written;
11598 p_total_data_size -= l_current_nb_bytes_written;
11600 l_current_nb_bytes_written = 0;
11601 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11602 l_nb_bytes_written += l_current_nb_bytes_written;
11603 p_data += l_current_nb_bytes_written;
11604 p_total_data_size -= l_current_nb_bytes_written;
11607 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11608 l_current_nb_bytes_written = 0;
11609 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11610 l_nb_bytes_written += l_current_nb_bytes_written;
11611 p_data += l_current_nb_bytes_written;
11612 p_total_data_size -= l_current_nb_bytes_written;
11616 l_current_nb_bytes_written = 0;
11617 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11621 l_nb_bytes_written += l_current_nb_bytes_written;
11622 * p_data_written = l_nb_bytes_written;
11624 /* Writing Psot in SOT marker */
11625 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11627 if (l_cp->m_specific_param.m_enc.m_cinema){
11628 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11634 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11636 OPJ_UINT32 * p_data_written,
11637 OPJ_UINT32 p_total_data_size,
11638 opj_stream_private_t *p_stream,
11639 struct opj_event_mgr * p_manager
11642 OPJ_UINT32 tilepartno=0;
11643 OPJ_UINT32 l_nb_bytes_written = 0;
11644 OPJ_UINT32 l_current_nb_bytes_written;
11645 OPJ_UINT32 l_part_tile_size;
11646 OPJ_UINT32 tot_num_tp;
11649 OPJ_BYTE * l_begin_data;
11650 opj_tcp_v2_t *l_tcp = 00;
11651 opj_tcd_v2_t * l_tcd = 00;
11652 opj_cp_v2_t * l_cp = 00;
11655 l_tcd = p_j2k->m_tcd;
11656 l_cp = &(p_j2k->m_cp);
11657 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11659 /*Get number of tile parts*/
11660 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11662 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11663 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11664 l_current_nb_bytes_written = 0;
11665 l_part_tile_size = 0;
11666 l_begin_data = p_data;
11668 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11672 l_nb_bytes_written += l_current_nb_bytes_written;
11673 p_data += l_current_nb_bytes_written;
11674 p_total_data_size -= l_current_nb_bytes_written;
11675 l_part_tile_size += l_nb_bytes_written;
11677 l_current_nb_bytes_written = 0;
11678 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11682 p_data += l_current_nb_bytes_written;
11683 l_nb_bytes_written += l_current_nb_bytes_written;
11684 p_total_data_size -= l_current_nb_bytes_written;
11685 l_part_tile_size += l_nb_bytes_written;
11687 /* Writing Psot in SOT marker */
11688 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11690 if (l_cp->m_specific_param.m_enc.m_cinema) {
11691 j2k_update_tlm(p_j2k,l_part_tile_size);
11694 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11697 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11698 l_tcd->cur_pino = pino;
11700 /*Get number of tile parts*/
11701 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11702 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11703 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11704 l_current_nb_bytes_written = 0;
11705 l_part_tile_size = 0;
11706 l_begin_data = p_data;
11708 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11712 l_nb_bytes_written += l_current_nb_bytes_written;
11713 p_data += l_current_nb_bytes_written;
11714 p_total_data_size -= l_current_nb_bytes_written;
11715 l_part_tile_size += l_current_nb_bytes_written;
11717 l_current_nb_bytes_written = 0;
11719 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11723 l_nb_bytes_written += l_current_nb_bytes_written;
11724 p_data += l_current_nb_bytes_written;
11725 p_total_data_size -= l_current_nb_bytes_written;
11726 l_part_tile_size += l_current_nb_bytes_written;
11728 /* Writing Psot in SOT marker */
11729 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11731 if (l_cp->m_specific_param.m_enc.m_cinema) {
11732 j2k_update_tlm(p_j2k,l_part_tile_size);
11735 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11739 *p_data_written = l_nb_bytes_written;
11745 * Writes the updated tlm.
11747 * @param p_stream the stream to write data to.
11748 * @param p_j2k J2K codec.
11749 * @param p_manager the user event manager.
11751 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11752 struct opj_stream_private *p_stream,
11753 struct opj_event_mgr * p_manager )
11755 OPJ_UINT32 l_tlm_size;
11756 OPJ_SIZE_T l_tlm_position, l_current_position;
11758 /* preconditions */
11759 assert(p_j2k != 00);
11760 assert(p_manager != 00);
11761 assert(p_stream != 00);
11763 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11764 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11765 l_current_position = opj_stream_tell(p_stream);
11767 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11771 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) {
11775 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11784 * Ends the encoding, i.e. frees memory.
11786 * @param p_stream the stream to write data to.
11787 * @param p_j2k J2K codec.
11788 * @param p_manager the user event manager.
11790 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11791 struct opj_stream_private *p_stream,
11792 struct opj_event_mgr * p_manager )
11794 /* preconditions */
11795 assert(p_j2k != 00);
11796 assert(p_manager != 00);
11797 assert(p_stream != 00);
11799 tcd_destroy_v2(p_j2k->m_tcd);
11802 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11803 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11804 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11805 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11808 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11809 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11810 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11813 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11819 * Destroys the memory associated with the decoding of headers.
11821 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
11822 opj_stream_private_t *p_stream,
11823 opj_event_mgr_t * p_manager )
11825 /* preconditions */
11826 assert(p_j2k != 00);
11827 assert(p_stream != 00);
11828 assert(p_manager != 00);
11830 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11831 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11832 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11835 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11844 * @param p_stream the stream to write data to.
11845 * @param p_j2k J2K codec.
11846 * @param p_manager the user event manager.
11848 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11849 struct opj_stream_private *p_stream,
11850 struct opj_event_mgr * p_manager )
11852 opj_codestream_info_t * l_cstr_info = 00;
11854 /* preconditions */
11855 assert(p_j2k != 00);
11856 assert(p_manager != 00);
11857 assert(p_stream != 00);
11860 /* TODO mergeV2: check this part which use cstr_info */
11861 /*l_cstr_info = p_j2k->cstr_info;
11865 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));
11867 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
11868 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
11870 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
11872 l_cstr_info->tw = p_j2k->m_cp.tw;
11873 l_cstr_info->th = p_j2k->m_cp.th;
11875 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
11876 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
11877 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
11878 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
11880 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
11882 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
11884 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
11886 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
11887 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
11890 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
11892 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
11894 /*l_cstr_info->maxmarknum = 100;
11895 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
11896 l_cstr_info->marknum = 0;
11899 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);
11903 * Creates a tile-coder decoder.
11905 * @param p_stream the stream to write data to.
11906 * @param p_j2k J2K codec.
11907 * @param p_manager the user event manager.
11909 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
11910 struct opj_stream_private *p_stream,
11911 struct opj_event_mgr * p_manager )
11913 /* preconditions */
11914 assert(p_j2k != 00);
11915 assert(p_manager != 00);
11916 assert(p_stream != 00);
11918 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
11920 if (! p_j2k->m_tcd) {
11921 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
11925 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
11926 tcd_destroy_v2(p_j2k->m_tcd);
11937 * @param p_j2k the jpeg2000 codec.
11938 * @param p_stream the stream to write data to.
11939 * @param p_manager the user event manager.
11941 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
11942 OPJ_UINT32 p_tile_index,
11944 OPJ_UINT32 p_data_size,
11945 opj_stream_private_t *p_stream,
11946 opj_event_mgr_t * p_manager )
11948 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
11952 return j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager);