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 Read the RGN marker (region-of-interest)
998 @param j2k J2K handle
1000 static void j2k_read_rgn(opj_j2k_t *j2k);
1003 * Reads a RGN marker (Region Of Interest)
1005 * @param p_header_data the data contained in the POC box.
1006 * @param p_j2k the jpeg2000 codec.
1007 * @param p_header_size the size of the data contained in the POC marker.
1008 * @param p_manager the user event manager.
1010 static opj_bool j2k_read_rgn_v2 (
1011 opj_j2k_v2_t *p_j2k,
1012 OPJ_BYTE * p_header_data,
1013 OPJ_UINT32 p_header_size,
1014 struct opj_event_mgr * p_manager
1018 * Writes the EOC marker (End of Codestream)
1020 * @param p_stream the stream to write data to.
1021 * @param p_j2k J2K codec.
1022 * @param p_manager the user event manager.
1024 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
1025 struct opj_stream_private *p_stream,
1026 struct opj_event_mgr * p_manager );
1029 Write the EOC marker (end of codestream)
1030 @param j2k J2K handle
1032 static void j2k_write_eoc(opj_j2k_t *j2k);
1034 Read the EOC marker (end of codestream)
1035 @param j2k J2K handle
1037 static void j2k_read_eoc(opj_j2k_t *j2k);
1040 * Reads a EOC marker (End Of Codestream)
1042 * @param p_header_data the data contained in the SOD box.
1043 * @param p_j2k the jpeg2000 codec.
1044 * @param p_header_size the size of the data contained in the SOD marker.
1045 * @param p_manager the user event manager.
1048 static opj_bool j2k_read_eoc_v2 (
1049 opj_j2k_v2_t *p_j2k,
1050 struct opj_stream_private *p_stream,
1051 struct opj_event_mgr * p_manager
1058 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1060 * @param p_stream the stream to write data to.
1061 * @param p_j2k J2K codec.
1062 * @param p_manager the user event manager.
1064 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1065 struct opj_stream_private *p_stream,
1066 struct opj_event_mgr * p_manager );
1071 * @param p_stream the stream to write data to.
1072 * @param p_j2k J2K codec.
1073 * @param p_manager the user event manager.
1075 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1076 struct opj_stream_private *p_stream,
1077 struct opj_event_mgr * p_manager );
1080 Read an unknown marker
1081 @param j2k J2K handle
1083 static void j2k_read_unk(opj_j2k_t *j2k);
1085 Add main header marker information
1086 @param cstr_info Codestream information structure
1087 @param type marker type
1088 @param pos byte offset of marker segment
1089 @param len length of marker segment
1091 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1093 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1095 Add tile header marker information
1096 @param tileno tile index number
1097 @param cstr_info Codestream information structure
1098 @param type marker type
1099 @param pos byte offset of marker segment
1100 @param len length of marker segment
1102 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1104 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);
1107 * Reads an unknown marker
1109 * @param p_stream the stream object to read from.
1110 * @param p_j2k the jpeg2000 codec.
1111 * @param p_manager the user event manager.
1113 * @return true if the marker could be deduced.
1115 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1116 struct opj_stream_private *p_stream,
1117 OPJ_UINT32 *output_marker,
1118 struct opj_event_mgr * p_manager );
1121 * Writes the MCT marker (Multiple Component Transform)
1123 * @param p_stream the stream to write data to.
1124 * @param p_j2k J2K codec.
1125 * @param p_manager the user event manager.
1127 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1128 opj_mct_data_t * p_mct_record,
1129 struct opj_stream_private *p_stream,
1130 struct opj_event_mgr * p_manager );
1133 * Reads a MCT marker (Multiple Component Transform)
1135 * @param p_header_data the data contained in the MCT box.
1136 * @param p_j2k the jpeg2000 codec.
1137 * @param p_header_size the size of the data contained in the MCT marker.
1138 * @param p_manager the user event manager.
1140 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1141 OPJ_BYTE * p_header_data,
1142 OPJ_UINT32 p_header_size,
1143 struct opj_event_mgr * p_manager );
1146 * Writes the MCC marker (Multiple Component Collection)
1148 * @param p_stream the stream to write data to.
1149 * @param p_j2k J2K codec.
1150 * @param p_manager the user event manager.
1152 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1153 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1154 struct opj_stream_private *p_stream,
1155 struct opj_event_mgr * p_manager );
1159 * Reads a MCC marker (Multiple Component Collection)
1161 * @param p_header_data the data contained in the MCC box.
1162 * @param p_j2k the jpeg2000 codec.
1163 * @param p_header_size the size of the data contained in the MCC marker.
1164 * @param p_manager the user event manager.
1166 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1167 OPJ_BYTE * p_header_data,
1168 OPJ_UINT32 p_header_size,
1169 struct opj_event_mgr * p_manager );
1172 * Writes the MCO marker (Multiple component transformation ordering)
1174 * @param p_stream the stream to write data to.
1175 * @param p_j2k J2K codec.
1176 * @param p_manager the user event manager.
1178 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1179 struct opj_stream_private *p_stream,
1180 struct opj_event_mgr * p_manager );
1183 * Reads a MCO marker (Multiple Component Transform Ordering)
1185 * @param p_header_data the data contained in the MCO box.
1186 * @param p_j2k the jpeg2000 codec.
1187 * @param p_header_size the size of the data contained in the MCO marker.
1188 * @param p_manager the user event manager.
1190 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1191 OPJ_BYTE * p_header_data,
1192 OPJ_UINT32 p_header_size,
1193 struct opj_event_mgr * p_manager );
1195 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1197 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1198 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1199 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1200 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1202 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1203 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1204 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1205 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1207 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1208 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1209 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1210 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1214 * Ends the encoding, i.e. frees memory.
1216 * @param p_stream the stream to write data to.
1217 * @param p_j2k J2K codec.
1218 * @param p_manager the user event manager.
1220 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1221 struct opj_stream_private *p_stream,
1222 struct opj_event_mgr * p_manager );
1225 * Writes the CBD marker (Component bit depth definition)
1227 * @param p_stream the stream to write data to.
1228 * @param p_j2k J2K codec.
1229 * @param p_manager the user event manager.
1231 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1232 struct opj_stream_private *p_stream,
1233 struct opj_event_mgr * p_manager );
1236 * Reads a CBD marker (Component bit depth definition)
1237 * @param p_header_data the data contained in the CBD box.
1238 * @param p_j2k the jpeg2000 codec.
1239 * @param p_header_size the size of the data contained in the CBD marker.
1240 * @param p_manager the user event manager.
1242 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1243 OPJ_BYTE * p_header_data,
1244 OPJ_UINT32 p_header_size,
1245 struct opj_event_mgr * p_manager);
1248 * Writes the image components.
1250 * @param p_stream the stream to write data to.
1251 * @param p_j2k J2K codec.
1252 * @param p_manager the user event manager.
1254 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1255 struct opj_stream_private *p_stream,
1256 struct opj_event_mgr * p_manager );
1259 * Writes regions of interests.
1261 * @param p_stream the stream to write data to.
1262 * @param p_j2k J2K codec.
1263 * @param p_manager the user event manager.
1265 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1266 struct opj_stream_private *p_stream,
1267 struct opj_event_mgr * p_manager );
1272 * @param p_stream the stream to write data to.
1273 * @param p_j2k J2K codec.
1274 * @param p_manager the user event manager.
1276 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1277 struct opj_stream_private *p_stream,
1278 struct opj_event_mgr * p_manager );
1281 * Checks the progression order changes values. Tells of the poc given as input are valid.
1282 * A nice message is outputted at errors.
1284 * @param p_pocs the progression order changes.
1285 * @param p_nb_pocs the number of progression order changes.
1286 * @param p_nb_resolutions the number of resolutions.
1287 * @param numcomps the number of components
1288 * @param numlayers the number of layers.
1290 * @return true if the pocs are valid.
1292 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1293 OPJ_UINT32 p_nb_pocs,
1294 OPJ_UINT32 p_nb_resolutions,
1295 OPJ_UINT32 numcomps,
1296 OPJ_UINT32 numlayers,
1297 opj_event_mgr_t * p_manager);
1300 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1302 * @param cp the coding parameters.
1303 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1304 * @param tileno the given tile.
1306 * @return the number of tile parts.
1308 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1310 /** mem allocation for TLM marker*/
1311 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1314 * Calculates the total number of tile parts needed by the encoder to
1315 * encode such an image. If not enough memory is available, then the function return false.
1317 * @param p_nb_tiles pointer that will hold the number of tile parts.
1318 * @param cp the coding parameters for the image.
1319 * @param image the image to encode.
1320 * @param p_j2k the p_j2k encoder.
1321 * @param p_manager the user event manager.
1323 * @return true if the function was successful, false else.
1325 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1327 OPJ_UINT32 * p_nb_tiles,
1329 opj_event_mgr_t * p_manager);
1331 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1333 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1335 static opj_codestream_index_t* j2k_create_cstr_index(void);
1341 /* ----------------------------------------------------------------------- */
1342 typedef struct j2k_prog_order{
1343 OPJ_PROG_ORDER enum_prog;
1347 j2k_prog_order_t j2k_prog_order_list[] = {
1353 {(OPJ_PROG_ORDER)-1, ""}
1361 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1369 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1371 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1373 j2k_read_int16_to_float,
1374 j2k_read_int32_to_float,
1375 j2k_read_float32_to_float,
1376 j2k_read_float64_to_float
1379 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1381 j2k_read_int16_to_int32,
1382 j2k_read_int32_to_int32,
1383 j2k_read_float32_to_int32,
1384 j2k_read_float64_to_int32
1387 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1389 j2k_write_float_to_int16,
1390 j2k_write_float_to_int32,
1391 j2k_write_float_to_float,
1392 j2k_write_float_to_float64
1395 typedef struct opj_dec_memory_marker_handler
1399 /** value of the state when the marker can appear */
1401 /** action linked to the marker */
1402 opj_bool (*handler) (
1403 opj_j2k_v2_t *p_j2k,
1404 OPJ_BYTE * p_header_data,
1405 OPJ_UINT32 p_header_size,
1406 struct opj_event_mgr * p_manager
1409 opj_dec_memory_marker_handler_t;
1411 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1414 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1415 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1416 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1417 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1418 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1419 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1420 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1421 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1422 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1423 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1424 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1425 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1426 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1428 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1429 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1430 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1431 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1432 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1433 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1435 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1436 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1437 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1438 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1439 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1440 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1441 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1442 {J2K_MS_SIZ, J2K_STATE_MHSIZ , opj_j2k_read_siz},
1443 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1444 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1445 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1446 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1447 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1449 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1450 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1451 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1452 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1453 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1454 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1456 #ifdef TODO_MS /* FIXME */
1457 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1458 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1459 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1460 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1462 #endif /* USE_JPWL */
1464 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1465 {J2K_MS_INSEC, 0, j2k_read_insec}
1466 #endif /* USE_JPSEC */
1467 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1472 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1474 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1475 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1479 for (i=0;i<p_nb_elem;++i) {
1480 opj_read_bytes(l_src_data,&l_temp,2);
1482 l_src_data+=sizeof(OPJ_INT16);
1484 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1488 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1490 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1491 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1495 for (i=0;i<p_nb_elem;++i) {
1496 opj_read_bytes(l_src_data,&l_temp,4);
1498 l_src_data+=sizeof(OPJ_INT32);
1500 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1504 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1506 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1507 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1511 for (i=0;i<p_nb_elem;++i) {
1512 opj_read_float(l_src_data,&l_temp);
1514 l_src_data+=sizeof(OPJ_FLOAT32);
1516 *(l_dest_data++) = l_temp;
1520 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1522 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1523 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1527 for (i=0;i<p_nb_elem;++i) {
1528 opj_read_double(l_src_data,&l_temp);
1530 l_src_data+=sizeof(OPJ_FLOAT64);
1532 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1536 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1538 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1539 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1543 for (i=0;i<p_nb_elem;++i) {
1544 opj_read_bytes(l_src_data,&l_temp,2);
1546 l_src_data+=sizeof(OPJ_INT16);
1548 *(l_dest_data++) = (OPJ_INT32) l_temp;
1552 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1554 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1555 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1559 for (i=0;i<p_nb_elem;++i) {
1560 opj_read_bytes(l_src_data,&l_temp,4);
1562 l_src_data+=sizeof(OPJ_INT32);
1564 *(l_dest_data++) = (OPJ_INT32) l_temp;
1568 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1570 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1571 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1575 for (i=0;i<p_nb_elem;++i) {
1576 opj_read_float(l_src_data,&l_temp);
1578 l_src_data+=sizeof(OPJ_FLOAT32);
1580 *(l_dest_data++) = (OPJ_INT32) l_temp;
1584 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1586 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1587 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1591 for (i=0;i<p_nb_elem;++i) {
1592 opj_read_double(l_src_data,&l_temp);
1594 l_src_data+=sizeof(OPJ_FLOAT64);
1596 *(l_dest_data++) = (OPJ_INT32) l_temp;
1600 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1602 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1603 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1607 for (i=0;i<p_nb_elem;++i) {
1608 l_temp = (OPJ_UINT32) *(l_src_data++);
1610 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1612 l_dest_data+=sizeof(OPJ_INT16);
1616 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1618 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1619 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1623 for (i=0;i<p_nb_elem;++i) {
1624 l_temp = (OPJ_UINT32) *(l_src_data++);
1626 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1628 l_dest_data+=sizeof(OPJ_INT32);
1632 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1634 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1635 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1639 for (i=0;i<p_nb_elem;++i) {
1640 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1642 opj_write_float(l_dest_data,l_temp);
1644 l_dest_data+=sizeof(OPJ_FLOAT32);
1648 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1650 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1651 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1655 for (i=0;i<p_nb_elem;++i) {
1656 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1658 opj_write_double(l_dest_data,l_temp);
1660 l_dest_data+=sizeof(OPJ_FLOAT64);
1666 * Converts an enum type progression order to string type.
1668 * @param prg_order the progression order to get.
1670 * @return the string representation of the given progression order.
1672 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1673 j2k_prog_order_t *po;
1674 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1675 if(po->enum_prog == prg_order){
1676 return po->str_prog;
1679 return po->str_prog;
1683 * Checks the progression order changes values. Tells if the poc given as input are valid.
1685 * @param p_pocs the progression order changes.
1686 * @param p_nb_pocs the number of progression order changes.
1687 * @param p_nb_resolutions the number of resolutions.
1688 * @param numcomps the number of components
1689 * @param numlayers the number of layers.
1690 * @param p_manager the user event manager.
1692 * @return true if the pocs are valid.
1694 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1695 OPJ_UINT32 p_nb_pocs,
1696 OPJ_UINT32 p_nb_resolutions,
1697 OPJ_UINT32 p_num_comps,
1698 OPJ_UINT32 p_num_layers,
1699 opj_event_mgr_t * p_manager)
1701 OPJ_UINT32* packet_array;
1702 OPJ_UINT32 index , resno, compno, layno;
1704 OPJ_UINT32 step_c = 1;
1705 OPJ_UINT32 step_r = p_num_comps * step_c;
1706 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1707 opj_bool loss = OPJ_FALSE;
1708 OPJ_UINT32 layno0 = 0;
1710 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1711 if (packet_array == 00) {
1712 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1715 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1717 if (p_nb_pocs == 0) {
1721 index = step_r * p_pocs->resno0;
1722 // take each resolution for each poc
1723 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1725 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1727 // take each comp of each resolution for each poc
1728 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1729 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1731 // and finally take each layer of each res of ...
1732 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1733 //index = step_r * resno + step_c * compno + step_l * layno;
1734 packet_array[comp_index] = 1;
1735 comp_index += step_l;
1738 res_index += step_c;
1745 // iterate through all the pocs
1746 for (i = 1; i < p_nb_pocs ; ++i) {
1747 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1749 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1750 index = step_r * p_pocs->resno0;
1752 // take each resolution for each poc
1753 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1754 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1756 // take each comp of each resolution for each poc
1757 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1758 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1760 // and finally take each layer of each res of ...
1761 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1762 //index = step_r * resno + step_c * compno + step_l * layno;
1763 packet_array[comp_index] = 1;
1764 comp_index += step_l;
1767 res_index += step_c;
1777 for (layno = 0; layno < p_num_layers ; ++layno) {
1778 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1779 for (compno = 0; compno < p_num_comps; ++compno) {
1780 loss |= (packet_array[index]!=1);
1781 //index = step_r * resno + step_c * compno + step_l * layno;
1788 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1791 opj_free(packet_array);
1796 /* ----------------------------------------------------------------------- */
1797 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1800 int tpnum=1,tpend=0;
1801 opj_tcp_t *tcp = &cp->tcps[tileno];
1802 prog = j2k_convert_progression_order(tcp->prg);
1807 if( cp->tp_flag == prog[i] ){
1808 tpend=1;cp->tp_pos=i;
1812 tpnum= tpnum * tcp->pocs[pino].compE;
1815 tpnum= tpnum * tcp->pocs[pino].resE;
1818 tpnum= tpnum * tcp->pocs[pino].prcE;
1821 tpnum= tpnum * tcp->pocs[pino].layE;
1833 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1835 * @param cp the coding parameters.
1836 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1837 * @param tileno the given tile.
1839 * @return the number of tile parts.
1841 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1843 const OPJ_CHAR *prog = 00;
1845 OPJ_UINT32 tpnum = 1;
1846 opj_tcp_v2_t *tcp = 00;
1847 opj_poc_t * l_current_poc = 00;
1850 assert(tileno < (cp->tw * cp->th));
1851 assert(pino < (cp->tcps[tileno].numpocs + 1));
1853 /* get the given tile coding parameter */
1854 tcp = &cp->tcps[tileno];
1857 l_current_poc = &(tcp->pocs[pino]);
1858 assert(l_current_poc != 0);
1860 /* get the progression order as a character string */
1861 prog = j2k_convert_progression_order(tcp->prg);
1862 assert(strlen(prog) > 0);
1864 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1868 /* component wise */
1870 tpnum *= l_current_poc->compE;
1872 /* resolution wise */
1874 tpnum *= l_current_poc->resE;
1878 tpnum *= l_current_poc->prcE;
1882 tpnum *= l_current_poc->layE;
1885 /* whould we split here ? */
1886 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1887 cp->m_specific_param.m_enc.m_tp_pos=i;
1899 /** mem allocation for TLM marker*/
1900 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1901 int pino,tileno,totnum_tp=0;
1903 OPJ_ARG_NOT_USED(img_numcomp);
1905 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1906 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1907 int cur_totnum_tp = 0;
1908 opj_tcp_t *tcp = &cp->tcps[tileno];
1909 for(pino = 0; pino <= tcp->numpocs; pino++) {
1911 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1912 if(!pi) { return -1;}
1913 tp_num = j2k_get_num_tp(cp,pino,tileno);
1914 totnum_tp = totnum_tp + tp_num;
1915 cur_totnum_tp = cur_totnum_tp + tp_num;
1916 pi_destroy(pi, cp, tileno);
1918 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1920 if (j2k->cstr_info) {
1921 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1922 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1930 * Calculates the total number of tile parts needed by the encoder to
1931 * encode such an image. If not enough memory is available, then the function return false.
1933 * @param p_nb_tiles pointer that will hold the number of tile parts.
1934 * @param cp the coding parameters for the image.
1935 * @param image the image to encode.
1936 * @param p_j2k the p_j2k encoder.
1937 * @param p_manager the user event manager.
1939 * @return true if the function was successful, false else.
1941 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1943 OPJ_UINT32 * p_nb_tiles,
1945 opj_event_mgr_t * p_manager)
1947 OPJ_UINT32 pino,tileno;
1948 OPJ_UINT32 l_nb_tiles;
1952 assert(p_nb_tiles != 00);
1954 assert(image != 00);
1955 assert(p_j2k != 00);
1956 assert(p_manager != 00);
1958 l_nb_tiles = cp->tw * cp->th;
1963 /* TODO mergeV2: check this part which use cstr_info */
1964 /*if (p_j2k->cstr_info) {
1965 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1967 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1968 OPJ_UINT32 cur_totnum_tp = 0;
1970 pi_update_encoding_parameters(image,cp,tileno);
1972 for (pino = 0; pino <= tcp->numpocs; ++pino)
1974 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1976 *p_nb_tiles = *p_nb_tiles + tp_num;
1978 cur_totnum_tp += tp_num;
1981 tcp->m_nb_tile_parts = cur_totnum_tp;
1983 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1984 if (l_info_tile_ptr->tp == 00) {
1988 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1990 l_info_tile_ptr->num_tps = cur_totnum_tp;
1997 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1998 OPJ_UINT32 cur_totnum_tp = 0;
2000 pi_update_encoding_parameters(image,cp,tileno);
2002 for (pino = 0; pino <= tcp->numpocs; ++pino) {
2003 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2005 *p_nb_tiles = *p_nb_tiles + tp_num;
2007 cur_totnum_tp += tp_num;
2009 tcp->m_nb_tile_parts = cur_totnum_tp;
2018 static void j2k_write_soc(opj_j2k_t *j2k) {
2019 opj_cio_t *cio = j2k->cio;
2020 cio_write(cio, J2K_MS_SOC, 2);
2023 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2028 /* update markers struct */
2029 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2030 #endif /* USE_JPWL */
2035 * Writes the SOC marker (Start Of Codestream)
2037 * @param p_stream the stream to write data to.
2038 * @param p_j2k J2K codec.
2039 * @param p_manager the user event manager.
2041 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2042 struct opj_stream_private *p_stream,
2043 struct opj_event_mgr * p_manager )
2045 /* 2 bytes will be written */
2046 OPJ_BYTE * l_start_stream = 00;
2049 assert(p_stream != 00);
2050 assert(p_j2k != 00);
2051 assert(p_manager != 00);
2053 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2055 /* write SOC identifier */
2056 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2058 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2064 /* update markers struct */
2066 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2068 assert( 0 && "TODO" );
2069 #endif /* USE_JPWL */
2078 * Reads a SOC marker (Start of Codestream)
2079 * @param p_header_data the data contained in the SOC box.
2080 * @param jp2 the jpeg2000 file codec.
2081 * @param p_header_size the size of the data contained in the SOC marker.
2082 * @param p_manager the user event manager.
2084 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2085 struct opj_stream_private *p_stream,
2086 struct opj_event_mgr * p_manager )
2088 OPJ_BYTE l_data [2];
2089 OPJ_UINT32 l_marker;
2092 assert(p_j2k != 00);
2093 assert(p_manager != 00);
2094 assert(p_stream != 00);
2096 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2100 opj_read_bytes(l_data,&l_marker,2);
2101 if (l_marker != J2K_MS_SOC) {
2105 /* Next marker should be a SIZ marker in the main header */
2106 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2108 /* FIXME move it in a index structure included in p_j2k*/
2109 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2111 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2113 /* Add the marker to the codestream index*/
2114 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2119 static void j2k_write_siz(opj_j2k_t *j2k) {
2123 opj_cio_t *cio = j2k->cio;
2124 opj_image_t *image = j2k->image;
2125 opj_cp_t *cp = j2k->cp;
2127 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2128 lenp = cio_tell(cio);
2130 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2131 cio_write(cio, image->x1, 4); /* Xsiz */
2132 cio_write(cio, image->y1, 4); /* Ysiz */
2133 cio_write(cio, image->x0, 4); /* X0siz */
2134 cio_write(cio, image->y0, 4); /* Y0siz */
2135 cio_write(cio, cp->tdx, 4); /* XTsiz */
2136 cio_write(cio, cp->tdy, 4); /* YTsiz */
2137 cio_write(cio, cp->tx0, 4); /* XT0siz */
2138 cio_write(cio, cp->ty0, 4); /* YT0siz */
2139 cio_write(cio, image->numcomps, 2); /* Csiz */
2140 for (i = 0; i < image->numcomps; i++) {
2141 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2142 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2143 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2145 len = cio_tell(cio) - lenp;
2146 cio_seek(cio, lenp);
2147 cio_write(cio, len, 2); /* Lsiz */
2148 cio_seek(cio, lenp + len);
2151 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2155 * Writes the SIZ marker (image and tile size)
2157 * @param p_stream the stream to write data to.
2158 * @param p_j2k J2K codec.
2159 * @param p_manager the user event manager.
2161 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2162 struct opj_stream_private *p_stream,
2163 struct opj_event_mgr * p_manager )
2166 OPJ_UINT32 l_size_len;
2167 OPJ_BYTE * l_current_ptr;
2168 opj_image_t * l_image = 00;
2169 opj_cp_v2_t *cp = 00;
2170 opj_image_comp_t * l_img_comp = 00;
2173 assert(p_stream != 00);
2174 assert(p_j2k != 00);
2175 assert(p_manager != 00);
2177 l_image = p_j2k->m_private_image;
2178 cp = &(p_j2k->m_cp);
2179 l_size_len = 40 + 3 * l_image->numcomps;
2180 l_img_comp = l_image->comps;
2182 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2184 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2185 = (OPJ_BYTE*)opj_realloc(
2186 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2188 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2192 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2195 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2197 /* write SOC identifier */
2198 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2201 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2204 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2207 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2210 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2213 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2216 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2219 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2222 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2225 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2228 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2231 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2234 for (i = 0; i < l_image->numcomps; ++i) {
2235 /* TODO here with MCT ? */
2236 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2239 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2242 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2248 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) {
2258 * Reads a SIZ marker (image and tile size)
2259 * @param p_header_data the data contained in the SIZ box.
2260 * @param jp2 the jpeg2000 file codec.
2261 * @param p_header_size the size of the data contained in the SIZ marker.
2262 * @param p_manager the user event manager.
2264 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2265 OPJ_BYTE * p_header_data,
2266 OPJ_UINT32 p_header_size,
2267 opj_event_mgr_t * p_manager
2270 OPJ_UINT32 l_size, i;
2271 OPJ_UINT32 l_nb_comp;
2272 OPJ_UINT32 l_nb_comp_remain;
2273 OPJ_UINT32 l_remaining_size;
2274 OPJ_UINT32 l_nb_tiles;
2276 opj_image_t *l_image = 00;
2277 opj_cp_v2_t *l_cp = 00;
2278 opj_image_comp_t * l_img_comp = 00;
2279 opj_tcp_v2_t * l_current_tile_param = 00;
2282 assert(p_j2k != 00);
2283 assert(p_manager != 00);
2284 assert(p_header_data != 00);
2286 l_image = p_j2k->m_private_image;
2287 l_cp = &(p_j2k->m_cp);
2289 /* minimum size == 39 - 3 (= minimum component parameter) */
2290 if (p_header_size < 36) {
2291 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2295 l_remaining_size = p_header_size - 36;
2296 l_nb_comp = l_remaining_size / 3;
2297 l_nb_comp_remain = l_remaining_size % 3;
2298 if (l_nb_comp_remain != 0){
2299 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2303 l_size = p_header_size + 2; /* Lsiz */
2305 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2307 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2308 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2310 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2312 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2314 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2316 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2318 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2320 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2322 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2324 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2327 l_image->numcomps = (OPJ_UINT16) l_tmp;
2329 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2333 if (l_image->numcomps != l_nb_comp) {
2334 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);
2339 if (l_cp->correct) {
2340 /* if JPWL is on, we check whether TX errors have damaged
2341 too much the SIZ parameters */
2342 if (!(l_image->x1 * l_image->y1)) {
2343 opj_event_msg_v2(p_manager, EVT_ERROR,
2344 "JPWL: bad image size (%d x %d)\n",
2345 l_image->x1, l_image->y1);
2346 if (!JPWL_ASSUME || JPWL_ASSUME) {
2347 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2352 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2353 if (l_image->numcomps != ((len - 38) / 3)) {
2354 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2355 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2356 l_image->numcomps, ((len - 38) / 3));
2358 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2361 */ /* we try to correct */
2362 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2363 if (l_image->numcomps < ((len - 38) / 3)) {
2364 len = 38 + 3 * l_image->numcomps;
2365 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2368 l_image->numcomps = ((len - 38) / 3);
2369 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2375 /* update components number in the jpwl_exp_comps filed */
2376 l_cp->exp_comps = l_image->numcomps;
2378 #endif /* USE_JPWL */
2380 /* Allocate the resulting image components */
2381 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2382 if (l_image->comps == 00){
2383 l_image->numcomps = 0;
2384 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2388 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2389 l_img_comp = l_image->comps;
2391 /* Read the component information */
2392 for (i = 0; i < l_image->numcomps; ++i){
2394 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2396 l_img_comp->prec = (tmp & 0x7f) + 1;
2397 l_img_comp->sgnd = tmp >> 7;
2398 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2400 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2401 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2403 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2406 if (l_cp->correct) {
2407 /* if JPWL is on, we check whether TX errors have damaged
2408 too much the SIZ parameters, again */
2409 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2410 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2411 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2412 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2414 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2417 /* we try to correct */
2418 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2419 if (!l_image->comps[i].dx) {
2420 l_image->comps[i].dx = 1;
2421 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2422 i, l_image->comps[i].dx);
2424 if (!l_image->comps[i].dy) {
2425 l_image->comps[i].dy = 1;
2426 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2427 i, l_image->comps[i].dy);
2431 #endif /* USE_JPWL */
2432 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2433 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2437 /* Compute the number of tiles */
2438 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2439 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2440 l_nb_tiles = l_cp->tw * l_cp->th;
2442 /* Define the tiles which will be decoded */
2443 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2444 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;
2445 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;
2446 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);
2447 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);
2450 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2451 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2452 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2453 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2457 if (l_cp->correct) {
2458 /* if JPWL is on, we check whether TX errors have damaged
2459 too much the SIZ parameters */
2460 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2461 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2462 "JPWL: bad number of tiles (%d x %d)\n",
2463 l_cp->tw, l_cp->th);
2465 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2468 /* we try to correct */
2469 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2472 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2475 if (l_cp->tw > l_cp->max_tiles) {
2477 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2478 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2479 l_cp->max_tiles, l_cp->tw);
2483 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2486 if (l_cp->th > l_cp->max_tiles) {
2488 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2489 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2490 l_cp->max_tiles, l_cp->th);
2494 #endif /* USE_JPWL */
2496 /* memory allocations */
2497 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2498 if (l_cp->tcps == 00) {
2499 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2502 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2505 if (l_cp->correct) {
2507 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2508 "JPWL: could not alloc tcps field of cp\n");
2509 if (!JPWL_ASSUME || JPWL_ASSUME) {
2510 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2515 #endif /* USE_JPWL */
2517 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2518 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2519 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2520 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2523 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2525 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2526 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2528 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2529 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2532 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2533 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2535 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2536 (opj_simple_mcc_decorrelation_data_t*)
2537 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2539 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2540 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2543 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));
2544 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2546 /* set up default dc level shift */
2547 for (i=0;i<l_image->numcomps;++i) {
2548 if (! l_image->comps[i].sgnd) {
2549 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2553 l_current_tile_param = l_cp->tcps;
2554 for (i = 0; i < l_nb_tiles; ++i) {
2555 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2556 if (l_current_tile_param->tccps == 00) {
2557 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2560 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2562 ++l_current_tile_param;
2565 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2566 opj_image_comp_header_update(l_image,l_cp);
2573 static void j2k_write_com(opj_j2k_t *j2k) {
2577 if(j2k->cp->comment) {
2578 opj_cio_t *cio = j2k->cio;
2579 char *comment = j2k->cp->comment;
2581 cio_write(cio, J2K_MS_COM, 2);
2582 lenp = cio_tell(cio);
2584 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2585 for (i = 0; i < strlen(comment); i++) {
2586 cio_write(cio, comment[i], 1);
2588 len = cio_tell(cio) - lenp;
2589 cio_seek(cio, lenp);
2590 cio_write(cio, len, 2);
2591 cio_seek(cio, lenp + len);
2595 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2601 * Writes the COM marker (comment)
2603 * @param p_stream the stream to write data to.
2604 * @param p_j2k J2K codec.
2605 * @param p_manager the user event manager.
2607 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2608 struct opj_stream_private *p_stream,
2609 struct opj_event_mgr * p_manager )
2611 OPJ_UINT32 l_comment_size;
2612 OPJ_UINT32 l_total_com_size;
2613 const OPJ_CHAR *l_comment;
2614 OPJ_BYTE * l_current_ptr = 00;
2617 assert(p_j2k != 00);
2618 assert(p_stream != 00);
2619 assert(p_manager != 00);
2621 l_comment = p_j2k->m_cp.comment;
2622 l_comment_size = strlen(l_comment);
2623 l_total_com_size = l_comment_size + 6;
2625 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2626 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2627 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2630 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2634 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2637 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2639 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2642 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2645 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2648 memcpy( l_current_ptr,l_comment,l_comment_size);
2650 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) {
2657 static void j2k_read_com(opj_j2k_t *j2k) {
2660 opj_cio_t *cio = j2k->cio;
2662 len = cio_read(cio, 2);
2663 cio_skip(cio, len - 2);
2666 * Reads a COM marker (comments)
2667 * @param p_header_data the data contained in the COM box.
2668 * @param jp2 the jpeg2000 file codec.
2669 * @param p_header_size the size of the data contained in the COM marker.
2670 * @param p_manager the user event manager.
2672 opj_bool j2k_read_com_v2 (
2673 opj_j2k_v2_t *p_j2k,
2674 OPJ_BYTE * p_header_data,
2675 OPJ_UINT32 p_header_size,
2676 struct opj_event_mgr * p_manager
2680 assert(p_j2k != 00);
2681 assert(p_manager != 00);
2682 assert(p_header_data != 00);
2683 (void)p_header_size;
2688 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2691 opj_cp_t *cp = j2k->cp;
2692 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2693 opj_tccp_t *tccp = &tcp->tccps[compno];
2694 opj_cio_t *cio = j2k->cio;
2696 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2697 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2698 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2699 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2700 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2702 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2703 for (i = 0; i < tccp->numresolutions; i++) {
2704 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2709 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2712 opj_cp_t *cp = j2k->cp;
2713 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2714 opj_tccp_t *tccp = &tcp->tccps[compno];
2715 opj_cio_t *cio = j2k->cio;
2717 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2719 /* If user wants to remove more resolutions than the codestream contains, return error*/
2720 assert(cp->reduce >= 0);
2721 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2722 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2723 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2724 j2k->state |= J2K_STATE_ERR;
2727 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2728 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2729 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2730 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2731 if (tccp->csty & J2K_CP_CSTY_PRT) {
2732 for (i = 0; i < tccp->numresolutions; i++) {
2733 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2734 tccp->prcw[i] = tmp & 0xf;
2735 tccp->prch[i] = tmp >> 4;
2740 if(j2k->cstr_info && compno == 0) {
2741 for (i = 0; i < tccp->numresolutions; i++) {
2742 if (tccp->csty & J2K_CP_CSTY_PRT) {
2743 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2744 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2747 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2748 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2755 static void j2k_write_cod(opj_j2k_t *j2k) {
2756 opj_cp_t *cp = NULL;
2757 opj_tcp_t *tcp = NULL;
2760 opj_cio_t *cio = j2k->cio;
2762 cio_write(cio, J2K_MS_COD, 2); /* COD */
2764 lenp = cio_tell(cio);
2768 tcp = &cp->tcps[j2k->curtileno];
2770 cio_write(cio, tcp->csty, 1); /* Scod */
2771 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2772 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2773 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2775 j2k_write_cox(j2k, 0);
2776 len = cio_tell(cio) - lenp;
2777 cio_seek(cio, lenp);
2778 cio_write(cio, len, 2); /* Lcod */
2779 cio_seek(cio, lenp + len);
2782 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2787 * Writes the COD marker (Coding style default)
2789 * @param p_stream the stream to write data to.
2790 * @param p_j2k J2K codec.
2791 * @param p_manager the user event manager.
2793 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2794 struct opj_stream_private *p_stream,
2795 struct opj_event_mgr * p_manager )
2797 opj_cp_v2_t *l_cp = 00;
2798 opj_tcp_v2_t *l_tcp = 00;
2799 OPJ_UINT32 l_code_size,l_remaining_size;
2800 OPJ_BYTE * l_current_data = 00;
2803 assert(p_j2k != 00);
2804 assert(p_manager != 00);
2805 assert(p_stream != 00);
2807 l_cp = &(p_j2k->m_cp);
2808 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2809 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2810 l_remaining_size = l_code_size;
2812 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2813 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2814 = (OPJ_BYTE*)opj_realloc(
2815 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2818 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2822 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2825 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2827 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2828 l_current_data += 2;
2830 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2831 l_current_data += 2;
2833 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2836 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2839 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2842 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2845 l_remaining_size -= 9;
2847 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2848 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2852 if (l_remaining_size != 0) {
2853 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2857 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) {
2866 * Reads a COD marker (Coding Styke defaults)
2867 * @param p_header_data the data contained in the COD box.
2868 * @param p_j2k the jpeg2000 codec.
2869 * @param p_header_size the size of the data contained in the COD marker.
2870 * @param p_manager the user event manager.
2872 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2873 OPJ_BYTE * p_header_data,
2874 OPJ_UINT32 p_header_size,
2875 opj_event_mgr_t * p_manager
2881 opj_cp_v2_t *l_cp = 00;
2882 opj_tcp_v2_t *l_tcp = 00;
2883 opj_image_t *l_image = 00;
2886 assert(p_header_data != 00);
2887 assert(p_j2k != 00);
2888 assert(p_manager != 00);
2890 l_image = p_j2k->m_private_image;
2891 l_cp = &(p_j2k->m_cp);
2893 /* If we are in the first tile-part header of the current tile */
2894 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2895 &l_cp->tcps[p_j2k->m_current_tile_number] :
2896 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2898 /* Make sure room is sufficient */
2899 if (p_header_size < 5) {
2900 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2904 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2906 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2908 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2909 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2912 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2913 if (l_cp->m_specific_param.m_dec.m_layer) {
2914 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2917 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2920 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2924 for (i = 0; i < l_image->numcomps; ++i) {
2925 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2928 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2929 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2933 if (p_header_size != 0) {
2934 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2938 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2939 j2k_copy_tile_component_parameters(p_j2k);
2942 #ifdef WIP_REMOVE_MSD
2943 if (p_j2k->cstr_info) {
2944 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2945 p_j2k->cstr_info->prog = l_tcp->prg;
2946 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2947 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2948 for (i = 0; i < l_image->numcomps; ++i) {
2949 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2957 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2960 opj_cp_t *cp = j2k->cp;
2961 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2962 opj_image_t *image = j2k->image;
2963 opj_cio_t *cio = j2k->cio;
2965 cio_write(cio, J2K_MS_COC, 2); /* COC */
2966 lenp = cio_tell(cio);
2968 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2969 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2970 j2k_write_cox(j2k, compno);
2971 len = cio_tell(cio) - lenp;
2972 cio_seek(cio, lenp);
2973 cio_write(cio, len, 2); /* Lcoc */
2974 cio_seek(cio, lenp + len);
2978 * Writes the COC marker (Coding style component)
2980 * @param p_comp_no the index of the component to output.
2981 * @param p_stream the stream to write data to.
2982 * @param p_j2k J2K codec.
2983 * @param p_manager the user event manager.
2985 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2986 OPJ_UINT32 p_comp_no,
2987 struct opj_stream_private *p_stream,
2988 struct opj_event_mgr * p_manager )
2990 OPJ_UINT32 l_coc_size,l_remaining_size;
2991 OPJ_UINT32 l_comp_room;
2994 assert(p_j2k != 00);
2995 assert(p_manager != 00);
2996 assert(p_stream != 00);
2998 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3000 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3002 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3003 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3004 = (OPJ_BYTE*)opj_realloc(
3005 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3007 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3011 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3014 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);
3016 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) {
3024 * Writes the COC marker (Coding style component)
3026 * @param p_comp_no the index of the component to output.
3027 * @param p_stream the stream to write data to.
3028 * @param p_j2k J2K codec.
3029 * @param p_manager the user event manager.
3031 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3032 OPJ_UINT32 p_comp_no,
3034 OPJ_UINT32 * p_data_written,
3035 struct opj_event_mgr * p_manager )
3037 opj_cp_v2_t *l_cp = 00;
3038 opj_tcp_v2_t *l_tcp = 00;
3039 OPJ_UINT32 l_coc_size,l_remaining_size;
3040 OPJ_BYTE * l_current_data = 00;
3041 opj_image_t *l_image = 00;
3042 OPJ_UINT32 l_comp_room;
3045 assert(p_j2k != 00);
3046 assert(p_manager != 00);
3048 l_cp = &(p_j2k->m_cp);
3049 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3050 l_image = p_j2k->m_private_image;
3051 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3053 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3054 l_remaining_size = l_coc_size;
3056 l_current_data = p_data;
3058 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3059 l_current_data += 2;
3061 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3062 l_current_data += 2;
3064 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3065 l_current_data+=l_comp_room;
3067 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3070 l_remaining_size -= (5 + l_comp_room);
3071 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3072 * p_data_written = l_coc_size;
3076 * Gets the maximum size taken by a coc.
3078 * @param p_j2k the jpeg2000 codec to use.
3080 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3083 OPJ_UINT32 l_nb_comp;
3084 OPJ_UINT32 l_nb_tiles;
3085 OPJ_UINT32 l_max = 0;
3089 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3090 l_nb_comp = p_j2k->m_private_image->numcomps;
3092 for (i=0;i<l_nb_tiles;++i) {
3093 for (j=0;j<l_nb_comp;++j) {
3094 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3103 * Reads a COC marker (Coding Style Component)
3104 * @param p_header_data the data contained in the COC box.
3105 * @param p_j2k the jpeg2000 codec.
3106 * @param p_header_size the size of the data contained in the COC marker.
3107 * @param p_manager the user event manager.
3109 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
3110 OPJ_BYTE * p_header_data,
3111 OPJ_UINT32 p_header_size,
3112 opj_event_mgr_t * p_manager
3115 opj_cp_v2_t *l_cp = NULL;
3116 opj_tcp_v2_t *l_tcp = NULL;
3117 opj_image_t *l_image = NULL;
3118 OPJ_UINT32 l_comp_room;
3119 OPJ_UINT32 l_comp_no;
3122 assert(p_header_data != 00);
3123 assert(p_j2k != 00);
3124 assert(p_manager != 00);
3126 l_cp = &(p_j2k->m_cp);
3127 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3128 &l_cp->tcps[p_j2k->m_current_tile_number] :
3129 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3130 l_image = p_j2k->m_private_image;
3132 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3134 /* make sure room is sufficient*/
3135 if (p_header_size < l_comp_room + 1) {
3136 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3139 p_header_size -= l_comp_room + 1;
3141 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3142 p_header_data += l_comp_room;
3143 if (l_comp_no >= l_image->numcomps) {
3144 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3148 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3151 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3152 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3156 if (p_header_size != 0) {
3157 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3163 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3164 int bandno, numbands;
3167 opj_cp_t *cp = j2k->cp;
3168 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3169 opj_tccp_t *tccp = &tcp->tccps[compno];
3170 opj_cio_t *cio = j2k->cio;
3172 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3173 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3175 for (bandno = 0; bandno < numbands; bandno++) {
3176 expn = tccp->stepsizes[bandno].expn;
3177 mant = tccp->stepsizes[bandno].mant;
3179 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3180 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3182 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3187 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3189 int bandno, numbands;
3191 opj_cp_t *cp = j2k->cp;
3192 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3193 opj_tccp_t *tccp = &tcp->tccps[compno];
3194 opj_cio_t *cio = j2k->cio;
3196 tmp = cio_read(cio, 1); /* Sqcx */
3197 tccp->qntsty = tmp & 0x1f;
3198 tccp->numgbits = tmp >> 5;
3199 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3200 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3203 if (j2k->cp->correct) {
3205 /* if JPWL is on, we check whether there are too many subbands */
3206 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3207 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3208 "JPWL: bad number of subbands in Sqcx (%d)\n",
3211 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3214 /* we try to correct */
3216 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3217 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3224 /* We check whether there are too many subbands */
3225 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3226 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3227 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3228 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3231 #endif /* USE_JPWL */
3233 for (bandno = 0; bandno < numbands; bandno++) {
3235 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3236 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3239 tmp = cio_read(cio, 2); /* SPqcx_i */
3243 if (bandno < J2K_MAXBANDS){
3244 tccp->stepsizes[bandno].expn = expn;
3245 tccp->stepsizes[bandno].mant = mant;
3249 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3250 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3251 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3252 tccp->stepsizes[bandno].expn =
3253 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3254 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3255 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3261 static void j2k_write_qcd(opj_j2k_t *j2k) {
3264 opj_cio_t *cio = j2k->cio;
3266 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3267 lenp = cio_tell(cio);
3269 j2k_write_qcx(j2k, 0);
3270 len = cio_tell(cio) - lenp;
3271 cio_seek(cio, lenp);
3272 cio_write(cio, len, 2); /* Lqcd */
3273 cio_seek(cio, lenp + len);
3276 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3280 * Writes the QCD marker (quantization default)
3282 * @param p_comp_number the index of the component to output.
3283 * @param p_stream the stream to write data to.
3284 * @param p_j2k J2K codec.
3285 * @param p_manager the user event manager.
3287 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3288 struct opj_stream_private *p_stream,
3289 struct opj_event_mgr * p_manager )
3291 opj_cp_v2_t *l_cp = 00;
3292 opj_tcp_v2_t *l_tcp = 00;
3293 OPJ_UINT32 l_qcd_size,l_remaining_size;
3294 OPJ_BYTE * l_current_data = 00;
3297 assert(p_j2k != 00);
3298 assert(p_manager != 00);
3299 assert(p_stream != 00);
3301 l_cp = &(p_j2k->m_cp);
3302 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3303 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3304 l_remaining_size = l_qcd_size;
3306 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3307 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3308 = (OPJ_BYTE*)opj_realloc(
3309 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3312 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3316 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3319 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3321 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3322 l_current_data += 2;
3324 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3325 l_current_data += 2;
3327 l_remaining_size -= 4;
3329 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3330 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3334 if (l_remaining_size != 0) {
3335 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3339 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) {
3347 static void j2k_read_qcd(opj_j2k_t *j2k) {
3351 opj_cio_t *cio = j2k->cio;
3352 opj_image_t *image = j2k->image;
3354 len = cio_read(cio, 2); /* Lqcd */
3355 pos = cio_tell(cio);
3356 for (i = 0; i < image->numcomps; i++) {
3358 j2k_read_qcx(j2k, i, len - 2);
3363 * Reads a QCD marker (Quantization defaults)
3364 * @param p_header_data the data contained in the QCD box.
3365 * @param p_j2k the jpeg2000 codec.
3366 * @param p_header_size the size of the data contained in the QCD marker.
3367 * @param p_manager the user event manager.
3369 opj_bool j2k_read_qcd_v2 (
3370 opj_j2k_v2_t *p_j2k,
3371 OPJ_BYTE * p_header_data,
3372 OPJ_UINT32 p_header_size,
3373 struct opj_event_mgr * p_manager
3377 assert(p_header_data != 00);
3378 assert(p_j2k != 00);
3379 assert(p_manager != 00);
3381 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3382 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3386 if (p_header_size != 0) {
3387 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3391 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3392 j2k_copy_tile_quantization_parameters(p_j2k);
3397 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3400 opj_cio_t *cio = j2k->cio;
3402 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3403 lenp = cio_tell(cio);
3405 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3406 j2k_write_qcx(j2k, compno);
3407 len = cio_tell(cio) - lenp;
3408 cio_seek(cio, lenp);
3409 cio_write(cio, len, 2); /* Lqcc */
3410 cio_seek(cio, lenp + len);
3414 * Writes the QCC marker (quantization component)
3416 * @param p_comp_no the index of the component to output.
3417 * @param p_stream the stream to write data to.
3418 * @param p_j2k J2K codec.
3419 * @param p_manager the user event manager.
3421 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3422 OPJ_UINT32 p_comp_no,
3423 struct opj_stream_private *p_stream,
3424 struct opj_event_mgr * p_manager )
3426 OPJ_UINT32 l_qcc_size,l_remaining_size;
3429 assert(p_j2k != 00);
3430 assert(p_manager != 00);
3431 assert(p_stream != 00);
3433 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3434 l_remaining_size = l_qcc_size;
3436 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3437 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3438 = (OPJ_BYTE*)opj_realloc(
3439 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3441 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3445 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3448 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);
3450 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) {
3458 * Writes the QCC marker (quantization component)
3460 * @param p_comp_no the index of the component to output.
3461 * @param p_stream the stream to write data to.
3462 * @param p_j2k J2K codec.
3463 * @param p_manager the user event manager.
3465 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3466 OPJ_UINT32 p_comp_no,
3468 OPJ_UINT32 * p_data_written,
3469 struct opj_event_mgr * p_manager )
3471 OPJ_UINT32 l_qcc_size,l_remaining_size;
3472 OPJ_BYTE * l_current_data = 00;
3475 assert(p_j2k != 00);
3476 assert(p_manager != 00);
3478 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3479 l_remaining_size = l_qcc_size;
3481 l_current_data = p_data;
3483 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3484 l_current_data += 2;
3486 if (p_j2k->m_private_image->numcomps <= 256) {
3489 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3490 l_current_data += 2;
3492 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3495 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3496 l_remaining_size -= 6;
3499 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3500 l_current_data += 2;
3502 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3505 l_remaining_size -= 6;
3508 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3510 *p_data_written = l_qcc_size;
3514 * Gets the maximum size taken by a qcc.
3516 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3518 return j2k_get_max_coc_size(p_j2k);
3521 static void j2k_read_qcc(opj_j2k_t *j2k) {
3523 int numcomp = j2k->image->numcomps;
3524 opj_cio_t *cio = j2k->cio;
3526 len = cio_read(cio, 2); /* Lqcc */
3527 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3530 if (j2k->cp->correct) {
3532 static int backup_compno = 0;
3534 /* compno is negative or larger than the number of components!!! */
3535 if ((compno < 0) || (compno >= numcomp)) {
3536 opj_event_msg(j2k->cinfo, EVT_ERROR,
3537 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3540 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3543 /* we try to correct */
3544 compno = backup_compno % numcomp;
3545 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3546 "- setting component number to %d\n",
3550 /* keep your private count of tiles */
3553 #endif /* USE_JPWL */
3555 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3559 * Reads a QCC marker (Quantization component)
3560 * @param p_header_data the data contained in the QCC box.
3561 * @param p_j2k the jpeg2000 codec.
3562 * @param p_header_size the size of the data contained in the QCC marker.
3563 * @param p_manager the user event manager.
3565 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3566 OPJ_BYTE * p_header_data,
3567 OPJ_UINT32 p_header_size,
3568 struct opj_event_mgr * p_manager)
3570 OPJ_UINT32 l_num_comp,l_comp_no;
3573 assert(p_header_data != 00);
3574 assert(p_j2k != 00);
3575 assert(p_manager != 00);
3577 l_num_comp = p_j2k->m_private_image->numcomps;
3579 if (l_num_comp <= 256) {
3580 if (p_header_size < 1) {
3581 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3584 opj_read_bytes(p_header_data,&l_comp_no,1);
3589 if (p_header_size < 2) {
3590 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3593 opj_read_bytes(p_header_data,&l_comp_no,2);
3599 if (p_j2k->m_cp.correct) {
3601 static OPJ_UINT32 backup_compno = 0;
3603 /* compno is negative or larger than the number of components!!! */
3604 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3605 opj_event_msg_v2(p_manager, EVT_ERROR,
3606 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3607 l_comp_no, l_num_comp);
3609 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3612 /* we try to correct */
3613 l_comp_no = backup_compno % l_num_comp;
3614 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3615 "- setting component number to %d\n",
3619 /* keep your private count of tiles */
3622 #endif /* USE_JPWL */
3624 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3625 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3629 if (p_header_size != 0) {
3630 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3638 static void j2k_write_poc(opj_j2k_t *j2k) {
3639 int len, numpchgs, i;
3641 int numcomps = j2k->image->numcomps;
3643 opj_cp_t *cp = j2k->cp;
3644 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3645 opj_tccp_t *tccp = &tcp->tccps[0];
3646 opj_cio_t *cio = j2k->cio;
3648 numpchgs = 1 + tcp->numpocs;
3649 cio_write(cio, J2K_MS_POC, 2); /* POC */
3650 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3651 cio_write(cio, len, 2); /* Lpoc */
3652 for (i = 0; i < numpchgs; i++) {
3653 opj_poc_t *poc = &tcp->pocs[i];
3654 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3655 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3656 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3657 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3658 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3659 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3660 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3661 poc->compno1 = int_min(poc->compno1, numcomps);
3662 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3667 * Writes the POC marker (Progression Order Change)
3669 * @param p_stream the stream to write data to.
3670 * @param p_j2k J2K codec.
3671 * @param p_manager the user event manager.
3673 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3674 struct opj_stream_private *p_stream,
3675 struct opj_event_mgr * p_manager )
3677 OPJ_UINT32 l_nb_comp;
3678 OPJ_UINT32 l_nb_poc;
3679 OPJ_UINT32 l_poc_size;
3680 OPJ_UINT32 l_written_size = 0;
3681 opj_tcp_v2_t *l_tcp = 00;
3682 opj_tccp_t *l_tccp = 00;
3683 OPJ_UINT32 l_poc_room;
3686 assert(p_j2k != 00);
3687 assert(p_manager != 00);
3688 assert(p_stream != 00);
3690 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3691 l_tccp = &l_tcp->tccps[0];
3692 l_nb_comp = p_j2k->m_private_image->numcomps;
3693 l_nb_poc = 1 + l_tcp->numpocs;
3695 if (l_nb_comp <= 256) {
3701 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3703 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3704 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3705 = (OPJ_BYTE*)opj_realloc(
3706 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3709 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3713 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3716 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3718 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) {
3727 * Writes the POC marker (Progression Order Change)
3729 * @param p_stream the stream to write data to.
3730 * @param p_j2k J2K codec.
3731 * @param p_manager the user event manager.
3733 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3735 OPJ_UINT32 * p_data_written,
3736 struct opj_event_mgr * p_manager )
3739 OPJ_BYTE * l_current_data = 00;
3740 OPJ_UINT32 l_nb_comp;
3741 OPJ_UINT32 l_nb_poc;
3742 OPJ_UINT32 l_poc_size;
3743 opj_image_t *l_image = 00;
3744 opj_tcp_v2_t *l_tcp = 00;
3745 opj_tccp_t *l_tccp = 00;
3746 opj_poc_t *l_current_poc = 00;
3747 OPJ_UINT32 l_poc_room;
3750 assert(p_j2k != 00);
3751 assert(p_manager != 00);
3753 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3754 l_tccp = &l_tcp->tccps[0];
3755 l_image = p_j2k->m_private_image;
3756 l_nb_comp = l_image->numcomps;
3757 l_nb_poc = 1 + l_tcp->numpocs;
3759 if (l_nb_comp <= 256) {
3766 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3768 l_current_data = p_data;
3770 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3771 l_current_data += 2;
3773 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3774 l_current_data += 2;
3776 l_current_poc = l_tcp->pocs;
3777 for (i = 0; i < l_nb_poc; ++i) {
3778 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3781 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3782 l_current_data+=l_poc_room;
3784 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3787 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3790 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3791 l_current_data+=l_poc_room;
3793 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3796 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3797 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3798 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3799 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3804 *p_data_written = l_poc_size;
3808 * Gets the maximum size taken by the writing of a POC.
3810 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3812 opj_tcp_v2_t * l_tcp = 00;
3813 OPJ_UINT32 l_nb_tiles = 0;
3814 OPJ_UINT32 l_max_poc = 0;
3817 l_tcp = p_j2k->m_cp.tcps;
3818 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3820 for (i=0;i<l_nb_tiles;++i) {
3821 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3827 return 4 + 9 * l_max_poc;
3831 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3833 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3836 OPJ_UINT32 l_nb_tiles;
3837 OPJ_UINT32 l_max = 0;
3838 opj_tcp_v2_t * l_tcp = 00;
3840 l_tcp = p_j2k->m_cp.tcps;
3841 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3843 for (i=0;i<l_nb_tiles;++i) {
3844 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3854 * Gets the maximum size taken by the headers of the SOT.
3856 * @param p_j2k the jpeg2000 codec to use.
3858 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3860 OPJ_UINT32 l_nb_bytes = 0;
3861 OPJ_UINT32 l_nb_comps;
3862 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3864 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3865 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3867 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3868 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3869 l_nb_bytes += l_nb_comps * l_coc_bytes;
3871 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3872 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3875 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3877 /*** DEVELOPER CORNER, Add room for your headers ***/
3882 static void j2k_read_poc(opj_j2k_t *j2k) {
3883 int len, numpchgs, i, old_poc;
3885 int numcomps = j2k->image->numcomps;
3887 opj_cp_t *cp = j2k->cp;
3888 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3889 opj_cio_t *cio = j2k->cio;
3891 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
3893 len = cio_read(cio, 2); /* Lpoc */
3894 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
3896 for (i = old_poc; i < numpchgs + old_poc; i++) {
3898 poc = &tcp->pocs[i];
3899 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
3900 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
3901 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
3902 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
3903 poc->compno1 = int_min(
3904 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
3905 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
3908 tcp->numpocs = numpchgs + old_poc - 1;
3912 * Reads a POC marker (Progression Order Change)
3914 * @param p_header_data the data contained in the POC box.
3915 * @param p_j2k the jpeg2000 codec.
3916 * @param p_header_size the size of the data contained in the POC marker.
3917 * @param p_manager the user event manager.
3919 opj_bool j2k_read_poc_v2 (
3920 opj_j2k_v2_t *p_j2k,
3921 OPJ_BYTE * p_header_data,
3922 OPJ_UINT32 p_header_size,
3923 struct opj_event_mgr * p_manager)
3925 OPJ_UINT32 i, l_nb_comp, l_tmp;
3926 opj_image_t * l_image = 00;
3927 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3928 OPJ_UINT32 l_chunk_size, l_comp_room;
3930 opj_cp_v2_t *l_cp = 00;
3931 opj_tcp_v2_t *l_tcp = 00;
3932 opj_poc_t *l_current_poc = 00;
3935 assert(p_header_data != 00);
3936 assert(p_j2k != 00);
3937 assert(p_manager != 00);
3939 l_image = p_j2k->m_private_image;
3940 l_nb_comp = l_image->numcomps;
3941 if (l_nb_comp <= 256) {
3947 l_chunk_size = 5 + 2 * l_comp_room;
3948 l_current_poc_nb = p_header_size / l_chunk_size;
3949 l_current_poc_remaining = p_header_size % l_chunk_size;
3951 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3952 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3956 l_cp = &(p_j2k->m_cp);
3957 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3958 &l_cp->tcps[p_j2k->m_current_tile_number] :
3959 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3960 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3961 l_current_poc_nb += l_old_poc_nb;
3963 assert(l_current_poc_nb < 32);
3965 /* now poc is in use.*/
3968 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3969 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3970 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3972 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3973 p_header_data+=l_comp_room;
3974 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3976 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3978 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3979 p_header_data+=l_comp_room;
3980 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3982 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3983 /* make sure comp is in acceptable bounds */
3984 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3988 l_tcp->numpocs = l_current_poc_nb - 1;
3992 static void j2k_read_crg(opj_j2k_t *j2k) {
3993 int len, i, Xcrg_i, Ycrg_i;
3995 opj_cio_t *cio = j2k->cio;
3996 int numcomps = j2k->image->numcomps;
3998 len = cio_read(cio, 2); /* Lcrg */
3999 for (i = 0; i < numcomps; i++) {
4000 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
4001 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4006 * Reads a CRG marker (Component registration)
4008 * @param p_header_data the data contained in the TLM box.
4009 * @param p_j2k the jpeg2000 codec.
4010 * @param p_header_size the size of the data contained in the TLM marker.
4011 * @param p_manager the user event manager.
4013 opj_bool j2k_read_crg_v2 (
4014 opj_j2k_v2_t *p_j2k,
4015 OPJ_BYTE * p_header_data,
4016 OPJ_UINT32 p_header_size,
4017 struct opj_event_mgr * p_manager
4020 OPJ_UINT32 l_nb_comp;
4022 assert(p_header_data != 00);
4023 assert(p_j2k != 00);
4024 assert(p_manager != 00);
4026 l_nb_comp = p_j2k->m_private_image->numcomps;
4028 if (p_header_size != l_nb_comp *4) {
4029 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4032 /* Do not care of this at the moment since only local variables are set here */
4035 (i = 0; i < l_nb_comp; ++i)
4037 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4039 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4046 static void j2k_read_tlm(opj_j2k_t *j2k) {
4047 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4048 long int Ttlm_i, Ptlm_i;
4050 opj_cio_t *cio = j2k->cio;
4052 len = cio_read(cio, 2); /* Ltlm */
4053 Ztlm = cio_read(cio, 1); /* Ztlm */
4054 Stlm = cio_read(cio, 1); /* Stlm */
4055 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4056 SP = (Stlm >> 6) & 0x01;
4057 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4058 for (i = 0; i < tile_tlm; i++) {
4059 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4060 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4065 * Reads a TLM marker (Tile Length Marker)
4067 * @param p_header_data the data contained in the TLM box.
4068 * @param p_j2k the jpeg2000 codec.
4069 * @param p_header_size the size of the data contained in the TLM marker.
4070 * @param p_manager the user event manager.
4072 opj_bool j2k_read_tlm_v2 (
4073 opj_j2k_v2_t *p_j2k,
4074 OPJ_BYTE * p_header_data,
4075 OPJ_UINT32 p_header_size,
4076 struct opj_event_mgr * p_manager
4079 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4081 assert(p_header_data != 00);
4082 assert(p_j2k != 00);
4083 assert(p_manager != 00);
4085 if (p_header_size < 2) {
4086 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4091 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4093 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4096 l_ST = ((l_Stlm >> 4) & 0x3);
4097 l_SP = (l_Stlm >> 6) & 0x1;
4099 l_Ptlm_size = (l_SP + 1) * 2;
4100 l_quotient = l_Ptlm_size + l_ST;
4102 l_tot_num_tp = p_header_size / l_quotient;
4103 l_tot_num_tp_remaining = p_header_size % l_quotient;
4105 if (l_tot_num_tp_remaining != 0) {
4106 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4109 /* FIXME Do not care of this at the moment since only local variables are set here */
4112 (i = 0; i < l_tot_num_tp; ++i)
4114 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4115 p_header_data += l_ST;
4116 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4117 p_header_data += l_Ptlm_size;
4122 static void j2k_read_plm(opj_j2k_t *j2k) {
4123 int len, i, Zplm, Nplm, add, packet_len = 0;
4125 opj_cio_t *cio = j2k->cio;
4127 len = cio_read(cio, 2); /* Lplm */
4128 Zplm = cio_read(cio, 1); /* Zplm */
4131 Nplm = cio_read(cio, 4); /* Nplm */
4133 for (i = Nplm; i > 0; i--) {
4134 add = cio_read(cio, 1);
4136 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4137 if ((add & 0x80) == 0) {
4148 * Reads a PLM marker (Packet length, main header marker)
4150 * @param p_header_data the data contained in the TLM box.
4151 * @param p_j2k the jpeg2000 codec.
4152 * @param p_header_size the size of the data contained in the TLM marker.
4153 * @param p_manager the user event manager.
4155 opj_bool j2k_read_plm_v2 (
4156 opj_j2k_v2_t *p_j2k,
4157 OPJ_BYTE * p_header_data,
4158 OPJ_UINT32 p_header_size,
4159 struct opj_event_mgr * p_manager
4163 assert(p_header_data != 00);
4164 assert(p_j2k != 00);
4165 assert(p_manager != 00);
4167 if (p_header_size < 1) {
4168 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4171 /* Do not care of this at the moment since only local variables are set here */
4173 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4180 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4182 p_header_size -= (1+l_Nplm);
4186 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4190 (i = 0; i < l_Nplm; ++i)
4192 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4194 // take only the last seven bytes
4195 l_packet_len |= (l_tmp & 0x7f);
4203 // store packet length and proceed to next packet
4210 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4218 static void j2k_read_plt(opj_j2k_t *j2k) {
4219 int len, i, Zplt, packet_len = 0, add;
4221 opj_cio_t *cio = j2k->cio;
4223 len = cio_read(cio, 2); /* Lplt */
4224 Zplt = cio_read(cio, 1); /* Zplt */
4225 for (i = len - 3; i > 0; i--) {
4226 add = cio_read(cio, 1);
4227 packet_len = (packet_len << 7) + add; /* Iplt_i */
4228 if ((add & 0x80) == 0) {
4236 * Reads a PLT marker (Packet length, tile-part header)
4238 * @param p_header_data the data contained in the PLT box.
4239 * @param p_j2k the jpeg2000 codec.
4240 * @param p_header_size the size of the data contained in the PLT marker.
4241 * @param p_manager the user event manager.
4243 opj_bool j2k_read_plt_v2 (
4244 opj_j2k_v2_t *p_j2k,
4245 OPJ_BYTE * p_header_data,
4246 OPJ_UINT32 p_header_size,
4247 struct opj_event_mgr * p_manager
4250 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4253 assert(p_header_data != 00);
4254 assert(p_j2k != 00);
4255 assert(p_manager != 00);
4257 if (p_header_size < 1) {
4258 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4262 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4266 for (i = 0; i < p_header_size; ++i) {
4267 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4269 /* take only the last seven bytes */
4270 l_packet_len |= (l_tmp & 0x7f);
4275 /* store packet length and proceed to next packet */
4280 if (l_packet_len != 0) {
4281 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4288 static void j2k_read_ppm(opj_j2k_t *j2k) {
4289 int len, Z_ppm, i, j;
4292 opj_cp_t *cp = j2k->cp;
4293 opj_cio_t *cio = j2k->cio;
4295 len = cio_read(cio, 2);
4298 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4301 if (cp->ppm_previous == 0) {
4302 N_ppm = cio_read(cio, 4); /* N_ppm */
4305 N_ppm = cp->ppm_previous;
4308 if (Z_ppm == 0) { /* First PPM marker */
4309 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4310 cp->ppm_data_first = cp->ppm_data;
4311 cp->ppm_len = N_ppm;
4312 } else { /* NON-first PPM marker */
4313 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4316 /* this memory allocation check could be done even in non-JPWL cases */
4318 if (!cp->ppm_data) {
4319 opj_event_msg(j2k->cinfo, EVT_ERROR,
4320 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4322 if (!JPWL_ASSUME || JPWL_ASSUME) {
4323 opj_free(cp->ppm_data);
4324 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4331 cp->ppm_data_first = cp->ppm_data;
4332 cp->ppm_len = N_ppm + cp->ppm_store;
4334 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4335 cp->ppm_data[j] = cio_read(cio, 1);
4339 break; /* Case of non-finished packet header in present marker but finished in next one */
4341 cp->ppm_previous = i - 1;
4346 * Reads a PPM marker (Packed packet headers, main header)
4348 * @param p_header_data the data contained in the POC box.
4349 * @param p_j2k the jpeg2000 codec.
4350 * @param p_header_size the size of the data contained in the POC marker.
4351 * @param p_manager the user event manager.
4354 opj_bool j2k_read_ppm_v2 (
4355 opj_j2k_v2_t *p_j2k,
4356 OPJ_BYTE * p_header_data,
4357 OPJ_UINT32 p_header_size,
4358 struct opj_event_mgr * p_manager
4362 opj_cp_v2_t *l_cp = 00;
4363 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4366 assert(p_header_data != 00);
4367 assert(p_j2k != 00);
4368 assert(p_manager != 00);
4370 if (p_header_size < 1) {
4371 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4375 l_cp = &(p_j2k->m_cp);
4378 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4382 /* First PPM marker */
4384 if (p_header_size < 4) {
4385 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4389 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4393 /* First PPM marker: Initialization */
4394 l_cp->ppm_len = l_N_ppm;
4395 l_cp->ppm_data_size = 0;
4397 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4398 if (l_cp->ppm_buffer == 00) {
4399 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4402 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4404 l_cp->ppm_data = l_cp->ppm_buffer;
4408 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4409 if (p_header_size >= 4) {
4411 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4414 l_cp->ppm_len += l_N_ppm ;
4416 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4417 if (l_cp->ppm_buffer == 00) {
4418 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4421 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4423 l_cp->ppm_data = l_cp->ppm_buffer;
4430 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4432 if (l_remaining_data <= p_header_size) {
4433 /* we must store less information than available in the packet */
4434 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4435 l_cp->ppm_data_size = l_cp->ppm_len;
4436 p_header_size -= l_remaining_data;
4437 p_header_data += l_remaining_data;
4440 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4441 l_cp->ppm_data_size += p_header_size;
4442 p_header_data += p_header_size;
4455 * Reads a PPM marker (Packed packet headers, main header)
4457 * @param p_header_data the data contained in the POC box.
4458 * @param p_j2k the jpeg2000 codec.
4459 * @param p_header_size the size of the data contained in the POC marker.
4460 * @param p_manager the user event manager.
4462 opj_bool j2k_read_ppm_v3 (
4463 opj_j2k_v2_t *p_j2k,
4464 OPJ_BYTE * p_header_data,
4465 OPJ_UINT32 p_header_size,
4466 struct opj_event_mgr * p_manager
4469 opj_cp_v2_t *l_cp = 00;
4470 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4473 assert(p_header_data != 00);
4474 assert(p_j2k != 00);
4475 assert(p_manager != 00);
4477 /* Minimum size of PPM marker is equal to the size of Zppm element */
4478 if (p_header_size < 1) {
4479 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4483 l_cp = &(p_j2k->m_cp);
4486 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4490 /* First PPM marker */
4492 /* We need now at least the Nppm^0 element */
4493 if (p_header_size < 4) {
4494 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4498 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4502 /* First PPM marker: Initialization */
4503 l_cp->ppm_len = l_N_ppm;
4504 l_cp->ppm_data_read = 0;
4506 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4507 if (l_cp->ppm_data == 00) {
4508 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4511 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4513 l_cp->ppm_data_current = l_cp->ppm_data;
4515 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4518 if (p_header_size < 4) {
4519 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4523 /* Uncompleted Ippm series in the previous PPM marker?*/
4524 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4525 /* Get the place where add the remaining Ippm series*/
4526 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4527 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4530 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4534 /* Increase the size of ppm_data to add the new Ippm series*/
4535 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4537 /* Keep the position of the place where concatenate the new series*/
4538 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4539 l_cp->ppm_len += l_N_ppm;
4544 l_remaining_data = p_header_size;
4546 while (l_remaining_data >= l_N_ppm) {
4547 /* read a complete Ippm series*/
4548 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4549 p_header_size -= l_N_ppm;
4550 p_header_data += l_N_ppm;
4552 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4556 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4561 l_remaining_data = p_header_size;
4565 l_remaining_data = p_header_size;
4567 /* Next Ippm series is a complete series ?*/
4568 if (l_remaining_data > l_N_ppm) {
4569 /* Increase the size of ppm_data to add the new Ippm series*/
4570 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4572 /* Keep the position of the place where concatenate the new series */
4573 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4574 l_cp->ppm_len += l_N_ppm;
4579 /* Need to read an incomplete Ippm series*/
4580 if (l_remaining_data) {
4581 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4583 /* Keep the position of the place where concatenate the new series*/
4584 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4585 l_cp->ppm_len += l_N_ppm;
4587 /* Read incomplete Ippm series*/
4588 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4589 p_header_size -= l_remaining_data;
4590 p_header_data += l_remaining_data;
4592 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4597 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4598 if (p_header_size >= 4) {
4600 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4603 l_cp->ppm_len += l_N_ppm ;
4605 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4606 if (l_cp->ppm_buffer == 00) {
4607 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4610 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4612 l_cp->ppm_data = l_cp->ppm_buffer;
4619 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4621 if (l_remaining_data <= p_header_size) {
4622 /* we must store less information than available in the packet */
4623 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4624 l_cp->ppm_data_size = l_cp->ppm_len;
4625 p_header_size -= l_remaining_data;
4626 p_header_data += l_remaining_data;
4629 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4630 l_cp->ppm_data_size += p_header_size;
4631 p_header_data += p_header_size;
4640 static void j2k_read_ppt(opj_j2k_t *j2k) {
4641 int len, Z_ppt, i, j = 0;
4643 opj_cp_t *cp = j2k->cp;
4644 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4645 opj_cio_t *cio = j2k->cio;
4647 len = cio_read(cio, 2);
4648 Z_ppt = cio_read(cio, 1);
4650 if (Z_ppt == 0) { /* First PPT marker */
4651 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4652 tcp->ppt_data_first = tcp->ppt_data;
4654 tcp->ppt_len = len - 3;
4655 } else { /* NON-first PPT marker */
4656 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4657 tcp->ppt_data_first = tcp->ppt_data;
4658 tcp->ppt_len = len - 3 + tcp->ppt_store;
4661 for (i = len - 3; i > 0; i--) {
4662 tcp->ppt_data[j] = cio_read(cio, 1);
4669 * Reads a PPT marker (Packed packet headers, tile-part header)
4671 * @param p_header_data the data contained in the PPT box.
4672 * @param p_j2k the jpeg2000 codec.
4673 * @param p_header_size the size of the data contained in the PPT marker.
4674 * @param p_manager the user event manager.
4676 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4677 OPJ_BYTE * p_header_data,
4678 OPJ_UINT32 p_header_size,
4679 struct opj_event_mgr * p_manager )
4681 opj_cp_v2_t *l_cp = 00;
4682 opj_tcp_v2_t *l_tcp = 00;
4686 assert(p_header_data != 00);
4687 assert(p_j2k != 00);
4688 assert(p_manager != 00);
4690 /* We need to have the Z_ppt element at minimum */
4691 if (p_header_size < 1) {
4692 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4696 l_cp = &(p_j2k->m_cp);
4698 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");
4702 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4705 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4709 /* Allocate buffer to read the packet header */
4711 /* First PPT marker */
4712 l_tcp->ppt_data_size = 0;
4713 l_tcp->ppt_len = p_header_size;
4715 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4716 if (l_tcp->ppt_buffer == 00) {
4717 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4720 l_tcp->ppt_data = l_tcp->ppt_buffer;
4722 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4725 l_tcp->ppt_len += p_header_size;
4727 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4728 if (l_tcp->ppt_buffer == 00) {
4729 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4732 l_tcp->ppt_data = l_tcp->ppt_buffer;
4734 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4737 /* Read packet header from buffer */
4738 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4740 l_tcp->ppt_data_size += p_header_size;
4746 * Writes the TLM marker (Tile Length Marker)
4748 * @param p_stream the stream to write data to.
4749 * @param p_j2k J2K codec.
4750 * @param p_manager the user event manager.
4752 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4753 struct opj_stream_private *p_stream,
4754 struct opj_event_mgr * p_manager )
4756 OPJ_BYTE * l_current_data = 00;
4757 OPJ_UINT32 l_tlm_size;
4760 assert(p_j2k != 00);
4761 assert(p_manager != 00);
4762 assert(p_stream != 00);
4764 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4766 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4767 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4768 = (OPJ_BYTE*)opj_realloc(
4769 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4772 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4776 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4779 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4781 /* change the way data is written to avoid seeking if possible */
4783 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4785 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4786 l_current_data += 2;
4788 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4789 l_current_data += 2;
4791 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4794 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4797 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4798 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) {
4805 static void j2k_write_tlm(opj_j2k_t *j2k){
4807 opj_cio_t *cio = j2k->cio;
4808 j2k->tlm_start = cio_tell(cio);
4809 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4810 lenp = 4 + (5*j2k->totnum_tp);
4811 cio_write(cio,lenp,2); /* Ltlm */
4812 cio_write(cio, 0,1); /* Ztlm=0*/
4813 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4814 cio_skip(cio,5*j2k->totnum_tp);
4817 static void j2k_write_sot(opj_j2k_t *j2k) {
4820 opj_cio_t *cio = j2k->cio;
4822 j2k->sot_start = cio_tell(cio);
4823 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4824 lenp = cio_tell(cio);
4825 cio_skip(cio, 2); /* Lsot (further) */
4826 cio_write(cio, j2k->curtileno, 2); /* Isot */
4827 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4828 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4829 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4830 len = cio_tell(cio) - lenp;
4831 cio_seek(cio, lenp);
4832 cio_write(cio, len, 2); /* Lsot */
4833 cio_seek(cio, lenp + len);
4837 /* update markers struct */
4838 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4839 #endif /* USE_JPWL */
4842 if( j2k->cstr_info && j2k->cur_tp_num==0){
4843 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4848 * Writes the SOT marker (Start of tile-part)
4850 * @param p_stream the stream to write data to.
4851 * @param p_j2k J2K codec.
4852 * @param p_manager the user event manager.
4854 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4856 OPJ_UINT32 * p_data_written,
4857 const struct opj_stream_private *p_stream,
4858 struct opj_event_mgr * p_manager )
4861 assert(p_j2k != 00);
4862 assert(p_manager != 00);
4863 assert(p_stream != 00);
4865 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4868 opj_write_bytes(p_data,10,2); /* Lsot */
4871 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4877 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4880 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4885 /* update markers struct */
4887 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4889 assert( 0 && "TODO" );
4890 #endif /* USE_JPWL */
4892 * p_data_written = 12;
4901 * Reads a PPT marker (Packed packet headers, tile-part header)
4903 * @param p_header_data the data contained in the PPT box.
4904 * @param p_j2k the jpeg2000 codec.
4905 * @param p_header_size the size of the data contained in the PPT marker.
4906 * @param p_manager the user event manager.
4908 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4909 OPJ_BYTE * p_header_data,
4910 OPJ_UINT32 p_header_size,
4911 opj_event_mgr_t * p_manager )
4913 opj_cp_v2_t *l_cp = 00;
4914 opj_tcp_v2_t *l_tcp = 00;
4915 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4916 OPJ_UINT32 l_current_part;
4917 OPJ_UINT32 l_tile_x,l_tile_y;
4920 assert(p_header_data != 00);
4921 assert(p_j2k != 00);
4922 assert(p_manager != 00);
4924 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4925 if (p_header_size != 8) {
4926 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4930 l_cp = &(p_j2k->m_cp);
4931 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4934 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4935 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4936 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4939 if (l_cp->correct) {
4941 int tileno = p_j2k->m_current_tile_number;
4942 static int backup_tileno = 0;
4944 /* tileno is negative or larger than the number of tiles!!! */
4945 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4946 opj_event_msg_v2(p_manager, EVT_ERROR,
4947 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4948 tileno, (l_cp->tw * l_cp->th));
4950 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4953 /* we try to correct */
4954 tileno = backup_tileno;
4955 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4956 "- setting tile number to %d\n",
4960 /* keep your private count of tiles */
4963 #endif /* USE_JPWL */
4965 /* look for the tile in the list of already processed tile (in parts). */
4966 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4967 /* since the time taken by this function can only grow at the time */
4969 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4972 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4973 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4975 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4981 if (l_cp->correct) {
4983 /* totlen is negative or larger than the bytes left!!! */
4984 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))) { */
4985 opj_event_msg_v2(p_manager, EVT_ERROR,
4986 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4987 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4989 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4992 /* we try to correct */
4994 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4995 "- setting Psot to %d => assuming it is the last tile\n",
4999 #endif /* USE_JPWL */
5001 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5003 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5004 "we assuming it is the last tile-part of the codestream.\n");
5005 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5008 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5011 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5014 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5015 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5016 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5017 if (l_tcp->m_nb_tile_parts) {
5018 if (l_current_part >= l_tcp->m_nb_tile_parts){
5019 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5020 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5021 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5025 l_tcp->m_nb_tile_parts = l_num_parts;
5028 /* If know the number of tile part header we will check if we didn't read the last*/
5029 if (l_tcp->m_nb_tile_parts) {
5030 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5031 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5035 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5036 /* Keep the size of data to skip after this marker */
5037 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5040 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5041 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5044 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5046 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5047 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5048 p_j2k->m_specific_param.m_decoder.m_skip_data =
5049 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5050 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5051 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5052 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5055 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5056 p_j2k->m_specific_param.m_decoder.m_skip_data =
5057 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5061 if (p_j2k->cstr_index)
5063 assert(p_j2k->cstr_index->tile_index != 00);
5064 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5065 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5067 if (l_num_parts != 0){
5068 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5069 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5072 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5073 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5074 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5076 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5077 (opj_tp_index_t*)opj_realloc(
5078 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5079 l_num_parts* sizeof(opj_tp_index_t));
5082 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5084 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5085 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5086 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5087 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5088 sizeof(opj_tp_index_t));
5091 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5092 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5093 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5094 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5095 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5096 * sizeof(opj_tp_index_t));
5105 /* 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 */
5106 /* if (p_j2k->cstr_info) {
5109 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5112 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5113 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5114 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5115 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5118 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5121 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5125 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5128 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5129 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5130 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5135 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5138 opj_tcp_t *tcp = NULL;
5139 opj_codestream_info_t *cstr_info = NULL;
5141 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5142 opj_cp_t *cp = j2k->cp;
5143 opj_cio_t *cio = j2k->cio;
5145 tcd->tp_num = j2k->tp_num ;
5146 tcd->cur_tp_num = j2k->cur_tp_num;
5148 cio_write(cio, J2K_MS_SOD, 2);
5150 if( j2k->cstr_info && j2k->cur_tp_num==0){
5151 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5154 if (j2k->curtileno == 0) {
5155 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5159 cstr_info = j2k->cstr_info;
5161 if (!j2k->cur_tp_num ) {
5162 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5163 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5166 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5167 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5171 /* update markers struct */
5172 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5173 #endif /* USE_JPWL */
5178 tcp = &cp->tcps[j2k->curtileno];
5179 for (layno = 0; layno < tcp->numlayers; layno++) {
5180 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5181 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5182 } else if (tcp->rates[layno]) {
5183 tcp->rates[layno]=1;
5186 if(j2k->cur_tp_num == 0){
5187 tcd->tcd_image->tiles->packno = 0;
5189 cstr_info->packno = 0;
5192 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5194 /* Writing Psot in SOT marker */
5195 totlen = cio_tell(cio) + l - j2k->sot_start;
5196 cio_seek(cio, j2k->sot_start + 6);
5197 cio_write(cio, totlen, 4);
5198 cio_seek(cio, j2k->sot_start + totlen);
5199 /* Writing Ttlm and Ptlm in TLM marker */
5201 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5202 cio_write(cio, j2k->curtileno, 1);
5203 cio_write(cio, totlen, 4);
5205 cio_seek(cio, j2k->sot_start + totlen);
5209 * Writes the SOD marker (Start of data)
5211 * @param p_stream the stream to write data to.
5212 * @param p_j2k J2K codec.
5213 * @param p_manager the user event manager.
5215 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5216 struct opj_tcd_v2 * p_tile_coder,
5218 OPJ_UINT32 * p_data_written,
5219 OPJ_UINT32 p_total_data_size,
5220 const struct opj_stream_private *p_stream,
5221 struct opj_event_mgr * p_manager )
5223 opj_tcp_v2_t *l_tcp = 00;
5224 opj_codestream_info_t *l_cstr_info = 00;
5225 opj_cp_v2_t *l_cp = 00;
5227 OPJ_UINT32 l_size_tile;
5228 OPJ_UINT32 l_remaining_data;
5231 assert(p_j2k != 00);
5232 assert(p_manager != 00);
5233 assert(p_stream != 00);
5235 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5238 /* make room for the EOF marker */
5239 l_remaining_data = p_total_data_size - 4;
5241 l_cp = &(p_j2k->m_cp);
5242 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5245 /* update tile coder */
5246 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5247 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5249 l_size_tile = l_cp->th * l_cp->tw;
5252 /* TODO mergeV2: check this part which use cstr_info */
5253 /*l_cstr_info = p_j2k->cstr_info;
5255 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5256 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5257 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5263 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5265 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5270 /* update markers struct */
5271 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5273 assert( 0 && "TODO" );
5274 #endif /* USE_JPWL */
5279 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5280 p_tile_coder->tcd_image->tiles->packno = 0;
5282 l_cstr_info->packno = 0;
5286 *p_data_written = 0;
5288 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)) {
5289 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5293 *p_data_written += 2;
5302 * Reads a SOD marker (Start Of Data)
5304 * @param p_header_data the data contained in the SOD box.
5305 * @param p_j2k the jpeg2000 codec.
5306 * @param p_header_size the size of the data contained in the SOD marker.
5307 * @param p_manager the user event manager.
5309 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
5310 opj_stream_private_t *p_stream,
5311 opj_event_mgr_t * p_manager
5314 OPJ_UINT32 l_current_read_size;
5315 opj_codestream_index_t * l_cstr_index = 00;
5316 OPJ_BYTE ** l_current_data = 00;
5317 opj_tcp_v2_t * l_tcp = 00;
5318 OPJ_UINT32 * l_tile_len = 00;
5321 assert(p_j2k != 00);
5322 assert(p_manager != 00);
5323 assert(p_stream != 00);
5325 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5327 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5328 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5329 // but we are in the last tile part,
5330 // so its result will fit on OPJ_UINT32 unless we find
5331 // a file with a single tile part of more than 4 GB...*/
5332 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5335 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5337 l_current_data = &(l_tcp->m_data);
5338 l_tile_len = &l_tcp->m_data_size;
5340 if (! *l_current_data) {
5341 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5344 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5347 if (*l_current_data == 00) {
5348 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5354 l_cstr_index = p_j2k->cstr_index;
5356 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5358 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5359 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5361 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5362 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5364 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5368 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5370 /*l_cstr_index->packno = 0;*/
5373 l_current_read_size = opj_stream_read_data( p_stream,
5374 *l_current_data + *l_tile_len,
5375 p_j2k->m_specific_param.m_decoder.m_sot_length,
5378 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5379 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5382 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5385 *l_tile_len += l_current_read_size;
5391 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5392 opj_cp_t *cp = j2k->cp;
5393 opj_tcp_t *tcp = &cp->tcps[tileno];
5394 opj_cio_t *cio = j2k->cio;
5395 int numcomps = j2k->image->numcomps;
5397 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5398 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5399 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5400 cio_write(cio, 0, 1); /* Srgn */
5401 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5405 * Writes the RGN marker (Region Of Interest)
5407 * @param p_tile_no the tile to output
5408 * @param p_comp_no the component to output
5409 * @param p_stream the stream to write data to.
5410 * @param p_j2k J2K codec.
5411 * @param p_manager the user event manager.
5413 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5414 OPJ_UINT32 p_tile_no,
5415 OPJ_UINT32 p_comp_no,
5416 struct opj_stream_private *p_stream,
5417 struct opj_event_mgr * p_manager )
5419 OPJ_BYTE * l_current_data = 00;
5420 OPJ_UINT32 l_nb_comp;
5421 OPJ_UINT32 l_rgn_size;
5422 opj_image_t *l_image = 00;
5423 opj_cp_v2_t *l_cp = 00;
5424 opj_tcp_v2_t *l_tcp = 00;
5425 opj_tccp_t *l_tccp = 00;
5426 OPJ_UINT32 l_comp_room;
5429 assert(p_j2k != 00);
5430 assert(p_manager != 00);
5431 assert(p_stream != 00);
5433 l_cp = &(p_j2k->m_cp);
5434 l_tcp = &l_cp->tcps[p_tile_no];
5435 l_tccp = &l_tcp->tccps[p_comp_no];
5437 l_nb_comp = l_image->numcomps;
5439 if (l_nb_comp <= 256) {
5446 l_rgn_size = 6 + l_comp_room;
5448 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5450 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5451 l_current_data += 2;
5453 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5454 l_current_data += 2;
5456 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5457 l_current_data+=l_comp_room;
5459 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5462 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5465 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) {
5472 static void j2k_read_rgn(opj_j2k_t *j2k) {
5473 int len, compno, roisty;
5475 opj_cp_t *cp = j2k->cp;
5476 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5477 opj_cio_t *cio = j2k->cio;
5478 int numcomps = j2k->image->numcomps;
5480 len = cio_read(cio, 2); /* Lrgn */
5481 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
5482 roisty = cio_read(cio, 1); /* Srgn */
5485 if (j2k->cp->correct) {
5486 /* totlen is negative or larger than the bytes left!!! */
5487 if (compno >= numcomps) {
5488 opj_event_msg(j2k->cinfo, EVT_ERROR,
5489 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5491 if (!JPWL_ASSUME || JPWL_ASSUME) {
5492 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5497 #endif /* USE_JPWL */
5499 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
5502 static void j2k_write_eoc(opj_j2k_t *j2k) {
5503 opj_cio_t *cio = j2k->cio;
5504 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5505 cio_write(cio, J2K_MS_EOC, 2);
5509 /* update markers struct */
5510 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5511 #endif /* USE_JPWL */
5516 * Writes the EOC marker (End of Codestream)
5518 * @param p_stream the stream to write data to.
5519 * @param p_j2k J2K codec.
5520 * @param p_manager the user event manager.
5522 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5523 struct opj_stream_private *p_stream,
5524 struct opj_event_mgr * p_manager )
5527 assert(p_j2k != 00);
5528 assert(p_manager != 00);
5529 assert(p_stream != 00);
5531 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5536 /* update markers struct */
5538 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5540 #endif /* USE_JPWL */
5542 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5546 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5555 * Reads a RGN marker (Region Of Interest)
5557 * @param p_header_data the data contained in the POC box.
5558 * @param p_j2k the jpeg2000 codec.
5559 * @param p_header_size the size of the data contained in the POC marker.
5560 * @param p_manager the user event manager.
5562 opj_bool j2k_read_rgn_v2 (
5563 opj_j2k_v2_t *p_j2k,
5564 OPJ_BYTE * p_header_data,
5565 OPJ_UINT32 p_header_size,
5566 struct opj_event_mgr * p_manager
5569 OPJ_UINT32 l_nb_comp;
5570 opj_image_t * l_image = 00;
5572 opj_cp_v2_t *l_cp = 00;
5573 opj_tcp_v2_t *l_tcp = 00;
5574 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5577 assert(p_header_data != 00);
5578 assert(p_j2k != 00);
5579 assert(p_manager != 00);
5581 l_image = p_j2k->m_private_image;
5582 l_nb_comp = l_image->numcomps;
5584 if (l_nb_comp <= 256) {
5589 if (p_header_size != 2 + l_comp_room) {
5590 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5594 l_cp = &(p_j2k->m_cp);
5595 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5596 &l_cp->tcps[p_j2k->m_current_tile_number] :
5597 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5599 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5600 p_header_data+=l_comp_room;
5601 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5605 if (l_cp->correct) {
5606 /* totlen is negative or larger than the bytes left!!! */
5607 if (l_comp_room >= l_nb_comp) {
5608 opj_event_msg_v2(p_manager, EVT_ERROR,
5609 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5610 l_comp_room, l_nb_comp);
5611 if (!JPWL_ASSUME || JPWL_ASSUME) {
5612 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5617 #endif /* USE_JPWL */
5619 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5626 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5628 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5631 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5638 * Updates the rates of the tcp.
5640 * @param p_stream the stream to write data to.
5641 * @param p_j2k J2K codec.
5642 * @param p_manager the user event manager.
5644 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5645 struct opj_stream_private *p_stream,
5646 struct opj_event_mgr * p_manager )
5648 opj_cp_v2_t * l_cp = 00;
5649 opj_image_t * l_image = 00;
5650 opj_tcp_v2_t * l_tcp = 00;
5651 opj_image_comp_t * l_img_comp = 00;
5654 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5655 OPJ_FLOAT32 * l_rates = 0;
5656 OPJ_FLOAT32 l_sot_remove;
5657 OPJ_UINT32 l_bits_empty, l_size_pixel;
5658 OPJ_UINT32 l_tile_size = 0;
5659 OPJ_UINT32 l_last_res;
5660 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5663 assert(p_j2k != 00);
5664 assert(p_manager != 00);
5665 assert(p_stream != 00);
5668 l_cp = &(p_j2k->m_cp);
5669 l_image = p_j2k->m_private_image;
5672 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5673 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5674 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5676 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5677 l_tp_stride_func = get_tp_stride;
5680 l_tp_stride_func = get_default_stride;
5683 for (i=0;i<l_cp->th;++i) {
5684 for (j=0;j<l_cp->tw;++j) {
5685 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5687 /* 4 borders of the tile rescale on the image if necessary */
5688 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5689 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5690 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5691 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5693 l_rates = l_tcp->rates;
5695 /* Modification of the RATE >> */
5697 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5699 ((*l_rates) * l_bits_empty)
5707 for (k = 1; k < l_tcp->numlayers; ++k) {
5709 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5711 ((*l_rates) * l_bits_empty)
5727 for (i=0;i<l_cp->th;++i) {
5728 for (j=0;j<l_cp->tw;++j) {
5729 l_rates = l_tcp->rates;
5732 *l_rates -= l_sot_remove;
5734 if (*l_rates < 30) {
5741 l_last_res = l_tcp->numlayers - 1;
5743 for (k = 1; k < l_last_res; ++k) {
5746 *l_rates -= l_sot_remove;
5748 if (*l_rates < *(l_rates - 1) + 10) {
5749 *l_rates = (*(l_rates - 1)) + 20;
5757 *l_rates -= (l_sot_remove + 2.f);
5759 if (*l_rates < *(l_rates - 1) + 10) {
5760 *l_rates = (*(l_rates - 1)) + 20;
5768 l_img_comp = l_image->comps;
5771 for (i=0;i<l_image->numcomps;++i) {
5772 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5774 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5782 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5784 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5786 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5787 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5788 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5789 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5793 if (l_cp->m_specific_param.m_enc.m_cinema) {
5794 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5795 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5796 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5800 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5801 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5807 static void j2k_read_eoc(opj_j2k_t *j2k) {
5809 opj_bool success = OPJ_FALSE;
5811 /* if packets should be decoded */
5812 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5813 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5814 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5815 for (i = 0; i < j2k->cp->tileno_size; i++) {
5816 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5817 if (j2k->cp->tileno[i] != -1)
5819 tileno = j2k->cp->tileno[i];
5820 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5821 opj_free(j2k->tile_data[tileno]);
5822 j2k->tile_data[tileno] = NULL;
5823 tcd_free_decode_tile(tcd, i);
5826 success = OPJ_FALSE;
5827 if (success == OPJ_FALSE) {
5828 j2k->state |= J2K_STATE_ERR;
5832 tcd_free_decode(tcd);
5835 /* if packets should not be decoded */
5837 for (i = 0; i < j2k->cp->tileno_size; i++) {
5838 tileno = j2k->cp->tileno[i];
5839 opj_free(j2k->tile_data[tileno]);
5840 j2k->tile_data[tileno] = NULL;
5843 if (j2k->state & J2K_STATE_ERR)
5844 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5846 j2k->state = J2K_STATE_MT;
5850 * Reads a EOC marker (End Of Codestream)
5852 * @param p_header_data the data contained in the SOD box.
5853 * @param p_j2k the jpeg2000 codec.
5854 * @param p_header_size the size of the data contained in the SOD marker.
5855 * @param p_manager the user event manager.
5858 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5859 struct opj_stream_private *p_stream,
5860 struct opj_event_mgr * p_manager )
5863 opj_tcd_v2_t * l_tcd = 00;
5864 OPJ_UINT32 l_nb_tiles;
5865 opj_tcp_v2_t * l_tcp = 00;
5869 assert(p_j2k != 00);
5870 assert(p_manager != 00);
5871 assert(p_stream != 00);
5873 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5874 l_tcp = p_j2k->m_cp.tcps;
5876 l_tcd = tcd_create_v2(OPJ_TRUE);
5878 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5882 for (i = 0; i < l_nb_tiles; ++i) {
5883 if (l_tcp->m_data) {
5884 if (! tcd_init_decode_tile(l_tcd, i)) {
5885 tcd_destroy_v2(l_tcd);
5886 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5890 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5894 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5899 j2k_tcp_destroy(l_tcp);
5903 tcd_destroy_v2(l_tcd);
5909 * Gets the offset of the header.
5911 * @param p_stream the stream to write data to.
5912 * @param p_j2k J2K codec.
5913 * @param p_manager the user event manager.
5915 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5916 struct opj_stream_private *p_stream,
5917 struct opj_event_mgr * p_manager )
5920 assert(p_j2k != 00);
5921 assert(p_manager != 00);
5922 assert(p_stream != 00);
5924 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5930 * Writes the MCT marker (Multiple Component Transform)
5932 * @param p_stream the stream to write data to.
5933 * @param p_j2k J2K codec.
5934 * @param p_manager the user event manager.
5936 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5937 struct opj_stream_private *p_stream,
5938 struct opj_event_mgr * p_manager )
5941 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5942 opj_mct_data_t * l_mct_record;
5943 opj_tcp_v2_t * l_tcp;
5946 assert(p_j2k != 00);
5947 assert(p_stream != 00);
5948 assert(p_manager != 00);
5950 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5954 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5955 l_mct_record = l_tcp->m_mct_records;
5957 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5959 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5966 l_mcc_record = l_tcp->m_mcc_records;
5968 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5970 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5977 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5985 * Writes the image components.
5987 * @param p_stream the stream to write data to.
5988 * @param p_j2k J2K codec.
5989 * @param p_manager the user event manager.
5991 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5992 struct opj_stream_private *p_stream,
5993 struct opj_event_mgr * p_manager )
5998 assert(p_j2k != 00);
5999 assert(p_manager != 00);
6000 assert(p_stream != 00);
6002 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6004 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6008 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6017 * Writes regions of interests.
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_regions( opj_j2k_v2_t *p_j2k,
6024 struct opj_stream_private *p_stream,
6025 struct opj_event_mgr * p_manager )
6028 const opj_tccp_t *l_tccp = 00;
6031 assert(p_j2k != 00);
6032 assert(p_manager != 00);
6033 assert(p_stream != 00);
6035 l_tccp = p_j2k->m_cp.tcps->tccps;
6037 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6038 if (l_tccp->roishift) {
6040 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6054 * @param p_stream the stream to write data to.
6055 * @param p_j2k J2K codec.
6056 * @param p_manager the user event manager.
6058 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6059 struct opj_stream_private *p_stream,
6060 struct opj_event_mgr * p_manager )
6062 opj_codestream_index_t * l_cstr_index = 00;
6065 assert(p_j2k != 00);
6066 assert(p_manager != 00);
6067 assert(p_stream != 00);
6069 l_cstr_index = p_j2k->cstr_index;
6071 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6073 /* The following adjustment is done to adjust the codestream size */
6074 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6075 /* the first bunch of bytes is not in the codestream */
6076 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6081 /* preparation of JPWL marker segments */
6085 /* encode according to JPWL */
6086 jpwl_encode(p_j2k, p_stream, image);
6090 assert( 0 && "TODO" );
6091 #endif /* USE_JPWL */
6096 typedef struct opj_dec_mstabent {
6099 /** value of the state when the marker can appear */
6101 /** action linked to the marker */
6102 void (*handler) (opj_j2k_t *j2k);
6103 } opj_dec_mstabent_t;
6105 opj_dec_mstabent_t j2k_dec_mstab[] = {
6106 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
6107 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
6108 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
6109 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6110 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
6111 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
6112 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
6113 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6114 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6115 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6116 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6117 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6118 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6119 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6120 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6121 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6123 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6124 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6127 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6128 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6129 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6130 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6131 #endif /* USE_JPWL */
6133 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6134 {J2K_MS_INSEC, 0, j2k_read_insec},
6135 #endif /* USE_JPSEC */
6137 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6140 static void j2k_read_unk(opj_j2k_t *j2k) {
6141 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6144 if (j2k->cp->correct) {
6146 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6147 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6148 id = cio_read(j2k->cio, 2);
6149 opj_event_msg(j2k->cinfo, EVT_ERROR,
6150 "JPWL: really don't know this marker %x\n",
6153 opj_event_msg(j2k->cinfo, EVT_ERROR,
6154 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6157 /* OK, activate this at your own risk!!! */
6158 /* we look for the marker at the minimum hamming distance from this */
6159 while (j2k_dec_mstab[m].id) {
6161 /* 1's where they differ */
6162 tmp_id = j2k_dec_mstab[m].id ^ id;
6164 /* compute the hamming distance between our id and the current */
6166 for (i = 0; i < 16; i++) {
6167 if ((tmp_id >> i) & 0x0001) {
6172 /* if current distance is smaller, set the minimum */
6173 if (cur_dist < min_dist) {
6174 min_dist = cur_dist;
6175 min_id = j2k_dec_mstab[m].id;
6178 /* jump to the next marker */
6182 /* do we substitute the marker? */
6183 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6184 opj_event_msg(j2k->cinfo, EVT_ERROR,
6185 "- marker %x is at distance %d from the read %x\n",
6186 min_id, min_dist, id);
6187 opj_event_msg(j2k->cinfo, EVT_ERROR,
6188 "- trying to substitute in place and crossing fingers!\n");
6189 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6190 cio_write(j2k->cio, min_id, 2);
6193 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6198 #endif /* USE_JPWL */
6203 * Reads an unknown marker
6205 * @param p_stream the stream object to read from.
6206 * @param p_j2k the jpeg2000 codec.
6207 * @param p_manager the user event manager.
6209 * @return true if the marker could be deduced.
6211 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6212 struct opj_stream_private *p_stream,
6213 OPJ_UINT32 *output_marker,
6214 struct opj_event_mgr * p_manager
6217 OPJ_UINT32 l_unknown_marker;
6218 const opj_dec_memory_marker_handler_t * l_marker_handler;
6219 OPJ_UINT32 l_size_unk = 2;
6222 assert(p_j2k != 00);
6223 assert(p_manager != 00);
6224 assert(p_stream != 00);
6226 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6229 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6230 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6231 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6235 /* read 2 bytes as the new marker ID*/
6236 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6238 if (!(l_unknown_marker < 0xff00)) {
6240 /* Get the marker handler from the marker ID*/
6241 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6243 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6244 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6248 if (l_marker_handler->id != J2K_MS_UNK) {
6249 /* Add the marker to the codestream index*/
6250 if (l_marker_handler->id != J2K_MS_SOT)
6251 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6252 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6254 break; /* next marker is known and well located */
6262 *output_marker = l_marker_handler->id ;
6268 Read the lookup table containing all the marker, status and action
6269 @param id Marker value
6271 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6272 opj_dec_mstabent_t *e;
6273 for (e = j2k_dec_mstab; e->id != 0; e++) {
6282 * Writes the MCT marker (Multiple Component Transform)
6284 * @param p_stream the stream to write data to.
6285 * @param p_j2k J2K codec.
6286 * @param p_manager the user event manager.
6288 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6289 opj_mct_data_t * p_mct_record,
6290 struct opj_stream_private *p_stream,
6291 struct opj_event_mgr * p_manager )
6293 OPJ_UINT32 l_mct_size;
6294 OPJ_BYTE * l_current_data = 00;
6298 assert(p_j2k != 00);
6299 assert(p_manager != 00);
6300 assert(p_stream != 00);
6302 l_mct_size = 10 + p_mct_record->m_data_size;
6304 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6305 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6306 = (OPJ_BYTE*)opj_realloc(
6307 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6310 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6314 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6317 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6319 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6320 l_current_data += 2;
6322 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6323 l_current_data += 2;
6325 opj_write_bytes(l_current_data,0,2); /* Zmct */
6326 l_current_data += 2;
6328 /* only one marker atm */
6329 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6331 opj_write_bytes(l_current_data,l_tmp,2);
6332 l_current_data += 2;
6334 opj_write_bytes(l_current_data,0,2); /* Ymct */
6337 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6339 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) {
6347 * Reads a MCT marker (Multiple Component Transform)
6349 * @param p_header_data the data contained in the MCT box.
6350 * @param p_j2k the jpeg2000 codec.
6351 * @param p_header_size the size of the data contained in the MCT marker.
6352 * @param p_manager the user event manager.
6354 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6355 OPJ_BYTE * p_header_data,
6356 OPJ_UINT32 p_header_size,
6357 struct opj_event_mgr * p_manager )
6360 opj_tcp_v2_t *l_tcp = 00;
6363 opj_mct_data_t * l_mct_data;
6366 assert(p_header_data != 00);
6367 assert(p_j2k != 00);
6369 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6370 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6371 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6373 if (p_header_size < 2) {
6374 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6379 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6382 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6386 if(p_header_size <= 6) {
6387 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6391 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6392 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6395 l_indix = l_tmp & 0xff;
6396 l_mct_data = l_tcp->m_mct_records;
6398 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6399 if (l_mct_data->m_index == l_indix) {
6406 if (i == l_tcp->m_nb_mct_records) {
6407 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6408 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6410 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));
6411 if(! l_tcp->m_mct_records) {
6412 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6416 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6417 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6420 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6423 if (l_mct_data->m_data) {
6424 opj_free(l_mct_data->m_data);
6425 l_mct_data->m_data = 00;
6428 l_mct_data->m_index = l_indix;
6429 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6430 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6432 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6435 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6441 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6442 if (! l_mct_data->m_data) {
6443 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6446 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6448 l_mct_data->m_data_size = p_header_size;
6449 ++l_tcp->m_nb_mct_records;
6455 * Writes the MCC marker (Multiple Component Collection)
6457 * @param p_stream the stream to write data to.
6458 * @param p_j2k J2K codec.
6459 * @param p_manager the user event manager.
6461 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6462 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6463 struct opj_stream_private *p_stream,
6464 struct opj_event_mgr * p_manager )
6467 OPJ_UINT32 l_mcc_size;
6468 OPJ_BYTE * l_current_data = 00;
6469 OPJ_UINT32 l_nb_bytes_for_comp;
6474 assert(p_j2k != 00);
6475 assert(p_manager != 00);
6476 assert(p_stream != 00);
6478 if (p_mcc_record->m_nb_comps > 255 ) {
6479 l_nb_bytes_for_comp = 2;
6483 l_nb_bytes_for_comp = 1;
6487 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6488 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6490 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6491 = (OPJ_BYTE*)opj_realloc(
6492 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6494 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6498 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6501 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6503 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6504 l_current_data += 2;
6506 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6507 l_current_data += 2;
6510 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6511 l_current_data += 2;
6513 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6516 /* only one marker atm */
6517 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6520 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6523 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6526 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 */
6529 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6530 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6531 l_current_data+=l_nb_bytes_for_comp;
6534 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 */
6537 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6539 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6540 l_current_data+=l_nb_bytes_for_comp;
6543 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6545 if (p_mcc_record->m_decorrelation_array) {
6546 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6549 if (p_mcc_record->m_offset_array) {
6550 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6553 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6556 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) {
6564 * Reads a MCC marker (Multiple Component Collection)
6566 * @param p_header_data the data contained in the MCC box.
6567 * @param p_j2k the jpeg2000 codec.
6568 * @param p_header_size the size of the data contained in the MCC marker.
6569 * @param p_manager the user event manager.
6571 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6572 OPJ_BYTE * p_header_data,
6573 OPJ_UINT32 p_header_size,
6574 struct opj_event_mgr * p_manager )
6579 opj_tcp_v2_t * l_tcp;
6580 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6581 opj_mct_data_t * l_mct_data;
6582 OPJ_UINT32 l_nb_collections;
6583 OPJ_UINT32 l_nb_comps;
6584 OPJ_UINT32 l_nb_bytes_by_comp;
6588 assert(p_header_data != 00);
6589 assert(p_j2k != 00);
6590 assert(p_manager != 00);
6592 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6593 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6594 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6596 if (p_header_size < 2) {
6597 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6602 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6605 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6609 if (p_header_size < 7) {
6610 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6614 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6617 l_mcc_record = l_tcp->m_mcc_records;
6619 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6620 if (l_mcc_record->m_index == l_indix) {
6627 if (i == l_tcp->m_nb_mcc_records) {
6628 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6629 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6631 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6632 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6633 if (! l_tcp->m_mcc_records) {
6634 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6637 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6638 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));
6640 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6642 l_mcc_record->m_index = l_indix;
6644 /* only one marker atm */
6645 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6648 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6652 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6655 if (l_nb_collections > 1) {
6656 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6662 for (i=0;i<l_nb_collections;++i) {
6663 if (p_header_size < 3) {
6664 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6668 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6672 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6676 opj_read_bytes(p_header_data,&l_nb_comps,2);
6681 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6682 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6684 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6685 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6689 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6691 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6692 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6693 p_header_data+=l_nb_bytes_by_comp;
6696 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6701 opj_read_bytes(p_header_data,&l_nb_comps,2);
6704 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6705 l_nb_comps &= 0x7fff;
6707 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6708 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6712 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6713 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6717 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6719 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6720 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6721 p_header_data+=l_nb_bytes_by_comp;
6724 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6729 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6732 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6733 l_mcc_record->m_decorrelation_array = 00;
6734 l_mcc_record->m_offset_array = 00;
6736 l_indix = l_tmp & 0xff;
6738 l_mct_data = l_tcp->m_mct_records;
6739 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6740 if (l_mct_data->m_index == l_indix) {
6741 l_mcc_record->m_decorrelation_array = l_mct_data;
6747 if (l_mcc_record->m_decorrelation_array == 00) {
6748 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6753 l_indix = (l_tmp >> 8) & 0xff;
6755 l_mct_data = l_tcp->m_mct_records;
6756 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6757 if (l_mct_data->m_index == l_indix) {
6758 l_mcc_record->m_offset_array = l_mct_data;
6764 if (l_mcc_record->m_offset_array == 00) {
6765 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6771 if (p_header_size != 0) {
6772 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6776 ++l_tcp->m_nb_mcc_records;
6783 * Writes the MCO marker (Multiple component transformation ordering)
6785 * @param p_stream the stream to write data to.
6786 * @param p_j2k J2K codec.
6787 * @param p_manager the user event manager.
6789 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6790 struct opj_stream_private *p_stream,
6791 struct opj_event_mgr * p_manager
6794 OPJ_BYTE * l_current_data = 00;
6795 OPJ_UINT32 l_mco_size;
6796 opj_tcp_v2_t * l_tcp = 00;
6797 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6801 assert(p_j2k != 00);
6802 assert(p_manager != 00);
6803 assert(p_stream != 00);
6805 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6806 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6808 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6809 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6811 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6812 = (OPJ_BYTE*)opj_realloc(
6813 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6815 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6820 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6823 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6824 l_current_data += 2;
6826 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6827 l_current_data += 2;
6829 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6832 l_mcc_record = l_tcp->m_mcc_records;
6833 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6834 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6840 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) {
6848 * Reads a MCO marker (Multiple Component Transform Ordering)
6850 * @param p_header_data the data contained in the MCO box.
6851 * @param p_j2k the jpeg2000 codec.
6852 * @param p_header_size the size of the data contained in the MCO marker.
6853 * @param p_manager the user event manager.
6855 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6856 OPJ_BYTE * p_header_data,
6857 OPJ_UINT32 p_header_size,
6858 struct opj_event_mgr * p_manager )
6860 OPJ_UINT32 l_tmp, i;
6861 OPJ_UINT32 l_nb_stages;
6862 opj_tcp_v2_t * l_tcp;
6863 opj_tccp_t * l_tccp;
6864 opj_image_t * l_image;
6865 opj_image_comp_t * l_img_comp;
6868 assert(p_header_data != 00);
6869 assert(p_j2k != 00);
6870 assert(p_manager != 00);
6872 l_image = p_j2k->m_private_image;
6873 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6874 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6875 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6877 if (p_header_size < 1) {
6878 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6882 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6885 if (l_nb_stages > 1) {
6886 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6890 if (p_header_size != l_nb_stages + 1) {
6891 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6895 l_tccp = l_tcp->tccps;
6896 l_img_comp = l_image->comps;
6898 for (i=0;i<l_image->numcomps;++i) {
6899 l_tccp->m_dc_level_shift = 0;
6903 if (l_tcp->m_mct_decoding_matrix) {
6904 opj_free(l_tcp->m_mct_decoding_matrix);
6905 l_tcp->m_mct_decoding_matrix = 00;
6908 for (i=0;i<l_nb_stages;++i) {
6909 opj_read_bytes(p_header_data,&l_tmp,1);
6912 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6920 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6923 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6924 opj_mct_data_t * l_deco_array, * l_offset_array;
6925 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6926 OPJ_UINT32 l_nb_elem;
6927 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6928 opj_tccp_t * l_tccp;
6931 assert(p_tcp != 00);
6933 l_mcc_record = p_tcp->m_mcc_records;
6935 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6936 if (l_mcc_record->m_index == p_index) {
6941 if (i==p_tcp->m_nb_mcc_records) {
6942 /** element discarded **/
6946 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6947 /** do not support number of comps != image */
6951 l_deco_array = l_mcc_record->m_decorrelation_array;
6954 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6955 if (l_deco_array->m_data_size != l_data_size) {
6959 l_nb_elem = p_image->numcomps * p_image->numcomps;
6960 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6961 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6963 if (! p_tcp->m_mct_decoding_matrix ) {
6967 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);
6970 l_offset_array = l_mcc_record->m_offset_array;
6972 if (l_offset_array) {
6973 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6974 if (l_offset_array->m_data_size != l_data_size) {
6978 l_nb_elem = p_image->numcomps;
6979 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6980 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6982 if (! l_offset_data ) {
6986 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6988 l_tccp = p_tcp->tccps;
6989 l_current_offset_data = l_offset_data;
6991 for (i=0;i<p_image->numcomps;++i) {
6992 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6996 opj_free(l_offset_data);
7003 * Writes the CBD marker (Component bit depth definition)
7005 * @param p_stream the stream to write data to.
7006 * @param p_j2k J2K codec.
7007 * @param p_manager the user event manager.
7009 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7010 struct opj_stream_private *p_stream,
7011 struct opj_event_mgr * p_manager )
7014 OPJ_UINT32 l_cbd_size;
7015 OPJ_BYTE * l_current_data = 00;
7016 opj_image_t *l_image = 00;
7017 opj_image_comp_t * l_comp = 00;
7020 assert(p_j2k != 00);
7021 assert(p_manager != 00);
7022 assert(p_stream != 00);
7024 l_image = p_j2k->m_private_image;
7025 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7027 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7028 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7029 = (OPJ_BYTE*)opj_realloc(
7030 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7033 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7037 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7040 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7042 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7043 l_current_data += 2;
7045 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7046 l_current_data += 2;
7048 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7051 l_comp = l_image->comps;
7053 for (i=0;i<l_image->numcomps;++i) {
7054 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7060 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) {
7068 * Reads a CBD marker (Component bit depth definition)
7069 * @param p_header_data the data contained in the CBD box.
7070 * @param p_j2k the jpeg2000 codec.
7071 * @param p_header_size the size of the data contained in the CBD marker.
7072 * @param p_manager the user event manager.
7074 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7075 OPJ_BYTE * p_header_data,
7076 OPJ_UINT32 p_header_size,
7077 struct opj_event_mgr * p_manager)
7079 OPJ_UINT32 l_nb_comp,l_num_comp;
7080 OPJ_UINT32 l_comp_def;
7082 opj_image_comp_t * l_comp = 00;
7085 assert(p_header_data != 00);
7086 assert(p_j2k != 00);
7087 assert(p_manager != 00);
7089 l_num_comp = p_j2k->m_private_image->numcomps;
7091 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7092 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7096 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7099 if (l_nb_comp != l_num_comp) {
7100 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7104 l_comp = p_j2k->m_private_image->comps;
7105 for (i=0;i<l_num_comp;++i) {
7106 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7108 l_comp->sgnd = (l_comp_def>>7) & 1;
7109 l_comp->prec = (l_comp_def&0x7f) + 1;
7117 /* ----------------------------------------------------------------------- */
7118 /* J2K / JPT decoder interface */
7119 /* ----------------------------------------------------------------------- */
7123 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7126 if(j2k->tile_len != NULL) {
7127 opj_free(j2k->tile_len);
7129 if(j2k->tile_data != NULL) {
7130 opj_free(j2k->tile_data);
7132 if(j2k->default_tcp != NULL) {
7133 opj_tcp_t *default_tcp = j2k->default_tcp;
7134 if(default_tcp->ppt_data_first != NULL) {
7135 opj_free(default_tcp->ppt_data_first);
7137 if(j2k->default_tcp->tccps != NULL) {
7138 opj_free(j2k->default_tcp->tccps);
7140 opj_free(j2k->default_tcp);
7142 if(j2k->cp != NULL) {
7143 opj_cp_t *cp = j2k->cp;
7144 if(cp->tcps != NULL) {
7145 for(i = 0; i < cp->tw * cp->th; i++) {
7146 if(cp->tcps[i].ppt_data_first != NULL) {
7147 opj_free(cp->tcps[i].ppt_data_first);
7149 if(cp->tcps[i].tccps != NULL) {
7150 opj_free(cp->tcps[i].tccps);
7155 if(cp->ppm_data_first != NULL) {
7156 opj_free(cp->ppm_data_first);
7158 if(cp->tileno != NULL) {
7159 opj_free(cp->tileno);
7161 if(cp->comment != NULL) {
7162 opj_free(cp->comment);
7171 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7173 if(j2k && parameters) {
7174 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7175 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7178 j2k->m_cp.correct = parameters->jpwl_correct;
7179 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7180 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7181 #endif /* USE_JPWL */
7188 * Read a JPT-stream and decode file
7191 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7192 opj_image_t *image = NULL;
7193 opj_jpt_msg_header_t header;
7195 opj_common_ptr cinfo = j2k->cinfo;
7197 OPJ_ARG_NOT_USED(cstr_info);
7201 /* create an empty image */
7202 image = opj_image_create0();
7205 j2k->state = J2K_STATE_MHSOC;
7207 /* Initialize the header */
7208 jpt_init_msg_header(&header);
7209 /* Read the first header of the message */
7210 jpt_read_msg_header(cinfo, cio, &header);
7212 position = cio_tell(cio);
7213 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7214 opj_image_destroy(image);
7215 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7220 opj_dec_mstabent_t *e = NULL;
7223 if (!cio_numbytesleft(cio)) {
7227 /* data-bin read -> need to read a new header */
7228 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7229 jpt_read_msg_header(cinfo, cio, &header);
7230 position = cio_tell(cio);
7231 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7232 opj_image_destroy(image);
7233 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7238 id = cio_read(cio, 2);
7239 if (id >> 8 != 0xff) {
7240 opj_image_destroy(image);
7241 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7244 e = j2k_dec_mstab_lookup(id);
7245 if (!(j2k->state & e->states)) {
7246 opj_image_destroy(image);
7247 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7253 if (j2k->state == J2K_STATE_MT) {
7256 if (j2k->state == J2K_STATE_NEOC) {
7260 if (j2k->state == J2K_STATE_NEOC) {
7264 if (j2k->state != J2K_STATE_MT) {
7265 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7271 /* ----------------------------------------------------------------------- */
7272 /* J2K encoder interface */
7273 /* ----------------------------------------------------------------------- */
7275 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7276 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7283 opj_j2k_v2_t* j2k_create_compress_v2(void)
7285 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7290 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7292 l_j2k->m_is_decoder = 0;
7293 l_j2k->m_cp.m_is_decoder = 0;
7295 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7296 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7301 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7303 /* validation list creation*/
7304 l_j2k->m_validation_list = opj_procedure_list_create();
7305 if (! l_j2k->m_validation_list) {
7310 /* execution list creation*/
7311 l_j2k->m_procedure_list = opj_procedure_list_create();
7312 if (! l_j2k->m_procedure_list) {
7320 void j2k_destroy_compress(opj_j2k_t *j2k) {
7324 if(j2k->cp != NULL) {
7325 opj_cp_t *cp = j2k->cp;
7328 opj_free(cp->comment);
7331 opj_free(cp->matrice);
7333 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7334 opj_free(cp->tcps[tileno].tccps);
7343 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7345 int tileno, numpocs_tile;
7346 opj_cp_t *cp = NULL;
7348 if(!j2k || !parameters || ! image) {
7352 /* create and initialize the coding parameters structure */
7353 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7355 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7358 /* set default values for cp */
7363 copy user encoding parameters
7365 cp->cinema = parameters->cp_cinema;
7366 cp->max_comp_size = parameters->max_comp_size;
7367 cp->rsiz = parameters->cp_rsiz;
7368 cp->disto_alloc = parameters->cp_disto_alloc;
7369 cp->fixed_alloc = parameters->cp_fixed_alloc;
7370 cp->fixed_quality = parameters->cp_fixed_quality;
7372 /* mod fixed_quality */
7373 if(parameters->cp_matrice) {
7374 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7375 cp->matrice = (int *) opj_malloc(array_size);
7376 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7380 cp->tdx = parameters->cp_tdx;
7381 cp->tdy = parameters->cp_tdy;
7384 cp->tx0 = parameters->cp_tx0;
7385 cp->ty0 = parameters->cp_ty0;
7387 /* comment string */
7388 if(parameters->cp_comment) {
7389 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7391 strcpy(cp->comment, parameters->cp_comment);
7396 calculate other encoding parameters
7399 if (parameters->tile_size_on) {
7400 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7401 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7403 cp->tdx = image->x1 - cp->tx0;
7404 cp->tdy = image->y1 - cp->ty0;
7407 if(parameters->tp_on){
7408 cp->tp_flag = parameters->tp_flag;
7413 for(i=0;i<image->numcomps ;i++){
7414 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7420 calculate JPWL encoding parameters
7423 if (parameters->jpwl_epc_on) {
7427 cp->epc_on = OPJ_TRUE;
7428 cp->info_on = OPJ_FALSE; /* no informative technique */
7431 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7432 cp->epb_on = OPJ_TRUE;
7434 cp->hprot_MH = parameters->jpwl_hprot_MH;
7435 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7436 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7437 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7439 /* if tile specs are not specified, copy MH specs */
7440 if (cp->hprot_TPH[0] == -1) {
7441 cp->hprot_TPH_tileno[0] = 0;
7442 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7444 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7445 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7446 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7447 cp->pprot[i] = parameters->jpwl_pprot[i];
7451 /* set ESD writing */
7452 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7453 cp->esd_on = OPJ_TRUE;
7455 cp->sens_size = parameters->jpwl_sens_size;
7456 cp->sens_addr = parameters->jpwl_sens_addr;
7457 cp->sens_range = parameters->jpwl_sens_range;
7459 cp->sens_MH = parameters->jpwl_sens_MH;
7460 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7461 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7462 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7466 /* always set RED writing to false: we are at the encoder */
7467 cp->red_on = OPJ_FALSE;
7470 cp->epc_on = OPJ_FALSE;
7472 #endif /* USE_JPWL */
7475 /* initialize the mutiple tiles */
7476 /* ---------------------------- */
7477 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7479 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7480 opj_tcp_t *tcp = &cp->tcps[tileno];
7481 tcp->numlayers = parameters->tcp_numlayers;
7482 assert ( tcp->numlayers >= 0 );
7483 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7485 if (cp->fixed_quality) {
7486 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7488 tcp->rates[j] = parameters->tcp_rates[j];
7490 if (cp->fixed_quality) { /* add fixed_quality */
7491 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7493 tcp->rates[j] = parameters->tcp_rates[j];
7497 tcp->csty = parameters->csty;
7498 tcp->prg = parameters->prog_order;
7499 tcp->mct = parameters->tcp_mct;
7503 if (parameters->numpocs) {
7504 /* initialisation of POC */
7506 assert( parameters->numpocs >= 0 );
7507 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7508 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7509 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7510 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7511 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7512 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7513 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7514 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7515 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7516 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7520 tcp->numpocs = numpocs_tile -1 ;
7525 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7527 for (i = 0; i < image->numcomps; i++) {
7528 opj_tccp_t *tccp = &tcp->tccps[i];
7529 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7530 tccp->numresolutions = parameters->numresolution;
7531 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7532 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7533 tccp->cblksty = parameters->mode;
7534 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7535 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7537 assert(parameters->roi_compno >= 0);
7538 if (i == (OPJ_UINT32)parameters->roi_compno) {
7539 tccp->roishift = parameters->roi_shift;
7544 if(parameters->cp_cinema)
7546 /*Precinct size for lowest frequency subband=128*/
7549 /*Precinct size at all other resolutions = 256*/
7550 for (j = 1; j < tccp->numresolutions; j++) {
7555 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7557 assert(tccp->numresolutions > 0);
7558 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7559 if (p < parameters->res_spec) {
7561 if (parameters->prcw_init[p] < 1) {
7564 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7567 if (parameters->prch_init[p] < 1) {
7570 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7574 int res_spec = parameters->res_spec;
7575 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7576 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7578 if (size_prcw < 1) {
7581 tccp->prcw[j] = int_floorlog2(size_prcw);
7584 if (size_prch < 1) {
7587 tccp->prch[j] = int_floorlog2(size_prch);
7591 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7594 for (j = 0; j < tccp->numresolutions; j++) {
7601 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7606 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7607 opj_cparameters_t *parameters,
7609 struct opj_event_mgr * p_manager)
7611 OPJ_UINT32 i, j, tileno, numpocs_tile;
7612 opj_cp_v2_t *cp = 00;
7615 if(!p_j2k || !parameters || ! image) {
7619 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7620 cp = &(p_j2k->m_cp);
7622 /* set default values for cp */
7627 copy user encoding parameters
7629 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7630 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7631 cp->rsiz = parameters->cp_rsiz;
7632 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7633 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7634 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7636 /* mod fixed_quality */
7637 if (parameters->cp_matrice) {
7638 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7639 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7640 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7644 cp->tdx = parameters->cp_tdx;
7645 cp->tdy = parameters->cp_tdy;
7648 cp->tx0 = parameters->cp_tx0;
7649 cp->ty0 = parameters->cp_ty0;
7651 /* comment string */
7652 if(parameters->cp_comment) {
7653 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7655 strcpy(cp->comment, parameters->cp_comment);
7660 calculate other encoding parameters
7663 if (parameters->tile_size_on) {
7664 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7665 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7667 cp->tdx = image->x1 - cp->tx0;
7668 cp->tdy = image->y1 - cp->ty0;
7671 if (parameters->tp_on) {
7672 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7673 cp->m_specific_param.m_enc.m_tp_on = 1;
7678 calculate JPWL encoding parameters
7681 if (parameters->jpwl_epc_on) {
7685 cp->epc_on = OPJ_TRUE;
7686 cp->info_on = OPJ_FALSE; /* no informative technique */
7689 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7690 cp->epb_on = OPJ_TRUE;
7692 cp->hprot_MH = parameters->jpwl_hprot_MH;
7693 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7694 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7695 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7697 /* if tile specs are not specified, copy MH specs */
7698 if (cp->hprot_TPH[0] == -1) {
7699 cp->hprot_TPH_tileno[0] = 0;
7700 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7702 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7703 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7704 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7705 cp->pprot[i] = parameters->jpwl_pprot[i];
7709 /* set ESD writing */
7710 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7711 cp->esd_on = OPJ_TRUE;
7713 cp->sens_size = parameters->jpwl_sens_size;
7714 cp->sens_addr = parameters->jpwl_sens_addr;
7715 cp->sens_range = parameters->jpwl_sens_range;
7717 cp->sens_MH = parameters->jpwl_sens_MH;
7718 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7719 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7720 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7724 /* always set RED writing to false: we are at the encoder */
7725 cp->red_on = OPJ_FALSE;
7728 cp->epc_on = OPJ_FALSE;
7730 #endif /* USE_JPWL */
7733 /* initialize the mutiple tiles */
7734 /* ---------------------------- */
7735 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7736 if (parameters->numpocs) {
7737 /* initialisation of POC */
7738 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7742 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7743 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7744 tcp->numlayers = parameters->tcp_numlayers;
7746 for (j = 0; j < tcp->numlayers; j++) {
7747 if(cp->m_specific_param.m_enc.m_cinema){
7748 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7749 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7751 tcp->rates[j] = parameters->tcp_rates[j];
7753 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7754 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7756 tcp->rates[j] = parameters->tcp_rates[j];
7761 tcp->csty = parameters->csty;
7762 tcp->prg = parameters->prog_order;
7763 tcp->mct = parameters->tcp_mct;
7768 if (parameters->numpocs) {
7769 /* initialisation of POC */
7772 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7773 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7774 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7776 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7777 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7778 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7779 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7780 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7781 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7782 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7788 tcp->numpocs = numpocs_tile -1 ;
7793 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7795 if (parameters->mct_data) {
7797 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7800 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7801 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7802 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7803 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7806 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7807 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7808 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7810 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7811 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7813 tcp->mct_norms = (OPJ_FLOAT64*)
7814 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7816 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7819 for (i = 0; i < image->numcomps; i++) {
7820 opj_tccp_t *tccp = &tcp->tccps[i];
7821 tccp->m_dc_level_shift = l_dc_shift[i];
7824 j2k_setup_mct_encoding(tcp,image);
7828 for (i = 0; i < image->numcomps; i++) {
7829 opj_tccp_t *tccp = &tcp->tccps[i];
7830 opj_image_comp_t * l_comp = &(image->comps[i]);
7832 if (! l_comp->sgnd) {
7833 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7838 for (i = 0; i < image->numcomps; i++) {
7839 opj_tccp_t *tccp = &tcp->tccps[i];
7841 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7842 tccp->numresolutions = parameters->numresolution;
7843 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7844 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7845 tccp->cblksty = parameters->mode;
7846 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7847 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7850 if (i == parameters->roi_compno) {
7851 tccp->roishift = parameters->roi_shift;
7856 if(parameters->cp_cinema) {
7857 //Precinct size for lowest frequency subband=128
7860 //Precinct size at all other resolutions = 256
7861 for (j = 1; j < tccp->numresolutions; j++) {
7866 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7867 OPJ_INT32 p = 0, it_res;
7868 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7869 if (p < parameters->res_spec) {
7871 if (parameters->prcw_init[p] < 1) {
7872 tccp->prcw[it_res] = 1;
7874 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7877 if (parameters->prch_init[p] < 1) {
7878 tccp->prch[it_res] = 1;
7880 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7884 int res_spec = parameters->res_spec;
7885 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7886 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7888 if (size_prcw < 1) {
7889 tccp->prcw[it_res] = 1;
7891 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7894 if (size_prch < 1) {
7895 tccp->prch[it_res] = 1;
7897 tccp->prch[it_res] = int_floorlog2(size_prch);
7901 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7904 for (j = 0; j < tccp->numresolutions; j++) {
7911 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7915 if (parameters->mct_data) {
7916 opj_free(parameters->mct_data);
7917 parameters->mct_data = 00;
7922 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7925 opj_cp_t *cp = NULL;
7927 opj_tcd_t *tcd = NULL; /* TCD component */
7935 j2k->cstr_info = cstr_info;
7938 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7939 cstr_info->image_w = image->x1 - image->x0;
7940 cstr_info->image_h = image->y1 - image->y0;
7941 cstr_info->prog = (&cp->tcps[0])->prg;
7942 cstr_info->tw = cp->tw;
7943 cstr_info->th = cp->th;
7944 cstr_info->tile_x = cp->tdx; /* new version parser */
7945 cstr_info->tile_y = cp->tdy; /* new version parser */
7946 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7947 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7948 cstr_info->numcomps = image->numcomps;
7949 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7950 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7951 for (compno=0; compno < image->numcomps; compno++) {
7952 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7954 cstr_info->D_max = 0.0; /* ADD Marcela */
7955 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7956 cstr_info->maxmarknum = 100;
7957 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7958 cstr_info->marknum = 0;
7968 for (compno = 1; compno < image->numcomps; compno++) {
7969 j2k_write_coc(j2k, compno);
7970 j2k_write_qcc(j2k, compno);
7974 for (compno = 0; compno < image->numcomps; compno++) {
7975 opj_tcp_t *tcp = &cp->tcps[0];
7976 if (tcp->tccps[compno].roishift)
7977 j2k_write_rgn(j2k, compno, 0);
7979 if (cp->comment != NULL) {
7983 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7987 if (cp->cinema == CINEMA4K_24) {
7992 /* uncomment only for testing JPSEC marker writing */
7993 /* j2k_write_sec(j2k); */
7997 cstr_info->main_head_end = cio_tell(cio) - 1;
8000 /**** Main Header ENDS here ***/
8002 /* create the tile encoder */
8003 tcd = tcd_create(j2k->cinfo);
8005 /* encode each tile */
8006 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8010 int acc_pack_num = 0;
8014 opj_tcp_t *tcp = &cp->tcps[tileno];
8015 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8017 j2k->curtileno = tileno;
8018 j2k->cur_tp_num = 0;
8019 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8020 /* initialisation before tile encoding */
8022 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8024 tcd_init_encode(tcd, image, cp, j2k->curtileno);
8029 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8030 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8031 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8032 cstr_info->tile[j2k->curtileno].marknum = 0;
8036 for(pino = 0; pino <= tcp->numpocs; pino++) {
8040 /*Get number of tile parts*/
8041 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8042 tcd->tp_pos = cp->tp_pos;
8044 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8045 j2k->tp_num = tilepartno;
8048 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8049 cio_tell(cio) + j2k->pos_correction;
8053 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8054 for (compno = 1; compno < image->numcomps; compno++) {
8055 j2k_write_coc(j2k, compno);
8056 j2k_write_qcc(j2k, compno);
8058 if (cp->tcps[tileno].numpocs) {
8065 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8066 cio_tell(cio) + j2k->pos_correction + 1;
8069 j2k_write_sod(j2k, tcd);
8073 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8074 cio_tell(cio) + j2k->pos_correction - 1;
8075 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8077 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8078 cstr_info->packno - acc_pack_num;
8079 acc_pack_num = cstr_info->packno;
8087 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8092 if (tile->PPT) { // BAD PPT !!!
8095 PPT_file=fopen("PPT","rb");
8096 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8097 for (i=0;i<tile->len_ppt;i++) {
8099 fread(&elmt, 1, 1, PPT_file);
8100 fwrite(&elmt,1,1,f);
8109 /* destroy the tile encoder */
8110 tcd_free_encode(tcd);
8113 opj_free(j2k->cur_totnum_tp);
8118 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8120 /* The following adjustment is done to adjust the codestream size */
8121 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8122 /* the first bunch of bytes is not in the codestream */
8123 cstr_info->codestream_size -= cstr_info->main_head_start;
8129 preparation of JPWL marker segments
8133 /* encode according to JPWL */
8134 jpwl_encode(j2k, cio, image);
8137 #endif /* USE_JPWL */
8142 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8144 assert(cstr_info != 00);
8146 /* expand the list? */
8147 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8148 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8149 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8152 /* add the marker */
8153 cstr_info->marker[cstr_info->marknum].type = type;
8154 cstr_info->marker[cstr_info->marknum].pos = pos;
8155 cstr_info->marker[cstr_info->marknum].len = len;
8156 cstr_info->marknum++;
8160 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8162 assert(cstr_index != 00);
8164 /* expand the list? */
8165 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8166 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8167 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8170 /* add the marker */
8171 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8172 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8173 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8174 cstr_index->marknum++;
8178 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8180 opj_marker_info_t *marker;
8182 assert(cstr_info != 00);
8184 /* expand the list? */
8185 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8186 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8187 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8190 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8192 /* add the marker */
8193 marker->type = type;
8196 cstr_info->tile[tileno].marknum++;
8199 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)
8201 assert(cstr_index != 00);
8202 assert(cstr_index->tile_index != 00);
8204 /* expand the list? */
8205 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8206 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8207 cstr_index->tile_index[tileno].marker =
8208 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8209 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8212 /* add the marker */
8213 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8214 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8215 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8216 cstr_index->tile_index[tileno].marknum++;
8218 if (type == J2K_MS_SOT) {
8219 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8221 if (cstr_index->tile_index[tileno].tp_index)
8222 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8229 * -----------------------------------------------------------------------
8230 * -----------------------------------------------------------------------
8231 * -----------------------------------------------------------------------
8235 * Ends the decompression procedures and possibiliy add data to be read after the
8238 opj_bool j2k_end_decompress(
8239 opj_j2k_v2_t *p_j2k,
8240 opj_stream_private_t *p_stream,
8241 opj_event_mgr_t * p_manager)
8250 * Reads a jpeg2000 codestream header structure.
8253 * @param p_stream the stream to read data from.
8254 * @param p_j2k the jpeg2000 codec.
8255 * @param p_manager the user event manager.
8257 * @return true if the box is valid.
8259 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8260 opj_j2k_v2_t* p_j2k,
8261 opj_image_t** p_image,
8262 struct opj_event_mgr* p_manager )
8265 assert(p_j2k != 00);
8266 assert(p_stream != 00);
8267 assert(p_manager != 00);
8269 /* create an empty image header */
8270 p_j2k->m_private_image = opj_image_create0();
8271 if (! p_j2k->m_private_image) {
8275 /* customization of the validation */
8276 j2k_setup_decoding_validation(p_j2k);
8278 /* validation of the parameters codec */
8279 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8280 opj_image_destroy(p_j2k->m_private_image);
8281 p_j2k->m_private_image = NULL;
8285 /* customization of the encoding */
8286 j2k_setup_header_reading(p_j2k);
8289 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8290 opj_image_destroy(p_j2k->m_private_image);
8291 p_j2k->m_private_image = NULL;
8295 *p_image = opj_image_create0();
8300 /* Copy codestream image information to the output image */
8301 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8303 /*Allocate and initialize some elements of codestrem index*/
8304 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8312 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8314 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8317 assert(p_j2k != 00);
8319 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8321 /* DEVELOPER CORNER, add your custom procedures */
8322 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8327 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8328 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8330 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8333 assert(p_j2k != 00);
8335 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8336 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8337 /* DEVELOPER CORNER, add your custom validation procedure */
8343 * The mct encoding validation procedure.
8345 * @param p_j2k the jpeg2000 codec to validate.
8346 * @param p_stream the input stream to validate.
8347 * @param p_manager the user event manager.
8349 * @return true if the parameters are correct.
8351 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8352 opj_stream_private_t *p_stream,
8353 opj_event_mgr_t * p_manager )
8355 opj_bool l_is_valid = OPJ_TRUE;
8359 assert(p_j2k != 00);
8360 assert(p_stream != 00);
8361 assert(p_manager != 00);
8363 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8364 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8365 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8367 for (i=0;i<l_nb_tiles;++i) {
8368 if (l_tcp->mct == 2) {
8369 opj_tccp_t * l_tccp = l_tcp->tccps;
8370 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8372 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8373 l_is_valid &= ! (l_tccp->qmfbid & 1);
8384 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8387 OPJ_UINT32 l_indix = 1;
8388 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8389 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8390 OPJ_UINT32 l_mct_size,l_nb_elem;
8391 OPJ_FLOAT32 * l_data, * l_current_data;
8392 opj_tccp_t * l_tccp;
8395 assert(p_tcp != 00);
8397 if (p_tcp->mct != 2) {
8401 if (p_tcp->m_mct_decoding_matrix) {
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;
8405 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) {
8409 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8411 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8413 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8415 if (l_mct_deco_data->m_data) {
8416 opj_free(l_mct_deco_data->m_data);
8417 l_mct_deco_data->m_data = 00;
8420 l_mct_deco_data->m_index = l_indix++;
8421 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8422 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8423 l_nb_elem = p_image->numcomps * p_image->numcomps;
8424 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8425 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8427 if (! l_mct_deco_data->m_data) {
8431 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);
8433 l_mct_deco_data->m_data_size = l_mct_size;
8434 ++p_tcp->m_nb_mct_records;
8437 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8438 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8439 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));
8441 if (! p_tcp->m_mct_records) {
8445 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8446 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8448 if (l_mct_deco_data) {
8449 l_mct_deco_data = l_mct_offset_data - 1;
8453 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8455 if (l_mct_offset_data->m_data) {
8456 opj_free(l_mct_offset_data->m_data);
8457 l_mct_offset_data->m_data = 00;
8460 l_mct_offset_data->m_index = l_indix++;
8461 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8462 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8463 l_nb_elem = p_image->numcomps;
8464 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8465 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8467 if (! l_mct_offset_data->m_data) {
8471 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8473 opj_free(l_mct_offset_data->m_data);
8474 l_mct_offset_data->m_data = 00;
8478 l_tccp = p_tcp->tccps;
8479 l_current_data = l_data;
8481 for (i=0;i<l_nb_elem;++i) {
8482 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8486 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8490 l_mct_offset_data->m_data_size = l_mct_size;
8492 ++p_tcp->m_nb_mct_records;
8494 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8495 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8496 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8497 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8499 if (! p_tcp->m_mcc_records) {
8502 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8503 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));
8507 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8508 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8509 l_mcc_data->m_is_irreversible = 1;
8510 l_mcc_data->m_nb_comps = p_image->numcomps;
8511 l_mcc_data->m_index = l_indix++;
8512 l_mcc_data->m_offset_array = l_mct_offset_data;
8513 ++p_tcp->m_nb_mcc_records;
8519 * Builds the cp decoder parameters to use to decode tile.
8521 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8522 opj_stream_private_t *p_stream,
8523 opj_event_mgr_t * p_manager )
8525 /* add here initialization of cp
8526 copy paste of setup_decoder */
8534 * Builds the cp encoder parameters to use to encode tile.
8536 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8537 opj_stream_private_t *p_stream,
8538 opj_event_mgr_t * p_manager )
8540 /* add here initialization of cp
8541 copy paste of setup_encoder */
8549 * The default encoding validation procedure without any extension.
8551 * @param p_j2k the jpeg2000 codec to validate.
8552 * @param p_stream the input stream to validate.
8553 * @param p_manager the user event manager.
8555 * @return true if the parameters are correct.
8557 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8558 opj_stream_private_t *p_stream,
8559 opj_event_mgr_t * p_manager )
8561 opj_bool l_is_valid = OPJ_TRUE;
8564 assert(p_j2k != 00);
8565 assert(p_stream != 00);
8566 assert(p_manager != 00);
8568 /* STATE checking */
8569 /* make sure the state is at 0 */
8570 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8572 /* POINTER validation */
8573 /* make sure a p_j2k codec is present */
8574 l_is_valid &= (p_j2k->m_procedure_list != 00);
8575 /* make sure a validation list is present */
8576 l_is_valid &= (p_j2k->m_validation_list != 00);
8578 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8579 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8583 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8584 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8588 /* PARAMETER VALIDATION */
8593 * The default decoding validation procedure without any extension.
8595 * @param p_j2k the jpeg2000 codec to validate.
8596 * @param p_stream the input stream to validate.
8597 * @param p_manager the user event manager.
8599 * @return true if the parameters are correct.
8601 opj_bool j2k_decoding_validation (
8602 opj_j2k_v2_t *p_j2k,
8603 opj_stream_private_t *p_stream,
8604 opj_event_mgr_t * p_manager
8607 opj_bool l_is_valid = OPJ_TRUE;
8610 assert(p_j2k != 00);
8611 assert(p_stream != 00);
8612 assert(p_manager != 00);
8615 /* STATE checking */
8616 /* make sure the state is at 0 */
8618 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8620 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8622 /* POINTER validation */
8623 /* make sure a p_j2k codec is present */
8624 /* make sure a procedure list is present */
8625 l_is_valid &= (p_j2k->m_procedure_list != 00);
8626 /* make sure a validation list is present */
8627 l_is_valid &= (p_j2k->m_validation_list != 00);
8629 /* PARAMETER VALIDATION */
8633 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8634 struct opj_stream_private *p_stream,
8635 struct opj_event_mgr * p_manager)
8637 OPJ_UINT32 l_current_marker;
8638 OPJ_UINT32 l_marker_size;
8639 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8642 assert(p_stream != 00);
8643 assert(p_j2k != 00);
8644 assert(p_manager != 00);
8646 /* We enter in the main header */
8647 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8649 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8650 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8651 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8655 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8656 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8657 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8661 /* Read 2 bytes as the new marker ID */
8662 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8664 /* Try to read until the SOT is detected */
8665 while (l_current_marker != J2K_MS_SOT) {
8667 /* Check if the current marker ID is valid */
8668 if (l_current_marker < 0xff00) {
8669 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8673 /* Get the marker handler from the marker ID */
8674 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8676 /* Manage case where marker is unknown */
8677 if (l_marker_handler->id == J2K_MS_UNK) {
8678 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8679 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8683 if (l_current_marker == J2K_MS_SOT)
8684 break; /* SOT marker is detected main header is completely read */
8685 else /* Get the marker handler from the marker ID */
8686 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8689 /* Check if the marker is known and if it is the right place to find it */
8690 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8691 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8695 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8696 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8697 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8701 /* read 2 bytes as the marker size */
8702 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8703 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8705 /* Check if the marker size is compatible with the header data size */
8706 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8707 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8708 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8709 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8712 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8715 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8716 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) {
8717 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8721 /* Read the marker segment with the correct marker handler */
8722 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8723 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8727 /* Add the marker to the codestream index*/
8728 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8729 l_marker_handler->id,
8730 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8731 l_marker_size + 4 );
8733 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8734 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8735 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8739 /* read 2 bytes as the new marker ID */
8740 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8743 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8745 /* Position of the last element if the main header */
8746 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8748 /* Next step: read a tile-part header */
8749 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8755 * Excutes the given procedures on the given codec.
8757 * @param p_procedure_list the list of procedures to execute
8758 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8759 * @param p_stream the stream to execute the procedures on.
8760 * @param p_manager the user manager.
8762 * @return true if all the procedures were successfully executed.
8764 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8765 opj_procedure_list_t * p_procedure_list,
8766 opj_stream_private_t *p_stream,
8767 opj_event_mgr_t * p_manager )
8769 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8770 opj_bool l_result = OPJ_TRUE;
8771 OPJ_UINT32 l_nb_proc, i;
8774 assert(p_procedure_list != 00);
8775 assert(p_j2k != 00);
8776 assert(p_stream != 00);
8777 assert(p_manager != 00);
8780 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8781 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);
8783 for (i=0;i<l_nb_proc;++i) {
8784 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8788 /* and clear the procedure list at the end.*/
8789 opj_procedure_list_clear(p_procedure_list);
8794 opj_bool j2k_copy_default_tcp_and_create_tcd
8796 opj_j2k_v2_t * p_j2k,
8797 opj_stream_private_t *p_stream,
8798 opj_event_mgr_t * p_manager
8801 opj_tcp_v2_t * l_tcp = 00;
8802 opj_tcp_v2_t * l_default_tcp = 00;
8803 OPJ_UINT32 l_nb_tiles;
8805 opj_tccp_t *l_current_tccp = 00;
8806 OPJ_UINT32 l_tccp_size;
8807 OPJ_UINT32 l_mct_size;
8808 opj_image_t * l_image;
8809 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8810 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8811 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8812 OPJ_UINT32 l_offset;
8815 assert(p_j2k != 00);
8816 assert(p_stream != 00);
8817 assert(p_manager != 00);
8819 l_image = p_j2k->m_private_image;
8820 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8821 l_tcp = p_j2k->m_cp.tcps;
8822 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8823 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8824 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8827 for (i=0; i<l_nb_tiles; ++i) {
8828 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8829 l_current_tccp = l_tcp->tccps;
8830 /*Copy default coding parameters into the current tile coding parameters*/
8831 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8832 /* Initialize some values of the current tile coding parameters*/
8834 l_tcp->ppt_data = 00;
8835 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8836 l_tcp->tccps = l_current_tccp;
8838 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8839 if (l_default_tcp->m_mct_decoding_matrix) {
8840 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8841 if (! l_tcp->m_mct_decoding_matrix ) {
8844 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8847 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8848 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8849 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8850 if (! l_tcp->m_mct_records) {
8853 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8855 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8856 l_src_mct_rec = l_default_tcp->m_mct_records;
8857 l_dest_mct_rec = l_tcp->m_mct_records;
8859 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8861 if (l_src_mct_rec->m_data) {
8863 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8864 if(! l_dest_mct_rec->m_data) {
8867 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8874 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8875 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8876 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8877 if (! l_tcp->m_mcc_records) {
8880 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8882 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8883 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8884 l_dest_mcc_rec = l_tcp->m_mcc_records;
8886 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8888 if (l_src_mcc_rec->m_decorrelation_array) {
8889 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8890 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8893 if (l_src_mcc_rec->m_offset_array) {
8894 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8895 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8902 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8903 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8905 /* Move to next tile cp*/
8909 /* Create the current tile decoder*/
8910 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8911 if (! p_j2k->m_tcd ) {
8915 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8916 tcd_destroy_v2(p_j2k->m_tcd);
8918 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8926 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8927 * with the marker value.
8928 * @param p_id Marker value to look up
8930 * @return the handler associated with the id.
8932 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8934 const opj_dec_memory_marker_handler_t *e;
8935 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8936 if (e->id == p_id) {
8937 break; /* we find a handler corresponding to the marker ID*/
8945 * Destroys a jpeg2000 codec.
8947 * @param p_j2k the jpeg20000 structure to destroy.
8949 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8955 if (p_j2k->m_is_decoder) {
8957 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8958 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8959 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8960 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8963 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8964 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8965 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8966 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8971 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8972 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8973 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8976 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8977 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8978 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8979 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8982 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8983 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8984 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8985 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8989 tcd_destroy_v2(p_j2k->m_tcd);
8991 j2k_cp_destroy(&(p_j2k->m_cp));
8992 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8994 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8995 p_j2k->m_procedure_list = 00;
8997 opj_procedure_list_destroy(p_j2k->m_validation_list);
8998 p_j2k->m_procedure_list = 00;
9000 j2k_destroy_cstr_index(p_j2k->cstr_index);
9001 p_j2k->cstr_index = NULL;
9003 opj_image_destroy(p_j2k->m_private_image);
9004 p_j2k->m_private_image = NULL;
9006 opj_image_destroy(p_j2k->m_output_image);
9007 p_j2k->m_output_image = NULL;
9012 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9016 if (p_cstr_ind->marker) {
9017 opj_free(p_cstr_ind->marker);
9018 p_cstr_ind->marker = NULL;
9021 if (p_cstr_ind->tile_index) {
9022 OPJ_UINT32 it_tile = 0;
9024 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9026 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9027 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9028 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9031 if(p_cstr_ind->tile_index[it_tile].tp_index){
9032 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9033 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9036 if(p_cstr_ind->tile_index[it_tile].marker){
9037 opj_free(p_cstr_ind->tile_index[it_tile].marker);
9038 p_cstr_ind->tile_index[it_tile].marker = NULL;
9043 opj_free( p_cstr_ind->tile_index);
9044 p_cstr_ind->tile_index = NULL;
9047 opj_free(p_cstr_ind);
9054 * Destroys a tile coding parameter structure.
9056 * @param p_tcp the tile coding parameter to destroy.
9058 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9064 if (p_tcp->ppt_buffer != 00) {
9065 opj_free(p_tcp->ppt_buffer);
9066 p_tcp->ppt_buffer = 00;
9069 if (p_tcp->tccps != 00) {
9070 opj_free(p_tcp->tccps);
9074 if (p_tcp->m_mct_coding_matrix != 00) {
9075 opj_free(p_tcp->m_mct_coding_matrix);
9076 p_tcp->m_mct_coding_matrix = 00;
9079 if (p_tcp->m_mct_decoding_matrix != 00) {
9080 opj_free(p_tcp->m_mct_decoding_matrix);
9081 p_tcp->m_mct_decoding_matrix = 00;
9084 if (p_tcp->m_mcc_records) {
9085 opj_free(p_tcp->m_mcc_records);
9086 p_tcp->m_mcc_records = 00;
9087 p_tcp->m_nb_max_mcc_records = 0;
9088 p_tcp->m_nb_mcc_records = 0;
9091 if (p_tcp->m_mct_records) {
9092 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9095 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9096 if (l_mct_data->m_data) {
9097 opj_free(l_mct_data->m_data);
9098 l_mct_data->m_data = 00;
9104 opj_free(p_tcp->m_mct_records);
9105 p_tcp->m_mct_records = 00;
9108 if (p_tcp->mct_norms != 00) {
9109 opj_free(p_tcp->mct_norms);
9110 p_tcp->mct_norms = 00;
9113 j2k_tcp_data_destroy(p_tcp);
9118 * Destroys the data inside a tile coding parameter structure.
9120 * @param p_tcp the tile coding parameter which contain data to destroy.
9122 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9124 if (p_tcp->m_data) {
9125 opj_free(p_tcp->m_data);
9126 p_tcp->m_data = NULL;
9127 p_tcp->m_data_size = 0;
9132 * Destroys a coding parameter structure.
9134 * @param p_cp the coding parameter to destroy.
9136 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9138 OPJ_UINT32 l_nb_tiles;
9139 opj_tcp_v2_t * l_current_tile = 00;
9150 l_current_tile = p_cp->tcps;
9151 l_nb_tiles = p_cp->th * p_cp->tw;
9154 (i = 0; i < l_nb_tiles; ++i)
9156 j2k_tcp_destroy(l_current_tile);
9159 opj_free(p_cp->tcps);
9163 (p_cp->ppm_buffer != 00)
9165 opj_free(p_cp->ppm_buffer);
9166 p_cp->ppm_buffer = 00;
9169 (p_cp->comment != 00)
9171 opj_free(p_cp->comment);
9175 (! p_cp->m_is_decoder)
9178 (p_cp->m_specific_param.m_enc.m_matrice)
9180 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9181 p_cp->m_specific_param.m_enc.m_matrice = 00;
9189 * Reads a tile header.
9190 * @param p_j2k the jpeg2000 codec.
9191 * @param p_stream the stream to write data to.
9192 * @param p_manager the user event manager.
9194 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9195 OPJ_UINT32 * p_tile_index,
9196 OPJ_UINT32 * p_data_size,
9197 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9198 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9199 OPJ_UINT32 * p_nb_comps,
9201 opj_stream_private_t *p_stream,
9202 opj_event_mgr_t * p_manager )
9204 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9205 OPJ_UINT32 l_marker_size;
9206 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9207 opj_tcp_v2_t * l_tcp = NULL;
9208 OPJ_UINT32 l_nb_tiles;
9211 assert(p_stream != 00);
9212 assert(p_j2k != 00);
9213 assert(p_manager != 00);
9215 /* Reach the End Of Codestream ?*/
9216 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9217 l_current_marker = J2K_MS_EOC;
9219 /* We need to encounter a SOT marker (a new tile-part header) */
9220 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9224 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9225 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9227 /* Try to read until the Start Of Data is detected */
9228 while (l_current_marker != J2K_MS_SOD) {
9230 /* Try to read 2 bytes (the marker size) 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,2,p_manager) != 2) {
9232 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9236 /* Read 2 bytes from the buffer as the marker size */
9237 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9239 /* Why this condition? FIXME */
9240 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9241 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9243 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9245 /* Get the marker handler from the marker ID */
9246 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9248 /* Check if the marker is known and if it is the right place to find it */
9249 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9250 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9253 /* FIXME manage case of unknown marker as in the main header ? */
9255 /* Check if the marker size is compatible with the header data size */
9256 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9257 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9258 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9259 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9262 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9265 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9266 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) {
9267 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9271 /* Read the marker segment with the correct marker handler */
9272 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9273 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9277 /* Add the marker to the codestream index*/
9278 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9280 l_marker_handler->id,
9281 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9282 l_marker_size + 4 );
9284 /* Keep the position of the last SOT marker read */
9285 if ( l_marker_handler->id == J2K_MS_SOT ) {
9286 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9287 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9289 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9294 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9295 /* Skip the rest of the tile part header*/
9296 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) {
9297 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9300 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9303 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9304 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9305 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9308 /* Read 2 bytes from the buffer as the new marker ID */
9309 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9313 /* If we didn't skip data before, we need to read the SOD marker*/
9314 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9315 /* Try to read the SOD marker and skip data ? FIXME */
9316 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
9322 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9323 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9324 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9325 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9329 /* Read 2 bytes from buffer as the new marker ID */
9330 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9334 /* Indicate we will try to read a new tile-part header*/
9335 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9336 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9337 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9339 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9340 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9341 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9345 /* Read 2 bytes from buffer as the new marker ID */
9346 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9350 /* Current marker is the EOC marker ?*/
9351 if (l_current_marker == J2K_MS_EOC) {
9352 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9353 p_j2k->m_current_tile_number = 0;
9354 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9359 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9360 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9361 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9363 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9364 ++p_j2k->m_current_tile_number;
9368 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9369 *p_go_on = OPJ_FALSE;
9375 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9376 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9380 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9381 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9383 *p_tile_index = p_j2k->m_current_tile_number;
9384 *p_go_on = OPJ_TRUE;
9385 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9386 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9387 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9388 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9389 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9390 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9392 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9398 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9399 OPJ_UINT32 p_tile_index,
9401 OPJ_UINT32 p_data_size,
9402 opj_stream_private_t *p_stream,
9403 opj_event_mgr_t * p_manager )
9405 OPJ_UINT32 l_current_marker;
9406 OPJ_BYTE l_data [2];
9407 opj_tcp_v2_t * l_tcp;
9410 assert(p_stream != 00);
9411 assert(p_j2k != 00);
9412 assert(p_manager != 00);
9414 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9415 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9419 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9420 if (! l_tcp->m_data) {
9421 j2k_tcp_destroy(l_tcp);
9425 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9429 p_j2k->cstr_index) ) {
9430 j2k_tcp_destroy(l_tcp);
9431 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9435 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9439 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9440 * we destroy just the data which will be re-read in read_tile_header*/
9441 /*j2k_tcp_destroy(l_tcp);
9442 p_j2k->m_tcd->tcp = 0;*/
9443 j2k_tcp_data_destroy(l_tcp);
9445 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9446 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9448 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9449 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9450 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9454 opj_read_bytes(l_data,&l_current_marker,2);
9456 if (l_current_marker == J2K_MS_EOC) {
9457 p_j2k->m_current_tile_number = 0;
9458 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9460 else if (l_current_marker != J2K_MS_SOT)
9462 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9471 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9473 OPJ_UINT32 i,j,k = 0;
9474 OPJ_UINT32 l_width_src,l_height_src;
9475 OPJ_UINT32 l_width_dest,l_height_dest;
9476 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9477 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9478 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9479 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9480 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9482 opj_image_comp_t * l_img_comp_src = 00;
9483 opj_image_comp_t * l_img_comp_dest = 00;
9485 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9486 opj_image_t * l_image_src = 00;
9487 OPJ_UINT32 l_size_comp, l_remaining;
9488 OPJ_INT32 * l_dest_ptr;
9489 opj_tcd_resolution_v2_t* l_res= 00;
9491 l_tilec = p_tcd->tcd_image->tiles->comps;
9492 l_image_src = p_tcd->image;
9493 l_img_comp_src = l_image_src->comps;
9495 l_img_comp_dest = p_output_image->comps;
9497 for (i=0; i<l_image_src->numcomps; i++) {
9499 /* Allocate output component buffer if necessary */
9500 if (!l_img_comp_dest->data) {
9502 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9503 if (! l_img_comp_dest->data) {
9508 /* Copy info from decoded comp image to output image */
9509 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9512 /* Compute the precision of the output buffer */
9513 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9514 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9515 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9521 if (l_size_comp == 3) {
9526 /* Current tile component size*/
9528 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9529 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9532 l_width_src = (l_res->x1 - l_res->x0);
9533 l_height_src = (l_res->y1 - l_res->y0);
9535 /* Border of the current output component*/
9536 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9537 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9538 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9539 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9542 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9543 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9547 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9548 * of the input buffer (decoded tile component) which will be move
9549 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9550 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9551 * by this input area.
9553 assert( l_res->x0 >= 0);
9554 assert( l_res->x1 >= 0);
9555 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9556 l_start_x_dest = l_res->x0 - l_x0_dest;
9557 l_offset_x0_src = 0;
9559 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9560 l_width_dest = l_width_src;
9561 l_offset_x1_src = 0;
9564 l_width_dest = l_x1_dest - l_res->x0 ;
9565 l_offset_x1_src = l_width_src - l_width_dest;
9569 l_start_x_dest = 0 ;
9570 l_offset_x0_src = l_x0_dest - l_res->x0;
9572 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9573 l_width_dest = l_width_src - l_offset_x0_src;
9574 l_offset_x1_src = 0;
9577 l_width_dest = l_img_comp_dest->w ;
9578 l_offset_x1_src = l_res->x1 - l_x1_dest;
9582 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9583 l_start_y_dest = l_res->y0 - l_y0_dest;
9584 l_offset_y0_src = 0;
9586 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9587 l_height_dest = l_height_src;
9588 l_offset_y1_src = 0;
9591 l_height_dest = l_y1_dest - l_res->y0 ;
9592 l_offset_y1_src = l_height_src - l_height_dest;
9596 l_start_y_dest = 0 ;
9597 l_offset_y0_src = l_y0_dest - l_res->y0;
9599 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9600 l_height_dest = l_height_src - l_offset_y0_src;
9601 l_offset_y1_src = 0;
9604 l_height_dest = l_img_comp_dest->h ;
9605 l_offset_y1_src = l_res->y1 - l_y1_dest;
9609 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9614 /* Compute the input buffer offset */
9615 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9616 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9617 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9619 /* Compute the output buffer offset */
9620 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9621 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9623 /* Move the output buffer to the first place where we will write*/
9624 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9627 fprintf(stdout, "COMPO[%d]:\n",i);
9628 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9629 "\t tile offset:%d, %d, %d, %d\n"
9630 "\t buffer offset: %d; %d, %d\n",
9631 l_res->x0, l_res->y0, l_width_src, l_height_src,
9632 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9633 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9635 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9636 "\t start offset: %d, line offset= %d\n",
9637 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9641 switch (l_size_comp) {
9644 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9645 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9647 if (l_img_comp_src->sgnd) {
9648 for (j = 0 ; j < l_height_dest ; ++j) {
9649 for ( k = 0 ; k < l_width_dest ; ++k) {
9650 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9653 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9654 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9658 for ( j = 0 ; j < l_height_dest ; ++j ) {
9659 for ( k = 0 ; k < l_width_dest ; ++k) {
9660 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9663 l_dest_ptr+= l_line_offset_dest;
9664 l_src_ptr += l_line_offset_src;
9668 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9669 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9674 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9675 l_src_ptr += l_start_offset_src;
9677 if (l_img_comp_src->sgnd) {
9678 for (j=0;j<l_height_dest;++j) {
9679 for (k=0;k<l_width_dest;++k) {
9680 *(l_dest_ptr++) = *(l_src_ptr++);
9683 l_dest_ptr+= l_line_offset_dest;
9684 l_src_ptr += l_line_offset_src ;
9688 for (j=0;j<l_height_dest;++j) {
9689 for (k=0;k<l_width_dest;++k) {
9690 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9693 l_dest_ptr+= l_line_offset_dest;
9694 l_src_ptr += l_line_offset_src ;
9698 l_src_ptr += l_end_offset_src;
9699 p_data = (OPJ_BYTE*) l_src_ptr;
9704 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9705 l_src_ptr += l_start_offset_src;
9707 for (j=0;j<l_height_dest;++j) {
9708 for (k=0;k<l_width_dest;++k) {
9709 *(l_dest_ptr++) = (*(l_src_ptr++));
9712 l_dest_ptr+= l_line_offset_dest;
9713 l_src_ptr += l_line_offset_src ;
9716 l_src_ptr += l_end_offset_src;
9717 p_data = (OPJ_BYTE*) l_src_ptr;
9731 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9733 * @param p_j2k the jpeg2000 codec.
9734 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9735 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9736 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9737 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9738 * @param p_manager the user event manager
9740 * @return true if the area could be set.
9742 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9743 opj_image_t* p_image,
9744 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9745 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9746 struct opj_event_mgr * p_manager )
9748 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9749 opj_image_t * l_image = p_j2k->m_private_image;
9752 OPJ_INT32 l_comp_x1, l_comp_y1;
9753 opj_image_comp_t* l_img_comp = NULL;
9755 /* Check if we are read the main header */
9756 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9757 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9761 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9762 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9764 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9765 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9766 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9767 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9773 /* Check if the positions provided by the user are correct */
9776 assert(p_start_x >= 0 );
9777 assert(p_start_y >= 0 );
9779 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9780 opj_event_msg_v2(p_manager, EVT_ERROR,
9781 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9782 p_start_x, l_image->x1);
9785 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9786 opj_event_msg_v2(p_manager, EVT_WARNING,
9787 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9788 p_start_x, l_image->x0);
9789 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9790 p_image->x0 = l_image->x0;
9793 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9794 p_image->x0 = p_start_x;
9798 if ((OPJ_UINT32)p_start_y > l_image->y1){
9799 opj_event_msg_v2(p_manager, EVT_ERROR,
9800 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9801 p_start_y, l_image->y1);
9804 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9805 opj_event_msg_v2(p_manager, EVT_WARNING,
9806 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9807 p_start_y, l_image->y0);
9808 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9809 p_image->y0 = l_image->y0;
9812 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9813 p_image->y0 = p_start_y;
9817 assert((OPJ_UINT32)p_end_x > 0);
9818 assert((OPJ_UINT32)p_end_y > 0);
9819 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9820 opj_event_msg_v2(p_manager, EVT_ERROR,
9821 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9822 p_end_x, l_image->x0);
9825 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9826 opj_event_msg_v2(p_manager, EVT_WARNING,
9827 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9828 p_end_x, l_image->x1);
9829 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9830 p_image->x1 = l_image->x1;
9833 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9834 p_image->x1 = p_end_x;
9838 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9839 opj_event_msg_v2(p_manager, EVT_ERROR,
9840 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9841 p_end_y, l_image->y0);
9844 if ((OPJ_UINT32)p_end_y > l_image->y1){
9845 opj_event_msg_v2(p_manager, EVT_WARNING,
9846 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9847 p_end_y, l_image->y1);
9848 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9849 p_image->y1 = l_image->y1;
9852 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9853 p_image->y1 = p_end_y;
9857 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9859 l_img_comp = p_image->comps;
9860 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9864 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9865 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9866 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9867 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9869 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9870 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9872 opj_event_msg_v2(p_manager, EVT_ERROR,
9873 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9877 l_img_comp->w = l_w;
9879 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9880 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9882 opj_event_msg_v2(p_manager, EVT_ERROR,
9883 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9887 l_img_comp->h = l_h;
9892 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9893 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9900 /* ----------------------------------------------------------------------- */
9901 /* J2K / JPT decoder interface */
9902 /* ----------------------------------------------------------------------- */
9904 * Creates a J2K decompression structure.
9906 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9908 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9910 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9914 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9916 l_j2k->m_is_decoder = 1;
9917 l_j2k->m_cp.m_is_decoder = 1;
9919 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9920 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9924 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9926 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9927 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9932 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9934 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9936 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9938 /* codestream index creation */
9939 l_j2k->cstr_index = j2k_create_cstr_index();
9941 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9942 if (!l_j2k->cstr_index){
9947 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9950 /* validation list creation */
9951 l_j2k->m_validation_list = opj_procedure_list_create();
9952 if (! l_j2k->m_validation_list) {
9957 /* execution list creation */
9958 l_j2k->m_procedure_list = opj_procedure_list_create();
9959 if (! l_j2k->m_procedure_list) {
9968 opj_codestream_index_t* j2k_create_cstr_index(void)
9970 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9971 opj_calloc(1,sizeof(opj_codestream_index_t));
9975 cstr_index->maxmarknum = 100;
9976 cstr_index->marknum = 0;
9977 cstr_index->marker = (opj_marker_info_t*)
9978 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9979 if (!cstr_index-> marker)
9982 cstr_index->tile_index = NULL;
9989 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9991 * @param p_tile_no the tile index.
9992 * @param p_comp_no the component being outputted.
9993 * @param p_j2k the J2K codec.
9995 * @return the number of bytes taken by the SPCod element.
9997 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9998 OPJ_UINT32 p_tile_no,
9999 OPJ_UINT32 p_comp_no )
10001 opj_cp_v2_t *l_cp = 00;
10002 opj_tcp_v2_t *l_tcp = 00;
10003 opj_tccp_t *l_tccp = 00;
10005 /* preconditions */
10006 assert(p_j2k != 00);
10008 l_cp = &(p_j2k->m_cp);
10009 l_tcp = &l_cp->tcps[p_tile_no];
10010 l_tccp = &l_tcp->tccps[p_comp_no];
10012 /* preconditions again */
10013 assert(p_tile_no < (l_cp->tw * l_cp->th));
10014 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10016 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10017 return 5 + l_tccp->numresolutions;
10025 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10027 * @param p_comp_no the component number to output.
10028 * @param p_stream the stream to write data to.
10029 * @param p_j2k J2K codec.
10030 * @param p_manager the user event manager.
10033 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10034 OPJ_UINT32 p_tile_no,
10035 OPJ_UINT32 p_comp_no,
10037 OPJ_UINT32 * p_header_size,
10038 struct opj_event_mgr * p_manager )
10041 opj_cp_v2_t *l_cp = 00;
10042 opj_tcp_v2_t *l_tcp = 00;
10043 opj_tccp_t *l_tccp = 00;
10045 /* preconditions */
10046 assert(p_j2k != 00);
10047 assert(p_header_size != 00);
10048 assert(p_manager != 00);
10049 assert(p_data != 00);
10051 l_cp = &(p_j2k->m_cp);
10052 l_tcp = &l_cp->tcps[p_tile_no];
10053 l_tccp = &l_tcp->tccps[p_comp_no];
10055 /* preconditions again */
10056 assert(p_tile_no < (l_cp->tw * l_cp->th));
10057 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10059 if (*p_header_size < 5) {
10060 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10064 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
10067 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
10070 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
10073 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
10076 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
10079 *p_header_size = *p_header_size - 5;
10081 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10083 if (*p_header_size < l_tccp->numresolutions) {
10084 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10088 for (i = 0; i < l_tccp->numresolutions; ++i) {
10089 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
10093 *p_header_size = *p_header_size - l_tccp->numresolutions;
10100 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10101 * @param p_header_data the data contained in the COM box.
10102 * @param p_j2k the jpeg2000 codec.
10103 * @param p_header_size the size of the data contained in the COM marker.
10104 * @param p_manager the user event manager.
10106 opj_bool j2k_read_SPCod_SPCoc(
10107 opj_j2k_v2_t *p_j2k,
10109 OPJ_BYTE * p_header_data,
10110 OPJ_UINT32 * p_header_size,
10111 struct opj_event_mgr * p_manager
10114 OPJ_UINT32 i, l_tmp;
10115 opj_cp_v2_t *l_cp = NULL;
10116 opj_tcp_v2_t *l_tcp = NULL;
10117 opj_tccp_t *l_tccp = NULL;
10118 OPJ_BYTE * l_current_ptr = NULL;
10120 /* preconditions */
10121 assert(p_j2k != 00);
10122 assert(p_manager != 00);
10123 assert(p_header_data != 00);
10125 l_cp = &(p_j2k->m_cp);
10126 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10127 &l_cp->tcps[p_j2k->m_current_tile_number] :
10128 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10130 /* precondition again */
10131 assert(compno < p_j2k->m_private_image->numcomps);
10133 l_tccp = &l_tcp->tccps[compno];
10134 l_current_ptr = p_header_data;
10136 /* make sure room is sufficient */
10137 if (*p_header_size < 5) {
10138 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10142 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10143 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10146 /* If user wants to remove more resolutions than the codestream contains, return error */
10147 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10148 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10149 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10150 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10154 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10156 l_tccp->cblkw += 2;
10158 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10160 l_tccp->cblkh += 2;
10162 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10165 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10168 *p_header_size = *p_header_size - 5;
10170 /* use custom precinct size ? */
10171 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10172 if (*p_header_size < l_tccp->numresolutions) {
10173 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10177 for (i = 0; i < l_tccp->numresolutions; ++i) {
10178 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10180 l_tccp->prcw[i] = l_tmp & 0xf;
10181 l_tccp->prch[i] = l_tmp >> 4;
10184 *p_header_size = *p_header_size - l_tccp->numresolutions;
10187 /* set default size for the precinct width and height */
10188 for (i = 0; i < l_tccp->numresolutions; ++i) {
10189 l_tccp->prcw[i] = 15;
10190 l_tccp->prch[i] = 15;
10194 #ifdef WIP_REMOVE_MSD
10196 if (p_j2k->cstr_info && compno == 0) {
10197 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10199 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10200 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10201 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10202 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10203 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10206 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10207 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10216 * Copies the tile component parameters of all the component from the first tile component.
10218 * @param p_j2k the J2k codec.
10220 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10224 opj_cp_v2_t *l_cp = NULL;
10225 opj_tcp_v2_t *l_tcp = NULL;
10226 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10227 OPJ_UINT32 l_prc_size;
10229 /* preconditions */
10230 assert(p_j2k != 00);
10232 l_cp = &(p_j2k->m_cp);
10233 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10234 &l_cp->tcps[p_j2k->m_current_tile_number] :
10235 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10237 l_ref_tccp = &l_tcp->tccps[0];
10238 l_copied_tccp = l_ref_tccp + 1;
10239 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10241 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10242 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10243 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10244 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10245 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10246 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10247 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10248 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10254 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10256 * @param p_tile_no the tile index.
10257 * @param p_comp_no the component being outputted.
10258 * @param p_j2k the J2K codec.
10260 * @return the number of bytes taken by the SPCod element.
10262 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10263 OPJ_UINT32 p_tile_no,
10264 OPJ_UINT32 p_comp_no )
10266 OPJ_UINT32 l_num_bands;
10268 opj_cp_v2_t *l_cp = 00;
10269 opj_tcp_v2_t *l_tcp = 00;
10270 opj_tccp_t *l_tccp = 00;
10272 /* preconditions */
10273 assert(p_j2k != 00);
10275 l_cp = &(p_j2k->m_cp);
10276 l_tcp = &l_cp->tcps[p_tile_no];
10277 l_tccp = &l_tcp->tccps[p_comp_no];
10279 /* preconditions again */
10280 assert(p_tile_no < l_cp->tw * l_cp->th);
10281 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10283 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10285 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10286 return 1 + l_num_bands;
10289 return 1 + 2*l_num_bands;
10294 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10296 * @param p_tile_no the tile to output.
10297 * @param p_comp_no the component number to output.
10298 * @param p_data the data buffer.
10299 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10300 * @param p_j2k J2K codec.
10301 * @param p_manager the user event manager.
10304 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10305 OPJ_UINT32 p_tile_no,
10306 OPJ_UINT32 p_comp_no,
10308 OPJ_UINT32 * p_header_size,
10309 struct opj_event_mgr * p_manager )
10311 OPJ_UINT32 l_header_size;
10312 OPJ_UINT32 l_band_no, l_num_bands;
10313 OPJ_UINT32 l_expn,l_mant;
10315 opj_cp_v2_t *l_cp = 00;
10316 opj_tcp_v2_t *l_tcp = 00;
10317 opj_tccp_t *l_tccp = 00;
10319 /* preconditions */
10320 assert(p_j2k != 00);
10321 assert(p_header_size != 00);
10322 assert(p_manager != 00);
10323 assert(p_data != 00);
10325 l_cp = &(p_j2k->m_cp);
10326 l_tcp = &l_cp->tcps[p_tile_no];
10327 l_tccp = &l_tcp->tccps[p_comp_no];
10329 /* preconditions again */
10330 assert(p_tile_no < l_cp->tw * l_cp->th);
10331 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10333 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10335 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10336 l_header_size = 1 + l_num_bands;
10338 if (*p_header_size < l_header_size) {
10339 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10343 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10346 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10347 l_expn = l_tccp->stepsizes[l_band_no].expn;
10348 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10353 l_header_size = 1 + 2*l_num_bands;
10355 if (*p_header_size < l_header_size) {
10356 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10360 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10363 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10364 l_expn = l_tccp->stepsizes[l_band_no].expn;
10365 l_mant = l_tccp->stepsizes[l_band_no].mant;
10367 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10372 *p_header_size = *p_header_size - l_header_size;
10378 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10380 * @param p_comp_no the component being targeted.
10381 * @param p_header_data the data contained in the COM box.
10382 * @param p_j2k the jpeg2000 codec.
10383 * @param p_header_size the size of the data contained in the COM marker.
10384 * @param p_manager the user event manager.
10386 opj_bool j2k_read_SQcd_SQcc(
10387 opj_j2k_v2_t *p_j2k,
10388 OPJ_UINT32 p_comp_no,
10389 OPJ_BYTE* p_header_data,
10390 OPJ_UINT32 * p_header_size,
10391 struct opj_event_mgr * p_manager
10395 OPJ_UINT32 l_band_no;
10396 opj_cp_v2_t *l_cp = 00;
10397 opj_tcp_v2_t *l_tcp = 00;
10398 opj_tccp_t *l_tccp = 00;
10399 OPJ_BYTE * l_current_ptr = 00;
10400 OPJ_UINT32 l_tmp, l_num_band;
10403 assert(p_j2k != 00);
10404 assert(p_manager != 00);
10405 assert(p_header_data != 00);
10407 l_cp = &(p_j2k->m_cp);
10408 /* come from tile part header or main header ?*/
10409 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10410 &l_cp->tcps[p_j2k->m_current_tile_number] :
10411 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10413 /* precondition again*/
10414 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10416 l_tccp = &l_tcp->tccps[p_comp_no];
10417 l_current_ptr = p_header_data;
10419 if (*p_header_size < 1) {
10420 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10423 *p_header_size -= 1;
10425 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10428 l_tccp->qntsty = l_tmp & 0x1f;
10429 l_tccp->numgbits = l_tmp >> 5;
10430 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10434 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10436 (*p_header_size) / 2;
10438 if( l_num_band > J2K_MAXBANDS ) {
10439 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10440 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10441 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10442 /*return OPJ_FALSE;*/
10447 if (l_cp->correct) {
10449 /* if JPWL is on, we check whether there are too many subbands */
10450 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10451 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10452 "JPWL: bad number of subbands in Sqcx (%d)\n",
10454 if (!JPWL_ASSUME) {
10455 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10458 /* we try to correct */
10460 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10461 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10466 #endif /* USE_JPWL */
10468 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10469 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10470 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10472 if (l_band_no < J2K_MAXBANDS){
10473 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10474 l_tccp->stepsizes[l_band_no].mant = 0;
10477 *p_header_size = *p_header_size - l_num_band;
10480 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10481 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10483 if (l_band_no < J2K_MAXBANDS){
10484 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10485 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10488 *p_header_size = *p_header_size - 2*l_num_band;
10491 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10492 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10493 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10494 l_tccp->stepsizes[l_band_no].expn =
10495 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10496 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10497 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10505 * Copies the tile component parameters of all the component from the first tile component.
10507 * @param p_j2k the J2k codec.
10509 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10512 opj_cp_v2_t *l_cp = NULL;
10513 opj_tcp_v2_t *l_tcp = NULL;
10514 opj_tccp_t *l_ref_tccp = NULL;
10515 opj_tccp_t *l_copied_tccp = NULL;
10518 /* preconditions */
10519 assert(p_j2k != 00);
10521 l_cp = &(p_j2k->m_cp);
10522 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10523 &l_cp->tcps[p_j2k->m_current_tile_number] :
10524 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10526 l_ref_tccp = &l_tcp->tccps[0];
10527 l_copied_tccp = l_ref_tccp + 1;
10528 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10530 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10531 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10532 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10533 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10539 * Dump some elements from the J2K decompression structure .
10541 *@param p_j2k the jpeg2000 codec.
10542 *@param flag flag to describe what elments are dump.
10543 *@param out_stream output stream where dump the elements.
10546 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10548 /* Check if the flag is compatible with j2k file*/
10549 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10550 fprintf(out_stream, "Wrong flag\n");
10554 /* Dump the image_header */
10555 if (flag & OPJ_IMG_INFO){
10556 if (p_j2k->m_private_image)
10557 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10560 /* Dump the codestream info from main header */
10561 if (flag & OPJ_J2K_MH_INFO){
10562 j2k_dump_MH_info(p_j2k, out_stream);
10566 /* Dump the codestream info of the current tile */
10567 if (flag & OPJ_J2K_TH_INFO){
10571 /* Dump the codestream index from main header */
10572 if (flag & OPJ_J2K_MH_IND){
10573 j2k_dump_MH_index(p_j2k, out_stream);
10576 /* Dump the codestream index of the current tile */
10577 if (flag & OPJ_J2K_TH_IND){
10584 * Dump index elements of the codestream extract from the main header.
10586 *@param p_j2k the jpeg2000 codec.
10587 *@param out_stream output stream where dump the elements.
10590 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10592 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10593 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10595 fprintf(out_stream, "Codestream index from main header: {\n");
10597 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10598 "\t Main header end position=%" PRIi64 "\n",
10599 cstr_index->main_head_start, cstr_index->main_head_end);
10601 fprintf(out_stream, "\t Marker list: {\n");
10603 if (cstr_index->marker){
10604 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10605 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10606 cstr_index->marker[it_marker].type,
10607 cstr_index->marker[it_marker].pos,
10608 cstr_index->marker[it_marker].len );
10612 fprintf(out_stream, "\t }\n");
10615 if (cstr_index->tile_index){
10617 /* Simple test to avoid to write empty information*/
10618 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10619 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10620 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10623 if (l_acc_nb_of_tile_part)
10625 fprintf(out_stream, "\t Tile index: {\n");
10627 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10628 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10630 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10632 if (cstr_index->tile_index[it_tile].tp_index){
10633 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10634 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10636 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10637 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10638 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10642 if (cstr_index->tile_index[it_tile].marker){
10643 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10644 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10645 cstr_index->tile_index[it_tile].marker[it_marker].type,
10646 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10647 cstr_index->tile_index[it_tile].marker[it_marker].len );
10651 fprintf(out_stream,"\t }\n");
10655 fprintf(out_stream,"}\n");
10660 * Dump info elements of the codestream extract from the main header.
10662 *@param p_j2k the jpeg2000 codec.
10663 *@param out_stream output stream where dump the elements.
10666 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10668 opj_tcp_v2_t * l_default_tile=NULL;
10670 fprintf(out_stream, "Codestream info from main header: {\n");
10672 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10673 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10674 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10676 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10677 if (l_default_tile)
10680 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10682 fprintf(out_stream, "\t default tile {\n");
10683 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10684 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10685 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10686 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10688 for (compno = 0; compno < numcomps; compno++) {
10689 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10691 OPJ_INT32 bandno, numbands;
10694 fprintf(out_stream, "\t\t comp %d {\n", compno);
10695 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10696 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10697 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10698 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10699 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10700 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10702 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10703 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10704 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10706 fprintf(out_stream, "\n");
10708 /* quantization style*/
10709 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10710 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10711 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10712 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10713 for (bandno = 0; bandno < numbands; bandno++) {
10714 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10715 l_tccp->stepsizes[bandno].expn);
10717 fprintf(out_stream, "\n");
10720 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10722 fprintf(out_stream, "\t\t }\n");
10723 } /*end of component of default tile*/
10724 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10728 fprintf(out_stream, "}\n");
10733 * Dump an image header structure.
10735 *@param img_header the image header to dump.
10736 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10737 *@param out_stream output stream where dump the elements.
10739 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10743 if (dev_dump_flag){
10744 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10748 fprintf(out_stream, "Image info {\n");
10749 tab[0] = '\t';tab[1] = '\0';
10752 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10753 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10754 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10756 if (img_header->comps){
10758 for (compno = 0; compno < img_header->numcomps; compno++) {
10759 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10760 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10761 fprintf(out_stream,"%s}\n",tab);
10765 fprintf(out_stream, "}\n");
10769 * Dump a component image header structure.
10771 *@param comp_header the component image header to dump.
10772 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10773 *@param out_stream output stream where dump the elements.
10775 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10779 if (dev_dump_flag){
10780 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10783 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10786 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10787 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10788 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10791 fprintf(out_stream, "}\n");
10796 * Get the codestream info from a JPEG2000 codec.
10798 *@param p_j2k the component image header to dump.
10800 *@return the codestream information extract from the jpg2000 codec
10802 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10805 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10806 opj_tcp_v2_t *l_default_tile;
10807 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10809 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10811 cstr_info->tx0 = p_j2k->m_cp.tx0;
10812 cstr_info->ty0 = p_j2k->m_cp.ty0;
10813 cstr_info->tdx = p_j2k->m_cp.tdx;
10814 cstr_info->tdy = p_j2k->m_cp.tdy;
10815 cstr_info->tw = p_j2k->m_cp.tw;
10816 cstr_info->th = p_j2k->m_cp.th;
10818 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10820 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10822 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10823 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10824 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10825 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10827 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10829 for (compno = 0; compno < numcomps; compno++) {
10830 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10831 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10832 OPJ_INT32 bandno, numbands;
10835 l_tccp_info->csty = l_tccp->csty;
10836 l_tccp_info->numresolutions = l_tccp->numresolutions;
10837 l_tccp_info->cblkw = l_tccp->cblkw;
10838 l_tccp_info->cblkh = l_tccp->cblkh;
10839 l_tccp_info->cblksty = l_tccp->cblksty;
10840 l_tccp_info->qmfbid = l_tccp->qmfbid;
10841 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10843 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10844 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10847 /* quantization style*/
10848 l_tccp_info->qntsty = l_tccp->qntsty;
10849 l_tccp_info->numgbits = l_tccp->numgbits;
10851 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10852 if (numbands < J2K_MAXBANDS) {
10853 for (bandno = 0; bandno < numbands; bandno++) {
10854 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10855 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10860 l_tccp_info->roishift = l_tccp->roishift;
10868 * Get the codestream index from a JPEG2000 codec.
10870 *@param p_j2k the component image header to dump.
10872 *@return the codestream index extract from the jpg2000 codec
10874 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10876 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10877 opj_calloc(1,sizeof(opj_codestream_index_t));
10881 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10882 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10883 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10885 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10886 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10887 if (!l_cstr_index->marker){
10888 opj_free( l_cstr_index);
10892 if (p_j2k->cstr_index->marker)
10893 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10895 opj_free(l_cstr_index->marker);
10896 l_cstr_index->marker = NULL;
10899 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10900 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10901 if (!l_cstr_index->tile_index){
10902 opj_free( l_cstr_index->marker);
10903 opj_free( l_cstr_index);
10907 if (!p_j2k->cstr_index->tile_index){
10908 opj_free(l_cstr_index->tile_index);
10909 l_cstr_index->tile_index = NULL;
10912 OPJ_UINT32 it_tile = 0;
10913 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10916 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10918 l_cstr_index->tile_index[it_tile].marker =
10919 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10921 if (!l_cstr_index->tile_index[it_tile].marker) {
10922 OPJ_UINT32 it_tile_free;
10924 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10925 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10928 opj_free( l_cstr_index->tile_index);
10929 opj_free( l_cstr_index->marker);
10930 opj_free( l_cstr_index);
10934 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10935 memcpy( l_cstr_index->tile_index[it_tile].marker,
10936 p_j2k->cstr_index->tile_index[it_tile].marker,
10937 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10939 opj_free(l_cstr_index->tile_index[it_tile].marker);
10940 l_cstr_index->tile_index[it_tile].marker = NULL;
10943 /* Tile part index*/
10944 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10946 l_cstr_index->tile_index[it_tile].tp_index =
10947 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10949 if(!l_cstr_index->tile_index[it_tile].tp_index){
10950 OPJ_UINT32 it_tile_free;
10952 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10953 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10954 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10957 opj_free( l_cstr_index->tile_index);
10958 opj_free( l_cstr_index->marker);
10959 opj_free( l_cstr_index);
10963 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10964 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10965 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10966 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10969 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10970 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10973 /* Packet index (NOT USED)*/
10974 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10975 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10980 return l_cstr_index;
10983 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10985 OPJ_UINT32 it_tile=0;
10987 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10988 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10989 if (!p_j2k->cstr_index->tile_index)
10992 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10993 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10994 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10995 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10996 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10997 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11007 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
11008 opj_stream_private_t *p_stream,
11009 opj_event_mgr_t * p_manager)
11011 opj_bool l_go_on = OPJ_TRUE;
11012 OPJ_UINT32 l_current_tile_no;
11013 OPJ_UINT32 l_data_size,l_max_data_size;
11014 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11015 OPJ_UINT32 l_nb_comps;
11016 OPJ_BYTE * l_current_data;
11018 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11019 if (! l_current_data) {
11022 l_max_data_size = 1000;
11027 if (! j2k_read_tile_header( p_j2k,
11028 &l_current_tile_no,
11030 &l_tile_x0, &l_tile_y0,
11031 &l_tile_x1, &l_tile_y1,
11036 opj_free(l_current_data);
11044 if (l_data_size > l_max_data_size) {
11045 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11046 if (! l_current_data) {
11047 opj_free(l_current_data);
11051 l_max_data_size = l_data_size;
11054 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11055 opj_free(l_current_data);
11058 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);
11060 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11061 opj_free(l_current_data);
11064 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11068 opj_free(l_current_data);
11074 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11076 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11079 assert(p_j2k != 00);
11081 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11082 /* DEVELOPER CORNER, add your custom procedures */
11087 * Read and decode one tile.
11089 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
11090 opj_stream_private_t *p_stream,
11091 opj_event_mgr_t * p_manager)
11093 opj_bool l_go_on = OPJ_TRUE;
11094 OPJ_UINT32 l_current_tile_no;
11095 OPJ_UINT32 l_tile_no_to_dec;
11096 OPJ_UINT32 l_data_size,l_max_data_size;
11097 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11098 OPJ_UINT32 l_nb_comps;
11099 OPJ_BYTE * l_current_data;
11101 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11102 if (! l_current_data) {
11105 l_max_data_size = 1000;
11107 /*Allocate and initialize some elements of codestrem index if not already done*/
11108 if( !p_j2k->cstr_index->tile_index)
11110 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11111 opj_free(l_current_data);
11115 /* Move into the codestream to the first SOT used to decode the desired tile */
11116 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11117 if (p_j2k->cstr_index->tile_index)
11118 if(p_j2k->cstr_index->tile_index->tp_index)
11120 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11121 /* the index for this tile has not been built,
11122 * so move to the last SOT read */
11123 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11124 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11129 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)) {
11130 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11134 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11135 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11136 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11140 if (! j2k_read_tile_header( p_j2k,
11141 &l_current_tile_no,
11143 &l_tile_x0, &l_tile_y0,
11144 &l_tile_x1, &l_tile_y1,
11149 opj_free(l_current_data);
11158 if (l_data_size > l_max_data_size) {
11159 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11160 if (! l_current_data) {
11161 opj_free(l_current_data);
11165 l_max_data_size = l_data_size;
11170 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11171 opj_free(l_current_data);
11174 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);
11176 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11177 opj_free(l_current_data);
11180 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11182 if(l_current_tile_no == l_tile_no_to_dec)
11184 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11185 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11186 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11192 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);
11197 opj_free(l_current_data);
11204 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11206 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11209 assert(p_j2k != 00);
11211 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11212 /* DEVELOPER CORNER, add your custom procedures */
11218 * Decodes the tiles of the stream.
11220 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
11221 opj_stream_private_t * p_stream,
11222 opj_image_t * p_image,
11223 opj_event_mgr_t * p_manager)
11230 p_j2k->m_output_image = opj_image_create0();
11231 if (! (p_j2k->m_output_image)) {
11234 opj_copy_image_header(p_image, p_j2k->m_output_image);
11236 /* customization of the decoding */
11237 j2k_setup_decoding(p_j2k);
11239 /* Decode the codestream */
11240 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11241 opj_image_destroy(p_j2k->m_private_image);
11242 p_j2k->m_private_image = NULL;
11246 /* Move data and copy one information from codec to output image*/
11247 for (compno = 0; compno < p_image->numcomps; compno++) {
11248 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11249 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11250 p_j2k->m_output_image->comps[compno].data = NULL;
11258 * Get the decoded tile.
11260 * @param p_j2k the jpeg2000 codestream codec.
11261 * @param p_stream input_stream
11262 * @param p_image output image. .
11263 * @param p_manager the user event manager
11264 * @param tile_index index of the tile we want decode
11266 * @return true if succeed.
11268 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11269 opj_stream_private_t *p_stream,
11270 opj_image_t* p_image,
11271 struct opj_event_mgr * p_manager,
11272 OPJ_UINT32 tile_index )
11275 OPJ_UINT32 l_tile_x, l_tile_y;
11276 opj_image_comp_t* l_img_comp;
11279 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11283 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11284 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);
11288 /* Compute the dimension of the desired tile*/
11289 l_tile_x = tile_index % p_j2k->m_cp.tw;
11290 l_tile_y = tile_index / p_j2k->m_cp.tw;
11292 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11293 if (p_image->x0 < p_j2k->m_private_image->x0)
11294 p_image->x0 = p_j2k->m_private_image->x0;
11295 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11296 if (p_image->x1 > p_j2k->m_private_image->x1)
11297 p_image->x1 = p_j2k->m_private_image->x1;
11299 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11300 if (p_image->y0 < p_j2k->m_private_image->y0)
11301 p_image->y0 = p_j2k->m_private_image->y0;
11302 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11303 if (p_image->y1 > p_j2k->m_private_image->y1)
11304 p_image->y1 = p_j2k->m_private_image->y1;
11306 l_img_comp = p_image->comps;
11307 for (compno=0; compno < p_image->numcomps; ++compno)
11309 OPJ_INT32 l_comp_x1, l_comp_y1;
11311 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11313 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11314 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11315 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11316 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11318 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11319 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11324 /* Destroy the previous output image*/
11325 if (p_j2k->m_output_image)
11326 opj_image_destroy(p_j2k->m_output_image);
11328 /* Create the ouput image from the information previously computed*/
11329 p_j2k->m_output_image = opj_image_create0();
11330 if (! (p_j2k->m_output_image)) {
11333 opj_copy_image_header(p_image, p_j2k->m_output_image);
11335 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11337 /* customization of the decoding */
11338 j2k_setup_decoding_tile(p_j2k);
11340 /* Decode the codestream */
11341 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11342 opj_image_destroy(p_j2k->m_private_image);
11343 p_j2k->m_private_image = NULL;
11347 /* Move data and copy one information from codec to output image*/
11348 for (compno = 0; compno < p_image->numcomps; compno++) {
11349 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11351 if (p_image->comps[compno].data)
11352 opj_free(p_image->comps[compno].data);
11354 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11356 p_j2k->m_output_image->comps[compno].data = NULL;
11362 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11364 OPJ_UINT32 it_comp;
11366 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11368 if (p_j2k->m_private_image) {
11369 if (p_j2k->m_private_image->comps) {
11370 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11371 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11372 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11373 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11374 if ( res_factor >= max_res){
11375 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11378 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11391 * Encodes all the tiles in a row.
11393 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11394 opj_stream_private_t *p_stream,
11395 opj_event_mgr_t * p_manager )
11398 OPJ_UINT32 l_nb_tiles;
11399 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11400 OPJ_BYTE * l_current_data;
11402 /* preconditions */
11403 assert(p_j2k != 00);
11404 assert(p_stream != 00);
11405 assert(p_manager != 00);
11407 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11408 if (! l_current_data) {
11411 l_max_tile_size = 1000;
11413 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11414 for (i=0;i<l_nb_tiles;++i) {
11415 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11416 opj_free(l_current_data);
11420 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11421 if (l_current_tile_size > l_max_tile_size) {
11422 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11423 if (! l_current_data) {
11426 l_max_tile_size = l_current_tile_size;
11429 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11431 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11436 opj_free(l_current_data);
11441 * Ends the compression procedures and possibility add data to be read after the
11444 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11445 opj_stream_private_t *p_stream,
11446 struct opj_event_mgr * p_manager)
11448 /* customization of the encoding */
11449 j2k_setup_end_compress(p_j2k);
11451 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11461 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11463 * @param p_j2k the jpeg2000 codec.
11464 * @param p_stream the stream object.
11465 * @param p_manager the user event manager.
11467 * @return true if the codec is valid.
11469 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11470 opj_stream_private_t *p_stream,
11471 opj_image_t * p_image,
11472 opj_event_mgr_t * p_manager)
11474 /* preconditions */
11475 assert(p_j2k != 00);
11476 assert(p_stream != 00);
11477 assert(p_manager != 00);
11479 p_j2k->m_private_image = opj_image_create0();
11480 opj_copy_image_header(p_image, p_j2k->m_private_image);
11482 // TODO_MSD: Find a better way
11483 if (p_image->comps) {
11484 OPJ_UINT32 it_comp;
11485 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11486 if (p_image->comps[it_comp].data) {
11487 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11488 p_image->comps[it_comp].data = NULL;
11494 /* customization of the validation */
11495 j2k_setup_encoding_validation (p_j2k);
11497 /* validation of the parameters codec */
11498 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11502 /* customization of the encoding */
11503 j2k_setup_header_writting(p_j2k);
11506 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11516 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11517 OPJ_UINT32 p_tile_index,
11518 opj_stream_private_t *p_stream,
11519 opj_event_mgr_t * p_manager )
11522 if (p_tile_index != p_j2k->m_current_tile_number) {
11523 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11527 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);
11529 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11530 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11531 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11533 /* initialisation before tile encoding */
11534 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11544 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11546 OPJ_UINT32 i,j,k = 0;
11547 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11548 opj_image_comp_t * l_img_comp = 00;
11549 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11550 opj_image_t * l_image = 00;
11551 OPJ_UINT32 l_size_comp, l_remaining;
11552 OPJ_INT32 * l_src_ptr;
11553 l_tilec = p_tcd->tcd_image->tiles->comps;
11554 l_image = p_tcd->image;
11555 l_img_comp = l_image->comps;
11557 for (i=0;i<p_tcd->image->numcomps;++i) {
11558 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11559 l_remaining = l_img_comp->prec & 7; /* (%8) */
11564 if (l_size_comp == 3) {
11568 l_width = (l_tilec->x1 - l_tilec->x0);
11569 l_height = (l_tilec->y1 - l_tilec->y0);
11570 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11571 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11572 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11573 l_stride = l_image_width - l_width;
11574 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11576 switch (l_size_comp) {
11579 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11580 if (l_img_comp->sgnd) {
11581 for (j=0;j<l_height;++j) {
11582 for (k=0;k<l_width;++k) {
11583 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11587 l_src_ptr += l_stride;
11591 for (j=0;j<l_height;++j) {
11592 for (k=0;k<l_width;++k) {
11593 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11597 l_src_ptr += l_stride;
11601 p_data = (OPJ_BYTE*) l_dest_ptr;
11606 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11607 if (l_img_comp->sgnd) {
11608 for (j=0;j<l_height;++j) {
11609 for (k=0;k<l_width;++k) {
11610 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11612 l_src_ptr += l_stride;
11616 for (j=0;j<l_height;++j) {
11617 for (k=0;k<l_width;++k) {
11618 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11620 l_src_ptr += l_stride;
11624 p_data = (OPJ_BYTE*) l_dest_ptr;
11629 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11630 for (j=0;j<l_height;++j) {
11631 for (k=0;k<l_width;++k) {
11632 *(l_dest_ptr++) = *(l_src_ptr++);
11634 l_src_ptr += l_stride;
11637 p_data = (OPJ_BYTE*) l_dest_ptr;
11650 * @param p_j2k the jpeg2000 codec.
11651 * @param p_stream the stream to write data to.
11652 * @param p_manager the user event manager.
11654 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11656 OPJ_UINT32 p_data_size,
11657 opj_stream_private_t *p_stream,
11658 opj_event_mgr_t * p_manager )
11660 opj_tcd_v2_t * l_tcd = 00;
11661 opj_cp_v2_t * l_cp = 00;
11662 opj_tcp_v2_t * l_tcp = 00;
11663 OPJ_UINT32 l_nb_bytes_written;
11664 OPJ_BYTE * l_current_data = 00;
11665 OPJ_UINT32 l_tile_size = 0;
11666 OPJ_UINT32 l_available_data;
11668 /* preconditions */
11669 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11671 l_tcd = p_j2k->m_tcd;
11672 l_cp = &(p_j2k->m_cp);
11673 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11675 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11676 l_available_data = l_tile_size;
11677 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11679 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11680 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11684 l_nb_bytes_written = 0;
11685 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11688 l_current_data += l_nb_bytes_written;
11689 l_available_data -= l_nb_bytes_written;
11691 l_nb_bytes_written = 0;
11692 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11696 l_available_data -= l_nb_bytes_written;
11697 l_nb_bytes_written = l_tile_size - l_available_data;
11699 if ( opj_stream_write_data( p_stream,
11700 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11701 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11705 ++p_j2k->m_current_tile_number;
11712 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11713 * are valid. Developers wanting to extend the library can add their own validation procedures.
11715 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11717 /* preconditions */
11718 assert(p_j2k != 00);
11720 /* DEVELOPER CORNER, insert your custom procedures */
11721 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11723 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11724 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11727 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11728 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11729 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11733 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11734 * are valid. Developers wanting to extend the library can add their own validation procedures.
11736 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11738 /* preconditions */
11739 assert(p_j2k != 00);
11741 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11742 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11744 /* DEVELOPER CORNER, add your custom validation procedure */
11745 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11750 * Sets up the procedures to do on writing header.
11751 * Developers wanting to extend the library can add their own writing procedures.
11753 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11755 /* preconditions */
11756 assert(p_j2k != 00);
11758 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11759 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11760 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11761 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11762 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11765 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11766 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11767 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11769 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11770 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11774 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11776 if (p_j2k->m_cp.comment != 00) {
11777 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11780 /* DEVELOPER CORNER, insert your custom procedures */
11781 if (p_j2k->m_cp.rsiz & MCT) {
11782 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11784 /* End of Developer Corner */
11786 if (p_j2k->cstr_index) {
11787 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11790 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11791 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11795 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11797 OPJ_UINT32 * p_data_written,
11798 OPJ_UINT32 p_total_data_size,
11799 opj_stream_private_t *p_stream,
11800 struct opj_event_mgr * p_manager )
11803 OPJ_UINT32 l_nb_bytes_written = 0;
11804 OPJ_UINT32 l_current_nb_bytes_written;
11805 OPJ_BYTE * l_begin_data = 00;
11807 opj_tcp_v2_t *l_tcp = 00;
11808 opj_tcd_v2_t * l_tcd = 00;
11809 opj_cp_v2_t * l_cp = 00;
11811 l_tcd = p_j2k->m_tcd;
11812 l_cp = &(p_j2k->m_cp);
11813 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11815 l_tcd->cur_pino = 0;
11817 /*Get number of tile parts*/
11818 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11823 l_current_nb_bytes_written = 0;
11824 l_begin_data = p_data;
11825 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11830 l_nb_bytes_written += l_current_nb_bytes_written;
11831 p_data += l_current_nb_bytes_written;
11832 p_total_data_size -= l_current_nb_bytes_written;
11834 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11835 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11836 l_current_nb_bytes_written = 0;
11837 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11838 l_nb_bytes_written += l_current_nb_bytes_written;
11839 p_data += l_current_nb_bytes_written;
11840 p_total_data_size -= l_current_nb_bytes_written;
11842 l_current_nb_bytes_written = 0;
11843 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11844 l_nb_bytes_written += l_current_nb_bytes_written;
11845 p_data += l_current_nb_bytes_written;
11846 p_total_data_size -= l_current_nb_bytes_written;
11849 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11850 l_current_nb_bytes_written = 0;
11851 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11852 l_nb_bytes_written += l_current_nb_bytes_written;
11853 p_data += l_current_nb_bytes_written;
11854 p_total_data_size -= l_current_nb_bytes_written;
11858 l_current_nb_bytes_written = 0;
11859 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11863 l_nb_bytes_written += l_current_nb_bytes_written;
11864 * p_data_written = l_nb_bytes_written;
11866 /* Writing Psot in SOT marker */
11867 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11869 if (l_cp->m_specific_param.m_enc.m_cinema){
11870 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11876 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11878 OPJ_UINT32 * p_data_written,
11879 OPJ_UINT32 p_total_data_size,
11880 opj_stream_private_t *p_stream,
11881 struct opj_event_mgr * p_manager
11884 OPJ_UINT32 tilepartno=0;
11885 OPJ_UINT32 l_nb_bytes_written = 0;
11886 OPJ_UINT32 l_current_nb_bytes_written;
11887 OPJ_UINT32 l_part_tile_size;
11888 OPJ_UINT32 tot_num_tp;
11891 OPJ_BYTE * l_begin_data;
11892 opj_tcp_v2_t *l_tcp = 00;
11893 opj_tcd_v2_t * l_tcd = 00;
11894 opj_cp_v2_t * l_cp = 00;
11897 l_tcd = p_j2k->m_tcd;
11898 l_cp = &(p_j2k->m_cp);
11899 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11901 /*Get number of tile parts*/
11902 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11904 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11905 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11906 l_current_nb_bytes_written = 0;
11907 l_part_tile_size = 0;
11908 l_begin_data = p_data;
11910 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11914 l_nb_bytes_written += l_current_nb_bytes_written;
11915 p_data += l_current_nb_bytes_written;
11916 p_total_data_size -= l_current_nb_bytes_written;
11917 l_part_tile_size += l_nb_bytes_written;
11919 l_current_nb_bytes_written = 0;
11920 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11924 p_data += l_current_nb_bytes_written;
11925 l_nb_bytes_written += l_current_nb_bytes_written;
11926 p_total_data_size -= l_current_nb_bytes_written;
11927 l_part_tile_size += l_nb_bytes_written;
11929 /* Writing Psot in SOT marker */
11930 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11932 if (l_cp->m_specific_param.m_enc.m_cinema) {
11933 j2k_update_tlm(p_j2k,l_part_tile_size);
11936 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11939 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11940 l_tcd->cur_pino = pino;
11942 /*Get number of tile parts*/
11943 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11944 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11945 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11946 l_current_nb_bytes_written = 0;
11947 l_part_tile_size = 0;
11948 l_begin_data = p_data;
11950 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11954 l_nb_bytes_written += l_current_nb_bytes_written;
11955 p_data += l_current_nb_bytes_written;
11956 p_total_data_size -= l_current_nb_bytes_written;
11957 l_part_tile_size += l_current_nb_bytes_written;
11959 l_current_nb_bytes_written = 0;
11961 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11965 l_nb_bytes_written += l_current_nb_bytes_written;
11966 p_data += l_current_nb_bytes_written;
11967 p_total_data_size -= l_current_nb_bytes_written;
11968 l_part_tile_size += l_current_nb_bytes_written;
11970 /* Writing Psot in SOT marker */
11971 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11973 if (l_cp->m_specific_param.m_enc.m_cinema) {
11974 j2k_update_tlm(p_j2k,l_part_tile_size);
11977 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11981 *p_data_written = l_nb_bytes_written;
11987 * Writes the updated tlm.
11989 * @param p_stream the stream to write data to.
11990 * @param p_j2k J2K codec.
11991 * @param p_manager the user event manager.
11993 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11994 struct opj_stream_private *p_stream,
11995 struct opj_event_mgr * p_manager )
11997 OPJ_UINT32 l_tlm_size;
11998 OPJ_SIZE_T l_tlm_position, l_current_position;
12000 /* preconditions */
12001 assert(p_j2k != 00);
12002 assert(p_manager != 00);
12003 assert(p_stream != 00);
12005 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12006 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12007 l_current_position = opj_stream_tell(p_stream);
12009 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12013 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) {
12017 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12026 * Ends the encoding, i.e. frees memory.
12028 * @param p_stream the stream to write data to.
12029 * @param p_j2k J2K codec.
12030 * @param p_manager the user event manager.
12032 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
12033 struct opj_stream_private *p_stream,
12034 struct opj_event_mgr * p_manager )
12036 /* preconditions */
12037 assert(p_j2k != 00);
12038 assert(p_manager != 00);
12039 assert(p_stream != 00);
12041 tcd_destroy_v2(p_j2k->m_tcd);
12044 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12045 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12046 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12047 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12050 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12051 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12052 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12055 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12061 * Destroys the memory associated with the decoding of headers.
12063 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12064 opj_stream_private_t *p_stream,
12065 opj_event_mgr_t * p_manager )
12067 /* preconditions */
12068 assert(p_j2k != 00);
12069 assert(p_stream != 00);
12070 assert(p_manager != 00);
12072 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12073 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12074 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12077 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12086 * @param p_stream the stream to write data to.
12087 * @param p_j2k J2K codec.
12088 * @param p_manager the user event manager.
12090 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12091 struct opj_stream_private *p_stream,
12092 struct opj_event_mgr * p_manager )
12094 opj_codestream_info_t * l_cstr_info = 00;
12096 /* preconditions */
12097 assert(p_j2k != 00);
12098 assert(p_manager != 00);
12099 assert(p_stream != 00);
12102 /* TODO mergeV2: check this part which use cstr_info */
12103 /*l_cstr_info = p_j2k->cstr_info;
12107 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));
12109 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12110 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12112 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12114 l_cstr_info->tw = p_j2k->m_cp.tw;
12115 l_cstr_info->th = p_j2k->m_cp.th;
12117 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12118 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12119 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12120 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12122 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12124 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12126 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12128 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12129 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12132 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12134 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12136 /*l_cstr_info->maxmarknum = 100;
12137 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12138 l_cstr_info->marknum = 0;
12141 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);
12145 * Creates a tile-coder decoder.
12147 * @param p_stream the stream to write data to.
12148 * @param p_j2k J2K codec.
12149 * @param p_manager the user event manager.
12151 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12152 struct opj_stream_private *p_stream,
12153 struct opj_event_mgr * p_manager )
12155 /* preconditions */
12156 assert(p_j2k != 00);
12157 assert(p_manager != 00);
12158 assert(p_stream != 00);
12160 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12162 if (! p_j2k->m_tcd) {
12163 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12167 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12168 tcd_destroy_v2(p_j2k->m_tcd);
12179 * @param p_j2k the jpeg2000 codec.
12180 * @param p_stream the stream to write data to.
12181 * @param p_manager the user event manager.
12183 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12184 OPJ_UINT32 p_tile_index,
12186 OPJ_UINT32 p_data_size,
12187 opj_stream_private_t *p_stream,
12188 opj_event_mgr_t * p_manager )
12190 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12191 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12195 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12196 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);