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-2012, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
41 /** @name Local static functions */
45 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
47 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 static opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
53 opj_stream_private_t *p_stream,
54 opj_event_mgr_t * p_manager);
57 * The default encoding validation procedure without any extension.
59 * @param p_j2k the jpeg2000 codec to validate.
60 * @param p_stream the input stream to validate.
61 * @param p_manager the user event manager.
63 * @return true if the parameters are correct.
65 static opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
66 opj_stream_private_t *p_stream,
67 opj_event_mgr_t * p_manager );
70 * The default decoding validation procedure without any extension.
72 * @param p_j2k the jpeg2000 codec to validate.
73 * @param p_stream the input stream to validate.
74 * @param p_manager the user event manager.
76 * @return true if the parameters are correct.
78 static opj_bool j2k_decoding_validation ( opj_j2k_v2_t * p_j2k,
79 opj_stream_private_t *p_stream,
80 opj_event_mgr_t * p_manager );
83 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
84 * are valid. Developpers wanting to extend the library can add their own validation procedures.
86 static void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
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_decoding_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_end_compress (opj_j2k_v2_t *p_j2k);
101 * The mct encoding validation procedure.
103 * @param p_j2k the jpeg2000 codec to validate.
104 * @param p_stream the input stream to validate.
105 * @param p_manager the user event manager.
107 * @return true if the parameters are correct.
109 static opj_bool j2k_mct_validation (opj_j2k_v2_t * p_j2k,
110 opj_stream_private_t *p_stream,
111 opj_event_mgr_t * p_manager );
114 * Builds the tcd decoder to use to decode tile.
116 static opj_bool j2k_build_decoder ( opj_j2k_v2_t * p_j2k,
117 opj_stream_private_t *p_stream,
118 opj_event_mgr_t * p_manager );
120 * Builds the tcd encoder to use to encode tile.
122 static opj_bool j2k_build_encoder ( opj_j2k_v2_t * p_j2k,
123 opj_stream_private_t *p_stream,
124 opj_event_mgr_t * p_manager );
127 * Creates a tile-coder decoder.
129 * @param p_stream the stream to write data to.
130 * @param p_j2k J2K codec.
131 * @param p_manager the user event manager.
133 static opj_bool j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134 struct opj_stream_private *p_stream,
135 struct opj_event_mgr * p_manager );
138 * Excutes the given procedures on the given codec.
140 * @param p_procedure_list the list of procedures to execute
141 * @param p_j2k the jpeg2000 codec to execute the procedures on.
142 * @param p_stream the stream to execute the procedures on.
143 * @param p_manager the user manager.
145 * @return true if all the procedures were successfully executed.
147 static opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
148 opj_procedure_list_t * p_procedure_list,
149 opj_stream_private_t *p_stream,
150 opj_event_mgr_t * p_manager);
153 * Updates the rates of the tcp.
155 * @param p_stream the stream to write data to.
156 * @param p_j2k J2K codec.
157 * @param p_manager the user event manager.
159 static opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
160 opj_stream_private_t *p_stream,
161 opj_event_mgr_t * p_manager );
164 * Copies the decoding tile parameters onto all the tile parameters.
165 * Creates also the tile decoder.
167 static opj_bool j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
168 opj_stream_private_t *p_stream,
169 opj_event_mgr_t * p_manager );
172 * Destroys the memory associated with the decoding of headers.
174 static opj_bool j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
175 opj_stream_private_t *p_stream,
176 opj_event_mgr_t * p_manager );
179 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
180 * with the marker value.
181 * @param p_id Marker value to look up
183 * @return the handler associated with the id.
185 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
188 * Destroys a tile coding parameter structure.
190 * @param p_tcp the tile coding parameter to destroy.
192 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
195 * Destroys the data inside a tile coding parameter structure.
197 * @param p_tcp the tile coding parameter which contain data to destroy.
199 static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
202 * Destroys a coding parameter structure.
204 * @param p_cp the coding parameter to destroy.
206 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
210 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
212 * @param p_comp_no the component number to output.
213 * @param p_stream the stream to write data to.
214 * @param p_j2k J2K codec.
215 * @param p_manager the user event manager.
218 static opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
219 OPJ_UINT32 p_tile_no,
220 OPJ_UINT32 p_comp_no,
222 OPJ_UINT32 * p_header_size,
223 struct opj_event_mgr * p_manager );
226 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
228 * @param p_tile_no the tile index.
229 * @param p_comp_no the component being outputted.
230 * @param p_j2k the J2K codec.
232 * @return the number of bytes taken by the SPCod element.
234 static OPJ_UINT32 j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
235 OPJ_UINT32 p_tile_no,
236 OPJ_UINT32 p_comp_no );
239 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
240 * @param p_header_data the data contained in the COM box.
241 * @param p_j2k the jpeg2000 codec.
242 * @param p_header_size the size of the data contained in the COM marker.
243 * @param p_manager the user event manager.
245 static opj_bool j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
247 OPJ_BYTE * p_header_data,
248 OPJ_UINT32 * p_header_size,
249 opj_event_mgr_t * p_manager );
252 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
254 * @param p_tile_no the tile index.
255 * @param p_comp_no the component being outputted.
256 * @param p_j2k the J2K codec.
258 * @return the number of bytes taken by the SPCod element.
260 static OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
261 OPJ_UINT32 p_tile_no,
262 OPJ_UINT32 p_comp_no );
265 * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
267 * @param p_tile_no the tile to output.
268 * @param p_comp_no the component number to output.
269 * @param p_data the data buffer.
270 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
271 * @param p_j2k J2K codec.
272 * @param p_manager the user event manager.
275 static opj_bool j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
276 OPJ_UINT32 p_tile_no,
277 OPJ_UINT32 p_comp_no,
279 OPJ_UINT32 * p_header_size,
280 opj_event_mgr_t * p_manager);
283 * Updates the Tile Length Marker.
285 static void j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
288 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
290 * @param p_tile_no the tile to output.
291 * @param p_comp_no the component number to output.
292 * @param p_data the data buffer.
293 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
294 * @param p_j2k J2K codec.
295 * @param p_manager the user event manager.
298 static opj_bool j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
300 OPJ_BYTE * p_header_data,
301 OPJ_UINT32 * p_header_size,
302 opj_event_mgr_t * p_manager );
305 * Copies the tile component parameters of all the component from the first tile component.
307 * @param p_j2k the J2k codec.
309 static void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k );
312 * Copies the tile quantization parameters of all the component from the first tile component.
314 * @param p_j2k the J2k codec.
316 static void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
321 static opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
322 opj_stream_private_t *p_stream,
323 opj_event_mgr_t * p_manager);
326 static opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
327 OPJ_UINT32 p_tile_index,
328 opj_stream_private_t *p_stream,
329 opj_event_mgr_t * p_manager );
331 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
333 static void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
335 static opj_bool j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
337 OPJ_UINT32 p_data_size,
338 opj_stream_private_t *p_stream,
339 opj_event_mgr_t * p_manager );
342 * Sets up the procedures to do on writing header.
343 * Developers wanting to extend the library can add their own writing procedures.
345 static void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
347 static opj_bool j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k,
349 OPJ_UINT32 * p_data_written,
350 OPJ_UINT32 p_total_data_size,
351 opj_stream_private_t *p_stream,
352 struct opj_event_mgr * p_manager );
354 static opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
356 OPJ_UINT32 * p_data_written,
357 OPJ_UINT32 p_total_data_size,
358 opj_stream_private_t *p_stream,
359 struct opj_event_mgr * p_manager );
362 * Gets the offset of the header.
364 * @param p_stream the stream to write data to.
365 * @param p_j2k J2K codec.
366 * @param p_manager the user event manager.
368 static opj_bool j2k_get_end_header( opj_j2k_v2_t *p_j2k,
369 struct opj_stream_private *p_stream,
370 struct opj_event_mgr * p_manager );
373 * -----------------------------------------------------------------------
374 * -----------------------------------------------------------------------
375 * -----------------------------------------------------------------------
379 Write the SOC marker (Start Of Codestream)
380 @param j2k J2K handle
382 static void j2k_write_soc(opj_j2k_t *j2k);
385 * Writes the SOC marker (Start Of Codestream)
387 * @param p_stream the stream to write data to.
388 * @param p_j2k J2K codec.
389 * @param p_manager the user event manager.
391 static opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
392 struct opj_stream_private *p_stream,
393 struct opj_event_mgr * p_manager );
396 * Reads a SOC marker (Start of Codestream)
397 * @param p_header_data the data contained in the SOC box.
398 * @param jp2 the jpeg2000 file codec.
399 * @param p_header_size the size of the data contained in the SOC marker.
400 * @param p_manager the user event manager.
402 static opj_bool opj_j2k_read_soc(
404 struct opj_stream_private *p_stream,
405 struct opj_event_mgr * p_manager
409 Write the SIZ marker (image and tile size)
410 @param j2k J2K handle
412 static void j2k_write_siz(opj_j2k_t *j2k);
415 * Writes the SIZ marker (image and tile size)
417 * @param p_stream the stream to write data to.
418 * @param p_j2k J2K codec.
419 * @param p_manager the user event manager.
421 static opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
422 struct opj_stream_private *p_stream,
423 struct opj_event_mgr * p_manager );
426 * Reads a SIZ marker (image and tile size)
427 * @param p_header_data the data contained in the SIZ box.
428 * @param jp2 the jpeg2000 file codec.
429 * @param p_header_size the size of the data contained in the SIZ marker.
430 * @param p_manager the user event manager.
432 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
433 OPJ_BYTE * p_header_data,
434 OPJ_UINT32 p_header_size,
435 opj_event_mgr_t * p_manager);
438 Write the COM marker (comment)
439 @param j2k J2K handle
441 static void j2k_write_com(opj_j2k_t *j2k);
444 * Writes the COM marker (comment)
446 * @param p_stream the stream to write data to.
447 * @param p_j2k J2K codec.
448 * @param p_manager the user event manager.
450 static opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
451 struct opj_stream_private *p_stream,
452 struct opj_event_mgr * p_manager );
455 Read the COM marker (comment)
456 @param j2k J2K handle
458 static void j2k_read_com(opj_j2k_t *j2k);
460 * Reads a COM marker (comments)
461 * @param p_header_data the data contained in the COM box.
462 * @param jp2 the jpeg2000 file codec.
463 * @param p_header_size the size of the data contained in the COM marker.
464 * @param p_manager the user event manager.
466 static opj_bool j2k_read_com_v2 (
468 OPJ_BYTE * p_header_data,
469 OPJ_UINT32 p_header_size,
470 struct opj_event_mgr * p_manager
473 Write the value concerning the specified component in the marker COD and COC
474 @param j2k J2K handle
475 @param compno Number of the component concerned by the information written
477 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
479 Read the value concerning the specified component in the marker COD and COC
480 @param j2k J2K handle
481 @param compno Number of the component concerned by the information read
483 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
485 Write the COD marker (coding style default)
486 @param j2k J2K handle
488 static void j2k_write_cod(opj_j2k_t *j2k);
491 * Writes the COD marker (Coding style default)
493 * @param p_stream the stream to write data to.
494 * @param p_j2k J2K codec.
495 * @param p_manager the user event manager.
497 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
498 struct opj_stream_private *p_stream,
499 struct opj_event_mgr * p_manager );
502 * Reads a COD marker (Coding Styke defaults)
503 * @param p_header_data the data contained in the COD box.
504 * @param p_j2k the jpeg2000 codec.
505 * @param p_header_size the size of the data contained in the COD marker.
506 * @param p_manager the user event manager.
508 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
509 OPJ_BYTE * p_header_data,
510 OPJ_UINT32 p_header_size,
511 opj_event_mgr_t * p_manager);
514 Write the COC marker (coding style component)
515 @param j2k J2K handle
516 @param compno Number of the component concerned by the information written
518 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
521 * Writes the COC marker (Coding style component)
523 * @param p_comp_number the index of the component to output.
524 * @param p_stream the stream to write data to.
525 * @param p_j2k J2K codec.
526 * @param p_manager the user event manager.
528 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
529 OPJ_UINT32 p_comp_number,
530 struct opj_stream_private *p_stream,
531 struct opj_event_mgr * p_manager
534 * Writes the COC marker (Coding style component)
536 * @param p_comp_no the index of the component to output.
537 * @param p_stream the stream to write data to.
538 * @param p_j2k J2K codec.
539 * @param p_manager the user event manager.
541 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
542 OPJ_UINT32 p_comp_no,
544 OPJ_UINT32 * p_data_written,
545 struct opj_event_mgr * p_manager );
548 * Gets the maximum size taken by a coc.
550 * @param p_j2k the jpeg2000 codec to use.
552 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
555 Read the COC marker (coding style component)
556 @param j2k J2K handle
558 static void j2k_read_coc(opj_j2k_t *j2k);
561 * Reads a COC marker (Coding Style Component)
562 * @param p_header_data the data contained in the COC box.
563 * @param p_j2k the jpeg2000 codec.
564 * @param p_header_size the size of the data contained in the COC marker.
565 * @param p_manager the user event manager.
567 static opj_bool j2k_read_coc_v2 (
569 OPJ_BYTE * p_header_data,
570 OPJ_UINT32 p_header_size,
571 struct opj_event_mgr * p_manager
575 Write the value concerning the specified component in the marker QCD and QCC
576 @param j2k J2K handle
577 @param compno Number of the component concerned by the information written
579 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
581 Read the value concerning the specified component in the marker QCD and QCC
582 @param j2k J2K handle
583 @param compno Number of the component concern by the information read
584 @param len Length of the information in the QCX part of the marker QCD/QCC
586 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
588 Write the QCD marker (quantization default)
589 @param j2k J2K handle
591 static void j2k_write_qcd(opj_j2k_t *j2k);
594 * Writes the QCD marker (quantization default)
596 * @param p_comp_number the index of the component to output.
597 * @param p_stream the stream to write data to.
598 * @param p_j2k J2K codec.
599 * @param p_manager the user event manager.
601 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
602 struct opj_stream_private *p_stream,
603 struct opj_event_mgr * p_manager );
606 Read the QCD marker (quantization default)
607 @param j2k J2K handle
609 static void j2k_read_qcd(opj_j2k_t *j2k);
612 * Reads a QCD marker (Quantization defaults)
613 * @param p_header_data the data contained in the QCD box.
614 * @param p_j2k the jpeg2000 codec.
615 * @param p_header_size the size of the data contained in the QCD marker.
616 * @param p_manager the user event manager.
618 static opj_bool j2k_read_qcd_v2 (
620 OPJ_BYTE * p_header_data,
621 OPJ_UINT32 p_header_size,
622 struct opj_event_mgr * p_manager
626 Write the QCC marker (quantization component)
627 @param j2k J2K handle
628 @param compno Number of the component concerned by the information written
630 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
633 * Writes the QCC marker (quantization component)
635 * @param p_comp_no the index of the component to output.
636 * @param p_stream the stream to write data to.
637 * @param p_j2k J2K codec.
638 * @param p_manager the user event manager.
640 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
641 OPJ_UINT32 p_comp_no,
642 struct opj_stream_private *p_stream,
643 struct opj_event_mgr * p_manager );
646 * Writes the QCC marker (quantization component)
648 * @param p_comp_no the index of the component to output.
649 * @param p_stream the stream to write data to.
650 * @param p_j2k J2K codec.
651 * @param p_manager the user event manager.
653 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
654 OPJ_UINT32 p_comp_no,
656 OPJ_UINT32 * p_data_written,
657 struct opj_event_mgr * p_manager );
660 * Gets the maximum size taken by a qcc.
662 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
665 Read the QCC marker (quantization component)
666 @param j2k J2K handle
668 static void j2k_read_qcc(opj_j2k_t *j2k);
670 * Reads a QCC marker (Quantization component)
671 * @param p_header_data the data contained in the QCC box.
672 * @param p_j2k the jpeg2000 codec.
673 * @param p_header_size the size of the data contained in the QCC marker.
674 * @param p_manager the user event manager.
676 static opj_bool j2k_read_qcc_v2(
678 OPJ_BYTE * p_header_data,
679 OPJ_UINT32 p_header_size,
680 struct opj_event_mgr * p_manager);
683 Write the POC marker (progression order change)
684 @param j2k J2K handle
686 static void j2k_write_poc(opj_j2k_t *j2k);
689 * Writes the POC marker (Progression Order Change)
691 * @param p_stream the stream to write data to.
692 * @param p_j2k J2K codec.
693 * @param p_manager the user event manager.
695 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
696 struct opj_stream_private *p_stream,
697 struct opj_event_mgr * p_manager );
700 * Writes the POC marker (Progression Order Change)
702 * @param p_stream the stream to write data to.
703 * @param p_j2k J2K codec.
704 * @param p_manager the user event manager.
706 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
708 OPJ_UINT32 * p_data_written,
709 struct opj_event_mgr * p_manager );
712 * Gets the maximum size taken by the writting of a POC.
714 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
717 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
719 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
722 * Gets the maximum size taken by the headers of the SOT.
724 * @param p_j2k the jpeg2000 codec to use.
726 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
729 Read the POC marker (progression order change)
730 @param j2k J2K handle
732 static void j2k_read_poc(opj_j2k_t *j2k);
734 * Reads a POC marker (Progression Order Change)
736 * @param p_header_data the data contained in the POC box.
737 * @param p_j2k the jpeg2000 codec.
738 * @param p_header_size the size of the data contained in the POC marker.
739 * @param p_manager the user event manager.
741 static opj_bool j2k_read_poc_v2 (
743 OPJ_BYTE * p_header_data,
744 OPJ_UINT32 p_header_size,
745 struct opj_event_mgr * p_manager
749 Read the CRG marker (component registration)
750 @param j2k J2K handle
752 static void j2k_read_crg(opj_j2k_t *j2k);
754 * Reads a CRG marker (Component registration)
756 * @param p_header_data the data contained in the TLM box.
757 * @param p_j2k the jpeg2000 codec.
758 * @param p_header_size the size of the data contained in the TLM marker.
759 * @param p_manager the user event manager.
761 static opj_bool j2k_read_crg_v2 (
763 OPJ_BYTE * p_header_data,
764 OPJ_UINT32 p_header_size,
765 struct opj_event_mgr * p_manager
768 Read the TLM marker (tile-part lengths)
769 @param j2k J2K handle
771 static void j2k_read_tlm(opj_j2k_t *j2k);
773 * Reads a TLM marker (Tile Length Marker)
775 * @param p_header_data the data contained in the TLM box.
776 * @param p_j2k the jpeg2000 codec.
777 * @param p_header_size the size of the data contained in the TLM marker.
778 * @param p_manager the user event manager.
780 static opj_bool j2k_read_tlm_v2 (
782 OPJ_BYTE * p_header_data,
783 OPJ_UINT32 p_header_size,
784 struct opj_event_mgr * p_manager
788 * Writes the updated tlm.
790 * @param p_stream the stream to write data to.
791 * @param p_j2k J2K codec.
792 * @param p_manager the user event manager.
794 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
795 struct opj_stream_private *p_stream,
796 struct opj_event_mgr * p_manager );
799 Read the PLM marker (packet length, main header)
800 @param j2k J2K handle
802 static void j2k_read_plm(opj_j2k_t *j2k);
805 * Reads a PLM marker (Packet length, main header marker)
807 * @param p_header_data the data contained in the TLM box.
808 * @param p_j2k the jpeg2000 codec.
809 * @param p_header_size the size of the data contained in the TLM marker.
810 * @param p_manager the user event manager.
812 static opj_bool j2k_read_plm_v2 (
814 OPJ_BYTE * p_header_data,
815 OPJ_UINT32 p_header_size,
816 struct opj_event_mgr * p_manager
819 Read the PLT marker (packet length, tile-part header)
820 @param j2k J2K handle
822 static void j2k_read_plt(opj_j2k_t *j2k);
824 * Reads a PLT marker (Packet length, tile-part header)
826 * @param p_header_data the data contained in the PLT box.
827 * @param p_j2k the jpeg2000 codec.
828 * @param p_header_size the size of the data contained in the PLT marker.
829 * @param p_manager the user event manager.
831 static opj_bool j2k_read_plt_v2 (
833 OPJ_BYTE * p_header_data,
834 OPJ_UINT32 p_header_size,
835 struct opj_event_mgr * p_manager
838 Read the PPM marker (packet packet headers, main header)
839 @param j2k J2K handle
841 static void j2k_read_ppm(opj_j2k_t *j2k);
843 * Reads a PPM marker (Packed packet headers, main header)
845 * @param p_header_data the data contained in the POC box.
846 * @param p_j2k the jpeg2000 codec.
847 * @param p_header_size the size of the data contained in the POC marker.
848 * @param p_manager the user event manager.
851 static opj_bool j2k_read_ppm_v2 (
853 OPJ_BYTE * p_header_data,
854 OPJ_UINT32 p_header_size,
855 struct opj_event_mgr * p_manager
859 static opj_bool j2k_read_ppm_v3 (
861 OPJ_BYTE * p_header_data,
862 OPJ_UINT32 p_header_size,
863 struct opj_event_mgr * p_manager
867 Read the PPT marker (packet packet headers, tile-part header)
868 @param j2k J2K handle
870 static void j2k_read_ppt(opj_j2k_t *j2k);
872 * Reads a PPT marker (Packed packet headers, tile-part header)
874 * @param p_header_data the data contained in the PPT box.
875 * @param p_j2k the jpeg2000 codec.
876 * @param p_header_size the size of the data contained in the PPT marker.
877 * @param p_manager the user event manager.
879 static opj_bool j2k_read_ppt_v2 (
881 OPJ_BYTE * p_header_data,
882 OPJ_UINT32 p_header_size,
883 struct opj_event_mgr * p_manager
886 Write the TLM marker (Mainheader)
887 @param j2k J2K handle
889 static void j2k_write_tlm(opj_j2k_t *j2k);
892 * Writes the TLM marker (Tile Length Marker)
894 * @param p_stream the stream to write data to.
895 * @param p_j2k J2K codec.
896 * @param p_manager the user event manager.
898 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
899 struct opj_stream_private *p_stream,
900 struct opj_event_mgr * p_manager );
903 Write the SOT marker (start of tile-part)
904 @param j2k J2K handle
906 static void j2k_write_sot(opj_j2k_t *j2k);
909 * Writes the SOT marker (Start of tile-part)
911 * @param p_stream the stream to write data to.
912 * @param p_j2k J2K codec.
913 * @param p_manager the user event manager.
915 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
917 OPJ_UINT32 * p_data_written,
918 const struct opj_stream_private *p_stream,
919 struct opj_event_mgr * p_manager );
922 * Reads a PPT marker (Packed packet headers, tile-part header)
924 * @param p_header_data the data contained in the PPT box.
925 * @param p_j2k the jpeg2000 codec.
926 * @param p_header_size the size of the data contained in the PPT marker.
927 * @param p_manager the user event manager.
929 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
930 OPJ_BYTE * p_header_data,
931 OPJ_UINT32 p_header_size,
932 opj_event_mgr_t * p_manager );
935 Write the SOD marker (start of data)
936 @param j2k J2K handle
937 @param tile_coder Pointer to a TCD handle
939 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
942 * Writes the SOD marker (Start of data)
944 * @param p_stream the stream to write data to.
945 * @param p_j2k J2K codec.
946 * @param p_manager the user event manager.
948 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
949 struct opj_tcd_v2 * p_tile_coder,
951 OPJ_UINT32 * p_data_written,
952 OPJ_UINT32 p_total_data_size,
953 const struct opj_stream_private *p_stream,
954 struct opj_event_mgr * p_manager );
957 * Reads a SOD marker (Start Of Data)
959 * @param p_header_data the data contained in the SOD box.
960 * @param p_j2k the jpeg2000 codec.
961 * @param p_header_size the size of the data contained in the SOD marker.
962 * @param p_manager the user event manager.
964 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
965 opj_stream_private_t *p_stream,
966 opj_event_mgr_t * p_manager );
969 * Updates the Tile Length Marker.
971 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
973 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
974 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
976 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
977 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
981 Write the RGN marker (region-of-interest)
982 @param j2k J2K handle
983 @param compno Number of the component concerned by the information written
984 @param tileno Number of the tile concerned by the information written
986 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
989 * Writes the RGN marker (Region Of Interest)
991 * @param p_tile_no the tile to output
992 * @param p_comp_no the component to output
993 * @param p_stream the stream to write data to.
994 * @param p_j2k J2K codec.
995 * @param p_manager the user event manager.
997 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
998 OPJ_UINT32 p_tile_no,
999 OPJ_UINT32 p_comp_no,
1000 struct opj_stream_private *p_stream,
1001 struct opj_event_mgr * p_manager );
1004 Read the RGN marker (region-of-interest)
1005 @param j2k J2K handle
1007 static void j2k_read_rgn(opj_j2k_t *j2k);
1010 * Reads a RGN marker (Region Of Interest)
1012 * @param p_header_data the data contained in the POC box.
1013 * @param p_j2k the jpeg2000 codec.
1014 * @param p_header_size the size of the data contained in the POC marker.
1015 * @param p_manager the user event manager.
1017 static opj_bool j2k_read_rgn_v2 (
1018 opj_j2k_v2_t *p_j2k,
1019 OPJ_BYTE * p_header_data,
1020 OPJ_UINT32 p_header_size,
1021 struct opj_event_mgr * p_manager
1025 * Writes the EOC marker (End of Codestream)
1027 * @param p_stream the stream to write data to.
1028 * @param p_j2k J2K codec.
1029 * @param p_manager the user event manager.
1031 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
1032 struct opj_stream_private *p_stream,
1033 struct opj_event_mgr * p_manager );
1036 Write the EOC marker (end of codestream)
1037 @param j2k J2K handle
1039 static void j2k_write_eoc(opj_j2k_t *j2k);
1041 Read the EOC marker (end of codestream)
1042 @param j2k J2K handle
1044 static void j2k_read_eoc(opj_j2k_t *j2k);
1047 * Reads a EOC marker (End Of Codestream)
1049 * @param p_header_data the data contained in the SOD box.
1050 * @param p_j2k the jpeg2000 codec.
1051 * @param p_header_size the size of the data contained in the SOD marker.
1052 * @param p_manager the user event manager.
1055 static opj_bool j2k_read_eoc_v2 (
1056 opj_j2k_v2_t *p_j2k,
1057 struct opj_stream_private *p_stream,
1058 struct opj_event_mgr * p_manager
1065 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1067 * @param p_stream the stream to write data to.
1068 * @param p_j2k J2K codec.
1069 * @param p_manager the user event manager.
1071 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1072 struct opj_stream_private *p_stream,
1073 struct opj_event_mgr * p_manager );
1078 * @param p_stream the stream to write data to.
1079 * @param p_j2k J2K codec.
1080 * @param p_manager the user event manager.
1082 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1083 struct opj_stream_private *p_stream,
1084 struct opj_event_mgr * p_manager );
1087 Read an unknown marker
1088 @param j2k J2K handle
1090 static void j2k_read_unk(opj_j2k_t *j2k);
1092 Add main header marker information
1093 @param cstr_info Codestream information structure
1094 @param type marker type
1095 @param pos byte offset of marker segment
1096 @param len length of marker segment
1098 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1100 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1102 Add tile header marker information
1103 @param tileno tile index number
1104 @param cstr_info Codestream information structure
1105 @param type marker type
1106 @param pos byte offset of marker segment
1107 @param len length of marker segment
1109 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1111 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
1114 * Reads an unknown marker
1116 * @param p_stream the stream object to read from.
1117 * @param p_j2k the jpeg2000 codec.
1118 * @param p_manager the user event manager.
1120 * @return true if the marker could be deduced.
1122 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1123 struct opj_stream_private *p_stream,
1124 OPJ_UINT32 *output_marker,
1125 struct opj_event_mgr * p_manager );
1128 * Writes the MCT marker (Multiple Component Transform)
1130 * @param p_stream the stream to write data to.
1131 * @param p_j2k J2K codec.
1132 * @param p_manager the user event manager.
1134 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1135 opj_mct_data_t * p_mct_record,
1136 struct opj_stream_private *p_stream,
1137 struct opj_event_mgr * p_manager );
1140 * Reads a MCT marker (Multiple Component Transform)
1142 * @param p_header_data the data contained in the MCT box.
1143 * @param p_j2k the jpeg2000 codec.
1144 * @param p_header_size the size of the data contained in the MCT marker.
1145 * @param p_manager the user event manager.
1147 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1148 OPJ_BYTE * p_header_data,
1149 OPJ_UINT32 p_header_size,
1150 struct opj_event_mgr * p_manager );
1153 * Writes the MCC marker (Multiple Component Collection)
1155 * @param p_stream the stream to write data to.
1156 * @param p_j2k J2K codec.
1157 * @param p_manager the user event manager.
1159 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1160 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1161 struct opj_stream_private *p_stream,
1162 struct opj_event_mgr * p_manager );
1166 * Reads a MCC marker (Multiple Component Collection)
1168 * @param p_header_data the data contained in the MCC box.
1169 * @param p_j2k the jpeg2000 codec.
1170 * @param p_header_size the size of the data contained in the MCC marker.
1171 * @param p_manager the user event manager.
1173 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1174 OPJ_BYTE * p_header_data,
1175 OPJ_UINT32 p_header_size,
1176 struct opj_event_mgr * p_manager );
1179 * Writes the MCO marker (Multiple component transformation ordering)
1181 * @param p_stream the stream to write data to.
1182 * @param p_j2k J2K codec.
1183 * @param p_manager the user event manager.
1185 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1186 struct opj_stream_private *p_stream,
1187 struct opj_event_mgr * p_manager );
1190 * Reads a MCO marker (Multiple Component Transform Ordering)
1192 * @param p_header_data the data contained in the MCO box.
1193 * @param p_j2k the jpeg2000 codec.
1194 * @param p_header_size the size of the data contained in the MCO marker.
1195 * @param p_manager the user event manager.
1197 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1198 OPJ_BYTE * p_header_data,
1199 OPJ_UINT32 p_header_size,
1200 struct opj_event_mgr * p_manager );
1202 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1204 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1205 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1206 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1207 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1209 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1210 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1211 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1212 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1214 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1215 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1216 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1217 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1221 * Ends the encoding, i.e. frees memory.
1223 * @param p_stream the stream to write data to.
1224 * @param p_j2k J2K codec.
1225 * @param p_manager the user event manager.
1227 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1228 struct opj_stream_private *p_stream,
1229 struct opj_event_mgr * p_manager );
1232 * Writes the CBD marker (Component bit depth definition)
1234 * @param p_stream the stream to write data to.
1235 * @param p_j2k J2K codec.
1236 * @param p_manager the user event manager.
1238 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1239 struct opj_stream_private *p_stream,
1240 struct opj_event_mgr * p_manager );
1243 * Reads a CBD marker (Component bit depth definition)
1244 * @param p_header_data the data contained in the CBD box.
1245 * @param p_j2k the jpeg2000 codec.
1246 * @param p_header_size the size of the data contained in the CBD marker.
1247 * @param p_manager the user event manager.
1249 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1250 OPJ_BYTE * p_header_data,
1251 OPJ_UINT32 p_header_size,
1252 struct opj_event_mgr * p_manager);
1255 * Writes the image components.
1257 * @param p_stream the stream to write data to.
1258 * @param p_j2k J2K codec.
1259 * @param p_manager the user event manager.
1261 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1262 struct opj_stream_private *p_stream,
1263 struct opj_event_mgr * p_manager );
1266 * Writes regions of interests.
1268 * @param p_stream the stream to write data to.
1269 * @param p_j2k J2K codec.
1270 * @param p_manager the user event manager.
1272 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1273 struct opj_stream_private *p_stream,
1274 struct opj_event_mgr * p_manager );
1279 * @param p_stream the stream to write data to.
1280 * @param p_j2k J2K codec.
1281 * @param p_manager the user event manager.
1283 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1284 struct opj_stream_private *p_stream,
1285 struct opj_event_mgr * p_manager );
1288 * Checks the progression order changes values. Tells of the poc given as input are valid.
1289 * A nice message is outputted at errors.
1291 * @param p_pocs the progression order changes.
1292 * @param p_nb_pocs the number of progression order changes.
1293 * @param p_nb_resolutions the number of resolutions.
1294 * @param numcomps the number of components
1295 * @param numlayers the number of layers.
1297 * @return true if the pocs are valid.
1299 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1300 OPJ_UINT32 p_nb_pocs,
1301 OPJ_UINT32 p_nb_resolutions,
1302 OPJ_UINT32 numcomps,
1303 OPJ_UINT32 numlayers,
1304 opj_event_mgr_t * p_manager);
1307 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1309 * @param cp the coding parameters.
1310 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1311 * @param tileno the given tile.
1313 * @return the number of tile parts.
1315 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1317 /** mem allocation for TLM marker*/
1318 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1321 * Calculates the total number of tile parts needed by the encoder to
1322 * encode such an image. If not enough memory is available, then the function return false.
1324 * @param p_nb_tiles pointer that will hold the number of tile parts.
1325 * @param cp the coding parameters for the image.
1326 * @param image the image to encode.
1327 * @param p_j2k the p_j2k encoder.
1328 * @param p_manager the user event manager.
1330 * @return true if the function was successful, false else.
1332 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1334 OPJ_UINT32 * p_nb_tiles,
1336 opj_event_mgr_t * p_manager);
1338 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1340 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1342 static opj_codestream_index_t* j2k_create_cstr_index(void);
1348 /* ----------------------------------------------------------------------- */
1349 typedef struct j2k_prog_order{
1350 OPJ_PROG_ORDER enum_prog;
1354 j2k_prog_order_t j2k_prog_order_list[] = {
1360 {(OPJ_PROG_ORDER)-1, ""}
1368 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1376 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1378 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1380 j2k_read_int16_to_float,
1381 j2k_read_int32_to_float,
1382 j2k_read_float32_to_float,
1383 j2k_read_float64_to_float
1386 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1388 j2k_read_int16_to_int32,
1389 j2k_read_int32_to_int32,
1390 j2k_read_float32_to_int32,
1391 j2k_read_float64_to_int32
1394 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1396 j2k_write_float_to_int16,
1397 j2k_write_float_to_int32,
1398 j2k_write_float_to_float,
1399 j2k_write_float_to_float64
1402 typedef struct opj_dec_memory_marker_handler
1406 /** value of the state when the marker can appear */
1408 /** action linked to the marker */
1409 opj_bool (*handler) (
1410 opj_j2k_v2_t *p_j2k,
1411 OPJ_BYTE * p_header_data,
1412 OPJ_UINT32 p_header_size,
1413 struct opj_event_mgr * p_manager
1416 opj_dec_memory_marker_handler_t;
1418 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1421 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1422 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1423 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1424 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1425 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1426 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1427 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1428 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1429 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1430 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1431 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1432 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1433 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1435 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1436 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1437 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1438 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1439 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1440 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1442 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1443 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1444 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1445 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1446 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1447 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1448 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1449 {J2K_MS_SIZ, J2K_STATE_MHSIZ , opj_j2k_read_siz},
1450 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1451 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1452 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1453 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1454 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1456 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1457 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1458 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1459 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1460 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1461 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1463 #ifdef TODO_MS /* FIXME */
1464 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1465 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1466 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1467 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1469 #endif /* USE_JPWL */
1471 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1472 {J2K_MS_INSEC, 0, j2k_read_insec}
1473 #endif /* USE_JPSEC */
1474 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1479 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1481 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1482 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1486 for (i=0;i<p_nb_elem;++i) {
1487 opj_read_bytes(l_src_data,&l_temp,2);
1489 l_src_data+=sizeof(OPJ_INT16);
1491 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1495 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1497 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1498 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1502 for (i=0;i<p_nb_elem;++i) {
1503 opj_read_bytes(l_src_data,&l_temp,4);
1505 l_src_data+=sizeof(OPJ_INT32);
1507 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1511 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1513 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1514 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1518 for (i=0;i<p_nb_elem;++i) {
1519 opj_read_float(l_src_data,&l_temp);
1521 l_src_data+=sizeof(OPJ_FLOAT32);
1523 *(l_dest_data++) = l_temp;
1527 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1529 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1530 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1534 for (i=0;i<p_nb_elem;++i) {
1535 opj_read_double(l_src_data,&l_temp);
1537 l_src_data+=sizeof(OPJ_FLOAT64);
1539 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1543 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1545 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1546 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1550 for (i=0;i<p_nb_elem;++i) {
1551 opj_read_bytes(l_src_data,&l_temp,2);
1553 l_src_data+=sizeof(OPJ_INT16);
1555 *(l_dest_data++) = (OPJ_INT32) l_temp;
1559 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1561 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1562 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1566 for (i=0;i<p_nb_elem;++i) {
1567 opj_read_bytes(l_src_data,&l_temp,4);
1569 l_src_data+=sizeof(OPJ_INT32);
1571 *(l_dest_data++) = (OPJ_INT32) l_temp;
1575 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1577 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1578 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1582 for (i=0;i<p_nb_elem;++i) {
1583 opj_read_float(l_src_data,&l_temp);
1585 l_src_data+=sizeof(OPJ_FLOAT32);
1587 *(l_dest_data++) = (OPJ_INT32) l_temp;
1591 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1593 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1594 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1598 for (i=0;i<p_nb_elem;++i) {
1599 opj_read_double(l_src_data,&l_temp);
1601 l_src_data+=sizeof(OPJ_FLOAT64);
1603 *(l_dest_data++) = (OPJ_INT32) l_temp;
1607 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1609 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1610 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1614 for (i=0;i<p_nb_elem;++i) {
1615 l_temp = (OPJ_UINT32) *(l_src_data++);
1617 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1619 l_dest_data+=sizeof(OPJ_INT16);
1623 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1625 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1626 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1630 for (i=0;i<p_nb_elem;++i) {
1631 l_temp = (OPJ_UINT32) *(l_src_data++);
1633 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1635 l_dest_data+=sizeof(OPJ_INT32);
1639 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1641 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1642 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1646 for (i=0;i<p_nb_elem;++i) {
1647 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1649 opj_write_float(l_dest_data,l_temp);
1651 l_dest_data+=sizeof(OPJ_FLOAT32);
1655 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1657 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1658 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1662 for (i=0;i<p_nb_elem;++i) {
1663 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1665 opj_write_double(l_dest_data,l_temp);
1667 l_dest_data+=sizeof(OPJ_FLOAT64);
1673 * Converts an enum type progression order to string type.
1675 * @param prg_order the progression order to get.
1677 * @return the string representation of the given progression order.
1679 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1680 j2k_prog_order_t *po;
1681 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1682 if(po->enum_prog == prg_order){
1683 return po->str_prog;
1686 return po->str_prog;
1690 * Checks the progression order changes values. Tells if the poc given as input are valid.
1692 * @param p_pocs the progression order changes.
1693 * @param p_nb_pocs the number of progression order changes.
1694 * @param p_nb_resolutions the number of resolutions.
1695 * @param numcomps the number of components
1696 * @param numlayers the number of layers.
1697 * @param p_manager the user event manager.
1699 * @return true if the pocs are valid.
1701 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1702 OPJ_UINT32 p_nb_pocs,
1703 OPJ_UINT32 p_nb_resolutions,
1704 OPJ_UINT32 p_num_comps,
1705 OPJ_UINT32 p_num_layers,
1706 opj_event_mgr_t * p_manager)
1708 OPJ_UINT32* packet_array;
1709 OPJ_UINT32 index , resno, compno, layno;
1711 OPJ_UINT32 step_c = 1;
1712 OPJ_UINT32 step_r = p_num_comps * step_c;
1713 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1714 opj_bool loss = OPJ_FALSE;
1715 OPJ_UINT32 layno0 = 0;
1717 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1718 if (packet_array == 00) {
1719 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1722 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1724 if (p_nb_pocs == 0) {
1728 index = step_r * p_pocs->resno0;
1729 // take each resolution for each poc
1730 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1732 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1734 // take each comp of each resolution for each poc
1735 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1736 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1738 // and finally take each layer of each res of ...
1739 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1740 //index = step_r * resno + step_c * compno + step_l * layno;
1741 packet_array[comp_index] = 1;
1742 comp_index += step_l;
1745 res_index += step_c;
1752 // iterate through all the pocs
1753 for (i = 1; i < p_nb_pocs ; ++i) {
1754 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1756 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1757 index = step_r * p_pocs->resno0;
1759 // take each resolution for each poc
1760 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1761 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1763 // take each comp of each resolution for each poc
1764 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1765 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1767 // and finally take each layer of each res of ...
1768 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1769 //index = step_r * resno + step_c * compno + step_l * layno;
1770 packet_array[comp_index] = 1;
1771 comp_index += step_l;
1774 res_index += step_c;
1784 for (layno = 0; layno < p_num_layers ; ++layno) {
1785 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1786 for (compno = 0; compno < p_num_comps; ++compno) {
1787 loss |= (packet_array[index]!=1);
1788 //index = step_r * resno + step_c * compno + step_l * layno;
1795 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1798 opj_free(packet_array);
1803 /* ----------------------------------------------------------------------- */
1804 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1807 int tpnum=1,tpend=0;
1808 opj_tcp_t *tcp = &cp->tcps[tileno];
1809 prog = j2k_convert_progression_order(tcp->prg);
1814 if( cp->tp_flag == prog[i] ){
1815 tpend=1;cp->tp_pos=i;
1819 tpnum= tpnum * tcp->pocs[pino].compE;
1822 tpnum= tpnum * tcp->pocs[pino].resE;
1825 tpnum= tpnum * tcp->pocs[pino].prcE;
1828 tpnum= tpnum * tcp->pocs[pino].layE;
1840 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1842 * @param cp the coding parameters.
1843 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1844 * @param tileno the given tile.
1846 * @return the number of tile parts.
1848 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1850 const OPJ_CHAR *prog = 00;
1852 OPJ_UINT32 tpnum = 1;
1853 opj_tcp_v2_t *tcp = 00;
1854 opj_poc_t * l_current_poc = 00;
1857 assert(tileno < (cp->tw * cp->th));
1858 assert(pino < (cp->tcps[tileno].numpocs + 1));
1860 /* get the given tile coding parameter */
1861 tcp = &cp->tcps[tileno];
1864 l_current_poc = &(tcp->pocs[pino]);
1865 assert(l_current_poc != 0);
1867 /* get the progression order as a character string */
1868 prog = j2k_convert_progression_order(tcp->prg);
1869 assert(strlen(prog) > 0);
1871 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1875 /* component wise */
1877 tpnum *= l_current_poc->compE;
1879 /* resolution wise */
1881 tpnum *= l_current_poc->resE;
1885 tpnum *= l_current_poc->prcE;
1889 tpnum *= l_current_poc->layE;
1892 /* whould we split here ? */
1893 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1894 cp->m_specific_param.m_enc.m_tp_pos=i;
1906 /** mem allocation for TLM marker*/
1907 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1908 int pino,tileno,totnum_tp=0;
1910 OPJ_ARG_NOT_USED(img_numcomp);
1912 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1913 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1914 int cur_totnum_tp = 0;
1915 opj_tcp_t *tcp = &cp->tcps[tileno];
1916 for(pino = 0; pino <= tcp->numpocs; pino++) {
1918 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1919 if(!pi) { return -1;}
1920 tp_num = j2k_get_num_tp(cp,pino,tileno);
1921 totnum_tp = totnum_tp + tp_num;
1922 cur_totnum_tp = cur_totnum_tp + tp_num;
1923 pi_destroy(pi, cp, tileno);
1925 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1927 if (j2k->cstr_info) {
1928 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1929 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1937 * Calculates the total number of tile parts needed by the encoder to
1938 * encode such an image. If not enough memory is available, then the function return false.
1940 * @param p_nb_tiles pointer that will hold the number of tile parts.
1941 * @param cp the coding parameters for the image.
1942 * @param image the image to encode.
1943 * @param p_j2k the p_j2k encoder.
1944 * @param p_manager the user event manager.
1946 * @return true if the function was successful, false else.
1948 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1950 OPJ_UINT32 * p_nb_tiles,
1952 opj_event_mgr_t * p_manager)
1954 OPJ_UINT32 pino,tileno;
1955 OPJ_UINT32 l_nb_tiles;
1959 assert(p_nb_tiles != 00);
1961 assert(image != 00);
1962 assert(p_j2k != 00);
1963 assert(p_manager != 00);
1965 l_nb_tiles = cp->tw * cp->th;
1970 /* TODO mergeV2: check this part which use cstr_info */
1971 /*if (p_j2k->cstr_info) {
1972 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1974 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1975 OPJ_UINT32 cur_totnum_tp = 0;
1977 pi_update_encoding_parameters(image,cp,tileno);
1979 for (pino = 0; pino <= tcp->numpocs; ++pino)
1981 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1983 *p_nb_tiles = *p_nb_tiles + tp_num;
1985 cur_totnum_tp += tp_num;
1988 tcp->m_nb_tile_parts = cur_totnum_tp;
1990 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1991 if (l_info_tile_ptr->tp == 00) {
1995 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1997 l_info_tile_ptr->num_tps = cur_totnum_tp;
2004 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2005 OPJ_UINT32 cur_totnum_tp = 0;
2007 pi_update_encoding_parameters(image,cp,tileno);
2009 for (pino = 0; pino <= tcp->numpocs; ++pino) {
2010 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2012 *p_nb_tiles = *p_nb_tiles + tp_num;
2014 cur_totnum_tp += tp_num;
2016 tcp->m_nb_tile_parts = cur_totnum_tp;
2025 static void j2k_write_soc(opj_j2k_t *j2k) {
2026 opj_cio_t *cio = j2k->cio;
2027 cio_write(cio, J2K_MS_SOC, 2);
2030 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2035 /* update markers struct */
2036 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2037 #endif /* USE_JPWL */
2042 * Writes the SOC marker (Start Of Codestream)
2044 * @param p_stream the stream to write data to.
2045 * @param p_j2k J2K codec.
2046 * @param p_manager the user event manager.
2048 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2049 struct opj_stream_private *p_stream,
2050 struct opj_event_mgr * p_manager )
2052 /* 2 bytes will be written */
2053 OPJ_BYTE * l_start_stream = 00;
2056 assert(p_stream != 00);
2057 assert(p_j2k != 00);
2058 assert(p_manager != 00);
2060 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2062 /* write SOC identifier */
2063 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2065 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2071 /* update markers struct */
2073 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2075 assert( 0 && "TODO" );
2076 #endif /* USE_JPWL */
2085 * Reads a SOC marker (Start of Codestream)
2086 * @param p_header_data the data contained in the SOC box.
2087 * @param jp2 the jpeg2000 file codec.
2088 * @param p_header_size the size of the data contained in the SOC marker.
2089 * @param p_manager the user event manager.
2091 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2092 struct opj_stream_private *p_stream,
2093 struct opj_event_mgr * p_manager )
2095 OPJ_BYTE l_data [2];
2096 OPJ_UINT32 l_marker;
2099 assert(p_j2k != 00);
2100 assert(p_manager != 00);
2101 assert(p_stream != 00);
2103 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2107 opj_read_bytes(l_data,&l_marker,2);
2108 if (l_marker != J2K_MS_SOC) {
2112 /* Next marker should be a SIZ marker in the main header */
2113 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2115 /* FIXME move it in a index structure included in p_j2k*/
2116 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2118 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2120 /* Add the marker to the codestream index*/
2121 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2126 static void j2k_write_siz(opj_j2k_t *j2k) {
2130 opj_cio_t *cio = j2k->cio;
2131 opj_image_t *image = j2k->image;
2132 opj_cp_t *cp = j2k->cp;
2134 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2135 lenp = cio_tell(cio);
2137 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2138 cio_write(cio, image->x1, 4); /* Xsiz */
2139 cio_write(cio, image->y1, 4); /* Ysiz */
2140 cio_write(cio, image->x0, 4); /* X0siz */
2141 cio_write(cio, image->y0, 4); /* Y0siz */
2142 cio_write(cio, cp->tdx, 4); /* XTsiz */
2143 cio_write(cio, cp->tdy, 4); /* YTsiz */
2144 cio_write(cio, cp->tx0, 4); /* XT0siz */
2145 cio_write(cio, cp->ty0, 4); /* YT0siz */
2146 cio_write(cio, image->numcomps, 2); /* Csiz */
2147 for (i = 0; i < image->numcomps; i++) {
2148 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2149 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2150 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2152 len = cio_tell(cio) - lenp;
2153 cio_seek(cio, lenp);
2154 cio_write(cio, len, 2); /* Lsiz */
2155 cio_seek(cio, lenp + len);
2158 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2162 * Writes the SIZ marker (image and tile size)
2164 * @param p_stream the stream to write data to.
2165 * @param p_j2k J2K codec.
2166 * @param p_manager the user event manager.
2168 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2169 struct opj_stream_private *p_stream,
2170 struct opj_event_mgr * p_manager )
2173 OPJ_UINT32 l_size_len;
2174 OPJ_BYTE * l_current_ptr;
2175 opj_image_t * l_image = 00;
2176 opj_cp_v2_t *cp = 00;
2177 opj_image_comp_t * l_img_comp = 00;
2180 assert(p_stream != 00);
2181 assert(p_j2k != 00);
2182 assert(p_manager != 00);
2184 l_image = p_j2k->m_private_image;
2185 cp = &(p_j2k->m_cp);
2186 l_size_len = 40 + 3 * l_image->numcomps;
2187 l_img_comp = l_image->comps;
2189 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2191 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2192 = (OPJ_BYTE*)opj_realloc(
2193 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2195 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2199 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2202 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2204 /* write SOC identifier */
2205 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2208 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2211 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2214 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2217 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2220 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2223 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2226 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2229 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2232 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2235 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2238 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2241 for (i = 0; i < l_image->numcomps; ++i) {
2242 /* TODO here with MCT ? */
2243 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2246 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2249 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2255 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) {
2265 * Reads a SIZ marker (image and tile size)
2266 * @param p_header_data the data contained in the SIZ box.
2267 * @param jp2 the jpeg2000 file codec.
2268 * @param p_header_size the size of the data contained in the SIZ marker.
2269 * @param p_manager the user event manager.
2271 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2272 OPJ_BYTE * p_header_data,
2273 OPJ_UINT32 p_header_size,
2274 opj_event_mgr_t * p_manager
2277 OPJ_UINT32 l_size, i;
2278 OPJ_UINT32 l_nb_comp;
2279 OPJ_UINT32 l_nb_comp_remain;
2280 OPJ_UINT32 l_remaining_size;
2281 OPJ_UINT32 l_nb_tiles;
2283 opj_image_t *l_image = 00;
2284 opj_cp_v2_t *l_cp = 00;
2285 opj_image_comp_t * l_img_comp = 00;
2286 opj_tcp_v2_t * l_current_tile_param = 00;
2289 assert(p_j2k != 00);
2290 assert(p_manager != 00);
2291 assert(p_header_data != 00);
2293 l_image = p_j2k->m_private_image;
2294 l_cp = &(p_j2k->m_cp);
2296 /* minimum size == 39 - 3 (= minimum component parameter) */
2297 if (p_header_size < 36) {
2298 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2302 l_remaining_size = p_header_size - 36;
2303 l_nb_comp = l_remaining_size / 3;
2304 l_nb_comp_remain = l_remaining_size % 3;
2305 if (l_nb_comp_remain != 0){
2306 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2310 l_size = p_header_size + 2; /* Lsiz */
2312 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2314 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2315 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2317 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2319 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2321 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2323 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2325 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2327 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2329 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2331 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2334 l_image->numcomps = (OPJ_UINT16) l_tmp;
2336 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2340 if (l_image->numcomps != l_nb_comp) {
2341 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);
2346 if (l_cp->correct) {
2347 /* if JPWL is on, we check whether TX errors have damaged
2348 too much the SIZ parameters */
2349 if (!(l_image->x1 * l_image->y1)) {
2350 opj_event_msg_v2(p_manager, EVT_ERROR,
2351 "JPWL: bad image size (%d x %d)\n",
2352 l_image->x1, l_image->y1);
2353 if (!JPWL_ASSUME || JPWL_ASSUME) {
2354 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2359 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2360 if (l_image->numcomps != ((len - 38) / 3)) {
2361 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2362 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2363 l_image->numcomps, ((len - 38) / 3));
2365 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2368 */ /* we try to correct */
2369 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2370 if (l_image->numcomps < ((len - 38) / 3)) {
2371 len = 38 + 3 * l_image->numcomps;
2372 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2375 l_image->numcomps = ((len - 38) / 3);
2376 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2382 /* update components number in the jpwl_exp_comps filed */
2383 l_cp->exp_comps = l_image->numcomps;
2385 #endif /* USE_JPWL */
2387 /* Allocate the resulting image components */
2388 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2389 if (l_image->comps == 00){
2390 l_image->numcomps = 0;
2391 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2395 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2396 l_img_comp = l_image->comps;
2398 /* Read the component information */
2399 for (i = 0; i < l_image->numcomps; ++i){
2401 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2403 l_img_comp->prec = (tmp & 0x7f) + 1;
2404 l_img_comp->sgnd = tmp >> 7;
2405 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2407 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2408 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2410 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2413 if (l_cp->correct) {
2414 /* if JPWL is on, we check whether TX errors have damaged
2415 too much the SIZ parameters, again */
2416 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2417 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2418 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2419 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2421 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2424 /* we try to correct */
2425 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2426 if (!l_image->comps[i].dx) {
2427 l_image->comps[i].dx = 1;
2428 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2429 i, l_image->comps[i].dx);
2431 if (!l_image->comps[i].dy) {
2432 l_image->comps[i].dy = 1;
2433 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2434 i, l_image->comps[i].dy);
2438 #endif /* USE_JPWL */
2439 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2440 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2444 /* Compute the number of tiles */
2445 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2446 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2447 l_nb_tiles = l_cp->tw * l_cp->th;
2449 /* Define the tiles which will be decoded */
2450 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2451 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;
2452 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;
2453 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);
2454 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);
2457 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2458 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2459 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2460 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2464 if (l_cp->correct) {
2465 /* if JPWL is on, we check whether TX errors have damaged
2466 too much the SIZ parameters */
2467 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2468 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2469 "JPWL: bad number of tiles (%d x %d)\n",
2470 l_cp->tw, l_cp->th);
2472 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2475 /* we try to correct */
2476 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2479 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2482 if (l_cp->tw > l_cp->max_tiles) {
2484 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2485 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2486 l_cp->max_tiles, l_cp->tw);
2490 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2493 if (l_cp->th > l_cp->max_tiles) {
2495 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2496 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2497 l_cp->max_tiles, l_cp->th);
2501 #endif /* USE_JPWL */
2503 /* memory allocations */
2504 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2505 if (l_cp->tcps == 00) {
2506 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2509 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2512 if (l_cp->correct) {
2514 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2515 "JPWL: could not alloc tcps field of cp\n");
2516 if (!JPWL_ASSUME || JPWL_ASSUME) {
2517 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2522 #endif /* USE_JPWL */
2524 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2525 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2526 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2527 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2530 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2532 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2533 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2535 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2536 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2539 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));
2540 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2542 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2543 (opj_simple_mcc_decorrelation_data_t*)
2544 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2546 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2547 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2550 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));
2551 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2553 /* set up default dc level shift */
2554 for (i=0;i<l_image->numcomps;++i) {
2555 if (! l_image->comps[i].sgnd) {
2556 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2560 l_current_tile_param = l_cp->tcps;
2561 for (i = 0; i < l_nb_tiles; ++i) {
2562 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2563 if (l_current_tile_param->tccps == 00) {
2564 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2567 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2569 ++l_current_tile_param;
2572 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2573 opj_image_comp_header_update(l_image,l_cp);
2580 static void j2k_write_com(opj_j2k_t *j2k) {
2584 if(j2k->cp->comment) {
2585 opj_cio_t *cio = j2k->cio;
2586 char *comment = j2k->cp->comment;
2588 cio_write(cio, J2K_MS_COM, 2);
2589 lenp = cio_tell(cio);
2591 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2592 for (i = 0; i < strlen(comment); i++) {
2593 cio_write(cio, comment[i], 1);
2595 len = cio_tell(cio) - lenp;
2596 cio_seek(cio, lenp);
2597 cio_write(cio, len, 2);
2598 cio_seek(cio, lenp + len);
2602 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2608 * Writes the COM marker (comment)
2610 * @param p_stream the stream to write data to.
2611 * @param p_j2k J2K codec.
2612 * @param p_manager the user event manager.
2614 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2615 struct opj_stream_private *p_stream,
2616 struct opj_event_mgr * p_manager )
2618 OPJ_UINT32 l_comment_size;
2619 OPJ_UINT32 l_total_com_size;
2620 const OPJ_CHAR *l_comment;
2621 OPJ_BYTE * l_current_ptr = 00;
2624 assert(p_j2k != 00);
2625 assert(p_stream != 00);
2626 assert(p_manager != 00);
2628 l_comment = p_j2k->m_cp.comment;
2629 l_comment_size = strlen(l_comment);
2630 l_total_com_size = l_comment_size + 6;
2632 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2633 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2634 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2637 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2641 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2644 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2646 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2649 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2652 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2655 memcpy( l_current_ptr,l_comment,l_comment_size);
2657 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) {
2664 static void j2k_read_com(opj_j2k_t *j2k) {
2667 opj_cio_t *cio = j2k->cio;
2669 len = cio_read(cio, 2);
2670 cio_skip(cio, len - 2);
2673 * Reads a COM marker (comments)
2674 * @param p_header_data the data contained in the COM box.
2675 * @param jp2 the jpeg2000 file codec.
2676 * @param p_header_size the size of the data contained in the COM marker.
2677 * @param p_manager the user event manager.
2679 opj_bool j2k_read_com_v2 (
2680 opj_j2k_v2_t *p_j2k,
2681 OPJ_BYTE * p_header_data,
2682 OPJ_UINT32 p_header_size,
2683 struct opj_event_mgr * p_manager
2687 assert(p_j2k != 00);
2688 assert(p_manager != 00);
2689 assert(p_header_data != 00);
2690 (void)p_header_size;
2695 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2698 opj_cp_t *cp = j2k->cp;
2699 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2700 opj_tccp_t *tccp = &tcp->tccps[compno];
2701 opj_cio_t *cio = j2k->cio;
2703 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2704 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2705 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2706 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2707 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2709 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2710 for (i = 0; i < tccp->numresolutions; i++) {
2711 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2716 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2719 opj_cp_t *cp = j2k->cp;
2720 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2721 opj_tccp_t *tccp = &tcp->tccps[compno];
2722 opj_cio_t *cio = j2k->cio;
2724 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2726 /* If user wants to remove more resolutions than the codestream contains, return error*/
2727 assert(cp->reduce >= 0);
2728 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2729 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2730 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2731 j2k->state |= J2K_STATE_ERR;
2734 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2735 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2736 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2737 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2738 if (tccp->csty & J2K_CP_CSTY_PRT) {
2739 for (i = 0; i < tccp->numresolutions; i++) {
2740 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2741 tccp->prcw[i] = tmp & 0xf;
2742 tccp->prch[i] = tmp >> 4;
2747 if(j2k->cstr_info && compno == 0) {
2748 for (i = 0; i < tccp->numresolutions; i++) {
2749 if (tccp->csty & J2K_CP_CSTY_PRT) {
2750 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2751 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2754 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2755 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2762 static void j2k_write_cod(opj_j2k_t *j2k) {
2763 opj_cp_t *cp = NULL;
2764 opj_tcp_t *tcp = NULL;
2767 opj_cio_t *cio = j2k->cio;
2769 cio_write(cio, J2K_MS_COD, 2); /* COD */
2771 lenp = cio_tell(cio);
2775 tcp = &cp->tcps[j2k->curtileno];
2777 cio_write(cio, tcp->csty, 1); /* Scod */
2778 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2779 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2780 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2782 j2k_write_cox(j2k, 0);
2783 len = cio_tell(cio) - lenp;
2784 cio_seek(cio, lenp);
2785 cio_write(cio, len, 2); /* Lcod */
2786 cio_seek(cio, lenp + len);
2789 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2794 * Writes the COD marker (Coding style default)
2796 * @param p_stream the stream to write data to.
2797 * @param p_j2k J2K codec.
2798 * @param p_manager the user event manager.
2800 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2801 struct opj_stream_private *p_stream,
2802 struct opj_event_mgr * p_manager )
2804 opj_cp_v2_t *l_cp = 00;
2805 opj_tcp_v2_t *l_tcp = 00;
2806 OPJ_UINT32 l_code_size,l_remaining_size;
2807 OPJ_BYTE * l_current_data = 00;
2810 assert(p_j2k != 00);
2811 assert(p_manager != 00);
2812 assert(p_stream != 00);
2814 l_cp = &(p_j2k->m_cp);
2815 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2816 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2817 l_remaining_size = l_code_size;
2819 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2820 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2821 = (OPJ_BYTE*)opj_realloc(
2822 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2825 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2829 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2832 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2834 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2835 l_current_data += 2;
2837 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2838 l_current_data += 2;
2840 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2843 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2846 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2849 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2852 l_remaining_size -= 9;
2854 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2855 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2859 if (l_remaining_size != 0) {
2860 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2864 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) {
2873 * Reads a COD marker (Coding Styke defaults)
2874 * @param p_header_data the data contained in the COD box.
2875 * @param p_j2k the jpeg2000 codec.
2876 * @param p_header_size the size of the data contained in the COD marker.
2877 * @param p_manager the user event manager.
2879 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2880 OPJ_BYTE * p_header_data,
2881 OPJ_UINT32 p_header_size,
2882 opj_event_mgr_t * p_manager
2888 opj_cp_v2_t *l_cp = 00;
2889 opj_tcp_v2_t *l_tcp = 00;
2890 opj_image_t *l_image = 00;
2893 assert(p_header_data != 00);
2894 assert(p_j2k != 00);
2895 assert(p_manager != 00);
2897 l_image = p_j2k->m_private_image;
2898 l_cp = &(p_j2k->m_cp);
2900 /* If we are in the first tile-part header of the current tile */
2901 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2902 &l_cp->tcps[p_j2k->m_current_tile_number] :
2903 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2905 /* Make sure room is sufficient */
2906 if (p_header_size < 5) {
2907 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2911 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2913 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2915 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2916 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2919 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2920 if (l_cp->m_specific_param.m_dec.m_layer) {
2921 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2924 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2927 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2931 for (i = 0; i < l_image->numcomps; ++i) {
2932 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2935 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2936 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2940 if (p_header_size != 0) {
2941 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2945 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2946 j2k_copy_tile_component_parameters(p_j2k);
2949 #ifdef WIP_REMOVE_MSD
2950 if (p_j2k->cstr_info) {
2951 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2952 p_j2k->cstr_info->prog = l_tcp->prg;
2953 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2954 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2955 for (i = 0; i < l_image->numcomps; ++i) {
2956 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2964 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2967 opj_cp_t *cp = j2k->cp;
2968 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2969 opj_image_t *image = j2k->image;
2970 opj_cio_t *cio = j2k->cio;
2972 cio_write(cio, J2K_MS_COC, 2); /* COC */
2973 lenp = cio_tell(cio);
2975 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2976 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2977 j2k_write_cox(j2k, compno);
2978 len = cio_tell(cio) - lenp;
2979 cio_seek(cio, lenp);
2980 cio_write(cio, len, 2); /* Lcoc */
2981 cio_seek(cio, lenp + len);
2985 * Writes the COC marker (Coding style component)
2987 * @param p_comp_no the index of the component to output.
2988 * @param p_stream the stream to write data to.
2989 * @param p_j2k J2K codec.
2990 * @param p_manager the user event manager.
2992 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2993 OPJ_UINT32 p_comp_no,
2994 struct opj_stream_private *p_stream,
2995 struct opj_event_mgr * p_manager )
2997 OPJ_UINT32 l_coc_size,l_remaining_size;
2998 OPJ_UINT32 l_comp_room;
3001 assert(p_j2k != 00);
3002 assert(p_manager != 00);
3003 assert(p_stream != 00);
3005 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3007 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3009 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3010 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3011 = (OPJ_BYTE*)opj_realloc(
3012 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3014 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3018 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3021 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);
3023 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) {
3031 * Writes the COC marker (Coding style component)
3033 * @param p_comp_no the index of the component to output.
3034 * @param p_stream the stream to write data to.
3035 * @param p_j2k J2K codec.
3036 * @param p_manager the user event manager.
3038 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3039 OPJ_UINT32 p_comp_no,
3041 OPJ_UINT32 * p_data_written,
3042 struct opj_event_mgr * p_manager )
3044 opj_cp_v2_t *l_cp = 00;
3045 opj_tcp_v2_t *l_tcp = 00;
3046 OPJ_UINT32 l_coc_size,l_remaining_size;
3047 OPJ_BYTE * l_current_data = 00;
3048 opj_image_t *l_image = 00;
3049 OPJ_UINT32 l_comp_room;
3052 assert(p_j2k != 00);
3053 assert(p_manager != 00);
3055 l_cp = &(p_j2k->m_cp);
3056 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3057 l_image = p_j2k->m_private_image;
3058 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3060 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3061 l_remaining_size = l_coc_size;
3063 l_current_data = p_data;
3065 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3066 l_current_data += 2;
3068 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3069 l_current_data += 2;
3071 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3072 l_current_data+=l_comp_room;
3074 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3077 l_remaining_size -= (5 + l_comp_room);
3078 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3079 * p_data_written = l_coc_size;
3083 * Gets the maximum size taken by a coc.
3085 * @param p_j2k the jpeg2000 codec to use.
3087 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3090 OPJ_UINT32 l_nb_comp;
3091 OPJ_UINT32 l_nb_tiles;
3092 OPJ_UINT32 l_max = 0;
3096 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3097 l_nb_comp = p_j2k->m_private_image->numcomps;
3099 for (i=0;i<l_nb_tiles;++i) {
3100 for (j=0;j<l_nb_comp;++j) {
3101 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3108 static void j2k_read_coc(opj_j2k_t *j2k) {
3111 opj_cp_t *cp = j2k->cp;
3112 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3113 opj_image_t *image = j2k->image;
3114 opj_cio_t *cio = j2k->cio;
3116 len = cio_read(cio, 2); /* Lcoc */
3117 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3118 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
3119 j2k_read_cox(j2k, compno);
3123 * Reads a COC marker (Coding Style Component)
3124 * @param p_header_data the data contained in the COC box.
3125 * @param p_j2k the jpeg2000 codec.
3126 * @param p_header_size the size of the data contained in the COC marker.
3127 * @param p_manager the user event manager.
3129 opj_bool j2k_read_coc_v2 (
3130 opj_j2k_v2_t *p_j2k,
3131 OPJ_BYTE * p_header_data,
3132 OPJ_UINT32 p_header_size,
3133 struct opj_event_mgr * p_manager
3136 opj_cp_v2_t *l_cp = NULL;
3137 opj_tcp_v2_t *l_tcp = NULL;
3138 opj_image_t *l_image = NULL;
3139 OPJ_UINT32 l_comp_room;
3140 OPJ_UINT32 l_comp_no;
3143 assert(p_header_data != 00);
3144 assert(p_j2k != 00);
3145 assert(p_manager != 00);
3147 l_cp = &(p_j2k->m_cp);
3148 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3149 &l_cp->tcps[p_j2k->m_current_tile_number] :
3150 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3151 l_image = p_j2k->m_private_image;
3153 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3155 /* make sure room is sufficient*/
3156 if (p_header_size < l_comp_room + 1) {
3157 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3160 p_header_size -= l_comp_room + 1;
3162 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3163 p_header_data += l_comp_room;
3164 if (l_comp_no >= l_image->numcomps) {
3165 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3169 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3172 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3173 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3177 if (p_header_size != 0) {
3178 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3184 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3185 int bandno, numbands;
3188 opj_cp_t *cp = j2k->cp;
3189 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3190 opj_tccp_t *tccp = &tcp->tccps[compno];
3191 opj_cio_t *cio = j2k->cio;
3193 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3194 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3196 for (bandno = 0; bandno < numbands; bandno++) {
3197 expn = tccp->stepsizes[bandno].expn;
3198 mant = tccp->stepsizes[bandno].mant;
3200 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3201 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3203 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3208 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3210 int bandno, numbands;
3212 opj_cp_t *cp = j2k->cp;
3213 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3214 opj_tccp_t *tccp = &tcp->tccps[compno];
3215 opj_cio_t *cio = j2k->cio;
3217 tmp = cio_read(cio, 1); /* Sqcx */
3218 tccp->qntsty = tmp & 0x1f;
3219 tccp->numgbits = tmp >> 5;
3220 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3221 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3224 if (j2k->cp->correct) {
3226 /* if JPWL is on, we check whether there are too many subbands */
3227 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3228 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3229 "JPWL: bad number of subbands in Sqcx (%d)\n",
3232 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3235 /* we try to correct */
3237 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3238 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3245 /* We check whether there are too many subbands */
3246 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3247 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3248 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3249 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3252 #endif /* USE_JPWL */
3254 for (bandno = 0; bandno < numbands; bandno++) {
3256 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3257 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3260 tmp = cio_read(cio, 2); /* SPqcx_i */
3264 if (bandno < J2K_MAXBANDS){
3265 tccp->stepsizes[bandno].expn = expn;
3266 tccp->stepsizes[bandno].mant = mant;
3270 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3271 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3272 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3273 tccp->stepsizes[bandno].expn =
3274 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3275 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3276 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3282 static void j2k_write_qcd(opj_j2k_t *j2k) {
3285 opj_cio_t *cio = j2k->cio;
3287 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3288 lenp = cio_tell(cio);
3290 j2k_write_qcx(j2k, 0);
3291 len = cio_tell(cio) - lenp;
3292 cio_seek(cio, lenp);
3293 cio_write(cio, len, 2); /* Lqcd */
3294 cio_seek(cio, lenp + len);
3297 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3301 * Writes the QCD marker (quantization default)
3303 * @param p_comp_number the index of the component to output.
3304 * @param p_stream the stream to write data to.
3305 * @param p_j2k J2K codec.
3306 * @param p_manager the user event manager.
3308 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3309 struct opj_stream_private *p_stream,
3310 struct opj_event_mgr * p_manager )
3312 opj_cp_v2_t *l_cp = 00;
3313 opj_tcp_v2_t *l_tcp = 00;
3314 OPJ_UINT32 l_qcd_size,l_remaining_size;
3315 OPJ_BYTE * l_current_data = 00;
3318 assert(p_j2k != 00);
3319 assert(p_manager != 00);
3320 assert(p_stream != 00);
3322 l_cp = &(p_j2k->m_cp);
3323 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3324 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3325 l_remaining_size = l_qcd_size;
3327 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3328 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3329 = (OPJ_BYTE*)opj_realloc(
3330 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3333 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3337 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3340 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3342 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3343 l_current_data += 2;
3345 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3346 l_current_data += 2;
3348 l_remaining_size -= 4;
3350 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3351 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3355 if (l_remaining_size != 0) {
3356 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3360 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) {
3368 static void j2k_read_qcd(opj_j2k_t *j2k) {
3372 opj_cio_t *cio = j2k->cio;
3373 opj_image_t *image = j2k->image;
3375 len = cio_read(cio, 2); /* Lqcd */
3376 pos = cio_tell(cio);
3377 for (i = 0; i < image->numcomps; i++) {
3379 j2k_read_qcx(j2k, i, len - 2);
3384 * Reads a QCD marker (Quantization defaults)
3385 * @param p_header_data the data contained in the QCD box.
3386 * @param p_j2k the jpeg2000 codec.
3387 * @param p_header_size the size of the data contained in the QCD marker.
3388 * @param p_manager the user event manager.
3390 opj_bool j2k_read_qcd_v2 (
3391 opj_j2k_v2_t *p_j2k,
3392 OPJ_BYTE * p_header_data,
3393 OPJ_UINT32 p_header_size,
3394 struct opj_event_mgr * p_manager
3398 assert(p_header_data != 00);
3399 assert(p_j2k != 00);
3400 assert(p_manager != 00);
3402 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3403 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3407 if (p_header_size != 0) {
3408 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3412 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3413 j2k_copy_tile_quantization_parameters(p_j2k);
3418 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3421 opj_cio_t *cio = j2k->cio;
3423 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3424 lenp = cio_tell(cio);
3426 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3427 j2k_write_qcx(j2k, compno);
3428 len = cio_tell(cio) - lenp;
3429 cio_seek(cio, lenp);
3430 cio_write(cio, len, 2); /* Lqcc */
3431 cio_seek(cio, lenp + len);
3435 * Writes the QCC marker (quantization component)
3437 * @param p_comp_no the index of the component to output.
3438 * @param p_stream the stream to write data to.
3439 * @param p_j2k J2K codec.
3440 * @param p_manager the user event manager.
3442 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3443 OPJ_UINT32 p_comp_no,
3444 struct opj_stream_private *p_stream,
3445 struct opj_event_mgr * p_manager )
3447 OPJ_UINT32 l_qcc_size,l_remaining_size;
3450 assert(p_j2k != 00);
3451 assert(p_manager != 00);
3452 assert(p_stream != 00);
3454 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3455 l_remaining_size = l_qcc_size;
3457 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3458 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3459 = (OPJ_BYTE*)opj_realloc(
3460 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3462 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3466 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3469 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);
3471 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) {
3479 * Writes the QCC marker (quantization component)
3481 * @param p_comp_no the index of the component to output.
3482 * @param p_stream the stream to write data to.
3483 * @param p_j2k J2K codec.
3484 * @param p_manager the user event manager.
3486 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3487 OPJ_UINT32 p_comp_no,
3489 OPJ_UINT32 * p_data_written,
3490 struct opj_event_mgr * p_manager )
3492 OPJ_UINT32 l_qcc_size,l_remaining_size;
3493 OPJ_BYTE * l_current_data = 00;
3496 assert(p_j2k != 00);
3497 assert(p_manager != 00);
3499 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3500 l_remaining_size = l_qcc_size;
3502 l_current_data = p_data;
3504 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3505 l_current_data += 2;
3507 if (p_j2k->m_private_image->numcomps <= 256) {
3510 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3511 l_current_data += 2;
3513 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3516 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3517 l_remaining_size -= 6;
3520 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3521 l_current_data += 2;
3523 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3526 l_remaining_size -= 6;
3529 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3531 *p_data_written = l_qcc_size;
3535 * Gets the maximum size taken by a qcc.
3537 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3539 return j2k_get_max_coc_size(p_j2k);
3542 static void j2k_read_qcc(opj_j2k_t *j2k) {
3544 int numcomp = j2k->image->numcomps;
3545 opj_cio_t *cio = j2k->cio;
3547 len = cio_read(cio, 2); /* Lqcc */
3548 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3551 if (j2k->cp->correct) {
3553 static int backup_compno = 0;
3555 /* compno is negative or larger than the number of components!!! */
3556 if ((compno < 0) || (compno >= numcomp)) {
3557 opj_event_msg(j2k->cinfo, EVT_ERROR,
3558 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3561 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3564 /* we try to correct */
3565 compno = backup_compno % numcomp;
3566 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3567 "- setting component number to %d\n",
3571 /* keep your private count of tiles */
3574 #endif /* USE_JPWL */
3576 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3580 * Reads a QCC marker (Quantization component)
3581 * @param p_header_data the data contained in the QCC box.
3582 * @param p_j2k the jpeg2000 codec.
3583 * @param p_header_size the size of the data contained in the QCC marker.
3584 * @param p_manager the user event manager.
3586 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3587 OPJ_BYTE * p_header_data,
3588 OPJ_UINT32 p_header_size,
3589 struct opj_event_mgr * p_manager)
3591 OPJ_UINT32 l_num_comp,l_comp_no;
3594 assert(p_header_data != 00);
3595 assert(p_j2k != 00);
3596 assert(p_manager != 00);
3598 l_num_comp = p_j2k->m_private_image->numcomps;
3600 if (l_num_comp <= 256) {
3601 if (p_header_size < 1) {
3602 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3605 opj_read_bytes(p_header_data,&l_comp_no,1);
3610 if (p_header_size < 2) {
3611 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3614 opj_read_bytes(p_header_data,&l_comp_no,2);
3620 if (p_j2k->m_cp.correct) {
3622 static OPJ_UINT32 backup_compno = 0;
3624 /* compno is negative or larger than the number of components!!! */
3625 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3626 opj_event_msg_v2(p_manager, EVT_ERROR,
3627 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3628 l_comp_no, l_num_comp);
3630 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3633 /* we try to correct */
3634 l_comp_no = backup_compno % l_num_comp;
3635 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3636 "- setting component number to %d\n",
3640 /* keep your private count of tiles */
3643 #endif /* USE_JPWL */
3645 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3646 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3650 if (p_header_size != 0) {
3651 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3659 static void j2k_write_poc(opj_j2k_t *j2k) {
3660 int len, numpchgs, i;
3662 int numcomps = j2k->image->numcomps;
3664 opj_cp_t *cp = j2k->cp;
3665 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3666 opj_tccp_t *tccp = &tcp->tccps[0];
3667 opj_cio_t *cio = j2k->cio;
3669 numpchgs = 1 + tcp->numpocs;
3670 cio_write(cio, J2K_MS_POC, 2); /* POC */
3671 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3672 cio_write(cio, len, 2); /* Lpoc */
3673 for (i = 0; i < numpchgs; i++) {
3674 opj_poc_t *poc = &tcp->pocs[i];
3675 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3676 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3677 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3678 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3679 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3680 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3681 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3682 poc->compno1 = int_min(poc->compno1, numcomps);
3683 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3688 * Writes the POC marker (Progression Order Change)
3690 * @param p_stream the stream to write data to.
3691 * @param p_j2k J2K codec.
3692 * @param p_manager the user event manager.
3694 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3695 struct opj_stream_private *p_stream,
3696 struct opj_event_mgr * p_manager )
3698 OPJ_UINT32 l_nb_comp;
3699 OPJ_UINT32 l_nb_poc;
3700 OPJ_UINT32 l_poc_size;
3701 OPJ_UINT32 l_written_size = 0;
3702 opj_tcp_v2_t *l_tcp = 00;
3703 opj_tccp_t *l_tccp = 00;
3704 OPJ_UINT32 l_poc_room;
3707 assert(p_j2k != 00);
3708 assert(p_manager != 00);
3709 assert(p_stream != 00);
3711 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3712 l_tccp = &l_tcp->tccps[0];
3713 l_nb_comp = p_j2k->m_private_image->numcomps;
3714 l_nb_poc = 1 + l_tcp->numpocs;
3716 if (l_nb_comp <= 256) {
3722 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3724 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3725 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3726 = (OPJ_BYTE*)opj_realloc(
3727 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3730 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3734 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3737 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3739 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) {
3748 * Writes the POC marker (Progression Order Change)
3750 * @param p_stream the stream to write data to.
3751 * @param p_j2k J2K codec.
3752 * @param p_manager the user event manager.
3754 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3756 OPJ_UINT32 * p_data_written,
3757 struct opj_event_mgr * p_manager )
3760 OPJ_BYTE * l_current_data = 00;
3761 OPJ_UINT32 l_nb_comp;
3762 OPJ_UINT32 l_nb_poc;
3763 OPJ_UINT32 l_poc_size;
3764 opj_image_t *l_image = 00;
3765 opj_tcp_v2_t *l_tcp = 00;
3766 opj_tccp_t *l_tccp = 00;
3767 opj_poc_t *l_current_poc = 00;
3768 OPJ_UINT32 l_poc_room;
3771 assert(p_j2k != 00);
3772 assert(p_manager != 00);
3774 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3775 l_tccp = &l_tcp->tccps[0];
3776 l_image = p_j2k->m_private_image;
3777 l_nb_comp = l_image->numcomps;
3778 l_nb_poc = 1 + l_tcp->numpocs;
3780 if (l_nb_comp <= 256) {
3787 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3789 l_current_data = p_data;
3791 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3792 l_current_data += 2;
3794 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3795 l_current_data += 2;
3797 l_current_poc = l_tcp->pocs;
3798 for (i = 0; i < l_nb_poc; ++i) {
3799 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3802 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3803 l_current_data+=l_poc_room;
3805 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3808 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3811 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3812 l_current_data+=l_poc_room;
3814 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3817 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3818 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3819 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3820 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3825 *p_data_written = l_poc_size;
3829 * Gets the maximum size taken by the writing of a POC.
3831 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3833 opj_tcp_v2_t * l_tcp = 00;
3834 OPJ_UINT32 l_nb_tiles = 0;
3835 OPJ_UINT32 l_max_poc = 0;
3838 l_tcp = p_j2k->m_cp.tcps;
3839 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3841 for (i=0;i<l_nb_tiles;++i) {
3842 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3848 return 4 + 9 * l_max_poc;
3852 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3854 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3857 OPJ_UINT32 l_nb_tiles;
3858 OPJ_UINT32 l_max = 0;
3859 opj_tcp_v2_t * l_tcp = 00;
3861 l_tcp = p_j2k->m_cp.tcps;
3862 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3864 for (i=0;i<l_nb_tiles;++i) {
3865 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3875 * Gets the maximum size taken by the headers of the SOT.
3877 * @param p_j2k the jpeg2000 codec to use.
3879 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3881 OPJ_UINT32 l_nb_bytes = 0;
3882 OPJ_UINT32 l_nb_comps;
3883 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3885 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3886 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3888 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3889 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3890 l_nb_bytes += l_nb_comps * l_coc_bytes;
3892 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3893 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3896 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3898 /*** DEVELOPER CORNER, Add room for your headers ***/
3903 static void j2k_read_poc(opj_j2k_t *j2k) {
3904 int len, numpchgs, i, old_poc;
3906 int numcomps = j2k->image->numcomps;
3908 opj_cp_t *cp = j2k->cp;
3909 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3910 opj_cio_t *cio = j2k->cio;
3912 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
3914 len = cio_read(cio, 2); /* Lpoc */
3915 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
3917 for (i = old_poc; i < numpchgs + old_poc; i++) {
3919 poc = &tcp->pocs[i];
3920 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
3921 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
3922 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
3923 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
3924 poc->compno1 = int_min(
3925 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
3926 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
3929 tcp->numpocs = numpchgs + old_poc - 1;
3933 * Reads a POC marker (Progression Order Change)
3935 * @param p_header_data the data contained in the POC box.
3936 * @param p_j2k the jpeg2000 codec.
3937 * @param p_header_size the size of the data contained in the POC marker.
3938 * @param p_manager the user event manager.
3940 opj_bool j2k_read_poc_v2 (
3941 opj_j2k_v2_t *p_j2k,
3942 OPJ_BYTE * p_header_data,
3943 OPJ_UINT32 p_header_size,
3944 struct opj_event_mgr * p_manager)
3946 OPJ_UINT32 i, l_nb_comp, l_tmp;
3947 opj_image_t * l_image = 00;
3948 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3949 OPJ_UINT32 l_chunk_size, l_comp_room;
3951 opj_cp_v2_t *l_cp = 00;
3952 opj_tcp_v2_t *l_tcp = 00;
3953 opj_poc_t *l_current_poc = 00;
3956 assert(p_header_data != 00);
3957 assert(p_j2k != 00);
3958 assert(p_manager != 00);
3960 l_image = p_j2k->m_private_image;
3961 l_nb_comp = l_image->numcomps;
3962 if (l_nb_comp <= 256) {
3968 l_chunk_size = 5 + 2 * l_comp_room;
3969 l_current_poc_nb = p_header_size / l_chunk_size;
3970 l_current_poc_remaining = p_header_size % l_chunk_size;
3972 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3973 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3977 l_cp = &(p_j2k->m_cp);
3978 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3979 &l_cp->tcps[p_j2k->m_current_tile_number] :
3980 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3981 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3982 l_current_poc_nb += l_old_poc_nb;
3984 assert(l_current_poc_nb < 32);
3986 /* now poc is in use.*/
3989 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3990 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3991 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3993 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3994 p_header_data+=l_comp_room;
3995 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3997 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3999 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
4000 p_header_data+=l_comp_room;
4001 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
4003 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4004 /* make sure comp is in acceptable bounds */
4005 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4009 l_tcp->numpocs = l_current_poc_nb - 1;
4013 static void j2k_read_crg(opj_j2k_t *j2k) {
4014 int len, i, Xcrg_i, Ycrg_i;
4016 opj_cio_t *cio = j2k->cio;
4017 int numcomps = j2k->image->numcomps;
4019 len = cio_read(cio, 2); /* Lcrg */
4020 for (i = 0; i < numcomps; i++) {
4021 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
4022 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4027 * Reads a CRG marker (Component registration)
4029 * @param p_header_data the data contained in the TLM box.
4030 * @param p_j2k the jpeg2000 codec.
4031 * @param p_header_size the size of the data contained in the TLM marker.
4032 * @param p_manager the user event manager.
4034 opj_bool j2k_read_crg_v2 (
4035 opj_j2k_v2_t *p_j2k,
4036 OPJ_BYTE * p_header_data,
4037 OPJ_UINT32 p_header_size,
4038 struct opj_event_mgr * p_manager
4041 OPJ_UINT32 l_nb_comp;
4043 assert(p_header_data != 00);
4044 assert(p_j2k != 00);
4045 assert(p_manager != 00);
4047 l_nb_comp = p_j2k->m_private_image->numcomps;
4049 if (p_header_size != l_nb_comp *4) {
4050 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4053 /* Do not care of this at the moment since only local variables are set here */
4056 (i = 0; i < l_nb_comp; ++i)
4058 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4060 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4067 static void j2k_read_tlm(opj_j2k_t *j2k) {
4068 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4069 long int Ttlm_i, Ptlm_i;
4071 opj_cio_t *cio = j2k->cio;
4073 len = cio_read(cio, 2); /* Ltlm */
4074 Ztlm = cio_read(cio, 1); /* Ztlm */
4075 Stlm = cio_read(cio, 1); /* Stlm */
4076 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4077 SP = (Stlm >> 6) & 0x01;
4078 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4079 for (i = 0; i < tile_tlm; i++) {
4080 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4081 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4086 * Reads a TLM marker (Tile Length Marker)
4088 * @param p_header_data the data contained in the TLM box.
4089 * @param p_j2k the jpeg2000 codec.
4090 * @param p_header_size the size of the data contained in the TLM marker.
4091 * @param p_manager the user event manager.
4093 opj_bool j2k_read_tlm_v2 (
4094 opj_j2k_v2_t *p_j2k,
4095 OPJ_BYTE * p_header_data,
4096 OPJ_UINT32 p_header_size,
4097 struct opj_event_mgr * p_manager
4100 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4102 assert(p_header_data != 00);
4103 assert(p_j2k != 00);
4104 assert(p_manager != 00);
4106 if (p_header_size < 2) {
4107 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4112 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4114 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4117 l_ST = ((l_Stlm >> 4) & 0x3);
4118 l_SP = (l_Stlm >> 6) & 0x1;
4120 l_Ptlm_size = (l_SP + 1) * 2;
4121 l_quotient = l_Ptlm_size + l_ST;
4123 l_tot_num_tp = p_header_size / l_quotient;
4124 l_tot_num_tp_remaining = p_header_size % l_quotient;
4126 if (l_tot_num_tp_remaining != 0) {
4127 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4130 /* FIXME Do not care of this at the moment since only local variables are set here */
4133 (i = 0; i < l_tot_num_tp; ++i)
4135 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4136 p_header_data += l_ST;
4137 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4138 p_header_data += l_Ptlm_size;
4143 static void j2k_read_plm(opj_j2k_t *j2k) {
4144 int len, i, Zplm, Nplm, add, packet_len = 0;
4146 opj_cio_t *cio = j2k->cio;
4148 len = cio_read(cio, 2); /* Lplm */
4149 Zplm = cio_read(cio, 1); /* Zplm */
4152 Nplm = cio_read(cio, 4); /* Nplm */
4154 for (i = Nplm; i > 0; i--) {
4155 add = cio_read(cio, 1);
4157 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4158 if ((add & 0x80) == 0) {
4169 * Reads a PLM marker (Packet length, main header marker)
4171 * @param p_header_data the data contained in the TLM box.
4172 * @param p_j2k the jpeg2000 codec.
4173 * @param p_header_size the size of the data contained in the TLM marker.
4174 * @param p_manager the user event manager.
4176 opj_bool j2k_read_plm_v2 (
4177 opj_j2k_v2_t *p_j2k,
4178 OPJ_BYTE * p_header_data,
4179 OPJ_UINT32 p_header_size,
4180 struct opj_event_mgr * p_manager
4184 assert(p_header_data != 00);
4185 assert(p_j2k != 00);
4186 assert(p_manager != 00);
4188 if (p_header_size < 1) {
4189 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4192 /* Do not care of this at the moment since only local variables are set here */
4194 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4201 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4203 p_header_size -= (1+l_Nplm);
4207 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4211 (i = 0; i < l_Nplm; ++i)
4213 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4215 // take only the last seven bytes
4216 l_packet_len |= (l_tmp & 0x7f);
4224 // store packet length and proceed to next packet
4231 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4239 static void j2k_read_plt(opj_j2k_t *j2k) {
4240 int len, i, Zplt, packet_len = 0, add;
4242 opj_cio_t *cio = j2k->cio;
4244 len = cio_read(cio, 2); /* Lplt */
4245 Zplt = cio_read(cio, 1); /* Zplt */
4246 for (i = len - 3; i > 0; i--) {
4247 add = cio_read(cio, 1);
4248 packet_len = (packet_len << 7) + add; /* Iplt_i */
4249 if ((add & 0x80) == 0) {
4257 * Reads a PLT marker (Packet length, tile-part header)
4259 * @param p_header_data the data contained in the PLT box.
4260 * @param p_j2k the jpeg2000 codec.
4261 * @param p_header_size the size of the data contained in the PLT marker.
4262 * @param p_manager the user event manager.
4264 opj_bool j2k_read_plt_v2 (
4265 opj_j2k_v2_t *p_j2k,
4266 OPJ_BYTE * p_header_data,
4267 OPJ_UINT32 p_header_size,
4268 struct opj_event_mgr * p_manager
4271 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4274 assert(p_header_data != 00);
4275 assert(p_j2k != 00);
4276 assert(p_manager != 00);
4278 if (p_header_size < 1) {
4279 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4283 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4287 for (i = 0; i < p_header_size; ++i) {
4288 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4290 /* take only the last seven bytes */
4291 l_packet_len |= (l_tmp & 0x7f);
4296 /* store packet length and proceed to next packet */
4301 if (l_packet_len != 0) {
4302 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4309 static void j2k_read_ppm(opj_j2k_t *j2k) {
4310 int len, Z_ppm, i, j;
4313 opj_cp_t *cp = j2k->cp;
4314 opj_cio_t *cio = j2k->cio;
4316 len = cio_read(cio, 2);
4319 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4322 if (cp->ppm_previous == 0) {
4323 N_ppm = cio_read(cio, 4); /* N_ppm */
4326 N_ppm = cp->ppm_previous;
4329 if (Z_ppm == 0) { /* First PPM marker */
4330 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4331 cp->ppm_data_first = cp->ppm_data;
4332 cp->ppm_len = N_ppm;
4333 } else { /* NON-first PPM marker */
4334 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4337 /* this memory allocation check could be done even in non-JPWL cases */
4339 if (!cp->ppm_data) {
4340 opj_event_msg(j2k->cinfo, EVT_ERROR,
4341 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4343 if (!JPWL_ASSUME || JPWL_ASSUME) {
4344 opj_free(cp->ppm_data);
4345 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4352 cp->ppm_data_first = cp->ppm_data;
4353 cp->ppm_len = N_ppm + cp->ppm_store;
4355 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4356 cp->ppm_data[j] = cio_read(cio, 1);
4360 break; /* Case of non-finished packet header in present marker but finished in next one */
4362 cp->ppm_previous = i - 1;
4367 * Reads a PPM marker (Packed packet headers, main header)
4369 * @param p_header_data the data contained in the POC box.
4370 * @param p_j2k the jpeg2000 codec.
4371 * @param p_header_size the size of the data contained in the POC marker.
4372 * @param p_manager the user event manager.
4375 opj_bool j2k_read_ppm_v2 (
4376 opj_j2k_v2_t *p_j2k,
4377 OPJ_BYTE * p_header_data,
4378 OPJ_UINT32 p_header_size,
4379 struct opj_event_mgr * p_manager
4383 opj_cp_v2_t *l_cp = 00;
4384 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4387 assert(p_header_data != 00);
4388 assert(p_j2k != 00);
4389 assert(p_manager != 00);
4391 if (p_header_size < 1) {
4392 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4396 l_cp = &(p_j2k->m_cp);
4399 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4403 /* First PPM marker */
4405 if (p_header_size < 4) {
4406 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4410 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4414 /* First PPM marker: Initialization */
4415 l_cp->ppm_len = l_N_ppm;
4416 l_cp->ppm_data_size = 0;
4418 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4419 if (l_cp->ppm_buffer == 00) {
4420 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4423 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4425 l_cp->ppm_data = l_cp->ppm_buffer;
4429 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4430 if (p_header_size >= 4) {
4432 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4435 l_cp->ppm_len += l_N_ppm ;
4437 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4438 if (l_cp->ppm_buffer == 00) {
4439 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4442 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4444 l_cp->ppm_data = l_cp->ppm_buffer;
4451 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4453 if (l_remaining_data <= p_header_size) {
4454 /* we must store less information than available in the packet */
4455 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4456 l_cp->ppm_data_size = l_cp->ppm_len;
4457 p_header_size -= l_remaining_data;
4458 p_header_data += l_remaining_data;
4461 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4462 l_cp->ppm_data_size += p_header_size;
4463 p_header_data += p_header_size;
4476 * Reads a PPM marker (Packed packet headers, main header)
4478 * @param p_header_data the data contained in the POC box.
4479 * @param p_j2k the jpeg2000 codec.
4480 * @param p_header_size the size of the data contained in the POC marker.
4481 * @param p_manager the user event manager.
4483 opj_bool j2k_read_ppm_v3 (
4484 opj_j2k_v2_t *p_j2k,
4485 OPJ_BYTE * p_header_data,
4486 OPJ_UINT32 p_header_size,
4487 struct opj_event_mgr * p_manager
4490 opj_cp_v2_t *l_cp = 00;
4491 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4494 assert(p_header_data != 00);
4495 assert(p_j2k != 00);
4496 assert(p_manager != 00);
4498 /* Minimum size of PPM marker is equal to the size of Zppm element */
4499 if (p_header_size < 1) {
4500 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4504 l_cp = &(p_j2k->m_cp);
4507 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4511 /* First PPM marker */
4513 /* We need now at least the Nppm^0 element */
4514 if (p_header_size < 4) {
4515 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4519 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4523 /* First PPM marker: Initialization */
4524 l_cp->ppm_len = l_N_ppm;
4525 l_cp->ppm_data_read = 0;
4527 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4528 if (l_cp->ppm_data == 00) {
4529 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4532 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4534 l_cp->ppm_data_current = l_cp->ppm_data;
4536 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4539 if (p_header_size < 4) {
4540 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4544 /* Uncompleted Ippm series in the previous PPM marker?*/
4545 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4546 /* Get the place where add the remaining Ippm series*/
4547 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4548 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4551 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4555 /* Increase the size of ppm_data to add the new Ippm series*/
4556 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4558 /* Keep the position of the place where concatenate the new series*/
4559 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4560 l_cp->ppm_len += l_N_ppm;
4565 l_remaining_data = p_header_size;
4567 while (l_remaining_data >= l_N_ppm) {
4568 /* read a complete Ippm series*/
4569 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4570 p_header_size -= l_N_ppm;
4571 p_header_data += l_N_ppm;
4573 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4577 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4582 l_remaining_data = p_header_size;
4586 l_remaining_data = p_header_size;
4588 /* Next Ippm series is a complete series ?*/
4589 if (l_remaining_data > l_N_ppm) {
4590 /* Increase the size of ppm_data to add the new Ippm series*/
4591 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4593 /* Keep the position of the place where concatenate the new series */
4594 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4595 l_cp->ppm_len += l_N_ppm;
4600 /* Need to read an incomplete Ippm series*/
4601 if (l_remaining_data) {
4602 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4604 /* Keep the position of the place where concatenate the new series*/
4605 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4606 l_cp->ppm_len += l_N_ppm;
4608 /* Read incomplete Ippm series*/
4609 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4610 p_header_size -= l_remaining_data;
4611 p_header_data += l_remaining_data;
4613 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4618 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4619 if (p_header_size >= 4) {
4621 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4624 l_cp->ppm_len += l_N_ppm ;
4626 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4627 if (l_cp->ppm_buffer == 00) {
4628 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4631 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4633 l_cp->ppm_data = l_cp->ppm_buffer;
4640 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4642 if (l_remaining_data <= p_header_size) {
4643 /* we must store less information than available in the packet */
4644 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4645 l_cp->ppm_data_size = l_cp->ppm_len;
4646 p_header_size -= l_remaining_data;
4647 p_header_data += l_remaining_data;
4650 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4651 l_cp->ppm_data_size += p_header_size;
4652 p_header_data += p_header_size;
4661 static void j2k_read_ppt(opj_j2k_t *j2k) {
4662 int len, Z_ppt, i, j = 0;
4664 opj_cp_t *cp = j2k->cp;
4665 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4666 opj_cio_t *cio = j2k->cio;
4668 len = cio_read(cio, 2);
4669 Z_ppt = cio_read(cio, 1);
4671 if (Z_ppt == 0) { /* First PPT marker */
4672 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4673 tcp->ppt_data_first = tcp->ppt_data;
4675 tcp->ppt_len = len - 3;
4676 } else { /* NON-first PPT marker */
4677 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4678 tcp->ppt_data_first = tcp->ppt_data;
4679 tcp->ppt_len = len - 3 + tcp->ppt_store;
4682 for (i = len - 3; i > 0; i--) {
4683 tcp->ppt_data[j] = cio_read(cio, 1);
4690 * Reads a PPT marker (Packed packet headers, tile-part header)
4692 * @param p_header_data the data contained in the PPT box.
4693 * @param p_j2k the jpeg2000 codec.
4694 * @param p_header_size the size of the data contained in the PPT marker.
4695 * @param p_manager the user event manager.
4697 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4698 OPJ_BYTE * p_header_data,
4699 OPJ_UINT32 p_header_size,
4700 struct opj_event_mgr * p_manager )
4702 opj_cp_v2_t *l_cp = 00;
4703 opj_tcp_v2_t *l_tcp = 00;
4707 assert(p_header_data != 00);
4708 assert(p_j2k != 00);
4709 assert(p_manager != 00);
4711 /* We need to have the Z_ppt element at minimum */
4712 if (p_header_size < 1) {
4713 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4717 l_cp = &(p_j2k->m_cp);
4719 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");
4723 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4726 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4730 /* Allocate buffer to read the packet header */
4732 /* First PPT marker */
4733 l_tcp->ppt_data_size = 0;
4734 l_tcp->ppt_len = p_header_size;
4736 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4737 if (l_tcp->ppt_buffer == 00) {
4738 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4741 l_tcp->ppt_data = l_tcp->ppt_buffer;
4743 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4746 l_tcp->ppt_len += p_header_size;
4748 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4749 if (l_tcp->ppt_buffer == 00) {
4750 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4753 l_tcp->ppt_data = l_tcp->ppt_buffer;
4755 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4758 /* Read packet header from buffer */
4759 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4761 l_tcp->ppt_data_size += p_header_size;
4767 * Writes the TLM marker (Tile Length Marker)
4769 * @param p_stream the stream to write data to.
4770 * @param p_j2k J2K codec.
4771 * @param p_manager the user event manager.
4773 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4774 struct opj_stream_private *p_stream,
4775 struct opj_event_mgr * p_manager )
4777 OPJ_BYTE * l_current_data = 00;
4778 OPJ_UINT32 l_tlm_size;
4781 assert(p_j2k != 00);
4782 assert(p_manager != 00);
4783 assert(p_stream != 00);
4785 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4787 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4788 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4789 = (OPJ_BYTE*)opj_realloc(
4790 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4793 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4797 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4800 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4802 /* change the way data is written to avoid seeking if possible */
4804 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4806 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4807 l_current_data += 2;
4809 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4810 l_current_data += 2;
4812 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4815 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4818 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4819 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) {
4826 static void j2k_write_tlm(opj_j2k_t *j2k){
4828 opj_cio_t *cio = j2k->cio;
4829 j2k->tlm_start = cio_tell(cio);
4830 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4831 lenp = 4 + (5*j2k->totnum_tp);
4832 cio_write(cio,lenp,2); /* Ltlm */
4833 cio_write(cio, 0,1); /* Ztlm=0*/
4834 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4835 cio_skip(cio,5*j2k->totnum_tp);
4838 static void j2k_write_sot(opj_j2k_t *j2k) {
4841 opj_cio_t *cio = j2k->cio;
4843 j2k->sot_start = cio_tell(cio);
4844 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4845 lenp = cio_tell(cio);
4846 cio_skip(cio, 2); /* Lsot (further) */
4847 cio_write(cio, j2k->curtileno, 2); /* Isot */
4848 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4849 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4850 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4851 len = cio_tell(cio) - lenp;
4852 cio_seek(cio, lenp);
4853 cio_write(cio, len, 2); /* Lsot */
4854 cio_seek(cio, lenp + len);
4858 /* update markers struct */
4859 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4860 #endif /* USE_JPWL */
4863 if( j2k->cstr_info && j2k->cur_tp_num==0){
4864 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4869 * Writes the SOT marker (Start of tile-part)
4871 * @param p_stream the stream to write data to.
4872 * @param p_j2k J2K codec.
4873 * @param p_manager the user event manager.
4875 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4877 OPJ_UINT32 * p_data_written,
4878 const struct opj_stream_private *p_stream,
4879 struct opj_event_mgr * p_manager )
4882 assert(p_j2k != 00);
4883 assert(p_manager != 00);
4884 assert(p_stream != 00);
4886 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4889 opj_write_bytes(p_data,10,2); /* Lsot */
4892 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4898 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4901 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4906 /* update markers struct */
4908 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4910 assert( 0 && "TODO" );
4911 #endif /* USE_JPWL */
4913 * p_data_written = 12;
4922 * Reads a PPT marker (Packed packet headers, tile-part header)
4924 * @param p_header_data the data contained in the PPT box.
4925 * @param p_j2k the jpeg2000 codec.
4926 * @param p_header_size the size of the data contained in the PPT marker.
4927 * @param p_manager the user event manager.
4929 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4930 OPJ_BYTE * p_header_data,
4931 OPJ_UINT32 p_header_size,
4932 opj_event_mgr_t * p_manager )
4934 opj_cp_v2_t *l_cp = 00;
4935 opj_tcp_v2_t *l_tcp = 00;
4936 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4937 OPJ_UINT32 l_current_part;
4938 OPJ_UINT32 l_tile_x,l_tile_y;
4941 assert(p_header_data != 00);
4942 assert(p_j2k != 00);
4943 assert(p_manager != 00);
4945 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4946 if (p_header_size != 8) {
4947 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4951 l_cp = &(p_j2k->m_cp);
4952 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4955 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4956 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4957 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4960 if (l_cp->correct) {
4962 int tileno = p_j2k->m_current_tile_number;
4963 static int backup_tileno = 0;
4965 /* tileno is negative or larger than the number of tiles!!! */
4966 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4967 opj_event_msg_v2(p_manager, EVT_ERROR,
4968 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4969 tileno, (l_cp->tw * l_cp->th));
4971 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4974 /* we try to correct */
4975 tileno = backup_tileno;
4976 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4977 "- setting tile number to %d\n",
4981 /* keep your private count of tiles */
4984 #endif /* USE_JPWL */
4986 /* look for the tile in the list of already processed tile (in parts). */
4987 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4988 /* since the time taken by this function can only grow at the time */
4990 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4993 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4994 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4996 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5002 if (l_cp->correct) {
5004 /* totlen is negative or larger than the bytes left!!! */
5005 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))) { */
5006 opj_event_msg_v2(p_manager, EVT_ERROR,
5007 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5008 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5010 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5013 /* we try to correct */
5015 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5016 "- setting Psot to %d => assuming it is the last tile\n",
5020 #endif /* USE_JPWL */
5022 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5024 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5025 "we assuming it is the last tile-part of the codestream.\n");
5026 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5029 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5032 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5035 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5036 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5037 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5038 if (l_tcp->m_nb_tile_parts) {
5039 if (l_current_part >= l_tcp->m_nb_tile_parts){
5040 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5041 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5042 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5046 l_tcp->m_nb_tile_parts = l_num_parts;
5049 /* If know the number of tile part header we will check if we didn't read the last*/
5050 if (l_tcp->m_nb_tile_parts) {
5051 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5052 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5056 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5057 /* Keep the size of data to skip after this marker */
5058 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5061 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5062 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5065 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5067 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5068 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5069 p_j2k->m_specific_param.m_decoder.m_skip_data =
5070 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5071 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5072 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5073 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5076 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5077 p_j2k->m_specific_param.m_decoder.m_skip_data =
5078 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5082 if (p_j2k->cstr_index)
5084 assert(p_j2k->cstr_index->tile_index != 00);
5085 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5086 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5088 if (l_num_parts != 0){
5089 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5090 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5093 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5094 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5095 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5097 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5098 (opj_tp_index_t*)opj_realloc(
5099 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5100 l_num_parts* sizeof(opj_tp_index_t));
5103 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5105 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5106 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5107 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5108 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5109 sizeof(opj_tp_index_t));
5112 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5113 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5114 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5115 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5116 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5117 * sizeof(opj_tp_index_t));
5126 /* 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 */
5127 /* if (p_j2k->cstr_info) {
5130 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5133 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5134 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5135 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5136 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5139 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5142 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5146 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5149 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5150 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5151 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5156 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5159 opj_tcp_t *tcp = NULL;
5160 opj_codestream_info_t *cstr_info = NULL;
5162 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5163 opj_cp_t *cp = j2k->cp;
5164 opj_cio_t *cio = j2k->cio;
5166 tcd->tp_num = j2k->tp_num ;
5167 tcd->cur_tp_num = j2k->cur_tp_num;
5169 cio_write(cio, J2K_MS_SOD, 2);
5171 if( j2k->cstr_info && j2k->cur_tp_num==0){
5172 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5175 if (j2k->curtileno == 0) {
5176 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5180 cstr_info = j2k->cstr_info;
5182 if (!j2k->cur_tp_num ) {
5183 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5184 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5187 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5188 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5192 /* update markers struct */
5193 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5194 #endif /* USE_JPWL */
5199 tcp = &cp->tcps[j2k->curtileno];
5200 for (layno = 0; layno < tcp->numlayers; layno++) {
5201 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5202 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5203 } else if (tcp->rates[layno]) {
5204 tcp->rates[layno]=1;
5207 if(j2k->cur_tp_num == 0){
5208 tcd->tcd_image->tiles->packno = 0;
5210 cstr_info->packno = 0;
5213 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5215 /* Writing Psot in SOT marker */
5216 totlen = cio_tell(cio) + l - j2k->sot_start;
5217 cio_seek(cio, j2k->sot_start + 6);
5218 cio_write(cio, totlen, 4);
5219 cio_seek(cio, j2k->sot_start + totlen);
5220 /* Writing Ttlm and Ptlm in TLM marker */
5222 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5223 cio_write(cio, j2k->curtileno, 1);
5224 cio_write(cio, totlen, 4);
5226 cio_seek(cio, j2k->sot_start + totlen);
5230 * Writes the SOD marker (Start of data)
5232 * @param p_stream the stream to write data to.
5233 * @param p_j2k J2K codec.
5234 * @param p_manager the user event manager.
5236 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5237 struct opj_tcd_v2 * p_tile_coder,
5239 OPJ_UINT32 * p_data_written,
5240 OPJ_UINT32 p_total_data_size,
5241 const struct opj_stream_private *p_stream,
5242 struct opj_event_mgr * p_manager )
5244 opj_tcp_v2_t *l_tcp = 00;
5245 opj_codestream_info_t *l_cstr_info = 00;
5246 opj_cp_v2_t *l_cp = 00;
5248 OPJ_UINT32 l_size_tile;
5249 OPJ_UINT32 l_remaining_data;
5252 assert(p_j2k != 00);
5253 assert(p_manager != 00);
5254 assert(p_stream != 00);
5256 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5259 /* make room for the EOF marker */
5260 l_remaining_data = p_total_data_size - 4;
5262 l_cp = &(p_j2k->m_cp);
5263 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5266 /* update tile coder */
5267 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5268 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5270 l_size_tile = l_cp->th * l_cp->tw;
5273 /* TODO mergeV2: check this part which use cstr_info */
5274 /*l_cstr_info = p_j2k->cstr_info;
5276 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5277 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5278 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5284 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5286 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5291 /* update markers struct */
5292 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5294 assert( 0 && "TODO" );
5295 #endif /* USE_JPWL */
5300 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5301 p_tile_coder->tcd_image->tiles->packno = 0;
5303 l_cstr_info->packno = 0;
5307 *p_data_written = 0;
5309 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)) {
5310 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5314 *p_data_written += 2;
5323 * Reads a SOD marker (Start Of Data)
5325 * @param p_header_data the data contained in the SOD box.
5326 * @param p_j2k the jpeg2000 codec.
5327 * @param p_header_size the size of the data contained in the SOD marker.
5328 * @param p_manager the user event manager.
5330 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
5331 opj_stream_private_t *p_stream,
5332 opj_event_mgr_t * p_manager
5335 OPJ_UINT32 l_current_read_size;
5336 opj_codestream_index_t * l_cstr_index = 00;
5337 OPJ_BYTE ** l_current_data = 00;
5338 opj_tcp_v2_t * l_tcp = 00;
5339 OPJ_UINT32 * l_tile_len = 00;
5342 assert(p_j2k != 00);
5343 assert(p_manager != 00);
5344 assert(p_stream != 00);
5346 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5348 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5349 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5350 // but we are in the last tile part,
5351 // so its result will fit on OPJ_UINT32 unless we find
5352 // a file with a single tile part of more than 4 GB...*/
5353 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5356 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5358 l_current_data = &(l_tcp->m_data);
5359 l_tile_len = &l_tcp->m_data_size;
5361 if (! *l_current_data) {
5362 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5365 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5368 if (*l_current_data == 00) {
5369 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5375 l_cstr_index = p_j2k->cstr_index;
5377 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5379 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5380 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5382 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5383 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5385 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5389 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5391 /*l_cstr_index->packno = 0;*/
5394 l_current_read_size = opj_stream_read_data( p_stream,
5395 *l_current_data + *l_tile_len,
5396 p_j2k->m_specific_param.m_decoder.m_sot_length,
5399 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5400 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5403 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5406 *l_tile_len += l_current_read_size;
5412 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5413 opj_cp_t *cp = j2k->cp;
5414 opj_tcp_t *tcp = &cp->tcps[tileno];
5415 opj_cio_t *cio = j2k->cio;
5416 int numcomps = j2k->image->numcomps;
5418 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5419 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5420 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5421 cio_write(cio, 0, 1); /* Srgn */
5422 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5426 * Writes the RGN marker (Region Of Interest)
5428 * @param p_tile_no the tile to output
5429 * @param p_comp_no the component to output
5430 * @param p_stream the stream to write data to.
5431 * @param p_j2k J2K codec.
5432 * @param p_manager the user event manager.
5434 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5435 OPJ_UINT32 p_tile_no,
5436 OPJ_UINT32 p_comp_no,
5437 struct opj_stream_private *p_stream,
5438 struct opj_event_mgr * p_manager )
5440 OPJ_BYTE * l_current_data = 00;
5441 OPJ_UINT32 l_nb_comp;
5442 OPJ_UINT32 l_rgn_size;
5443 opj_image_t *l_image = 00;
5444 opj_cp_v2_t *l_cp = 00;
5445 opj_tcp_v2_t *l_tcp = 00;
5446 opj_tccp_t *l_tccp = 00;
5447 OPJ_UINT32 l_comp_room;
5450 assert(p_j2k != 00);
5451 assert(p_manager != 00);
5452 assert(p_stream != 00);
5454 l_cp = &(p_j2k->m_cp);
5455 l_tcp = &l_cp->tcps[p_tile_no];
5456 l_tccp = &l_tcp->tccps[p_comp_no];
5458 l_nb_comp = l_image->numcomps;
5460 if (l_nb_comp <= 256) {
5467 l_rgn_size = 6 + l_comp_room;
5469 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5471 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5472 l_current_data += 2;
5474 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5475 l_current_data += 2;
5477 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5478 l_current_data+=l_comp_room;
5480 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5483 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5486 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) {
5493 static void j2k_read_rgn(opj_j2k_t *j2k) {
5494 int len, compno, roisty;
5496 opj_cp_t *cp = j2k->cp;
5497 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5498 opj_cio_t *cio = j2k->cio;
5499 int numcomps = j2k->image->numcomps;
5501 len = cio_read(cio, 2); /* Lrgn */
5502 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
5503 roisty = cio_read(cio, 1); /* Srgn */
5506 if (j2k->cp->correct) {
5507 /* totlen is negative or larger than the bytes left!!! */
5508 if (compno >= numcomps) {
5509 opj_event_msg(j2k->cinfo, EVT_ERROR,
5510 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5512 if (!JPWL_ASSUME || JPWL_ASSUME) {
5513 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5518 #endif /* USE_JPWL */
5520 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
5523 static void j2k_write_eoc(opj_j2k_t *j2k) {
5524 opj_cio_t *cio = j2k->cio;
5525 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5526 cio_write(cio, J2K_MS_EOC, 2);
5530 /* update markers struct */
5531 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5532 #endif /* USE_JPWL */
5537 * Writes the EOC marker (End of Codestream)
5539 * @param p_stream the stream to write data to.
5540 * @param p_j2k J2K codec.
5541 * @param p_manager the user event manager.
5543 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5544 struct opj_stream_private *p_stream,
5545 struct opj_event_mgr * p_manager )
5548 assert(p_j2k != 00);
5549 assert(p_manager != 00);
5550 assert(p_stream != 00);
5552 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5557 /* update markers struct */
5559 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5561 #endif /* USE_JPWL */
5563 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5567 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5576 * Reads a RGN marker (Region Of Interest)
5578 * @param p_header_data the data contained in the POC box.
5579 * @param p_j2k the jpeg2000 codec.
5580 * @param p_header_size the size of the data contained in the POC marker.
5581 * @param p_manager the user event manager.
5583 opj_bool j2k_read_rgn_v2 (
5584 opj_j2k_v2_t *p_j2k,
5585 OPJ_BYTE * p_header_data,
5586 OPJ_UINT32 p_header_size,
5587 struct opj_event_mgr * p_manager
5590 OPJ_UINT32 l_nb_comp;
5591 opj_image_t * l_image = 00;
5593 opj_cp_v2_t *l_cp = 00;
5594 opj_tcp_v2_t *l_tcp = 00;
5595 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5598 assert(p_header_data != 00);
5599 assert(p_j2k != 00);
5600 assert(p_manager != 00);
5602 l_image = p_j2k->m_private_image;
5603 l_nb_comp = l_image->numcomps;
5605 if (l_nb_comp <= 256) {
5610 if (p_header_size != 2 + l_comp_room) {
5611 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5615 l_cp = &(p_j2k->m_cp);
5616 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5617 &l_cp->tcps[p_j2k->m_current_tile_number] :
5618 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5620 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5621 p_header_data+=l_comp_room;
5622 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5626 if (l_cp->correct) {
5627 /* totlen is negative or larger than the bytes left!!! */
5628 if (l_comp_room >= l_nb_comp) {
5629 opj_event_msg_v2(p_manager, EVT_ERROR,
5630 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5631 l_comp_room, l_nb_comp);
5632 if (!JPWL_ASSUME || JPWL_ASSUME) {
5633 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5638 #endif /* USE_JPWL */
5640 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5647 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5649 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5652 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5659 * Updates the rates of the tcp.
5661 * @param p_stream the stream to write data to.
5662 * @param p_j2k J2K codec.
5663 * @param p_manager the user event manager.
5665 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5666 struct opj_stream_private *p_stream,
5667 struct opj_event_mgr * p_manager )
5669 opj_cp_v2_t * l_cp = 00;
5670 opj_image_t * l_image = 00;
5671 opj_tcp_v2_t * l_tcp = 00;
5672 opj_image_comp_t * l_img_comp = 00;
5675 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5676 OPJ_FLOAT32 * l_rates = 0;
5677 OPJ_FLOAT32 l_sot_remove;
5678 OPJ_UINT32 l_bits_empty, l_size_pixel;
5679 OPJ_UINT32 l_tile_size = 0;
5680 OPJ_UINT32 l_last_res;
5681 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5684 assert(p_j2k != 00);
5685 assert(p_manager != 00);
5686 assert(p_stream != 00);
5689 l_cp = &(p_j2k->m_cp);
5690 l_image = p_j2k->m_private_image;
5693 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5694 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5695 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5697 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5698 l_tp_stride_func = get_tp_stride;
5701 l_tp_stride_func = get_default_stride;
5704 for (i=0;i<l_cp->th;++i) {
5705 for (j=0;j<l_cp->tw;++j) {
5706 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5708 /* 4 borders of the tile rescale on the image if necessary */
5709 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5710 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5711 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5712 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5714 l_rates = l_tcp->rates;
5716 /* Modification of the RATE >> */
5718 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5720 ((*l_rates) * l_bits_empty)
5728 for (k = 1; k < l_tcp->numlayers; ++k) {
5730 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5732 ((*l_rates) * l_bits_empty)
5748 for (i=0;i<l_cp->th;++i) {
5749 for (j=0;j<l_cp->tw;++j) {
5750 l_rates = l_tcp->rates;
5753 *l_rates -= l_sot_remove;
5755 if (*l_rates < 30) {
5762 l_last_res = l_tcp->numlayers - 1;
5764 for (k = 1; k < l_last_res; ++k) {
5767 *l_rates -= l_sot_remove;
5769 if (*l_rates < *(l_rates - 1) + 10) {
5770 *l_rates = (*(l_rates - 1)) + 20;
5778 *l_rates -= (l_sot_remove + 2.f);
5780 if (*l_rates < *(l_rates - 1) + 10) {
5781 *l_rates = (*(l_rates - 1)) + 20;
5789 l_img_comp = l_image->comps;
5792 for (i=0;i<l_image->numcomps;++i) {
5793 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5795 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5803 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5805 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5807 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5808 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5809 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5810 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5814 if (l_cp->m_specific_param.m_enc.m_cinema) {
5815 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5816 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5817 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5821 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5822 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5828 static void j2k_read_eoc(opj_j2k_t *j2k) {
5830 opj_bool success = OPJ_FALSE;
5832 /* if packets should be decoded */
5833 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5834 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5835 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5836 for (i = 0; i < j2k->cp->tileno_size; i++) {
5837 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5838 if (j2k->cp->tileno[i] != -1)
5840 tileno = j2k->cp->tileno[i];
5841 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5842 opj_free(j2k->tile_data[tileno]);
5843 j2k->tile_data[tileno] = NULL;
5844 tcd_free_decode_tile(tcd, i);
5847 success = OPJ_FALSE;
5848 if (success == OPJ_FALSE) {
5849 j2k->state |= J2K_STATE_ERR;
5853 tcd_free_decode(tcd);
5856 /* if packets should not be decoded */
5858 for (i = 0; i < j2k->cp->tileno_size; i++) {
5859 tileno = j2k->cp->tileno[i];
5860 opj_free(j2k->tile_data[tileno]);
5861 j2k->tile_data[tileno] = NULL;
5864 if (j2k->state & J2K_STATE_ERR)
5865 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5867 j2k->state = J2K_STATE_MT;
5871 * Reads a EOC marker (End Of Codestream)
5873 * @param p_header_data the data contained in the SOD box.
5874 * @param p_j2k the jpeg2000 codec.
5875 * @param p_header_size the size of the data contained in the SOD marker.
5876 * @param p_manager the user event manager.
5879 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5880 struct opj_stream_private *p_stream,
5881 struct opj_event_mgr * p_manager )
5884 opj_tcd_v2_t * l_tcd = 00;
5885 OPJ_UINT32 l_nb_tiles;
5886 opj_tcp_v2_t * l_tcp = 00;
5890 assert(p_j2k != 00);
5891 assert(p_manager != 00);
5892 assert(p_stream != 00);
5894 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5895 l_tcp = p_j2k->m_cp.tcps;
5897 l_tcd = tcd_create_v2(OPJ_TRUE);
5899 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5903 for (i = 0; i < l_nb_tiles; ++i) {
5904 if (l_tcp->m_data) {
5905 if (! tcd_init_decode_tile(l_tcd, i)) {
5906 tcd_destroy_v2(l_tcd);
5907 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5911 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5915 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5920 j2k_tcp_destroy(l_tcp);
5924 tcd_destroy_v2(l_tcd);
5930 * Gets the offset of the header.
5932 * @param p_stream the stream to write data to.
5933 * @param p_j2k J2K codec.
5934 * @param p_manager the user event manager.
5936 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5937 struct opj_stream_private *p_stream,
5938 struct opj_event_mgr * p_manager )
5941 assert(p_j2k != 00);
5942 assert(p_manager != 00);
5943 assert(p_stream != 00);
5945 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5951 * Writes the MCT marker (Multiple Component Transform)
5953 * @param p_stream the stream to write data to.
5954 * @param p_j2k J2K codec.
5955 * @param p_manager the user event manager.
5957 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5958 struct opj_stream_private *p_stream,
5959 struct opj_event_mgr * p_manager )
5962 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5963 opj_mct_data_t * l_mct_record;
5964 opj_tcp_v2_t * l_tcp;
5967 assert(p_j2k != 00);
5968 assert(p_stream != 00);
5969 assert(p_manager != 00);
5971 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5975 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5976 l_mct_record = l_tcp->m_mct_records;
5978 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5980 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5987 l_mcc_record = l_tcp->m_mcc_records;
5989 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5991 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5998 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6006 * Writes the image components.
6008 * @param p_stream the stream to write data to.
6009 * @param p_j2k J2K codec.
6010 * @param p_manager the user event manager.
6012 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6013 struct opj_stream_private *p_stream,
6014 struct opj_event_mgr * p_manager )
6019 assert(p_j2k != 00);
6020 assert(p_manager != 00);
6021 assert(p_stream != 00);
6023 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6025 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6029 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6038 * Writes regions of interests.
6040 * @param p_stream the stream to write data to.
6041 * @param p_j2k J2K codec.
6042 * @param p_manager the user event manager.
6044 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
6045 struct opj_stream_private *p_stream,
6046 struct opj_event_mgr * p_manager )
6049 const opj_tccp_t *l_tccp = 00;
6052 assert(p_j2k != 00);
6053 assert(p_manager != 00);
6054 assert(p_stream != 00);
6056 l_tccp = p_j2k->m_cp.tcps->tccps;
6058 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6059 if (l_tccp->roishift) {
6061 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6075 * @param p_stream the stream to write data to.
6076 * @param p_j2k J2K codec.
6077 * @param p_manager the user event manager.
6079 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6080 struct opj_stream_private *p_stream,
6081 struct opj_event_mgr * p_manager )
6083 opj_codestream_index_t * l_cstr_index = 00;
6086 assert(p_j2k != 00);
6087 assert(p_manager != 00);
6088 assert(p_stream != 00);
6090 l_cstr_index = p_j2k->cstr_index;
6092 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6094 /* The following adjustment is done to adjust the codestream size */
6095 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6096 /* the first bunch of bytes is not in the codestream */
6097 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6102 /* preparation of JPWL marker segments */
6106 /* encode according to JPWL */
6107 jpwl_encode(p_j2k, p_stream, image);
6111 assert( 0 && "TODO" );
6112 #endif /* USE_JPWL */
6117 typedef struct opj_dec_mstabent {
6120 /** value of the state when the marker can appear */
6122 /** action linked to the marker */
6123 void (*handler) (opj_j2k_t *j2k);
6124 } opj_dec_mstabent_t;
6126 opj_dec_mstabent_t j2k_dec_mstab[] = {
6127 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
6128 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
6129 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
6130 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6131 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
6132 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
6133 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6134 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6135 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6136 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6137 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6138 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6139 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6140 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6141 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6142 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6144 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6145 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6148 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6149 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6150 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6151 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6152 #endif /* USE_JPWL */
6154 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6155 {J2K_MS_INSEC, 0, j2k_read_insec},
6156 #endif /* USE_JPSEC */
6158 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6161 static void j2k_read_unk(opj_j2k_t *j2k) {
6162 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6165 if (j2k->cp->correct) {
6167 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6168 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6169 id = cio_read(j2k->cio, 2);
6170 opj_event_msg(j2k->cinfo, EVT_ERROR,
6171 "JPWL: really don't know this marker %x\n",
6174 opj_event_msg(j2k->cinfo, EVT_ERROR,
6175 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6178 /* OK, activate this at your own risk!!! */
6179 /* we look for the marker at the minimum hamming distance from this */
6180 while (j2k_dec_mstab[m].id) {
6182 /* 1's where they differ */
6183 tmp_id = j2k_dec_mstab[m].id ^ id;
6185 /* compute the hamming distance between our id and the current */
6187 for (i = 0; i < 16; i++) {
6188 if ((tmp_id >> i) & 0x0001) {
6193 /* if current distance is smaller, set the minimum */
6194 if (cur_dist < min_dist) {
6195 min_dist = cur_dist;
6196 min_id = j2k_dec_mstab[m].id;
6199 /* jump to the next marker */
6203 /* do we substitute the marker? */
6204 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6205 opj_event_msg(j2k->cinfo, EVT_ERROR,
6206 "- marker %x is at distance %d from the read %x\n",
6207 min_id, min_dist, id);
6208 opj_event_msg(j2k->cinfo, EVT_ERROR,
6209 "- trying to substitute in place and crossing fingers!\n");
6210 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6211 cio_write(j2k->cio, min_id, 2);
6214 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6219 #endif /* USE_JPWL */
6224 * Reads an unknown marker
6226 * @param p_stream the stream object to read from.
6227 * @param p_j2k the jpeg2000 codec.
6228 * @param p_manager the user event manager.
6230 * @return true if the marker could be deduced.
6232 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6233 struct opj_stream_private *p_stream,
6234 OPJ_UINT32 *output_marker,
6235 struct opj_event_mgr * p_manager
6238 OPJ_UINT32 l_unknown_marker;
6239 const opj_dec_memory_marker_handler_t * l_marker_handler;
6240 OPJ_UINT32 l_size_unk = 2;
6243 assert(p_j2k != 00);
6244 assert(p_manager != 00);
6245 assert(p_stream != 00);
6247 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6250 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6251 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6252 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6256 /* read 2 bytes as the new marker ID*/
6257 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6259 if (!(l_unknown_marker < 0xff00)) {
6261 /* Get the marker handler from the marker ID*/
6262 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6264 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6265 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6269 if (l_marker_handler->id != J2K_MS_UNK) {
6270 /* Add the marker to the codestream index*/
6271 if (l_marker_handler->id != J2K_MS_SOT)
6272 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6273 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6275 break; /* next marker is known and well located */
6283 *output_marker = l_marker_handler->id ;
6289 Read the lookup table containing all the marker, status and action
6290 @param id Marker value
6292 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6293 opj_dec_mstabent_t *e;
6294 for (e = j2k_dec_mstab; e->id != 0; e++) {
6303 * Writes the MCT marker (Multiple Component Transform)
6305 * @param p_stream the stream to write data to.
6306 * @param p_j2k J2K codec.
6307 * @param p_manager the user event manager.
6309 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6310 opj_mct_data_t * p_mct_record,
6311 struct opj_stream_private *p_stream,
6312 struct opj_event_mgr * p_manager )
6314 OPJ_UINT32 l_mct_size;
6315 OPJ_BYTE * l_current_data = 00;
6319 assert(p_j2k != 00);
6320 assert(p_manager != 00);
6321 assert(p_stream != 00);
6323 l_mct_size = 10 + p_mct_record->m_data_size;
6325 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6326 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6327 = (OPJ_BYTE*)opj_realloc(
6328 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6331 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6335 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6338 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6340 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6341 l_current_data += 2;
6343 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6344 l_current_data += 2;
6346 opj_write_bytes(l_current_data,0,2); /* Zmct */
6347 l_current_data += 2;
6349 /* only one marker atm */
6350 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6352 opj_write_bytes(l_current_data,l_tmp,2);
6353 l_current_data += 2;
6355 opj_write_bytes(l_current_data,0,2); /* Ymct */
6358 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6360 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) {
6368 * Reads a MCT marker (Multiple Component Transform)
6370 * @param p_header_data the data contained in the MCT box.
6371 * @param p_j2k the jpeg2000 codec.
6372 * @param p_header_size the size of the data contained in the MCT marker.
6373 * @param p_manager the user event manager.
6375 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6376 OPJ_BYTE * p_header_data,
6377 OPJ_UINT32 p_header_size,
6378 struct opj_event_mgr * p_manager )
6381 opj_tcp_v2_t *l_tcp = 00;
6384 opj_mct_data_t * l_mct_data;
6387 assert(p_header_data != 00);
6388 assert(p_j2k != 00);
6390 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6391 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6392 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6394 if (p_header_size < 2) {
6395 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6400 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6403 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6407 if(p_header_size <= 6) {
6408 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6412 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6413 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6416 l_indix = l_tmp & 0xff;
6417 l_mct_data = l_tcp->m_mct_records;
6419 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6420 if (l_mct_data->m_index == l_indix) {
6427 if (i == l_tcp->m_nb_mct_records) {
6428 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6429 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6431 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));
6432 if(! l_tcp->m_mct_records) {
6433 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6437 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6438 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6441 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6444 if (l_mct_data->m_data) {
6445 opj_free(l_mct_data->m_data);
6446 l_mct_data->m_data = 00;
6449 l_mct_data->m_index = l_indix;
6450 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6451 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6453 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6456 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6462 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6463 if (! l_mct_data->m_data) {
6464 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6467 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6469 l_mct_data->m_data_size = p_header_size;
6470 ++l_tcp->m_nb_mct_records;
6476 * Writes the MCC marker (Multiple Component Collection)
6478 * @param p_stream the stream to write data to.
6479 * @param p_j2k J2K codec.
6480 * @param p_manager the user event manager.
6482 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6483 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6484 struct opj_stream_private *p_stream,
6485 struct opj_event_mgr * p_manager )
6488 OPJ_UINT32 l_mcc_size;
6489 OPJ_BYTE * l_current_data = 00;
6490 OPJ_UINT32 l_nb_bytes_for_comp;
6495 assert(p_j2k != 00);
6496 assert(p_manager != 00);
6497 assert(p_stream != 00);
6499 if (p_mcc_record->m_nb_comps > 255 ) {
6500 l_nb_bytes_for_comp = 2;
6504 l_nb_bytes_for_comp = 1;
6508 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6509 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6511 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6512 = (OPJ_BYTE*)opj_realloc(
6513 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6515 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6519 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6522 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6524 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6525 l_current_data += 2;
6527 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6528 l_current_data += 2;
6531 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6532 l_current_data += 2;
6534 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6537 /* only one marker atm */
6538 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6541 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6544 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6547 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 */
6550 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6551 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6552 l_current_data+=l_nb_bytes_for_comp;
6555 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 */
6558 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6560 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6561 l_current_data+=l_nb_bytes_for_comp;
6564 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6566 if (p_mcc_record->m_decorrelation_array) {
6567 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6570 if (p_mcc_record->m_offset_array) {
6571 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6574 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6577 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) {
6585 * Reads a MCC marker (Multiple Component Collection)
6587 * @param p_header_data the data contained in the MCC box.
6588 * @param p_j2k the jpeg2000 codec.
6589 * @param p_header_size the size of the data contained in the MCC marker.
6590 * @param p_manager the user event manager.
6592 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6593 OPJ_BYTE * p_header_data,
6594 OPJ_UINT32 p_header_size,
6595 struct opj_event_mgr * p_manager )
6600 opj_tcp_v2_t * l_tcp;
6601 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6602 opj_mct_data_t * l_mct_data;
6603 OPJ_UINT32 l_nb_collections;
6604 OPJ_UINT32 l_nb_comps;
6605 OPJ_UINT32 l_nb_bytes_by_comp;
6609 assert(p_header_data != 00);
6610 assert(p_j2k != 00);
6611 assert(p_manager != 00);
6613 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6614 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6615 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6617 if (p_header_size < 2) {
6618 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6623 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6626 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6630 if (p_header_size < 7) {
6631 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6635 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6638 l_mcc_record = l_tcp->m_mcc_records;
6640 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6641 if (l_mcc_record->m_index == l_indix) {
6648 if (i == l_tcp->m_nb_mcc_records) {
6649 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6650 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6652 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6653 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6654 if (! l_tcp->m_mcc_records) {
6655 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6658 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6659 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));
6661 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6663 l_mcc_record->m_index = l_indix;
6665 /* only one marker atm */
6666 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6669 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6673 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6676 if (l_nb_collections > 1) {
6677 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6683 for (i=0;i<l_nb_collections;++i) {
6684 if (p_header_size < 3) {
6685 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6689 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6693 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6697 opj_read_bytes(p_header_data,&l_nb_comps,2);
6702 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6703 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6705 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6706 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6710 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6712 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6713 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6714 p_header_data+=l_nb_bytes_by_comp;
6717 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6722 opj_read_bytes(p_header_data,&l_nb_comps,2);
6725 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6726 l_nb_comps &= 0x7fff;
6728 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6729 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6733 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6734 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6738 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6740 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6741 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6742 p_header_data+=l_nb_bytes_by_comp;
6745 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6750 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6753 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6754 l_mcc_record->m_decorrelation_array = 00;
6755 l_mcc_record->m_offset_array = 00;
6757 l_indix = l_tmp & 0xff;
6759 l_mct_data = l_tcp->m_mct_records;
6760 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6761 if (l_mct_data->m_index == l_indix) {
6762 l_mcc_record->m_decorrelation_array = l_mct_data;
6768 if (l_mcc_record->m_decorrelation_array == 00) {
6769 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6774 l_indix = (l_tmp >> 8) & 0xff;
6776 l_mct_data = l_tcp->m_mct_records;
6777 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6778 if (l_mct_data->m_index == l_indix) {
6779 l_mcc_record->m_offset_array = l_mct_data;
6785 if (l_mcc_record->m_offset_array == 00) {
6786 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6792 if (p_header_size != 0) {
6793 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6797 ++l_tcp->m_nb_mcc_records;
6804 * Writes the MCO marker (Multiple component transformation ordering)
6806 * @param p_stream the stream to write data to.
6807 * @param p_j2k J2K codec.
6808 * @param p_manager the user event manager.
6810 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6811 struct opj_stream_private *p_stream,
6812 struct opj_event_mgr * p_manager
6815 OPJ_BYTE * l_current_data = 00;
6816 OPJ_UINT32 l_mco_size;
6817 opj_tcp_v2_t * l_tcp = 00;
6818 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6822 assert(p_j2k != 00);
6823 assert(p_manager != 00);
6824 assert(p_stream != 00);
6826 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6827 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6829 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6830 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6832 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6833 = (OPJ_BYTE*)opj_realloc(
6834 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6836 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6841 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6844 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6845 l_current_data += 2;
6847 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6848 l_current_data += 2;
6850 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6853 l_mcc_record = l_tcp->m_mcc_records;
6854 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6855 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6861 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) {
6869 * Reads a MCO marker (Multiple Component Transform Ordering)
6871 * @param p_header_data the data contained in the MCO box.
6872 * @param p_j2k the jpeg2000 codec.
6873 * @param p_header_size the size of the data contained in the MCO marker.
6874 * @param p_manager the user event manager.
6876 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6877 OPJ_BYTE * p_header_data,
6878 OPJ_UINT32 p_header_size,
6879 struct opj_event_mgr * p_manager )
6881 OPJ_UINT32 l_tmp, i;
6882 OPJ_UINT32 l_nb_stages;
6883 opj_tcp_v2_t * l_tcp;
6884 opj_tccp_t * l_tccp;
6885 opj_image_t * l_image;
6886 opj_image_comp_t * l_img_comp;
6889 assert(p_header_data != 00);
6890 assert(p_j2k != 00);
6891 assert(p_manager != 00);
6893 l_image = p_j2k->m_private_image;
6894 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6895 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6896 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6898 if (p_header_size < 1) {
6899 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6903 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6906 if (l_nb_stages > 1) {
6907 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6911 if (p_header_size != l_nb_stages + 1) {
6912 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6916 l_tccp = l_tcp->tccps;
6917 l_img_comp = l_image->comps;
6919 for (i=0;i<l_image->numcomps;++i) {
6920 l_tccp->m_dc_level_shift = 0;
6924 if (l_tcp->m_mct_decoding_matrix) {
6925 opj_free(l_tcp->m_mct_decoding_matrix);
6926 l_tcp->m_mct_decoding_matrix = 00;
6929 for (i=0;i<l_nb_stages;++i) {
6930 opj_read_bytes(p_header_data,&l_tmp,1);
6933 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6941 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6944 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6945 opj_mct_data_t * l_deco_array, * l_offset_array;
6946 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6947 OPJ_UINT32 l_nb_elem;
6948 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6949 opj_tccp_t * l_tccp;
6952 assert(p_tcp != 00);
6954 l_mcc_record = p_tcp->m_mcc_records;
6956 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6957 if (l_mcc_record->m_index == p_index) {
6962 if (i==p_tcp->m_nb_mcc_records) {
6963 /** element discarded **/
6967 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6968 /** do not support number of comps != image */
6972 l_deco_array = l_mcc_record->m_decorrelation_array;
6975 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6976 if (l_deco_array->m_data_size != l_data_size) {
6980 l_nb_elem = p_image->numcomps * p_image->numcomps;
6981 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6982 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6984 if (! p_tcp->m_mct_decoding_matrix ) {
6988 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);
6991 l_offset_array = l_mcc_record->m_offset_array;
6993 if (l_offset_array) {
6994 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6995 if (l_offset_array->m_data_size != l_data_size) {
6999 l_nb_elem = p_image->numcomps;
7000 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7001 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7003 if (! l_offset_data ) {
7007 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7009 l_tccp = p_tcp->tccps;
7010 l_current_offset_data = l_offset_data;
7012 for (i=0;i<p_image->numcomps;++i) {
7013 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7017 opj_free(l_offset_data);
7024 * Writes the CBD marker (Component bit depth definition)
7026 * @param p_stream the stream to write data to.
7027 * @param p_j2k J2K codec.
7028 * @param p_manager the user event manager.
7030 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7031 struct opj_stream_private *p_stream,
7032 struct opj_event_mgr * p_manager )
7035 OPJ_UINT32 l_cbd_size;
7036 OPJ_BYTE * l_current_data = 00;
7037 opj_image_t *l_image = 00;
7038 opj_image_comp_t * l_comp = 00;
7041 assert(p_j2k != 00);
7042 assert(p_manager != 00);
7043 assert(p_stream != 00);
7045 l_image = p_j2k->m_private_image;
7046 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7048 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7049 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7050 = (OPJ_BYTE*)opj_realloc(
7051 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7054 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7058 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7061 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7063 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7064 l_current_data += 2;
7066 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7067 l_current_data += 2;
7069 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7072 l_comp = l_image->comps;
7074 for (i=0;i<l_image->numcomps;++i) {
7075 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7081 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) {
7089 * Reads a CBD marker (Component bit depth definition)
7090 * @param p_header_data the data contained in the CBD box.
7091 * @param p_j2k the jpeg2000 codec.
7092 * @param p_header_size the size of the data contained in the CBD marker.
7093 * @param p_manager the user event manager.
7095 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7096 OPJ_BYTE * p_header_data,
7097 OPJ_UINT32 p_header_size,
7098 struct opj_event_mgr * p_manager)
7100 OPJ_UINT32 l_nb_comp,l_num_comp;
7101 OPJ_UINT32 l_comp_def;
7103 opj_image_comp_t * l_comp = 00;
7106 assert(p_header_data != 00);
7107 assert(p_j2k != 00);
7108 assert(p_manager != 00);
7110 l_num_comp = p_j2k->m_private_image->numcomps;
7112 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7113 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7117 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7120 if (l_nb_comp != l_num_comp) {
7121 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7125 l_comp = p_j2k->m_private_image->comps;
7126 for (i=0;i<l_num_comp;++i) {
7127 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7129 l_comp->sgnd = (l_comp_def>>7) & 1;
7130 l_comp->prec = (l_comp_def&0x7f) + 1;
7138 /* ----------------------------------------------------------------------- */
7139 /* J2K / JPT decoder interface */
7140 /* ----------------------------------------------------------------------- */
7144 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7147 if(j2k->tile_len != NULL) {
7148 opj_free(j2k->tile_len);
7150 if(j2k->tile_data != NULL) {
7151 opj_free(j2k->tile_data);
7153 if(j2k->default_tcp != NULL) {
7154 opj_tcp_t *default_tcp = j2k->default_tcp;
7155 if(default_tcp->ppt_data_first != NULL) {
7156 opj_free(default_tcp->ppt_data_first);
7158 if(j2k->default_tcp->tccps != NULL) {
7159 opj_free(j2k->default_tcp->tccps);
7161 opj_free(j2k->default_tcp);
7163 if(j2k->cp != NULL) {
7164 opj_cp_t *cp = j2k->cp;
7165 if(cp->tcps != NULL) {
7166 for(i = 0; i < cp->tw * cp->th; i++) {
7167 if(cp->tcps[i].ppt_data_first != NULL) {
7168 opj_free(cp->tcps[i].ppt_data_first);
7170 if(cp->tcps[i].tccps != NULL) {
7171 opj_free(cp->tcps[i].tccps);
7176 if(cp->ppm_data_first != NULL) {
7177 opj_free(cp->ppm_data_first);
7179 if(cp->tileno != NULL) {
7180 opj_free(cp->tileno);
7182 if(cp->comment != NULL) {
7183 opj_free(cp->comment);
7192 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7194 if(j2k && parameters) {
7195 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7196 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7199 j2k->m_cp.correct = parameters->jpwl_correct;
7200 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7201 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7202 #endif /* USE_JPWL */
7209 * Read a JPT-stream and decode file
7212 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7213 opj_image_t *image = NULL;
7214 opj_jpt_msg_header_t header;
7216 opj_common_ptr cinfo = j2k->cinfo;
7218 OPJ_ARG_NOT_USED(cstr_info);
7222 /* create an empty image */
7223 image = opj_image_create0();
7226 j2k->state = J2K_STATE_MHSOC;
7228 /* Initialize the header */
7229 jpt_init_msg_header(&header);
7230 /* Read the first header of the message */
7231 jpt_read_msg_header(cinfo, cio, &header);
7233 position = cio_tell(cio);
7234 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7235 opj_image_destroy(image);
7236 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7241 opj_dec_mstabent_t *e = NULL;
7244 if (!cio_numbytesleft(cio)) {
7248 /* data-bin read -> need to read a new header */
7249 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7250 jpt_read_msg_header(cinfo, cio, &header);
7251 position = cio_tell(cio);
7252 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7253 opj_image_destroy(image);
7254 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7259 id = cio_read(cio, 2);
7260 if (id >> 8 != 0xff) {
7261 opj_image_destroy(image);
7262 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7265 e = j2k_dec_mstab_lookup(id);
7266 if (!(j2k->state & e->states)) {
7267 opj_image_destroy(image);
7268 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7274 if (j2k->state == J2K_STATE_MT) {
7277 if (j2k->state == J2K_STATE_NEOC) {
7281 if (j2k->state == J2K_STATE_NEOC) {
7285 if (j2k->state != J2K_STATE_MT) {
7286 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7292 /* ----------------------------------------------------------------------- */
7293 /* J2K encoder interface */
7294 /* ----------------------------------------------------------------------- */
7296 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7297 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7304 opj_j2k_v2_t* j2k_create_compress_v2(void)
7306 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7311 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7313 l_j2k->m_is_decoder = 0;
7314 l_j2k->m_cp.m_is_decoder = 0;
7316 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7317 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7322 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7324 /* validation list creation*/
7325 l_j2k->m_validation_list = opj_procedure_list_create();
7326 if (! l_j2k->m_validation_list) {
7331 /* execution list creation*/
7332 l_j2k->m_procedure_list = opj_procedure_list_create();
7333 if (! l_j2k->m_procedure_list) {
7341 void j2k_destroy_compress(opj_j2k_t *j2k) {
7345 if(j2k->cp != NULL) {
7346 opj_cp_t *cp = j2k->cp;
7349 opj_free(cp->comment);
7352 opj_free(cp->matrice);
7354 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7355 opj_free(cp->tcps[tileno].tccps);
7364 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7366 int tileno, numpocs_tile;
7367 opj_cp_t *cp = NULL;
7369 if(!j2k || !parameters || ! image) {
7373 /* create and initialize the coding parameters structure */
7374 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7376 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7379 /* set default values for cp */
7384 copy user encoding parameters
7386 cp->cinema = parameters->cp_cinema;
7387 cp->max_comp_size = parameters->max_comp_size;
7388 cp->rsiz = parameters->cp_rsiz;
7389 cp->disto_alloc = parameters->cp_disto_alloc;
7390 cp->fixed_alloc = parameters->cp_fixed_alloc;
7391 cp->fixed_quality = parameters->cp_fixed_quality;
7393 /* mod fixed_quality */
7394 if(parameters->cp_matrice) {
7395 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7396 cp->matrice = (int *) opj_malloc(array_size);
7397 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7401 cp->tdx = parameters->cp_tdx;
7402 cp->tdy = parameters->cp_tdy;
7405 cp->tx0 = parameters->cp_tx0;
7406 cp->ty0 = parameters->cp_ty0;
7408 /* comment string */
7409 if(parameters->cp_comment) {
7410 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7412 strcpy(cp->comment, parameters->cp_comment);
7417 calculate other encoding parameters
7420 if (parameters->tile_size_on) {
7421 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7422 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7424 cp->tdx = image->x1 - cp->tx0;
7425 cp->tdy = image->y1 - cp->ty0;
7428 if(parameters->tp_on){
7429 cp->tp_flag = parameters->tp_flag;
7434 for(i=0;i<image->numcomps ;i++){
7435 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7441 calculate JPWL encoding parameters
7444 if (parameters->jpwl_epc_on) {
7448 cp->epc_on = OPJ_TRUE;
7449 cp->info_on = OPJ_FALSE; /* no informative technique */
7452 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7453 cp->epb_on = OPJ_TRUE;
7455 cp->hprot_MH = parameters->jpwl_hprot_MH;
7456 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7457 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7458 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7460 /* if tile specs are not specified, copy MH specs */
7461 if (cp->hprot_TPH[0] == -1) {
7462 cp->hprot_TPH_tileno[0] = 0;
7463 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7465 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7466 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7467 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7468 cp->pprot[i] = parameters->jpwl_pprot[i];
7472 /* set ESD writing */
7473 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7474 cp->esd_on = OPJ_TRUE;
7476 cp->sens_size = parameters->jpwl_sens_size;
7477 cp->sens_addr = parameters->jpwl_sens_addr;
7478 cp->sens_range = parameters->jpwl_sens_range;
7480 cp->sens_MH = parameters->jpwl_sens_MH;
7481 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7482 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7483 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7487 /* always set RED writing to false: we are at the encoder */
7488 cp->red_on = OPJ_FALSE;
7491 cp->epc_on = OPJ_FALSE;
7493 #endif /* USE_JPWL */
7496 /* initialize the mutiple tiles */
7497 /* ---------------------------- */
7498 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7500 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7501 opj_tcp_t *tcp = &cp->tcps[tileno];
7502 tcp->numlayers = parameters->tcp_numlayers;
7503 assert ( tcp->numlayers >= 0 );
7504 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7506 if (cp->fixed_quality) {
7507 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7509 tcp->rates[j] = parameters->tcp_rates[j];
7511 if (cp->fixed_quality) { /* add fixed_quality */
7512 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7514 tcp->rates[j] = parameters->tcp_rates[j];
7518 tcp->csty = parameters->csty;
7519 tcp->prg = parameters->prog_order;
7520 tcp->mct = parameters->tcp_mct;
7524 if (parameters->numpocs) {
7525 /* initialisation of POC */
7527 assert( parameters->numpocs >= 0 );
7528 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7529 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7530 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7531 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7532 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7533 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7534 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7535 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7536 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7537 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7541 tcp->numpocs = numpocs_tile -1 ;
7546 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7548 for (i = 0; i < image->numcomps; i++) {
7549 opj_tccp_t *tccp = &tcp->tccps[i];
7550 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7551 tccp->numresolutions = parameters->numresolution;
7552 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7553 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7554 tccp->cblksty = parameters->mode;
7555 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7556 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7558 assert(parameters->roi_compno >= 0);
7559 if (i == (OPJ_UINT32)parameters->roi_compno) {
7560 tccp->roishift = parameters->roi_shift;
7565 if(parameters->cp_cinema)
7567 /*Precinct size for lowest frequency subband=128*/
7570 /*Precinct size at all other resolutions = 256*/
7571 for (j = 1; j < tccp->numresolutions; j++) {
7576 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7578 assert(tccp->numresolutions > 0);
7579 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7580 if (p < parameters->res_spec) {
7582 if (parameters->prcw_init[p] < 1) {
7585 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7588 if (parameters->prch_init[p] < 1) {
7591 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7595 int res_spec = parameters->res_spec;
7596 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7597 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7599 if (size_prcw < 1) {
7602 tccp->prcw[j] = int_floorlog2(size_prcw);
7605 if (size_prch < 1) {
7608 tccp->prch[j] = int_floorlog2(size_prch);
7612 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7615 for (j = 0; j < tccp->numresolutions; j++) {
7622 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7627 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7628 opj_cparameters_t *parameters,
7630 struct opj_event_mgr * p_manager)
7632 OPJ_UINT32 i, j, tileno, numpocs_tile;
7633 opj_cp_v2_t *cp = 00;
7636 if(!p_j2k || !parameters || ! image) {
7640 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7641 cp = &(p_j2k->m_cp);
7643 /* set default values for cp */
7648 copy user encoding parameters
7650 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7651 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7652 cp->rsiz = parameters->cp_rsiz;
7653 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7654 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7655 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7657 /* mod fixed_quality */
7658 if (parameters->cp_matrice) {
7659 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7660 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7661 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7665 cp->tdx = parameters->cp_tdx;
7666 cp->tdy = parameters->cp_tdy;
7669 cp->tx0 = parameters->cp_tx0;
7670 cp->ty0 = parameters->cp_ty0;
7672 /* comment string */
7673 if(parameters->cp_comment) {
7674 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7676 strcpy(cp->comment, parameters->cp_comment);
7681 calculate other encoding parameters
7684 if (parameters->tile_size_on) {
7685 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7686 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7688 cp->tdx = image->x1 - cp->tx0;
7689 cp->tdy = image->y1 - cp->ty0;
7692 if (parameters->tp_on) {
7693 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7694 cp->m_specific_param.m_enc.m_tp_on = 1;
7699 calculate JPWL encoding parameters
7702 if (parameters->jpwl_epc_on) {
7706 cp->epc_on = OPJ_TRUE;
7707 cp->info_on = OPJ_FALSE; /* no informative technique */
7710 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7711 cp->epb_on = OPJ_TRUE;
7713 cp->hprot_MH = parameters->jpwl_hprot_MH;
7714 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7715 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7716 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7718 /* if tile specs are not specified, copy MH specs */
7719 if (cp->hprot_TPH[0] == -1) {
7720 cp->hprot_TPH_tileno[0] = 0;
7721 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7723 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7724 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7725 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7726 cp->pprot[i] = parameters->jpwl_pprot[i];
7730 /* set ESD writing */
7731 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7732 cp->esd_on = OPJ_TRUE;
7734 cp->sens_size = parameters->jpwl_sens_size;
7735 cp->sens_addr = parameters->jpwl_sens_addr;
7736 cp->sens_range = parameters->jpwl_sens_range;
7738 cp->sens_MH = parameters->jpwl_sens_MH;
7739 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7740 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7741 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7745 /* always set RED writing to false: we are at the encoder */
7746 cp->red_on = OPJ_FALSE;
7749 cp->epc_on = OPJ_FALSE;
7751 #endif /* USE_JPWL */
7754 /* initialize the mutiple tiles */
7755 /* ---------------------------- */
7756 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7757 if (parameters->numpocs) {
7758 /* initialisation of POC */
7759 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7763 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7764 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7765 tcp->numlayers = parameters->tcp_numlayers;
7767 for (j = 0; j < tcp->numlayers; j++) {
7768 if(cp->m_specific_param.m_enc.m_cinema){
7769 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7770 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7772 tcp->rates[j] = parameters->tcp_rates[j];
7774 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7775 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7777 tcp->rates[j] = parameters->tcp_rates[j];
7782 tcp->csty = parameters->csty;
7783 tcp->prg = parameters->prog_order;
7784 tcp->mct = parameters->tcp_mct;
7789 if (parameters->numpocs) {
7790 /* initialisation of POC */
7793 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7794 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7795 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7797 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7798 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7799 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7800 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7801 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7802 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7803 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7809 tcp->numpocs = numpocs_tile -1 ;
7814 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7816 if (parameters->mct_data) {
7818 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7821 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7822 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7823 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7824 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7827 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7828 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7829 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7831 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7832 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7834 tcp->mct_norms = (OPJ_FLOAT64*)
7835 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7837 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7840 for (i = 0; i < image->numcomps; i++) {
7841 opj_tccp_t *tccp = &tcp->tccps[i];
7842 tccp->m_dc_level_shift = l_dc_shift[i];
7845 j2k_setup_mct_encoding(tcp,image);
7849 for (i = 0; i < image->numcomps; i++) {
7850 opj_tccp_t *tccp = &tcp->tccps[i];
7851 opj_image_comp_t * l_comp = &(image->comps[i]);
7853 if (! l_comp->sgnd) {
7854 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7859 for (i = 0; i < image->numcomps; i++) {
7860 opj_tccp_t *tccp = &tcp->tccps[i];
7862 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7863 tccp->numresolutions = parameters->numresolution;
7864 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7865 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7866 tccp->cblksty = parameters->mode;
7867 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7868 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7871 if (i == parameters->roi_compno) {
7872 tccp->roishift = parameters->roi_shift;
7877 if(parameters->cp_cinema) {
7878 //Precinct size for lowest frequency subband=128
7881 //Precinct size at all other resolutions = 256
7882 for (j = 1; j < tccp->numresolutions; j++) {
7887 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7888 OPJ_INT32 p = 0, it_res;
7889 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7890 if (p < parameters->res_spec) {
7892 if (parameters->prcw_init[p] < 1) {
7893 tccp->prcw[it_res] = 1;
7895 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7898 if (parameters->prch_init[p] < 1) {
7899 tccp->prch[it_res] = 1;
7901 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7905 int res_spec = parameters->res_spec;
7906 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7907 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7909 if (size_prcw < 1) {
7910 tccp->prcw[it_res] = 1;
7912 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7915 if (size_prch < 1) {
7916 tccp->prch[it_res] = 1;
7918 tccp->prch[it_res] = int_floorlog2(size_prch);
7922 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7925 for (j = 0; j < tccp->numresolutions; j++) {
7932 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7936 if (parameters->mct_data) {
7937 opj_free(parameters->mct_data);
7938 parameters->mct_data = 00;
7943 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7946 opj_cp_t *cp = NULL;
7948 opj_tcd_t *tcd = NULL; /* TCD component */
7956 j2k->cstr_info = cstr_info;
7959 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7960 cstr_info->image_w = image->x1 - image->x0;
7961 cstr_info->image_h = image->y1 - image->y0;
7962 cstr_info->prog = (&cp->tcps[0])->prg;
7963 cstr_info->tw = cp->tw;
7964 cstr_info->th = cp->th;
7965 cstr_info->tile_x = cp->tdx; /* new version parser */
7966 cstr_info->tile_y = cp->tdy; /* new version parser */
7967 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7968 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7969 cstr_info->numcomps = image->numcomps;
7970 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7971 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7972 for (compno=0; compno < image->numcomps; compno++) {
7973 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7975 cstr_info->D_max = 0.0; /* ADD Marcela */
7976 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7977 cstr_info->maxmarknum = 100;
7978 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7979 cstr_info->marknum = 0;
7989 for (compno = 1; compno < image->numcomps; compno++) {
7990 j2k_write_coc(j2k, compno);
7991 j2k_write_qcc(j2k, compno);
7995 for (compno = 0; compno < image->numcomps; compno++) {
7996 opj_tcp_t *tcp = &cp->tcps[0];
7997 if (tcp->tccps[compno].roishift)
7998 j2k_write_rgn(j2k, compno, 0);
8000 if (cp->comment != NULL) {
8004 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
8008 if (cp->cinema == CINEMA4K_24) {
8013 /* uncomment only for testing JPSEC marker writing */
8014 /* j2k_write_sec(j2k); */
8018 cstr_info->main_head_end = cio_tell(cio) - 1;
8021 /**** Main Header ENDS here ***/
8023 /* create the tile encoder */
8024 tcd = tcd_create(j2k->cinfo);
8026 /* encode each tile */
8027 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8031 int acc_pack_num = 0;
8035 opj_tcp_t *tcp = &cp->tcps[tileno];
8036 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8038 j2k->curtileno = tileno;
8039 j2k->cur_tp_num = 0;
8040 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8041 /* initialisation before tile encoding */
8043 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8045 tcd_init_encode(tcd, image, cp, j2k->curtileno);
8050 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8051 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8052 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8053 cstr_info->tile[j2k->curtileno].marknum = 0;
8057 for(pino = 0; pino <= tcp->numpocs; pino++) {
8061 /*Get number of tile parts*/
8062 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8063 tcd->tp_pos = cp->tp_pos;
8065 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8066 j2k->tp_num = tilepartno;
8069 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8070 cio_tell(cio) + j2k->pos_correction;
8074 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8075 for (compno = 1; compno < image->numcomps; compno++) {
8076 j2k_write_coc(j2k, compno);
8077 j2k_write_qcc(j2k, compno);
8079 if (cp->tcps[tileno].numpocs) {
8086 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8087 cio_tell(cio) + j2k->pos_correction + 1;
8090 j2k_write_sod(j2k, tcd);
8094 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8095 cio_tell(cio) + j2k->pos_correction - 1;
8096 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8098 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8099 cstr_info->packno - acc_pack_num;
8100 acc_pack_num = cstr_info->packno;
8108 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8113 if (tile->PPT) { // BAD PPT !!!
8116 PPT_file=fopen("PPT","rb");
8117 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8118 for (i=0;i<tile->len_ppt;i++) {
8120 fread(&elmt, 1, 1, PPT_file);
8121 fwrite(&elmt,1,1,f);
8130 /* destroy the tile encoder */
8131 tcd_free_encode(tcd);
8134 opj_free(j2k->cur_totnum_tp);
8139 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8141 /* The following adjustment is done to adjust the codestream size */
8142 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8143 /* the first bunch of bytes is not in the codestream */
8144 cstr_info->codestream_size -= cstr_info->main_head_start;
8150 preparation of JPWL marker segments
8154 /* encode according to JPWL */
8155 jpwl_encode(j2k, cio, image);
8158 #endif /* USE_JPWL */
8163 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8165 assert(cstr_info != 00);
8167 /* expand the list? */
8168 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8169 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8170 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8173 /* add the marker */
8174 cstr_info->marker[cstr_info->marknum].type = type;
8175 cstr_info->marker[cstr_info->marknum].pos = pos;
8176 cstr_info->marker[cstr_info->marknum].len = len;
8177 cstr_info->marknum++;
8181 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8183 assert(cstr_index != 00);
8185 /* expand the list? */
8186 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8187 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8188 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8191 /* add the marker */
8192 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8193 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8194 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8195 cstr_index->marknum++;
8199 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8201 opj_marker_info_t *marker;
8203 assert(cstr_info != 00);
8205 /* expand the list? */
8206 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8207 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8208 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8211 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8213 /* add the marker */
8214 marker->type = type;
8217 cstr_info->tile[tileno].marknum++;
8220 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)
8222 assert(cstr_index != 00);
8223 assert(cstr_index->tile_index != 00);
8225 /* expand the list? */
8226 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8227 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8228 cstr_index->tile_index[tileno].marker =
8229 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8230 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8233 /* add the marker */
8234 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8235 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8236 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8237 cstr_index->tile_index[tileno].marknum++;
8239 if (type == J2K_MS_SOT) {
8240 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8242 if (cstr_index->tile_index[tileno].tp_index)
8243 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8250 * -----------------------------------------------------------------------
8251 * -----------------------------------------------------------------------
8252 * -----------------------------------------------------------------------
8256 * Ends the decompression procedures and possibiliy add data to be read after the
8259 opj_bool j2k_end_decompress(
8260 opj_j2k_v2_t *p_j2k,
8261 opj_stream_private_t *p_stream,
8262 opj_event_mgr_t * p_manager)
8271 * Reads a jpeg2000 codestream header structure.
8274 * @param p_stream the stream to read data from.
8275 * @param p_j2k the jpeg2000 codec.
8276 * @param p_manager the user event manager.
8278 * @return true if the box is valid.
8280 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8281 opj_j2k_v2_t* p_j2k,
8282 opj_image_t** p_image,
8283 struct opj_event_mgr* p_manager )
8286 assert(p_j2k != 00);
8287 assert(p_stream != 00);
8288 assert(p_manager != 00);
8290 /* create an empty image header */
8291 p_j2k->m_private_image = opj_image_create0();
8292 if (! p_j2k->m_private_image) {
8296 /* customization of the validation */
8297 j2k_setup_decoding_validation(p_j2k);
8299 /* validation of the parameters codec */
8300 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8301 opj_image_destroy(p_j2k->m_private_image);
8302 p_j2k->m_private_image = NULL;
8306 /* customization of the encoding */
8307 j2k_setup_header_reading(p_j2k);
8310 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8311 opj_image_destroy(p_j2k->m_private_image);
8312 p_j2k->m_private_image = NULL;
8316 *p_image = opj_image_create0();
8321 /* Copy codestream image information to the output image */
8322 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8324 /*Allocate and initialize some elements of codestrem index*/
8325 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8333 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8335 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8338 assert(p_j2k != 00);
8340 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8342 /* DEVELOPER CORNER, add your custom procedures */
8343 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8348 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8349 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8351 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8354 assert(p_j2k != 00);
8356 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8357 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8358 /* DEVELOPER CORNER, add your custom validation procedure */
8364 * The mct encoding validation procedure.
8366 * @param p_j2k the jpeg2000 codec to validate.
8367 * @param p_stream the input stream to validate.
8368 * @param p_manager the user event manager.
8370 * @return true if the parameters are correct.
8372 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8373 opj_stream_private_t *p_stream,
8374 opj_event_mgr_t * p_manager )
8376 opj_bool l_is_valid = OPJ_TRUE;
8380 assert(p_j2k != 00);
8381 assert(p_stream != 00);
8382 assert(p_manager != 00);
8384 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8385 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8386 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8388 for (i=0;i<l_nb_tiles;++i) {
8389 if (l_tcp->mct == 2) {
8390 opj_tccp_t * l_tccp = l_tcp->tccps;
8391 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8393 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8394 l_is_valid &= ! (l_tccp->qmfbid & 1);
8405 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8408 OPJ_UINT32 l_indix = 1;
8409 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8410 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8411 OPJ_UINT32 l_mct_size,l_nb_elem;
8412 OPJ_FLOAT32 * l_data, * l_current_data;
8413 opj_tccp_t * l_tccp;
8416 assert(p_tcp != 00);
8418 if (p_tcp->mct != 2) {
8422 if (p_tcp->m_mct_decoding_matrix) {
8423 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8424 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8426 p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
8427 if (! p_tcp->m_mct_records) {
8430 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8432 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8434 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8436 if (l_mct_deco_data->m_data) {
8437 opj_free(l_mct_deco_data->m_data);
8438 l_mct_deco_data->m_data = 00;
8441 l_mct_deco_data->m_index = l_indix++;
8442 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8443 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8444 l_nb_elem = p_image->numcomps * p_image->numcomps;
8445 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8446 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8448 if (! l_mct_deco_data->m_data) {
8452 j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem);
8454 l_mct_deco_data->m_data_size = l_mct_size;
8455 ++p_tcp->m_nb_mct_records;
8458 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8459 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8460 p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
8462 if (! p_tcp->m_mct_records) {
8466 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8467 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8469 if (l_mct_deco_data) {
8470 l_mct_deco_data = l_mct_offset_data - 1;
8474 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8476 if (l_mct_offset_data->m_data) {
8477 opj_free(l_mct_offset_data->m_data);
8478 l_mct_offset_data->m_data = 00;
8481 l_mct_offset_data->m_index = l_indix++;
8482 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8483 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8484 l_nb_elem = p_image->numcomps;
8485 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8486 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8488 if (! l_mct_offset_data->m_data) {
8492 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8494 opj_free(l_mct_offset_data->m_data);
8495 l_mct_offset_data->m_data = 00;
8499 l_tccp = p_tcp->tccps;
8500 l_current_data = l_data;
8502 for (i=0;i<l_nb_elem;++i) {
8503 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8507 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8511 l_mct_offset_data->m_data_size = l_mct_size;
8513 ++p_tcp->m_nb_mct_records;
8515 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8516 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8517 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8518 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8520 if (! p_tcp->m_mcc_records) {
8523 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8524 memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
8528 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8529 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8530 l_mcc_data->m_is_irreversible = 1;
8531 l_mcc_data->m_nb_comps = p_image->numcomps;
8532 l_mcc_data->m_index = l_indix++;
8533 l_mcc_data->m_offset_array = l_mct_offset_data;
8534 ++p_tcp->m_nb_mcc_records;
8540 * Builds the cp decoder parameters to use to decode tile.
8542 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8543 opj_stream_private_t *p_stream,
8544 opj_event_mgr_t * p_manager )
8546 /* add here initialization of cp
8547 copy paste of setup_decoder */
8555 * Builds the cp encoder parameters to use to encode tile.
8557 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8558 opj_stream_private_t *p_stream,
8559 opj_event_mgr_t * p_manager )
8561 /* add here initialization of cp
8562 copy paste of setup_encoder */
8570 * The default encoding validation procedure without any extension.
8572 * @param p_j2k the jpeg2000 codec to validate.
8573 * @param p_stream the input stream to validate.
8574 * @param p_manager the user event manager.
8576 * @return true if the parameters are correct.
8578 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8579 opj_stream_private_t *p_stream,
8580 opj_event_mgr_t * p_manager )
8582 opj_bool l_is_valid = OPJ_TRUE;
8585 assert(p_j2k != 00);
8586 assert(p_stream != 00);
8587 assert(p_manager != 00);
8589 /* STATE checking */
8590 /* make sure the state is at 0 */
8591 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8593 /* POINTER validation */
8594 /* make sure a p_j2k codec is present */
8595 l_is_valid &= (p_j2k->m_procedure_list != 00);
8596 /* make sure a validation list is present */
8597 l_is_valid &= (p_j2k->m_validation_list != 00);
8599 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8600 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8604 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8605 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8609 /* PARAMETER VALIDATION */
8614 * The default decoding validation procedure without any extension.
8616 * @param p_j2k the jpeg2000 codec to validate.
8617 * @param p_stream the input stream to validate.
8618 * @param p_manager the user event manager.
8620 * @return true if the parameters are correct.
8622 opj_bool j2k_decoding_validation (
8623 opj_j2k_v2_t *p_j2k,
8624 opj_stream_private_t *p_stream,
8625 opj_event_mgr_t * p_manager
8628 opj_bool l_is_valid = OPJ_TRUE;
8631 assert(p_j2k != 00);
8632 assert(p_stream != 00);
8633 assert(p_manager != 00);
8636 /* STATE checking */
8637 /* make sure the state is at 0 */
8639 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8641 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8643 /* POINTER validation */
8644 /* make sure a p_j2k codec is present */
8645 /* make sure a procedure list is present */
8646 l_is_valid &= (p_j2k->m_procedure_list != 00);
8647 /* make sure a validation list is present */
8648 l_is_valid &= (p_j2k->m_validation_list != 00);
8650 /* PARAMETER VALIDATION */
8654 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8655 struct opj_stream_private *p_stream,
8656 struct opj_event_mgr * p_manager)
8658 OPJ_UINT32 l_current_marker;
8659 OPJ_UINT32 l_marker_size;
8660 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8663 assert(p_stream != 00);
8664 assert(p_j2k != 00);
8665 assert(p_manager != 00);
8667 /* We enter in the main header */
8668 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8670 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8671 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8672 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8676 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8677 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8678 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8682 /* Read 2 bytes as the new marker ID */
8683 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8685 /* Try to read until the SOT is detected */
8686 while (l_current_marker != J2K_MS_SOT) {
8688 /* Check if the current marker ID is valid */
8689 if (l_current_marker < 0xff00) {
8690 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8694 /* Get the marker handler from the marker ID */
8695 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8697 /* Manage case where marker is unknown */
8698 if (l_marker_handler->id == J2K_MS_UNK) {
8699 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8700 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8704 if (l_current_marker == J2K_MS_SOT)
8705 break; /* SOT marker is detected main header is completely read */
8706 else /* Get the marker handler from the marker ID */
8707 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8710 /* Check if the marker is known and if it is the right place to find it */
8711 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8712 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8716 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8717 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8718 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8722 /* read 2 bytes as the marker size */
8723 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8724 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8726 /* Check if the marker size is compatible with the header data size */
8727 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8728 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8729 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8730 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8733 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8736 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8737 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) {
8738 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8742 /* Read the marker segment with the correct marker handler */
8743 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8744 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8748 /* Add the marker to the codestream index*/
8749 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8750 l_marker_handler->id,
8751 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8752 l_marker_size + 4 );
8754 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8755 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8756 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8760 /* read 2 bytes as the new marker ID */
8761 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8764 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8766 /* Position of the last element if the main header */
8767 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8769 /* Next step: read a tile-part header */
8770 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8776 * Excutes the given procedures on the given codec.
8778 * @param p_procedure_list the list of procedures to execute
8779 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8780 * @param p_stream the stream to execute the procedures on.
8781 * @param p_manager the user manager.
8783 * @return true if all the procedures were successfully executed.
8785 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8786 opj_procedure_list_t * p_procedure_list,
8787 opj_stream_private_t *p_stream,
8788 opj_event_mgr_t * p_manager )
8790 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8791 opj_bool l_result = OPJ_TRUE;
8792 OPJ_UINT32 l_nb_proc, i;
8795 assert(p_procedure_list != 00);
8796 assert(p_j2k != 00);
8797 assert(p_stream != 00);
8798 assert(p_manager != 00);
8801 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8802 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);
8804 for (i=0;i<l_nb_proc;++i) {
8805 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8809 /* and clear the procedure list at the end.*/
8810 opj_procedure_list_clear(p_procedure_list);
8815 opj_bool j2k_copy_default_tcp_and_create_tcd
8817 opj_j2k_v2_t * p_j2k,
8818 opj_stream_private_t *p_stream,
8819 opj_event_mgr_t * p_manager
8822 opj_tcp_v2_t * l_tcp = 00;
8823 opj_tcp_v2_t * l_default_tcp = 00;
8824 OPJ_UINT32 l_nb_tiles;
8826 opj_tccp_t *l_current_tccp = 00;
8827 OPJ_UINT32 l_tccp_size;
8828 OPJ_UINT32 l_mct_size;
8829 opj_image_t * l_image;
8830 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8831 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8832 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8833 OPJ_UINT32 l_offset;
8836 assert(p_j2k != 00);
8837 assert(p_stream != 00);
8838 assert(p_manager != 00);
8840 l_image = p_j2k->m_private_image;
8841 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8842 l_tcp = p_j2k->m_cp.tcps;
8843 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8844 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8845 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8848 for (i=0; i<l_nb_tiles; ++i) {
8849 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8850 l_current_tccp = l_tcp->tccps;
8851 /*Copy default coding parameters into the current tile coding parameters*/
8852 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8853 /* Initialize some values of the current tile coding parameters*/
8855 l_tcp->ppt_data = 00;
8856 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8857 l_tcp->tccps = l_current_tccp;
8859 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8860 if (l_default_tcp->m_mct_decoding_matrix) {
8861 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8862 if (! l_tcp->m_mct_decoding_matrix ) {
8865 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8868 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8869 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8870 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8871 if (! l_tcp->m_mct_records) {
8874 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8876 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8877 l_src_mct_rec = l_default_tcp->m_mct_records;
8878 l_dest_mct_rec = l_tcp->m_mct_records;
8880 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8882 if (l_src_mct_rec->m_data) {
8884 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8885 if(! l_dest_mct_rec->m_data) {
8888 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8895 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8896 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8897 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8898 if (! l_tcp->m_mcc_records) {
8901 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8903 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8904 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8905 l_dest_mcc_rec = l_tcp->m_mcc_records;
8907 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8909 if (l_src_mcc_rec->m_decorrelation_array) {
8910 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8911 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8914 if (l_src_mcc_rec->m_offset_array) {
8915 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8916 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8923 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8924 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8926 /* Move to next tile cp*/
8930 /* Create the current tile decoder*/
8931 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8932 if (! p_j2k->m_tcd ) {
8936 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8937 tcd_destroy_v2(p_j2k->m_tcd);
8939 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8947 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8948 * with the marker value.
8949 * @param p_id Marker value to look up
8951 * @return the handler associated with the id.
8953 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8955 const opj_dec_memory_marker_handler_t *e;
8956 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8957 if (e->id == p_id) {
8958 break; /* we find a handler corresponding to the marker ID*/
8966 * Destroys a jpeg2000 codec.
8968 * @param p_j2k the jpeg20000 structure to destroy.
8970 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8976 if (p_j2k->m_is_decoder) {
8978 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8979 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8980 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8981 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8984 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8985 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8986 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8987 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8992 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8993 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8994 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8997 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8998 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8999 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
9000 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
9003 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
9004 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
9005 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
9006 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
9010 tcd_destroy_v2(p_j2k->m_tcd);
9012 j2k_cp_destroy(&(p_j2k->m_cp));
9013 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
9015 opj_procedure_list_destroy(p_j2k->m_procedure_list);
9016 p_j2k->m_procedure_list = 00;
9018 opj_procedure_list_destroy(p_j2k->m_validation_list);
9019 p_j2k->m_procedure_list = 00;
9021 j2k_destroy_cstr_index(p_j2k->cstr_index);
9022 p_j2k->cstr_index = NULL;
9024 opj_image_destroy(p_j2k->m_private_image);
9025 p_j2k->m_private_image = NULL;
9027 opj_image_destroy(p_j2k->m_output_image);
9028 p_j2k->m_output_image = NULL;
9033 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9037 if (p_cstr_ind->marker) {
9038 opj_free(p_cstr_ind->marker);
9039 p_cstr_ind->marker = NULL;
9042 if (p_cstr_ind->tile_index) {
9043 OPJ_UINT32 it_tile = 0;
9045 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9047 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9048 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9049 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9052 if(p_cstr_ind->tile_index[it_tile].tp_index){
9053 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9054 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9057 if(p_cstr_ind->tile_index[it_tile].marker){
9058 opj_free(p_cstr_ind->tile_index[it_tile].marker);
9059 p_cstr_ind->tile_index[it_tile].marker = NULL;
9064 opj_free( p_cstr_ind->tile_index);
9065 p_cstr_ind->tile_index = NULL;
9068 opj_free(p_cstr_ind);
9075 * Destroys a tile coding parameter structure.
9077 * @param p_tcp the tile coding parameter to destroy.
9079 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9085 if (p_tcp->ppt_buffer != 00) {
9086 opj_free(p_tcp->ppt_buffer);
9087 p_tcp->ppt_buffer = 00;
9090 if (p_tcp->tccps != 00) {
9091 opj_free(p_tcp->tccps);
9095 if (p_tcp->m_mct_coding_matrix != 00) {
9096 opj_free(p_tcp->m_mct_coding_matrix);
9097 p_tcp->m_mct_coding_matrix = 00;
9100 if (p_tcp->m_mct_decoding_matrix != 00) {
9101 opj_free(p_tcp->m_mct_decoding_matrix);
9102 p_tcp->m_mct_decoding_matrix = 00;
9105 if (p_tcp->m_mcc_records) {
9106 opj_free(p_tcp->m_mcc_records);
9107 p_tcp->m_mcc_records = 00;
9108 p_tcp->m_nb_max_mcc_records = 0;
9109 p_tcp->m_nb_mcc_records = 0;
9112 if (p_tcp->m_mct_records) {
9113 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9116 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9117 if (l_mct_data->m_data) {
9118 opj_free(l_mct_data->m_data);
9119 l_mct_data->m_data = 00;
9125 opj_free(p_tcp->m_mct_records);
9126 p_tcp->m_mct_records = 00;
9129 if (p_tcp->mct_norms != 00) {
9130 opj_free(p_tcp->mct_norms);
9131 p_tcp->mct_norms = 00;
9134 j2k_tcp_data_destroy(p_tcp);
9139 * Destroys the data inside a tile coding parameter structure.
9141 * @param p_tcp the tile coding parameter which contain data to destroy.
9143 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9145 if (p_tcp->m_data) {
9146 opj_free(p_tcp->m_data);
9147 p_tcp->m_data = NULL;
9148 p_tcp->m_data_size = 0;
9153 * Destroys a coding parameter structure.
9155 * @param p_cp the coding parameter to destroy.
9157 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9159 OPJ_UINT32 l_nb_tiles;
9160 opj_tcp_v2_t * l_current_tile = 00;
9171 l_current_tile = p_cp->tcps;
9172 l_nb_tiles = p_cp->th * p_cp->tw;
9175 (i = 0; i < l_nb_tiles; ++i)
9177 j2k_tcp_destroy(l_current_tile);
9180 opj_free(p_cp->tcps);
9184 (p_cp->ppm_buffer != 00)
9186 opj_free(p_cp->ppm_buffer);
9187 p_cp->ppm_buffer = 00;
9190 (p_cp->comment != 00)
9192 opj_free(p_cp->comment);
9196 (! p_cp->m_is_decoder)
9199 (p_cp->m_specific_param.m_enc.m_matrice)
9201 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9202 p_cp->m_specific_param.m_enc.m_matrice = 00;
9210 * Reads a tile header.
9211 * @param p_j2k the jpeg2000 codec.
9212 * @param p_stream the stream to write data to.
9213 * @param p_manager the user event manager.
9215 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9216 OPJ_UINT32 * p_tile_index,
9217 OPJ_UINT32 * p_data_size,
9218 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9219 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9220 OPJ_UINT32 * p_nb_comps,
9222 opj_stream_private_t *p_stream,
9223 opj_event_mgr_t * p_manager )
9225 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9226 OPJ_UINT32 l_marker_size;
9227 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9228 opj_tcp_v2_t * l_tcp = NULL;
9229 OPJ_UINT32 l_nb_tiles;
9232 assert(p_stream != 00);
9233 assert(p_j2k != 00);
9234 assert(p_manager != 00);
9236 /* Reach the End Of Codestream ?*/
9237 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9238 l_current_marker = J2K_MS_EOC;
9240 /* We need to encounter a SOT marker (a new tile-part header) */
9241 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9245 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9246 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9248 /* Try to read until the Start Of Data is detected */
9249 while (l_current_marker != J2K_MS_SOD) {
9251 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9252 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9253 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9257 /* Read 2 bytes from the buffer as the marker size */
9258 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9260 /* Why this condition? FIXME */
9261 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9262 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9264 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9266 /* Get the marker handler from the marker ID */
9267 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9269 /* Check if the marker is known and if it is the right place to find it */
9270 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9271 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9274 /* FIXME manage case of unknown marker as in the main header ? */
9276 /* Check if the marker size is compatible with the header data size */
9277 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9278 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9279 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9280 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9283 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9286 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9287 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) {
9288 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9292 /* Read the marker segment with the correct marker handler */
9293 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9294 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9298 /* Add the marker to the codestream index*/
9299 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9301 l_marker_handler->id,
9302 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9303 l_marker_size + 4 );
9305 /* Keep the position of the last SOT marker read */
9306 if ( l_marker_handler->id == J2K_MS_SOT ) {
9307 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9308 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9310 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9315 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9316 /* Skip the rest of the tile part header*/
9317 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) {
9318 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9321 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9324 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9325 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9326 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9329 /* Read 2 bytes from the buffer as the new marker ID */
9330 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9334 /* If we didn't skip data before, we need to read the SOD marker*/
9335 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9336 /* Try to read the SOD marker and skip data ? FIXME */
9337 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
9343 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9344 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9345 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9346 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9350 /* Read 2 bytes from buffer as the new marker ID */
9351 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9355 /* Indicate we will try to read a new tile-part header*/
9356 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9357 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9358 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9360 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9361 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9362 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9366 /* Read 2 bytes from buffer as the new marker ID */
9367 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9371 /* Current marker is the EOC marker ?*/
9372 if (l_current_marker == J2K_MS_EOC) {
9373 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9374 p_j2k->m_current_tile_number = 0;
9375 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9380 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9381 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9382 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9384 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9385 ++p_j2k->m_current_tile_number;
9389 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9390 *p_go_on = OPJ_FALSE;
9396 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9397 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9401 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9402 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9404 *p_tile_index = p_j2k->m_current_tile_number;
9405 *p_go_on = OPJ_TRUE;
9406 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9407 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9408 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9409 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9410 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9411 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9413 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9419 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9420 OPJ_UINT32 p_tile_index,
9422 OPJ_UINT32 p_data_size,
9423 opj_stream_private_t *p_stream,
9424 opj_event_mgr_t * p_manager )
9426 OPJ_UINT32 l_current_marker;
9427 OPJ_BYTE l_data [2];
9428 opj_tcp_v2_t * l_tcp;
9431 assert(p_stream != 00);
9432 assert(p_j2k != 00);
9433 assert(p_manager != 00);
9435 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9436 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9440 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9441 if (! l_tcp->m_data) {
9442 j2k_tcp_destroy(l_tcp);
9446 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9450 p_j2k->cstr_index) ) {
9451 j2k_tcp_destroy(l_tcp);
9452 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9456 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9460 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9461 * we destroy just the data which will be re-read in read_tile_header*/
9462 /*j2k_tcp_destroy(l_tcp);
9463 p_j2k->m_tcd->tcp = 0;*/
9464 j2k_tcp_data_destroy(l_tcp);
9466 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9467 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9469 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9470 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9471 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9475 opj_read_bytes(l_data,&l_current_marker,2);
9477 if (l_current_marker == J2K_MS_EOC) {
9478 p_j2k->m_current_tile_number = 0;
9479 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9481 else if (l_current_marker != J2K_MS_SOT)
9483 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9492 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9494 OPJ_UINT32 i,j,k = 0;
9495 OPJ_UINT32 l_width_src,l_height_src;
9496 OPJ_UINT32 l_width_dest,l_height_dest;
9497 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9498 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9499 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9500 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9501 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9503 opj_image_comp_t * l_img_comp_src = 00;
9504 opj_image_comp_t * l_img_comp_dest = 00;
9506 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9507 opj_image_t * l_image_src = 00;
9508 OPJ_UINT32 l_size_comp, l_remaining;
9509 OPJ_INT32 * l_dest_ptr;
9510 opj_tcd_resolution_v2_t* l_res= 00;
9512 l_tilec = p_tcd->tcd_image->tiles->comps;
9513 l_image_src = p_tcd->image;
9514 l_img_comp_src = l_image_src->comps;
9516 l_img_comp_dest = p_output_image->comps;
9518 for (i=0; i<l_image_src->numcomps; i++) {
9520 /* Allocate output component buffer if necessary */
9521 if (!l_img_comp_dest->data) {
9523 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9524 if (! l_img_comp_dest->data) {
9529 /* Copy info from decoded comp image to output image */
9530 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9533 /* Compute the precision of the output buffer */
9534 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9535 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9536 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9542 if (l_size_comp == 3) {
9547 /* Current tile component size*/
9549 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9550 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9553 l_width_src = (l_res->x1 - l_res->x0);
9554 l_height_src = (l_res->y1 - l_res->y0);
9556 /* Border of the current output component*/
9557 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9558 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9559 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9560 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9563 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9564 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9568 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9569 * of the input buffer (decoded tile component) which will be move
9570 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9571 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9572 * by this input area.
9574 assert( l_res->x0 >= 0);
9575 assert( l_res->x1 >= 0);
9576 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9577 l_start_x_dest = l_res->x0 - l_x0_dest;
9578 l_offset_x0_src = 0;
9580 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9581 l_width_dest = l_width_src;
9582 l_offset_x1_src = 0;
9585 l_width_dest = l_x1_dest - l_res->x0 ;
9586 l_offset_x1_src = l_width_src - l_width_dest;
9590 l_start_x_dest = 0 ;
9591 l_offset_x0_src = l_x0_dest - l_res->x0;
9593 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9594 l_width_dest = l_width_src - l_offset_x0_src;
9595 l_offset_x1_src = 0;
9598 l_width_dest = l_img_comp_dest->w ;
9599 l_offset_x1_src = l_res->x1 - l_x1_dest;
9603 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9604 l_start_y_dest = l_res->y0 - l_y0_dest;
9605 l_offset_y0_src = 0;
9607 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9608 l_height_dest = l_height_src;
9609 l_offset_y1_src = 0;
9612 l_height_dest = l_y1_dest - l_res->y0 ;
9613 l_offset_y1_src = l_height_src - l_height_dest;
9617 l_start_y_dest = 0 ;
9618 l_offset_y0_src = l_y0_dest - l_res->y0;
9620 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9621 l_height_dest = l_height_src - l_offset_y0_src;
9622 l_offset_y1_src = 0;
9625 l_height_dest = l_img_comp_dest->h ;
9626 l_offset_y1_src = l_res->y1 - l_y1_dest;
9630 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9635 /* Compute the input buffer offset */
9636 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9637 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9638 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9640 /* Compute the output buffer offset */
9641 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9642 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9644 /* Move the output buffer to the first place where we will write*/
9645 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9648 fprintf(stdout, "COMPO[%d]:\n",i);
9649 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9650 "\t tile offset:%d, %d, %d, %d\n"
9651 "\t buffer offset: %d; %d, %d\n",
9652 l_res->x0, l_res->y0, l_width_src, l_height_src,
9653 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9654 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9656 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9657 "\t start offset: %d, line offset= %d\n",
9658 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9662 switch (l_size_comp) {
9665 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9666 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9668 if (l_img_comp_src->sgnd) {
9669 for (j = 0 ; j < l_height_dest ; ++j) {
9670 for ( k = 0 ; k < l_width_dest ; ++k) {
9671 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9674 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9675 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9679 for ( j = 0 ; j < l_height_dest ; ++j ) {
9680 for ( k = 0 ; k < l_width_dest ; ++k) {
9681 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9684 l_dest_ptr+= l_line_offset_dest;
9685 l_src_ptr += l_line_offset_src;
9689 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9690 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9695 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9696 l_src_ptr += l_start_offset_src;
9698 if (l_img_comp_src->sgnd) {
9699 for (j=0;j<l_height_dest;++j) {
9700 for (k=0;k<l_width_dest;++k) {
9701 *(l_dest_ptr++) = *(l_src_ptr++);
9704 l_dest_ptr+= l_line_offset_dest;
9705 l_src_ptr += l_line_offset_src ;
9709 for (j=0;j<l_height_dest;++j) {
9710 for (k=0;k<l_width_dest;++k) {
9711 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9714 l_dest_ptr+= l_line_offset_dest;
9715 l_src_ptr += l_line_offset_src ;
9719 l_src_ptr += l_end_offset_src;
9720 p_data = (OPJ_BYTE*) l_src_ptr;
9725 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9726 l_src_ptr += l_start_offset_src;
9728 for (j=0;j<l_height_dest;++j) {
9729 for (k=0;k<l_width_dest;++k) {
9730 *(l_dest_ptr++) = (*(l_src_ptr++));
9733 l_dest_ptr+= l_line_offset_dest;
9734 l_src_ptr += l_line_offset_src ;
9737 l_src_ptr += l_end_offset_src;
9738 p_data = (OPJ_BYTE*) l_src_ptr;
9752 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9754 * @param p_j2k the jpeg2000 codec.
9755 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9756 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9757 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9758 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9759 * @param p_manager the user event manager
9761 * @return true if the area could be set.
9763 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9764 opj_image_t* p_image,
9765 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9766 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9767 struct opj_event_mgr * p_manager )
9769 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9770 opj_image_t * l_image = p_j2k->m_private_image;
9773 OPJ_INT32 l_comp_x1, l_comp_y1;
9774 opj_image_comp_t* l_img_comp = NULL;
9776 /* Check if we are read the main header */
9777 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9778 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9782 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9783 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9785 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9786 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9787 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9788 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9794 /* Check if the positions provided by the user are correct */
9797 assert(p_start_x >= 0 );
9798 assert(p_start_y >= 0 );
9800 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9801 opj_event_msg_v2(p_manager, EVT_ERROR,
9802 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9803 p_start_x, l_image->x1);
9806 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9807 opj_event_msg_v2(p_manager, EVT_WARNING,
9808 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9809 p_start_x, l_image->x0);
9810 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9811 p_image->x0 = l_image->x0;
9814 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9815 p_image->x0 = p_start_x;
9819 if ((OPJ_UINT32)p_start_y > l_image->y1){
9820 opj_event_msg_v2(p_manager, EVT_ERROR,
9821 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9822 p_start_y, l_image->y1);
9825 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9826 opj_event_msg_v2(p_manager, EVT_WARNING,
9827 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9828 p_start_y, l_image->y0);
9829 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9830 p_image->y0 = l_image->y0;
9833 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9834 p_image->y0 = p_start_y;
9838 assert((OPJ_UINT32)p_end_x > 0);
9839 assert((OPJ_UINT32)p_end_y > 0);
9840 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9841 opj_event_msg_v2(p_manager, EVT_ERROR,
9842 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9843 p_end_x, l_image->x0);
9846 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9847 opj_event_msg_v2(p_manager, EVT_WARNING,
9848 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9849 p_end_x, l_image->x1);
9850 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9851 p_image->x1 = l_image->x1;
9854 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9855 p_image->x1 = p_end_x;
9859 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9860 opj_event_msg_v2(p_manager, EVT_ERROR,
9861 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9862 p_end_y, l_image->y0);
9865 if ((OPJ_UINT32)p_end_y > l_image->y1){
9866 opj_event_msg_v2(p_manager, EVT_WARNING,
9867 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9868 p_end_y, l_image->y1);
9869 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9870 p_image->y1 = l_image->y1;
9873 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9874 p_image->y1 = p_end_y;
9878 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9880 l_img_comp = p_image->comps;
9881 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9885 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9886 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9887 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9888 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9890 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9891 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9893 opj_event_msg_v2(p_manager, EVT_ERROR,
9894 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9898 l_img_comp->w = l_w;
9900 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9901 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9903 opj_event_msg_v2(p_manager, EVT_ERROR,
9904 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9908 l_img_comp->h = l_h;
9913 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9914 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9921 /* ----------------------------------------------------------------------- */
9922 /* J2K / JPT decoder interface */
9923 /* ----------------------------------------------------------------------- */
9925 * Creates a J2K decompression structure.
9927 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9929 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9931 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9935 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9937 l_j2k->m_is_decoder = 1;
9938 l_j2k->m_cp.m_is_decoder = 1;
9940 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9941 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9945 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9947 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9948 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9953 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9955 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9957 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9959 /* codestream index creation */
9960 l_j2k->cstr_index = j2k_create_cstr_index();
9962 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9963 if (!l_j2k->cstr_index){
9968 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9971 /* validation list creation */
9972 l_j2k->m_validation_list = opj_procedure_list_create();
9973 if (! l_j2k->m_validation_list) {
9978 /* execution list creation */
9979 l_j2k->m_procedure_list = opj_procedure_list_create();
9980 if (! l_j2k->m_procedure_list) {
9989 opj_codestream_index_t* j2k_create_cstr_index(void)
9991 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9992 opj_calloc(1,sizeof(opj_codestream_index_t));
9996 cstr_index->maxmarknum = 100;
9997 cstr_index->marknum = 0;
9998 cstr_index->marker = (opj_marker_info_t*)
9999 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
10000 if (!cstr_index-> marker)
10003 cstr_index->tile_index = NULL;
10010 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
10012 * @param p_tile_no the tile index.
10013 * @param p_comp_no the component being outputted.
10014 * @param p_j2k the J2K codec.
10016 * @return the number of bytes taken by the SPCod element.
10018 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
10019 OPJ_UINT32 p_tile_no,
10020 OPJ_UINT32 p_comp_no )
10022 opj_cp_v2_t *l_cp = 00;
10023 opj_tcp_v2_t *l_tcp = 00;
10024 opj_tccp_t *l_tccp = 00;
10026 /* preconditions */
10027 assert(p_j2k != 00);
10029 l_cp = &(p_j2k->m_cp);
10030 l_tcp = &l_cp->tcps[p_tile_no];
10031 l_tccp = &l_tcp->tccps[p_comp_no];
10033 /* preconditions again */
10034 assert(p_tile_no < (l_cp->tw * l_cp->th));
10035 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10037 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10038 return 5 + l_tccp->numresolutions;
10046 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10048 * @param p_comp_no the component number to output.
10049 * @param p_stream the stream to write data to.
10050 * @param p_j2k J2K codec.
10051 * @param p_manager the user event manager.
10054 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10055 OPJ_UINT32 p_tile_no,
10056 OPJ_UINT32 p_comp_no,
10058 OPJ_UINT32 * p_header_size,
10059 struct opj_event_mgr * p_manager )
10062 opj_cp_v2_t *l_cp = 00;
10063 opj_tcp_v2_t *l_tcp = 00;
10064 opj_tccp_t *l_tccp = 00;
10066 /* preconditions */
10067 assert(p_j2k != 00);
10068 assert(p_header_size != 00);
10069 assert(p_manager != 00);
10070 assert(p_data != 00);
10072 l_cp = &(p_j2k->m_cp);
10073 l_tcp = &l_cp->tcps[p_tile_no];
10074 l_tccp = &l_tcp->tccps[p_comp_no];
10076 /* preconditions again */
10077 assert(p_tile_no < (l_cp->tw * l_cp->th));
10078 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10080 if (*p_header_size < 5) {
10081 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10085 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
10088 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
10091 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
10094 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
10097 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
10100 *p_header_size = *p_header_size - 5;
10102 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10104 if (*p_header_size < l_tccp->numresolutions) {
10105 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10109 for (i = 0; i < l_tccp->numresolutions; ++i) {
10110 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
10114 *p_header_size = *p_header_size - l_tccp->numresolutions;
10121 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10122 * @param p_header_data the data contained in the COM box.
10123 * @param p_j2k the jpeg2000 codec.
10124 * @param p_header_size the size of the data contained in the COM marker.
10125 * @param p_manager the user event manager.
10127 opj_bool j2k_read_SPCod_SPCoc(
10128 opj_j2k_v2_t *p_j2k,
10130 OPJ_BYTE * p_header_data,
10131 OPJ_UINT32 * p_header_size,
10132 struct opj_event_mgr * p_manager
10135 OPJ_UINT32 i, l_tmp;
10136 opj_cp_v2_t *l_cp = NULL;
10137 opj_tcp_v2_t *l_tcp = NULL;
10138 opj_tccp_t *l_tccp = NULL;
10139 OPJ_BYTE * l_current_ptr = NULL;
10141 /* preconditions */
10142 assert(p_j2k != 00);
10143 assert(p_manager != 00);
10144 assert(p_header_data != 00);
10146 l_cp = &(p_j2k->m_cp);
10147 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10148 &l_cp->tcps[p_j2k->m_current_tile_number] :
10149 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10151 /* precondition again */
10152 assert(compno < p_j2k->m_private_image->numcomps);
10154 l_tccp = &l_tcp->tccps[compno];
10155 l_current_ptr = p_header_data;
10157 /* make sure room is sufficient */
10158 if (*p_header_size < 5) {
10159 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10163 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10164 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10167 /* If user wants to remove more resolutions than the codestream contains, return error */
10168 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10169 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10170 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10171 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10175 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10177 l_tccp->cblkw += 2;
10179 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10181 l_tccp->cblkh += 2;
10183 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10186 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10189 *p_header_size = *p_header_size - 5;
10191 /* use custom precinct size ? */
10192 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10193 if (*p_header_size < l_tccp->numresolutions) {
10194 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10198 for (i = 0; i < l_tccp->numresolutions; ++i) {
10199 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10201 l_tccp->prcw[i] = l_tmp & 0xf;
10202 l_tccp->prch[i] = l_tmp >> 4;
10205 *p_header_size = *p_header_size - l_tccp->numresolutions;
10208 /* set default size for the precinct width and height */
10209 for (i = 0; i < l_tccp->numresolutions; ++i) {
10210 l_tccp->prcw[i] = 15;
10211 l_tccp->prch[i] = 15;
10215 #ifdef WIP_REMOVE_MSD
10217 if (p_j2k->cstr_info && compno == 0) {
10218 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10220 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10221 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10222 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10223 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10224 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10227 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10228 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10237 * Copies the tile component parameters of all the component from the first tile component.
10239 * @param p_j2k the J2k codec.
10241 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10245 opj_cp_v2_t *l_cp = NULL;
10246 opj_tcp_v2_t *l_tcp = NULL;
10247 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10248 OPJ_UINT32 l_prc_size;
10250 /* preconditions */
10251 assert(p_j2k != 00);
10253 l_cp = &(p_j2k->m_cp);
10254 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10255 &l_cp->tcps[p_j2k->m_current_tile_number] :
10256 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10258 l_ref_tccp = &l_tcp->tccps[0];
10259 l_copied_tccp = l_ref_tccp + 1;
10260 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10262 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10263 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10264 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10265 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10266 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10267 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10268 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10269 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10275 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10277 * @param p_tile_no the tile index.
10278 * @param p_comp_no the component being outputted.
10279 * @param p_j2k the J2K codec.
10281 * @return the number of bytes taken by the SPCod element.
10283 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10284 OPJ_UINT32 p_tile_no,
10285 OPJ_UINT32 p_comp_no )
10287 OPJ_UINT32 l_num_bands;
10289 opj_cp_v2_t *l_cp = 00;
10290 opj_tcp_v2_t *l_tcp = 00;
10291 opj_tccp_t *l_tccp = 00;
10293 /* preconditions */
10294 assert(p_j2k != 00);
10296 l_cp = &(p_j2k->m_cp);
10297 l_tcp = &l_cp->tcps[p_tile_no];
10298 l_tccp = &l_tcp->tccps[p_comp_no];
10300 /* preconditions again */
10301 assert(p_tile_no < l_cp->tw * l_cp->th);
10302 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10304 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10306 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10307 return 1 + l_num_bands;
10310 return 1 + 2*l_num_bands;
10315 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10317 * @param p_tile_no the tile to output.
10318 * @param p_comp_no the component number to output.
10319 * @param p_data the data buffer.
10320 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10321 * @param p_j2k J2K codec.
10322 * @param p_manager the user event manager.
10325 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10326 OPJ_UINT32 p_tile_no,
10327 OPJ_UINT32 p_comp_no,
10329 OPJ_UINT32 * p_header_size,
10330 struct opj_event_mgr * p_manager )
10332 OPJ_UINT32 l_header_size;
10333 OPJ_UINT32 l_band_no, l_num_bands;
10334 OPJ_UINT32 l_expn,l_mant;
10336 opj_cp_v2_t *l_cp = 00;
10337 opj_tcp_v2_t *l_tcp = 00;
10338 opj_tccp_t *l_tccp = 00;
10340 /* preconditions */
10341 assert(p_j2k != 00);
10342 assert(p_header_size != 00);
10343 assert(p_manager != 00);
10344 assert(p_data != 00);
10346 l_cp = &(p_j2k->m_cp);
10347 l_tcp = &l_cp->tcps[p_tile_no];
10348 l_tccp = &l_tcp->tccps[p_comp_no];
10350 /* preconditions again */
10351 assert(p_tile_no < l_cp->tw * l_cp->th);
10352 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10354 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10356 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10357 l_header_size = 1 + l_num_bands;
10359 if (*p_header_size < l_header_size) {
10360 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10364 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10367 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10368 l_expn = l_tccp->stepsizes[l_band_no].expn;
10369 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10374 l_header_size = 1 + 2*l_num_bands;
10376 if (*p_header_size < l_header_size) {
10377 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10381 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10384 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10385 l_expn = l_tccp->stepsizes[l_band_no].expn;
10386 l_mant = l_tccp->stepsizes[l_band_no].mant;
10388 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10393 *p_header_size = *p_header_size - l_header_size;
10399 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10401 * @param p_comp_no the component being targeted.
10402 * @param p_header_data the data contained in the COM box.
10403 * @param p_j2k the jpeg2000 codec.
10404 * @param p_header_size the size of the data contained in the COM marker.
10405 * @param p_manager the user event manager.
10407 opj_bool j2k_read_SQcd_SQcc(
10408 opj_j2k_v2_t *p_j2k,
10409 OPJ_UINT32 p_comp_no,
10410 OPJ_BYTE* p_header_data,
10411 OPJ_UINT32 * p_header_size,
10412 struct opj_event_mgr * p_manager
10416 OPJ_UINT32 l_band_no;
10417 opj_cp_v2_t *l_cp = 00;
10418 opj_tcp_v2_t *l_tcp = 00;
10419 opj_tccp_t *l_tccp = 00;
10420 OPJ_BYTE * l_current_ptr = 00;
10421 OPJ_UINT32 l_tmp, l_num_band;
10424 assert(p_j2k != 00);
10425 assert(p_manager != 00);
10426 assert(p_header_data != 00);
10428 l_cp = &(p_j2k->m_cp);
10429 /* come from tile part header or main header ?*/
10430 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10431 &l_cp->tcps[p_j2k->m_current_tile_number] :
10432 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10434 /* precondition again*/
10435 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10437 l_tccp = &l_tcp->tccps[p_comp_no];
10438 l_current_ptr = p_header_data;
10440 if (*p_header_size < 1) {
10441 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10444 *p_header_size -= 1;
10446 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10449 l_tccp->qntsty = l_tmp & 0x1f;
10450 l_tccp->numgbits = l_tmp >> 5;
10451 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10455 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10457 (*p_header_size) / 2;
10459 if( l_num_band > J2K_MAXBANDS ) {
10460 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10461 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10462 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10463 /*return OPJ_FALSE;*/
10468 if (l_cp->correct) {
10470 /* if JPWL is on, we check whether there are too many subbands */
10471 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10472 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10473 "JPWL: bad number of subbands in Sqcx (%d)\n",
10475 if (!JPWL_ASSUME) {
10476 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10479 /* we try to correct */
10481 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10482 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10487 #endif /* USE_JPWL */
10489 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10490 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10491 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10493 if (l_band_no < J2K_MAXBANDS){
10494 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10495 l_tccp->stepsizes[l_band_no].mant = 0;
10498 *p_header_size = *p_header_size - l_num_band;
10501 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10502 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10504 if (l_band_no < J2K_MAXBANDS){
10505 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10506 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10509 *p_header_size = *p_header_size - 2*l_num_band;
10512 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10513 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10514 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10515 l_tccp->stepsizes[l_band_no].expn =
10516 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10517 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10518 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10526 * Copies the tile component parameters of all the component from the first tile component.
10528 * @param p_j2k the J2k codec.
10530 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10533 opj_cp_v2_t *l_cp = NULL;
10534 opj_tcp_v2_t *l_tcp = NULL;
10535 opj_tccp_t *l_ref_tccp = NULL;
10536 opj_tccp_t *l_copied_tccp = NULL;
10539 /* preconditions */
10540 assert(p_j2k != 00);
10542 l_cp = &(p_j2k->m_cp);
10543 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10544 &l_cp->tcps[p_j2k->m_current_tile_number] :
10545 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10547 l_ref_tccp = &l_tcp->tccps[0];
10548 l_copied_tccp = l_ref_tccp + 1;
10549 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10551 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10552 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10553 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10554 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10560 * Dump some elements from the J2K decompression structure .
10562 *@param p_j2k the jpeg2000 codec.
10563 *@param flag flag to describe what elments are dump.
10564 *@param out_stream output stream where dump the elements.
10567 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10569 /* Check if the flag is compatible with j2k file*/
10570 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10571 fprintf(out_stream, "Wrong flag\n");
10575 /* Dump the image_header */
10576 if (flag & OPJ_IMG_INFO){
10577 if (p_j2k->m_private_image)
10578 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10581 /* Dump the codestream info from main header */
10582 if (flag & OPJ_J2K_MH_INFO){
10583 j2k_dump_MH_info(p_j2k, out_stream);
10587 /* Dump the codestream info of the current tile */
10588 if (flag & OPJ_J2K_TH_INFO){
10592 /* Dump the codestream index from main header */
10593 if (flag & OPJ_J2K_MH_IND){
10594 j2k_dump_MH_index(p_j2k, out_stream);
10597 /* Dump the codestream index of the current tile */
10598 if (flag & OPJ_J2K_TH_IND){
10605 * Dump index elements of the codestream extract from the main header.
10607 *@param p_j2k the jpeg2000 codec.
10608 *@param out_stream output stream where dump the elements.
10611 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10613 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10614 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10616 fprintf(out_stream, "Codestream index from main header: {\n");
10618 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10619 "\t Main header end position=%" PRIi64 "\n",
10620 cstr_index->main_head_start, cstr_index->main_head_end);
10622 fprintf(out_stream, "\t Marker list: {\n");
10624 if (cstr_index->marker){
10625 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10626 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10627 cstr_index->marker[it_marker].type,
10628 cstr_index->marker[it_marker].pos,
10629 cstr_index->marker[it_marker].len );
10633 fprintf(out_stream, "\t }\n");
10636 if (cstr_index->tile_index){
10638 /* Simple test to avoid to write empty information*/
10639 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10640 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10641 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10644 if (l_acc_nb_of_tile_part)
10646 fprintf(out_stream, "\t Tile index: {\n");
10648 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10649 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10651 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10653 if (cstr_index->tile_index[it_tile].tp_index){
10654 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10655 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10657 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10658 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10659 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10663 if (cstr_index->tile_index[it_tile].marker){
10664 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10665 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10666 cstr_index->tile_index[it_tile].marker[it_marker].type,
10667 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10668 cstr_index->tile_index[it_tile].marker[it_marker].len );
10672 fprintf(out_stream,"\t }\n");
10676 fprintf(out_stream,"}\n");
10681 * Dump info elements of the codestream extract from the main header.
10683 *@param p_j2k the jpeg2000 codec.
10684 *@param out_stream output stream where dump the elements.
10687 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10689 opj_tcp_v2_t * l_default_tile=NULL;
10691 fprintf(out_stream, "Codestream info from main header: {\n");
10693 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10694 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10695 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10697 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10698 if (l_default_tile)
10701 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10703 fprintf(out_stream, "\t default tile {\n");
10704 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10705 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10706 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10707 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10709 for (compno = 0; compno < numcomps; compno++) {
10710 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10712 OPJ_INT32 bandno, numbands;
10715 fprintf(out_stream, "\t\t comp %d {\n", compno);
10716 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10717 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10718 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10719 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10720 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10721 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10723 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10724 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10725 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10727 fprintf(out_stream, "\n");
10729 /* quantization style*/
10730 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10731 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10732 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10733 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10734 for (bandno = 0; bandno < numbands; bandno++) {
10735 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10736 l_tccp->stepsizes[bandno].expn);
10738 fprintf(out_stream, "\n");
10741 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10743 fprintf(out_stream, "\t\t }\n");
10744 } /*end of component of default tile*/
10745 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10749 fprintf(out_stream, "}\n");
10754 * Dump an image header structure.
10756 *@param img_header the image header to dump.
10757 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10758 *@param out_stream output stream where dump the elements.
10760 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10764 if (dev_dump_flag){
10765 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10769 fprintf(out_stream, "Image info {\n");
10770 tab[0] = '\t';tab[1] = '\0';
10773 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10774 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10775 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10777 if (img_header->comps){
10779 for (compno = 0; compno < img_header->numcomps; compno++) {
10780 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10781 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10782 fprintf(out_stream,"%s}\n",tab);
10786 fprintf(out_stream, "}\n");
10790 * Dump a component image header structure.
10792 *@param comp_header the component image header to dump.
10793 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10794 *@param out_stream output stream where dump the elements.
10796 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10800 if (dev_dump_flag){
10801 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10804 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10807 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10808 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10809 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10812 fprintf(out_stream, "}\n");
10817 * Get the codestream info from a JPEG2000 codec.
10819 *@param p_j2k the component image header to dump.
10821 *@return the codestream information extract from the jpg2000 codec
10823 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10826 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10827 opj_tcp_v2_t *l_default_tile;
10828 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10830 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10832 cstr_info->tx0 = p_j2k->m_cp.tx0;
10833 cstr_info->ty0 = p_j2k->m_cp.ty0;
10834 cstr_info->tdx = p_j2k->m_cp.tdx;
10835 cstr_info->tdy = p_j2k->m_cp.tdy;
10836 cstr_info->tw = p_j2k->m_cp.tw;
10837 cstr_info->th = p_j2k->m_cp.th;
10839 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10841 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10843 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10844 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10845 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10846 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10848 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10850 for (compno = 0; compno < numcomps; compno++) {
10851 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10852 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10853 OPJ_INT32 bandno, numbands;
10856 l_tccp_info->csty = l_tccp->csty;
10857 l_tccp_info->numresolutions = l_tccp->numresolutions;
10858 l_tccp_info->cblkw = l_tccp->cblkw;
10859 l_tccp_info->cblkh = l_tccp->cblkh;
10860 l_tccp_info->cblksty = l_tccp->cblksty;
10861 l_tccp_info->qmfbid = l_tccp->qmfbid;
10862 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10864 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10865 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10868 /* quantization style*/
10869 l_tccp_info->qntsty = l_tccp->qntsty;
10870 l_tccp_info->numgbits = l_tccp->numgbits;
10872 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10873 if (numbands < J2K_MAXBANDS) {
10874 for (bandno = 0; bandno < numbands; bandno++) {
10875 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10876 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10881 l_tccp_info->roishift = l_tccp->roishift;
10889 * Get the codestream index from a JPEG2000 codec.
10891 *@param p_j2k the component image header to dump.
10893 *@return the codestream index extract from the jpg2000 codec
10895 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10897 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10898 opj_calloc(1,sizeof(opj_codestream_index_t));
10902 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10903 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10904 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10906 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10907 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10908 if (!l_cstr_index->marker){
10909 opj_free( l_cstr_index);
10913 if (p_j2k->cstr_index->marker)
10914 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10916 opj_free(l_cstr_index->marker);
10917 l_cstr_index->marker = NULL;
10920 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10921 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10922 if (!l_cstr_index->tile_index){
10923 opj_free( l_cstr_index->marker);
10924 opj_free( l_cstr_index);
10928 if (!p_j2k->cstr_index->tile_index){
10929 opj_free(l_cstr_index->tile_index);
10930 l_cstr_index->tile_index = NULL;
10933 OPJ_UINT32 it_tile = 0;
10934 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10937 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10939 l_cstr_index->tile_index[it_tile].marker =
10940 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10942 if (!l_cstr_index->tile_index[it_tile].marker) {
10943 OPJ_UINT32 it_tile_free;
10945 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10946 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10949 opj_free( l_cstr_index->tile_index);
10950 opj_free( l_cstr_index->marker);
10951 opj_free( l_cstr_index);
10955 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10956 memcpy( l_cstr_index->tile_index[it_tile].marker,
10957 p_j2k->cstr_index->tile_index[it_tile].marker,
10958 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10960 opj_free(l_cstr_index->tile_index[it_tile].marker);
10961 l_cstr_index->tile_index[it_tile].marker = NULL;
10964 /* Tile part index*/
10965 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10967 l_cstr_index->tile_index[it_tile].tp_index =
10968 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10970 if(!l_cstr_index->tile_index[it_tile].tp_index){
10971 OPJ_UINT32 it_tile_free;
10973 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10974 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10975 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10978 opj_free( l_cstr_index->tile_index);
10979 opj_free( l_cstr_index->marker);
10980 opj_free( l_cstr_index);
10984 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10985 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10986 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10987 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10990 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10991 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10994 /* Packet index (NOT USED)*/
10995 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10996 l_cstr_index->tile_index[it_tile].packet_index = NULL;
11001 return l_cstr_index;
11004 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
11006 OPJ_UINT32 it_tile=0;
11008 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
11009 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
11010 if (!p_j2k->cstr_index->tile_index)
11013 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
11014 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
11015 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
11016 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
11017 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
11018 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11028 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
11029 opj_stream_private_t *p_stream,
11030 opj_event_mgr_t * p_manager)
11032 opj_bool l_go_on = OPJ_TRUE;
11033 OPJ_UINT32 l_current_tile_no;
11034 OPJ_UINT32 l_data_size,l_max_data_size;
11035 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11036 OPJ_UINT32 l_nb_comps;
11037 OPJ_BYTE * l_current_data;
11039 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11040 if (! l_current_data) {
11043 l_max_data_size = 1000;
11048 if (! j2k_read_tile_header( p_j2k,
11049 &l_current_tile_no,
11051 &l_tile_x0, &l_tile_y0,
11052 &l_tile_x1, &l_tile_y1,
11057 opj_free(l_current_data);
11065 if (l_data_size > l_max_data_size) {
11066 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11067 if (! l_current_data) {
11068 opj_free(l_current_data);
11072 l_max_data_size = l_data_size;
11075 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11076 opj_free(l_current_data);
11079 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);
11081 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11082 opj_free(l_current_data);
11085 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11089 opj_free(l_current_data);
11095 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11097 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11100 assert(p_j2k != 00);
11102 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11103 /* DEVELOPER CORNER, add your custom procedures */
11108 * Read and decode one tile.
11110 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
11111 opj_stream_private_t *p_stream,
11112 opj_event_mgr_t * p_manager)
11114 opj_bool l_go_on = OPJ_TRUE;
11115 OPJ_UINT32 l_current_tile_no;
11116 OPJ_UINT32 l_tile_no_to_dec;
11117 OPJ_UINT32 l_data_size,l_max_data_size;
11118 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11119 OPJ_UINT32 l_nb_comps;
11120 OPJ_BYTE * l_current_data;
11122 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11123 if (! l_current_data) {
11126 l_max_data_size = 1000;
11128 /*Allocate and initialize some elements of codestrem index if not already done*/
11129 if( !p_j2k->cstr_index->tile_index)
11131 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11132 opj_free(l_current_data);
11136 /* Move into the codestream to the first SOT used to decode the desired tile */
11137 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11138 if (p_j2k->cstr_index->tile_index)
11139 if(p_j2k->cstr_index->tile_index->tp_index)
11141 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11142 /* the index for this tile has not been built,
11143 * so move to the last SOT read */
11144 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11145 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11150 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)) {
11151 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11155 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11156 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11157 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11161 if (! j2k_read_tile_header( p_j2k,
11162 &l_current_tile_no,
11164 &l_tile_x0, &l_tile_y0,
11165 &l_tile_x1, &l_tile_y1,
11170 opj_free(l_current_data);
11179 if (l_data_size > l_max_data_size) {
11180 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11181 if (! l_current_data) {
11182 opj_free(l_current_data);
11186 l_max_data_size = l_data_size;
11191 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11192 opj_free(l_current_data);
11195 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);
11197 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11198 opj_free(l_current_data);
11201 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11203 if(l_current_tile_no == l_tile_no_to_dec)
11205 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11206 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11207 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11213 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);
11218 opj_free(l_current_data);
11225 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11227 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11230 assert(p_j2k != 00);
11232 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11233 /* DEVELOPER CORNER, add your custom procedures */
11239 * Decodes the tiles of the stream.
11241 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
11242 opj_stream_private_t * p_stream,
11243 opj_image_t * p_image,
11244 opj_event_mgr_t * p_manager)
11251 p_j2k->m_output_image = opj_image_create0();
11252 if (! (p_j2k->m_output_image)) {
11255 opj_copy_image_header(p_image, p_j2k->m_output_image);
11257 /* customization of the decoding */
11258 j2k_setup_decoding(p_j2k);
11260 /* Decode the codestream */
11261 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11262 opj_image_destroy(p_j2k->m_private_image);
11263 p_j2k->m_private_image = NULL;
11267 /* Move data and copy one information from codec to output image*/
11268 for (compno = 0; compno < p_image->numcomps; compno++) {
11269 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11270 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11271 p_j2k->m_output_image->comps[compno].data = NULL;
11279 * Get the decoded tile.
11281 * @param p_j2k the jpeg2000 codestream codec.
11282 * @param p_stream input_stream
11283 * @param p_image output image. .
11284 * @param p_manager the user event manager
11285 * @param tile_index index of the tile we want decode
11287 * @return true if succeed.
11289 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11290 opj_stream_private_t *p_stream,
11291 opj_image_t* p_image,
11292 struct opj_event_mgr * p_manager,
11293 OPJ_UINT32 tile_index )
11296 OPJ_UINT32 l_tile_x, l_tile_y;
11297 opj_image_comp_t* l_img_comp;
11300 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11304 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11305 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);
11309 /* Compute the dimension of the desired tile*/
11310 l_tile_x = tile_index % p_j2k->m_cp.tw;
11311 l_tile_y = tile_index / p_j2k->m_cp.tw;
11313 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11314 if (p_image->x0 < p_j2k->m_private_image->x0)
11315 p_image->x0 = p_j2k->m_private_image->x0;
11316 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11317 if (p_image->x1 > p_j2k->m_private_image->x1)
11318 p_image->x1 = p_j2k->m_private_image->x1;
11320 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11321 if (p_image->y0 < p_j2k->m_private_image->y0)
11322 p_image->y0 = p_j2k->m_private_image->y0;
11323 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11324 if (p_image->y1 > p_j2k->m_private_image->y1)
11325 p_image->y1 = p_j2k->m_private_image->y1;
11327 l_img_comp = p_image->comps;
11328 for (compno=0; compno < p_image->numcomps; ++compno)
11330 OPJ_INT32 l_comp_x1, l_comp_y1;
11332 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11334 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11335 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11336 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11337 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11339 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11340 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11345 /* Destroy the previous output image*/
11346 if (p_j2k->m_output_image)
11347 opj_image_destroy(p_j2k->m_output_image);
11349 /* Create the ouput image from the information previously computed*/
11350 p_j2k->m_output_image = opj_image_create0();
11351 if (! (p_j2k->m_output_image)) {
11354 opj_copy_image_header(p_image, p_j2k->m_output_image);
11356 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11358 /* customization of the decoding */
11359 j2k_setup_decoding_tile(p_j2k);
11361 /* Decode the codestream */
11362 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11363 opj_image_destroy(p_j2k->m_private_image);
11364 p_j2k->m_private_image = NULL;
11368 /* Move data and copy one information from codec to output image*/
11369 for (compno = 0; compno < p_image->numcomps; compno++) {
11370 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11372 if (p_image->comps[compno].data)
11373 opj_free(p_image->comps[compno].data);
11375 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11377 p_j2k->m_output_image->comps[compno].data = NULL;
11383 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11385 OPJ_UINT32 it_comp;
11387 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11389 if (p_j2k->m_private_image) {
11390 if (p_j2k->m_private_image->comps) {
11391 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11392 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11393 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11394 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11395 if ( res_factor >= max_res){
11396 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11399 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11412 * Encodes all the tiles in a row.
11414 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11415 opj_stream_private_t *p_stream,
11416 opj_event_mgr_t * p_manager )
11419 OPJ_UINT32 l_nb_tiles;
11420 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11421 OPJ_BYTE * l_current_data;
11423 /* preconditions */
11424 assert(p_j2k != 00);
11425 assert(p_stream != 00);
11426 assert(p_manager != 00);
11428 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11429 if (! l_current_data) {
11432 l_max_tile_size = 1000;
11434 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11435 for (i=0;i<l_nb_tiles;++i) {
11436 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11437 opj_free(l_current_data);
11441 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11442 if (l_current_tile_size > l_max_tile_size) {
11443 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11444 if (! l_current_data) {
11447 l_max_tile_size = l_current_tile_size;
11450 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11452 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11457 opj_free(l_current_data);
11462 * Ends the compression procedures and possibility add data to be read after the
11465 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11466 opj_stream_private_t *p_stream,
11467 struct opj_event_mgr * p_manager)
11469 /* customization of the encoding */
11470 j2k_setup_end_compress(p_j2k);
11472 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11482 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11484 * @param p_j2k the jpeg2000 codec.
11485 * @param p_stream the stream object.
11486 * @param p_manager the user event manager.
11488 * @return true if the codec is valid.
11490 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11491 opj_stream_private_t *p_stream,
11492 opj_image_t * p_image,
11493 opj_event_mgr_t * p_manager)
11495 /* preconditions */
11496 assert(p_j2k != 00);
11497 assert(p_stream != 00);
11498 assert(p_manager != 00);
11500 p_j2k->m_private_image = opj_image_create0();
11501 opj_copy_image_header(p_image, p_j2k->m_private_image);
11503 // TODO_MSD: Find a better way
11504 if (p_image->comps) {
11505 OPJ_UINT32 it_comp;
11506 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11507 if (p_image->comps[it_comp].data) {
11508 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11509 p_image->comps[it_comp].data = NULL;
11515 /* customization of the validation */
11516 j2k_setup_encoding_validation (p_j2k);
11518 /* validation of the parameters codec */
11519 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11523 /* customization of the encoding */
11524 j2k_setup_header_writting(p_j2k);
11527 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11537 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11538 OPJ_UINT32 p_tile_index,
11539 opj_stream_private_t *p_stream,
11540 opj_event_mgr_t * p_manager )
11543 if (p_tile_index != p_j2k->m_current_tile_number) {
11544 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11548 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);
11550 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11551 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11552 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11554 /* initialisation before tile encoding */
11555 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11565 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11567 OPJ_UINT32 i,j,k = 0;
11568 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11569 opj_image_comp_t * l_img_comp = 00;
11570 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11571 opj_image_t * l_image = 00;
11572 OPJ_UINT32 l_size_comp, l_remaining;
11573 OPJ_INT32 * l_src_ptr;
11574 l_tilec = p_tcd->tcd_image->tiles->comps;
11575 l_image = p_tcd->image;
11576 l_img_comp = l_image->comps;
11578 for (i=0;i<p_tcd->image->numcomps;++i) {
11579 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11580 l_remaining = l_img_comp->prec & 7; /* (%8) */
11585 if (l_size_comp == 3) {
11589 l_width = (l_tilec->x1 - l_tilec->x0);
11590 l_height = (l_tilec->y1 - l_tilec->y0);
11591 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11592 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11593 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11594 l_stride = l_image_width - l_width;
11595 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11597 switch (l_size_comp) {
11600 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11601 if (l_img_comp->sgnd) {
11602 for (j=0;j<l_height;++j) {
11603 for (k=0;k<l_width;++k) {
11604 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11608 l_src_ptr += l_stride;
11612 for (j=0;j<l_height;++j) {
11613 for (k=0;k<l_width;++k) {
11614 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11618 l_src_ptr += l_stride;
11622 p_data = (OPJ_BYTE*) l_dest_ptr;
11627 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11628 if (l_img_comp->sgnd) {
11629 for (j=0;j<l_height;++j) {
11630 for (k=0;k<l_width;++k) {
11631 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11633 l_src_ptr += l_stride;
11637 for (j=0;j<l_height;++j) {
11638 for (k=0;k<l_width;++k) {
11639 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11641 l_src_ptr += l_stride;
11645 p_data = (OPJ_BYTE*) l_dest_ptr;
11650 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11651 for (j=0;j<l_height;++j) {
11652 for (k=0;k<l_width;++k) {
11653 *(l_dest_ptr++) = *(l_src_ptr++);
11655 l_src_ptr += l_stride;
11658 p_data = (OPJ_BYTE*) l_dest_ptr;
11671 * @param p_j2k the jpeg2000 codec.
11672 * @param p_stream the stream to write data to.
11673 * @param p_manager the user event manager.
11675 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11677 OPJ_UINT32 p_data_size,
11678 opj_stream_private_t *p_stream,
11679 opj_event_mgr_t * p_manager )
11681 opj_tcd_v2_t * l_tcd = 00;
11682 opj_cp_v2_t * l_cp = 00;
11683 opj_tcp_v2_t * l_tcp = 00;
11684 OPJ_UINT32 l_nb_bytes_written;
11685 OPJ_BYTE * l_current_data = 00;
11686 OPJ_UINT32 l_tile_size = 0;
11687 OPJ_UINT32 l_available_data;
11689 /* preconditions */
11690 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11692 l_tcd = p_j2k->m_tcd;
11693 l_cp = &(p_j2k->m_cp);
11694 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11696 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11697 l_available_data = l_tile_size;
11698 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11700 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11701 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11705 l_nb_bytes_written = 0;
11706 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11709 l_current_data += l_nb_bytes_written;
11710 l_available_data -= l_nb_bytes_written;
11712 l_nb_bytes_written = 0;
11713 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11717 l_available_data -= l_nb_bytes_written;
11718 l_nb_bytes_written = l_tile_size - l_available_data;
11720 if ( opj_stream_write_data( p_stream,
11721 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11722 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11726 ++p_j2k->m_current_tile_number;
11733 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11734 * are valid. Developers wanting to extend the library can add their own validation procedures.
11736 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11738 /* preconditions */
11739 assert(p_j2k != 00);
11741 /* DEVELOPER CORNER, insert your custom procedures */
11742 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11744 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11745 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11748 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11749 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11750 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11754 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11755 * are valid. Developers wanting to extend the library can add their own validation procedures.
11757 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11759 /* preconditions */
11760 assert(p_j2k != 00);
11762 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11763 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11765 /* DEVELOPER CORNER, add your custom validation procedure */
11766 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11771 * Sets up the procedures to do on writing header.
11772 * Developers wanting to extend the library can add their own writing procedures.
11774 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11776 /* preconditions */
11777 assert(p_j2k != 00);
11779 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11780 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11781 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11782 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11783 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11786 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11787 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11788 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11790 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11791 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11795 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11797 if (p_j2k->m_cp.comment != 00) {
11798 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11801 /* DEVELOPER CORNER, insert your custom procedures */
11802 if (p_j2k->m_cp.rsiz & MCT) {
11803 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11805 /* End of Developer Corner */
11807 if (p_j2k->cstr_index) {
11808 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11811 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11812 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11816 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11818 OPJ_UINT32 * p_data_written,
11819 OPJ_UINT32 p_total_data_size,
11820 opj_stream_private_t *p_stream,
11821 struct opj_event_mgr * p_manager )
11824 OPJ_UINT32 l_nb_bytes_written = 0;
11825 OPJ_UINT32 l_current_nb_bytes_written;
11826 OPJ_BYTE * l_begin_data = 00;
11828 opj_tcp_v2_t *l_tcp = 00;
11829 opj_tcd_v2_t * l_tcd = 00;
11830 opj_cp_v2_t * l_cp = 00;
11832 l_tcd = p_j2k->m_tcd;
11833 l_cp = &(p_j2k->m_cp);
11834 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11836 l_tcd->cur_pino = 0;
11838 /*Get number of tile parts*/
11839 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11844 l_current_nb_bytes_written = 0;
11845 l_begin_data = p_data;
11846 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11851 l_nb_bytes_written += l_current_nb_bytes_written;
11852 p_data += l_current_nb_bytes_written;
11853 p_total_data_size -= l_current_nb_bytes_written;
11855 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11856 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11857 l_current_nb_bytes_written = 0;
11858 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11859 l_nb_bytes_written += l_current_nb_bytes_written;
11860 p_data += l_current_nb_bytes_written;
11861 p_total_data_size -= l_current_nb_bytes_written;
11863 l_current_nb_bytes_written = 0;
11864 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11865 l_nb_bytes_written += l_current_nb_bytes_written;
11866 p_data += l_current_nb_bytes_written;
11867 p_total_data_size -= l_current_nb_bytes_written;
11870 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11871 l_current_nb_bytes_written = 0;
11872 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11873 l_nb_bytes_written += l_current_nb_bytes_written;
11874 p_data += l_current_nb_bytes_written;
11875 p_total_data_size -= l_current_nb_bytes_written;
11879 l_current_nb_bytes_written = 0;
11880 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11884 l_nb_bytes_written += l_current_nb_bytes_written;
11885 * p_data_written = l_nb_bytes_written;
11887 /* Writing Psot in SOT marker */
11888 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11890 if (l_cp->m_specific_param.m_enc.m_cinema){
11891 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11897 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11899 OPJ_UINT32 * p_data_written,
11900 OPJ_UINT32 p_total_data_size,
11901 opj_stream_private_t *p_stream,
11902 struct opj_event_mgr * p_manager
11905 OPJ_UINT32 tilepartno=0;
11906 OPJ_UINT32 l_nb_bytes_written = 0;
11907 OPJ_UINT32 l_current_nb_bytes_written;
11908 OPJ_UINT32 l_part_tile_size;
11909 OPJ_UINT32 tot_num_tp;
11912 OPJ_BYTE * l_begin_data;
11913 opj_tcp_v2_t *l_tcp = 00;
11914 opj_tcd_v2_t * l_tcd = 00;
11915 opj_cp_v2_t * l_cp = 00;
11918 l_tcd = p_j2k->m_tcd;
11919 l_cp = &(p_j2k->m_cp);
11920 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11922 /*Get number of tile parts*/
11923 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11925 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11926 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11927 l_current_nb_bytes_written = 0;
11928 l_part_tile_size = 0;
11929 l_begin_data = p_data;
11931 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11935 l_nb_bytes_written += l_current_nb_bytes_written;
11936 p_data += l_current_nb_bytes_written;
11937 p_total_data_size -= l_current_nb_bytes_written;
11938 l_part_tile_size += l_nb_bytes_written;
11940 l_current_nb_bytes_written = 0;
11941 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11945 p_data += l_current_nb_bytes_written;
11946 l_nb_bytes_written += l_current_nb_bytes_written;
11947 p_total_data_size -= l_current_nb_bytes_written;
11948 l_part_tile_size += l_nb_bytes_written;
11950 /* Writing Psot in SOT marker */
11951 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11953 if (l_cp->m_specific_param.m_enc.m_cinema) {
11954 j2k_update_tlm(p_j2k,l_part_tile_size);
11957 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11960 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11961 l_tcd->cur_pino = pino;
11963 /*Get number of tile parts*/
11964 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11965 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11966 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11967 l_current_nb_bytes_written = 0;
11968 l_part_tile_size = 0;
11969 l_begin_data = p_data;
11971 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11975 l_nb_bytes_written += l_current_nb_bytes_written;
11976 p_data += l_current_nb_bytes_written;
11977 p_total_data_size -= l_current_nb_bytes_written;
11978 l_part_tile_size += l_current_nb_bytes_written;
11980 l_current_nb_bytes_written = 0;
11982 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11986 l_nb_bytes_written += l_current_nb_bytes_written;
11987 p_data += l_current_nb_bytes_written;
11988 p_total_data_size -= l_current_nb_bytes_written;
11989 l_part_tile_size += l_current_nb_bytes_written;
11991 /* Writing Psot in SOT marker */
11992 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11994 if (l_cp->m_specific_param.m_enc.m_cinema) {
11995 j2k_update_tlm(p_j2k,l_part_tile_size);
11998 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12002 *p_data_written = l_nb_bytes_written;
12008 * Writes the updated tlm.
12010 * @param p_stream the stream to write data to.
12011 * @param p_j2k J2K codec.
12012 * @param p_manager the user event manager.
12014 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
12015 struct opj_stream_private *p_stream,
12016 struct opj_event_mgr * p_manager )
12018 OPJ_UINT32 l_tlm_size;
12019 OPJ_SIZE_T l_tlm_position, l_current_position;
12021 /* preconditions */
12022 assert(p_j2k != 00);
12023 assert(p_manager != 00);
12024 assert(p_stream != 00);
12026 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12027 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12028 l_current_position = opj_stream_tell(p_stream);
12030 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12034 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) {
12038 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12047 * Ends the encoding, i.e. frees memory.
12049 * @param p_stream the stream to write data to.
12050 * @param p_j2k J2K codec.
12051 * @param p_manager the user event manager.
12053 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
12054 struct opj_stream_private *p_stream,
12055 struct opj_event_mgr * p_manager )
12057 /* preconditions */
12058 assert(p_j2k != 00);
12059 assert(p_manager != 00);
12060 assert(p_stream != 00);
12062 tcd_destroy_v2(p_j2k->m_tcd);
12065 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12066 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12067 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12068 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12071 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12072 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12073 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12076 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12082 * Destroys the memory associated with the decoding of headers.
12084 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12085 opj_stream_private_t *p_stream,
12086 opj_event_mgr_t * p_manager )
12088 /* preconditions */
12089 assert(p_j2k != 00);
12090 assert(p_stream != 00);
12091 assert(p_manager != 00);
12093 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12094 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12095 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12098 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12107 * @param p_stream the stream to write data to.
12108 * @param p_j2k J2K codec.
12109 * @param p_manager the user event manager.
12111 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12112 struct opj_stream_private *p_stream,
12113 struct opj_event_mgr * p_manager )
12115 opj_codestream_info_t * l_cstr_info = 00;
12117 /* preconditions */
12118 assert(p_j2k != 00);
12119 assert(p_manager != 00);
12120 assert(p_stream != 00);
12123 /* TODO mergeV2: check this part which use cstr_info */
12124 /*l_cstr_info = p_j2k->cstr_info;
12128 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));
12130 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12131 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12133 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12135 l_cstr_info->tw = p_j2k->m_cp.tw;
12136 l_cstr_info->th = p_j2k->m_cp.th;
12138 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12139 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12140 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12141 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12143 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12145 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12147 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12149 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12150 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12153 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12155 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12157 /*l_cstr_info->maxmarknum = 100;
12158 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12159 l_cstr_info->marknum = 0;
12162 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);
12166 * Creates a tile-coder decoder.
12168 * @param p_stream the stream to write data to.
12169 * @param p_j2k J2K codec.
12170 * @param p_manager the user event manager.
12172 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12173 struct opj_stream_private *p_stream,
12174 struct opj_event_mgr * p_manager )
12176 /* preconditions */
12177 assert(p_j2k != 00);
12178 assert(p_manager != 00);
12179 assert(p_stream != 00);
12181 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12183 if (! p_j2k->m_tcd) {
12184 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12188 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12189 tcd_destroy_v2(p_j2k->m_tcd);
12200 * @param p_j2k the jpeg2000 codec.
12201 * @param p_stream the stream to write data to.
12202 * @param p_manager the user event manager.
12204 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12205 OPJ_UINT32 p_tile_index,
12207 OPJ_UINT32 p_data_size,
12208 opj_stream_private_t *p_stream,
12209 opj_event_mgr_t * p_manager )
12211 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12212 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12216 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12217 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);