2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9 * Copyright (c) 2006-2007, Parvatha Elangovan
10 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Copyright (c) 2011-2012, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
41 /** @name Local static functions */
45 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
47 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 static opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
53 opj_stream_private_t *p_stream,
54 opj_event_mgr_t * p_manager);
57 * The default encoding validation procedure without any extension.
59 * @param p_j2k the jpeg2000 codec to validate.
60 * @param p_stream the input stream to validate.
61 * @param p_manager the user event manager.
63 * @return true if the parameters are correct.
65 static opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
66 opj_stream_private_t *p_stream,
67 opj_event_mgr_t * p_manager );
70 * The default decoding validation procedure without any extension.
72 * @param p_j2k the jpeg2000 codec to validate.
73 * @param p_stream the input stream to validate.
74 * @param p_manager the user event manager.
76 * @return true if the parameters are correct.
78 static opj_bool j2k_decoding_validation ( opj_j2k_v2_t * p_j2k,
79 opj_stream_private_t *p_stream,
80 opj_event_mgr_t * p_manager );
83 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
84 * are valid. Developpers wanting to extend the library can add their own validation procedures.
86 static void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
89 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
90 * are valid. Developpers wanting to extend the library can add their own validation procedures.
92 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
95 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
96 * are valid. Developpers wanting to extend the library can add their own validation procedures.
98 static void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
101 * The mct encoding validation procedure.
103 * @param p_j2k the jpeg2000 codec to validate.
104 * @param p_stream the input stream to validate.
105 * @param p_manager the user event manager.
107 * @return true if the parameters are correct.
109 static opj_bool j2k_mct_validation (opj_j2k_v2_t * p_j2k,
110 opj_stream_private_t *p_stream,
111 opj_event_mgr_t * p_manager );
114 * Builds the tcd decoder to use to decode tile.
116 static opj_bool j2k_build_decoder ( opj_j2k_v2_t * p_j2k,
117 opj_stream_private_t *p_stream,
118 opj_event_mgr_t * p_manager );
120 * Builds the tcd encoder to use to encode tile.
122 static opj_bool j2k_build_encoder ( opj_j2k_v2_t * p_j2k,
123 opj_stream_private_t *p_stream,
124 opj_event_mgr_t * p_manager );
127 * Creates a tile-coder decoder.
129 * @param p_stream the stream to write data to.
130 * @param p_j2k J2K codec.
131 * @param p_manager the user event manager.
133 static opj_bool j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134 struct opj_stream_private *p_stream,
135 struct opj_event_mgr * p_manager );
138 * Excutes the given procedures on the given codec.
140 * @param p_procedure_list the list of procedures to execute
141 * @param p_j2k the jpeg2000 codec to execute the procedures on.
142 * @param p_stream the stream to execute the procedures on.
143 * @param p_manager the user manager.
145 * @return true if all the procedures were successfully executed.
147 static opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
148 opj_procedure_list_t * p_procedure_list,
149 opj_stream_private_t *p_stream,
150 opj_event_mgr_t * p_manager);
153 * Updates the rates of the tcp.
155 * @param p_stream the stream to write data to.
156 * @param p_j2k J2K codec.
157 * @param p_manager the user event manager.
159 static opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
160 opj_stream_private_t *p_stream,
161 opj_event_mgr_t * p_manager );
164 * Copies the decoding tile parameters onto all the tile parameters.
165 * Creates also the tile decoder.
167 static opj_bool j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
168 opj_stream_private_t *p_stream,
169 opj_event_mgr_t * p_manager );
172 * Destroys the memory associated with the decoding of headers.
174 static opj_bool j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
175 opj_stream_private_t *p_stream,
176 opj_event_mgr_t * p_manager );
179 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
180 * with the marker value.
181 * @param p_id Marker value to look up
183 * @return the handler associated with the id.
185 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
188 * Destroys a tile coding parameter structure.
190 * @param p_tcp the tile coding parameter to destroy.
192 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
195 * Destroys the data inside a tile coding parameter structure.
197 * @param p_tcp the tile coding parameter which contain data to destroy.
199 static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
202 * Destroys a coding parameter structure.
204 * @param p_cp the coding parameter to destroy.
206 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
210 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
212 * @param p_comp_no the component number to output.
213 * @param p_stream the stream to write data to.
214 * @param p_j2k J2K codec.
215 * @param p_manager the user event manager.
218 static opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
219 OPJ_UINT32 p_tile_no,
220 OPJ_UINT32 p_comp_no,
222 OPJ_UINT32 * p_header_size,
223 struct opj_event_mgr * p_manager );
226 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
228 * @param p_tile_no the tile index.
229 * @param p_comp_no the component being outputted.
230 * @param p_j2k the J2K codec.
232 * @return the number of bytes taken by the SPCod element.
234 static OPJ_UINT32 j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
235 OPJ_UINT32 p_tile_no,
236 OPJ_UINT32 p_comp_no );
239 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
240 * @param p_header_data the data contained in the COM box.
241 * @param p_j2k the jpeg2000 codec.
242 * @param p_header_size the size of the data contained in the COM marker.
243 * @param p_manager the user event manager.
245 static opj_bool j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
247 OPJ_BYTE * p_header_data,
248 OPJ_UINT32 * p_header_size,
249 opj_event_mgr_t * p_manager );
252 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
254 * @param p_tile_no the tile index.
255 * @param p_comp_no the component being outputted.
256 * @param p_j2k the J2K codec.
258 * @return the number of bytes taken by the SPCod element.
260 static OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
261 OPJ_UINT32 p_tile_no,
262 OPJ_UINT32 p_comp_no );
265 * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
267 * @param p_tile_no the tile to output.
268 * @param p_comp_no the component number to output.
269 * @param p_data the data buffer.
270 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
271 * @param p_j2k J2K codec.
272 * @param p_manager the user event manager.
275 static opj_bool j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
276 OPJ_UINT32 p_tile_no,
277 OPJ_UINT32 p_comp_no,
279 OPJ_UINT32 * p_header_size,
280 opj_event_mgr_t * p_manager);
283 * Updates the Tile Length Marker.
285 static void j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
288 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
290 * @param p_tile_no the tile to output.
291 * @param p_comp_no the component number to output.
292 * @param p_data the data buffer.
293 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
294 * @param p_j2k J2K codec.
295 * @param p_manager the user event manager.
298 static opj_bool j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
300 OPJ_BYTE * p_header_data,
301 OPJ_UINT32 * p_header_size,
302 opj_event_mgr_t * p_manager );
305 * Copies the tile component parameters of all the component from the first tile component.
307 * @param p_j2k the J2k codec.
309 static void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k );
312 * Copies the tile quantization parameters of all the component from the first tile component.
314 * @param p_j2k the J2k codec.
316 static void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
321 static opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
322 opj_stream_private_t *p_stream,
323 opj_event_mgr_t * p_manager);
326 static opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
327 OPJ_UINT32 p_tile_index,
328 opj_stream_private_t *p_stream,
329 opj_event_mgr_t * p_manager );
331 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
333 static void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
335 static opj_bool j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
337 OPJ_UINT32 p_data_size,
338 opj_stream_private_t *p_stream,
339 opj_event_mgr_t * p_manager );
342 * Sets up the procedures to do on writing header.
343 * Developers wanting to extend the library can add their own writing procedures.
345 static void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
347 static opj_bool j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k,
349 OPJ_UINT32 * p_data_written,
350 OPJ_UINT32 p_total_data_size,
351 opj_stream_private_t *p_stream,
352 struct opj_event_mgr * p_manager );
354 static opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
356 OPJ_UINT32 * p_data_written,
357 OPJ_UINT32 p_total_data_size,
358 opj_stream_private_t *p_stream,
359 struct opj_event_mgr * p_manager );
362 * Gets the offset of the header.
364 * @param p_stream the stream to write data to.
365 * @param p_j2k J2K codec.
366 * @param p_manager the user event manager.
368 static opj_bool j2k_get_end_header( opj_j2k_v2_t *p_j2k,
369 struct opj_stream_private *p_stream,
370 struct opj_event_mgr * p_manager );
373 * -----------------------------------------------------------------------
374 * -----------------------------------------------------------------------
375 * -----------------------------------------------------------------------
379 Write the SOC marker (Start Of Codestream)
380 @param j2k J2K handle
382 static void j2k_write_soc(opj_j2k_t *j2k);
385 * Writes the SOC marker (Start Of Codestream)
387 * @param p_stream the stream to write data to.
388 * @param p_j2k J2K codec.
389 * @param p_manager the user event manager.
391 static opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
392 struct opj_stream_private *p_stream,
393 struct opj_event_mgr * p_manager );
396 * Reads a SOC marker (Start of Codestream)
397 * @param p_header_data the data contained in the SOC box.
398 * @param jp2 the jpeg2000 file codec.
399 * @param p_header_size the size of the data contained in the SOC marker.
400 * @param p_manager the user event manager.
402 static opj_bool opj_j2k_read_soc(
404 struct opj_stream_private *p_stream,
405 struct opj_event_mgr * p_manager
409 Write the SIZ marker (image and tile size)
410 @param j2k J2K handle
412 static void j2k_write_siz(opj_j2k_t *j2k);
415 * Writes the SIZ marker (image and tile size)
417 * @param p_stream the stream to write data to.
418 * @param p_j2k J2K codec.
419 * @param p_manager the user event manager.
421 static opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
422 struct opj_stream_private *p_stream,
423 struct opj_event_mgr * p_manager );
426 * Reads a SIZ marker (image and tile size)
427 * @param p_header_data the data contained in the SIZ box.
428 * @param jp2 the jpeg2000 file codec.
429 * @param p_header_size the size of the data contained in the SIZ marker.
430 * @param p_manager the user event manager.
432 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
433 OPJ_BYTE * p_header_data,
434 OPJ_UINT32 p_header_size,
435 opj_event_mgr_t * p_manager);
438 Write the COM marker (comment)
439 @param j2k J2K handle
441 static void j2k_write_com(opj_j2k_t *j2k);
444 * Writes the COM marker (comment)
446 * @param p_stream the stream to write data to.
447 * @param p_j2k J2K codec.
448 * @param p_manager the user event manager.
450 static opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
451 struct opj_stream_private *p_stream,
452 struct opj_event_mgr * p_manager );
455 Read the COM marker (comment)
456 @param j2k J2K handle
458 static void j2k_read_com(opj_j2k_t *j2k);
460 * Reads a COM marker (comments)
461 * @param p_header_data the data contained in the COM box.
462 * @param jp2 the jpeg2000 file codec.
463 * @param p_header_size the size of the data contained in the COM marker.
464 * @param p_manager the user event manager.
466 static opj_bool j2k_read_com_v2 (
468 OPJ_BYTE * p_header_data,
469 OPJ_UINT32 p_header_size,
470 struct opj_event_mgr * p_manager
473 Write the value concerning the specified component in the marker COD and COC
474 @param j2k J2K handle
475 @param compno Number of the component concerned by the information written
477 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
479 Read the value concerning the specified component in the marker COD and COC
480 @param j2k J2K handle
481 @param compno Number of the component concerned by the information read
483 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
485 Write the COD marker (coding style default)
486 @param j2k J2K handle
488 static void j2k_write_cod(opj_j2k_t *j2k);
491 * Writes the COD marker (Coding style default)
493 * @param p_stream the stream to write data to.
494 * @param p_j2k J2K codec.
495 * @param p_manager the user event manager.
497 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
498 struct opj_stream_private *p_stream,
499 struct opj_event_mgr * p_manager );
502 * Reads a COD marker (Coding Styke defaults)
503 * @param p_header_data the data contained in the COD box.
504 * @param p_j2k the jpeg2000 codec.
505 * @param p_header_size the size of the data contained in the COD marker.
506 * @param p_manager the user event manager.
508 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
509 OPJ_BYTE * p_header_data,
510 OPJ_UINT32 p_header_size,
511 opj_event_mgr_t * p_manager);
514 Write the COC marker (coding style component)
515 @param j2k J2K handle
516 @param compno Number of the component concerned by the information written
518 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
521 * Writes the COC marker (Coding style component)
523 * @param p_comp_number the index of the component to output.
524 * @param p_stream the stream to write data to.
525 * @param p_j2k J2K codec.
526 * @param p_manager the user event manager.
528 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
529 OPJ_UINT32 p_comp_number,
530 struct opj_stream_private *p_stream,
531 struct opj_event_mgr * p_manager
534 * Writes the COC marker (Coding style component)
536 * @param p_comp_no the index of the component to output.
537 * @param p_stream the stream to write data to.
538 * @param p_j2k J2K codec.
539 * @param p_manager the user event manager.
541 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
542 OPJ_UINT32 p_comp_no,
544 OPJ_UINT32 * p_data_written,
545 struct opj_event_mgr * p_manager );
548 * Gets the maximum size taken by a coc.
550 * @param p_j2k the jpeg2000 codec to use.
552 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
556 * Reads a COC marker (Coding Style Component)
557 * @param p_header_data the data contained in the COC box.
558 * @param p_j2k the jpeg2000 codec.
559 * @param p_header_size the size of the data contained in the COC marker.
560 * @param p_manager the user event manager.
562 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
563 OPJ_BYTE * p_header_data,
564 OPJ_UINT32 p_header_size,
565 opj_event_mgr_t * p_manager );
568 Write the value concerning the specified component in the marker QCD and QCC
569 @param j2k J2K handle
570 @param compno Number of the component concerned by the information written
572 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
574 Read the value concerning the specified component in the marker QCD and QCC
575 @param j2k J2K handle
576 @param compno Number of the component concern by the information read
577 @param len Length of the information in the QCX part of the marker QCD/QCC
579 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
581 Write the QCD marker (quantization default)
582 @param j2k J2K handle
584 static void j2k_write_qcd(opj_j2k_t *j2k);
587 * Writes the QCD marker (quantization default)
589 * @param p_comp_number the index of the component to output.
590 * @param p_stream the stream to write data to.
591 * @param p_j2k J2K codec.
592 * @param p_manager the user event manager.
594 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
595 struct opj_stream_private *p_stream,
596 struct opj_event_mgr * p_manager );
599 Read the QCD marker (quantization default)
600 @param j2k J2K handle
602 static void j2k_read_qcd(opj_j2k_t *j2k);
605 * Reads a QCD marker (Quantization defaults)
606 * @param p_header_data the data contained in the QCD box.
607 * @param p_j2k the jpeg2000 codec.
608 * @param p_header_size the size of the data contained in the QCD marker.
609 * @param p_manager the user event manager.
611 static opj_bool j2k_read_qcd_v2 (
613 OPJ_BYTE * p_header_data,
614 OPJ_UINT32 p_header_size,
615 struct opj_event_mgr * p_manager
619 Write the QCC marker (quantization component)
620 @param j2k J2K handle
621 @param compno Number of the component concerned by the information written
623 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
626 * Writes the QCC marker (quantization component)
628 * @param p_comp_no the index of the component to output.
629 * @param p_stream the stream to write data to.
630 * @param p_j2k J2K codec.
631 * @param p_manager the user event manager.
633 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
634 OPJ_UINT32 p_comp_no,
635 struct opj_stream_private *p_stream,
636 struct opj_event_mgr * p_manager );
639 * Writes the QCC marker (quantization component)
641 * @param p_comp_no the index of the component to output.
642 * @param p_stream the stream to write data to.
643 * @param p_j2k J2K codec.
644 * @param p_manager the user event manager.
646 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
647 OPJ_UINT32 p_comp_no,
649 OPJ_UINT32 * p_data_written,
650 struct opj_event_mgr * p_manager );
653 * Gets the maximum size taken by a qcc.
655 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
658 Read the QCC marker (quantization component)
659 @param j2k J2K handle
661 static void j2k_read_qcc(opj_j2k_t *j2k);
663 * Reads a QCC marker (Quantization component)
664 * @param p_header_data the data contained in the QCC box.
665 * @param p_j2k the jpeg2000 codec.
666 * @param p_header_size the size of the data contained in the QCC marker.
667 * @param p_manager the user event manager.
669 static opj_bool j2k_read_qcc_v2(
671 OPJ_BYTE * p_header_data,
672 OPJ_UINT32 p_header_size,
673 struct opj_event_mgr * p_manager);
676 Write the POC marker (progression order change)
677 @param j2k J2K handle
679 static void j2k_write_poc(opj_j2k_t *j2k);
682 * Writes the POC marker (Progression Order Change)
684 * @param p_stream the stream to write data to.
685 * @param p_j2k J2K codec.
686 * @param p_manager the user event manager.
688 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
689 struct opj_stream_private *p_stream,
690 struct opj_event_mgr * p_manager );
693 * Writes the POC marker (Progression Order Change)
695 * @param p_stream the stream to write data to.
696 * @param p_j2k J2K codec.
697 * @param p_manager the user event manager.
699 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
701 OPJ_UINT32 * p_data_written,
702 struct opj_event_mgr * p_manager );
705 * Gets the maximum size taken by the writting of a POC.
707 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
710 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
712 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
715 * Gets the maximum size taken by the headers of the SOT.
717 * @param p_j2k the jpeg2000 codec to use.
719 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
722 Read the POC marker (progression order change)
723 @param j2k J2K handle
725 static void j2k_read_poc(opj_j2k_t *j2k);
727 * Reads a POC marker (Progression Order Change)
729 * @param p_header_data the data contained in the POC box.
730 * @param p_j2k the jpeg2000 codec.
731 * @param p_header_size the size of the data contained in the POC marker.
732 * @param p_manager the user event manager.
734 static opj_bool j2k_read_poc_v2 (
736 OPJ_BYTE * p_header_data,
737 OPJ_UINT32 p_header_size,
738 struct opj_event_mgr * p_manager
742 Read the CRG marker (component registration)
743 @param j2k J2K handle
745 static void j2k_read_crg(opj_j2k_t *j2k);
747 * Reads a CRG marker (Component registration)
749 * @param p_header_data the data contained in the TLM box.
750 * @param p_j2k the jpeg2000 codec.
751 * @param p_header_size the size of the data contained in the TLM marker.
752 * @param p_manager the user event manager.
754 static opj_bool j2k_read_crg_v2 (
756 OPJ_BYTE * p_header_data,
757 OPJ_UINT32 p_header_size,
758 struct opj_event_mgr * p_manager
761 Read the TLM marker (tile-part lengths)
762 @param j2k J2K handle
764 static void j2k_read_tlm(opj_j2k_t *j2k);
766 * Reads a TLM marker (Tile Length Marker)
768 * @param p_header_data the data contained in the TLM box.
769 * @param p_j2k the jpeg2000 codec.
770 * @param p_header_size the size of the data contained in the TLM marker.
771 * @param p_manager the user event manager.
773 static opj_bool j2k_read_tlm_v2 (
775 OPJ_BYTE * p_header_data,
776 OPJ_UINT32 p_header_size,
777 struct opj_event_mgr * p_manager
781 * Writes the updated tlm.
783 * @param p_stream the stream to write data to.
784 * @param p_j2k J2K codec.
785 * @param p_manager the user event manager.
787 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
788 struct opj_stream_private *p_stream,
789 struct opj_event_mgr * p_manager );
792 Read the PLM marker (packet length, main header)
793 @param j2k J2K handle
795 static void j2k_read_plm(opj_j2k_t *j2k);
798 * Reads a PLM marker (Packet length, main header marker)
800 * @param p_header_data the data contained in the TLM box.
801 * @param p_j2k the jpeg2000 codec.
802 * @param p_header_size the size of the data contained in the TLM marker.
803 * @param p_manager the user event manager.
805 static opj_bool j2k_read_plm_v2 (
807 OPJ_BYTE * p_header_data,
808 OPJ_UINT32 p_header_size,
809 struct opj_event_mgr * p_manager
812 Read the PLT marker (packet length, tile-part header)
813 @param j2k J2K handle
815 static void j2k_read_plt(opj_j2k_t *j2k);
817 * Reads a PLT marker (Packet length, tile-part header)
819 * @param p_header_data the data contained in the PLT box.
820 * @param p_j2k the jpeg2000 codec.
821 * @param p_header_size the size of the data contained in the PLT marker.
822 * @param p_manager the user event manager.
824 static opj_bool j2k_read_plt_v2 (
826 OPJ_BYTE * p_header_data,
827 OPJ_UINT32 p_header_size,
828 struct opj_event_mgr * p_manager
831 Read the PPM marker (packet packet headers, main header)
832 @param j2k J2K handle
834 static void j2k_read_ppm(opj_j2k_t *j2k);
836 * Reads a PPM marker (Packed packet headers, main header)
838 * @param p_header_data the data contained in the POC box.
839 * @param p_j2k the jpeg2000 codec.
840 * @param p_header_size the size of the data contained in the POC marker.
841 * @param p_manager the user event manager.
844 static opj_bool j2k_read_ppm_v2 (
846 OPJ_BYTE * p_header_data,
847 OPJ_UINT32 p_header_size,
848 struct opj_event_mgr * p_manager
852 static opj_bool j2k_read_ppm_v3 (
854 OPJ_BYTE * p_header_data,
855 OPJ_UINT32 p_header_size,
856 struct opj_event_mgr * p_manager
860 Read the PPT marker (packet packet headers, tile-part header)
861 @param j2k J2K handle
863 static void j2k_read_ppt(opj_j2k_t *j2k);
865 * Reads a PPT marker (Packed packet headers, tile-part header)
867 * @param p_header_data the data contained in the PPT box.
868 * @param p_j2k the jpeg2000 codec.
869 * @param p_header_size the size of the data contained in the PPT marker.
870 * @param p_manager the user event manager.
872 static opj_bool j2k_read_ppt_v2 (
874 OPJ_BYTE * p_header_data,
875 OPJ_UINT32 p_header_size,
876 struct opj_event_mgr * p_manager
879 Write the TLM marker (Mainheader)
880 @param j2k J2K handle
882 static void j2k_write_tlm(opj_j2k_t *j2k);
885 * Writes the TLM marker (Tile Length Marker)
887 * @param p_stream the stream to write data to.
888 * @param p_j2k J2K codec.
889 * @param p_manager the user event manager.
891 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
892 struct opj_stream_private *p_stream,
893 struct opj_event_mgr * p_manager );
896 Write the SOT marker (start of tile-part)
897 @param j2k J2K handle
899 static void j2k_write_sot(opj_j2k_t *j2k);
902 * Writes the SOT marker (Start of tile-part)
904 * @param p_stream the stream to write data to.
905 * @param p_j2k J2K codec.
906 * @param p_manager the user event manager.
908 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
910 OPJ_UINT32 * p_data_written,
911 const struct opj_stream_private *p_stream,
912 struct opj_event_mgr * p_manager );
915 * Reads a PPT marker (Packed packet headers, tile-part header)
917 * @param p_header_data the data contained in the PPT box.
918 * @param p_j2k the jpeg2000 codec.
919 * @param p_header_size the size of the data contained in the PPT marker.
920 * @param p_manager the user event manager.
922 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
923 OPJ_BYTE * p_header_data,
924 OPJ_UINT32 p_header_size,
925 opj_event_mgr_t * p_manager );
928 Write the SOD marker (start of data)
929 @param j2k J2K handle
930 @param tile_coder Pointer to a TCD handle
932 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
935 * Writes the SOD marker (Start of data)
937 * @param p_stream the stream to write data to.
938 * @param p_j2k J2K codec.
939 * @param p_manager the user event manager.
941 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
942 struct opj_tcd_v2 * p_tile_coder,
944 OPJ_UINT32 * p_data_written,
945 OPJ_UINT32 p_total_data_size,
946 const struct opj_stream_private *p_stream,
947 struct opj_event_mgr * p_manager );
950 * Reads a SOD marker (Start Of Data)
952 * @param p_header_data the data contained in the SOD box.
953 * @param p_j2k the jpeg2000 codec.
954 * @param p_header_size the size of the data contained in the SOD marker.
955 * @param p_manager the user event manager.
957 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
958 opj_stream_private_t *p_stream,
959 opj_event_mgr_t * p_manager );
962 * Updates the Tile Length Marker.
964 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
966 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
967 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
969 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
970 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
974 Write the RGN marker (region-of-interest)
975 @param j2k J2K handle
976 @param compno Number of the component concerned by the information written
977 @param tileno Number of the tile concerned by the information written
979 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
982 * Writes the RGN marker (Region Of Interest)
984 * @param p_tile_no the tile to output
985 * @param p_comp_no the component to output
986 * @param p_stream the stream to write data to.
987 * @param p_j2k J2K codec.
988 * @param p_manager the user event manager.
990 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
991 OPJ_UINT32 p_tile_no,
992 OPJ_UINT32 p_comp_no,
993 struct opj_stream_private *p_stream,
994 struct opj_event_mgr * p_manager );
997 * Reads a RGN marker (Region Of Interest)
999 * @param p_header_data the data contained in the POC box.
1000 * @param p_j2k the jpeg2000 codec.
1001 * @param p_header_size the size of the data contained in the POC marker.
1002 * @param p_manager the user event manager.
1004 static opj_bool j2k_read_rgn_v2 (
1005 opj_j2k_v2_t *p_j2k,
1006 OPJ_BYTE * p_header_data,
1007 OPJ_UINT32 p_header_size,
1008 struct opj_event_mgr * p_manager
1012 * Writes the EOC marker (End of Codestream)
1014 * @param p_stream the stream to write data to.
1015 * @param p_j2k J2K codec.
1016 * @param p_manager the user event manager.
1018 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
1019 struct opj_stream_private *p_stream,
1020 struct opj_event_mgr * p_manager );
1023 Write the EOC marker (end of codestream)
1024 @param j2k J2K handle
1026 static void j2k_write_eoc(opj_j2k_t *j2k);
1028 Read the EOC marker (end of codestream)
1029 @param j2k J2K handle
1031 static void j2k_read_eoc(opj_j2k_t *j2k);
1034 * Reads a EOC marker (End Of Codestream)
1036 * @param p_header_data the data contained in the SOD box.
1037 * @param p_j2k the jpeg2000 codec.
1038 * @param p_header_size the size of the data contained in the SOD marker.
1039 * @param p_manager the user event manager.
1042 static opj_bool j2k_read_eoc_v2 (
1043 opj_j2k_v2_t *p_j2k,
1044 struct opj_stream_private *p_stream,
1045 struct opj_event_mgr * p_manager
1052 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1054 * @param p_stream the stream to write data to.
1055 * @param p_j2k J2K codec.
1056 * @param p_manager the user event manager.
1058 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1059 struct opj_stream_private *p_stream,
1060 struct opj_event_mgr * p_manager );
1065 * @param p_stream the stream to write data to.
1066 * @param p_j2k J2K codec.
1067 * @param p_manager the user event manager.
1069 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1070 struct opj_stream_private *p_stream,
1071 struct opj_event_mgr * p_manager );
1074 Read an unknown marker
1075 @param j2k J2K handle
1077 static void j2k_read_unk(opj_j2k_t *j2k);
1079 Add main header marker information
1080 @param cstr_info Codestream information structure
1081 @param type marker type
1082 @param pos byte offset of marker segment
1083 @param len length of marker segment
1085 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1087 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1089 Add tile header marker information
1090 @param tileno tile index number
1091 @param cstr_info Codestream information structure
1092 @param type marker type
1093 @param pos byte offset of marker segment
1094 @param len length of marker segment
1096 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1098 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);
1101 * Reads an unknown marker
1103 * @param p_stream the stream object to read from.
1104 * @param p_j2k the jpeg2000 codec.
1105 * @param p_manager the user event manager.
1107 * @return true if the marker could be deduced.
1109 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1110 struct opj_stream_private *p_stream,
1111 OPJ_UINT32 *output_marker,
1112 struct opj_event_mgr * p_manager );
1115 * Writes the MCT marker (Multiple Component Transform)
1117 * @param p_stream the stream to write data to.
1118 * @param p_j2k J2K codec.
1119 * @param p_manager the user event manager.
1121 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1122 opj_mct_data_t * p_mct_record,
1123 struct opj_stream_private *p_stream,
1124 struct opj_event_mgr * p_manager );
1127 * Reads a MCT marker (Multiple Component Transform)
1129 * @param p_header_data the data contained in the MCT box.
1130 * @param p_j2k the jpeg2000 codec.
1131 * @param p_header_size the size of the data contained in the MCT marker.
1132 * @param p_manager the user event manager.
1134 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1135 OPJ_BYTE * p_header_data,
1136 OPJ_UINT32 p_header_size,
1137 struct opj_event_mgr * p_manager );
1140 * Writes the MCC marker (Multiple Component Collection)
1142 * @param p_stream the stream to write data to.
1143 * @param p_j2k J2K codec.
1144 * @param p_manager the user event manager.
1146 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1147 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1148 struct opj_stream_private *p_stream,
1149 struct opj_event_mgr * p_manager );
1153 * Reads a MCC marker (Multiple Component Collection)
1155 * @param p_header_data the data contained in the MCC box.
1156 * @param p_j2k the jpeg2000 codec.
1157 * @param p_header_size the size of the data contained in the MCC marker.
1158 * @param p_manager the user event manager.
1160 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1161 OPJ_BYTE * p_header_data,
1162 OPJ_UINT32 p_header_size,
1163 struct opj_event_mgr * p_manager );
1166 * Writes the MCO marker (Multiple component transformation ordering)
1168 * @param p_stream the stream to write data to.
1169 * @param p_j2k J2K codec.
1170 * @param p_manager the user event manager.
1172 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1173 struct opj_stream_private *p_stream,
1174 struct opj_event_mgr * p_manager );
1177 * Reads a MCO marker (Multiple Component Transform Ordering)
1179 * @param p_header_data the data contained in the MCO box.
1180 * @param p_j2k the jpeg2000 codec.
1181 * @param p_header_size the size of the data contained in the MCO marker.
1182 * @param p_manager the user event manager.
1184 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1185 OPJ_BYTE * p_header_data,
1186 OPJ_UINT32 p_header_size,
1187 struct opj_event_mgr * p_manager );
1189 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1191 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1192 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1193 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1194 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1196 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1197 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1198 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1199 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1201 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1202 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1203 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1204 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1208 * Ends the encoding, i.e. frees memory.
1210 * @param p_stream the stream to write data to.
1211 * @param p_j2k J2K codec.
1212 * @param p_manager the user event manager.
1214 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1215 struct opj_stream_private *p_stream,
1216 struct opj_event_mgr * p_manager );
1219 * Writes the CBD marker (Component bit depth definition)
1221 * @param p_stream the stream to write data to.
1222 * @param p_j2k J2K codec.
1223 * @param p_manager the user event manager.
1225 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1226 struct opj_stream_private *p_stream,
1227 struct opj_event_mgr * p_manager );
1230 * Reads a CBD marker (Component bit depth definition)
1231 * @param p_header_data the data contained in the CBD box.
1232 * @param p_j2k the jpeg2000 codec.
1233 * @param p_header_size the size of the data contained in the CBD marker.
1234 * @param p_manager the user event manager.
1236 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1237 OPJ_BYTE * p_header_data,
1238 OPJ_UINT32 p_header_size,
1239 struct opj_event_mgr * p_manager);
1242 * Writes the image components.
1244 * @param p_stream the stream to write data to.
1245 * @param p_j2k J2K codec.
1246 * @param p_manager the user event manager.
1248 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1249 struct opj_stream_private *p_stream,
1250 struct opj_event_mgr * p_manager );
1253 * Writes regions of interests.
1255 * @param p_stream the stream to write data to.
1256 * @param p_j2k J2K codec.
1257 * @param p_manager the user event manager.
1259 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1260 struct opj_stream_private *p_stream,
1261 struct opj_event_mgr * p_manager );
1266 * @param p_stream the stream to write data to.
1267 * @param p_j2k J2K codec.
1268 * @param p_manager the user event manager.
1270 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1271 struct opj_stream_private *p_stream,
1272 struct opj_event_mgr * p_manager );
1275 * Checks the progression order changes values. Tells of the poc given as input are valid.
1276 * A nice message is outputted at errors.
1278 * @param p_pocs the progression order changes.
1279 * @param p_nb_pocs the number of progression order changes.
1280 * @param p_nb_resolutions the number of resolutions.
1281 * @param numcomps the number of components
1282 * @param numlayers the number of layers.
1284 * @return true if the pocs are valid.
1286 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1287 OPJ_UINT32 p_nb_pocs,
1288 OPJ_UINT32 p_nb_resolutions,
1289 OPJ_UINT32 numcomps,
1290 OPJ_UINT32 numlayers,
1291 opj_event_mgr_t * p_manager);
1294 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1296 * @param cp the coding parameters.
1297 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1298 * @param tileno the given tile.
1300 * @return the number of tile parts.
1302 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1304 /** mem allocation for TLM marker*/
1305 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1308 * Calculates the total number of tile parts needed by the encoder to
1309 * encode such an image. If not enough memory is available, then the function return false.
1311 * @param p_nb_tiles pointer that will hold the number of tile parts.
1312 * @param cp the coding parameters for the image.
1313 * @param image the image to encode.
1314 * @param p_j2k the p_j2k encoder.
1315 * @param p_manager the user event manager.
1317 * @return true if the function was successful, false else.
1319 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1321 OPJ_UINT32 * p_nb_tiles,
1323 opj_event_mgr_t * p_manager);
1325 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1327 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1329 static opj_codestream_index_t* j2k_create_cstr_index(void);
1335 /* ----------------------------------------------------------------------- */
1336 typedef struct j2k_prog_order{
1337 OPJ_PROG_ORDER enum_prog;
1341 j2k_prog_order_t j2k_prog_order_list[] = {
1347 {(OPJ_PROG_ORDER)-1, ""}
1355 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1363 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1365 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1367 j2k_read_int16_to_float,
1368 j2k_read_int32_to_float,
1369 j2k_read_float32_to_float,
1370 j2k_read_float64_to_float
1373 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1375 j2k_read_int16_to_int32,
1376 j2k_read_int32_to_int32,
1377 j2k_read_float32_to_int32,
1378 j2k_read_float64_to_int32
1381 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1383 j2k_write_float_to_int16,
1384 j2k_write_float_to_int32,
1385 j2k_write_float_to_float,
1386 j2k_write_float_to_float64
1389 typedef struct opj_dec_memory_marker_handler
1393 /** value of the state when the marker can appear */
1395 /** action linked to the marker */
1396 opj_bool (*handler) (
1397 opj_j2k_v2_t *p_j2k,
1398 OPJ_BYTE * p_header_data,
1399 OPJ_UINT32 p_header_size,
1400 struct opj_event_mgr * p_manager
1403 opj_dec_memory_marker_handler_t;
1405 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1408 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1409 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1410 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1411 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1412 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1413 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1414 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1415 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1416 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1417 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1418 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1419 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1420 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1422 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1423 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1424 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1425 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1426 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1427 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1429 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1430 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1431 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1432 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1433 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1434 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1435 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1436 {J2K_MS_SIZ, J2K_STATE_MHSIZ , opj_j2k_read_siz},
1437 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1438 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1439 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1440 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1441 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1443 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1444 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1445 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1446 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1447 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1448 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1450 #ifdef TODO_MS /* FIXME */
1451 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1452 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1453 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1454 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1456 #endif /* USE_JPWL */
1458 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1459 {J2K_MS_INSEC, 0, j2k_read_insec}
1460 #endif /* USE_JPSEC */
1461 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1466 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1468 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1469 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1473 for (i=0;i<p_nb_elem;++i) {
1474 opj_read_bytes(l_src_data,&l_temp,2);
1476 l_src_data+=sizeof(OPJ_INT16);
1478 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1482 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1484 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1485 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1489 for (i=0;i<p_nb_elem;++i) {
1490 opj_read_bytes(l_src_data,&l_temp,4);
1492 l_src_data+=sizeof(OPJ_INT32);
1494 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1498 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1500 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1501 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1505 for (i=0;i<p_nb_elem;++i) {
1506 opj_read_float(l_src_data,&l_temp);
1508 l_src_data+=sizeof(OPJ_FLOAT32);
1510 *(l_dest_data++) = l_temp;
1514 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1516 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1517 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1521 for (i=0;i<p_nb_elem;++i) {
1522 opj_read_double(l_src_data,&l_temp);
1524 l_src_data+=sizeof(OPJ_FLOAT64);
1526 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1530 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1532 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1533 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1537 for (i=0;i<p_nb_elem;++i) {
1538 opj_read_bytes(l_src_data,&l_temp,2);
1540 l_src_data+=sizeof(OPJ_INT16);
1542 *(l_dest_data++) = (OPJ_INT32) l_temp;
1546 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1548 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1549 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1553 for (i=0;i<p_nb_elem;++i) {
1554 opj_read_bytes(l_src_data,&l_temp,4);
1556 l_src_data+=sizeof(OPJ_INT32);
1558 *(l_dest_data++) = (OPJ_INT32) l_temp;
1562 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1564 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1565 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1569 for (i=0;i<p_nb_elem;++i) {
1570 opj_read_float(l_src_data,&l_temp);
1572 l_src_data+=sizeof(OPJ_FLOAT32);
1574 *(l_dest_data++) = (OPJ_INT32) l_temp;
1578 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1580 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1581 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1585 for (i=0;i<p_nb_elem;++i) {
1586 opj_read_double(l_src_data,&l_temp);
1588 l_src_data+=sizeof(OPJ_FLOAT64);
1590 *(l_dest_data++) = (OPJ_INT32) l_temp;
1594 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1596 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1597 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1601 for (i=0;i<p_nb_elem;++i) {
1602 l_temp = (OPJ_UINT32) *(l_src_data++);
1604 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1606 l_dest_data+=sizeof(OPJ_INT16);
1610 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1612 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1613 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1617 for (i=0;i<p_nb_elem;++i) {
1618 l_temp = (OPJ_UINT32) *(l_src_data++);
1620 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1622 l_dest_data+=sizeof(OPJ_INT32);
1626 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1628 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1629 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1633 for (i=0;i<p_nb_elem;++i) {
1634 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1636 opj_write_float(l_dest_data,l_temp);
1638 l_dest_data+=sizeof(OPJ_FLOAT32);
1642 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1644 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1645 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1649 for (i=0;i<p_nb_elem;++i) {
1650 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1652 opj_write_double(l_dest_data,l_temp);
1654 l_dest_data+=sizeof(OPJ_FLOAT64);
1660 * Converts an enum type progression order to string type.
1662 * @param prg_order the progression order to get.
1664 * @return the string representation of the given progression order.
1666 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1667 j2k_prog_order_t *po;
1668 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1669 if(po->enum_prog == prg_order){
1670 return po->str_prog;
1673 return po->str_prog;
1677 * Checks the progression order changes values. Tells if the poc given as input are valid.
1679 * @param p_pocs the progression order changes.
1680 * @param p_nb_pocs the number of progression order changes.
1681 * @param p_nb_resolutions the number of resolutions.
1682 * @param numcomps the number of components
1683 * @param numlayers the number of layers.
1684 * @param p_manager the user event manager.
1686 * @return true if the pocs are valid.
1688 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1689 OPJ_UINT32 p_nb_pocs,
1690 OPJ_UINT32 p_nb_resolutions,
1691 OPJ_UINT32 p_num_comps,
1692 OPJ_UINT32 p_num_layers,
1693 opj_event_mgr_t * p_manager)
1695 OPJ_UINT32* packet_array;
1696 OPJ_UINT32 index , resno, compno, layno;
1698 OPJ_UINT32 step_c = 1;
1699 OPJ_UINT32 step_r = p_num_comps * step_c;
1700 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1701 opj_bool loss = OPJ_FALSE;
1702 OPJ_UINT32 layno0 = 0;
1704 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1705 if (packet_array == 00) {
1706 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1709 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1711 if (p_nb_pocs == 0) {
1715 index = step_r * p_pocs->resno0;
1716 // take each resolution for each poc
1717 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1719 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1721 // take each comp of each resolution for each poc
1722 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1723 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1725 // and finally take each layer of each res of ...
1726 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1727 //index = step_r * resno + step_c * compno + step_l * layno;
1728 packet_array[comp_index] = 1;
1729 comp_index += step_l;
1732 res_index += step_c;
1739 // iterate through all the pocs
1740 for (i = 1; i < p_nb_pocs ; ++i) {
1741 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1743 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1744 index = step_r * p_pocs->resno0;
1746 // take each resolution for each poc
1747 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1748 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1750 // take each comp of each resolution for each poc
1751 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1752 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1754 // and finally take each layer of each res of ...
1755 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1756 //index = step_r * resno + step_c * compno + step_l * layno;
1757 packet_array[comp_index] = 1;
1758 comp_index += step_l;
1761 res_index += step_c;
1771 for (layno = 0; layno < p_num_layers ; ++layno) {
1772 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1773 for (compno = 0; compno < p_num_comps; ++compno) {
1774 loss |= (packet_array[index]!=1);
1775 //index = step_r * resno + step_c * compno + step_l * layno;
1782 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1785 opj_free(packet_array);
1790 /* ----------------------------------------------------------------------- */
1791 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1794 int tpnum=1,tpend=0;
1795 opj_tcp_t *tcp = &cp->tcps[tileno];
1796 prog = j2k_convert_progression_order(tcp->prg);
1801 if( cp->tp_flag == prog[i] ){
1802 tpend=1;cp->tp_pos=i;
1806 tpnum= tpnum * tcp->pocs[pino].compE;
1809 tpnum= tpnum * tcp->pocs[pino].resE;
1812 tpnum= tpnum * tcp->pocs[pino].prcE;
1815 tpnum= tpnum * tcp->pocs[pino].layE;
1827 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1829 * @param cp the coding parameters.
1830 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1831 * @param tileno the given tile.
1833 * @return the number of tile parts.
1835 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1837 const OPJ_CHAR *prog = 00;
1839 OPJ_UINT32 tpnum = 1;
1840 opj_tcp_v2_t *tcp = 00;
1841 opj_poc_t * l_current_poc = 00;
1844 assert(tileno < (cp->tw * cp->th));
1845 assert(pino < (cp->tcps[tileno].numpocs + 1));
1847 /* get the given tile coding parameter */
1848 tcp = &cp->tcps[tileno];
1851 l_current_poc = &(tcp->pocs[pino]);
1852 assert(l_current_poc != 0);
1854 /* get the progression order as a character string */
1855 prog = j2k_convert_progression_order(tcp->prg);
1856 assert(strlen(prog) > 0);
1858 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1862 /* component wise */
1864 tpnum *= l_current_poc->compE;
1866 /* resolution wise */
1868 tpnum *= l_current_poc->resE;
1872 tpnum *= l_current_poc->prcE;
1876 tpnum *= l_current_poc->layE;
1879 /* whould we split here ? */
1880 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1881 cp->m_specific_param.m_enc.m_tp_pos=i;
1893 /** mem allocation for TLM marker*/
1894 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1895 int pino,tileno,totnum_tp=0;
1897 OPJ_ARG_NOT_USED(img_numcomp);
1899 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1900 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1901 int cur_totnum_tp = 0;
1902 opj_tcp_t *tcp = &cp->tcps[tileno];
1903 for(pino = 0; pino <= tcp->numpocs; pino++) {
1905 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1906 if(!pi) { return -1;}
1907 tp_num = j2k_get_num_tp(cp,pino,tileno);
1908 totnum_tp = totnum_tp + tp_num;
1909 cur_totnum_tp = cur_totnum_tp + tp_num;
1910 pi_destroy(pi, cp, tileno);
1912 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1914 if (j2k->cstr_info) {
1915 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1916 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1924 * Calculates the total number of tile parts needed by the encoder to
1925 * encode such an image. If not enough memory is available, then the function return false.
1927 * @param p_nb_tiles pointer that will hold the number of tile parts.
1928 * @param cp the coding parameters for the image.
1929 * @param image the image to encode.
1930 * @param p_j2k the p_j2k encoder.
1931 * @param p_manager the user event manager.
1933 * @return true if the function was successful, false else.
1935 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1937 OPJ_UINT32 * p_nb_tiles,
1939 opj_event_mgr_t * p_manager)
1941 OPJ_UINT32 pino,tileno;
1942 OPJ_UINT32 l_nb_tiles;
1946 assert(p_nb_tiles != 00);
1948 assert(image != 00);
1949 assert(p_j2k != 00);
1950 assert(p_manager != 00);
1952 l_nb_tiles = cp->tw * cp->th;
1957 /* TODO mergeV2: check this part which use cstr_info */
1958 /*if (p_j2k->cstr_info) {
1959 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1961 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1962 OPJ_UINT32 cur_totnum_tp = 0;
1964 pi_update_encoding_parameters(image,cp,tileno);
1966 for (pino = 0; pino <= tcp->numpocs; ++pino)
1968 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1970 *p_nb_tiles = *p_nb_tiles + tp_num;
1972 cur_totnum_tp += tp_num;
1975 tcp->m_nb_tile_parts = cur_totnum_tp;
1977 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1978 if (l_info_tile_ptr->tp == 00) {
1982 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1984 l_info_tile_ptr->num_tps = cur_totnum_tp;
1991 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1992 OPJ_UINT32 cur_totnum_tp = 0;
1994 pi_update_encoding_parameters(image,cp,tileno);
1996 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1997 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1999 *p_nb_tiles = *p_nb_tiles + tp_num;
2001 cur_totnum_tp += tp_num;
2003 tcp->m_nb_tile_parts = cur_totnum_tp;
2012 static void j2k_write_soc(opj_j2k_t *j2k) {
2013 opj_cio_t *cio = j2k->cio;
2014 cio_write(cio, J2K_MS_SOC, 2);
2017 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2022 /* update markers struct */
2023 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2024 #endif /* USE_JPWL */
2029 * Writes the SOC marker (Start Of Codestream)
2031 * @param p_stream the stream to write data to.
2032 * @param p_j2k J2K codec.
2033 * @param p_manager the user event manager.
2035 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2036 struct opj_stream_private *p_stream,
2037 struct opj_event_mgr * p_manager )
2039 /* 2 bytes will be written */
2040 OPJ_BYTE * l_start_stream = 00;
2043 assert(p_stream != 00);
2044 assert(p_j2k != 00);
2045 assert(p_manager != 00);
2047 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2049 /* write SOC identifier */
2050 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2052 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2058 /* update markers struct */
2060 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2062 assert( 0 && "TODO" );
2063 #endif /* USE_JPWL */
2072 * Reads a SOC marker (Start of Codestream)
2073 * @param p_header_data the data contained in the SOC box.
2074 * @param jp2 the jpeg2000 file codec.
2075 * @param p_header_size the size of the data contained in the SOC marker.
2076 * @param p_manager the user event manager.
2078 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2079 struct opj_stream_private *p_stream,
2080 struct opj_event_mgr * p_manager )
2082 OPJ_BYTE l_data [2];
2083 OPJ_UINT32 l_marker;
2086 assert(p_j2k != 00);
2087 assert(p_manager != 00);
2088 assert(p_stream != 00);
2090 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2094 opj_read_bytes(l_data,&l_marker,2);
2095 if (l_marker != J2K_MS_SOC) {
2099 /* Next marker should be a SIZ marker in the main header */
2100 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2102 /* FIXME move it in a index structure included in p_j2k*/
2103 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2105 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2107 /* Add the marker to the codestream index*/
2108 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2113 static void j2k_write_siz(opj_j2k_t *j2k) {
2117 opj_cio_t *cio = j2k->cio;
2118 opj_image_t *image = j2k->image;
2119 opj_cp_t *cp = j2k->cp;
2121 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2122 lenp = cio_tell(cio);
2124 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2125 cio_write(cio, image->x1, 4); /* Xsiz */
2126 cio_write(cio, image->y1, 4); /* Ysiz */
2127 cio_write(cio, image->x0, 4); /* X0siz */
2128 cio_write(cio, image->y0, 4); /* Y0siz */
2129 cio_write(cio, cp->tdx, 4); /* XTsiz */
2130 cio_write(cio, cp->tdy, 4); /* YTsiz */
2131 cio_write(cio, cp->tx0, 4); /* XT0siz */
2132 cio_write(cio, cp->ty0, 4); /* YT0siz */
2133 cio_write(cio, image->numcomps, 2); /* Csiz */
2134 for (i = 0; i < image->numcomps; i++) {
2135 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2136 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2137 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2139 len = cio_tell(cio) - lenp;
2140 cio_seek(cio, lenp);
2141 cio_write(cio, len, 2); /* Lsiz */
2142 cio_seek(cio, lenp + len);
2145 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2149 * Writes the SIZ marker (image and tile size)
2151 * @param p_stream the stream to write data to.
2152 * @param p_j2k J2K codec.
2153 * @param p_manager the user event manager.
2155 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2156 struct opj_stream_private *p_stream,
2157 struct opj_event_mgr * p_manager )
2160 OPJ_UINT32 l_size_len;
2161 OPJ_BYTE * l_current_ptr;
2162 opj_image_t * l_image = 00;
2163 opj_cp_v2_t *cp = 00;
2164 opj_image_comp_t * l_img_comp = 00;
2167 assert(p_stream != 00);
2168 assert(p_j2k != 00);
2169 assert(p_manager != 00);
2171 l_image = p_j2k->m_private_image;
2172 cp = &(p_j2k->m_cp);
2173 l_size_len = 40 + 3 * l_image->numcomps;
2174 l_img_comp = l_image->comps;
2176 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2178 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2179 = (OPJ_BYTE*)opj_realloc(
2180 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2182 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2186 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2189 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2191 /* write SOC identifier */
2192 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2195 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2198 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2201 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2204 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2207 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2210 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2213 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2216 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2219 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2222 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2225 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2228 for (i = 0; i < l_image->numcomps; ++i) {
2229 /* TODO here with MCT ? */
2230 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2233 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2236 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2242 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) {
2252 * Reads a SIZ marker (image and tile size)
2253 * @param p_header_data the data contained in the SIZ box.
2254 * @param jp2 the jpeg2000 file codec.
2255 * @param p_header_size the size of the data contained in the SIZ marker.
2256 * @param p_manager the user event manager.
2258 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2259 OPJ_BYTE * p_header_data,
2260 OPJ_UINT32 p_header_size,
2261 opj_event_mgr_t * p_manager
2264 OPJ_UINT32 l_size, i;
2265 OPJ_UINT32 l_nb_comp;
2266 OPJ_UINT32 l_nb_comp_remain;
2267 OPJ_UINT32 l_remaining_size;
2268 OPJ_UINT32 l_nb_tiles;
2270 opj_image_t *l_image = 00;
2271 opj_cp_v2_t *l_cp = 00;
2272 opj_image_comp_t * l_img_comp = 00;
2273 opj_tcp_v2_t * l_current_tile_param = 00;
2276 assert(p_j2k != 00);
2277 assert(p_manager != 00);
2278 assert(p_header_data != 00);
2280 l_image = p_j2k->m_private_image;
2281 l_cp = &(p_j2k->m_cp);
2283 /* minimum size == 39 - 3 (= minimum component parameter) */
2284 if (p_header_size < 36) {
2285 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2289 l_remaining_size = p_header_size - 36;
2290 l_nb_comp = l_remaining_size / 3;
2291 l_nb_comp_remain = l_remaining_size % 3;
2292 if (l_nb_comp_remain != 0){
2293 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2297 l_size = p_header_size + 2; /* Lsiz */
2299 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2301 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2302 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2304 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2306 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2308 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2310 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2312 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2314 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2316 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2318 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2321 l_image->numcomps = (OPJ_UINT16) l_tmp;
2323 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2327 if (l_image->numcomps != l_nb_comp) {
2328 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);
2333 if (l_cp->correct) {
2334 /* if JPWL is on, we check whether TX errors have damaged
2335 too much the SIZ parameters */
2336 if (!(l_image->x1 * l_image->y1)) {
2337 opj_event_msg_v2(p_manager, EVT_ERROR,
2338 "JPWL: bad image size (%d x %d)\n",
2339 l_image->x1, l_image->y1);
2340 if (!JPWL_ASSUME || JPWL_ASSUME) {
2341 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2346 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2347 if (l_image->numcomps != ((len - 38) / 3)) {
2348 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2349 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2350 l_image->numcomps, ((len - 38) / 3));
2352 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2355 */ /* we try to correct */
2356 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2357 if (l_image->numcomps < ((len - 38) / 3)) {
2358 len = 38 + 3 * l_image->numcomps;
2359 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2362 l_image->numcomps = ((len - 38) / 3);
2363 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2369 /* update components number in the jpwl_exp_comps filed */
2370 l_cp->exp_comps = l_image->numcomps;
2372 #endif /* USE_JPWL */
2374 /* Allocate the resulting image components */
2375 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2376 if (l_image->comps == 00){
2377 l_image->numcomps = 0;
2378 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2382 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2383 l_img_comp = l_image->comps;
2385 /* Read the component information */
2386 for (i = 0; i < l_image->numcomps; ++i){
2388 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2390 l_img_comp->prec = (tmp & 0x7f) + 1;
2391 l_img_comp->sgnd = tmp >> 7;
2392 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2394 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2395 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2397 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2400 if (l_cp->correct) {
2401 /* if JPWL is on, we check whether TX errors have damaged
2402 too much the SIZ parameters, again */
2403 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2404 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2405 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2406 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2408 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2411 /* we try to correct */
2412 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2413 if (!l_image->comps[i].dx) {
2414 l_image->comps[i].dx = 1;
2415 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2416 i, l_image->comps[i].dx);
2418 if (!l_image->comps[i].dy) {
2419 l_image->comps[i].dy = 1;
2420 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2421 i, l_image->comps[i].dy);
2425 #endif /* USE_JPWL */
2426 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2427 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2431 /* Compute the number of tiles */
2432 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2433 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2434 l_nb_tiles = l_cp->tw * l_cp->th;
2436 /* Define the tiles which will be decoded */
2437 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2438 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;
2439 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;
2440 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);
2441 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);
2444 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2445 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2446 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2447 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2451 if (l_cp->correct) {
2452 /* if JPWL is on, we check whether TX errors have damaged
2453 too much the SIZ parameters */
2454 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2455 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2456 "JPWL: bad number of tiles (%d x %d)\n",
2457 l_cp->tw, l_cp->th);
2459 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2462 /* we try to correct */
2463 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2466 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2469 if (l_cp->tw > l_cp->max_tiles) {
2471 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2472 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2473 l_cp->max_tiles, l_cp->tw);
2477 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2480 if (l_cp->th > l_cp->max_tiles) {
2482 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2483 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2484 l_cp->max_tiles, l_cp->th);
2488 #endif /* USE_JPWL */
2490 /* memory allocations */
2491 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2492 if (l_cp->tcps == 00) {
2493 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2496 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2499 if (l_cp->correct) {
2501 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2502 "JPWL: could not alloc tcps field of cp\n");
2503 if (!JPWL_ASSUME || JPWL_ASSUME) {
2504 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2509 #endif /* USE_JPWL */
2511 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2512 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2513 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2514 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2517 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2519 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2520 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2522 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2523 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2526 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));
2527 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2529 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2530 (opj_simple_mcc_decorrelation_data_t*)
2531 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2533 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2534 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2537 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));
2538 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2540 /* set up default dc level shift */
2541 for (i=0;i<l_image->numcomps;++i) {
2542 if (! l_image->comps[i].sgnd) {
2543 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2547 l_current_tile_param = l_cp->tcps;
2548 for (i = 0; i < l_nb_tiles; ++i) {
2549 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2550 if (l_current_tile_param->tccps == 00) {
2551 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2554 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2556 ++l_current_tile_param;
2559 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2560 opj_image_comp_header_update(l_image,l_cp);
2567 static void j2k_write_com(opj_j2k_t *j2k) {
2571 if(j2k->cp->comment) {
2572 opj_cio_t *cio = j2k->cio;
2573 char *comment = j2k->cp->comment;
2575 cio_write(cio, J2K_MS_COM, 2);
2576 lenp = cio_tell(cio);
2578 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2579 for (i = 0; i < strlen(comment); i++) {
2580 cio_write(cio, comment[i], 1);
2582 len = cio_tell(cio) - lenp;
2583 cio_seek(cio, lenp);
2584 cio_write(cio, len, 2);
2585 cio_seek(cio, lenp + len);
2589 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2595 * Writes the COM marker (comment)
2597 * @param p_stream the stream to write data to.
2598 * @param p_j2k J2K codec.
2599 * @param p_manager the user event manager.
2601 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2602 struct opj_stream_private *p_stream,
2603 struct opj_event_mgr * p_manager )
2605 OPJ_UINT32 l_comment_size;
2606 OPJ_UINT32 l_total_com_size;
2607 const OPJ_CHAR *l_comment;
2608 OPJ_BYTE * l_current_ptr = 00;
2611 assert(p_j2k != 00);
2612 assert(p_stream != 00);
2613 assert(p_manager != 00);
2615 l_comment = p_j2k->m_cp.comment;
2616 l_comment_size = strlen(l_comment);
2617 l_total_com_size = l_comment_size + 6;
2619 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2620 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2621 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2624 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2628 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2631 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2633 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2636 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2639 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2642 memcpy( l_current_ptr,l_comment,l_comment_size);
2644 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) {
2651 static void j2k_read_com(opj_j2k_t *j2k) {
2654 opj_cio_t *cio = j2k->cio;
2656 len = cio_read(cio, 2);
2657 cio_skip(cio, len - 2);
2660 * Reads a COM marker (comments)
2661 * @param p_header_data the data contained in the COM box.
2662 * @param jp2 the jpeg2000 file codec.
2663 * @param p_header_size the size of the data contained in the COM marker.
2664 * @param p_manager the user event manager.
2666 opj_bool j2k_read_com_v2 (
2667 opj_j2k_v2_t *p_j2k,
2668 OPJ_BYTE * p_header_data,
2669 OPJ_UINT32 p_header_size,
2670 struct opj_event_mgr * p_manager
2674 assert(p_j2k != 00);
2675 assert(p_manager != 00);
2676 assert(p_header_data != 00);
2677 (void)p_header_size;
2682 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2685 opj_cp_t *cp = j2k->cp;
2686 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2687 opj_tccp_t *tccp = &tcp->tccps[compno];
2688 opj_cio_t *cio = j2k->cio;
2690 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2691 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2692 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2693 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2694 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2696 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2697 for (i = 0; i < tccp->numresolutions; i++) {
2698 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2703 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2706 opj_cp_t *cp = j2k->cp;
2707 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2708 opj_tccp_t *tccp = &tcp->tccps[compno];
2709 opj_cio_t *cio = j2k->cio;
2711 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2713 /* If user wants to remove more resolutions than the codestream contains, return error*/
2714 assert(cp->reduce >= 0);
2715 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2716 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2717 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2718 j2k->state |= J2K_STATE_ERR;
2721 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2722 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2723 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2724 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2725 if (tccp->csty & J2K_CP_CSTY_PRT) {
2726 for (i = 0; i < tccp->numresolutions; i++) {
2727 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2728 tccp->prcw[i] = tmp & 0xf;
2729 tccp->prch[i] = tmp >> 4;
2734 if(j2k->cstr_info && compno == 0) {
2735 for (i = 0; i < tccp->numresolutions; i++) {
2736 if (tccp->csty & J2K_CP_CSTY_PRT) {
2737 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2738 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2741 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2742 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2749 static void j2k_write_cod(opj_j2k_t *j2k) {
2750 opj_cp_t *cp = NULL;
2751 opj_tcp_t *tcp = NULL;
2754 opj_cio_t *cio = j2k->cio;
2756 cio_write(cio, J2K_MS_COD, 2); /* COD */
2758 lenp = cio_tell(cio);
2762 tcp = &cp->tcps[j2k->curtileno];
2764 cio_write(cio, tcp->csty, 1); /* Scod */
2765 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2766 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2767 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2769 j2k_write_cox(j2k, 0);
2770 len = cio_tell(cio) - lenp;
2771 cio_seek(cio, lenp);
2772 cio_write(cio, len, 2); /* Lcod */
2773 cio_seek(cio, lenp + len);
2776 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2781 * Writes the COD marker (Coding style default)
2783 * @param p_stream the stream to write data to.
2784 * @param p_j2k J2K codec.
2785 * @param p_manager the user event manager.
2787 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2788 struct opj_stream_private *p_stream,
2789 struct opj_event_mgr * p_manager )
2791 opj_cp_v2_t *l_cp = 00;
2792 opj_tcp_v2_t *l_tcp = 00;
2793 OPJ_UINT32 l_code_size,l_remaining_size;
2794 OPJ_BYTE * l_current_data = 00;
2797 assert(p_j2k != 00);
2798 assert(p_manager != 00);
2799 assert(p_stream != 00);
2801 l_cp = &(p_j2k->m_cp);
2802 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2803 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2804 l_remaining_size = l_code_size;
2806 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2807 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2808 = (OPJ_BYTE*)opj_realloc(
2809 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2812 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2816 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2819 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2821 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2822 l_current_data += 2;
2824 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2825 l_current_data += 2;
2827 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2830 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2833 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2836 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2839 l_remaining_size -= 9;
2841 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2842 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2846 if (l_remaining_size != 0) {
2847 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2851 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) {
2860 * Reads a COD marker (Coding Styke defaults)
2861 * @param p_header_data the data contained in the COD box.
2862 * @param p_j2k the jpeg2000 codec.
2863 * @param p_header_size the size of the data contained in the COD marker.
2864 * @param p_manager the user event manager.
2866 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2867 OPJ_BYTE * p_header_data,
2868 OPJ_UINT32 p_header_size,
2869 opj_event_mgr_t * p_manager
2875 opj_cp_v2_t *l_cp = 00;
2876 opj_tcp_v2_t *l_tcp = 00;
2877 opj_image_t *l_image = 00;
2880 assert(p_header_data != 00);
2881 assert(p_j2k != 00);
2882 assert(p_manager != 00);
2884 l_image = p_j2k->m_private_image;
2885 l_cp = &(p_j2k->m_cp);
2887 /* If we are in the first tile-part header of the current tile */
2888 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2889 &l_cp->tcps[p_j2k->m_current_tile_number] :
2890 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2892 /* Make sure room is sufficient */
2893 if (p_header_size < 5) {
2894 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2898 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2900 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2902 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2903 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2906 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2907 if (l_cp->m_specific_param.m_dec.m_layer) {
2908 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2911 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2914 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2918 for (i = 0; i < l_image->numcomps; ++i) {
2919 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2922 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2923 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2927 if (p_header_size != 0) {
2928 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2932 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2933 j2k_copy_tile_component_parameters(p_j2k);
2936 #ifdef WIP_REMOVE_MSD
2937 if (p_j2k->cstr_info) {
2938 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2939 p_j2k->cstr_info->prog = l_tcp->prg;
2940 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2941 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2942 for (i = 0; i < l_image->numcomps; ++i) {
2943 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2951 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2954 opj_cp_t *cp = j2k->cp;
2955 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2956 opj_image_t *image = j2k->image;
2957 opj_cio_t *cio = j2k->cio;
2959 cio_write(cio, J2K_MS_COC, 2); /* COC */
2960 lenp = cio_tell(cio);
2962 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2963 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2964 j2k_write_cox(j2k, compno);
2965 len = cio_tell(cio) - lenp;
2966 cio_seek(cio, lenp);
2967 cio_write(cio, len, 2); /* Lcoc */
2968 cio_seek(cio, lenp + len);
2972 * Writes the COC marker (Coding style component)
2974 * @param p_comp_no the index of the component to output.
2975 * @param p_stream the stream to write data to.
2976 * @param p_j2k J2K codec.
2977 * @param p_manager the user event manager.
2979 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2980 OPJ_UINT32 p_comp_no,
2981 struct opj_stream_private *p_stream,
2982 struct opj_event_mgr * p_manager )
2984 OPJ_UINT32 l_coc_size,l_remaining_size;
2985 OPJ_UINT32 l_comp_room;
2988 assert(p_j2k != 00);
2989 assert(p_manager != 00);
2990 assert(p_stream != 00);
2992 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2994 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2996 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2997 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2998 = (OPJ_BYTE*)opj_realloc(
2999 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3001 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3005 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3008 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);
3010 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) {
3018 * Writes the COC marker (Coding style component)
3020 * @param p_comp_no the index of the component to output.
3021 * @param p_stream the stream to write data to.
3022 * @param p_j2k J2K codec.
3023 * @param p_manager the user event manager.
3025 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3026 OPJ_UINT32 p_comp_no,
3028 OPJ_UINT32 * p_data_written,
3029 struct opj_event_mgr * p_manager )
3031 opj_cp_v2_t *l_cp = 00;
3032 opj_tcp_v2_t *l_tcp = 00;
3033 OPJ_UINT32 l_coc_size,l_remaining_size;
3034 OPJ_BYTE * l_current_data = 00;
3035 opj_image_t *l_image = 00;
3036 OPJ_UINT32 l_comp_room;
3039 assert(p_j2k != 00);
3040 assert(p_manager != 00);
3042 l_cp = &(p_j2k->m_cp);
3043 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3044 l_image = p_j2k->m_private_image;
3045 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3047 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3048 l_remaining_size = l_coc_size;
3050 l_current_data = p_data;
3052 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3053 l_current_data += 2;
3055 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3056 l_current_data += 2;
3058 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3059 l_current_data+=l_comp_room;
3061 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3064 l_remaining_size -= (5 + l_comp_room);
3065 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3066 * p_data_written = l_coc_size;
3070 * Gets the maximum size taken by a coc.
3072 * @param p_j2k the jpeg2000 codec to use.
3074 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3077 OPJ_UINT32 l_nb_comp;
3078 OPJ_UINT32 l_nb_tiles;
3079 OPJ_UINT32 l_max = 0;
3083 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3084 l_nb_comp = p_j2k->m_private_image->numcomps;
3086 for (i=0;i<l_nb_tiles;++i) {
3087 for (j=0;j<l_nb_comp;++j) {
3088 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3097 * Reads a COC marker (Coding Style Component)
3098 * @param p_header_data the data contained in the COC box.
3099 * @param p_j2k the jpeg2000 codec.
3100 * @param p_header_size the size of the data contained in the COC marker.
3101 * @param p_manager the user event manager.
3103 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
3104 OPJ_BYTE * p_header_data,
3105 OPJ_UINT32 p_header_size,
3106 opj_event_mgr_t * p_manager
3109 opj_cp_v2_t *l_cp = NULL;
3110 opj_tcp_v2_t *l_tcp = NULL;
3111 opj_image_t *l_image = NULL;
3112 OPJ_UINT32 l_comp_room;
3113 OPJ_UINT32 l_comp_no;
3116 assert(p_header_data != 00);
3117 assert(p_j2k != 00);
3118 assert(p_manager != 00);
3120 l_cp = &(p_j2k->m_cp);
3121 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3122 &l_cp->tcps[p_j2k->m_current_tile_number] :
3123 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3124 l_image = p_j2k->m_private_image;
3126 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3128 /* make sure room is sufficient*/
3129 if (p_header_size < l_comp_room + 1) {
3130 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3133 p_header_size -= l_comp_room + 1;
3135 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3136 p_header_data += l_comp_room;
3137 if (l_comp_no >= l_image->numcomps) {
3138 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3142 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3145 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3146 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3150 if (p_header_size != 0) {
3151 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3157 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3158 int bandno, numbands;
3161 opj_cp_t *cp = j2k->cp;
3162 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3163 opj_tccp_t *tccp = &tcp->tccps[compno];
3164 opj_cio_t *cio = j2k->cio;
3166 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3167 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3169 for (bandno = 0; bandno < numbands; bandno++) {
3170 expn = tccp->stepsizes[bandno].expn;
3171 mant = tccp->stepsizes[bandno].mant;
3173 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3174 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3176 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3181 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3183 int bandno, numbands;
3185 opj_cp_t *cp = j2k->cp;
3186 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3187 opj_tccp_t *tccp = &tcp->tccps[compno];
3188 opj_cio_t *cio = j2k->cio;
3190 tmp = cio_read(cio, 1); /* Sqcx */
3191 tccp->qntsty = tmp & 0x1f;
3192 tccp->numgbits = tmp >> 5;
3193 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3194 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3197 if (j2k->cp->correct) {
3199 /* if JPWL is on, we check whether there are too many subbands */
3200 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3201 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3202 "JPWL: bad number of subbands in Sqcx (%d)\n",
3205 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3208 /* we try to correct */
3210 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3211 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3218 /* We check whether there are too many subbands */
3219 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3220 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3221 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3222 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3225 #endif /* USE_JPWL */
3227 for (bandno = 0; bandno < numbands; bandno++) {
3229 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3230 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3233 tmp = cio_read(cio, 2); /* SPqcx_i */
3237 if (bandno < J2K_MAXBANDS){
3238 tccp->stepsizes[bandno].expn = expn;
3239 tccp->stepsizes[bandno].mant = mant;
3243 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3244 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3245 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3246 tccp->stepsizes[bandno].expn =
3247 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3248 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3249 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3255 static void j2k_write_qcd(opj_j2k_t *j2k) {
3258 opj_cio_t *cio = j2k->cio;
3260 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3261 lenp = cio_tell(cio);
3263 j2k_write_qcx(j2k, 0);
3264 len = cio_tell(cio) - lenp;
3265 cio_seek(cio, lenp);
3266 cio_write(cio, len, 2); /* Lqcd */
3267 cio_seek(cio, lenp + len);
3270 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3274 * Writes the QCD marker (quantization default)
3276 * @param p_comp_number the index of the component to output.
3277 * @param p_stream the stream to write data to.
3278 * @param p_j2k J2K codec.
3279 * @param p_manager the user event manager.
3281 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3282 struct opj_stream_private *p_stream,
3283 struct opj_event_mgr * p_manager )
3285 opj_cp_v2_t *l_cp = 00;
3286 opj_tcp_v2_t *l_tcp = 00;
3287 OPJ_UINT32 l_qcd_size,l_remaining_size;
3288 OPJ_BYTE * l_current_data = 00;
3291 assert(p_j2k != 00);
3292 assert(p_manager != 00);
3293 assert(p_stream != 00);
3295 l_cp = &(p_j2k->m_cp);
3296 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3297 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3298 l_remaining_size = l_qcd_size;
3300 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3301 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3302 = (OPJ_BYTE*)opj_realloc(
3303 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3306 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3310 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3313 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3315 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3316 l_current_data += 2;
3318 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3319 l_current_data += 2;
3321 l_remaining_size -= 4;
3323 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3324 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3328 if (l_remaining_size != 0) {
3329 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3333 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) {
3341 static void j2k_read_qcd(opj_j2k_t *j2k) {
3345 opj_cio_t *cio = j2k->cio;
3346 opj_image_t *image = j2k->image;
3348 len = cio_read(cio, 2); /* Lqcd */
3349 pos = cio_tell(cio);
3350 for (i = 0; i < image->numcomps; i++) {
3352 j2k_read_qcx(j2k, i, len - 2);
3357 * Reads a QCD marker (Quantization defaults)
3358 * @param p_header_data the data contained in the QCD box.
3359 * @param p_j2k the jpeg2000 codec.
3360 * @param p_header_size the size of the data contained in the QCD marker.
3361 * @param p_manager the user event manager.
3363 opj_bool j2k_read_qcd_v2 (
3364 opj_j2k_v2_t *p_j2k,
3365 OPJ_BYTE * p_header_data,
3366 OPJ_UINT32 p_header_size,
3367 struct opj_event_mgr * p_manager
3371 assert(p_header_data != 00);
3372 assert(p_j2k != 00);
3373 assert(p_manager != 00);
3375 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3376 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3380 if (p_header_size != 0) {
3381 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3385 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3386 j2k_copy_tile_quantization_parameters(p_j2k);
3391 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3394 opj_cio_t *cio = j2k->cio;
3396 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3397 lenp = cio_tell(cio);
3399 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3400 j2k_write_qcx(j2k, compno);
3401 len = cio_tell(cio) - lenp;
3402 cio_seek(cio, lenp);
3403 cio_write(cio, len, 2); /* Lqcc */
3404 cio_seek(cio, lenp + len);
3408 * Writes the QCC marker (quantization component)
3410 * @param p_comp_no the index of the component to output.
3411 * @param p_stream the stream to write data to.
3412 * @param p_j2k J2K codec.
3413 * @param p_manager the user event manager.
3415 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3416 OPJ_UINT32 p_comp_no,
3417 struct opj_stream_private *p_stream,
3418 struct opj_event_mgr * p_manager )
3420 OPJ_UINT32 l_qcc_size,l_remaining_size;
3423 assert(p_j2k != 00);
3424 assert(p_manager != 00);
3425 assert(p_stream != 00);
3427 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3428 l_remaining_size = l_qcc_size;
3430 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3431 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3432 = (OPJ_BYTE*)opj_realloc(
3433 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3435 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3439 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3442 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);
3444 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) {
3452 * Writes the QCC marker (quantization component)
3454 * @param p_comp_no the index of the component to output.
3455 * @param p_stream the stream to write data to.
3456 * @param p_j2k J2K codec.
3457 * @param p_manager the user event manager.
3459 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3460 OPJ_UINT32 p_comp_no,
3462 OPJ_UINT32 * p_data_written,
3463 struct opj_event_mgr * p_manager )
3465 OPJ_UINT32 l_qcc_size,l_remaining_size;
3466 OPJ_BYTE * l_current_data = 00;
3469 assert(p_j2k != 00);
3470 assert(p_manager != 00);
3472 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3473 l_remaining_size = l_qcc_size;
3475 l_current_data = p_data;
3477 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3478 l_current_data += 2;
3480 if (p_j2k->m_private_image->numcomps <= 256) {
3483 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3484 l_current_data += 2;
3486 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3489 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3490 l_remaining_size -= 6;
3493 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3494 l_current_data += 2;
3496 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3499 l_remaining_size -= 6;
3502 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3504 *p_data_written = l_qcc_size;
3508 * Gets the maximum size taken by a qcc.
3510 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3512 return j2k_get_max_coc_size(p_j2k);
3515 static void j2k_read_qcc(opj_j2k_t *j2k) {
3517 int numcomp = j2k->image->numcomps;
3518 opj_cio_t *cio = j2k->cio;
3520 len = cio_read(cio, 2); /* Lqcc */
3521 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3524 if (j2k->cp->correct) {
3526 static int backup_compno = 0;
3528 /* compno is negative or larger than the number of components!!! */
3529 if ((compno < 0) || (compno >= numcomp)) {
3530 opj_event_msg(j2k->cinfo, EVT_ERROR,
3531 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3534 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3537 /* we try to correct */
3538 compno = backup_compno % numcomp;
3539 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3540 "- setting component number to %d\n",
3544 /* keep your private count of tiles */
3547 #endif /* USE_JPWL */
3549 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3553 * Reads a QCC marker (Quantization component)
3554 * @param p_header_data the data contained in the QCC box.
3555 * @param p_j2k the jpeg2000 codec.
3556 * @param p_header_size the size of the data contained in the QCC marker.
3557 * @param p_manager the user event manager.
3559 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3560 OPJ_BYTE * p_header_data,
3561 OPJ_UINT32 p_header_size,
3562 struct opj_event_mgr * p_manager)
3564 OPJ_UINT32 l_num_comp,l_comp_no;
3567 assert(p_header_data != 00);
3568 assert(p_j2k != 00);
3569 assert(p_manager != 00);
3571 l_num_comp = p_j2k->m_private_image->numcomps;
3573 if (l_num_comp <= 256) {
3574 if (p_header_size < 1) {
3575 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3578 opj_read_bytes(p_header_data,&l_comp_no,1);
3583 if (p_header_size < 2) {
3584 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3587 opj_read_bytes(p_header_data,&l_comp_no,2);
3593 if (p_j2k->m_cp.correct) {
3595 static OPJ_UINT32 backup_compno = 0;
3597 /* compno is negative or larger than the number of components!!! */
3598 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3599 opj_event_msg_v2(p_manager, EVT_ERROR,
3600 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3601 l_comp_no, l_num_comp);
3603 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3606 /* we try to correct */
3607 l_comp_no = backup_compno % l_num_comp;
3608 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3609 "- setting component number to %d\n",
3613 /* keep your private count of tiles */
3616 #endif /* USE_JPWL */
3618 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3619 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3623 if (p_header_size != 0) {
3624 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3632 static void j2k_write_poc(opj_j2k_t *j2k) {
3633 int len, numpchgs, i;
3635 int numcomps = j2k->image->numcomps;
3637 opj_cp_t *cp = j2k->cp;
3638 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3639 opj_tccp_t *tccp = &tcp->tccps[0];
3640 opj_cio_t *cio = j2k->cio;
3642 numpchgs = 1 + tcp->numpocs;
3643 cio_write(cio, J2K_MS_POC, 2); /* POC */
3644 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3645 cio_write(cio, len, 2); /* Lpoc */
3646 for (i = 0; i < numpchgs; i++) {
3647 opj_poc_t *poc = &tcp->pocs[i];
3648 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3649 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3650 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3651 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3652 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3653 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3654 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3655 poc->compno1 = int_min(poc->compno1, numcomps);
3656 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3661 * Writes the POC marker (Progression Order Change)
3663 * @param p_stream the stream to write data to.
3664 * @param p_j2k J2K codec.
3665 * @param p_manager the user event manager.
3667 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3668 struct opj_stream_private *p_stream,
3669 struct opj_event_mgr * p_manager )
3671 OPJ_UINT32 l_nb_comp;
3672 OPJ_UINT32 l_nb_poc;
3673 OPJ_UINT32 l_poc_size;
3674 OPJ_UINT32 l_written_size = 0;
3675 opj_tcp_v2_t *l_tcp = 00;
3676 opj_tccp_t *l_tccp = 00;
3677 OPJ_UINT32 l_poc_room;
3680 assert(p_j2k != 00);
3681 assert(p_manager != 00);
3682 assert(p_stream != 00);
3684 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3685 l_tccp = &l_tcp->tccps[0];
3686 l_nb_comp = p_j2k->m_private_image->numcomps;
3687 l_nb_poc = 1 + l_tcp->numpocs;
3689 if (l_nb_comp <= 256) {
3695 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3697 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3698 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3699 = (OPJ_BYTE*)opj_realloc(
3700 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3703 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3707 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3710 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3712 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) {
3721 * Writes the POC marker (Progression Order Change)
3723 * @param p_stream the stream to write data to.
3724 * @param p_j2k J2K codec.
3725 * @param p_manager the user event manager.
3727 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3729 OPJ_UINT32 * p_data_written,
3730 struct opj_event_mgr * p_manager )
3733 OPJ_BYTE * l_current_data = 00;
3734 OPJ_UINT32 l_nb_comp;
3735 OPJ_UINT32 l_nb_poc;
3736 OPJ_UINT32 l_poc_size;
3737 opj_image_t *l_image = 00;
3738 opj_tcp_v2_t *l_tcp = 00;
3739 opj_tccp_t *l_tccp = 00;
3740 opj_poc_t *l_current_poc = 00;
3741 OPJ_UINT32 l_poc_room;
3744 assert(p_j2k != 00);
3745 assert(p_manager != 00);
3747 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3748 l_tccp = &l_tcp->tccps[0];
3749 l_image = p_j2k->m_private_image;
3750 l_nb_comp = l_image->numcomps;
3751 l_nb_poc = 1 + l_tcp->numpocs;
3753 if (l_nb_comp <= 256) {
3760 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3762 l_current_data = p_data;
3764 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3765 l_current_data += 2;
3767 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3768 l_current_data += 2;
3770 l_current_poc = l_tcp->pocs;
3771 for (i = 0; i < l_nb_poc; ++i) {
3772 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3775 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3776 l_current_data+=l_poc_room;
3778 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3781 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3784 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3785 l_current_data+=l_poc_room;
3787 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3790 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3791 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3792 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3793 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3798 *p_data_written = l_poc_size;
3802 * Gets the maximum size taken by the writing of a POC.
3804 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3806 opj_tcp_v2_t * l_tcp = 00;
3807 OPJ_UINT32 l_nb_tiles = 0;
3808 OPJ_UINT32 l_max_poc = 0;
3811 l_tcp = p_j2k->m_cp.tcps;
3812 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3814 for (i=0;i<l_nb_tiles;++i) {
3815 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3821 return 4 + 9 * l_max_poc;
3825 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3827 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3830 OPJ_UINT32 l_nb_tiles;
3831 OPJ_UINT32 l_max = 0;
3832 opj_tcp_v2_t * l_tcp = 00;
3834 l_tcp = p_j2k->m_cp.tcps;
3835 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3837 for (i=0;i<l_nb_tiles;++i) {
3838 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3848 * Gets the maximum size taken by the headers of the SOT.
3850 * @param p_j2k the jpeg2000 codec to use.
3852 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3854 OPJ_UINT32 l_nb_bytes = 0;
3855 OPJ_UINT32 l_nb_comps;
3856 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3858 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3859 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3861 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3862 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3863 l_nb_bytes += l_nb_comps * l_coc_bytes;
3865 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3866 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3869 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3871 /*** DEVELOPER CORNER, Add room for your headers ***/
3876 static void j2k_read_poc(opj_j2k_t *j2k) {
3877 int len, numpchgs, i, old_poc;
3879 int numcomps = j2k->image->numcomps;
3881 opj_cp_t *cp = j2k->cp;
3882 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3883 opj_cio_t *cio = j2k->cio;
3885 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
3887 len = cio_read(cio, 2); /* Lpoc */
3888 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
3890 for (i = old_poc; i < numpchgs + old_poc; i++) {
3892 poc = &tcp->pocs[i];
3893 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
3894 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
3895 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
3896 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
3897 poc->compno1 = int_min(
3898 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
3899 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
3902 tcp->numpocs = numpchgs + old_poc - 1;
3906 * Reads a POC marker (Progression Order Change)
3908 * @param p_header_data the data contained in the POC box.
3909 * @param p_j2k the jpeg2000 codec.
3910 * @param p_header_size the size of the data contained in the POC marker.
3911 * @param p_manager the user event manager.
3913 opj_bool j2k_read_poc_v2 (
3914 opj_j2k_v2_t *p_j2k,
3915 OPJ_BYTE * p_header_data,
3916 OPJ_UINT32 p_header_size,
3917 struct opj_event_mgr * p_manager)
3919 OPJ_UINT32 i, l_nb_comp, l_tmp;
3920 opj_image_t * l_image = 00;
3921 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3922 OPJ_UINT32 l_chunk_size, l_comp_room;
3924 opj_cp_v2_t *l_cp = 00;
3925 opj_tcp_v2_t *l_tcp = 00;
3926 opj_poc_t *l_current_poc = 00;
3929 assert(p_header_data != 00);
3930 assert(p_j2k != 00);
3931 assert(p_manager != 00);
3933 l_image = p_j2k->m_private_image;
3934 l_nb_comp = l_image->numcomps;
3935 if (l_nb_comp <= 256) {
3941 l_chunk_size = 5 + 2 * l_comp_room;
3942 l_current_poc_nb = p_header_size / l_chunk_size;
3943 l_current_poc_remaining = p_header_size % l_chunk_size;
3945 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3946 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3950 l_cp = &(p_j2k->m_cp);
3951 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3952 &l_cp->tcps[p_j2k->m_current_tile_number] :
3953 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3954 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3955 l_current_poc_nb += l_old_poc_nb;
3957 assert(l_current_poc_nb < 32);
3959 /* now poc is in use.*/
3962 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3963 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3964 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3966 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3967 p_header_data+=l_comp_room;
3968 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3970 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3972 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3973 p_header_data+=l_comp_room;
3974 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3976 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3977 /* make sure comp is in acceptable bounds */
3978 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3982 l_tcp->numpocs = l_current_poc_nb - 1;
3986 static void j2k_read_crg(opj_j2k_t *j2k) {
3987 int len, i, Xcrg_i, Ycrg_i;
3989 opj_cio_t *cio = j2k->cio;
3990 int numcomps = j2k->image->numcomps;
3992 len = cio_read(cio, 2); /* Lcrg */
3993 for (i = 0; i < numcomps; i++) {
3994 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
3995 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4000 * Reads a CRG marker (Component registration)
4002 * @param p_header_data the data contained in the TLM box.
4003 * @param p_j2k the jpeg2000 codec.
4004 * @param p_header_size the size of the data contained in the TLM marker.
4005 * @param p_manager the user event manager.
4007 opj_bool j2k_read_crg_v2 (
4008 opj_j2k_v2_t *p_j2k,
4009 OPJ_BYTE * p_header_data,
4010 OPJ_UINT32 p_header_size,
4011 struct opj_event_mgr * p_manager
4014 OPJ_UINT32 l_nb_comp;
4016 assert(p_header_data != 00);
4017 assert(p_j2k != 00);
4018 assert(p_manager != 00);
4020 l_nb_comp = p_j2k->m_private_image->numcomps;
4022 if (p_header_size != l_nb_comp *4) {
4023 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4026 /* Do not care of this at the moment since only local variables are set here */
4029 (i = 0; i < l_nb_comp; ++i)
4031 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4033 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4040 static void j2k_read_tlm(opj_j2k_t *j2k) {
4041 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4042 long int Ttlm_i, Ptlm_i;
4044 opj_cio_t *cio = j2k->cio;
4046 len = cio_read(cio, 2); /* Ltlm */
4047 Ztlm = cio_read(cio, 1); /* Ztlm */
4048 Stlm = cio_read(cio, 1); /* Stlm */
4049 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4050 SP = (Stlm >> 6) & 0x01;
4051 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4052 for (i = 0; i < tile_tlm; i++) {
4053 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4054 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4059 * Reads a TLM marker (Tile Length Marker)
4061 * @param p_header_data the data contained in the TLM box.
4062 * @param p_j2k the jpeg2000 codec.
4063 * @param p_header_size the size of the data contained in the TLM marker.
4064 * @param p_manager the user event manager.
4066 opj_bool j2k_read_tlm_v2 (
4067 opj_j2k_v2_t *p_j2k,
4068 OPJ_BYTE * p_header_data,
4069 OPJ_UINT32 p_header_size,
4070 struct opj_event_mgr * p_manager
4073 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4075 assert(p_header_data != 00);
4076 assert(p_j2k != 00);
4077 assert(p_manager != 00);
4079 if (p_header_size < 2) {
4080 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4085 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4087 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4090 l_ST = ((l_Stlm >> 4) & 0x3);
4091 l_SP = (l_Stlm >> 6) & 0x1;
4093 l_Ptlm_size = (l_SP + 1) * 2;
4094 l_quotient = l_Ptlm_size + l_ST;
4096 l_tot_num_tp = p_header_size / l_quotient;
4097 l_tot_num_tp_remaining = p_header_size % l_quotient;
4099 if (l_tot_num_tp_remaining != 0) {
4100 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4103 /* FIXME Do not care of this at the moment since only local variables are set here */
4106 (i = 0; i < l_tot_num_tp; ++i)
4108 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4109 p_header_data += l_ST;
4110 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4111 p_header_data += l_Ptlm_size;
4116 static void j2k_read_plm(opj_j2k_t *j2k) {
4117 int len, i, Zplm, Nplm, add, packet_len = 0;
4119 opj_cio_t *cio = j2k->cio;
4121 len = cio_read(cio, 2); /* Lplm */
4122 Zplm = cio_read(cio, 1); /* Zplm */
4125 Nplm = cio_read(cio, 4); /* Nplm */
4127 for (i = Nplm; i > 0; i--) {
4128 add = cio_read(cio, 1);
4130 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4131 if ((add & 0x80) == 0) {
4142 * Reads a PLM marker (Packet length, main header marker)
4144 * @param p_header_data the data contained in the TLM box.
4145 * @param p_j2k the jpeg2000 codec.
4146 * @param p_header_size the size of the data contained in the TLM marker.
4147 * @param p_manager the user event manager.
4149 opj_bool j2k_read_plm_v2 (
4150 opj_j2k_v2_t *p_j2k,
4151 OPJ_BYTE * p_header_data,
4152 OPJ_UINT32 p_header_size,
4153 struct opj_event_mgr * p_manager
4157 assert(p_header_data != 00);
4158 assert(p_j2k != 00);
4159 assert(p_manager != 00);
4161 if (p_header_size < 1) {
4162 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4165 /* Do not care of this at the moment since only local variables are set here */
4167 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4174 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4176 p_header_size -= (1+l_Nplm);
4180 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4184 (i = 0; i < l_Nplm; ++i)
4186 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4188 // take only the last seven bytes
4189 l_packet_len |= (l_tmp & 0x7f);
4197 // store packet length and proceed to next packet
4204 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4212 static void j2k_read_plt(opj_j2k_t *j2k) {
4213 int len, i, Zplt, packet_len = 0, add;
4215 opj_cio_t *cio = j2k->cio;
4217 len = cio_read(cio, 2); /* Lplt */
4218 Zplt = cio_read(cio, 1); /* Zplt */
4219 for (i = len - 3; i > 0; i--) {
4220 add = cio_read(cio, 1);
4221 packet_len = (packet_len << 7) + add; /* Iplt_i */
4222 if ((add & 0x80) == 0) {
4230 * Reads a PLT marker (Packet length, tile-part header)
4232 * @param p_header_data the data contained in the PLT box.
4233 * @param p_j2k the jpeg2000 codec.
4234 * @param p_header_size the size of the data contained in the PLT marker.
4235 * @param p_manager the user event manager.
4237 opj_bool j2k_read_plt_v2 (
4238 opj_j2k_v2_t *p_j2k,
4239 OPJ_BYTE * p_header_data,
4240 OPJ_UINT32 p_header_size,
4241 struct opj_event_mgr * p_manager
4244 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4247 assert(p_header_data != 00);
4248 assert(p_j2k != 00);
4249 assert(p_manager != 00);
4251 if (p_header_size < 1) {
4252 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4256 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4260 for (i = 0; i < p_header_size; ++i) {
4261 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4263 /* take only the last seven bytes */
4264 l_packet_len |= (l_tmp & 0x7f);
4269 /* store packet length and proceed to next packet */
4274 if (l_packet_len != 0) {
4275 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4282 static void j2k_read_ppm(opj_j2k_t *j2k) {
4283 int len, Z_ppm, i, j;
4286 opj_cp_t *cp = j2k->cp;
4287 opj_cio_t *cio = j2k->cio;
4289 len = cio_read(cio, 2);
4292 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4295 if (cp->ppm_previous == 0) {
4296 N_ppm = cio_read(cio, 4); /* N_ppm */
4299 N_ppm = cp->ppm_previous;
4302 if (Z_ppm == 0) { /* First PPM marker */
4303 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4304 cp->ppm_data_first = cp->ppm_data;
4305 cp->ppm_len = N_ppm;
4306 } else { /* NON-first PPM marker */
4307 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4310 /* this memory allocation check could be done even in non-JPWL cases */
4312 if (!cp->ppm_data) {
4313 opj_event_msg(j2k->cinfo, EVT_ERROR,
4314 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4316 if (!JPWL_ASSUME || JPWL_ASSUME) {
4317 opj_free(cp->ppm_data);
4318 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4325 cp->ppm_data_first = cp->ppm_data;
4326 cp->ppm_len = N_ppm + cp->ppm_store;
4328 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4329 cp->ppm_data[j] = cio_read(cio, 1);
4333 break; /* Case of non-finished packet header in present marker but finished in next one */
4335 cp->ppm_previous = i - 1;
4340 * Reads a PPM marker (Packed packet headers, main header)
4342 * @param p_header_data the data contained in the POC box.
4343 * @param p_j2k the jpeg2000 codec.
4344 * @param p_header_size the size of the data contained in the POC marker.
4345 * @param p_manager the user event manager.
4348 opj_bool j2k_read_ppm_v2 (
4349 opj_j2k_v2_t *p_j2k,
4350 OPJ_BYTE * p_header_data,
4351 OPJ_UINT32 p_header_size,
4352 struct opj_event_mgr * p_manager
4356 opj_cp_v2_t *l_cp = 00;
4357 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4360 assert(p_header_data != 00);
4361 assert(p_j2k != 00);
4362 assert(p_manager != 00);
4364 if (p_header_size < 1) {
4365 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4369 l_cp = &(p_j2k->m_cp);
4372 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4376 /* First PPM marker */
4378 if (p_header_size < 4) {
4379 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4383 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4387 /* First PPM marker: Initialization */
4388 l_cp->ppm_len = l_N_ppm;
4389 l_cp->ppm_data_size = 0;
4391 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4392 if (l_cp->ppm_buffer == 00) {
4393 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4396 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4398 l_cp->ppm_data = l_cp->ppm_buffer;
4402 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4403 if (p_header_size >= 4) {
4405 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4408 l_cp->ppm_len += l_N_ppm ;
4410 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4411 if (l_cp->ppm_buffer == 00) {
4412 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4415 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4417 l_cp->ppm_data = l_cp->ppm_buffer;
4424 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4426 if (l_remaining_data <= p_header_size) {
4427 /* we must store less information than available in the packet */
4428 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4429 l_cp->ppm_data_size = l_cp->ppm_len;
4430 p_header_size -= l_remaining_data;
4431 p_header_data += l_remaining_data;
4434 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4435 l_cp->ppm_data_size += p_header_size;
4436 p_header_data += p_header_size;
4449 * Reads a PPM marker (Packed packet headers, main header)
4451 * @param p_header_data the data contained in the POC box.
4452 * @param p_j2k the jpeg2000 codec.
4453 * @param p_header_size the size of the data contained in the POC marker.
4454 * @param p_manager the user event manager.
4456 opj_bool j2k_read_ppm_v3 (
4457 opj_j2k_v2_t *p_j2k,
4458 OPJ_BYTE * p_header_data,
4459 OPJ_UINT32 p_header_size,
4460 struct opj_event_mgr * p_manager
4463 opj_cp_v2_t *l_cp = 00;
4464 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4467 assert(p_header_data != 00);
4468 assert(p_j2k != 00);
4469 assert(p_manager != 00);
4471 /* Minimum size of PPM marker is equal to the size of Zppm element */
4472 if (p_header_size < 1) {
4473 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4477 l_cp = &(p_j2k->m_cp);
4480 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4484 /* First PPM marker */
4486 /* We need now at least the Nppm^0 element */
4487 if (p_header_size < 4) {
4488 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4492 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4496 /* First PPM marker: Initialization */
4497 l_cp->ppm_len = l_N_ppm;
4498 l_cp->ppm_data_read = 0;
4500 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4501 if (l_cp->ppm_data == 00) {
4502 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4505 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4507 l_cp->ppm_data_current = l_cp->ppm_data;
4509 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4512 if (p_header_size < 4) {
4513 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4517 /* Uncompleted Ippm series in the previous PPM marker?*/
4518 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4519 /* Get the place where add the remaining Ippm series*/
4520 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4521 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4524 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4528 /* Increase the size of ppm_data to add the new Ippm series*/
4529 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4531 /* Keep the position of the place where concatenate the new series*/
4532 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4533 l_cp->ppm_len += l_N_ppm;
4538 l_remaining_data = p_header_size;
4540 while (l_remaining_data >= l_N_ppm) {
4541 /* read a complete Ippm series*/
4542 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4543 p_header_size -= l_N_ppm;
4544 p_header_data += l_N_ppm;
4546 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4550 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4555 l_remaining_data = p_header_size;
4559 l_remaining_data = p_header_size;
4561 /* Next Ippm series is a complete series ?*/
4562 if (l_remaining_data > l_N_ppm) {
4563 /* Increase the size of ppm_data to add the new Ippm series*/
4564 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4566 /* Keep the position of the place where concatenate the new series */
4567 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4568 l_cp->ppm_len += l_N_ppm;
4573 /* Need to read an incomplete Ippm series*/
4574 if (l_remaining_data) {
4575 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4577 /* Keep the position of the place where concatenate the new series*/
4578 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4579 l_cp->ppm_len += l_N_ppm;
4581 /* Read incomplete Ippm series*/
4582 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4583 p_header_size -= l_remaining_data;
4584 p_header_data += l_remaining_data;
4586 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4591 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4592 if (p_header_size >= 4) {
4594 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4597 l_cp->ppm_len += l_N_ppm ;
4599 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4600 if (l_cp->ppm_buffer == 00) {
4601 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4604 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4606 l_cp->ppm_data = l_cp->ppm_buffer;
4613 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4615 if (l_remaining_data <= p_header_size) {
4616 /* we must store less information than available in the packet */
4617 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4618 l_cp->ppm_data_size = l_cp->ppm_len;
4619 p_header_size -= l_remaining_data;
4620 p_header_data += l_remaining_data;
4623 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4624 l_cp->ppm_data_size += p_header_size;
4625 p_header_data += p_header_size;
4634 static void j2k_read_ppt(opj_j2k_t *j2k) {
4635 int len, Z_ppt, i, j = 0;
4637 opj_cp_t *cp = j2k->cp;
4638 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4639 opj_cio_t *cio = j2k->cio;
4641 len = cio_read(cio, 2);
4642 Z_ppt = cio_read(cio, 1);
4644 if (Z_ppt == 0) { /* First PPT marker */
4645 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4646 tcp->ppt_data_first = tcp->ppt_data;
4648 tcp->ppt_len = len - 3;
4649 } else { /* NON-first PPT marker */
4650 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4651 tcp->ppt_data_first = tcp->ppt_data;
4652 tcp->ppt_len = len - 3 + tcp->ppt_store;
4655 for (i = len - 3; i > 0; i--) {
4656 tcp->ppt_data[j] = cio_read(cio, 1);
4663 * Reads a PPT marker (Packed packet headers, tile-part header)
4665 * @param p_header_data the data contained in the PPT box.
4666 * @param p_j2k the jpeg2000 codec.
4667 * @param p_header_size the size of the data contained in the PPT marker.
4668 * @param p_manager the user event manager.
4670 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4671 OPJ_BYTE * p_header_data,
4672 OPJ_UINT32 p_header_size,
4673 struct opj_event_mgr * p_manager )
4675 opj_cp_v2_t *l_cp = 00;
4676 opj_tcp_v2_t *l_tcp = 00;
4680 assert(p_header_data != 00);
4681 assert(p_j2k != 00);
4682 assert(p_manager != 00);
4684 /* We need to have the Z_ppt element at minimum */
4685 if (p_header_size < 1) {
4686 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4690 l_cp = &(p_j2k->m_cp);
4692 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");
4696 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4699 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4703 /* Allocate buffer to read the packet header */
4705 /* First PPT marker */
4706 l_tcp->ppt_data_size = 0;
4707 l_tcp->ppt_len = p_header_size;
4709 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4710 if (l_tcp->ppt_buffer == 00) {
4711 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4714 l_tcp->ppt_data = l_tcp->ppt_buffer;
4716 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4719 l_tcp->ppt_len += p_header_size;
4721 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4722 if (l_tcp->ppt_buffer == 00) {
4723 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4726 l_tcp->ppt_data = l_tcp->ppt_buffer;
4728 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4731 /* Read packet header from buffer */
4732 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4734 l_tcp->ppt_data_size += p_header_size;
4740 * Writes the TLM marker (Tile Length Marker)
4742 * @param p_stream the stream to write data to.
4743 * @param p_j2k J2K codec.
4744 * @param p_manager the user event manager.
4746 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4747 struct opj_stream_private *p_stream,
4748 struct opj_event_mgr * p_manager )
4750 OPJ_BYTE * l_current_data = 00;
4751 OPJ_UINT32 l_tlm_size;
4754 assert(p_j2k != 00);
4755 assert(p_manager != 00);
4756 assert(p_stream != 00);
4758 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4760 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4761 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4762 = (OPJ_BYTE*)opj_realloc(
4763 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4766 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4770 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4773 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4775 /* change the way data is written to avoid seeking if possible */
4777 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4779 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4780 l_current_data += 2;
4782 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4783 l_current_data += 2;
4785 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4788 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4791 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4792 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) {
4799 static void j2k_write_tlm(opj_j2k_t *j2k){
4801 opj_cio_t *cio = j2k->cio;
4802 j2k->tlm_start = cio_tell(cio);
4803 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4804 lenp = 4 + (5*j2k->totnum_tp);
4805 cio_write(cio,lenp,2); /* Ltlm */
4806 cio_write(cio, 0,1); /* Ztlm=0*/
4807 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4808 cio_skip(cio,5*j2k->totnum_tp);
4811 static void j2k_write_sot(opj_j2k_t *j2k) {
4814 opj_cio_t *cio = j2k->cio;
4816 j2k->sot_start = cio_tell(cio);
4817 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4818 lenp = cio_tell(cio);
4819 cio_skip(cio, 2); /* Lsot (further) */
4820 cio_write(cio, j2k->curtileno, 2); /* Isot */
4821 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4822 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4823 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4824 len = cio_tell(cio) - lenp;
4825 cio_seek(cio, lenp);
4826 cio_write(cio, len, 2); /* Lsot */
4827 cio_seek(cio, lenp + len);
4831 /* update markers struct */
4832 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4833 #endif /* USE_JPWL */
4836 if( j2k->cstr_info && j2k->cur_tp_num==0){
4837 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4842 * Writes the SOT marker (Start of tile-part)
4844 * @param p_stream the stream to write data to.
4845 * @param p_j2k J2K codec.
4846 * @param p_manager the user event manager.
4848 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4850 OPJ_UINT32 * p_data_written,
4851 const struct opj_stream_private *p_stream,
4852 struct opj_event_mgr * p_manager )
4855 assert(p_j2k != 00);
4856 assert(p_manager != 00);
4857 assert(p_stream != 00);
4859 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4862 opj_write_bytes(p_data,10,2); /* Lsot */
4865 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4871 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4874 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4879 /* update markers struct */
4881 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4883 assert( 0 && "TODO" );
4884 #endif /* USE_JPWL */
4886 * p_data_written = 12;
4895 * Reads a PPT marker (Packed packet headers, tile-part header)
4897 * @param p_header_data the data contained in the PPT box.
4898 * @param p_j2k the jpeg2000 codec.
4899 * @param p_header_size the size of the data contained in the PPT marker.
4900 * @param p_manager the user event manager.
4902 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4903 OPJ_BYTE * p_header_data,
4904 OPJ_UINT32 p_header_size,
4905 opj_event_mgr_t * p_manager )
4907 opj_cp_v2_t *l_cp = 00;
4908 opj_tcp_v2_t *l_tcp = 00;
4909 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4910 OPJ_UINT32 l_current_part;
4911 OPJ_UINT32 l_tile_x,l_tile_y;
4914 assert(p_header_data != 00);
4915 assert(p_j2k != 00);
4916 assert(p_manager != 00);
4918 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4919 if (p_header_size != 8) {
4920 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4924 l_cp = &(p_j2k->m_cp);
4925 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4928 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4929 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4930 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4933 if (l_cp->correct) {
4935 int tileno = p_j2k->m_current_tile_number;
4936 static int backup_tileno = 0;
4938 /* tileno is negative or larger than the number of tiles!!! */
4939 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4940 opj_event_msg_v2(p_manager, EVT_ERROR,
4941 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4942 tileno, (l_cp->tw * l_cp->th));
4944 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4947 /* we try to correct */
4948 tileno = backup_tileno;
4949 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4950 "- setting tile number to %d\n",
4954 /* keep your private count of tiles */
4957 #endif /* USE_JPWL */
4959 /* look for the tile in the list of already processed tile (in parts). */
4960 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4961 /* since the time taken by this function can only grow at the time */
4963 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4966 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4967 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4969 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4975 if (l_cp->correct) {
4977 /* totlen is negative or larger than the bytes left!!! */
4978 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))) { */
4979 opj_event_msg_v2(p_manager, EVT_ERROR,
4980 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4981 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4983 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4986 /* we try to correct */
4988 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4989 "- setting Psot to %d => assuming it is the last tile\n",
4993 #endif /* USE_JPWL */
4995 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4997 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4998 "we assuming it is the last tile-part of the codestream.\n");
4999 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5002 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5005 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5008 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5009 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5010 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5011 if (l_tcp->m_nb_tile_parts) {
5012 if (l_current_part >= l_tcp->m_nb_tile_parts){
5013 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5014 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5015 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5019 l_tcp->m_nb_tile_parts = l_num_parts;
5022 /* If know the number of tile part header we will check if we didn't read the last*/
5023 if (l_tcp->m_nb_tile_parts) {
5024 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5025 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5029 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5030 /* Keep the size of data to skip after this marker */
5031 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5034 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5035 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5038 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5040 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5041 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5042 p_j2k->m_specific_param.m_decoder.m_skip_data =
5043 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5044 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5045 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5046 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5049 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5050 p_j2k->m_specific_param.m_decoder.m_skip_data =
5051 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5055 if (p_j2k->cstr_index)
5057 assert(p_j2k->cstr_index->tile_index != 00);
5058 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5059 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5061 if (l_num_parts != 0){
5062 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5063 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5066 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5067 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5068 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5070 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5071 (opj_tp_index_t*)opj_realloc(
5072 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5073 l_num_parts* sizeof(opj_tp_index_t));
5076 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5078 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5079 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5080 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5081 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5082 sizeof(opj_tp_index_t));
5085 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5086 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5087 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5088 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5089 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5090 * sizeof(opj_tp_index_t));
5099 /* 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 */
5100 /* if (p_j2k->cstr_info) {
5103 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5106 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5107 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5108 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5109 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5112 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5115 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5119 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5122 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5123 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5124 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5129 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5132 opj_tcp_t *tcp = NULL;
5133 opj_codestream_info_t *cstr_info = NULL;
5135 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5136 opj_cp_t *cp = j2k->cp;
5137 opj_cio_t *cio = j2k->cio;
5139 tcd->tp_num = j2k->tp_num ;
5140 tcd->cur_tp_num = j2k->cur_tp_num;
5142 cio_write(cio, J2K_MS_SOD, 2);
5144 if( j2k->cstr_info && j2k->cur_tp_num==0){
5145 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5148 if (j2k->curtileno == 0) {
5149 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5153 cstr_info = j2k->cstr_info;
5155 if (!j2k->cur_tp_num ) {
5156 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5157 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5160 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5161 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5165 /* update markers struct */
5166 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5167 #endif /* USE_JPWL */
5172 tcp = &cp->tcps[j2k->curtileno];
5173 for (layno = 0; layno < tcp->numlayers; layno++) {
5174 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5175 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5176 } else if (tcp->rates[layno]) {
5177 tcp->rates[layno]=1;
5180 if(j2k->cur_tp_num == 0){
5181 tcd->tcd_image->tiles->packno = 0;
5183 cstr_info->packno = 0;
5186 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5188 /* Writing Psot in SOT marker */
5189 totlen = cio_tell(cio) + l - j2k->sot_start;
5190 cio_seek(cio, j2k->sot_start + 6);
5191 cio_write(cio, totlen, 4);
5192 cio_seek(cio, j2k->sot_start + totlen);
5193 /* Writing Ttlm and Ptlm in TLM marker */
5195 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5196 cio_write(cio, j2k->curtileno, 1);
5197 cio_write(cio, totlen, 4);
5199 cio_seek(cio, j2k->sot_start + totlen);
5203 * Writes the SOD marker (Start of data)
5205 * @param p_stream the stream to write data to.
5206 * @param p_j2k J2K codec.
5207 * @param p_manager the user event manager.
5209 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5210 struct opj_tcd_v2 * p_tile_coder,
5212 OPJ_UINT32 * p_data_written,
5213 OPJ_UINT32 p_total_data_size,
5214 const struct opj_stream_private *p_stream,
5215 struct opj_event_mgr * p_manager )
5217 opj_tcp_v2_t *l_tcp = 00;
5218 opj_codestream_info_t *l_cstr_info = 00;
5219 opj_cp_v2_t *l_cp = 00;
5221 OPJ_UINT32 l_size_tile;
5222 OPJ_UINT32 l_remaining_data;
5225 assert(p_j2k != 00);
5226 assert(p_manager != 00);
5227 assert(p_stream != 00);
5229 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5232 /* make room for the EOF marker */
5233 l_remaining_data = p_total_data_size - 4;
5235 l_cp = &(p_j2k->m_cp);
5236 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5239 /* update tile coder */
5240 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5241 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5243 l_size_tile = l_cp->th * l_cp->tw;
5246 /* TODO mergeV2: check this part which use cstr_info */
5247 /*l_cstr_info = p_j2k->cstr_info;
5249 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5250 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5251 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5257 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5259 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5264 /* update markers struct */
5265 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5267 assert( 0 && "TODO" );
5268 #endif /* USE_JPWL */
5273 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5274 p_tile_coder->tcd_image->tiles->packno = 0;
5276 l_cstr_info->packno = 0;
5280 *p_data_written = 0;
5282 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)) {
5283 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5287 *p_data_written += 2;
5296 * Reads a SOD marker (Start Of Data)
5298 * @param p_header_data the data contained in the SOD box.
5299 * @param p_j2k the jpeg2000 codec.
5300 * @param p_header_size the size of the data contained in the SOD marker.
5301 * @param p_manager the user event manager.
5303 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
5304 opj_stream_private_t *p_stream,
5305 opj_event_mgr_t * p_manager
5308 OPJ_UINT32 l_current_read_size;
5309 opj_codestream_index_t * l_cstr_index = 00;
5310 OPJ_BYTE ** l_current_data = 00;
5311 opj_tcp_v2_t * l_tcp = 00;
5312 OPJ_UINT32 * l_tile_len = 00;
5315 assert(p_j2k != 00);
5316 assert(p_manager != 00);
5317 assert(p_stream != 00);
5319 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5321 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5322 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5323 // but we are in the last tile part,
5324 // so its result will fit on OPJ_UINT32 unless we find
5325 // a file with a single tile part of more than 4 GB...*/
5326 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5329 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5331 l_current_data = &(l_tcp->m_data);
5332 l_tile_len = &l_tcp->m_data_size;
5334 if (! *l_current_data) {
5335 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5338 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5341 if (*l_current_data == 00) {
5342 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5348 l_cstr_index = p_j2k->cstr_index;
5350 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5352 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5353 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5355 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5356 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5358 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5362 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5364 /*l_cstr_index->packno = 0;*/
5367 l_current_read_size = opj_stream_read_data( p_stream,
5368 *l_current_data + *l_tile_len,
5369 p_j2k->m_specific_param.m_decoder.m_sot_length,
5372 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5373 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5376 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5379 *l_tile_len += l_current_read_size;
5385 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5386 opj_cp_t *cp = j2k->cp;
5387 opj_tcp_t *tcp = &cp->tcps[tileno];
5388 opj_cio_t *cio = j2k->cio;
5389 int numcomps = j2k->image->numcomps;
5391 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5392 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5393 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5394 cio_write(cio, 0, 1); /* Srgn */
5395 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5399 * Writes the RGN marker (Region Of Interest)
5401 * @param p_tile_no the tile to output
5402 * @param p_comp_no the component to output
5403 * @param p_stream the stream to write data to.
5404 * @param p_j2k J2K codec.
5405 * @param p_manager the user event manager.
5407 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5408 OPJ_UINT32 p_tile_no,
5409 OPJ_UINT32 p_comp_no,
5410 struct opj_stream_private *p_stream,
5411 struct opj_event_mgr * p_manager )
5413 OPJ_BYTE * l_current_data = 00;
5414 OPJ_UINT32 l_nb_comp;
5415 OPJ_UINT32 l_rgn_size;
5416 opj_image_t *l_image = 00;
5417 opj_cp_v2_t *l_cp = 00;
5418 opj_tcp_v2_t *l_tcp = 00;
5419 opj_tccp_t *l_tccp = 00;
5420 OPJ_UINT32 l_comp_room;
5423 assert(p_j2k != 00);
5424 assert(p_manager != 00);
5425 assert(p_stream != 00);
5427 l_cp = &(p_j2k->m_cp);
5428 l_tcp = &l_cp->tcps[p_tile_no];
5429 l_tccp = &l_tcp->tccps[p_comp_no];
5431 l_nb_comp = l_image->numcomps;
5433 if (l_nb_comp <= 256) {
5440 l_rgn_size = 6 + l_comp_room;
5442 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5444 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5445 l_current_data += 2;
5447 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5448 l_current_data += 2;
5450 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5451 l_current_data+=l_comp_room;
5453 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5456 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5459 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) {
5467 static void j2k_write_eoc(opj_j2k_t *j2k) {
5468 opj_cio_t *cio = j2k->cio;
5469 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5470 cio_write(cio, J2K_MS_EOC, 2);
5474 /* update markers struct */
5475 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5476 #endif /* USE_JPWL */
5481 * Writes the EOC marker (End of Codestream)
5483 * @param p_stream the stream to write data to.
5484 * @param p_j2k J2K codec.
5485 * @param p_manager the user event manager.
5487 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5488 struct opj_stream_private *p_stream,
5489 struct opj_event_mgr * p_manager )
5492 assert(p_j2k != 00);
5493 assert(p_manager != 00);
5494 assert(p_stream != 00);
5496 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5501 /* update markers struct */
5503 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5505 #endif /* USE_JPWL */
5507 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5511 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5520 * Reads a RGN marker (Region Of Interest)
5522 * @param p_header_data the data contained in the POC box.
5523 * @param p_j2k the jpeg2000 codec.
5524 * @param p_header_size the size of the data contained in the POC marker.
5525 * @param p_manager the user event manager.
5527 opj_bool j2k_read_rgn_v2 (
5528 opj_j2k_v2_t *p_j2k,
5529 OPJ_BYTE * p_header_data,
5530 OPJ_UINT32 p_header_size,
5531 struct opj_event_mgr * p_manager
5534 OPJ_UINT32 l_nb_comp;
5535 opj_image_t * l_image = 00;
5537 opj_cp_v2_t *l_cp = 00;
5538 opj_tcp_v2_t *l_tcp = 00;
5539 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5542 assert(p_header_data != 00);
5543 assert(p_j2k != 00);
5544 assert(p_manager != 00);
5546 l_image = p_j2k->m_private_image;
5547 l_nb_comp = l_image->numcomps;
5549 if (l_nb_comp <= 256) {
5554 if (p_header_size != 2 + l_comp_room) {
5555 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5559 l_cp = &(p_j2k->m_cp);
5560 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5561 &l_cp->tcps[p_j2k->m_current_tile_number] :
5562 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5564 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5565 p_header_data+=l_comp_room;
5566 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5570 if (l_cp->correct) {
5571 /* totlen is negative or larger than the bytes left!!! */
5572 if (l_comp_room >= l_nb_comp) {
5573 opj_event_msg_v2(p_manager, EVT_ERROR,
5574 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5575 l_comp_room, l_nb_comp);
5576 if (!JPWL_ASSUME || JPWL_ASSUME) {
5577 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5582 #endif /* USE_JPWL */
5584 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5591 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5593 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5596 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5603 * Updates the rates of the tcp.
5605 * @param p_stream the stream to write data to.
5606 * @param p_j2k J2K codec.
5607 * @param p_manager the user event manager.
5609 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5610 struct opj_stream_private *p_stream,
5611 struct opj_event_mgr * p_manager )
5613 opj_cp_v2_t * l_cp = 00;
5614 opj_image_t * l_image = 00;
5615 opj_tcp_v2_t * l_tcp = 00;
5616 opj_image_comp_t * l_img_comp = 00;
5619 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5620 OPJ_FLOAT32 * l_rates = 0;
5621 OPJ_FLOAT32 l_sot_remove;
5622 OPJ_UINT32 l_bits_empty, l_size_pixel;
5623 OPJ_UINT32 l_tile_size = 0;
5624 OPJ_UINT32 l_last_res;
5625 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5628 assert(p_j2k != 00);
5629 assert(p_manager != 00);
5630 assert(p_stream != 00);
5633 l_cp = &(p_j2k->m_cp);
5634 l_image = p_j2k->m_private_image;
5637 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5638 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5639 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5641 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5642 l_tp_stride_func = get_tp_stride;
5645 l_tp_stride_func = get_default_stride;
5648 for (i=0;i<l_cp->th;++i) {
5649 for (j=0;j<l_cp->tw;++j) {
5650 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5652 /* 4 borders of the tile rescale on the image if necessary */
5653 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5654 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5655 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5656 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5658 l_rates = l_tcp->rates;
5660 /* Modification of the RATE >> */
5662 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5664 ((*l_rates) * l_bits_empty)
5672 for (k = 1; k < l_tcp->numlayers; ++k) {
5674 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5676 ((*l_rates) * l_bits_empty)
5692 for (i=0;i<l_cp->th;++i) {
5693 for (j=0;j<l_cp->tw;++j) {
5694 l_rates = l_tcp->rates;
5697 *l_rates -= l_sot_remove;
5699 if (*l_rates < 30) {
5706 l_last_res = l_tcp->numlayers - 1;
5708 for (k = 1; k < l_last_res; ++k) {
5711 *l_rates -= l_sot_remove;
5713 if (*l_rates < *(l_rates - 1) + 10) {
5714 *l_rates = (*(l_rates - 1)) + 20;
5722 *l_rates -= (l_sot_remove + 2.f);
5724 if (*l_rates < *(l_rates - 1) + 10) {
5725 *l_rates = (*(l_rates - 1)) + 20;
5733 l_img_comp = l_image->comps;
5736 for (i=0;i<l_image->numcomps;++i) {
5737 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5739 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5747 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5749 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5751 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5752 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5753 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5754 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5758 if (l_cp->m_specific_param.m_enc.m_cinema) {
5759 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5760 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5761 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5765 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5766 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5772 static void j2k_read_eoc(opj_j2k_t *j2k) {
5774 opj_bool success = OPJ_FALSE;
5776 /* if packets should be decoded */
5777 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5778 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5779 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5780 for (i = 0; i < j2k->cp->tileno_size; i++) {
5781 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5782 if (j2k->cp->tileno[i] != -1)
5784 tileno = j2k->cp->tileno[i];
5785 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5786 opj_free(j2k->tile_data[tileno]);
5787 j2k->tile_data[tileno] = NULL;
5788 tcd_free_decode_tile(tcd, i);
5791 success = OPJ_FALSE;
5792 if (success == OPJ_FALSE) {
5793 j2k->state |= J2K_STATE_ERR;
5797 tcd_free_decode(tcd);
5800 /* if packets should not be decoded */
5802 for (i = 0; i < j2k->cp->tileno_size; i++) {
5803 tileno = j2k->cp->tileno[i];
5804 opj_free(j2k->tile_data[tileno]);
5805 j2k->tile_data[tileno] = NULL;
5808 if (j2k->state & J2K_STATE_ERR)
5809 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5811 j2k->state = J2K_STATE_MT;
5815 * Reads a EOC marker (End Of Codestream)
5817 * @param p_header_data the data contained in the SOD box.
5818 * @param p_j2k the jpeg2000 codec.
5819 * @param p_header_size the size of the data contained in the SOD marker.
5820 * @param p_manager the user event manager.
5823 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5824 struct opj_stream_private *p_stream,
5825 struct opj_event_mgr * p_manager )
5828 opj_tcd_v2_t * l_tcd = 00;
5829 OPJ_UINT32 l_nb_tiles;
5830 opj_tcp_v2_t * l_tcp = 00;
5834 assert(p_j2k != 00);
5835 assert(p_manager != 00);
5836 assert(p_stream != 00);
5838 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5839 l_tcp = p_j2k->m_cp.tcps;
5841 l_tcd = tcd_create_v2(OPJ_TRUE);
5843 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5847 for (i = 0; i < l_nb_tiles; ++i) {
5848 if (l_tcp->m_data) {
5849 if (! tcd_init_decode_tile(l_tcd, i)) {
5850 tcd_destroy_v2(l_tcd);
5851 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5855 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5859 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5864 j2k_tcp_destroy(l_tcp);
5868 tcd_destroy_v2(l_tcd);
5874 * Gets the offset of the header.
5876 * @param p_stream the stream to write data to.
5877 * @param p_j2k J2K codec.
5878 * @param p_manager the user event manager.
5880 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5881 struct opj_stream_private *p_stream,
5882 struct opj_event_mgr * p_manager )
5885 assert(p_j2k != 00);
5886 assert(p_manager != 00);
5887 assert(p_stream != 00);
5889 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5895 * Writes the MCT marker (Multiple Component Transform)
5897 * @param p_stream the stream to write data to.
5898 * @param p_j2k J2K codec.
5899 * @param p_manager the user event manager.
5901 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5902 struct opj_stream_private *p_stream,
5903 struct opj_event_mgr * p_manager )
5906 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5907 opj_mct_data_t * l_mct_record;
5908 opj_tcp_v2_t * l_tcp;
5911 assert(p_j2k != 00);
5912 assert(p_stream != 00);
5913 assert(p_manager != 00);
5915 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5919 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5920 l_mct_record = l_tcp->m_mct_records;
5922 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5924 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5931 l_mcc_record = l_tcp->m_mcc_records;
5933 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5935 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5942 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5950 * Writes the image components.
5952 * @param p_stream the stream to write data to.
5953 * @param p_j2k J2K codec.
5954 * @param p_manager the user event manager.
5956 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5957 struct opj_stream_private *p_stream,
5958 struct opj_event_mgr * p_manager )
5963 assert(p_j2k != 00);
5964 assert(p_manager != 00);
5965 assert(p_stream != 00);
5967 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5969 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5973 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5982 * Writes regions of interests.
5984 * @param p_stream the stream to write data to.
5985 * @param p_j2k J2K codec.
5986 * @param p_manager the user event manager.
5988 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5989 struct opj_stream_private *p_stream,
5990 struct opj_event_mgr * p_manager )
5993 const opj_tccp_t *l_tccp = 00;
5996 assert(p_j2k != 00);
5997 assert(p_manager != 00);
5998 assert(p_stream != 00);
6000 l_tccp = p_j2k->m_cp.tcps->tccps;
6002 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6003 if (l_tccp->roishift) {
6005 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6019 * @param p_stream the stream to write data to.
6020 * @param p_j2k J2K codec.
6021 * @param p_manager the user event manager.
6023 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6024 struct opj_stream_private *p_stream,
6025 struct opj_event_mgr * p_manager )
6027 opj_codestream_index_t * l_cstr_index = 00;
6030 assert(p_j2k != 00);
6031 assert(p_manager != 00);
6032 assert(p_stream != 00);
6034 l_cstr_index = p_j2k->cstr_index;
6036 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6038 /* The following adjustment is done to adjust the codestream size */
6039 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6040 /* the first bunch of bytes is not in the codestream */
6041 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6046 /* preparation of JPWL marker segments */
6050 /* encode according to JPWL */
6051 jpwl_encode(p_j2k, p_stream, image);
6055 assert( 0 && "TODO" );
6056 #endif /* USE_JPWL */
6061 typedef struct opj_dec_mstabent {
6064 /** value of the state when the marker can appear */
6066 /** action linked to the marker */
6067 void (*handler) (opj_j2k_t *j2k);
6068 } opj_dec_mstabent_t;
6070 opj_dec_mstabent_t j2k_dec_mstab[] = {
6071 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
6072 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
6073 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
6074 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6075 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
6076 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
6077 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
6078 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
6079 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6080 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6081 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6082 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6083 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6084 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6085 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6086 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6088 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6089 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6092 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6093 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6094 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6095 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6096 #endif /* USE_JPWL */
6098 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6099 {J2K_MS_INSEC, 0, j2k_read_insec},
6100 #endif /* USE_JPSEC */
6102 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6105 static void j2k_read_unk(opj_j2k_t *j2k) {
6106 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6109 if (j2k->cp->correct) {
6111 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6112 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6113 id = cio_read(j2k->cio, 2);
6114 opj_event_msg(j2k->cinfo, EVT_ERROR,
6115 "JPWL: really don't know this marker %x\n",
6118 opj_event_msg(j2k->cinfo, EVT_ERROR,
6119 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6122 /* OK, activate this at your own risk!!! */
6123 /* we look for the marker at the minimum hamming distance from this */
6124 while (j2k_dec_mstab[m].id) {
6126 /* 1's where they differ */
6127 tmp_id = j2k_dec_mstab[m].id ^ id;
6129 /* compute the hamming distance between our id and the current */
6131 for (i = 0; i < 16; i++) {
6132 if ((tmp_id >> i) & 0x0001) {
6137 /* if current distance is smaller, set the minimum */
6138 if (cur_dist < min_dist) {
6139 min_dist = cur_dist;
6140 min_id = j2k_dec_mstab[m].id;
6143 /* jump to the next marker */
6147 /* do we substitute the marker? */
6148 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6149 opj_event_msg(j2k->cinfo, EVT_ERROR,
6150 "- marker %x is at distance %d from the read %x\n",
6151 min_id, min_dist, id);
6152 opj_event_msg(j2k->cinfo, EVT_ERROR,
6153 "- trying to substitute in place and crossing fingers!\n");
6154 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6155 cio_write(j2k->cio, min_id, 2);
6158 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6163 #endif /* USE_JPWL */
6168 * Reads an unknown marker
6170 * @param p_stream the stream object to read from.
6171 * @param p_j2k the jpeg2000 codec.
6172 * @param p_manager the user event manager.
6174 * @return true if the marker could be deduced.
6176 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6177 struct opj_stream_private *p_stream,
6178 OPJ_UINT32 *output_marker,
6179 struct opj_event_mgr * p_manager
6182 OPJ_UINT32 l_unknown_marker;
6183 const opj_dec_memory_marker_handler_t * l_marker_handler;
6184 OPJ_UINT32 l_size_unk = 2;
6187 assert(p_j2k != 00);
6188 assert(p_manager != 00);
6189 assert(p_stream != 00);
6191 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6194 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6195 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6196 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6200 /* read 2 bytes as the new marker ID*/
6201 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6203 if (!(l_unknown_marker < 0xff00)) {
6205 /* Get the marker handler from the marker ID*/
6206 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6208 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6209 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6213 if (l_marker_handler->id != J2K_MS_UNK) {
6214 /* Add the marker to the codestream index*/
6215 if (l_marker_handler->id != J2K_MS_SOT)
6216 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6217 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6219 break; /* next marker is known and well located */
6227 *output_marker = l_marker_handler->id ;
6233 Read the lookup table containing all the marker, status and action
6234 @param id Marker value
6236 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6237 opj_dec_mstabent_t *e;
6238 for (e = j2k_dec_mstab; e->id != 0; e++) {
6247 * Writes the MCT marker (Multiple Component Transform)
6249 * @param p_stream the stream to write data to.
6250 * @param p_j2k J2K codec.
6251 * @param p_manager the user event manager.
6253 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6254 opj_mct_data_t * p_mct_record,
6255 struct opj_stream_private *p_stream,
6256 struct opj_event_mgr * p_manager )
6258 OPJ_UINT32 l_mct_size;
6259 OPJ_BYTE * l_current_data = 00;
6263 assert(p_j2k != 00);
6264 assert(p_manager != 00);
6265 assert(p_stream != 00);
6267 l_mct_size = 10 + p_mct_record->m_data_size;
6269 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6270 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6271 = (OPJ_BYTE*)opj_realloc(
6272 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6275 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6279 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6282 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6284 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6285 l_current_data += 2;
6287 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6288 l_current_data += 2;
6290 opj_write_bytes(l_current_data,0,2); /* Zmct */
6291 l_current_data += 2;
6293 /* only one marker atm */
6294 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6296 opj_write_bytes(l_current_data,l_tmp,2);
6297 l_current_data += 2;
6299 opj_write_bytes(l_current_data,0,2); /* Ymct */
6302 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6304 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) {
6312 * Reads a MCT marker (Multiple Component Transform)
6314 * @param p_header_data the data contained in the MCT box.
6315 * @param p_j2k the jpeg2000 codec.
6316 * @param p_header_size the size of the data contained in the MCT marker.
6317 * @param p_manager the user event manager.
6319 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6320 OPJ_BYTE * p_header_data,
6321 OPJ_UINT32 p_header_size,
6322 struct opj_event_mgr * p_manager )
6325 opj_tcp_v2_t *l_tcp = 00;
6328 opj_mct_data_t * l_mct_data;
6331 assert(p_header_data != 00);
6332 assert(p_j2k != 00);
6334 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6335 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6336 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6338 if (p_header_size < 2) {
6339 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6344 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6347 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6351 if(p_header_size <= 6) {
6352 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6356 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6357 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6360 l_indix = l_tmp & 0xff;
6361 l_mct_data = l_tcp->m_mct_records;
6363 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6364 if (l_mct_data->m_index == l_indix) {
6371 if (i == l_tcp->m_nb_mct_records) {
6372 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6373 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6375 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));
6376 if(! l_tcp->m_mct_records) {
6377 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6381 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6382 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6385 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6388 if (l_mct_data->m_data) {
6389 opj_free(l_mct_data->m_data);
6390 l_mct_data->m_data = 00;
6393 l_mct_data->m_index = l_indix;
6394 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6395 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6397 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6400 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6406 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6407 if (! l_mct_data->m_data) {
6408 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6411 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6413 l_mct_data->m_data_size = p_header_size;
6414 ++l_tcp->m_nb_mct_records;
6420 * Writes the MCC marker (Multiple Component Collection)
6422 * @param p_stream the stream to write data to.
6423 * @param p_j2k J2K codec.
6424 * @param p_manager the user event manager.
6426 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6427 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6428 struct opj_stream_private *p_stream,
6429 struct opj_event_mgr * p_manager )
6432 OPJ_UINT32 l_mcc_size;
6433 OPJ_BYTE * l_current_data = 00;
6434 OPJ_UINT32 l_nb_bytes_for_comp;
6439 assert(p_j2k != 00);
6440 assert(p_manager != 00);
6441 assert(p_stream != 00);
6443 if (p_mcc_record->m_nb_comps > 255 ) {
6444 l_nb_bytes_for_comp = 2;
6448 l_nb_bytes_for_comp = 1;
6452 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6453 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6455 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6456 = (OPJ_BYTE*)opj_realloc(
6457 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6459 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6463 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6466 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6468 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6469 l_current_data += 2;
6471 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6472 l_current_data += 2;
6475 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6476 l_current_data += 2;
6478 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6481 /* only one marker atm */
6482 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6485 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6488 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6491 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 */
6494 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6495 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6496 l_current_data+=l_nb_bytes_for_comp;
6499 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 */
6502 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6504 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6505 l_current_data+=l_nb_bytes_for_comp;
6508 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6510 if (p_mcc_record->m_decorrelation_array) {
6511 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6514 if (p_mcc_record->m_offset_array) {
6515 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6518 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6521 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) {
6529 * Reads a MCC marker (Multiple Component Collection)
6531 * @param p_header_data the data contained in the MCC box.
6532 * @param p_j2k the jpeg2000 codec.
6533 * @param p_header_size the size of the data contained in the MCC marker.
6534 * @param p_manager the user event manager.
6536 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6537 OPJ_BYTE * p_header_data,
6538 OPJ_UINT32 p_header_size,
6539 struct opj_event_mgr * p_manager )
6544 opj_tcp_v2_t * l_tcp;
6545 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6546 opj_mct_data_t * l_mct_data;
6547 OPJ_UINT32 l_nb_collections;
6548 OPJ_UINT32 l_nb_comps;
6549 OPJ_UINT32 l_nb_bytes_by_comp;
6553 assert(p_header_data != 00);
6554 assert(p_j2k != 00);
6555 assert(p_manager != 00);
6557 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6558 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6559 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6561 if (p_header_size < 2) {
6562 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6567 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6570 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6574 if (p_header_size < 7) {
6575 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6579 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6582 l_mcc_record = l_tcp->m_mcc_records;
6584 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6585 if (l_mcc_record->m_index == l_indix) {
6592 if (i == l_tcp->m_nb_mcc_records) {
6593 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6594 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6596 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6597 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6598 if (! l_tcp->m_mcc_records) {
6599 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6602 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6603 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));
6605 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6607 l_mcc_record->m_index = l_indix;
6609 /* only one marker atm */
6610 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6613 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6617 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6620 if (l_nb_collections > 1) {
6621 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6627 for (i=0;i<l_nb_collections;++i) {
6628 if (p_header_size < 3) {
6629 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6633 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6637 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6641 opj_read_bytes(p_header_data,&l_nb_comps,2);
6646 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6647 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6649 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6650 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6654 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6656 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6657 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6658 p_header_data+=l_nb_bytes_by_comp;
6661 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6666 opj_read_bytes(p_header_data,&l_nb_comps,2);
6669 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6670 l_nb_comps &= 0x7fff;
6672 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6673 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6677 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6678 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6682 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6684 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6685 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6686 p_header_data+=l_nb_bytes_by_comp;
6689 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6694 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6697 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6698 l_mcc_record->m_decorrelation_array = 00;
6699 l_mcc_record->m_offset_array = 00;
6701 l_indix = l_tmp & 0xff;
6703 l_mct_data = l_tcp->m_mct_records;
6704 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6705 if (l_mct_data->m_index == l_indix) {
6706 l_mcc_record->m_decorrelation_array = l_mct_data;
6712 if (l_mcc_record->m_decorrelation_array == 00) {
6713 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6718 l_indix = (l_tmp >> 8) & 0xff;
6720 l_mct_data = l_tcp->m_mct_records;
6721 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6722 if (l_mct_data->m_index == l_indix) {
6723 l_mcc_record->m_offset_array = l_mct_data;
6729 if (l_mcc_record->m_offset_array == 00) {
6730 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6736 if (p_header_size != 0) {
6737 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6741 ++l_tcp->m_nb_mcc_records;
6748 * Writes the MCO marker (Multiple component transformation ordering)
6750 * @param p_stream the stream to write data to.
6751 * @param p_j2k J2K codec.
6752 * @param p_manager the user event manager.
6754 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6755 struct opj_stream_private *p_stream,
6756 struct opj_event_mgr * p_manager
6759 OPJ_BYTE * l_current_data = 00;
6760 OPJ_UINT32 l_mco_size;
6761 opj_tcp_v2_t * l_tcp = 00;
6762 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6766 assert(p_j2k != 00);
6767 assert(p_manager != 00);
6768 assert(p_stream != 00);
6770 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6771 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6773 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6774 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6776 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6777 = (OPJ_BYTE*)opj_realloc(
6778 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6780 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6785 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6788 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6789 l_current_data += 2;
6791 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6792 l_current_data += 2;
6794 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6797 l_mcc_record = l_tcp->m_mcc_records;
6798 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6799 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6805 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) {
6813 * Reads a MCO marker (Multiple Component Transform Ordering)
6815 * @param p_header_data the data contained in the MCO box.
6816 * @param p_j2k the jpeg2000 codec.
6817 * @param p_header_size the size of the data contained in the MCO marker.
6818 * @param p_manager the user event manager.
6820 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6821 OPJ_BYTE * p_header_data,
6822 OPJ_UINT32 p_header_size,
6823 struct opj_event_mgr * p_manager )
6825 OPJ_UINT32 l_tmp, i;
6826 OPJ_UINT32 l_nb_stages;
6827 opj_tcp_v2_t * l_tcp;
6828 opj_tccp_t * l_tccp;
6829 opj_image_t * l_image;
6830 opj_image_comp_t * l_img_comp;
6833 assert(p_header_data != 00);
6834 assert(p_j2k != 00);
6835 assert(p_manager != 00);
6837 l_image = p_j2k->m_private_image;
6838 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6839 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6840 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6842 if (p_header_size < 1) {
6843 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6847 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6850 if (l_nb_stages > 1) {
6851 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6855 if (p_header_size != l_nb_stages + 1) {
6856 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6860 l_tccp = l_tcp->tccps;
6861 l_img_comp = l_image->comps;
6863 for (i=0;i<l_image->numcomps;++i) {
6864 l_tccp->m_dc_level_shift = 0;
6868 if (l_tcp->m_mct_decoding_matrix) {
6869 opj_free(l_tcp->m_mct_decoding_matrix);
6870 l_tcp->m_mct_decoding_matrix = 00;
6873 for (i=0;i<l_nb_stages;++i) {
6874 opj_read_bytes(p_header_data,&l_tmp,1);
6877 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6885 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6888 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6889 opj_mct_data_t * l_deco_array, * l_offset_array;
6890 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6891 OPJ_UINT32 l_nb_elem;
6892 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6893 opj_tccp_t * l_tccp;
6896 assert(p_tcp != 00);
6898 l_mcc_record = p_tcp->m_mcc_records;
6900 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6901 if (l_mcc_record->m_index == p_index) {
6906 if (i==p_tcp->m_nb_mcc_records) {
6907 /** element discarded **/
6911 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6912 /** do not support number of comps != image */
6916 l_deco_array = l_mcc_record->m_decorrelation_array;
6919 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6920 if (l_deco_array->m_data_size != l_data_size) {
6924 l_nb_elem = p_image->numcomps * p_image->numcomps;
6925 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6926 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6928 if (! p_tcp->m_mct_decoding_matrix ) {
6932 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);
6935 l_offset_array = l_mcc_record->m_offset_array;
6937 if (l_offset_array) {
6938 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6939 if (l_offset_array->m_data_size != l_data_size) {
6943 l_nb_elem = p_image->numcomps;
6944 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6945 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6947 if (! l_offset_data ) {
6951 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6953 l_tccp = p_tcp->tccps;
6954 l_current_offset_data = l_offset_data;
6956 for (i=0;i<p_image->numcomps;++i) {
6957 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6961 opj_free(l_offset_data);
6968 * Writes the CBD marker (Component bit depth definition)
6970 * @param p_stream the stream to write data to.
6971 * @param p_j2k J2K codec.
6972 * @param p_manager the user event manager.
6974 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6975 struct opj_stream_private *p_stream,
6976 struct opj_event_mgr * p_manager )
6979 OPJ_UINT32 l_cbd_size;
6980 OPJ_BYTE * l_current_data = 00;
6981 opj_image_t *l_image = 00;
6982 opj_image_comp_t * l_comp = 00;
6985 assert(p_j2k != 00);
6986 assert(p_manager != 00);
6987 assert(p_stream != 00);
6989 l_image = p_j2k->m_private_image;
6990 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6992 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6993 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6994 = (OPJ_BYTE*)opj_realloc(
6995 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6998 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7002 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7005 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7007 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7008 l_current_data += 2;
7010 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7011 l_current_data += 2;
7013 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7016 l_comp = l_image->comps;
7018 for (i=0;i<l_image->numcomps;++i) {
7019 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7025 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) {
7033 * Reads a CBD marker (Component bit depth definition)
7034 * @param p_header_data the data contained in the CBD box.
7035 * @param p_j2k the jpeg2000 codec.
7036 * @param p_header_size the size of the data contained in the CBD marker.
7037 * @param p_manager the user event manager.
7039 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7040 OPJ_BYTE * p_header_data,
7041 OPJ_UINT32 p_header_size,
7042 struct opj_event_mgr * p_manager)
7044 OPJ_UINT32 l_nb_comp,l_num_comp;
7045 OPJ_UINT32 l_comp_def;
7047 opj_image_comp_t * l_comp = 00;
7050 assert(p_header_data != 00);
7051 assert(p_j2k != 00);
7052 assert(p_manager != 00);
7054 l_num_comp = p_j2k->m_private_image->numcomps;
7056 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7057 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7061 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7064 if (l_nb_comp != l_num_comp) {
7065 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7069 l_comp = p_j2k->m_private_image->comps;
7070 for (i=0;i<l_num_comp;++i) {
7071 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7073 l_comp->sgnd = (l_comp_def>>7) & 1;
7074 l_comp->prec = (l_comp_def&0x7f) + 1;
7082 /* ----------------------------------------------------------------------- */
7083 /* J2K / JPT decoder interface */
7084 /* ----------------------------------------------------------------------- */
7088 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7091 if(j2k->tile_len != NULL) {
7092 opj_free(j2k->tile_len);
7094 if(j2k->tile_data != NULL) {
7095 opj_free(j2k->tile_data);
7097 if(j2k->default_tcp != NULL) {
7098 opj_tcp_t *default_tcp = j2k->default_tcp;
7099 if(default_tcp->ppt_data_first != NULL) {
7100 opj_free(default_tcp->ppt_data_first);
7102 if(j2k->default_tcp->tccps != NULL) {
7103 opj_free(j2k->default_tcp->tccps);
7105 opj_free(j2k->default_tcp);
7107 if(j2k->cp != NULL) {
7108 opj_cp_t *cp = j2k->cp;
7109 if(cp->tcps != NULL) {
7110 for(i = 0; i < cp->tw * cp->th; i++) {
7111 if(cp->tcps[i].ppt_data_first != NULL) {
7112 opj_free(cp->tcps[i].ppt_data_first);
7114 if(cp->tcps[i].tccps != NULL) {
7115 opj_free(cp->tcps[i].tccps);
7120 if(cp->ppm_data_first != NULL) {
7121 opj_free(cp->ppm_data_first);
7123 if(cp->tileno != NULL) {
7124 opj_free(cp->tileno);
7126 if(cp->comment != NULL) {
7127 opj_free(cp->comment);
7136 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7138 if(j2k && parameters) {
7139 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7140 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7143 j2k->m_cp.correct = parameters->jpwl_correct;
7144 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7145 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7146 #endif /* USE_JPWL */
7153 * Read a JPT-stream and decode file
7156 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7157 opj_image_t *image = NULL;
7158 opj_jpt_msg_header_t header;
7160 opj_common_ptr cinfo = j2k->cinfo;
7162 OPJ_ARG_NOT_USED(cstr_info);
7166 /* create an empty image */
7167 image = opj_image_create0();
7170 j2k->state = J2K_STATE_MHSOC;
7172 /* Initialize the header */
7173 jpt_init_msg_header(&header);
7174 /* Read the first header of the message */
7175 jpt_read_msg_header(cinfo, cio, &header);
7177 position = cio_tell(cio);
7178 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7179 opj_image_destroy(image);
7180 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7185 opj_dec_mstabent_t *e = NULL;
7188 if (!cio_numbytesleft(cio)) {
7192 /* data-bin read -> need to read a new header */
7193 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7194 jpt_read_msg_header(cinfo, cio, &header);
7195 position = cio_tell(cio);
7196 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7197 opj_image_destroy(image);
7198 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7203 id = cio_read(cio, 2);
7204 if (id >> 8 != 0xff) {
7205 opj_image_destroy(image);
7206 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7209 e = j2k_dec_mstab_lookup(id);
7210 if (!(j2k->state & e->states)) {
7211 opj_image_destroy(image);
7212 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7218 if (j2k->state == J2K_STATE_MT) {
7221 if (j2k->state == J2K_STATE_NEOC) {
7225 if (j2k->state == J2K_STATE_NEOC) {
7229 if (j2k->state != J2K_STATE_MT) {
7230 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7236 /* ----------------------------------------------------------------------- */
7237 /* J2K encoder interface */
7238 /* ----------------------------------------------------------------------- */
7240 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7241 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7248 opj_j2k_v2_t* j2k_create_compress_v2(void)
7250 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7255 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7257 l_j2k->m_is_decoder = 0;
7258 l_j2k->m_cp.m_is_decoder = 0;
7260 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7261 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7266 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7268 /* validation list creation*/
7269 l_j2k->m_validation_list = opj_procedure_list_create();
7270 if (! l_j2k->m_validation_list) {
7275 /* execution list creation*/
7276 l_j2k->m_procedure_list = opj_procedure_list_create();
7277 if (! l_j2k->m_procedure_list) {
7285 void j2k_destroy_compress(opj_j2k_t *j2k) {
7289 if(j2k->cp != NULL) {
7290 opj_cp_t *cp = j2k->cp;
7293 opj_free(cp->comment);
7296 opj_free(cp->matrice);
7298 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7299 opj_free(cp->tcps[tileno].tccps);
7308 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7310 int tileno, numpocs_tile;
7311 opj_cp_t *cp = NULL;
7313 if(!j2k || !parameters || ! image) {
7317 /* create and initialize the coding parameters structure */
7318 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7320 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7323 /* set default values for cp */
7328 copy user encoding parameters
7330 cp->cinema = parameters->cp_cinema;
7331 cp->max_comp_size = parameters->max_comp_size;
7332 cp->rsiz = parameters->cp_rsiz;
7333 cp->disto_alloc = parameters->cp_disto_alloc;
7334 cp->fixed_alloc = parameters->cp_fixed_alloc;
7335 cp->fixed_quality = parameters->cp_fixed_quality;
7337 /* mod fixed_quality */
7338 if(parameters->cp_matrice) {
7339 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7340 cp->matrice = (int *) opj_malloc(array_size);
7341 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7345 cp->tdx = parameters->cp_tdx;
7346 cp->tdy = parameters->cp_tdy;
7349 cp->tx0 = parameters->cp_tx0;
7350 cp->ty0 = parameters->cp_ty0;
7352 /* comment string */
7353 if(parameters->cp_comment) {
7354 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7356 strcpy(cp->comment, parameters->cp_comment);
7361 calculate other encoding parameters
7364 if (parameters->tile_size_on) {
7365 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7366 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7368 cp->tdx = image->x1 - cp->tx0;
7369 cp->tdy = image->y1 - cp->ty0;
7372 if(parameters->tp_on){
7373 cp->tp_flag = parameters->tp_flag;
7378 for(i=0;i<image->numcomps ;i++){
7379 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7385 calculate JPWL encoding parameters
7388 if (parameters->jpwl_epc_on) {
7392 cp->epc_on = OPJ_TRUE;
7393 cp->info_on = OPJ_FALSE; /* no informative technique */
7396 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7397 cp->epb_on = OPJ_TRUE;
7399 cp->hprot_MH = parameters->jpwl_hprot_MH;
7400 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7401 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7402 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7404 /* if tile specs are not specified, copy MH specs */
7405 if (cp->hprot_TPH[0] == -1) {
7406 cp->hprot_TPH_tileno[0] = 0;
7407 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7409 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7410 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7411 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7412 cp->pprot[i] = parameters->jpwl_pprot[i];
7416 /* set ESD writing */
7417 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7418 cp->esd_on = OPJ_TRUE;
7420 cp->sens_size = parameters->jpwl_sens_size;
7421 cp->sens_addr = parameters->jpwl_sens_addr;
7422 cp->sens_range = parameters->jpwl_sens_range;
7424 cp->sens_MH = parameters->jpwl_sens_MH;
7425 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7426 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7427 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7431 /* always set RED writing to false: we are at the encoder */
7432 cp->red_on = OPJ_FALSE;
7435 cp->epc_on = OPJ_FALSE;
7437 #endif /* USE_JPWL */
7440 /* initialize the mutiple tiles */
7441 /* ---------------------------- */
7442 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7444 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7445 opj_tcp_t *tcp = &cp->tcps[tileno];
7446 tcp->numlayers = parameters->tcp_numlayers;
7447 assert ( tcp->numlayers >= 0 );
7448 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7450 if (cp->fixed_quality) {
7451 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7453 tcp->rates[j] = parameters->tcp_rates[j];
7455 if (cp->fixed_quality) { /* add fixed_quality */
7456 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7458 tcp->rates[j] = parameters->tcp_rates[j];
7462 tcp->csty = parameters->csty;
7463 tcp->prg = parameters->prog_order;
7464 tcp->mct = parameters->tcp_mct;
7468 if (parameters->numpocs) {
7469 /* initialisation of POC */
7471 assert( parameters->numpocs >= 0 );
7472 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7473 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7474 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7475 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7476 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7477 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7478 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7479 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7480 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7481 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7485 tcp->numpocs = numpocs_tile -1 ;
7490 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7492 for (i = 0; i < image->numcomps; i++) {
7493 opj_tccp_t *tccp = &tcp->tccps[i];
7494 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7495 tccp->numresolutions = parameters->numresolution;
7496 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7497 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7498 tccp->cblksty = parameters->mode;
7499 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7500 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7502 assert(parameters->roi_compno >= 0);
7503 if (i == (OPJ_UINT32)parameters->roi_compno) {
7504 tccp->roishift = parameters->roi_shift;
7509 if(parameters->cp_cinema)
7511 /*Precinct size for lowest frequency subband=128*/
7514 /*Precinct size at all other resolutions = 256*/
7515 for (j = 1; j < tccp->numresolutions; j++) {
7520 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7522 assert(tccp->numresolutions > 0);
7523 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7524 if (p < parameters->res_spec) {
7526 if (parameters->prcw_init[p] < 1) {
7529 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7532 if (parameters->prch_init[p] < 1) {
7535 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7539 int res_spec = parameters->res_spec;
7540 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7541 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7543 if (size_prcw < 1) {
7546 tccp->prcw[j] = int_floorlog2(size_prcw);
7549 if (size_prch < 1) {
7552 tccp->prch[j] = int_floorlog2(size_prch);
7556 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7559 for (j = 0; j < tccp->numresolutions; j++) {
7566 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7571 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7572 opj_cparameters_t *parameters,
7574 struct opj_event_mgr * p_manager)
7576 OPJ_UINT32 i, j, tileno, numpocs_tile;
7577 opj_cp_v2_t *cp = 00;
7580 if(!p_j2k || !parameters || ! image) {
7584 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7585 cp = &(p_j2k->m_cp);
7587 /* set default values for cp */
7592 copy user encoding parameters
7594 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7595 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7596 cp->rsiz = parameters->cp_rsiz;
7597 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7598 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7599 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7601 /* mod fixed_quality */
7602 if (parameters->cp_matrice) {
7603 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7604 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7605 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7609 cp->tdx = parameters->cp_tdx;
7610 cp->tdy = parameters->cp_tdy;
7613 cp->tx0 = parameters->cp_tx0;
7614 cp->ty0 = parameters->cp_ty0;
7616 /* comment string */
7617 if(parameters->cp_comment) {
7618 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7620 strcpy(cp->comment, parameters->cp_comment);
7625 calculate other encoding parameters
7628 if (parameters->tile_size_on) {
7629 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7630 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7632 cp->tdx = image->x1 - cp->tx0;
7633 cp->tdy = image->y1 - cp->ty0;
7636 if (parameters->tp_on) {
7637 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7638 cp->m_specific_param.m_enc.m_tp_on = 1;
7643 calculate JPWL encoding parameters
7646 if (parameters->jpwl_epc_on) {
7650 cp->epc_on = OPJ_TRUE;
7651 cp->info_on = OPJ_FALSE; /* no informative technique */
7654 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7655 cp->epb_on = OPJ_TRUE;
7657 cp->hprot_MH = parameters->jpwl_hprot_MH;
7658 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7659 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7660 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7662 /* if tile specs are not specified, copy MH specs */
7663 if (cp->hprot_TPH[0] == -1) {
7664 cp->hprot_TPH_tileno[0] = 0;
7665 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7667 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7668 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7669 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7670 cp->pprot[i] = parameters->jpwl_pprot[i];
7674 /* set ESD writing */
7675 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7676 cp->esd_on = OPJ_TRUE;
7678 cp->sens_size = parameters->jpwl_sens_size;
7679 cp->sens_addr = parameters->jpwl_sens_addr;
7680 cp->sens_range = parameters->jpwl_sens_range;
7682 cp->sens_MH = parameters->jpwl_sens_MH;
7683 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7684 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7685 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7689 /* always set RED writing to false: we are at the encoder */
7690 cp->red_on = OPJ_FALSE;
7693 cp->epc_on = OPJ_FALSE;
7695 #endif /* USE_JPWL */
7698 /* initialize the mutiple tiles */
7699 /* ---------------------------- */
7700 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7701 if (parameters->numpocs) {
7702 /* initialisation of POC */
7703 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7707 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7708 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7709 tcp->numlayers = parameters->tcp_numlayers;
7711 for (j = 0; j < tcp->numlayers; j++) {
7712 if(cp->m_specific_param.m_enc.m_cinema){
7713 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7714 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7716 tcp->rates[j] = parameters->tcp_rates[j];
7718 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7719 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7721 tcp->rates[j] = parameters->tcp_rates[j];
7726 tcp->csty = parameters->csty;
7727 tcp->prg = parameters->prog_order;
7728 tcp->mct = parameters->tcp_mct;
7733 if (parameters->numpocs) {
7734 /* initialisation of POC */
7737 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7738 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7739 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7741 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7742 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7743 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7744 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7745 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7746 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7747 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7753 tcp->numpocs = numpocs_tile -1 ;
7758 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7760 if (parameters->mct_data) {
7762 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7765 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7766 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7767 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7768 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7771 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7772 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7773 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7775 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7776 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7778 tcp->mct_norms = (OPJ_FLOAT64*)
7779 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7781 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7784 for (i = 0; i < image->numcomps; i++) {
7785 opj_tccp_t *tccp = &tcp->tccps[i];
7786 tccp->m_dc_level_shift = l_dc_shift[i];
7789 j2k_setup_mct_encoding(tcp,image);
7793 for (i = 0; i < image->numcomps; i++) {
7794 opj_tccp_t *tccp = &tcp->tccps[i];
7795 opj_image_comp_t * l_comp = &(image->comps[i]);
7797 if (! l_comp->sgnd) {
7798 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7803 for (i = 0; i < image->numcomps; i++) {
7804 opj_tccp_t *tccp = &tcp->tccps[i];
7806 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7807 tccp->numresolutions = parameters->numresolution;
7808 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7809 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7810 tccp->cblksty = parameters->mode;
7811 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7812 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7815 if (i == parameters->roi_compno) {
7816 tccp->roishift = parameters->roi_shift;
7821 if(parameters->cp_cinema) {
7822 //Precinct size for lowest frequency subband=128
7825 //Precinct size at all other resolutions = 256
7826 for (j = 1; j < tccp->numresolutions; j++) {
7831 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7832 OPJ_INT32 p = 0, it_res;
7833 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7834 if (p < parameters->res_spec) {
7836 if (parameters->prcw_init[p] < 1) {
7837 tccp->prcw[it_res] = 1;
7839 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7842 if (parameters->prch_init[p] < 1) {
7843 tccp->prch[it_res] = 1;
7845 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7849 int res_spec = parameters->res_spec;
7850 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7851 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7853 if (size_prcw < 1) {
7854 tccp->prcw[it_res] = 1;
7856 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7859 if (size_prch < 1) {
7860 tccp->prch[it_res] = 1;
7862 tccp->prch[it_res] = int_floorlog2(size_prch);
7866 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7869 for (j = 0; j < tccp->numresolutions; j++) {
7876 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7880 if (parameters->mct_data) {
7881 opj_free(parameters->mct_data);
7882 parameters->mct_data = 00;
7887 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7890 opj_cp_t *cp = NULL;
7892 opj_tcd_t *tcd = NULL; /* TCD component */
7900 j2k->cstr_info = cstr_info;
7903 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7904 cstr_info->image_w = image->x1 - image->x0;
7905 cstr_info->image_h = image->y1 - image->y0;
7906 cstr_info->prog = (&cp->tcps[0])->prg;
7907 cstr_info->tw = cp->tw;
7908 cstr_info->th = cp->th;
7909 cstr_info->tile_x = cp->tdx; /* new version parser */
7910 cstr_info->tile_y = cp->tdy; /* new version parser */
7911 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7912 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7913 cstr_info->numcomps = image->numcomps;
7914 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7915 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7916 for (compno=0; compno < image->numcomps; compno++) {
7917 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7919 cstr_info->D_max = 0.0; /* ADD Marcela */
7920 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7921 cstr_info->maxmarknum = 100;
7922 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7923 cstr_info->marknum = 0;
7933 for (compno = 1; compno < image->numcomps; compno++) {
7934 j2k_write_coc(j2k, compno);
7935 j2k_write_qcc(j2k, compno);
7939 for (compno = 0; compno < image->numcomps; compno++) {
7940 opj_tcp_t *tcp = &cp->tcps[0];
7941 if (tcp->tccps[compno].roishift)
7942 j2k_write_rgn(j2k, compno, 0);
7944 if (cp->comment != NULL) {
7948 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7952 if (cp->cinema == CINEMA4K_24) {
7957 /* uncomment only for testing JPSEC marker writing */
7958 /* j2k_write_sec(j2k); */
7962 cstr_info->main_head_end = cio_tell(cio) - 1;
7965 /**** Main Header ENDS here ***/
7967 /* create the tile encoder */
7968 tcd = tcd_create(j2k->cinfo);
7970 /* encode each tile */
7971 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7975 int acc_pack_num = 0;
7979 opj_tcp_t *tcp = &cp->tcps[tileno];
7980 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7982 j2k->curtileno = tileno;
7983 j2k->cur_tp_num = 0;
7984 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7985 /* initialisation before tile encoding */
7987 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7989 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7994 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7995 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7996 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7997 cstr_info->tile[j2k->curtileno].marknum = 0;
8001 for(pino = 0; pino <= tcp->numpocs; pino++) {
8005 /*Get number of tile parts*/
8006 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8007 tcd->tp_pos = cp->tp_pos;
8009 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8010 j2k->tp_num = tilepartno;
8013 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8014 cio_tell(cio) + j2k->pos_correction;
8018 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8019 for (compno = 1; compno < image->numcomps; compno++) {
8020 j2k_write_coc(j2k, compno);
8021 j2k_write_qcc(j2k, compno);
8023 if (cp->tcps[tileno].numpocs) {
8030 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8031 cio_tell(cio) + j2k->pos_correction + 1;
8034 j2k_write_sod(j2k, tcd);
8038 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8039 cio_tell(cio) + j2k->pos_correction - 1;
8040 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8042 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8043 cstr_info->packno - acc_pack_num;
8044 acc_pack_num = cstr_info->packno;
8052 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8057 if (tile->PPT) { // BAD PPT !!!
8060 PPT_file=fopen("PPT","rb");
8061 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8062 for (i=0;i<tile->len_ppt;i++) {
8064 fread(&elmt, 1, 1, PPT_file);
8065 fwrite(&elmt,1,1,f);
8074 /* destroy the tile encoder */
8075 tcd_free_encode(tcd);
8078 opj_free(j2k->cur_totnum_tp);
8083 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8085 /* The following adjustment is done to adjust the codestream size */
8086 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8087 /* the first bunch of bytes is not in the codestream */
8088 cstr_info->codestream_size -= cstr_info->main_head_start;
8094 preparation of JPWL marker segments
8098 /* encode according to JPWL */
8099 jpwl_encode(j2k, cio, image);
8102 #endif /* USE_JPWL */
8107 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8109 assert(cstr_info != 00);
8111 /* expand the list? */
8112 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8113 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8114 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8117 /* add the marker */
8118 cstr_info->marker[cstr_info->marknum].type = type;
8119 cstr_info->marker[cstr_info->marknum].pos = pos;
8120 cstr_info->marker[cstr_info->marknum].len = len;
8121 cstr_info->marknum++;
8125 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8127 assert(cstr_index != 00);
8129 /* expand the list? */
8130 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8131 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8132 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8135 /* add the marker */
8136 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8137 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8138 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8139 cstr_index->marknum++;
8143 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8145 opj_marker_info_t *marker;
8147 assert(cstr_info != 00);
8149 /* expand the list? */
8150 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8151 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8152 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8155 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8157 /* add the marker */
8158 marker->type = type;
8161 cstr_info->tile[tileno].marknum++;
8164 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)
8166 assert(cstr_index != 00);
8167 assert(cstr_index->tile_index != 00);
8169 /* expand the list? */
8170 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8171 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8172 cstr_index->tile_index[tileno].marker =
8173 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8174 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8177 /* add the marker */
8178 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8179 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8180 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8181 cstr_index->tile_index[tileno].marknum++;
8183 if (type == J2K_MS_SOT) {
8184 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8186 if (cstr_index->tile_index[tileno].tp_index)
8187 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8194 * -----------------------------------------------------------------------
8195 * -----------------------------------------------------------------------
8196 * -----------------------------------------------------------------------
8200 * Ends the decompression procedures and possibiliy add data to be read after the
8203 opj_bool j2k_end_decompress(
8204 opj_j2k_v2_t *p_j2k,
8205 opj_stream_private_t *p_stream,
8206 opj_event_mgr_t * p_manager)
8215 * Reads a jpeg2000 codestream header structure.
8218 * @param p_stream the stream to read data from.
8219 * @param p_j2k the jpeg2000 codec.
8220 * @param p_manager the user event manager.
8222 * @return true if the box is valid.
8224 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8225 opj_j2k_v2_t* p_j2k,
8226 opj_image_t** p_image,
8227 struct opj_event_mgr* p_manager )
8230 assert(p_j2k != 00);
8231 assert(p_stream != 00);
8232 assert(p_manager != 00);
8234 /* create an empty image header */
8235 p_j2k->m_private_image = opj_image_create0();
8236 if (! p_j2k->m_private_image) {
8240 /* customization of the validation */
8241 j2k_setup_decoding_validation(p_j2k);
8243 /* validation of the parameters codec */
8244 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8245 opj_image_destroy(p_j2k->m_private_image);
8246 p_j2k->m_private_image = NULL;
8250 /* customization of the encoding */
8251 j2k_setup_header_reading(p_j2k);
8254 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8255 opj_image_destroy(p_j2k->m_private_image);
8256 p_j2k->m_private_image = NULL;
8260 *p_image = opj_image_create0();
8265 /* Copy codestream image information to the output image */
8266 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8268 /*Allocate and initialize some elements of codestrem index*/
8269 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8277 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8279 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8282 assert(p_j2k != 00);
8284 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8286 /* DEVELOPER CORNER, add your custom procedures */
8287 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8292 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8293 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8295 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8298 assert(p_j2k != 00);
8300 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8301 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8302 /* DEVELOPER CORNER, add your custom validation procedure */
8308 * The mct encoding validation procedure.
8310 * @param p_j2k the jpeg2000 codec to validate.
8311 * @param p_stream the input stream to validate.
8312 * @param p_manager the user event manager.
8314 * @return true if the parameters are correct.
8316 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8317 opj_stream_private_t *p_stream,
8318 opj_event_mgr_t * p_manager )
8320 opj_bool l_is_valid = OPJ_TRUE;
8324 assert(p_j2k != 00);
8325 assert(p_stream != 00);
8326 assert(p_manager != 00);
8328 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8329 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8330 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8332 for (i=0;i<l_nb_tiles;++i) {
8333 if (l_tcp->mct == 2) {
8334 opj_tccp_t * l_tccp = l_tcp->tccps;
8335 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8337 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8338 l_is_valid &= ! (l_tccp->qmfbid & 1);
8349 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8352 OPJ_UINT32 l_indix = 1;
8353 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8354 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8355 OPJ_UINT32 l_mct_size,l_nb_elem;
8356 OPJ_FLOAT32 * l_data, * l_current_data;
8357 opj_tccp_t * l_tccp;
8360 assert(p_tcp != 00);
8362 if (p_tcp->mct != 2) {
8366 if (p_tcp->m_mct_decoding_matrix) {
8367 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8368 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8370 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));
8371 if (! p_tcp->m_mct_records) {
8374 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8376 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8378 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8380 if (l_mct_deco_data->m_data) {
8381 opj_free(l_mct_deco_data->m_data);
8382 l_mct_deco_data->m_data = 00;
8385 l_mct_deco_data->m_index = l_indix++;
8386 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8387 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8388 l_nb_elem = p_image->numcomps * p_image->numcomps;
8389 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8390 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8392 if (! l_mct_deco_data->m_data) {
8396 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);
8398 l_mct_deco_data->m_data_size = l_mct_size;
8399 ++p_tcp->m_nb_mct_records;
8402 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8403 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8404 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));
8406 if (! p_tcp->m_mct_records) {
8410 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8411 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8413 if (l_mct_deco_data) {
8414 l_mct_deco_data = l_mct_offset_data - 1;
8418 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8420 if (l_mct_offset_data->m_data) {
8421 opj_free(l_mct_offset_data->m_data);
8422 l_mct_offset_data->m_data = 00;
8425 l_mct_offset_data->m_index = l_indix++;
8426 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8427 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8428 l_nb_elem = p_image->numcomps;
8429 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8430 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8432 if (! l_mct_offset_data->m_data) {
8436 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8438 opj_free(l_mct_offset_data->m_data);
8439 l_mct_offset_data->m_data = 00;
8443 l_tccp = p_tcp->tccps;
8444 l_current_data = l_data;
8446 for (i=0;i<l_nb_elem;++i) {
8447 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8451 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8455 l_mct_offset_data->m_data_size = l_mct_size;
8457 ++p_tcp->m_nb_mct_records;
8459 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8460 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8461 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8462 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8464 if (! p_tcp->m_mcc_records) {
8467 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8468 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));
8472 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8473 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8474 l_mcc_data->m_is_irreversible = 1;
8475 l_mcc_data->m_nb_comps = p_image->numcomps;
8476 l_mcc_data->m_index = l_indix++;
8477 l_mcc_data->m_offset_array = l_mct_offset_data;
8478 ++p_tcp->m_nb_mcc_records;
8484 * Builds the cp decoder parameters to use to decode tile.
8486 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8487 opj_stream_private_t *p_stream,
8488 opj_event_mgr_t * p_manager )
8490 /* add here initialization of cp
8491 copy paste of setup_decoder */
8499 * Builds the cp encoder parameters to use to encode tile.
8501 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8502 opj_stream_private_t *p_stream,
8503 opj_event_mgr_t * p_manager )
8505 /* add here initialization of cp
8506 copy paste of setup_encoder */
8514 * The default encoding validation procedure without any extension.
8516 * @param p_j2k the jpeg2000 codec to validate.
8517 * @param p_stream the input stream to validate.
8518 * @param p_manager the user event manager.
8520 * @return true if the parameters are correct.
8522 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8523 opj_stream_private_t *p_stream,
8524 opj_event_mgr_t * p_manager )
8526 opj_bool l_is_valid = OPJ_TRUE;
8529 assert(p_j2k != 00);
8530 assert(p_stream != 00);
8531 assert(p_manager != 00);
8533 /* STATE checking */
8534 /* make sure the state is at 0 */
8535 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8537 /* POINTER validation */
8538 /* make sure a p_j2k codec is present */
8539 l_is_valid &= (p_j2k->m_procedure_list != 00);
8540 /* make sure a validation list is present */
8541 l_is_valid &= (p_j2k->m_validation_list != 00);
8543 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8544 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8548 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8549 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8553 /* PARAMETER VALIDATION */
8558 * The default decoding validation procedure without any extension.
8560 * @param p_j2k the jpeg2000 codec to validate.
8561 * @param p_stream the input stream to validate.
8562 * @param p_manager the user event manager.
8564 * @return true if the parameters are correct.
8566 opj_bool j2k_decoding_validation (
8567 opj_j2k_v2_t *p_j2k,
8568 opj_stream_private_t *p_stream,
8569 opj_event_mgr_t * p_manager
8572 opj_bool l_is_valid = OPJ_TRUE;
8575 assert(p_j2k != 00);
8576 assert(p_stream != 00);
8577 assert(p_manager != 00);
8580 /* STATE checking */
8581 /* make sure the state is at 0 */
8583 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8585 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8587 /* POINTER validation */
8588 /* make sure a p_j2k codec is present */
8589 /* make sure a procedure list is present */
8590 l_is_valid &= (p_j2k->m_procedure_list != 00);
8591 /* make sure a validation list is present */
8592 l_is_valid &= (p_j2k->m_validation_list != 00);
8594 /* PARAMETER VALIDATION */
8598 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8599 struct opj_stream_private *p_stream,
8600 struct opj_event_mgr * p_manager)
8602 OPJ_UINT32 l_current_marker;
8603 OPJ_UINT32 l_marker_size;
8604 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8607 assert(p_stream != 00);
8608 assert(p_j2k != 00);
8609 assert(p_manager != 00);
8611 /* We enter in the main header */
8612 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8614 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8615 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8616 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8620 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8621 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8622 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8626 /* Read 2 bytes as the new marker ID */
8627 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8629 /* Try to read until the SOT is detected */
8630 while (l_current_marker != J2K_MS_SOT) {
8632 /* Check if the current marker ID is valid */
8633 if (l_current_marker < 0xff00) {
8634 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8638 /* Get the marker handler from the marker ID */
8639 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8641 /* Manage case where marker is unknown */
8642 if (l_marker_handler->id == J2K_MS_UNK) {
8643 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8644 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8648 if (l_current_marker == J2K_MS_SOT)
8649 break; /* SOT marker is detected main header is completely read */
8650 else /* Get the marker handler from the marker ID */
8651 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8654 /* Check if the marker is known and if it is the right place to find it */
8655 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8656 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8660 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8661 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8662 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8666 /* read 2 bytes as the marker size */
8667 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8668 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8670 /* Check if the marker size is compatible with the header data size */
8671 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8672 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8673 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8674 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8677 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8680 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8681 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) {
8682 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8686 /* Read the marker segment with the correct marker handler */
8687 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8688 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8692 /* Add the marker to the codestream index*/
8693 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8694 l_marker_handler->id,
8695 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8696 l_marker_size + 4 );
8698 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8699 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8700 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8704 /* read 2 bytes as the new marker ID */
8705 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8708 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8710 /* Position of the last element if the main header */
8711 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8713 /* Next step: read a tile-part header */
8714 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8720 * Excutes the given procedures on the given codec.
8722 * @param p_procedure_list the list of procedures to execute
8723 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8724 * @param p_stream the stream to execute the procedures on.
8725 * @param p_manager the user manager.
8727 * @return true if all the procedures were successfully executed.
8729 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8730 opj_procedure_list_t * p_procedure_list,
8731 opj_stream_private_t *p_stream,
8732 opj_event_mgr_t * p_manager )
8734 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8735 opj_bool l_result = OPJ_TRUE;
8736 OPJ_UINT32 l_nb_proc, i;
8739 assert(p_procedure_list != 00);
8740 assert(p_j2k != 00);
8741 assert(p_stream != 00);
8742 assert(p_manager != 00);
8745 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8746 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);
8748 for (i=0;i<l_nb_proc;++i) {
8749 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8753 /* and clear the procedure list at the end.*/
8754 opj_procedure_list_clear(p_procedure_list);
8759 opj_bool j2k_copy_default_tcp_and_create_tcd
8761 opj_j2k_v2_t * p_j2k,
8762 opj_stream_private_t *p_stream,
8763 opj_event_mgr_t * p_manager
8766 opj_tcp_v2_t * l_tcp = 00;
8767 opj_tcp_v2_t * l_default_tcp = 00;
8768 OPJ_UINT32 l_nb_tiles;
8770 opj_tccp_t *l_current_tccp = 00;
8771 OPJ_UINT32 l_tccp_size;
8772 OPJ_UINT32 l_mct_size;
8773 opj_image_t * l_image;
8774 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8775 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8776 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8777 OPJ_UINT32 l_offset;
8780 assert(p_j2k != 00);
8781 assert(p_stream != 00);
8782 assert(p_manager != 00);
8784 l_image = p_j2k->m_private_image;
8785 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8786 l_tcp = p_j2k->m_cp.tcps;
8787 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8788 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8789 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8792 for (i=0; i<l_nb_tiles; ++i) {
8793 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8794 l_current_tccp = l_tcp->tccps;
8795 /*Copy default coding parameters into the current tile coding parameters*/
8796 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8797 /* Initialize some values of the current tile coding parameters*/
8799 l_tcp->ppt_data = 00;
8800 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8801 l_tcp->tccps = l_current_tccp;
8803 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8804 if (l_default_tcp->m_mct_decoding_matrix) {
8805 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8806 if (! l_tcp->m_mct_decoding_matrix ) {
8809 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8812 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8813 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8814 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8815 if (! l_tcp->m_mct_records) {
8818 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8820 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8821 l_src_mct_rec = l_default_tcp->m_mct_records;
8822 l_dest_mct_rec = l_tcp->m_mct_records;
8824 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8826 if (l_src_mct_rec->m_data) {
8828 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8829 if(! l_dest_mct_rec->m_data) {
8832 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8839 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8840 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8841 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8842 if (! l_tcp->m_mcc_records) {
8845 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8847 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8848 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8849 l_dest_mcc_rec = l_tcp->m_mcc_records;
8851 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8853 if (l_src_mcc_rec->m_decorrelation_array) {
8854 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8855 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8858 if (l_src_mcc_rec->m_offset_array) {
8859 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8860 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8867 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8868 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8870 /* Move to next tile cp*/
8874 /* Create the current tile decoder*/
8875 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8876 if (! p_j2k->m_tcd ) {
8880 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8881 tcd_destroy_v2(p_j2k->m_tcd);
8883 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8891 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8892 * with the marker value.
8893 * @param p_id Marker value to look up
8895 * @return the handler associated with the id.
8897 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8899 const opj_dec_memory_marker_handler_t *e;
8900 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8901 if (e->id == p_id) {
8902 break; /* we find a handler corresponding to the marker ID*/
8910 * Destroys a jpeg2000 codec.
8912 * @param p_j2k the jpeg20000 structure to destroy.
8914 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8920 if (p_j2k->m_is_decoder) {
8922 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8923 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8924 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8925 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8928 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8929 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8930 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8931 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8936 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8937 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8938 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8941 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8942 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8943 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8944 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8947 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8948 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8949 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8950 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8954 tcd_destroy_v2(p_j2k->m_tcd);
8956 j2k_cp_destroy(&(p_j2k->m_cp));
8957 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8959 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8960 p_j2k->m_procedure_list = 00;
8962 opj_procedure_list_destroy(p_j2k->m_validation_list);
8963 p_j2k->m_procedure_list = 00;
8965 j2k_destroy_cstr_index(p_j2k->cstr_index);
8966 p_j2k->cstr_index = NULL;
8968 opj_image_destroy(p_j2k->m_private_image);
8969 p_j2k->m_private_image = NULL;
8971 opj_image_destroy(p_j2k->m_output_image);
8972 p_j2k->m_output_image = NULL;
8977 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8981 if (p_cstr_ind->marker) {
8982 opj_free(p_cstr_ind->marker);
8983 p_cstr_ind->marker = NULL;
8986 if (p_cstr_ind->tile_index) {
8987 OPJ_UINT32 it_tile = 0;
8989 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8991 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8992 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8993 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8996 if(p_cstr_ind->tile_index[it_tile].tp_index){
8997 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8998 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9001 if(p_cstr_ind->tile_index[it_tile].marker){
9002 opj_free(p_cstr_ind->tile_index[it_tile].marker);
9003 p_cstr_ind->tile_index[it_tile].marker = NULL;
9008 opj_free( p_cstr_ind->tile_index);
9009 p_cstr_ind->tile_index = NULL;
9012 opj_free(p_cstr_ind);
9019 * Destroys a tile coding parameter structure.
9021 * @param p_tcp the tile coding parameter to destroy.
9023 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9029 if (p_tcp->ppt_buffer != 00) {
9030 opj_free(p_tcp->ppt_buffer);
9031 p_tcp->ppt_buffer = 00;
9034 if (p_tcp->tccps != 00) {
9035 opj_free(p_tcp->tccps);
9039 if (p_tcp->m_mct_coding_matrix != 00) {
9040 opj_free(p_tcp->m_mct_coding_matrix);
9041 p_tcp->m_mct_coding_matrix = 00;
9044 if (p_tcp->m_mct_decoding_matrix != 00) {
9045 opj_free(p_tcp->m_mct_decoding_matrix);
9046 p_tcp->m_mct_decoding_matrix = 00;
9049 if (p_tcp->m_mcc_records) {
9050 opj_free(p_tcp->m_mcc_records);
9051 p_tcp->m_mcc_records = 00;
9052 p_tcp->m_nb_max_mcc_records = 0;
9053 p_tcp->m_nb_mcc_records = 0;
9056 if (p_tcp->m_mct_records) {
9057 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9060 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9061 if (l_mct_data->m_data) {
9062 opj_free(l_mct_data->m_data);
9063 l_mct_data->m_data = 00;
9069 opj_free(p_tcp->m_mct_records);
9070 p_tcp->m_mct_records = 00;
9073 if (p_tcp->mct_norms != 00) {
9074 opj_free(p_tcp->mct_norms);
9075 p_tcp->mct_norms = 00;
9078 j2k_tcp_data_destroy(p_tcp);
9083 * Destroys the data inside a tile coding parameter structure.
9085 * @param p_tcp the tile coding parameter which contain data to destroy.
9087 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9089 if (p_tcp->m_data) {
9090 opj_free(p_tcp->m_data);
9091 p_tcp->m_data = NULL;
9092 p_tcp->m_data_size = 0;
9097 * Destroys a coding parameter structure.
9099 * @param p_cp the coding parameter to destroy.
9101 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9103 OPJ_UINT32 l_nb_tiles;
9104 opj_tcp_v2_t * l_current_tile = 00;
9115 l_current_tile = p_cp->tcps;
9116 l_nb_tiles = p_cp->th * p_cp->tw;
9119 (i = 0; i < l_nb_tiles; ++i)
9121 j2k_tcp_destroy(l_current_tile);
9124 opj_free(p_cp->tcps);
9128 (p_cp->ppm_buffer != 00)
9130 opj_free(p_cp->ppm_buffer);
9131 p_cp->ppm_buffer = 00;
9134 (p_cp->comment != 00)
9136 opj_free(p_cp->comment);
9140 (! p_cp->m_is_decoder)
9143 (p_cp->m_specific_param.m_enc.m_matrice)
9145 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9146 p_cp->m_specific_param.m_enc.m_matrice = 00;
9154 * Reads a tile header.
9155 * @param p_j2k the jpeg2000 codec.
9156 * @param p_stream the stream to write data to.
9157 * @param p_manager the user event manager.
9159 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9160 OPJ_UINT32 * p_tile_index,
9161 OPJ_UINT32 * p_data_size,
9162 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9163 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9164 OPJ_UINT32 * p_nb_comps,
9166 opj_stream_private_t *p_stream,
9167 opj_event_mgr_t * p_manager )
9169 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9170 OPJ_UINT32 l_marker_size;
9171 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9172 opj_tcp_v2_t * l_tcp = NULL;
9173 OPJ_UINT32 l_nb_tiles;
9176 assert(p_stream != 00);
9177 assert(p_j2k != 00);
9178 assert(p_manager != 00);
9180 /* Reach the End Of Codestream ?*/
9181 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9182 l_current_marker = J2K_MS_EOC;
9184 /* We need to encounter a SOT marker (a new tile-part header) */
9185 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9189 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9190 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9192 /* Try to read until the Start Of Data is detected */
9193 while (l_current_marker != J2K_MS_SOD) {
9195 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9196 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9197 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9201 /* Read 2 bytes from the buffer as the marker size */
9202 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9204 /* Why this condition? FIXME */
9205 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9206 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9208 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9210 /* Get the marker handler from the marker ID */
9211 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9213 /* Check if the marker is known and if it is the right place to find it */
9214 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9215 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9218 /* FIXME manage case of unknown marker as in the main header ? */
9220 /* Check if the marker size is compatible with the header data size */
9221 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9222 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9223 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9224 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9227 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9230 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9231 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) {
9232 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9236 /* Read the marker segment with the correct marker handler */
9237 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9238 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9242 /* Add the marker to the codestream index*/
9243 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9245 l_marker_handler->id,
9246 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9247 l_marker_size + 4 );
9249 /* Keep the position of the last SOT marker read */
9250 if ( l_marker_handler->id == J2K_MS_SOT ) {
9251 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9252 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9254 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9259 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9260 /* Skip the rest of the tile part header*/
9261 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) {
9262 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9265 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9268 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9269 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9270 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9273 /* Read 2 bytes from the buffer as the new marker ID */
9274 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9278 /* If we didn't skip data before, we need to read the SOD marker*/
9279 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9280 /* Try to read the SOD marker and skip data ? FIXME */
9281 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
9287 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9288 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9289 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9290 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9294 /* Read 2 bytes from buffer as the new marker ID */
9295 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9299 /* Indicate we will try to read a new tile-part header*/
9300 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9301 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9302 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9304 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9305 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9306 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9310 /* Read 2 bytes from buffer as the new marker ID */
9311 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9315 /* Current marker is the EOC marker ?*/
9316 if (l_current_marker == J2K_MS_EOC) {
9317 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9318 p_j2k->m_current_tile_number = 0;
9319 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9324 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9325 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9326 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9328 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9329 ++p_j2k->m_current_tile_number;
9333 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9334 *p_go_on = OPJ_FALSE;
9340 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9341 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9345 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9346 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9348 *p_tile_index = p_j2k->m_current_tile_number;
9349 *p_go_on = OPJ_TRUE;
9350 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9351 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9352 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9353 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9354 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9355 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9357 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9363 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9364 OPJ_UINT32 p_tile_index,
9366 OPJ_UINT32 p_data_size,
9367 opj_stream_private_t *p_stream,
9368 opj_event_mgr_t * p_manager )
9370 OPJ_UINT32 l_current_marker;
9371 OPJ_BYTE l_data [2];
9372 opj_tcp_v2_t * l_tcp;
9375 assert(p_stream != 00);
9376 assert(p_j2k != 00);
9377 assert(p_manager != 00);
9379 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9380 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9384 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9385 if (! l_tcp->m_data) {
9386 j2k_tcp_destroy(l_tcp);
9390 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9394 p_j2k->cstr_index) ) {
9395 j2k_tcp_destroy(l_tcp);
9396 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9400 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9404 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9405 * we destroy just the data which will be re-read in read_tile_header*/
9406 /*j2k_tcp_destroy(l_tcp);
9407 p_j2k->m_tcd->tcp = 0;*/
9408 j2k_tcp_data_destroy(l_tcp);
9410 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9411 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9413 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9414 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9415 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9419 opj_read_bytes(l_data,&l_current_marker,2);
9421 if (l_current_marker == J2K_MS_EOC) {
9422 p_j2k->m_current_tile_number = 0;
9423 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9425 else if (l_current_marker != J2K_MS_SOT)
9427 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9436 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9438 OPJ_UINT32 i,j,k = 0;
9439 OPJ_UINT32 l_width_src,l_height_src;
9440 OPJ_UINT32 l_width_dest,l_height_dest;
9441 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9442 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9443 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9444 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9445 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9447 opj_image_comp_t * l_img_comp_src = 00;
9448 opj_image_comp_t * l_img_comp_dest = 00;
9450 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9451 opj_image_t * l_image_src = 00;
9452 OPJ_UINT32 l_size_comp, l_remaining;
9453 OPJ_INT32 * l_dest_ptr;
9454 opj_tcd_resolution_v2_t* l_res= 00;
9456 l_tilec = p_tcd->tcd_image->tiles->comps;
9457 l_image_src = p_tcd->image;
9458 l_img_comp_src = l_image_src->comps;
9460 l_img_comp_dest = p_output_image->comps;
9462 for (i=0; i<l_image_src->numcomps; i++) {
9464 /* Allocate output component buffer if necessary */
9465 if (!l_img_comp_dest->data) {
9467 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9468 if (! l_img_comp_dest->data) {
9473 /* Copy info from decoded comp image to output image */
9474 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9477 /* Compute the precision of the output buffer */
9478 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9479 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9480 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9486 if (l_size_comp == 3) {
9491 /* Current tile component size*/
9493 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9494 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9497 l_width_src = (l_res->x1 - l_res->x0);
9498 l_height_src = (l_res->y1 - l_res->y0);
9500 /* Border of the current output component*/
9501 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9502 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9503 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9504 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9507 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9508 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9512 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9513 * of the input buffer (decoded tile component) which will be move
9514 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9515 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9516 * by this input area.
9518 assert( l_res->x0 >= 0);
9519 assert( l_res->x1 >= 0);
9520 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9521 l_start_x_dest = l_res->x0 - l_x0_dest;
9522 l_offset_x0_src = 0;
9524 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9525 l_width_dest = l_width_src;
9526 l_offset_x1_src = 0;
9529 l_width_dest = l_x1_dest - l_res->x0 ;
9530 l_offset_x1_src = l_width_src - l_width_dest;
9534 l_start_x_dest = 0 ;
9535 l_offset_x0_src = l_x0_dest - l_res->x0;
9537 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9538 l_width_dest = l_width_src - l_offset_x0_src;
9539 l_offset_x1_src = 0;
9542 l_width_dest = l_img_comp_dest->w ;
9543 l_offset_x1_src = l_res->x1 - l_x1_dest;
9547 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9548 l_start_y_dest = l_res->y0 - l_y0_dest;
9549 l_offset_y0_src = 0;
9551 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9552 l_height_dest = l_height_src;
9553 l_offset_y1_src = 0;
9556 l_height_dest = l_y1_dest - l_res->y0 ;
9557 l_offset_y1_src = l_height_src - l_height_dest;
9561 l_start_y_dest = 0 ;
9562 l_offset_y0_src = l_y0_dest - l_res->y0;
9564 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9565 l_height_dest = l_height_src - l_offset_y0_src;
9566 l_offset_y1_src = 0;
9569 l_height_dest = l_img_comp_dest->h ;
9570 l_offset_y1_src = l_res->y1 - l_y1_dest;
9574 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9579 /* Compute the input buffer offset */
9580 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9581 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9582 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9584 /* Compute the output buffer offset */
9585 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9586 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9588 /* Move the output buffer to the first place where we will write*/
9589 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9592 fprintf(stdout, "COMPO[%d]:\n",i);
9593 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9594 "\t tile offset:%d, %d, %d, %d\n"
9595 "\t buffer offset: %d; %d, %d\n",
9596 l_res->x0, l_res->y0, l_width_src, l_height_src,
9597 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9598 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9600 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9601 "\t start offset: %d, line offset= %d\n",
9602 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9606 switch (l_size_comp) {
9609 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9610 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9612 if (l_img_comp_src->sgnd) {
9613 for (j = 0 ; j < l_height_dest ; ++j) {
9614 for ( k = 0 ; k < l_width_dest ; ++k) {
9615 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9618 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9619 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9623 for ( j = 0 ; j < l_height_dest ; ++j ) {
9624 for ( k = 0 ; k < l_width_dest ; ++k) {
9625 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9628 l_dest_ptr+= l_line_offset_dest;
9629 l_src_ptr += l_line_offset_src;
9633 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9634 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9639 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9640 l_src_ptr += l_start_offset_src;
9642 if (l_img_comp_src->sgnd) {
9643 for (j=0;j<l_height_dest;++j) {
9644 for (k=0;k<l_width_dest;++k) {
9645 *(l_dest_ptr++) = *(l_src_ptr++);
9648 l_dest_ptr+= l_line_offset_dest;
9649 l_src_ptr += l_line_offset_src ;
9653 for (j=0;j<l_height_dest;++j) {
9654 for (k=0;k<l_width_dest;++k) {
9655 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9658 l_dest_ptr+= l_line_offset_dest;
9659 l_src_ptr += l_line_offset_src ;
9663 l_src_ptr += l_end_offset_src;
9664 p_data = (OPJ_BYTE*) l_src_ptr;
9669 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9670 l_src_ptr += l_start_offset_src;
9672 for (j=0;j<l_height_dest;++j) {
9673 for (k=0;k<l_width_dest;++k) {
9674 *(l_dest_ptr++) = (*(l_src_ptr++));
9677 l_dest_ptr+= l_line_offset_dest;
9678 l_src_ptr += l_line_offset_src ;
9681 l_src_ptr += l_end_offset_src;
9682 p_data = (OPJ_BYTE*) l_src_ptr;
9696 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9698 * @param p_j2k the jpeg2000 codec.
9699 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9700 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9701 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9702 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9703 * @param p_manager the user event manager
9705 * @return true if the area could be set.
9707 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9708 opj_image_t* p_image,
9709 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9710 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9711 struct opj_event_mgr * p_manager )
9713 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9714 opj_image_t * l_image = p_j2k->m_private_image;
9717 OPJ_INT32 l_comp_x1, l_comp_y1;
9718 opj_image_comp_t* l_img_comp = NULL;
9720 /* Check if we are read the main header */
9721 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9722 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9726 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9727 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9729 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9730 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9731 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9732 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9738 /* Check if the positions provided by the user are correct */
9741 assert(p_start_x >= 0 );
9742 assert(p_start_y >= 0 );
9744 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9745 opj_event_msg_v2(p_manager, EVT_ERROR,
9746 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9747 p_start_x, l_image->x1);
9750 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9751 opj_event_msg_v2(p_manager, EVT_WARNING,
9752 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9753 p_start_x, l_image->x0);
9754 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9755 p_image->x0 = l_image->x0;
9758 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9759 p_image->x0 = p_start_x;
9763 if ((OPJ_UINT32)p_start_y > l_image->y1){
9764 opj_event_msg_v2(p_manager, EVT_ERROR,
9765 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9766 p_start_y, l_image->y1);
9769 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9770 opj_event_msg_v2(p_manager, EVT_WARNING,
9771 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9772 p_start_y, l_image->y0);
9773 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9774 p_image->y0 = l_image->y0;
9777 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9778 p_image->y0 = p_start_y;
9782 assert((OPJ_UINT32)p_end_x > 0);
9783 assert((OPJ_UINT32)p_end_y > 0);
9784 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9785 opj_event_msg_v2(p_manager, EVT_ERROR,
9786 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9787 p_end_x, l_image->x0);
9790 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9791 opj_event_msg_v2(p_manager, EVT_WARNING,
9792 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9793 p_end_x, l_image->x1);
9794 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9795 p_image->x1 = l_image->x1;
9798 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9799 p_image->x1 = p_end_x;
9803 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9804 opj_event_msg_v2(p_manager, EVT_ERROR,
9805 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9806 p_end_y, l_image->y0);
9809 if ((OPJ_UINT32)p_end_y > l_image->y1){
9810 opj_event_msg_v2(p_manager, EVT_WARNING,
9811 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9812 p_end_y, l_image->y1);
9813 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9814 p_image->y1 = l_image->y1;
9817 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9818 p_image->y1 = p_end_y;
9822 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9824 l_img_comp = p_image->comps;
9825 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9829 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9830 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9831 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9832 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9834 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9835 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9837 opj_event_msg_v2(p_manager, EVT_ERROR,
9838 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9842 l_img_comp->w = l_w;
9844 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9845 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9847 opj_event_msg_v2(p_manager, EVT_ERROR,
9848 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9852 l_img_comp->h = l_h;
9857 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9858 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9865 /* ----------------------------------------------------------------------- */
9866 /* J2K / JPT decoder interface */
9867 /* ----------------------------------------------------------------------- */
9869 * Creates a J2K decompression structure.
9871 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9873 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9875 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9879 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9881 l_j2k->m_is_decoder = 1;
9882 l_j2k->m_cp.m_is_decoder = 1;
9884 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9885 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9889 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9891 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9892 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9897 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9899 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9901 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9903 /* codestream index creation */
9904 l_j2k->cstr_index = j2k_create_cstr_index();
9906 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9907 if (!l_j2k->cstr_index){
9912 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9915 /* validation list creation */
9916 l_j2k->m_validation_list = opj_procedure_list_create();
9917 if (! l_j2k->m_validation_list) {
9922 /* execution list creation */
9923 l_j2k->m_procedure_list = opj_procedure_list_create();
9924 if (! l_j2k->m_procedure_list) {
9933 opj_codestream_index_t* j2k_create_cstr_index(void)
9935 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9936 opj_calloc(1,sizeof(opj_codestream_index_t));
9940 cstr_index->maxmarknum = 100;
9941 cstr_index->marknum = 0;
9942 cstr_index->marker = (opj_marker_info_t*)
9943 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9944 if (!cstr_index-> marker)
9947 cstr_index->tile_index = NULL;
9954 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9956 * @param p_tile_no the tile index.
9957 * @param p_comp_no the component being outputted.
9958 * @param p_j2k the J2K codec.
9960 * @return the number of bytes taken by the SPCod element.
9962 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9963 OPJ_UINT32 p_tile_no,
9964 OPJ_UINT32 p_comp_no )
9966 opj_cp_v2_t *l_cp = 00;
9967 opj_tcp_v2_t *l_tcp = 00;
9968 opj_tccp_t *l_tccp = 00;
9971 assert(p_j2k != 00);
9973 l_cp = &(p_j2k->m_cp);
9974 l_tcp = &l_cp->tcps[p_tile_no];
9975 l_tccp = &l_tcp->tccps[p_comp_no];
9977 /* preconditions again */
9978 assert(p_tile_no < (l_cp->tw * l_cp->th));
9979 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9981 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9982 return 5 + l_tccp->numresolutions;
9990 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9992 * @param p_comp_no the component number to output.
9993 * @param p_stream the stream to write data to.
9994 * @param p_j2k J2K codec.
9995 * @param p_manager the user event manager.
9998 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9999 OPJ_UINT32 p_tile_no,
10000 OPJ_UINT32 p_comp_no,
10002 OPJ_UINT32 * p_header_size,
10003 struct opj_event_mgr * p_manager )
10006 opj_cp_v2_t *l_cp = 00;
10007 opj_tcp_v2_t *l_tcp = 00;
10008 opj_tccp_t *l_tccp = 00;
10010 /* preconditions */
10011 assert(p_j2k != 00);
10012 assert(p_header_size != 00);
10013 assert(p_manager != 00);
10014 assert(p_data != 00);
10016 l_cp = &(p_j2k->m_cp);
10017 l_tcp = &l_cp->tcps[p_tile_no];
10018 l_tccp = &l_tcp->tccps[p_comp_no];
10020 /* preconditions again */
10021 assert(p_tile_no < (l_cp->tw * l_cp->th));
10022 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10024 if (*p_header_size < 5) {
10025 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10029 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
10032 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
10035 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
10038 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
10041 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
10044 *p_header_size = *p_header_size - 5;
10046 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10048 if (*p_header_size < l_tccp->numresolutions) {
10049 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10053 for (i = 0; i < l_tccp->numresolutions; ++i) {
10054 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
10058 *p_header_size = *p_header_size - l_tccp->numresolutions;
10065 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10066 * @param p_header_data the data contained in the COM box.
10067 * @param p_j2k the jpeg2000 codec.
10068 * @param p_header_size the size of the data contained in the COM marker.
10069 * @param p_manager the user event manager.
10071 opj_bool j2k_read_SPCod_SPCoc(
10072 opj_j2k_v2_t *p_j2k,
10074 OPJ_BYTE * p_header_data,
10075 OPJ_UINT32 * p_header_size,
10076 struct opj_event_mgr * p_manager
10079 OPJ_UINT32 i, l_tmp;
10080 opj_cp_v2_t *l_cp = NULL;
10081 opj_tcp_v2_t *l_tcp = NULL;
10082 opj_tccp_t *l_tccp = NULL;
10083 OPJ_BYTE * l_current_ptr = NULL;
10085 /* preconditions */
10086 assert(p_j2k != 00);
10087 assert(p_manager != 00);
10088 assert(p_header_data != 00);
10090 l_cp = &(p_j2k->m_cp);
10091 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10092 &l_cp->tcps[p_j2k->m_current_tile_number] :
10093 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10095 /* precondition again */
10096 assert(compno < p_j2k->m_private_image->numcomps);
10098 l_tccp = &l_tcp->tccps[compno];
10099 l_current_ptr = p_header_data;
10101 /* make sure room is sufficient */
10102 if (*p_header_size < 5) {
10103 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10107 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10108 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10111 /* If user wants to remove more resolutions than the codestream contains, return error */
10112 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10113 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10114 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10115 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10119 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10121 l_tccp->cblkw += 2;
10123 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10125 l_tccp->cblkh += 2;
10127 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10130 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10133 *p_header_size = *p_header_size - 5;
10135 /* use custom precinct size ? */
10136 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10137 if (*p_header_size < l_tccp->numresolutions) {
10138 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10142 for (i = 0; i < l_tccp->numresolutions; ++i) {
10143 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10145 l_tccp->prcw[i] = l_tmp & 0xf;
10146 l_tccp->prch[i] = l_tmp >> 4;
10149 *p_header_size = *p_header_size - l_tccp->numresolutions;
10152 /* set default size for the precinct width and height */
10153 for (i = 0; i < l_tccp->numresolutions; ++i) {
10154 l_tccp->prcw[i] = 15;
10155 l_tccp->prch[i] = 15;
10159 #ifdef WIP_REMOVE_MSD
10161 if (p_j2k->cstr_info && compno == 0) {
10162 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10164 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10165 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10166 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10167 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10168 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10171 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10172 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10181 * Copies the tile component parameters of all the component from the first tile component.
10183 * @param p_j2k the J2k codec.
10185 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10189 opj_cp_v2_t *l_cp = NULL;
10190 opj_tcp_v2_t *l_tcp = NULL;
10191 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10192 OPJ_UINT32 l_prc_size;
10194 /* preconditions */
10195 assert(p_j2k != 00);
10197 l_cp = &(p_j2k->m_cp);
10198 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10199 &l_cp->tcps[p_j2k->m_current_tile_number] :
10200 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10202 l_ref_tccp = &l_tcp->tccps[0];
10203 l_copied_tccp = l_ref_tccp + 1;
10204 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10206 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10207 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10208 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10209 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10210 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10211 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10212 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10213 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10219 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10221 * @param p_tile_no the tile index.
10222 * @param p_comp_no the component being outputted.
10223 * @param p_j2k the J2K codec.
10225 * @return the number of bytes taken by the SPCod element.
10227 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10228 OPJ_UINT32 p_tile_no,
10229 OPJ_UINT32 p_comp_no )
10231 OPJ_UINT32 l_num_bands;
10233 opj_cp_v2_t *l_cp = 00;
10234 opj_tcp_v2_t *l_tcp = 00;
10235 opj_tccp_t *l_tccp = 00;
10237 /* preconditions */
10238 assert(p_j2k != 00);
10240 l_cp = &(p_j2k->m_cp);
10241 l_tcp = &l_cp->tcps[p_tile_no];
10242 l_tccp = &l_tcp->tccps[p_comp_no];
10244 /* preconditions again */
10245 assert(p_tile_no < l_cp->tw * l_cp->th);
10246 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10248 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10250 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10251 return 1 + l_num_bands;
10254 return 1 + 2*l_num_bands;
10259 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10261 * @param p_tile_no the tile to output.
10262 * @param p_comp_no the component number to output.
10263 * @param p_data the data buffer.
10264 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10265 * @param p_j2k J2K codec.
10266 * @param p_manager the user event manager.
10269 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10270 OPJ_UINT32 p_tile_no,
10271 OPJ_UINT32 p_comp_no,
10273 OPJ_UINT32 * p_header_size,
10274 struct opj_event_mgr * p_manager )
10276 OPJ_UINT32 l_header_size;
10277 OPJ_UINT32 l_band_no, l_num_bands;
10278 OPJ_UINT32 l_expn,l_mant;
10280 opj_cp_v2_t *l_cp = 00;
10281 opj_tcp_v2_t *l_tcp = 00;
10282 opj_tccp_t *l_tccp = 00;
10284 /* preconditions */
10285 assert(p_j2k != 00);
10286 assert(p_header_size != 00);
10287 assert(p_manager != 00);
10288 assert(p_data != 00);
10290 l_cp = &(p_j2k->m_cp);
10291 l_tcp = &l_cp->tcps[p_tile_no];
10292 l_tccp = &l_tcp->tccps[p_comp_no];
10294 /* preconditions again */
10295 assert(p_tile_no < l_cp->tw * l_cp->th);
10296 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10298 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10300 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10301 l_header_size = 1 + l_num_bands;
10303 if (*p_header_size < l_header_size) {
10304 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10308 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10311 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10312 l_expn = l_tccp->stepsizes[l_band_no].expn;
10313 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10318 l_header_size = 1 + 2*l_num_bands;
10320 if (*p_header_size < l_header_size) {
10321 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10325 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10328 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10329 l_expn = l_tccp->stepsizes[l_band_no].expn;
10330 l_mant = l_tccp->stepsizes[l_band_no].mant;
10332 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10337 *p_header_size = *p_header_size - l_header_size;
10343 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10345 * @param p_comp_no the component being targeted.
10346 * @param p_header_data the data contained in the COM box.
10347 * @param p_j2k the jpeg2000 codec.
10348 * @param p_header_size the size of the data contained in the COM marker.
10349 * @param p_manager the user event manager.
10351 opj_bool j2k_read_SQcd_SQcc(
10352 opj_j2k_v2_t *p_j2k,
10353 OPJ_UINT32 p_comp_no,
10354 OPJ_BYTE* p_header_data,
10355 OPJ_UINT32 * p_header_size,
10356 struct opj_event_mgr * p_manager
10360 OPJ_UINT32 l_band_no;
10361 opj_cp_v2_t *l_cp = 00;
10362 opj_tcp_v2_t *l_tcp = 00;
10363 opj_tccp_t *l_tccp = 00;
10364 OPJ_BYTE * l_current_ptr = 00;
10365 OPJ_UINT32 l_tmp, l_num_band;
10368 assert(p_j2k != 00);
10369 assert(p_manager != 00);
10370 assert(p_header_data != 00);
10372 l_cp = &(p_j2k->m_cp);
10373 /* come from tile part header or main header ?*/
10374 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10375 &l_cp->tcps[p_j2k->m_current_tile_number] :
10376 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10378 /* precondition again*/
10379 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10381 l_tccp = &l_tcp->tccps[p_comp_no];
10382 l_current_ptr = p_header_data;
10384 if (*p_header_size < 1) {
10385 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10388 *p_header_size -= 1;
10390 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10393 l_tccp->qntsty = l_tmp & 0x1f;
10394 l_tccp->numgbits = l_tmp >> 5;
10395 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10399 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10401 (*p_header_size) / 2;
10403 if( l_num_band > J2K_MAXBANDS ) {
10404 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10405 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10406 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10407 /*return OPJ_FALSE;*/
10412 if (l_cp->correct) {
10414 /* if JPWL is on, we check whether there are too many subbands */
10415 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10416 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10417 "JPWL: bad number of subbands in Sqcx (%d)\n",
10419 if (!JPWL_ASSUME) {
10420 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10423 /* we try to correct */
10425 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10426 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10431 #endif /* USE_JPWL */
10433 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10434 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10435 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10437 if (l_band_no < J2K_MAXBANDS){
10438 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10439 l_tccp->stepsizes[l_band_no].mant = 0;
10442 *p_header_size = *p_header_size - l_num_band;
10445 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10446 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10448 if (l_band_no < J2K_MAXBANDS){
10449 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10450 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10453 *p_header_size = *p_header_size - 2*l_num_band;
10456 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10457 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10458 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10459 l_tccp->stepsizes[l_band_no].expn =
10460 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10461 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10462 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10470 * Copies the tile component parameters of all the component from the first tile component.
10472 * @param p_j2k the J2k codec.
10474 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10477 opj_cp_v2_t *l_cp = NULL;
10478 opj_tcp_v2_t *l_tcp = NULL;
10479 opj_tccp_t *l_ref_tccp = NULL;
10480 opj_tccp_t *l_copied_tccp = NULL;
10483 /* preconditions */
10484 assert(p_j2k != 00);
10486 l_cp = &(p_j2k->m_cp);
10487 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10488 &l_cp->tcps[p_j2k->m_current_tile_number] :
10489 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10491 l_ref_tccp = &l_tcp->tccps[0];
10492 l_copied_tccp = l_ref_tccp + 1;
10493 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10495 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10496 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10497 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10498 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10504 * Dump some elements from the J2K decompression structure .
10506 *@param p_j2k the jpeg2000 codec.
10507 *@param flag flag to describe what elments are dump.
10508 *@param out_stream output stream where dump the elements.
10511 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10513 /* Check if the flag is compatible with j2k file*/
10514 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10515 fprintf(out_stream, "Wrong flag\n");
10519 /* Dump the image_header */
10520 if (flag & OPJ_IMG_INFO){
10521 if (p_j2k->m_private_image)
10522 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10525 /* Dump the codestream info from main header */
10526 if (flag & OPJ_J2K_MH_INFO){
10527 j2k_dump_MH_info(p_j2k, out_stream);
10531 /* Dump the codestream info of the current tile */
10532 if (flag & OPJ_J2K_TH_INFO){
10536 /* Dump the codestream index from main header */
10537 if (flag & OPJ_J2K_MH_IND){
10538 j2k_dump_MH_index(p_j2k, out_stream);
10541 /* Dump the codestream index of the current tile */
10542 if (flag & OPJ_J2K_TH_IND){
10549 * Dump index elements of the codestream extract from the main header.
10551 *@param p_j2k the jpeg2000 codec.
10552 *@param out_stream output stream where dump the elements.
10555 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10557 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10558 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10560 fprintf(out_stream, "Codestream index from main header: {\n");
10562 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10563 "\t Main header end position=%" PRIi64 "\n",
10564 cstr_index->main_head_start, cstr_index->main_head_end);
10566 fprintf(out_stream, "\t Marker list: {\n");
10568 if (cstr_index->marker){
10569 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10570 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10571 cstr_index->marker[it_marker].type,
10572 cstr_index->marker[it_marker].pos,
10573 cstr_index->marker[it_marker].len );
10577 fprintf(out_stream, "\t }\n");
10580 if (cstr_index->tile_index){
10582 /* Simple test to avoid to write empty information*/
10583 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10584 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10585 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10588 if (l_acc_nb_of_tile_part)
10590 fprintf(out_stream, "\t Tile index: {\n");
10592 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10593 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10595 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10597 if (cstr_index->tile_index[it_tile].tp_index){
10598 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10599 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10601 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10602 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10603 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10607 if (cstr_index->tile_index[it_tile].marker){
10608 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10609 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10610 cstr_index->tile_index[it_tile].marker[it_marker].type,
10611 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10612 cstr_index->tile_index[it_tile].marker[it_marker].len );
10616 fprintf(out_stream,"\t }\n");
10620 fprintf(out_stream,"}\n");
10625 * Dump info elements of the codestream extract from the main header.
10627 *@param p_j2k the jpeg2000 codec.
10628 *@param out_stream output stream where dump the elements.
10631 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10633 opj_tcp_v2_t * l_default_tile=NULL;
10635 fprintf(out_stream, "Codestream info from main header: {\n");
10637 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10638 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10639 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10641 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10642 if (l_default_tile)
10645 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10647 fprintf(out_stream, "\t default tile {\n");
10648 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10649 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10650 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10651 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10653 for (compno = 0; compno < numcomps; compno++) {
10654 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10656 OPJ_INT32 bandno, numbands;
10659 fprintf(out_stream, "\t\t comp %d {\n", compno);
10660 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10661 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10662 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10663 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10664 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10665 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10667 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10668 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10669 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10671 fprintf(out_stream, "\n");
10673 /* quantization style*/
10674 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10675 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10676 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10677 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10678 for (bandno = 0; bandno < numbands; bandno++) {
10679 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10680 l_tccp->stepsizes[bandno].expn);
10682 fprintf(out_stream, "\n");
10685 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10687 fprintf(out_stream, "\t\t }\n");
10688 } /*end of component of default tile*/
10689 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10693 fprintf(out_stream, "}\n");
10698 * Dump an image header structure.
10700 *@param img_header the image header to dump.
10701 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10702 *@param out_stream output stream where dump the elements.
10704 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10708 if (dev_dump_flag){
10709 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10713 fprintf(out_stream, "Image info {\n");
10714 tab[0] = '\t';tab[1] = '\0';
10717 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10718 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10719 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10721 if (img_header->comps){
10723 for (compno = 0; compno < img_header->numcomps; compno++) {
10724 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10725 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10726 fprintf(out_stream,"%s}\n",tab);
10730 fprintf(out_stream, "}\n");
10734 * Dump a component image header structure.
10736 *@param comp_header the component image header to dump.
10737 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10738 *@param out_stream output stream where dump the elements.
10740 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10744 if (dev_dump_flag){
10745 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10748 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10751 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10752 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10753 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10756 fprintf(out_stream, "}\n");
10761 * Get the codestream info from a JPEG2000 codec.
10763 *@param p_j2k the component image header to dump.
10765 *@return the codestream information extract from the jpg2000 codec
10767 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10770 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10771 opj_tcp_v2_t *l_default_tile;
10772 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10774 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10776 cstr_info->tx0 = p_j2k->m_cp.tx0;
10777 cstr_info->ty0 = p_j2k->m_cp.ty0;
10778 cstr_info->tdx = p_j2k->m_cp.tdx;
10779 cstr_info->tdy = p_j2k->m_cp.tdy;
10780 cstr_info->tw = p_j2k->m_cp.tw;
10781 cstr_info->th = p_j2k->m_cp.th;
10783 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10785 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10787 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10788 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10789 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10790 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10792 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10794 for (compno = 0; compno < numcomps; compno++) {
10795 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10796 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10797 OPJ_INT32 bandno, numbands;
10800 l_tccp_info->csty = l_tccp->csty;
10801 l_tccp_info->numresolutions = l_tccp->numresolutions;
10802 l_tccp_info->cblkw = l_tccp->cblkw;
10803 l_tccp_info->cblkh = l_tccp->cblkh;
10804 l_tccp_info->cblksty = l_tccp->cblksty;
10805 l_tccp_info->qmfbid = l_tccp->qmfbid;
10806 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10808 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10809 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10812 /* quantization style*/
10813 l_tccp_info->qntsty = l_tccp->qntsty;
10814 l_tccp_info->numgbits = l_tccp->numgbits;
10816 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10817 if (numbands < J2K_MAXBANDS) {
10818 for (bandno = 0; bandno < numbands; bandno++) {
10819 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10820 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10825 l_tccp_info->roishift = l_tccp->roishift;
10833 * Get the codestream index from a JPEG2000 codec.
10835 *@param p_j2k the component image header to dump.
10837 *@return the codestream index extract from the jpg2000 codec
10839 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10841 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10842 opj_calloc(1,sizeof(opj_codestream_index_t));
10846 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10847 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10848 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10850 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10851 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10852 if (!l_cstr_index->marker){
10853 opj_free( l_cstr_index);
10857 if (p_j2k->cstr_index->marker)
10858 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10860 opj_free(l_cstr_index->marker);
10861 l_cstr_index->marker = NULL;
10864 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10865 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10866 if (!l_cstr_index->tile_index){
10867 opj_free( l_cstr_index->marker);
10868 opj_free( l_cstr_index);
10872 if (!p_j2k->cstr_index->tile_index){
10873 opj_free(l_cstr_index->tile_index);
10874 l_cstr_index->tile_index = NULL;
10877 OPJ_UINT32 it_tile = 0;
10878 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10881 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10883 l_cstr_index->tile_index[it_tile].marker =
10884 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10886 if (!l_cstr_index->tile_index[it_tile].marker) {
10887 OPJ_UINT32 it_tile_free;
10889 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10890 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10893 opj_free( l_cstr_index->tile_index);
10894 opj_free( l_cstr_index->marker);
10895 opj_free( l_cstr_index);
10899 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10900 memcpy( l_cstr_index->tile_index[it_tile].marker,
10901 p_j2k->cstr_index->tile_index[it_tile].marker,
10902 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10904 opj_free(l_cstr_index->tile_index[it_tile].marker);
10905 l_cstr_index->tile_index[it_tile].marker = NULL;
10908 /* Tile part index*/
10909 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10911 l_cstr_index->tile_index[it_tile].tp_index =
10912 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10914 if(!l_cstr_index->tile_index[it_tile].tp_index){
10915 OPJ_UINT32 it_tile_free;
10917 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10918 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10919 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10922 opj_free( l_cstr_index->tile_index);
10923 opj_free( l_cstr_index->marker);
10924 opj_free( l_cstr_index);
10928 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10929 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10930 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10931 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10934 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10935 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10938 /* Packet index (NOT USED)*/
10939 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10940 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10945 return l_cstr_index;
10948 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10950 OPJ_UINT32 it_tile=0;
10952 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10953 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10954 if (!p_j2k->cstr_index->tile_index)
10957 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10958 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10959 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10960 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10961 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10962 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10972 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10973 opj_stream_private_t *p_stream,
10974 opj_event_mgr_t * p_manager)
10976 opj_bool l_go_on = OPJ_TRUE;
10977 OPJ_UINT32 l_current_tile_no;
10978 OPJ_UINT32 l_data_size,l_max_data_size;
10979 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10980 OPJ_UINT32 l_nb_comps;
10981 OPJ_BYTE * l_current_data;
10983 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10984 if (! l_current_data) {
10987 l_max_data_size = 1000;
10992 if (! j2k_read_tile_header( p_j2k,
10993 &l_current_tile_no,
10995 &l_tile_x0, &l_tile_y0,
10996 &l_tile_x1, &l_tile_y1,
11001 opj_free(l_current_data);
11009 if (l_data_size > l_max_data_size) {
11010 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11011 if (! l_current_data) {
11012 opj_free(l_current_data);
11016 l_max_data_size = l_data_size;
11019 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11020 opj_free(l_current_data);
11023 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);
11025 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11026 opj_free(l_current_data);
11029 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11033 opj_free(l_current_data);
11039 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11041 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11044 assert(p_j2k != 00);
11046 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11047 /* DEVELOPER CORNER, add your custom procedures */
11052 * Read and decode one tile.
11054 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
11055 opj_stream_private_t *p_stream,
11056 opj_event_mgr_t * p_manager)
11058 opj_bool l_go_on = OPJ_TRUE;
11059 OPJ_UINT32 l_current_tile_no;
11060 OPJ_UINT32 l_tile_no_to_dec;
11061 OPJ_UINT32 l_data_size,l_max_data_size;
11062 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11063 OPJ_UINT32 l_nb_comps;
11064 OPJ_BYTE * l_current_data;
11066 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11067 if (! l_current_data) {
11070 l_max_data_size = 1000;
11072 /*Allocate and initialize some elements of codestrem index if not already done*/
11073 if( !p_j2k->cstr_index->tile_index)
11075 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11076 opj_free(l_current_data);
11080 /* Move into the codestream to the first SOT used to decode the desired tile */
11081 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11082 if (p_j2k->cstr_index->tile_index)
11083 if(p_j2k->cstr_index->tile_index->tp_index)
11085 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11086 /* the index for this tile has not been built,
11087 * so move to the last SOT read */
11088 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11089 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11094 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)) {
11095 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11099 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11100 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11101 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11105 if (! j2k_read_tile_header( p_j2k,
11106 &l_current_tile_no,
11108 &l_tile_x0, &l_tile_y0,
11109 &l_tile_x1, &l_tile_y1,
11114 opj_free(l_current_data);
11123 if (l_data_size > l_max_data_size) {
11124 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11125 if (! l_current_data) {
11126 opj_free(l_current_data);
11130 l_max_data_size = l_data_size;
11135 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11136 opj_free(l_current_data);
11139 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);
11141 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11142 opj_free(l_current_data);
11145 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11147 if(l_current_tile_no == l_tile_no_to_dec)
11149 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11150 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11151 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11157 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);
11162 opj_free(l_current_data);
11169 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11171 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11174 assert(p_j2k != 00);
11176 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11177 /* DEVELOPER CORNER, add your custom procedures */
11183 * Decodes the tiles of the stream.
11185 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
11186 opj_stream_private_t * p_stream,
11187 opj_image_t * p_image,
11188 opj_event_mgr_t * p_manager)
11195 p_j2k->m_output_image = opj_image_create0();
11196 if (! (p_j2k->m_output_image)) {
11199 opj_copy_image_header(p_image, p_j2k->m_output_image);
11201 /* customization of the decoding */
11202 j2k_setup_decoding(p_j2k);
11204 /* Decode the codestream */
11205 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11206 opj_image_destroy(p_j2k->m_private_image);
11207 p_j2k->m_private_image = NULL;
11211 /* Move data and copy one information from codec to output image*/
11212 for (compno = 0; compno < p_image->numcomps; compno++) {
11213 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11214 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11215 p_j2k->m_output_image->comps[compno].data = NULL;
11223 * Get the decoded tile.
11225 * @param p_j2k the jpeg2000 codestream codec.
11226 * @param p_stream input_stream
11227 * @param p_image output image. .
11228 * @param p_manager the user event manager
11229 * @param tile_index index of the tile we want decode
11231 * @return true if succeed.
11233 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11234 opj_stream_private_t *p_stream,
11235 opj_image_t* p_image,
11236 struct opj_event_mgr * p_manager,
11237 OPJ_UINT32 tile_index )
11240 OPJ_UINT32 l_tile_x, l_tile_y;
11241 opj_image_comp_t* l_img_comp;
11244 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11248 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11249 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);
11253 /* Compute the dimension of the desired tile*/
11254 l_tile_x = tile_index % p_j2k->m_cp.tw;
11255 l_tile_y = tile_index / p_j2k->m_cp.tw;
11257 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11258 if (p_image->x0 < p_j2k->m_private_image->x0)
11259 p_image->x0 = p_j2k->m_private_image->x0;
11260 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11261 if (p_image->x1 > p_j2k->m_private_image->x1)
11262 p_image->x1 = p_j2k->m_private_image->x1;
11264 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11265 if (p_image->y0 < p_j2k->m_private_image->y0)
11266 p_image->y0 = p_j2k->m_private_image->y0;
11267 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11268 if (p_image->y1 > p_j2k->m_private_image->y1)
11269 p_image->y1 = p_j2k->m_private_image->y1;
11271 l_img_comp = p_image->comps;
11272 for (compno=0; compno < p_image->numcomps; ++compno)
11274 OPJ_INT32 l_comp_x1, l_comp_y1;
11276 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11278 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11279 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11280 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11281 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11283 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11284 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11289 /* Destroy the previous output image*/
11290 if (p_j2k->m_output_image)
11291 opj_image_destroy(p_j2k->m_output_image);
11293 /* Create the ouput image from the information previously computed*/
11294 p_j2k->m_output_image = opj_image_create0();
11295 if (! (p_j2k->m_output_image)) {
11298 opj_copy_image_header(p_image, p_j2k->m_output_image);
11300 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11302 /* customization of the decoding */
11303 j2k_setup_decoding_tile(p_j2k);
11305 /* Decode the codestream */
11306 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11307 opj_image_destroy(p_j2k->m_private_image);
11308 p_j2k->m_private_image = NULL;
11312 /* Move data and copy one information from codec to output image*/
11313 for (compno = 0; compno < p_image->numcomps; compno++) {
11314 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11316 if (p_image->comps[compno].data)
11317 opj_free(p_image->comps[compno].data);
11319 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11321 p_j2k->m_output_image->comps[compno].data = NULL;
11327 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11329 OPJ_UINT32 it_comp;
11331 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11333 if (p_j2k->m_private_image) {
11334 if (p_j2k->m_private_image->comps) {
11335 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11336 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11337 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11338 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11339 if ( res_factor >= max_res){
11340 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11343 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11356 * Encodes all the tiles in a row.
11358 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11359 opj_stream_private_t *p_stream,
11360 opj_event_mgr_t * p_manager )
11363 OPJ_UINT32 l_nb_tiles;
11364 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11365 OPJ_BYTE * l_current_data;
11367 /* preconditions */
11368 assert(p_j2k != 00);
11369 assert(p_stream != 00);
11370 assert(p_manager != 00);
11372 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11373 if (! l_current_data) {
11376 l_max_tile_size = 1000;
11378 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11379 for (i=0;i<l_nb_tiles;++i) {
11380 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11381 opj_free(l_current_data);
11385 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11386 if (l_current_tile_size > l_max_tile_size) {
11387 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11388 if (! l_current_data) {
11391 l_max_tile_size = l_current_tile_size;
11394 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11396 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11401 opj_free(l_current_data);
11406 * Ends the compression procedures and possibility add data to be read after the
11409 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11410 opj_stream_private_t *p_stream,
11411 struct opj_event_mgr * p_manager)
11413 /* customization of the encoding */
11414 j2k_setup_end_compress(p_j2k);
11416 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11426 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11428 * @param p_j2k the jpeg2000 codec.
11429 * @param p_stream the stream object.
11430 * @param p_manager the user event manager.
11432 * @return true if the codec is valid.
11434 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11435 opj_stream_private_t *p_stream,
11436 opj_image_t * p_image,
11437 opj_event_mgr_t * p_manager)
11439 /* preconditions */
11440 assert(p_j2k != 00);
11441 assert(p_stream != 00);
11442 assert(p_manager != 00);
11444 p_j2k->m_private_image = opj_image_create0();
11445 opj_copy_image_header(p_image, p_j2k->m_private_image);
11447 // TODO_MSD: Find a better way
11448 if (p_image->comps) {
11449 OPJ_UINT32 it_comp;
11450 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11451 if (p_image->comps[it_comp].data) {
11452 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11453 p_image->comps[it_comp].data = NULL;
11459 /* customization of the validation */
11460 j2k_setup_encoding_validation (p_j2k);
11462 /* validation of the parameters codec */
11463 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11467 /* customization of the encoding */
11468 j2k_setup_header_writting(p_j2k);
11471 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11481 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11482 OPJ_UINT32 p_tile_index,
11483 opj_stream_private_t *p_stream,
11484 opj_event_mgr_t * p_manager )
11487 if (p_tile_index != p_j2k->m_current_tile_number) {
11488 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11492 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);
11494 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11495 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11496 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11498 /* initialisation before tile encoding */
11499 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11509 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11511 OPJ_UINT32 i,j,k = 0;
11512 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11513 opj_image_comp_t * l_img_comp = 00;
11514 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11515 opj_image_t * l_image = 00;
11516 OPJ_UINT32 l_size_comp, l_remaining;
11517 OPJ_INT32 * l_src_ptr;
11518 l_tilec = p_tcd->tcd_image->tiles->comps;
11519 l_image = p_tcd->image;
11520 l_img_comp = l_image->comps;
11522 for (i=0;i<p_tcd->image->numcomps;++i) {
11523 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11524 l_remaining = l_img_comp->prec & 7; /* (%8) */
11529 if (l_size_comp == 3) {
11533 l_width = (l_tilec->x1 - l_tilec->x0);
11534 l_height = (l_tilec->y1 - l_tilec->y0);
11535 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11536 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11537 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11538 l_stride = l_image_width - l_width;
11539 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11541 switch (l_size_comp) {
11544 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11545 if (l_img_comp->sgnd) {
11546 for (j=0;j<l_height;++j) {
11547 for (k=0;k<l_width;++k) {
11548 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11552 l_src_ptr += l_stride;
11556 for (j=0;j<l_height;++j) {
11557 for (k=0;k<l_width;++k) {
11558 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11562 l_src_ptr += l_stride;
11566 p_data = (OPJ_BYTE*) l_dest_ptr;
11571 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11572 if (l_img_comp->sgnd) {
11573 for (j=0;j<l_height;++j) {
11574 for (k=0;k<l_width;++k) {
11575 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11577 l_src_ptr += l_stride;
11581 for (j=0;j<l_height;++j) {
11582 for (k=0;k<l_width;++k) {
11583 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11585 l_src_ptr += l_stride;
11589 p_data = (OPJ_BYTE*) l_dest_ptr;
11594 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11595 for (j=0;j<l_height;++j) {
11596 for (k=0;k<l_width;++k) {
11597 *(l_dest_ptr++) = *(l_src_ptr++);
11599 l_src_ptr += l_stride;
11602 p_data = (OPJ_BYTE*) l_dest_ptr;
11615 * @param p_j2k the jpeg2000 codec.
11616 * @param p_stream the stream to write data to.
11617 * @param p_manager the user event manager.
11619 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11621 OPJ_UINT32 p_data_size,
11622 opj_stream_private_t *p_stream,
11623 opj_event_mgr_t * p_manager )
11625 opj_tcd_v2_t * l_tcd = 00;
11626 opj_cp_v2_t * l_cp = 00;
11627 opj_tcp_v2_t * l_tcp = 00;
11628 OPJ_UINT32 l_nb_bytes_written;
11629 OPJ_BYTE * l_current_data = 00;
11630 OPJ_UINT32 l_tile_size = 0;
11631 OPJ_UINT32 l_available_data;
11633 /* preconditions */
11634 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11636 l_tcd = p_j2k->m_tcd;
11637 l_cp = &(p_j2k->m_cp);
11638 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11640 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11641 l_available_data = l_tile_size;
11642 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11644 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11645 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11649 l_nb_bytes_written = 0;
11650 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11653 l_current_data += l_nb_bytes_written;
11654 l_available_data -= l_nb_bytes_written;
11656 l_nb_bytes_written = 0;
11657 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11661 l_available_data -= l_nb_bytes_written;
11662 l_nb_bytes_written = l_tile_size - l_available_data;
11664 if ( opj_stream_write_data( p_stream,
11665 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11666 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11670 ++p_j2k->m_current_tile_number;
11677 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11678 * are valid. Developers wanting to extend the library can add their own validation procedures.
11680 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11682 /* preconditions */
11683 assert(p_j2k != 00);
11685 /* DEVELOPER CORNER, insert your custom procedures */
11686 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11688 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11689 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11692 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11693 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11694 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11698 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11699 * are valid. Developers wanting to extend the library can add their own validation procedures.
11701 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11703 /* preconditions */
11704 assert(p_j2k != 00);
11706 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11707 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11709 /* DEVELOPER CORNER, add your custom validation procedure */
11710 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11715 * Sets up the procedures to do on writing header.
11716 * Developers wanting to extend the library can add their own writing procedures.
11718 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11720 /* preconditions */
11721 assert(p_j2k != 00);
11723 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11724 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11725 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11726 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11727 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11730 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11731 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11732 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11734 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11735 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11739 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11741 if (p_j2k->m_cp.comment != 00) {
11742 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11745 /* DEVELOPER CORNER, insert your custom procedures */
11746 if (p_j2k->m_cp.rsiz & MCT) {
11747 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11749 /* End of Developer Corner */
11751 if (p_j2k->cstr_index) {
11752 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11755 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11756 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11760 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11762 OPJ_UINT32 * p_data_written,
11763 OPJ_UINT32 p_total_data_size,
11764 opj_stream_private_t *p_stream,
11765 struct opj_event_mgr * p_manager )
11768 OPJ_UINT32 l_nb_bytes_written = 0;
11769 OPJ_UINT32 l_current_nb_bytes_written;
11770 OPJ_BYTE * l_begin_data = 00;
11772 opj_tcp_v2_t *l_tcp = 00;
11773 opj_tcd_v2_t * l_tcd = 00;
11774 opj_cp_v2_t * l_cp = 00;
11776 l_tcd = p_j2k->m_tcd;
11777 l_cp = &(p_j2k->m_cp);
11778 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11780 l_tcd->cur_pino = 0;
11782 /*Get number of tile parts*/
11783 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11788 l_current_nb_bytes_written = 0;
11789 l_begin_data = p_data;
11790 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11795 l_nb_bytes_written += l_current_nb_bytes_written;
11796 p_data += l_current_nb_bytes_written;
11797 p_total_data_size -= l_current_nb_bytes_written;
11799 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11800 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11801 l_current_nb_bytes_written = 0;
11802 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11803 l_nb_bytes_written += l_current_nb_bytes_written;
11804 p_data += l_current_nb_bytes_written;
11805 p_total_data_size -= l_current_nb_bytes_written;
11807 l_current_nb_bytes_written = 0;
11808 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11809 l_nb_bytes_written += l_current_nb_bytes_written;
11810 p_data += l_current_nb_bytes_written;
11811 p_total_data_size -= l_current_nb_bytes_written;
11814 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11815 l_current_nb_bytes_written = 0;
11816 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11817 l_nb_bytes_written += l_current_nb_bytes_written;
11818 p_data += l_current_nb_bytes_written;
11819 p_total_data_size -= l_current_nb_bytes_written;
11823 l_current_nb_bytes_written = 0;
11824 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11828 l_nb_bytes_written += l_current_nb_bytes_written;
11829 * p_data_written = l_nb_bytes_written;
11831 /* Writing Psot in SOT marker */
11832 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11834 if (l_cp->m_specific_param.m_enc.m_cinema){
11835 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11841 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11843 OPJ_UINT32 * p_data_written,
11844 OPJ_UINT32 p_total_data_size,
11845 opj_stream_private_t *p_stream,
11846 struct opj_event_mgr * p_manager
11849 OPJ_UINT32 tilepartno=0;
11850 OPJ_UINT32 l_nb_bytes_written = 0;
11851 OPJ_UINT32 l_current_nb_bytes_written;
11852 OPJ_UINT32 l_part_tile_size;
11853 OPJ_UINT32 tot_num_tp;
11856 OPJ_BYTE * l_begin_data;
11857 opj_tcp_v2_t *l_tcp = 00;
11858 opj_tcd_v2_t * l_tcd = 00;
11859 opj_cp_v2_t * l_cp = 00;
11862 l_tcd = p_j2k->m_tcd;
11863 l_cp = &(p_j2k->m_cp);
11864 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11866 /*Get number of tile parts*/
11867 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11869 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11870 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11871 l_current_nb_bytes_written = 0;
11872 l_part_tile_size = 0;
11873 l_begin_data = p_data;
11875 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11879 l_nb_bytes_written += l_current_nb_bytes_written;
11880 p_data += l_current_nb_bytes_written;
11881 p_total_data_size -= l_current_nb_bytes_written;
11882 l_part_tile_size += l_nb_bytes_written;
11884 l_current_nb_bytes_written = 0;
11885 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11889 p_data += l_current_nb_bytes_written;
11890 l_nb_bytes_written += l_current_nb_bytes_written;
11891 p_total_data_size -= l_current_nb_bytes_written;
11892 l_part_tile_size += l_nb_bytes_written;
11894 /* Writing Psot in SOT marker */
11895 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11897 if (l_cp->m_specific_param.m_enc.m_cinema) {
11898 j2k_update_tlm(p_j2k,l_part_tile_size);
11901 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11904 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11905 l_tcd->cur_pino = pino;
11907 /*Get number of tile parts*/
11908 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11909 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11910 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11911 l_current_nb_bytes_written = 0;
11912 l_part_tile_size = 0;
11913 l_begin_data = p_data;
11915 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11919 l_nb_bytes_written += l_current_nb_bytes_written;
11920 p_data += l_current_nb_bytes_written;
11921 p_total_data_size -= l_current_nb_bytes_written;
11922 l_part_tile_size += l_current_nb_bytes_written;
11924 l_current_nb_bytes_written = 0;
11926 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11930 l_nb_bytes_written += l_current_nb_bytes_written;
11931 p_data += l_current_nb_bytes_written;
11932 p_total_data_size -= l_current_nb_bytes_written;
11933 l_part_tile_size += l_current_nb_bytes_written;
11935 /* Writing Psot in SOT marker */
11936 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11938 if (l_cp->m_specific_param.m_enc.m_cinema) {
11939 j2k_update_tlm(p_j2k,l_part_tile_size);
11942 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11946 *p_data_written = l_nb_bytes_written;
11952 * Writes the updated tlm.
11954 * @param p_stream the stream to write data to.
11955 * @param p_j2k J2K codec.
11956 * @param p_manager the user event manager.
11958 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11959 struct opj_stream_private *p_stream,
11960 struct opj_event_mgr * p_manager )
11962 OPJ_UINT32 l_tlm_size;
11963 OPJ_SIZE_T l_tlm_position, l_current_position;
11965 /* preconditions */
11966 assert(p_j2k != 00);
11967 assert(p_manager != 00);
11968 assert(p_stream != 00);
11970 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11971 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11972 l_current_position = opj_stream_tell(p_stream);
11974 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11978 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) {
11982 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11991 * Ends the encoding, i.e. frees memory.
11993 * @param p_stream the stream to write data to.
11994 * @param p_j2k J2K codec.
11995 * @param p_manager the user event manager.
11997 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11998 struct opj_stream_private *p_stream,
11999 struct opj_event_mgr * p_manager )
12001 /* preconditions */
12002 assert(p_j2k != 00);
12003 assert(p_manager != 00);
12004 assert(p_stream != 00);
12006 tcd_destroy_v2(p_j2k->m_tcd);
12009 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12010 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12011 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12012 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12015 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12016 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12017 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12020 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12026 * Destroys the memory associated with the decoding of headers.
12028 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12029 opj_stream_private_t *p_stream,
12030 opj_event_mgr_t * p_manager )
12032 /* preconditions */
12033 assert(p_j2k != 00);
12034 assert(p_stream != 00);
12035 assert(p_manager != 00);
12037 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12038 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12039 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12042 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12051 * @param p_stream the stream to write data to.
12052 * @param p_j2k J2K codec.
12053 * @param p_manager the user event manager.
12055 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12056 struct opj_stream_private *p_stream,
12057 struct opj_event_mgr * p_manager )
12059 opj_codestream_info_t * l_cstr_info = 00;
12061 /* preconditions */
12062 assert(p_j2k != 00);
12063 assert(p_manager != 00);
12064 assert(p_stream != 00);
12067 /* TODO mergeV2: check this part which use cstr_info */
12068 /*l_cstr_info = p_j2k->cstr_info;
12072 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));
12074 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12075 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12077 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12079 l_cstr_info->tw = p_j2k->m_cp.tw;
12080 l_cstr_info->th = p_j2k->m_cp.th;
12082 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12083 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12084 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12085 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12087 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12089 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12091 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12093 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12094 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12097 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12099 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12101 /*l_cstr_info->maxmarknum = 100;
12102 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12103 l_cstr_info->marknum = 0;
12106 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);
12110 * Creates a tile-coder decoder.
12112 * @param p_stream the stream to write data to.
12113 * @param p_j2k J2K codec.
12114 * @param p_manager the user event manager.
12116 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12117 struct opj_stream_private *p_stream,
12118 struct opj_event_mgr * p_manager )
12120 /* preconditions */
12121 assert(p_j2k != 00);
12122 assert(p_manager != 00);
12123 assert(p_stream != 00);
12125 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12127 if (! p_j2k->m_tcd) {
12128 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12132 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12133 tcd_destroy_v2(p_j2k->m_tcd);
12144 * @param p_j2k the jpeg2000 codec.
12145 * @param p_stream the stream to write data to.
12146 * @param p_manager the user event manager.
12148 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12149 OPJ_UINT32 p_tile_index,
12151 OPJ_UINT32 p_data_size,
12152 opj_stream_private_t *p_stream,
12153 opj_event_mgr_t * p_manager )
12155 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12156 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12160 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12161 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);