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 * Reads a QCD marker (Quantization defaults)
600 * @param p_header_data the data contained in the QCD box.
601 * @param p_j2k the jpeg2000 codec.
602 * @param p_header_size the size of the data contained in the QCD marker.
603 * @param p_manager the user event manager.
605 static opj_bool j2k_read_qcd_v2 (
607 OPJ_BYTE * p_header_data,
608 OPJ_UINT32 p_header_size,
609 struct opj_event_mgr * p_manager
613 Write the QCC marker (quantization component)
614 @param j2k J2K handle
615 @param compno Number of the component concerned by the information written
617 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
620 * Writes the QCC marker (quantization component)
622 * @param p_comp_no the index of the component to output.
623 * @param p_stream the stream to write data to.
624 * @param p_j2k J2K codec.
625 * @param p_manager the user event manager.
627 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
628 OPJ_UINT32 p_comp_no,
629 struct opj_stream_private *p_stream,
630 struct opj_event_mgr * p_manager );
633 * Writes the QCC marker (quantization component)
635 * @param p_comp_no the index of the component to output.
636 * @param p_stream the stream to write data to.
637 * @param p_j2k J2K codec.
638 * @param p_manager the user event manager.
640 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
641 OPJ_UINT32 p_comp_no,
643 OPJ_UINT32 * p_data_written,
644 struct opj_event_mgr * p_manager );
647 * Gets the maximum size taken by a qcc.
649 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
652 Read the QCC marker (quantization component)
653 @param j2k J2K handle
655 static void j2k_read_qcc(opj_j2k_t *j2k);
657 * Reads a QCC marker (Quantization component)
658 * @param p_header_data the data contained in the QCC box.
659 * @param p_j2k the jpeg2000 codec.
660 * @param p_header_size the size of the data contained in the QCC marker.
661 * @param p_manager the user event manager.
663 static opj_bool j2k_read_qcc_v2(
665 OPJ_BYTE * p_header_data,
666 OPJ_UINT32 p_header_size,
667 struct opj_event_mgr * p_manager);
670 Write the POC marker (progression order change)
671 @param j2k J2K handle
673 static void j2k_write_poc(opj_j2k_t *j2k);
676 * Writes the POC marker (Progression Order Change)
678 * @param p_stream the stream to write data to.
679 * @param p_j2k J2K codec.
680 * @param p_manager the user event manager.
682 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
683 struct opj_stream_private *p_stream,
684 struct opj_event_mgr * p_manager );
687 * Writes the POC marker (Progression Order Change)
689 * @param p_stream the stream to write data to.
690 * @param p_j2k J2K codec.
691 * @param p_manager the user event manager.
693 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
695 OPJ_UINT32 * p_data_written,
696 struct opj_event_mgr * p_manager );
699 * Gets the maximum size taken by the writting of a POC.
701 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
704 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
706 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
709 * Gets the maximum size taken by the headers of the SOT.
711 * @param p_j2k the jpeg2000 codec to use.
713 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
716 Read the POC marker (progression order change)
717 @param j2k J2K handle
719 static void j2k_read_poc(opj_j2k_t *j2k);
721 * Reads a POC marker (Progression Order Change)
723 * @param p_header_data the data contained in the POC box.
724 * @param p_j2k the jpeg2000 codec.
725 * @param p_header_size the size of the data contained in the POC marker.
726 * @param p_manager the user event manager.
728 static opj_bool j2k_read_poc_v2 (
730 OPJ_BYTE * p_header_data,
731 OPJ_UINT32 p_header_size,
732 struct opj_event_mgr * p_manager
736 Read the CRG marker (component registration)
737 @param j2k J2K handle
739 static void j2k_read_crg(opj_j2k_t *j2k);
741 * Reads a CRG marker (Component registration)
743 * @param p_header_data the data contained in the TLM box.
744 * @param p_j2k the jpeg2000 codec.
745 * @param p_header_size the size of the data contained in the TLM marker.
746 * @param p_manager the user event manager.
748 static opj_bool j2k_read_crg_v2 (
750 OPJ_BYTE * p_header_data,
751 OPJ_UINT32 p_header_size,
752 struct opj_event_mgr * p_manager
755 Read the TLM marker (tile-part lengths)
756 @param j2k J2K handle
758 static void j2k_read_tlm(opj_j2k_t *j2k);
760 * Reads a TLM marker (Tile Length Marker)
762 * @param p_header_data the data contained in the TLM box.
763 * @param p_j2k the jpeg2000 codec.
764 * @param p_header_size the size of the data contained in the TLM marker.
765 * @param p_manager the user event manager.
767 static opj_bool j2k_read_tlm_v2 (
769 OPJ_BYTE * p_header_data,
770 OPJ_UINT32 p_header_size,
771 struct opj_event_mgr * p_manager
775 * Writes the updated tlm.
777 * @param p_stream the stream to write data to.
778 * @param p_j2k J2K codec.
779 * @param p_manager the user event manager.
781 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
782 struct opj_stream_private *p_stream,
783 struct opj_event_mgr * p_manager );
786 Read the PLM marker (packet length, main header)
787 @param j2k J2K handle
789 static void j2k_read_plm(opj_j2k_t *j2k);
792 * Reads a PLM marker (Packet length, main header marker)
794 * @param p_header_data the data contained in the TLM box.
795 * @param p_j2k the jpeg2000 codec.
796 * @param p_header_size the size of the data contained in the TLM marker.
797 * @param p_manager the user event manager.
799 static opj_bool j2k_read_plm_v2 (
801 OPJ_BYTE * p_header_data,
802 OPJ_UINT32 p_header_size,
803 struct opj_event_mgr * p_manager
806 Read the PLT marker (packet length, tile-part header)
807 @param j2k J2K handle
809 static void j2k_read_plt(opj_j2k_t *j2k);
811 * Reads a PLT marker (Packet length, tile-part header)
813 * @param p_header_data the data contained in the PLT box.
814 * @param p_j2k the jpeg2000 codec.
815 * @param p_header_size the size of the data contained in the PLT marker.
816 * @param p_manager the user event manager.
818 static opj_bool j2k_read_plt_v2 (
820 OPJ_BYTE * p_header_data,
821 OPJ_UINT32 p_header_size,
822 struct opj_event_mgr * p_manager
825 Read the PPM marker (packet packet headers, main header)
826 @param j2k J2K handle
828 static void j2k_read_ppm(opj_j2k_t *j2k);
830 * Reads a PPM marker (Packed packet headers, main header)
832 * @param p_header_data the data contained in the POC box.
833 * @param p_j2k the jpeg2000 codec.
834 * @param p_header_size the size of the data contained in the POC marker.
835 * @param p_manager the user event manager.
838 static opj_bool j2k_read_ppm_v2 (
840 OPJ_BYTE * p_header_data,
841 OPJ_UINT32 p_header_size,
842 struct opj_event_mgr * p_manager
846 static opj_bool j2k_read_ppm_v3 (
848 OPJ_BYTE * p_header_data,
849 OPJ_UINT32 p_header_size,
850 struct opj_event_mgr * p_manager
854 Read the PPT marker (packet packet headers, tile-part header)
855 @param j2k J2K handle
857 static void j2k_read_ppt(opj_j2k_t *j2k);
859 * Reads a PPT marker (Packed packet headers, tile-part header)
861 * @param p_header_data the data contained in the PPT box.
862 * @param p_j2k the jpeg2000 codec.
863 * @param p_header_size the size of the data contained in the PPT marker.
864 * @param p_manager the user event manager.
866 static opj_bool j2k_read_ppt_v2 (
868 OPJ_BYTE * p_header_data,
869 OPJ_UINT32 p_header_size,
870 struct opj_event_mgr * p_manager
873 Write the TLM marker (Mainheader)
874 @param j2k J2K handle
876 static void j2k_write_tlm(opj_j2k_t *j2k);
879 * Writes the TLM marker (Tile Length Marker)
881 * @param p_stream the stream to write data to.
882 * @param p_j2k J2K codec.
883 * @param p_manager the user event manager.
885 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
886 struct opj_stream_private *p_stream,
887 struct opj_event_mgr * p_manager );
890 Write the SOT marker (start of tile-part)
891 @param j2k J2K handle
893 static void j2k_write_sot(opj_j2k_t *j2k);
896 * Writes the SOT marker (Start of tile-part)
898 * @param p_stream the stream to write data to.
899 * @param p_j2k J2K codec.
900 * @param p_manager the user event manager.
902 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
904 OPJ_UINT32 * p_data_written,
905 const struct opj_stream_private *p_stream,
906 struct opj_event_mgr * p_manager );
909 * Reads a PPT marker (Packed packet headers, tile-part header)
911 * @param p_header_data the data contained in the PPT box.
912 * @param p_j2k the jpeg2000 codec.
913 * @param p_header_size the size of the data contained in the PPT marker.
914 * @param p_manager the user event manager.
916 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
917 OPJ_BYTE * p_header_data,
918 OPJ_UINT32 p_header_size,
919 opj_event_mgr_t * p_manager );
922 Write the SOD marker (start of data)
923 @param j2k J2K handle
924 @param tile_coder Pointer to a TCD handle
926 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
929 * Writes the SOD marker (Start of data)
931 * @param p_stream the stream to write data to.
932 * @param p_j2k J2K codec.
933 * @param p_manager the user event manager.
935 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
936 struct opj_tcd_v2 * p_tile_coder,
938 OPJ_UINT32 * p_data_written,
939 OPJ_UINT32 p_total_data_size,
940 const struct opj_stream_private *p_stream,
941 struct opj_event_mgr * p_manager );
944 * Reads a SOD marker (Start Of Data)
946 * @param p_header_data the data contained in the SOD box.
947 * @param p_j2k the jpeg2000 codec.
948 * @param p_header_size the size of the data contained in the SOD marker.
949 * @param p_manager the user event manager.
951 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
952 opj_stream_private_t *p_stream,
953 opj_event_mgr_t * p_manager );
956 * Updates the Tile Length Marker.
958 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
960 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
961 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
963 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
964 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
968 Write the RGN marker (region-of-interest)
969 @param j2k J2K handle
970 @param compno Number of the component concerned by the information written
971 @param tileno Number of the tile concerned by the information written
973 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
976 * Writes the RGN marker (Region Of Interest)
978 * @param p_tile_no the tile to output
979 * @param p_comp_no the component to output
980 * @param p_stream the stream to write data to.
981 * @param p_j2k J2K codec.
982 * @param p_manager the user event manager.
984 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
985 OPJ_UINT32 p_tile_no,
986 OPJ_UINT32 p_comp_no,
987 struct opj_stream_private *p_stream,
988 struct opj_event_mgr * p_manager );
991 * Reads a RGN marker (Region Of Interest)
993 * @param p_header_data the data contained in the POC box.
994 * @param p_j2k the jpeg2000 codec.
995 * @param p_header_size the size of the data contained in the POC marker.
996 * @param p_manager the user event manager.
998 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
999 OPJ_BYTE * p_header_data,
1000 OPJ_UINT32 p_header_size,
1001 opj_event_mgr_t * p_manager );
1004 * Writes the EOC marker (End of Codestream)
1006 * @param p_stream the stream to write data to.
1007 * @param p_j2k J2K codec.
1008 * @param p_manager the user event manager.
1010 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
1011 struct opj_stream_private *p_stream,
1012 struct opj_event_mgr * p_manager );
1015 Write the EOC marker (end of codestream)
1016 @param j2k J2K handle
1018 static void j2k_write_eoc(opj_j2k_t *j2k);
1020 Read the EOC marker (end of codestream)
1021 @param j2k J2K handle
1023 static void j2k_read_eoc(opj_j2k_t *j2k);
1026 * Reads a EOC marker (End Of Codestream)
1028 * @param p_header_data the data contained in the SOD box.
1029 * @param p_j2k the jpeg2000 codec.
1030 * @param p_header_size the size of the data contained in the SOD marker.
1031 * @param p_manager the user event manager.
1034 static opj_bool j2k_read_eoc_v2 (
1035 opj_j2k_v2_t *p_j2k,
1036 struct opj_stream_private *p_stream,
1037 struct opj_event_mgr * p_manager
1044 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1046 * @param p_stream the stream to write data to.
1047 * @param p_j2k J2K codec.
1048 * @param p_manager the user event manager.
1050 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1051 struct opj_stream_private *p_stream,
1052 struct opj_event_mgr * p_manager );
1057 * @param p_stream the stream to write data to.
1058 * @param p_j2k J2K codec.
1059 * @param p_manager the user event manager.
1061 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1062 struct opj_stream_private *p_stream,
1063 struct opj_event_mgr * p_manager );
1066 Read an unknown marker
1067 @param j2k J2K handle
1069 static void j2k_read_unk(opj_j2k_t *j2k);
1071 Add main header marker information
1072 @param cstr_info Codestream information structure
1073 @param type marker type
1074 @param pos byte offset of marker segment
1075 @param len length of marker segment
1077 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1079 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1081 Add tile header marker information
1082 @param tileno tile index number
1083 @param cstr_info Codestream information structure
1084 @param type marker type
1085 @param pos byte offset of marker segment
1086 @param len length of marker segment
1088 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1090 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);
1093 * Reads an unknown marker
1095 * @param p_stream the stream object to read from.
1096 * @param p_j2k the jpeg2000 codec.
1097 * @param p_manager the user event manager.
1099 * @return true if the marker could be deduced.
1101 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1102 struct opj_stream_private *p_stream,
1103 OPJ_UINT32 *output_marker,
1104 struct opj_event_mgr * p_manager );
1107 * Writes the MCT marker (Multiple Component Transform)
1109 * @param p_stream the stream to write data to.
1110 * @param p_j2k J2K codec.
1111 * @param p_manager the user event manager.
1113 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1114 opj_mct_data_t * p_mct_record,
1115 struct opj_stream_private *p_stream,
1116 struct opj_event_mgr * p_manager );
1119 * Reads a MCT marker (Multiple Component Transform)
1121 * @param p_header_data the data contained in the MCT box.
1122 * @param p_j2k the jpeg2000 codec.
1123 * @param p_header_size the size of the data contained in the MCT marker.
1124 * @param p_manager the user event manager.
1126 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1127 OPJ_BYTE * p_header_data,
1128 OPJ_UINT32 p_header_size,
1129 struct opj_event_mgr * p_manager );
1132 * Writes the MCC marker (Multiple Component Collection)
1134 * @param p_stream the stream to write data to.
1135 * @param p_j2k J2K codec.
1136 * @param p_manager the user event manager.
1138 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1139 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1140 struct opj_stream_private *p_stream,
1141 struct opj_event_mgr * p_manager );
1145 * Reads a MCC marker (Multiple Component Collection)
1147 * @param p_header_data the data contained in the MCC box.
1148 * @param p_j2k the jpeg2000 codec.
1149 * @param p_header_size the size of the data contained in the MCC marker.
1150 * @param p_manager the user event manager.
1152 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1153 OPJ_BYTE * p_header_data,
1154 OPJ_UINT32 p_header_size,
1155 struct opj_event_mgr * p_manager );
1158 * Writes the MCO marker (Multiple component transformation ordering)
1160 * @param p_stream the stream to write data to.
1161 * @param p_j2k J2K codec.
1162 * @param p_manager the user event manager.
1164 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1165 struct opj_stream_private *p_stream,
1166 struct opj_event_mgr * p_manager );
1169 * Reads a MCO marker (Multiple Component Transform Ordering)
1171 * @param p_header_data the data contained in the MCO box.
1172 * @param p_j2k the jpeg2000 codec.
1173 * @param p_header_size the size of the data contained in the MCO marker.
1174 * @param p_manager the user event manager.
1176 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1177 OPJ_BYTE * p_header_data,
1178 OPJ_UINT32 p_header_size,
1179 struct opj_event_mgr * p_manager );
1181 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1183 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1184 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1185 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1186 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1188 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1189 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1190 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1191 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1193 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1194 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1195 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1196 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1200 * Ends the encoding, i.e. frees memory.
1202 * @param p_stream the stream to write data to.
1203 * @param p_j2k J2K codec.
1204 * @param p_manager the user event manager.
1206 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1207 struct opj_stream_private *p_stream,
1208 struct opj_event_mgr * p_manager );
1211 * Writes the CBD marker (Component bit depth definition)
1213 * @param p_stream the stream to write data to.
1214 * @param p_j2k J2K codec.
1215 * @param p_manager the user event manager.
1217 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1218 struct opj_stream_private *p_stream,
1219 struct opj_event_mgr * p_manager );
1222 * Reads a CBD marker (Component bit depth definition)
1223 * @param p_header_data the data contained in the CBD box.
1224 * @param p_j2k the jpeg2000 codec.
1225 * @param p_header_size the size of the data contained in the CBD marker.
1226 * @param p_manager the user event manager.
1228 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1229 OPJ_BYTE * p_header_data,
1230 OPJ_UINT32 p_header_size,
1231 struct opj_event_mgr * p_manager);
1234 * Writes the image components.
1236 * @param p_stream the stream to write data to.
1237 * @param p_j2k J2K codec.
1238 * @param p_manager the user event manager.
1240 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1241 struct opj_stream_private *p_stream,
1242 struct opj_event_mgr * p_manager );
1245 * Writes regions of interests.
1247 * @param p_stream the stream to write data to.
1248 * @param p_j2k J2K codec.
1249 * @param p_manager the user event manager.
1251 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1252 struct opj_stream_private *p_stream,
1253 struct opj_event_mgr * p_manager );
1258 * @param p_stream the stream to write data to.
1259 * @param p_j2k J2K codec.
1260 * @param p_manager the user event manager.
1262 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1263 struct opj_stream_private *p_stream,
1264 struct opj_event_mgr * p_manager );
1267 * Checks the progression order changes values. Tells of the poc given as input are valid.
1268 * A nice message is outputted at errors.
1270 * @param p_pocs the progression order changes.
1271 * @param p_nb_pocs the number of progression order changes.
1272 * @param p_nb_resolutions the number of resolutions.
1273 * @param numcomps the number of components
1274 * @param numlayers the number of layers.
1276 * @return true if the pocs are valid.
1278 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1279 OPJ_UINT32 p_nb_pocs,
1280 OPJ_UINT32 p_nb_resolutions,
1281 OPJ_UINT32 numcomps,
1282 OPJ_UINT32 numlayers,
1283 opj_event_mgr_t * p_manager);
1286 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1288 * @param cp the coding parameters.
1289 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1290 * @param tileno the given tile.
1292 * @return the number of tile parts.
1294 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1296 /** mem allocation for TLM marker*/
1297 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1300 * Calculates the total number of tile parts needed by the encoder to
1301 * encode such an image. If not enough memory is available, then the function return false.
1303 * @param p_nb_tiles pointer that will hold the number of tile parts.
1304 * @param cp the coding parameters for the image.
1305 * @param image the image to encode.
1306 * @param p_j2k the p_j2k encoder.
1307 * @param p_manager the user event manager.
1309 * @return true if the function was successful, false else.
1311 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1313 OPJ_UINT32 * p_nb_tiles,
1315 opj_event_mgr_t * p_manager);
1317 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1319 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1321 static opj_codestream_index_t* j2k_create_cstr_index(void);
1327 /* ----------------------------------------------------------------------- */
1328 typedef struct j2k_prog_order{
1329 OPJ_PROG_ORDER enum_prog;
1333 j2k_prog_order_t j2k_prog_order_list[] = {
1339 {(OPJ_PROG_ORDER)-1, ""}
1347 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1355 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1357 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1359 j2k_read_int16_to_float,
1360 j2k_read_int32_to_float,
1361 j2k_read_float32_to_float,
1362 j2k_read_float64_to_float
1365 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1367 j2k_read_int16_to_int32,
1368 j2k_read_int32_to_int32,
1369 j2k_read_float32_to_int32,
1370 j2k_read_float64_to_int32
1373 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1375 j2k_write_float_to_int16,
1376 j2k_write_float_to_int32,
1377 j2k_write_float_to_float,
1378 j2k_write_float_to_float64
1381 typedef struct opj_dec_memory_marker_handler
1385 /** value of the state when the marker can appear */
1387 /** action linked to the marker */
1388 opj_bool (*handler) (
1389 opj_j2k_v2_t *p_j2k,
1390 OPJ_BYTE * p_header_data,
1391 OPJ_UINT32 p_header_size,
1392 struct opj_event_mgr * p_manager
1395 opj_dec_memory_marker_handler_t;
1397 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1400 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1401 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1402 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1403 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1404 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1405 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1406 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1407 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1408 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1409 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1410 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1411 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1412 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1414 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1415 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1416 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1417 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1418 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1419 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1421 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1422 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1423 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1424 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1425 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1426 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1427 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1428 {J2K_MS_SIZ, J2K_STATE_MHSIZ , opj_j2k_read_siz},
1429 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1430 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1431 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1432 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1433 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1435 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1436 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1437 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1438 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1439 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1440 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1442 #ifdef TODO_MS /* FIXME */
1443 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1444 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1445 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1446 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1448 #endif /* USE_JPWL */
1450 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1451 {J2K_MS_INSEC, 0, j2k_read_insec}
1452 #endif /* USE_JPSEC */
1453 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1458 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1460 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1461 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1465 for (i=0;i<p_nb_elem;++i) {
1466 opj_read_bytes(l_src_data,&l_temp,2);
1468 l_src_data+=sizeof(OPJ_INT16);
1470 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1474 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1476 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1477 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1481 for (i=0;i<p_nb_elem;++i) {
1482 opj_read_bytes(l_src_data,&l_temp,4);
1484 l_src_data+=sizeof(OPJ_INT32);
1486 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1490 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1492 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1493 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1497 for (i=0;i<p_nb_elem;++i) {
1498 opj_read_float(l_src_data,&l_temp);
1500 l_src_data+=sizeof(OPJ_FLOAT32);
1502 *(l_dest_data++) = l_temp;
1506 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1508 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1509 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1513 for (i=0;i<p_nb_elem;++i) {
1514 opj_read_double(l_src_data,&l_temp);
1516 l_src_data+=sizeof(OPJ_FLOAT64);
1518 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1522 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1524 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1525 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1529 for (i=0;i<p_nb_elem;++i) {
1530 opj_read_bytes(l_src_data,&l_temp,2);
1532 l_src_data+=sizeof(OPJ_INT16);
1534 *(l_dest_data++) = (OPJ_INT32) l_temp;
1538 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1540 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1541 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1545 for (i=0;i<p_nb_elem;++i) {
1546 opj_read_bytes(l_src_data,&l_temp,4);
1548 l_src_data+=sizeof(OPJ_INT32);
1550 *(l_dest_data++) = (OPJ_INT32) l_temp;
1554 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1556 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1557 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1561 for (i=0;i<p_nb_elem;++i) {
1562 opj_read_float(l_src_data,&l_temp);
1564 l_src_data+=sizeof(OPJ_FLOAT32);
1566 *(l_dest_data++) = (OPJ_INT32) l_temp;
1570 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1572 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1573 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1577 for (i=0;i<p_nb_elem;++i) {
1578 opj_read_double(l_src_data,&l_temp);
1580 l_src_data+=sizeof(OPJ_FLOAT64);
1582 *(l_dest_data++) = (OPJ_INT32) l_temp;
1586 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1588 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1589 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1593 for (i=0;i<p_nb_elem;++i) {
1594 l_temp = (OPJ_UINT32) *(l_src_data++);
1596 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1598 l_dest_data+=sizeof(OPJ_INT16);
1602 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1604 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1605 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1609 for (i=0;i<p_nb_elem;++i) {
1610 l_temp = (OPJ_UINT32) *(l_src_data++);
1612 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1614 l_dest_data+=sizeof(OPJ_INT32);
1618 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1620 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1621 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1625 for (i=0;i<p_nb_elem;++i) {
1626 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1628 opj_write_float(l_dest_data,l_temp);
1630 l_dest_data+=sizeof(OPJ_FLOAT32);
1634 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1636 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1637 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1641 for (i=0;i<p_nb_elem;++i) {
1642 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1644 opj_write_double(l_dest_data,l_temp);
1646 l_dest_data+=sizeof(OPJ_FLOAT64);
1652 * Converts an enum type progression order to string type.
1654 * @param prg_order the progression order to get.
1656 * @return the string representation of the given progression order.
1658 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1659 j2k_prog_order_t *po;
1660 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1661 if(po->enum_prog == prg_order){
1662 return po->str_prog;
1665 return po->str_prog;
1669 * Checks the progression order changes values. Tells if the poc given as input are valid.
1671 * @param p_pocs the progression order changes.
1672 * @param p_nb_pocs the number of progression order changes.
1673 * @param p_nb_resolutions the number of resolutions.
1674 * @param numcomps the number of components
1675 * @param numlayers the number of layers.
1676 * @param p_manager the user event manager.
1678 * @return true if the pocs are valid.
1680 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1681 OPJ_UINT32 p_nb_pocs,
1682 OPJ_UINT32 p_nb_resolutions,
1683 OPJ_UINT32 p_num_comps,
1684 OPJ_UINT32 p_num_layers,
1685 opj_event_mgr_t * p_manager)
1687 OPJ_UINT32* packet_array;
1688 OPJ_UINT32 index , resno, compno, layno;
1690 OPJ_UINT32 step_c = 1;
1691 OPJ_UINT32 step_r = p_num_comps * step_c;
1692 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1693 opj_bool loss = OPJ_FALSE;
1694 OPJ_UINT32 layno0 = 0;
1696 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1697 if (packet_array == 00) {
1698 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1701 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1703 if (p_nb_pocs == 0) {
1707 index = step_r * p_pocs->resno0;
1708 // take each resolution for each poc
1709 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1711 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1713 // take each comp of each resolution for each poc
1714 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1715 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1717 // and finally take each layer of each res of ...
1718 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1719 //index = step_r * resno + step_c * compno + step_l * layno;
1720 packet_array[comp_index] = 1;
1721 comp_index += step_l;
1724 res_index += step_c;
1731 // iterate through all the pocs
1732 for (i = 1; i < p_nb_pocs ; ++i) {
1733 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1735 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1736 index = step_r * p_pocs->resno0;
1738 // take each resolution for each poc
1739 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1740 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1742 // take each comp of each resolution for each poc
1743 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1744 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1746 // and finally take each layer of each res of ...
1747 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1748 //index = step_r * resno + step_c * compno + step_l * layno;
1749 packet_array[comp_index] = 1;
1750 comp_index += step_l;
1753 res_index += step_c;
1763 for (layno = 0; layno < p_num_layers ; ++layno) {
1764 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1765 for (compno = 0; compno < p_num_comps; ++compno) {
1766 loss |= (packet_array[index]!=1);
1767 //index = step_r * resno + step_c * compno + step_l * layno;
1774 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1777 opj_free(packet_array);
1782 /* ----------------------------------------------------------------------- */
1783 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1786 int tpnum=1,tpend=0;
1787 opj_tcp_t *tcp = &cp->tcps[tileno];
1788 prog = j2k_convert_progression_order(tcp->prg);
1793 if( cp->tp_flag == prog[i] ){
1794 tpend=1;cp->tp_pos=i;
1798 tpnum= tpnum * tcp->pocs[pino].compE;
1801 tpnum= tpnum * tcp->pocs[pino].resE;
1804 tpnum= tpnum * tcp->pocs[pino].prcE;
1807 tpnum= tpnum * tcp->pocs[pino].layE;
1819 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1821 * @param cp the coding parameters.
1822 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1823 * @param tileno the given tile.
1825 * @return the number of tile parts.
1827 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1829 const OPJ_CHAR *prog = 00;
1831 OPJ_UINT32 tpnum = 1;
1832 opj_tcp_v2_t *tcp = 00;
1833 opj_poc_t * l_current_poc = 00;
1836 assert(tileno < (cp->tw * cp->th));
1837 assert(pino < (cp->tcps[tileno].numpocs + 1));
1839 /* get the given tile coding parameter */
1840 tcp = &cp->tcps[tileno];
1843 l_current_poc = &(tcp->pocs[pino]);
1844 assert(l_current_poc != 0);
1846 /* get the progression order as a character string */
1847 prog = j2k_convert_progression_order(tcp->prg);
1848 assert(strlen(prog) > 0);
1850 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1854 /* component wise */
1856 tpnum *= l_current_poc->compE;
1858 /* resolution wise */
1860 tpnum *= l_current_poc->resE;
1864 tpnum *= l_current_poc->prcE;
1868 tpnum *= l_current_poc->layE;
1871 /* whould we split here ? */
1872 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1873 cp->m_specific_param.m_enc.m_tp_pos=i;
1885 /** mem allocation for TLM marker*/
1886 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1887 int pino,tileno,totnum_tp=0;
1889 OPJ_ARG_NOT_USED(img_numcomp);
1891 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1892 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1893 int cur_totnum_tp = 0;
1894 opj_tcp_t *tcp = &cp->tcps[tileno];
1895 for(pino = 0; pino <= tcp->numpocs; pino++) {
1897 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1898 if(!pi) { return -1;}
1899 tp_num = j2k_get_num_tp(cp,pino,tileno);
1900 totnum_tp = totnum_tp + tp_num;
1901 cur_totnum_tp = cur_totnum_tp + tp_num;
1902 pi_destroy(pi, cp, tileno);
1904 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1906 if (j2k->cstr_info) {
1907 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1908 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1916 * Calculates the total number of tile parts needed by the encoder to
1917 * encode such an image. If not enough memory is available, then the function return false.
1919 * @param p_nb_tiles pointer that will hold the number of tile parts.
1920 * @param cp the coding parameters for the image.
1921 * @param image the image to encode.
1922 * @param p_j2k the p_j2k encoder.
1923 * @param p_manager the user event manager.
1925 * @return true if the function was successful, false else.
1927 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1929 OPJ_UINT32 * p_nb_tiles,
1931 opj_event_mgr_t * p_manager)
1933 OPJ_UINT32 pino,tileno;
1934 OPJ_UINT32 l_nb_tiles;
1938 assert(p_nb_tiles != 00);
1940 assert(image != 00);
1941 assert(p_j2k != 00);
1942 assert(p_manager != 00);
1944 l_nb_tiles = cp->tw * cp->th;
1949 /* TODO mergeV2: check this part which use cstr_info */
1950 /*if (p_j2k->cstr_info) {
1951 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1953 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1954 OPJ_UINT32 cur_totnum_tp = 0;
1956 pi_update_encoding_parameters(image,cp,tileno);
1958 for (pino = 0; pino <= tcp->numpocs; ++pino)
1960 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1962 *p_nb_tiles = *p_nb_tiles + tp_num;
1964 cur_totnum_tp += tp_num;
1967 tcp->m_nb_tile_parts = cur_totnum_tp;
1969 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1970 if (l_info_tile_ptr->tp == 00) {
1974 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1976 l_info_tile_ptr->num_tps = cur_totnum_tp;
1983 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1984 OPJ_UINT32 cur_totnum_tp = 0;
1986 pi_update_encoding_parameters(image,cp,tileno);
1988 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1989 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1991 *p_nb_tiles = *p_nb_tiles + tp_num;
1993 cur_totnum_tp += tp_num;
1995 tcp->m_nb_tile_parts = cur_totnum_tp;
2004 static void j2k_write_soc(opj_j2k_t *j2k) {
2005 opj_cio_t *cio = j2k->cio;
2006 cio_write(cio, J2K_MS_SOC, 2);
2009 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2014 /* update markers struct */
2015 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2016 #endif /* USE_JPWL */
2021 * Writes the SOC marker (Start Of Codestream)
2023 * @param p_stream the stream to write data to.
2024 * @param p_j2k J2K codec.
2025 * @param p_manager the user event manager.
2027 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2028 struct opj_stream_private *p_stream,
2029 struct opj_event_mgr * p_manager )
2031 /* 2 bytes will be written */
2032 OPJ_BYTE * l_start_stream = 00;
2035 assert(p_stream != 00);
2036 assert(p_j2k != 00);
2037 assert(p_manager != 00);
2039 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2041 /* write SOC identifier */
2042 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2044 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2050 /* update markers struct */
2052 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2054 assert( 0 && "TODO" );
2055 #endif /* USE_JPWL */
2064 * Reads a SOC marker (Start of Codestream)
2065 * @param p_header_data the data contained in the SOC box.
2066 * @param jp2 the jpeg2000 file codec.
2067 * @param p_header_size the size of the data contained in the SOC marker.
2068 * @param p_manager the user event manager.
2070 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2071 struct opj_stream_private *p_stream,
2072 struct opj_event_mgr * p_manager )
2074 OPJ_BYTE l_data [2];
2075 OPJ_UINT32 l_marker;
2078 assert(p_j2k != 00);
2079 assert(p_manager != 00);
2080 assert(p_stream != 00);
2082 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2086 opj_read_bytes(l_data,&l_marker,2);
2087 if (l_marker != J2K_MS_SOC) {
2091 /* Next marker should be a SIZ marker in the main header */
2092 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2094 /* FIXME move it in a index structure included in p_j2k*/
2095 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2097 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2099 /* Add the marker to the codestream index*/
2100 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2105 static void j2k_write_siz(opj_j2k_t *j2k) {
2109 opj_cio_t *cio = j2k->cio;
2110 opj_image_t *image = j2k->image;
2111 opj_cp_t *cp = j2k->cp;
2113 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2114 lenp = cio_tell(cio);
2116 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2117 cio_write(cio, image->x1, 4); /* Xsiz */
2118 cio_write(cio, image->y1, 4); /* Ysiz */
2119 cio_write(cio, image->x0, 4); /* X0siz */
2120 cio_write(cio, image->y0, 4); /* Y0siz */
2121 cio_write(cio, cp->tdx, 4); /* XTsiz */
2122 cio_write(cio, cp->tdy, 4); /* YTsiz */
2123 cio_write(cio, cp->tx0, 4); /* XT0siz */
2124 cio_write(cio, cp->ty0, 4); /* YT0siz */
2125 cio_write(cio, image->numcomps, 2); /* Csiz */
2126 for (i = 0; i < image->numcomps; i++) {
2127 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2128 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2129 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2131 len = cio_tell(cio) - lenp;
2132 cio_seek(cio, lenp);
2133 cio_write(cio, len, 2); /* Lsiz */
2134 cio_seek(cio, lenp + len);
2137 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2141 * Writes the SIZ marker (image and tile size)
2143 * @param p_stream the stream to write data to.
2144 * @param p_j2k J2K codec.
2145 * @param p_manager the user event manager.
2147 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2148 struct opj_stream_private *p_stream,
2149 struct opj_event_mgr * p_manager )
2152 OPJ_UINT32 l_size_len;
2153 OPJ_BYTE * l_current_ptr;
2154 opj_image_t * l_image = 00;
2155 opj_cp_v2_t *cp = 00;
2156 opj_image_comp_t * l_img_comp = 00;
2159 assert(p_stream != 00);
2160 assert(p_j2k != 00);
2161 assert(p_manager != 00);
2163 l_image = p_j2k->m_private_image;
2164 cp = &(p_j2k->m_cp);
2165 l_size_len = 40 + 3 * l_image->numcomps;
2166 l_img_comp = l_image->comps;
2168 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2170 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2171 = (OPJ_BYTE*)opj_realloc(
2172 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2174 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2178 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2181 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2183 /* write SOC identifier */
2184 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2187 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2190 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2193 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2196 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2199 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2202 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2205 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2208 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2211 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2214 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2217 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2220 for (i = 0; i < l_image->numcomps; ++i) {
2221 /* TODO here with MCT ? */
2222 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2225 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2228 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2234 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) {
2244 * Reads a SIZ marker (image and tile size)
2245 * @param p_header_data the data contained in the SIZ box.
2246 * @param jp2 the jpeg2000 file codec.
2247 * @param p_header_size the size of the data contained in the SIZ marker.
2248 * @param p_manager the user event manager.
2250 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2251 OPJ_BYTE * p_header_data,
2252 OPJ_UINT32 p_header_size,
2253 opj_event_mgr_t * p_manager
2256 OPJ_UINT32 l_size, i;
2257 OPJ_UINT32 l_nb_comp;
2258 OPJ_UINT32 l_nb_comp_remain;
2259 OPJ_UINT32 l_remaining_size;
2260 OPJ_UINT32 l_nb_tiles;
2262 opj_image_t *l_image = 00;
2263 opj_cp_v2_t *l_cp = 00;
2264 opj_image_comp_t * l_img_comp = 00;
2265 opj_tcp_v2_t * l_current_tile_param = 00;
2268 assert(p_j2k != 00);
2269 assert(p_manager != 00);
2270 assert(p_header_data != 00);
2272 l_image = p_j2k->m_private_image;
2273 l_cp = &(p_j2k->m_cp);
2275 /* minimum size == 39 - 3 (= minimum component parameter) */
2276 if (p_header_size < 36) {
2277 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2281 l_remaining_size = p_header_size - 36;
2282 l_nb_comp = l_remaining_size / 3;
2283 l_nb_comp_remain = l_remaining_size % 3;
2284 if (l_nb_comp_remain != 0){
2285 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2289 l_size = p_header_size + 2; /* Lsiz */
2291 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2293 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2294 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2296 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2298 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2300 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2302 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2304 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2306 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2308 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2310 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2313 l_image->numcomps = (OPJ_UINT16) l_tmp;
2315 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2319 if (l_image->numcomps != l_nb_comp) {
2320 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);
2325 if (l_cp->correct) {
2326 /* if JPWL is on, we check whether TX errors have damaged
2327 too much the SIZ parameters */
2328 if (!(l_image->x1 * l_image->y1)) {
2329 opj_event_msg_v2(p_manager, EVT_ERROR,
2330 "JPWL: bad image size (%d x %d)\n",
2331 l_image->x1, l_image->y1);
2332 if (!JPWL_ASSUME || JPWL_ASSUME) {
2333 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2338 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2339 if (l_image->numcomps != ((len - 38) / 3)) {
2340 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2341 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2342 l_image->numcomps, ((len - 38) / 3));
2344 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2347 */ /* we try to correct */
2348 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2349 if (l_image->numcomps < ((len - 38) / 3)) {
2350 len = 38 + 3 * l_image->numcomps;
2351 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2354 l_image->numcomps = ((len - 38) / 3);
2355 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2361 /* update components number in the jpwl_exp_comps filed */
2362 l_cp->exp_comps = l_image->numcomps;
2364 #endif /* USE_JPWL */
2366 /* Allocate the resulting image components */
2367 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2368 if (l_image->comps == 00){
2369 l_image->numcomps = 0;
2370 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2374 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2375 l_img_comp = l_image->comps;
2377 /* Read the component information */
2378 for (i = 0; i < l_image->numcomps; ++i){
2380 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2382 l_img_comp->prec = (tmp & 0x7f) + 1;
2383 l_img_comp->sgnd = tmp >> 7;
2384 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2386 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2387 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2389 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2392 if (l_cp->correct) {
2393 /* if JPWL is on, we check whether TX errors have damaged
2394 too much the SIZ parameters, again */
2395 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2396 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2397 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2398 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2400 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2403 /* we try to correct */
2404 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2405 if (!l_image->comps[i].dx) {
2406 l_image->comps[i].dx = 1;
2407 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2408 i, l_image->comps[i].dx);
2410 if (!l_image->comps[i].dy) {
2411 l_image->comps[i].dy = 1;
2412 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2413 i, l_image->comps[i].dy);
2417 #endif /* USE_JPWL */
2418 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2419 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2423 /* Compute the number of tiles */
2424 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2425 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2426 l_nb_tiles = l_cp->tw * l_cp->th;
2428 /* Define the tiles which will be decoded */
2429 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2430 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;
2431 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;
2432 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);
2433 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);
2436 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2437 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2438 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2439 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2443 if (l_cp->correct) {
2444 /* if JPWL is on, we check whether TX errors have damaged
2445 too much the SIZ parameters */
2446 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2447 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2448 "JPWL: bad number of tiles (%d x %d)\n",
2449 l_cp->tw, l_cp->th);
2451 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2454 /* we try to correct */
2455 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2458 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2461 if (l_cp->tw > l_cp->max_tiles) {
2463 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2464 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2465 l_cp->max_tiles, l_cp->tw);
2469 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2472 if (l_cp->th > l_cp->max_tiles) {
2474 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2475 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2476 l_cp->max_tiles, l_cp->th);
2480 #endif /* USE_JPWL */
2482 /* memory allocations */
2483 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2484 if (l_cp->tcps == 00) {
2485 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2488 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2491 if (l_cp->correct) {
2493 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2494 "JPWL: could not alloc tcps field of cp\n");
2495 if (!JPWL_ASSUME || JPWL_ASSUME) {
2496 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2501 #endif /* USE_JPWL */
2503 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2504 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2505 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2506 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2509 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2511 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2512 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2514 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2515 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2518 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));
2519 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2521 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2522 (opj_simple_mcc_decorrelation_data_t*)
2523 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2525 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2526 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2529 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));
2530 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2532 /* set up default dc level shift */
2533 for (i=0;i<l_image->numcomps;++i) {
2534 if (! l_image->comps[i].sgnd) {
2535 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2539 l_current_tile_param = l_cp->tcps;
2540 for (i = 0; i < l_nb_tiles; ++i) {
2541 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2542 if (l_current_tile_param->tccps == 00) {
2543 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2546 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2548 ++l_current_tile_param;
2551 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2552 opj_image_comp_header_update(l_image,l_cp);
2559 static void j2k_write_com(opj_j2k_t *j2k) {
2563 if(j2k->cp->comment) {
2564 opj_cio_t *cio = j2k->cio;
2565 char *comment = j2k->cp->comment;
2567 cio_write(cio, J2K_MS_COM, 2);
2568 lenp = cio_tell(cio);
2570 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2571 for (i = 0; i < strlen(comment); i++) {
2572 cio_write(cio, comment[i], 1);
2574 len = cio_tell(cio) - lenp;
2575 cio_seek(cio, lenp);
2576 cio_write(cio, len, 2);
2577 cio_seek(cio, lenp + len);
2581 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2587 * Writes the COM marker (comment)
2589 * @param p_stream the stream to write data to.
2590 * @param p_j2k J2K codec.
2591 * @param p_manager the user event manager.
2593 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2594 struct opj_stream_private *p_stream,
2595 struct opj_event_mgr * p_manager )
2597 OPJ_UINT32 l_comment_size;
2598 OPJ_UINT32 l_total_com_size;
2599 const OPJ_CHAR *l_comment;
2600 OPJ_BYTE * l_current_ptr = 00;
2603 assert(p_j2k != 00);
2604 assert(p_stream != 00);
2605 assert(p_manager != 00);
2607 l_comment = p_j2k->m_cp.comment;
2608 l_comment_size = strlen(l_comment);
2609 l_total_com_size = l_comment_size + 6;
2611 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2612 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2613 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2616 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2620 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2623 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2625 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2628 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2631 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2634 memcpy( l_current_ptr,l_comment,l_comment_size);
2636 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) {
2643 static void j2k_read_com(opj_j2k_t *j2k) {
2646 opj_cio_t *cio = j2k->cio;
2648 len = cio_read(cio, 2);
2649 cio_skip(cio, len - 2);
2652 * Reads a COM marker (comments)
2653 * @param p_header_data the data contained in the COM box.
2654 * @param jp2 the jpeg2000 file codec.
2655 * @param p_header_size the size of the data contained in the COM marker.
2656 * @param p_manager the user event manager.
2658 opj_bool j2k_read_com_v2 (
2659 opj_j2k_v2_t *p_j2k,
2660 OPJ_BYTE * p_header_data,
2661 OPJ_UINT32 p_header_size,
2662 struct opj_event_mgr * p_manager
2666 assert(p_j2k != 00);
2667 assert(p_manager != 00);
2668 assert(p_header_data != 00);
2669 (void)p_header_size;
2674 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2677 opj_cp_t *cp = j2k->cp;
2678 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2679 opj_tccp_t *tccp = &tcp->tccps[compno];
2680 opj_cio_t *cio = j2k->cio;
2682 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2683 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2684 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2685 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2686 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2688 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2689 for (i = 0; i < tccp->numresolutions; i++) {
2690 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2695 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2698 opj_cp_t *cp = j2k->cp;
2699 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2700 opj_tccp_t *tccp = &tcp->tccps[compno];
2701 opj_cio_t *cio = j2k->cio;
2703 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2705 /* If user wants to remove more resolutions than the codestream contains, return error*/
2706 assert(cp->reduce >= 0);
2707 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2708 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2709 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2710 j2k->state |= J2K_STATE_ERR;
2713 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2714 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2715 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2716 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2717 if (tccp->csty & J2K_CP_CSTY_PRT) {
2718 for (i = 0; i < tccp->numresolutions; i++) {
2719 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2720 tccp->prcw[i] = tmp & 0xf;
2721 tccp->prch[i] = tmp >> 4;
2726 if(j2k->cstr_info && compno == 0) {
2727 for (i = 0; i < tccp->numresolutions; i++) {
2728 if (tccp->csty & J2K_CP_CSTY_PRT) {
2729 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2730 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2733 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2734 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2741 static void j2k_write_cod(opj_j2k_t *j2k) {
2742 opj_cp_t *cp = NULL;
2743 opj_tcp_t *tcp = NULL;
2746 opj_cio_t *cio = j2k->cio;
2748 cio_write(cio, J2K_MS_COD, 2); /* COD */
2750 lenp = cio_tell(cio);
2754 tcp = &cp->tcps[j2k->curtileno];
2756 cio_write(cio, tcp->csty, 1); /* Scod */
2757 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2758 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2759 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2761 j2k_write_cox(j2k, 0);
2762 len = cio_tell(cio) - lenp;
2763 cio_seek(cio, lenp);
2764 cio_write(cio, len, 2); /* Lcod */
2765 cio_seek(cio, lenp + len);
2768 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2773 * Writes the COD marker (Coding style default)
2775 * @param p_stream the stream to write data to.
2776 * @param p_j2k J2K codec.
2777 * @param p_manager the user event manager.
2779 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2780 struct opj_stream_private *p_stream,
2781 struct opj_event_mgr * p_manager )
2783 opj_cp_v2_t *l_cp = 00;
2784 opj_tcp_v2_t *l_tcp = 00;
2785 OPJ_UINT32 l_code_size,l_remaining_size;
2786 OPJ_BYTE * l_current_data = 00;
2789 assert(p_j2k != 00);
2790 assert(p_manager != 00);
2791 assert(p_stream != 00);
2793 l_cp = &(p_j2k->m_cp);
2794 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2795 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2796 l_remaining_size = l_code_size;
2798 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2799 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2800 = (OPJ_BYTE*)opj_realloc(
2801 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2804 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2808 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2811 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2813 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2814 l_current_data += 2;
2816 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2817 l_current_data += 2;
2819 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2822 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2825 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2828 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2831 l_remaining_size -= 9;
2833 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2834 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2838 if (l_remaining_size != 0) {
2839 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2843 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) {
2852 * Reads a COD marker (Coding Styke defaults)
2853 * @param p_header_data the data contained in the COD box.
2854 * @param p_j2k the jpeg2000 codec.
2855 * @param p_header_size the size of the data contained in the COD marker.
2856 * @param p_manager the user event manager.
2858 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2859 OPJ_BYTE * p_header_data,
2860 OPJ_UINT32 p_header_size,
2861 opj_event_mgr_t * p_manager
2867 opj_cp_v2_t *l_cp = 00;
2868 opj_tcp_v2_t *l_tcp = 00;
2869 opj_image_t *l_image = 00;
2872 assert(p_header_data != 00);
2873 assert(p_j2k != 00);
2874 assert(p_manager != 00);
2876 l_image = p_j2k->m_private_image;
2877 l_cp = &(p_j2k->m_cp);
2879 /* If we are in the first tile-part header of the current tile */
2880 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2881 &l_cp->tcps[p_j2k->m_current_tile_number] :
2882 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2884 /* Make sure room is sufficient */
2885 if (p_header_size < 5) {
2886 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2890 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2892 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2894 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2895 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2898 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2899 if (l_cp->m_specific_param.m_dec.m_layer) {
2900 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2903 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2906 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2910 for (i = 0; i < l_image->numcomps; ++i) {
2911 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2914 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2915 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2919 if (p_header_size != 0) {
2920 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2924 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2925 j2k_copy_tile_component_parameters(p_j2k);
2928 #ifdef WIP_REMOVE_MSD
2929 if (p_j2k->cstr_info) {
2930 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2931 p_j2k->cstr_info->prog = l_tcp->prg;
2932 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2933 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2934 for (i = 0; i < l_image->numcomps; ++i) {
2935 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2943 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2946 opj_cp_t *cp = j2k->cp;
2947 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2948 opj_image_t *image = j2k->image;
2949 opj_cio_t *cio = j2k->cio;
2951 cio_write(cio, J2K_MS_COC, 2); /* COC */
2952 lenp = cio_tell(cio);
2954 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2955 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2956 j2k_write_cox(j2k, compno);
2957 len = cio_tell(cio) - lenp;
2958 cio_seek(cio, lenp);
2959 cio_write(cio, len, 2); /* Lcoc */
2960 cio_seek(cio, lenp + len);
2964 * Writes the COC marker (Coding style component)
2966 * @param p_comp_no the index of the component to output.
2967 * @param p_stream the stream to write data to.
2968 * @param p_j2k J2K codec.
2969 * @param p_manager the user event manager.
2971 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2972 OPJ_UINT32 p_comp_no,
2973 struct opj_stream_private *p_stream,
2974 struct opj_event_mgr * p_manager )
2976 OPJ_UINT32 l_coc_size,l_remaining_size;
2977 OPJ_UINT32 l_comp_room;
2980 assert(p_j2k != 00);
2981 assert(p_manager != 00);
2982 assert(p_stream != 00);
2984 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2986 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2988 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2989 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2990 = (OPJ_BYTE*)opj_realloc(
2991 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2993 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2997 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3000 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);
3002 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) {
3010 * Writes the COC marker (Coding style component)
3012 * @param p_comp_no the index of the component to output.
3013 * @param p_stream the stream to write data to.
3014 * @param p_j2k J2K codec.
3015 * @param p_manager the user event manager.
3017 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3018 OPJ_UINT32 p_comp_no,
3020 OPJ_UINT32 * p_data_written,
3021 struct opj_event_mgr * p_manager )
3023 opj_cp_v2_t *l_cp = 00;
3024 opj_tcp_v2_t *l_tcp = 00;
3025 OPJ_UINT32 l_coc_size,l_remaining_size;
3026 OPJ_BYTE * l_current_data = 00;
3027 opj_image_t *l_image = 00;
3028 OPJ_UINT32 l_comp_room;
3031 assert(p_j2k != 00);
3032 assert(p_manager != 00);
3034 l_cp = &(p_j2k->m_cp);
3035 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3036 l_image = p_j2k->m_private_image;
3037 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3039 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3040 l_remaining_size = l_coc_size;
3042 l_current_data = p_data;
3044 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3045 l_current_data += 2;
3047 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3048 l_current_data += 2;
3050 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3051 l_current_data+=l_comp_room;
3053 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3056 l_remaining_size -= (5 + l_comp_room);
3057 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3058 * p_data_written = l_coc_size;
3062 * Gets the maximum size taken by a coc.
3064 * @param p_j2k the jpeg2000 codec to use.
3066 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3069 OPJ_UINT32 l_nb_comp;
3070 OPJ_UINT32 l_nb_tiles;
3071 OPJ_UINT32 l_max = 0;
3075 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3076 l_nb_comp = p_j2k->m_private_image->numcomps;
3078 for (i=0;i<l_nb_tiles;++i) {
3079 for (j=0;j<l_nb_comp;++j) {
3080 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3089 * Reads a COC marker (Coding Style Component)
3090 * @param p_header_data the data contained in the COC box.
3091 * @param p_j2k the jpeg2000 codec.
3092 * @param p_header_size the size of the data contained in the COC marker.
3093 * @param p_manager the user event manager.
3095 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
3096 OPJ_BYTE * p_header_data,
3097 OPJ_UINT32 p_header_size,
3098 opj_event_mgr_t * p_manager
3101 opj_cp_v2_t *l_cp = NULL;
3102 opj_tcp_v2_t *l_tcp = NULL;
3103 opj_image_t *l_image = NULL;
3104 OPJ_UINT32 l_comp_room;
3105 OPJ_UINT32 l_comp_no;
3108 assert(p_header_data != 00);
3109 assert(p_j2k != 00);
3110 assert(p_manager != 00);
3112 l_cp = &(p_j2k->m_cp);
3113 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3114 &l_cp->tcps[p_j2k->m_current_tile_number] :
3115 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3116 l_image = p_j2k->m_private_image;
3118 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3120 /* make sure room is sufficient*/
3121 if (p_header_size < l_comp_room + 1) {
3122 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3125 p_header_size -= l_comp_room + 1;
3127 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3128 p_header_data += l_comp_room;
3129 if (l_comp_no >= l_image->numcomps) {
3130 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3134 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3137 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3138 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3142 if (p_header_size != 0) {
3143 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3149 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3150 int bandno, numbands;
3153 opj_cp_t *cp = j2k->cp;
3154 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3155 opj_tccp_t *tccp = &tcp->tccps[compno];
3156 opj_cio_t *cio = j2k->cio;
3158 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3159 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3161 for (bandno = 0; bandno < numbands; bandno++) {
3162 expn = tccp->stepsizes[bandno].expn;
3163 mant = tccp->stepsizes[bandno].mant;
3165 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3166 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3168 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3173 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3175 int bandno, numbands;
3177 opj_cp_t *cp = j2k->cp;
3178 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3179 opj_tccp_t *tccp = &tcp->tccps[compno];
3180 opj_cio_t *cio = j2k->cio;
3182 tmp = cio_read(cio, 1); /* Sqcx */
3183 tccp->qntsty = tmp & 0x1f;
3184 tccp->numgbits = tmp >> 5;
3185 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3186 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3189 if (j2k->cp->correct) {
3191 /* if JPWL is on, we check whether there are too many subbands */
3192 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3193 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3194 "JPWL: bad number of subbands in Sqcx (%d)\n",
3197 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3200 /* we try to correct */
3202 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3203 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3210 /* We check whether there are too many subbands */
3211 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3212 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3213 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3214 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3217 #endif /* USE_JPWL */
3219 for (bandno = 0; bandno < numbands; bandno++) {
3221 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3222 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3225 tmp = cio_read(cio, 2); /* SPqcx_i */
3229 if (bandno < J2K_MAXBANDS){
3230 tccp->stepsizes[bandno].expn = expn;
3231 tccp->stepsizes[bandno].mant = mant;
3235 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3236 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3237 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3238 tccp->stepsizes[bandno].expn =
3239 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3240 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3241 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3247 static void j2k_write_qcd(opj_j2k_t *j2k) {
3250 opj_cio_t *cio = j2k->cio;
3252 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3253 lenp = cio_tell(cio);
3255 j2k_write_qcx(j2k, 0);
3256 len = cio_tell(cio) - lenp;
3257 cio_seek(cio, lenp);
3258 cio_write(cio, len, 2); /* Lqcd */
3259 cio_seek(cio, lenp + len);
3262 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3266 * Writes the QCD marker (quantization default)
3268 * @param p_comp_number the index of the component to output.
3269 * @param p_stream the stream to write data to.
3270 * @param p_j2k J2K codec.
3271 * @param p_manager the user event manager.
3273 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3274 struct opj_stream_private *p_stream,
3275 struct opj_event_mgr * p_manager )
3277 opj_cp_v2_t *l_cp = 00;
3278 opj_tcp_v2_t *l_tcp = 00;
3279 OPJ_UINT32 l_qcd_size,l_remaining_size;
3280 OPJ_BYTE * l_current_data = 00;
3283 assert(p_j2k != 00);
3284 assert(p_manager != 00);
3285 assert(p_stream != 00);
3287 l_cp = &(p_j2k->m_cp);
3288 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3289 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3290 l_remaining_size = l_qcd_size;
3292 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3293 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3294 = (OPJ_BYTE*)opj_realloc(
3295 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3298 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3302 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3305 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3307 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3308 l_current_data += 2;
3310 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3311 l_current_data += 2;
3313 l_remaining_size -= 4;
3315 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3316 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3320 if (l_remaining_size != 0) {
3321 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3325 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) {
3333 * Reads a QCD marker (Quantization defaults)
3334 * @param p_header_data the data contained in the QCD box.
3335 * @param p_j2k the jpeg2000 codec.
3336 * @param p_header_size the size of the data contained in the QCD marker.
3337 * @param p_manager the user event manager.
3339 opj_bool j2k_read_qcd_v2 (
3340 opj_j2k_v2_t *p_j2k,
3341 OPJ_BYTE * p_header_data,
3342 OPJ_UINT32 p_header_size,
3343 struct opj_event_mgr * p_manager
3347 assert(p_header_data != 00);
3348 assert(p_j2k != 00);
3349 assert(p_manager != 00);
3351 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3352 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3356 if (p_header_size != 0) {
3357 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3361 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3362 j2k_copy_tile_quantization_parameters(p_j2k);
3367 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3370 opj_cio_t *cio = j2k->cio;
3372 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3373 lenp = cio_tell(cio);
3375 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3376 j2k_write_qcx(j2k, compno);
3377 len = cio_tell(cio) - lenp;
3378 cio_seek(cio, lenp);
3379 cio_write(cio, len, 2); /* Lqcc */
3380 cio_seek(cio, lenp + len);
3384 * Writes the QCC marker (quantization component)
3386 * @param p_comp_no the index of the component to output.
3387 * @param p_stream the stream to write data to.
3388 * @param p_j2k J2K codec.
3389 * @param p_manager the user event manager.
3391 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3392 OPJ_UINT32 p_comp_no,
3393 struct opj_stream_private *p_stream,
3394 struct opj_event_mgr * p_manager )
3396 OPJ_UINT32 l_qcc_size,l_remaining_size;
3399 assert(p_j2k != 00);
3400 assert(p_manager != 00);
3401 assert(p_stream != 00);
3403 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3404 l_remaining_size = l_qcc_size;
3406 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3407 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3408 = (OPJ_BYTE*)opj_realloc(
3409 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3411 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3415 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3418 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);
3420 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) {
3428 * Writes the QCC marker (quantization component)
3430 * @param p_comp_no the index of the component to output.
3431 * @param p_stream the stream to write data to.
3432 * @param p_j2k J2K codec.
3433 * @param p_manager the user event manager.
3435 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3436 OPJ_UINT32 p_comp_no,
3438 OPJ_UINT32 * p_data_written,
3439 struct opj_event_mgr * p_manager )
3441 OPJ_UINT32 l_qcc_size,l_remaining_size;
3442 OPJ_BYTE * l_current_data = 00;
3445 assert(p_j2k != 00);
3446 assert(p_manager != 00);
3448 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3449 l_remaining_size = l_qcc_size;
3451 l_current_data = p_data;
3453 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3454 l_current_data += 2;
3456 if (p_j2k->m_private_image->numcomps <= 256) {
3459 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3460 l_current_data += 2;
3462 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3465 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3466 l_remaining_size -= 6;
3469 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3470 l_current_data += 2;
3472 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3475 l_remaining_size -= 6;
3478 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3480 *p_data_written = l_qcc_size;
3484 * Gets the maximum size taken by a qcc.
3486 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3488 return j2k_get_max_coc_size(p_j2k);
3491 static void j2k_read_qcc(opj_j2k_t *j2k) {
3493 int numcomp = j2k->image->numcomps;
3494 opj_cio_t *cio = j2k->cio;
3496 len = cio_read(cio, 2); /* Lqcc */
3497 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3500 if (j2k->cp->correct) {
3502 static int backup_compno = 0;
3504 /* compno is negative or larger than the number of components!!! */
3505 if ((compno < 0) || (compno >= numcomp)) {
3506 opj_event_msg(j2k->cinfo, EVT_ERROR,
3507 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3510 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3513 /* we try to correct */
3514 compno = backup_compno % numcomp;
3515 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3516 "- setting component number to %d\n",
3520 /* keep your private count of tiles */
3523 #endif /* USE_JPWL */
3525 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3529 * Reads a QCC marker (Quantization component)
3530 * @param p_header_data the data contained in the QCC box.
3531 * @param p_j2k the jpeg2000 codec.
3532 * @param p_header_size the size of the data contained in the QCC marker.
3533 * @param p_manager the user event manager.
3535 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3536 OPJ_BYTE * p_header_data,
3537 OPJ_UINT32 p_header_size,
3538 struct opj_event_mgr * p_manager)
3540 OPJ_UINT32 l_num_comp,l_comp_no;
3543 assert(p_header_data != 00);
3544 assert(p_j2k != 00);
3545 assert(p_manager != 00);
3547 l_num_comp = p_j2k->m_private_image->numcomps;
3549 if (l_num_comp <= 256) {
3550 if (p_header_size < 1) {
3551 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3554 opj_read_bytes(p_header_data,&l_comp_no,1);
3559 if (p_header_size < 2) {
3560 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3563 opj_read_bytes(p_header_data,&l_comp_no,2);
3569 if (p_j2k->m_cp.correct) {
3571 static OPJ_UINT32 backup_compno = 0;
3573 /* compno is negative or larger than the number of components!!! */
3574 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3575 opj_event_msg_v2(p_manager, EVT_ERROR,
3576 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3577 l_comp_no, l_num_comp);
3579 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3582 /* we try to correct */
3583 l_comp_no = backup_compno % l_num_comp;
3584 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3585 "- setting component number to %d\n",
3589 /* keep your private count of tiles */
3592 #endif /* USE_JPWL */
3594 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3595 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3599 if (p_header_size != 0) {
3600 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3608 static void j2k_write_poc(opj_j2k_t *j2k) {
3609 int len, numpchgs, i;
3611 int numcomps = j2k->image->numcomps;
3613 opj_cp_t *cp = j2k->cp;
3614 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3615 opj_tccp_t *tccp = &tcp->tccps[0];
3616 opj_cio_t *cio = j2k->cio;
3618 numpchgs = 1 + tcp->numpocs;
3619 cio_write(cio, J2K_MS_POC, 2); /* POC */
3620 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3621 cio_write(cio, len, 2); /* Lpoc */
3622 for (i = 0; i < numpchgs; i++) {
3623 opj_poc_t *poc = &tcp->pocs[i];
3624 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3625 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3626 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3627 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3628 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3629 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3630 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3631 poc->compno1 = int_min(poc->compno1, numcomps);
3632 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3637 * Writes the POC marker (Progression Order Change)
3639 * @param p_stream the stream to write data to.
3640 * @param p_j2k J2K codec.
3641 * @param p_manager the user event manager.
3643 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3644 struct opj_stream_private *p_stream,
3645 struct opj_event_mgr * p_manager )
3647 OPJ_UINT32 l_nb_comp;
3648 OPJ_UINT32 l_nb_poc;
3649 OPJ_UINT32 l_poc_size;
3650 OPJ_UINT32 l_written_size = 0;
3651 opj_tcp_v2_t *l_tcp = 00;
3652 opj_tccp_t *l_tccp = 00;
3653 OPJ_UINT32 l_poc_room;
3656 assert(p_j2k != 00);
3657 assert(p_manager != 00);
3658 assert(p_stream != 00);
3660 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3661 l_tccp = &l_tcp->tccps[0];
3662 l_nb_comp = p_j2k->m_private_image->numcomps;
3663 l_nb_poc = 1 + l_tcp->numpocs;
3665 if (l_nb_comp <= 256) {
3671 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3673 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3674 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3675 = (OPJ_BYTE*)opj_realloc(
3676 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3679 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3683 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3686 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3688 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) {
3697 * Writes the POC marker (Progression Order Change)
3699 * @param p_stream the stream to write data to.
3700 * @param p_j2k J2K codec.
3701 * @param p_manager the user event manager.
3703 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3705 OPJ_UINT32 * p_data_written,
3706 struct opj_event_mgr * p_manager )
3709 OPJ_BYTE * l_current_data = 00;
3710 OPJ_UINT32 l_nb_comp;
3711 OPJ_UINT32 l_nb_poc;
3712 OPJ_UINT32 l_poc_size;
3713 opj_image_t *l_image = 00;
3714 opj_tcp_v2_t *l_tcp = 00;
3715 opj_tccp_t *l_tccp = 00;
3716 opj_poc_t *l_current_poc = 00;
3717 OPJ_UINT32 l_poc_room;
3720 assert(p_j2k != 00);
3721 assert(p_manager != 00);
3723 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3724 l_tccp = &l_tcp->tccps[0];
3725 l_image = p_j2k->m_private_image;
3726 l_nb_comp = l_image->numcomps;
3727 l_nb_poc = 1 + l_tcp->numpocs;
3729 if (l_nb_comp <= 256) {
3736 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3738 l_current_data = p_data;
3740 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3741 l_current_data += 2;
3743 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3744 l_current_data += 2;
3746 l_current_poc = l_tcp->pocs;
3747 for (i = 0; i < l_nb_poc; ++i) {
3748 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3751 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3752 l_current_data+=l_poc_room;
3754 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3757 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3760 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3761 l_current_data+=l_poc_room;
3763 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3766 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3767 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3768 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3769 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3774 *p_data_written = l_poc_size;
3778 * Gets the maximum size taken by the writing of a POC.
3780 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3782 opj_tcp_v2_t * l_tcp = 00;
3783 OPJ_UINT32 l_nb_tiles = 0;
3784 OPJ_UINT32 l_max_poc = 0;
3787 l_tcp = p_j2k->m_cp.tcps;
3788 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3790 for (i=0;i<l_nb_tiles;++i) {
3791 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3797 return 4 + 9 * l_max_poc;
3801 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3803 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3806 OPJ_UINT32 l_nb_tiles;
3807 OPJ_UINT32 l_max = 0;
3808 opj_tcp_v2_t * l_tcp = 00;
3810 l_tcp = p_j2k->m_cp.tcps;
3811 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3813 for (i=0;i<l_nb_tiles;++i) {
3814 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3824 * Gets the maximum size taken by the headers of the SOT.
3826 * @param p_j2k the jpeg2000 codec to use.
3828 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3830 OPJ_UINT32 l_nb_bytes = 0;
3831 OPJ_UINT32 l_nb_comps;
3832 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3834 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3835 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3837 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3838 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3839 l_nb_bytes += l_nb_comps * l_coc_bytes;
3841 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3842 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3845 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3847 /*** DEVELOPER CORNER, Add room for your headers ***/
3852 static void j2k_read_poc(opj_j2k_t *j2k) {
3853 int len, numpchgs, i, old_poc;
3855 int numcomps = j2k->image->numcomps;
3857 opj_cp_t *cp = j2k->cp;
3858 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3859 opj_cio_t *cio = j2k->cio;
3861 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
3863 len = cio_read(cio, 2); /* Lpoc */
3864 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
3866 for (i = old_poc; i < numpchgs + old_poc; i++) {
3868 poc = &tcp->pocs[i];
3869 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
3870 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
3871 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
3872 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
3873 poc->compno1 = int_min(
3874 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
3875 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
3878 tcp->numpocs = numpchgs + old_poc - 1;
3882 * Reads a POC marker (Progression Order Change)
3884 * @param p_header_data the data contained in the POC box.
3885 * @param p_j2k the jpeg2000 codec.
3886 * @param p_header_size the size of the data contained in the POC marker.
3887 * @param p_manager the user event manager.
3889 opj_bool j2k_read_poc_v2 (
3890 opj_j2k_v2_t *p_j2k,
3891 OPJ_BYTE * p_header_data,
3892 OPJ_UINT32 p_header_size,
3893 struct opj_event_mgr * p_manager)
3895 OPJ_UINT32 i, l_nb_comp, l_tmp;
3896 opj_image_t * l_image = 00;
3897 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3898 OPJ_UINT32 l_chunk_size, l_comp_room;
3900 opj_cp_v2_t *l_cp = 00;
3901 opj_tcp_v2_t *l_tcp = 00;
3902 opj_poc_t *l_current_poc = 00;
3905 assert(p_header_data != 00);
3906 assert(p_j2k != 00);
3907 assert(p_manager != 00);
3909 l_image = p_j2k->m_private_image;
3910 l_nb_comp = l_image->numcomps;
3911 if (l_nb_comp <= 256) {
3917 l_chunk_size = 5 + 2 * l_comp_room;
3918 l_current_poc_nb = p_header_size / l_chunk_size;
3919 l_current_poc_remaining = p_header_size % l_chunk_size;
3921 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3922 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3926 l_cp = &(p_j2k->m_cp);
3927 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3928 &l_cp->tcps[p_j2k->m_current_tile_number] :
3929 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3930 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3931 l_current_poc_nb += l_old_poc_nb;
3933 assert(l_current_poc_nb < 32);
3935 /* now poc is in use.*/
3938 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3939 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3940 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3942 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3943 p_header_data+=l_comp_room;
3944 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3946 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3948 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3949 p_header_data+=l_comp_room;
3950 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3952 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3953 /* make sure comp is in acceptable bounds */
3954 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3958 l_tcp->numpocs = l_current_poc_nb - 1;
3962 static void j2k_read_crg(opj_j2k_t *j2k) {
3963 int len, i, Xcrg_i, Ycrg_i;
3965 opj_cio_t *cio = j2k->cio;
3966 int numcomps = j2k->image->numcomps;
3968 len = cio_read(cio, 2); /* Lcrg */
3969 for (i = 0; i < numcomps; i++) {
3970 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
3971 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
3976 * Reads a CRG marker (Component registration)
3978 * @param p_header_data the data contained in the TLM box.
3979 * @param p_j2k the jpeg2000 codec.
3980 * @param p_header_size the size of the data contained in the TLM marker.
3981 * @param p_manager the user event manager.
3983 opj_bool j2k_read_crg_v2 (
3984 opj_j2k_v2_t *p_j2k,
3985 OPJ_BYTE * p_header_data,
3986 OPJ_UINT32 p_header_size,
3987 struct opj_event_mgr * p_manager
3990 OPJ_UINT32 l_nb_comp;
3992 assert(p_header_data != 00);
3993 assert(p_j2k != 00);
3994 assert(p_manager != 00);
3996 l_nb_comp = p_j2k->m_private_image->numcomps;
3998 if (p_header_size != l_nb_comp *4) {
3999 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4002 /* Do not care of this at the moment since only local variables are set here */
4005 (i = 0; i < l_nb_comp; ++i)
4007 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4009 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4016 static void j2k_read_tlm(opj_j2k_t *j2k) {
4017 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4018 long int Ttlm_i, Ptlm_i;
4020 opj_cio_t *cio = j2k->cio;
4022 len = cio_read(cio, 2); /* Ltlm */
4023 Ztlm = cio_read(cio, 1); /* Ztlm */
4024 Stlm = cio_read(cio, 1); /* Stlm */
4025 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4026 SP = (Stlm >> 6) & 0x01;
4027 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4028 for (i = 0; i < tile_tlm; i++) {
4029 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4030 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4035 * Reads a TLM marker (Tile Length Marker)
4037 * @param p_header_data the data contained in the TLM box.
4038 * @param p_j2k the jpeg2000 codec.
4039 * @param p_header_size the size of the data contained in the TLM marker.
4040 * @param p_manager the user event manager.
4042 opj_bool j2k_read_tlm_v2 (
4043 opj_j2k_v2_t *p_j2k,
4044 OPJ_BYTE * p_header_data,
4045 OPJ_UINT32 p_header_size,
4046 struct opj_event_mgr * p_manager
4049 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4051 assert(p_header_data != 00);
4052 assert(p_j2k != 00);
4053 assert(p_manager != 00);
4055 if (p_header_size < 2) {
4056 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4061 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4063 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4066 l_ST = ((l_Stlm >> 4) & 0x3);
4067 l_SP = (l_Stlm >> 6) & 0x1;
4069 l_Ptlm_size = (l_SP + 1) * 2;
4070 l_quotient = l_Ptlm_size + l_ST;
4072 l_tot_num_tp = p_header_size / l_quotient;
4073 l_tot_num_tp_remaining = p_header_size % l_quotient;
4075 if (l_tot_num_tp_remaining != 0) {
4076 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4079 /* FIXME Do not care of this at the moment since only local variables are set here */
4082 (i = 0; i < l_tot_num_tp; ++i)
4084 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4085 p_header_data += l_ST;
4086 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4087 p_header_data += l_Ptlm_size;
4092 static void j2k_read_plm(opj_j2k_t *j2k) {
4093 int len, i, Zplm, Nplm, add, packet_len = 0;
4095 opj_cio_t *cio = j2k->cio;
4097 len = cio_read(cio, 2); /* Lplm */
4098 Zplm = cio_read(cio, 1); /* Zplm */
4101 Nplm = cio_read(cio, 4); /* Nplm */
4103 for (i = Nplm; i > 0; i--) {
4104 add = cio_read(cio, 1);
4106 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4107 if ((add & 0x80) == 0) {
4118 * Reads a PLM marker (Packet length, main header marker)
4120 * @param p_header_data the data contained in the TLM box.
4121 * @param p_j2k the jpeg2000 codec.
4122 * @param p_header_size the size of the data contained in the TLM marker.
4123 * @param p_manager the user event manager.
4125 opj_bool j2k_read_plm_v2 (
4126 opj_j2k_v2_t *p_j2k,
4127 OPJ_BYTE * p_header_data,
4128 OPJ_UINT32 p_header_size,
4129 struct opj_event_mgr * p_manager
4133 assert(p_header_data != 00);
4134 assert(p_j2k != 00);
4135 assert(p_manager != 00);
4137 if (p_header_size < 1) {
4138 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4141 /* Do not care of this at the moment since only local variables are set here */
4143 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4150 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4152 p_header_size -= (1+l_Nplm);
4156 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4160 (i = 0; i < l_Nplm; ++i)
4162 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4164 // take only the last seven bytes
4165 l_packet_len |= (l_tmp & 0x7f);
4173 // store packet length and proceed to next packet
4180 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4188 static void j2k_read_plt(opj_j2k_t *j2k) {
4189 int len, i, Zplt, packet_len = 0, add;
4191 opj_cio_t *cio = j2k->cio;
4193 len = cio_read(cio, 2); /* Lplt */
4194 Zplt = cio_read(cio, 1); /* Zplt */
4195 for (i = len - 3; i > 0; i--) {
4196 add = cio_read(cio, 1);
4197 packet_len = (packet_len << 7) + add; /* Iplt_i */
4198 if ((add & 0x80) == 0) {
4206 * Reads a PLT marker (Packet length, tile-part header)
4208 * @param p_header_data the data contained in the PLT box.
4209 * @param p_j2k the jpeg2000 codec.
4210 * @param p_header_size the size of the data contained in the PLT marker.
4211 * @param p_manager the user event manager.
4213 opj_bool j2k_read_plt_v2 (
4214 opj_j2k_v2_t *p_j2k,
4215 OPJ_BYTE * p_header_data,
4216 OPJ_UINT32 p_header_size,
4217 struct opj_event_mgr * p_manager
4220 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4223 assert(p_header_data != 00);
4224 assert(p_j2k != 00);
4225 assert(p_manager != 00);
4227 if (p_header_size < 1) {
4228 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4232 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4236 for (i = 0; i < p_header_size; ++i) {
4237 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4239 /* take only the last seven bytes */
4240 l_packet_len |= (l_tmp & 0x7f);
4245 /* store packet length and proceed to next packet */
4250 if (l_packet_len != 0) {
4251 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4258 static void j2k_read_ppm(opj_j2k_t *j2k) {
4259 int len, Z_ppm, i, j;
4262 opj_cp_t *cp = j2k->cp;
4263 opj_cio_t *cio = j2k->cio;
4265 len = cio_read(cio, 2);
4268 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4271 if (cp->ppm_previous == 0) {
4272 N_ppm = cio_read(cio, 4); /* N_ppm */
4275 N_ppm = cp->ppm_previous;
4278 if (Z_ppm == 0) { /* First PPM marker */
4279 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4280 cp->ppm_data_first = cp->ppm_data;
4281 cp->ppm_len = N_ppm;
4282 } else { /* NON-first PPM marker */
4283 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4286 /* this memory allocation check could be done even in non-JPWL cases */
4288 if (!cp->ppm_data) {
4289 opj_event_msg(j2k->cinfo, EVT_ERROR,
4290 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4292 if (!JPWL_ASSUME || JPWL_ASSUME) {
4293 opj_free(cp->ppm_data);
4294 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4301 cp->ppm_data_first = cp->ppm_data;
4302 cp->ppm_len = N_ppm + cp->ppm_store;
4304 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4305 cp->ppm_data[j] = cio_read(cio, 1);
4309 break; /* Case of non-finished packet header in present marker but finished in next one */
4311 cp->ppm_previous = i - 1;
4316 * Reads a PPM marker (Packed packet headers, main header)
4318 * @param p_header_data the data contained in the POC box.
4319 * @param p_j2k the jpeg2000 codec.
4320 * @param p_header_size the size of the data contained in the POC marker.
4321 * @param p_manager the user event manager.
4324 opj_bool j2k_read_ppm_v2 (
4325 opj_j2k_v2_t *p_j2k,
4326 OPJ_BYTE * p_header_data,
4327 OPJ_UINT32 p_header_size,
4328 struct opj_event_mgr * p_manager
4332 opj_cp_v2_t *l_cp = 00;
4333 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4336 assert(p_header_data != 00);
4337 assert(p_j2k != 00);
4338 assert(p_manager != 00);
4340 if (p_header_size < 1) {
4341 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4345 l_cp = &(p_j2k->m_cp);
4348 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4352 /* First PPM marker */
4354 if (p_header_size < 4) {
4355 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4359 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4363 /* First PPM marker: Initialization */
4364 l_cp->ppm_len = l_N_ppm;
4365 l_cp->ppm_data_size = 0;
4367 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4368 if (l_cp->ppm_buffer == 00) {
4369 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4372 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4374 l_cp->ppm_data = l_cp->ppm_buffer;
4378 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4379 if (p_header_size >= 4) {
4381 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4384 l_cp->ppm_len += l_N_ppm ;
4386 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4387 if (l_cp->ppm_buffer == 00) {
4388 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4391 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4393 l_cp->ppm_data = l_cp->ppm_buffer;
4400 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4402 if (l_remaining_data <= p_header_size) {
4403 /* we must store less information than available in the packet */
4404 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4405 l_cp->ppm_data_size = l_cp->ppm_len;
4406 p_header_size -= l_remaining_data;
4407 p_header_data += l_remaining_data;
4410 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4411 l_cp->ppm_data_size += p_header_size;
4412 p_header_data += p_header_size;
4425 * Reads a PPM marker (Packed packet headers, main header)
4427 * @param p_header_data the data contained in the POC box.
4428 * @param p_j2k the jpeg2000 codec.
4429 * @param p_header_size the size of the data contained in the POC marker.
4430 * @param p_manager the user event manager.
4432 opj_bool j2k_read_ppm_v3 (
4433 opj_j2k_v2_t *p_j2k,
4434 OPJ_BYTE * p_header_data,
4435 OPJ_UINT32 p_header_size,
4436 struct opj_event_mgr * p_manager
4439 opj_cp_v2_t *l_cp = 00;
4440 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4443 assert(p_header_data != 00);
4444 assert(p_j2k != 00);
4445 assert(p_manager != 00);
4447 /* Minimum size of PPM marker is equal to the size of Zppm element */
4448 if (p_header_size < 1) {
4449 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4453 l_cp = &(p_j2k->m_cp);
4456 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4460 /* First PPM marker */
4462 /* We need now at least the Nppm^0 element */
4463 if (p_header_size < 4) {
4464 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4468 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4472 /* First PPM marker: Initialization */
4473 l_cp->ppm_len = l_N_ppm;
4474 l_cp->ppm_data_read = 0;
4476 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4477 if (l_cp->ppm_data == 00) {
4478 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4481 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4483 l_cp->ppm_data_current = l_cp->ppm_data;
4485 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4488 if (p_header_size < 4) {
4489 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4493 /* Uncompleted Ippm series in the previous PPM marker?*/
4494 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4495 /* Get the place where add the remaining Ippm series*/
4496 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4497 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4500 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4504 /* Increase the size of ppm_data to add the new Ippm series*/
4505 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4507 /* Keep the position of the place where concatenate the new series*/
4508 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4509 l_cp->ppm_len += l_N_ppm;
4514 l_remaining_data = p_header_size;
4516 while (l_remaining_data >= l_N_ppm) {
4517 /* read a complete Ippm series*/
4518 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4519 p_header_size -= l_N_ppm;
4520 p_header_data += l_N_ppm;
4522 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4526 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4531 l_remaining_data = p_header_size;
4535 l_remaining_data = p_header_size;
4537 /* Next Ippm series is a complete series ?*/
4538 if (l_remaining_data > l_N_ppm) {
4539 /* Increase the size of ppm_data to add the new Ippm series*/
4540 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4542 /* Keep the position of the place where concatenate the new series */
4543 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4544 l_cp->ppm_len += l_N_ppm;
4549 /* Need to read an incomplete Ippm series*/
4550 if (l_remaining_data) {
4551 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4553 /* Keep the position of the place where concatenate the new series*/
4554 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4555 l_cp->ppm_len += l_N_ppm;
4557 /* Read incomplete Ippm series*/
4558 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4559 p_header_size -= l_remaining_data;
4560 p_header_data += l_remaining_data;
4562 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4567 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4568 if (p_header_size >= 4) {
4570 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4573 l_cp->ppm_len += l_N_ppm ;
4575 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4576 if (l_cp->ppm_buffer == 00) {
4577 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4580 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4582 l_cp->ppm_data = l_cp->ppm_buffer;
4589 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4591 if (l_remaining_data <= p_header_size) {
4592 /* we must store less information than available in the packet */
4593 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4594 l_cp->ppm_data_size = l_cp->ppm_len;
4595 p_header_size -= l_remaining_data;
4596 p_header_data += l_remaining_data;
4599 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4600 l_cp->ppm_data_size += p_header_size;
4601 p_header_data += p_header_size;
4610 static void j2k_read_ppt(opj_j2k_t *j2k) {
4611 int len, Z_ppt, i, j = 0;
4613 opj_cp_t *cp = j2k->cp;
4614 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4615 opj_cio_t *cio = j2k->cio;
4617 len = cio_read(cio, 2);
4618 Z_ppt = cio_read(cio, 1);
4620 if (Z_ppt == 0) { /* First PPT marker */
4621 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4622 tcp->ppt_data_first = tcp->ppt_data;
4624 tcp->ppt_len = len - 3;
4625 } else { /* NON-first PPT marker */
4626 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4627 tcp->ppt_data_first = tcp->ppt_data;
4628 tcp->ppt_len = len - 3 + tcp->ppt_store;
4631 for (i = len - 3; i > 0; i--) {
4632 tcp->ppt_data[j] = cio_read(cio, 1);
4639 * Reads a PPT marker (Packed packet headers, tile-part header)
4641 * @param p_header_data the data contained in the PPT box.
4642 * @param p_j2k the jpeg2000 codec.
4643 * @param p_header_size the size of the data contained in the PPT marker.
4644 * @param p_manager the user event manager.
4646 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4647 OPJ_BYTE * p_header_data,
4648 OPJ_UINT32 p_header_size,
4649 struct opj_event_mgr * p_manager )
4651 opj_cp_v2_t *l_cp = 00;
4652 opj_tcp_v2_t *l_tcp = 00;
4656 assert(p_header_data != 00);
4657 assert(p_j2k != 00);
4658 assert(p_manager != 00);
4660 /* We need to have the Z_ppt element at minimum */
4661 if (p_header_size < 1) {
4662 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4666 l_cp = &(p_j2k->m_cp);
4668 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");
4672 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4675 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4679 /* Allocate buffer to read the packet header */
4681 /* First PPT marker */
4682 l_tcp->ppt_data_size = 0;
4683 l_tcp->ppt_len = p_header_size;
4685 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4686 if (l_tcp->ppt_buffer == 00) {
4687 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4690 l_tcp->ppt_data = l_tcp->ppt_buffer;
4692 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4695 l_tcp->ppt_len += p_header_size;
4697 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4698 if (l_tcp->ppt_buffer == 00) {
4699 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4702 l_tcp->ppt_data = l_tcp->ppt_buffer;
4704 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4707 /* Read packet header from buffer */
4708 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4710 l_tcp->ppt_data_size += p_header_size;
4716 * Writes the TLM marker (Tile Length Marker)
4718 * @param p_stream the stream to write data to.
4719 * @param p_j2k J2K codec.
4720 * @param p_manager the user event manager.
4722 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4723 struct opj_stream_private *p_stream,
4724 struct opj_event_mgr * p_manager )
4726 OPJ_BYTE * l_current_data = 00;
4727 OPJ_UINT32 l_tlm_size;
4730 assert(p_j2k != 00);
4731 assert(p_manager != 00);
4732 assert(p_stream != 00);
4734 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4736 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4737 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4738 = (OPJ_BYTE*)opj_realloc(
4739 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4742 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4746 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4749 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4751 /* change the way data is written to avoid seeking if possible */
4753 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4755 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4756 l_current_data += 2;
4758 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4759 l_current_data += 2;
4761 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4764 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4767 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4768 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) {
4775 static void j2k_write_tlm(opj_j2k_t *j2k){
4777 opj_cio_t *cio = j2k->cio;
4778 j2k->tlm_start = cio_tell(cio);
4779 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4780 lenp = 4 + (5*j2k->totnum_tp);
4781 cio_write(cio,lenp,2); /* Ltlm */
4782 cio_write(cio, 0,1); /* Ztlm=0*/
4783 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4784 cio_skip(cio,5*j2k->totnum_tp);
4787 static void j2k_write_sot(opj_j2k_t *j2k) {
4790 opj_cio_t *cio = j2k->cio;
4792 j2k->sot_start = cio_tell(cio);
4793 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4794 lenp = cio_tell(cio);
4795 cio_skip(cio, 2); /* Lsot (further) */
4796 cio_write(cio, j2k->curtileno, 2); /* Isot */
4797 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4798 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4799 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4800 len = cio_tell(cio) - lenp;
4801 cio_seek(cio, lenp);
4802 cio_write(cio, len, 2); /* Lsot */
4803 cio_seek(cio, lenp + len);
4807 /* update markers struct */
4808 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4809 #endif /* USE_JPWL */
4812 if( j2k->cstr_info && j2k->cur_tp_num==0){
4813 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4818 * Writes the SOT marker (Start of tile-part)
4820 * @param p_stream the stream to write data to.
4821 * @param p_j2k J2K codec.
4822 * @param p_manager the user event manager.
4824 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4826 OPJ_UINT32 * p_data_written,
4827 const struct opj_stream_private *p_stream,
4828 struct opj_event_mgr * p_manager )
4831 assert(p_j2k != 00);
4832 assert(p_manager != 00);
4833 assert(p_stream != 00);
4835 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4838 opj_write_bytes(p_data,10,2); /* Lsot */
4841 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4847 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4850 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4855 /* update markers struct */
4857 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4859 assert( 0 && "TODO" );
4860 #endif /* USE_JPWL */
4862 * p_data_written = 12;
4871 * Reads a PPT marker (Packed packet headers, tile-part header)
4873 * @param p_header_data the data contained in the PPT box.
4874 * @param p_j2k the jpeg2000 codec.
4875 * @param p_header_size the size of the data contained in the PPT marker.
4876 * @param p_manager the user event manager.
4878 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4879 OPJ_BYTE * p_header_data,
4880 OPJ_UINT32 p_header_size,
4881 opj_event_mgr_t * p_manager )
4883 opj_cp_v2_t *l_cp = 00;
4884 opj_tcp_v2_t *l_tcp = 00;
4885 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4886 OPJ_UINT32 l_current_part;
4887 OPJ_UINT32 l_tile_x,l_tile_y;
4890 assert(p_header_data != 00);
4891 assert(p_j2k != 00);
4892 assert(p_manager != 00);
4894 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4895 if (p_header_size != 8) {
4896 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4900 l_cp = &(p_j2k->m_cp);
4901 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4904 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4905 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4906 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4909 if (l_cp->correct) {
4911 int tileno = p_j2k->m_current_tile_number;
4912 static int backup_tileno = 0;
4914 /* tileno is negative or larger than the number of tiles!!! */
4915 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4916 opj_event_msg_v2(p_manager, EVT_ERROR,
4917 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4918 tileno, (l_cp->tw * l_cp->th));
4920 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4923 /* we try to correct */
4924 tileno = backup_tileno;
4925 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4926 "- setting tile number to %d\n",
4930 /* keep your private count of tiles */
4933 #endif /* USE_JPWL */
4935 /* look for the tile in the list of already processed tile (in parts). */
4936 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4937 /* since the time taken by this function can only grow at the time */
4939 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4942 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4943 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4945 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4951 if (l_cp->correct) {
4953 /* totlen is negative or larger than the bytes left!!! */
4954 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))) { */
4955 opj_event_msg_v2(p_manager, EVT_ERROR,
4956 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4957 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4959 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4962 /* we try to correct */
4964 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4965 "- setting Psot to %d => assuming it is the last tile\n",
4969 #endif /* USE_JPWL */
4971 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4973 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4974 "we assuming it is the last tile-part of the codestream.\n");
4975 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4978 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4981 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4984 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4985 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4986 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4987 if (l_tcp->m_nb_tile_parts) {
4988 if (l_current_part >= l_tcp->m_nb_tile_parts){
4989 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4990 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4991 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4995 l_tcp->m_nb_tile_parts = l_num_parts;
4998 /* If know the number of tile part header we will check if we didn't read the last*/
4999 if (l_tcp->m_nb_tile_parts) {
5000 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5001 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5005 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5006 /* Keep the size of data to skip after this marker */
5007 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5010 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5011 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5014 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5016 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5017 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5018 p_j2k->m_specific_param.m_decoder.m_skip_data =
5019 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5020 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5021 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5022 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5025 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5026 p_j2k->m_specific_param.m_decoder.m_skip_data =
5027 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5031 if (p_j2k->cstr_index)
5033 assert(p_j2k->cstr_index->tile_index != 00);
5034 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5035 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5037 if (l_num_parts != 0){
5038 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5039 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5042 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5043 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5044 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5046 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5047 (opj_tp_index_t*)opj_realloc(
5048 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5049 l_num_parts* sizeof(opj_tp_index_t));
5052 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5054 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5055 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5056 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5057 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5058 sizeof(opj_tp_index_t));
5061 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5062 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5063 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5064 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5065 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5066 * sizeof(opj_tp_index_t));
5075 /* 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 */
5076 /* if (p_j2k->cstr_info) {
5079 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5082 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5083 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5084 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5085 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5088 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5091 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5095 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5098 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5099 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5100 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5105 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5108 opj_tcp_t *tcp = NULL;
5109 opj_codestream_info_t *cstr_info = NULL;
5111 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5112 opj_cp_t *cp = j2k->cp;
5113 opj_cio_t *cio = j2k->cio;
5115 tcd->tp_num = j2k->tp_num ;
5116 tcd->cur_tp_num = j2k->cur_tp_num;
5118 cio_write(cio, J2K_MS_SOD, 2);
5120 if( j2k->cstr_info && j2k->cur_tp_num==0){
5121 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5124 if (j2k->curtileno == 0) {
5125 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5129 cstr_info = j2k->cstr_info;
5131 if (!j2k->cur_tp_num ) {
5132 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5133 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5136 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5137 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5141 /* update markers struct */
5142 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5143 #endif /* USE_JPWL */
5148 tcp = &cp->tcps[j2k->curtileno];
5149 for (layno = 0; layno < tcp->numlayers; layno++) {
5150 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5151 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5152 } else if (tcp->rates[layno]) {
5153 tcp->rates[layno]=1;
5156 if(j2k->cur_tp_num == 0){
5157 tcd->tcd_image->tiles->packno = 0;
5159 cstr_info->packno = 0;
5162 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5164 /* Writing Psot in SOT marker */
5165 totlen = cio_tell(cio) + l - j2k->sot_start;
5166 cio_seek(cio, j2k->sot_start + 6);
5167 cio_write(cio, totlen, 4);
5168 cio_seek(cio, j2k->sot_start + totlen);
5169 /* Writing Ttlm and Ptlm in TLM marker */
5171 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5172 cio_write(cio, j2k->curtileno, 1);
5173 cio_write(cio, totlen, 4);
5175 cio_seek(cio, j2k->sot_start + totlen);
5179 * Writes the SOD marker (Start of data)
5181 * @param p_stream the stream to write data to.
5182 * @param p_j2k J2K codec.
5183 * @param p_manager the user event manager.
5185 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5186 struct opj_tcd_v2 * p_tile_coder,
5188 OPJ_UINT32 * p_data_written,
5189 OPJ_UINT32 p_total_data_size,
5190 const struct opj_stream_private *p_stream,
5191 struct opj_event_mgr * p_manager )
5193 opj_tcp_v2_t *l_tcp = 00;
5194 opj_codestream_info_t *l_cstr_info = 00;
5195 opj_cp_v2_t *l_cp = 00;
5197 OPJ_UINT32 l_size_tile;
5198 OPJ_UINT32 l_remaining_data;
5201 assert(p_j2k != 00);
5202 assert(p_manager != 00);
5203 assert(p_stream != 00);
5205 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5208 /* make room for the EOF marker */
5209 l_remaining_data = p_total_data_size - 4;
5211 l_cp = &(p_j2k->m_cp);
5212 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5215 /* update tile coder */
5216 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5217 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5219 l_size_tile = l_cp->th * l_cp->tw;
5222 /* TODO mergeV2: check this part which use cstr_info */
5223 /*l_cstr_info = p_j2k->cstr_info;
5225 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5226 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5227 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5233 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5235 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5240 /* update markers struct */
5241 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5243 assert( 0 && "TODO" );
5244 #endif /* USE_JPWL */
5249 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5250 p_tile_coder->tcd_image->tiles->packno = 0;
5252 l_cstr_info->packno = 0;
5256 *p_data_written = 0;
5258 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)) {
5259 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5263 *p_data_written += 2;
5272 * Reads a SOD marker (Start Of Data)
5274 * @param p_header_data the data contained in the SOD box.
5275 * @param p_j2k the jpeg2000 codec.
5276 * @param p_header_size the size of the data contained in the SOD marker.
5277 * @param p_manager the user event manager.
5279 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
5280 opj_stream_private_t *p_stream,
5281 opj_event_mgr_t * p_manager
5284 OPJ_UINT32 l_current_read_size;
5285 opj_codestream_index_t * l_cstr_index = 00;
5286 OPJ_BYTE ** l_current_data = 00;
5287 opj_tcp_v2_t * l_tcp = 00;
5288 OPJ_UINT32 * l_tile_len = 00;
5291 assert(p_j2k != 00);
5292 assert(p_manager != 00);
5293 assert(p_stream != 00);
5295 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5297 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5298 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5299 // but we are in the last tile part,
5300 // so its result will fit on OPJ_UINT32 unless we find
5301 // a file with a single tile part of more than 4 GB...*/
5302 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5305 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5307 l_current_data = &(l_tcp->m_data);
5308 l_tile_len = &l_tcp->m_data_size;
5310 if (! *l_current_data) {
5311 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5314 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5317 if (*l_current_data == 00) {
5318 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5324 l_cstr_index = p_j2k->cstr_index;
5326 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5328 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5329 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5331 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5332 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5334 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5338 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5340 /*l_cstr_index->packno = 0;*/
5343 l_current_read_size = opj_stream_read_data( p_stream,
5344 *l_current_data + *l_tile_len,
5345 p_j2k->m_specific_param.m_decoder.m_sot_length,
5348 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5349 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5352 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5355 *l_tile_len += l_current_read_size;
5361 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5362 opj_cp_t *cp = j2k->cp;
5363 opj_tcp_t *tcp = &cp->tcps[tileno];
5364 opj_cio_t *cio = j2k->cio;
5365 int numcomps = j2k->image->numcomps;
5367 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5368 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5369 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5370 cio_write(cio, 0, 1); /* Srgn */
5371 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5375 * Writes the RGN marker (Region Of Interest)
5377 * @param p_tile_no the tile to output
5378 * @param p_comp_no the component to output
5379 * @param p_stream the stream to write data to.
5380 * @param p_j2k J2K codec.
5381 * @param p_manager the user event manager.
5383 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5384 OPJ_UINT32 p_tile_no,
5385 OPJ_UINT32 p_comp_no,
5386 struct opj_stream_private *p_stream,
5387 struct opj_event_mgr * p_manager )
5389 OPJ_BYTE * l_current_data = 00;
5390 OPJ_UINT32 l_nb_comp;
5391 OPJ_UINT32 l_rgn_size;
5392 opj_image_t *l_image = 00;
5393 opj_cp_v2_t *l_cp = 00;
5394 opj_tcp_v2_t *l_tcp = 00;
5395 opj_tccp_t *l_tccp = 00;
5396 OPJ_UINT32 l_comp_room;
5399 assert(p_j2k != 00);
5400 assert(p_manager != 00);
5401 assert(p_stream != 00);
5403 l_cp = &(p_j2k->m_cp);
5404 l_tcp = &l_cp->tcps[p_tile_no];
5405 l_tccp = &l_tcp->tccps[p_comp_no];
5407 l_nb_comp = l_image->numcomps;
5409 if (l_nb_comp <= 256) {
5416 l_rgn_size = 6 + l_comp_room;
5418 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5420 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5421 l_current_data += 2;
5423 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5424 l_current_data += 2;
5426 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5427 l_current_data+=l_comp_room;
5429 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5432 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5435 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) {
5443 static void j2k_write_eoc(opj_j2k_t *j2k) {
5444 opj_cio_t *cio = j2k->cio;
5445 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5446 cio_write(cio, J2K_MS_EOC, 2);
5450 /* update markers struct */
5451 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5452 #endif /* USE_JPWL */
5457 * Writes the EOC marker (End of Codestream)
5459 * @param p_stream the stream to write data to.
5460 * @param p_j2k J2K codec.
5461 * @param p_manager the user event manager.
5463 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5464 struct opj_stream_private *p_stream,
5465 struct opj_event_mgr * p_manager )
5468 assert(p_j2k != 00);
5469 assert(p_manager != 00);
5470 assert(p_stream != 00);
5472 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5477 /* update markers struct */
5479 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5481 #endif /* USE_JPWL */
5483 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5487 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5496 * Reads a RGN marker (Region Of Interest)
5498 * @param p_header_data the data contained in the POC box.
5499 * @param p_j2k the jpeg2000 codec.
5500 * @param p_header_size the size of the data contained in the POC marker.
5501 * @param p_manager the user event manager.
5503 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
5504 OPJ_BYTE * p_header_data,
5505 OPJ_UINT32 p_header_size,
5506 opj_event_mgr_t * p_manager
5509 OPJ_UINT32 l_nb_comp;
5510 opj_image_t * l_image = 00;
5512 opj_cp_v2_t *l_cp = 00;
5513 opj_tcp_v2_t *l_tcp = 00;
5514 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5517 assert(p_header_data != 00);
5518 assert(p_j2k != 00);
5519 assert(p_manager != 00);
5521 l_image = p_j2k->m_private_image;
5522 l_nb_comp = l_image->numcomps;
5524 if (l_nb_comp <= 256) {
5529 if (p_header_size != 2 + l_comp_room) {
5530 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5534 l_cp = &(p_j2k->m_cp);
5535 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5536 &l_cp->tcps[p_j2k->m_current_tile_number] :
5537 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5539 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5540 p_header_data+=l_comp_room;
5541 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5545 if (l_cp->correct) {
5546 /* totlen is negative or larger than the bytes left!!! */
5547 if (l_comp_room >= l_nb_comp) {
5548 opj_event_msg_v2(p_manager, EVT_ERROR,
5549 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5550 l_comp_room, l_nb_comp);
5551 if (!JPWL_ASSUME || JPWL_ASSUME) {
5552 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5557 #endif /* USE_JPWL */
5559 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5566 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5568 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5571 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5578 * Updates the rates of the tcp.
5580 * @param p_stream the stream to write data to.
5581 * @param p_j2k J2K codec.
5582 * @param p_manager the user event manager.
5584 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5585 struct opj_stream_private *p_stream,
5586 struct opj_event_mgr * p_manager )
5588 opj_cp_v2_t * l_cp = 00;
5589 opj_image_t * l_image = 00;
5590 opj_tcp_v2_t * l_tcp = 00;
5591 opj_image_comp_t * l_img_comp = 00;
5594 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5595 OPJ_FLOAT32 * l_rates = 0;
5596 OPJ_FLOAT32 l_sot_remove;
5597 OPJ_UINT32 l_bits_empty, l_size_pixel;
5598 OPJ_UINT32 l_tile_size = 0;
5599 OPJ_UINT32 l_last_res;
5600 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5603 assert(p_j2k != 00);
5604 assert(p_manager != 00);
5605 assert(p_stream != 00);
5608 l_cp = &(p_j2k->m_cp);
5609 l_image = p_j2k->m_private_image;
5612 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5613 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5614 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5616 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5617 l_tp_stride_func = get_tp_stride;
5620 l_tp_stride_func = get_default_stride;
5623 for (i=0;i<l_cp->th;++i) {
5624 for (j=0;j<l_cp->tw;++j) {
5625 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5627 /* 4 borders of the tile rescale on the image if necessary */
5628 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5629 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5630 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5631 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5633 l_rates = l_tcp->rates;
5635 /* Modification of the RATE >> */
5637 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5639 ((*l_rates) * l_bits_empty)
5647 for (k = 1; k < l_tcp->numlayers; ++k) {
5649 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5651 ((*l_rates) * l_bits_empty)
5667 for (i=0;i<l_cp->th;++i) {
5668 for (j=0;j<l_cp->tw;++j) {
5669 l_rates = l_tcp->rates;
5672 *l_rates -= l_sot_remove;
5674 if (*l_rates < 30) {
5681 l_last_res = l_tcp->numlayers - 1;
5683 for (k = 1; k < l_last_res; ++k) {
5686 *l_rates -= l_sot_remove;
5688 if (*l_rates < *(l_rates - 1) + 10) {
5689 *l_rates = (*(l_rates - 1)) + 20;
5697 *l_rates -= (l_sot_remove + 2.f);
5699 if (*l_rates < *(l_rates - 1) + 10) {
5700 *l_rates = (*(l_rates - 1)) + 20;
5708 l_img_comp = l_image->comps;
5711 for (i=0;i<l_image->numcomps;++i) {
5712 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5714 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5722 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5724 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5726 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5727 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5728 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5729 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5733 if (l_cp->m_specific_param.m_enc.m_cinema) {
5734 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5735 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5736 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5740 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5741 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5747 static void j2k_read_eoc(opj_j2k_t *j2k) {
5749 opj_bool success = OPJ_FALSE;
5751 /* if packets should be decoded */
5752 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5753 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5754 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5755 for (i = 0; i < j2k->cp->tileno_size; i++) {
5756 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5757 if (j2k->cp->tileno[i] != -1)
5759 tileno = j2k->cp->tileno[i];
5760 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5761 opj_free(j2k->tile_data[tileno]);
5762 j2k->tile_data[tileno] = NULL;
5763 tcd_free_decode_tile(tcd, i);
5766 success = OPJ_FALSE;
5767 if (success == OPJ_FALSE) {
5768 j2k->state |= J2K_STATE_ERR;
5772 tcd_free_decode(tcd);
5775 /* if packets should not be decoded */
5777 for (i = 0; i < j2k->cp->tileno_size; i++) {
5778 tileno = j2k->cp->tileno[i];
5779 opj_free(j2k->tile_data[tileno]);
5780 j2k->tile_data[tileno] = NULL;
5783 if (j2k->state & J2K_STATE_ERR)
5784 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5786 j2k->state = J2K_STATE_MT;
5790 * Reads a EOC marker (End Of Codestream)
5792 * @param p_header_data the data contained in the SOD box.
5793 * @param p_j2k the jpeg2000 codec.
5794 * @param p_header_size the size of the data contained in the SOD marker.
5795 * @param p_manager the user event manager.
5798 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5799 struct opj_stream_private *p_stream,
5800 struct opj_event_mgr * p_manager )
5803 opj_tcd_v2_t * l_tcd = 00;
5804 OPJ_UINT32 l_nb_tiles;
5805 opj_tcp_v2_t * l_tcp = 00;
5809 assert(p_j2k != 00);
5810 assert(p_manager != 00);
5811 assert(p_stream != 00);
5813 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5814 l_tcp = p_j2k->m_cp.tcps;
5816 l_tcd = tcd_create_v2(OPJ_TRUE);
5818 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5822 for (i = 0; i < l_nb_tiles; ++i) {
5823 if (l_tcp->m_data) {
5824 if (! tcd_init_decode_tile(l_tcd, i)) {
5825 tcd_destroy_v2(l_tcd);
5826 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5830 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5834 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5839 j2k_tcp_destroy(l_tcp);
5843 tcd_destroy_v2(l_tcd);
5849 * Gets the offset of the header.
5851 * @param p_stream the stream to write data to.
5852 * @param p_j2k J2K codec.
5853 * @param p_manager the user event manager.
5855 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5856 struct opj_stream_private *p_stream,
5857 struct opj_event_mgr * p_manager )
5860 assert(p_j2k != 00);
5861 assert(p_manager != 00);
5862 assert(p_stream != 00);
5864 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5870 * Writes the MCT marker (Multiple Component Transform)
5872 * @param p_stream the stream to write data to.
5873 * @param p_j2k J2K codec.
5874 * @param p_manager the user event manager.
5876 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5877 struct opj_stream_private *p_stream,
5878 struct opj_event_mgr * p_manager )
5881 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5882 opj_mct_data_t * l_mct_record;
5883 opj_tcp_v2_t * l_tcp;
5886 assert(p_j2k != 00);
5887 assert(p_stream != 00);
5888 assert(p_manager != 00);
5890 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5894 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5895 l_mct_record = l_tcp->m_mct_records;
5897 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5899 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5906 l_mcc_record = l_tcp->m_mcc_records;
5908 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5910 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5917 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5925 * Writes the image components.
5927 * @param p_stream the stream to write data to.
5928 * @param p_j2k J2K codec.
5929 * @param p_manager the user event manager.
5931 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5932 struct opj_stream_private *p_stream,
5933 struct opj_event_mgr * p_manager )
5938 assert(p_j2k != 00);
5939 assert(p_manager != 00);
5940 assert(p_stream != 00);
5942 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5944 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5948 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5957 * Writes regions of interests.
5959 * @param p_stream the stream to write data to.
5960 * @param p_j2k J2K codec.
5961 * @param p_manager the user event manager.
5963 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5964 struct opj_stream_private *p_stream,
5965 struct opj_event_mgr * p_manager )
5968 const opj_tccp_t *l_tccp = 00;
5971 assert(p_j2k != 00);
5972 assert(p_manager != 00);
5973 assert(p_stream != 00);
5975 l_tccp = p_j2k->m_cp.tcps->tccps;
5977 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5978 if (l_tccp->roishift) {
5980 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5994 * @param p_stream the stream to write data to.
5995 * @param p_j2k J2K codec.
5996 * @param p_manager the user event manager.
5998 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5999 struct opj_stream_private *p_stream,
6000 struct opj_event_mgr * p_manager )
6002 opj_codestream_index_t * l_cstr_index = 00;
6005 assert(p_j2k != 00);
6006 assert(p_manager != 00);
6007 assert(p_stream != 00);
6009 l_cstr_index = p_j2k->cstr_index;
6011 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6013 /* The following adjustment is done to adjust the codestream size */
6014 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6015 /* the first bunch of bytes is not in the codestream */
6016 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6021 /* preparation of JPWL marker segments */
6025 /* encode according to JPWL */
6026 jpwl_encode(p_j2k, p_stream, image);
6030 assert( 0 && "TODO" );
6031 #endif /* USE_JPWL */
6036 typedef struct opj_dec_mstabent {
6039 /** value of the state when the marker can appear */
6041 /** action linked to the marker */
6042 void (*handler) (opj_j2k_t *j2k);
6043 } opj_dec_mstabent_t;
6045 opj_dec_mstabent_t j2k_dec_mstab[] = {
6046 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
6047 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
6048 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
6049 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6050 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
6051 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
6052 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
6053 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
6054 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
6055 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6056 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6057 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6058 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6059 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6060 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6061 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6063 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6064 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6067 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6068 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6069 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6070 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6071 #endif /* USE_JPWL */
6073 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6074 {J2K_MS_INSEC, 0, j2k_read_insec},
6075 #endif /* USE_JPSEC */
6077 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6080 static void j2k_read_unk(opj_j2k_t *j2k) {
6081 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6084 if (j2k->cp->correct) {
6086 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6087 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6088 id = cio_read(j2k->cio, 2);
6089 opj_event_msg(j2k->cinfo, EVT_ERROR,
6090 "JPWL: really don't know this marker %x\n",
6093 opj_event_msg(j2k->cinfo, EVT_ERROR,
6094 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6097 /* OK, activate this at your own risk!!! */
6098 /* we look for the marker at the minimum hamming distance from this */
6099 while (j2k_dec_mstab[m].id) {
6101 /* 1's where they differ */
6102 tmp_id = j2k_dec_mstab[m].id ^ id;
6104 /* compute the hamming distance between our id and the current */
6106 for (i = 0; i < 16; i++) {
6107 if ((tmp_id >> i) & 0x0001) {
6112 /* if current distance is smaller, set the minimum */
6113 if (cur_dist < min_dist) {
6114 min_dist = cur_dist;
6115 min_id = j2k_dec_mstab[m].id;
6118 /* jump to the next marker */
6122 /* do we substitute the marker? */
6123 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6124 opj_event_msg(j2k->cinfo, EVT_ERROR,
6125 "- marker %x is at distance %d from the read %x\n",
6126 min_id, min_dist, id);
6127 opj_event_msg(j2k->cinfo, EVT_ERROR,
6128 "- trying to substitute in place and crossing fingers!\n");
6129 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6130 cio_write(j2k->cio, min_id, 2);
6133 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6138 #endif /* USE_JPWL */
6143 * Reads an unknown marker
6145 * @param p_stream the stream object to read from.
6146 * @param p_j2k the jpeg2000 codec.
6147 * @param p_manager the user event manager.
6149 * @return true if the marker could be deduced.
6151 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6152 struct opj_stream_private *p_stream,
6153 OPJ_UINT32 *output_marker,
6154 struct opj_event_mgr * p_manager
6157 OPJ_UINT32 l_unknown_marker;
6158 const opj_dec_memory_marker_handler_t * l_marker_handler;
6159 OPJ_UINT32 l_size_unk = 2;
6162 assert(p_j2k != 00);
6163 assert(p_manager != 00);
6164 assert(p_stream != 00);
6166 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6169 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6170 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6171 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6175 /* read 2 bytes as the new marker ID*/
6176 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6178 if (!(l_unknown_marker < 0xff00)) {
6180 /* Get the marker handler from the marker ID*/
6181 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6183 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6184 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6188 if (l_marker_handler->id != J2K_MS_UNK) {
6189 /* Add the marker to the codestream index*/
6190 if (l_marker_handler->id != J2K_MS_SOT)
6191 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6192 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6194 break; /* next marker is known and well located */
6202 *output_marker = l_marker_handler->id ;
6208 Read the lookup table containing all the marker, status and action
6209 @param id Marker value
6211 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6212 opj_dec_mstabent_t *e;
6213 for (e = j2k_dec_mstab; e->id != 0; e++) {
6222 * Writes the MCT marker (Multiple Component Transform)
6224 * @param p_stream the stream to write data to.
6225 * @param p_j2k J2K codec.
6226 * @param p_manager the user event manager.
6228 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6229 opj_mct_data_t * p_mct_record,
6230 struct opj_stream_private *p_stream,
6231 struct opj_event_mgr * p_manager )
6233 OPJ_UINT32 l_mct_size;
6234 OPJ_BYTE * l_current_data = 00;
6238 assert(p_j2k != 00);
6239 assert(p_manager != 00);
6240 assert(p_stream != 00);
6242 l_mct_size = 10 + p_mct_record->m_data_size;
6244 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6245 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6246 = (OPJ_BYTE*)opj_realloc(
6247 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6250 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6254 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6257 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6259 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6260 l_current_data += 2;
6262 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6263 l_current_data += 2;
6265 opj_write_bytes(l_current_data,0,2); /* Zmct */
6266 l_current_data += 2;
6268 /* only one marker atm */
6269 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6271 opj_write_bytes(l_current_data,l_tmp,2);
6272 l_current_data += 2;
6274 opj_write_bytes(l_current_data,0,2); /* Ymct */
6277 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6279 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) {
6287 * Reads a MCT marker (Multiple Component Transform)
6289 * @param p_header_data the data contained in the MCT box.
6290 * @param p_j2k the jpeg2000 codec.
6291 * @param p_header_size the size of the data contained in the MCT marker.
6292 * @param p_manager the user event manager.
6294 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6295 OPJ_BYTE * p_header_data,
6296 OPJ_UINT32 p_header_size,
6297 struct opj_event_mgr * p_manager )
6300 opj_tcp_v2_t *l_tcp = 00;
6303 opj_mct_data_t * l_mct_data;
6306 assert(p_header_data != 00);
6307 assert(p_j2k != 00);
6309 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6310 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6311 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6313 if (p_header_size < 2) {
6314 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6319 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6322 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6326 if(p_header_size <= 6) {
6327 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6331 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6332 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6335 l_indix = l_tmp & 0xff;
6336 l_mct_data = l_tcp->m_mct_records;
6338 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6339 if (l_mct_data->m_index == l_indix) {
6346 if (i == l_tcp->m_nb_mct_records) {
6347 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6348 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6350 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));
6351 if(! l_tcp->m_mct_records) {
6352 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6356 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6357 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6360 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6363 if (l_mct_data->m_data) {
6364 opj_free(l_mct_data->m_data);
6365 l_mct_data->m_data = 00;
6368 l_mct_data->m_index = l_indix;
6369 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6370 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6372 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6375 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6381 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6382 if (! l_mct_data->m_data) {
6383 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6386 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6388 l_mct_data->m_data_size = p_header_size;
6389 ++l_tcp->m_nb_mct_records;
6395 * Writes the MCC marker (Multiple Component Collection)
6397 * @param p_stream the stream to write data to.
6398 * @param p_j2k J2K codec.
6399 * @param p_manager the user event manager.
6401 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6402 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6403 struct opj_stream_private *p_stream,
6404 struct opj_event_mgr * p_manager )
6407 OPJ_UINT32 l_mcc_size;
6408 OPJ_BYTE * l_current_data = 00;
6409 OPJ_UINT32 l_nb_bytes_for_comp;
6414 assert(p_j2k != 00);
6415 assert(p_manager != 00);
6416 assert(p_stream != 00);
6418 if (p_mcc_record->m_nb_comps > 255 ) {
6419 l_nb_bytes_for_comp = 2;
6423 l_nb_bytes_for_comp = 1;
6427 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6428 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6430 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6431 = (OPJ_BYTE*)opj_realloc(
6432 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6434 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6438 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6441 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6443 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6444 l_current_data += 2;
6446 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6447 l_current_data += 2;
6450 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6451 l_current_data += 2;
6453 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6456 /* only one marker atm */
6457 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6460 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6463 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6466 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 */
6469 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6470 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6471 l_current_data+=l_nb_bytes_for_comp;
6474 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 */
6477 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6479 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6480 l_current_data+=l_nb_bytes_for_comp;
6483 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6485 if (p_mcc_record->m_decorrelation_array) {
6486 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6489 if (p_mcc_record->m_offset_array) {
6490 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6493 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6496 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) {
6504 * Reads a MCC marker (Multiple Component Collection)
6506 * @param p_header_data the data contained in the MCC box.
6507 * @param p_j2k the jpeg2000 codec.
6508 * @param p_header_size the size of the data contained in the MCC marker.
6509 * @param p_manager the user event manager.
6511 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6512 OPJ_BYTE * p_header_data,
6513 OPJ_UINT32 p_header_size,
6514 struct opj_event_mgr * p_manager )
6519 opj_tcp_v2_t * l_tcp;
6520 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6521 opj_mct_data_t * l_mct_data;
6522 OPJ_UINT32 l_nb_collections;
6523 OPJ_UINT32 l_nb_comps;
6524 OPJ_UINT32 l_nb_bytes_by_comp;
6528 assert(p_header_data != 00);
6529 assert(p_j2k != 00);
6530 assert(p_manager != 00);
6532 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6533 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6534 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6536 if (p_header_size < 2) {
6537 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6542 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6545 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6549 if (p_header_size < 7) {
6550 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6554 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6557 l_mcc_record = l_tcp->m_mcc_records;
6559 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6560 if (l_mcc_record->m_index == l_indix) {
6567 if (i == l_tcp->m_nb_mcc_records) {
6568 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6569 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6571 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6572 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6573 if (! l_tcp->m_mcc_records) {
6574 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6577 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6578 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));
6580 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6582 l_mcc_record->m_index = l_indix;
6584 /* only one marker atm */
6585 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6588 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6592 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6595 if (l_nb_collections > 1) {
6596 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6602 for (i=0;i<l_nb_collections;++i) {
6603 if (p_header_size < 3) {
6604 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6608 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6612 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6616 opj_read_bytes(p_header_data,&l_nb_comps,2);
6621 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6622 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6624 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6625 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6629 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6631 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6632 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6633 p_header_data+=l_nb_bytes_by_comp;
6636 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6641 opj_read_bytes(p_header_data,&l_nb_comps,2);
6644 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6645 l_nb_comps &= 0x7fff;
6647 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6648 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6652 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6653 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6657 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6659 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6660 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6661 p_header_data+=l_nb_bytes_by_comp;
6664 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6669 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6672 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6673 l_mcc_record->m_decorrelation_array = 00;
6674 l_mcc_record->m_offset_array = 00;
6676 l_indix = l_tmp & 0xff;
6678 l_mct_data = l_tcp->m_mct_records;
6679 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6680 if (l_mct_data->m_index == l_indix) {
6681 l_mcc_record->m_decorrelation_array = l_mct_data;
6687 if (l_mcc_record->m_decorrelation_array == 00) {
6688 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6693 l_indix = (l_tmp >> 8) & 0xff;
6695 l_mct_data = l_tcp->m_mct_records;
6696 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6697 if (l_mct_data->m_index == l_indix) {
6698 l_mcc_record->m_offset_array = l_mct_data;
6704 if (l_mcc_record->m_offset_array == 00) {
6705 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6711 if (p_header_size != 0) {
6712 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6716 ++l_tcp->m_nb_mcc_records;
6723 * Writes the MCO marker (Multiple component transformation ordering)
6725 * @param p_stream the stream to write data to.
6726 * @param p_j2k J2K codec.
6727 * @param p_manager the user event manager.
6729 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6730 struct opj_stream_private *p_stream,
6731 struct opj_event_mgr * p_manager
6734 OPJ_BYTE * l_current_data = 00;
6735 OPJ_UINT32 l_mco_size;
6736 opj_tcp_v2_t * l_tcp = 00;
6737 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6741 assert(p_j2k != 00);
6742 assert(p_manager != 00);
6743 assert(p_stream != 00);
6745 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6746 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6748 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6749 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6751 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6752 = (OPJ_BYTE*)opj_realloc(
6753 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6755 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6760 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6763 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6764 l_current_data += 2;
6766 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6767 l_current_data += 2;
6769 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6772 l_mcc_record = l_tcp->m_mcc_records;
6773 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6774 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6780 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) {
6788 * Reads a MCO marker (Multiple Component Transform Ordering)
6790 * @param p_header_data the data contained in the MCO box.
6791 * @param p_j2k the jpeg2000 codec.
6792 * @param p_header_size the size of the data contained in the MCO marker.
6793 * @param p_manager the user event manager.
6795 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6796 OPJ_BYTE * p_header_data,
6797 OPJ_UINT32 p_header_size,
6798 struct opj_event_mgr * p_manager )
6800 OPJ_UINT32 l_tmp, i;
6801 OPJ_UINT32 l_nb_stages;
6802 opj_tcp_v2_t * l_tcp;
6803 opj_tccp_t * l_tccp;
6804 opj_image_t * l_image;
6805 opj_image_comp_t * l_img_comp;
6808 assert(p_header_data != 00);
6809 assert(p_j2k != 00);
6810 assert(p_manager != 00);
6812 l_image = p_j2k->m_private_image;
6813 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6814 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6815 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6817 if (p_header_size < 1) {
6818 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6822 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6825 if (l_nb_stages > 1) {
6826 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6830 if (p_header_size != l_nb_stages + 1) {
6831 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6835 l_tccp = l_tcp->tccps;
6836 l_img_comp = l_image->comps;
6838 for (i=0;i<l_image->numcomps;++i) {
6839 l_tccp->m_dc_level_shift = 0;
6843 if (l_tcp->m_mct_decoding_matrix) {
6844 opj_free(l_tcp->m_mct_decoding_matrix);
6845 l_tcp->m_mct_decoding_matrix = 00;
6848 for (i=0;i<l_nb_stages;++i) {
6849 opj_read_bytes(p_header_data,&l_tmp,1);
6852 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6860 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6863 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6864 opj_mct_data_t * l_deco_array, * l_offset_array;
6865 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6866 OPJ_UINT32 l_nb_elem;
6867 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6868 opj_tccp_t * l_tccp;
6871 assert(p_tcp != 00);
6873 l_mcc_record = p_tcp->m_mcc_records;
6875 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6876 if (l_mcc_record->m_index == p_index) {
6881 if (i==p_tcp->m_nb_mcc_records) {
6882 /** element discarded **/
6886 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6887 /** do not support number of comps != image */
6891 l_deco_array = l_mcc_record->m_decorrelation_array;
6894 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6895 if (l_deco_array->m_data_size != l_data_size) {
6899 l_nb_elem = p_image->numcomps * p_image->numcomps;
6900 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6901 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6903 if (! p_tcp->m_mct_decoding_matrix ) {
6907 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);
6910 l_offset_array = l_mcc_record->m_offset_array;
6912 if (l_offset_array) {
6913 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6914 if (l_offset_array->m_data_size != l_data_size) {
6918 l_nb_elem = p_image->numcomps;
6919 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6920 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6922 if (! l_offset_data ) {
6926 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6928 l_tccp = p_tcp->tccps;
6929 l_current_offset_data = l_offset_data;
6931 for (i=0;i<p_image->numcomps;++i) {
6932 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6936 opj_free(l_offset_data);
6943 * Writes the CBD marker (Component bit depth definition)
6945 * @param p_stream the stream to write data to.
6946 * @param p_j2k J2K codec.
6947 * @param p_manager the user event manager.
6949 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6950 struct opj_stream_private *p_stream,
6951 struct opj_event_mgr * p_manager )
6954 OPJ_UINT32 l_cbd_size;
6955 OPJ_BYTE * l_current_data = 00;
6956 opj_image_t *l_image = 00;
6957 opj_image_comp_t * l_comp = 00;
6960 assert(p_j2k != 00);
6961 assert(p_manager != 00);
6962 assert(p_stream != 00);
6964 l_image = p_j2k->m_private_image;
6965 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6967 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6968 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6969 = (OPJ_BYTE*)opj_realloc(
6970 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6973 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6977 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6980 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6982 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6983 l_current_data += 2;
6985 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6986 l_current_data += 2;
6988 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6991 l_comp = l_image->comps;
6993 for (i=0;i<l_image->numcomps;++i) {
6994 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7000 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) {
7008 * Reads a CBD marker (Component bit depth definition)
7009 * @param p_header_data the data contained in the CBD box.
7010 * @param p_j2k the jpeg2000 codec.
7011 * @param p_header_size the size of the data contained in the CBD marker.
7012 * @param p_manager the user event manager.
7014 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7015 OPJ_BYTE * p_header_data,
7016 OPJ_UINT32 p_header_size,
7017 struct opj_event_mgr * p_manager)
7019 OPJ_UINT32 l_nb_comp,l_num_comp;
7020 OPJ_UINT32 l_comp_def;
7022 opj_image_comp_t * l_comp = 00;
7025 assert(p_header_data != 00);
7026 assert(p_j2k != 00);
7027 assert(p_manager != 00);
7029 l_num_comp = p_j2k->m_private_image->numcomps;
7031 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7032 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7036 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7039 if (l_nb_comp != l_num_comp) {
7040 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7044 l_comp = p_j2k->m_private_image->comps;
7045 for (i=0;i<l_num_comp;++i) {
7046 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7048 l_comp->sgnd = (l_comp_def>>7) & 1;
7049 l_comp->prec = (l_comp_def&0x7f) + 1;
7057 /* ----------------------------------------------------------------------- */
7058 /* J2K / JPT decoder interface */
7059 /* ----------------------------------------------------------------------- */
7063 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7066 if(j2k->tile_len != NULL) {
7067 opj_free(j2k->tile_len);
7069 if(j2k->tile_data != NULL) {
7070 opj_free(j2k->tile_data);
7072 if(j2k->default_tcp != NULL) {
7073 opj_tcp_t *default_tcp = j2k->default_tcp;
7074 if(default_tcp->ppt_data_first != NULL) {
7075 opj_free(default_tcp->ppt_data_first);
7077 if(j2k->default_tcp->tccps != NULL) {
7078 opj_free(j2k->default_tcp->tccps);
7080 opj_free(j2k->default_tcp);
7082 if(j2k->cp != NULL) {
7083 opj_cp_t *cp = j2k->cp;
7084 if(cp->tcps != NULL) {
7085 for(i = 0; i < cp->tw * cp->th; i++) {
7086 if(cp->tcps[i].ppt_data_first != NULL) {
7087 opj_free(cp->tcps[i].ppt_data_first);
7089 if(cp->tcps[i].tccps != NULL) {
7090 opj_free(cp->tcps[i].tccps);
7095 if(cp->ppm_data_first != NULL) {
7096 opj_free(cp->ppm_data_first);
7098 if(cp->tileno != NULL) {
7099 opj_free(cp->tileno);
7101 if(cp->comment != NULL) {
7102 opj_free(cp->comment);
7111 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7113 if(j2k && parameters) {
7114 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7115 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7118 j2k->m_cp.correct = parameters->jpwl_correct;
7119 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7120 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7121 #endif /* USE_JPWL */
7128 * Read a JPT-stream and decode file
7131 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7132 opj_image_t *image = NULL;
7133 opj_jpt_msg_header_t header;
7135 opj_common_ptr cinfo = j2k->cinfo;
7137 OPJ_ARG_NOT_USED(cstr_info);
7141 /* create an empty image */
7142 image = opj_image_create0();
7145 j2k->state = J2K_STATE_MHSOC;
7147 /* Initialize the header */
7148 jpt_init_msg_header(&header);
7149 /* Read the first header of the message */
7150 jpt_read_msg_header(cinfo, cio, &header);
7152 position = cio_tell(cio);
7153 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7154 opj_image_destroy(image);
7155 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7160 opj_dec_mstabent_t *e = NULL;
7163 if (!cio_numbytesleft(cio)) {
7167 /* data-bin read -> need to read a new header */
7168 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7169 jpt_read_msg_header(cinfo, cio, &header);
7170 position = cio_tell(cio);
7171 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7172 opj_image_destroy(image);
7173 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7178 id = cio_read(cio, 2);
7179 if (id >> 8 != 0xff) {
7180 opj_image_destroy(image);
7181 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7184 e = j2k_dec_mstab_lookup(id);
7185 if (!(j2k->state & e->states)) {
7186 opj_image_destroy(image);
7187 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7193 if (j2k->state == J2K_STATE_MT) {
7196 if (j2k->state == J2K_STATE_NEOC) {
7200 if (j2k->state == J2K_STATE_NEOC) {
7204 if (j2k->state != J2K_STATE_MT) {
7205 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7211 /* ----------------------------------------------------------------------- */
7212 /* J2K encoder interface */
7213 /* ----------------------------------------------------------------------- */
7215 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7216 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7223 opj_j2k_v2_t* j2k_create_compress_v2(void)
7225 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7230 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7232 l_j2k->m_is_decoder = 0;
7233 l_j2k->m_cp.m_is_decoder = 0;
7235 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7236 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7241 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7243 /* validation list creation*/
7244 l_j2k->m_validation_list = opj_procedure_list_create();
7245 if (! l_j2k->m_validation_list) {
7250 /* execution list creation*/
7251 l_j2k->m_procedure_list = opj_procedure_list_create();
7252 if (! l_j2k->m_procedure_list) {
7260 void j2k_destroy_compress(opj_j2k_t *j2k) {
7264 if(j2k->cp != NULL) {
7265 opj_cp_t *cp = j2k->cp;
7268 opj_free(cp->comment);
7271 opj_free(cp->matrice);
7273 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7274 opj_free(cp->tcps[tileno].tccps);
7283 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7285 int tileno, numpocs_tile;
7286 opj_cp_t *cp = NULL;
7288 if(!j2k || !parameters || ! image) {
7292 /* create and initialize the coding parameters structure */
7293 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7295 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7298 /* set default values for cp */
7303 copy user encoding parameters
7305 cp->cinema = parameters->cp_cinema;
7306 cp->max_comp_size = parameters->max_comp_size;
7307 cp->rsiz = parameters->cp_rsiz;
7308 cp->disto_alloc = parameters->cp_disto_alloc;
7309 cp->fixed_alloc = parameters->cp_fixed_alloc;
7310 cp->fixed_quality = parameters->cp_fixed_quality;
7312 /* mod fixed_quality */
7313 if(parameters->cp_matrice) {
7314 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7315 cp->matrice = (int *) opj_malloc(array_size);
7316 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7320 cp->tdx = parameters->cp_tdx;
7321 cp->tdy = parameters->cp_tdy;
7324 cp->tx0 = parameters->cp_tx0;
7325 cp->ty0 = parameters->cp_ty0;
7327 /* comment string */
7328 if(parameters->cp_comment) {
7329 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7331 strcpy(cp->comment, parameters->cp_comment);
7336 calculate other encoding parameters
7339 if (parameters->tile_size_on) {
7340 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7341 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7343 cp->tdx = image->x1 - cp->tx0;
7344 cp->tdy = image->y1 - cp->ty0;
7347 if(parameters->tp_on){
7348 cp->tp_flag = parameters->tp_flag;
7353 for(i=0;i<image->numcomps ;i++){
7354 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7360 calculate JPWL encoding parameters
7363 if (parameters->jpwl_epc_on) {
7367 cp->epc_on = OPJ_TRUE;
7368 cp->info_on = OPJ_FALSE; /* no informative technique */
7371 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7372 cp->epb_on = OPJ_TRUE;
7374 cp->hprot_MH = parameters->jpwl_hprot_MH;
7375 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7376 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7377 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7379 /* if tile specs are not specified, copy MH specs */
7380 if (cp->hprot_TPH[0] == -1) {
7381 cp->hprot_TPH_tileno[0] = 0;
7382 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7384 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7385 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7386 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7387 cp->pprot[i] = parameters->jpwl_pprot[i];
7391 /* set ESD writing */
7392 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7393 cp->esd_on = OPJ_TRUE;
7395 cp->sens_size = parameters->jpwl_sens_size;
7396 cp->sens_addr = parameters->jpwl_sens_addr;
7397 cp->sens_range = parameters->jpwl_sens_range;
7399 cp->sens_MH = parameters->jpwl_sens_MH;
7400 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7401 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7402 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7406 /* always set RED writing to false: we are at the encoder */
7407 cp->red_on = OPJ_FALSE;
7410 cp->epc_on = OPJ_FALSE;
7412 #endif /* USE_JPWL */
7415 /* initialize the mutiple tiles */
7416 /* ---------------------------- */
7417 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7419 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7420 opj_tcp_t *tcp = &cp->tcps[tileno];
7421 tcp->numlayers = parameters->tcp_numlayers;
7422 assert ( tcp->numlayers >= 0 );
7423 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7425 if (cp->fixed_quality) {
7426 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7428 tcp->rates[j] = parameters->tcp_rates[j];
7430 if (cp->fixed_quality) { /* add fixed_quality */
7431 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7433 tcp->rates[j] = parameters->tcp_rates[j];
7437 tcp->csty = parameters->csty;
7438 tcp->prg = parameters->prog_order;
7439 tcp->mct = parameters->tcp_mct;
7443 if (parameters->numpocs) {
7444 /* initialisation of POC */
7446 assert( parameters->numpocs >= 0 );
7447 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7448 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7449 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7450 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7451 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7452 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7453 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7454 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7455 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7456 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7460 tcp->numpocs = numpocs_tile -1 ;
7465 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7467 for (i = 0; i < image->numcomps; i++) {
7468 opj_tccp_t *tccp = &tcp->tccps[i];
7469 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7470 tccp->numresolutions = parameters->numresolution;
7471 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7472 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7473 tccp->cblksty = parameters->mode;
7474 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7475 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7477 assert(parameters->roi_compno >= 0);
7478 if (i == (OPJ_UINT32)parameters->roi_compno) {
7479 tccp->roishift = parameters->roi_shift;
7484 if(parameters->cp_cinema)
7486 /*Precinct size for lowest frequency subband=128*/
7489 /*Precinct size at all other resolutions = 256*/
7490 for (j = 1; j < tccp->numresolutions; j++) {
7495 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7497 assert(tccp->numresolutions > 0);
7498 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7499 if (p < parameters->res_spec) {
7501 if (parameters->prcw_init[p] < 1) {
7504 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7507 if (parameters->prch_init[p] < 1) {
7510 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7514 int res_spec = parameters->res_spec;
7515 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7516 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7518 if (size_prcw < 1) {
7521 tccp->prcw[j] = int_floorlog2(size_prcw);
7524 if (size_prch < 1) {
7527 tccp->prch[j] = int_floorlog2(size_prch);
7531 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7534 for (j = 0; j < tccp->numresolutions; j++) {
7541 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7546 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7547 opj_cparameters_t *parameters,
7549 struct opj_event_mgr * p_manager)
7551 OPJ_UINT32 i, j, tileno, numpocs_tile;
7552 opj_cp_v2_t *cp = 00;
7555 if(!p_j2k || !parameters || ! image) {
7559 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7560 cp = &(p_j2k->m_cp);
7562 /* set default values for cp */
7567 copy user encoding parameters
7569 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7570 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7571 cp->rsiz = parameters->cp_rsiz;
7572 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7573 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7574 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7576 /* mod fixed_quality */
7577 if (parameters->cp_matrice) {
7578 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7579 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7580 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7584 cp->tdx = parameters->cp_tdx;
7585 cp->tdy = parameters->cp_tdy;
7588 cp->tx0 = parameters->cp_tx0;
7589 cp->ty0 = parameters->cp_ty0;
7591 /* comment string */
7592 if(parameters->cp_comment) {
7593 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7595 strcpy(cp->comment, parameters->cp_comment);
7600 calculate other encoding parameters
7603 if (parameters->tile_size_on) {
7604 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7605 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7607 cp->tdx = image->x1 - cp->tx0;
7608 cp->tdy = image->y1 - cp->ty0;
7611 if (parameters->tp_on) {
7612 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7613 cp->m_specific_param.m_enc.m_tp_on = 1;
7618 calculate JPWL encoding parameters
7621 if (parameters->jpwl_epc_on) {
7625 cp->epc_on = OPJ_TRUE;
7626 cp->info_on = OPJ_FALSE; /* no informative technique */
7629 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7630 cp->epb_on = OPJ_TRUE;
7632 cp->hprot_MH = parameters->jpwl_hprot_MH;
7633 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7634 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7635 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7637 /* if tile specs are not specified, copy MH specs */
7638 if (cp->hprot_TPH[0] == -1) {
7639 cp->hprot_TPH_tileno[0] = 0;
7640 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7642 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7643 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7644 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7645 cp->pprot[i] = parameters->jpwl_pprot[i];
7649 /* set ESD writing */
7650 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7651 cp->esd_on = OPJ_TRUE;
7653 cp->sens_size = parameters->jpwl_sens_size;
7654 cp->sens_addr = parameters->jpwl_sens_addr;
7655 cp->sens_range = parameters->jpwl_sens_range;
7657 cp->sens_MH = parameters->jpwl_sens_MH;
7658 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7659 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7660 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7664 /* always set RED writing to false: we are at the encoder */
7665 cp->red_on = OPJ_FALSE;
7668 cp->epc_on = OPJ_FALSE;
7670 #endif /* USE_JPWL */
7673 /* initialize the mutiple tiles */
7674 /* ---------------------------- */
7675 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7676 if (parameters->numpocs) {
7677 /* initialisation of POC */
7678 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7682 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7683 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7684 tcp->numlayers = parameters->tcp_numlayers;
7686 for (j = 0; j < tcp->numlayers; j++) {
7687 if(cp->m_specific_param.m_enc.m_cinema){
7688 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7689 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7691 tcp->rates[j] = parameters->tcp_rates[j];
7693 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7694 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7696 tcp->rates[j] = parameters->tcp_rates[j];
7701 tcp->csty = parameters->csty;
7702 tcp->prg = parameters->prog_order;
7703 tcp->mct = parameters->tcp_mct;
7708 if (parameters->numpocs) {
7709 /* initialisation of POC */
7712 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7713 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7714 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7716 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7717 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7718 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7719 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7720 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7721 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7722 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7728 tcp->numpocs = numpocs_tile -1 ;
7733 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7735 if (parameters->mct_data) {
7737 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7740 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7741 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7742 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7743 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7746 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7747 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7748 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7750 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7751 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7753 tcp->mct_norms = (OPJ_FLOAT64*)
7754 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7756 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7759 for (i = 0; i < image->numcomps; i++) {
7760 opj_tccp_t *tccp = &tcp->tccps[i];
7761 tccp->m_dc_level_shift = l_dc_shift[i];
7764 j2k_setup_mct_encoding(tcp,image);
7768 for (i = 0; i < image->numcomps; i++) {
7769 opj_tccp_t *tccp = &tcp->tccps[i];
7770 opj_image_comp_t * l_comp = &(image->comps[i]);
7772 if (! l_comp->sgnd) {
7773 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7778 for (i = 0; i < image->numcomps; i++) {
7779 opj_tccp_t *tccp = &tcp->tccps[i];
7781 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7782 tccp->numresolutions = parameters->numresolution;
7783 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7784 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7785 tccp->cblksty = parameters->mode;
7786 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7787 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7790 if (i == parameters->roi_compno) {
7791 tccp->roishift = parameters->roi_shift;
7796 if(parameters->cp_cinema) {
7797 //Precinct size for lowest frequency subband=128
7800 //Precinct size at all other resolutions = 256
7801 for (j = 1; j < tccp->numresolutions; j++) {
7806 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7807 OPJ_INT32 p = 0, it_res;
7808 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7809 if (p < parameters->res_spec) {
7811 if (parameters->prcw_init[p] < 1) {
7812 tccp->prcw[it_res] = 1;
7814 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7817 if (parameters->prch_init[p] < 1) {
7818 tccp->prch[it_res] = 1;
7820 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7824 int res_spec = parameters->res_spec;
7825 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7826 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7828 if (size_prcw < 1) {
7829 tccp->prcw[it_res] = 1;
7831 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7834 if (size_prch < 1) {
7835 tccp->prch[it_res] = 1;
7837 tccp->prch[it_res] = int_floorlog2(size_prch);
7841 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7844 for (j = 0; j < tccp->numresolutions; j++) {
7851 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7855 if (parameters->mct_data) {
7856 opj_free(parameters->mct_data);
7857 parameters->mct_data = 00;
7862 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7865 opj_cp_t *cp = NULL;
7867 opj_tcd_t *tcd = NULL; /* TCD component */
7875 j2k->cstr_info = cstr_info;
7878 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7879 cstr_info->image_w = image->x1 - image->x0;
7880 cstr_info->image_h = image->y1 - image->y0;
7881 cstr_info->prog = (&cp->tcps[0])->prg;
7882 cstr_info->tw = cp->tw;
7883 cstr_info->th = cp->th;
7884 cstr_info->tile_x = cp->tdx; /* new version parser */
7885 cstr_info->tile_y = cp->tdy; /* new version parser */
7886 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7887 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7888 cstr_info->numcomps = image->numcomps;
7889 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7890 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7891 for (compno=0; compno < image->numcomps; compno++) {
7892 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7894 cstr_info->D_max = 0.0; /* ADD Marcela */
7895 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7896 cstr_info->maxmarknum = 100;
7897 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7898 cstr_info->marknum = 0;
7908 for (compno = 1; compno < image->numcomps; compno++) {
7909 j2k_write_coc(j2k, compno);
7910 j2k_write_qcc(j2k, compno);
7914 for (compno = 0; compno < image->numcomps; compno++) {
7915 opj_tcp_t *tcp = &cp->tcps[0];
7916 if (tcp->tccps[compno].roishift)
7917 j2k_write_rgn(j2k, compno, 0);
7919 if (cp->comment != NULL) {
7923 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7927 if (cp->cinema == CINEMA4K_24) {
7932 /* uncomment only for testing JPSEC marker writing */
7933 /* j2k_write_sec(j2k); */
7937 cstr_info->main_head_end = cio_tell(cio) - 1;
7940 /**** Main Header ENDS here ***/
7942 /* create the tile encoder */
7943 tcd = tcd_create(j2k->cinfo);
7945 /* encode each tile */
7946 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7950 int acc_pack_num = 0;
7954 opj_tcp_t *tcp = &cp->tcps[tileno];
7955 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7957 j2k->curtileno = tileno;
7958 j2k->cur_tp_num = 0;
7959 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7960 /* initialisation before tile encoding */
7962 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7964 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7969 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7970 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7971 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7972 cstr_info->tile[j2k->curtileno].marknum = 0;
7976 for(pino = 0; pino <= tcp->numpocs; pino++) {
7980 /*Get number of tile parts*/
7981 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7982 tcd->tp_pos = cp->tp_pos;
7984 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7985 j2k->tp_num = tilepartno;
7988 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7989 cio_tell(cio) + j2k->pos_correction;
7993 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7994 for (compno = 1; compno < image->numcomps; compno++) {
7995 j2k_write_coc(j2k, compno);
7996 j2k_write_qcc(j2k, compno);
7998 if (cp->tcps[tileno].numpocs) {
8005 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8006 cio_tell(cio) + j2k->pos_correction + 1;
8009 j2k_write_sod(j2k, tcd);
8013 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8014 cio_tell(cio) + j2k->pos_correction - 1;
8015 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8017 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8018 cstr_info->packno - acc_pack_num;
8019 acc_pack_num = cstr_info->packno;
8027 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8032 if (tile->PPT) { // BAD PPT !!!
8035 PPT_file=fopen("PPT","rb");
8036 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8037 for (i=0;i<tile->len_ppt;i++) {
8039 fread(&elmt, 1, 1, PPT_file);
8040 fwrite(&elmt,1,1,f);
8049 /* destroy the tile encoder */
8050 tcd_free_encode(tcd);
8053 opj_free(j2k->cur_totnum_tp);
8058 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8060 /* The following adjustment is done to adjust the codestream size */
8061 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8062 /* the first bunch of bytes is not in the codestream */
8063 cstr_info->codestream_size -= cstr_info->main_head_start;
8069 preparation of JPWL marker segments
8073 /* encode according to JPWL */
8074 jpwl_encode(j2k, cio, image);
8077 #endif /* USE_JPWL */
8082 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8084 assert(cstr_info != 00);
8086 /* expand the list? */
8087 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8088 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8089 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8092 /* add the marker */
8093 cstr_info->marker[cstr_info->marknum].type = type;
8094 cstr_info->marker[cstr_info->marknum].pos = pos;
8095 cstr_info->marker[cstr_info->marknum].len = len;
8096 cstr_info->marknum++;
8100 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8102 assert(cstr_index != 00);
8104 /* expand the list? */
8105 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8106 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8107 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8110 /* add the marker */
8111 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8112 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8113 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8114 cstr_index->marknum++;
8118 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8120 opj_marker_info_t *marker;
8122 assert(cstr_info != 00);
8124 /* expand the list? */
8125 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8126 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8127 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8130 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8132 /* add the marker */
8133 marker->type = type;
8136 cstr_info->tile[tileno].marknum++;
8139 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)
8141 assert(cstr_index != 00);
8142 assert(cstr_index->tile_index != 00);
8144 /* expand the list? */
8145 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8146 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8147 cstr_index->tile_index[tileno].marker =
8148 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8149 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8152 /* add the marker */
8153 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8154 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8155 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8156 cstr_index->tile_index[tileno].marknum++;
8158 if (type == J2K_MS_SOT) {
8159 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8161 if (cstr_index->tile_index[tileno].tp_index)
8162 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8169 * -----------------------------------------------------------------------
8170 * -----------------------------------------------------------------------
8171 * -----------------------------------------------------------------------
8175 * Ends the decompression procedures and possibiliy add data to be read after the
8178 opj_bool j2k_end_decompress(
8179 opj_j2k_v2_t *p_j2k,
8180 opj_stream_private_t *p_stream,
8181 opj_event_mgr_t * p_manager)
8190 * Reads a jpeg2000 codestream header structure.
8193 * @param p_stream the stream to read data from.
8194 * @param p_j2k the jpeg2000 codec.
8195 * @param p_manager the user event manager.
8197 * @return true if the box is valid.
8199 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8200 opj_j2k_v2_t* p_j2k,
8201 opj_image_t** p_image,
8202 struct opj_event_mgr* p_manager )
8205 assert(p_j2k != 00);
8206 assert(p_stream != 00);
8207 assert(p_manager != 00);
8209 /* create an empty image header */
8210 p_j2k->m_private_image = opj_image_create0();
8211 if (! p_j2k->m_private_image) {
8215 /* customization of the validation */
8216 j2k_setup_decoding_validation(p_j2k);
8218 /* validation of the parameters codec */
8219 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8220 opj_image_destroy(p_j2k->m_private_image);
8221 p_j2k->m_private_image = NULL;
8225 /* customization of the encoding */
8226 j2k_setup_header_reading(p_j2k);
8229 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8230 opj_image_destroy(p_j2k->m_private_image);
8231 p_j2k->m_private_image = NULL;
8235 *p_image = opj_image_create0();
8240 /* Copy codestream image information to the output image */
8241 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8243 /*Allocate and initialize some elements of codestrem index*/
8244 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8252 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8254 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8257 assert(p_j2k != 00);
8259 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8261 /* DEVELOPER CORNER, add your custom procedures */
8262 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8267 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8268 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8270 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8273 assert(p_j2k != 00);
8275 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8276 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8277 /* DEVELOPER CORNER, add your custom validation procedure */
8283 * The mct encoding validation procedure.
8285 * @param p_j2k the jpeg2000 codec to validate.
8286 * @param p_stream the input stream to validate.
8287 * @param p_manager the user event manager.
8289 * @return true if the parameters are correct.
8291 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8292 opj_stream_private_t *p_stream,
8293 opj_event_mgr_t * p_manager )
8295 opj_bool l_is_valid = OPJ_TRUE;
8299 assert(p_j2k != 00);
8300 assert(p_stream != 00);
8301 assert(p_manager != 00);
8303 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8304 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8305 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8307 for (i=0;i<l_nb_tiles;++i) {
8308 if (l_tcp->mct == 2) {
8309 opj_tccp_t * l_tccp = l_tcp->tccps;
8310 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8312 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8313 l_is_valid &= ! (l_tccp->qmfbid & 1);
8324 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8327 OPJ_UINT32 l_indix = 1;
8328 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8329 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8330 OPJ_UINT32 l_mct_size,l_nb_elem;
8331 OPJ_FLOAT32 * l_data, * l_current_data;
8332 opj_tccp_t * l_tccp;
8335 assert(p_tcp != 00);
8337 if (p_tcp->mct != 2) {
8341 if (p_tcp->m_mct_decoding_matrix) {
8342 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8343 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8345 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));
8346 if (! p_tcp->m_mct_records) {
8349 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8351 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8353 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8355 if (l_mct_deco_data->m_data) {
8356 opj_free(l_mct_deco_data->m_data);
8357 l_mct_deco_data->m_data = 00;
8360 l_mct_deco_data->m_index = l_indix++;
8361 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8362 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8363 l_nb_elem = p_image->numcomps * p_image->numcomps;
8364 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8365 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8367 if (! l_mct_deco_data->m_data) {
8371 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);
8373 l_mct_deco_data->m_data_size = l_mct_size;
8374 ++p_tcp->m_nb_mct_records;
8377 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8378 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8379 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));
8381 if (! p_tcp->m_mct_records) {
8385 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8386 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8388 if (l_mct_deco_data) {
8389 l_mct_deco_data = l_mct_offset_data - 1;
8393 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8395 if (l_mct_offset_data->m_data) {
8396 opj_free(l_mct_offset_data->m_data);
8397 l_mct_offset_data->m_data = 00;
8400 l_mct_offset_data->m_index = l_indix++;
8401 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8402 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8403 l_nb_elem = p_image->numcomps;
8404 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8405 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8407 if (! l_mct_offset_data->m_data) {
8411 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8413 opj_free(l_mct_offset_data->m_data);
8414 l_mct_offset_data->m_data = 00;
8418 l_tccp = p_tcp->tccps;
8419 l_current_data = l_data;
8421 for (i=0;i<l_nb_elem;++i) {
8422 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8426 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8430 l_mct_offset_data->m_data_size = l_mct_size;
8432 ++p_tcp->m_nb_mct_records;
8434 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8435 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8436 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8437 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8439 if (! p_tcp->m_mcc_records) {
8442 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8443 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));
8447 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8448 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8449 l_mcc_data->m_is_irreversible = 1;
8450 l_mcc_data->m_nb_comps = p_image->numcomps;
8451 l_mcc_data->m_index = l_indix++;
8452 l_mcc_data->m_offset_array = l_mct_offset_data;
8453 ++p_tcp->m_nb_mcc_records;
8459 * Builds the cp decoder parameters to use to decode tile.
8461 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8462 opj_stream_private_t *p_stream,
8463 opj_event_mgr_t * p_manager )
8465 /* add here initialization of cp
8466 copy paste of setup_decoder */
8474 * Builds the cp encoder parameters to use to encode tile.
8476 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8477 opj_stream_private_t *p_stream,
8478 opj_event_mgr_t * p_manager )
8480 /* add here initialization of cp
8481 copy paste of setup_encoder */
8489 * The default encoding validation procedure without any extension.
8491 * @param p_j2k the jpeg2000 codec to validate.
8492 * @param p_stream the input stream to validate.
8493 * @param p_manager the user event manager.
8495 * @return true if the parameters are correct.
8497 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8498 opj_stream_private_t *p_stream,
8499 opj_event_mgr_t * p_manager )
8501 opj_bool l_is_valid = OPJ_TRUE;
8504 assert(p_j2k != 00);
8505 assert(p_stream != 00);
8506 assert(p_manager != 00);
8508 /* STATE checking */
8509 /* make sure the state is at 0 */
8510 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8512 /* POINTER validation */
8513 /* make sure a p_j2k codec is present */
8514 l_is_valid &= (p_j2k->m_procedure_list != 00);
8515 /* make sure a validation list is present */
8516 l_is_valid &= (p_j2k->m_validation_list != 00);
8518 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8519 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8523 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8524 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8528 /* PARAMETER VALIDATION */
8533 * The default decoding validation procedure without any extension.
8535 * @param p_j2k the jpeg2000 codec to validate.
8536 * @param p_stream the input stream to validate.
8537 * @param p_manager the user event manager.
8539 * @return true if the parameters are correct.
8541 opj_bool j2k_decoding_validation (
8542 opj_j2k_v2_t *p_j2k,
8543 opj_stream_private_t *p_stream,
8544 opj_event_mgr_t * p_manager
8547 opj_bool l_is_valid = OPJ_TRUE;
8550 assert(p_j2k != 00);
8551 assert(p_stream != 00);
8552 assert(p_manager != 00);
8555 /* STATE checking */
8556 /* make sure the state is at 0 */
8558 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8560 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8562 /* POINTER validation */
8563 /* make sure a p_j2k codec is present */
8564 /* make sure a procedure list is present */
8565 l_is_valid &= (p_j2k->m_procedure_list != 00);
8566 /* make sure a validation list is present */
8567 l_is_valid &= (p_j2k->m_validation_list != 00);
8569 /* PARAMETER VALIDATION */
8573 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8574 struct opj_stream_private *p_stream,
8575 struct opj_event_mgr * p_manager)
8577 OPJ_UINT32 l_current_marker;
8578 OPJ_UINT32 l_marker_size;
8579 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8582 assert(p_stream != 00);
8583 assert(p_j2k != 00);
8584 assert(p_manager != 00);
8586 /* We enter in the main header */
8587 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8589 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8590 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8591 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8595 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8596 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8597 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8601 /* Read 2 bytes as the new marker ID */
8602 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8604 /* Try to read until the SOT is detected */
8605 while (l_current_marker != J2K_MS_SOT) {
8607 /* Check if the current marker ID is valid */
8608 if (l_current_marker < 0xff00) {
8609 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8613 /* Get the marker handler from the marker ID */
8614 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8616 /* Manage case where marker is unknown */
8617 if (l_marker_handler->id == J2K_MS_UNK) {
8618 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8619 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8623 if (l_current_marker == J2K_MS_SOT)
8624 break; /* SOT marker is detected main header is completely read */
8625 else /* Get the marker handler from the marker ID */
8626 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8629 /* Check if the marker is known and if it is the right place to find it */
8630 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8631 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8635 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8636 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8637 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8641 /* read 2 bytes as the marker size */
8642 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8643 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8645 /* Check if the marker size is compatible with the header data size */
8646 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8647 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8648 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8649 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8652 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8655 /* Try to read the rest of the marker segment 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,l_marker_size,p_manager) != l_marker_size) {
8657 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8661 /* Read the marker segment with the correct marker handler */
8662 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8663 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8667 /* Add the marker to the codestream index*/
8668 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8669 l_marker_handler->id,
8670 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8671 l_marker_size + 4 );
8673 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8674 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8675 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8679 /* read 2 bytes as the new marker ID */
8680 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8683 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8685 /* Position of the last element if the main header */
8686 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8688 /* Next step: read a tile-part header */
8689 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8695 * Excutes the given procedures on the given codec.
8697 * @param p_procedure_list the list of procedures to execute
8698 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8699 * @param p_stream the stream to execute the procedures on.
8700 * @param p_manager the user manager.
8702 * @return true if all the procedures were successfully executed.
8704 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8705 opj_procedure_list_t * p_procedure_list,
8706 opj_stream_private_t *p_stream,
8707 opj_event_mgr_t * p_manager )
8709 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8710 opj_bool l_result = OPJ_TRUE;
8711 OPJ_UINT32 l_nb_proc, i;
8714 assert(p_procedure_list != 00);
8715 assert(p_j2k != 00);
8716 assert(p_stream != 00);
8717 assert(p_manager != 00);
8720 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8721 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);
8723 for (i=0;i<l_nb_proc;++i) {
8724 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8728 /* and clear the procedure list at the end.*/
8729 opj_procedure_list_clear(p_procedure_list);
8734 opj_bool j2k_copy_default_tcp_and_create_tcd
8736 opj_j2k_v2_t * p_j2k,
8737 opj_stream_private_t *p_stream,
8738 opj_event_mgr_t * p_manager
8741 opj_tcp_v2_t * l_tcp = 00;
8742 opj_tcp_v2_t * l_default_tcp = 00;
8743 OPJ_UINT32 l_nb_tiles;
8745 opj_tccp_t *l_current_tccp = 00;
8746 OPJ_UINT32 l_tccp_size;
8747 OPJ_UINT32 l_mct_size;
8748 opj_image_t * l_image;
8749 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8750 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8751 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8752 OPJ_UINT32 l_offset;
8755 assert(p_j2k != 00);
8756 assert(p_stream != 00);
8757 assert(p_manager != 00);
8759 l_image = p_j2k->m_private_image;
8760 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8761 l_tcp = p_j2k->m_cp.tcps;
8762 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8763 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8764 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8767 for (i=0; i<l_nb_tiles; ++i) {
8768 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8769 l_current_tccp = l_tcp->tccps;
8770 /*Copy default coding parameters into the current tile coding parameters*/
8771 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8772 /* Initialize some values of the current tile coding parameters*/
8774 l_tcp->ppt_data = 00;
8775 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8776 l_tcp->tccps = l_current_tccp;
8778 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8779 if (l_default_tcp->m_mct_decoding_matrix) {
8780 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8781 if (! l_tcp->m_mct_decoding_matrix ) {
8784 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8787 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8788 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8789 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8790 if (! l_tcp->m_mct_records) {
8793 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8795 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8796 l_src_mct_rec = l_default_tcp->m_mct_records;
8797 l_dest_mct_rec = l_tcp->m_mct_records;
8799 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8801 if (l_src_mct_rec->m_data) {
8803 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8804 if(! l_dest_mct_rec->m_data) {
8807 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8814 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8815 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8816 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8817 if (! l_tcp->m_mcc_records) {
8820 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8822 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8823 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8824 l_dest_mcc_rec = l_tcp->m_mcc_records;
8826 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8828 if (l_src_mcc_rec->m_decorrelation_array) {
8829 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8830 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8833 if (l_src_mcc_rec->m_offset_array) {
8834 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8835 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8842 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8843 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8845 /* Move to next tile cp*/
8849 /* Create the current tile decoder*/
8850 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8851 if (! p_j2k->m_tcd ) {
8855 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8856 tcd_destroy_v2(p_j2k->m_tcd);
8858 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8866 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8867 * with the marker value.
8868 * @param p_id Marker value to look up
8870 * @return the handler associated with the id.
8872 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8874 const opj_dec_memory_marker_handler_t *e;
8875 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8876 if (e->id == p_id) {
8877 break; /* we find a handler corresponding to the marker ID*/
8885 * Destroys a jpeg2000 codec.
8887 * @param p_j2k the jpeg20000 structure to destroy.
8889 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8895 if (p_j2k->m_is_decoder) {
8897 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8898 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8899 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8900 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8903 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8904 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8905 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8906 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8911 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8912 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8913 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8916 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8917 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8918 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8919 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8922 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8923 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8924 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8925 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8929 tcd_destroy_v2(p_j2k->m_tcd);
8931 j2k_cp_destroy(&(p_j2k->m_cp));
8932 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8934 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8935 p_j2k->m_procedure_list = 00;
8937 opj_procedure_list_destroy(p_j2k->m_validation_list);
8938 p_j2k->m_procedure_list = 00;
8940 j2k_destroy_cstr_index(p_j2k->cstr_index);
8941 p_j2k->cstr_index = NULL;
8943 opj_image_destroy(p_j2k->m_private_image);
8944 p_j2k->m_private_image = NULL;
8946 opj_image_destroy(p_j2k->m_output_image);
8947 p_j2k->m_output_image = NULL;
8952 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8956 if (p_cstr_ind->marker) {
8957 opj_free(p_cstr_ind->marker);
8958 p_cstr_ind->marker = NULL;
8961 if (p_cstr_ind->tile_index) {
8962 OPJ_UINT32 it_tile = 0;
8964 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8966 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8967 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8968 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8971 if(p_cstr_ind->tile_index[it_tile].tp_index){
8972 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8973 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8976 if(p_cstr_ind->tile_index[it_tile].marker){
8977 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8978 p_cstr_ind->tile_index[it_tile].marker = NULL;
8983 opj_free( p_cstr_ind->tile_index);
8984 p_cstr_ind->tile_index = NULL;
8987 opj_free(p_cstr_ind);
8994 * Destroys a tile coding parameter structure.
8996 * @param p_tcp the tile coding parameter to destroy.
8998 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9004 if (p_tcp->ppt_buffer != 00) {
9005 opj_free(p_tcp->ppt_buffer);
9006 p_tcp->ppt_buffer = 00;
9009 if (p_tcp->tccps != 00) {
9010 opj_free(p_tcp->tccps);
9014 if (p_tcp->m_mct_coding_matrix != 00) {
9015 opj_free(p_tcp->m_mct_coding_matrix);
9016 p_tcp->m_mct_coding_matrix = 00;
9019 if (p_tcp->m_mct_decoding_matrix != 00) {
9020 opj_free(p_tcp->m_mct_decoding_matrix);
9021 p_tcp->m_mct_decoding_matrix = 00;
9024 if (p_tcp->m_mcc_records) {
9025 opj_free(p_tcp->m_mcc_records);
9026 p_tcp->m_mcc_records = 00;
9027 p_tcp->m_nb_max_mcc_records = 0;
9028 p_tcp->m_nb_mcc_records = 0;
9031 if (p_tcp->m_mct_records) {
9032 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9035 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9036 if (l_mct_data->m_data) {
9037 opj_free(l_mct_data->m_data);
9038 l_mct_data->m_data = 00;
9044 opj_free(p_tcp->m_mct_records);
9045 p_tcp->m_mct_records = 00;
9048 if (p_tcp->mct_norms != 00) {
9049 opj_free(p_tcp->mct_norms);
9050 p_tcp->mct_norms = 00;
9053 j2k_tcp_data_destroy(p_tcp);
9058 * Destroys the data inside a tile coding parameter structure.
9060 * @param p_tcp the tile coding parameter which contain data to destroy.
9062 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9064 if (p_tcp->m_data) {
9065 opj_free(p_tcp->m_data);
9066 p_tcp->m_data = NULL;
9067 p_tcp->m_data_size = 0;
9072 * Destroys a coding parameter structure.
9074 * @param p_cp the coding parameter to destroy.
9076 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9078 OPJ_UINT32 l_nb_tiles;
9079 opj_tcp_v2_t * l_current_tile = 00;
9090 l_current_tile = p_cp->tcps;
9091 l_nb_tiles = p_cp->th * p_cp->tw;
9094 (i = 0; i < l_nb_tiles; ++i)
9096 j2k_tcp_destroy(l_current_tile);
9099 opj_free(p_cp->tcps);
9103 (p_cp->ppm_buffer != 00)
9105 opj_free(p_cp->ppm_buffer);
9106 p_cp->ppm_buffer = 00;
9109 (p_cp->comment != 00)
9111 opj_free(p_cp->comment);
9115 (! p_cp->m_is_decoder)
9118 (p_cp->m_specific_param.m_enc.m_matrice)
9120 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9121 p_cp->m_specific_param.m_enc.m_matrice = 00;
9129 * Reads a tile header.
9130 * @param p_j2k the jpeg2000 codec.
9131 * @param p_stream the stream to write data to.
9132 * @param p_manager the user event manager.
9134 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9135 OPJ_UINT32 * p_tile_index,
9136 OPJ_UINT32 * p_data_size,
9137 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9138 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9139 OPJ_UINT32 * p_nb_comps,
9141 opj_stream_private_t *p_stream,
9142 opj_event_mgr_t * p_manager )
9144 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9145 OPJ_UINT32 l_marker_size;
9146 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9147 opj_tcp_v2_t * l_tcp = NULL;
9148 OPJ_UINT32 l_nb_tiles;
9151 assert(p_stream != 00);
9152 assert(p_j2k != 00);
9153 assert(p_manager != 00);
9155 /* Reach the End Of Codestream ?*/
9156 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9157 l_current_marker = J2K_MS_EOC;
9159 /* We need to encounter a SOT marker (a new tile-part header) */
9160 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9164 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9165 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9167 /* Try to read until the Start Of Data is detected */
9168 while (l_current_marker != J2K_MS_SOD) {
9170 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9171 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9172 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9176 /* Read 2 bytes from the buffer as the marker size */
9177 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9179 /* Why this condition? FIXME */
9180 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9181 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9183 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9185 /* Get the marker handler from the marker ID */
9186 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9188 /* Check if the marker is known and if it is the right place to find it */
9189 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9190 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9193 /* FIXME manage case of unknown marker as in the main header ? */
9195 /* Check if the marker size is compatible with the header data size */
9196 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9197 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9198 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9199 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9202 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9205 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9206 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) {
9207 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9211 /* Read the marker segment with the correct marker handler */
9212 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9213 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9217 /* Add the marker to the codestream index*/
9218 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9220 l_marker_handler->id,
9221 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9222 l_marker_size + 4 );
9224 /* Keep the position of the last SOT marker read */
9225 if ( l_marker_handler->id == J2K_MS_SOT ) {
9226 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9227 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9229 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9234 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9235 /* Skip the rest of the tile part header*/
9236 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) {
9237 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9240 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9243 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9244 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9245 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9248 /* Read 2 bytes from the buffer as the new marker ID */
9249 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9253 /* If we didn't skip data before, we need to read the SOD marker*/
9254 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9255 /* Try to read the SOD marker and skip data ? FIXME */
9256 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
9262 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9263 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9264 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9265 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9269 /* Read 2 bytes from buffer as the new marker ID */
9270 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9274 /* Indicate we will try to read a new tile-part header*/
9275 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9276 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9277 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9279 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9280 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9281 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9285 /* Read 2 bytes from buffer as the new marker ID */
9286 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9290 /* Current marker is the EOC marker ?*/
9291 if (l_current_marker == J2K_MS_EOC) {
9292 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9293 p_j2k->m_current_tile_number = 0;
9294 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9299 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9300 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9301 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9303 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9304 ++p_j2k->m_current_tile_number;
9308 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9309 *p_go_on = OPJ_FALSE;
9315 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9316 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9320 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9321 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9323 *p_tile_index = p_j2k->m_current_tile_number;
9324 *p_go_on = OPJ_TRUE;
9325 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9326 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9327 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9328 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9329 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9330 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9332 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9338 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9339 OPJ_UINT32 p_tile_index,
9341 OPJ_UINT32 p_data_size,
9342 opj_stream_private_t *p_stream,
9343 opj_event_mgr_t * p_manager )
9345 OPJ_UINT32 l_current_marker;
9346 OPJ_BYTE l_data [2];
9347 opj_tcp_v2_t * l_tcp;
9350 assert(p_stream != 00);
9351 assert(p_j2k != 00);
9352 assert(p_manager != 00);
9354 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9355 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9359 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9360 if (! l_tcp->m_data) {
9361 j2k_tcp_destroy(l_tcp);
9365 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9369 p_j2k->cstr_index) ) {
9370 j2k_tcp_destroy(l_tcp);
9371 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9375 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9379 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9380 * we destroy just the data which will be re-read in read_tile_header*/
9381 /*j2k_tcp_destroy(l_tcp);
9382 p_j2k->m_tcd->tcp = 0;*/
9383 j2k_tcp_data_destroy(l_tcp);
9385 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9386 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9388 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9389 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9390 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9394 opj_read_bytes(l_data,&l_current_marker,2);
9396 if (l_current_marker == J2K_MS_EOC) {
9397 p_j2k->m_current_tile_number = 0;
9398 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9400 else if (l_current_marker != J2K_MS_SOT)
9402 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9411 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9413 OPJ_UINT32 i,j,k = 0;
9414 OPJ_UINT32 l_width_src,l_height_src;
9415 OPJ_UINT32 l_width_dest,l_height_dest;
9416 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9417 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9418 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9419 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9420 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9422 opj_image_comp_t * l_img_comp_src = 00;
9423 opj_image_comp_t * l_img_comp_dest = 00;
9425 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9426 opj_image_t * l_image_src = 00;
9427 OPJ_UINT32 l_size_comp, l_remaining;
9428 OPJ_INT32 * l_dest_ptr;
9429 opj_tcd_resolution_v2_t* l_res= 00;
9431 l_tilec = p_tcd->tcd_image->tiles->comps;
9432 l_image_src = p_tcd->image;
9433 l_img_comp_src = l_image_src->comps;
9435 l_img_comp_dest = p_output_image->comps;
9437 for (i=0; i<l_image_src->numcomps; i++) {
9439 /* Allocate output component buffer if necessary */
9440 if (!l_img_comp_dest->data) {
9442 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9443 if (! l_img_comp_dest->data) {
9448 /* Copy info from decoded comp image to output image */
9449 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9452 /* Compute the precision of the output buffer */
9453 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9454 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9455 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9461 if (l_size_comp == 3) {
9466 /* Current tile component size*/
9468 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9469 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9472 l_width_src = (l_res->x1 - l_res->x0);
9473 l_height_src = (l_res->y1 - l_res->y0);
9475 /* Border of the current output component*/
9476 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9477 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9478 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9479 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9482 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9483 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9487 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9488 * of the input buffer (decoded tile component) which will be move
9489 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9490 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9491 * by this input area.
9493 assert( l_res->x0 >= 0);
9494 assert( l_res->x1 >= 0);
9495 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9496 l_start_x_dest = l_res->x0 - l_x0_dest;
9497 l_offset_x0_src = 0;
9499 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9500 l_width_dest = l_width_src;
9501 l_offset_x1_src = 0;
9504 l_width_dest = l_x1_dest - l_res->x0 ;
9505 l_offset_x1_src = l_width_src - l_width_dest;
9509 l_start_x_dest = 0 ;
9510 l_offset_x0_src = l_x0_dest - l_res->x0;
9512 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9513 l_width_dest = l_width_src - l_offset_x0_src;
9514 l_offset_x1_src = 0;
9517 l_width_dest = l_img_comp_dest->w ;
9518 l_offset_x1_src = l_res->x1 - l_x1_dest;
9522 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9523 l_start_y_dest = l_res->y0 - l_y0_dest;
9524 l_offset_y0_src = 0;
9526 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9527 l_height_dest = l_height_src;
9528 l_offset_y1_src = 0;
9531 l_height_dest = l_y1_dest - l_res->y0 ;
9532 l_offset_y1_src = l_height_src - l_height_dest;
9536 l_start_y_dest = 0 ;
9537 l_offset_y0_src = l_y0_dest - l_res->y0;
9539 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9540 l_height_dest = l_height_src - l_offset_y0_src;
9541 l_offset_y1_src = 0;
9544 l_height_dest = l_img_comp_dest->h ;
9545 l_offset_y1_src = l_res->y1 - l_y1_dest;
9549 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9554 /* Compute the input buffer offset */
9555 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9556 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9557 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9559 /* Compute the output buffer offset */
9560 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9561 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9563 /* Move the output buffer to the first place where we will write*/
9564 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9567 fprintf(stdout, "COMPO[%d]:\n",i);
9568 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9569 "\t tile offset:%d, %d, %d, %d\n"
9570 "\t buffer offset: %d; %d, %d\n",
9571 l_res->x0, l_res->y0, l_width_src, l_height_src,
9572 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9573 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9575 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9576 "\t start offset: %d, line offset= %d\n",
9577 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9581 switch (l_size_comp) {
9584 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9585 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9587 if (l_img_comp_src->sgnd) {
9588 for (j = 0 ; j < l_height_dest ; ++j) {
9589 for ( k = 0 ; k < l_width_dest ; ++k) {
9590 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9593 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9594 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9598 for ( j = 0 ; j < l_height_dest ; ++j ) {
9599 for ( k = 0 ; k < l_width_dest ; ++k) {
9600 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9603 l_dest_ptr+= l_line_offset_dest;
9604 l_src_ptr += l_line_offset_src;
9608 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9609 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9614 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9615 l_src_ptr += l_start_offset_src;
9617 if (l_img_comp_src->sgnd) {
9618 for (j=0;j<l_height_dest;++j) {
9619 for (k=0;k<l_width_dest;++k) {
9620 *(l_dest_ptr++) = *(l_src_ptr++);
9623 l_dest_ptr+= l_line_offset_dest;
9624 l_src_ptr += l_line_offset_src ;
9628 for (j=0;j<l_height_dest;++j) {
9629 for (k=0;k<l_width_dest;++k) {
9630 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9633 l_dest_ptr+= l_line_offset_dest;
9634 l_src_ptr += l_line_offset_src ;
9638 l_src_ptr += l_end_offset_src;
9639 p_data = (OPJ_BYTE*) l_src_ptr;
9644 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9645 l_src_ptr += l_start_offset_src;
9647 for (j=0;j<l_height_dest;++j) {
9648 for (k=0;k<l_width_dest;++k) {
9649 *(l_dest_ptr++) = (*(l_src_ptr++));
9652 l_dest_ptr+= l_line_offset_dest;
9653 l_src_ptr += l_line_offset_src ;
9656 l_src_ptr += l_end_offset_src;
9657 p_data = (OPJ_BYTE*) l_src_ptr;
9671 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9673 * @param p_j2k the jpeg2000 codec.
9674 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9675 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9676 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9677 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9678 * @param p_manager the user event manager
9680 * @return true if the area could be set.
9682 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9683 opj_image_t* p_image,
9684 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9685 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9686 struct opj_event_mgr * p_manager )
9688 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9689 opj_image_t * l_image = p_j2k->m_private_image;
9692 OPJ_INT32 l_comp_x1, l_comp_y1;
9693 opj_image_comp_t* l_img_comp = NULL;
9695 /* Check if we are read the main header */
9696 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9697 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9701 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9702 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9704 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9705 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9706 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9707 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9713 /* Check if the positions provided by the user are correct */
9716 assert(p_start_x >= 0 );
9717 assert(p_start_y >= 0 );
9719 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9720 opj_event_msg_v2(p_manager, EVT_ERROR,
9721 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9722 p_start_x, l_image->x1);
9725 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9726 opj_event_msg_v2(p_manager, EVT_WARNING,
9727 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9728 p_start_x, l_image->x0);
9729 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9730 p_image->x0 = l_image->x0;
9733 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9734 p_image->x0 = p_start_x;
9738 if ((OPJ_UINT32)p_start_y > l_image->y1){
9739 opj_event_msg_v2(p_manager, EVT_ERROR,
9740 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9741 p_start_y, l_image->y1);
9744 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9745 opj_event_msg_v2(p_manager, EVT_WARNING,
9746 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9747 p_start_y, l_image->y0);
9748 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9749 p_image->y0 = l_image->y0;
9752 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9753 p_image->y0 = p_start_y;
9757 assert((OPJ_UINT32)p_end_x > 0);
9758 assert((OPJ_UINT32)p_end_y > 0);
9759 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9760 opj_event_msg_v2(p_manager, EVT_ERROR,
9761 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9762 p_end_x, l_image->x0);
9765 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9766 opj_event_msg_v2(p_manager, EVT_WARNING,
9767 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9768 p_end_x, l_image->x1);
9769 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9770 p_image->x1 = l_image->x1;
9773 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9774 p_image->x1 = p_end_x;
9778 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9779 opj_event_msg_v2(p_manager, EVT_ERROR,
9780 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9781 p_end_y, l_image->y0);
9784 if ((OPJ_UINT32)p_end_y > l_image->y1){
9785 opj_event_msg_v2(p_manager, EVT_WARNING,
9786 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9787 p_end_y, l_image->y1);
9788 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9789 p_image->y1 = l_image->y1;
9792 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9793 p_image->y1 = p_end_y;
9797 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9799 l_img_comp = p_image->comps;
9800 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9804 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9805 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9806 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9807 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9809 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9810 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9812 opj_event_msg_v2(p_manager, EVT_ERROR,
9813 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9817 l_img_comp->w = l_w;
9819 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9820 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9822 opj_event_msg_v2(p_manager, EVT_ERROR,
9823 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9827 l_img_comp->h = l_h;
9832 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9833 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9840 /* ----------------------------------------------------------------------- */
9841 /* J2K / JPT decoder interface */
9842 /* ----------------------------------------------------------------------- */
9844 * Creates a J2K decompression structure.
9846 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9848 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9850 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9854 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9856 l_j2k->m_is_decoder = 1;
9857 l_j2k->m_cp.m_is_decoder = 1;
9859 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9860 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9864 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9866 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9867 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9872 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9874 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9876 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9878 /* codestream index creation */
9879 l_j2k->cstr_index = j2k_create_cstr_index();
9881 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9882 if (!l_j2k->cstr_index){
9887 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9890 /* validation list creation */
9891 l_j2k->m_validation_list = opj_procedure_list_create();
9892 if (! l_j2k->m_validation_list) {
9897 /* execution list creation */
9898 l_j2k->m_procedure_list = opj_procedure_list_create();
9899 if (! l_j2k->m_procedure_list) {
9908 opj_codestream_index_t* j2k_create_cstr_index(void)
9910 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9911 opj_calloc(1,sizeof(opj_codestream_index_t));
9915 cstr_index->maxmarknum = 100;
9916 cstr_index->marknum = 0;
9917 cstr_index->marker = (opj_marker_info_t*)
9918 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9919 if (!cstr_index-> marker)
9922 cstr_index->tile_index = NULL;
9929 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9931 * @param p_tile_no the tile index.
9932 * @param p_comp_no the component being outputted.
9933 * @param p_j2k the J2K codec.
9935 * @return the number of bytes taken by the SPCod element.
9937 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9938 OPJ_UINT32 p_tile_no,
9939 OPJ_UINT32 p_comp_no )
9941 opj_cp_v2_t *l_cp = 00;
9942 opj_tcp_v2_t *l_tcp = 00;
9943 opj_tccp_t *l_tccp = 00;
9946 assert(p_j2k != 00);
9948 l_cp = &(p_j2k->m_cp);
9949 l_tcp = &l_cp->tcps[p_tile_no];
9950 l_tccp = &l_tcp->tccps[p_comp_no];
9952 /* preconditions again */
9953 assert(p_tile_no < (l_cp->tw * l_cp->th));
9954 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9956 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9957 return 5 + l_tccp->numresolutions;
9965 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9967 * @param p_comp_no the component number to output.
9968 * @param p_stream the stream to write data to.
9969 * @param p_j2k J2K codec.
9970 * @param p_manager the user event manager.
9973 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9974 OPJ_UINT32 p_tile_no,
9975 OPJ_UINT32 p_comp_no,
9977 OPJ_UINT32 * p_header_size,
9978 struct opj_event_mgr * p_manager )
9981 opj_cp_v2_t *l_cp = 00;
9982 opj_tcp_v2_t *l_tcp = 00;
9983 opj_tccp_t *l_tccp = 00;
9986 assert(p_j2k != 00);
9987 assert(p_header_size != 00);
9988 assert(p_manager != 00);
9989 assert(p_data != 00);
9991 l_cp = &(p_j2k->m_cp);
9992 l_tcp = &l_cp->tcps[p_tile_no];
9993 l_tccp = &l_tcp->tccps[p_comp_no];
9995 /* preconditions again */
9996 assert(p_tile_no < (l_cp->tw * l_cp->th));
9997 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9999 if (*p_header_size < 5) {
10000 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10004 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
10007 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
10010 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
10013 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
10016 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
10019 *p_header_size = *p_header_size - 5;
10021 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10023 if (*p_header_size < l_tccp->numresolutions) {
10024 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10028 for (i = 0; i < l_tccp->numresolutions; ++i) {
10029 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
10033 *p_header_size = *p_header_size - l_tccp->numresolutions;
10040 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10041 * @param p_header_data the data contained in the COM box.
10042 * @param p_j2k the jpeg2000 codec.
10043 * @param p_header_size the size of the data contained in the COM marker.
10044 * @param p_manager the user event manager.
10046 opj_bool j2k_read_SPCod_SPCoc(
10047 opj_j2k_v2_t *p_j2k,
10049 OPJ_BYTE * p_header_data,
10050 OPJ_UINT32 * p_header_size,
10051 struct opj_event_mgr * p_manager
10054 OPJ_UINT32 i, l_tmp;
10055 opj_cp_v2_t *l_cp = NULL;
10056 opj_tcp_v2_t *l_tcp = NULL;
10057 opj_tccp_t *l_tccp = NULL;
10058 OPJ_BYTE * l_current_ptr = NULL;
10060 /* preconditions */
10061 assert(p_j2k != 00);
10062 assert(p_manager != 00);
10063 assert(p_header_data != 00);
10065 l_cp = &(p_j2k->m_cp);
10066 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10067 &l_cp->tcps[p_j2k->m_current_tile_number] :
10068 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10070 /* precondition again */
10071 assert(compno < p_j2k->m_private_image->numcomps);
10073 l_tccp = &l_tcp->tccps[compno];
10074 l_current_ptr = p_header_data;
10076 /* make sure room is sufficient */
10077 if (*p_header_size < 5) {
10078 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10082 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10083 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10086 /* If user wants to remove more resolutions than the codestream contains, return error */
10087 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10088 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10089 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10090 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10094 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10096 l_tccp->cblkw += 2;
10098 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10100 l_tccp->cblkh += 2;
10102 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10105 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10108 *p_header_size = *p_header_size - 5;
10110 /* use custom precinct size ? */
10111 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10112 if (*p_header_size < l_tccp->numresolutions) {
10113 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10117 for (i = 0; i < l_tccp->numresolutions; ++i) {
10118 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10120 l_tccp->prcw[i] = l_tmp & 0xf;
10121 l_tccp->prch[i] = l_tmp >> 4;
10124 *p_header_size = *p_header_size - l_tccp->numresolutions;
10127 /* set default size for the precinct width and height */
10128 for (i = 0; i < l_tccp->numresolutions; ++i) {
10129 l_tccp->prcw[i] = 15;
10130 l_tccp->prch[i] = 15;
10134 #ifdef WIP_REMOVE_MSD
10136 if (p_j2k->cstr_info && compno == 0) {
10137 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10139 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10140 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10141 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10142 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10143 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10146 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10147 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10156 * Copies the tile component parameters of all the component from the first tile component.
10158 * @param p_j2k the J2k codec.
10160 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10164 opj_cp_v2_t *l_cp = NULL;
10165 opj_tcp_v2_t *l_tcp = NULL;
10166 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10167 OPJ_UINT32 l_prc_size;
10169 /* preconditions */
10170 assert(p_j2k != 00);
10172 l_cp = &(p_j2k->m_cp);
10173 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10174 &l_cp->tcps[p_j2k->m_current_tile_number] :
10175 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10177 l_ref_tccp = &l_tcp->tccps[0];
10178 l_copied_tccp = l_ref_tccp + 1;
10179 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10181 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10182 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10183 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10184 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10185 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10186 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10187 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10188 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10194 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10196 * @param p_tile_no the tile index.
10197 * @param p_comp_no the component being outputted.
10198 * @param p_j2k the J2K codec.
10200 * @return the number of bytes taken by the SPCod element.
10202 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10203 OPJ_UINT32 p_tile_no,
10204 OPJ_UINT32 p_comp_no )
10206 OPJ_UINT32 l_num_bands;
10208 opj_cp_v2_t *l_cp = 00;
10209 opj_tcp_v2_t *l_tcp = 00;
10210 opj_tccp_t *l_tccp = 00;
10212 /* preconditions */
10213 assert(p_j2k != 00);
10215 l_cp = &(p_j2k->m_cp);
10216 l_tcp = &l_cp->tcps[p_tile_no];
10217 l_tccp = &l_tcp->tccps[p_comp_no];
10219 /* preconditions again */
10220 assert(p_tile_no < l_cp->tw * l_cp->th);
10221 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10223 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10225 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10226 return 1 + l_num_bands;
10229 return 1 + 2*l_num_bands;
10234 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10236 * @param p_tile_no the tile to output.
10237 * @param p_comp_no the component number to output.
10238 * @param p_data the data buffer.
10239 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10240 * @param p_j2k J2K codec.
10241 * @param p_manager the user event manager.
10244 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10245 OPJ_UINT32 p_tile_no,
10246 OPJ_UINT32 p_comp_no,
10248 OPJ_UINT32 * p_header_size,
10249 struct opj_event_mgr * p_manager )
10251 OPJ_UINT32 l_header_size;
10252 OPJ_UINT32 l_band_no, l_num_bands;
10253 OPJ_UINT32 l_expn,l_mant;
10255 opj_cp_v2_t *l_cp = 00;
10256 opj_tcp_v2_t *l_tcp = 00;
10257 opj_tccp_t *l_tccp = 00;
10259 /* preconditions */
10260 assert(p_j2k != 00);
10261 assert(p_header_size != 00);
10262 assert(p_manager != 00);
10263 assert(p_data != 00);
10265 l_cp = &(p_j2k->m_cp);
10266 l_tcp = &l_cp->tcps[p_tile_no];
10267 l_tccp = &l_tcp->tccps[p_comp_no];
10269 /* preconditions again */
10270 assert(p_tile_no < l_cp->tw * l_cp->th);
10271 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10273 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10275 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10276 l_header_size = 1 + l_num_bands;
10278 if (*p_header_size < l_header_size) {
10279 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10283 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10286 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10287 l_expn = l_tccp->stepsizes[l_band_no].expn;
10288 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10293 l_header_size = 1 + 2*l_num_bands;
10295 if (*p_header_size < l_header_size) {
10296 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10300 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10303 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10304 l_expn = l_tccp->stepsizes[l_band_no].expn;
10305 l_mant = l_tccp->stepsizes[l_band_no].mant;
10307 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10312 *p_header_size = *p_header_size - l_header_size;
10318 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10320 * @param p_comp_no the component being targeted.
10321 * @param p_header_data the data contained in the COM box.
10322 * @param p_j2k the jpeg2000 codec.
10323 * @param p_header_size the size of the data contained in the COM marker.
10324 * @param p_manager the user event manager.
10326 opj_bool j2k_read_SQcd_SQcc(
10327 opj_j2k_v2_t *p_j2k,
10328 OPJ_UINT32 p_comp_no,
10329 OPJ_BYTE* p_header_data,
10330 OPJ_UINT32 * p_header_size,
10331 struct opj_event_mgr * p_manager
10335 OPJ_UINT32 l_band_no;
10336 opj_cp_v2_t *l_cp = 00;
10337 opj_tcp_v2_t *l_tcp = 00;
10338 opj_tccp_t *l_tccp = 00;
10339 OPJ_BYTE * l_current_ptr = 00;
10340 OPJ_UINT32 l_tmp, l_num_band;
10343 assert(p_j2k != 00);
10344 assert(p_manager != 00);
10345 assert(p_header_data != 00);
10347 l_cp = &(p_j2k->m_cp);
10348 /* come from tile part header or main header ?*/
10349 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10350 &l_cp->tcps[p_j2k->m_current_tile_number] :
10351 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10353 /* precondition again*/
10354 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10356 l_tccp = &l_tcp->tccps[p_comp_no];
10357 l_current_ptr = p_header_data;
10359 if (*p_header_size < 1) {
10360 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10363 *p_header_size -= 1;
10365 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10368 l_tccp->qntsty = l_tmp & 0x1f;
10369 l_tccp->numgbits = l_tmp >> 5;
10370 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10374 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10376 (*p_header_size) / 2;
10378 if( l_num_band > J2K_MAXBANDS ) {
10379 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10380 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10381 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10382 /*return OPJ_FALSE;*/
10387 if (l_cp->correct) {
10389 /* if JPWL is on, we check whether there are too many subbands */
10390 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10391 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10392 "JPWL: bad number of subbands in Sqcx (%d)\n",
10394 if (!JPWL_ASSUME) {
10395 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10398 /* we try to correct */
10400 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10401 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10406 #endif /* USE_JPWL */
10408 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10409 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10410 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10412 if (l_band_no < J2K_MAXBANDS){
10413 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10414 l_tccp->stepsizes[l_band_no].mant = 0;
10417 *p_header_size = *p_header_size - l_num_band;
10420 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10421 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10423 if (l_band_no < J2K_MAXBANDS){
10424 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10425 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10428 *p_header_size = *p_header_size - 2*l_num_band;
10431 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10432 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10433 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10434 l_tccp->stepsizes[l_band_no].expn =
10435 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10436 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10437 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10445 * Copies the tile component parameters of all the component from the first tile component.
10447 * @param p_j2k the J2k codec.
10449 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10452 opj_cp_v2_t *l_cp = NULL;
10453 opj_tcp_v2_t *l_tcp = NULL;
10454 opj_tccp_t *l_ref_tccp = NULL;
10455 opj_tccp_t *l_copied_tccp = NULL;
10458 /* preconditions */
10459 assert(p_j2k != 00);
10461 l_cp = &(p_j2k->m_cp);
10462 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10463 &l_cp->tcps[p_j2k->m_current_tile_number] :
10464 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10466 l_ref_tccp = &l_tcp->tccps[0];
10467 l_copied_tccp = l_ref_tccp + 1;
10468 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10470 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10471 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10472 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10473 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10479 * Dump some elements from the J2K decompression structure .
10481 *@param p_j2k the jpeg2000 codec.
10482 *@param flag flag to describe what elments are dump.
10483 *@param out_stream output stream where dump the elements.
10486 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10488 /* Check if the flag is compatible with j2k file*/
10489 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10490 fprintf(out_stream, "Wrong flag\n");
10494 /* Dump the image_header */
10495 if (flag & OPJ_IMG_INFO){
10496 if (p_j2k->m_private_image)
10497 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10500 /* Dump the codestream info from main header */
10501 if (flag & OPJ_J2K_MH_INFO){
10502 j2k_dump_MH_info(p_j2k, out_stream);
10506 /* Dump the codestream info of the current tile */
10507 if (flag & OPJ_J2K_TH_INFO){
10511 /* Dump the codestream index from main header */
10512 if (flag & OPJ_J2K_MH_IND){
10513 j2k_dump_MH_index(p_j2k, out_stream);
10516 /* Dump the codestream index of the current tile */
10517 if (flag & OPJ_J2K_TH_IND){
10524 * Dump index elements of the codestream extract from the main header.
10526 *@param p_j2k the jpeg2000 codec.
10527 *@param out_stream output stream where dump the elements.
10530 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10532 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10533 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10535 fprintf(out_stream, "Codestream index from main header: {\n");
10537 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10538 "\t Main header end position=%" PRIi64 "\n",
10539 cstr_index->main_head_start, cstr_index->main_head_end);
10541 fprintf(out_stream, "\t Marker list: {\n");
10543 if (cstr_index->marker){
10544 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10545 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10546 cstr_index->marker[it_marker].type,
10547 cstr_index->marker[it_marker].pos,
10548 cstr_index->marker[it_marker].len );
10552 fprintf(out_stream, "\t }\n");
10555 if (cstr_index->tile_index){
10557 /* Simple test to avoid to write empty information*/
10558 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10559 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10560 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10563 if (l_acc_nb_of_tile_part)
10565 fprintf(out_stream, "\t Tile index: {\n");
10567 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10568 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10570 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10572 if (cstr_index->tile_index[it_tile].tp_index){
10573 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10574 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10576 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10577 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10578 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10582 if (cstr_index->tile_index[it_tile].marker){
10583 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10584 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10585 cstr_index->tile_index[it_tile].marker[it_marker].type,
10586 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10587 cstr_index->tile_index[it_tile].marker[it_marker].len );
10591 fprintf(out_stream,"\t }\n");
10595 fprintf(out_stream,"}\n");
10600 * Dump info elements of the codestream extract from the main header.
10602 *@param p_j2k the jpeg2000 codec.
10603 *@param out_stream output stream where dump the elements.
10606 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10608 opj_tcp_v2_t * l_default_tile=NULL;
10610 fprintf(out_stream, "Codestream info from main header: {\n");
10612 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10613 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10614 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10616 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10617 if (l_default_tile)
10620 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10622 fprintf(out_stream, "\t default tile {\n");
10623 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10624 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10625 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10626 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10628 for (compno = 0; compno < numcomps; compno++) {
10629 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10631 OPJ_INT32 bandno, numbands;
10634 fprintf(out_stream, "\t\t comp %d {\n", compno);
10635 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10636 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10637 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10638 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10639 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10640 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10642 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10643 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10644 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10646 fprintf(out_stream, "\n");
10648 /* quantization style*/
10649 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10650 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10651 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10652 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10653 for (bandno = 0; bandno < numbands; bandno++) {
10654 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10655 l_tccp->stepsizes[bandno].expn);
10657 fprintf(out_stream, "\n");
10660 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10662 fprintf(out_stream, "\t\t }\n");
10663 } /*end of component of default tile*/
10664 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10668 fprintf(out_stream, "}\n");
10673 * Dump an image header structure.
10675 *@param img_header the image header to dump.
10676 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10677 *@param out_stream output stream where dump the elements.
10679 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10683 if (dev_dump_flag){
10684 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10688 fprintf(out_stream, "Image info {\n");
10689 tab[0] = '\t';tab[1] = '\0';
10692 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10693 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10694 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10696 if (img_header->comps){
10698 for (compno = 0; compno < img_header->numcomps; compno++) {
10699 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10700 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10701 fprintf(out_stream,"%s}\n",tab);
10705 fprintf(out_stream, "}\n");
10709 * Dump a component image header structure.
10711 *@param comp_header the component image header to dump.
10712 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10713 *@param out_stream output stream where dump the elements.
10715 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10719 if (dev_dump_flag){
10720 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10723 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10726 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10727 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10728 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10731 fprintf(out_stream, "}\n");
10736 * Get the codestream info from a JPEG2000 codec.
10738 *@param p_j2k the component image header to dump.
10740 *@return the codestream information extract from the jpg2000 codec
10742 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10745 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10746 opj_tcp_v2_t *l_default_tile;
10747 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10749 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10751 cstr_info->tx0 = p_j2k->m_cp.tx0;
10752 cstr_info->ty0 = p_j2k->m_cp.ty0;
10753 cstr_info->tdx = p_j2k->m_cp.tdx;
10754 cstr_info->tdy = p_j2k->m_cp.tdy;
10755 cstr_info->tw = p_j2k->m_cp.tw;
10756 cstr_info->th = p_j2k->m_cp.th;
10758 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10760 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10762 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10763 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10764 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10765 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10767 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10769 for (compno = 0; compno < numcomps; compno++) {
10770 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10771 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10772 OPJ_INT32 bandno, numbands;
10775 l_tccp_info->csty = l_tccp->csty;
10776 l_tccp_info->numresolutions = l_tccp->numresolutions;
10777 l_tccp_info->cblkw = l_tccp->cblkw;
10778 l_tccp_info->cblkh = l_tccp->cblkh;
10779 l_tccp_info->cblksty = l_tccp->cblksty;
10780 l_tccp_info->qmfbid = l_tccp->qmfbid;
10781 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10783 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10784 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10787 /* quantization style*/
10788 l_tccp_info->qntsty = l_tccp->qntsty;
10789 l_tccp_info->numgbits = l_tccp->numgbits;
10791 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10792 if (numbands < J2K_MAXBANDS) {
10793 for (bandno = 0; bandno < numbands; bandno++) {
10794 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10795 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10800 l_tccp_info->roishift = l_tccp->roishift;
10808 * Get the codestream index from a JPEG2000 codec.
10810 *@param p_j2k the component image header to dump.
10812 *@return the codestream index extract from the jpg2000 codec
10814 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10816 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10817 opj_calloc(1,sizeof(opj_codestream_index_t));
10821 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10822 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10823 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10825 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10826 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10827 if (!l_cstr_index->marker){
10828 opj_free( l_cstr_index);
10832 if (p_j2k->cstr_index->marker)
10833 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10835 opj_free(l_cstr_index->marker);
10836 l_cstr_index->marker = NULL;
10839 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10840 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10841 if (!l_cstr_index->tile_index){
10842 opj_free( l_cstr_index->marker);
10843 opj_free( l_cstr_index);
10847 if (!p_j2k->cstr_index->tile_index){
10848 opj_free(l_cstr_index->tile_index);
10849 l_cstr_index->tile_index = NULL;
10852 OPJ_UINT32 it_tile = 0;
10853 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10856 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10858 l_cstr_index->tile_index[it_tile].marker =
10859 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10861 if (!l_cstr_index->tile_index[it_tile].marker) {
10862 OPJ_UINT32 it_tile_free;
10864 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10865 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10868 opj_free( l_cstr_index->tile_index);
10869 opj_free( l_cstr_index->marker);
10870 opj_free( l_cstr_index);
10874 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10875 memcpy( l_cstr_index->tile_index[it_tile].marker,
10876 p_j2k->cstr_index->tile_index[it_tile].marker,
10877 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10879 opj_free(l_cstr_index->tile_index[it_tile].marker);
10880 l_cstr_index->tile_index[it_tile].marker = NULL;
10883 /* Tile part index*/
10884 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10886 l_cstr_index->tile_index[it_tile].tp_index =
10887 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10889 if(!l_cstr_index->tile_index[it_tile].tp_index){
10890 OPJ_UINT32 it_tile_free;
10892 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10893 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10894 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10897 opj_free( l_cstr_index->tile_index);
10898 opj_free( l_cstr_index->marker);
10899 opj_free( l_cstr_index);
10903 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10904 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10905 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10906 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10909 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10910 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10913 /* Packet index (NOT USED)*/
10914 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10915 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10920 return l_cstr_index;
10923 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10925 OPJ_UINT32 it_tile=0;
10927 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10928 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10929 if (!p_j2k->cstr_index->tile_index)
10932 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10933 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10934 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10935 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10936 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10937 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10947 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10948 opj_stream_private_t *p_stream,
10949 opj_event_mgr_t * p_manager)
10951 opj_bool l_go_on = OPJ_TRUE;
10952 OPJ_UINT32 l_current_tile_no;
10953 OPJ_UINT32 l_data_size,l_max_data_size;
10954 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10955 OPJ_UINT32 l_nb_comps;
10956 OPJ_BYTE * l_current_data;
10958 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10959 if (! l_current_data) {
10962 l_max_data_size = 1000;
10967 if (! j2k_read_tile_header( p_j2k,
10968 &l_current_tile_no,
10970 &l_tile_x0, &l_tile_y0,
10971 &l_tile_x1, &l_tile_y1,
10976 opj_free(l_current_data);
10984 if (l_data_size > l_max_data_size) {
10985 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10986 if (! l_current_data) {
10987 opj_free(l_current_data);
10991 l_max_data_size = l_data_size;
10994 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10995 opj_free(l_current_data);
10998 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);
11000 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11001 opj_free(l_current_data);
11004 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11008 opj_free(l_current_data);
11014 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11016 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11019 assert(p_j2k != 00);
11021 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11022 /* DEVELOPER CORNER, add your custom procedures */
11027 * Read and decode one tile.
11029 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
11030 opj_stream_private_t *p_stream,
11031 opj_event_mgr_t * p_manager)
11033 opj_bool l_go_on = OPJ_TRUE;
11034 OPJ_UINT32 l_current_tile_no;
11035 OPJ_UINT32 l_tile_no_to_dec;
11036 OPJ_UINT32 l_data_size,l_max_data_size;
11037 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11038 OPJ_UINT32 l_nb_comps;
11039 OPJ_BYTE * l_current_data;
11041 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11042 if (! l_current_data) {
11045 l_max_data_size = 1000;
11047 /*Allocate and initialize some elements of codestrem index if not already done*/
11048 if( !p_j2k->cstr_index->tile_index)
11050 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11051 opj_free(l_current_data);
11055 /* Move into the codestream to the first SOT used to decode the desired tile */
11056 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11057 if (p_j2k->cstr_index->tile_index)
11058 if(p_j2k->cstr_index->tile_index->tp_index)
11060 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11061 /* the index for this tile has not been built,
11062 * so move to the last SOT read */
11063 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11064 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11069 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)) {
11070 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11074 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11075 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11076 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11080 if (! j2k_read_tile_header( p_j2k,
11081 &l_current_tile_no,
11083 &l_tile_x0, &l_tile_y0,
11084 &l_tile_x1, &l_tile_y1,
11089 opj_free(l_current_data);
11098 if (l_data_size > l_max_data_size) {
11099 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11100 if (! l_current_data) {
11101 opj_free(l_current_data);
11105 l_max_data_size = l_data_size;
11110 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11111 opj_free(l_current_data);
11114 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);
11116 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11117 opj_free(l_current_data);
11120 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11122 if(l_current_tile_no == l_tile_no_to_dec)
11124 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11125 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11126 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11132 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);
11137 opj_free(l_current_data);
11144 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11146 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11149 assert(p_j2k != 00);
11151 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11152 /* DEVELOPER CORNER, add your custom procedures */
11158 * Decodes the tiles of the stream.
11160 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
11161 opj_stream_private_t * p_stream,
11162 opj_image_t * p_image,
11163 opj_event_mgr_t * p_manager)
11170 p_j2k->m_output_image = opj_image_create0();
11171 if (! (p_j2k->m_output_image)) {
11174 opj_copy_image_header(p_image, p_j2k->m_output_image);
11176 /* customization of the decoding */
11177 j2k_setup_decoding(p_j2k);
11179 /* Decode the codestream */
11180 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11181 opj_image_destroy(p_j2k->m_private_image);
11182 p_j2k->m_private_image = NULL;
11186 /* Move data and copy one information from codec to output image*/
11187 for (compno = 0; compno < p_image->numcomps; compno++) {
11188 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11189 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11190 p_j2k->m_output_image->comps[compno].data = NULL;
11198 * Get the decoded tile.
11200 * @param p_j2k the jpeg2000 codestream codec.
11201 * @param p_stream input_stream
11202 * @param p_image output image. .
11203 * @param p_manager the user event manager
11204 * @param tile_index index of the tile we want decode
11206 * @return true if succeed.
11208 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11209 opj_stream_private_t *p_stream,
11210 opj_image_t* p_image,
11211 struct opj_event_mgr * p_manager,
11212 OPJ_UINT32 tile_index )
11215 OPJ_UINT32 l_tile_x, l_tile_y;
11216 opj_image_comp_t* l_img_comp;
11219 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11223 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11224 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);
11228 /* Compute the dimension of the desired tile*/
11229 l_tile_x = tile_index % p_j2k->m_cp.tw;
11230 l_tile_y = tile_index / p_j2k->m_cp.tw;
11232 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11233 if (p_image->x0 < p_j2k->m_private_image->x0)
11234 p_image->x0 = p_j2k->m_private_image->x0;
11235 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11236 if (p_image->x1 > p_j2k->m_private_image->x1)
11237 p_image->x1 = p_j2k->m_private_image->x1;
11239 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11240 if (p_image->y0 < p_j2k->m_private_image->y0)
11241 p_image->y0 = p_j2k->m_private_image->y0;
11242 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11243 if (p_image->y1 > p_j2k->m_private_image->y1)
11244 p_image->y1 = p_j2k->m_private_image->y1;
11246 l_img_comp = p_image->comps;
11247 for (compno=0; compno < p_image->numcomps; ++compno)
11249 OPJ_INT32 l_comp_x1, l_comp_y1;
11251 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11253 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11254 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11255 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11256 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11258 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11259 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11264 /* Destroy the previous output image*/
11265 if (p_j2k->m_output_image)
11266 opj_image_destroy(p_j2k->m_output_image);
11268 /* Create the ouput image from the information previously computed*/
11269 p_j2k->m_output_image = opj_image_create0();
11270 if (! (p_j2k->m_output_image)) {
11273 opj_copy_image_header(p_image, p_j2k->m_output_image);
11275 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11277 /* customization of the decoding */
11278 j2k_setup_decoding_tile(p_j2k);
11280 /* Decode the codestream */
11281 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11282 opj_image_destroy(p_j2k->m_private_image);
11283 p_j2k->m_private_image = NULL;
11287 /* Move data and copy one information from codec to output image*/
11288 for (compno = 0; compno < p_image->numcomps; compno++) {
11289 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11291 if (p_image->comps[compno].data)
11292 opj_free(p_image->comps[compno].data);
11294 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11296 p_j2k->m_output_image->comps[compno].data = NULL;
11302 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11304 OPJ_UINT32 it_comp;
11306 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11308 if (p_j2k->m_private_image) {
11309 if (p_j2k->m_private_image->comps) {
11310 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11311 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11312 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11313 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11314 if ( res_factor >= max_res){
11315 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11318 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11331 * Encodes all the tiles in a row.
11333 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11334 opj_stream_private_t *p_stream,
11335 opj_event_mgr_t * p_manager )
11338 OPJ_UINT32 l_nb_tiles;
11339 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11340 OPJ_BYTE * l_current_data;
11342 /* preconditions */
11343 assert(p_j2k != 00);
11344 assert(p_stream != 00);
11345 assert(p_manager != 00);
11347 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11348 if (! l_current_data) {
11351 l_max_tile_size = 1000;
11353 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11354 for (i=0;i<l_nb_tiles;++i) {
11355 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11356 opj_free(l_current_data);
11360 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11361 if (l_current_tile_size > l_max_tile_size) {
11362 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11363 if (! l_current_data) {
11366 l_max_tile_size = l_current_tile_size;
11369 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11371 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11376 opj_free(l_current_data);
11381 * Ends the compression procedures and possibility add data to be read after the
11384 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11385 opj_stream_private_t *p_stream,
11386 struct opj_event_mgr * p_manager)
11388 /* customization of the encoding */
11389 j2k_setup_end_compress(p_j2k);
11391 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11401 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11403 * @param p_j2k the jpeg2000 codec.
11404 * @param p_stream the stream object.
11405 * @param p_manager the user event manager.
11407 * @return true if the codec is valid.
11409 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11410 opj_stream_private_t *p_stream,
11411 opj_image_t * p_image,
11412 opj_event_mgr_t * p_manager)
11414 /* preconditions */
11415 assert(p_j2k != 00);
11416 assert(p_stream != 00);
11417 assert(p_manager != 00);
11419 p_j2k->m_private_image = opj_image_create0();
11420 opj_copy_image_header(p_image, p_j2k->m_private_image);
11422 // TODO_MSD: Find a better way
11423 if (p_image->comps) {
11424 OPJ_UINT32 it_comp;
11425 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11426 if (p_image->comps[it_comp].data) {
11427 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11428 p_image->comps[it_comp].data = NULL;
11434 /* customization of the validation */
11435 j2k_setup_encoding_validation (p_j2k);
11437 /* validation of the parameters codec */
11438 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11442 /* customization of the encoding */
11443 j2k_setup_header_writting(p_j2k);
11446 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11456 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11457 OPJ_UINT32 p_tile_index,
11458 opj_stream_private_t *p_stream,
11459 opj_event_mgr_t * p_manager )
11462 if (p_tile_index != p_j2k->m_current_tile_number) {
11463 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11467 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);
11469 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11470 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11471 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11473 /* initialisation before tile encoding */
11474 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11484 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11486 OPJ_UINT32 i,j,k = 0;
11487 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11488 opj_image_comp_t * l_img_comp = 00;
11489 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11490 opj_image_t * l_image = 00;
11491 OPJ_UINT32 l_size_comp, l_remaining;
11492 OPJ_INT32 * l_src_ptr;
11493 l_tilec = p_tcd->tcd_image->tiles->comps;
11494 l_image = p_tcd->image;
11495 l_img_comp = l_image->comps;
11497 for (i=0;i<p_tcd->image->numcomps;++i) {
11498 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11499 l_remaining = l_img_comp->prec & 7; /* (%8) */
11504 if (l_size_comp == 3) {
11508 l_width = (l_tilec->x1 - l_tilec->x0);
11509 l_height = (l_tilec->y1 - l_tilec->y0);
11510 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11511 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11512 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11513 l_stride = l_image_width - l_width;
11514 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11516 switch (l_size_comp) {
11519 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11520 if (l_img_comp->sgnd) {
11521 for (j=0;j<l_height;++j) {
11522 for (k=0;k<l_width;++k) {
11523 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11527 l_src_ptr += l_stride;
11531 for (j=0;j<l_height;++j) {
11532 for (k=0;k<l_width;++k) {
11533 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11537 l_src_ptr += l_stride;
11541 p_data = (OPJ_BYTE*) l_dest_ptr;
11546 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11547 if (l_img_comp->sgnd) {
11548 for (j=0;j<l_height;++j) {
11549 for (k=0;k<l_width;++k) {
11550 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11552 l_src_ptr += l_stride;
11556 for (j=0;j<l_height;++j) {
11557 for (k=0;k<l_width;++k) {
11558 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11560 l_src_ptr += l_stride;
11564 p_data = (OPJ_BYTE*) l_dest_ptr;
11569 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11570 for (j=0;j<l_height;++j) {
11571 for (k=0;k<l_width;++k) {
11572 *(l_dest_ptr++) = *(l_src_ptr++);
11574 l_src_ptr += l_stride;
11577 p_data = (OPJ_BYTE*) l_dest_ptr;
11590 * @param p_j2k the jpeg2000 codec.
11591 * @param p_stream the stream to write data to.
11592 * @param p_manager the user event manager.
11594 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11596 OPJ_UINT32 p_data_size,
11597 opj_stream_private_t *p_stream,
11598 opj_event_mgr_t * p_manager )
11600 opj_tcd_v2_t * l_tcd = 00;
11601 opj_cp_v2_t * l_cp = 00;
11602 opj_tcp_v2_t * l_tcp = 00;
11603 OPJ_UINT32 l_nb_bytes_written;
11604 OPJ_BYTE * l_current_data = 00;
11605 OPJ_UINT32 l_tile_size = 0;
11606 OPJ_UINT32 l_available_data;
11608 /* preconditions */
11609 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11611 l_tcd = p_j2k->m_tcd;
11612 l_cp = &(p_j2k->m_cp);
11613 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11615 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11616 l_available_data = l_tile_size;
11617 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11619 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11620 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11624 l_nb_bytes_written = 0;
11625 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11628 l_current_data += l_nb_bytes_written;
11629 l_available_data -= l_nb_bytes_written;
11631 l_nb_bytes_written = 0;
11632 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11636 l_available_data -= l_nb_bytes_written;
11637 l_nb_bytes_written = l_tile_size - l_available_data;
11639 if ( opj_stream_write_data( p_stream,
11640 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11641 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11645 ++p_j2k->m_current_tile_number;
11652 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11653 * are valid. Developers wanting to extend the library can add their own validation procedures.
11655 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11657 /* preconditions */
11658 assert(p_j2k != 00);
11660 /* DEVELOPER CORNER, insert your custom procedures */
11661 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11663 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11664 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11667 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11668 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11669 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11673 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11674 * are valid. Developers wanting to extend the library can add their own validation procedures.
11676 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11678 /* preconditions */
11679 assert(p_j2k != 00);
11681 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11682 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11684 /* DEVELOPER CORNER, add your custom validation procedure */
11685 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11690 * Sets up the procedures to do on writing header.
11691 * Developers wanting to extend the library can add their own writing procedures.
11693 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11695 /* preconditions */
11696 assert(p_j2k != 00);
11698 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11699 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11700 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11701 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11702 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11705 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11706 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11707 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11709 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11710 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11714 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11716 if (p_j2k->m_cp.comment != 00) {
11717 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11720 /* DEVELOPER CORNER, insert your custom procedures */
11721 if (p_j2k->m_cp.rsiz & MCT) {
11722 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11724 /* End of Developer Corner */
11726 if (p_j2k->cstr_index) {
11727 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11730 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11731 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11735 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11737 OPJ_UINT32 * p_data_written,
11738 OPJ_UINT32 p_total_data_size,
11739 opj_stream_private_t *p_stream,
11740 struct opj_event_mgr * p_manager )
11743 OPJ_UINT32 l_nb_bytes_written = 0;
11744 OPJ_UINT32 l_current_nb_bytes_written;
11745 OPJ_BYTE * l_begin_data = 00;
11747 opj_tcp_v2_t *l_tcp = 00;
11748 opj_tcd_v2_t * l_tcd = 00;
11749 opj_cp_v2_t * l_cp = 00;
11751 l_tcd = p_j2k->m_tcd;
11752 l_cp = &(p_j2k->m_cp);
11753 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11755 l_tcd->cur_pino = 0;
11757 /*Get number of tile parts*/
11758 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11763 l_current_nb_bytes_written = 0;
11764 l_begin_data = p_data;
11765 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11770 l_nb_bytes_written += l_current_nb_bytes_written;
11771 p_data += l_current_nb_bytes_written;
11772 p_total_data_size -= l_current_nb_bytes_written;
11774 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11775 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11776 l_current_nb_bytes_written = 0;
11777 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11778 l_nb_bytes_written += l_current_nb_bytes_written;
11779 p_data += l_current_nb_bytes_written;
11780 p_total_data_size -= l_current_nb_bytes_written;
11782 l_current_nb_bytes_written = 0;
11783 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11784 l_nb_bytes_written += l_current_nb_bytes_written;
11785 p_data += l_current_nb_bytes_written;
11786 p_total_data_size -= l_current_nb_bytes_written;
11789 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11790 l_current_nb_bytes_written = 0;
11791 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11792 l_nb_bytes_written += l_current_nb_bytes_written;
11793 p_data += l_current_nb_bytes_written;
11794 p_total_data_size -= l_current_nb_bytes_written;
11798 l_current_nb_bytes_written = 0;
11799 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11803 l_nb_bytes_written += l_current_nb_bytes_written;
11804 * p_data_written = l_nb_bytes_written;
11806 /* Writing Psot in SOT marker */
11807 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11809 if (l_cp->m_specific_param.m_enc.m_cinema){
11810 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11816 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11818 OPJ_UINT32 * p_data_written,
11819 OPJ_UINT32 p_total_data_size,
11820 opj_stream_private_t *p_stream,
11821 struct opj_event_mgr * p_manager
11824 OPJ_UINT32 tilepartno=0;
11825 OPJ_UINT32 l_nb_bytes_written = 0;
11826 OPJ_UINT32 l_current_nb_bytes_written;
11827 OPJ_UINT32 l_part_tile_size;
11828 OPJ_UINT32 tot_num_tp;
11831 OPJ_BYTE * l_begin_data;
11832 opj_tcp_v2_t *l_tcp = 00;
11833 opj_tcd_v2_t * l_tcd = 00;
11834 opj_cp_v2_t * l_cp = 00;
11837 l_tcd = p_j2k->m_tcd;
11838 l_cp = &(p_j2k->m_cp);
11839 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11841 /*Get number of tile parts*/
11842 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11844 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11845 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11846 l_current_nb_bytes_written = 0;
11847 l_part_tile_size = 0;
11848 l_begin_data = p_data;
11850 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11854 l_nb_bytes_written += l_current_nb_bytes_written;
11855 p_data += l_current_nb_bytes_written;
11856 p_total_data_size -= l_current_nb_bytes_written;
11857 l_part_tile_size += l_nb_bytes_written;
11859 l_current_nb_bytes_written = 0;
11860 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11864 p_data += l_current_nb_bytes_written;
11865 l_nb_bytes_written += l_current_nb_bytes_written;
11866 p_total_data_size -= l_current_nb_bytes_written;
11867 l_part_tile_size += l_nb_bytes_written;
11869 /* Writing Psot in SOT marker */
11870 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11872 if (l_cp->m_specific_param.m_enc.m_cinema) {
11873 j2k_update_tlm(p_j2k,l_part_tile_size);
11876 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11879 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11880 l_tcd->cur_pino = pino;
11882 /*Get number of tile parts*/
11883 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11884 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11885 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11886 l_current_nb_bytes_written = 0;
11887 l_part_tile_size = 0;
11888 l_begin_data = p_data;
11890 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11894 l_nb_bytes_written += l_current_nb_bytes_written;
11895 p_data += l_current_nb_bytes_written;
11896 p_total_data_size -= l_current_nb_bytes_written;
11897 l_part_tile_size += l_current_nb_bytes_written;
11899 l_current_nb_bytes_written = 0;
11901 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11905 l_nb_bytes_written += l_current_nb_bytes_written;
11906 p_data += l_current_nb_bytes_written;
11907 p_total_data_size -= l_current_nb_bytes_written;
11908 l_part_tile_size += l_current_nb_bytes_written;
11910 /* Writing Psot in SOT marker */
11911 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11913 if (l_cp->m_specific_param.m_enc.m_cinema) {
11914 j2k_update_tlm(p_j2k,l_part_tile_size);
11917 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11921 *p_data_written = l_nb_bytes_written;
11927 * Writes the updated tlm.
11929 * @param p_stream the stream to write data to.
11930 * @param p_j2k J2K codec.
11931 * @param p_manager the user event manager.
11933 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11934 struct opj_stream_private *p_stream,
11935 struct opj_event_mgr * p_manager )
11937 OPJ_UINT32 l_tlm_size;
11938 OPJ_SIZE_T l_tlm_position, l_current_position;
11940 /* preconditions */
11941 assert(p_j2k != 00);
11942 assert(p_manager != 00);
11943 assert(p_stream != 00);
11945 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11946 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11947 l_current_position = opj_stream_tell(p_stream);
11949 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11953 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) {
11957 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11966 * Ends the encoding, i.e. frees memory.
11968 * @param p_stream the stream to write data to.
11969 * @param p_j2k J2K codec.
11970 * @param p_manager the user event manager.
11972 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11973 struct opj_stream_private *p_stream,
11974 struct opj_event_mgr * p_manager )
11976 /* preconditions */
11977 assert(p_j2k != 00);
11978 assert(p_manager != 00);
11979 assert(p_stream != 00);
11981 tcd_destroy_v2(p_j2k->m_tcd);
11984 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11985 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11986 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11987 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11990 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11991 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11992 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11995 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12001 * Destroys the memory associated with the decoding of headers.
12003 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12004 opj_stream_private_t *p_stream,
12005 opj_event_mgr_t * p_manager )
12007 /* preconditions */
12008 assert(p_j2k != 00);
12009 assert(p_stream != 00);
12010 assert(p_manager != 00);
12012 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12013 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12014 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12017 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12026 * @param p_stream the stream to write data to.
12027 * @param p_j2k J2K codec.
12028 * @param p_manager the user event manager.
12030 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12031 struct opj_stream_private *p_stream,
12032 struct opj_event_mgr * p_manager )
12034 opj_codestream_info_t * l_cstr_info = 00;
12036 /* preconditions */
12037 assert(p_j2k != 00);
12038 assert(p_manager != 00);
12039 assert(p_stream != 00);
12042 /* TODO mergeV2: check this part which use cstr_info */
12043 /*l_cstr_info = p_j2k->cstr_info;
12047 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));
12049 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12050 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12052 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12054 l_cstr_info->tw = p_j2k->m_cp.tw;
12055 l_cstr_info->th = p_j2k->m_cp.th;
12057 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12058 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12059 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12060 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12062 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12064 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12066 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12068 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12069 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12072 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12074 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12076 /*l_cstr_info->maxmarknum = 100;
12077 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12078 l_cstr_info->marknum = 0;
12081 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);
12085 * Creates a tile-coder decoder.
12087 * @param p_stream the stream to write data to.
12088 * @param p_j2k J2K codec.
12089 * @param p_manager the user event manager.
12091 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12092 struct opj_stream_private *p_stream,
12093 struct opj_event_mgr * p_manager )
12095 /* preconditions */
12096 assert(p_j2k != 00);
12097 assert(p_manager != 00);
12098 assert(p_stream != 00);
12100 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12102 if (! p_j2k->m_tcd) {
12103 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12107 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12108 tcd_destroy_v2(p_j2k->m_tcd);
12119 * @param p_j2k the jpeg2000 codec.
12120 * @param p_stream the stream to write data to.
12121 * @param p_manager the user event manager.
12123 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12124 OPJ_UINT32 p_tile_index,
12126 OPJ_UINT32 p_data_size,
12127 opj_stream_private_t *p_stream,
12128 opj_event_mgr_t * p_manager )
12130 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12131 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12135 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12136 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);