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 Read the SIZ marker (image and tile size)
427 @param j2k J2K handle
429 static void j2k_read_siz(opj_j2k_t *j2k);
432 * Reads a SIZ marker (image and tile size)
433 * @param p_header_data the data contained in the SIZ box.
434 * @param jp2 the jpeg2000 file codec.
435 * @param p_header_size the size of the data contained in the SIZ marker.
436 * @param p_manager the user event manager.
438 static opj_bool j2k_read_siz_v2 (
440 OPJ_BYTE * p_header_data,
441 OPJ_UINT32 p_header_size,
442 struct opj_event_mgr * p_manager
446 Write the COM marker (comment)
447 @param j2k J2K handle
449 static void j2k_write_com(opj_j2k_t *j2k);
452 * Writes the COM marker (comment)
454 * @param p_stream the stream to write data to.
455 * @param p_j2k J2K codec.
456 * @param p_manager the user event manager.
458 static opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
459 struct opj_stream_private *p_stream,
460 struct opj_event_mgr * p_manager );
463 Read the COM marker (comment)
464 @param j2k J2K handle
466 static void j2k_read_com(opj_j2k_t *j2k);
468 * Reads a COM marker (comments)
469 * @param p_header_data the data contained in the COM box.
470 * @param jp2 the jpeg2000 file codec.
471 * @param p_header_size the size of the data contained in the COM marker.
472 * @param p_manager the user event manager.
474 static opj_bool j2k_read_com_v2 (
476 OPJ_BYTE * p_header_data,
477 OPJ_UINT32 p_header_size,
478 struct opj_event_mgr * p_manager
481 Write the value concerning the specified component in the marker COD and COC
482 @param j2k J2K handle
483 @param compno Number of the component concerned by the information written
485 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
487 Read the value concerning the specified component in the marker COD and COC
488 @param j2k J2K handle
489 @param compno Number of the component concerned by the information read
491 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
493 Write the COD marker (coding style default)
494 @param j2k J2K handle
496 static void j2k_write_cod(opj_j2k_t *j2k);
499 * Writes the COD marker (Coding style default)
501 * @param p_stream the stream to write data to.
502 * @param p_j2k J2K codec.
503 * @param p_manager the user event manager.
505 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
506 struct opj_stream_private *p_stream,
507 struct opj_event_mgr * p_manager );
510 Read the COD marker (coding style default)
511 @param j2k J2K handle
513 static void j2k_read_cod(opj_j2k_t *j2k);
516 * Reads a COD marker (Coding Styke defaults)
517 * @param p_header_data the data contained in the COD box.
518 * @param p_j2k the jpeg2000 codec.
519 * @param p_header_size the size of the data contained in the COD marker.
520 * @param p_manager the user event manager.
522 static opj_bool j2k_read_cod_v2 (
524 OPJ_BYTE * p_header_data,
525 OPJ_UINT32 p_header_size,
526 struct opj_event_mgr * p_manager
530 Write the COC marker (coding style component)
531 @param j2k J2K handle
532 @param compno Number of the component concerned by the information written
534 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
537 * Writes the COC marker (Coding style component)
539 * @param p_comp_number the index of the component to output.
540 * @param p_stream the stream to write data to.
541 * @param p_j2k J2K codec.
542 * @param p_manager the user event manager.
544 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
545 OPJ_UINT32 p_comp_number,
546 struct opj_stream_private *p_stream,
547 struct opj_event_mgr * p_manager
550 * Writes the COC marker (Coding style component)
552 * @param p_comp_no the index of the component to output.
553 * @param p_stream the stream to write data to.
554 * @param p_j2k J2K codec.
555 * @param p_manager the user event manager.
557 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
558 OPJ_UINT32 p_comp_no,
560 OPJ_UINT32 * p_data_written,
561 struct opj_event_mgr * p_manager );
564 * Gets the maximum size taken by a coc.
566 * @param p_j2k the jpeg2000 codec to use.
568 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
571 Read the COC marker (coding style component)
572 @param j2k J2K handle
574 static void j2k_read_coc(opj_j2k_t *j2k);
577 * Reads a COC marker (Coding Style Component)
578 * @param p_header_data the data contained in the COC box.
579 * @param p_j2k the jpeg2000 codec.
580 * @param p_header_size the size of the data contained in the COC marker.
581 * @param p_manager the user event manager.
583 static opj_bool j2k_read_coc_v2 (
585 OPJ_BYTE * p_header_data,
586 OPJ_UINT32 p_header_size,
587 struct opj_event_mgr * p_manager
591 Write the value concerning the specified component in the marker QCD and QCC
592 @param j2k J2K handle
593 @param compno Number of the component concerned by the information written
595 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
597 Read the value concerning the specified component in the marker QCD and QCC
598 @param j2k J2K handle
599 @param compno Number of the component concern by the information read
600 @param len Length of the information in the QCX part of the marker QCD/QCC
602 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
604 Write the QCD marker (quantization default)
605 @param j2k J2K handle
607 static void j2k_write_qcd(opj_j2k_t *j2k);
610 * Writes the QCD marker (quantization default)
612 * @param p_comp_number the index of the component to output.
613 * @param p_stream the stream to write data to.
614 * @param p_j2k J2K codec.
615 * @param p_manager the user event manager.
617 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
618 struct opj_stream_private *p_stream,
619 struct opj_event_mgr * p_manager );
622 Read the QCD marker (quantization default)
623 @param j2k J2K handle
625 static void j2k_read_qcd(opj_j2k_t *j2k);
628 * Reads a QCD marker (Quantization defaults)
629 * @param p_header_data the data contained in the QCD box.
630 * @param p_j2k the jpeg2000 codec.
631 * @param p_header_size the size of the data contained in the QCD marker.
632 * @param p_manager the user event manager.
634 static opj_bool j2k_read_qcd_v2 (
636 OPJ_BYTE * p_header_data,
637 OPJ_UINT32 p_header_size,
638 struct opj_event_mgr * p_manager
642 Write the QCC marker (quantization component)
643 @param j2k J2K handle
644 @param compno Number of the component concerned by the information written
646 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
649 * Writes the QCC marker (quantization component)
651 * @param p_comp_no the index of the component to output.
652 * @param p_stream the stream to write data to.
653 * @param p_j2k J2K codec.
654 * @param p_manager the user event manager.
656 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
657 OPJ_UINT32 p_comp_no,
658 struct opj_stream_private *p_stream,
659 struct opj_event_mgr * p_manager );
662 * Writes the QCC marker (quantization component)
664 * @param p_comp_no the index of the component to output.
665 * @param p_stream the stream to write data to.
666 * @param p_j2k J2K codec.
667 * @param p_manager the user event manager.
669 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
670 OPJ_UINT32 p_comp_no,
672 OPJ_UINT32 * p_data_written,
673 struct opj_event_mgr * p_manager );
676 * Gets the maximum size taken by a qcc.
678 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
681 Read the QCC marker (quantization component)
682 @param j2k J2K handle
684 static void j2k_read_qcc(opj_j2k_t *j2k);
686 * Reads a QCC marker (Quantization component)
687 * @param p_header_data the data contained in the QCC box.
688 * @param p_j2k the jpeg2000 codec.
689 * @param p_header_size the size of the data contained in the QCC marker.
690 * @param p_manager the user event manager.
692 static opj_bool j2k_read_qcc_v2(
694 OPJ_BYTE * p_header_data,
695 OPJ_UINT32 p_header_size,
696 struct opj_event_mgr * p_manager);
699 Write the POC marker (progression order change)
700 @param j2k J2K handle
702 static void j2k_write_poc(opj_j2k_t *j2k);
705 * Writes the POC marker (Progression Order Change)
707 * @param p_stream the stream to write data to.
708 * @param p_j2k J2K codec.
709 * @param p_manager the user event manager.
711 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
712 struct opj_stream_private *p_stream,
713 struct opj_event_mgr * p_manager );
716 * Writes the POC marker (Progression Order Change)
718 * @param p_stream the stream to write data to.
719 * @param p_j2k J2K codec.
720 * @param p_manager the user event manager.
722 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
724 OPJ_UINT32 * p_data_written,
725 struct opj_event_mgr * p_manager );
728 * Gets the maximum size taken by the writting of a POC.
730 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
733 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
735 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
738 * Gets the maximum size taken by the headers of the SOT.
740 * @param p_j2k the jpeg2000 codec to use.
742 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
745 Read the POC marker (progression order change)
746 @param j2k J2K handle
748 static void j2k_read_poc(opj_j2k_t *j2k);
750 * Reads a POC marker (Progression Order Change)
752 * @param p_header_data the data contained in the POC box.
753 * @param p_j2k the jpeg2000 codec.
754 * @param p_header_size the size of the data contained in the POC marker.
755 * @param p_manager the user event manager.
757 static opj_bool j2k_read_poc_v2 (
759 OPJ_BYTE * p_header_data,
760 OPJ_UINT32 p_header_size,
761 struct opj_event_mgr * p_manager
765 Read the CRG marker (component registration)
766 @param j2k J2K handle
768 static void j2k_read_crg(opj_j2k_t *j2k);
770 * Reads a CRG marker (Component registration)
772 * @param p_header_data the data contained in the TLM box.
773 * @param p_j2k the jpeg2000 codec.
774 * @param p_header_size the size of the data contained in the TLM marker.
775 * @param p_manager the user event manager.
777 static opj_bool j2k_read_crg_v2 (
779 OPJ_BYTE * p_header_data,
780 OPJ_UINT32 p_header_size,
781 struct opj_event_mgr * p_manager
784 Read the TLM marker (tile-part lengths)
785 @param j2k J2K handle
787 static void j2k_read_tlm(opj_j2k_t *j2k);
789 * Reads a TLM marker (Tile Length Marker)
791 * @param p_header_data the data contained in the TLM box.
792 * @param p_j2k the jpeg2000 codec.
793 * @param p_header_size the size of the data contained in the TLM marker.
794 * @param p_manager the user event manager.
796 static opj_bool j2k_read_tlm_v2 (
798 OPJ_BYTE * p_header_data,
799 OPJ_UINT32 p_header_size,
800 struct opj_event_mgr * p_manager
804 * Writes the updated tlm.
806 * @param p_stream the stream to write data to.
807 * @param p_j2k J2K codec.
808 * @param p_manager the user event manager.
810 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
811 struct opj_stream_private *p_stream,
812 struct opj_event_mgr * p_manager );
815 Read the PLM marker (packet length, main header)
816 @param j2k J2K handle
818 static void j2k_read_plm(opj_j2k_t *j2k);
821 * Reads a PLM marker (Packet length, main header marker)
823 * @param p_header_data the data contained in the TLM box.
824 * @param p_j2k the jpeg2000 codec.
825 * @param p_header_size the size of the data contained in the TLM marker.
826 * @param p_manager the user event manager.
828 static opj_bool j2k_read_plm_v2 (
830 OPJ_BYTE * p_header_data,
831 OPJ_UINT32 p_header_size,
832 struct opj_event_mgr * p_manager
835 Read the PLT marker (packet length, tile-part header)
836 @param j2k J2K handle
838 static void j2k_read_plt(opj_j2k_t *j2k);
840 * Reads a PLT marker (Packet length, tile-part header)
842 * @param p_header_data the data contained in the PLT box.
843 * @param p_j2k the jpeg2000 codec.
844 * @param p_header_size the size of the data contained in the PLT marker.
845 * @param p_manager the user event manager.
847 static opj_bool j2k_read_plt_v2 (
849 OPJ_BYTE * p_header_data,
850 OPJ_UINT32 p_header_size,
851 struct opj_event_mgr * p_manager
854 Read the PPM marker (packet packet headers, main header)
855 @param j2k J2K handle
857 static void j2k_read_ppm(opj_j2k_t *j2k);
859 * Reads a PPM marker (Packed packet headers, main header)
861 * @param p_header_data the data contained in the POC box.
862 * @param p_j2k the jpeg2000 codec.
863 * @param p_header_size the size of the data contained in the POC marker.
864 * @param p_manager the user event manager.
867 static opj_bool j2k_read_ppm_v2 (
869 OPJ_BYTE * p_header_data,
870 OPJ_UINT32 p_header_size,
871 struct opj_event_mgr * p_manager
875 static opj_bool j2k_read_ppm_v3 (
877 OPJ_BYTE * p_header_data,
878 OPJ_UINT32 p_header_size,
879 struct opj_event_mgr * p_manager
883 Read the PPT marker (packet packet headers, tile-part header)
884 @param j2k J2K handle
886 static void j2k_read_ppt(opj_j2k_t *j2k);
888 * Reads a PPT marker (Packed packet headers, tile-part header)
890 * @param p_header_data the data contained in the PPT box.
891 * @param p_j2k the jpeg2000 codec.
892 * @param p_header_size the size of the data contained in the PPT marker.
893 * @param p_manager the user event manager.
895 static opj_bool j2k_read_ppt_v2 (
897 OPJ_BYTE * p_header_data,
898 OPJ_UINT32 p_header_size,
899 struct opj_event_mgr * p_manager
902 Write the TLM marker (Mainheader)
903 @param j2k J2K handle
905 static void j2k_write_tlm(opj_j2k_t *j2k);
908 * Writes the TLM marker (Tile Length Marker)
910 * @param p_stream the stream to write data to.
911 * @param p_j2k J2K codec.
912 * @param p_manager the user event manager.
914 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
915 struct opj_stream_private *p_stream,
916 struct opj_event_mgr * p_manager );
919 Write the SOT marker (start of tile-part)
920 @param j2k J2K handle
922 static void j2k_write_sot(opj_j2k_t *j2k);
925 * Writes the SOT marker (Start of tile-part)
927 * @param p_stream the stream to write data to.
928 * @param p_j2k J2K codec.
929 * @param p_manager the user event manager.
931 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
933 OPJ_UINT32 * p_data_written,
934 const struct opj_stream_private *p_stream,
935 struct opj_event_mgr * p_manager );
938 * Reads a PPT marker (Packed packet headers, tile-part header)
940 * @param p_header_data the data contained in the PPT box.
941 * @param p_j2k the jpeg2000 codec.
942 * @param p_header_size the size of the data contained in the PPT marker.
943 * @param p_manager the user event manager.
945 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
946 OPJ_BYTE * p_header_data,
947 OPJ_UINT32 p_header_size,
948 opj_event_mgr_t * p_manager );
951 Write the SOD marker (start of data)
952 @param j2k J2K handle
953 @param tile_coder Pointer to a TCD handle
955 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
958 * Writes the SOD marker (Start of data)
960 * @param p_stream the stream to write data to.
961 * @param p_j2k J2K codec.
962 * @param p_manager the user event manager.
964 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
965 struct opj_tcd_v2 * p_tile_coder,
967 OPJ_UINT32 * p_data_written,
968 OPJ_UINT32 p_total_data_size,
969 const struct opj_stream_private *p_stream,
970 struct opj_event_mgr * p_manager );
973 Read the SOD marker (start of data)
974 @param j2k J2K handle
976 static void j2k_read_sod(opj_j2k_t *j2k);
979 * Reads a SOD marker (Start Of Data)
981 * @param p_header_data the data contained in the SOD box.
982 * @param p_j2k the jpeg2000 codec.
983 * @param p_header_size the size of the data contained in the SOD marker.
984 * @param p_manager the user event manager.
986 static opj_bool j2k_read_sod_v2 (
988 struct opj_stream_private *p_stream,
989 struct opj_event_mgr * p_manager
993 * Updates the Tile Length Marker.
995 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
997 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
998 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
1000 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
1001 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
1005 Write the RGN marker (region-of-interest)
1006 @param j2k J2K handle
1007 @param compno Number of the component concerned by the information written
1008 @param tileno Number of the tile concerned by the information written
1010 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
1013 * Writes the RGN marker (Region Of Interest)
1015 * @param p_tile_no the tile to output
1016 * @param p_comp_no the component to output
1017 * @param p_stream the stream to write data to.
1018 * @param p_j2k J2K codec.
1019 * @param p_manager the user event manager.
1021 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
1022 OPJ_UINT32 p_tile_no,
1023 OPJ_UINT32 p_comp_no,
1024 struct opj_stream_private *p_stream,
1025 struct opj_event_mgr * p_manager );
1028 Read the RGN marker (region-of-interest)
1029 @param j2k J2K handle
1031 static void j2k_read_rgn(opj_j2k_t *j2k);
1034 * Reads a RGN marker (Region Of Interest)
1036 * @param p_header_data the data contained in the POC box.
1037 * @param p_j2k the jpeg2000 codec.
1038 * @param p_header_size the size of the data contained in the POC marker.
1039 * @param p_manager the user event manager.
1041 static opj_bool j2k_read_rgn_v2 (
1042 opj_j2k_v2_t *p_j2k,
1043 OPJ_BYTE * p_header_data,
1044 OPJ_UINT32 p_header_size,
1045 struct opj_event_mgr * p_manager
1049 * Writes the EOC marker (End of Codestream)
1051 * @param p_stream the stream to write data to.
1052 * @param p_j2k J2K codec.
1053 * @param p_manager the user event manager.
1055 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
1056 struct opj_stream_private *p_stream,
1057 struct opj_event_mgr * p_manager );
1060 Write the EOC marker (end of codestream)
1061 @param j2k J2K handle
1063 static void j2k_write_eoc(opj_j2k_t *j2k);
1065 Read the EOC marker (end of codestream)
1066 @param j2k J2K handle
1068 static void j2k_read_eoc(opj_j2k_t *j2k);
1071 * Reads a EOC marker (End Of Codestream)
1073 * @param p_header_data the data contained in the SOD box.
1074 * @param p_j2k the jpeg2000 codec.
1075 * @param p_header_size the size of the data contained in the SOD marker.
1076 * @param p_manager the user event manager.
1079 static opj_bool j2k_read_eoc_v2 (
1080 opj_j2k_v2_t *p_j2k,
1081 struct opj_stream_private *p_stream,
1082 struct opj_event_mgr * p_manager
1089 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1091 * @param p_stream the stream to write data to.
1092 * @param p_j2k J2K codec.
1093 * @param p_manager the user event manager.
1095 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1096 struct opj_stream_private *p_stream,
1097 struct opj_event_mgr * p_manager );
1102 * @param p_stream the stream to write data to.
1103 * @param p_j2k J2K codec.
1104 * @param p_manager the user event manager.
1106 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1107 struct opj_stream_private *p_stream,
1108 struct opj_event_mgr * p_manager );
1111 Read an unknown marker
1112 @param j2k J2K handle
1114 static void j2k_read_unk(opj_j2k_t *j2k);
1116 Add main header marker information
1117 @param cstr_info Codestream information structure
1118 @param type marker type
1119 @param pos byte offset of marker segment
1120 @param len length of marker segment
1122 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1124 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1126 Add tile header marker information
1127 @param tileno tile index number
1128 @param cstr_info Codestream information structure
1129 @param type marker type
1130 @param pos byte offset of marker segment
1131 @param len length of marker segment
1133 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1135 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);
1138 * Reads an unknown marker
1140 * @param p_stream the stream object to read from.
1141 * @param p_j2k the jpeg2000 codec.
1142 * @param p_manager the user event manager.
1144 * @return true if the marker could be deduced.
1146 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1147 struct opj_stream_private *p_stream,
1148 OPJ_UINT32 *output_marker,
1149 struct opj_event_mgr * p_manager );
1152 * Writes the MCT marker (Multiple Component Transform)
1154 * @param p_stream the stream to write data to.
1155 * @param p_j2k J2K codec.
1156 * @param p_manager the user event manager.
1158 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1159 opj_mct_data_t * p_mct_record,
1160 struct opj_stream_private *p_stream,
1161 struct opj_event_mgr * p_manager );
1164 * Reads a MCT marker (Multiple Component Transform)
1166 * @param p_header_data the data contained in the MCT box.
1167 * @param p_j2k the jpeg2000 codec.
1168 * @param p_header_size the size of the data contained in the MCT marker.
1169 * @param p_manager the user event manager.
1171 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1172 OPJ_BYTE * p_header_data,
1173 OPJ_UINT32 p_header_size,
1174 struct opj_event_mgr * p_manager );
1177 * Writes the MCC marker (Multiple Component Collection)
1179 * @param p_stream the stream to write data to.
1180 * @param p_j2k J2K codec.
1181 * @param p_manager the user event manager.
1183 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1184 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1185 struct opj_stream_private *p_stream,
1186 struct opj_event_mgr * p_manager );
1190 * Reads a MCC marker (Multiple Component Collection)
1192 * @param p_header_data the data contained in the MCC box.
1193 * @param p_j2k the jpeg2000 codec.
1194 * @param p_header_size the size of the data contained in the MCC marker.
1195 * @param p_manager the user event manager.
1197 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1198 OPJ_BYTE * p_header_data,
1199 OPJ_UINT32 p_header_size,
1200 struct opj_event_mgr * p_manager );
1203 * Writes the MCO marker (Multiple component transformation ordering)
1205 * @param p_stream the stream to write data to.
1206 * @param p_j2k J2K codec.
1207 * @param p_manager the user event manager.
1209 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1210 struct opj_stream_private *p_stream,
1211 struct opj_event_mgr * p_manager );
1214 * Reads a MCO marker (Multiple Component Transform Ordering)
1216 * @param p_header_data the data contained in the MCO box.
1217 * @param p_j2k the jpeg2000 codec.
1218 * @param p_header_size the size of the data contained in the MCO marker.
1219 * @param p_manager the user event manager.
1221 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1222 OPJ_BYTE * p_header_data,
1223 OPJ_UINT32 p_header_size,
1224 struct opj_event_mgr * p_manager );
1226 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1228 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1229 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1230 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1231 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1233 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1234 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1235 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1236 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1238 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1239 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1240 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1241 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1245 * Ends the encoding, i.e. frees memory.
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_end_encoding( opj_j2k_v2_t *p_j2k,
1252 struct opj_stream_private *p_stream,
1253 struct opj_event_mgr * p_manager );
1256 * Writes the CBD marker (Component bit depth definition)
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_cbd( opj_j2k_v2_t *p_j2k,
1263 struct opj_stream_private *p_stream,
1264 struct opj_event_mgr * p_manager );
1267 * Reads a CBD marker (Component bit depth definition)
1268 * @param p_header_data the data contained in the CBD box.
1269 * @param p_j2k the jpeg2000 codec.
1270 * @param p_header_size the size of the data contained in the CBD marker.
1271 * @param p_manager the user event manager.
1273 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1274 OPJ_BYTE * p_header_data,
1275 OPJ_UINT32 p_header_size,
1276 struct opj_event_mgr * p_manager);
1279 * Writes the image components.
1281 * @param p_stream the stream to write data to.
1282 * @param p_j2k J2K codec.
1283 * @param p_manager the user event manager.
1285 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1286 struct opj_stream_private *p_stream,
1287 struct opj_event_mgr * p_manager );
1290 * Writes regions of interests.
1292 * @param p_stream the stream to write data to.
1293 * @param p_j2k J2K codec.
1294 * @param p_manager the user event manager.
1296 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1297 struct opj_stream_private *p_stream,
1298 struct opj_event_mgr * p_manager );
1303 * @param p_stream the stream to write data to.
1304 * @param p_j2k J2K codec.
1305 * @param p_manager the user event manager.
1307 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1308 struct opj_stream_private *p_stream,
1309 struct opj_event_mgr * p_manager );
1312 * Checks the progression order changes values. Tells of the poc given as input are valid.
1313 * A nice message is outputted at errors.
1315 * @param p_pocs the progression order changes.
1316 * @param p_nb_pocs the number of progression order changes.
1317 * @param p_nb_resolutions the number of resolutions.
1318 * @param numcomps the number of components
1319 * @param numlayers the number of layers.
1321 * @return true if the pocs are valid.
1323 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1324 OPJ_UINT32 p_nb_pocs,
1325 OPJ_UINT32 p_nb_resolutions,
1326 OPJ_UINT32 numcomps,
1327 OPJ_UINT32 numlayers,
1328 opj_event_mgr_t * p_manager);
1331 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1333 * @param cp the coding parameters.
1334 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1335 * @param tileno the given tile.
1337 * @return the number of tile parts.
1339 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1341 /** mem allocation for TLM marker*/
1342 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1345 * Calculates the total number of tile parts needed by the encoder to
1346 * encode such an image. If not enough memory is available, then the function return false.
1348 * @param p_nb_tiles pointer that will hold the number of tile parts.
1349 * @param cp the coding parameters for the image.
1350 * @param image the image to encode.
1351 * @param p_j2k the p_j2k encoder.
1352 * @param p_manager the user event manager.
1354 * @return true if the function was successful, false else.
1356 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1358 OPJ_UINT32 * p_nb_tiles,
1360 opj_event_mgr_t * p_manager);
1362 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1364 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1366 static opj_codestream_index_t* j2k_create_cstr_index(void);
1372 /* ----------------------------------------------------------------------- */
1373 typedef struct j2k_prog_order{
1374 OPJ_PROG_ORDER enum_prog;
1378 j2k_prog_order_t j2k_prog_order_list[] = {
1384 {(OPJ_PROG_ORDER)-1, ""}
1392 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1400 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1402 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1404 j2k_read_int16_to_float,
1405 j2k_read_int32_to_float,
1406 j2k_read_float32_to_float,
1407 j2k_read_float64_to_float
1410 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1412 j2k_read_int16_to_int32,
1413 j2k_read_int32_to_int32,
1414 j2k_read_float32_to_int32,
1415 j2k_read_float64_to_int32
1418 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1420 j2k_write_float_to_int16,
1421 j2k_write_float_to_int32,
1422 j2k_write_float_to_float,
1423 j2k_write_float_to_float64
1426 typedef struct opj_dec_memory_marker_handler
1430 /** value of the state when the marker can appear */
1432 /** action linked to the marker */
1433 opj_bool (*handler) (
1434 opj_j2k_v2_t *p_j2k,
1435 OPJ_BYTE * p_header_data,
1436 OPJ_UINT32 p_header_size,
1437 struct opj_event_mgr * p_manager
1440 opj_dec_memory_marker_handler_t;
1442 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1445 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1446 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1447 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1448 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1449 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1450 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1451 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1452 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1453 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1454 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1455 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1456 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1457 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1459 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1460 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1461 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1462 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1463 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1464 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1466 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1467 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1468 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1469 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1470 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1471 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1472 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1473 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
1474 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1475 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1476 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1477 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1478 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1480 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1481 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1482 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1483 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1484 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1485 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1487 #ifdef TODO_MS /* FIXME */
1488 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1489 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1490 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1491 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1493 #endif /* USE_JPWL */
1495 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1496 {J2K_MS_INSEC, 0, j2k_read_insec}
1497 #endif /* USE_JPSEC */
1498 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1503 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1505 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1506 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1510 for (i=0;i<p_nb_elem;++i) {
1511 opj_read_bytes(l_src_data,&l_temp,2);
1513 l_src_data+=sizeof(OPJ_INT16);
1515 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1519 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1521 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1522 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1526 for (i=0;i<p_nb_elem;++i) {
1527 opj_read_bytes(l_src_data,&l_temp,4);
1529 l_src_data+=sizeof(OPJ_INT32);
1531 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1535 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1537 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1538 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1542 for (i=0;i<p_nb_elem;++i) {
1543 opj_read_float(l_src_data,&l_temp);
1545 l_src_data+=sizeof(OPJ_FLOAT32);
1547 *(l_dest_data++) = l_temp;
1551 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1553 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1554 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1558 for (i=0;i<p_nb_elem;++i) {
1559 opj_read_double(l_src_data,&l_temp);
1561 l_src_data+=sizeof(OPJ_FLOAT64);
1563 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1567 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1569 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1570 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1574 for (i=0;i<p_nb_elem;++i) {
1575 opj_read_bytes(l_src_data,&l_temp,2);
1577 l_src_data+=sizeof(OPJ_INT16);
1579 *(l_dest_data++) = (OPJ_INT32) l_temp;
1583 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1585 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1586 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1590 for (i=0;i<p_nb_elem;++i) {
1591 opj_read_bytes(l_src_data,&l_temp,4);
1593 l_src_data+=sizeof(OPJ_INT32);
1595 *(l_dest_data++) = (OPJ_INT32) l_temp;
1599 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1601 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1602 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1606 for (i=0;i<p_nb_elem;++i) {
1607 opj_read_float(l_src_data,&l_temp);
1609 l_src_data+=sizeof(OPJ_FLOAT32);
1611 *(l_dest_data++) = (OPJ_INT32) l_temp;
1615 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1617 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1618 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1622 for (i=0;i<p_nb_elem;++i) {
1623 opj_read_double(l_src_data,&l_temp);
1625 l_src_data+=sizeof(OPJ_FLOAT64);
1627 *(l_dest_data++) = (OPJ_INT32) l_temp;
1631 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1633 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1634 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1638 for (i=0;i<p_nb_elem;++i) {
1639 l_temp = (OPJ_UINT32) *(l_src_data++);
1641 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1643 l_dest_data+=sizeof(OPJ_INT16);
1647 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1649 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1650 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1654 for (i=0;i<p_nb_elem;++i) {
1655 l_temp = (OPJ_UINT32) *(l_src_data++);
1657 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1659 l_dest_data+=sizeof(OPJ_INT32);
1663 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1665 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1666 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1670 for (i=0;i<p_nb_elem;++i) {
1671 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1673 opj_write_float(l_dest_data,l_temp);
1675 l_dest_data+=sizeof(OPJ_FLOAT32);
1679 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1681 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1682 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1686 for (i=0;i<p_nb_elem;++i) {
1687 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1689 opj_write_double(l_dest_data,l_temp);
1691 l_dest_data+=sizeof(OPJ_FLOAT64);
1697 * Converts an enum type progression order to string type.
1699 * @param prg_order the progression order to get.
1701 * @return the string representation of the given progression order.
1703 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1704 j2k_prog_order_t *po;
1705 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1706 if(po->enum_prog == prg_order){
1707 return po->str_prog;
1710 return po->str_prog;
1714 * Checks the progression order changes values. Tells if the poc given as input are valid.
1716 * @param p_pocs the progression order changes.
1717 * @param p_nb_pocs the number of progression order changes.
1718 * @param p_nb_resolutions the number of resolutions.
1719 * @param numcomps the number of components
1720 * @param numlayers the number of layers.
1721 * @param p_manager the user event manager.
1723 * @return true if the pocs are valid.
1725 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1726 OPJ_UINT32 p_nb_pocs,
1727 OPJ_UINT32 p_nb_resolutions,
1728 OPJ_UINT32 p_num_comps,
1729 OPJ_UINT32 p_num_layers,
1730 opj_event_mgr_t * p_manager)
1732 OPJ_UINT32* packet_array;
1733 OPJ_UINT32 index , resno, compno, layno;
1735 OPJ_UINT32 step_c = 1;
1736 OPJ_UINT32 step_r = p_num_comps * step_c;
1737 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1738 opj_bool loss = OPJ_FALSE;
1739 OPJ_UINT32 layno0 = 0;
1741 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1742 if (packet_array == 00) {
1743 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1746 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1748 if (p_nb_pocs == 0) {
1752 index = step_r * p_pocs->resno0;
1753 // take each resolution for each poc
1754 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1756 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1758 // take each comp of each resolution for each poc
1759 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1760 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1762 // and finally take each layer of each res of ...
1763 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1764 //index = step_r * resno + step_c * compno + step_l * layno;
1765 packet_array[comp_index] = 1;
1766 comp_index += step_l;
1769 res_index += step_c;
1776 // iterate through all the pocs
1777 for (i = 1; i < p_nb_pocs ; ++i) {
1778 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1780 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1781 index = step_r * p_pocs->resno0;
1783 // take each resolution for each poc
1784 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1785 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1787 // take each comp of each resolution for each poc
1788 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1789 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1791 // and finally take each layer of each res of ...
1792 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1793 //index = step_r * resno + step_c * compno + step_l * layno;
1794 packet_array[comp_index] = 1;
1795 comp_index += step_l;
1798 res_index += step_c;
1808 for (layno = 0; layno < p_num_layers ; ++layno) {
1809 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1810 for (compno = 0; compno < p_num_comps; ++compno) {
1811 loss |= (packet_array[index]!=1);
1812 //index = step_r * resno + step_c * compno + step_l * layno;
1819 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1822 opj_free(packet_array);
1827 /* ----------------------------------------------------------------------- */
1828 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1831 int tpnum=1,tpend=0;
1832 opj_tcp_t *tcp = &cp->tcps[tileno];
1833 prog = j2k_convert_progression_order(tcp->prg);
1838 if( cp->tp_flag == prog[i] ){
1839 tpend=1;cp->tp_pos=i;
1843 tpnum= tpnum * tcp->pocs[pino].compE;
1846 tpnum= tpnum * tcp->pocs[pino].resE;
1849 tpnum= tpnum * tcp->pocs[pino].prcE;
1852 tpnum= tpnum * tcp->pocs[pino].layE;
1864 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1866 * @param cp the coding parameters.
1867 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1868 * @param tileno the given tile.
1870 * @return the number of tile parts.
1872 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1874 const OPJ_CHAR *prog = 00;
1876 OPJ_UINT32 tpnum = 1;
1877 opj_tcp_v2_t *tcp = 00;
1878 opj_poc_t * l_current_poc = 00;
1881 assert(tileno < (cp->tw * cp->th));
1882 assert(pino < (cp->tcps[tileno].numpocs + 1));
1884 /* get the given tile coding parameter */
1885 tcp = &cp->tcps[tileno];
1888 l_current_poc = &(tcp->pocs[pino]);
1889 assert(l_current_poc != 0);
1891 /* get the progression order as a character string */
1892 prog = j2k_convert_progression_order(tcp->prg);
1893 assert(strlen(prog) > 0);
1895 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1899 /* component wise */
1901 tpnum *= l_current_poc->compE;
1903 /* resolution wise */
1905 tpnum *= l_current_poc->resE;
1909 tpnum *= l_current_poc->prcE;
1913 tpnum *= l_current_poc->layE;
1916 /* whould we split here ? */
1917 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1918 cp->m_specific_param.m_enc.m_tp_pos=i;
1930 /** mem allocation for TLM marker*/
1931 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1932 int pino,tileno,totnum_tp=0;
1934 OPJ_ARG_NOT_USED(img_numcomp);
1936 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1937 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1938 int cur_totnum_tp = 0;
1939 opj_tcp_t *tcp = &cp->tcps[tileno];
1940 for(pino = 0; pino <= tcp->numpocs; pino++) {
1942 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1943 if(!pi) { return -1;}
1944 tp_num = j2k_get_num_tp(cp,pino,tileno);
1945 totnum_tp = totnum_tp + tp_num;
1946 cur_totnum_tp = cur_totnum_tp + tp_num;
1947 pi_destroy(pi, cp, tileno);
1949 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1951 if (j2k->cstr_info) {
1952 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1953 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1961 * Calculates the total number of tile parts needed by the encoder to
1962 * encode such an image. If not enough memory is available, then the function return false.
1964 * @param p_nb_tiles pointer that will hold the number of tile parts.
1965 * @param cp the coding parameters for the image.
1966 * @param image the image to encode.
1967 * @param p_j2k the p_j2k encoder.
1968 * @param p_manager the user event manager.
1970 * @return true if the function was successful, false else.
1972 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1974 OPJ_UINT32 * p_nb_tiles,
1976 opj_event_mgr_t * p_manager)
1978 OPJ_UINT32 pino,tileno;
1979 OPJ_UINT32 l_nb_tiles;
1983 assert(p_nb_tiles != 00);
1985 assert(image != 00);
1986 assert(p_j2k != 00);
1987 assert(p_manager != 00);
1989 l_nb_tiles = cp->tw * cp->th;
1994 /* TODO mergeV2: check this part which use cstr_info */
1995 /*if (p_j2k->cstr_info) {
1996 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1998 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1999 OPJ_UINT32 cur_totnum_tp = 0;
2001 pi_update_encoding_parameters(image,cp,tileno);
2003 for (pino = 0; pino <= tcp->numpocs; ++pino)
2005 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2007 *p_nb_tiles = *p_nb_tiles + tp_num;
2009 cur_totnum_tp += tp_num;
2012 tcp->m_nb_tile_parts = cur_totnum_tp;
2014 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
2015 if (l_info_tile_ptr->tp == 00) {
2019 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
2021 l_info_tile_ptr->num_tps = cur_totnum_tp;
2028 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2029 OPJ_UINT32 cur_totnum_tp = 0;
2031 pi_update_encoding_parameters(image,cp,tileno);
2033 for (pino = 0; pino <= tcp->numpocs; ++pino) {
2034 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2036 *p_nb_tiles = *p_nb_tiles + tp_num;
2038 cur_totnum_tp += tp_num;
2040 tcp->m_nb_tile_parts = cur_totnum_tp;
2049 static void j2k_write_soc(opj_j2k_t *j2k) {
2050 opj_cio_t *cio = j2k->cio;
2051 cio_write(cio, J2K_MS_SOC, 2);
2054 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2059 /* update markers struct */
2060 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2061 #endif /* USE_JPWL */
2066 * Writes the SOC marker (Start Of Codestream)
2068 * @param p_stream the stream to write data to.
2069 * @param p_j2k J2K codec.
2070 * @param p_manager the user event manager.
2072 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2073 struct opj_stream_private *p_stream,
2074 struct opj_event_mgr * p_manager )
2076 /* 2 bytes will be written */
2077 OPJ_BYTE * l_start_stream = 00;
2080 assert(p_stream != 00);
2081 assert(p_j2k != 00);
2082 assert(p_manager != 00);
2084 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2086 /* write SOC identifier */
2087 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2089 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2095 /* update markers struct */
2097 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2099 assert( 0 && "TODO" );
2100 #endif /* USE_JPWL */
2109 * Reads a SOC marker (Start of Codestream)
2110 * @param p_header_data the data contained in the SOC box.
2111 * @param jp2 the jpeg2000 file codec.
2112 * @param p_header_size the size of the data contained in the SOC marker.
2113 * @param p_manager the user event manager.
2115 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2116 struct opj_stream_private *p_stream,
2117 struct opj_event_mgr * p_manager )
2119 OPJ_BYTE l_data [2];
2120 OPJ_UINT32 l_marker;
2123 assert(p_j2k != 00);
2124 assert(p_manager != 00);
2125 assert(p_stream != 00);
2127 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2131 opj_read_bytes(l_data,&l_marker,2);
2132 if (l_marker != J2K_MS_SOC) {
2136 /* Next marker should be a SIZ marker in the main header */
2137 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2139 /* FIXME move it in a index structure included in p_j2k*/
2140 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2142 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2144 /* Add the marker to the codestream index*/
2145 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2150 static void j2k_write_siz(opj_j2k_t *j2k) {
2154 opj_cio_t *cio = j2k->cio;
2155 opj_image_t *image = j2k->image;
2156 opj_cp_t *cp = j2k->cp;
2158 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2159 lenp = cio_tell(cio);
2161 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2162 cio_write(cio, image->x1, 4); /* Xsiz */
2163 cio_write(cio, image->y1, 4); /* Ysiz */
2164 cio_write(cio, image->x0, 4); /* X0siz */
2165 cio_write(cio, image->y0, 4); /* Y0siz */
2166 cio_write(cio, cp->tdx, 4); /* XTsiz */
2167 cio_write(cio, cp->tdy, 4); /* YTsiz */
2168 cio_write(cio, cp->tx0, 4); /* XT0siz */
2169 cio_write(cio, cp->ty0, 4); /* YT0siz */
2170 cio_write(cio, image->numcomps, 2); /* Csiz */
2171 for (i = 0; i < image->numcomps; i++) {
2172 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2173 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2174 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2176 len = cio_tell(cio) - lenp;
2177 cio_seek(cio, lenp);
2178 cio_write(cio, len, 2); /* Lsiz */
2179 cio_seek(cio, lenp + len);
2182 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2186 * Writes the SIZ marker (image and tile size)
2188 * @param p_stream the stream to write data to.
2189 * @param p_j2k J2K codec.
2190 * @param p_manager the user event manager.
2192 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2193 struct opj_stream_private *p_stream,
2194 struct opj_event_mgr * p_manager )
2197 OPJ_UINT32 l_size_len;
2198 OPJ_BYTE * l_current_ptr;
2199 opj_image_t * l_image = 00;
2200 opj_cp_v2_t *cp = 00;
2201 opj_image_comp_t * l_img_comp = 00;
2204 assert(p_stream != 00);
2205 assert(p_j2k != 00);
2206 assert(p_manager != 00);
2208 l_image = p_j2k->m_private_image;
2209 cp = &(p_j2k->m_cp);
2210 l_size_len = 40 + 3 * l_image->numcomps;
2211 l_img_comp = l_image->comps;
2213 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2215 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2216 = (OPJ_BYTE*)opj_realloc(
2217 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2219 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2223 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2226 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2228 /* write SOC identifier */
2229 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2232 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2235 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2238 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2241 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2244 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2247 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2250 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2253 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2256 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2259 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2262 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2265 for (i = 0; i < l_image->numcomps; ++i) {
2266 /* TODO here with MCT ? */
2267 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2270 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2273 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2279 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) {
2286 static void j2k_read_siz(opj_j2k_t *j2k) {
2290 opj_cio_t *cio = j2k->cio;
2291 opj_image_t *image = j2k->image;
2292 opj_cp_t *cp = j2k->cp;
2294 len = cio_read(cio, 2); /* Lsiz */
2295 cio_read(cio, 2); /* Rsiz (capabilities) */
2296 image->x1 = cio_read(cio, 4); /* Xsiz */
2297 image->y1 = cio_read(cio, 4); /* Ysiz */
2298 image->x0 = cio_read(cio, 4); /* X0siz */
2299 image->y0 = cio_read(cio, 4); /* Y0siz */
2300 cp->tdx = cio_read(cio, 4); /* XTsiz */
2301 cp->tdy = cio_read(cio, 4); /* YTsiz */
2302 cp->tx0 = cio_read(cio, 4); /* XT0siz */
2303 cp->ty0 = cio_read(cio, 4); /* YT0siz */
2305 /* the following code triggers: */
2306 /* warning: comparison of unsigned expression < 0 is always false */
2308 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
2309 opj_event_msg(j2k->cinfo, EVT_ERROR,
2310 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
2311 image->x0,image->x1,image->y0,image->y1);
2316 image->numcomps = cio_read(cio, 2); /* Csiz */
2319 if (j2k->cp->correct) {
2320 /* if JPWL is on, we check whether TX errors have damaged
2321 too much the SIZ parameters */
2322 if (!(image->x1 * image->y1)) {
2323 opj_event_msg(j2k->cinfo, EVT_ERROR,
2324 "JPWL: bad image size (%d x %d)\n",
2325 image->x1, image->y1);
2326 if (!JPWL_ASSUME || JPWL_ASSUME) {
2327 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2331 assert( len >= 38 );
2332 if (image->numcomps != (OPJ_UINT32)((len - 38) / 3)) {
2333 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2334 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2335 image->numcomps, ((len - 38) / 3));
2337 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2340 /* we try to correct */
2341 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
2342 if (image->numcomps < (OPJ_UINT32)((len - 38) / 3)) {
2343 len = 38 + 3 * image->numcomps;
2344 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2347 image->numcomps = ((len - 38) / 3);
2348 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2353 /* update components number in the jpwl_exp_comps filed */
2354 cp->exp_comps = image->numcomps;
2356 #endif /* USE_JPWL */
2358 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
2359 for (i = 0; i < image->numcomps; i++) {
2361 tmp = cio_read(cio, 1); /* Ssiz_i */
2362 image->comps[i].prec = (tmp & 0x7f) + 1;
2363 image->comps[i].sgnd = tmp >> 7;
2364 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
2365 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
2368 if (j2k->cp->correct) {
2369 /* if JPWL is on, we check whether TX errors have damaged
2370 too much the SIZ parameters, again */
2371 if (!(image->comps[i].dx * image->comps[i].dy)) {
2372 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2373 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2374 i, i, image->comps[i].dx, image->comps[i].dy);
2376 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2379 /* we try to correct */
2380 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2381 if (!image->comps[i].dx) {
2382 image->comps[i].dx = 1;
2383 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2384 i, image->comps[i].dx);
2386 if (!image->comps[i].dy) {
2387 image->comps[i].dy = 1;
2388 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2389 i, image->comps[i].dy);
2394 #endif /* USE_JPWL */
2396 /* TODO: unused ? */
2397 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
2398 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
2400 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
2401 image->comps[i].factor = cp->reduce; /* reducing factor per component */
2404 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2405 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2408 if (j2k->cp->correct) {
2409 /* if JPWL is on, we check whether TX errors have damaged
2410 too much the SIZ parameters */
2411 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
2412 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2413 "JPWL: bad number of tiles (%d x %d)\n",
2416 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2419 /* we try to correct */
2420 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2423 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2426 if (cp->tw > cp->max_tiles) {
2428 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
2429 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2430 cp->max_tiles, cp->tw);
2434 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2437 if (cp->th > cp->max_tiles) {
2439 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2440 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2441 cp->max_tiles, cp->th);
2445 #endif /* USE_JPWL */
2447 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2448 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
2449 cp->tileno_size = 0;
2452 if (j2k->cp->correct) {
2454 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2455 "JPWL: could not alloc tcps field of cp\n");
2456 if (!JPWL_ASSUME || JPWL_ASSUME) {
2457 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2462 #endif /* USE_JPWL */
2464 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2465 cp->tcps[i].POC = 0;
2466 cp->tcps[i].numpocs = 0;
2467 cp->tcps[i].first = 1;
2470 /* Initialization for PPM marker */
2472 cp->ppm_data = NULL;
2473 cp->ppm_data_first = NULL;
2474 cp->ppm_previous = 0;
2477 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2478 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2479 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2481 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
2482 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
2483 j2k->state = J2K_STATE_MH;
2486 if (j2k->cstr_info) {
2487 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2488 cstr_info->image_w = image->x1 - image->x0;
2489 cstr_info->image_h = image->y1 - image->y0;
2490 cstr_info->numcomps = image->numcomps;
2491 cstr_info->tw = cp->tw;
2492 cstr_info->th = cp->th;
2493 cstr_info->tile_x = cp->tdx;
2494 cstr_info->tile_y = cp->tdy;
2495 cstr_info->tile_Ox = cp->tx0;
2496 cstr_info->tile_Oy = cp->ty0;
2497 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
2503 * Reads a SIZ marker (image and tile size)
2504 * @param p_header_data the data contained in the SIZ box.
2505 * @param jp2 the jpeg2000 file codec.
2506 * @param p_header_size the size of the data contained in the SIZ marker.
2507 * @param p_manager the user event manager.
2509 opj_bool j2k_read_siz_v2 (
2510 opj_j2k_v2_t *p_j2k,
2511 OPJ_BYTE * p_header_data,
2512 OPJ_UINT32 p_header_size,
2513 struct opj_event_mgr * p_manager
2516 OPJ_UINT32 l_size, i;
2517 OPJ_UINT32 l_nb_comp;
2518 OPJ_UINT32 l_nb_comp_remain;
2519 OPJ_UINT32 l_remaining_size;
2520 OPJ_UINT32 l_nb_tiles;
2522 opj_image_t *l_image = 00;
2523 opj_cp_v2_t *l_cp = 00;
2524 opj_image_comp_t * l_img_comp = 00;
2525 opj_tcp_v2_t * l_current_tile_param = 00;
2528 assert(p_j2k != 00);
2529 assert(p_manager != 00);
2530 assert(p_header_data != 00);
2532 l_image = p_j2k->m_private_image;
2533 l_cp = &(p_j2k->m_cp);
2535 /* minimum size == 39 - 3 (= minimum component parameter) */
2536 if (p_header_size < 36) {
2537 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2541 l_remaining_size = p_header_size - 36;
2542 l_nb_comp = l_remaining_size / 3;
2543 l_nb_comp_remain = l_remaining_size % 3;
2544 if (l_nb_comp_remain != 0){
2545 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2549 l_size = p_header_size + 2; /* Lsiz */
2551 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2553 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2554 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2556 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2558 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2560 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2562 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2564 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2566 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2568 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2570 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2573 l_image->numcomps = (OPJ_UINT16) l_tmp;
2575 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2579 if (l_image->numcomps != l_nb_comp) {
2580 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);
2585 if (l_cp->correct) {
2586 /* if JPWL is on, we check whether TX errors have damaged
2587 too much the SIZ parameters */
2588 if (!(l_image->x1 * l_image->y1)) {
2589 opj_event_msg_v2(p_manager, EVT_ERROR,
2590 "JPWL: bad image size (%d x %d)\n",
2591 l_image->x1, l_image->y1);
2592 if (!JPWL_ASSUME || JPWL_ASSUME) {
2593 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2598 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2599 if (l_image->numcomps != ((len - 38) / 3)) {
2600 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2601 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2602 l_image->numcomps, ((len - 38) / 3));
2604 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2607 */ /* we try to correct */
2608 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2609 if (l_image->numcomps < ((len - 38) / 3)) {
2610 len = 38 + 3 * l_image->numcomps;
2611 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2614 l_image->numcomps = ((len - 38) / 3);
2615 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2621 /* update components number in the jpwl_exp_comps filed */
2622 l_cp->exp_comps = l_image->numcomps;
2624 #endif /* USE_JPWL */
2626 /* Allocate the resulting image components */
2627 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2628 if (l_image->comps == 00){
2629 l_image->numcomps = 0;
2630 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2634 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2635 l_img_comp = l_image->comps;
2637 /* Read the component information */
2638 for (i = 0; i < l_image->numcomps; ++i){
2640 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2642 l_img_comp->prec = (tmp & 0x7f) + 1;
2643 l_img_comp->sgnd = tmp >> 7;
2644 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2646 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2647 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2649 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2652 if (l_cp->correct) {
2653 /* if JPWL is on, we check whether TX errors have damaged
2654 too much the SIZ parameters, again */
2655 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2656 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2657 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2658 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2660 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2663 /* we try to correct */
2664 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2665 if (!l_image->comps[i].dx) {
2666 l_image->comps[i].dx = 1;
2667 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2668 i, l_image->comps[i].dx);
2670 if (!l_image->comps[i].dy) {
2671 l_image->comps[i].dy = 1;
2672 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2673 i, l_image->comps[i].dy);
2677 #endif /* USE_JPWL */
2678 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2679 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2683 /* Compute the number of tiles */
2684 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2685 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2686 l_nb_tiles = l_cp->tw * l_cp->th;
2688 /* Define the tiles which will be decoded */
2689 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2690 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;
2691 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;
2692 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);
2693 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);
2696 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2697 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2698 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2699 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2703 if (l_cp->correct) {
2704 /* if JPWL is on, we check whether TX errors have damaged
2705 too much the SIZ parameters */
2706 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2707 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2708 "JPWL: bad number of tiles (%d x %d)\n",
2709 l_cp->tw, l_cp->th);
2711 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2714 /* we try to correct */
2715 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2718 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2721 if (l_cp->tw > l_cp->max_tiles) {
2723 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2724 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2725 l_cp->max_tiles, l_cp->tw);
2729 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2732 if (l_cp->th > l_cp->max_tiles) {
2734 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2735 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2736 l_cp->max_tiles, l_cp->th);
2740 #endif /* USE_JPWL */
2742 /* memory allocations */
2743 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2744 if (l_cp->tcps == 00) {
2745 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2748 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2751 if (l_cp->correct) {
2753 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2754 "JPWL: could not alloc tcps field of cp\n");
2755 if (!JPWL_ASSUME || JPWL_ASSUME) {
2756 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2761 #endif /* USE_JPWL */
2763 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2764 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2765 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2766 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2769 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2771 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2772 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2774 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2775 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2778 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));
2779 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2781 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2782 (opj_simple_mcc_decorrelation_data_t*)
2783 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2785 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2786 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2789 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));
2790 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2792 /* set up default dc level shift */
2793 for (i=0;i<l_image->numcomps;++i) {
2794 if (! l_image->comps[i].sgnd) {
2795 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2799 l_current_tile_param = l_cp->tcps;
2800 for (i = 0; i < l_nb_tiles; ++i) {
2801 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2802 if (l_current_tile_param->tccps == 00) {
2803 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2806 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2808 ++l_current_tile_param;
2811 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2812 opj_image_comp_header_update(l_image,l_cp);
2819 static void j2k_write_com(opj_j2k_t *j2k) {
2823 if(j2k->cp->comment) {
2824 opj_cio_t *cio = j2k->cio;
2825 char *comment = j2k->cp->comment;
2827 cio_write(cio, J2K_MS_COM, 2);
2828 lenp = cio_tell(cio);
2830 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2831 for (i = 0; i < strlen(comment); i++) {
2832 cio_write(cio, comment[i], 1);
2834 len = cio_tell(cio) - lenp;
2835 cio_seek(cio, lenp);
2836 cio_write(cio, len, 2);
2837 cio_seek(cio, lenp + len);
2841 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2847 * Writes the COM marker (comment)
2849 * @param p_stream the stream to write data to.
2850 * @param p_j2k J2K codec.
2851 * @param p_manager the user event manager.
2853 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2854 struct opj_stream_private *p_stream,
2855 struct opj_event_mgr * p_manager )
2857 OPJ_UINT32 l_comment_size;
2858 OPJ_UINT32 l_total_com_size;
2859 const OPJ_CHAR *l_comment;
2860 OPJ_BYTE * l_current_ptr = 00;
2863 assert(p_j2k != 00);
2864 assert(p_stream != 00);
2865 assert(p_manager != 00);
2867 l_comment = p_j2k->m_cp.comment;
2868 l_comment_size = strlen(l_comment);
2869 l_total_com_size = l_comment_size + 6;
2871 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2872 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2873 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2876 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2880 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2883 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2885 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2888 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2891 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2894 memcpy( l_current_ptr,l_comment,l_comment_size);
2896 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) {
2903 static void j2k_read_com(opj_j2k_t *j2k) {
2906 opj_cio_t *cio = j2k->cio;
2908 len = cio_read(cio, 2);
2909 cio_skip(cio, len - 2);
2912 * Reads a COM marker (comments)
2913 * @param p_header_data the data contained in the COM box.
2914 * @param jp2 the jpeg2000 file codec.
2915 * @param p_header_size the size of the data contained in the COM marker.
2916 * @param p_manager the user event manager.
2918 opj_bool j2k_read_com_v2 (
2919 opj_j2k_v2_t *p_j2k,
2920 OPJ_BYTE * p_header_data,
2921 OPJ_UINT32 p_header_size,
2922 struct opj_event_mgr * p_manager
2926 assert(p_j2k != 00);
2927 assert(p_manager != 00);
2928 assert(p_header_data != 00);
2929 (void)p_header_size;
2934 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2937 opj_cp_t *cp = j2k->cp;
2938 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2939 opj_tccp_t *tccp = &tcp->tccps[compno];
2940 opj_cio_t *cio = j2k->cio;
2942 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2943 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2944 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2945 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2946 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2948 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2949 for (i = 0; i < tccp->numresolutions; i++) {
2950 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2955 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2958 opj_cp_t *cp = j2k->cp;
2959 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2960 opj_tccp_t *tccp = &tcp->tccps[compno];
2961 opj_cio_t *cio = j2k->cio;
2963 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2965 /* If user wants to remove more resolutions than the codestream contains, return error*/
2966 assert(cp->reduce >= 0);
2967 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2968 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2969 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2970 j2k->state |= J2K_STATE_ERR;
2973 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2974 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2975 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2976 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2977 if (tccp->csty & J2K_CP_CSTY_PRT) {
2978 for (i = 0; i < tccp->numresolutions; i++) {
2979 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2980 tccp->prcw[i] = tmp & 0xf;
2981 tccp->prch[i] = tmp >> 4;
2986 if(j2k->cstr_info && compno == 0) {
2987 for (i = 0; i < tccp->numresolutions; i++) {
2988 if (tccp->csty & J2K_CP_CSTY_PRT) {
2989 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2990 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2993 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2994 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
3001 static void j2k_write_cod(opj_j2k_t *j2k) {
3002 opj_cp_t *cp = NULL;
3003 opj_tcp_t *tcp = NULL;
3006 opj_cio_t *cio = j2k->cio;
3008 cio_write(cio, J2K_MS_COD, 2); /* COD */
3010 lenp = cio_tell(cio);
3014 tcp = &cp->tcps[j2k->curtileno];
3016 cio_write(cio, tcp->csty, 1); /* Scod */
3017 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
3018 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
3019 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
3021 j2k_write_cox(j2k, 0);
3022 len = cio_tell(cio) - lenp;
3023 cio_seek(cio, lenp);
3024 cio_write(cio, len, 2); /* Lcod */
3025 cio_seek(cio, lenp + len);
3028 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
3033 * Writes the COD marker (Coding style default)
3035 * @param p_stream the stream to write data to.
3036 * @param p_j2k J2K codec.
3037 * @param p_manager the user event manager.
3039 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
3040 struct opj_stream_private *p_stream,
3041 struct opj_event_mgr * p_manager )
3043 opj_cp_v2_t *l_cp = 00;
3044 opj_tcp_v2_t *l_tcp = 00;
3045 OPJ_UINT32 l_code_size,l_remaining_size;
3046 OPJ_BYTE * l_current_data = 00;
3049 assert(p_j2k != 00);
3050 assert(p_manager != 00);
3051 assert(p_stream != 00);
3053 l_cp = &(p_j2k->m_cp);
3054 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3055 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
3056 l_remaining_size = l_code_size;
3058 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3059 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3060 = (OPJ_BYTE*)opj_realloc(
3061 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3064 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3068 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
3071 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3073 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
3074 l_current_data += 2;
3076 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
3077 l_current_data += 2;
3079 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
3082 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
3085 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
3088 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
3091 l_remaining_size -= 9;
3093 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3094 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3098 if (l_remaining_size != 0) {
3099 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3103 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) {
3110 static void j2k_read_cod(opj_j2k_t *j2k) {
3114 opj_cio_t *cio = j2k->cio;
3115 opj_cp_t *cp = j2k->cp;
3116 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3117 opj_image_t *image = j2k->image;
3119 len = cio_read(cio, 2); /* Lcod */
3120 tcp->csty = cio_read(cio, 1); /* Scod */
3121 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
3122 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
3123 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
3125 pos = cio_tell(cio);
3126 for (i = 0; i < image->numcomps; i++) {
3127 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
3129 j2k_read_cox(j2k, i);
3133 if (j2k->cstr_info) {
3134 opj_codestream_info_t *cstr_info = j2k->cstr_info;
3135 cstr_info->prog = tcp->prg;
3136 cstr_info->numlayers = tcp->numlayers;
3137 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
3138 for (i = 0; i < image->numcomps; i++) {
3139 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
3145 * Reads a COD marker (Coding Styke defaults)
3146 * @param p_header_data the data contained in the COD box.
3147 * @param p_j2k the jpeg2000 codec.
3148 * @param p_header_size the size of the data contained in the COD marker.
3149 * @param p_manager the user event manager.
3151 opj_bool j2k_read_cod_v2 (
3152 opj_j2k_v2_t *p_j2k,
3153 OPJ_BYTE * p_header_data,
3154 OPJ_UINT32 p_header_size,
3155 struct opj_event_mgr * p_manager
3161 opj_cp_v2_t *l_cp = 00;
3162 opj_tcp_v2_t *l_tcp = 00;
3163 opj_image_t *l_image = 00;
3166 assert(p_header_data != 00);
3167 assert(p_j2k != 00);
3168 assert(p_manager != 00);
3170 l_image = p_j2k->m_private_image;
3171 l_cp = &(p_j2k->m_cp);
3173 /* If we are in the first tile-part header of the current tile */
3174 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3175 &l_cp->tcps[p_j2k->m_current_tile_number] :
3176 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3178 /* Make sure room is sufficient */
3179 if (p_header_size < 5) {
3180 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3184 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
3186 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
3188 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
3189 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
3192 /* If user didn't set a number layer to decode take the max specify in the codestream. */
3193 if (l_cp->m_specific_param.m_dec.m_layer) {
3194 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
3197 l_tcp->num_layers_to_decode = l_tcp->numlayers;
3200 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
3204 for (i = 0; i < l_image->numcomps; ++i) {
3205 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
3208 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3209 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3213 if (p_header_size != 0) {
3214 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3218 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
3219 j2k_copy_tile_component_parameters(p_j2k);
3222 #ifdef WIP_REMOVE_MSD
3223 if (p_j2k->cstr_info) {
3224 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
3225 p_j2k->cstr_info->prog = l_tcp->prg;
3226 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
3227 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
3228 for (i = 0; i < l_image->numcomps; ++i) {
3229 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
3237 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
3240 opj_cp_t *cp = j2k->cp;
3241 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3242 opj_image_t *image = j2k->image;
3243 opj_cio_t *cio = j2k->cio;
3245 cio_write(cio, J2K_MS_COC, 2); /* COC */
3246 lenp = cio_tell(cio);
3248 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3249 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
3250 j2k_write_cox(j2k, compno);
3251 len = cio_tell(cio) - lenp;
3252 cio_seek(cio, lenp);
3253 cio_write(cio, len, 2); /* Lcoc */
3254 cio_seek(cio, lenp + len);
3258 * Writes the COC marker (Coding style component)
3260 * @param p_comp_no the index of the component to output.
3261 * @param p_stream the stream to write data to.
3262 * @param p_j2k J2K codec.
3263 * @param p_manager the user event manager.
3265 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
3266 OPJ_UINT32 p_comp_no,
3267 struct opj_stream_private *p_stream,
3268 struct opj_event_mgr * p_manager )
3270 OPJ_UINT32 l_coc_size,l_remaining_size;
3271 OPJ_UINT32 l_comp_room;
3274 assert(p_j2k != 00);
3275 assert(p_manager != 00);
3276 assert(p_stream != 00);
3278 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3280 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3282 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3283 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3284 = (OPJ_BYTE*)opj_realloc(
3285 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3287 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3291 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3294 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);
3296 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) {
3304 * Writes the COC marker (Coding style component)
3306 * @param p_comp_no the index of the component to output.
3307 * @param p_stream the stream to write data to.
3308 * @param p_j2k J2K codec.
3309 * @param p_manager the user event manager.
3311 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3312 OPJ_UINT32 p_comp_no,
3314 OPJ_UINT32 * p_data_written,
3315 struct opj_event_mgr * p_manager )
3317 opj_cp_v2_t *l_cp = 00;
3318 opj_tcp_v2_t *l_tcp = 00;
3319 OPJ_UINT32 l_coc_size,l_remaining_size;
3320 OPJ_BYTE * l_current_data = 00;
3321 opj_image_t *l_image = 00;
3322 OPJ_UINT32 l_comp_room;
3325 assert(p_j2k != 00);
3326 assert(p_manager != 00);
3328 l_cp = &(p_j2k->m_cp);
3329 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3330 l_image = p_j2k->m_private_image;
3331 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3333 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3334 l_remaining_size = l_coc_size;
3336 l_current_data = p_data;
3338 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3339 l_current_data += 2;
3341 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3342 l_current_data += 2;
3344 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3345 l_current_data+=l_comp_room;
3347 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3350 l_remaining_size -= (5 + l_comp_room);
3351 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3352 * p_data_written = l_coc_size;
3356 * Gets the maximum size taken by a coc.
3358 * @param p_j2k the jpeg2000 codec to use.
3360 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3363 OPJ_UINT32 l_nb_comp;
3364 OPJ_UINT32 l_nb_tiles;
3365 OPJ_UINT32 l_max = 0;
3369 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3370 l_nb_comp = p_j2k->m_private_image->numcomps;
3372 for (i=0;i<l_nb_tiles;++i) {
3373 for (j=0;j<l_nb_comp;++j) {
3374 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3381 static void j2k_read_coc(opj_j2k_t *j2k) {
3384 opj_cp_t *cp = j2k->cp;
3385 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3386 opj_image_t *image = j2k->image;
3387 opj_cio_t *cio = j2k->cio;
3389 len = cio_read(cio, 2); /* Lcoc */
3390 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3391 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
3392 j2k_read_cox(j2k, compno);
3396 * Reads a COC marker (Coding Style Component)
3397 * @param p_header_data the data contained in the COC box.
3398 * @param p_j2k the jpeg2000 codec.
3399 * @param p_header_size the size of the data contained in the COC marker.
3400 * @param p_manager the user event manager.
3402 opj_bool j2k_read_coc_v2 (
3403 opj_j2k_v2_t *p_j2k,
3404 OPJ_BYTE * p_header_data,
3405 OPJ_UINT32 p_header_size,
3406 struct opj_event_mgr * p_manager
3409 opj_cp_v2_t *l_cp = NULL;
3410 opj_tcp_v2_t *l_tcp = NULL;
3411 opj_image_t *l_image = NULL;
3412 OPJ_UINT32 l_comp_room;
3413 OPJ_UINT32 l_comp_no;
3416 assert(p_header_data != 00);
3417 assert(p_j2k != 00);
3418 assert(p_manager != 00);
3420 l_cp = &(p_j2k->m_cp);
3421 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3422 &l_cp->tcps[p_j2k->m_current_tile_number] :
3423 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3424 l_image = p_j2k->m_private_image;
3426 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3428 /* make sure room is sufficient*/
3429 if (p_header_size < l_comp_room + 1) {
3430 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3433 p_header_size -= l_comp_room + 1;
3435 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3436 p_header_data += l_comp_room;
3437 if (l_comp_no >= l_image->numcomps) {
3438 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3442 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3445 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3446 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3450 if (p_header_size != 0) {
3451 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3457 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3458 int bandno, numbands;
3461 opj_cp_t *cp = j2k->cp;
3462 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3463 opj_tccp_t *tccp = &tcp->tccps[compno];
3464 opj_cio_t *cio = j2k->cio;
3466 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3467 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3469 for (bandno = 0; bandno < numbands; bandno++) {
3470 expn = tccp->stepsizes[bandno].expn;
3471 mant = tccp->stepsizes[bandno].mant;
3473 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3474 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3476 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3481 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3483 int bandno, numbands;
3485 opj_cp_t *cp = j2k->cp;
3486 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3487 opj_tccp_t *tccp = &tcp->tccps[compno];
3488 opj_cio_t *cio = j2k->cio;
3490 tmp = cio_read(cio, 1); /* Sqcx */
3491 tccp->qntsty = tmp & 0x1f;
3492 tccp->numgbits = tmp >> 5;
3493 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3494 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3497 if (j2k->cp->correct) {
3499 /* if JPWL is on, we check whether there are too many subbands */
3500 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3501 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3502 "JPWL: bad number of subbands in Sqcx (%d)\n",
3505 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3508 /* we try to correct */
3510 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3511 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3518 /* We check whether there are too many subbands */
3519 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3520 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3521 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3522 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3525 #endif /* USE_JPWL */
3527 for (bandno = 0; bandno < numbands; bandno++) {
3529 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3530 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3533 tmp = cio_read(cio, 2); /* SPqcx_i */
3537 if (bandno < J2K_MAXBANDS){
3538 tccp->stepsizes[bandno].expn = expn;
3539 tccp->stepsizes[bandno].mant = mant;
3543 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3544 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3545 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3546 tccp->stepsizes[bandno].expn =
3547 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3548 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3549 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3555 static void j2k_write_qcd(opj_j2k_t *j2k) {
3558 opj_cio_t *cio = j2k->cio;
3560 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3561 lenp = cio_tell(cio);
3563 j2k_write_qcx(j2k, 0);
3564 len = cio_tell(cio) - lenp;
3565 cio_seek(cio, lenp);
3566 cio_write(cio, len, 2); /* Lqcd */
3567 cio_seek(cio, lenp + len);
3570 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3574 * Writes the QCD marker (quantization default)
3576 * @param p_comp_number the index of the component to output.
3577 * @param p_stream the stream to write data to.
3578 * @param p_j2k J2K codec.
3579 * @param p_manager the user event manager.
3581 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3582 struct opj_stream_private *p_stream,
3583 struct opj_event_mgr * p_manager )
3585 opj_cp_v2_t *l_cp = 00;
3586 opj_tcp_v2_t *l_tcp = 00;
3587 OPJ_UINT32 l_qcd_size,l_remaining_size;
3588 OPJ_BYTE * l_current_data = 00;
3591 assert(p_j2k != 00);
3592 assert(p_manager != 00);
3593 assert(p_stream != 00);
3595 l_cp = &(p_j2k->m_cp);
3596 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3597 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3598 l_remaining_size = l_qcd_size;
3600 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3601 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3602 = (OPJ_BYTE*)opj_realloc(
3603 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3606 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3610 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3613 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3615 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3616 l_current_data += 2;
3618 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3619 l_current_data += 2;
3621 l_remaining_size -= 4;
3623 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3624 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3628 if (l_remaining_size != 0) {
3629 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3633 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) {
3641 static void j2k_read_qcd(opj_j2k_t *j2k) {
3645 opj_cio_t *cio = j2k->cio;
3646 opj_image_t *image = j2k->image;
3648 len = cio_read(cio, 2); /* Lqcd */
3649 pos = cio_tell(cio);
3650 for (i = 0; i < image->numcomps; i++) {
3652 j2k_read_qcx(j2k, i, len - 2);
3657 * Reads a QCD marker (Quantization defaults)
3658 * @param p_header_data the data contained in the QCD box.
3659 * @param p_j2k the jpeg2000 codec.
3660 * @param p_header_size the size of the data contained in the QCD marker.
3661 * @param p_manager the user event manager.
3663 opj_bool j2k_read_qcd_v2 (
3664 opj_j2k_v2_t *p_j2k,
3665 OPJ_BYTE * p_header_data,
3666 OPJ_UINT32 p_header_size,
3667 struct opj_event_mgr * p_manager
3671 assert(p_header_data != 00);
3672 assert(p_j2k != 00);
3673 assert(p_manager != 00);
3675 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3676 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3680 if (p_header_size != 0) {
3681 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3685 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3686 j2k_copy_tile_quantization_parameters(p_j2k);
3691 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3694 opj_cio_t *cio = j2k->cio;
3696 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3697 lenp = cio_tell(cio);
3699 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3700 j2k_write_qcx(j2k, compno);
3701 len = cio_tell(cio) - lenp;
3702 cio_seek(cio, lenp);
3703 cio_write(cio, len, 2); /* Lqcc */
3704 cio_seek(cio, lenp + len);
3708 * Writes the QCC marker (quantization component)
3710 * @param p_comp_no the index of the component to output.
3711 * @param p_stream the stream to write data to.
3712 * @param p_j2k J2K codec.
3713 * @param p_manager the user event manager.
3715 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3716 OPJ_UINT32 p_comp_no,
3717 struct opj_stream_private *p_stream,
3718 struct opj_event_mgr * p_manager )
3720 OPJ_UINT32 l_qcc_size,l_remaining_size;
3723 assert(p_j2k != 00);
3724 assert(p_manager != 00);
3725 assert(p_stream != 00);
3727 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3728 l_remaining_size = l_qcc_size;
3730 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3731 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3732 = (OPJ_BYTE*)opj_realloc(
3733 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3735 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3739 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3742 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);
3744 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) {
3752 * Writes the QCC marker (quantization component)
3754 * @param p_comp_no the index of the component to output.
3755 * @param p_stream the stream to write data to.
3756 * @param p_j2k J2K codec.
3757 * @param p_manager the user event manager.
3759 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3760 OPJ_UINT32 p_comp_no,
3762 OPJ_UINT32 * p_data_written,
3763 struct opj_event_mgr * p_manager )
3765 OPJ_UINT32 l_qcc_size,l_remaining_size;
3766 OPJ_BYTE * l_current_data = 00;
3769 assert(p_j2k != 00);
3770 assert(p_manager != 00);
3772 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3773 l_remaining_size = l_qcc_size;
3775 l_current_data = p_data;
3777 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3778 l_current_data += 2;
3780 if (p_j2k->m_private_image->numcomps <= 256) {
3783 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3784 l_current_data += 2;
3786 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3789 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3790 l_remaining_size -= 6;
3793 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3794 l_current_data += 2;
3796 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3799 l_remaining_size -= 6;
3802 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3804 *p_data_written = l_qcc_size;
3808 * Gets the maximum size taken by a qcc.
3810 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3812 return j2k_get_max_coc_size(p_j2k);
3815 static void j2k_read_qcc(opj_j2k_t *j2k) {
3817 int numcomp = j2k->image->numcomps;
3818 opj_cio_t *cio = j2k->cio;
3820 len = cio_read(cio, 2); /* Lqcc */
3821 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3824 if (j2k->cp->correct) {
3826 static int backup_compno = 0;
3828 /* compno is negative or larger than the number of components!!! */
3829 if ((compno < 0) || (compno >= numcomp)) {
3830 opj_event_msg(j2k->cinfo, EVT_ERROR,
3831 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3834 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3837 /* we try to correct */
3838 compno = backup_compno % numcomp;
3839 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3840 "- setting component number to %d\n",
3844 /* keep your private count of tiles */
3847 #endif /* USE_JPWL */
3849 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3853 * Reads a QCC marker (Quantization component)
3854 * @param p_header_data the data contained in the QCC box.
3855 * @param p_j2k the jpeg2000 codec.
3856 * @param p_header_size the size of the data contained in the QCC marker.
3857 * @param p_manager the user event manager.
3859 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3860 OPJ_BYTE * p_header_data,
3861 OPJ_UINT32 p_header_size,
3862 struct opj_event_mgr * p_manager)
3864 OPJ_UINT32 l_num_comp,l_comp_no;
3867 assert(p_header_data != 00);
3868 assert(p_j2k != 00);
3869 assert(p_manager != 00);
3871 l_num_comp = p_j2k->m_private_image->numcomps;
3873 if (l_num_comp <= 256) {
3874 if (p_header_size < 1) {
3875 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3878 opj_read_bytes(p_header_data,&l_comp_no,1);
3883 if (p_header_size < 2) {
3884 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3887 opj_read_bytes(p_header_data,&l_comp_no,2);
3893 if (p_j2k->m_cp.correct) {
3895 static OPJ_UINT32 backup_compno = 0;
3897 /* compno is negative or larger than the number of components!!! */
3898 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3899 opj_event_msg_v2(p_manager, EVT_ERROR,
3900 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3901 l_comp_no, l_num_comp);
3903 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3906 /* we try to correct */
3907 l_comp_no = backup_compno % l_num_comp;
3908 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3909 "- setting component number to %d\n",
3913 /* keep your private count of tiles */
3916 #endif /* USE_JPWL */
3918 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3919 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3923 if (p_header_size != 0) {
3924 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3932 static void j2k_write_poc(opj_j2k_t *j2k) {
3933 int len, numpchgs, i;
3935 int numcomps = j2k->image->numcomps;
3937 opj_cp_t *cp = j2k->cp;
3938 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3939 opj_tccp_t *tccp = &tcp->tccps[0];
3940 opj_cio_t *cio = j2k->cio;
3942 numpchgs = 1 + tcp->numpocs;
3943 cio_write(cio, J2K_MS_POC, 2); /* POC */
3944 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3945 cio_write(cio, len, 2); /* Lpoc */
3946 for (i = 0; i < numpchgs; i++) {
3947 opj_poc_t *poc = &tcp->pocs[i];
3948 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3949 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3950 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3951 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3952 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3953 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3954 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3955 poc->compno1 = int_min(poc->compno1, numcomps);
3956 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3961 * Writes the POC marker (Progression Order Change)
3963 * @param p_stream the stream to write data to.
3964 * @param p_j2k J2K codec.
3965 * @param p_manager the user event manager.
3967 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3968 struct opj_stream_private *p_stream,
3969 struct opj_event_mgr * p_manager )
3971 OPJ_UINT32 l_nb_comp;
3972 OPJ_UINT32 l_nb_poc;
3973 OPJ_UINT32 l_poc_size;
3974 OPJ_UINT32 l_written_size = 0;
3975 opj_tcp_v2_t *l_tcp = 00;
3976 opj_tccp_t *l_tccp = 00;
3977 OPJ_UINT32 l_poc_room;
3980 assert(p_j2k != 00);
3981 assert(p_manager != 00);
3982 assert(p_stream != 00);
3984 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3985 l_tccp = &l_tcp->tccps[0];
3986 l_nb_comp = p_j2k->m_private_image->numcomps;
3987 l_nb_poc = 1 + l_tcp->numpocs;
3989 if (l_nb_comp <= 256) {
3995 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3997 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3998 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3999 = (OPJ_BYTE*)opj_realloc(
4000 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4003 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4007 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
4010 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
4012 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) {
4021 * Writes the POC marker (Progression Order Change)
4023 * @param p_stream the stream to write data to.
4024 * @param p_j2k J2K codec.
4025 * @param p_manager the user event manager.
4027 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
4029 OPJ_UINT32 * p_data_written,
4030 struct opj_event_mgr * p_manager )
4033 OPJ_BYTE * l_current_data = 00;
4034 OPJ_UINT32 l_nb_comp;
4035 OPJ_UINT32 l_nb_poc;
4036 OPJ_UINT32 l_poc_size;
4037 opj_image_t *l_image = 00;
4038 opj_tcp_v2_t *l_tcp = 00;
4039 opj_tccp_t *l_tccp = 00;
4040 opj_poc_t *l_current_poc = 00;
4041 OPJ_UINT32 l_poc_room;
4044 assert(p_j2k != 00);
4045 assert(p_manager != 00);
4047 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4048 l_tccp = &l_tcp->tccps[0];
4049 l_image = p_j2k->m_private_image;
4050 l_nb_comp = l_image->numcomps;
4051 l_nb_poc = 1 + l_tcp->numpocs;
4053 if (l_nb_comp <= 256) {
4060 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4062 l_current_data = p_data;
4064 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
4065 l_current_data += 2;
4067 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
4068 l_current_data += 2;
4070 l_current_poc = l_tcp->pocs;
4071 for (i = 0; i < l_nb_poc; ++i) {
4072 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
4075 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
4076 l_current_data+=l_poc_room;
4078 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
4081 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
4084 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
4085 l_current_data+=l_poc_room;
4087 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
4090 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
4091 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
4092 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
4093 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
4098 *p_data_written = l_poc_size;
4102 * Gets the maximum size taken by the writing of a POC.
4104 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
4106 opj_tcp_v2_t * l_tcp = 00;
4107 OPJ_UINT32 l_nb_tiles = 0;
4108 OPJ_UINT32 l_max_poc = 0;
4111 l_tcp = p_j2k->m_cp.tcps;
4112 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4114 for (i=0;i<l_nb_tiles;++i) {
4115 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
4121 return 4 + 9 * l_max_poc;
4125 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
4127 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
4130 OPJ_UINT32 l_nb_tiles;
4131 OPJ_UINT32 l_max = 0;
4132 opj_tcp_v2_t * l_tcp = 00;
4134 l_tcp = p_j2k->m_cp.tcps;
4135 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
4137 for (i=0;i<l_nb_tiles;++i) {
4138 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
4148 * Gets the maximum size taken by the headers of the SOT.
4150 * @param p_j2k the jpeg2000 codec to use.
4152 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
4154 OPJ_UINT32 l_nb_bytes = 0;
4155 OPJ_UINT32 l_nb_comps;
4156 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
4158 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
4159 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
4161 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
4162 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
4163 l_nb_bytes += l_nb_comps * l_coc_bytes;
4165 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
4166 l_nb_bytes += l_nb_comps * l_qcc_bytes;
4169 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
4171 /*** DEVELOPER CORNER, Add room for your headers ***/
4176 static void j2k_read_poc(opj_j2k_t *j2k) {
4177 int len, numpchgs, i, old_poc;
4179 int numcomps = j2k->image->numcomps;
4181 opj_cp_t *cp = j2k->cp;
4182 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
4183 opj_cio_t *cio = j2k->cio;
4185 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
4187 len = cio_read(cio, 2); /* Lpoc */
4188 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
4190 for (i = old_poc; i < numpchgs + old_poc; i++) {
4192 poc = &tcp->pocs[i];
4193 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
4194 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
4195 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
4196 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
4197 poc->compno1 = int_min(
4198 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
4199 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
4202 tcp->numpocs = numpchgs + old_poc - 1;
4206 * Reads a POC marker (Progression Order Change)
4208 * @param p_header_data the data contained in the POC box.
4209 * @param p_j2k the jpeg2000 codec.
4210 * @param p_header_size the size of the data contained in the POC marker.
4211 * @param p_manager the user event manager.
4213 opj_bool j2k_read_poc_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)
4219 OPJ_UINT32 i, l_nb_comp, l_tmp;
4220 opj_image_t * l_image = 00;
4221 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
4222 OPJ_UINT32 l_chunk_size, l_comp_room;
4224 opj_cp_v2_t *l_cp = 00;
4225 opj_tcp_v2_t *l_tcp = 00;
4226 opj_poc_t *l_current_poc = 00;
4229 assert(p_header_data != 00);
4230 assert(p_j2k != 00);
4231 assert(p_manager != 00);
4233 l_image = p_j2k->m_private_image;
4234 l_nb_comp = l_image->numcomps;
4235 if (l_nb_comp <= 256) {
4241 l_chunk_size = 5 + 2 * l_comp_room;
4242 l_current_poc_nb = p_header_size / l_chunk_size;
4243 l_current_poc_remaining = p_header_size % l_chunk_size;
4245 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
4246 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
4250 l_cp = &(p_j2k->m_cp);
4251 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4252 &l_cp->tcps[p_j2k->m_current_tile_number] :
4253 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4254 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
4255 l_current_poc_nb += l_old_poc_nb;
4257 assert(l_current_poc_nb < 32);
4259 /* now poc is in use.*/
4262 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
4263 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
4264 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
4266 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
4267 p_header_data+=l_comp_room;
4268 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
4270 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
4272 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
4273 p_header_data+=l_comp_room;
4274 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
4276 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4277 /* make sure comp is in acceptable bounds */
4278 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4282 l_tcp->numpocs = l_current_poc_nb - 1;
4286 static void j2k_read_crg(opj_j2k_t *j2k) {
4287 int len, i, Xcrg_i, Ycrg_i;
4289 opj_cio_t *cio = j2k->cio;
4290 int numcomps = j2k->image->numcomps;
4292 len = cio_read(cio, 2); /* Lcrg */
4293 for (i = 0; i < numcomps; i++) {
4294 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
4295 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4300 * Reads a CRG marker (Component registration)
4302 * @param p_header_data the data contained in the TLM box.
4303 * @param p_j2k the jpeg2000 codec.
4304 * @param p_header_size the size of the data contained in the TLM marker.
4305 * @param p_manager the user event manager.
4307 opj_bool j2k_read_crg_v2 (
4308 opj_j2k_v2_t *p_j2k,
4309 OPJ_BYTE * p_header_data,
4310 OPJ_UINT32 p_header_size,
4311 struct opj_event_mgr * p_manager
4314 OPJ_UINT32 l_nb_comp;
4316 assert(p_header_data != 00);
4317 assert(p_j2k != 00);
4318 assert(p_manager != 00);
4320 l_nb_comp = p_j2k->m_private_image->numcomps;
4322 if (p_header_size != l_nb_comp *4) {
4323 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4326 /* Do not care of this at the moment since only local variables are set here */
4329 (i = 0; i < l_nb_comp; ++i)
4331 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4333 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4340 static void j2k_read_tlm(opj_j2k_t *j2k) {
4341 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4342 long int Ttlm_i, Ptlm_i;
4344 opj_cio_t *cio = j2k->cio;
4346 len = cio_read(cio, 2); /* Ltlm */
4347 Ztlm = cio_read(cio, 1); /* Ztlm */
4348 Stlm = cio_read(cio, 1); /* Stlm */
4349 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4350 SP = (Stlm >> 6) & 0x01;
4351 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4352 for (i = 0; i < tile_tlm; i++) {
4353 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4354 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4359 * Reads a TLM marker (Tile Length Marker)
4361 * @param p_header_data the data contained in the TLM box.
4362 * @param p_j2k the jpeg2000 codec.
4363 * @param p_header_size the size of the data contained in the TLM marker.
4364 * @param p_manager the user event manager.
4366 opj_bool j2k_read_tlm_v2 (
4367 opj_j2k_v2_t *p_j2k,
4368 OPJ_BYTE * p_header_data,
4369 OPJ_UINT32 p_header_size,
4370 struct opj_event_mgr * p_manager
4373 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4375 assert(p_header_data != 00);
4376 assert(p_j2k != 00);
4377 assert(p_manager != 00);
4379 if (p_header_size < 2) {
4380 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4385 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4387 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4390 l_ST = ((l_Stlm >> 4) & 0x3);
4391 l_SP = (l_Stlm >> 6) & 0x1;
4393 l_Ptlm_size = (l_SP + 1) * 2;
4394 l_quotient = l_Ptlm_size + l_ST;
4396 l_tot_num_tp = p_header_size / l_quotient;
4397 l_tot_num_tp_remaining = p_header_size % l_quotient;
4399 if (l_tot_num_tp_remaining != 0) {
4400 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4403 /* FIXME Do not care of this at the moment since only local variables are set here */
4406 (i = 0; i < l_tot_num_tp; ++i)
4408 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4409 p_header_data += l_ST;
4410 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4411 p_header_data += l_Ptlm_size;
4416 static void j2k_read_plm(opj_j2k_t *j2k) {
4417 int len, i, Zplm, Nplm, add, packet_len = 0;
4419 opj_cio_t *cio = j2k->cio;
4421 len = cio_read(cio, 2); /* Lplm */
4422 Zplm = cio_read(cio, 1); /* Zplm */
4425 Nplm = cio_read(cio, 4); /* Nplm */
4427 for (i = Nplm; i > 0; i--) {
4428 add = cio_read(cio, 1);
4430 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4431 if ((add & 0x80) == 0) {
4442 * Reads a PLM marker (Packet length, main header marker)
4444 * @param p_header_data the data contained in the TLM box.
4445 * @param p_j2k the jpeg2000 codec.
4446 * @param p_header_size the size of the data contained in the TLM marker.
4447 * @param p_manager the user event manager.
4449 opj_bool j2k_read_plm_v2 (
4450 opj_j2k_v2_t *p_j2k,
4451 OPJ_BYTE * p_header_data,
4452 OPJ_UINT32 p_header_size,
4453 struct opj_event_mgr * p_manager
4457 assert(p_header_data != 00);
4458 assert(p_j2k != 00);
4459 assert(p_manager != 00);
4461 if (p_header_size < 1) {
4462 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4465 /* Do not care of this at the moment since only local variables are set here */
4467 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4474 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4476 p_header_size -= (1+l_Nplm);
4480 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4484 (i = 0; i < l_Nplm; ++i)
4486 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4488 // take only the last seven bytes
4489 l_packet_len |= (l_tmp & 0x7f);
4497 // store packet length and proceed to next packet
4504 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4512 static void j2k_read_plt(opj_j2k_t *j2k) {
4513 int len, i, Zplt, packet_len = 0, add;
4515 opj_cio_t *cio = j2k->cio;
4517 len = cio_read(cio, 2); /* Lplt */
4518 Zplt = cio_read(cio, 1); /* Zplt */
4519 for (i = len - 3; i > 0; i--) {
4520 add = cio_read(cio, 1);
4521 packet_len = (packet_len << 7) + add; /* Iplt_i */
4522 if ((add & 0x80) == 0) {
4530 * Reads a PLT marker (Packet length, tile-part header)
4532 * @param p_header_data the data contained in the PLT box.
4533 * @param p_j2k the jpeg2000 codec.
4534 * @param p_header_size the size of the data contained in the PLT marker.
4535 * @param p_manager the user event manager.
4537 opj_bool j2k_read_plt_v2 (
4538 opj_j2k_v2_t *p_j2k,
4539 OPJ_BYTE * p_header_data,
4540 OPJ_UINT32 p_header_size,
4541 struct opj_event_mgr * p_manager
4544 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4547 assert(p_header_data != 00);
4548 assert(p_j2k != 00);
4549 assert(p_manager != 00);
4551 if (p_header_size < 1) {
4552 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4556 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4560 for (i = 0; i < p_header_size; ++i) {
4561 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4563 /* take only the last seven bytes */
4564 l_packet_len |= (l_tmp & 0x7f);
4569 /* store packet length and proceed to next packet */
4574 if (l_packet_len != 0) {
4575 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4582 static void j2k_read_ppm(opj_j2k_t *j2k) {
4583 int len, Z_ppm, i, j;
4586 opj_cp_t *cp = j2k->cp;
4587 opj_cio_t *cio = j2k->cio;
4589 len = cio_read(cio, 2);
4592 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4595 if (cp->ppm_previous == 0) {
4596 N_ppm = cio_read(cio, 4); /* N_ppm */
4599 N_ppm = cp->ppm_previous;
4602 if (Z_ppm == 0) { /* First PPM marker */
4603 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4604 cp->ppm_data_first = cp->ppm_data;
4605 cp->ppm_len = N_ppm;
4606 } else { /* NON-first PPM marker */
4607 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4610 /* this memory allocation check could be done even in non-JPWL cases */
4612 if (!cp->ppm_data) {
4613 opj_event_msg(j2k->cinfo, EVT_ERROR,
4614 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4616 if (!JPWL_ASSUME || JPWL_ASSUME) {
4617 opj_free(cp->ppm_data);
4618 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4625 cp->ppm_data_first = cp->ppm_data;
4626 cp->ppm_len = N_ppm + cp->ppm_store;
4628 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4629 cp->ppm_data[j] = cio_read(cio, 1);
4633 break; /* Case of non-finished packet header in present marker but finished in next one */
4635 cp->ppm_previous = i - 1;
4640 * Reads a PPM marker (Packed packet headers, main header)
4642 * @param p_header_data the data contained in the POC box.
4643 * @param p_j2k the jpeg2000 codec.
4644 * @param p_header_size the size of the data contained in the POC marker.
4645 * @param p_manager the user event manager.
4648 opj_bool j2k_read_ppm_v2 (
4649 opj_j2k_v2_t *p_j2k,
4650 OPJ_BYTE * p_header_data,
4651 OPJ_UINT32 p_header_size,
4652 struct opj_event_mgr * p_manager
4656 opj_cp_v2_t *l_cp = 00;
4657 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4660 assert(p_header_data != 00);
4661 assert(p_j2k != 00);
4662 assert(p_manager != 00);
4664 if (p_header_size < 1) {
4665 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4669 l_cp = &(p_j2k->m_cp);
4672 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4676 /* First PPM marker */
4678 if (p_header_size < 4) {
4679 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4683 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4687 /* First PPM marker: Initialization */
4688 l_cp->ppm_len = l_N_ppm;
4689 l_cp->ppm_data_size = 0;
4691 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4692 if (l_cp->ppm_buffer == 00) {
4693 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4696 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4698 l_cp->ppm_data = l_cp->ppm_buffer;
4702 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4703 if (p_header_size >= 4) {
4705 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4708 l_cp->ppm_len += l_N_ppm ;
4710 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4711 if (l_cp->ppm_buffer == 00) {
4712 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4715 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4717 l_cp->ppm_data = l_cp->ppm_buffer;
4724 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4726 if (l_remaining_data <= p_header_size) {
4727 /* we must store less information than available in the packet */
4728 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4729 l_cp->ppm_data_size = l_cp->ppm_len;
4730 p_header_size -= l_remaining_data;
4731 p_header_data += l_remaining_data;
4734 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4735 l_cp->ppm_data_size += p_header_size;
4736 p_header_data += p_header_size;
4749 * Reads a PPM marker (Packed packet headers, main header)
4751 * @param p_header_data the data contained in the POC box.
4752 * @param p_j2k the jpeg2000 codec.
4753 * @param p_header_size the size of the data contained in the POC marker.
4754 * @param p_manager the user event manager.
4756 opj_bool j2k_read_ppm_v3 (
4757 opj_j2k_v2_t *p_j2k,
4758 OPJ_BYTE * p_header_data,
4759 OPJ_UINT32 p_header_size,
4760 struct opj_event_mgr * p_manager
4763 opj_cp_v2_t *l_cp = 00;
4764 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4767 assert(p_header_data != 00);
4768 assert(p_j2k != 00);
4769 assert(p_manager != 00);
4771 /* Minimum size of PPM marker is equal to the size of Zppm element */
4772 if (p_header_size < 1) {
4773 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4777 l_cp = &(p_j2k->m_cp);
4780 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4784 /* First PPM marker */
4786 /* We need now at least the Nppm^0 element */
4787 if (p_header_size < 4) {
4788 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4792 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4796 /* First PPM marker: Initialization */
4797 l_cp->ppm_len = l_N_ppm;
4798 l_cp->ppm_data_read = 0;
4800 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4801 if (l_cp->ppm_data == 00) {
4802 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4805 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4807 l_cp->ppm_data_current = l_cp->ppm_data;
4809 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4812 if (p_header_size < 4) {
4813 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4817 /* Uncompleted Ippm series in the previous PPM marker?*/
4818 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4819 /* Get the place where add the remaining Ippm series*/
4820 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4821 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4824 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4828 /* Increase the size of ppm_data to add the new Ippm series*/
4829 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4831 /* Keep the position of the place where concatenate the new series*/
4832 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4833 l_cp->ppm_len += l_N_ppm;
4838 l_remaining_data = p_header_size;
4840 while (l_remaining_data >= l_N_ppm) {
4841 /* read a complete Ippm series*/
4842 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4843 p_header_size -= l_N_ppm;
4844 p_header_data += l_N_ppm;
4846 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4850 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4855 l_remaining_data = p_header_size;
4859 l_remaining_data = p_header_size;
4861 /* Next Ippm series is a complete series ?*/
4862 if (l_remaining_data > l_N_ppm) {
4863 /* Increase the size of ppm_data to add the new Ippm series*/
4864 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4866 /* Keep the position of the place where concatenate the new series */
4867 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4868 l_cp->ppm_len += l_N_ppm;
4873 /* Need to read an incomplete Ippm series*/
4874 if (l_remaining_data) {
4875 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4877 /* Keep the position of the place where concatenate the new series*/
4878 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4879 l_cp->ppm_len += l_N_ppm;
4881 /* Read incomplete Ippm series*/
4882 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4883 p_header_size -= l_remaining_data;
4884 p_header_data += l_remaining_data;
4886 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4891 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4892 if (p_header_size >= 4) {
4894 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4897 l_cp->ppm_len += l_N_ppm ;
4899 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4900 if (l_cp->ppm_buffer == 00) {
4901 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4904 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4906 l_cp->ppm_data = l_cp->ppm_buffer;
4913 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4915 if (l_remaining_data <= p_header_size) {
4916 /* we must store less information than available in the packet */
4917 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4918 l_cp->ppm_data_size = l_cp->ppm_len;
4919 p_header_size -= l_remaining_data;
4920 p_header_data += l_remaining_data;
4923 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4924 l_cp->ppm_data_size += p_header_size;
4925 p_header_data += p_header_size;
4934 static void j2k_read_ppt(opj_j2k_t *j2k) {
4935 int len, Z_ppt, i, j = 0;
4937 opj_cp_t *cp = j2k->cp;
4938 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4939 opj_cio_t *cio = j2k->cio;
4941 len = cio_read(cio, 2);
4942 Z_ppt = cio_read(cio, 1);
4944 if (Z_ppt == 0) { /* First PPT marker */
4945 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4946 tcp->ppt_data_first = tcp->ppt_data;
4948 tcp->ppt_len = len - 3;
4949 } else { /* NON-first PPT marker */
4950 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4951 tcp->ppt_data_first = tcp->ppt_data;
4952 tcp->ppt_len = len - 3 + tcp->ppt_store;
4955 for (i = len - 3; i > 0; i--) {
4956 tcp->ppt_data[j] = cio_read(cio, 1);
4963 * Reads a PPT marker (Packed packet headers, tile-part header)
4965 * @param p_header_data the data contained in the PPT box.
4966 * @param p_j2k the jpeg2000 codec.
4967 * @param p_header_size the size of the data contained in the PPT marker.
4968 * @param p_manager the user event manager.
4970 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4971 OPJ_BYTE * p_header_data,
4972 OPJ_UINT32 p_header_size,
4973 struct opj_event_mgr * p_manager )
4975 opj_cp_v2_t *l_cp = 00;
4976 opj_tcp_v2_t *l_tcp = 00;
4980 assert(p_header_data != 00);
4981 assert(p_j2k != 00);
4982 assert(p_manager != 00);
4984 /* We need to have the Z_ppt element at minimum */
4985 if (p_header_size < 1) {
4986 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4990 l_cp = &(p_j2k->m_cp);
4992 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");
4996 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4999 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
5003 /* Allocate buffer to read the packet header */
5005 /* First PPT marker */
5006 l_tcp->ppt_data_size = 0;
5007 l_tcp->ppt_len = p_header_size;
5009 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
5010 if (l_tcp->ppt_buffer == 00) {
5011 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5014 l_tcp->ppt_data = l_tcp->ppt_buffer;
5016 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
5019 l_tcp->ppt_len += p_header_size;
5021 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
5022 if (l_tcp->ppt_buffer == 00) {
5023 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5026 l_tcp->ppt_data = l_tcp->ppt_buffer;
5028 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
5031 /* Read packet header from buffer */
5032 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
5034 l_tcp->ppt_data_size += p_header_size;
5040 * Writes the TLM marker (Tile Length Marker)
5042 * @param p_stream the stream to write data to.
5043 * @param p_j2k J2K codec.
5044 * @param p_manager the user event manager.
5046 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
5047 struct opj_stream_private *p_stream,
5048 struct opj_event_mgr * p_manager )
5050 OPJ_BYTE * l_current_data = 00;
5051 OPJ_UINT32 l_tlm_size;
5054 assert(p_j2k != 00);
5055 assert(p_manager != 00);
5056 assert(p_stream != 00);
5058 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5060 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5061 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5062 = (OPJ_BYTE*)opj_realloc(
5063 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5066 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5070 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
5073 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5075 /* change the way data is written to avoid seeking if possible */
5077 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
5079 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
5080 l_current_data += 2;
5082 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
5083 l_current_data += 2;
5085 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
5088 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5091 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
5092 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) {
5099 static void j2k_write_tlm(opj_j2k_t *j2k){
5101 opj_cio_t *cio = j2k->cio;
5102 j2k->tlm_start = cio_tell(cio);
5103 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
5104 lenp = 4 + (5*j2k->totnum_tp);
5105 cio_write(cio,lenp,2); /* Ltlm */
5106 cio_write(cio, 0,1); /* Ztlm=0*/
5107 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5108 cio_skip(cio,5*j2k->totnum_tp);
5111 static void j2k_write_sot(opj_j2k_t *j2k) {
5114 opj_cio_t *cio = j2k->cio;
5116 j2k->sot_start = cio_tell(cio);
5117 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
5118 lenp = cio_tell(cio);
5119 cio_skip(cio, 2); /* Lsot (further) */
5120 cio_write(cio, j2k->curtileno, 2); /* Isot */
5121 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
5122 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
5123 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
5124 len = cio_tell(cio) - lenp;
5125 cio_seek(cio, lenp);
5126 cio_write(cio, len, 2); /* Lsot */
5127 cio_seek(cio, lenp + len);
5131 /* update markers struct */
5132 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
5133 #endif /* USE_JPWL */
5136 if( j2k->cstr_info && j2k->cur_tp_num==0){
5137 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
5142 * Writes the SOT marker (Start of tile-part)
5144 * @param p_stream the stream to write data to.
5145 * @param p_j2k J2K codec.
5146 * @param p_manager the user event manager.
5148 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
5150 OPJ_UINT32 * p_data_written,
5151 const struct opj_stream_private *p_stream,
5152 struct opj_event_mgr * p_manager )
5155 assert(p_j2k != 00);
5156 assert(p_manager != 00);
5157 assert(p_stream != 00);
5159 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
5162 opj_write_bytes(p_data,10,2); /* Lsot */
5165 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
5171 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
5174 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
5179 /* update markers struct */
5181 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
5183 assert( 0 && "TODO" );
5184 #endif /* USE_JPWL */
5186 * p_data_written = 12;
5195 * Reads a PPT marker (Packed packet headers, tile-part header)
5197 * @param p_header_data the data contained in the PPT box.
5198 * @param p_j2k the jpeg2000 codec.
5199 * @param p_header_size the size of the data contained in the PPT marker.
5200 * @param p_manager the user event manager.
5202 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
5203 OPJ_BYTE * p_header_data,
5204 OPJ_UINT32 p_header_size,
5205 opj_event_mgr_t * p_manager )
5207 opj_cp_v2_t *l_cp = 00;
5208 opj_tcp_v2_t *l_tcp = 00;
5209 OPJ_UINT32 l_tot_len, l_num_parts = 0;
5210 OPJ_UINT32 l_current_part;
5211 OPJ_UINT32 l_tile_x,l_tile_y;
5214 assert(p_header_data != 00);
5215 assert(p_j2k != 00);
5216 assert(p_manager != 00);
5218 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
5219 if (p_header_size != 8) {
5220 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
5224 l_cp = &(p_j2k->m_cp);
5225 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
5228 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5229 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
5230 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
5233 if (l_cp->correct) {
5235 int tileno = p_j2k->m_current_tile_number;
5236 static int backup_tileno = 0;
5238 /* tileno is negative or larger than the number of tiles!!! */
5239 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
5240 opj_event_msg_v2(p_manager, EVT_ERROR,
5241 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5242 tileno, (l_cp->tw * l_cp->th));
5244 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5247 /* we try to correct */
5248 tileno = backup_tileno;
5249 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5250 "- setting tile number to %d\n",
5254 /* keep your private count of tiles */
5257 #endif /* USE_JPWL */
5259 /* look for the tile in the list of already processed tile (in parts). */
5260 /* Optimization possible here with a more complex data structure and with the removing of tiles */
5261 /* since the time taken by this function can only grow at the time */
5263 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
5266 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
5267 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
5269 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5275 if (l_cp->correct) {
5277 /* totlen is negative or larger than the bytes left!!! */
5278 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))) { */
5279 opj_event_msg_v2(p_manager, EVT_ERROR,
5280 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5281 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5283 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5286 /* we try to correct */
5288 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5289 "- setting Psot to %d => assuming it is the last tile\n",
5293 #endif /* USE_JPWL */
5295 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5297 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5298 "we assuming it is the last tile-part of the codestream.\n");
5299 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5302 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5305 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5308 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5309 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5310 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5311 if (l_tcp->m_nb_tile_parts) {
5312 if (l_current_part >= l_tcp->m_nb_tile_parts){
5313 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5314 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5315 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5319 l_tcp->m_nb_tile_parts = l_num_parts;
5322 /* If know the number of tile part header we will check if we didn't read the last*/
5323 if (l_tcp->m_nb_tile_parts) {
5324 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5325 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5329 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5330 /* Keep the size of data to skip after this marker */
5331 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5334 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5335 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5338 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5340 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5341 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5342 p_j2k->m_specific_param.m_decoder.m_skip_data =
5343 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5344 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5345 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5346 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5349 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5350 p_j2k->m_specific_param.m_decoder.m_skip_data =
5351 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5355 if (p_j2k->cstr_index)
5357 assert(p_j2k->cstr_index->tile_index != 00);
5358 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5359 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5361 if (l_num_parts != 0){
5362 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5363 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5366 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5367 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5368 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5370 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5371 (opj_tp_index_t*)opj_realloc(
5372 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5373 l_num_parts* sizeof(opj_tp_index_t));
5376 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5378 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5379 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5380 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5381 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5382 sizeof(opj_tp_index_t));
5385 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5386 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5387 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5388 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5389 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5390 * sizeof(opj_tp_index_t));
5399 /* 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 */
5400 /* if (p_j2k->cstr_info) {
5403 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5406 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5407 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5408 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5409 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5412 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5415 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5419 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5422 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5423 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5424 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5429 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5432 opj_tcp_t *tcp = NULL;
5433 opj_codestream_info_t *cstr_info = NULL;
5435 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5436 opj_cp_t *cp = j2k->cp;
5437 opj_cio_t *cio = j2k->cio;
5439 tcd->tp_num = j2k->tp_num ;
5440 tcd->cur_tp_num = j2k->cur_tp_num;
5442 cio_write(cio, J2K_MS_SOD, 2);
5444 if( j2k->cstr_info && j2k->cur_tp_num==0){
5445 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5448 if (j2k->curtileno == 0) {
5449 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5453 cstr_info = j2k->cstr_info;
5455 if (!j2k->cur_tp_num ) {
5456 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5457 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5460 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5461 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5465 /* update markers struct */
5466 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5467 #endif /* USE_JPWL */
5472 tcp = &cp->tcps[j2k->curtileno];
5473 for (layno = 0; layno < tcp->numlayers; layno++) {
5474 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5475 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5476 } else if (tcp->rates[layno]) {
5477 tcp->rates[layno]=1;
5480 if(j2k->cur_tp_num == 0){
5481 tcd->tcd_image->tiles->packno = 0;
5483 cstr_info->packno = 0;
5486 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5488 /* Writing Psot in SOT marker */
5489 totlen = cio_tell(cio) + l - j2k->sot_start;
5490 cio_seek(cio, j2k->sot_start + 6);
5491 cio_write(cio, totlen, 4);
5492 cio_seek(cio, j2k->sot_start + totlen);
5493 /* Writing Ttlm and Ptlm in TLM marker */
5495 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5496 cio_write(cio, j2k->curtileno, 1);
5497 cio_write(cio, totlen, 4);
5499 cio_seek(cio, j2k->sot_start + totlen);
5503 * Writes the SOD marker (Start of data)
5505 * @param p_stream the stream to write data to.
5506 * @param p_j2k J2K codec.
5507 * @param p_manager the user event manager.
5509 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5510 struct opj_tcd_v2 * p_tile_coder,
5512 OPJ_UINT32 * p_data_written,
5513 OPJ_UINT32 p_total_data_size,
5514 const struct opj_stream_private *p_stream,
5515 struct opj_event_mgr * p_manager )
5517 opj_tcp_v2_t *l_tcp = 00;
5518 opj_codestream_info_t *l_cstr_info = 00;
5519 opj_cp_v2_t *l_cp = 00;
5521 OPJ_UINT32 l_size_tile;
5522 OPJ_UINT32 l_remaining_data;
5525 assert(p_j2k != 00);
5526 assert(p_manager != 00);
5527 assert(p_stream != 00);
5529 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5532 /* make room for the EOF marker */
5533 l_remaining_data = p_total_data_size - 4;
5535 l_cp = &(p_j2k->m_cp);
5536 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5539 /* update tile coder */
5540 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5541 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5543 l_size_tile = l_cp->th * l_cp->tw;
5546 /* TODO mergeV2: check this part which use cstr_info */
5547 /*l_cstr_info = p_j2k->cstr_info;
5549 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5550 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5551 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5557 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5559 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5564 /* update markers struct */
5565 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5567 assert( 0 && "TODO" );
5568 #endif /* USE_JPWL */
5573 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5574 p_tile_coder->tcd_image->tiles->packno = 0;
5576 l_cstr_info->packno = 0;
5580 *p_data_written = 0;
5582 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)) {
5583 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5587 *p_data_written += 2;
5593 static void j2k_read_sod(opj_j2k_t *j2k) {
5594 int len, truncate = 0, i;
5595 unsigned char *data = NULL, *data_ptr = NULL;
5597 opj_cio_t *cio = j2k->cio;
5598 int curtileno = j2k->curtileno;
5601 if (j2k->cstr_info) {
5602 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5603 cio_tell(cio) + j2k->pos_correction - 1;
5604 if (j2k->cur_tp_num == 0)
5605 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5606 j2k->cstr_info->packno = 0;
5609 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
5611 if (len == cio_numbytesleft(cio) + 1) {
5612 truncate = 1; /* Case of a truncate codestream */
5615 data = j2k->tile_data[curtileno];
5616 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
5618 data_ptr = data + j2k->tile_len[curtileno];
5619 for (i = 0; i < len; i++) {
5620 data_ptr[i] = cio_read(cio, 1);
5623 j2k->tile_len[curtileno] += len;
5624 j2k->tile_data[curtileno] = data;
5627 j2k->state = J2K_STATE_TPHSOT;
5629 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
5635 * Reads a SOD marker (Start Of Data)
5637 * @param p_header_data the data contained in the SOD box.
5638 * @param p_j2k the jpeg2000 codec.
5639 * @param p_header_size the size of the data contained in the SOD marker.
5640 * @param p_manager the user event manager.
5642 opj_bool j2k_read_sod_v2 (
5643 opj_j2k_v2_t *p_j2k,
5644 struct opj_stream_private *p_stream,
5645 struct opj_event_mgr * p_manager
5648 OPJ_UINT32 l_current_read_size;
5649 opj_codestream_index_t * l_cstr_index = 00;
5650 OPJ_BYTE ** l_current_data = 00;
5651 opj_tcp_v2_t * l_tcp = 00;
5652 OPJ_UINT32 * l_tile_len = 00;
5655 assert(p_j2k != 00);
5656 assert(p_manager != 00);
5657 assert(p_stream != 00);
5659 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5661 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5662 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5663 // but we are in the last tile part,
5664 // so its result will fit on OPJ_UINT32 unless we find
5665 // a file with a single tile part of more than 4 GB...*/
5666 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5669 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5671 l_current_data = &(l_tcp->m_data);
5672 l_tile_len = &l_tcp->m_data_size;
5674 if (! *l_current_data) {
5675 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5678 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5681 if (*l_current_data == 00) {
5682 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5688 l_cstr_index = p_j2k->cstr_index;
5690 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5692 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5693 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5695 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5696 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5698 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5702 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5704 /*l_cstr_index->packno = 0;*/
5707 l_current_read_size = opj_stream_read_data( p_stream,
5708 *l_current_data + *l_tile_len,
5709 p_j2k->m_specific_param.m_decoder.m_sot_length,
5712 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5713 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5716 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5719 *l_tile_len += l_current_read_size;
5725 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5726 opj_cp_t *cp = j2k->cp;
5727 opj_tcp_t *tcp = &cp->tcps[tileno];
5728 opj_cio_t *cio = j2k->cio;
5729 int numcomps = j2k->image->numcomps;
5731 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5732 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5733 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5734 cio_write(cio, 0, 1); /* Srgn */
5735 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5739 * Writes the RGN marker (Region Of Interest)
5741 * @param p_tile_no the tile to output
5742 * @param p_comp_no the component to output
5743 * @param p_stream the stream to write data to.
5744 * @param p_j2k J2K codec.
5745 * @param p_manager the user event manager.
5747 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5748 OPJ_UINT32 p_tile_no,
5749 OPJ_UINT32 p_comp_no,
5750 struct opj_stream_private *p_stream,
5751 struct opj_event_mgr * p_manager )
5753 OPJ_BYTE * l_current_data = 00;
5754 OPJ_UINT32 l_nb_comp;
5755 OPJ_UINT32 l_rgn_size;
5756 opj_image_t *l_image = 00;
5757 opj_cp_v2_t *l_cp = 00;
5758 opj_tcp_v2_t *l_tcp = 00;
5759 opj_tccp_t *l_tccp = 00;
5760 OPJ_UINT32 l_comp_room;
5763 assert(p_j2k != 00);
5764 assert(p_manager != 00);
5765 assert(p_stream != 00);
5767 l_cp = &(p_j2k->m_cp);
5768 l_tcp = &l_cp->tcps[p_tile_no];
5769 l_tccp = &l_tcp->tccps[p_comp_no];
5771 l_nb_comp = l_image->numcomps;
5773 if (l_nb_comp <= 256) {
5780 l_rgn_size = 6 + l_comp_room;
5782 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5784 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5785 l_current_data += 2;
5787 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5788 l_current_data += 2;
5790 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5791 l_current_data+=l_comp_room;
5793 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5796 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5799 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) {
5806 static void j2k_read_rgn(opj_j2k_t *j2k) {
5807 int len, compno, roisty;
5809 opj_cp_t *cp = j2k->cp;
5810 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5811 opj_cio_t *cio = j2k->cio;
5812 int numcomps = j2k->image->numcomps;
5814 len = cio_read(cio, 2); /* Lrgn */
5815 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
5816 roisty = cio_read(cio, 1); /* Srgn */
5819 if (j2k->cp->correct) {
5820 /* totlen is negative or larger than the bytes left!!! */
5821 if (compno >= numcomps) {
5822 opj_event_msg(j2k->cinfo, EVT_ERROR,
5823 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5825 if (!JPWL_ASSUME || JPWL_ASSUME) {
5826 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5831 #endif /* USE_JPWL */
5833 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
5836 static void j2k_write_eoc(opj_j2k_t *j2k) {
5837 opj_cio_t *cio = j2k->cio;
5838 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5839 cio_write(cio, J2K_MS_EOC, 2);
5843 /* update markers struct */
5844 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5845 #endif /* USE_JPWL */
5850 * Writes the EOC marker (End of Codestream)
5852 * @param p_stream the stream to write data to.
5853 * @param p_j2k J2K codec.
5854 * @param p_manager the user event manager.
5856 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5857 struct opj_stream_private *p_stream,
5858 struct opj_event_mgr * p_manager )
5861 assert(p_j2k != 00);
5862 assert(p_manager != 00);
5863 assert(p_stream != 00);
5865 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5870 /* update markers struct */
5872 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5874 #endif /* USE_JPWL */
5876 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5880 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5889 * Reads a RGN marker (Region Of Interest)
5891 * @param p_header_data the data contained in the POC box.
5892 * @param p_j2k the jpeg2000 codec.
5893 * @param p_header_size the size of the data contained in the POC marker.
5894 * @param p_manager the user event manager.
5896 opj_bool j2k_read_rgn_v2 (
5897 opj_j2k_v2_t *p_j2k,
5898 OPJ_BYTE * p_header_data,
5899 OPJ_UINT32 p_header_size,
5900 struct opj_event_mgr * p_manager
5903 OPJ_UINT32 l_nb_comp;
5904 opj_image_t * l_image = 00;
5906 opj_cp_v2_t *l_cp = 00;
5907 opj_tcp_v2_t *l_tcp = 00;
5908 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5911 assert(p_header_data != 00);
5912 assert(p_j2k != 00);
5913 assert(p_manager != 00);
5915 l_image = p_j2k->m_private_image;
5916 l_nb_comp = l_image->numcomps;
5918 if (l_nb_comp <= 256) {
5923 if (p_header_size != 2 + l_comp_room) {
5924 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5928 l_cp = &(p_j2k->m_cp);
5929 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5930 &l_cp->tcps[p_j2k->m_current_tile_number] :
5931 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5933 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5934 p_header_data+=l_comp_room;
5935 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5939 if (l_cp->correct) {
5940 /* totlen is negative or larger than the bytes left!!! */
5941 if (l_comp_room >= l_nb_comp) {
5942 opj_event_msg_v2(p_manager, EVT_ERROR,
5943 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5944 l_comp_room, l_nb_comp);
5945 if (!JPWL_ASSUME || JPWL_ASSUME) {
5946 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5951 #endif /* USE_JPWL */
5953 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5960 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5962 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5965 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5972 * Updates the rates of the tcp.
5974 * @param p_stream the stream to write data to.
5975 * @param p_j2k J2K codec.
5976 * @param p_manager the user event manager.
5978 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5979 struct opj_stream_private *p_stream,
5980 struct opj_event_mgr * p_manager )
5982 opj_cp_v2_t * l_cp = 00;
5983 opj_image_t * l_image = 00;
5984 opj_tcp_v2_t * l_tcp = 00;
5985 opj_image_comp_t * l_img_comp = 00;
5988 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5989 OPJ_FLOAT32 * l_rates = 0;
5990 OPJ_FLOAT32 l_sot_remove;
5991 OPJ_UINT32 l_bits_empty, l_size_pixel;
5992 OPJ_UINT32 l_tile_size = 0;
5993 OPJ_UINT32 l_last_res;
5994 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5997 assert(p_j2k != 00);
5998 assert(p_manager != 00);
5999 assert(p_stream != 00);
6002 l_cp = &(p_j2k->m_cp);
6003 l_image = p_j2k->m_private_image;
6006 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
6007 l_size_pixel = l_image->numcomps * l_image->comps->prec;
6008 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
6010 if (l_cp->m_specific_param.m_enc.m_tp_on) {
6011 l_tp_stride_func = get_tp_stride;
6014 l_tp_stride_func = get_default_stride;
6017 for (i=0;i<l_cp->th;++i) {
6018 for (j=0;j<l_cp->tw;++j) {
6019 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
6021 /* 4 borders of the tile rescale on the image if necessary */
6022 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
6023 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
6024 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
6025 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
6027 l_rates = l_tcp->rates;
6029 /* Modification of the RATE >> */
6031 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6033 ((*l_rates) * l_bits_empty)
6041 for (k = 1; k < l_tcp->numlayers; ++k) {
6043 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6045 ((*l_rates) * l_bits_empty)
6061 for (i=0;i<l_cp->th;++i) {
6062 for (j=0;j<l_cp->tw;++j) {
6063 l_rates = l_tcp->rates;
6066 *l_rates -= l_sot_remove;
6068 if (*l_rates < 30) {
6075 l_last_res = l_tcp->numlayers - 1;
6077 for (k = 1; k < l_last_res; ++k) {
6080 *l_rates -= l_sot_remove;
6082 if (*l_rates < *(l_rates - 1) + 10) {
6083 *l_rates = (*(l_rates - 1)) + 20;
6091 *l_rates -= (l_sot_remove + 2.f);
6093 if (*l_rates < *(l_rates - 1) + 10) {
6094 *l_rates = (*(l_rates - 1)) + 20;
6102 l_img_comp = l_image->comps;
6105 for (i=0;i<l_image->numcomps;++i) {
6106 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
6108 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
6116 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
6118 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
6120 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
6121 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
6122 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
6123 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
6127 if (l_cp->m_specific_param.m_enc.m_cinema) {
6128 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
6129 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
6130 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6134 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
6135 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
6141 static void j2k_read_eoc(opj_j2k_t *j2k) {
6143 opj_bool success = OPJ_FALSE;
6145 /* if packets should be decoded */
6146 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
6147 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
6148 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
6149 for (i = 0; i < j2k->cp->tileno_size; i++) {
6150 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
6151 if (j2k->cp->tileno[i] != -1)
6153 tileno = j2k->cp->tileno[i];
6154 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
6155 opj_free(j2k->tile_data[tileno]);
6156 j2k->tile_data[tileno] = NULL;
6157 tcd_free_decode_tile(tcd, i);
6160 success = OPJ_FALSE;
6161 if (success == OPJ_FALSE) {
6162 j2k->state |= J2K_STATE_ERR;
6166 tcd_free_decode(tcd);
6169 /* if packets should not be decoded */
6171 for (i = 0; i < j2k->cp->tileno_size; i++) {
6172 tileno = j2k->cp->tileno[i];
6173 opj_free(j2k->tile_data[tileno]);
6174 j2k->tile_data[tileno] = NULL;
6177 if (j2k->state & J2K_STATE_ERR)
6178 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
6180 j2k->state = J2K_STATE_MT;
6184 * Reads a EOC marker (End Of Codestream)
6186 * @param p_header_data the data contained in the SOD box.
6187 * @param p_j2k the jpeg2000 codec.
6188 * @param p_header_size the size of the data contained in the SOD marker.
6189 * @param p_manager the user event manager.
6192 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
6193 struct opj_stream_private *p_stream,
6194 struct opj_event_mgr * p_manager )
6197 opj_tcd_v2_t * l_tcd = 00;
6198 OPJ_UINT32 l_nb_tiles;
6199 opj_tcp_v2_t * l_tcp = 00;
6203 assert(p_j2k != 00);
6204 assert(p_manager != 00);
6205 assert(p_stream != 00);
6207 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6208 l_tcp = p_j2k->m_cp.tcps;
6210 l_tcd = tcd_create_v2(OPJ_TRUE);
6212 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6216 for (i = 0; i < l_nb_tiles; ++i) {
6217 if (l_tcp->m_data) {
6218 if (! tcd_init_decode_tile(l_tcd, i)) {
6219 tcd_destroy_v2(l_tcd);
6220 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6224 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
6228 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
6233 j2k_tcp_destroy(l_tcp);
6237 tcd_destroy_v2(l_tcd);
6243 * Gets the offset of the header.
6245 * @param p_stream the stream to write data to.
6246 * @param p_j2k J2K codec.
6247 * @param p_manager the user event manager.
6249 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
6250 struct opj_stream_private *p_stream,
6251 struct opj_event_mgr * p_manager )
6254 assert(p_j2k != 00);
6255 assert(p_manager != 00);
6256 assert(p_stream != 00);
6258 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
6264 * Writes the MCT marker (Multiple Component Transform)
6266 * @param p_stream the stream to write data to.
6267 * @param p_j2k J2K codec.
6268 * @param p_manager the user event manager.
6270 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
6271 struct opj_stream_private *p_stream,
6272 struct opj_event_mgr * p_manager )
6275 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6276 opj_mct_data_t * l_mct_record;
6277 opj_tcp_v2_t * l_tcp;
6280 assert(p_j2k != 00);
6281 assert(p_stream != 00);
6282 assert(p_manager != 00);
6284 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
6288 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6289 l_mct_record = l_tcp->m_mct_records;
6291 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6293 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
6300 l_mcc_record = l_tcp->m_mcc_records;
6302 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6304 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6311 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6319 * Writes the image components.
6321 * @param p_stream the stream to write data to.
6322 * @param p_j2k J2K codec.
6323 * @param p_manager the user event manager.
6325 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6326 struct opj_stream_private *p_stream,
6327 struct opj_event_mgr * p_manager )
6332 assert(p_j2k != 00);
6333 assert(p_manager != 00);
6334 assert(p_stream != 00);
6336 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6338 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6342 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6351 * Writes regions of interests.
6353 * @param p_stream the stream to write data to.
6354 * @param p_j2k J2K codec.
6355 * @param p_manager the user event manager.
6357 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
6358 struct opj_stream_private *p_stream,
6359 struct opj_event_mgr * p_manager )
6362 const opj_tccp_t *l_tccp = 00;
6365 assert(p_j2k != 00);
6366 assert(p_manager != 00);
6367 assert(p_stream != 00);
6369 l_tccp = p_j2k->m_cp.tcps->tccps;
6371 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6372 if (l_tccp->roishift) {
6374 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6388 * @param p_stream the stream to write data to.
6389 * @param p_j2k J2K codec.
6390 * @param p_manager the user event manager.
6392 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6393 struct opj_stream_private *p_stream,
6394 struct opj_event_mgr * p_manager )
6396 opj_codestream_index_t * l_cstr_index = 00;
6399 assert(p_j2k != 00);
6400 assert(p_manager != 00);
6401 assert(p_stream != 00);
6403 l_cstr_index = p_j2k->cstr_index;
6405 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6407 /* The following adjustment is done to adjust the codestream size */
6408 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6409 /* the first bunch of bytes is not in the codestream */
6410 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6415 /* preparation of JPWL marker segments */
6419 /* encode according to JPWL */
6420 jpwl_encode(p_j2k, p_stream, image);
6424 assert( 0 && "TODO" );
6425 #endif /* USE_JPWL */
6430 typedef struct opj_dec_mstabent {
6433 /** value of the state when the marker can appear */
6435 /** action linked to the marker */
6436 void (*handler) (opj_j2k_t *j2k);
6437 } opj_dec_mstabent_t;
6439 opj_dec_mstabent_t j2k_dec_mstab[] = {
6440 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
6441 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
6442 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
6443 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6444 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
6445 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
6446 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6447 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6448 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6449 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6450 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6451 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6452 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6453 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6454 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6455 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6457 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6458 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6461 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6462 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6463 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6464 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6465 #endif /* USE_JPWL */
6467 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6468 {J2K_MS_INSEC, 0, j2k_read_insec},
6469 #endif /* USE_JPSEC */
6471 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6474 static void j2k_read_unk(opj_j2k_t *j2k) {
6475 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6478 if (j2k->cp->correct) {
6480 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6481 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6482 id = cio_read(j2k->cio, 2);
6483 opj_event_msg(j2k->cinfo, EVT_ERROR,
6484 "JPWL: really don't know this marker %x\n",
6487 opj_event_msg(j2k->cinfo, EVT_ERROR,
6488 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6491 /* OK, activate this at your own risk!!! */
6492 /* we look for the marker at the minimum hamming distance from this */
6493 while (j2k_dec_mstab[m].id) {
6495 /* 1's where they differ */
6496 tmp_id = j2k_dec_mstab[m].id ^ id;
6498 /* compute the hamming distance between our id and the current */
6500 for (i = 0; i < 16; i++) {
6501 if ((tmp_id >> i) & 0x0001) {
6506 /* if current distance is smaller, set the minimum */
6507 if (cur_dist < min_dist) {
6508 min_dist = cur_dist;
6509 min_id = j2k_dec_mstab[m].id;
6512 /* jump to the next marker */
6516 /* do we substitute the marker? */
6517 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6518 opj_event_msg(j2k->cinfo, EVT_ERROR,
6519 "- marker %x is at distance %d from the read %x\n",
6520 min_id, min_dist, id);
6521 opj_event_msg(j2k->cinfo, EVT_ERROR,
6522 "- trying to substitute in place and crossing fingers!\n");
6523 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6524 cio_write(j2k->cio, min_id, 2);
6527 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6532 #endif /* USE_JPWL */
6537 * Reads an unknown marker
6539 * @param p_stream the stream object to read from.
6540 * @param p_j2k the jpeg2000 codec.
6541 * @param p_manager the user event manager.
6543 * @return true if the marker could be deduced.
6545 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6546 struct opj_stream_private *p_stream,
6547 OPJ_UINT32 *output_marker,
6548 struct opj_event_mgr * p_manager
6551 OPJ_UINT32 l_unknown_marker;
6552 const opj_dec_memory_marker_handler_t * l_marker_handler;
6553 OPJ_UINT32 l_size_unk = 2;
6556 assert(p_j2k != 00);
6557 assert(p_manager != 00);
6558 assert(p_stream != 00);
6560 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6563 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6564 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6565 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6569 /* read 2 bytes as the new marker ID*/
6570 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6572 if (!(l_unknown_marker < 0xff00)) {
6574 /* Get the marker handler from the marker ID*/
6575 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6577 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6578 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6582 if (l_marker_handler->id != J2K_MS_UNK) {
6583 /* Add the marker to the codestream index*/
6584 if (l_marker_handler->id != J2K_MS_SOT)
6585 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6586 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6588 break; /* next marker is known and well located */
6596 *output_marker = l_marker_handler->id ;
6602 Read the lookup table containing all the marker, status and action
6603 @param id Marker value
6605 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6606 opj_dec_mstabent_t *e;
6607 for (e = j2k_dec_mstab; e->id != 0; e++) {
6616 * Writes the MCT marker (Multiple Component Transform)
6618 * @param p_stream the stream to write data to.
6619 * @param p_j2k J2K codec.
6620 * @param p_manager the user event manager.
6622 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6623 opj_mct_data_t * p_mct_record,
6624 struct opj_stream_private *p_stream,
6625 struct opj_event_mgr * p_manager )
6627 OPJ_UINT32 l_mct_size;
6628 OPJ_BYTE * l_current_data = 00;
6632 assert(p_j2k != 00);
6633 assert(p_manager != 00);
6634 assert(p_stream != 00);
6636 l_mct_size = 10 + p_mct_record->m_data_size;
6638 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6639 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6640 = (OPJ_BYTE*)opj_realloc(
6641 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6644 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6648 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6651 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6653 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6654 l_current_data += 2;
6656 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6657 l_current_data += 2;
6659 opj_write_bytes(l_current_data,0,2); /* Zmct */
6660 l_current_data += 2;
6662 /* only one marker atm */
6663 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6665 opj_write_bytes(l_current_data,l_tmp,2);
6666 l_current_data += 2;
6668 opj_write_bytes(l_current_data,0,2); /* Ymct */
6671 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6673 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) {
6681 * Reads a MCT marker (Multiple Component Transform)
6683 * @param p_header_data the data contained in the MCT box.
6684 * @param p_j2k the jpeg2000 codec.
6685 * @param p_header_size the size of the data contained in the MCT marker.
6686 * @param p_manager the user event manager.
6688 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6689 OPJ_BYTE * p_header_data,
6690 OPJ_UINT32 p_header_size,
6691 struct opj_event_mgr * p_manager )
6694 opj_tcp_v2_t *l_tcp = 00;
6697 opj_mct_data_t * l_mct_data;
6700 assert(p_header_data != 00);
6701 assert(p_j2k != 00);
6703 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6704 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6705 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6707 if (p_header_size < 2) {
6708 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6713 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6716 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6720 if(p_header_size <= 6) {
6721 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6725 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6726 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6729 l_indix = l_tmp & 0xff;
6730 l_mct_data = l_tcp->m_mct_records;
6732 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6733 if (l_mct_data->m_index == l_indix) {
6740 if (i == l_tcp->m_nb_mct_records) {
6741 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6742 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6744 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));
6745 if(! l_tcp->m_mct_records) {
6746 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6750 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6751 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6754 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6757 if (l_mct_data->m_data) {
6758 opj_free(l_mct_data->m_data);
6759 l_mct_data->m_data = 00;
6762 l_mct_data->m_index = l_indix;
6763 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6764 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6766 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6769 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6775 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6776 if (! l_mct_data->m_data) {
6777 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6780 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6782 l_mct_data->m_data_size = p_header_size;
6783 ++l_tcp->m_nb_mct_records;
6789 * Writes the MCC marker (Multiple Component Collection)
6791 * @param p_stream the stream to write data to.
6792 * @param p_j2k J2K codec.
6793 * @param p_manager the user event manager.
6795 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6796 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6797 struct opj_stream_private *p_stream,
6798 struct opj_event_mgr * p_manager )
6801 OPJ_UINT32 l_mcc_size;
6802 OPJ_BYTE * l_current_data = 00;
6803 OPJ_UINT32 l_nb_bytes_for_comp;
6808 assert(p_j2k != 00);
6809 assert(p_manager != 00);
6810 assert(p_stream != 00);
6812 if (p_mcc_record->m_nb_comps > 255 ) {
6813 l_nb_bytes_for_comp = 2;
6817 l_nb_bytes_for_comp = 1;
6821 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6822 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6824 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6825 = (OPJ_BYTE*)opj_realloc(
6826 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6828 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6832 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6835 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6837 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6838 l_current_data += 2;
6840 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6841 l_current_data += 2;
6844 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6845 l_current_data += 2;
6847 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6850 /* only one marker atm */
6851 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6854 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6857 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6860 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 */
6863 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6864 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6865 l_current_data+=l_nb_bytes_for_comp;
6868 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 */
6871 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6873 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6874 l_current_data+=l_nb_bytes_for_comp;
6877 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6879 if (p_mcc_record->m_decorrelation_array) {
6880 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6883 if (p_mcc_record->m_offset_array) {
6884 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6887 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6890 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) {
6898 * Reads a MCC marker (Multiple Component Collection)
6900 * @param p_header_data the data contained in the MCC box.
6901 * @param p_j2k the jpeg2000 codec.
6902 * @param p_header_size the size of the data contained in the MCC marker.
6903 * @param p_manager the user event manager.
6905 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6906 OPJ_BYTE * p_header_data,
6907 OPJ_UINT32 p_header_size,
6908 struct opj_event_mgr * p_manager )
6913 opj_tcp_v2_t * l_tcp;
6914 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6915 opj_mct_data_t * l_mct_data;
6916 OPJ_UINT32 l_nb_collections;
6917 OPJ_UINT32 l_nb_comps;
6918 OPJ_UINT32 l_nb_bytes_by_comp;
6922 assert(p_header_data != 00);
6923 assert(p_j2k != 00);
6924 assert(p_manager != 00);
6926 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6927 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6928 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6930 if (p_header_size < 2) {
6931 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6936 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6939 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6943 if (p_header_size < 7) {
6944 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6948 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6951 l_mcc_record = l_tcp->m_mcc_records;
6953 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6954 if (l_mcc_record->m_index == l_indix) {
6961 if (i == l_tcp->m_nb_mcc_records) {
6962 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6963 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6965 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6966 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6967 if (! l_tcp->m_mcc_records) {
6968 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6971 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6972 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));
6974 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6976 l_mcc_record->m_index = l_indix;
6978 /* only one marker atm */
6979 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6982 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6986 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6989 if (l_nb_collections > 1) {
6990 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6996 for (i=0;i<l_nb_collections;++i) {
6997 if (p_header_size < 3) {
6998 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7002 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
7006 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
7010 opj_read_bytes(p_header_data,&l_nb_comps,2);
7015 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7016 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
7018 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
7019 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7023 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
7025 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7026 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
7027 p_header_data+=l_nb_bytes_by_comp;
7030 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7035 opj_read_bytes(p_header_data,&l_nb_comps,2);
7038 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7039 l_nb_comps &= 0x7fff;
7041 if (l_nb_comps != l_mcc_record->m_nb_comps) {
7042 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
7046 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
7047 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7051 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
7053 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7054 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
7055 p_header_data+=l_nb_bytes_by_comp;
7058 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7063 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
7066 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
7067 l_mcc_record->m_decorrelation_array = 00;
7068 l_mcc_record->m_offset_array = 00;
7070 l_indix = l_tmp & 0xff;
7072 l_mct_data = l_tcp->m_mct_records;
7073 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7074 if (l_mct_data->m_index == l_indix) {
7075 l_mcc_record->m_decorrelation_array = l_mct_data;
7081 if (l_mcc_record->m_decorrelation_array == 00) {
7082 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7087 l_indix = (l_tmp >> 8) & 0xff;
7089 l_mct_data = l_tcp->m_mct_records;
7090 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7091 if (l_mct_data->m_index == l_indix) {
7092 l_mcc_record->m_offset_array = l_mct_data;
7098 if (l_mcc_record->m_offset_array == 00) {
7099 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7105 if (p_header_size != 0) {
7106 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7110 ++l_tcp->m_nb_mcc_records;
7117 * Writes the MCO marker (Multiple component transformation ordering)
7119 * @param p_stream the stream to write data to.
7120 * @param p_j2k J2K codec.
7121 * @param p_manager the user event manager.
7123 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
7124 struct opj_stream_private *p_stream,
7125 struct opj_event_mgr * p_manager
7128 OPJ_BYTE * l_current_data = 00;
7129 OPJ_UINT32 l_mco_size;
7130 opj_tcp_v2_t * l_tcp = 00;
7131 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7135 assert(p_j2k != 00);
7136 assert(p_manager != 00);
7137 assert(p_stream != 00);
7139 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
7140 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7142 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
7143 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7145 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7146 = (OPJ_BYTE*)opj_realloc(
7147 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7149 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
7154 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
7157 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
7158 l_current_data += 2;
7160 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
7161 l_current_data += 2;
7163 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
7166 l_mcc_record = l_tcp->m_mcc_records;
7167 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
7168 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
7174 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) {
7182 * Reads a MCO marker (Multiple Component Transform Ordering)
7184 * @param p_header_data the data contained in the MCO box.
7185 * @param p_j2k the jpeg2000 codec.
7186 * @param p_header_size the size of the data contained in the MCO marker.
7187 * @param p_manager the user event manager.
7189 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
7190 OPJ_BYTE * p_header_data,
7191 OPJ_UINT32 p_header_size,
7192 struct opj_event_mgr * p_manager )
7194 OPJ_UINT32 l_tmp, i;
7195 OPJ_UINT32 l_nb_stages;
7196 opj_tcp_v2_t * l_tcp;
7197 opj_tccp_t * l_tccp;
7198 opj_image_t * l_image;
7199 opj_image_comp_t * l_img_comp;
7202 assert(p_header_data != 00);
7203 assert(p_j2k != 00);
7204 assert(p_manager != 00);
7206 l_image = p_j2k->m_private_image;
7207 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7208 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7209 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7211 if (p_header_size < 1) {
7212 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
7216 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
7219 if (l_nb_stages > 1) {
7220 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
7224 if (p_header_size != l_nb_stages + 1) {
7225 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
7229 l_tccp = l_tcp->tccps;
7230 l_img_comp = l_image->comps;
7232 for (i=0;i<l_image->numcomps;++i) {
7233 l_tccp->m_dc_level_shift = 0;
7237 if (l_tcp->m_mct_decoding_matrix) {
7238 opj_free(l_tcp->m_mct_decoding_matrix);
7239 l_tcp->m_mct_decoding_matrix = 00;
7242 for (i=0;i<l_nb_stages;++i) {
7243 opj_read_bytes(p_header_data,&l_tmp,1);
7246 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
7254 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
7257 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7258 opj_mct_data_t * l_deco_array, * l_offset_array;
7259 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
7260 OPJ_UINT32 l_nb_elem;
7261 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
7262 opj_tccp_t * l_tccp;
7265 assert(p_tcp != 00);
7267 l_mcc_record = p_tcp->m_mcc_records;
7269 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
7270 if (l_mcc_record->m_index == p_index) {
7275 if (i==p_tcp->m_nb_mcc_records) {
7276 /** element discarded **/
7280 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
7281 /** do not support number of comps != image */
7285 l_deco_array = l_mcc_record->m_decorrelation_array;
7288 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
7289 if (l_deco_array->m_data_size != l_data_size) {
7293 l_nb_elem = p_image->numcomps * p_image->numcomps;
7294 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
7295 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7297 if (! p_tcp->m_mct_decoding_matrix ) {
7301 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);
7304 l_offset_array = l_mcc_record->m_offset_array;
7306 if (l_offset_array) {
7307 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
7308 if (l_offset_array->m_data_size != l_data_size) {
7312 l_nb_elem = p_image->numcomps;
7313 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7314 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7316 if (! l_offset_data ) {
7320 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7322 l_tccp = p_tcp->tccps;
7323 l_current_offset_data = l_offset_data;
7325 for (i=0;i<p_image->numcomps;++i) {
7326 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7330 opj_free(l_offset_data);
7337 * Writes the CBD marker (Component bit depth definition)
7339 * @param p_stream the stream to write data to.
7340 * @param p_j2k J2K codec.
7341 * @param p_manager the user event manager.
7343 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7344 struct opj_stream_private *p_stream,
7345 struct opj_event_mgr * p_manager )
7348 OPJ_UINT32 l_cbd_size;
7349 OPJ_BYTE * l_current_data = 00;
7350 opj_image_t *l_image = 00;
7351 opj_image_comp_t * l_comp = 00;
7354 assert(p_j2k != 00);
7355 assert(p_manager != 00);
7356 assert(p_stream != 00);
7358 l_image = p_j2k->m_private_image;
7359 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7361 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7362 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7363 = (OPJ_BYTE*)opj_realloc(
7364 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7367 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7371 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7374 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7376 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7377 l_current_data += 2;
7379 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7380 l_current_data += 2;
7382 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7385 l_comp = l_image->comps;
7387 for (i=0;i<l_image->numcomps;++i) {
7388 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7394 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) {
7402 * Reads a CBD marker (Component bit depth definition)
7403 * @param p_header_data the data contained in the CBD box.
7404 * @param p_j2k the jpeg2000 codec.
7405 * @param p_header_size the size of the data contained in the CBD marker.
7406 * @param p_manager the user event manager.
7408 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7409 OPJ_BYTE * p_header_data,
7410 OPJ_UINT32 p_header_size,
7411 struct opj_event_mgr * p_manager)
7413 OPJ_UINT32 l_nb_comp,l_num_comp;
7414 OPJ_UINT32 l_comp_def;
7416 opj_image_comp_t * l_comp = 00;
7419 assert(p_header_data != 00);
7420 assert(p_j2k != 00);
7421 assert(p_manager != 00);
7423 l_num_comp = p_j2k->m_private_image->numcomps;
7425 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7426 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7430 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7433 if (l_nb_comp != l_num_comp) {
7434 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7438 l_comp = p_j2k->m_private_image->comps;
7439 for (i=0;i<l_num_comp;++i) {
7440 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7442 l_comp->sgnd = (l_comp_def>>7) & 1;
7443 l_comp->prec = (l_comp_def&0x7f) + 1;
7451 /* ----------------------------------------------------------------------- */
7452 /* J2K / JPT decoder interface */
7453 /* ----------------------------------------------------------------------- */
7457 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7460 if(j2k->tile_len != NULL) {
7461 opj_free(j2k->tile_len);
7463 if(j2k->tile_data != NULL) {
7464 opj_free(j2k->tile_data);
7466 if(j2k->default_tcp != NULL) {
7467 opj_tcp_t *default_tcp = j2k->default_tcp;
7468 if(default_tcp->ppt_data_first != NULL) {
7469 opj_free(default_tcp->ppt_data_first);
7471 if(j2k->default_tcp->tccps != NULL) {
7472 opj_free(j2k->default_tcp->tccps);
7474 opj_free(j2k->default_tcp);
7476 if(j2k->cp != NULL) {
7477 opj_cp_t *cp = j2k->cp;
7478 if(cp->tcps != NULL) {
7479 for(i = 0; i < cp->tw * cp->th; i++) {
7480 if(cp->tcps[i].ppt_data_first != NULL) {
7481 opj_free(cp->tcps[i].ppt_data_first);
7483 if(cp->tcps[i].tccps != NULL) {
7484 opj_free(cp->tcps[i].tccps);
7489 if(cp->ppm_data_first != NULL) {
7490 opj_free(cp->ppm_data_first);
7492 if(cp->tileno != NULL) {
7493 opj_free(cp->tileno);
7495 if(cp->comment != NULL) {
7496 opj_free(cp->comment);
7505 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7507 if(j2k && parameters) {
7508 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7509 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7512 j2k->m_cp.correct = parameters->jpwl_correct;
7513 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7514 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7515 #endif /* USE_JPWL */
7522 * Read a JPT-stream and decode file
7525 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7526 opj_image_t *image = NULL;
7527 opj_jpt_msg_header_t header;
7529 opj_common_ptr cinfo = j2k->cinfo;
7531 OPJ_ARG_NOT_USED(cstr_info);
7535 /* create an empty image */
7536 image = opj_image_create0();
7539 j2k->state = J2K_STATE_MHSOC;
7541 /* Initialize the header */
7542 jpt_init_msg_header(&header);
7543 /* Read the first header of the message */
7544 jpt_read_msg_header(cinfo, cio, &header);
7546 position = cio_tell(cio);
7547 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7548 opj_image_destroy(image);
7549 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7554 opj_dec_mstabent_t *e = NULL;
7557 if (!cio_numbytesleft(cio)) {
7561 /* data-bin read -> need to read a new header */
7562 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7563 jpt_read_msg_header(cinfo, cio, &header);
7564 position = cio_tell(cio);
7565 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7566 opj_image_destroy(image);
7567 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7572 id = cio_read(cio, 2);
7573 if (id >> 8 != 0xff) {
7574 opj_image_destroy(image);
7575 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7578 e = j2k_dec_mstab_lookup(id);
7579 if (!(j2k->state & e->states)) {
7580 opj_image_destroy(image);
7581 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7587 if (j2k->state == J2K_STATE_MT) {
7590 if (j2k->state == J2K_STATE_NEOC) {
7594 if (j2k->state == J2K_STATE_NEOC) {
7598 if (j2k->state != J2K_STATE_MT) {
7599 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7605 /* ----------------------------------------------------------------------- */
7606 /* J2K encoder interface */
7607 /* ----------------------------------------------------------------------- */
7609 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7610 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7617 opj_j2k_v2_t* j2k_create_compress_v2(void)
7619 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7624 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7626 l_j2k->m_is_decoder = 0;
7627 l_j2k->m_cp.m_is_decoder = 0;
7629 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7630 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7635 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7637 /* validation list creation*/
7638 l_j2k->m_validation_list = opj_procedure_list_create();
7639 if (! l_j2k->m_validation_list) {
7644 /* execution list creation*/
7645 l_j2k->m_procedure_list = opj_procedure_list_create();
7646 if (! l_j2k->m_procedure_list) {
7654 void j2k_destroy_compress(opj_j2k_t *j2k) {
7658 if(j2k->cp != NULL) {
7659 opj_cp_t *cp = j2k->cp;
7662 opj_free(cp->comment);
7665 opj_free(cp->matrice);
7667 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7668 opj_free(cp->tcps[tileno].tccps);
7677 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7679 int tileno, numpocs_tile;
7680 opj_cp_t *cp = NULL;
7682 if(!j2k || !parameters || ! image) {
7686 /* create and initialize the coding parameters structure */
7687 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7689 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7692 /* set default values for cp */
7697 copy user encoding parameters
7699 cp->cinema = parameters->cp_cinema;
7700 cp->max_comp_size = parameters->max_comp_size;
7701 cp->rsiz = parameters->cp_rsiz;
7702 cp->disto_alloc = parameters->cp_disto_alloc;
7703 cp->fixed_alloc = parameters->cp_fixed_alloc;
7704 cp->fixed_quality = parameters->cp_fixed_quality;
7706 /* mod fixed_quality */
7707 if(parameters->cp_matrice) {
7708 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7709 cp->matrice = (int *) opj_malloc(array_size);
7710 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7714 cp->tdx = parameters->cp_tdx;
7715 cp->tdy = parameters->cp_tdy;
7718 cp->tx0 = parameters->cp_tx0;
7719 cp->ty0 = parameters->cp_ty0;
7721 /* comment string */
7722 if(parameters->cp_comment) {
7723 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7725 strcpy(cp->comment, parameters->cp_comment);
7730 calculate other encoding parameters
7733 if (parameters->tile_size_on) {
7734 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7735 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7737 cp->tdx = image->x1 - cp->tx0;
7738 cp->tdy = image->y1 - cp->ty0;
7741 if(parameters->tp_on){
7742 cp->tp_flag = parameters->tp_flag;
7747 for(i=0;i<image->numcomps ;i++){
7748 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7754 calculate JPWL encoding parameters
7757 if (parameters->jpwl_epc_on) {
7761 cp->epc_on = OPJ_TRUE;
7762 cp->info_on = OPJ_FALSE; /* no informative technique */
7765 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7766 cp->epb_on = OPJ_TRUE;
7768 cp->hprot_MH = parameters->jpwl_hprot_MH;
7769 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7770 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7771 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7773 /* if tile specs are not specified, copy MH specs */
7774 if (cp->hprot_TPH[0] == -1) {
7775 cp->hprot_TPH_tileno[0] = 0;
7776 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7778 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7779 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7780 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7781 cp->pprot[i] = parameters->jpwl_pprot[i];
7785 /* set ESD writing */
7786 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7787 cp->esd_on = OPJ_TRUE;
7789 cp->sens_size = parameters->jpwl_sens_size;
7790 cp->sens_addr = parameters->jpwl_sens_addr;
7791 cp->sens_range = parameters->jpwl_sens_range;
7793 cp->sens_MH = parameters->jpwl_sens_MH;
7794 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7795 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7796 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7800 /* always set RED writing to false: we are at the encoder */
7801 cp->red_on = OPJ_FALSE;
7804 cp->epc_on = OPJ_FALSE;
7806 #endif /* USE_JPWL */
7809 /* initialize the mutiple tiles */
7810 /* ---------------------------- */
7811 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7813 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7814 opj_tcp_t *tcp = &cp->tcps[tileno];
7815 tcp->numlayers = parameters->tcp_numlayers;
7816 assert ( tcp->numlayers >= 0 );
7817 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7819 if (cp->fixed_quality) {
7820 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7822 tcp->rates[j] = parameters->tcp_rates[j];
7824 if (cp->fixed_quality) { /* add fixed_quality */
7825 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7827 tcp->rates[j] = parameters->tcp_rates[j];
7831 tcp->csty = parameters->csty;
7832 tcp->prg = parameters->prog_order;
7833 tcp->mct = parameters->tcp_mct;
7837 if (parameters->numpocs) {
7838 /* initialisation of POC */
7840 assert( parameters->numpocs >= 0 );
7841 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7842 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7843 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7844 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7845 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7846 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7847 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7848 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7849 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7850 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7854 tcp->numpocs = numpocs_tile -1 ;
7859 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7861 for (i = 0; i < image->numcomps; i++) {
7862 opj_tccp_t *tccp = &tcp->tccps[i];
7863 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7864 tccp->numresolutions = parameters->numresolution;
7865 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7866 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7867 tccp->cblksty = parameters->mode;
7868 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7869 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7871 assert(parameters->roi_compno >= 0);
7872 if (i == (OPJ_UINT32)parameters->roi_compno) {
7873 tccp->roishift = parameters->roi_shift;
7878 if(parameters->cp_cinema)
7880 /*Precinct size for lowest frequency subband=128*/
7883 /*Precinct size at all other resolutions = 256*/
7884 for (j = 1; j < tccp->numresolutions; j++) {
7889 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7891 assert(tccp->numresolutions > 0);
7892 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7893 if (p < parameters->res_spec) {
7895 if (parameters->prcw_init[p] < 1) {
7898 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7901 if (parameters->prch_init[p] < 1) {
7904 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7908 int res_spec = parameters->res_spec;
7909 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7910 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7912 if (size_prcw < 1) {
7915 tccp->prcw[j] = int_floorlog2(size_prcw);
7918 if (size_prch < 1) {
7921 tccp->prch[j] = int_floorlog2(size_prch);
7925 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7928 for (j = 0; j < tccp->numresolutions; j++) {
7935 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7940 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7941 opj_cparameters_t *parameters,
7943 struct opj_event_mgr * p_manager)
7945 OPJ_UINT32 i, j, tileno, numpocs_tile;
7946 opj_cp_v2_t *cp = 00;
7949 if(!p_j2k || !parameters || ! image) {
7953 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7954 cp = &(p_j2k->m_cp);
7956 /* set default values for cp */
7961 copy user encoding parameters
7963 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7964 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7965 cp->rsiz = parameters->cp_rsiz;
7966 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7967 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7968 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7970 /* mod fixed_quality */
7971 if (parameters->cp_matrice) {
7972 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7973 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7974 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7978 cp->tdx = parameters->cp_tdx;
7979 cp->tdy = parameters->cp_tdy;
7982 cp->tx0 = parameters->cp_tx0;
7983 cp->ty0 = parameters->cp_ty0;
7985 /* comment string */
7986 if(parameters->cp_comment) {
7987 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7989 strcpy(cp->comment, parameters->cp_comment);
7994 calculate other encoding parameters
7997 if (parameters->tile_size_on) {
7998 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7999 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8001 cp->tdx = image->x1 - cp->tx0;
8002 cp->tdy = image->y1 - cp->ty0;
8005 if (parameters->tp_on) {
8006 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
8007 cp->m_specific_param.m_enc.m_tp_on = 1;
8012 calculate JPWL encoding parameters
8015 if (parameters->jpwl_epc_on) {
8019 cp->epc_on = OPJ_TRUE;
8020 cp->info_on = OPJ_FALSE; /* no informative technique */
8023 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8024 cp->epb_on = OPJ_TRUE;
8026 cp->hprot_MH = parameters->jpwl_hprot_MH;
8027 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8028 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8029 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8031 /* if tile specs are not specified, copy MH specs */
8032 if (cp->hprot_TPH[0] == -1) {
8033 cp->hprot_TPH_tileno[0] = 0;
8034 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8036 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8037 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8038 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8039 cp->pprot[i] = parameters->jpwl_pprot[i];
8043 /* set ESD writing */
8044 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8045 cp->esd_on = OPJ_TRUE;
8047 cp->sens_size = parameters->jpwl_sens_size;
8048 cp->sens_addr = parameters->jpwl_sens_addr;
8049 cp->sens_range = parameters->jpwl_sens_range;
8051 cp->sens_MH = parameters->jpwl_sens_MH;
8052 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8053 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8054 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8058 /* always set RED writing to false: we are at the encoder */
8059 cp->red_on = OPJ_FALSE;
8062 cp->epc_on = OPJ_FALSE;
8064 #endif /* USE_JPWL */
8067 /* initialize the mutiple tiles */
8068 /* ---------------------------- */
8069 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
8070 if (parameters->numpocs) {
8071 /* initialisation of POC */
8072 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
8076 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8077 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
8078 tcp->numlayers = parameters->tcp_numlayers;
8080 for (j = 0; j < tcp->numlayers; j++) {
8081 if(cp->m_specific_param.m_enc.m_cinema){
8082 if (cp->m_specific_param.m_enc.m_fixed_quality) {
8083 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8085 tcp->rates[j] = parameters->tcp_rates[j];
8087 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
8088 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8090 tcp->rates[j] = parameters->tcp_rates[j];
8095 tcp->csty = parameters->csty;
8096 tcp->prg = parameters->prog_order;
8097 tcp->mct = parameters->tcp_mct;
8102 if (parameters->numpocs) {
8103 /* initialisation of POC */
8106 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
8107 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8108 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8110 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
8111 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
8112 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
8113 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
8114 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
8115 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
8116 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
8122 tcp->numpocs = numpocs_tile -1 ;
8127 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8129 if (parameters->mct_data) {
8131 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
8134 /* TODO MSD : merge v2 add invert.c or used a external lib ?
8135 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
8136 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8137 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
8140 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8141 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
8142 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
8144 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8145 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
8147 tcp->mct_norms = (OPJ_FLOAT64*)
8148 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
8150 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
8153 for (i = 0; i < image->numcomps; i++) {
8154 opj_tccp_t *tccp = &tcp->tccps[i];
8155 tccp->m_dc_level_shift = l_dc_shift[i];
8158 j2k_setup_mct_encoding(tcp,image);
8162 for (i = 0; i < image->numcomps; i++) {
8163 opj_tccp_t *tccp = &tcp->tccps[i];
8164 opj_image_comp_t * l_comp = &(image->comps[i]);
8166 if (! l_comp->sgnd) {
8167 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
8172 for (i = 0; i < image->numcomps; i++) {
8173 opj_tccp_t *tccp = &tcp->tccps[i];
8175 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
8176 tccp->numresolutions = parameters->numresolution;
8177 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8178 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8179 tccp->cblksty = parameters->mode;
8180 tccp->qmfbid = parameters->irreversible ? 0 : 1;
8181 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8184 if (i == parameters->roi_compno) {
8185 tccp->roishift = parameters->roi_shift;
8190 if(parameters->cp_cinema) {
8191 //Precinct size for lowest frequency subband=128
8194 //Precinct size at all other resolutions = 256
8195 for (j = 1; j < tccp->numresolutions; j++) {
8200 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8201 OPJ_INT32 p = 0, it_res;
8202 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
8203 if (p < parameters->res_spec) {
8205 if (parameters->prcw_init[p] < 1) {
8206 tccp->prcw[it_res] = 1;
8208 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
8211 if (parameters->prch_init[p] < 1) {
8212 tccp->prch[it_res] = 1;
8214 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
8218 int res_spec = parameters->res_spec;
8219 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8220 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8222 if (size_prcw < 1) {
8223 tccp->prcw[it_res] = 1;
8225 tccp->prcw[it_res] = int_floorlog2(size_prcw);
8228 if (size_prch < 1) {
8229 tccp->prch[it_res] = 1;
8231 tccp->prch[it_res] = int_floorlog2(size_prch);
8235 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
8238 for (j = 0; j < tccp->numresolutions; j++) {
8245 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8249 if (parameters->mct_data) {
8250 opj_free(parameters->mct_data);
8251 parameters->mct_data = 00;
8256 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
8259 opj_cp_t *cp = NULL;
8261 opj_tcd_t *tcd = NULL; /* TCD component */
8269 j2k->cstr_info = cstr_info;
8272 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
8273 cstr_info->image_w = image->x1 - image->x0;
8274 cstr_info->image_h = image->y1 - image->y0;
8275 cstr_info->prog = (&cp->tcps[0])->prg;
8276 cstr_info->tw = cp->tw;
8277 cstr_info->th = cp->th;
8278 cstr_info->tile_x = cp->tdx; /* new version parser */
8279 cstr_info->tile_y = cp->tdy; /* new version parser */
8280 cstr_info->tile_Ox = cp->tx0; /* new version parser */
8281 cstr_info->tile_Oy = cp->ty0; /* new version parser */
8282 cstr_info->numcomps = image->numcomps;
8283 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
8284 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
8285 for (compno=0; compno < image->numcomps; compno++) {
8286 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
8288 cstr_info->D_max = 0.0; /* ADD Marcela */
8289 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
8290 cstr_info->maxmarknum = 100;
8291 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
8292 cstr_info->marknum = 0;
8302 for (compno = 1; compno < image->numcomps; compno++) {
8303 j2k_write_coc(j2k, compno);
8304 j2k_write_qcc(j2k, compno);
8308 for (compno = 0; compno < image->numcomps; compno++) {
8309 opj_tcp_t *tcp = &cp->tcps[0];
8310 if (tcp->tccps[compno].roishift)
8311 j2k_write_rgn(j2k, compno, 0);
8313 if (cp->comment != NULL) {
8317 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
8321 if (cp->cinema == CINEMA4K_24) {
8326 /* uncomment only for testing JPSEC marker writing */
8327 /* j2k_write_sec(j2k); */
8331 cstr_info->main_head_end = cio_tell(cio) - 1;
8334 /**** Main Header ENDS here ***/
8336 /* create the tile encoder */
8337 tcd = tcd_create(j2k->cinfo);
8339 /* encode each tile */
8340 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8344 int acc_pack_num = 0;
8348 opj_tcp_t *tcp = &cp->tcps[tileno];
8349 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8351 j2k->curtileno = tileno;
8352 j2k->cur_tp_num = 0;
8353 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8354 /* initialisation before tile encoding */
8356 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8358 tcd_init_encode(tcd, image, cp, j2k->curtileno);
8363 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8364 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8365 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8366 cstr_info->tile[j2k->curtileno].marknum = 0;
8370 for(pino = 0; pino <= tcp->numpocs; pino++) {
8374 /*Get number of tile parts*/
8375 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8376 tcd->tp_pos = cp->tp_pos;
8378 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8379 j2k->tp_num = tilepartno;
8382 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8383 cio_tell(cio) + j2k->pos_correction;
8387 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8388 for (compno = 1; compno < image->numcomps; compno++) {
8389 j2k_write_coc(j2k, compno);
8390 j2k_write_qcc(j2k, compno);
8392 if (cp->tcps[tileno].numpocs) {
8399 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8400 cio_tell(cio) + j2k->pos_correction + 1;
8403 j2k_write_sod(j2k, tcd);
8407 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8408 cio_tell(cio) + j2k->pos_correction - 1;
8409 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8411 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8412 cstr_info->packno - acc_pack_num;
8413 acc_pack_num = cstr_info->packno;
8421 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8426 if (tile->PPT) { // BAD PPT !!!
8429 PPT_file=fopen("PPT","rb");
8430 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8431 for (i=0;i<tile->len_ppt;i++) {
8433 fread(&elmt, 1, 1, PPT_file);
8434 fwrite(&elmt,1,1,f);
8443 /* destroy the tile encoder */
8444 tcd_free_encode(tcd);
8447 opj_free(j2k->cur_totnum_tp);
8452 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8454 /* The following adjustment is done to adjust the codestream size */
8455 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8456 /* the first bunch of bytes is not in the codestream */
8457 cstr_info->codestream_size -= cstr_info->main_head_start;
8463 preparation of JPWL marker segments
8467 /* encode according to JPWL */
8468 jpwl_encode(j2k, cio, image);
8471 #endif /* USE_JPWL */
8476 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8478 assert(cstr_info != 00);
8480 /* expand the list? */
8481 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8482 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8483 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8486 /* add the marker */
8487 cstr_info->marker[cstr_info->marknum].type = type;
8488 cstr_info->marker[cstr_info->marknum].pos = pos;
8489 cstr_info->marker[cstr_info->marknum].len = len;
8490 cstr_info->marknum++;
8494 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8496 assert(cstr_index != 00);
8498 /* expand the list? */
8499 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8500 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8501 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8504 /* add the marker */
8505 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8506 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8507 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8508 cstr_index->marknum++;
8512 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8514 opj_marker_info_t *marker;
8516 assert(cstr_info != 00);
8518 /* expand the list? */
8519 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8520 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8521 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8524 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8526 /* add the marker */
8527 marker->type = type;
8530 cstr_info->tile[tileno].marknum++;
8533 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)
8535 assert(cstr_index != 00);
8536 assert(cstr_index->tile_index != 00);
8538 /* expand the list? */
8539 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8540 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8541 cstr_index->tile_index[tileno].marker =
8542 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8543 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8546 /* add the marker */
8547 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8548 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8549 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8550 cstr_index->tile_index[tileno].marknum++;
8552 if (type == J2K_MS_SOT) {
8553 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8555 if (cstr_index->tile_index[tileno].tp_index)
8556 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8563 * -----------------------------------------------------------------------
8564 * -----------------------------------------------------------------------
8565 * -----------------------------------------------------------------------
8569 * Ends the decompression procedures and possibiliy add data to be read after the
8572 opj_bool j2k_end_decompress(
8573 opj_j2k_v2_t *p_j2k,
8574 opj_stream_private_t *p_stream,
8575 opj_event_mgr_t * p_manager)
8584 * Reads a jpeg2000 codestream header structure.
8587 * @param p_stream the stream to read data from.
8588 * @param p_j2k the jpeg2000 codec.
8589 * @param p_manager the user event manager.
8591 * @return true if the box is valid.
8593 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8594 opj_j2k_v2_t* p_j2k,
8595 opj_image_t** p_image,
8596 struct opj_event_mgr* p_manager )
8599 assert(p_j2k != 00);
8600 assert(p_stream != 00);
8601 assert(p_manager != 00);
8603 /* create an empty image header */
8604 p_j2k->m_private_image = opj_image_create0();
8605 if (! p_j2k->m_private_image) {
8609 /* customization of the validation */
8610 j2k_setup_decoding_validation(p_j2k);
8612 /* validation of the parameters codec */
8613 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8614 opj_image_destroy(p_j2k->m_private_image);
8615 p_j2k->m_private_image = NULL;
8619 /* customization of the encoding */
8620 j2k_setup_header_reading(p_j2k);
8623 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8624 opj_image_destroy(p_j2k->m_private_image);
8625 p_j2k->m_private_image = NULL;
8629 *p_image = opj_image_create0();
8634 /* Copy codestream image information to the output image */
8635 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8637 /*Allocate and initialize some elements of codestrem index*/
8638 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8646 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8648 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8651 assert(p_j2k != 00);
8653 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8655 /* DEVELOPER CORNER, add your custom procedures */
8656 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8661 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8662 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8664 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8667 assert(p_j2k != 00);
8669 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8670 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8671 /* DEVELOPER CORNER, add your custom validation procedure */
8677 * The mct encoding validation procedure.
8679 * @param p_j2k the jpeg2000 codec to validate.
8680 * @param p_stream the input stream to validate.
8681 * @param p_manager the user event manager.
8683 * @return true if the parameters are correct.
8685 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8686 opj_stream_private_t *p_stream,
8687 opj_event_mgr_t * p_manager )
8689 opj_bool l_is_valid = OPJ_TRUE;
8693 assert(p_j2k != 00);
8694 assert(p_stream != 00);
8695 assert(p_manager != 00);
8697 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8698 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8699 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8701 for (i=0;i<l_nb_tiles;++i) {
8702 if (l_tcp->mct == 2) {
8703 opj_tccp_t * l_tccp = l_tcp->tccps;
8704 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8706 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8707 l_is_valid &= ! (l_tccp->qmfbid & 1);
8718 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8721 OPJ_UINT32 l_indix = 1;
8722 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8723 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8724 OPJ_UINT32 l_mct_size,l_nb_elem;
8725 OPJ_FLOAT32 * l_data, * l_current_data;
8726 opj_tccp_t * l_tccp;
8729 assert(p_tcp != 00);
8731 if (p_tcp->mct != 2) {
8735 if (p_tcp->m_mct_decoding_matrix) {
8736 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8737 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8739 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));
8740 if (! p_tcp->m_mct_records) {
8743 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8745 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8747 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8749 if (l_mct_deco_data->m_data) {
8750 opj_free(l_mct_deco_data->m_data);
8751 l_mct_deco_data->m_data = 00;
8754 l_mct_deco_data->m_index = l_indix++;
8755 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8756 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8757 l_nb_elem = p_image->numcomps * p_image->numcomps;
8758 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8759 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8761 if (! l_mct_deco_data->m_data) {
8765 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);
8767 l_mct_deco_data->m_data_size = l_mct_size;
8768 ++p_tcp->m_nb_mct_records;
8771 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8772 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8773 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));
8775 if (! p_tcp->m_mct_records) {
8779 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8780 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8782 if (l_mct_deco_data) {
8783 l_mct_deco_data = l_mct_offset_data - 1;
8787 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8789 if (l_mct_offset_data->m_data) {
8790 opj_free(l_mct_offset_data->m_data);
8791 l_mct_offset_data->m_data = 00;
8794 l_mct_offset_data->m_index = l_indix++;
8795 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8796 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8797 l_nb_elem = p_image->numcomps;
8798 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8799 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8801 if (! l_mct_offset_data->m_data) {
8805 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8807 opj_free(l_mct_offset_data->m_data);
8808 l_mct_offset_data->m_data = 00;
8812 l_tccp = p_tcp->tccps;
8813 l_current_data = l_data;
8815 for (i=0;i<l_nb_elem;++i) {
8816 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8820 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8824 l_mct_offset_data->m_data_size = l_mct_size;
8826 ++p_tcp->m_nb_mct_records;
8828 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8829 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8830 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8831 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8833 if (! p_tcp->m_mcc_records) {
8836 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8837 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));
8841 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8842 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8843 l_mcc_data->m_is_irreversible = 1;
8844 l_mcc_data->m_nb_comps = p_image->numcomps;
8845 l_mcc_data->m_index = l_indix++;
8846 l_mcc_data->m_offset_array = l_mct_offset_data;
8847 ++p_tcp->m_nb_mcc_records;
8853 * Builds the cp decoder parameters to use to decode tile.
8855 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8856 opj_stream_private_t *p_stream,
8857 opj_event_mgr_t * p_manager )
8859 /* add here initialization of cp
8860 copy paste of setup_decoder */
8868 * Builds the cp encoder parameters to use to encode tile.
8870 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8871 opj_stream_private_t *p_stream,
8872 opj_event_mgr_t * p_manager )
8874 /* add here initialization of cp
8875 copy paste of setup_encoder */
8883 * The default encoding validation procedure without any extension.
8885 * @param p_j2k the jpeg2000 codec to validate.
8886 * @param p_stream the input stream to validate.
8887 * @param p_manager the user event manager.
8889 * @return true if the parameters are correct.
8891 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8892 opj_stream_private_t *p_stream,
8893 opj_event_mgr_t * p_manager )
8895 opj_bool l_is_valid = OPJ_TRUE;
8898 assert(p_j2k != 00);
8899 assert(p_stream != 00);
8900 assert(p_manager != 00);
8902 /* STATE checking */
8903 /* make sure the state is at 0 */
8904 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8906 /* POINTER validation */
8907 /* make sure a p_j2k codec is present */
8908 l_is_valid &= (p_j2k->m_procedure_list != 00);
8909 /* make sure a validation list is present */
8910 l_is_valid &= (p_j2k->m_validation_list != 00);
8912 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8913 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8917 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8918 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8922 /* PARAMETER VALIDATION */
8927 * The default decoding validation procedure without any extension.
8929 * @param p_j2k the jpeg2000 codec to validate.
8930 * @param p_stream the input stream to validate.
8931 * @param p_manager the user event manager.
8933 * @return true if the parameters are correct.
8935 opj_bool j2k_decoding_validation (
8936 opj_j2k_v2_t *p_j2k,
8937 opj_stream_private_t *p_stream,
8938 opj_event_mgr_t * p_manager
8941 opj_bool l_is_valid = OPJ_TRUE;
8944 assert(p_j2k != 00);
8945 assert(p_stream != 00);
8946 assert(p_manager != 00);
8949 /* STATE checking */
8950 /* make sure the state is at 0 */
8952 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8954 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8956 /* POINTER validation */
8957 /* make sure a p_j2k codec is present */
8958 /* make sure a procedure list is present */
8959 l_is_valid &= (p_j2k->m_procedure_list != 00);
8960 /* make sure a validation list is present */
8961 l_is_valid &= (p_j2k->m_validation_list != 00);
8963 /* PARAMETER VALIDATION */
8967 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8968 struct opj_stream_private *p_stream,
8969 struct opj_event_mgr * p_manager)
8971 OPJ_UINT32 l_current_marker;
8972 OPJ_UINT32 l_marker_size;
8973 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8976 assert(p_stream != 00);
8977 assert(p_j2k != 00);
8978 assert(p_manager != 00);
8980 /* We enter in the main header */
8981 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8983 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8984 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8985 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8989 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8990 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8991 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8995 /* Read 2 bytes as the new marker ID */
8996 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8998 /* Try to read until the SOT is detected */
8999 while (l_current_marker != J2K_MS_SOT) {
9001 /* Check if the current marker ID is valid */
9002 if (l_current_marker < 0xff00) {
9003 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
9007 /* Get the marker handler from the marker ID */
9008 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9010 /* Manage case where marker is unknown */
9011 if (l_marker_handler->id == J2K_MS_UNK) {
9012 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
9013 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
9017 if (l_current_marker == J2K_MS_SOT)
9018 break; /* SOT marker is detected main header is completely read */
9019 else /* Get the marker handler from the marker ID */
9020 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9023 /* Check if the marker is known and if it is the right place to find it */
9024 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9025 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9029 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9030 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9031 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9035 /* read 2 bytes as the marker size */
9036 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9037 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9039 /* Check if the marker size is compatible with the header data size */
9040 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9041 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9042 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9043 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9046 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9049 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9050 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) {
9051 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9055 /* Read the marker segment with the correct marker handler */
9056 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9057 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
9061 /* Add the marker to the codestream index*/
9062 j2k_add_mhmarker_v2(p_j2k->cstr_index,
9063 l_marker_handler->id,
9064 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9065 l_marker_size + 4 );
9067 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9068 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9069 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9073 /* read 2 bytes as the new marker ID */
9074 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9077 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
9079 /* Position of the last element if the main header */
9080 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
9082 /* Next step: read a tile-part header */
9083 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9089 * Excutes the given procedures on the given codec.
9091 * @param p_procedure_list the list of procedures to execute
9092 * @param p_j2k the jpeg2000 codec to execute the procedures on.
9093 * @param p_stream the stream to execute the procedures on.
9094 * @param p_manager the user manager.
9096 * @return true if all the procedures were successfully executed.
9098 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
9099 opj_procedure_list_t * p_procedure_list,
9100 opj_stream_private_t *p_stream,
9101 opj_event_mgr_t * p_manager )
9103 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
9104 opj_bool l_result = OPJ_TRUE;
9105 OPJ_UINT32 l_nb_proc, i;
9108 assert(p_procedure_list != 00);
9109 assert(p_j2k != 00);
9110 assert(p_stream != 00);
9111 assert(p_manager != 00);
9114 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
9115 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);
9117 for (i=0;i<l_nb_proc;++i) {
9118 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
9122 /* and clear the procedure list at the end.*/
9123 opj_procedure_list_clear(p_procedure_list);
9128 opj_bool j2k_copy_default_tcp_and_create_tcd
9130 opj_j2k_v2_t * p_j2k,
9131 opj_stream_private_t *p_stream,
9132 opj_event_mgr_t * p_manager
9135 opj_tcp_v2_t * l_tcp = 00;
9136 opj_tcp_v2_t * l_default_tcp = 00;
9137 OPJ_UINT32 l_nb_tiles;
9139 opj_tccp_t *l_current_tccp = 00;
9140 OPJ_UINT32 l_tccp_size;
9141 OPJ_UINT32 l_mct_size;
9142 opj_image_t * l_image;
9143 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
9144 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
9145 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
9146 OPJ_UINT32 l_offset;
9149 assert(p_j2k != 00);
9150 assert(p_stream != 00);
9151 assert(p_manager != 00);
9153 l_image = p_j2k->m_private_image;
9154 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9155 l_tcp = p_j2k->m_cp.tcps;
9156 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
9157 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9158 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
9161 for (i=0; i<l_nb_tiles; ++i) {
9162 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
9163 l_current_tccp = l_tcp->tccps;
9164 /*Copy default coding parameters into the current tile coding parameters*/
9165 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
9166 /* Initialize some values of the current tile coding parameters*/
9168 l_tcp->ppt_data = 00;
9169 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
9170 l_tcp->tccps = l_current_tccp;
9172 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
9173 if (l_default_tcp->m_mct_decoding_matrix) {
9174 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
9175 if (! l_tcp->m_mct_decoding_matrix ) {
9178 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
9181 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
9182 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
9183 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
9184 if (! l_tcp->m_mct_records) {
9187 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
9189 /* Copy the mct record data from dflt_tile_cp to the current tile*/
9190 l_src_mct_rec = l_default_tcp->m_mct_records;
9191 l_dest_mct_rec = l_tcp->m_mct_records;
9193 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
9195 if (l_src_mct_rec->m_data) {
9197 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
9198 if(! l_dest_mct_rec->m_data) {
9201 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
9208 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
9209 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
9210 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
9211 if (! l_tcp->m_mcc_records) {
9214 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
9216 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
9217 l_src_mcc_rec = l_default_tcp->m_mcc_records;
9218 l_dest_mcc_rec = l_tcp->m_mcc_records;
9220 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
9222 if (l_src_mcc_rec->m_decorrelation_array) {
9223 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
9224 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
9227 if (l_src_mcc_rec->m_offset_array) {
9228 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
9229 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
9236 /* Copy all the dflt_tile_compo_cp to the current tile cp */
9237 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
9239 /* Move to next tile cp*/
9243 /* Create the current tile decoder*/
9244 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
9245 if (! p_j2k->m_tcd ) {
9249 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
9250 tcd_destroy_v2(p_j2k->m_tcd);
9252 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9260 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
9261 * with the marker value.
9262 * @param p_id Marker value to look up
9264 * @return the handler associated with the id.
9266 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
9268 const opj_dec_memory_marker_handler_t *e;
9269 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
9270 if (e->id == p_id) {
9271 break; /* we find a handler corresponding to the marker ID*/
9279 * Destroys a jpeg2000 codec.
9281 * @param p_j2k the jpeg20000 structure to destroy.
9283 void j2k_destroy (opj_j2k_v2_t *p_j2k)
9289 if (p_j2k->m_is_decoder) {
9291 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
9292 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9293 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9294 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
9297 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
9298 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
9299 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
9300 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
9305 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
9306 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9307 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
9310 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
9311 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
9312 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
9313 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
9316 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
9317 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
9318 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
9319 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
9323 tcd_destroy_v2(p_j2k->m_tcd);
9325 j2k_cp_destroy(&(p_j2k->m_cp));
9326 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
9328 opj_procedure_list_destroy(p_j2k->m_procedure_list);
9329 p_j2k->m_procedure_list = 00;
9331 opj_procedure_list_destroy(p_j2k->m_validation_list);
9332 p_j2k->m_procedure_list = 00;
9334 j2k_destroy_cstr_index(p_j2k->cstr_index);
9335 p_j2k->cstr_index = NULL;
9337 opj_image_destroy(p_j2k->m_private_image);
9338 p_j2k->m_private_image = NULL;
9340 opj_image_destroy(p_j2k->m_output_image);
9341 p_j2k->m_output_image = NULL;
9346 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9350 if (p_cstr_ind->marker) {
9351 opj_free(p_cstr_ind->marker);
9352 p_cstr_ind->marker = NULL;
9355 if (p_cstr_ind->tile_index) {
9356 OPJ_UINT32 it_tile = 0;
9358 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9360 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9361 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9362 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9365 if(p_cstr_ind->tile_index[it_tile].tp_index){
9366 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9367 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9370 if(p_cstr_ind->tile_index[it_tile].marker){
9371 opj_free(p_cstr_ind->tile_index[it_tile].marker);
9372 p_cstr_ind->tile_index[it_tile].marker = NULL;
9377 opj_free( p_cstr_ind->tile_index);
9378 p_cstr_ind->tile_index = NULL;
9381 opj_free(p_cstr_ind);
9388 * Destroys a tile coding parameter structure.
9390 * @param p_tcp the tile coding parameter to destroy.
9392 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9398 if (p_tcp->ppt_buffer != 00) {
9399 opj_free(p_tcp->ppt_buffer);
9400 p_tcp->ppt_buffer = 00;
9403 if (p_tcp->tccps != 00) {
9404 opj_free(p_tcp->tccps);
9408 if (p_tcp->m_mct_coding_matrix != 00) {
9409 opj_free(p_tcp->m_mct_coding_matrix);
9410 p_tcp->m_mct_coding_matrix = 00;
9413 if (p_tcp->m_mct_decoding_matrix != 00) {
9414 opj_free(p_tcp->m_mct_decoding_matrix);
9415 p_tcp->m_mct_decoding_matrix = 00;
9418 if (p_tcp->m_mcc_records) {
9419 opj_free(p_tcp->m_mcc_records);
9420 p_tcp->m_mcc_records = 00;
9421 p_tcp->m_nb_max_mcc_records = 0;
9422 p_tcp->m_nb_mcc_records = 0;
9425 if (p_tcp->m_mct_records) {
9426 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9429 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9430 if (l_mct_data->m_data) {
9431 opj_free(l_mct_data->m_data);
9432 l_mct_data->m_data = 00;
9438 opj_free(p_tcp->m_mct_records);
9439 p_tcp->m_mct_records = 00;
9442 if (p_tcp->mct_norms != 00) {
9443 opj_free(p_tcp->mct_norms);
9444 p_tcp->mct_norms = 00;
9447 j2k_tcp_data_destroy(p_tcp);
9452 * Destroys the data inside a tile coding parameter structure.
9454 * @param p_tcp the tile coding parameter which contain data to destroy.
9456 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9458 if (p_tcp->m_data) {
9459 opj_free(p_tcp->m_data);
9460 p_tcp->m_data = NULL;
9461 p_tcp->m_data_size = 0;
9466 * Destroys a coding parameter structure.
9468 * @param p_cp the coding parameter to destroy.
9470 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9472 OPJ_UINT32 l_nb_tiles;
9473 opj_tcp_v2_t * l_current_tile = 00;
9484 l_current_tile = p_cp->tcps;
9485 l_nb_tiles = p_cp->th * p_cp->tw;
9488 (i = 0; i < l_nb_tiles; ++i)
9490 j2k_tcp_destroy(l_current_tile);
9493 opj_free(p_cp->tcps);
9497 (p_cp->ppm_buffer != 00)
9499 opj_free(p_cp->ppm_buffer);
9500 p_cp->ppm_buffer = 00;
9503 (p_cp->comment != 00)
9505 opj_free(p_cp->comment);
9509 (! p_cp->m_is_decoder)
9512 (p_cp->m_specific_param.m_enc.m_matrice)
9514 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9515 p_cp->m_specific_param.m_enc.m_matrice = 00;
9523 * Reads a tile header.
9524 * @param p_j2k the jpeg2000 codec.
9525 * @param p_stream the stream to write data to.
9526 * @param p_manager the user event manager.
9528 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9529 OPJ_UINT32 * p_tile_index,
9530 OPJ_UINT32 * p_data_size,
9531 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9532 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9533 OPJ_UINT32 * p_nb_comps,
9535 opj_stream_private_t *p_stream,
9536 opj_event_mgr_t * p_manager )
9538 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9539 OPJ_UINT32 l_marker_size;
9540 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9541 opj_tcp_v2_t * l_tcp = NULL;
9542 OPJ_UINT32 l_nb_tiles;
9545 assert(p_stream != 00);
9546 assert(p_j2k != 00);
9547 assert(p_manager != 00);
9549 /* Reach the End Of Codestream ?*/
9550 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9551 l_current_marker = J2K_MS_EOC;
9553 /* We need to encounter a SOT marker (a new tile-part header) */
9554 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9558 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9559 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9561 /* Try to read until the Start Of Data is detected */
9562 while (l_current_marker != J2K_MS_SOD) {
9564 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9565 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9566 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9570 /* Read 2 bytes from the buffer as the marker size */
9571 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9573 /* Why this condition? FIXME */
9574 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9575 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9577 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9579 /* Get the marker handler from the marker ID */
9580 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9582 /* Check if the marker is known and if it is the right place to find it */
9583 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9584 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9587 /* FIXME manage case of unknown marker as in the main header ? */
9589 /* Check if the marker size is compatible with the header data size */
9590 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9591 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9592 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9593 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9596 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9599 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9600 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) {
9601 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9605 /* Read the marker segment with the correct marker handler */
9606 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9607 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9611 /* Add the marker to the codestream index*/
9612 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9614 l_marker_handler->id,
9615 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9616 l_marker_size + 4 );
9618 /* Keep the position of the last SOT marker read */
9619 if ( l_marker_handler->id == J2K_MS_SOT ) {
9620 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9621 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9623 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9628 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9629 /* Skip the rest of the tile part header*/
9630 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) {
9631 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9634 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9637 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9638 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9639 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9642 /* Read 2 bytes from the buffer as the new marker ID */
9643 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9647 /* If we didn't skip data before, we need to read the SOD marker*/
9648 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9649 /* Try to read the SOD marker and skip data ? FIXME */
9650 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
9656 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9657 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9658 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9659 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9663 /* Read 2 bytes from buffer as the new marker ID */
9664 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9668 /* Indicate we will try to read a new tile-part header*/
9669 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9670 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9671 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9673 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9674 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9675 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9679 /* Read 2 bytes from buffer as the new marker ID */
9680 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9684 /* Current marker is the EOC marker ?*/
9685 if (l_current_marker == J2K_MS_EOC) {
9686 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9687 p_j2k->m_current_tile_number = 0;
9688 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9693 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9694 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9695 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9697 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9698 ++p_j2k->m_current_tile_number;
9702 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9703 *p_go_on = OPJ_FALSE;
9709 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9710 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9714 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9715 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9717 *p_tile_index = p_j2k->m_current_tile_number;
9718 *p_go_on = OPJ_TRUE;
9719 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9720 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9721 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9722 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9723 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9724 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9726 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9732 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9733 OPJ_UINT32 p_tile_index,
9735 OPJ_UINT32 p_data_size,
9736 opj_stream_private_t *p_stream,
9737 opj_event_mgr_t * p_manager )
9739 OPJ_UINT32 l_current_marker;
9740 OPJ_BYTE l_data [2];
9741 opj_tcp_v2_t * l_tcp;
9744 assert(p_stream != 00);
9745 assert(p_j2k != 00);
9746 assert(p_manager != 00);
9748 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9749 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9753 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9754 if (! l_tcp->m_data) {
9755 j2k_tcp_destroy(l_tcp);
9759 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9763 p_j2k->cstr_index) ) {
9764 j2k_tcp_destroy(l_tcp);
9765 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9769 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9773 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9774 * we destroy just the data which will be re-read in read_tile_header*/
9775 /*j2k_tcp_destroy(l_tcp);
9776 p_j2k->m_tcd->tcp = 0;*/
9777 j2k_tcp_data_destroy(l_tcp);
9779 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9780 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9782 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9783 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9784 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9788 opj_read_bytes(l_data,&l_current_marker,2);
9790 if (l_current_marker == J2K_MS_EOC) {
9791 p_j2k->m_current_tile_number = 0;
9792 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9794 else if (l_current_marker != J2K_MS_SOT)
9796 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9805 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9807 OPJ_UINT32 i,j,k = 0;
9808 OPJ_UINT32 l_width_src,l_height_src;
9809 OPJ_UINT32 l_width_dest,l_height_dest;
9810 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9811 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9812 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9813 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9814 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9816 opj_image_comp_t * l_img_comp_src = 00;
9817 opj_image_comp_t * l_img_comp_dest = 00;
9819 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9820 opj_image_t * l_image_src = 00;
9821 OPJ_UINT32 l_size_comp, l_remaining;
9822 OPJ_INT32 * l_dest_ptr;
9823 opj_tcd_resolution_v2_t* l_res= 00;
9825 l_tilec = p_tcd->tcd_image->tiles->comps;
9826 l_image_src = p_tcd->image;
9827 l_img_comp_src = l_image_src->comps;
9829 l_img_comp_dest = p_output_image->comps;
9831 for (i=0; i<l_image_src->numcomps; i++) {
9833 /* Allocate output component buffer if necessary */
9834 if (!l_img_comp_dest->data) {
9836 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9837 if (! l_img_comp_dest->data) {
9842 /* Copy info from decoded comp image to output image */
9843 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9846 /* Compute the precision of the output buffer */
9847 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9848 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9849 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9855 if (l_size_comp == 3) {
9860 /* Current tile component size*/
9862 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9863 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9866 l_width_src = (l_res->x1 - l_res->x0);
9867 l_height_src = (l_res->y1 - l_res->y0);
9869 /* Border of the current output component*/
9870 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9871 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9872 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9873 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9876 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9877 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9881 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9882 * of the input buffer (decoded tile component) which will be move
9883 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9884 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9885 * by this input area.
9887 assert( l_res->x0 >= 0);
9888 assert( l_res->x1 >= 0);
9889 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9890 l_start_x_dest = l_res->x0 - l_x0_dest;
9891 l_offset_x0_src = 0;
9893 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9894 l_width_dest = l_width_src;
9895 l_offset_x1_src = 0;
9898 l_width_dest = l_x1_dest - l_res->x0 ;
9899 l_offset_x1_src = l_width_src - l_width_dest;
9903 l_start_x_dest = 0 ;
9904 l_offset_x0_src = l_x0_dest - l_res->x0;
9906 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9907 l_width_dest = l_width_src - l_offset_x0_src;
9908 l_offset_x1_src = 0;
9911 l_width_dest = l_img_comp_dest->w ;
9912 l_offset_x1_src = l_res->x1 - l_x1_dest;
9916 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9917 l_start_y_dest = l_res->y0 - l_y0_dest;
9918 l_offset_y0_src = 0;
9920 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9921 l_height_dest = l_height_src;
9922 l_offset_y1_src = 0;
9925 l_height_dest = l_y1_dest - l_res->y0 ;
9926 l_offset_y1_src = l_height_src - l_height_dest;
9930 l_start_y_dest = 0 ;
9931 l_offset_y0_src = l_y0_dest - l_res->y0;
9933 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9934 l_height_dest = l_height_src - l_offset_y0_src;
9935 l_offset_y1_src = 0;
9938 l_height_dest = l_img_comp_dest->h ;
9939 l_offset_y1_src = l_res->y1 - l_y1_dest;
9943 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9948 /* Compute the input buffer offset */
9949 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9950 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9951 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9953 /* Compute the output buffer offset */
9954 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9955 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9957 /* Move the output buffer to the first place where we will write*/
9958 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9961 fprintf(stdout, "COMPO[%d]:\n",i);
9962 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9963 "\t tile offset:%d, %d, %d, %d\n"
9964 "\t buffer offset: %d; %d, %d\n",
9965 l_res->x0, l_res->y0, l_width_src, l_height_src,
9966 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9967 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9969 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9970 "\t start offset: %d, line offset= %d\n",
9971 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9975 switch (l_size_comp) {
9978 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9979 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9981 if (l_img_comp_src->sgnd) {
9982 for (j = 0 ; j < l_height_dest ; ++j) {
9983 for ( k = 0 ; k < l_width_dest ; ++k) {
9984 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9987 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9988 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9992 for ( j = 0 ; j < l_height_dest ; ++j ) {
9993 for ( k = 0 ; k < l_width_dest ; ++k) {
9994 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9997 l_dest_ptr+= l_line_offset_dest;
9998 l_src_ptr += l_line_offset_src;
10002 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
10003 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
10008 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
10009 l_src_ptr += l_start_offset_src;
10011 if (l_img_comp_src->sgnd) {
10012 for (j=0;j<l_height_dest;++j) {
10013 for (k=0;k<l_width_dest;++k) {
10014 *(l_dest_ptr++) = *(l_src_ptr++);
10017 l_dest_ptr+= l_line_offset_dest;
10018 l_src_ptr += l_line_offset_src ;
10022 for (j=0;j<l_height_dest;++j) {
10023 for (k=0;k<l_width_dest;++k) {
10024 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10027 l_dest_ptr+= l_line_offset_dest;
10028 l_src_ptr += l_line_offset_src ;
10032 l_src_ptr += l_end_offset_src;
10033 p_data = (OPJ_BYTE*) l_src_ptr;
10038 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
10039 l_src_ptr += l_start_offset_src;
10041 for (j=0;j<l_height_dest;++j) {
10042 for (k=0;k<l_width_dest;++k) {
10043 *(l_dest_ptr++) = (*(l_src_ptr++));
10046 l_dest_ptr+= l_line_offset_dest;
10047 l_src_ptr += l_line_offset_src ;
10050 l_src_ptr += l_end_offset_src;
10051 p_data = (OPJ_BYTE*) l_src_ptr;
10065 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
10067 * @param p_j2k the jpeg2000 codec.
10068 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
10069 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
10070 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
10071 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
10072 * @param p_manager the user event manager
10074 * @return true if the area could be set.
10076 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
10077 opj_image_t* p_image,
10078 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
10079 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
10080 struct opj_event_mgr * p_manager )
10082 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
10083 opj_image_t * l_image = p_j2k->m_private_image;
10085 OPJ_UINT32 it_comp;
10086 OPJ_INT32 l_comp_x1, l_comp_y1;
10087 opj_image_comp_t* l_img_comp = NULL;
10089 /* Check if we are read the main header */
10090 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
10091 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
10095 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
10096 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
10098 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10099 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10100 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10101 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10107 /* Check if the positions provided by the user are correct */
10110 assert(p_start_x >= 0 );
10111 assert(p_start_y >= 0 );
10113 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
10114 opj_event_msg_v2(p_manager, EVT_ERROR,
10115 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
10116 p_start_x, l_image->x1);
10119 else if ((OPJ_UINT32)p_start_x < l_image->x0){
10120 opj_event_msg_v2(p_manager, EVT_WARNING,
10121 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
10122 p_start_x, l_image->x0);
10123 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10124 p_image->x0 = l_image->x0;
10127 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
10128 p_image->x0 = p_start_x;
10132 if ((OPJ_UINT32)p_start_y > l_image->y1){
10133 opj_event_msg_v2(p_manager, EVT_ERROR,
10134 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
10135 p_start_y, l_image->y1);
10138 else if ((OPJ_UINT32)p_start_y < l_image->y0){
10139 opj_event_msg_v2(p_manager, EVT_WARNING,
10140 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
10141 p_start_y, l_image->y0);
10142 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10143 p_image->y0 = l_image->y0;
10146 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
10147 p_image->y0 = p_start_y;
10151 assert((OPJ_UINT32)p_end_x > 0);
10152 assert((OPJ_UINT32)p_end_y > 0);
10153 if ((OPJ_UINT32)p_end_x < l_image->x0) {
10154 opj_event_msg_v2(p_manager, EVT_ERROR,
10155 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
10156 p_end_x, l_image->x0);
10159 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
10160 opj_event_msg_v2(p_manager, EVT_WARNING,
10161 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
10162 p_end_x, l_image->x1);
10163 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10164 p_image->x1 = l_image->x1;
10167 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
10168 p_image->x1 = p_end_x;
10172 if ((OPJ_UINT32)p_end_y < l_image->y0) {
10173 opj_event_msg_v2(p_manager, EVT_ERROR,
10174 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
10175 p_end_y, l_image->y0);
10178 if ((OPJ_UINT32)p_end_y > l_image->y1){
10179 opj_event_msg_v2(p_manager, EVT_WARNING,
10180 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
10181 p_end_y, l_image->y1);
10182 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10183 p_image->y1 = l_image->y1;
10186 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
10187 p_image->y1 = p_end_y;
10191 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
10193 l_img_comp = p_image->comps;
10194 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
10198 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10199 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10200 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10201 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10203 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
10204 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10206 opj_event_msg_v2(p_manager, EVT_ERROR,
10207 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
10211 l_img_comp->w = l_w;
10213 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
10214 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10216 opj_event_msg_v2(p_manager, EVT_ERROR,
10217 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
10221 l_img_comp->h = l_h;
10226 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
10227 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
10234 /* ----------------------------------------------------------------------- */
10235 /* J2K / JPT decoder interface */
10236 /* ----------------------------------------------------------------------- */
10238 * Creates a J2K decompression structure.
10240 * @return a handle to a J2K decompressor if successful, NULL otherwise.
10242 opj_j2k_v2_t* opj_j2k_create_decompress(void)
10244 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
10248 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
10250 l_j2k->m_is_decoder = 1;
10251 l_j2k->m_cp.m_is_decoder = 1;
10253 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
10254 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
10255 j2k_destroy(l_j2k);
10258 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
10260 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
10261 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
10262 j2k_destroy(l_j2k);
10266 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
10268 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
10270 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
10272 /* codestream index creation */
10273 l_j2k->cstr_index = j2k_create_cstr_index();
10275 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
10276 if (!l_j2k->cstr_index){
10277 j2k_destroy(l_j2k);
10281 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
10284 /* validation list creation */
10285 l_j2k->m_validation_list = opj_procedure_list_create();
10286 if (! l_j2k->m_validation_list) {
10287 j2k_destroy(l_j2k);
10291 /* execution list creation */
10292 l_j2k->m_procedure_list = opj_procedure_list_create();
10293 if (! l_j2k->m_procedure_list) {
10294 j2k_destroy(l_j2k);
10302 opj_codestream_index_t* j2k_create_cstr_index(void)
10304 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
10305 opj_calloc(1,sizeof(opj_codestream_index_t));
10309 cstr_index->maxmarknum = 100;
10310 cstr_index->marknum = 0;
10311 cstr_index->marker = (opj_marker_info_t*)
10312 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
10313 if (!cstr_index-> marker)
10316 cstr_index->tile_index = NULL;
10323 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
10325 * @param p_tile_no the tile index.
10326 * @param p_comp_no the component being outputted.
10327 * @param p_j2k the J2K codec.
10329 * @return the number of bytes taken by the SPCod element.
10331 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
10332 OPJ_UINT32 p_tile_no,
10333 OPJ_UINT32 p_comp_no )
10335 opj_cp_v2_t *l_cp = 00;
10336 opj_tcp_v2_t *l_tcp = 00;
10337 opj_tccp_t *l_tccp = 00;
10339 /* preconditions */
10340 assert(p_j2k != 00);
10342 l_cp = &(p_j2k->m_cp);
10343 l_tcp = &l_cp->tcps[p_tile_no];
10344 l_tccp = &l_tcp->tccps[p_comp_no];
10346 /* preconditions again */
10347 assert(p_tile_no < (l_cp->tw * l_cp->th));
10348 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10350 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10351 return 5 + l_tccp->numresolutions;
10359 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10361 * @param p_comp_no the component number to output.
10362 * @param p_stream the stream to write data to.
10363 * @param p_j2k J2K codec.
10364 * @param p_manager the user event manager.
10367 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10368 OPJ_UINT32 p_tile_no,
10369 OPJ_UINT32 p_comp_no,
10371 OPJ_UINT32 * p_header_size,
10372 struct opj_event_mgr * p_manager )
10375 opj_cp_v2_t *l_cp = 00;
10376 opj_tcp_v2_t *l_tcp = 00;
10377 opj_tccp_t *l_tccp = 00;
10379 /* preconditions */
10380 assert(p_j2k != 00);
10381 assert(p_header_size != 00);
10382 assert(p_manager != 00);
10383 assert(p_data != 00);
10385 l_cp = &(p_j2k->m_cp);
10386 l_tcp = &l_cp->tcps[p_tile_no];
10387 l_tccp = &l_tcp->tccps[p_comp_no];
10389 /* preconditions again */
10390 assert(p_tile_no < (l_cp->tw * l_cp->th));
10391 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10393 if (*p_header_size < 5) {
10394 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10398 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
10401 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
10404 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
10407 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
10410 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
10413 *p_header_size = *p_header_size - 5;
10415 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10417 if (*p_header_size < l_tccp->numresolutions) {
10418 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10422 for (i = 0; i < l_tccp->numresolutions; ++i) {
10423 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
10427 *p_header_size = *p_header_size - l_tccp->numresolutions;
10434 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10435 * @param p_header_data the data contained in the COM box.
10436 * @param p_j2k the jpeg2000 codec.
10437 * @param p_header_size the size of the data contained in the COM marker.
10438 * @param p_manager the user event manager.
10440 opj_bool j2k_read_SPCod_SPCoc(
10441 opj_j2k_v2_t *p_j2k,
10443 OPJ_BYTE * p_header_data,
10444 OPJ_UINT32 * p_header_size,
10445 struct opj_event_mgr * p_manager
10448 OPJ_UINT32 i, l_tmp;
10449 opj_cp_v2_t *l_cp = NULL;
10450 opj_tcp_v2_t *l_tcp = NULL;
10451 opj_tccp_t *l_tccp = NULL;
10452 OPJ_BYTE * l_current_ptr = NULL;
10454 /* preconditions */
10455 assert(p_j2k != 00);
10456 assert(p_manager != 00);
10457 assert(p_header_data != 00);
10459 l_cp = &(p_j2k->m_cp);
10460 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10461 &l_cp->tcps[p_j2k->m_current_tile_number] :
10462 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10464 /* precondition again */
10465 assert(compno < p_j2k->m_private_image->numcomps);
10467 l_tccp = &l_tcp->tccps[compno];
10468 l_current_ptr = p_header_data;
10470 /* make sure room is sufficient */
10471 if (*p_header_size < 5) {
10472 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10476 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10477 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10480 /* If user wants to remove more resolutions than the codestream contains, return error */
10481 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10482 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10483 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10484 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10488 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10490 l_tccp->cblkw += 2;
10492 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10494 l_tccp->cblkh += 2;
10496 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10499 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10502 *p_header_size = *p_header_size - 5;
10504 /* use custom precinct size ? */
10505 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10506 if (*p_header_size < l_tccp->numresolutions) {
10507 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10511 for (i = 0; i < l_tccp->numresolutions; ++i) {
10512 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10514 l_tccp->prcw[i] = l_tmp & 0xf;
10515 l_tccp->prch[i] = l_tmp >> 4;
10518 *p_header_size = *p_header_size - l_tccp->numresolutions;
10521 /* set default size for the precinct width and height */
10522 for (i = 0; i < l_tccp->numresolutions; ++i) {
10523 l_tccp->prcw[i] = 15;
10524 l_tccp->prch[i] = 15;
10528 #ifdef WIP_REMOVE_MSD
10530 if (p_j2k->cstr_info && compno == 0) {
10531 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10533 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10534 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10535 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10536 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10537 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10540 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10541 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10550 * Copies the tile component parameters of all the component from the first tile component.
10552 * @param p_j2k the J2k codec.
10554 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10558 opj_cp_v2_t *l_cp = NULL;
10559 opj_tcp_v2_t *l_tcp = NULL;
10560 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10561 OPJ_UINT32 l_prc_size;
10563 /* preconditions */
10564 assert(p_j2k != 00);
10566 l_cp = &(p_j2k->m_cp);
10567 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10568 &l_cp->tcps[p_j2k->m_current_tile_number] :
10569 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10571 l_ref_tccp = &l_tcp->tccps[0];
10572 l_copied_tccp = l_ref_tccp + 1;
10573 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10575 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10576 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10577 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10578 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10579 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10580 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10581 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10582 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10588 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10590 * @param p_tile_no the tile index.
10591 * @param p_comp_no the component being outputted.
10592 * @param p_j2k the J2K codec.
10594 * @return the number of bytes taken by the SPCod element.
10596 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10597 OPJ_UINT32 p_tile_no,
10598 OPJ_UINT32 p_comp_no )
10600 OPJ_UINT32 l_num_bands;
10602 opj_cp_v2_t *l_cp = 00;
10603 opj_tcp_v2_t *l_tcp = 00;
10604 opj_tccp_t *l_tccp = 00;
10606 /* preconditions */
10607 assert(p_j2k != 00);
10609 l_cp = &(p_j2k->m_cp);
10610 l_tcp = &l_cp->tcps[p_tile_no];
10611 l_tccp = &l_tcp->tccps[p_comp_no];
10613 /* preconditions again */
10614 assert(p_tile_no < l_cp->tw * l_cp->th);
10615 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10617 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10619 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10620 return 1 + l_num_bands;
10623 return 1 + 2*l_num_bands;
10628 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10630 * @param p_tile_no the tile to output.
10631 * @param p_comp_no the component number to output.
10632 * @param p_data the data buffer.
10633 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10634 * @param p_j2k J2K codec.
10635 * @param p_manager the user event manager.
10638 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10639 OPJ_UINT32 p_tile_no,
10640 OPJ_UINT32 p_comp_no,
10642 OPJ_UINT32 * p_header_size,
10643 struct opj_event_mgr * p_manager )
10645 OPJ_UINT32 l_header_size;
10646 OPJ_UINT32 l_band_no, l_num_bands;
10647 OPJ_UINT32 l_expn,l_mant;
10649 opj_cp_v2_t *l_cp = 00;
10650 opj_tcp_v2_t *l_tcp = 00;
10651 opj_tccp_t *l_tccp = 00;
10653 /* preconditions */
10654 assert(p_j2k != 00);
10655 assert(p_header_size != 00);
10656 assert(p_manager != 00);
10657 assert(p_data != 00);
10659 l_cp = &(p_j2k->m_cp);
10660 l_tcp = &l_cp->tcps[p_tile_no];
10661 l_tccp = &l_tcp->tccps[p_comp_no];
10663 /* preconditions again */
10664 assert(p_tile_no < l_cp->tw * l_cp->th);
10665 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10667 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10669 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10670 l_header_size = 1 + l_num_bands;
10672 if (*p_header_size < l_header_size) {
10673 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10677 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10680 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10681 l_expn = l_tccp->stepsizes[l_band_no].expn;
10682 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10687 l_header_size = 1 + 2*l_num_bands;
10689 if (*p_header_size < l_header_size) {
10690 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10694 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10697 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10698 l_expn = l_tccp->stepsizes[l_band_no].expn;
10699 l_mant = l_tccp->stepsizes[l_band_no].mant;
10701 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10706 *p_header_size = *p_header_size - l_header_size;
10712 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10714 * @param p_comp_no the component being targeted.
10715 * @param p_header_data the data contained in the COM box.
10716 * @param p_j2k the jpeg2000 codec.
10717 * @param p_header_size the size of the data contained in the COM marker.
10718 * @param p_manager the user event manager.
10720 opj_bool j2k_read_SQcd_SQcc(
10721 opj_j2k_v2_t *p_j2k,
10722 OPJ_UINT32 p_comp_no,
10723 OPJ_BYTE* p_header_data,
10724 OPJ_UINT32 * p_header_size,
10725 struct opj_event_mgr * p_manager
10729 OPJ_UINT32 l_band_no;
10730 opj_cp_v2_t *l_cp = 00;
10731 opj_tcp_v2_t *l_tcp = 00;
10732 opj_tccp_t *l_tccp = 00;
10733 OPJ_BYTE * l_current_ptr = 00;
10734 OPJ_UINT32 l_tmp, l_num_band;
10737 assert(p_j2k != 00);
10738 assert(p_manager != 00);
10739 assert(p_header_data != 00);
10741 l_cp = &(p_j2k->m_cp);
10742 /* come from tile part header or main header ?*/
10743 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10744 &l_cp->tcps[p_j2k->m_current_tile_number] :
10745 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10747 /* precondition again*/
10748 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10750 l_tccp = &l_tcp->tccps[p_comp_no];
10751 l_current_ptr = p_header_data;
10753 if (*p_header_size < 1) {
10754 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10757 *p_header_size -= 1;
10759 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10762 l_tccp->qntsty = l_tmp & 0x1f;
10763 l_tccp->numgbits = l_tmp >> 5;
10764 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10768 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10770 (*p_header_size) / 2;
10772 if( l_num_band > J2K_MAXBANDS ) {
10773 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10774 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10775 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10776 /*return OPJ_FALSE;*/
10781 if (l_cp->correct) {
10783 /* if JPWL is on, we check whether there are too many subbands */
10784 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10785 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10786 "JPWL: bad number of subbands in Sqcx (%d)\n",
10788 if (!JPWL_ASSUME) {
10789 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10792 /* we try to correct */
10794 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10795 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10800 #endif /* USE_JPWL */
10802 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10803 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10804 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10806 if (l_band_no < J2K_MAXBANDS){
10807 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10808 l_tccp->stepsizes[l_band_no].mant = 0;
10811 *p_header_size = *p_header_size - l_num_band;
10814 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10815 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10817 if (l_band_no < J2K_MAXBANDS){
10818 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10819 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10822 *p_header_size = *p_header_size - 2*l_num_band;
10825 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10826 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10827 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10828 l_tccp->stepsizes[l_band_no].expn =
10829 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10830 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10831 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10839 * Copies the tile component parameters of all the component from the first tile component.
10841 * @param p_j2k the J2k codec.
10843 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10846 opj_cp_v2_t *l_cp = NULL;
10847 opj_tcp_v2_t *l_tcp = NULL;
10848 opj_tccp_t *l_ref_tccp = NULL;
10849 opj_tccp_t *l_copied_tccp = NULL;
10852 /* preconditions */
10853 assert(p_j2k != 00);
10855 l_cp = &(p_j2k->m_cp);
10856 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10857 &l_cp->tcps[p_j2k->m_current_tile_number] :
10858 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10860 l_ref_tccp = &l_tcp->tccps[0];
10861 l_copied_tccp = l_ref_tccp + 1;
10862 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10864 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10865 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10866 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10867 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10873 * Dump some elements from the J2K decompression structure .
10875 *@param p_j2k the jpeg2000 codec.
10876 *@param flag flag to describe what elments are dump.
10877 *@param out_stream output stream where dump the elements.
10880 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10882 /* Check if the flag is compatible with j2k file*/
10883 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10884 fprintf(out_stream, "Wrong flag\n");
10888 /* Dump the image_header */
10889 if (flag & OPJ_IMG_INFO){
10890 if (p_j2k->m_private_image)
10891 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10894 /* Dump the codestream info from main header */
10895 if (flag & OPJ_J2K_MH_INFO){
10896 j2k_dump_MH_info(p_j2k, out_stream);
10900 /* Dump the codestream info of the current tile */
10901 if (flag & OPJ_J2K_TH_INFO){
10905 /* Dump the codestream index from main header */
10906 if (flag & OPJ_J2K_MH_IND){
10907 j2k_dump_MH_index(p_j2k, out_stream);
10910 /* Dump the codestream index of the current tile */
10911 if (flag & OPJ_J2K_TH_IND){
10918 * Dump index elements of the codestream extract from the main header.
10920 *@param p_j2k the jpeg2000 codec.
10921 *@param out_stream output stream where dump the elements.
10924 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10926 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10927 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10929 fprintf(out_stream, "Codestream index from main header: {\n");
10931 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10932 "\t Main header end position=%" PRIi64 "\n",
10933 cstr_index->main_head_start, cstr_index->main_head_end);
10935 fprintf(out_stream, "\t Marker list: {\n");
10937 if (cstr_index->marker){
10938 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10939 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10940 cstr_index->marker[it_marker].type,
10941 cstr_index->marker[it_marker].pos,
10942 cstr_index->marker[it_marker].len );
10946 fprintf(out_stream, "\t }\n");
10949 if (cstr_index->tile_index){
10951 /* Simple test to avoid to write empty information*/
10952 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10953 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10954 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10957 if (l_acc_nb_of_tile_part)
10959 fprintf(out_stream, "\t Tile index: {\n");
10961 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10962 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10964 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10966 if (cstr_index->tile_index[it_tile].tp_index){
10967 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10968 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10970 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10971 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10972 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10976 if (cstr_index->tile_index[it_tile].marker){
10977 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10978 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10979 cstr_index->tile_index[it_tile].marker[it_marker].type,
10980 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10981 cstr_index->tile_index[it_tile].marker[it_marker].len );
10985 fprintf(out_stream,"\t }\n");
10989 fprintf(out_stream,"}\n");
10994 * Dump info elements of the codestream extract from the main header.
10996 *@param p_j2k the jpeg2000 codec.
10997 *@param out_stream output stream where dump the elements.
11000 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11002 opj_tcp_v2_t * l_default_tile=NULL;
11004 fprintf(out_stream, "Codestream info from main header: {\n");
11006 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
11007 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
11008 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
11010 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11011 if (l_default_tile)
11014 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
11016 fprintf(out_stream, "\t default tile {\n");
11017 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
11018 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
11019 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
11020 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
11022 for (compno = 0; compno < numcomps; compno++) {
11023 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11025 OPJ_INT32 bandno, numbands;
11028 fprintf(out_stream, "\t\t comp %d {\n", compno);
11029 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
11030 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
11031 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
11032 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
11033 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
11034 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
11036 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
11037 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
11038 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
11040 fprintf(out_stream, "\n");
11042 /* quantization style*/
11043 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
11044 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
11045 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
11046 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11047 for (bandno = 0; bandno < numbands; bandno++) {
11048 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
11049 l_tccp->stepsizes[bandno].expn);
11051 fprintf(out_stream, "\n");
11054 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
11056 fprintf(out_stream, "\t\t }\n");
11057 } /*end of component of default tile*/
11058 fprintf(out_stream, "\t }\n"); /*end of default tile*/
11062 fprintf(out_stream, "}\n");
11067 * Dump an image header structure.
11069 *@param img_header the image header to dump.
11070 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11071 *@param out_stream output stream where dump the elements.
11073 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
11077 if (dev_dump_flag){
11078 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
11082 fprintf(out_stream, "Image info {\n");
11083 tab[0] = '\t';tab[1] = '\0';
11086 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
11087 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
11088 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
11090 if (img_header->comps){
11092 for (compno = 0; compno < img_header->numcomps; compno++) {
11093 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
11094 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
11095 fprintf(out_stream,"%s}\n",tab);
11099 fprintf(out_stream, "}\n");
11103 * Dump a component image header structure.
11105 *@param comp_header the component image header to dump.
11106 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11107 *@param out_stream output stream where dump the elements.
11109 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
11113 if (dev_dump_flag){
11114 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
11117 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
11120 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
11121 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
11122 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
11125 fprintf(out_stream, "}\n");
11130 * Get the codestream info from a JPEG2000 codec.
11132 *@param p_j2k the component image header to dump.
11134 *@return the codestream information extract from the jpg2000 codec
11136 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
11139 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
11140 opj_tcp_v2_t *l_default_tile;
11141 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
11143 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
11145 cstr_info->tx0 = p_j2k->m_cp.tx0;
11146 cstr_info->ty0 = p_j2k->m_cp.ty0;
11147 cstr_info->tdx = p_j2k->m_cp.tdx;
11148 cstr_info->tdy = p_j2k->m_cp.tdy;
11149 cstr_info->tw = p_j2k->m_cp.tw;
11150 cstr_info->th = p_j2k->m_cp.th;
11152 cstr_info->tile_info = NULL; /* Not fill from the main header*/
11154 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11156 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
11157 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
11158 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
11159 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
11161 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
11163 for (compno = 0; compno < numcomps; compno++) {
11164 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11165 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
11166 OPJ_INT32 bandno, numbands;
11169 l_tccp_info->csty = l_tccp->csty;
11170 l_tccp_info->numresolutions = l_tccp->numresolutions;
11171 l_tccp_info->cblkw = l_tccp->cblkw;
11172 l_tccp_info->cblkh = l_tccp->cblkh;
11173 l_tccp_info->cblksty = l_tccp->cblksty;
11174 l_tccp_info->qmfbid = l_tccp->qmfbid;
11175 if (l_tccp->numresolutions < J2K_MAXRLVLS)
11177 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
11178 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
11181 /* quantization style*/
11182 l_tccp_info->qntsty = l_tccp->qntsty;
11183 l_tccp_info->numgbits = l_tccp->numgbits;
11185 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11186 if (numbands < J2K_MAXBANDS) {
11187 for (bandno = 0; bandno < numbands; bandno++) {
11188 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
11189 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
11194 l_tccp_info->roishift = l_tccp->roishift;
11202 * Get the codestream index from a JPEG2000 codec.
11204 *@param p_j2k the component image header to dump.
11206 *@return the codestream index extract from the jpg2000 codec
11208 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
11210 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
11211 opj_calloc(1,sizeof(opj_codestream_index_t));
11215 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
11216 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
11217 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
11219 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
11220 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
11221 if (!l_cstr_index->marker){
11222 opj_free( l_cstr_index);
11226 if (p_j2k->cstr_index->marker)
11227 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
11229 opj_free(l_cstr_index->marker);
11230 l_cstr_index->marker = NULL;
11233 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
11234 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
11235 if (!l_cstr_index->tile_index){
11236 opj_free( l_cstr_index->marker);
11237 opj_free( l_cstr_index);
11241 if (!p_j2k->cstr_index->tile_index){
11242 opj_free(l_cstr_index->tile_index);
11243 l_cstr_index->tile_index = NULL;
11246 OPJ_UINT32 it_tile = 0;
11247 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
11250 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
11252 l_cstr_index->tile_index[it_tile].marker =
11253 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
11255 if (!l_cstr_index->tile_index[it_tile].marker) {
11256 OPJ_UINT32 it_tile_free;
11258 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11259 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11262 opj_free( l_cstr_index->tile_index);
11263 opj_free( l_cstr_index->marker);
11264 opj_free( l_cstr_index);
11268 if (p_j2k->cstr_index->tile_index[it_tile].marker)
11269 memcpy( l_cstr_index->tile_index[it_tile].marker,
11270 p_j2k->cstr_index->tile_index[it_tile].marker,
11271 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
11273 opj_free(l_cstr_index->tile_index[it_tile].marker);
11274 l_cstr_index->tile_index[it_tile].marker = NULL;
11277 /* Tile part index*/
11278 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
11280 l_cstr_index->tile_index[it_tile].tp_index =
11281 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
11283 if(!l_cstr_index->tile_index[it_tile].tp_index){
11284 OPJ_UINT32 it_tile_free;
11286 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11287 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11288 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
11291 opj_free( l_cstr_index->tile_index);
11292 opj_free( l_cstr_index->marker);
11293 opj_free( l_cstr_index);
11297 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
11298 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
11299 p_j2k->cstr_index->tile_index[it_tile].tp_index,
11300 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
11303 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
11304 l_cstr_index->tile_index[it_tile].tp_index = NULL;
11307 /* Packet index (NOT USED)*/
11308 l_cstr_index->tile_index[it_tile].nb_packet = 0;
11309 l_cstr_index->tile_index[it_tile].packet_index = NULL;
11314 return l_cstr_index;
11317 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
11319 OPJ_UINT32 it_tile=0;
11321 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
11322 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
11323 if (!p_j2k->cstr_index->tile_index)
11326 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
11327 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
11328 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
11329 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
11330 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
11331 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11341 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
11342 opj_stream_private_t *p_stream,
11343 opj_event_mgr_t * p_manager)
11345 opj_bool l_go_on = OPJ_TRUE;
11346 OPJ_UINT32 l_current_tile_no;
11347 OPJ_UINT32 l_data_size,l_max_data_size;
11348 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11349 OPJ_UINT32 l_nb_comps;
11350 OPJ_BYTE * l_current_data;
11352 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11353 if (! l_current_data) {
11356 l_max_data_size = 1000;
11361 if (! j2k_read_tile_header( p_j2k,
11362 &l_current_tile_no,
11364 &l_tile_x0, &l_tile_y0,
11365 &l_tile_x1, &l_tile_y1,
11370 opj_free(l_current_data);
11378 if (l_data_size > l_max_data_size) {
11379 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11380 if (! l_current_data) {
11381 opj_free(l_current_data);
11385 l_max_data_size = l_data_size;
11388 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11389 opj_free(l_current_data);
11392 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);
11394 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11395 opj_free(l_current_data);
11398 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11402 opj_free(l_current_data);
11408 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11410 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11413 assert(p_j2k != 00);
11415 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11416 /* DEVELOPER CORNER, add your custom procedures */
11421 * Read and decode one tile.
11423 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
11424 opj_stream_private_t *p_stream,
11425 opj_event_mgr_t * p_manager)
11427 opj_bool l_go_on = OPJ_TRUE;
11428 OPJ_UINT32 l_current_tile_no;
11429 OPJ_UINT32 l_tile_no_to_dec;
11430 OPJ_UINT32 l_data_size,l_max_data_size;
11431 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11432 OPJ_UINT32 l_nb_comps;
11433 OPJ_BYTE * l_current_data;
11435 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11436 if (! l_current_data) {
11439 l_max_data_size = 1000;
11441 /*Allocate and initialize some elements of codestrem index if not already done*/
11442 if( !p_j2k->cstr_index->tile_index)
11444 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11445 opj_free(l_current_data);
11449 /* Move into the codestream to the first SOT used to decode the desired tile */
11450 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11451 if (p_j2k->cstr_index->tile_index)
11452 if(p_j2k->cstr_index->tile_index->tp_index)
11454 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11455 /* the index for this tile has not been built,
11456 * so move to the last SOT read */
11457 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11458 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11463 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)) {
11464 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11468 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11469 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11470 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11474 if (! j2k_read_tile_header( p_j2k,
11475 &l_current_tile_no,
11477 &l_tile_x0, &l_tile_y0,
11478 &l_tile_x1, &l_tile_y1,
11483 opj_free(l_current_data);
11492 if (l_data_size > l_max_data_size) {
11493 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11494 if (! l_current_data) {
11495 opj_free(l_current_data);
11499 l_max_data_size = l_data_size;
11504 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11505 opj_free(l_current_data);
11508 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);
11510 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11511 opj_free(l_current_data);
11514 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11516 if(l_current_tile_no == l_tile_no_to_dec)
11518 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11519 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11520 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11526 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);
11531 opj_free(l_current_data);
11538 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11540 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11543 assert(p_j2k != 00);
11545 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11546 /* DEVELOPER CORNER, add your custom procedures */
11552 * Decodes the tiles of the stream.
11554 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
11555 opj_stream_private_t * p_stream,
11556 opj_image_t * p_image,
11557 opj_event_mgr_t * p_manager)
11564 p_j2k->m_output_image = opj_image_create0();
11565 if (! (p_j2k->m_output_image)) {
11568 opj_copy_image_header(p_image, p_j2k->m_output_image);
11570 /* customization of the decoding */
11571 j2k_setup_decoding(p_j2k);
11573 /* Decode the codestream */
11574 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11575 opj_image_destroy(p_j2k->m_private_image);
11576 p_j2k->m_private_image = NULL;
11580 /* Move data and copy one information from codec to output image*/
11581 for (compno = 0; compno < p_image->numcomps; compno++) {
11582 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11583 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11584 p_j2k->m_output_image->comps[compno].data = NULL;
11592 * Get the decoded tile.
11594 * @param p_j2k the jpeg2000 codestream codec.
11595 * @param p_stream input_stream
11596 * @param p_image output image. .
11597 * @param p_manager the user event manager
11598 * @param tile_index index of the tile we want decode
11600 * @return true if succeed.
11602 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11603 opj_stream_private_t *p_stream,
11604 opj_image_t* p_image,
11605 struct opj_event_mgr * p_manager,
11606 OPJ_UINT32 tile_index )
11609 OPJ_UINT32 l_tile_x, l_tile_y;
11610 opj_image_comp_t* l_img_comp;
11613 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11617 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11618 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);
11622 /* Compute the dimension of the desired tile*/
11623 l_tile_x = tile_index % p_j2k->m_cp.tw;
11624 l_tile_y = tile_index / p_j2k->m_cp.tw;
11626 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11627 if (p_image->x0 < p_j2k->m_private_image->x0)
11628 p_image->x0 = p_j2k->m_private_image->x0;
11629 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11630 if (p_image->x1 > p_j2k->m_private_image->x1)
11631 p_image->x1 = p_j2k->m_private_image->x1;
11633 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11634 if (p_image->y0 < p_j2k->m_private_image->y0)
11635 p_image->y0 = p_j2k->m_private_image->y0;
11636 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11637 if (p_image->y1 > p_j2k->m_private_image->y1)
11638 p_image->y1 = p_j2k->m_private_image->y1;
11640 l_img_comp = p_image->comps;
11641 for (compno=0; compno < p_image->numcomps; ++compno)
11643 OPJ_INT32 l_comp_x1, l_comp_y1;
11645 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11647 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11648 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11649 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11650 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11652 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11653 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11658 /* Destroy the previous output image*/
11659 if (p_j2k->m_output_image)
11660 opj_image_destroy(p_j2k->m_output_image);
11662 /* Create the ouput image from the information previously computed*/
11663 p_j2k->m_output_image = opj_image_create0();
11664 if (! (p_j2k->m_output_image)) {
11667 opj_copy_image_header(p_image, p_j2k->m_output_image);
11669 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11671 /* customization of the decoding */
11672 j2k_setup_decoding_tile(p_j2k);
11674 /* Decode the codestream */
11675 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11676 opj_image_destroy(p_j2k->m_private_image);
11677 p_j2k->m_private_image = NULL;
11681 /* Move data and copy one information from codec to output image*/
11682 for (compno = 0; compno < p_image->numcomps; compno++) {
11683 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11685 if (p_image->comps[compno].data)
11686 opj_free(p_image->comps[compno].data);
11688 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11690 p_j2k->m_output_image->comps[compno].data = NULL;
11696 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11698 OPJ_UINT32 it_comp;
11700 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11702 if (p_j2k->m_private_image) {
11703 if (p_j2k->m_private_image->comps) {
11704 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11705 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11706 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11707 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11708 if ( res_factor >= max_res){
11709 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11712 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11725 * Encodes all the tiles in a row.
11727 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11728 opj_stream_private_t *p_stream,
11729 opj_event_mgr_t * p_manager )
11732 OPJ_UINT32 l_nb_tiles;
11733 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11734 OPJ_BYTE * l_current_data;
11736 /* preconditions */
11737 assert(p_j2k != 00);
11738 assert(p_stream != 00);
11739 assert(p_manager != 00);
11741 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11742 if (! l_current_data) {
11745 l_max_tile_size = 1000;
11747 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11748 for (i=0;i<l_nb_tiles;++i) {
11749 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11750 opj_free(l_current_data);
11754 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11755 if (l_current_tile_size > l_max_tile_size) {
11756 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11757 if (! l_current_data) {
11760 l_max_tile_size = l_current_tile_size;
11763 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11765 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11770 opj_free(l_current_data);
11775 * Ends the compression procedures and possibility add data to be read after the
11778 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11779 opj_stream_private_t *p_stream,
11780 struct opj_event_mgr * p_manager)
11782 /* customization of the encoding */
11783 j2k_setup_end_compress(p_j2k);
11785 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11795 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11797 * @param p_j2k the jpeg2000 codec.
11798 * @param p_stream the stream object.
11799 * @param p_manager the user event manager.
11801 * @return true if the codec is valid.
11803 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11804 opj_stream_private_t *p_stream,
11805 opj_image_t * p_image,
11806 opj_event_mgr_t * p_manager)
11808 /* preconditions */
11809 assert(p_j2k != 00);
11810 assert(p_stream != 00);
11811 assert(p_manager != 00);
11813 p_j2k->m_private_image = opj_image_create0();
11814 opj_copy_image_header(p_image, p_j2k->m_private_image);
11816 // TODO_MSD: Find a better way
11817 if (p_image->comps) {
11818 OPJ_UINT32 it_comp;
11819 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11820 if (p_image->comps[it_comp].data) {
11821 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11822 p_image->comps[it_comp].data = NULL;
11828 /* customization of the validation */
11829 j2k_setup_encoding_validation (p_j2k);
11831 /* validation of the parameters codec */
11832 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11836 /* customization of the encoding */
11837 j2k_setup_header_writting(p_j2k);
11840 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11850 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11851 OPJ_UINT32 p_tile_index,
11852 opj_stream_private_t *p_stream,
11853 opj_event_mgr_t * p_manager )
11856 if (p_tile_index != p_j2k->m_current_tile_number) {
11857 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11861 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);
11863 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11864 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11865 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11867 /* initialisation before tile encoding */
11868 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11878 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11880 OPJ_UINT32 i,j,k = 0;
11881 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11882 opj_image_comp_t * l_img_comp = 00;
11883 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11884 opj_image_t * l_image = 00;
11885 OPJ_UINT32 l_size_comp, l_remaining;
11886 OPJ_INT32 * l_src_ptr;
11887 l_tilec = p_tcd->tcd_image->tiles->comps;
11888 l_image = p_tcd->image;
11889 l_img_comp = l_image->comps;
11891 for (i=0;i<p_tcd->image->numcomps;++i) {
11892 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11893 l_remaining = l_img_comp->prec & 7; /* (%8) */
11898 if (l_size_comp == 3) {
11902 l_width = (l_tilec->x1 - l_tilec->x0);
11903 l_height = (l_tilec->y1 - l_tilec->y0);
11904 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11905 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11906 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11907 l_stride = l_image_width - l_width;
11908 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11910 switch (l_size_comp) {
11913 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11914 if (l_img_comp->sgnd) {
11915 for (j=0;j<l_height;++j) {
11916 for (k=0;k<l_width;++k) {
11917 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11921 l_src_ptr += l_stride;
11925 for (j=0;j<l_height;++j) {
11926 for (k=0;k<l_width;++k) {
11927 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11931 l_src_ptr += l_stride;
11935 p_data = (OPJ_BYTE*) l_dest_ptr;
11940 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11941 if (l_img_comp->sgnd) {
11942 for (j=0;j<l_height;++j) {
11943 for (k=0;k<l_width;++k) {
11944 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11946 l_src_ptr += l_stride;
11950 for (j=0;j<l_height;++j) {
11951 for (k=0;k<l_width;++k) {
11952 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11954 l_src_ptr += l_stride;
11958 p_data = (OPJ_BYTE*) l_dest_ptr;
11963 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11964 for (j=0;j<l_height;++j) {
11965 for (k=0;k<l_width;++k) {
11966 *(l_dest_ptr++) = *(l_src_ptr++);
11968 l_src_ptr += l_stride;
11971 p_data = (OPJ_BYTE*) l_dest_ptr;
11984 * @param p_j2k the jpeg2000 codec.
11985 * @param p_stream the stream to write data to.
11986 * @param p_manager the user event manager.
11988 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11990 OPJ_UINT32 p_data_size,
11991 opj_stream_private_t *p_stream,
11992 opj_event_mgr_t * p_manager )
11994 opj_tcd_v2_t * l_tcd = 00;
11995 opj_cp_v2_t * l_cp = 00;
11996 opj_tcp_v2_t * l_tcp = 00;
11997 OPJ_UINT32 l_nb_bytes_written;
11998 OPJ_BYTE * l_current_data = 00;
11999 OPJ_UINT32 l_tile_size = 0;
12000 OPJ_UINT32 l_available_data;
12002 /* preconditions */
12003 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12005 l_tcd = p_j2k->m_tcd;
12006 l_cp = &(p_j2k->m_cp);
12007 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12009 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
12010 l_available_data = l_tile_size;
12011 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
12013 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
12014 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
12018 l_nb_bytes_written = 0;
12019 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12022 l_current_data += l_nb_bytes_written;
12023 l_available_data -= l_nb_bytes_written;
12025 l_nb_bytes_written = 0;
12026 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12030 l_available_data -= l_nb_bytes_written;
12031 l_nb_bytes_written = l_tile_size - l_available_data;
12033 if ( opj_stream_write_data( p_stream,
12034 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
12035 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
12039 ++p_j2k->m_current_tile_number;
12046 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12047 * are valid. Developers wanting to extend the library can add their own validation procedures.
12049 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
12051 /* preconditions */
12052 assert(p_j2k != 00);
12054 /* DEVELOPER CORNER, insert your custom procedures */
12055 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
12057 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12058 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
12061 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
12062 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
12063 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
12067 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12068 * are valid. Developers wanting to extend the library can add their own validation procedures.
12070 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
12072 /* preconditions */
12073 assert(p_j2k != 00);
12075 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
12076 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
12078 /* DEVELOPER CORNER, add your custom validation procedure */
12079 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
12084 * Sets up the procedures to do on writing header.
12085 * Developers wanting to extend the library can add their own writing procedures.
12087 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
12089 /* preconditions */
12090 assert(p_j2k != 00);
12092 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
12093 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
12094 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
12095 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
12096 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
12099 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12100 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
12101 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
12103 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
12104 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
12108 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
12110 if (p_j2k->m_cp.comment != 00) {
12111 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
12114 /* DEVELOPER CORNER, insert your custom procedures */
12115 if (p_j2k->m_cp.rsiz & MCT) {
12116 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
12118 /* End of Developer Corner */
12120 if (p_j2k->cstr_index) {
12121 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
12124 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
12125 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
12129 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
12131 OPJ_UINT32 * p_data_written,
12132 OPJ_UINT32 p_total_data_size,
12133 opj_stream_private_t *p_stream,
12134 struct opj_event_mgr * p_manager )
12137 OPJ_UINT32 l_nb_bytes_written = 0;
12138 OPJ_UINT32 l_current_nb_bytes_written;
12139 OPJ_BYTE * l_begin_data = 00;
12141 opj_tcp_v2_t *l_tcp = 00;
12142 opj_tcd_v2_t * l_tcd = 00;
12143 opj_cp_v2_t * l_cp = 00;
12145 l_tcd = p_j2k->m_tcd;
12146 l_cp = &(p_j2k->m_cp);
12147 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12149 l_tcd->cur_pino = 0;
12151 /*Get number of tile parts*/
12152 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12157 l_current_nb_bytes_written = 0;
12158 l_begin_data = p_data;
12159 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
12164 l_nb_bytes_written += l_current_nb_bytes_written;
12165 p_data += l_current_nb_bytes_written;
12166 p_total_data_size -= l_current_nb_bytes_written;
12168 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
12169 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
12170 l_current_nb_bytes_written = 0;
12171 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12172 l_nb_bytes_written += l_current_nb_bytes_written;
12173 p_data += l_current_nb_bytes_written;
12174 p_total_data_size -= l_current_nb_bytes_written;
12176 l_current_nb_bytes_written = 0;
12177 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12178 l_nb_bytes_written += l_current_nb_bytes_written;
12179 p_data += l_current_nb_bytes_written;
12180 p_total_data_size -= l_current_nb_bytes_written;
12183 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
12184 l_current_nb_bytes_written = 0;
12185 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
12186 l_nb_bytes_written += l_current_nb_bytes_written;
12187 p_data += l_current_nb_bytes_written;
12188 p_total_data_size -= l_current_nb_bytes_written;
12192 l_current_nb_bytes_written = 0;
12193 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12197 l_nb_bytes_written += l_current_nb_bytes_written;
12198 * p_data_written = l_nb_bytes_written;
12200 /* Writing Psot in SOT marker */
12201 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
12203 if (l_cp->m_specific_param.m_enc.m_cinema){
12204 j2k_update_tlm(p_j2k,l_nb_bytes_written);
12210 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
12212 OPJ_UINT32 * p_data_written,
12213 OPJ_UINT32 p_total_data_size,
12214 opj_stream_private_t *p_stream,
12215 struct opj_event_mgr * p_manager
12218 OPJ_UINT32 tilepartno=0;
12219 OPJ_UINT32 l_nb_bytes_written = 0;
12220 OPJ_UINT32 l_current_nb_bytes_written;
12221 OPJ_UINT32 l_part_tile_size;
12222 OPJ_UINT32 tot_num_tp;
12225 OPJ_BYTE * l_begin_data;
12226 opj_tcp_v2_t *l_tcp = 00;
12227 opj_tcd_v2_t * l_tcd = 00;
12228 opj_cp_v2_t * l_cp = 00;
12231 l_tcd = p_j2k->m_tcd;
12232 l_cp = &(p_j2k->m_cp);
12233 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12235 /*Get number of tile parts*/
12236 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
12238 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
12239 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12240 l_current_nb_bytes_written = 0;
12241 l_part_tile_size = 0;
12242 l_begin_data = p_data;
12244 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12248 l_nb_bytes_written += l_current_nb_bytes_written;
12249 p_data += l_current_nb_bytes_written;
12250 p_total_data_size -= l_current_nb_bytes_written;
12251 l_part_tile_size += l_nb_bytes_written;
12253 l_current_nb_bytes_written = 0;
12254 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12258 p_data += l_current_nb_bytes_written;
12259 l_nb_bytes_written += l_current_nb_bytes_written;
12260 p_total_data_size -= l_current_nb_bytes_written;
12261 l_part_tile_size += l_nb_bytes_written;
12263 /* Writing Psot in SOT marker */
12264 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12266 if (l_cp->m_specific_param.m_enc.m_cinema) {
12267 j2k_update_tlm(p_j2k,l_part_tile_size);
12270 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12273 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
12274 l_tcd->cur_pino = pino;
12276 /*Get number of tile parts*/
12277 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
12278 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
12279 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12280 l_current_nb_bytes_written = 0;
12281 l_part_tile_size = 0;
12282 l_begin_data = p_data;
12284 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12288 l_nb_bytes_written += l_current_nb_bytes_written;
12289 p_data += l_current_nb_bytes_written;
12290 p_total_data_size -= l_current_nb_bytes_written;
12291 l_part_tile_size += l_current_nb_bytes_written;
12293 l_current_nb_bytes_written = 0;
12295 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12299 l_nb_bytes_written += l_current_nb_bytes_written;
12300 p_data += l_current_nb_bytes_written;
12301 p_total_data_size -= l_current_nb_bytes_written;
12302 l_part_tile_size += l_current_nb_bytes_written;
12304 /* Writing Psot in SOT marker */
12305 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12307 if (l_cp->m_specific_param.m_enc.m_cinema) {
12308 j2k_update_tlm(p_j2k,l_part_tile_size);
12311 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12315 *p_data_written = l_nb_bytes_written;
12321 * Writes the updated tlm.
12323 * @param p_stream the stream to write data to.
12324 * @param p_j2k J2K codec.
12325 * @param p_manager the user event manager.
12327 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
12328 struct opj_stream_private *p_stream,
12329 struct opj_event_mgr * p_manager )
12331 OPJ_UINT32 l_tlm_size;
12332 OPJ_SIZE_T l_tlm_position, l_current_position;
12334 /* preconditions */
12335 assert(p_j2k != 00);
12336 assert(p_manager != 00);
12337 assert(p_stream != 00);
12339 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12340 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12341 l_current_position = opj_stream_tell(p_stream);
12343 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12347 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) {
12351 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12360 * Ends the encoding, i.e. frees memory.
12362 * @param p_stream the stream to write data to.
12363 * @param p_j2k J2K codec.
12364 * @param p_manager the user event manager.
12366 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
12367 struct opj_stream_private *p_stream,
12368 struct opj_event_mgr * p_manager )
12370 /* preconditions */
12371 assert(p_j2k != 00);
12372 assert(p_manager != 00);
12373 assert(p_stream != 00);
12375 tcd_destroy_v2(p_j2k->m_tcd);
12378 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12379 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12380 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12381 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12384 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12385 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12386 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12389 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12395 * Destroys the memory associated with the decoding of headers.
12397 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12398 opj_stream_private_t *p_stream,
12399 opj_event_mgr_t * p_manager )
12401 /* preconditions */
12402 assert(p_j2k != 00);
12403 assert(p_stream != 00);
12404 assert(p_manager != 00);
12406 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12407 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12408 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12411 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12420 * @param p_stream the stream to write data to.
12421 * @param p_j2k J2K codec.
12422 * @param p_manager the user event manager.
12424 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12425 struct opj_stream_private *p_stream,
12426 struct opj_event_mgr * p_manager )
12428 opj_codestream_info_t * l_cstr_info = 00;
12430 /* preconditions */
12431 assert(p_j2k != 00);
12432 assert(p_manager != 00);
12433 assert(p_stream != 00);
12436 /* TODO mergeV2: check this part which use cstr_info */
12437 /*l_cstr_info = p_j2k->cstr_info;
12441 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));
12443 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12444 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12446 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12448 l_cstr_info->tw = p_j2k->m_cp.tw;
12449 l_cstr_info->th = p_j2k->m_cp.th;
12451 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12452 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12453 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12454 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12456 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12458 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12460 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12462 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12463 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12466 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12468 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12470 /*l_cstr_info->maxmarknum = 100;
12471 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12472 l_cstr_info->marknum = 0;
12475 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);
12479 * Creates a tile-coder decoder.
12481 * @param p_stream the stream to write data to.
12482 * @param p_j2k J2K codec.
12483 * @param p_manager the user event manager.
12485 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12486 struct opj_stream_private *p_stream,
12487 struct opj_event_mgr * p_manager )
12489 /* preconditions */
12490 assert(p_j2k != 00);
12491 assert(p_manager != 00);
12492 assert(p_stream != 00);
12494 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12496 if (! p_j2k->m_tcd) {
12497 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12501 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12502 tcd_destroy_v2(p_j2k->m_tcd);
12513 * @param p_j2k the jpeg2000 codec.
12514 * @param p_stream the stream to write data to.
12515 * @param p_manager the user event manager.
12517 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12518 OPJ_UINT32 p_tile_index,
12520 OPJ_UINT32 p_data_size,
12521 opj_stream_private_t *p_stream,
12522 opj_event_mgr_t * p_manager )
12524 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12525 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12529 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12530 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);