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 (
510 OPJ_BYTE * p_header_data,
511 OPJ_UINT32 p_header_size,
512 struct opj_event_mgr * p_manager
516 Write the COC marker (coding style component)
517 @param j2k J2K handle
518 @param compno Number of the component concerned by the information written
520 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
523 * Writes the COC marker (Coding style component)
525 * @param p_comp_number the index of the component to output.
526 * @param p_stream the stream to write data to.
527 * @param p_j2k J2K codec.
528 * @param p_manager the user event manager.
530 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
531 OPJ_UINT32 p_comp_number,
532 struct opj_stream_private *p_stream,
533 struct opj_event_mgr * p_manager
536 * Writes the COC marker (Coding style component)
538 * @param p_comp_no the index of the component to output.
539 * @param p_stream the stream to write data to.
540 * @param p_j2k J2K codec.
541 * @param p_manager the user event manager.
543 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
544 OPJ_UINT32 p_comp_no,
546 OPJ_UINT32 * p_data_written,
547 struct opj_event_mgr * p_manager );
550 * Gets the maximum size taken by a coc.
552 * @param p_j2k the jpeg2000 codec to use.
554 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
557 Read the COC marker (coding style component)
558 @param j2k J2K handle
560 static void j2k_read_coc(opj_j2k_t *j2k);
563 * Reads a COC marker (Coding Style Component)
564 * @param p_header_data the data contained in the COC box.
565 * @param p_j2k the jpeg2000 codec.
566 * @param p_header_size the size of the data contained in the COC marker.
567 * @param p_manager the user event manager.
569 static opj_bool j2k_read_coc_v2 (
571 OPJ_BYTE * p_header_data,
572 OPJ_UINT32 p_header_size,
573 struct opj_event_mgr * p_manager
577 Write the value concerning the specified component in the marker QCD and QCC
578 @param j2k J2K handle
579 @param compno Number of the component concerned by the information written
581 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
583 Read the value concerning the specified component in the marker QCD and QCC
584 @param j2k J2K handle
585 @param compno Number of the component concern by the information read
586 @param len Length of the information in the QCX part of the marker QCD/QCC
588 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
590 Write the QCD marker (quantization default)
591 @param j2k J2K handle
593 static void j2k_write_qcd(opj_j2k_t *j2k);
596 * Writes the QCD marker (quantization default)
598 * @param p_comp_number the index of the component to output.
599 * @param p_stream the stream to write data to.
600 * @param p_j2k J2K codec.
601 * @param p_manager the user event manager.
603 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
604 struct opj_stream_private *p_stream,
605 struct opj_event_mgr * p_manager );
608 Read the QCD marker (quantization default)
609 @param j2k J2K handle
611 static void j2k_read_qcd(opj_j2k_t *j2k);
614 * Reads a QCD marker (Quantization defaults)
615 * @param p_header_data the data contained in the QCD box.
616 * @param p_j2k the jpeg2000 codec.
617 * @param p_header_size the size of the data contained in the QCD marker.
618 * @param p_manager the user event manager.
620 static opj_bool j2k_read_qcd_v2 (
622 OPJ_BYTE * p_header_data,
623 OPJ_UINT32 p_header_size,
624 struct opj_event_mgr * p_manager
628 Write the QCC marker (quantization component)
629 @param j2k J2K handle
630 @param compno Number of the component concerned by the information written
632 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
635 * Writes the QCC marker (quantization component)
637 * @param p_comp_no the index of the component to output.
638 * @param p_stream the stream to write data to.
639 * @param p_j2k J2K codec.
640 * @param p_manager the user event manager.
642 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
643 OPJ_UINT32 p_comp_no,
644 struct opj_stream_private *p_stream,
645 struct opj_event_mgr * p_manager );
648 * Writes the QCC marker (quantization component)
650 * @param p_comp_no the index of the component to output.
651 * @param p_stream the stream to write data to.
652 * @param p_j2k J2K codec.
653 * @param p_manager the user event manager.
655 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
656 OPJ_UINT32 p_comp_no,
658 OPJ_UINT32 * p_data_written,
659 struct opj_event_mgr * p_manager );
662 * Gets the maximum size taken by a qcc.
664 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
667 Read the QCC marker (quantization component)
668 @param j2k J2K handle
670 static void j2k_read_qcc(opj_j2k_t *j2k);
672 * Reads a QCC marker (Quantization component)
673 * @param p_header_data the data contained in the QCC box.
674 * @param p_j2k the jpeg2000 codec.
675 * @param p_header_size the size of the data contained in the QCC marker.
676 * @param p_manager the user event manager.
678 static opj_bool j2k_read_qcc_v2(
680 OPJ_BYTE * p_header_data,
681 OPJ_UINT32 p_header_size,
682 struct opj_event_mgr * p_manager);
685 Write the POC marker (progression order change)
686 @param j2k J2K handle
688 static void j2k_write_poc(opj_j2k_t *j2k);
691 * Writes the POC marker (Progression Order Change)
693 * @param p_stream the stream to write data to.
694 * @param p_j2k J2K codec.
695 * @param p_manager the user event manager.
697 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
698 struct opj_stream_private *p_stream,
699 struct opj_event_mgr * p_manager );
702 * Writes the POC marker (Progression Order Change)
704 * @param p_stream the stream to write data to.
705 * @param p_j2k J2K codec.
706 * @param p_manager the user event manager.
708 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
710 OPJ_UINT32 * p_data_written,
711 struct opj_event_mgr * p_manager );
714 * Gets the maximum size taken by the writting of a POC.
716 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
719 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
721 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
724 * Gets the maximum size taken by the headers of the SOT.
726 * @param p_j2k the jpeg2000 codec to use.
728 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
731 Read the POC marker (progression order change)
732 @param j2k J2K handle
734 static void j2k_read_poc(opj_j2k_t *j2k);
736 * Reads a POC marker (Progression Order Change)
738 * @param p_header_data the data contained in the POC box.
739 * @param p_j2k the jpeg2000 codec.
740 * @param p_header_size the size of the data contained in the POC marker.
741 * @param p_manager the user event manager.
743 static opj_bool j2k_read_poc_v2 (
745 OPJ_BYTE * p_header_data,
746 OPJ_UINT32 p_header_size,
747 struct opj_event_mgr * p_manager
751 Read the CRG marker (component registration)
752 @param j2k J2K handle
754 static void j2k_read_crg(opj_j2k_t *j2k);
756 * Reads a CRG marker (Component registration)
758 * @param p_header_data the data contained in the TLM box.
759 * @param p_j2k the jpeg2000 codec.
760 * @param p_header_size the size of the data contained in the TLM marker.
761 * @param p_manager the user event manager.
763 static opj_bool j2k_read_crg_v2 (
765 OPJ_BYTE * p_header_data,
766 OPJ_UINT32 p_header_size,
767 struct opj_event_mgr * p_manager
770 Read the TLM marker (tile-part lengths)
771 @param j2k J2K handle
773 static void j2k_read_tlm(opj_j2k_t *j2k);
775 * Reads a TLM marker (Tile Length Marker)
777 * @param p_header_data the data contained in the TLM box.
778 * @param p_j2k the jpeg2000 codec.
779 * @param p_header_size the size of the data contained in the TLM marker.
780 * @param p_manager the user event manager.
782 static opj_bool j2k_read_tlm_v2 (
784 OPJ_BYTE * p_header_data,
785 OPJ_UINT32 p_header_size,
786 struct opj_event_mgr * p_manager
790 * Writes the updated tlm.
792 * @param p_stream the stream to write data to.
793 * @param p_j2k J2K codec.
794 * @param p_manager the user event manager.
796 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
797 struct opj_stream_private *p_stream,
798 struct opj_event_mgr * p_manager );
801 Read the PLM marker (packet length, main header)
802 @param j2k J2K handle
804 static void j2k_read_plm(opj_j2k_t *j2k);
807 * Reads a PLM marker (Packet length, main header marker)
809 * @param p_header_data the data contained in the TLM box.
810 * @param p_j2k the jpeg2000 codec.
811 * @param p_header_size the size of the data contained in the TLM marker.
812 * @param p_manager the user event manager.
814 static opj_bool j2k_read_plm_v2 (
816 OPJ_BYTE * p_header_data,
817 OPJ_UINT32 p_header_size,
818 struct opj_event_mgr * p_manager
821 Read the PLT marker (packet length, tile-part header)
822 @param j2k J2K handle
824 static void j2k_read_plt(opj_j2k_t *j2k);
826 * Reads a PLT marker (Packet length, tile-part header)
828 * @param p_header_data the data contained in the PLT box.
829 * @param p_j2k the jpeg2000 codec.
830 * @param p_header_size the size of the data contained in the PLT marker.
831 * @param p_manager the user event manager.
833 static opj_bool j2k_read_plt_v2 (
835 OPJ_BYTE * p_header_data,
836 OPJ_UINT32 p_header_size,
837 struct opj_event_mgr * p_manager
840 Read the PPM marker (packet packet headers, main header)
841 @param j2k J2K handle
843 static void j2k_read_ppm(opj_j2k_t *j2k);
845 * Reads a PPM marker (Packed packet headers, main header)
847 * @param p_header_data the data contained in the POC box.
848 * @param p_j2k the jpeg2000 codec.
849 * @param p_header_size the size of the data contained in the POC marker.
850 * @param p_manager the user event manager.
853 static opj_bool j2k_read_ppm_v2 (
855 OPJ_BYTE * p_header_data,
856 OPJ_UINT32 p_header_size,
857 struct opj_event_mgr * p_manager
861 static opj_bool j2k_read_ppm_v3 (
863 OPJ_BYTE * p_header_data,
864 OPJ_UINT32 p_header_size,
865 struct opj_event_mgr * p_manager
869 Read the PPT marker (packet packet headers, tile-part header)
870 @param j2k J2K handle
872 static void j2k_read_ppt(opj_j2k_t *j2k);
874 * Reads a PPT marker (Packed packet headers, tile-part header)
876 * @param p_header_data the data contained in the PPT box.
877 * @param p_j2k the jpeg2000 codec.
878 * @param p_header_size the size of the data contained in the PPT marker.
879 * @param p_manager the user event manager.
881 static opj_bool j2k_read_ppt_v2 (
883 OPJ_BYTE * p_header_data,
884 OPJ_UINT32 p_header_size,
885 struct opj_event_mgr * p_manager
888 Write the TLM marker (Mainheader)
889 @param j2k J2K handle
891 static void j2k_write_tlm(opj_j2k_t *j2k);
894 * Writes the TLM marker (Tile Length Marker)
896 * @param p_stream the stream to write data to.
897 * @param p_j2k J2K codec.
898 * @param p_manager the user event manager.
900 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
901 struct opj_stream_private *p_stream,
902 struct opj_event_mgr * p_manager );
905 Write the SOT marker (start of tile-part)
906 @param j2k J2K handle
908 static void j2k_write_sot(opj_j2k_t *j2k);
911 * Writes the SOT marker (Start of tile-part)
913 * @param p_stream the stream to write data to.
914 * @param p_j2k J2K codec.
915 * @param p_manager the user event manager.
917 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
919 OPJ_UINT32 * p_data_written,
920 const struct opj_stream_private *p_stream,
921 struct opj_event_mgr * p_manager );
924 * Reads a PPT marker (Packed packet headers, tile-part header)
926 * @param p_header_data the data contained in the PPT box.
927 * @param p_j2k the jpeg2000 codec.
928 * @param p_header_size the size of the data contained in the PPT marker.
929 * @param p_manager the user event manager.
931 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
932 OPJ_BYTE * p_header_data,
933 OPJ_UINT32 p_header_size,
934 opj_event_mgr_t * p_manager );
937 Write the SOD marker (start of data)
938 @param j2k J2K handle
939 @param tile_coder Pointer to a TCD handle
941 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
944 * Writes the SOD marker (Start of data)
946 * @param p_stream the stream to write data to.
947 * @param p_j2k J2K codec.
948 * @param p_manager the user event manager.
950 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
951 struct opj_tcd_v2 * p_tile_coder,
953 OPJ_UINT32 * p_data_written,
954 OPJ_UINT32 p_total_data_size,
955 const struct opj_stream_private *p_stream,
956 struct opj_event_mgr * p_manager );
959 * Reads a SOD marker (Start Of Data)
961 * @param p_header_data the data contained in the SOD box.
962 * @param p_j2k the jpeg2000 codec.
963 * @param p_header_size the size of the data contained in the SOD marker.
964 * @param p_manager the user event manager.
966 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
967 opj_stream_private_t *p_stream,
968 opj_event_mgr_t * p_manager );
971 * Updates the Tile Length Marker.
973 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
975 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
976 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
978 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
979 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
983 Write the RGN marker (region-of-interest)
984 @param j2k J2K handle
985 @param compno Number of the component concerned by the information written
986 @param tileno Number of the tile concerned by the information written
988 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
991 * Writes the RGN marker (Region Of Interest)
993 * @param p_tile_no the tile to output
994 * @param p_comp_no the component to output
995 * @param p_stream the stream to write data to.
996 * @param p_j2k J2K codec.
997 * @param p_manager the user event manager.
999 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
1000 OPJ_UINT32 p_tile_no,
1001 OPJ_UINT32 p_comp_no,
1002 struct opj_stream_private *p_stream,
1003 struct opj_event_mgr * p_manager );
1006 Read the RGN marker (region-of-interest)
1007 @param j2k J2K handle
1009 static void j2k_read_rgn(opj_j2k_t *j2k);
1012 * Reads a RGN marker (Region Of Interest)
1014 * @param p_header_data the data contained in the POC box.
1015 * @param p_j2k the jpeg2000 codec.
1016 * @param p_header_size the size of the data contained in the POC marker.
1017 * @param p_manager the user event manager.
1019 static opj_bool j2k_read_rgn_v2 (
1020 opj_j2k_v2_t *p_j2k,
1021 OPJ_BYTE * p_header_data,
1022 OPJ_UINT32 p_header_size,
1023 struct opj_event_mgr * p_manager
1027 * Writes the EOC marker (End of Codestream)
1029 * @param p_stream the stream to write data to.
1030 * @param p_j2k J2K codec.
1031 * @param p_manager the user event manager.
1033 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
1034 struct opj_stream_private *p_stream,
1035 struct opj_event_mgr * p_manager );
1038 Write the EOC marker (end of codestream)
1039 @param j2k J2K handle
1041 static void j2k_write_eoc(opj_j2k_t *j2k);
1043 Read the EOC marker (end of codestream)
1044 @param j2k J2K handle
1046 static void j2k_read_eoc(opj_j2k_t *j2k);
1049 * Reads a EOC marker (End Of Codestream)
1051 * @param p_header_data the data contained in the SOD box.
1052 * @param p_j2k the jpeg2000 codec.
1053 * @param p_header_size the size of the data contained in the SOD marker.
1054 * @param p_manager the user event manager.
1057 static opj_bool j2k_read_eoc_v2 (
1058 opj_j2k_v2_t *p_j2k,
1059 struct opj_stream_private *p_stream,
1060 struct opj_event_mgr * p_manager
1067 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1069 * @param p_stream the stream to write data to.
1070 * @param p_j2k J2K codec.
1071 * @param p_manager the user event manager.
1073 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1074 struct opj_stream_private *p_stream,
1075 struct opj_event_mgr * p_manager );
1080 * @param p_stream the stream to write data to.
1081 * @param p_j2k J2K codec.
1082 * @param p_manager the user event manager.
1084 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1085 struct opj_stream_private *p_stream,
1086 struct opj_event_mgr * p_manager );
1089 Read an unknown marker
1090 @param j2k J2K handle
1092 static void j2k_read_unk(opj_j2k_t *j2k);
1094 Add main header marker information
1095 @param cstr_info Codestream information structure
1096 @param type marker type
1097 @param pos byte offset of marker segment
1098 @param len length of marker segment
1100 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1102 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1104 Add tile header marker information
1105 @param tileno tile index number
1106 @param cstr_info Codestream information structure
1107 @param type marker type
1108 @param pos byte offset of marker segment
1109 @param len length of marker segment
1111 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1113 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);
1116 * Reads an unknown marker
1118 * @param p_stream the stream object to read from.
1119 * @param p_j2k the jpeg2000 codec.
1120 * @param p_manager the user event manager.
1122 * @return true if the marker could be deduced.
1124 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1125 struct opj_stream_private *p_stream,
1126 OPJ_UINT32 *output_marker,
1127 struct opj_event_mgr * p_manager );
1130 * Writes the MCT marker (Multiple Component Transform)
1132 * @param p_stream the stream to write data to.
1133 * @param p_j2k J2K codec.
1134 * @param p_manager the user event manager.
1136 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1137 opj_mct_data_t * p_mct_record,
1138 struct opj_stream_private *p_stream,
1139 struct opj_event_mgr * p_manager );
1142 * Reads a MCT marker (Multiple Component Transform)
1144 * @param p_header_data the data contained in the MCT box.
1145 * @param p_j2k the jpeg2000 codec.
1146 * @param p_header_size the size of the data contained in the MCT marker.
1147 * @param p_manager the user event manager.
1149 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1150 OPJ_BYTE * p_header_data,
1151 OPJ_UINT32 p_header_size,
1152 struct opj_event_mgr * p_manager );
1155 * Writes the MCC marker (Multiple Component Collection)
1157 * @param p_stream the stream to write data to.
1158 * @param p_j2k J2K codec.
1159 * @param p_manager the user event manager.
1161 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1162 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1163 struct opj_stream_private *p_stream,
1164 struct opj_event_mgr * p_manager );
1168 * Reads a MCC marker (Multiple Component Collection)
1170 * @param p_header_data the data contained in the MCC box.
1171 * @param p_j2k the jpeg2000 codec.
1172 * @param p_header_size the size of the data contained in the MCC marker.
1173 * @param p_manager the user event manager.
1175 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1176 OPJ_BYTE * p_header_data,
1177 OPJ_UINT32 p_header_size,
1178 struct opj_event_mgr * p_manager );
1181 * Writes the MCO marker (Multiple component transformation ordering)
1183 * @param p_stream the stream to write data to.
1184 * @param p_j2k J2K codec.
1185 * @param p_manager the user event manager.
1187 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1188 struct opj_stream_private *p_stream,
1189 struct opj_event_mgr * p_manager );
1192 * Reads a MCO marker (Multiple Component Transform Ordering)
1194 * @param p_header_data the data contained in the MCO box.
1195 * @param p_j2k the jpeg2000 codec.
1196 * @param p_header_size the size of the data contained in the MCO marker.
1197 * @param p_manager the user event manager.
1199 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1200 OPJ_BYTE * p_header_data,
1201 OPJ_UINT32 p_header_size,
1202 struct opj_event_mgr * p_manager );
1204 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1206 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1207 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1208 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1209 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1211 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1212 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1213 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1214 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1216 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1217 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1218 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1219 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1223 * Ends the encoding, i.e. frees memory.
1225 * @param p_stream the stream to write data to.
1226 * @param p_j2k J2K codec.
1227 * @param p_manager the user event manager.
1229 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1230 struct opj_stream_private *p_stream,
1231 struct opj_event_mgr * p_manager );
1234 * Writes the CBD marker (Component bit depth definition)
1236 * @param p_stream the stream to write data to.
1237 * @param p_j2k J2K codec.
1238 * @param p_manager the user event manager.
1240 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1241 struct opj_stream_private *p_stream,
1242 struct opj_event_mgr * p_manager );
1245 * Reads a CBD marker (Component bit depth definition)
1246 * @param p_header_data the data contained in the CBD box.
1247 * @param p_j2k the jpeg2000 codec.
1248 * @param p_header_size the size of the data contained in the CBD marker.
1249 * @param p_manager the user event manager.
1251 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1252 OPJ_BYTE * p_header_data,
1253 OPJ_UINT32 p_header_size,
1254 struct opj_event_mgr * p_manager);
1257 * Writes the image components.
1259 * @param p_stream the stream to write data to.
1260 * @param p_j2k J2K codec.
1261 * @param p_manager the user event manager.
1263 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1264 struct opj_stream_private *p_stream,
1265 struct opj_event_mgr * p_manager );
1268 * Writes regions of interests.
1270 * @param p_stream the stream to write data to.
1271 * @param p_j2k J2K codec.
1272 * @param p_manager the user event manager.
1274 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1275 struct opj_stream_private *p_stream,
1276 struct opj_event_mgr * p_manager );
1281 * @param p_stream the stream to write data to.
1282 * @param p_j2k J2K codec.
1283 * @param p_manager the user event manager.
1285 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1286 struct opj_stream_private *p_stream,
1287 struct opj_event_mgr * p_manager );
1290 * Checks the progression order changes values. Tells of the poc given as input are valid.
1291 * A nice message is outputted at errors.
1293 * @param p_pocs the progression order changes.
1294 * @param p_nb_pocs the number of progression order changes.
1295 * @param p_nb_resolutions the number of resolutions.
1296 * @param numcomps the number of components
1297 * @param numlayers the number of layers.
1299 * @return true if the pocs are valid.
1301 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1302 OPJ_UINT32 p_nb_pocs,
1303 OPJ_UINT32 p_nb_resolutions,
1304 OPJ_UINT32 numcomps,
1305 OPJ_UINT32 numlayers,
1306 opj_event_mgr_t * p_manager);
1309 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1311 * @param cp the coding parameters.
1312 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1313 * @param tileno the given tile.
1315 * @return the number of tile parts.
1317 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1319 /** mem allocation for TLM marker*/
1320 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1323 * Calculates the total number of tile parts needed by the encoder to
1324 * encode such an image. If not enough memory is available, then the function return false.
1326 * @param p_nb_tiles pointer that will hold the number of tile parts.
1327 * @param cp the coding parameters for the image.
1328 * @param image the image to encode.
1329 * @param p_j2k the p_j2k encoder.
1330 * @param p_manager the user event manager.
1332 * @return true if the function was successful, false else.
1334 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1336 OPJ_UINT32 * p_nb_tiles,
1338 opj_event_mgr_t * p_manager);
1340 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1342 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1344 static opj_codestream_index_t* j2k_create_cstr_index(void);
1350 /* ----------------------------------------------------------------------- */
1351 typedef struct j2k_prog_order{
1352 OPJ_PROG_ORDER enum_prog;
1356 j2k_prog_order_t j2k_prog_order_list[] = {
1362 {(OPJ_PROG_ORDER)-1, ""}
1370 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1378 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1380 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1382 j2k_read_int16_to_float,
1383 j2k_read_int32_to_float,
1384 j2k_read_float32_to_float,
1385 j2k_read_float64_to_float
1388 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1390 j2k_read_int16_to_int32,
1391 j2k_read_int32_to_int32,
1392 j2k_read_float32_to_int32,
1393 j2k_read_float64_to_int32
1396 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1398 j2k_write_float_to_int16,
1399 j2k_write_float_to_int32,
1400 j2k_write_float_to_float,
1401 j2k_write_float_to_float64
1404 typedef struct opj_dec_memory_marker_handler
1408 /** value of the state when the marker can appear */
1410 /** action linked to the marker */
1411 opj_bool (*handler) (
1412 opj_j2k_v2_t *p_j2k,
1413 OPJ_BYTE * p_header_data,
1414 OPJ_UINT32 p_header_size,
1415 struct opj_event_mgr * p_manager
1418 opj_dec_memory_marker_handler_t;
1420 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1423 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1424 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1425 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1426 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1427 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1428 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1429 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1430 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1431 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1432 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1433 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1434 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1435 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1437 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1438 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1439 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1440 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1441 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1442 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1444 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1445 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1446 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1447 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1448 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1449 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1450 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1451 {J2K_MS_SIZ, J2K_STATE_MHSIZ , opj_j2k_read_siz},
1452 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1453 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1454 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1455 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1456 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1458 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1459 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1460 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1461 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1462 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1463 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1465 #ifdef TODO_MS /* FIXME */
1466 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1467 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1468 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1469 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1471 #endif /* USE_JPWL */
1473 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1474 {J2K_MS_INSEC, 0, j2k_read_insec}
1475 #endif /* USE_JPSEC */
1476 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1481 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1483 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1484 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1488 for (i=0;i<p_nb_elem;++i) {
1489 opj_read_bytes(l_src_data,&l_temp,2);
1491 l_src_data+=sizeof(OPJ_INT16);
1493 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1497 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1499 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1500 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1504 for (i=0;i<p_nb_elem;++i) {
1505 opj_read_bytes(l_src_data,&l_temp,4);
1507 l_src_data+=sizeof(OPJ_INT32);
1509 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1513 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1515 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1516 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1520 for (i=0;i<p_nb_elem;++i) {
1521 opj_read_float(l_src_data,&l_temp);
1523 l_src_data+=sizeof(OPJ_FLOAT32);
1525 *(l_dest_data++) = l_temp;
1529 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1531 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1532 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1536 for (i=0;i<p_nb_elem;++i) {
1537 opj_read_double(l_src_data,&l_temp);
1539 l_src_data+=sizeof(OPJ_FLOAT64);
1541 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1545 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1547 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1548 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1552 for (i=0;i<p_nb_elem;++i) {
1553 opj_read_bytes(l_src_data,&l_temp,2);
1555 l_src_data+=sizeof(OPJ_INT16);
1557 *(l_dest_data++) = (OPJ_INT32) l_temp;
1561 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1563 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1564 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1568 for (i=0;i<p_nb_elem;++i) {
1569 opj_read_bytes(l_src_data,&l_temp,4);
1571 l_src_data+=sizeof(OPJ_INT32);
1573 *(l_dest_data++) = (OPJ_INT32) l_temp;
1577 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1579 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1580 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1584 for (i=0;i<p_nb_elem;++i) {
1585 opj_read_float(l_src_data,&l_temp);
1587 l_src_data+=sizeof(OPJ_FLOAT32);
1589 *(l_dest_data++) = (OPJ_INT32) l_temp;
1593 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1595 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1596 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1600 for (i=0;i<p_nb_elem;++i) {
1601 opj_read_double(l_src_data,&l_temp);
1603 l_src_data+=sizeof(OPJ_FLOAT64);
1605 *(l_dest_data++) = (OPJ_INT32) l_temp;
1609 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1611 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1612 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1616 for (i=0;i<p_nb_elem;++i) {
1617 l_temp = (OPJ_UINT32) *(l_src_data++);
1619 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1621 l_dest_data+=sizeof(OPJ_INT16);
1625 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1627 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1628 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1632 for (i=0;i<p_nb_elem;++i) {
1633 l_temp = (OPJ_UINT32) *(l_src_data++);
1635 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1637 l_dest_data+=sizeof(OPJ_INT32);
1641 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1643 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1644 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1648 for (i=0;i<p_nb_elem;++i) {
1649 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1651 opj_write_float(l_dest_data,l_temp);
1653 l_dest_data+=sizeof(OPJ_FLOAT32);
1657 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1659 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1660 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1664 for (i=0;i<p_nb_elem;++i) {
1665 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1667 opj_write_double(l_dest_data,l_temp);
1669 l_dest_data+=sizeof(OPJ_FLOAT64);
1675 * Converts an enum type progression order to string type.
1677 * @param prg_order the progression order to get.
1679 * @return the string representation of the given progression order.
1681 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1682 j2k_prog_order_t *po;
1683 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1684 if(po->enum_prog == prg_order){
1685 return po->str_prog;
1688 return po->str_prog;
1692 * Checks the progression order changes values. Tells if the poc given as input are valid.
1694 * @param p_pocs the progression order changes.
1695 * @param p_nb_pocs the number of progression order changes.
1696 * @param p_nb_resolutions the number of resolutions.
1697 * @param numcomps the number of components
1698 * @param numlayers the number of layers.
1699 * @param p_manager the user event manager.
1701 * @return true if the pocs are valid.
1703 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1704 OPJ_UINT32 p_nb_pocs,
1705 OPJ_UINT32 p_nb_resolutions,
1706 OPJ_UINT32 p_num_comps,
1707 OPJ_UINT32 p_num_layers,
1708 opj_event_mgr_t * p_manager)
1710 OPJ_UINT32* packet_array;
1711 OPJ_UINT32 index , resno, compno, layno;
1713 OPJ_UINT32 step_c = 1;
1714 OPJ_UINT32 step_r = p_num_comps * step_c;
1715 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1716 opj_bool loss = OPJ_FALSE;
1717 OPJ_UINT32 layno0 = 0;
1719 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1720 if (packet_array == 00) {
1721 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1724 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1726 if (p_nb_pocs == 0) {
1730 index = step_r * p_pocs->resno0;
1731 // take each resolution for each poc
1732 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1734 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1736 // take each comp of each resolution for each poc
1737 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1738 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1740 // and finally take each layer of each res of ...
1741 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1742 //index = step_r * resno + step_c * compno + step_l * layno;
1743 packet_array[comp_index] = 1;
1744 comp_index += step_l;
1747 res_index += step_c;
1754 // iterate through all the pocs
1755 for (i = 1; i < p_nb_pocs ; ++i) {
1756 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1758 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1759 index = step_r * p_pocs->resno0;
1761 // take each resolution for each poc
1762 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1763 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1765 // take each comp of each resolution for each poc
1766 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1767 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1769 // and finally take each layer of each res of ...
1770 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1771 //index = step_r * resno + step_c * compno + step_l * layno;
1772 packet_array[comp_index] = 1;
1773 comp_index += step_l;
1776 res_index += step_c;
1786 for (layno = 0; layno < p_num_layers ; ++layno) {
1787 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1788 for (compno = 0; compno < p_num_comps; ++compno) {
1789 loss |= (packet_array[index]!=1);
1790 //index = step_r * resno + step_c * compno + step_l * layno;
1797 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1800 opj_free(packet_array);
1805 /* ----------------------------------------------------------------------- */
1806 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1809 int tpnum=1,tpend=0;
1810 opj_tcp_t *tcp = &cp->tcps[tileno];
1811 prog = j2k_convert_progression_order(tcp->prg);
1816 if( cp->tp_flag == prog[i] ){
1817 tpend=1;cp->tp_pos=i;
1821 tpnum= tpnum * tcp->pocs[pino].compE;
1824 tpnum= tpnum * tcp->pocs[pino].resE;
1827 tpnum= tpnum * tcp->pocs[pino].prcE;
1830 tpnum= tpnum * tcp->pocs[pino].layE;
1842 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1844 * @param cp the coding parameters.
1845 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1846 * @param tileno the given tile.
1848 * @return the number of tile parts.
1850 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1852 const OPJ_CHAR *prog = 00;
1854 OPJ_UINT32 tpnum = 1;
1855 opj_tcp_v2_t *tcp = 00;
1856 opj_poc_t * l_current_poc = 00;
1859 assert(tileno < (cp->tw * cp->th));
1860 assert(pino < (cp->tcps[tileno].numpocs + 1));
1862 /* get the given tile coding parameter */
1863 tcp = &cp->tcps[tileno];
1866 l_current_poc = &(tcp->pocs[pino]);
1867 assert(l_current_poc != 0);
1869 /* get the progression order as a character string */
1870 prog = j2k_convert_progression_order(tcp->prg);
1871 assert(strlen(prog) > 0);
1873 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1877 /* component wise */
1879 tpnum *= l_current_poc->compE;
1881 /* resolution wise */
1883 tpnum *= l_current_poc->resE;
1887 tpnum *= l_current_poc->prcE;
1891 tpnum *= l_current_poc->layE;
1894 /* whould we split here ? */
1895 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1896 cp->m_specific_param.m_enc.m_tp_pos=i;
1908 /** mem allocation for TLM marker*/
1909 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1910 int pino,tileno,totnum_tp=0;
1912 OPJ_ARG_NOT_USED(img_numcomp);
1914 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1915 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1916 int cur_totnum_tp = 0;
1917 opj_tcp_t *tcp = &cp->tcps[tileno];
1918 for(pino = 0; pino <= tcp->numpocs; pino++) {
1920 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1921 if(!pi) { return -1;}
1922 tp_num = j2k_get_num_tp(cp,pino,tileno);
1923 totnum_tp = totnum_tp + tp_num;
1924 cur_totnum_tp = cur_totnum_tp + tp_num;
1925 pi_destroy(pi, cp, tileno);
1927 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1929 if (j2k->cstr_info) {
1930 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1931 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1939 * Calculates the total number of tile parts needed by the encoder to
1940 * encode such an image. If not enough memory is available, then the function return false.
1942 * @param p_nb_tiles pointer that will hold the number of tile parts.
1943 * @param cp the coding parameters for the image.
1944 * @param image the image to encode.
1945 * @param p_j2k the p_j2k encoder.
1946 * @param p_manager the user event manager.
1948 * @return true if the function was successful, false else.
1950 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1952 OPJ_UINT32 * p_nb_tiles,
1954 opj_event_mgr_t * p_manager)
1956 OPJ_UINT32 pino,tileno;
1957 OPJ_UINT32 l_nb_tiles;
1961 assert(p_nb_tiles != 00);
1963 assert(image != 00);
1964 assert(p_j2k != 00);
1965 assert(p_manager != 00);
1967 l_nb_tiles = cp->tw * cp->th;
1972 /* TODO mergeV2: check this part which use cstr_info */
1973 /*if (p_j2k->cstr_info) {
1974 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1976 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1977 OPJ_UINT32 cur_totnum_tp = 0;
1979 pi_update_encoding_parameters(image,cp,tileno);
1981 for (pino = 0; pino <= tcp->numpocs; ++pino)
1983 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1985 *p_nb_tiles = *p_nb_tiles + tp_num;
1987 cur_totnum_tp += tp_num;
1990 tcp->m_nb_tile_parts = cur_totnum_tp;
1992 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1993 if (l_info_tile_ptr->tp == 00) {
1997 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1999 l_info_tile_ptr->num_tps = cur_totnum_tp;
2006 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2007 OPJ_UINT32 cur_totnum_tp = 0;
2009 pi_update_encoding_parameters(image,cp,tileno);
2011 for (pino = 0; pino <= tcp->numpocs; ++pino) {
2012 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2014 *p_nb_tiles = *p_nb_tiles + tp_num;
2016 cur_totnum_tp += tp_num;
2018 tcp->m_nb_tile_parts = cur_totnum_tp;
2027 static void j2k_write_soc(opj_j2k_t *j2k) {
2028 opj_cio_t *cio = j2k->cio;
2029 cio_write(cio, J2K_MS_SOC, 2);
2032 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2037 /* update markers struct */
2038 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2039 #endif /* USE_JPWL */
2044 * Writes the SOC marker (Start Of Codestream)
2046 * @param p_stream the stream to write data to.
2047 * @param p_j2k J2K codec.
2048 * @param p_manager the user event manager.
2050 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2051 struct opj_stream_private *p_stream,
2052 struct opj_event_mgr * p_manager )
2054 /* 2 bytes will be written */
2055 OPJ_BYTE * l_start_stream = 00;
2058 assert(p_stream != 00);
2059 assert(p_j2k != 00);
2060 assert(p_manager != 00);
2062 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2064 /* write SOC identifier */
2065 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2067 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2073 /* update markers struct */
2075 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2077 assert( 0 && "TODO" );
2078 #endif /* USE_JPWL */
2087 * Reads a SOC marker (Start of Codestream)
2088 * @param p_header_data the data contained in the SOC box.
2089 * @param jp2 the jpeg2000 file codec.
2090 * @param p_header_size the size of the data contained in the SOC marker.
2091 * @param p_manager the user event manager.
2093 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2094 struct opj_stream_private *p_stream,
2095 struct opj_event_mgr * p_manager )
2097 OPJ_BYTE l_data [2];
2098 OPJ_UINT32 l_marker;
2101 assert(p_j2k != 00);
2102 assert(p_manager != 00);
2103 assert(p_stream != 00);
2105 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2109 opj_read_bytes(l_data,&l_marker,2);
2110 if (l_marker != J2K_MS_SOC) {
2114 /* Next marker should be a SIZ marker in the main header */
2115 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2117 /* FIXME move it in a index structure included in p_j2k*/
2118 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2120 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2122 /* Add the marker to the codestream index*/
2123 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2128 static void j2k_write_siz(opj_j2k_t *j2k) {
2132 opj_cio_t *cio = j2k->cio;
2133 opj_image_t *image = j2k->image;
2134 opj_cp_t *cp = j2k->cp;
2136 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2137 lenp = cio_tell(cio);
2139 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2140 cio_write(cio, image->x1, 4); /* Xsiz */
2141 cio_write(cio, image->y1, 4); /* Ysiz */
2142 cio_write(cio, image->x0, 4); /* X0siz */
2143 cio_write(cio, image->y0, 4); /* Y0siz */
2144 cio_write(cio, cp->tdx, 4); /* XTsiz */
2145 cio_write(cio, cp->tdy, 4); /* YTsiz */
2146 cio_write(cio, cp->tx0, 4); /* XT0siz */
2147 cio_write(cio, cp->ty0, 4); /* YT0siz */
2148 cio_write(cio, image->numcomps, 2); /* Csiz */
2149 for (i = 0; i < image->numcomps; i++) {
2150 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2151 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2152 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2154 len = cio_tell(cio) - lenp;
2155 cio_seek(cio, lenp);
2156 cio_write(cio, len, 2); /* Lsiz */
2157 cio_seek(cio, lenp + len);
2160 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2164 * Writes the SIZ marker (image and tile size)
2166 * @param p_stream the stream to write data to.
2167 * @param p_j2k J2K codec.
2168 * @param p_manager the user event manager.
2170 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2171 struct opj_stream_private *p_stream,
2172 struct opj_event_mgr * p_manager )
2175 OPJ_UINT32 l_size_len;
2176 OPJ_BYTE * l_current_ptr;
2177 opj_image_t * l_image = 00;
2178 opj_cp_v2_t *cp = 00;
2179 opj_image_comp_t * l_img_comp = 00;
2182 assert(p_stream != 00);
2183 assert(p_j2k != 00);
2184 assert(p_manager != 00);
2186 l_image = p_j2k->m_private_image;
2187 cp = &(p_j2k->m_cp);
2188 l_size_len = 40 + 3 * l_image->numcomps;
2189 l_img_comp = l_image->comps;
2191 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2193 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2194 = (OPJ_BYTE*)opj_realloc(
2195 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2197 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2201 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2204 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2206 /* write SOC identifier */
2207 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2210 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2213 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2216 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2219 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2222 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2225 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2228 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2231 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2234 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2237 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2240 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2243 for (i = 0; i < l_image->numcomps; ++i) {
2244 /* TODO here with MCT ? */
2245 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2248 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2251 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2257 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) {
2267 * Reads a SIZ marker (image and tile size)
2268 * @param p_header_data the data contained in the SIZ box.
2269 * @param jp2 the jpeg2000 file codec.
2270 * @param p_header_size the size of the data contained in the SIZ marker.
2271 * @param p_manager the user event manager.
2273 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2274 OPJ_BYTE * p_header_data,
2275 OPJ_UINT32 p_header_size,
2276 opj_event_mgr_t * p_manager
2279 OPJ_UINT32 l_size, i;
2280 OPJ_UINT32 l_nb_comp;
2281 OPJ_UINT32 l_nb_comp_remain;
2282 OPJ_UINT32 l_remaining_size;
2283 OPJ_UINT32 l_nb_tiles;
2285 opj_image_t *l_image = 00;
2286 opj_cp_v2_t *l_cp = 00;
2287 opj_image_comp_t * l_img_comp = 00;
2288 opj_tcp_v2_t * l_current_tile_param = 00;
2291 assert(p_j2k != 00);
2292 assert(p_manager != 00);
2293 assert(p_header_data != 00);
2295 l_image = p_j2k->m_private_image;
2296 l_cp = &(p_j2k->m_cp);
2298 /* minimum size == 39 - 3 (= minimum component parameter) */
2299 if (p_header_size < 36) {
2300 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2304 l_remaining_size = p_header_size - 36;
2305 l_nb_comp = l_remaining_size / 3;
2306 l_nb_comp_remain = l_remaining_size % 3;
2307 if (l_nb_comp_remain != 0){
2308 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2312 l_size = p_header_size + 2; /* Lsiz */
2314 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2316 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2317 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2319 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2321 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2323 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2325 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2327 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2329 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2331 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2333 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2336 l_image->numcomps = (OPJ_UINT16) l_tmp;
2338 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2342 if (l_image->numcomps != l_nb_comp) {
2343 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);
2348 if (l_cp->correct) {
2349 /* if JPWL is on, we check whether TX errors have damaged
2350 too much the SIZ parameters */
2351 if (!(l_image->x1 * l_image->y1)) {
2352 opj_event_msg_v2(p_manager, EVT_ERROR,
2353 "JPWL: bad image size (%d x %d)\n",
2354 l_image->x1, l_image->y1);
2355 if (!JPWL_ASSUME || JPWL_ASSUME) {
2356 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2361 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2362 if (l_image->numcomps != ((len - 38) / 3)) {
2363 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2364 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2365 l_image->numcomps, ((len - 38) / 3));
2367 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2370 */ /* we try to correct */
2371 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2372 if (l_image->numcomps < ((len - 38) / 3)) {
2373 len = 38 + 3 * l_image->numcomps;
2374 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2377 l_image->numcomps = ((len - 38) / 3);
2378 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2384 /* update components number in the jpwl_exp_comps filed */
2385 l_cp->exp_comps = l_image->numcomps;
2387 #endif /* USE_JPWL */
2389 /* Allocate the resulting image components */
2390 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2391 if (l_image->comps == 00){
2392 l_image->numcomps = 0;
2393 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2397 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2398 l_img_comp = l_image->comps;
2400 /* Read the component information */
2401 for (i = 0; i < l_image->numcomps; ++i){
2403 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2405 l_img_comp->prec = (tmp & 0x7f) + 1;
2406 l_img_comp->sgnd = tmp >> 7;
2407 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2409 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2410 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2412 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2415 if (l_cp->correct) {
2416 /* if JPWL is on, we check whether TX errors have damaged
2417 too much the SIZ parameters, again */
2418 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2419 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2420 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2421 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2423 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2426 /* we try to correct */
2427 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2428 if (!l_image->comps[i].dx) {
2429 l_image->comps[i].dx = 1;
2430 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2431 i, l_image->comps[i].dx);
2433 if (!l_image->comps[i].dy) {
2434 l_image->comps[i].dy = 1;
2435 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2436 i, l_image->comps[i].dy);
2440 #endif /* USE_JPWL */
2441 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2442 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2446 /* Compute the number of tiles */
2447 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2448 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2449 l_nb_tiles = l_cp->tw * l_cp->th;
2451 /* Define the tiles which will be decoded */
2452 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2453 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;
2454 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;
2455 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);
2456 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);
2459 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2460 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2461 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2462 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2466 if (l_cp->correct) {
2467 /* if JPWL is on, we check whether TX errors have damaged
2468 too much the SIZ parameters */
2469 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2470 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2471 "JPWL: bad number of tiles (%d x %d)\n",
2472 l_cp->tw, l_cp->th);
2474 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2477 /* we try to correct */
2478 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2481 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2484 if (l_cp->tw > l_cp->max_tiles) {
2486 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2487 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2488 l_cp->max_tiles, l_cp->tw);
2492 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2495 if (l_cp->th > l_cp->max_tiles) {
2497 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2498 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2499 l_cp->max_tiles, l_cp->th);
2503 #endif /* USE_JPWL */
2505 /* memory allocations */
2506 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2507 if (l_cp->tcps == 00) {
2508 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2511 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2514 if (l_cp->correct) {
2516 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2517 "JPWL: could not alloc tcps field of cp\n");
2518 if (!JPWL_ASSUME || JPWL_ASSUME) {
2519 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2524 #endif /* USE_JPWL */
2526 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2527 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2528 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2529 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2532 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2534 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2535 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2537 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2538 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2541 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));
2542 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2544 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2545 (opj_simple_mcc_decorrelation_data_t*)
2546 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2548 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2549 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2552 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));
2553 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2555 /* set up default dc level shift */
2556 for (i=0;i<l_image->numcomps;++i) {
2557 if (! l_image->comps[i].sgnd) {
2558 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2562 l_current_tile_param = l_cp->tcps;
2563 for (i = 0; i < l_nb_tiles; ++i) {
2564 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2565 if (l_current_tile_param->tccps == 00) {
2566 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2569 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2571 ++l_current_tile_param;
2574 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2575 opj_image_comp_header_update(l_image,l_cp);
2582 static void j2k_write_com(opj_j2k_t *j2k) {
2586 if(j2k->cp->comment) {
2587 opj_cio_t *cio = j2k->cio;
2588 char *comment = j2k->cp->comment;
2590 cio_write(cio, J2K_MS_COM, 2);
2591 lenp = cio_tell(cio);
2593 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2594 for (i = 0; i < strlen(comment); i++) {
2595 cio_write(cio, comment[i], 1);
2597 len = cio_tell(cio) - lenp;
2598 cio_seek(cio, lenp);
2599 cio_write(cio, len, 2);
2600 cio_seek(cio, lenp + len);
2604 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2610 * Writes the COM marker (comment)
2612 * @param p_stream the stream to write data to.
2613 * @param p_j2k J2K codec.
2614 * @param p_manager the user event manager.
2616 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2617 struct opj_stream_private *p_stream,
2618 struct opj_event_mgr * p_manager )
2620 OPJ_UINT32 l_comment_size;
2621 OPJ_UINT32 l_total_com_size;
2622 const OPJ_CHAR *l_comment;
2623 OPJ_BYTE * l_current_ptr = 00;
2626 assert(p_j2k != 00);
2627 assert(p_stream != 00);
2628 assert(p_manager != 00);
2630 l_comment = p_j2k->m_cp.comment;
2631 l_comment_size = strlen(l_comment);
2632 l_total_com_size = l_comment_size + 6;
2634 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2635 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2636 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2639 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2643 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2646 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2648 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2651 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2654 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2657 memcpy( l_current_ptr,l_comment,l_comment_size);
2659 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) {
2666 static void j2k_read_com(opj_j2k_t *j2k) {
2669 opj_cio_t *cio = j2k->cio;
2671 len = cio_read(cio, 2);
2672 cio_skip(cio, len - 2);
2675 * Reads a COM marker (comments)
2676 * @param p_header_data the data contained in the COM box.
2677 * @param jp2 the jpeg2000 file codec.
2678 * @param p_header_size the size of the data contained in the COM marker.
2679 * @param p_manager the user event manager.
2681 opj_bool j2k_read_com_v2 (
2682 opj_j2k_v2_t *p_j2k,
2683 OPJ_BYTE * p_header_data,
2684 OPJ_UINT32 p_header_size,
2685 struct opj_event_mgr * p_manager
2689 assert(p_j2k != 00);
2690 assert(p_manager != 00);
2691 assert(p_header_data != 00);
2692 (void)p_header_size;
2697 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2700 opj_cp_t *cp = j2k->cp;
2701 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2702 opj_tccp_t *tccp = &tcp->tccps[compno];
2703 opj_cio_t *cio = j2k->cio;
2705 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2706 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2707 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2708 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2709 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2711 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2712 for (i = 0; i < tccp->numresolutions; i++) {
2713 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2718 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2721 opj_cp_t *cp = j2k->cp;
2722 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2723 opj_tccp_t *tccp = &tcp->tccps[compno];
2724 opj_cio_t *cio = j2k->cio;
2726 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2728 /* If user wants to remove more resolutions than the codestream contains, return error*/
2729 assert(cp->reduce >= 0);
2730 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2731 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2732 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2733 j2k->state |= J2K_STATE_ERR;
2736 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2737 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2738 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2739 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2740 if (tccp->csty & J2K_CP_CSTY_PRT) {
2741 for (i = 0; i < tccp->numresolutions; i++) {
2742 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2743 tccp->prcw[i] = tmp & 0xf;
2744 tccp->prch[i] = tmp >> 4;
2749 if(j2k->cstr_info && compno == 0) {
2750 for (i = 0; i < tccp->numresolutions; i++) {
2751 if (tccp->csty & J2K_CP_CSTY_PRT) {
2752 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2753 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2756 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2757 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2764 static void j2k_write_cod(opj_j2k_t *j2k) {
2765 opj_cp_t *cp = NULL;
2766 opj_tcp_t *tcp = NULL;
2769 opj_cio_t *cio = j2k->cio;
2771 cio_write(cio, J2K_MS_COD, 2); /* COD */
2773 lenp = cio_tell(cio);
2777 tcp = &cp->tcps[j2k->curtileno];
2779 cio_write(cio, tcp->csty, 1); /* Scod */
2780 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2781 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2782 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2784 j2k_write_cox(j2k, 0);
2785 len = cio_tell(cio) - lenp;
2786 cio_seek(cio, lenp);
2787 cio_write(cio, len, 2); /* Lcod */
2788 cio_seek(cio, lenp + len);
2791 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2796 * Writes the COD marker (Coding style default)
2798 * @param p_stream the stream to write data to.
2799 * @param p_j2k J2K codec.
2800 * @param p_manager the user event manager.
2802 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2803 struct opj_stream_private *p_stream,
2804 struct opj_event_mgr * p_manager )
2806 opj_cp_v2_t *l_cp = 00;
2807 opj_tcp_v2_t *l_tcp = 00;
2808 OPJ_UINT32 l_code_size,l_remaining_size;
2809 OPJ_BYTE * l_current_data = 00;
2812 assert(p_j2k != 00);
2813 assert(p_manager != 00);
2814 assert(p_stream != 00);
2816 l_cp = &(p_j2k->m_cp);
2817 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2818 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2819 l_remaining_size = l_code_size;
2821 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2822 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2823 = (OPJ_BYTE*)opj_realloc(
2824 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2827 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2831 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2834 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2836 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2837 l_current_data += 2;
2839 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2840 l_current_data += 2;
2842 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2845 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2848 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2851 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2854 l_remaining_size -= 9;
2856 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2857 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2861 if (l_remaining_size != 0) {
2862 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2866 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) {
2875 * Reads a COD marker (Coding Styke defaults)
2876 * @param p_header_data the data contained in the COD box.
2877 * @param p_j2k the jpeg2000 codec.
2878 * @param p_header_size the size of the data contained in the COD marker.
2879 * @param p_manager the user event manager.
2881 opj_bool j2k_read_cod_v2 (
2882 opj_j2k_v2_t *p_j2k,
2883 OPJ_BYTE * p_header_data,
2884 OPJ_UINT32 p_header_size,
2885 struct opj_event_mgr * p_manager
2891 opj_cp_v2_t *l_cp = 00;
2892 opj_tcp_v2_t *l_tcp = 00;
2893 opj_image_t *l_image = 00;
2896 assert(p_header_data != 00);
2897 assert(p_j2k != 00);
2898 assert(p_manager != 00);
2900 l_image = p_j2k->m_private_image;
2901 l_cp = &(p_j2k->m_cp);
2903 /* If we are in the first tile-part header of the current tile */
2904 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2905 &l_cp->tcps[p_j2k->m_current_tile_number] :
2906 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2908 /* Make sure room is sufficient */
2909 if (p_header_size < 5) {
2910 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2914 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2916 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2918 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2919 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2922 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2923 if (l_cp->m_specific_param.m_dec.m_layer) {
2924 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2927 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2930 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2934 for (i = 0; i < l_image->numcomps; ++i) {
2935 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2938 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2939 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2943 if (p_header_size != 0) {
2944 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2948 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2949 j2k_copy_tile_component_parameters(p_j2k);
2952 #ifdef WIP_REMOVE_MSD
2953 if (p_j2k->cstr_info) {
2954 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2955 p_j2k->cstr_info->prog = l_tcp->prg;
2956 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2957 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2958 for (i = 0; i < l_image->numcomps; ++i) {
2959 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2967 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2970 opj_cp_t *cp = j2k->cp;
2971 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2972 opj_image_t *image = j2k->image;
2973 opj_cio_t *cio = j2k->cio;
2975 cio_write(cio, J2K_MS_COC, 2); /* COC */
2976 lenp = cio_tell(cio);
2978 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2979 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2980 j2k_write_cox(j2k, compno);
2981 len = cio_tell(cio) - lenp;
2982 cio_seek(cio, lenp);
2983 cio_write(cio, len, 2); /* Lcoc */
2984 cio_seek(cio, lenp + len);
2988 * Writes the COC marker (Coding style component)
2990 * @param p_comp_no the index of the component to output.
2991 * @param p_stream the stream to write data to.
2992 * @param p_j2k J2K codec.
2993 * @param p_manager the user event manager.
2995 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2996 OPJ_UINT32 p_comp_no,
2997 struct opj_stream_private *p_stream,
2998 struct opj_event_mgr * p_manager )
3000 OPJ_UINT32 l_coc_size,l_remaining_size;
3001 OPJ_UINT32 l_comp_room;
3004 assert(p_j2k != 00);
3005 assert(p_manager != 00);
3006 assert(p_stream != 00);
3008 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3010 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3012 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3013 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3014 = (OPJ_BYTE*)opj_realloc(
3015 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3017 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3021 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3024 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);
3026 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) {
3034 * Writes the COC marker (Coding style component)
3036 * @param p_comp_no the index of the component to output.
3037 * @param p_stream the stream to write data to.
3038 * @param p_j2k J2K codec.
3039 * @param p_manager the user event manager.
3041 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3042 OPJ_UINT32 p_comp_no,
3044 OPJ_UINT32 * p_data_written,
3045 struct opj_event_mgr * p_manager )
3047 opj_cp_v2_t *l_cp = 00;
3048 opj_tcp_v2_t *l_tcp = 00;
3049 OPJ_UINT32 l_coc_size,l_remaining_size;
3050 OPJ_BYTE * l_current_data = 00;
3051 opj_image_t *l_image = 00;
3052 OPJ_UINT32 l_comp_room;
3055 assert(p_j2k != 00);
3056 assert(p_manager != 00);
3058 l_cp = &(p_j2k->m_cp);
3059 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3060 l_image = p_j2k->m_private_image;
3061 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3063 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3064 l_remaining_size = l_coc_size;
3066 l_current_data = p_data;
3068 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3069 l_current_data += 2;
3071 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3072 l_current_data += 2;
3074 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3075 l_current_data+=l_comp_room;
3077 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3080 l_remaining_size -= (5 + l_comp_room);
3081 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3082 * p_data_written = l_coc_size;
3086 * Gets the maximum size taken by a coc.
3088 * @param p_j2k the jpeg2000 codec to use.
3090 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3093 OPJ_UINT32 l_nb_comp;
3094 OPJ_UINT32 l_nb_tiles;
3095 OPJ_UINT32 l_max = 0;
3099 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3100 l_nb_comp = p_j2k->m_private_image->numcomps;
3102 for (i=0;i<l_nb_tiles;++i) {
3103 for (j=0;j<l_nb_comp;++j) {
3104 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3111 static void j2k_read_coc(opj_j2k_t *j2k) {
3114 opj_cp_t *cp = j2k->cp;
3115 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3116 opj_image_t *image = j2k->image;
3117 opj_cio_t *cio = j2k->cio;
3119 len = cio_read(cio, 2); /* Lcoc */
3120 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3121 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
3122 j2k_read_cox(j2k, compno);
3126 * Reads a COC marker (Coding Style Component)
3127 * @param p_header_data the data contained in the COC box.
3128 * @param p_j2k the jpeg2000 codec.
3129 * @param p_header_size the size of the data contained in the COC marker.
3130 * @param p_manager the user event manager.
3132 opj_bool j2k_read_coc_v2 (
3133 opj_j2k_v2_t *p_j2k,
3134 OPJ_BYTE * p_header_data,
3135 OPJ_UINT32 p_header_size,
3136 struct opj_event_mgr * p_manager
3139 opj_cp_v2_t *l_cp = NULL;
3140 opj_tcp_v2_t *l_tcp = NULL;
3141 opj_image_t *l_image = NULL;
3142 OPJ_UINT32 l_comp_room;
3143 OPJ_UINT32 l_comp_no;
3146 assert(p_header_data != 00);
3147 assert(p_j2k != 00);
3148 assert(p_manager != 00);
3150 l_cp = &(p_j2k->m_cp);
3151 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3152 &l_cp->tcps[p_j2k->m_current_tile_number] :
3153 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3154 l_image = p_j2k->m_private_image;
3156 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3158 /* make sure room is sufficient*/
3159 if (p_header_size < l_comp_room + 1) {
3160 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3163 p_header_size -= l_comp_room + 1;
3165 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3166 p_header_data += l_comp_room;
3167 if (l_comp_no >= l_image->numcomps) {
3168 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3172 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3175 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3176 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3180 if (p_header_size != 0) {
3181 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3187 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3188 int bandno, numbands;
3191 opj_cp_t *cp = j2k->cp;
3192 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3193 opj_tccp_t *tccp = &tcp->tccps[compno];
3194 opj_cio_t *cio = j2k->cio;
3196 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3197 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3199 for (bandno = 0; bandno < numbands; bandno++) {
3200 expn = tccp->stepsizes[bandno].expn;
3201 mant = tccp->stepsizes[bandno].mant;
3203 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3204 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3206 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3211 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3213 int bandno, numbands;
3215 opj_cp_t *cp = j2k->cp;
3216 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3217 opj_tccp_t *tccp = &tcp->tccps[compno];
3218 opj_cio_t *cio = j2k->cio;
3220 tmp = cio_read(cio, 1); /* Sqcx */
3221 tccp->qntsty = tmp & 0x1f;
3222 tccp->numgbits = tmp >> 5;
3223 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3224 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3227 if (j2k->cp->correct) {
3229 /* if JPWL is on, we check whether there are too many subbands */
3230 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3231 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3232 "JPWL: bad number of subbands in Sqcx (%d)\n",
3235 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3238 /* we try to correct */
3240 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3241 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3248 /* We check whether there are too many subbands */
3249 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3250 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3251 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3252 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3255 #endif /* USE_JPWL */
3257 for (bandno = 0; bandno < numbands; bandno++) {
3259 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3260 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3263 tmp = cio_read(cio, 2); /* SPqcx_i */
3267 if (bandno < J2K_MAXBANDS){
3268 tccp->stepsizes[bandno].expn = expn;
3269 tccp->stepsizes[bandno].mant = mant;
3273 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3274 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3275 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3276 tccp->stepsizes[bandno].expn =
3277 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3278 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3279 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3285 static void j2k_write_qcd(opj_j2k_t *j2k) {
3288 opj_cio_t *cio = j2k->cio;
3290 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3291 lenp = cio_tell(cio);
3293 j2k_write_qcx(j2k, 0);
3294 len = cio_tell(cio) - lenp;
3295 cio_seek(cio, lenp);
3296 cio_write(cio, len, 2); /* Lqcd */
3297 cio_seek(cio, lenp + len);
3300 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3304 * Writes the QCD marker (quantization default)
3306 * @param p_comp_number the index of the component to output.
3307 * @param p_stream the stream to write data to.
3308 * @param p_j2k J2K codec.
3309 * @param p_manager the user event manager.
3311 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3312 struct opj_stream_private *p_stream,
3313 struct opj_event_mgr * p_manager )
3315 opj_cp_v2_t *l_cp = 00;
3316 opj_tcp_v2_t *l_tcp = 00;
3317 OPJ_UINT32 l_qcd_size,l_remaining_size;
3318 OPJ_BYTE * l_current_data = 00;
3321 assert(p_j2k != 00);
3322 assert(p_manager != 00);
3323 assert(p_stream != 00);
3325 l_cp = &(p_j2k->m_cp);
3326 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3327 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3328 l_remaining_size = l_qcd_size;
3330 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3331 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3332 = (OPJ_BYTE*)opj_realloc(
3333 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3336 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3340 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3343 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3345 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3346 l_current_data += 2;
3348 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3349 l_current_data += 2;
3351 l_remaining_size -= 4;
3353 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3354 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3358 if (l_remaining_size != 0) {
3359 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3363 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) {
3371 static void j2k_read_qcd(opj_j2k_t *j2k) {
3375 opj_cio_t *cio = j2k->cio;
3376 opj_image_t *image = j2k->image;
3378 len = cio_read(cio, 2); /* Lqcd */
3379 pos = cio_tell(cio);
3380 for (i = 0; i < image->numcomps; i++) {
3382 j2k_read_qcx(j2k, i, len - 2);
3387 * Reads a QCD marker (Quantization defaults)
3388 * @param p_header_data the data contained in the QCD box.
3389 * @param p_j2k the jpeg2000 codec.
3390 * @param p_header_size the size of the data contained in the QCD marker.
3391 * @param p_manager the user event manager.
3393 opj_bool j2k_read_qcd_v2 (
3394 opj_j2k_v2_t *p_j2k,
3395 OPJ_BYTE * p_header_data,
3396 OPJ_UINT32 p_header_size,
3397 struct opj_event_mgr * p_manager
3401 assert(p_header_data != 00);
3402 assert(p_j2k != 00);
3403 assert(p_manager != 00);
3405 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3406 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3410 if (p_header_size != 0) {
3411 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3415 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3416 j2k_copy_tile_quantization_parameters(p_j2k);
3421 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3424 opj_cio_t *cio = j2k->cio;
3426 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3427 lenp = cio_tell(cio);
3429 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3430 j2k_write_qcx(j2k, compno);
3431 len = cio_tell(cio) - lenp;
3432 cio_seek(cio, lenp);
3433 cio_write(cio, len, 2); /* Lqcc */
3434 cio_seek(cio, lenp + len);
3438 * Writes the QCC marker (quantization component)
3440 * @param p_comp_no the index of the component to output.
3441 * @param p_stream the stream to write data to.
3442 * @param p_j2k J2K codec.
3443 * @param p_manager the user event manager.
3445 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3446 OPJ_UINT32 p_comp_no,
3447 struct opj_stream_private *p_stream,
3448 struct opj_event_mgr * p_manager )
3450 OPJ_UINT32 l_qcc_size,l_remaining_size;
3453 assert(p_j2k != 00);
3454 assert(p_manager != 00);
3455 assert(p_stream != 00);
3457 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3458 l_remaining_size = l_qcc_size;
3460 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3461 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3462 = (OPJ_BYTE*)opj_realloc(
3463 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3465 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3469 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3472 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);
3474 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) {
3482 * Writes the QCC marker (quantization component)
3484 * @param p_comp_no the index of the component to output.
3485 * @param p_stream the stream to write data to.
3486 * @param p_j2k J2K codec.
3487 * @param p_manager the user event manager.
3489 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3490 OPJ_UINT32 p_comp_no,
3492 OPJ_UINT32 * p_data_written,
3493 struct opj_event_mgr * p_manager )
3495 OPJ_UINT32 l_qcc_size,l_remaining_size;
3496 OPJ_BYTE * l_current_data = 00;
3499 assert(p_j2k != 00);
3500 assert(p_manager != 00);
3502 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3503 l_remaining_size = l_qcc_size;
3505 l_current_data = p_data;
3507 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3508 l_current_data += 2;
3510 if (p_j2k->m_private_image->numcomps <= 256) {
3513 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3514 l_current_data += 2;
3516 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3519 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3520 l_remaining_size -= 6;
3523 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3524 l_current_data += 2;
3526 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3529 l_remaining_size -= 6;
3532 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3534 *p_data_written = l_qcc_size;
3538 * Gets the maximum size taken by a qcc.
3540 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3542 return j2k_get_max_coc_size(p_j2k);
3545 static void j2k_read_qcc(opj_j2k_t *j2k) {
3547 int numcomp = j2k->image->numcomps;
3548 opj_cio_t *cio = j2k->cio;
3550 len = cio_read(cio, 2); /* Lqcc */
3551 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3554 if (j2k->cp->correct) {
3556 static int backup_compno = 0;
3558 /* compno is negative or larger than the number of components!!! */
3559 if ((compno < 0) || (compno >= numcomp)) {
3560 opj_event_msg(j2k->cinfo, EVT_ERROR,
3561 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3564 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3567 /* we try to correct */
3568 compno = backup_compno % numcomp;
3569 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3570 "- setting component number to %d\n",
3574 /* keep your private count of tiles */
3577 #endif /* USE_JPWL */
3579 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3583 * Reads a QCC marker (Quantization component)
3584 * @param p_header_data the data contained in the QCC box.
3585 * @param p_j2k the jpeg2000 codec.
3586 * @param p_header_size the size of the data contained in the QCC marker.
3587 * @param p_manager the user event manager.
3589 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3590 OPJ_BYTE * p_header_data,
3591 OPJ_UINT32 p_header_size,
3592 struct opj_event_mgr * p_manager)
3594 OPJ_UINT32 l_num_comp,l_comp_no;
3597 assert(p_header_data != 00);
3598 assert(p_j2k != 00);
3599 assert(p_manager != 00);
3601 l_num_comp = p_j2k->m_private_image->numcomps;
3603 if (l_num_comp <= 256) {
3604 if (p_header_size < 1) {
3605 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3608 opj_read_bytes(p_header_data,&l_comp_no,1);
3613 if (p_header_size < 2) {
3614 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3617 opj_read_bytes(p_header_data,&l_comp_no,2);
3623 if (p_j2k->m_cp.correct) {
3625 static OPJ_UINT32 backup_compno = 0;
3627 /* compno is negative or larger than the number of components!!! */
3628 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3629 opj_event_msg_v2(p_manager, EVT_ERROR,
3630 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3631 l_comp_no, l_num_comp);
3633 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3636 /* we try to correct */
3637 l_comp_no = backup_compno % l_num_comp;
3638 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3639 "- setting component number to %d\n",
3643 /* keep your private count of tiles */
3646 #endif /* USE_JPWL */
3648 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3649 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3653 if (p_header_size != 0) {
3654 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3662 static void j2k_write_poc(opj_j2k_t *j2k) {
3663 int len, numpchgs, i;
3665 int numcomps = j2k->image->numcomps;
3667 opj_cp_t *cp = j2k->cp;
3668 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3669 opj_tccp_t *tccp = &tcp->tccps[0];
3670 opj_cio_t *cio = j2k->cio;
3672 numpchgs = 1 + tcp->numpocs;
3673 cio_write(cio, J2K_MS_POC, 2); /* POC */
3674 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3675 cio_write(cio, len, 2); /* Lpoc */
3676 for (i = 0; i < numpchgs; i++) {
3677 opj_poc_t *poc = &tcp->pocs[i];
3678 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3679 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3680 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3681 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3682 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3683 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3684 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3685 poc->compno1 = int_min(poc->compno1, numcomps);
3686 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3691 * Writes the POC marker (Progression Order Change)
3693 * @param p_stream the stream to write data to.
3694 * @param p_j2k J2K codec.
3695 * @param p_manager the user event manager.
3697 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3698 struct opj_stream_private *p_stream,
3699 struct opj_event_mgr * p_manager )
3701 OPJ_UINT32 l_nb_comp;
3702 OPJ_UINT32 l_nb_poc;
3703 OPJ_UINT32 l_poc_size;
3704 OPJ_UINT32 l_written_size = 0;
3705 opj_tcp_v2_t *l_tcp = 00;
3706 opj_tccp_t *l_tccp = 00;
3707 OPJ_UINT32 l_poc_room;
3710 assert(p_j2k != 00);
3711 assert(p_manager != 00);
3712 assert(p_stream != 00);
3714 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3715 l_tccp = &l_tcp->tccps[0];
3716 l_nb_comp = p_j2k->m_private_image->numcomps;
3717 l_nb_poc = 1 + l_tcp->numpocs;
3719 if (l_nb_comp <= 256) {
3725 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3727 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3728 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3729 = (OPJ_BYTE*)opj_realloc(
3730 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3733 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3737 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3740 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3742 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) {
3751 * Writes the POC marker (Progression Order Change)
3753 * @param p_stream the stream to write data to.
3754 * @param p_j2k J2K codec.
3755 * @param p_manager the user event manager.
3757 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3759 OPJ_UINT32 * p_data_written,
3760 struct opj_event_mgr * p_manager )
3763 OPJ_BYTE * l_current_data = 00;
3764 OPJ_UINT32 l_nb_comp;
3765 OPJ_UINT32 l_nb_poc;
3766 OPJ_UINT32 l_poc_size;
3767 opj_image_t *l_image = 00;
3768 opj_tcp_v2_t *l_tcp = 00;
3769 opj_tccp_t *l_tccp = 00;
3770 opj_poc_t *l_current_poc = 00;
3771 OPJ_UINT32 l_poc_room;
3774 assert(p_j2k != 00);
3775 assert(p_manager != 00);
3777 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3778 l_tccp = &l_tcp->tccps[0];
3779 l_image = p_j2k->m_private_image;
3780 l_nb_comp = l_image->numcomps;
3781 l_nb_poc = 1 + l_tcp->numpocs;
3783 if (l_nb_comp <= 256) {
3790 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3792 l_current_data = p_data;
3794 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3795 l_current_data += 2;
3797 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3798 l_current_data += 2;
3800 l_current_poc = l_tcp->pocs;
3801 for (i = 0; i < l_nb_poc; ++i) {
3802 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3805 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3806 l_current_data+=l_poc_room;
3808 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3811 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3814 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3815 l_current_data+=l_poc_room;
3817 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3820 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3821 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3822 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3823 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3828 *p_data_written = l_poc_size;
3832 * Gets the maximum size taken by the writing of a POC.
3834 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3836 opj_tcp_v2_t * l_tcp = 00;
3837 OPJ_UINT32 l_nb_tiles = 0;
3838 OPJ_UINT32 l_max_poc = 0;
3841 l_tcp = p_j2k->m_cp.tcps;
3842 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3844 for (i=0;i<l_nb_tiles;++i) {
3845 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3851 return 4 + 9 * l_max_poc;
3855 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3857 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3860 OPJ_UINT32 l_nb_tiles;
3861 OPJ_UINT32 l_max = 0;
3862 opj_tcp_v2_t * l_tcp = 00;
3864 l_tcp = p_j2k->m_cp.tcps;
3865 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3867 for (i=0;i<l_nb_tiles;++i) {
3868 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3878 * Gets the maximum size taken by the headers of the SOT.
3880 * @param p_j2k the jpeg2000 codec to use.
3882 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3884 OPJ_UINT32 l_nb_bytes = 0;
3885 OPJ_UINT32 l_nb_comps;
3886 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3888 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3889 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3891 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3892 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3893 l_nb_bytes += l_nb_comps * l_coc_bytes;
3895 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3896 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3899 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3901 /*** DEVELOPER CORNER, Add room for your headers ***/
3906 static void j2k_read_poc(opj_j2k_t *j2k) {
3907 int len, numpchgs, i, old_poc;
3909 int numcomps = j2k->image->numcomps;
3911 opj_cp_t *cp = j2k->cp;
3912 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3913 opj_cio_t *cio = j2k->cio;
3915 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
3917 len = cio_read(cio, 2); /* Lpoc */
3918 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
3920 for (i = old_poc; i < numpchgs + old_poc; i++) {
3922 poc = &tcp->pocs[i];
3923 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
3924 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
3925 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
3926 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
3927 poc->compno1 = int_min(
3928 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
3929 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
3932 tcp->numpocs = numpchgs + old_poc - 1;
3936 * Reads a POC marker (Progression Order Change)
3938 * @param p_header_data the data contained in the POC box.
3939 * @param p_j2k the jpeg2000 codec.
3940 * @param p_header_size the size of the data contained in the POC marker.
3941 * @param p_manager the user event manager.
3943 opj_bool j2k_read_poc_v2 (
3944 opj_j2k_v2_t *p_j2k,
3945 OPJ_BYTE * p_header_data,
3946 OPJ_UINT32 p_header_size,
3947 struct opj_event_mgr * p_manager)
3949 OPJ_UINT32 i, l_nb_comp, l_tmp;
3950 opj_image_t * l_image = 00;
3951 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3952 OPJ_UINT32 l_chunk_size, l_comp_room;
3954 opj_cp_v2_t *l_cp = 00;
3955 opj_tcp_v2_t *l_tcp = 00;
3956 opj_poc_t *l_current_poc = 00;
3959 assert(p_header_data != 00);
3960 assert(p_j2k != 00);
3961 assert(p_manager != 00);
3963 l_image = p_j2k->m_private_image;
3964 l_nb_comp = l_image->numcomps;
3965 if (l_nb_comp <= 256) {
3971 l_chunk_size = 5 + 2 * l_comp_room;
3972 l_current_poc_nb = p_header_size / l_chunk_size;
3973 l_current_poc_remaining = p_header_size % l_chunk_size;
3975 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3976 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3980 l_cp = &(p_j2k->m_cp);
3981 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3982 &l_cp->tcps[p_j2k->m_current_tile_number] :
3983 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3984 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3985 l_current_poc_nb += l_old_poc_nb;
3987 assert(l_current_poc_nb < 32);
3989 /* now poc is in use.*/
3992 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3993 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3994 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3996 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3997 p_header_data+=l_comp_room;
3998 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
4000 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
4002 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
4003 p_header_data+=l_comp_room;
4004 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
4006 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4007 /* make sure comp is in acceptable bounds */
4008 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4012 l_tcp->numpocs = l_current_poc_nb - 1;
4016 static void j2k_read_crg(opj_j2k_t *j2k) {
4017 int len, i, Xcrg_i, Ycrg_i;
4019 opj_cio_t *cio = j2k->cio;
4020 int numcomps = j2k->image->numcomps;
4022 len = cio_read(cio, 2); /* Lcrg */
4023 for (i = 0; i < numcomps; i++) {
4024 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
4025 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4030 * Reads a CRG marker (Component registration)
4032 * @param p_header_data the data contained in the TLM box.
4033 * @param p_j2k the jpeg2000 codec.
4034 * @param p_header_size the size of the data contained in the TLM marker.
4035 * @param p_manager the user event manager.
4037 opj_bool j2k_read_crg_v2 (
4038 opj_j2k_v2_t *p_j2k,
4039 OPJ_BYTE * p_header_data,
4040 OPJ_UINT32 p_header_size,
4041 struct opj_event_mgr * p_manager
4044 OPJ_UINT32 l_nb_comp;
4046 assert(p_header_data != 00);
4047 assert(p_j2k != 00);
4048 assert(p_manager != 00);
4050 l_nb_comp = p_j2k->m_private_image->numcomps;
4052 if (p_header_size != l_nb_comp *4) {
4053 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4056 /* Do not care of this at the moment since only local variables are set here */
4059 (i = 0; i < l_nb_comp; ++i)
4061 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4063 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4070 static void j2k_read_tlm(opj_j2k_t *j2k) {
4071 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4072 long int Ttlm_i, Ptlm_i;
4074 opj_cio_t *cio = j2k->cio;
4076 len = cio_read(cio, 2); /* Ltlm */
4077 Ztlm = cio_read(cio, 1); /* Ztlm */
4078 Stlm = cio_read(cio, 1); /* Stlm */
4079 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4080 SP = (Stlm >> 6) & 0x01;
4081 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4082 for (i = 0; i < tile_tlm; i++) {
4083 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4084 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4089 * Reads a TLM marker (Tile Length Marker)
4091 * @param p_header_data the data contained in the TLM box.
4092 * @param p_j2k the jpeg2000 codec.
4093 * @param p_header_size the size of the data contained in the TLM marker.
4094 * @param p_manager the user event manager.
4096 opj_bool j2k_read_tlm_v2 (
4097 opj_j2k_v2_t *p_j2k,
4098 OPJ_BYTE * p_header_data,
4099 OPJ_UINT32 p_header_size,
4100 struct opj_event_mgr * p_manager
4103 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4105 assert(p_header_data != 00);
4106 assert(p_j2k != 00);
4107 assert(p_manager != 00);
4109 if (p_header_size < 2) {
4110 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4115 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4117 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4120 l_ST = ((l_Stlm >> 4) & 0x3);
4121 l_SP = (l_Stlm >> 6) & 0x1;
4123 l_Ptlm_size = (l_SP + 1) * 2;
4124 l_quotient = l_Ptlm_size + l_ST;
4126 l_tot_num_tp = p_header_size / l_quotient;
4127 l_tot_num_tp_remaining = p_header_size % l_quotient;
4129 if (l_tot_num_tp_remaining != 0) {
4130 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4133 /* FIXME Do not care of this at the moment since only local variables are set here */
4136 (i = 0; i < l_tot_num_tp; ++i)
4138 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4139 p_header_data += l_ST;
4140 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4141 p_header_data += l_Ptlm_size;
4146 static void j2k_read_plm(opj_j2k_t *j2k) {
4147 int len, i, Zplm, Nplm, add, packet_len = 0;
4149 opj_cio_t *cio = j2k->cio;
4151 len = cio_read(cio, 2); /* Lplm */
4152 Zplm = cio_read(cio, 1); /* Zplm */
4155 Nplm = cio_read(cio, 4); /* Nplm */
4157 for (i = Nplm; i > 0; i--) {
4158 add = cio_read(cio, 1);
4160 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4161 if ((add & 0x80) == 0) {
4172 * Reads a PLM marker (Packet length, main header marker)
4174 * @param p_header_data the data contained in the TLM box.
4175 * @param p_j2k the jpeg2000 codec.
4176 * @param p_header_size the size of the data contained in the TLM marker.
4177 * @param p_manager the user event manager.
4179 opj_bool j2k_read_plm_v2 (
4180 opj_j2k_v2_t *p_j2k,
4181 OPJ_BYTE * p_header_data,
4182 OPJ_UINT32 p_header_size,
4183 struct opj_event_mgr * p_manager
4187 assert(p_header_data != 00);
4188 assert(p_j2k != 00);
4189 assert(p_manager != 00);
4191 if (p_header_size < 1) {
4192 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4195 /* Do not care of this at the moment since only local variables are set here */
4197 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4204 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4206 p_header_size -= (1+l_Nplm);
4210 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4214 (i = 0; i < l_Nplm; ++i)
4216 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4218 // take only the last seven bytes
4219 l_packet_len |= (l_tmp & 0x7f);
4227 // store packet length and proceed to next packet
4234 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4242 static void j2k_read_plt(opj_j2k_t *j2k) {
4243 int len, i, Zplt, packet_len = 0, add;
4245 opj_cio_t *cio = j2k->cio;
4247 len = cio_read(cio, 2); /* Lplt */
4248 Zplt = cio_read(cio, 1); /* Zplt */
4249 for (i = len - 3; i > 0; i--) {
4250 add = cio_read(cio, 1);
4251 packet_len = (packet_len << 7) + add; /* Iplt_i */
4252 if ((add & 0x80) == 0) {
4260 * Reads a PLT marker (Packet length, tile-part header)
4262 * @param p_header_data the data contained in the PLT box.
4263 * @param p_j2k the jpeg2000 codec.
4264 * @param p_header_size the size of the data contained in the PLT marker.
4265 * @param p_manager the user event manager.
4267 opj_bool j2k_read_plt_v2 (
4268 opj_j2k_v2_t *p_j2k,
4269 OPJ_BYTE * p_header_data,
4270 OPJ_UINT32 p_header_size,
4271 struct opj_event_mgr * p_manager
4274 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4277 assert(p_header_data != 00);
4278 assert(p_j2k != 00);
4279 assert(p_manager != 00);
4281 if (p_header_size < 1) {
4282 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4286 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4290 for (i = 0; i < p_header_size; ++i) {
4291 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4293 /* take only the last seven bytes */
4294 l_packet_len |= (l_tmp & 0x7f);
4299 /* store packet length and proceed to next packet */
4304 if (l_packet_len != 0) {
4305 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4312 static void j2k_read_ppm(opj_j2k_t *j2k) {
4313 int len, Z_ppm, i, j;
4316 opj_cp_t *cp = j2k->cp;
4317 opj_cio_t *cio = j2k->cio;
4319 len = cio_read(cio, 2);
4322 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4325 if (cp->ppm_previous == 0) {
4326 N_ppm = cio_read(cio, 4); /* N_ppm */
4329 N_ppm = cp->ppm_previous;
4332 if (Z_ppm == 0) { /* First PPM marker */
4333 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4334 cp->ppm_data_first = cp->ppm_data;
4335 cp->ppm_len = N_ppm;
4336 } else { /* NON-first PPM marker */
4337 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4340 /* this memory allocation check could be done even in non-JPWL cases */
4342 if (!cp->ppm_data) {
4343 opj_event_msg(j2k->cinfo, EVT_ERROR,
4344 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4346 if (!JPWL_ASSUME || JPWL_ASSUME) {
4347 opj_free(cp->ppm_data);
4348 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4355 cp->ppm_data_first = cp->ppm_data;
4356 cp->ppm_len = N_ppm + cp->ppm_store;
4358 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4359 cp->ppm_data[j] = cio_read(cio, 1);
4363 break; /* Case of non-finished packet header in present marker but finished in next one */
4365 cp->ppm_previous = i - 1;
4370 * Reads a PPM marker (Packed packet headers, main header)
4372 * @param p_header_data the data contained in the POC box.
4373 * @param p_j2k the jpeg2000 codec.
4374 * @param p_header_size the size of the data contained in the POC marker.
4375 * @param p_manager the user event manager.
4378 opj_bool j2k_read_ppm_v2 (
4379 opj_j2k_v2_t *p_j2k,
4380 OPJ_BYTE * p_header_data,
4381 OPJ_UINT32 p_header_size,
4382 struct opj_event_mgr * p_manager
4386 opj_cp_v2_t *l_cp = 00;
4387 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4390 assert(p_header_data != 00);
4391 assert(p_j2k != 00);
4392 assert(p_manager != 00);
4394 if (p_header_size < 1) {
4395 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4399 l_cp = &(p_j2k->m_cp);
4402 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4406 /* First PPM marker */
4408 if (p_header_size < 4) {
4409 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4413 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4417 /* First PPM marker: Initialization */
4418 l_cp->ppm_len = l_N_ppm;
4419 l_cp->ppm_data_size = 0;
4421 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4422 if (l_cp->ppm_buffer == 00) {
4423 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4426 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4428 l_cp->ppm_data = l_cp->ppm_buffer;
4432 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4433 if (p_header_size >= 4) {
4435 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4438 l_cp->ppm_len += l_N_ppm ;
4440 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4441 if (l_cp->ppm_buffer == 00) {
4442 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4445 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4447 l_cp->ppm_data = l_cp->ppm_buffer;
4454 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4456 if (l_remaining_data <= p_header_size) {
4457 /* we must store less information than available in the packet */
4458 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4459 l_cp->ppm_data_size = l_cp->ppm_len;
4460 p_header_size -= l_remaining_data;
4461 p_header_data += l_remaining_data;
4464 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4465 l_cp->ppm_data_size += p_header_size;
4466 p_header_data += p_header_size;
4479 * Reads a PPM marker (Packed packet headers, main header)
4481 * @param p_header_data the data contained in the POC box.
4482 * @param p_j2k the jpeg2000 codec.
4483 * @param p_header_size the size of the data contained in the POC marker.
4484 * @param p_manager the user event manager.
4486 opj_bool j2k_read_ppm_v3 (
4487 opj_j2k_v2_t *p_j2k,
4488 OPJ_BYTE * p_header_data,
4489 OPJ_UINT32 p_header_size,
4490 struct opj_event_mgr * p_manager
4493 opj_cp_v2_t *l_cp = 00;
4494 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4497 assert(p_header_data != 00);
4498 assert(p_j2k != 00);
4499 assert(p_manager != 00);
4501 /* Minimum size of PPM marker is equal to the size of Zppm element */
4502 if (p_header_size < 1) {
4503 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4507 l_cp = &(p_j2k->m_cp);
4510 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4514 /* First PPM marker */
4516 /* We need now at least the Nppm^0 element */
4517 if (p_header_size < 4) {
4518 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4522 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4526 /* First PPM marker: Initialization */
4527 l_cp->ppm_len = l_N_ppm;
4528 l_cp->ppm_data_read = 0;
4530 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4531 if (l_cp->ppm_data == 00) {
4532 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4535 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4537 l_cp->ppm_data_current = l_cp->ppm_data;
4539 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4542 if (p_header_size < 4) {
4543 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4547 /* Uncompleted Ippm series in the previous PPM marker?*/
4548 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4549 /* Get the place where add the remaining Ippm series*/
4550 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4551 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4554 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4558 /* Increase the size of ppm_data to add the new Ippm series*/
4559 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4561 /* Keep the position of the place where concatenate the new series*/
4562 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4563 l_cp->ppm_len += l_N_ppm;
4568 l_remaining_data = p_header_size;
4570 while (l_remaining_data >= l_N_ppm) {
4571 /* read a complete Ippm series*/
4572 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4573 p_header_size -= l_N_ppm;
4574 p_header_data += l_N_ppm;
4576 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4580 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4585 l_remaining_data = p_header_size;
4589 l_remaining_data = p_header_size;
4591 /* Next Ippm series is a complete series ?*/
4592 if (l_remaining_data > l_N_ppm) {
4593 /* Increase the size of ppm_data to add the new Ippm series*/
4594 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4596 /* Keep the position of the place where concatenate the new series */
4597 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4598 l_cp->ppm_len += l_N_ppm;
4603 /* Need to read an incomplete Ippm series*/
4604 if (l_remaining_data) {
4605 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4607 /* Keep the position of the place where concatenate the new series*/
4608 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4609 l_cp->ppm_len += l_N_ppm;
4611 /* Read incomplete Ippm series*/
4612 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4613 p_header_size -= l_remaining_data;
4614 p_header_data += l_remaining_data;
4616 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4621 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4622 if (p_header_size >= 4) {
4624 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4627 l_cp->ppm_len += l_N_ppm ;
4629 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4630 if (l_cp->ppm_buffer == 00) {
4631 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4634 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4636 l_cp->ppm_data = l_cp->ppm_buffer;
4643 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4645 if (l_remaining_data <= p_header_size) {
4646 /* we must store less information than available in the packet */
4647 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4648 l_cp->ppm_data_size = l_cp->ppm_len;
4649 p_header_size -= l_remaining_data;
4650 p_header_data += l_remaining_data;
4653 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4654 l_cp->ppm_data_size += p_header_size;
4655 p_header_data += p_header_size;
4664 static void j2k_read_ppt(opj_j2k_t *j2k) {
4665 int len, Z_ppt, i, j = 0;
4667 opj_cp_t *cp = j2k->cp;
4668 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4669 opj_cio_t *cio = j2k->cio;
4671 len = cio_read(cio, 2);
4672 Z_ppt = cio_read(cio, 1);
4674 if (Z_ppt == 0) { /* First PPT marker */
4675 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4676 tcp->ppt_data_first = tcp->ppt_data;
4678 tcp->ppt_len = len - 3;
4679 } else { /* NON-first PPT marker */
4680 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4681 tcp->ppt_data_first = tcp->ppt_data;
4682 tcp->ppt_len = len - 3 + tcp->ppt_store;
4685 for (i = len - 3; i > 0; i--) {
4686 tcp->ppt_data[j] = cio_read(cio, 1);
4693 * Reads a PPT marker (Packed packet headers, tile-part header)
4695 * @param p_header_data the data contained in the PPT box.
4696 * @param p_j2k the jpeg2000 codec.
4697 * @param p_header_size the size of the data contained in the PPT marker.
4698 * @param p_manager the user event manager.
4700 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4701 OPJ_BYTE * p_header_data,
4702 OPJ_UINT32 p_header_size,
4703 struct opj_event_mgr * p_manager )
4705 opj_cp_v2_t *l_cp = 00;
4706 opj_tcp_v2_t *l_tcp = 00;
4710 assert(p_header_data != 00);
4711 assert(p_j2k != 00);
4712 assert(p_manager != 00);
4714 /* We need to have the Z_ppt element at minimum */
4715 if (p_header_size < 1) {
4716 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4720 l_cp = &(p_j2k->m_cp);
4722 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");
4726 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4729 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4733 /* Allocate buffer to read the packet header */
4735 /* First PPT marker */
4736 l_tcp->ppt_data_size = 0;
4737 l_tcp->ppt_len = p_header_size;
4739 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4740 if (l_tcp->ppt_buffer == 00) {
4741 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4744 l_tcp->ppt_data = l_tcp->ppt_buffer;
4746 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4749 l_tcp->ppt_len += p_header_size;
4751 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4752 if (l_tcp->ppt_buffer == 00) {
4753 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4756 l_tcp->ppt_data = l_tcp->ppt_buffer;
4758 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4761 /* Read packet header from buffer */
4762 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4764 l_tcp->ppt_data_size += p_header_size;
4770 * Writes the TLM marker (Tile Length Marker)
4772 * @param p_stream the stream to write data to.
4773 * @param p_j2k J2K codec.
4774 * @param p_manager the user event manager.
4776 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4777 struct opj_stream_private *p_stream,
4778 struct opj_event_mgr * p_manager )
4780 OPJ_BYTE * l_current_data = 00;
4781 OPJ_UINT32 l_tlm_size;
4784 assert(p_j2k != 00);
4785 assert(p_manager != 00);
4786 assert(p_stream != 00);
4788 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4790 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4791 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4792 = (OPJ_BYTE*)opj_realloc(
4793 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4796 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4800 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4803 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4805 /* change the way data is written to avoid seeking if possible */
4807 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4809 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4810 l_current_data += 2;
4812 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4813 l_current_data += 2;
4815 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4818 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4821 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4822 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) {
4829 static void j2k_write_tlm(opj_j2k_t *j2k){
4831 opj_cio_t *cio = j2k->cio;
4832 j2k->tlm_start = cio_tell(cio);
4833 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4834 lenp = 4 + (5*j2k->totnum_tp);
4835 cio_write(cio,lenp,2); /* Ltlm */
4836 cio_write(cio, 0,1); /* Ztlm=0*/
4837 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4838 cio_skip(cio,5*j2k->totnum_tp);
4841 static void j2k_write_sot(opj_j2k_t *j2k) {
4844 opj_cio_t *cio = j2k->cio;
4846 j2k->sot_start = cio_tell(cio);
4847 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4848 lenp = cio_tell(cio);
4849 cio_skip(cio, 2); /* Lsot (further) */
4850 cio_write(cio, j2k->curtileno, 2); /* Isot */
4851 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4852 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4853 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4854 len = cio_tell(cio) - lenp;
4855 cio_seek(cio, lenp);
4856 cio_write(cio, len, 2); /* Lsot */
4857 cio_seek(cio, lenp + len);
4861 /* update markers struct */
4862 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4863 #endif /* USE_JPWL */
4866 if( j2k->cstr_info && j2k->cur_tp_num==0){
4867 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4872 * Writes the SOT marker (Start of tile-part)
4874 * @param p_stream the stream to write data to.
4875 * @param p_j2k J2K codec.
4876 * @param p_manager the user event manager.
4878 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4880 OPJ_UINT32 * p_data_written,
4881 const struct opj_stream_private *p_stream,
4882 struct opj_event_mgr * p_manager )
4885 assert(p_j2k != 00);
4886 assert(p_manager != 00);
4887 assert(p_stream != 00);
4889 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4892 opj_write_bytes(p_data,10,2); /* Lsot */
4895 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4901 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4904 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4909 /* update markers struct */
4911 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4913 assert( 0 && "TODO" );
4914 #endif /* USE_JPWL */
4916 * p_data_written = 12;
4925 * Reads a PPT marker (Packed packet headers, tile-part header)
4927 * @param p_header_data the data contained in the PPT box.
4928 * @param p_j2k the jpeg2000 codec.
4929 * @param p_header_size the size of the data contained in the PPT marker.
4930 * @param p_manager the user event manager.
4932 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4933 OPJ_BYTE * p_header_data,
4934 OPJ_UINT32 p_header_size,
4935 opj_event_mgr_t * p_manager )
4937 opj_cp_v2_t *l_cp = 00;
4938 opj_tcp_v2_t *l_tcp = 00;
4939 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4940 OPJ_UINT32 l_current_part;
4941 OPJ_UINT32 l_tile_x,l_tile_y;
4944 assert(p_header_data != 00);
4945 assert(p_j2k != 00);
4946 assert(p_manager != 00);
4948 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4949 if (p_header_size != 8) {
4950 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4954 l_cp = &(p_j2k->m_cp);
4955 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4958 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4959 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4960 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4963 if (l_cp->correct) {
4965 int tileno = p_j2k->m_current_tile_number;
4966 static int backup_tileno = 0;
4968 /* tileno is negative or larger than the number of tiles!!! */
4969 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4970 opj_event_msg_v2(p_manager, EVT_ERROR,
4971 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4972 tileno, (l_cp->tw * l_cp->th));
4974 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4977 /* we try to correct */
4978 tileno = backup_tileno;
4979 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4980 "- setting tile number to %d\n",
4984 /* keep your private count of tiles */
4987 #endif /* USE_JPWL */
4989 /* look for the tile in the list of already processed tile (in parts). */
4990 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4991 /* since the time taken by this function can only grow at the time */
4993 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4996 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4997 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4999 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5005 if (l_cp->correct) {
5007 /* totlen is negative or larger than the bytes left!!! */
5008 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))) { */
5009 opj_event_msg_v2(p_manager, EVT_ERROR,
5010 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5011 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5013 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5016 /* we try to correct */
5018 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5019 "- setting Psot to %d => assuming it is the last tile\n",
5023 #endif /* USE_JPWL */
5025 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5027 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5028 "we assuming it is the last tile-part of the codestream.\n");
5029 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5032 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5035 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5038 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5039 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5040 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5041 if (l_tcp->m_nb_tile_parts) {
5042 if (l_current_part >= l_tcp->m_nb_tile_parts){
5043 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5044 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5045 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5049 l_tcp->m_nb_tile_parts = l_num_parts;
5052 /* If know the number of tile part header we will check if we didn't read the last*/
5053 if (l_tcp->m_nb_tile_parts) {
5054 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5055 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5059 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5060 /* Keep the size of data to skip after this marker */
5061 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5064 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5065 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5068 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5070 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5071 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5072 p_j2k->m_specific_param.m_decoder.m_skip_data =
5073 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5074 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5075 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5076 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5079 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5080 p_j2k->m_specific_param.m_decoder.m_skip_data =
5081 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5085 if (p_j2k->cstr_index)
5087 assert(p_j2k->cstr_index->tile_index != 00);
5088 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5089 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5091 if (l_num_parts != 0){
5092 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5093 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5096 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5097 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5098 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5100 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5101 (opj_tp_index_t*)opj_realloc(
5102 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5103 l_num_parts* sizeof(opj_tp_index_t));
5106 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5108 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5109 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5110 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5111 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5112 sizeof(opj_tp_index_t));
5115 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5116 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5117 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5118 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5119 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5120 * sizeof(opj_tp_index_t));
5129 /* 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 */
5130 /* if (p_j2k->cstr_info) {
5133 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5136 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5137 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5138 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5139 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5142 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5145 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5149 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5152 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5153 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5154 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5159 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5162 opj_tcp_t *tcp = NULL;
5163 opj_codestream_info_t *cstr_info = NULL;
5165 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5166 opj_cp_t *cp = j2k->cp;
5167 opj_cio_t *cio = j2k->cio;
5169 tcd->tp_num = j2k->tp_num ;
5170 tcd->cur_tp_num = j2k->cur_tp_num;
5172 cio_write(cio, J2K_MS_SOD, 2);
5174 if( j2k->cstr_info && j2k->cur_tp_num==0){
5175 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5178 if (j2k->curtileno == 0) {
5179 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5183 cstr_info = j2k->cstr_info;
5185 if (!j2k->cur_tp_num ) {
5186 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5187 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5190 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5191 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5195 /* update markers struct */
5196 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5197 #endif /* USE_JPWL */
5202 tcp = &cp->tcps[j2k->curtileno];
5203 for (layno = 0; layno < tcp->numlayers; layno++) {
5204 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5205 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5206 } else if (tcp->rates[layno]) {
5207 tcp->rates[layno]=1;
5210 if(j2k->cur_tp_num == 0){
5211 tcd->tcd_image->tiles->packno = 0;
5213 cstr_info->packno = 0;
5216 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5218 /* Writing Psot in SOT marker */
5219 totlen = cio_tell(cio) + l - j2k->sot_start;
5220 cio_seek(cio, j2k->sot_start + 6);
5221 cio_write(cio, totlen, 4);
5222 cio_seek(cio, j2k->sot_start + totlen);
5223 /* Writing Ttlm and Ptlm in TLM marker */
5225 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5226 cio_write(cio, j2k->curtileno, 1);
5227 cio_write(cio, totlen, 4);
5229 cio_seek(cio, j2k->sot_start + totlen);
5233 * Writes the SOD marker (Start of data)
5235 * @param p_stream the stream to write data to.
5236 * @param p_j2k J2K codec.
5237 * @param p_manager the user event manager.
5239 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5240 struct opj_tcd_v2 * p_tile_coder,
5242 OPJ_UINT32 * p_data_written,
5243 OPJ_UINT32 p_total_data_size,
5244 const struct opj_stream_private *p_stream,
5245 struct opj_event_mgr * p_manager )
5247 opj_tcp_v2_t *l_tcp = 00;
5248 opj_codestream_info_t *l_cstr_info = 00;
5249 opj_cp_v2_t *l_cp = 00;
5251 OPJ_UINT32 l_size_tile;
5252 OPJ_UINT32 l_remaining_data;
5255 assert(p_j2k != 00);
5256 assert(p_manager != 00);
5257 assert(p_stream != 00);
5259 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5262 /* make room for the EOF marker */
5263 l_remaining_data = p_total_data_size - 4;
5265 l_cp = &(p_j2k->m_cp);
5266 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5269 /* update tile coder */
5270 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5271 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5273 l_size_tile = l_cp->th * l_cp->tw;
5276 /* TODO mergeV2: check this part which use cstr_info */
5277 /*l_cstr_info = p_j2k->cstr_info;
5279 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5280 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5281 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5287 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5289 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5294 /* update markers struct */
5295 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5297 assert( 0 && "TODO" );
5298 #endif /* USE_JPWL */
5303 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5304 p_tile_coder->tcd_image->tiles->packno = 0;
5306 l_cstr_info->packno = 0;
5310 *p_data_written = 0;
5312 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)) {
5313 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5317 *p_data_written += 2;
5326 * Reads a SOD marker (Start Of Data)
5328 * @param p_header_data the data contained in the SOD box.
5329 * @param p_j2k the jpeg2000 codec.
5330 * @param p_header_size the size of the data contained in the SOD marker.
5331 * @param p_manager the user event manager.
5333 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
5334 opj_stream_private_t *p_stream,
5335 opj_event_mgr_t * p_manager
5338 OPJ_UINT32 l_current_read_size;
5339 opj_codestream_index_t * l_cstr_index = 00;
5340 OPJ_BYTE ** l_current_data = 00;
5341 opj_tcp_v2_t * l_tcp = 00;
5342 OPJ_UINT32 * l_tile_len = 00;
5345 assert(p_j2k != 00);
5346 assert(p_manager != 00);
5347 assert(p_stream != 00);
5349 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5351 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5352 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5353 // but we are in the last tile part,
5354 // so its result will fit on OPJ_UINT32 unless we find
5355 // a file with a single tile part of more than 4 GB...*/
5356 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5359 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5361 l_current_data = &(l_tcp->m_data);
5362 l_tile_len = &l_tcp->m_data_size;
5364 if (! *l_current_data) {
5365 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5368 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5371 if (*l_current_data == 00) {
5372 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5378 l_cstr_index = p_j2k->cstr_index;
5380 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5382 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5383 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5385 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5386 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5388 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5392 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5394 /*l_cstr_index->packno = 0;*/
5397 l_current_read_size = opj_stream_read_data( p_stream,
5398 *l_current_data + *l_tile_len,
5399 p_j2k->m_specific_param.m_decoder.m_sot_length,
5402 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5403 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5406 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5409 *l_tile_len += l_current_read_size;
5415 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5416 opj_cp_t *cp = j2k->cp;
5417 opj_tcp_t *tcp = &cp->tcps[tileno];
5418 opj_cio_t *cio = j2k->cio;
5419 int numcomps = j2k->image->numcomps;
5421 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5422 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5423 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5424 cio_write(cio, 0, 1); /* Srgn */
5425 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5429 * Writes the RGN marker (Region Of Interest)
5431 * @param p_tile_no the tile to output
5432 * @param p_comp_no the component to output
5433 * @param p_stream the stream to write data to.
5434 * @param p_j2k J2K codec.
5435 * @param p_manager the user event manager.
5437 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5438 OPJ_UINT32 p_tile_no,
5439 OPJ_UINT32 p_comp_no,
5440 struct opj_stream_private *p_stream,
5441 struct opj_event_mgr * p_manager )
5443 OPJ_BYTE * l_current_data = 00;
5444 OPJ_UINT32 l_nb_comp;
5445 OPJ_UINT32 l_rgn_size;
5446 opj_image_t *l_image = 00;
5447 opj_cp_v2_t *l_cp = 00;
5448 opj_tcp_v2_t *l_tcp = 00;
5449 opj_tccp_t *l_tccp = 00;
5450 OPJ_UINT32 l_comp_room;
5453 assert(p_j2k != 00);
5454 assert(p_manager != 00);
5455 assert(p_stream != 00);
5457 l_cp = &(p_j2k->m_cp);
5458 l_tcp = &l_cp->tcps[p_tile_no];
5459 l_tccp = &l_tcp->tccps[p_comp_no];
5461 l_nb_comp = l_image->numcomps;
5463 if (l_nb_comp <= 256) {
5470 l_rgn_size = 6 + l_comp_room;
5472 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5474 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5475 l_current_data += 2;
5477 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5478 l_current_data += 2;
5480 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5481 l_current_data+=l_comp_room;
5483 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5486 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5489 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) {
5496 static void j2k_read_rgn(opj_j2k_t *j2k) {
5497 int len, compno, roisty;
5499 opj_cp_t *cp = j2k->cp;
5500 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5501 opj_cio_t *cio = j2k->cio;
5502 int numcomps = j2k->image->numcomps;
5504 len = cio_read(cio, 2); /* Lrgn */
5505 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
5506 roisty = cio_read(cio, 1); /* Srgn */
5509 if (j2k->cp->correct) {
5510 /* totlen is negative or larger than the bytes left!!! */
5511 if (compno >= numcomps) {
5512 opj_event_msg(j2k->cinfo, EVT_ERROR,
5513 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5515 if (!JPWL_ASSUME || JPWL_ASSUME) {
5516 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5521 #endif /* USE_JPWL */
5523 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
5526 static void j2k_write_eoc(opj_j2k_t *j2k) {
5527 opj_cio_t *cio = j2k->cio;
5528 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5529 cio_write(cio, J2K_MS_EOC, 2);
5533 /* update markers struct */
5534 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5535 #endif /* USE_JPWL */
5540 * Writes the EOC marker (End of Codestream)
5542 * @param p_stream the stream to write data to.
5543 * @param p_j2k J2K codec.
5544 * @param p_manager the user event manager.
5546 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5547 struct opj_stream_private *p_stream,
5548 struct opj_event_mgr * p_manager )
5551 assert(p_j2k != 00);
5552 assert(p_manager != 00);
5553 assert(p_stream != 00);
5555 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5560 /* update markers struct */
5562 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5564 #endif /* USE_JPWL */
5566 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5570 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5579 * Reads a RGN marker (Region Of Interest)
5581 * @param p_header_data the data contained in the POC box.
5582 * @param p_j2k the jpeg2000 codec.
5583 * @param p_header_size the size of the data contained in the POC marker.
5584 * @param p_manager the user event manager.
5586 opj_bool j2k_read_rgn_v2 (
5587 opj_j2k_v2_t *p_j2k,
5588 OPJ_BYTE * p_header_data,
5589 OPJ_UINT32 p_header_size,
5590 struct opj_event_mgr * p_manager
5593 OPJ_UINT32 l_nb_comp;
5594 opj_image_t * l_image = 00;
5596 opj_cp_v2_t *l_cp = 00;
5597 opj_tcp_v2_t *l_tcp = 00;
5598 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5601 assert(p_header_data != 00);
5602 assert(p_j2k != 00);
5603 assert(p_manager != 00);
5605 l_image = p_j2k->m_private_image;
5606 l_nb_comp = l_image->numcomps;
5608 if (l_nb_comp <= 256) {
5613 if (p_header_size != 2 + l_comp_room) {
5614 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5618 l_cp = &(p_j2k->m_cp);
5619 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5620 &l_cp->tcps[p_j2k->m_current_tile_number] :
5621 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5623 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5624 p_header_data+=l_comp_room;
5625 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5629 if (l_cp->correct) {
5630 /* totlen is negative or larger than the bytes left!!! */
5631 if (l_comp_room >= l_nb_comp) {
5632 opj_event_msg_v2(p_manager, EVT_ERROR,
5633 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5634 l_comp_room, l_nb_comp);
5635 if (!JPWL_ASSUME || JPWL_ASSUME) {
5636 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5641 #endif /* USE_JPWL */
5643 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5650 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5652 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5655 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5662 * Updates the rates of the tcp.
5664 * @param p_stream the stream to write data to.
5665 * @param p_j2k J2K codec.
5666 * @param p_manager the user event manager.
5668 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5669 struct opj_stream_private *p_stream,
5670 struct opj_event_mgr * p_manager )
5672 opj_cp_v2_t * l_cp = 00;
5673 opj_image_t * l_image = 00;
5674 opj_tcp_v2_t * l_tcp = 00;
5675 opj_image_comp_t * l_img_comp = 00;
5678 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5679 OPJ_FLOAT32 * l_rates = 0;
5680 OPJ_FLOAT32 l_sot_remove;
5681 OPJ_UINT32 l_bits_empty, l_size_pixel;
5682 OPJ_UINT32 l_tile_size = 0;
5683 OPJ_UINT32 l_last_res;
5684 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5687 assert(p_j2k != 00);
5688 assert(p_manager != 00);
5689 assert(p_stream != 00);
5692 l_cp = &(p_j2k->m_cp);
5693 l_image = p_j2k->m_private_image;
5696 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5697 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5698 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5700 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5701 l_tp_stride_func = get_tp_stride;
5704 l_tp_stride_func = get_default_stride;
5707 for (i=0;i<l_cp->th;++i) {
5708 for (j=0;j<l_cp->tw;++j) {
5709 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5711 /* 4 borders of the tile rescale on the image if necessary */
5712 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5713 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5714 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5715 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5717 l_rates = l_tcp->rates;
5719 /* Modification of the RATE >> */
5721 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5723 ((*l_rates) * l_bits_empty)
5731 for (k = 1; k < l_tcp->numlayers; ++k) {
5733 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5735 ((*l_rates) * l_bits_empty)
5751 for (i=0;i<l_cp->th;++i) {
5752 for (j=0;j<l_cp->tw;++j) {
5753 l_rates = l_tcp->rates;
5756 *l_rates -= l_sot_remove;
5758 if (*l_rates < 30) {
5765 l_last_res = l_tcp->numlayers - 1;
5767 for (k = 1; k < l_last_res; ++k) {
5770 *l_rates -= l_sot_remove;
5772 if (*l_rates < *(l_rates - 1) + 10) {
5773 *l_rates = (*(l_rates - 1)) + 20;
5781 *l_rates -= (l_sot_remove + 2.f);
5783 if (*l_rates < *(l_rates - 1) + 10) {
5784 *l_rates = (*(l_rates - 1)) + 20;
5792 l_img_comp = l_image->comps;
5795 for (i=0;i<l_image->numcomps;++i) {
5796 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5798 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5806 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5808 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5810 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5811 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5812 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5813 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5817 if (l_cp->m_specific_param.m_enc.m_cinema) {
5818 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5819 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5820 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5824 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5825 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5831 static void j2k_read_eoc(opj_j2k_t *j2k) {
5833 opj_bool success = OPJ_FALSE;
5835 /* if packets should be decoded */
5836 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5837 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5838 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5839 for (i = 0; i < j2k->cp->tileno_size; i++) {
5840 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5841 if (j2k->cp->tileno[i] != -1)
5843 tileno = j2k->cp->tileno[i];
5844 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5845 opj_free(j2k->tile_data[tileno]);
5846 j2k->tile_data[tileno] = NULL;
5847 tcd_free_decode_tile(tcd, i);
5850 success = OPJ_FALSE;
5851 if (success == OPJ_FALSE) {
5852 j2k->state |= J2K_STATE_ERR;
5856 tcd_free_decode(tcd);
5859 /* if packets should not be decoded */
5861 for (i = 0; i < j2k->cp->tileno_size; i++) {
5862 tileno = j2k->cp->tileno[i];
5863 opj_free(j2k->tile_data[tileno]);
5864 j2k->tile_data[tileno] = NULL;
5867 if (j2k->state & J2K_STATE_ERR)
5868 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5870 j2k->state = J2K_STATE_MT;
5874 * Reads a EOC marker (End Of Codestream)
5876 * @param p_header_data the data contained in the SOD box.
5877 * @param p_j2k the jpeg2000 codec.
5878 * @param p_header_size the size of the data contained in the SOD marker.
5879 * @param p_manager the user event manager.
5882 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5883 struct opj_stream_private *p_stream,
5884 struct opj_event_mgr * p_manager )
5887 opj_tcd_v2_t * l_tcd = 00;
5888 OPJ_UINT32 l_nb_tiles;
5889 opj_tcp_v2_t * l_tcp = 00;
5893 assert(p_j2k != 00);
5894 assert(p_manager != 00);
5895 assert(p_stream != 00);
5897 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5898 l_tcp = p_j2k->m_cp.tcps;
5900 l_tcd = tcd_create_v2(OPJ_TRUE);
5902 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5906 for (i = 0; i < l_nb_tiles; ++i) {
5907 if (l_tcp->m_data) {
5908 if (! tcd_init_decode_tile(l_tcd, i)) {
5909 tcd_destroy_v2(l_tcd);
5910 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5914 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5918 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5923 j2k_tcp_destroy(l_tcp);
5927 tcd_destroy_v2(l_tcd);
5933 * Gets the offset of the header.
5935 * @param p_stream the stream to write data to.
5936 * @param p_j2k J2K codec.
5937 * @param p_manager the user event manager.
5939 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5940 struct opj_stream_private *p_stream,
5941 struct opj_event_mgr * p_manager )
5944 assert(p_j2k != 00);
5945 assert(p_manager != 00);
5946 assert(p_stream != 00);
5948 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5954 * Writes the MCT marker (Multiple Component Transform)
5956 * @param p_stream the stream to write data to.
5957 * @param p_j2k J2K codec.
5958 * @param p_manager the user event manager.
5960 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5961 struct opj_stream_private *p_stream,
5962 struct opj_event_mgr * p_manager )
5965 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5966 opj_mct_data_t * l_mct_record;
5967 opj_tcp_v2_t * l_tcp;
5970 assert(p_j2k != 00);
5971 assert(p_stream != 00);
5972 assert(p_manager != 00);
5974 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5978 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5979 l_mct_record = l_tcp->m_mct_records;
5981 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5983 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5990 l_mcc_record = l_tcp->m_mcc_records;
5992 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5994 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6001 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6009 * Writes the image components.
6011 * @param p_stream the stream to write data to.
6012 * @param p_j2k J2K codec.
6013 * @param p_manager the user event manager.
6015 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6016 struct opj_stream_private *p_stream,
6017 struct opj_event_mgr * p_manager )
6022 assert(p_j2k != 00);
6023 assert(p_manager != 00);
6024 assert(p_stream != 00);
6026 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6028 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6032 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6041 * Writes regions of interests.
6043 * @param p_stream the stream to write data to.
6044 * @param p_j2k J2K codec.
6045 * @param p_manager the user event manager.
6047 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
6048 struct opj_stream_private *p_stream,
6049 struct opj_event_mgr * p_manager )
6052 const opj_tccp_t *l_tccp = 00;
6055 assert(p_j2k != 00);
6056 assert(p_manager != 00);
6057 assert(p_stream != 00);
6059 l_tccp = p_j2k->m_cp.tcps->tccps;
6061 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6062 if (l_tccp->roishift) {
6064 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6078 * @param p_stream the stream to write data to.
6079 * @param p_j2k J2K codec.
6080 * @param p_manager the user event manager.
6082 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6083 struct opj_stream_private *p_stream,
6084 struct opj_event_mgr * p_manager )
6086 opj_codestream_index_t * l_cstr_index = 00;
6089 assert(p_j2k != 00);
6090 assert(p_manager != 00);
6091 assert(p_stream != 00);
6093 l_cstr_index = p_j2k->cstr_index;
6095 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6097 /* The following adjustment is done to adjust the codestream size */
6098 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6099 /* the first bunch of bytes is not in the codestream */
6100 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6105 /* preparation of JPWL marker segments */
6109 /* encode according to JPWL */
6110 jpwl_encode(p_j2k, p_stream, image);
6114 assert( 0 && "TODO" );
6115 #endif /* USE_JPWL */
6120 typedef struct opj_dec_mstabent {
6123 /** value of the state when the marker can appear */
6125 /** action linked to the marker */
6126 void (*handler) (opj_j2k_t *j2k);
6127 } opj_dec_mstabent_t;
6129 opj_dec_mstabent_t j2k_dec_mstab[] = {
6130 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
6131 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
6132 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
6133 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6134 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
6135 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
6136 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6137 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6138 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6139 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6140 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6141 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6142 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6143 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6144 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6145 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6147 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6148 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6151 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6152 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6153 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6154 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6155 #endif /* USE_JPWL */
6157 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6158 {J2K_MS_INSEC, 0, j2k_read_insec},
6159 #endif /* USE_JPSEC */
6161 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6164 static void j2k_read_unk(opj_j2k_t *j2k) {
6165 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6168 if (j2k->cp->correct) {
6170 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6171 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6172 id = cio_read(j2k->cio, 2);
6173 opj_event_msg(j2k->cinfo, EVT_ERROR,
6174 "JPWL: really don't know this marker %x\n",
6177 opj_event_msg(j2k->cinfo, EVT_ERROR,
6178 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6181 /* OK, activate this at your own risk!!! */
6182 /* we look for the marker at the minimum hamming distance from this */
6183 while (j2k_dec_mstab[m].id) {
6185 /* 1's where they differ */
6186 tmp_id = j2k_dec_mstab[m].id ^ id;
6188 /* compute the hamming distance between our id and the current */
6190 for (i = 0; i < 16; i++) {
6191 if ((tmp_id >> i) & 0x0001) {
6196 /* if current distance is smaller, set the minimum */
6197 if (cur_dist < min_dist) {
6198 min_dist = cur_dist;
6199 min_id = j2k_dec_mstab[m].id;
6202 /* jump to the next marker */
6206 /* do we substitute the marker? */
6207 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6208 opj_event_msg(j2k->cinfo, EVT_ERROR,
6209 "- marker %x is at distance %d from the read %x\n",
6210 min_id, min_dist, id);
6211 opj_event_msg(j2k->cinfo, EVT_ERROR,
6212 "- trying to substitute in place and crossing fingers!\n");
6213 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6214 cio_write(j2k->cio, min_id, 2);
6217 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6222 #endif /* USE_JPWL */
6227 * Reads an unknown marker
6229 * @param p_stream the stream object to read from.
6230 * @param p_j2k the jpeg2000 codec.
6231 * @param p_manager the user event manager.
6233 * @return true if the marker could be deduced.
6235 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6236 struct opj_stream_private *p_stream,
6237 OPJ_UINT32 *output_marker,
6238 struct opj_event_mgr * p_manager
6241 OPJ_UINT32 l_unknown_marker;
6242 const opj_dec_memory_marker_handler_t * l_marker_handler;
6243 OPJ_UINT32 l_size_unk = 2;
6246 assert(p_j2k != 00);
6247 assert(p_manager != 00);
6248 assert(p_stream != 00);
6250 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6253 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6254 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6255 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6259 /* read 2 bytes as the new marker ID*/
6260 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6262 if (!(l_unknown_marker < 0xff00)) {
6264 /* Get the marker handler from the marker ID*/
6265 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6267 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6268 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6272 if (l_marker_handler->id != J2K_MS_UNK) {
6273 /* Add the marker to the codestream index*/
6274 if (l_marker_handler->id != J2K_MS_SOT)
6275 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6276 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6278 break; /* next marker is known and well located */
6286 *output_marker = l_marker_handler->id ;
6292 Read the lookup table containing all the marker, status and action
6293 @param id Marker value
6295 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6296 opj_dec_mstabent_t *e;
6297 for (e = j2k_dec_mstab; e->id != 0; e++) {
6306 * Writes the MCT marker (Multiple Component Transform)
6308 * @param p_stream the stream to write data to.
6309 * @param p_j2k J2K codec.
6310 * @param p_manager the user event manager.
6312 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6313 opj_mct_data_t * p_mct_record,
6314 struct opj_stream_private *p_stream,
6315 struct opj_event_mgr * p_manager )
6317 OPJ_UINT32 l_mct_size;
6318 OPJ_BYTE * l_current_data = 00;
6322 assert(p_j2k != 00);
6323 assert(p_manager != 00);
6324 assert(p_stream != 00);
6326 l_mct_size = 10 + p_mct_record->m_data_size;
6328 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6329 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6330 = (OPJ_BYTE*)opj_realloc(
6331 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6334 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6338 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6341 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6343 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6344 l_current_data += 2;
6346 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6347 l_current_data += 2;
6349 opj_write_bytes(l_current_data,0,2); /* Zmct */
6350 l_current_data += 2;
6352 /* only one marker atm */
6353 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6355 opj_write_bytes(l_current_data,l_tmp,2);
6356 l_current_data += 2;
6358 opj_write_bytes(l_current_data,0,2); /* Ymct */
6361 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6363 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) {
6371 * Reads a MCT marker (Multiple Component Transform)
6373 * @param p_header_data the data contained in the MCT box.
6374 * @param p_j2k the jpeg2000 codec.
6375 * @param p_header_size the size of the data contained in the MCT marker.
6376 * @param p_manager the user event manager.
6378 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6379 OPJ_BYTE * p_header_data,
6380 OPJ_UINT32 p_header_size,
6381 struct opj_event_mgr * p_manager )
6384 opj_tcp_v2_t *l_tcp = 00;
6387 opj_mct_data_t * l_mct_data;
6390 assert(p_header_data != 00);
6391 assert(p_j2k != 00);
6393 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6394 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6395 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6397 if (p_header_size < 2) {
6398 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6403 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6406 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6410 if(p_header_size <= 6) {
6411 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6415 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6416 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6419 l_indix = l_tmp & 0xff;
6420 l_mct_data = l_tcp->m_mct_records;
6422 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6423 if (l_mct_data->m_index == l_indix) {
6430 if (i == l_tcp->m_nb_mct_records) {
6431 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6432 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6434 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));
6435 if(! l_tcp->m_mct_records) {
6436 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6440 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6441 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6444 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6447 if (l_mct_data->m_data) {
6448 opj_free(l_mct_data->m_data);
6449 l_mct_data->m_data = 00;
6452 l_mct_data->m_index = l_indix;
6453 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6454 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6456 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6459 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6465 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6466 if (! l_mct_data->m_data) {
6467 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6470 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6472 l_mct_data->m_data_size = p_header_size;
6473 ++l_tcp->m_nb_mct_records;
6479 * Writes the MCC marker (Multiple Component Collection)
6481 * @param p_stream the stream to write data to.
6482 * @param p_j2k J2K codec.
6483 * @param p_manager the user event manager.
6485 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6486 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6487 struct opj_stream_private *p_stream,
6488 struct opj_event_mgr * p_manager )
6491 OPJ_UINT32 l_mcc_size;
6492 OPJ_BYTE * l_current_data = 00;
6493 OPJ_UINT32 l_nb_bytes_for_comp;
6498 assert(p_j2k != 00);
6499 assert(p_manager != 00);
6500 assert(p_stream != 00);
6502 if (p_mcc_record->m_nb_comps > 255 ) {
6503 l_nb_bytes_for_comp = 2;
6507 l_nb_bytes_for_comp = 1;
6511 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6512 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6514 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6515 = (OPJ_BYTE*)opj_realloc(
6516 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6518 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6522 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6525 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6527 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6528 l_current_data += 2;
6530 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6531 l_current_data += 2;
6534 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6535 l_current_data += 2;
6537 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6540 /* only one marker atm */
6541 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6544 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6547 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6550 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 */
6553 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6554 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6555 l_current_data+=l_nb_bytes_for_comp;
6558 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 */
6561 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6563 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6564 l_current_data+=l_nb_bytes_for_comp;
6567 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6569 if (p_mcc_record->m_decorrelation_array) {
6570 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6573 if (p_mcc_record->m_offset_array) {
6574 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6577 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6580 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) {
6588 * Reads a MCC marker (Multiple Component Collection)
6590 * @param p_header_data the data contained in the MCC box.
6591 * @param p_j2k the jpeg2000 codec.
6592 * @param p_header_size the size of the data contained in the MCC marker.
6593 * @param p_manager the user event manager.
6595 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6596 OPJ_BYTE * p_header_data,
6597 OPJ_UINT32 p_header_size,
6598 struct opj_event_mgr * p_manager )
6603 opj_tcp_v2_t * l_tcp;
6604 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6605 opj_mct_data_t * l_mct_data;
6606 OPJ_UINT32 l_nb_collections;
6607 OPJ_UINT32 l_nb_comps;
6608 OPJ_UINT32 l_nb_bytes_by_comp;
6612 assert(p_header_data != 00);
6613 assert(p_j2k != 00);
6614 assert(p_manager != 00);
6616 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6617 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6618 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6620 if (p_header_size < 2) {
6621 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6626 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6629 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6633 if (p_header_size < 7) {
6634 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6638 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6641 l_mcc_record = l_tcp->m_mcc_records;
6643 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6644 if (l_mcc_record->m_index == l_indix) {
6651 if (i == l_tcp->m_nb_mcc_records) {
6652 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6653 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6655 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6656 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6657 if (! l_tcp->m_mcc_records) {
6658 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6661 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6662 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));
6664 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6666 l_mcc_record->m_index = l_indix;
6668 /* only one marker atm */
6669 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6672 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6676 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6679 if (l_nb_collections > 1) {
6680 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6686 for (i=0;i<l_nb_collections;++i) {
6687 if (p_header_size < 3) {
6688 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6692 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6696 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6700 opj_read_bytes(p_header_data,&l_nb_comps,2);
6705 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6706 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6708 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6709 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6713 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6715 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6716 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6717 p_header_data+=l_nb_bytes_by_comp;
6720 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6725 opj_read_bytes(p_header_data,&l_nb_comps,2);
6728 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6729 l_nb_comps &= 0x7fff;
6731 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6732 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6736 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6737 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6741 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6743 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6744 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6745 p_header_data+=l_nb_bytes_by_comp;
6748 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6753 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6756 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6757 l_mcc_record->m_decorrelation_array = 00;
6758 l_mcc_record->m_offset_array = 00;
6760 l_indix = l_tmp & 0xff;
6762 l_mct_data = l_tcp->m_mct_records;
6763 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6764 if (l_mct_data->m_index == l_indix) {
6765 l_mcc_record->m_decorrelation_array = l_mct_data;
6771 if (l_mcc_record->m_decorrelation_array == 00) {
6772 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6777 l_indix = (l_tmp >> 8) & 0xff;
6779 l_mct_data = l_tcp->m_mct_records;
6780 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6781 if (l_mct_data->m_index == l_indix) {
6782 l_mcc_record->m_offset_array = l_mct_data;
6788 if (l_mcc_record->m_offset_array == 00) {
6789 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6795 if (p_header_size != 0) {
6796 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6800 ++l_tcp->m_nb_mcc_records;
6807 * Writes the MCO marker (Multiple component transformation ordering)
6809 * @param p_stream the stream to write data to.
6810 * @param p_j2k J2K codec.
6811 * @param p_manager the user event manager.
6813 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6814 struct opj_stream_private *p_stream,
6815 struct opj_event_mgr * p_manager
6818 OPJ_BYTE * l_current_data = 00;
6819 OPJ_UINT32 l_mco_size;
6820 opj_tcp_v2_t * l_tcp = 00;
6821 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6825 assert(p_j2k != 00);
6826 assert(p_manager != 00);
6827 assert(p_stream != 00);
6829 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6830 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6832 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6833 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6835 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6836 = (OPJ_BYTE*)opj_realloc(
6837 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6839 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6844 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6847 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6848 l_current_data += 2;
6850 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6851 l_current_data += 2;
6853 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6856 l_mcc_record = l_tcp->m_mcc_records;
6857 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6858 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6864 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) {
6872 * Reads a MCO marker (Multiple Component Transform Ordering)
6874 * @param p_header_data the data contained in the MCO box.
6875 * @param p_j2k the jpeg2000 codec.
6876 * @param p_header_size the size of the data contained in the MCO marker.
6877 * @param p_manager the user event manager.
6879 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6880 OPJ_BYTE * p_header_data,
6881 OPJ_UINT32 p_header_size,
6882 struct opj_event_mgr * p_manager )
6884 OPJ_UINT32 l_tmp, i;
6885 OPJ_UINT32 l_nb_stages;
6886 opj_tcp_v2_t * l_tcp;
6887 opj_tccp_t * l_tccp;
6888 opj_image_t * l_image;
6889 opj_image_comp_t * l_img_comp;
6892 assert(p_header_data != 00);
6893 assert(p_j2k != 00);
6894 assert(p_manager != 00);
6896 l_image = p_j2k->m_private_image;
6897 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6898 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6899 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6901 if (p_header_size < 1) {
6902 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6906 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6909 if (l_nb_stages > 1) {
6910 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6914 if (p_header_size != l_nb_stages + 1) {
6915 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6919 l_tccp = l_tcp->tccps;
6920 l_img_comp = l_image->comps;
6922 for (i=0;i<l_image->numcomps;++i) {
6923 l_tccp->m_dc_level_shift = 0;
6927 if (l_tcp->m_mct_decoding_matrix) {
6928 opj_free(l_tcp->m_mct_decoding_matrix);
6929 l_tcp->m_mct_decoding_matrix = 00;
6932 for (i=0;i<l_nb_stages;++i) {
6933 opj_read_bytes(p_header_data,&l_tmp,1);
6936 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6944 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6947 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6948 opj_mct_data_t * l_deco_array, * l_offset_array;
6949 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6950 OPJ_UINT32 l_nb_elem;
6951 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6952 opj_tccp_t * l_tccp;
6955 assert(p_tcp != 00);
6957 l_mcc_record = p_tcp->m_mcc_records;
6959 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6960 if (l_mcc_record->m_index == p_index) {
6965 if (i==p_tcp->m_nb_mcc_records) {
6966 /** element discarded **/
6970 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6971 /** do not support number of comps != image */
6975 l_deco_array = l_mcc_record->m_decorrelation_array;
6978 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6979 if (l_deco_array->m_data_size != l_data_size) {
6983 l_nb_elem = p_image->numcomps * p_image->numcomps;
6984 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6985 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6987 if (! p_tcp->m_mct_decoding_matrix ) {
6991 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);
6994 l_offset_array = l_mcc_record->m_offset_array;
6996 if (l_offset_array) {
6997 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6998 if (l_offset_array->m_data_size != l_data_size) {
7002 l_nb_elem = p_image->numcomps;
7003 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7004 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7006 if (! l_offset_data ) {
7010 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7012 l_tccp = p_tcp->tccps;
7013 l_current_offset_data = l_offset_data;
7015 for (i=0;i<p_image->numcomps;++i) {
7016 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7020 opj_free(l_offset_data);
7027 * Writes the CBD marker (Component bit depth definition)
7029 * @param p_stream the stream to write data to.
7030 * @param p_j2k J2K codec.
7031 * @param p_manager the user event manager.
7033 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7034 struct opj_stream_private *p_stream,
7035 struct opj_event_mgr * p_manager )
7038 OPJ_UINT32 l_cbd_size;
7039 OPJ_BYTE * l_current_data = 00;
7040 opj_image_t *l_image = 00;
7041 opj_image_comp_t * l_comp = 00;
7044 assert(p_j2k != 00);
7045 assert(p_manager != 00);
7046 assert(p_stream != 00);
7048 l_image = p_j2k->m_private_image;
7049 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7051 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7052 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7053 = (OPJ_BYTE*)opj_realloc(
7054 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7057 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7061 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7064 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7066 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7067 l_current_data += 2;
7069 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7070 l_current_data += 2;
7072 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7075 l_comp = l_image->comps;
7077 for (i=0;i<l_image->numcomps;++i) {
7078 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7084 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) {
7092 * Reads a CBD marker (Component bit depth definition)
7093 * @param p_header_data the data contained in the CBD box.
7094 * @param p_j2k the jpeg2000 codec.
7095 * @param p_header_size the size of the data contained in the CBD marker.
7096 * @param p_manager the user event manager.
7098 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7099 OPJ_BYTE * p_header_data,
7100 OPJ_UINT32 p_header_size,
7101 struct opj_event_mgr * p_manager)
7103 OPJ_UINT32 l_nb_comp,l_num_comp;
7104 OPJ_UINT32 l_comp_def;
7106 opj_image_comp_t * l_comp = 00;
7109 assert(p_header_data != 00);
7110 assert(p_j2k != 00);
7111 assert(p_manager != 00);
7113 l_num_comp = p_j2k->m_private_image->numcomps;
7115 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7116 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7120 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7123 if (l_nb_comp != l_num_comp) {
7124 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7128 l_comp = p_j2k->m_private_image->comps;
7129 for (i=0;i<l_num_comp;++i) {
7130 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7132 l_comp->sgnd = (l_comp_def>>7) & 1;
7133 l_comp->prec = (l_comp_def&0x7f) + 1;
7141 /* ----------------------------------------------------------------------- */
7142 /* J2K / JPT decoder interface */
7143 /* ----------------------------------------------------------------------- */
7147 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7150 if(j2k->tile_len != NULL) {
7151 opj_free(j2k->tile_len);
7153 if(j2k->tile_data != NULL) {
7154 opj_free(j2k->tile_data);
7156 if(j2k->default_tcp != NULL) {
7157 opj_tcp_t *default_tcp = j2k->default_tcp;
7158 if(default_tcp->ppt_data_first != NULL) {
7159 opj_free(default_tcp->ppt_data_first);
7161 if(j2k->default_tcp->tccps != NULL) {
7162 opj_free(j2k->default_tcp->tccps);
7164 opj_free(j2k->default_tcp);
7166 if(j2k->cp != NULL) {
7167 opj_cp_t *cp = j2k->cp;
7168 if(cp->tcps != NULL) {
7169 for(i = 0; i < cp->tw * cp->th; i++) {
7170 if(cp->tcps[i].ppt_data_first != NULL) {
7171 opj_free(cp->tcps[i].ppt_data_first);
7173 if(cp->tcps[i].tccps != NULL) {
7174 opj_free(cp->tcps[i].tccps);
7179 if(cp->ppm_data_first != NULL) {
7180 opj_free(cp->ppm_data_first);
7182 if(cp->tileno != NULL) {
7183 opj_free(cp->tileno);
7185 if(cp->comment != NULL) {
7186 opj_free(cp->comment);
7195 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7197 if(j2k && parameters) {
7198 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7199 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7202 j2k->m_cp.correct = parameters->jpwl_correct;
7203 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7204 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7205 #endif /* USE_JPWL */
7212 * Read a JPT-stream and decode file
7215 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7216 opj_image_t *image = NULL;
7217 opj_jpt_msg_header_t header;
7219 opj_common_ptr cinfo = j2k->cinfo;
7221 OPJ_ARG_NOT_USED(cstr_info);
7225 /* create an empty image */
7226 image = opj_image_create0();
7229 j2k->state = J2K_STATE_MHSOC;
7231 /* Initialize the header */
7232 jpt_init_msg_header(&header);
7233 /* Read the first header of the message */
7234 jpt_read_msg_header(cinfo, cio, &header);
7236 position = cio_tell(cio);
7237 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7238 opj_image_destroy(image);
7239 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7244 opj_dec_mstabent_t *e = NULL;
7247 if (!cio_numbytesleft(cio)) {
7251 /* data-bin read -> need to read a new header */
7252 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7253 jpt_read_msg_header(cinfo, cio, &header);
7254 position = cio_tell(cio);
7255 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7256 opj_image_destroy(image);
7257 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7262 id = cio_read(cio, 2);
7263 if (id >> 8 != 0xff) {
7264 opj_image_destroy(image);
7265 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7268 e = j2k_dec_mstab_lookup(id);
7269 if (!(j2k->state & e->states)) {
7270 opj_image_destroy(image);
7271 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7277 if (j2k->state == J2K_STATE_MT) {
7280 if (j2k->state == J2K_STATE_NEOC) {
7284 if (j2k->state == J2K_STATE_NEOC) {
7288 if (j2k->state != J2K_STATE_MT) {
7289 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7295 /* ----------------------------------------------------------------------- */
7296 /* J2K encoder interface */
7297 /* ----------------------------------------------------------------------- */
7299 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7300 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7307 opj_j2k_v2_t* j2k_create_compress_v2(void)
7309 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7314 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7316 l_j2k->m_is_decoder = 0;
7317 l_j2k->m_cp.m_is_decoder = 0;
7319 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7320 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7325 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7327 /* validation list creation*/
7328 l_j2k->m_validation_list = opj_procedure_list_create();
7329 if (! l_j2k->m_validation_list) {
7334 /* execution list creation*/
7335 l_j2k->m_procedure_list = opj_procedure_list_create();
7336 if (! l_j2k->m_procedure_list) {
7344 void j2k_destroy_compress(opj_j2k_t *j2k) {
7348 if(j2k->cp != NULL) {
7349 opj_cp_t *cp = j2k->cp;
7352 opj_free(cp->comment);
7355 opj_free(cp->matrice);
7357 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7358 opj_free(cp->tcps[tileno].tccps);
7367 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7369 int tileno, numpocs_tile;
7370 opj_cp_t *cp = NULL;
7372 if(!j2k || !parameters || ! image) {
7376 /* create and initialize the coding parameters structure */
7377 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7379 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7382 /* set default values for cp */
7387 copy user encoding parameters
7389 cp->cinema = parameters->cp_cinema;
7390 cp->max_comp_size = parameters->max_comp_size;
7391 cp->rsiz = parameters->cp_rsiz;
7392 cp->disto_alloc = parameters->cp_disto_alloc;
7393 cp->fixed_alloc = parameters->cp_fixed_alloc;
7394 cp->fixed_quality = parameters->cp_fixed_quality;
7396 /* mod fixed_quality */
7397 if(parameters->cp_matrice) {
7398 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7399 cp->matrice = (int *) opj_malloc(array_size);
7400 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7404 cp->tdx = parameters->cp_tdx;
7405 cp->tdy = parameters->cp_tdy;
7408 cp->tx0 = parameters->cp_tx0;
7409 cp->ty0 = parameters->cp_ty0;
7411 /* comment string */
7412 if(parameters->cp_comment) {
7413 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7415 strcpy(cp->comment, parameters->cp_comment);
7420 calculate other encoding parameters
7423 if (parameters->tile_size_on) {
7424 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7425 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7427 cp->tdx = image->x1 - cp->tx0;
7428 cp->tdy = image->y1 - cp->ty0;
7431 if(parameters->tp_on){
7432 cp->tp_flag = parameters->tp_flag;
7437 for(i=0;i<image->numcomps ;i++){
7438 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7444 calculate JPWL encoding parameters
7447 if (parameters->jpwl_epc_on) {
7451 cp->epc_on = OPJ_TRUE;
7452 cp->info_on = OPJ_FALSE; /* no informative technique */
7455 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7456 cp->epb_on = OPJ_TRUE;
7458 cp->hprot_MH = parameters->jpwl_hprot_MH;
7459 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7460 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7461 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7463 /* if tile specs are not specified, copy MH specs */
7464 if (cp->hprot_TPH[0] == -1) {
7465 cp->hprot_TPH_tileno[0] = 0;
7466 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7468 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7469 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7470 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7471 cp->pprot[i] = parameters->jpwl_pprot[i];
7475 /* set ESD writing */
7476 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7477 cp->esd_on = OPJ_TRUE;
7479 cp->sens_size = parameters->jpwl_sens_size;
7480 cp->sens_addr = parameters->jpwl_sens_addr;
7481 cp->sens_range = parameters->jpwl_sens_range;
7483 cp->sens_MH = parameters->jpwl_sens_MH;
7484 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7485 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7486 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7490 /* always set RED writing to false: we are at the encoder */
7491 cp->red_on = OPJ_FALSE;
7494 cp->epc_on = OPJ_FALSE;
7496 #endif /* USE_JPWL */
7499 /* initialize the mutiple tiles */
7500 /* ---------------------------- */
7501 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7503 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7504 opj_tcp_t *tcp = &cp->tcps[tileno];
7505 tcp->numlayers = parameters->tcp_numlayers;
7506 assert ( tcp->numlayers >= 0 );
7507 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7509 if (cp->fixed_quality) {
7510 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7512 tcp->rates[j] = parameters->tcp_rates[j];
7514 if (cp->fixed_quality) { /* add fixed_quality */
7515 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7517 tcp->rates[j] = parameters->tcp_rates[j];
7521 tcp->csty = parameters->csty;
7522 tcp->prg = parameters->prog_order;
7523 tcp->mct = parameters->tcp_mct;
7527 if (parameters->numpocs) {
7528 /* initialisation of POC */
7530 assert( parameters->numpocs >= 0 );
7531 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7532 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7533 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7534 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7535 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7536 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7537 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7538 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7539 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7540 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7544 tcp->numpocs = numpocs_tile -1 ;
7549 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7551 for (i = 0; i < image->numcomps; i++) {
7552 opj_tccp_t *tccp = &tcp->tccps[i];
7553 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7554 tccp->numresolutions = parameters->numresolution;
7555 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7556 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7557 tccp->cblksty = parameters->mode;
7558 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7559 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7561 assert(parameters->roi_compno >= 0);
7562 if (i == (OPJ_UINT32)parameters->roi_compno) {
7563 tccp->roishift = parameters->roi_shift;
7568 if(parameters->cp_cinema)
7570 /*Precinct size for lowest frequency subband=128*/
7573 /*Precinct size at all other resolutions = 256*/
7574 for (j = 1; j < tccp->numresolutions; j++) {
7579 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7581 assert(tccp->numresolutions > 0);
7582 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7583 if (p < parameters->res_spec) {
7585 if (parameters->prcw_init[p] < 1) {
7588 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7591 if (parameters->prch_init[p] < 1) {
7594 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7598 int res_spec = parameters->res_spec;
7599 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7600 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7602 if (size_prcw < 1) {
7605 tccp->prcw[j] = int_floorlog2(size_prcw);
7608 if (size_prch < 1) {
7611 tccp->prch[j] = int_floorlog2(size_prch);
7615 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7618 for (j = 0; j < tccp->numresolutions; j++) {
7625 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7630 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7631 opj_cparameters_t *parameters,
7633 struct opj_event_mgr * p_manager)
7635 OPJ_UINT32 i, j, tileno, numpocs_tile;
7636 opj_cp_v2_t *cp = 00;
7639 if(!p_j2k || !parameters || ! image) {
7643 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7644 cp = &(p_j2k->m_cp);
7646 /* set default values for cp */
7651 copy user encoding parameters
7653 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7654 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7655 cp->rsiz = parameters->cp_rsiz;
7656 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7657 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7658 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7660 /* mod fixed_quality */
7661 if (parameters->cp_matrice) {
7662 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7663 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7664 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7668 cp->tdx = parameters->cp_tdx;
7669 cp->tdy = parameters->cp_tdy;
7672 cp->tx0 = parameters->cp_tx0;
7673 cp->ty0 = parameters->cp_ty0;
7675 /* comment string */
7676 if(parameters->cp_comment) {
7677 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7679 strcpy(cp->comment, parameters->cp_comment);
7684 calculate other encoding parameters
7687 if (parameters->tile_size_on) {
7688 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7689 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7691 cp->tdx = image->x1 - cp->tx0;
7692 cp->tdy = image->y1 - cp->ty0;
7695 if (parameters->tp_on) {
7696 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7697 cp->m_specific_param.m_enc.m_tp_on = 1;
7702 calculate JPWL encoding parameters
7705 if (parameters->jpwl_epc_on) {
7709 cp->epc_on = OPJ_TRUE;
7710 cp->info_on = OPJ_FALSE; /* no informative technique */
7713 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7714 cp->epb_on = OPJ_TRUE;
7716 cp->hprot_MH = parameters->jpwl_hprot_MH;
7717 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7718 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7719 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7721 /* if tile specs are not specified, copy MH specs */
7722 if (cp->hprot_TPH[0] == -1) {
7723 cp->hprot_TPH_tileno[0] = 0;
7724 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7726 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7727 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7728 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7729 cp->pprot[i] = parameters->jpwl_pprot[i];
7733 /* set ESD writing */
7734 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7735 cp->esd_on = OPJ_TRUE;
7737 cp->sens_size = parameters->jpwl_sens_size;
7738 cp->sens_addr = parameters->jpwl_sens_addr;
7739 cp->sens_range = parameters->jpwl_sens_range;
7741 cp->sens_MH = parameters->jpwl_sens_MH;
7742 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7743 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7744 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7748 /* always set RED writing to false: we are at the encoder */
7749 cp->red_on = OPJ_FALSE;
7752 cp->epc_on = OPJ_FALSE;
7754 #endif /* USE_JPWL */
7757 /* initialize the mutiple tiles */
7758 /* ---------------------------- */
7759 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7760 if (parameters->numpocs) {
7761 /* initialisation of POC */
7762 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7766 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7767 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7768 tcp->numlayers = parameters->tcp_numlayers;
7770 for (j = 0; j < tcp->numlayers; j++) {
7771 if(cp->m_specific_param.m_enc.m_cinema){
7772 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7773 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7775 tcp->rates[j] = parameters->tcp_rates[j];
7777 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7778 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7780 tcp->rates[j] = parameters->tcp_rates[j];
7785 tcp->csty = parameters->csty;
7786 tcp->prg = parameters->prog_order;
7787 tcp->mct = parameters->tcp_mct;
7792 if (parameters->numpocs) {
7793 /* initialisation of POC */
7796 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7797 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7798 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7800 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7801 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7802 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7803 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7804 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7805 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7806 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7812 tcp->numpocs = numpocs_tile -1 ;
7817 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7819 if (parameters->mct_data) {
7821 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7824 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7825 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7826 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7827 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7830 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7831 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7832 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7834 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7835 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7837 tcp->mct_norms = (OPJ_FLOAT64*)
7838 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7840 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7843 for (i = 0; i < image->numcomps; i++) {
7844 opj_tccp_t *tccp = &tcp->tccps[i];
7845 tccp->m_dc_level_shift = l_dc_shift[i];
7848 j2k_setup_mct_encoding(tcp,image);
7852 for (i = 0; i < image->numcomps; i++) {
7853 opj_tccp_t *tccp = &tcp->tccps[i];
7854 opj_image_comp_t * l_comp = &(image->comps[i]);
7856 if (! l_comp->sgnd) {
7857 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7862 for (i = 0; i < image->numcomps; i++) {
7863 opj_tccp_t *tccp = &tcp->tccps[i];
7865 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7866 tccp->numresolutions = parameters->numresolution;
7867 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7868 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7869 tccp->cblksty = parameters->mode;
7870 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7871 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7874 if (i == parameters->roi_compno) {
7875 tccp->roishift = parameters->roi_shift;
7880 if(parameters->cp_cinema) {
7881 //Precinct size for lowest frequency subband=128
7884 //Precinct size at all other resolutions = 256
7885 for (j = 1; j < tccp->numresolutions; j++) {
7890 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7891 OPJ_INT32 p = 0, it_res;
7892 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7893 if (p < parameters->res_spec) {
7895 if (parameters->prcw_init[p] < 1) {
7896 tccp->prcw[it_res] = 1;
7898 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7901 if (parameters->prch_init[p] < 1) {
7902 tccp->prch[it_res] = 1;
7904 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7908 int res_spec = parameters->res_spec;
7909 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7910 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7912 if (size_prcw < 1) {
7913 tccp->prcw[it_res] = 1;
7915 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7918 if (size_prch < 1) {
7919 tccp->prch[it_res] = 1;
7921 tccp->prch[it_res] = int_floorlog2(size_prch);
7925 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7928 for (j = 0; j < tccp->numresolutions; j++) {
7935 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7939 if (parameters->mct_data) {
7940 opj_free(parameters->mct_data);
7941 parameters->mct_data = 00;
7946 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7949 opj_cp_t *cp = NULL;
7951 opj_tcd_t *tcd = NULL; /* TCD component */
7959 j2k->cstr_info = cstr_info;
7962 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7963 cstr_info->image_w = image->x1 - image->x0;
7964 cstr_info->image_h = image->y1 - image->y0;
7965 cstr_info->prog = (&cp->tcps[0])->prg;
7966 cstr_info->tw = cp->tw;
7967 cstr_info->th = cp->th;
7968 cstr_info->tile_x = cp->tdx; /* new version parser */
7969 cstr_info->tile_y = cp->tdy; /* new version parser */
7970 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7971 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7972 cstr_info->numcomps = image->numcomps;
7973 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7974 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7975 for (compno=0; compno < image->numcomps; compno++) {
7976 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7978 cstr_info->D_max = 0.0; /* ADD Marcela */
7979 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7980 cstr_info->maxmarknum = 100;
7981 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7982 cstr_info->marknum = 0;
7992 for (compno = 1; compno < image->numcomps; compno++) {
7993 j2k_write_coc(j2k, compno);
7994 j2k_write_qcc(j2k, compno);
7998 for (compno = 0; compno < image->numcomps; compno++) {
7999 opj_tcp_t *tcp = &cp->tcps[0];
8000 if (tcp->tccps[compno].roishift)
8001 j2k_write_rgn(j2k, compno, 0);
8003 if (cp->comment != NULL) {
8007 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
8011 if (cp->cinema == CINEMA4K_24) {
8016 /* uncomment only for testing JPSEC marker writing */
8017 /* j2k_write_sec(j2k); */
8021 cstr_info->main_head_end = cio_tell(cio) - 1;
8024 /**** Main Header ENDS here ***/
8026 /* create the tile encoder */
8027 tcd = tcd_create(j2k->cinfo);
8029 /* encode each tile */
8030 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8034 int acc_pack_num = 0;
8038 opj_tcp_t *tcp = &cp->tcps[tileno];
8039 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8041 j2k->curtileno = tileno;
8042 j2k->cur_tp_num = 0;
8043 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8044 /* initialisation before tile encoding */
8046 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8048 tcd_init_encode(tcd, image, cp, j2k->curtileno);
8053 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8054 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8055 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8056 cstr_info->tile[j2k->curtileno].marknum = 0;
8060 for(pino = 0; pino <= tcp->numpocs; pino++) {
8064 /*Get number of tile parts*/
8065 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8066 tcd->tp_pos = cp->tp_pos;
8068 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8069 j2k->tp_num = tilepartno;
8072 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8073 cio_tell(cio) + j2k->pos_correction;
8077 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8078 for (compno = 1; compno < image->numcomps; compno++) {
8079 j2k_write_coc(j2k, compno);
8080 j2k_write_qcc(j2k, compno);
8082 if (cp->tcps[tileno].numpocs) {
8089 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8090 cio_tell(cio) + j2k->pos_correction + 1;
8093 j2k_write_sod(j2k, tcd);
8097 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8098 cio_tell(cio) + j2k->pos_correction - 1;
8099 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8101 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8102 cstr_info->packno - acc_pack_num;
8103 acc_pack_num = cstr_info->packno;
8111 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8116 if (tile->PPT) { // BAD PPT !!!
8119 PPT_file=fopen("PPT","rb");
8120 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8121 for (i=0;i<tile->len_ppt;i++) {
8123 fread(&elmt, 1, 1, PPT_file);
8124 fwrite(&elmt,1,1,f);
8133 /* destroy the tile encoder */
8134 tcd_free_encode(tcd);
8137 opj_free(j2k->cur_totnum_tp);
8142 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8144 /* The following adjustment is done to adjust the codestream size */
8145 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8146 /* the first bunch of bytes is not in the codestream */
8147 cstr_info->codestream_size -= cstr_info->main_head_start;
8153 preparation of JPWL marker segments
8157 /* encode according to JPWL */
8158 jpwl_encode(j2k, cio, image);
8161 #endif /* USE_JPWL */
8166 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8168 assert(cstr_info != 00);
8170 /* expand the list? */
8171 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8172 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8173 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8176 /* add the marker */
8177 cstr_info->marker[cstr_info->marknum].type = type;
8178 cstr_info->marker[cstr_info->marknum].pos = pos;
8179 cstr_info->marker[cstr_info->marknum].len = len;
8180 cstr_info->marknum++;
8184 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8186 assert(cstr_index != 00);
8188 /* expand the list? */
8189 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8190 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8191 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8194 /* add the marker */
8195 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8196 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8197 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8198 cstr_index->marknum++;
8202 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8204 opj_marker_info_t *marker;
8206 assert(cstr_info != 00);
8208 /* expand the list? */
8209 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8210 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8211 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8214 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8216 /* add the marker */
8217 marker->type = type;
8220 cstr_info->tile[tileno].marknum++;
8223 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)
8225 assert(cstr_index != 00);
8226 assert(cstr_index->tile_index != 00);
8228 /* expand the list? */
8229 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8230 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8231 cstr_index->tile_index[tileno].marker =
8232 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8233 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8236 /* add the marker */
8237 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8238 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8239 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8240 cstr_index->tile_index[tileno].marknum++;
8242 if (type == J2K_MS_SOT) {
8243 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8245 if (cstr_index->tile_index[tileno].tp_index)
8246 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8253 * -----------------------------------------------------------------------
8254 * -----------------------------------------------------------------------
8255 * -----------------------------------------------------------------------
8259 * Ends the decompression procedures and possibiliy add data to be read after the
8262 opj_bool j2k_end_decompress(
8263 opj_j2k_v2_t *p_j2k,
8264 opj_stream_private_t *p_stream,
8265 opj_event_mgr_t * p_manager)
8274 * Reads a jpeg2000 codestream header structure.
8277 * @param p_stream the stream to read data from.
8278 * @param p_j2k the jpeg2000 codec.
8279 * @param p_manager the user event manager.
8281 * @return true if the box is valid.
8283 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8284 opj_j2k_v2_t* p_j2k,
8285 opj_image_t** p_image,
8286 struct opj_event_mgr* p_manager )
8289 assert(p_j2k != 00);
8290 assert(p_stream != 00);
8291 assert(p_manager != 00);
8293 /* create an empty image header */
8294 p_j2k->m_private_image = opj_image_create0();
8295 if (! p_j2k->m_private_image) {
8299 /* customization of the validation */
8300 j2k_setup_decoding_validation(p_j2k);
8302 /* validation of the parameters codec */
8303 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8304 opj_image_destroy(p_j2k->m_private_image);
8305 p_j2k->m_private_image = NULL;
8309 /* customization of the encoding */
8310 j2k_setup_header_reading(p_j2k);
8313 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8314 opj_image_destroy(p_j2k->m_private_image);
8315 p_j2k->m_private_image = NULL;
8319 *p_image = opj_image_create0();
8324 /* Copy codestream image information to the output image */
8325 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8327 /*Allocate and initialize some elements of codestrem index*/
8328 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8336 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8338 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8341 assert(p_j2k != 00);
8343 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8345 /* DEVELOPER CORNER, add your custom procedures */
8346 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8351 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8352 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8354 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8357 assert(p_j2k != 00);
8359 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8360 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8361 /* DEVELOPER CORNER, add your custom validation procedure */
8367 * The mct encoding validation procedure.
8369 * @param p_j2k the jpeg2000 codec to validate.
8370 * @param p_stream the input stream to validate.
8371 * @param p_manager the user event manager.
8373 * @return true if the parameters are correct.
8375 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8376 opj_stream_private_t *p_stream,
8377 opj_event_mgr_t * p_manager )
8379 opj_bool l_is_valid = OPJ_TRUE;
8383 assert(p_j2k != 00);
8384 assert(p_stream != 00);
8385 assert(p_manager != 00);
8387 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8388 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8389 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8391 for (i=0;i<l_nb_tiles;++i) {
8392 if (l_tcp->mct == 2) {
8393 opj_tccp_t * l_tccp = l_tcp->tccps;
8394 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8396 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8397 l_is_valid &= ! (l_tccp->qmfbid & 1);
8408 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8411 OPJ_UINT32 l_indix = 1;
8412 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8413 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8414 OPJ_UINT32 l_mct_size,l_nb_elem;
8415 OPJ_FLOAT32 * l_data, * l_current_data;
8416 opj_tccp_t * l_tccp;
8419 assert(p_tcp != 00);
8421 if (p_tcp->mct != 2) {
8425 if (p_tcp->m_mct_decoding_matrix) {
8426 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8427 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8429 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));
8430 if (! p_tcp->m_mct_records) {
8433 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8435 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8437 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8439 if (l_mct_deco_data->m_data) {
8440 opj_free(l_mct_deco_data->m_data);
8441 l_mct_deco_data->m_data = 00;
8444 l_mct_deco_data->m_index = l_indix++;
8445 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8446 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8447 l_nb_elem = p_image->numcomps * p_image->numcomps;
8448 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8449 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8451 if (! l_mct_deco_data->m_data) {
8455 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);
8457 l_mct_deco_data->m_data_size = l_mct_size;
8458 ++p_tcp->m_nb_mct_records;
8461 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8462 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8463 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));
8465 if (! p_tcp->m_mct_records) {
8469 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8470 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8472 if (l_mct_deco_data) {
8473 l_mct_deco_data = l_mct_offset_data - 1;
8477 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8479 if (l_mct_offset_data->m_data) {
8480 opj_free(l_mct_offset_data->m_data);
8481 l_mct_offset_data->m_data = 00;
8484 l_mct_offset_data->m_index = l_indix++;
8485 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8486 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8487 l_nb_elem = p_image->numcomps;
8488 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8489 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8491 if (! l_mct_offset_data->m_data) {
8495 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8497 opj_free(l_mct_offset_data->m_data);
8498 l_mct_offset_data->m_data = 00;
8502 l_tccp = p_tcp->tccps;
8503 l_current_data = l_data;
8505 for (i=0;i<l_nb_elem;++i) {
8506 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8510 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8514 l_mct_offset_data->m_data_size = l_mct_size;
8516 ++p_tcp->m_nb_mct_records;
8518 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8519 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8520 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8521 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8523 if (! p_tcp->m_mcc_records) {
8526 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8527 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));
8531 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8532 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8533 l_mcc_data->m_is_irreversible = 1;
8534 l_mcc_data->m_nb_comps = p_image->numcomps;
8535 l_mcc_data->m_index = l_indix++;
8536 l_mcc_data->m_offset_array = l_mct_offset_data;
8537 ++p_tcp->m_nb_mcc_records;
8543 * Builds the cp decoder parameters to use to decode tile.
8545 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8546 opj_stream_private_t *p_stream,
8547 opj_event_mgr_t * p_manager )
8549 /* add here initialization of cp
8550 copy paste of setup_decoder */
8558 * Builds the cp encoder parameters to use to encode tile.
8560 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8561 opj_stream_private_t *p_stream,
8562 opj_event_mgr_t * p_manager )
8564 /* add here initialization of cp
8565 copy paste of setup_encoder */
8573 * The default encoding validation procedure without any extension.
8575 * @param p_j2k the jpeg2000 codec to validate.
8576 * @param p_stream the input stream to validate.
8577 * @param p_manager the user event manager.
8579 * @return true if the parameters are correct.
8581 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8582 opj_stream_private_t *p_stream,
8583 opj_event_mgr_t * p_manager )
8585 opj_bool l_is_valid = OPJ_TRUE;
8588 assert(p_j2k != 00);
8589 assert(p_stream != 00);
8590 assert(p_manager != 00);
8592 /* STATE checking */
8593 /* make sure the state is at 0 */
8594 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8596 /* POINTER validation */
8597 /* make sure a p_j2k codec is present */
8598 l_is_valid &= (p_j2k->m_procedure_list != 00);
8599 /* make sure a validation list is present */
8600 l_is_valid &= (p_j2k->m_validation_list != 00);
8602 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8603 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8607 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8608 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8612 /* PARAMETER VALIDATION */
8617 * The default decoding validation procedure without any extension.
8619 * @param p_j2k the jpeg2000 codec to validate.
8620 * @param p_stream the input stream to validate.
8621 * @param p_manager the user event manager.
8623 * @return true if the parameters are correct.
8625 opj_bool j2k_decoding_validation (
8626 opj_j2k_v2_t *p_j2k,
8627 opj_stream_private_t *p_stream,
8628 opj_event_mgr_t * p_manager
8631 opj_bool l_is_valid = OPJ_TRUE;
8634 assert(p_j2k != 00);
8635 assert(p_stream != 00);
8636 assert(p_manager != 00);
8639 /* STATE checking */
8640 /* make sure the state is at 0 */
8642 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8644 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8646 /* POINTER validation */
8647 /* make sure a p_j2k codec is present */
8648 /* make sure a procedure list is present */
8649 l_is_valid &= (p_j2k->m_procedure_list != 00);
8650 /* make sure a validation list is present */
8651 l_is_valid &= (p_j2k->m_validation_list != 00);
8653 /* PARAMETER VALIDATION */
8657 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8658 struct opj_stream_private *p_stream,
8659 struct opj_event_mgr * p_manager)
8661 OPJ_UINT32 l_current_marker;
8662 OPJ_UINT32 l_marker_size;
8663 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8666 assert(p_stream != 00);
8667 assert(p_j2k != 00);
8668 assert(p_manager != 00);
8670 /* We enter in the main header */
8671 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8673 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8674 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8675 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8679 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8680 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8681 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8685 /* Read 2 bytes as the new marker ID */
8686 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8688 /* Try to read until the SOT is detected */
8689 while (l_current_marker != J2K_MS_SOT) {
8691 /* Check if the current marker ID is valid */
8692 if (l_current_marker < 0xff00) {
8693 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8697 /* Get the marker handler from the marker ID */
8698 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8700 /* Manage case where marker is unknown */
8701 if (l_marker_handler->id == J2K_MS_UNK) {
8702 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8703 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8707 if (l_current_marker == J2K_MS_SOT)
8708 break; /* SOT marker is detected main header is completely read */
8709 else /* Get the marker handler from the marker ID */
8710 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8713 /* Check if the marker is known and if it is the right place to find it */
8714 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8715 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8719 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8720 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8721 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8725 /* read 2 bytes as the marker size */
8726 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8727 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8729 /* Check if the marker size is compatible with the header data size */
8730 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8731 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8732 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8733 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8736 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8739 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8740 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) {
8741 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8745 /* Read the marker segment with the correct marker handler */
8746 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8747 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8751 /* Add the marker to the codestream index*/
8752 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8753 l_marker_handler->id,
8754 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8755 l_marker_size + 4 );
8757 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8758 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8759 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8763 /* read 2 bytes as the new marker ID */
8764 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8767 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8769 /* Position of the last element if the main header */
8770 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8772 /* Next step: read a tile-part header */
8773 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8779 * Excutes the given procedures on the given codec.
8781 * @param p_procedure_list the list of procedures to execute
8782 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8783 * @param p_stream the stream to execute the procedures on.
8784 * @param p_manager the user manager.
8786 * @return true if all the procedures were successfully executed.
8788 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8789 opj_procedure_list_t * p_procedure_list,
8790 opj_stream_private_t *p_stream,
8791 opj_event_mgr_t * p_manager )
8793 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8794 opj_bool l_result = OPJ_TRUE;
8795 OPJ_UINT32 l_nb_proc, i;
8798 assert(p_procedure_list != 00);
8799 assert(p_j2k != 00);
8800 assert(p_stream != 00);
8801 assert(p_manager != 00);
8804 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8805 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);
8807 for (i=0;i<l_nb_proc;++i) {
8808 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8812 /* and clear the procedure list at the end.*/
8813 opj_procedure_list_clear(p_procedure_list);
8818 opj_bool j2k_copy_default_tcp_and_create_tcd
8820 opj_j2k_v2_t * p_j2k,
8821 opj_stream_private_t *p_stream,
8822 opj_event_mgr_t * p_manager
8825 opj_tcp_v2_t * l_tcp = 00;
8826 opj_tcp_v2_t * l_default_tcp = 00;
8827 OPJ_UINT32 l_nb_tiles;
8829 opj_tccp_t *l_current_tccp = 00;
8830 OPJ_UINT32 l_tccp_size;
8831 OPJ_UINT32 l_mct_size;
8832 opj_image_t * l_image;
8833 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8834 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8835 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8836 OPJ_UINT32 l_offset;
8839 assert(p_j2k != 00);
8840 assert(p_stream != 00);
8841 assert(p_manager != 00);
8843 l_image = p_j2k->m_private_image;
8844 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8845 l_tcp = p_j2k->m_cp.tcps;
8846 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8847 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8848 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8851 for (i=0; i<l_nb_tiles; ++i) {
8852 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8853 l_current_tccp = l_tcp->tccps;
8854 /*Copy default coding parameters into the current tile coding parameters*/
8855 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8856 /* Initialize some values of the current tile coding parameters*/
8858 l_tcp->ppt_data = 00;
8859 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8860 l_tcp->tccps = l_current_tccp;
8862 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8863 if (l_default_tcp->m_mct_decoding_matrix) {
8864 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8865 if (! l_tcp->m_mct_decoding_matrix ) {
8868 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8871 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8872 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8873 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8874 if (! l_tcp->m_mct_records) {
8877 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8879 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8880 l_src_mct_rec = l_default_tcp->m_mct_records;
8881 l_dest_mct_rec = l_tcp->m_mct_records;
8883 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8885 if (l_src_mct_rec->m_data) {
8887 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8888 if(! l_dest_mct_rec->m_data) {
8891 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8898 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8899 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8900 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8901 if (! l_tcp->m_mcc_records) {
8904 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8906 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8907 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8908 l_dest_mcc_rec = l_tcp->m_mcc_records;
8910 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8912 if (l_src_mcc_rec->m_decorrelation_array) {
8913 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8914 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8917 if (l_src_mcc_rec->m_offset_array) {
8918 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8919 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8926 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8927 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8929 /* Move to next tile cp*/
8933 /* Create the current tile decoder*/
8934 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8935 if (! p_j2k->m_tcd ) {
8939 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8940 tcd_destroy_v2(p_j2k->m_tcd);
8942 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8950 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8951 * with the marker value.
8952 * @param p_id Marker value to look up
8954 * @return the handler associated with the id.
8956 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8958 const opj_dec_memory_marker_handler_t *e;
8959 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8960 if (e->id == p_id) {
8961 break; /* we find a handler corresponding to the marker ID*/
8969 * Destroys a jpeg2000 codec.
8971 * @param p_j2k the jpeg20000 structure to destroy.
8973 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8979 if (p_j2k->m_is_decoder) {
8981 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8982 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8983 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8984 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8987 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8988 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8989 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8990 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8995 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8996 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8997 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
9000 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
9001 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
9002 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
9003 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
9006 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
9007 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
9008 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
9009 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
9013 tcd_destroy_v2(p_j2k->m_tcd);
9015 j2k_cp_destroy(&(p_j2k->m_cp));
9016 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
9018 opj_procedure_list_destroy(p_j2k->m_procedure_list);
9019 p_j2k->m_procedure_list = 00;
9021 opj_procedure_list_destroy(p_j2k->m_validation_list);
9022 p_j2k->m_procedure_list = 00;
9024 j2k_destroy_cstr_index(p_j2k->cstr_index);
9025 p_j2k->cstr_index = NULL;
9027 opj_image_destroy(p_j2k->m_private_image);
9028 p_j2k->m_private_image = NULL;
9030 opj_image_destroy(p_j2k->m_output_image);
9031 p_j2k->m_output_image = NULL;
9036 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9040 if (p_cstr_ind->marker) {
9041 opj_free(p_cstr_ind->marker);
9042 p_cstr_ind->marker = NULL;
9045 if (p_cstr_ind->tile_index) {
9046 OPJ_UINT32 it_tile = 0;
9048 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9050 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9051 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9052 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9055 if(p_cstr_ind->tile_index[it_tile].tp_index){
9056 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9057 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9060 if(p_cstr_ind->tile_index[it_tile].marker){
9061 opj_free(p_cstr_ind->tile_index[it_tile].marker);
9062 p_cstr_ind->tile_index[it_tile].marker = NULL;
9067 opj_free( p_cstr_ind->tile_index);
9068 p_cstr_ind->tile_index = NULL;
9071 opj_free(p_cstr_ind);
9078 * Destroys a tile coding parameter structure.
9080 * @param p_tcp the tile coding parameter to destroy.
9082 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9088 if (p_tcp->ppt_buffer != 00) {
9089 opj_free(p_tcp->ppt_buffer);
9090 p_tcp->ppt_buffer = 00;
9093 if (p_tcp->tccps != 00) {
9094 opj_free(p_tcp->tccps);
9098 if (p_tcp->m_mct_coding_matrix != 00) {
9099 opj_free(p_tcp->m_mct_coding_matrix);
9100 p_tcp->m_mct_coding_matrix = 00;
9103 if (p_tcp->m_mct_decoding_matrix != 00) {
9104 opj_free(p_tcp->m_mct_decoding_matrix);
9105 p_tcp->m_mct_decoding_matrix = 00;
9108 if (p_tcp->m_mcc_records) {
9109 opj_free(p_tcp->m_mcc_records);
9110 p_tcp->m_mcc_records = 00;
9111 p_tcp->m_nb_max_mcc_records = 0;
9112 p_tcp->m_nb_mcc_records = 0;
9115 if (p_tcp->m_mct_records) {
9116 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9119 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9120 if (l_mct_data->m_data) {
9121 opj_free(l_mct_data->m_data);
9122 l_mct_data->m_data = 00;
9128 opj_free(p_tcp->m_mct_records);
9129 p_tcp->m_mct_records = 00;
9132 if (p_tcp->mct_norms != 00) {
9133 opj_free(p_tcp->mct_norms);
9134 p_tcp->mct_norms = 00;
9137 j2k_tcp_data_destroy(p_tcp);
9142 * Destroys the data inside a tile coding parameter structure.
9144 * @param p_tcp the tile coding parameter which contain data to destroy.
9146 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9148 if (p_tcp->m_data) {
9149 opj_free(p_tcp->m_data);
9150 p_tcp->m_data = NULL;
9151 p_tcp->m_data_size = 0;
9156 * Destroys a coding parameter structure.
9158 * @param p_cp the coding parameter to destroy.
9160 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9162 OPJ_UINT32 l_nb_tiles;
9163 opj_tcp_v2_t * l_current_tile = 00;
9174 l_current_tile = p_cp->tcps;
9175 l_nb_tiles = p_cp->th * p_cp->tw;
9178 (i = 0; i < l_nb_tiles; ++i)
9180 j2k_tcp_destroy(l_current_tile);
9183 opj_free(p_cp->tcps);
9187 (p_cp->ppm_buffer != 00)
9189 opj_free(p_cp->ppm_buffer);
9190 p_cp->ppm_buffer = 00;
9193 (p_cp->comment != 00)
9195 opj_free(p_cp->comment);
9199 (! p_cp->m_is_decoder)
9202 (p_cp->m_specific_param.m_enc.m_matrice)
9204 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9205 p_cp->m_specific_param.m_enc.m_matrice = 00;
9213 * Reads a tile header.
9214 * @param p_j2k the jpeg2000 codec.
9215 * @param p_stream the stream to write data to.
9216 * @param p_manager the user event manager.
9218 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9219 OPJ_UINT32 * p_tile_index,
9220 OPJ_UINT32 * p_data_size,
9221 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9222 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9223 OPJ_UINT32 * p_nb_comps,
9225 opj_stream_private_t *p_stream,
9226 opj_event_mgr_t * p_manager )
9228 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9229 OPJ_UINT32 l_marker_size;
9230 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9231 opj_tcp_v2_t * l_tcp = NULL;
9232 OPJ_UINT32 l_nb_tiles;
9235 assert(p_stream != 00);
9236 assert(p_j2k != 00);
9237 assert(p_manager != 00);
9239 /* Reach the End Of Codestream ?*/
9240 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9241 l_current_marker = J2K_MS_EOC;
9243 /* We need to encounter a SOT marker (a new tile-part header) */
9244 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9248 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9249 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9251 /* Try to read until the Start Of Data is detected */
9252 while (l_current_marker != J2K_MS_SOD) {
9254 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9255 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9256 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9260 /* Read 2 bytes from the buffer as the marker size */
9261 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9263 /* Why this condition? FIXME */
9264 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9265 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9267 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9269 /* Get the marker handler from the marker ID */
9270 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9272 /* Check if the marker is known and if it is the right place to find it */
9273 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9274 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9277 /* FIXME manage case of unknown marker as in the main header ? */
9279 /* Check if the marker size is compatible with the header data size */
9280 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9281 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9282 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9283 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9286 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9289 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9290 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) {
9291 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9295 /* Read the marker segment with the correct marker handler */
9296 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9297 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9301 /* Add the marker to the codestream index*/
9302 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9304 l_marker_handler->id,
9305 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9306 l_marker_size + 4 );
9308 /* Keep the position of the last SOT marker read */
9309 if ( l_marker_handler->id == J2K_MS_SOT ) {
9310 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9311 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9313 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9318 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9319 /* Skip the rest of the tile part header*/
9320 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) {
9321 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9324 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9327 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9328 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9329 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9332 /* Read 2 bytes from the buffer as the new marker ID */
9333 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9337 /* If we didn't skip data before, we need to read the SOD marker*/
9338 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9339 /* Try to read the SOD marker and skip data ? FIXME */
9340 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
9346 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9347 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9348 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9349 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9353 /* Read 2 bytes from buffer as the new marker ID */
9354 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9358 /* Indicate we will try to read a new tile-part header*/
9359 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9360 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9361 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9363 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9364 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9365 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9369 /* Read 2 bytes from buffer as the new marker ID */
9370 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9374 /* Current marker is the EOC marker ?*/
9375 if (l_current_marker == J2K_MS_EOC) {
9376 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9377 p_j2k->m_current_tile_number = 0;
9378 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9383 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9384 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9385 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9387 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9388 ++p_j2k->m_current_tile_number;
9392 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9393 *p_go_on = OPJ_FALSE;
9399 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9400 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9404 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9405 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9407 *p_tile_index = p_j2k->m_current_tile_number;
9408 *p_go_on = OPJ_TRUE;
9409 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9410 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9411 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9412 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9413 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9414 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9416 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9422 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9423 OPJ_UINT32 p_tile_index,
9425 OPJ_UINT32 p_data_size,
9426 opj_stream_private_t *p_stream,
9427 opj_event_mgr_t * p_manager )
9429 OPJ_UINT32 l_current_marker;
9430 OPJ_BYTE l_data [2];
9431 opj_tcp_v2_t * l_tcp;
9434 assert(p_stream != 00);
9435 assert(p_j2k != 00);
9436 assert(p_manager != 00);
9438 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9439 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9443 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9444 if (! l_tcp->m_data) {
9445 j2k_tcp_destroy(l_tcp);
9449 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9453 p_j2k->cstr_index) ) {
9454 j2k_tcp_destroy(l_tcp);
9455 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9459 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9463 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9464 * we destroy just the data which will be re-read in read_tile_header*/
9465 /*j2k_tcp_destroy(l_tcp);
9466 p_j2k->m_tcd->tcp = 0;*/
9467 j2k_tcp_data_destroy(l_tcp);
9469 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9470 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9472 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9473 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9474 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9478 opj_read_bytes(l_data,&l_current_marker,2);
9480 if (l_current_marker == J2K_MS_EOC) {
9481 p_j2k->m_current_tile_number = 0;
9482 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9484 else if (l_current_marker != J2K_MS_SOT)
9486 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9495 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9497 OPJ_UINT32 i,j,k = 0;
9498 OPJ_UINT32 l_width_src,l_height_src;
9499 OPJ_UINT32 l_width_dest,l_height_dest;
9500 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9501 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9502 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9503 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9504 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9506 opj_image_comp_t * l_img_comp_src = 00;
9507 opj_image_comp_t * l_img_comp_dest = 00;
9509 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9510 opj_image_t * l_image_src = 00;
9511 OPJ_UINT32 l_size_comp, l_remaining;
9512 OPJ_INT32 * l_dest_ptr;
9513 opj_tcd_resolution_v2_t* l_res= 00;
9515 l_tilec = p_tcd->tcd_image->tiles->comps;
9516 l_image_src = p_tcd->image;
9517 l_img_comp_src = l_image_src->comps;
9519 l_img_comp_dest = p_output_image->comps;
9521 for (i=0; i<l_image_src->numcomps; i++) {
9523 /* Allocate output component buffer if necessary */
9524 if (!l_img_comp_dest->data) {
9526 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9527 if (! l_img_comp_dest->data) {
9532 /* Copy info from decoded comp image to output image */
9533 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9536 /* Compute the precision of the output buffer */
9537 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9538 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9539 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9545 if (l_size_comp == 3) {
9550 /* Current tile component size*/
9552 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9553 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9556 l_width_src = (l_res->x1 - l_res->x0);
9557 l_height_src = (l_res->y1 - l_res->y0);
9559 /* Border of the current output component*/
9560 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9561 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9562 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9563 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9566 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9567 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9571 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9572 * of the input buffer (decoded tile component) which will be move
9573 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9574 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9575 * by this input area.
9577 assert( l_res->x0 >= 0);
9578 assert( l_res->x1 >= 0);
9579 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9580 l_start_x_dest = l_res->x0 - l_x0_dest;
9581 l_offset_x0_src = 0;
9583 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9584 l_width_dest = l_width_src;
9585 l_offset_x1_src = 0;
9588 l_width_dest = l_x1_dest - l_res->x0 ;
9589 l_offset_x1_src = l_width_src - l_width_dest;
9593 l_start_x_dest = 0 ;
9594 l_offset_x0_src = l_x0_dest - l_res->x0;
9596 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9597 l_width_dest = l_width_src - l_offset_x0_src;
9598 l_offset_x1_src = 0;
9601 l_width_dest = l_img_comp_dest->w ;
9602 l_offset_x1_src = l_res->x1 - l_x1_dest;
9606 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9607 l_start_y_dest = l_res->y0 - l_y0_dest;
9608 l_offset_y0_src = 0;
9610 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9611 l_height_dest = l_height_src;
9612 l_offset_y1_src = 0;
9615 l_height_dest = l_y1_dest - l_res->y0 ;
9616 l_offset_y1_src = l_height_src - l_height_dest;
9620 l_start_y_dest = 0 ;
9621 l_offset_y0_src = l_y0_dest - l_res->y0;
9623 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9624 l_height_dest = l_height_src - l_offset_y0_src;
9625 l_offset_y1_src = 0;
9628 l_height_dest = l_img_comp_dest->h ;
9629 l_offset_y1_src = l_res->y1 - l_y1_dest;
9633 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9638 /* Compute the input buffer offset */
9639 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9640 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9641 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9643 /* Compute the output buffer offset */
9644 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9645 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9647 /* Move the output buffer to the first place where we will write*/
9648 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9651 fprintf(stdout, "COMPO[%d]:\n",i);
9652 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9653 "\t tile offset:%d, %d, %d, %d\n"
9654 "\t buffer offset: %d; %d, %d\n",
9655 l_res->x0, l_res->y0, l_width_src, l_height_src,
9656 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9657 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9659 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9660 "\t start offset: %d, line offset= %d\n",
9661 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9665 switch (l_size_comp) {
9668 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9669 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9671 if (l_img_comp_src->sgnd) {
9672 for (j = 0 ; j < l_height_dest ; ++j) {
9673 for ( k = 0 ; k < l_width_dest ; ++k) {
9674 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9677 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9678 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9682 for ( j = 0 ; j < l_height_dest ; ++j ) {
9683 for ( k = 0 ; k < l_width_dest ; ++k) {
9684 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9687 l_dest_ptr+= l_line_offset_dest;
9688 l_src_ptr += l_line_offset_src;
9692 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9693 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9698 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9699 l_src_ptr += l_start_offset_src;
9701 if (l_img_comp_src->sgnd) {
9702 for (j=0;j<l_height_dest;++j) {
9703 for (k=0;k<l_width_dest;++k) {
9704 *(l_dest_ptr++) = *(l_src_ptr++);
9707 l_dest_ptr+= l_line_offset_dest;
9708 l_src_ptr += l_line_offset_src ;
9712 for (j=0;j<l_height_dest;++j) {
9713 for (k=0;k<l_width_dest;++k) {
9714 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9717 l_dest_ptr+= l_line_offset_dest;
9718 l_src_ptr += l_line_offset_src ;
9722 l_src_ptr += l_end_offset_src;
9723 p_data = (OPJ_BYTE*) l_src_ptr;
9728 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9729 l_src_ptr += l_start_offset_src;
9731 for (j=0;j<l_height_dest;++j) {
9732 for (k=0;k<l_width_dest;++k) {
9733 *(l_dest_ptr++) = (*(l_src_ptr++));
9736 l_dest_ptr+= l_line_offset_dest;
9737 l_src_ptr += l_line_offset_src ;
9740 l_src_ptr += l_end_offset_src;
9741 p_data = (OPJ_BYTE*) l_src_ptr;
9755 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9757 * @param p_j2k the jpeg2000 codec.
9758 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9759 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9760 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9761 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9762 * @param p_manager the user event manager
9764 * @return true if the area could be set.
9766 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9767 opj_image_t* p_image,
9768 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9769 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9770 struct opj_event_mgr * p_manager )
9772 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9773 opj_image_t * l_image = p_j2k->m_private_image;
9776 OPJ_INT32 l_comp_x1, l_comp_y1;
9777 opj_image_comp_t* l_img_comp = NULL;
9779 /* Check if we are read the main header */
9780 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9781 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9785 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9786 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9788 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9789 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9790 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9791 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9797 /* Check if the positions provided by the user are correct */
9800 assert(p_start_x >= 0 );
9801 assert(p_start_y >= 0 );
9803 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9804 opj_event_msg_v2(p_manager, EVT_ERROR,
9805 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9806 p_start_x, l_image->x1);
9809 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9810 opj_event_msg_v2(p_manager, EVT_WARNING,
9811 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9812 p_start_x, l_image->x0);
9813 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9814 p_image->x0 = l_image->x0;
9817 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9818 p_image->x0 = p_start_x;
9822 if ((OPJ_UINT32)p_start_y > l_image->y1){
9823 opj_event_msg_v2(p_manager, EVT_ERROR,
9824 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9825 p_start_y, l_image->y1);
9828 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9829 opj_event_msg_v2(p_manager, EVT_WARNING,
9830 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9831 p_start_y, l_image->y0);
9832 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9833 p_image->y0 = l_image->y0;
9836 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9837 p_image->y0 = p_start_y;
9841 assert((OPJ_UINT32)p_end_x > 0);
9842 assert((OPJ_UINT32)p_end_y > 0);
9843 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9844 opj_event_msg_v2(p_manager, EVT_ERROR,
9845 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9846 p_end_x, l_image->x0);
9849 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9850 opj_event_msg_v2(p_manager, EVT_WARNING,
9851 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9852 p_end_x, l_image->x1);
9853 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9854 p_image->x1 = l_image->x1;
9857 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9858 p_image->x1 = p_end_x;
9862 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9863 opj_event_msg_v2(p_manager, EVT_ERROR,
9864 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9865 p_end_y, l_image->y0);
9868 if ((OPJ_UINT32)p_end_y > l_image->y1){
9869 opj_event_msg_v2(p_manager, EVT_WARNING,
9870 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9871 p_end_y, l_image->y1);
9872 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9873 p_image->y1 = l_image->y1;
9876 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9877 p_image->y1 = p_end_y;
9881 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9883 l_img_comp = p_image->comps;
9884 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9888 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9889 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9890 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9891 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9893 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9894 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9896 opj_event_msg_v2(p_manager, EVT_ERROR,
9897 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9901 l_img_comp->w = l_w;
9903 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9904 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9906 opj_event_msg_v2(p_manager, EVT_ERROR,
9907 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9911 l_img_comp->h = l_h;
9916 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9917 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9924 /* ----------------------------------------------------------------------- */
9925 /* J2K / JPT decoder interface */
9926 /* ----------------------------------------------------------------------- */
9928 * Creates a J2K decompression structure.
9930 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9932 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9934 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9938 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9940 l_j2k->m_is_decoder = 1;
9941 l_j2k->m_cp.m_is_decoder = 1;
9943 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9944 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9948 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9950 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9951 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9956 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9958 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9960 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9962 /* codestream index creation */
9963 l_j2k->cstr_index = j2k_create_cstr_index();
9965 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9966 if (!l_j2k->cstr_index){
9971 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9974 /* validation list creation */
9975 l_j2k->m_validation_list = opj_procedure_list_create();
9976 if (! l_j2k->m_validation_list) {
9981 /* execution list creation */
9982 l_j2k->m_procedure_list = opj_procedure_list_create();
9983 if (! l_j2k->m_procedure_list) {
9992 opj_codestream_index_t* j2k_create_cstr_index(void)
9994 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9995 opj_calloc(1,sizeof(opj_codestream_index_t));
9999 cstr_index->maxmarknum = 100;
10000 cstr_index->marknum = 0;
10001 cstr_index->marker = (opj_marker_info_t*)
10002 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
10003 if (!cstr_index-> marker)
10006 cstr_index->tile_index = NULL;
10013 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
10015 * @param p_tile_no the tile index.
10016 * @param p_comp_no the component being outputted.
10017 * @param p_j2k the J2K codec.
10019 * @return the number of bytes taken by the SPCod element.
10021 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
10022 OPJ_UINT32 p_tile_no,
10023 OPJ_UINT32 p_comp_no )
10025 opj_cp_v2_t *l_cp = 00;
10026 opj_tcp_v2_t *l_tcp = 00;
10027 opj_tccp_t *l_tccp = 00;
10029 /* preconditions */
10030 assert(p_j2k != 00);
10032 l_cp = &(p_j2k->m_cp);
10033 l_tcp = &l_cp->tcps[p_tile_no];
10034 l_tccp = &l_tcp->tccps[p_comp_no];
10036 /* preconditions again */
10037 assert(p_tile_no < (l_cp->tw * l_cp->th));
10038 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10040 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10041 return 5 + l_tccp->numresolutions;
10049 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10051 * @param p_comp_no the component number to output.
10052 * @param p_stream the stream to write data to.
10053 * @param p_j2k J2K codec.
10054 * @param p_manager the user event manager.
10057 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10058 OPJ_UINT32 p_tile_no,
10059 OPJ_UINT32 p_comp_no,
10061 OPJ_UINT32 * p_header_size,
10062 struct opj_event_mgr * p_manager )
10065 opj_cp_v2_t *l_cp = 00;
10066 opj_tcp_v2_t *l_tcp = 00;
10067 opj_tccp_t *l_tccp = 00;
10069 /* preconditions */
10070 assert(p_j2k != 00);
10071 assert(p_header_size != 00);
10072 assert(p_manager != 00);
10073 assert(p_data != 00);
10075 l_cp = &(p_j2k->m_cp);
10076 l_tcp = &l_cp->tcps[p_tile_no];
10077 l_tccp = &l_tcp->tccps[p_comp_no];
10079 /* preconditions again */
10080 assert(p_tile_no < (l_cp->tw * l_cp->th));
10081 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10083 if (*p_header_size < 5) {
10084 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10088 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
10091 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
10094 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
10097 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
10100 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
10103 *p_header_size = *p_header_size - 5;
10105 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10107 if (*p_header_size < l_tccp->numresolutions) {
10108 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10112 for (i = 0; i < l_tccp->numresolutions; ++i) {
10113 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
10117 *p_header_size = *p_header_size - l_tccp->numresolutions;
10124 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10125 * @param p_header_data the data contained in the COM box.
10126 * @param p_j2k the jpeg2000 codec.
10127 * @param p_header_size the size of the data contained in the COM marker.
10128 * @param p_manager the user event manager.
10130 opj_bool j2k_read_SPCod_SPCoc(
10131 opj_j2k_v2_t *p_j2k,
10133 OPJ_BYTE * p_header_data,
10134 OPJ_UINT32 * p_header_size,
10135 struct opj_event_mgr * p_manager
10138 OPJ_UINT32 i, l_tmp;
10139 opj_cp_v2_t *l_cp = NULL;
10140 opj_tcp_v2_t *l_tcp = NULL;
10141 opj_tccp_t *l_tccp = NULL;
10142 OPJ_BYTE * l_current_ptr = NULL;
10144 /* preconditions */
10145 assert(p_j2k != 00);
10146 assert(p_manager != 00);
10147 assert(p_header_data != 00);
10149 l_cp = &(p_j2k->m_cp);
10150 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10151 &l_cp->tcps[p_j2k->m_current_tile_number] :
10152 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10154 /* precondition again */
10155 assert(compno < p_j2k->m_private_image->numcomps);
10157 l_tccp = &l_tcp->tccps[compno];
10158 l_current_ptr = p_header_data;
10160 /* make sure room is sufficient */
10161 if (*p_header_size < 5) {
10162 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10166 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10167 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10170 /* If user wants to remove more resolutions than the codestream contains, return error */
10171 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10172 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10173 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10174 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10178 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10180 l_tccp->cblkw += 2;
10182 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10184 l_tccp->cblkh += 2;
10186 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10189 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10192 *p_header_size = *p_header_size - 5;
10194 /* use custom precinct size ? */
10195 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10196 if (*p_header_size < l_tccp->numresolutions) {
10197 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10201 for (i = 0; i < l_tccp->numresolutions; ++i) {
10202 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10204 l_tccp->prcw[i] = l_tmp & 0xf;
10205 l_tccp->prch[i] = l_tmp >> 4;
10208 *p_header_size = *p_header_size - l_tccp->numresolutions;
10211 /* set default size for the precinct width and height */
10212 for (i = 0; i < l_tccp->numresolutions; ++i) {
10213 l_tccp->prcw[i] = 15;
10214 l_tccp->prch[i] = 15;
10218 #ifdef WIP_REMOVE_MSD
10220 if (p_j2k->cstr_info && compno == 0) {
10221 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10223 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10224 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10225 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10226 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10227 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10230 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10231 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10240 * Copies the tile component parameters of all the component from the first tile component.
10242 * @param p_j2k the J2k codec.
10244 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10248 opj_cp_v2_t *l_cp = NULL;
10249 opj_tcp_v2_t *l_tcp = NULL;
10250 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10251 OPJ_UINT32 l_prc_size;
10253 /* preconditions */
10254 assert(p_j2k != 00);
10256 l_cp = &(p_j2k->m_cp);
10257 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10258 &l_cp->tcps[p_j2k->m_current_tile_number] :
10259 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10261 l_ref_tccp = &l_tcp->tccps[0];
10262 l_copied_tccp = l_ref_tccp + 1;
10263 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10265 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10266 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10267 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10268 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10269 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10270 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10271 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10272 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10278 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10280 * @param p_tile_no the tile index.
10281 * @param p_comp_no the component being outputted.
10282 * @param p_j2k the J2K codec.
10284 * @return the number of bytes taken by the SPCod element.
10286 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10287 OPJ_UINT32 p_tile_no,
10288 OPJ_UINT32 p_comp_no )
10290 OPJ_UINT32 l_num_bands;
10292 opj_cp_v2_t *l_cp = 00;
10293 opj_tcp_v2_t *l_tcp = 00;
10294 opj_tccp_t *l_tccp = 00;
10296 /* preconditions */
10297 assert(p_j2k != 00);
10299 l_cp = &(p_j2k->m_cp);
10300 l_tcp = &l_cp->tcps[p_tile_no];
10301 l_tccp = &l_tcp->tccps[p_comp_no];
10303 /* preconditions again */
10304 assert(p_tile_no < l_cp->tw * l_cp->th);
10305 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10307 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10309 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10310 return 1 + l_num_bands;
10313 return 1 + 2*l_num_bands;
10318 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10320 * @param p_tile_no the tile to output.
10321 * @param p_comp_no the component number to output.
10322 * @param p_data the data buffer.
10323 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10324 * @param p_j2k J2K codec.
10325 * @param p_manager the user event manager.
10328 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10329 OPJ_UINT32 p_tile_no,
10330 OPJ_UINT32 p_comp_no,
10332 OPJ_UINT32 * p_header_size,
10333 struct opj_event_mgr * p_manager )
10335 OPJ_UINT32 l_header_size;
10336 OPJ_UINT32 l_band_no, l_num_bands;
10337 OPJ_UINT32 l_expn,l_mant;
10339 opj_cp_v2_t *l_cp = 00;
10340 opj_tcp_v2_t *l_tcp = 00;
10341 opj_tccp_t *l_tccp = 00;
10343 /* preconditions */
10344 assert(p_j2k != 00);
10345 assert(p_header_size != 00);
10346 assert(p_manager != 00);
10347 assert(p_data != 00);
10349 l_cp = &(p_j2k->m_cp);
10350 l_tcp = &l_cp->tcps[p_tile_no];
10351 l_tccp = &l_tcp->tccps[p_comp_no];
10353 /* preconditions again */
10354 assert(p_tile_no < l_cp->tw * l_cp->th);
10355 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10357 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10359 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10360 l_header_size = 1 + l_num_bands;
10362 if (*p_header_size < l_header_size) {
10363 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10367 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10370 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10371 l_expn = l_tccp->stepsizes[l_band_no].expn;
10372 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10377 l_header_size = 1 + 2*l_num_bands;
10379 if (*p_header_size < l_header_size) {
10380 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10384 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10387 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10388 l_expn = l_tccp->stepsizes[l_band_no].expn;
10389 l_mant = l_tccp->stepsizes[l_band_no].mant;
10391 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10396 *p_header_size = *p_header_size - l_header_size;
10402 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10404 * @param p_comp_no the component being targeted.
10405 * @param p_header_data the data contained in the COM box.
10406 * @param p_j2k the jpeg2000 codec.
10407 * @param p_header_size the size of the data contained in the COM marker.
10408 * @param p_manager the user event manager.
10410 opj_bool j2k_read_SQcd_SQcc(
10411 opj_j2k_v2_t *p_j2k,
10412 OPJ_UINT32 p_comp_no,
10413 OPJ_BYTE* p_header_data,
10414 OPJ_UINT32 * p_header_size,
10415 struct opj_event_mgr * p_manager
10419 OPJ_UINT32 l_band_no;
10420 opj_cp_v2_t *l_cp = 00;
10421 opj_tcp_v2_t *l_tcp = 00;
10422 opj_tccp_t *l_tccp = 00;
10423 OPJ_BYTE * l_current_ptr = 00;
10424 OPJ_UINT32 l_tmp, l_num_band;
10427 assert(p_j2k != 00);
10428 assert(p_manager != 00);
10429 assert(p_header_data != 00);
10431 l_cp = &(p_j2k->m_cp);
10432 /* come from tile part header or main header ?*/
10433 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10434 &l_cp->tcps[p_j2k->m_current_tile_number] :
10435 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10437 /* precondition again*/
10438 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10440 l_tccp = &l_tcp->tccps[p_comp_no];
10441 l_current_ptr = p_header_data;
10443 if (*p_header_size < 1) {
10444 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10447 *p_header_size -= 1;
10449 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10452 l_tccp->qntsty = l_tmp & 0x1f;
10453 l_tccp->numgbits = l_tmp >> 5;
10454 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10458 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10460 (*p_header_size) / 2;
10462 if( l_num_band > J2K_MAXBANDS ) {
10463 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10464 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10465 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10466 /*return OPJ_FALSE;*/
10471 if (l_cp->correct) {
10473 /* if JPWL is on, we check whether there are too many subbands */
10474 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10475 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10476 "JPWL: bad number of subbands in Sqcx (%d)\n",
10478 if (!JPWL_ASSUME) {
10479 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10482 /* we try to correct */
10484 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10485 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10490 #endif /* USE_JPWL */
10492 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10493 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10494 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10496 if (l_band_no < J2K_MAXBANDS){
10497 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10498 l_tccp->stepsizes[l_band_no].mant = 0;
10501 *p_header_size = *p_header_size - l_num_band;
10504 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10505 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10507 if (l_band_no < J2K_MAXBANDS){
10508 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10509 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10512 *p_header_size = *p_header_size - 2*l_num_band;
10515 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10516 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10517 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10518 l_tccp->stepsizes[l_band_no].expn =
10519 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10520 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10521 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10529 * Copies the tile component parameters of all the component from the first tile component.
10531 * @param p_j2k the J2k codec.
10533 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10536 opj_cp_v2_t *l_cp = NULL;
10537 opj_tcp_v2_t *l_tcp = NULL;
10538 opj_tccp_t *l_ref_tccp = NULL;
10539 opj_tccp_t *l_copied_tccp = NULL;
10542 /* preconditions */
10543 assert(p_j2k != 00);
10545 l_cp = &(p_j2k->m_cp);
10546 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10547 &l_cp->tcps[p_j2k->m_current_tile_number] :
10548 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10550 l_ref_tccp = &l_tcp->tccps[0];
10551 l_copied_tccp = l_ref_tccp + 1;
10552 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10554 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10555 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10556 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10557 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10563 * Dump some elements from the J2K decompression structure .
10565 *@param p_j2k the jpeg2000 codec.
10566 *@param flag flag to describe what elments are dump.
10567 *@param out_stream output stream where dump the elements.
10570 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10572 /* Check if the flag is compatible with j2k file*/
10573 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10574 fprintf(out_stream, "Wrong flag\n");
10578 /* Dump the image_header */
10579 if (flag & OPJ_IMG_INFO){
10580 if (p_j2k->m_private_image)
10581 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10584 /* Dump the codestream info from main header */
10585 if (flag & OPJ_J2K_MH_INFO){
10586 j2k_dump_MH_info(p_j2k, out_stream);
10590 /* Dump the codestream info of the current tile */
10591 if (flag & OPJ_J2K_TH_INFO){
10595 /* Dump the codestream index from main header */
10596 if (flag & OPJ_J2K_MH_IND){
10597 j2k_dump_MH_index(p_j2k, out_stream);
10600 /* Dump the codestream index of the current tile */
10601 if (flag & OPJ_J2K_TH_IND){
10608 * Dump index elements of the codestream extract from the main header.
10610 *@param p_j2k the jpeg2000 codec.
10611 *@param out_stream output stream where dump the elements.
10614 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10616 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10617 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10619 fprintf(out_stream, "Codestream index from main header: {\n");
10621 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10622 "\t Main header end position=%" PRIi64 "\n",
10623 cstr_index->main_head_start, cstr_index->main_head_end);
10625 fprintf(out_stream, "\t Marker list: {\n");
10627 if (cstr_index->marker){
10628 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10629 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10630 cstr_index->marker[it_marker].type,
10631 cstr_index->marker[it_marker].pos,
10632 cstr_index->marker[it_marker].len );
10636 fprintf(out_stream, "\t }\n");
10639 if (cstr_index->tile_index){
10641 /* Simple test to avoid to write empty information*/
10642 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10643 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10644 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10647 if (l_acc_nb_of_tile_part)
10649 fprintf(out_stream, "\t Tile index: {\n");
10651 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10652 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10654 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10656 if (cstr_index->tile_index[it_tile].tp_index){
10657 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10658 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10660 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10661 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10662 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10666 if (cstr_index->tile_index[it_tile].marker){
10667 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10668 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10669 cstr_index->tile_index[it_tile].marker[it_marker].type,
10670 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10671 cstr_index->tile_index[it_tile].marker[it_marker].len );
10675 fprintf(out_stream,"\t }\n");
10679 fprintf(out_stream,"}\n");
10684 * Dump info elements of the codestream extract from the main header.
10686 *@param p_j2k the jpeg2000 codec.
10687 *@param out_stream output stream where dump the elements.
10690 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10692 opj_tcp_v2_t * l_default_tile=NULL;
10694 fprintf(out_stream, "Codestream info from main header: {\n");
10696 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10697 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10698 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10700 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10701 if (l_default_tile)
10704 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10706 fprintf(out_stream, "\t default tile {\n");
10707 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10708 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10709 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10710 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10712 for (compno = 0; compno < numcomps; compno++) {
10713 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10715 OPJ_INT32 bandno, numbands;
10718 fprintf(out_stream, "\t\t comp %d {\n", compno);
10719 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10720 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10721 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10722 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10723 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10724 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10726 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10727 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10728 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10730 fprintf(out_stream, "\n");
10732 /* quantization style*/
10733 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10734 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10735 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10736 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10737 for (bandno = 0; bandno < numbands; bandno++) {
10738 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10739 l_tccp->stepsizes[bandno].expn);
10741 fprintf(out_stream, "\n");
10744 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10746 fprintf(out_stream, "\t\t }\n");
10747 } /*end of component of default tile*/
10748 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10752 fprintf(out_stream, "}\n");
10757 * Dump an image header structure.
10759 *@param img_header the image header to dump.
10760 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10761 *@param out_stream output stream where dump the elements.
10763 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10767 if (dev_dump_flag){
10768 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10772 fprintf(out_stream, "Image info {\n");
10773 tab[0] = '\t';tab[1] = '\0';
10776 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10777 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10778 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10780 if (img_header->comps){
10782 for (compno = 0; compno < img_header->numcomps; compno++) {
10783 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10784 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10785 fprintf(out_stream,"%s}\n",tab);
10789 fprintf(out_stream, "}\n");
10793 * Dump a component image header structure.
10795 *@param comp_header the component image header to dump.
10796 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10797 *@param out_stream output stream where dump the elements.
10799 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10803 if (dev_dump_flag){
10804 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10807 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10810 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10811 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10812 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10815 fprintf(out_stream, "}\n");
10820 * Get the codestream info from a JPEG2000 codec.
10822 *@param p_j2k the component image header to dump.
10824 *@return the codestream information extract from the jpg2000 codec
10826 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10829 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10830 opj_tcp_v2_t *l_default_tile;
10831 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10833 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10835 cstr_info->tx0 = p_j2k->m_cp.tx0;
10836 cstr_info->ty0 = p_j2k->m_cp.ty0;
10837 cstr_info->tdx = p_j2k->m_cp.tdx;
10838 cstr_info->tdy = p_j2k->m_cp.tdy;
10839 cstr_info->tw = p_j2k->m_cp.tw;
10840 cstr_info->th = p_j2k->m_cp.th;
10842 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10844 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10846 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10847 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10848 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10849 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10851 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10853 for (compno = 0; compno < numcomps; compno++) {
10854 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10855 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10856 OPJ_INT32 bandno, numbands;
10859 l_tccp_info->csty = l_tccp->csty;
10860 l_tccp_info->numresolutions = l_tccp->numresolutions;
10861 l_tccp_info->cblkw = l_tccp->cblkw;
10862 l_tccp_info->cblkh = l_tccp->cblkh;
10863 l_tccp_info->cblksty = l_tccp->cblksty;
10864 l_tccp_info->qmfbid = l_tccp->qmfbid;
10865 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10867 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10868 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10871 /* quantization style*/
10872 l_tccp_info->qntsty = l_tccp->qntsty;
10873 l_tccp_info->numgbits = l_tccp->numgbits;
10875 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10876 if (numbands < J2K_MAXBANDS) {
10877 for (bandno = 0; bandno < numbands; bandno++) {
10878 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10879 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10884 l_tccp_info->roishift = l_tccp->roishift;
10892 * Get the codestream index from a JPEG2000 codec.
10894 *@param p_j2k the component image header to dump.
10896 *@return the codestream index extract from the jpg2000 codec
10898 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10900 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10901 opj_calloc(1,sizeof(opj_codestream_index_t));
10905 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10906 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10907 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10909 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10910 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10911 if (!l_cstr_index->marker){
10912 opj_free( l_cstr_index);
10916 if (p_j2k->cstr_index->marker)
10917 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10919 opj_free(l_cstr_index->marker);
10920 l_cstr_index->marker = NULL;
10923 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10924 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10925 if (!l_cstr_index->tile_index){
10926 opj_free( l_cstr_index->marker);
10927 opj_free( l_cstr_index);
10931 if (!p_j2k->cstr_index->tile_index){
10932 opj_free(l_cstr_index->tile_index);
10933 l_cstr_index->tile_index = NULL;
10936 OPJ_UINT32 it_tile = 0;
10937 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10940 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10942 l_cstr_index->tile_index[it_tile].marker =
10943 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10945 if (!l_cstr_index->tile_index[it_tile].marker) {
10946 OPJ_UINT32 it_tile_free;
10948 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10949 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10952 opj_free( l_cstr_index->tile_index);
10953 opj_free( l_cstr_index->marker);
10954 opj_free( l_cstr_index);
10958 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10959 memcpy( l_cstr_index->tile_index[it_tile].marker,
10960 p_j2k->cstr_index->tile_index[it_tile].marker,
10961 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10963 opj_free(l_cstr_index->tile_index[it_tile].marker);
10964 l_cstr_index->tile_index[it_tile].marker = NULL;
10967 /* Tile part index*/
10968 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10970 l_cstr_index->tile_index[it_tile].tp_index =
10971 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10973 if(!l_cstr_index->tile_index[it_tile].tp_index){
10974 OPJ_UINT32 it_tile_free;
10976 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10977 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10978 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10981 opj_free( l_cstr_index->tile_index);
10982 opj_free( l_cstr_index->marker);
10983 opj_free( l_cstr_index);
10987 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10988 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10989 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10990 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10993 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10994 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10997 /* Packet index (NOT USED)*/
10998 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10999 l_cstr_index->tile_index[it_tile].packet_index = NULL;
11004 return l_cstr_index;
11007 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
11009 OPJ_UINT32 it_tile=0;
11011 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
11012 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
11013 if (!p_j2k->cstr_index->tile_index)
11016 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
11017 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
11018 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
11019 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
11020 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
11021 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11031 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
11032 opj_stream_private_t *p_stream,
11033 opj_event_mgr_t * p_manager)
11035 opj_bool l_go_on = OPJ_TRUE;
11036 OPJ_UINT32 l_current_tile_no;
11037 OPJ_UINT32 l_data_size,l_max_data_size;
11038 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11039 OPJ_UINT32 l_nb_comps;
11040 OPJ_BYTE * l_current_data;
11042 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11043 if (! l_current_data) {
11046 l_max_data_size = 1000;
11051 if (! j2k_read_tile_header( p_j2k,
11052 &l_current_tile_no,
11054 &l_tile_x0, &l_tile_y0,
11055 &l_tile_x1, &l_tile_y1,
11060 opj_free(l_current_data);
11068 if (l_data_size > l_max_data_size) {
11069 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11070 if (! l_current_data) {
11071 opj_free(l_current_data);
11075 l_max_data_size = l_data_size;
11078 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11079 opj_free(l_current_data);
11082 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);
11084 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11085 opj_free(l_current_data);
11088 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11092 opj_free(l_current_data);
11098 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11100 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11103 assert(p_j2k != 00);
11105 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11106 /* DEVELOPER CORNER, add your custom procedures */
11111 * Read and decode one tile.
11113 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
11114 opj_stream_private_t *p_stream,
11115 opj_event_mgr_t * p_manager)
11117 opj_bool l_go_on = OPJ_TRUE;
11118 OPJ_UINT32 l_current_tile_no;
11119 OPJ_UINT32 l_tile_no_to_dec;
11120 OPJ_UINT32 l_data_size,l_max_data_size;
11121 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11122 OPJ_UINT32 l_nb_comps;
11123 OPJ_BYTE * l_current_data;
11125 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11126 if (! l_current_data) {
11129 l_max_data_size = 1000;
11131 /*Allocate and initialize some elements of codestrem index if not already done*/
11132 if( !p_j2k->cstr_index->tile_index)
11134 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11135 opj_free(l_current_data);
11139 /* Move into the codestream to the first SOT used to decode the desired tile */
11140 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11141 if (p_j2k->cstr_index->tile_index)
11142 if(p_j2k->cstr_index->tile_index->tp_index)
11144 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11145 /* the index for this tile has not been built,
11146 * so move to the last SOT read */
11147 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11148 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11153 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)) {
11154 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11158 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11159 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11160 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11164 if (! j2k_read_tile_header( p_j2k,
11165 &l_current_tile_no,
11167 &l_tile_x0, &l_tile_y0,
11168 &l_tile_x1, &l_tile_y1,
11173 opj_free(l_current_data);
11182 if (l_data_size > l_max_data_size) {
11183 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11184 if (! l_current_data) {
11185 opj_free(l_current_data);
11189 l_max_data_size = l_data_size;
11194 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11195 opj_free(l_current_data);
11198 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);
11200 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11201 opj_free(l_current_data);
11204 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11206 if(l_current_tile_no == l_tile_no_to_dec)
11208 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11209 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11210 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11216 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);
11221 opj_free(l_current_data);
11228 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11230 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11233 assert(p_j2k != 00);
11235 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11236 /* DEVELOPER CORNER, add your custom procedures */
11242 * Decodes the tiles of the stream.
11244 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
11245 opj_stream_private_t * p_stream,
11246 opj_image_t * p_image,
11247 opj_event_mgr_t * p_manager)
11254 p_j2k->m_output_image = opj_image_create0();
11255 if (! (p_j2k->m_output_image)) {
11258 opj_copy_image_header(p_image, p_j2k->m_output_image);
11260 /* customization of the decoding */
11261 j2k_setup_decoding(p_j2k);
11263 /* Decode the codestream */
11264 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11265 opj_image_destroy(p_j2k->m_private_image);
11266 p_j2k->m_private_image = NULL;
11270 /* Move data and copy one information from codec to output image*/
11271 for (compno = 0; compno < p_image->numcomps; compno++) {
11272 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11273 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11274 p_j2k->m_output_image->comps[compno].data = NULL;
11282 * Get the decoded tile.
11284 * @param p_j2k the jpeg2000 codestream codec.
11285 * @param p_stream input_stream
11286 * @param p_image output image. .
11287 * @param p_manager the user event manager
11288 * @param tile_index index of the tile we want decode
11290 * @return true if succeed.
11292 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11293 opj_stream_private_t *p_stream,
11294 opj_image_t* p_image,
11295 struct opj_event_mgr * p_manager,
11296 OPJ_UINT32 tile_index )
11299 OPJ_UINT32 l_tile_x, l_tile_y;
11300 opj_image_comp_t* l_img_comp;
11303 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11307 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11308 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);
11312 /* Compute the dimension of the desired tile*/
11313 l_tile_x = tile_index % p_j2k->m_cp.tw;
11314 l_tile_y = tile_index / p_j2k->m_cp.tw;
11316 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11317 if (p_image->x0 < p_j2k->m_private_image->x0)
11318 p_image->x0 = p_j2k->m_private_image->x0;
11319 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11320 if (p_image->x1 > p_j2k->m_private_image->x1)
11321 p_image->x1 = p_j2k->m_private_image->x1;
11323 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11324 if (p_image->y0 < p_j2k->m_private_image->y0)
11325 p_image->y0 = p_j2k->m_private_image->y0;
11326 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11327 if (p_image->y1 > p_j2k->m_private_image->y1)
11328 p_image->y1 = p_j2k->m_private_image->y1;
11330 l_img_comp = p_image->comps;
11331 for (compno=0; compno < p_image->numcomps; ++compno)
11333 OPJ_INT32 l_comp_x1, l_comp_y1;
11335 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11337 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11338 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11339 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11340 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11342 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11343 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11348 /* Destroy the previous output image*/
11349 if (p_j2k->m_output_image)
11350 opj_image_destroy(p_j2k->m_output_image);
11352 /* Create the ouput image from the information previously computed*/
11353 p_j2k->m_output_image = opj_image_create0();
11354 if (! (p_j2k->m_output_image)) {
11357 opj_copy_image_header(p_image, p_j2k->m_output_image);
11359 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11361 /* customization of the decoding */
11362 j2k_setup_decoding_tile(p_j2k);
11364 /* Decode the codestream */
11365 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11366 opj_image_destroy(p_j2k->m_private_image);
11367 p_j2k->m_private_image = NULL;
11371 /* Move data and copy one information from codec to output image*/
11372 for (compno = 0; compno < p_image->numcomps; compno++) {
11373 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11375 if (p_image->comps[compno].data)
11376 opj_free(p_image->comps[compno].data);
11378 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11380 p_j2k->m_output_image->comps[compno].data = NULL;
11386 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11388 OPJ_UINT32 it_comp;
11390 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11392 if (p_j2k->m_private_image) {
11393 if (p_j2k->m_private_image->comps) {
11394 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11395 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11396 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11397 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11398 if ( res_factor >= max_res){
11399 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11402 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11415 * Encodes all the tiles in a row.
11417 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11418 opj_stream_private_t *p_stream,
11419 opj_event_mgr_t * p_manager )
11422 OPJ_UINT32 l_nb_tiles;
11423 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11424 OPJ_BYTE * l_current_data;
11426 /* preconditions */
11427 assert(p_j2k != 00);
11428 assert(p_stream != 00);
11429 assert(p_manager != 00);
11431 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11432 if (! l_current_data) {
11435 l_max_tile_size = 1000;
11437 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11438 for (i=0;i<l_nb_tiles;++i) {
11439 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11440 opj_free(l_current_data);
11444 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11445 if (l_current_tile_size > l_max_tile_size) {
11446 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11447 if (! l_current_data) {
11450 l_max_tile_size = l_current_tile_size;
11453 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11455 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11460 opj_free(l_current_data);
11465 * Ends the compression procedures and possibility add data to be read after the
11468 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11469 opj_stream_private_t *p_stream,
11470 struct opj_event_mgr * p_manager)
11472 /* customization of the encoding */
11473 j2k_setup_end_compress(p_j2k);
11475 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11485 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11487 * @param p_j2k the jpeg2000 codec.
11488 * @param p_stream the stream object.
11489 * @param p_manager the user event manager.
11491 * @return true if the codec is valid.
11493 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11494 opj_stream_private_t *p_stream,
11495 opj_image_t * p_image,
11496 opj_event_mgr_t * p_manager)
11498 /* preconditions */
11499 assert(p_j2k != 00);
11500 assert(p_stream != 00);
11501 assert(p_manager != 00);
11503 p_j2k->m_private_image = opj_image_create0();
11504 opj_copy_image_header(p_image, p_j2k->m_private_image);
11506 // TODO_MSD: Find a better way
11507 if (p_image->comps) {
11508 OPJ_UINT32 it_comp;
11509 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11510 if (p_image->comps[it_comp].data) {
11511 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11512 p_image->comps[it_comp].data = NULL;
11518 /* customization of the validation */
11519 j2k_setup_encoding_validation (p_j2k);
11521 /* validation of the parameters codec */
11522 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11526 /* customization of the encoding */
11527 j2k_setup_header_writting(p_j2k);
11530 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11540 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11541 OPJ_UINT32 p_tile_index,
11542 opj_stream_private_t *p_stream,
11543 opj_event_mgr_t * p_manager )
11546 if (p_tile_index != p_j2k->m_current_tile_number) {
11547 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11551 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);
11553 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11554 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11555 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11557 /* initialisation before tile encoding */
11558 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11568 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11570 OPJ_UINT32 i,j,k = 0;
11571 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11572 opj_image_comp_t * l_img_comp = 00;
11573 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11574 opj_image_t * l_image = 00;
11575 OPJ_UINT32 l_size_comp, l_remaining;
11576 OPJ_INT32 * l_src_ptr;
11577 l_tilec = p_tcd->tcd_image->tiles->comps;
11578 l_image = p_tcd->image;
11579 l_img_comp = l_image->comps;
11581 for (i=0;i<p_tcd->image->numcomps;++i) {
11582 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11583 l_remaining = l_img_comp->prec & 7; /* (%8) */
11588 if (l_size_comp == 3) {
11592 l_width = (l_tilec->x1 - l_tilec->x0);
11593 l_height = (l_tilec->y1 - l_tilec->y0);
11594 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11595 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11596 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11597 l_stride = l_image_width - l_width;
11598 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11600 switch (l_size_comp) {
11603 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11604 if (l_img_comp->sgnd) {
11605 for (j=0;j<l_height;++j) {
11606 for (k=0;k<l_width;++k) {
11607 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11611 l_src_ptr += l_stride;
11615 for (j=0;j<l_height;++j) {
11616 for (k=0;k<l_width;++k) {
11617 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11621 l_src_ptr += l_stride;
11625 p_data = (OPJ_BYTE*) l_dest_ptr;
11630 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11631 if (l_img_comp->sgnd) {
11632 for (j=0;j<l_height;++j) {
11633 for (k=0;k<l_width;++k) {
11634 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11636 l_src_ptr += l_stride;
11640 for (j=0;j<l_height;++j) {
11641 for (k=0;k<l_width;++k) {
11642 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11644 l_src_ptr += l_stride;
11648 p_data = (OPJ_BYTE*) l_dest_ptr;
11653 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11654 for (j=0;j<l_height;++j) {
11655 for (k=0;k<l_width;++k) {
11656 *(l_dest_ptr++) = *(l_src_ptr++);
11658 l_src_ptr += l_stride;
11661 p_data = (OPJ_BYTE*) l_dest_ptr;
11674 * @param p_j2k the jpeg2000 codec.
11675 * @param p_stream the stream to write data to.
11676 * @param p_manager the user event manager.
11678 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11680 OPJ_UINT32 p_data_size,
11681 opj_stream_private_t *p_stream,
11682 opj_event_mgr_t * p_manager )
11684 opj_tcd_v2_t * l_tcd = 00;
11685 opj_cp_v2_t * l_cp = 00;
11686 opj_tcp_v2_t * l_tcp = 00;
11687 OPJ_UINT32 l_nb_bytes_written;
11688 OPJ_BYTE * l_current_data = 00;
11689 OPJ_UINT32 l_tile_size = 0;
11690 OPJ_UINT32 l_available_data;
11692 /* preconditions */
11693 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11695 l_tcd = p_j2k->m_tcd;
11696 l_cp = &(p_j2k->m_cp);
11697 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11699 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11700 l_available_data = l_tile_size;
11701 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11703 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11704 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11708 l_nb_bytes_written = 0;
11709 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11712 l_current_data += l_nb_bytes_written;
11713 l_available_data -= l_nb_bytes_written;
11715 l_nb_bytes_written = 0;
11716 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11720 l_available_data -= l_nb_bytes_written;
11721 l_nb_bytes_written = l_tile_size - l_available_data;
11723 if ( opj_stream_write_data( p_stream,
11724 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11725 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11729 ++p_j2k->m_current_tile_number;
11736 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11737 * are valid. Developers wanting to extend the library can add their own validation procedures.
11739 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11741 /* preconditions */
11742 assert(p_j2k != 00);
11744 /* DEVELOPER CORNER, insert your custom procedures */
11745 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11747 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11748 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11751 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11752 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11753 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11757 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11758 * are valid. Developers wanting to extend the library can add their own validation procedures.
11760 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11762 /* preconditions */
11763 assert(p_j2k != 00);
11765 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11766 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11768 /* DEVELOPER CORNER, add your custom validation procedure */
11769 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11774 * Sets up the procedures to do on writing header.
11775 * Developers wanting to extend the library can add their own writing procedures.
11777 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11779 /* preconditions */
11780 assert(p_j2k != 00);
11782 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11783 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11784 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11785 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11786 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11789 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11790 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11791 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11793 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11794 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11798 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11800 if (p_j2k->m_cp.comment != 00) {
11801 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11804 /* DEVELOPER CORNER, insert your custom procedures */
11805 if (p_j2k->m_cp.rsiz & MCT) {
11806 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11808 /* End of Developer Corner */
11810 if (p_j2k->cstr_index) {
11811 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11814 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11815 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11819 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11821 OPJ_UINT32 * p_data_written,
11822 OPJ_UINT32 p_total_data_size,
11823 opj_stream_private_t *p_stream,
11824 struct opj_event_mgr * p_manager )
11827 OPJ_UINT32 l_nb_bytes_written = 0;
11828 OPJ_UINT32 l_current_nb_bytes_written;
11829 OPJ_BYTE * l_begin_data = 00;
11831 opj_tcp_v2_t *l_tcp = 00;
11832 opj_tcd_v2_t * l_tcd = 00;
11833 opj_cp_v2_t * l_cp = 00;
11835 l_tcd = p_j2k->m_tcd;
11836 l_cp = &(p_j2k->m_cp);
11837 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11839 l_tcd->cur_pino = 0;
11841 /*Get number of tile parts*/
11842 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11847 l_current_nb_bytes_written = 0;
11848 l_begin_data = p_data;
11849 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11854 l_nb_bytes_written += l_current_nb_bytes_written;
11855 p_data += l_current_nb_bytes_written;
11856 p_total_data_size -= l_current_nb_bytes_written;
11858 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11859 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11860 l_current_nb_bytes_written = 0;
11861 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11862 l_nb_bytes_written += l_current_nb_bytes_written;
11863 p_data += l_current_nb_bytes_written;
11864 p_total_data_size -= l_current_nb_bytes_written;
11866 l_current_nb_bytes_written = 0;
11867 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11868 l_nb_bytes_written += l_current_nb_bytes_written;
11869 p_data += l_current_nb_bytes_written;
11870 p_total_data_size -= l_current_nb_bytes_written;
11873 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11874 l_current_nb_bytes_written = 0;
11875 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11876 l_nb_bytes_written += l_current_nb_bytes_written;
11877 p_data += l_current_nb_bytes_written;
11878 p_total_data_size -= l_current_nb_bytes_written;
11882 l_current_nb_bytes_written = 0;
11883 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11887 l_nb_bytes_written += l_current_nb_bytes_written;
11888 * p_data_written = l_nb_bytes_written;
11890 /* Writing Psot in SOT marker */
11891 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11893 if (l_cp->m_specific_param.m_enc.m_cinema){
11894 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11900 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11902 OPJ_UINT32 * p_data_written,
11903 OPJ_UINT32 p_total_data_size,
11904 opj_stream_private_t *p_stream,
11905 struct opj_event_mgr * p_manager
11908 OPJ_UINT32 tilepartno=0;
11909 OPJ_UINT32 l_nb_bytes_written = 0;
11910 OPJ_UINT32 l_current_nb_bytes_written;
11911 OPJ_UINT32 l_part_tile_size;
11912 OPJ_UINT32 tot_num_tp;
11915 OPJ_BYTE * l_begin_data;
11916 opj_tcp_v2_t *l_tcp = 00;
11917 opj_tcd_v2_t * l_tcd = 00;
11918 opj_cp_v2_t * l_cp = 00;
11921 l_tcd = p_j2k->m_tcd;
11922 l_cp = &(p_j2k->m_cp);
11923 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11925 /*Get number of tile parts*/
11926 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11928 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11929 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11930 l_current_nb_bytes_written = 0;
11931 l_part_tile_size = 0;
11932 l_begin_data = p_data;
11934 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11938 l_nb_bytes_written += l_current_nb_bytes_written;
11939 p_data += l_current_nb_bytes_written;
11940 p_total_data_size -= l_current_nb_bytes_written;
11941 l_part_tile_size += l_nb_bytes_written;
11943 l_current_nb_bytes_written = 0;
11944 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11948 p_data += l_current_nb_bytes_written;
11949 l_nb_bytes_written += l_current_nb_bytes_written;
11950 p_total_data_size -= l_current_nb_bytes_written;
11951 l_part_tile_size += l_nb_bytes_written;
11953 /* Writing Psot in SOT marker */
11954 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11956 if (l_cp->m_specific_param.m_enc.m_cinema) {
11957 j2k_update_tlm(p_j2k,l_part_tile_size);
11960 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11963 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11964 l_tcd->cur_pino = pino;
11966 /*Get number of tile parts*/
11967 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11968 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11969 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11970 l_current_nb_bytes_written = 0;
11971 l_part_tile_size = 0;
11972 l_begin_data = p_data;
11974 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11978 l_nb_bytes_written += l_current_nb_bytes_written;
11979 p_data += l_current_nb_bytes_written;
11980 p_total_data_size -= l_current_nb_bytes_written;
11981 l_part_tile_size += l_current_nb_bytes_written;
11983 l_current_nb_bytes_written = 0;
11985 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11989 l_nb_bytes_written += l_current_nb_bytes_written;
11990 p_data += l_current_nb_bytes_written;
11991 p_total_data_size -= l_current_nb_bytes_written;
11992 l_part_tile_size += l_current_nb_bytes_written;
11994 /* Writing Psot in SOT marker */
11995 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11997 if (l_cp->m_specific_param.m_enc.m_cinema) {
11998 j2k_update_tlm(p_j2k,l_part_tile_size);
12001 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12005 *p_data_written = l_nb_bytes_written;
12011 * Writes the updated tlm.
12013 * @param p_stream the stream to write data to.
12014 * @param p_j2k J2K codec.
12015 * @param p_manager the user event manager.
12017 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
12018 struct opj_stream_private *p_stream,
12019 struct opj_event_mgr * p_manager )
12021 OPJ_UINT32 l_tlm_size;
12022 OPJ_SIZE_T l_tlm_position, l_current_position;
12024 /* preconditions */
12025 assert(p_j2k != 00);
12026 assert(p_manager != 00);
12027 assert(p_stream != 00);
12029 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12030 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12031 l_current_position = opj_stream_tell(p_stream);
12033 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12037 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) {
12041 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12050 * Ends the encoding, i.e. frees memory.
12052 * @param p_stream the stream to write data to.
12053 * @param p_j2k J2K codec.
12054 * @param p_manager the user event manager.
12056 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
12057 struct opj_stream_private *p_stream,
12058 struct opj_event_mgr * p_manager )
12060 /* preconditions */
12061 assert(p_j2k != 00);
12062 assert(p_manager != 00);
12063 assert(p_stream != 00);
12065 tcd_destroy_v2(p_j2k->m_tcd);
12068 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12069 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12070 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12071 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12074 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12075 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12076 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12079 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12085 * Destroys the memory associated with the decoding of headers.
12087 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12088 opj_stream_private_t *p_stream,
12089 opj_event_mgr_t * p_manager )
12091 /* preconditions */
12092 assert(p_j2k != 00);
12093 assert(p_stream != 00);
12094 assert(p_manager != 00);
12096 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12097 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12098 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12101 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12110 * @param p_stream the stream to write data to.
12111 * @param p_j2k J2K codec.
12112 * @param p_manager the user event manager.
12114 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12115 struct opj_stream_private *p_stream,
12116 struct opj_event_mgr * p_manager )
12118 opj_codestream_info_t * l_cstr_info = 00;
12120 /* preconditions */
12121 assert(p_j2k != 00);
12122 assert(p_manager != 00);
12123 assert(p_stream != 00);
12126 /* TODO mergeV2: check this part which use cstr_info */
12127 /*l_cstr_info = p_j2k->cstr_info;
12131 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));
12133 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12134 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12136 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12138 l_cstr_info->tw = p_j2k->m_cp.tw;
12139 l_cstr_info->th = p_j2k->m_cp.th;
12141 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12142 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12143 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12144 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12146 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12148 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12150 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12152 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12153 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12156 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12158 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12160 /*l_cstr_info->maxmarknum = 100;
12161 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12162 l_cstr_info->marknum = 0;
12165 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);
12169 * Creates a tile-coder decoder.
12171 * @param p_stream the stream to write data to.
12172 * @param p_j2k J2K codec.
12173 * @param p_manager the user event manager.
12175 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12176 struct opj_stream_private *p_stream,
12177 struct opj_event_mgr * p_manager )
12179 /* preconditions */
12180 assert(p_j2k != 00);
12181 assert(p_manager != 00);
12182 assert(p_stream != 00);
12184 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12186 if (! p_j2k->m_tcd) {
12187 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12191 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12192 tcd_destroy_v2(p_j2k->m_tcd);
12203 * @param p_j2k the jpeg2000 codec.
12204 * @param p_stream the stream to write data to.
12205 * @param p_manager the user event manager.
12207 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12208 OPJ_UINT32 p_tile_index,
12210 OPJ_UINT32 p_data_size,
12211 opj_stream_private_t *p_stream,
12212 opj_event_mgr_t * p_manager )
12214 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12215 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12219 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12220 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);