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 j2k_read_sot_v2 (
947 OPJ_BYTE * p_header_data,
948 OPJ_UINT32 p_header_size,
949 struct opj_event_mgr * p_manager
952 Write the SOD marker (start of data)
953 @param j2k J2K handle
954 @param tile_coder Pointer to a TCD handle
956 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
959 * Writes the SOD marker (Start of data)
961 * @param p_stream the stream to write data to.
962 * @param p_j2k J2K codec.
963 * @param p_manager the user event manager.
965 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
966 struct opj_tcd_v2 * p_tile_coder,
968 OPJ_UINT32 * p_data_written,
969 OPJ_UINT32 p_total_data_size,
970 const struct opj_stream_private *p_stream,
971 struct opj_event_mgr * p_manager );
974 Read the SOD marker (start of data)
975 @param j2k J2K handle
977 static void j2k_read_sod(opj_j2k_t *j2k);
980 * Reads a SOD marker (Start Of Data)
982 * @param p_header_data the data contained in the SOD box.
983 * @param p_j2k the jpeg2000 codec.
984 * @param p_header_size the size of the data contained in the SOD marker.
985 * @param p_manager the user event manager.
987 static opj_bool j2k_read_sod_v2 (
989 struct opj_stream_private *p_stream,
990 struct opj_event_mgr * p_manager
994 * Updates the Tile Length Marker.
996 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
998 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
999 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
1001 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
1002 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
1006 Write the RGN marker (region-of-interest)
1007 @param j2k J2K handle
1008 @param compno Number of the component concerned by the information written
1009 @param tileno Number of the tile concerned by the information written
1011 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
1014 * Writes the RGN marker (Region Of Interest)
1016 * @param p_tile_no the tile to output
1017 * @param p_comp_no the component to output
1018 * @param p_stream the stream to write data to.
1019 * @param p_j2k J2K codec.
1020 * @param p_manager the user event manager.
1022 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
1023 OPJ_UINT32 p_tile_no,
1024 OPJ_UINT32 p_comp_no,
1025 struct opj_stream_private *p_stream,
1026 struct opj_event_mgr * p_manager );
1029 Read the RGN marker (region-of-interest)
1030 @param j2k J2K handle
1032 static void j2k_read_rgn(opj_j2k_t *j2k);
1035 * Reads a RGN marker (Region Of Interest)
1037 * @param p_header_data the data contained in the POC box.
1038 * @param p_j2k the jpeg2000 codec.
1039 * @param p_header_size the size of the data contained in the POC marker.
1040 * @param p_manager the user event manager.
1042 static opj_bool j2k_read_rgn_v2 (
1043 opj_j2k_v2_t *p_j2k,
1044 OPJ_BYTE * p_header_data,
1045 OPJ_UINT32 p_header_size,
1046 struct opj_event_mgr * p_manager
1050 * Writes the EOC marker (End of Codestream)
1052 * @param p_stream the stream to write data to.
1053 * @param p_j2k J2K codec.
1054 * @param p_manager the user event manager.
1056 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
1057 struct opj_stream_private *p_stream,
1058 struct opj_event_mgr * p_manager );
1061 Write the EOC marker (end of codestream)
1062 @param j2k J2K handle
1064 static void j2k_write_eoc(opj_j2k_t *j2k);
1066 Read the EOC marker (end of codestream)
1067 @param j2k J2K handle
1069 static void j2k_read_eoc(opj_j2k_t *j2k);
1072 * Reads a EOC marker (End Of Codestream)
1074 * @param p_header_data the data contained in the SOD box.
1075 * @param p_j2k the jpeg2000 codec.
1076 * @param p_header_size the size of the data contained in the SOD marker.
1077 * @param p_manager the user event manager.
1080 static opj_bool j2k_read_eoc_v2 (
1081 opj_j2k_v2_t *p_j2k,
1082 struct opj_stream_private *p_stream,
1083 struct opj_event_mgr * p_manager
1090 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1092 * @param p_stream the stream to write data to.
1093 * @param p_j2k J2K codec.
1094 * @param p_manager the user event manager.
1096 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1097 struct opj_stream_private *p_stream,
1098 struct opj_event_mgr * p_manager );
1103 * @param p_stream the stream to write data to.
1104 * @param p_j2k J2K codec.
1105 * @param p_manager the user event manager.
1107 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1108 struct opj_stream_private *p_stream,
1109 struct opj_event_mgr * p_manager );
1112 Read an unknown marker
1113 @param j2k J2K handle
1115 static void j2k_read_unk(opj_j2k_t *j2k);
1117 Add main header marker information
1118 @param cstr_info Codestream information structure
1119 @param type marker type
1120 @param pos byte offset of marker segment
1121 @param len length of marker segment
1123 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1125 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1127 Add tile header marker information
1128 @param tileno tile index number
1129 @param cstr_info Codestream information structure
1130 @param type marker type
1131 @param pos byte offset of marker segment
1132 @param len length of marker segment
1134 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1136 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);
1139 * Reads an unknown marker
1141 * @param p_stream the stream object to read from.
1142 * @param p_j2k the jpeg2000 codec.
1143 * @param p_manager the user event manager.
1145 * @return true if the marker could be deduced.
1147 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1148 struct opj_stream_private *p_stream,
1149 OPJ_UINT32 *output_marker,
1150 struct opj_event_mgr * p_manager );
1153 * Writes the MCT marker (Multiple Component Transform)
1155 * @param p_stream the stream to write data to.
1156 * @param p_j2k J2K codec.
1157 * @param p_manager the user event manager.
1159 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1160 opj_mct_data_t * p_mct_record,
1161 struct opj_stream_private *p_stream,
1162 struct opj_event_mgr * p_manager );
1165 * Reads a MCT marker (Multiple Component Transform)
1167 * @param p_header_data the data contained in the MCT box.
1168 * @param p_j2k the jpeg2000 codec.
1169 * @param p_header_size the size of the data contained in the MCT marker.
1170 * @param p_manager the user event manager.
1172 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1173 OPJ_BYTE * p_header_data,
1174 OPJ_UINT32 p_header_size,
1175 struct opj_event_mgr * p_manager );
1178 * Writes the MCC marker (Multiple Component Collection)
1180 * @param p_stream the stream to write data to.
1181 * @param p_j2k J2K codec.
1182 * @param p_manager the user event manager.
1184 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1185 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1186 struct opj_stream_private *p_stream,
1187 struct opj_event_mgr * p_manager );
1191 * Reads a MCC marker (Multiple Component Collection)
1193 * @param p_header_data the data contained in the MCC box.
1194 * @param p_j2k the jpeg2000 codec.
1195 * @param p_header_size the size of the data contained in the MCC marker.
1196 * @param p_manager the user event manager.
1198 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1199 OPJ_BYTE * p_header_data,
1200 OPJ_UINT32 p_header_size,
1201 struct opj_event_mgr * p_manager );
1204 * Writes the MCO marker (Multiple component transformation ordering)
1206 * @param p_stream the stream to write data to.
1207 * @param p_j2k J2K codec.
1208 * @param p_manager the user event manager.
1210 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1211 struct opj_stream_private *p_stream,
1212 struct opj_event_mgr * p_manager );
1215 * Reads a MCO marker (Multiple Component Transform Ordering)
1217 * @param p_header_data the data contained in the MCO box.
1218 * @param p_j2k the jpeg2000 codec.
1219 * @param p_header_size the size of the data contained in the MCO marker.
1220 * @param p_manager the user event manager.
1222 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1223 OPJ_BYTE * p_header_data,
1224 OPJ_UINT32 p_header_size,
1225 struct opj_event_mgr * p_manager );
1227 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1229 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1230 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1231 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1232 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1234 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1235 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1236 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1237 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1239 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1240 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1241 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1242 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1246 * Ends the encoding, i.e. frees memory.
1248 * @param p_stream the stream to write data to.
1249 * @param p_j2k J2K codec.
1250 * @param p_manager the user event manager.
1252 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1253 struct opj_stream_private *p_stream,
1254 struct opj_event_mgr * p_manager );
1257 * Writes the CBD marker (Component bit depth definition)
1259 * @param p_stream the stream to write data to.
1260 * @param p_j2k J2K codec.
1261 * @param p_manager the user event manager.
1263 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1264 struct opj_stream_private *p_stream,
1265 struct opj_event_mgr * p_manager );
1268 * Reads a CBD marker (Component bit depth definition)
1269 * @param p_header_data the data contained in the CBD box.
1270 * @param p_j2k the jpeg2000 codec.
1271 * @param p_header_size the size of the data contained in the CBD marker.
1272 * @param p_manager the user event manager.
1274 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1275 OPJ_BYTE * p_header_data,
1276 OPJ_UINT32 p_header_size,
1277 struct opj_event_mgr * p_manager);
1280 * Writes the image components.
1282 * @param p_stream the stream to write data to.
1283 * @param p_j2k J2K codec.
1284 * @param p_manager the user event manager.
1286 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1287 struct opj_stream_private *p_stream,
1288 struct opj_event_mgr * p_manager );
1291 * Writes regions of interests.
1293 * @param p_stream the stream to write data to.
1294 * @param p_j2k J2K codec.
1295 * @param p_manager the user event manager.
1297 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1298 struct opj_stream_private *p_stream,
1299 struct opj_event_mgr * p_manager );
1304 * @param p_stream the stream to write data to.
1305 * @param p_j2k J2K codec.
1306 * @param p_manager the user event manager.
1308 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1309 struct opj_stream_private *p_stream,
1310 struct opj_event_mgr * p_manager );
1313 * Checks the progression order changes values. Tells of the poc given as input are valid.
1314 * A nice message is outputted at errors.
1316 * @param p_pocs the progression order changes.
1317 * @param p_nb_pocs the number of progression order changes.
1318 * @param p_nb_resolutions the number of resolutions.
1319 * @param numcomps the number of components
1320 * @param numlayers the number of layers.
1322 * @return true if the pocs are valid.
1324 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1325 OPJ_UINT32 p_nb_pocs,
1326 OPJ_UINT32 p_nb_resolutions,
1327 OPJ_UINT32 numcomps,
1328 OPJ_UINT32 numlayers,
1329 opj_event_mgr_t * p_manager);
1332 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1334 * @param cp the coding parameters.
1335 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1336 * @param tileno the given tile.
1338 * @return the number of tile parts.
1340 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1342 /** mem allocation for TLM marker*/
1343 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1346 * Calculates the total number of tile parts needed by the encoder to
1347 * encode such an image. If not enough memory is available, then the function return false.
1349 * @param p_nb_tiles pointer that will hold the number of tile parts.
1350 * @param cp the coding parameters for the image.
1351 * @param image the image to encode.
1352 * @param p_j2k the p_j2k encoder.
1353 * @param p_manager the user event manager.
1355 * @return true if the function was successful, false else.
1357 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1359 OPJ_UINT32 * p_nb_tiles,
1361 opj_event_mgr_t * p_manager);
1363 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1365 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1367 static opj_codestream_index_t* j2k_create_cstr_index(void);
1373 /* ----------------------------------------------------------------------- */
1374 typedef struct j2k_prog_order{
1375 OPJ_PROG_ORDER enum_prog;
1379 j2k_prog_order_t j2k_prog_order_list[] = {
1385 {(OPJ_PROG_ORDER)-1, ""}
1393 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1401 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1403 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1405 j2k_read_int16_to_float,
1406 j2k_read_int32_to_float,
1407 j2k_read_float32_to_float,
1408 j2k_read_float64_to_float
1411 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1413 j2k_read_int16_to_int32,
1414 j2k_read_int32_to_int32,
1415 j2k_read_float32_to_int32,
1416 j2k_read_float64_to_int32
1419 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1421 j2k_write_float_to_int16,
1422 j2k_write_float_to_int32,
1423 j2k_write_float_to_float,
1424 j2k_write_float_to_float64
1427 typedef struct opj_dec_memory_marker_handler
1431 /** value of the state when the marker can appear */
1433 /** action linked to the marker */
1434 opj_bool (*handler) (
1435 opj_j2k_v2_t *p_j2k,
1436 OPJ_BYTE * p_header_data,
1437 OPJ_UINT32 p_header_size,
1438 struct opj_event_mgr * p_manager
1441 opj_dec_memory_marker_handler_t;
1443 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1446 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1447 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1448 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1449 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1450 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1451 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1452 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1453 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1454 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1455 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1456 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1457 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1458 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1460 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1461 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1462 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1463 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1464 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1465 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1467 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
1468 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1469 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1470 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1471 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1472 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1473 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1474 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
1475 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1476 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1477 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1478 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1479 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1481 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1482 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1483 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1484 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1485 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1486 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1488 #ifdef TODO_MS /* FIXME */
1489 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1490 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1491 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1492 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1494 #endif /* USE_JPWL */
1496 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1497 {J2K_MS_INSEC, 0, j2k_read_insec}
1498 #endif /* USE_JPSEC */
1499 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1504 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1506 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1507 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1511 for (i=0;i<p_nb_elem;++i) {
1512 opj_read_bytes(l_src_data,&l_temp,2);
1514 l_src_data+=sizeof(OPJ_INT16);
1516 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1520 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1522 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1523 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1527 for (i=0;i<p_nb_elem;++i) {
1528 opj_read_bytes(l_src_data,&l_temp,4);
1530 l_src_data+=sizeof(OPJ_INT32);
1532 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1536 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1538 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1539 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1543 for (i=0;i<p_nb_elem;++i) {
1544 opj_read_float(l_src_data,&l_temp);
1546 l_src_data+=sizeof(OPJ_FLOAT32);
1548 *(l_dest_data++) = l_temp;
1552 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1554 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1555 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1559 for (i=0;i<p_nb_elem;++i) {
1560 opj_read_double(l_src_data,&l_temp);
1562 l_src_data+=sizeof(OPJ_FLOAT64);
1564 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1568 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1570 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1571 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1575 for (i=0;i<p_nb_elem;++i) {
1576 opj_read_bytes(l_src_data,&l_temp,2);
1578 l_src_data+=sizeof(OPJ_INT16);
1580 *(l_dest_data++) = (OPJ_INT32) l_temp;
1584 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1586 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1587 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1591 for (i=0;i<p_nb_elem;++i) {
1592 opj_read_bytes(l_src_data,&l_temp,4);
1594 l_src_data+=sizeof(OPJ_INT32);
1596 *(l_dest_data++) = (OPJ_INT32) l_temp;
1600 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1602 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1603 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1607 for (i=0;i<p_nb_elem;++i) {
1608 opj_read_float(l_src_data,&l_temp);
1610 l_src_data+=sizeof(OPJ_FLOAT32);
1612 *(l_dest_data++) = (OPJ_INT32) l_temp;
1616 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1618 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1619 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1623 for (i=0;i<p_nb_elem;++i) {
1624 opj_read_double(l_src_data,&l_temp);
1626 l_src_data+=sizeof(OPJ_FLOAT64);
1628 *(l_dest_data++) = (OPJ_INT32) l_temp;
1632 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1634 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1635 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1639 for (i=0;i<p_nb_elem;++i) {
1640 l_temp = (OPJ_UINT32) *(l_src_data++);
1642 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1644 l_dest_data+=sizeof(OPJ_INT16);
1648 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1650 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1651 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1655 for (i=0;i<p_nb_elem;++i) {
1656 l_temp = (OPJ_UINT32) *(l_src_data++);
1658 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1660 l_dest_data+=sizeof(OPJ_INT32);
1664 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1666 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1667 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1671 for (i=0;i<p_nb_elem;++i) {
1672 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1674 opj_write_float(l_dest_data,l_temp);
1676 l_dest_data+=sizeof(OPJ_FLOAT32);
1680 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1682 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1683 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1687 for (i=0;i<p_nb_elem;++i) {
1688 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1690 opj_write_double(l_dest_data,l_temp);
1692 l_dest_data+=sizeof(OPJ_FLOAT64);
1698 * Converts an enum type progression order to string type.
1700 * @param prg_order the progression order to get.
1702 * @return the string representation of the given progression order.
1704 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1705 j2k_prog_order_t *po;
1706 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1707 if(po->enum_prog == prg_order){
1708 return po->str_prog;
1711 return po->str_prog;
1715 * Checks the progression order changes values. Tells if the poc given as input are valid.
1717 * @param p_pocs the progression order changes.
1718 * @param p_nb_pocs the number of progression order changes.
1719 * @param p_nb_resolutions the number of resolutions.
1720 * @param numcomps the number of components
1721 * @param numlayers the number of layers.
1722 * @param p_manager the user event manager.
1724 * @return true if the pocs are valid.
1726 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1727 OPJ_UINT32 p_nb_pocs,
1728 OPJ_UINT32 p_nb_resolutions,
1729 OPJ_UINT32 p_num_comps,
1730 OPJ_UINT32 p_num_layers,
1731 opj_event_mgr_t * p_manager)
1733 OPJ_UINT32* packet_array;
1734 OPJ_UINT32 index , resno, compno, layno;
1736 OPJ_UINT32 step_c = 1;
1737 OPJ_UINT32 step_r = p_num_comps * step_c;
1738 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1739 opj_bool loss = OPJ_FALSE;
1740 OPJ_UINT32 layno0 = 0;
1742 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1743 if (packet_array == 00) {
1744 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1747 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1749 if (p_nb_pocs == 0) {
1753 index = step_r * p_pocs->resno0;
1754 // take each resolution for each poc
1755 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1757 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1759 // take each comp of each resolution for each poc
1760 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1761 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1763 // and finally take each layer of each res of ...
1764 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1765 //index = step_r * resno + step_c * compno + step_l * layno;
1766 packet_array[comp_index] = 1;
1767 comp_index += step_l;
1770 res_index += step_c;
1777 // iterate through all the pocs
1778 for (i = 1; i < p_nb_pocs ; ++i) {
1779 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1781 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1782 index = step_r * p_pocs->resno0;
1784 // take each resolution for each poc
1785 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1786 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1788 // take each comp of each resolution for each poc
1789 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1790 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1792 // and finally take each layer of each res of ...
1793 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1794 //index = step_r * resno + step_c * compno + step_l * layno;
1795 packet_array[comp_index] = 1;
1796 comp_index += step_l;
1799 res_index += step_c;
1809 for (layno = 0; layno < p_num_layers ; ++layno) {
1810 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1811 for (compno = 0; compno < p_num_comps; ++compno) {
1812 loss |= (packet_array[index]!=1);
1813 //index = step_r * resno + step_c * compno + step_l * layno;
1820 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1823 opj_free(packet_array);
1828 /* ----------------------------------------------------------------------- */
1829 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1832 int tpnum=1,tpend=0;
1833 opj_tcp_t *tcp = &cp->tcps[tileno];
1834 prog = j2k_convert_progression_order(tcp->prg);
1839 if( cp->tp_flag == prog[i] ){
1840 tpend=1;cp->tp_pos=i;
1844 tpnum= tpnum * tcp->pocs[pino].compE;
1847 tpnum= tpnum * tcp->pocs[pino].resE;
1850 tpnum= tpnum * tcp->pocs[pino].prcE;
1853 tpnum= tpnum * tcp->pocs[pino].layE;
1865 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1867 * @param cp the coding parameters.
1868 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1869 * @param tileno the given tile.
1871 * @return the number of tile parts.
1873 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1875 const OPJ_CHAR *prog = 00;
1877 OPJ_UINT32 tpnum = 1;
1878 opj_tcp_v2_t *tcp = 00;
1879 opj_poc_t * l_current_poc = 00;
1882 assert(tileno < (cp->tw * cp->th));
1883 assert(pino < (cp->tcps[tileno].numpocs + 1));
1885 /* get the given tile coding parameter */
1886 tcp = &cp->tcps[tileno];
1889 l_current_poc = &(tcp->pocs[pino]);
1890 assert(l_current_poc != 0);
1892 /* get the progression order as a character string */
1893 prog = j2k_convert_progression_order(tcp->prg);
1894 assert(strlen(prog) > 0);
1896 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1900 /* component wise */
1902 tpnum *= l_current_poc->compE;
1904 /* resolution wise */
1906 tpnum *= l_current_poc->resE;
1910 tpnum *= l_current_poc->prcE;
1914 tpnum *= l_current_poc->layE;
1917 /* whould we split here ? */
1918 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1919 cp->m_specific_param.m_enc.m_tp_pos=i;
1931 /** mem allocation for TLM marker*/
1932 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1933 int pino,tileno,totnum_tp=0;
1935 OPJ_ARG_NOT_USED(img_numcomp);
1937 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1938 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1939 int cur_totnum_tp = 0;
1940 opj_tcp_t *tcp = &cp->tcps[tileno];
1941 for(pino = 0; pino <= tcp->numpocs; pino++) {
1943 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1944 if(!pi) { return -1;}
1945 tp_num = j2k_get_num_tp(cp,pino,tileno);
1946 totnum_tp = totnum_tp + tp_num;
1947 cur_totnum_tp = cur_totnum_tp + tp_num;
1948 pi_destroy(pi, cp, tileno);
1950 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1952 if (j2k->cstr_info) {
1953 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1954 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1962 * Calculates the total number of tile parts needed by the encoder to
1963 * encode such an image. If not enough memory is available, then the function return false.
1965 * @param p_nb_tiles pointer that will hold the number of tile parts.
1966 * @param cp the coding parameters for the image.
1967 * @param image the image to encode.
1968 * @param p_j2k the p_j2k encoder.
1969 * @param p_manager the user event manager.
1971 * @return true if the function was successful, false else.
1973 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1975 OPJ_UINT32 * p_nb_tiles,
1977 opj_event_mgr_t * p_manager)
1979 OPJ_UINT32 pino,tileno;
1980 OPJ_UINT32 l_nb_tiles;
1984 assert(p_nb_tiles != 00);
1986 assert(image != 00);
1987 assert(p_j2k != 00);
1988 assert(p_manager != 00);
1990 l_nb_tiles = cp->tw * cp->th;
1995 /* TODO mergeV2: check this part which use cstr_info */
1996 /*if (p_j2k->cstr_info) {
1997 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1999 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2000 OPJ_UINT32 cur_totnum_tp = 0;
2002 pi_update_encoding_parameters(image,cp,tileno);
2004 for (pino = 0; pino <= tcp->numpocs; ++pino)
2006 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2008 *p_nb_tiles = *p_nb_tiles + tp_num;
2010 cur_totnum_tp += tp_num;
2013 tcp->m_nb_tile_parts = cur_totnum_tp;
2015 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
2016 if (l_info_tile_ptr->tp == 00) {
2020 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
2022 l_info_tile_ptr->num_tps = cur_totnum_tp;
2029 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2030 OPJ_UINT32 cur_totnum_tp = 0;
2032 pi_update_encoding_parameters(image,cp,tileno);
2034 for (pino = 0; pino <= tcp->numpocs; ++pino) {
2035 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2037 *p_nb_tiles = *p_nb_tiles + tp_num;
2039 cur_totnum_tp += tp_num;
2041 tcp->m_nb_tile_parts = cur_totnum_tp;
2050 static void j2k_write_soc(opj_j2k_t *j2k) {
2051 opj_cio_t *cio = j2k->cio;
2052 cio_write(cio, J2K_MS_SOC, 2);
2055 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2060 /* update markers struct */
2061 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2062 #endif /* USE_JPWL */
2067 * Writes the SOC marker (Start Of Codestream)
2069 * @param p_stream the stream to write data to.
2070 * @param p_j2k J2K codec.
2071 * @param p_manager the user event manager.
2073 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2074 struct opj_stream_private *p_stream,
2075 struct opj_event_mgr * p_manager )
2077 /* 2 bytes will be written */
2078 OPJ_BYTE * l_start_stream = 00;
2081 assert(p_stream != 00);
2082 assert(p_j2k != 00);
2083 assert(p_manager != 00);
2085 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2087 /* write SOC identifier */
2088 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2090 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2096 /* update markers struct */
2098 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2100 assert( 0 && "TODO" );
2101 #endif /* USE_JPWL */
2110 * Reads a SOC marker (Start of Codestream)
2111 * @param p_header_data the data contained in the SOC box.
2112 * @param jp2 the jpeg2000 file codec.
2113 * @param p_header_size the size of the data contained in the SOC marker.
2114 * @param p_manager the user event manager.
2116 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2117 struct opj_stream_private *p_stream,
2118 struct opj_event_mgr * p_manager )
2120 OPJ_BYTE l_data [2];
2121 OPJ_UINT32 l_marker;
2124 assert(p_j2k != 00);
2125 assert(p_manager != 00);
2126 assert(p_stream != 00);
2128 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2132 opj_read_bytes(l_data,&l_marker,2);
2133 if (l_marker != J2K_MS_SOC) {
2137 /* Next marker should be a SIZ marker in the main header */
2138 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2140 /* FIXME move it in a index structure included in p_j2k*/
2141 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2143 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2145 /* Add the marker to the codestream index*/
2146 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2151 static void j2k_write_siz(opj_j2k_t *j2k) {
2155 opj_cio_t *cio = j2k->cio;
2156 opj_image_t *image = j2k->image;
2157 opj_cp_t *cp = j2k->cp;
2159 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2160 lenp = cio_tell(cio);
2162 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2163 cio_write(cio, image->x1, 4); /* Xsiz */
2164 cio_write(cio, image->y1, 4); /* Ysiz */
2165 cio_write(cio, image->x0, 4); /* X0siz */
2166 cio_write(cio, image->y0, 4); /* Y0siz */
2167 cio_write(cio, cp->tdx, 4); /* XTsiz */
2168 cio_write(cio, cp->tdy, 4); /* YTsiz */
2169 cio_write(cio, cp->tx0, 4); /* XT0siz */
2170 cio_write(cio, cp->ty0, 4); /* YT0siz */
2171 cio_write(cio, image->numcomps, 2); /* Csiz */
2172 for (i = 0; i < image->numcomps; i++) {
2173 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2174 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2175 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2177 len = cio_tell(cio) - lenp;
2178 cio_seek(cio, lenp);
2179 cio_write(cio, len, 2); /* Lsiz */
2180 cio_seek(cio, lenp + len);
2183 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2187 * Writes the SIZ marker (image and tile size)
2189 * @param p_stream the stream to write data to.
2190 * @param p_j2k J2K codec.
2191 * @param p_manager the user event manager.
2193 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2194 struct opj_stream_private *p_stream,
2195 struct opj_event_mgr * p_manager )
2198 OPJ_UINT32 l_size_len;
2199 OPJ_BYTE * l_current_ptr;
2200 opj_image_t * l_image = 00;
2201 opj_cp_v2_t *cp = 00;
2202 opj_image_comp_t * l_img_comp = 00;
2205 assert(p_stream != 00);
2206 assert(p_j2k != 00);
2207 assert(p_manager != 00);
2209 l_image = p_j2k->m_private_image;
2210 cp = &(p_j2k->m_cp);
2211 l_size_len = 40 + 3 * l_image->numcomps;
2212 l_img_comp = l_image->comps;
2214 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2216 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2217 = (OPJ_BYTE*)opj_realloc(
2218 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2220 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2224 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2227 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2229 /* write SOC identifier */
2230 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2233 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2236 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2239 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2242 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2245 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2248 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2251 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2254 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2257 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2260 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2263 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2266 for (i = 0; i < l_image->numcomps; ++i) {
2267 /* TODO here with MCT ? */
2268 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2271 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2274 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2280 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) {
2287 static void j2k_read_siz(opj_j2k_t *j2k) {
2291 opj_cio_t *cio = j2k->cio;
2292 opj_image_t *image = j2k->image;
2293 opj_cp_t *cp = j2k->cp;
2295 len = cio_read(cio, 2); /* Lsiz */
2296 cio_read(cio, 2); /* Rsiz (capabilities) */
2297 image->x1 = cio_read(cio, 4); /* Xsiz */
2298 image->y1 = cio_read(cio, 4); /* Ysiz */
2299 image->x0 = cio_read(cio, 4); /* X0siz */
2300 image->y0 = cio_read(cio, 4); /* Y0siz */
2301 cp->tdx = cio_read(cio, 4); /* XTsiz */
2302 cp->tdy = cio_read(cio, 4); /* YTsiz */
2303 cp->tx0 = cio_read(cio, 4); /* XT0siz */
2304 cp->ty0 = cio_read(cio, 4); /* YT0siz */
2306 /* the following code triggers: */
2307 /* warning: comparison of unsigned expression < 0 is always false */
2309 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
2310 opj_event_msg(j2k->cinfo, EVT_ERROR,
2311 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
2312 image->x0,image->x1,image->y0,image->y1);
2317 image->numcomps = cio_read(cio, 2); /* Csiz */
2320 if (j2k->cp->correct) {
2321 /* if JPWL is on, we check whether TX errors have damaged
2322 too much the SIZ parameters */
2323 if (!(image->x1 * image->y1)) {
2324 opj_event_msg(j2k->cinfo, EVT_ERROR,
2325 "JPWL: bad image size (%d x %d)\n",
2326 image->x1, image->y1);
2327 if (!JPWL_ASSUME || JPWL_ASSUME) {
2328 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2332 assert( len >= 38 );
2333 if (image->numcomps != (OPJ_UINT32)((len - 38) / 3)) {
2334 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2335 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2336 image->numcomps, ((len - 38) / 3));
2338 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2341 /* we try to correct */
2342 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
2343 if (image->numcomps < (OPJ_UINT32)((len - 38) / 3)) {
2344 len = 38 + 3 * image->numcomps;
2345 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2348 image->numcomps = ((len - 38) / 3);
2349 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2354 /* update components number in the jpwl_exp_comps filed */
2355 cp->exp_comps = image->numcomps;
2357 #endif /* USE_JPWL */
2359 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
2360 for (i = 0; i < image->numcomps; i++) {
2362 tmp = cio_read(cio, 1); /* Ssiz_i */
2363 image->comps[i].prec = (tmp & 0x7f) + 1;
2364 image->comps[i].sgnd = tmp >> 7;
2365 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
2366 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
2369 if (j2k->cp->correct) {
2370 /* if JPWL is on, we check whether TX errors have damaged
2371 too much the SIZ parameters, again */
2372 if (!(image->comps[i].dx * image->comps[i].dy)) {
2373 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2374 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2375 i, i, image->comps[i].dx, image->comps[i].dy);
2377 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2380 /* we try to correct */
2381 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2382 if (!image->comps[i].dx) {
2383 image->comps[i].dx = 1;
2384 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2385 i, image->comps[i].dx);
2387 if (!image->comps[i].dy) {
2388 image->comps[i].dy = 1;
2389 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2390 i, image->comps[i].dy);
2395 #endif /* USE_JPWL */
2397 /* TODO: unused ? */
2398 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
2399 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
2401 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
2402 image->comps[i].factor = cp->reduce; /* reducing factor per component */
2405 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2406 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2409 if (j2k->cp->correct) {
2410 /* if JPWL is on, we check whether TX errors have damaged
2411 too much the SIZ parameters */
2412 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
2413 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2414 "JPWL: bad number of tiles (%d x %d)\n",
2417 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2420 /* we try to correct */
2421 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2424 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2427 if (cp->tw > cp->max_tiles) {
2429 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
2430 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2431 cp->max_tiles, cp->tw);
2435 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2438 if (cp->th > cp->max_tiles) {
2440 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2441 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2442 cp->max_tiles, cp->th);
2446 #endif /* USE_JPWL */
2448 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2449 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
2450 cp->tileno_size = 0;
2453 if (j2k->cp->correct) {
2455 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2456 "JPWL: could not alloc tcps field of cp\n");
2457 if (!JPWL_ASSUME || JPWL_ASSUME) {
2458 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2463 #endif /* USE_JPWL */
2465 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2466 cp->tcps[i].POC = 0;
2467 cp->tcps[i].numpocs = 0;
2468 cp->tcps[i].first = 1;
2471 /* Initialization for PPM marker */
2473 cp->ppm_data = NULL;
2474 cp->ppm_data_first = NULL;
2475 cp->ppm_previous = 0;
2478 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2479 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2480 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2482 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
2483 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
2484 j2k->state = J2K_STATE_MH;
2487 if (j2k->cstr_info) {
2488 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2489 cstr_info->image_w = image->x1 - image->x0;
2490 cstr_info->image_h = image->y1 - image->y0;
2491 cstr_info->numcomps = image->numcomps;
2492 cstr_info->tw = cp->tw;
2493 cstr_info->th = cp->th;
2494 cstr_info->tile_x = cp->tdx;
2495 cstr_info->tile_y = cp->tdy;
2496 cstr_info->tile_Ox = cp->tx0;
2497 cstr_info->tile_Oy = cp->ty0;
2498 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
2504 * Reads a SIZ marker (image and tile size)
2505 * @param p_header_data the data contained in the SIZ box.
2506 * @param jp2 the jpeg2000 file codec.
2507 * @param p_header_size the size of the data contained in the SIZ marker.
2508 * @param p_manager the user event manager.
2510 opj_bool j2k_read_siz_v2 (
2511 opj_j2k_v2_t *p_j2k,
2512 OPJ_BYTE * p_header_data,
2513 OPJ_UINT32 p_header_size,
2514 struct opj_event_mgr * p_manager
2517 OPJ_UINT32 l_size, i;
2518 OPJ_UINT32 l_nb_comp;
2519 OPJ_UINT32 l_nb_comp_remain;
2520 OPJ_UINT32 l_remaining_size;
2521 OPJ_UINT32 l_nb_tiles;
2523 opj_image_t *l_image = 00;
2524 opj_cp_v2_t *l_cp = 00;
2525 opj_image_comp_t * l_img_comp = 00;
2526 opj_tcp_v2_t * l_current_tile_param = 00;
2529 assert(p_j2k != 00);
2530 assert(p_manager != 00);
2531 assert(p_header_data != 00);
2533 l_image = p_j2k->m_private_image;
2534 l_cp = &(p_j2k->m_cp);
2536 /* minimum size == 39 - 3 (= minimum component parameter) */
2537 if (p_header_size < 36) {
2538 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2542 l_remaining_size = p_header_size - 36;
2543 l_nb_comp = l_remaining_size / 3;
2544 l_nb_comp_remain = l_remaining_size % 3;
2545 if (l_nb_comp_remain != 0){
2546 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2550 l_size = p_header_size + 2; /* Lsiz */
2552 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2554 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2555 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2557 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2559 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2561 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2563 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2565 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2567 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2569 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2571 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2574 l_image->numcomps = (OPJ_UINT16) l_tmp;
2576 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2580 if (l_image->numcomps != l_nb_comp) {
2581 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);
2586 if (l_cp->correct) {
2587 /* if JPWL is on, we check whether TX errors have damaged
2588 too much the SIZ parameters */
2589 if (!(l_image->x1 * l_image->y1)) {
2590 opj_event_msg_v2(p_manager, EVT_ERROR,
2591 "JPWL: bad image size (%d x %d)\n",
2592 l_image->x1, l_image->y1);
2593 if (!JPWL_ASSUME || JPWL_ASSUME) {
2594 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2599 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2600 if (l_image->numcomps != ((len - 38) / 3)) {
2601 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2602 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2603 l_image->numcomps, ((len - 38) / 3));
2605 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2608 */ /* we try to correct */
2609 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2610 if (l_image->numcomps < ((len - 38) / 3)) {
2611 len = 38 + 3 * l_image->numcomps;
2612 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2615 l_image->numcomps = ((len - 38) / 3);
2616 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2622 /* update components number in the jpwl_exp_comps filed */
2623 l_cp->exp_comps = l_image->numcomps;
2625 #endif /* USE_JPWL */
2627 /* Allocate the resulting image components */
2628 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2629 if (l_image->comps == 00){
2630 l_image->numcomps = 0;
2631 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2635 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2636 l_img_comp = l_image->comps;
2638 /* Read the component information */
2639 for (i = 0; i < l_image->numcomps; ++i){
2641 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2643 l_img_comp->prec = (tmp & 0x7f) + 1;
2644 l_img_comp->sgnd = tmp >> 7;
2645 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2647 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2648 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2650 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2653 if (l_cp->correct) {
2654 /* if JPWL is on, we check whether TX errors have damaged
2655 too much the SIZ parameters, again */
2656 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2657 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2658 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2659 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2661 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2664 /* we try to correct */
2665 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2666 if (!l_image->comps[i].dx) {
2667 l_image->comps[i].dx = 1;
2668 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2669 i, l_image->comps[i].dx);
2671 if (!l_image->comps[i].dy) {
2672 l_image->comps[i].dy = 1;
2673 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2674 i, l_image->comps[i].dy);
2678 #endif /* USE_JPWL */
2679 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2680 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2684 /* Compute the number of tiles */
2685 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2686 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2687 l_nb_tiles = l_cp->tw * l_cp->th;
2689 /* Define the tiles which will be decoded */
2690 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2691 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;
2692 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;
2693 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);
2694 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);
2697 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2698 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2699 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2700 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2704 if (l_cp->correct) {
2705 /* if JPWL is on, we check whether TX errors have damaged
2706 too much the SIZ parameters */
2707 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2708 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2709 "JPWL: bad number of tiles (%d x %d)\n",
2710 l_cp->tw, l_cp->th);
2712 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2715 /* we try to correct */
2716 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2719 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2722 if (l_cp->tw > l_cp->max_tiles) {
2724 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2725 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2726 l_cp->max_tiles, l_cp->tw);
2730 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2733 if (l_cp->th > l_cp->max_tiles) {
2735 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2736 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2737 l_cp->max_tiles, l_cp->th);
2741 #endif /* USE_JPWL */
2743 /* memory allocations */
2744 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2745 if (l_cp->tcps == 00) {
2746 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2749 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2752 if (l_cp->correct) {
2754 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2755 "JPWL: could not alloc tcps field of cp\n");
2756 if (!JPWL_ASSUME || JPWL_ASSUME) {
2757 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2762 #endif /* USE_JPWL */
2764 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2765 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2766 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2767 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2770 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2772 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2773 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2775 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2776 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2779 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));
2780 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2782 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2783 (opj_simple_mcc_decorrelation_data_t*)
2784 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2786 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2787 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2790 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));
2791 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2793 /* set up default dc level shift */
2794 for (i=0;i<l_image->numcomps;++i) {
2795 if (! l_image->comps[i].sgnd) {
2796 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2800 l_current_tile_param = l_cp->tcps;
2801 for (i = 0; i < l_nb_tiles; ++i) {
2802 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2803 if (l_current_tile_param->tccps == 00) {
2804 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2807 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2809 ++l_current_tile_param;
2812 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2813 opj_image_comp_header_update(l_image,l_cp);
2820 static void j2k_write_com(opj_j2k_t *j2k) {
2824 if(j2k->cp->comment) {
2825 opj_cio_t *cio = j2k->cio;
2826 char *comment = j2k->cp->comment;
2828 cio_write(cio, J2K_MS_COM, 2);
2829 lenp = cio_tell(cio);
2831 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2832 for (i = 0; i < strlen(comment); i++) {
2833 cio_write(cio, comment[i], 1);
2835 len = cio_tell(cio) - lenp;
2836 cio_seek(cio, lenp);
2837 cio_write(cio, len, 2);
2838 cio_seek(cio, lenp + len);
2842 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2848 * Writes the COM marker (comment)
2850 * @param p_stream the stream to write data to.
2851 * @param p_j2k J2K codec.
2852 * @param p_manager the user event manager.
2854 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2855 struct opj_stream_private *p_stream,
2856 struct opj_event_mgr * p_manager )
2858 OPJ_UINT32 l_comment_size;
2859 OPJ_UINT32 l_total_com_size;
2860 const OPJ_CHAR *l_comment;
2861 OPJ_BYTE * l_current_ptr = 00;
2864 assert(p_j2k != 00);
2865 assert(p_stream != 00);
2866 assert(p_manager != 00);
2868 l_comment = p_j2k->m_cp.comment;
2869 l_comment_size = strlen(l_comment);
2870 l_total_com_size = l_comment_size + 6;
2872 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2873 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2874 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2877 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2881 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2884 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2886 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2889 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2892 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2895 memcpy( l_current_ptr,l_comment,l_comment_size);
2897 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) {
2904 static void j2k_read_com(opj_j2k_t *j2k) {
2907 opj_cio_t *cio = j2k->cio;
2909 len = cio_read(cio, 2);
2910 cio_skip(cio, len - 2);
2913 * Reads a COM marker (comments)
2914 * @param p_header_data the data contained in the COM box.
2915 * @param jp2 the jpeg2000 file codec.
2916 * @param p_header_size the size of the data contained in the COM marker.
2917 * @param p_manager the user event manager.
2919 opj_bool j2k_read_com_v2 (
2920 opj_j2k_v2_t *p_j2k,
2921 OPJ_BYTE * p_header_data,
2922 OPJ_UINT32 p_header_size,
2923 struct opj_event_mgr * p_manager
2927 assert(p_j2k != 00);
2928 assert(p_manager != 00);
2929 assert(p_header_data != 00);
2930 (void)p_header_size;
2935 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2938 opj_cp_t *cp = j2k->cp;
2939 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2940 opj_tccp_t *tccp = &tcp->tccps[compno];
2941 opj_cio_t *cio = j2k->cio;
2943 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2944 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2945 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2946 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2947 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2949 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2950 for (i = 0; i < tccp->numresolutions; i++) {
2951 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2956 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2959 opj_cp_t *cp = j2k->cp;
2960 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2961 opj_tccp_t *tccp = &tcp->tccps[compno];
2962 opj_cio_t *cio = j2k->cio;
2964 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2966 /* If user wants to remove more resolutions than the codestream contains, return error*/
2967 assert(cp->reduce >= 0);
2968 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2969 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2970 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2971 j2k->state |= J2K_STATE_ERR;
2974 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2975 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2976 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2977 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2978 if (tccp->csty & J2K_CP_CSTY_PRT) {
2979 for (i = 0; i < tccp->numresolutions; i++) {
2980 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2981 tccp->prcw[i] = tmp & 0xf;
2982 tccp->prch[i] = tmp >> 4;
2987 if(j2k->cstr_info && compno == 0) {
2988 for (i = 0; i < tccp->numresolutions; i++) {
2989 if (tccp->csty & J2K_CP_CSTY_PRT) {
2990 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2991 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2994 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2995 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
3002 static void j2k_write_cod(opj_j2k_t *j2k) {
3003 opj_cp_t *cp = NULL;
3004 opj_tcp_t *tcp = NULL;
3007 opj_cio_t *cio = j2k->cio;
3009 cio_write(cio, J2K_MS_COD, 2); /* COD */
3011 lenp = cio_tell(cio);
3015 tcp = &cp->tcps[j2k->curtileno];
3017 cio_write(cio, tcp->csty, 1); /* Scod */
3018 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
3019 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
3020 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
3022 j2k_write_cox(j2k, 0);
3023 len = cio_tell(cio) - lenp;
3024 cio_seek(cio, lenp);
3025 cio_write(cio, len, 2); /* Lcod */
3026 cio_seek(cio, lenp + len);
3029 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
3034 * Writes the COD marker (Coding style default)
3036 * @param p_stream the stream to write data to.
3037 * @param p_j2k J2K codec.
3038 * @param p_manager the user event manager.
3040 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
3041 struct opj_stream_private *p_stream,
3042 struct opj_event_mgr * p_manager )
3044 opj_cp_v2_t *l_cp = 00;
3045 opj_tcp_v2_t *l_tcp = 00;
3046 OPJ_UINT32 l_code_size,l_remaining_size;
3047 OPJ_BYTE * l_current_data = 00;
3050 assert(p_j2k != 00);
3051 assert(p_manager != 00);
3052 assert(p_stream != 00);
3054 l_cp = &(p_j2k->m_cp);
3055 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3056 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
3057 l_remaining_size = l_code_size;
3059 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3060 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3061 = (OPJ_BYTE*)opj_realloc(
3062 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3065 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3069 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
3072 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3074 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
3075 l_current_data += 2;
3077 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
3078 l_current_data += 2;
3080 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
3083 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
3086 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
3089 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
3092 l_remaining_size -= 9;
3094 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3095 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3099 if (l_remaining_size != 0) {
3100 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3104 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) {
3111 static void j2k_read_cod(opj_j2k_t *j2k) {
3115 opj_cio_t *cio = j2k->cio;
3116 opj_cp_t *cp = j2k->cp;
3117 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3118 opj_image_t *image = j2k->image;
3120 len = cio_read(cio, 2); /* Lcod */
3121 tcp->csty = cio_read(cio, 1); /* Scod */
3122 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
3123 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
3124 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
3126 pos = cio_tell(cio);
3127 for (i = 0; i < image->numcomps; i++) {
3128 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
3130 j2k_read_cox(j2k, i);
3134 if (j2k->cstr_info) {
3135 opj_codestream_info_t *cstr_info = j2k->cstr_info;
3136 cstr_info->prog = tcp->prg;
3137 cstr_info->numlayers = tcp->numlayers;
3138 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
3139 for (i = 0; i < image->numcomps; i++) {
3140 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
3146 * Reads a COD marker (Coding Styke defaults)
3147 * @param p_header_data the data contained in the COD box.
3148 * @param p_j2k the jpeg2000 codec.
3149 * @param p_header_size the size of the data contained in the COD marker.
3150 * @param p_manager the user event manager.
3152 opj_bool j2k_read_cod_v2 (
3153 opj_j2k_v2_t *p_j2k,
3154 OPJ_BYTE * p_header_data,
3155 OPJ_UINT32 p_header_size,
3156 struct opj_event_mgr * p_manager
3162 opj_cp_v2_t *l_cp = 00;
3163 opj_tcp_v2_t *l_tcp = 00;
3164 opj_image_t *l_image = 00;
3167 assert(p_header_data != 00);
3168 assert(p_j2k != 00);
3169 assert(p_manager != 00);
3171 l_image = p_j2k->m_private_image;
3172 l_cp = &(p_j2k->m_cp);
3174 /* If we are in the first tile-part header of the current tile */
3175 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3176 &l_cp->tcps[p_j2k->m_current_tile_number] :
3177 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3179 /* Make sure room is sufficient */
3180 if (p_header_size < 5) {
3181 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3185 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
3187 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
3189 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
3190 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
3193 /* If user didn't set a number layer to decode take the max specify in the codestream. */
3194 if (l_cp->m_specific_param.m_dec.m_layer) {
3195 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
3198 l_tcp->num_layers_to_decode = l_tcp->numlayers;
3201 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
3205 for (i = 0; i < l_image->numcomps; ++i) {
3206 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
3209 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3210 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3214 if (p_header_size != 0) {
3215 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3219 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
3220 j2k_copy_tile_component_parameters(p_j2k);
3223 #ifdef WIP_REMOVE_MSD
3224 if (p_j2k->cstr_info) {
3225 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
3226 p_j2k->cstr_info->prog = l_tcp->prg;
3227 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
3228 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
3229 for (i = 0; i < l_image->numcomps; ++i) {
3230 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
3238 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
3241 opj_cp_t *cp = j2k->cp;
3242 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3243 opj_image_t *image = j2k->image;
3244 opj_cio_t *cio = j2k->cio;
3246 cio_write(cio, J2K_MS_COC, 2); /* COC */
3247 lenp = cio_tell(cio);
3249 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3250 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
3251 j2k_write_cox(j2k, compno);
3252 len = cio_tell(cio) - lenp;
3253 cio_seek(cio, lenp);
3254 cio_write(cio, len, 2); /* Lcoc */
3255 cio_seek(cio, lenp + len);
3259 * Writes the COC marker (Coding style component)
3261 * @param p_comp_no the index of the component to output.
3262 * @param p_stream the stream to write data to.
3263 * @param p_j2k J2K codec.
3264 * @param p_manager the user event manager.
3266 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
3267 OPJ_UINT32 p_comp_no,
3268 struct opj_stream_private *p_stream,
3269 struct opj_event_mgr * p_manager )
3271 OPJ_UINT32 l_coc_size,l_remaining_size;
3272 OPJ_UINT32 l_comp_room;
3275 assert(p_j2k != 00);
3276 assert(p_manager != 00);
3277 assert(p_stream != 00);
3279 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3281 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3283 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3284 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3285 = (OPJ_BYTE*)opj_realloc(
3286 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3288 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3292 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3295 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);
3297 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) {
3305 * Writes the COC marker (Coding style component)
3307 * @param p_comp_no the index of the component to output.
3308 * @param p_stream the stream to write data to.
3309 * @param p_j2k J2K codec.
3310 * @param p_manager the user event manager.
3312 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3313 OPJ_UINT32 p_comp_no,
3315 OPJ_UINT32 * p_data_written,
3316 struct opj_event_mgr * p_manager )
3318 opj_cp_v2_t *l_cp = 00;
3319 opj_tcp_v2_t *l_tcp = 00;
3320 OPJ_UINT32 l_coc_size,l_remaining_size;
3321 OPJ_BYTE * l_current_data = 00;
3322 opj_image_t *l_image = 00;
3323 OPJ_UINT32 l_comp_room;
3326 assert(p_j2k != 00);
3327 assert(p_manager != 00);
3329 l_cp = &(p_j2k->m_cp);
3330 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3331 l_image = p_j2k->m_private_image;
3332 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3334 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3335 l_remaining_size = l_coc_size;
3337 l_current_data = p_data;
3339 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3340 l_current_data += 2;
3342 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3343 l_current_data += 2;
3345 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3346 l_current_data+=l_comp_room;
3348 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3351 l_remaining_size -= (5 + l_comp_room);
3352 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3353 * p_data_written = l_coc_size;
3357 * Gets the maximum size taken by a coc.
3359 * @param p_j2k the jpeg2000 codec to use.
3361 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3364 OPJ_UINT32 l_nb_comp;
3365 OPJ_UINT32 l_nb_tiles;
3366 OPJ_UINT32 l_max = 0;
3370 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3371 l_nb_comp = p_j2k->m_private_image->numcomps;
3373 for (i=0;i<l_nb_tiles;++i) {
3374 for (j=0;j<l_nb_comp;++j) {
3375 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3382 static void j2k_read_coc(opj_j2k_t *j2k) {
3385 opj_cp_t *cp = j2k->cp;
3386 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3387 opj_image_t *image = j2k->image;
3388 opj_cio_t *cio = j2k->cio;
3390 len = cio_read(cio, 2); /* Lcoc */
3391 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3392 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
3393 j2k_read_cox(j2k, compno);
3397 * Reads a COC marker (Coding Style Component)
3398 * @param p_header_data the data contained in the COC box.
3399 * @param p_j2k the jpeg2000 codec.
3400 * @param p_header_size the size of the data contained in the COC marker.
3401 * @param p_manager the user event manager.
3403 opj_bool j2k_read_coc_v2 (
3404 opj_j2k_v2_t *p_j2k,
3405 OPJ_BYTE * p_header_data,
3406 OPJ_UINT32 p_header_size,
3407 struct opj_event_mgr * p_manager
3410 opj_cp_v2_t *l_cp = NULL;
3411 opj_tcp_v2_t *l_tcp = NULL;
3412 opj_image_t *l_image = NULL;
3413 OPJ_UINT32 l_comp_room;
3414 OPJ_UINT32 l_comp_no;
3417 assert(p_header_data != 00);
3418 assert(p_j2k != 00);
3419 assert(p_manager != 00);
3421 l_cp = &(p_j2k->m_cp);
3422 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3423 &l_cp->tcps[p_j2k->m_current_tile_number] :
3424 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3425 l_image = p_j2k->m_private_image;
3427 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3429 /* make sure room is sufficient*/
3430 if (p_header_size < l_comp_room + 1) {
3431 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3434 p_header_size -= l_comp_room + 1;
3436 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3437 p_header_data += l_comp_room;
3438 if (l_comp_no >= l_image->numcomps) {
3439 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3443 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3446 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3447 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3451 if (p_header_size != 0) {
3452 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3458 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3459 int bandno, numbands;
3462 opj_cp_t *cp = j2k->cp;
3463 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3464 opj_tccp_t *tccp = &tcp->tccps[compno];
3465 opj_cio_t *cio = j2k->cio;
3467 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3468 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3470 for (bandno = 0; bandno < numbands; bandno++) {
3471 expn = tccp->stepsizes[bandno].expn;
3472 mant = tccp->stepsizes[bandno].mant;
3474 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3475 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3477 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3482 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3484 int bandno, numbands;
3486 opj_cp_t *cp = j2k->cp;
3487 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3488 opj_tccp_t *tccp = &tcp->tccps[compno];
3489 opj_cio_t *cio = j2k->cio;
3491 tmp = cio_read(cio, 1); /* Sqcx */
3492 tccp->qntsty = tmp & 0x1f;
3493 tccp->numgbits = tmp >> 5;
3494 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3495 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3498 if (j2k->cp->correct) {
3500 /* if JPWL is on, we check whether there are too many subbands */
3501 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3502 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3503 "JPWL: bad number of subbands in Sqcx (%d)\n",
3506 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3509 /* we try to correct */
3511 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3512 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3519 /* We check whether there are too many subbands */
3520 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3521 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3522 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3523 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3526 #endif /* USE_JPWL */
3528 for (bandno = 0; bandno < numbands; bandno++) {
3530 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3531 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3534 tmp = cio_read(cio, 2); /* SPqcx_i */
3538 if (bandno < J2K_MAXBANDS){
3539 tccp->stepsizes[bandno].expn = expn;
3540 tccp->stepsizes[bandno].mant = mant;
3544 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3545 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3546 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3547 tccp->stepsizes[bandno].expn =
3548 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3549 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3550 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3556 static void j2k_write_qcd(opj_j2k_t *j2k) {
3559 opj_cio_t *cio = j2k->cio;
3561 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3562 lenp = cio_tell(cio);
3564 j2k_write_qcx(j2k, 0);
3565 len = cio_tell(cio) - lenp;
3566 cio_seek(cio, lenp);
3567 cio_write(cio, len, 2); /* Lqcd */
3568 cio_seek(cio, lenp + len);
3571 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3575 * Writes the QCD marker (quantization default)
3577 * @param p_comp_number the index of the component to output.
3578 * @param p_stream the stream to write data to.
3579 * @param p_j2k J2K codec.
3580 * @param p_manager the user event manager.
3582 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3583 struct opj_stream_private *p_stream,
3584 struct opj_event_mgr * p_manager )
3586 opj_cp_v2_t *l_cp = 00;
3587 opj_tcp_v2_t *l_tcp = 00;
3588 OPJ_UINT32 l_qcd_size,l_remaining_size;
3589 OPJ_BYTE * l_current_data = 00;
3592 assert(p_j2k != 00);
3593 assert(p_manager != 00);
3594 assert(p_stream != 00);
3596 l_cp = &(p_j2k->m_cp);
3597 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3598 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3599 l_remaining_size = l_qcd_size;
3601 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3602 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3603 = (OPJ_BYTE*)opj_realloc(
3604 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3607 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3611 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3614 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3616 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3617 l_current_data += 2;
3619 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3620 l_current_data += 2;
3622 l_remaining_size -= 4;
3624 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3625 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3629 if (l_remaining_size != 0) {
3630 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3634 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) {
3642 static void j2k_read_qcd(opj_j2k_t *j2k) {
3646 opj_cio_t *cio = j2k->cio;
3647 opj_image_t *image = j2k->image;
3649 len = cio_read(cio, 2); /* Lqcd */
3650 pos = cio_tell(cio);
3651 for (i = 0; i < image->numcomps; i++) {
3653 j2k_read_qcx(j2k, i, len - 2);
3658 * Reads a QCD marker (Quantization defaults)
3659 * @param p_header_data the data contained in the QCD box.
3660 * @param p_j2k the jpeg2000 codec.
3661 * @param p_header_size the size of the data contained in the QCD marker.
3662 * @param p_manager the user event manager.
3664 opj_bool j2k_read_qcd_v2 (
3665 opj_j2k_v2_t *p_j2k,
3666 OPJ_BYTE * p_header_data,
3667 OPJ_UINT32 p_header_size,
3668 struct opj_event_mgr * p_manager
3672 assert(p_header_data != 00);
3673 assert(p_j2k != 00);
3674 assert(p_manager != 00);
3676 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3677 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3681 if (p_header_size != 0) {
3682 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3686 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3687 j2k_copy_tile_quantization_parameters(p_j2k);
3692 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3695 opj_cio_t *cio = j2k->cio;
3697 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3698 lenp = cio_tell(cio);
3700 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3701 j2k_write_qcx(j2k, compno);
3702 len = cio_tell(cio) - lenp;
3703 cio_seek(cio, lenp);
3704 cio_write(cio, len, 2); /* Lqcc */
3705 cio_seek(cio, lenp + len);
3709 * Writes the QCC marker (quantization component)
3711 * @param p_comp_no the index of the component to output.
3712 * @param p_stream the stream to write data to.
3713 * @param p_j2k J2K codec.
3714 * @param p_manager the user event manager.
3716 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3717 OPJ_UINT32 p_comp_no,
3718 struct opj_stream_private *p_stream,
3719 struct opj_event_mgr * p_manager )
3721 OPJ_UINT32 l_qcc_size,l_remaining_size;
3724 assert(p_j2k != 00);
3725 assert(p_manager != 00);
3726 assert(p_stream != 00);
3728 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3729 l_remaining_size = l_qcc_size;
3731 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3732 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3733 = (OPJ_BYTE*)opj_realloc(
3734 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3736 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3740 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3743 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);
3745 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) {
3753 * Writes the QCC marker (quantization component)
3755 * @param p_comp_no the index of the component to output.
3756 * @param p_stream the stream to write data to.
3757 * @param p_j2k J2K codec.
3758 * @param p_manager the user event manager.
3760 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3761 OPJ_UINT32 p_comp_no,
3763 OPJ_UINT32 * p_data_written,
3764 struct opj_event_mgr * p_manager )
3766 OPJ_UINT32 l_qcc_size,l_remaining_size;
3767 OPJ_BYTE * l_current_data = 00;
3770 assert(p_j2k != 00);
3771 assert(p_manager != 00);
3773 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3774 l_remaining_size = l_qcc_size;
3776 l_current_data = p_data;
3778 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3779 l_current_data += 2;
3781 if (p_j2k->m_private_image->numcomps <= 256) {
3784 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3785 l_current_data += 2;
3787 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3790 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3791 l_remaining_size -= 6;
3794 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3795 l_current_data += 2;
3797 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3800 l_remaining_size -= 6;
3803 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3805 *p_data_written = l_qcc_size;
3809 * Gets the maximum size taken by a qcc.
3811 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3813 return j2k_get_max_coc_size(p_j2k);
3816 static void j2k_read_qcc(opj_j2k_t *j2k) {
3818 int numcomp = j2k->image->numcomps;
3819 opj_cio_t *cio = j2k->cio;
3821 len = cio_read(cio, 2); /* Lqcc */
3822 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3825 if (j2k->cp->correct) {
3827 static int backup_compno = 0;
3829 /* compno is negative or larger than the number of components!!! */
3830 if ((compno < 0) || (compno >= numcomp)) {
3831 opj_event_msg(j2k->cinfo, EVT_ERROR,
3832 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3835 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3838 /* we try to correct */
3839 compno = backup_compno % numcomp;
3840 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3841 "- setting component number to %d\n",
3845 /* keep your private count of tiles */
3848 #endif /* USE_JPWL */
3850 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3854 * Reads a QCC marker (Quantization component)
3855 * @param p_header_data the data contained in the QCC box.
3856 * @param p_j2k the jpeg2000 codec.
3857 * @param p_header_size the size of the data contained in the QCC marker.
3858 * @param p_manager the user event manager.
3860 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3861 OPJ_BYTE * p_header_data,
3862 OPJ_UINT32 p_header_size,
3863 struct opj_event_mgr * p_manager)
3865 OPJ_UINT32 l_num_comp,l_comp_no;
3868 assert(p_header_data != 00);
3869 assert(p_j2k != 00);
3870 assert(p_manager != 00);
3872 l_num_comp = p_j2k->m_private_image->numcomps;
3874 if (l_num_comp <= 256) {
3875 if (p_header_size < 1) {
3876 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3879 opj_read_bytes(p_header_data,&l_comp_no,1);
3884 if (p_header_size < 2) {
3885 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3888 opj_read_bytes(p_header_data,&l_comp_no,2);
3894 if (p_j2k->m_cp.correct) {
3896 static OPJ_UINT32 backup_compno = 0;
3898 /* compno is negative or larger than the number of components!!! */
3899 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3900 opj_event_msg_v2(p_manager, EVT_ERROR,
3901 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3902 l_comp_no, l_num_comp);
3904 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3907 /* we try to correct */
3908 l_comp_no = backup_compno % l_num_comp;
3909 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3910 "- setting component number to %d\n",
3914 /* keep your private count of tiles */
3917 #endif /* USE_JPWL */
3919 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3920 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3924 if (p_header_size != 0) {
3925 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3933 static void j2k_write_poc(opj_j2k_t *j2k) {
3934 int len, numpchgs, i;
3936 int numcomps = j2k->image->numcomps;
3938 opj_cp_t *cp = j2k->cp;
3939 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3940 opj_tccp_t *tccp = &tcp->tccps[0];
3941 opj_cio_t *cio = j2k->cio;
3943 numpchgs = 1 + tcp->numpocs;
3944 cio_write(cio, J2K_MS_POC, 2); /* POC */
3945 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3946 cio_write(cio, len, 2); /* Lpoc */
3947 for (i = 0; i < numpchgs; i++) {
3948 opj_poc_t *poc = &tcp->pocs[i];
3949 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3950 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3951 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3952 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3953 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3954 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3955 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3956 poc->compno1 = int_min(poc->compno1, numcomps);
3957 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3962 * Writes the POC marker (Progression Order Change)
3964 * @param p_stream the stream to write data to.
3965 * @param p_j2k J2K codec.
3966 * @param p_manager the user event manager.
3968 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3969 struct opj_stream_private *p_stream,
3970 struct opj_event_mgr * p_manager )
3972 OPJ_UINT32 l_nb_comp;
3973 OPJ_UINT32 l_nb_poc;
3974 OPJ_UINT32 l_poc_size;
3975 OPJ_UINT32 l_written_size = 0;
3976 opj_tcp_v2_t *l_tcp = 00;
3977 opj_tccp_t *l_tccp = 00;
3978 OPJ_UINT32 l_poc_room;
3981 assert(p_j2k != 00);
3982 assert(p_manager != 00);
3983 assert(p_stream != 00);
3985 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3986 l_tccp = &l_tcp->tccps[0];
3987 l_nb_comp = p_j2k->m_private_image->numcomps;
3988 l_nb_poc = 1 + l_tcp->numpocs;
3990 if (l_nb_comp <= 256) {
3996 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3998 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3999 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4000 = (OPJ_BYTE*)opj_realloc(
4001 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4004 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4008 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
4011 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
4013 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) {
4022 * Writes the POC marker (Progression Order Change)
4024 * @param p_stream the stream to write data to.
4025 * @param p_j2k J2K codec.
4026 * @param p_manager the user event manager.
4028 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
4030 OPJ_UINT32 * p_data_written,
4031 struct opj_event_mgr * p_manager )
4034 OPJ_BYTE * l_current_data = 00;
4035 OPJ_UINT32 l_nb_comp;
4036 OPJ_UINT32 l_nb_poc;
4037 OPJ_UINT32 l_poc_size;
4038 opj_image_t *l_image = 00;
4039 opj_tcp_v2_t *l_tcp = 00;
4040 opj_tccp_t *l_tccp = 00;
4041 opj_poc_t *l_current_poc = 00;
4042 OPJ_UINT32 l_poc_room;
4045 assert(p_j2k != 00);
4046 assert(p_manager != 00);
4048 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4049 l_tccp = &l_tcp->tccps[0];
4050 l_image = p_j2k->m_private_image;
4051 l_nb_comp = l_image->numcomps;
4052 l_nb_poc = 1 + l_tcp->numpocs;
4054 if (l_nb_comp <= 256) {
4061 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4063 l_current_data = p_data;
4065 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
4066 l_current_data += 2;
4068 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
4069 l_current_data += 2;
4071 l_current_poc = l_tcp->pocs;
4072 for (i = 0; i < l_nb_poc; ++i) {
4073 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
4076 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
4077 l_current_data+=l_poc_room;
4079 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
4082 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
4085 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
4086 l_current_data+=l_poc_room;
4088 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
4091 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
4092 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
4093 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
4094 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
4099 *p_data_written = l_poc_size;
4103 * Gets the maximum size taken by the writing of a POC.
4105 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
4107 opj_tcp_v2_t * l_tcp = 00;
4108 OPJ_UINT32 l_nb_tiles = 0;
4109 OPJ_UINT32 l_max_poc = 0;
4112 l_tcp = p_j2k->m_cp.tcps;
4113 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4115 for (i=0;i<l_nb_tiles;++i) {
4116 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
4122 return 4 + 9 * l_max_poc;
4126 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
4128 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
4131 OPJ_UINT32 l_nb_tiles;
4132 OPJ_UINT32 l_max = 0;
4133 opj_tcp_v2_t * l_tcp = 00;
4135 l_tcp = p_j2k->m_cp.tcps;
4136 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
4138 for (i=0;i<l_nb_tiles;++i) {
4139 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
4149 * Gets the maximum size taken by the headers of the SOT.
4151 * @param p_j2k the jpeg2000 codec to use.
4153 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
4155 OPJ_UINT32 l_nb_bytes = 0;
4156 OPJ_UINT32 l_nb_comps;
4157 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
4159 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
4160 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
4162 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
4163 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
4164 l_nb_bytes += l_nb_comps * l_coc_bytes;
4166 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
4167 l_nb_bytes += l_nb_comps * l_qcc_bytes;
4170 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
4172 /*** DEVELOPER CORNER, Add room for your headers ***/
4177 static void j2k_read_poc(opj_j2k_t *j2k) {
4178 int len, numpchgs, i, old_poc;
4180 int numcomps = j2k->image->numcomps;
4182 opj_cp_t *cp = j2k->cp;
4183 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
4184 opj_cio_t *cio = j2k->cio;
4186 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
4188 len = cio_read(cio, 2); /* Lpoc */
4189 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
4191 for (i = old_poc; i < numpchgs + old_poc; i++) {
4193 poc = &tcp->pocs[i];
4194 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
4195 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
4196 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
4197 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
4198 poc->compno1 = int_min(
4199 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
4200 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
4203 tcp->numpocs = numpchgs + old_poc - 1;
4207 * Reads a POC marker (Progression Order Change)
4209 * @param p_header_data the data contained in the POC box.
4210 * @param p_j2k the jpeg2000 codec.
4211 * @param p_header_size the size of the data contained in the POC marker.
4212 * @param p_manager the user event manager.
4214 opj_bool j2k_read_poc_v2 (
4215 opj_j2k_v2_t *p_j2k,
4216 OPJ_BYTE * p_header_data,
4217 OPJ_UINT32 p_header_size,
4218 struct opj_event_mgr * p_manager)
4220 OPJ_UINT32 i, l_nb_comp, l_tmp;
4221 opj_image_t * l_image = 00;
4222 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
4223 OPJ_UINT32 l_chunk_size, l_comp_room;
4225 opj_cp_v2_t *l_cp = 00;
4226 opj_tcp_v2_t *l_tcp = 00;
4227 opj_poc_t *l_current_poc = 00;
4230 assert(p_header_data != 00);
4231 assert(p_j2k != 00);
4232 assert(p_manager != 00);
4234 l_image = p_j2k->m_private_image;
4235 l_nb_comp = l_image->numcomps;
4236 if (l_nb_comp <= 256) {
4242 l_chunk_size = 5 + 2 * l_comp_room;
4243 l_current_poc_nb = p_header_size / l_chunk_size;
4244 l_current_poc_remaining = p_header_size % l_chunk_size;
4246 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
4247 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
4251 l_cp = &(p_j2k->m_cp);
4252 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4253 &l_cp->tcps[p_j2k->m_current_tile_number] :
4254 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4255 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
4256 l_current_poc_nb += l_old_poc_nb;
4258 assert(l_current_poc_nb < 32);
4260 /* now poc is in use.*/
4263 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
4264 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
4265 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
4267 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
4268 p_header_data+=l_comp_room;
4269 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
4271 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
4273 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
4274 p_header_data+=l_comp_room;
4275 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
4277 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4278 /* make sure comp is in acceptable bounds */
4279 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4283 l_tcp->numpocs = l_current_poc_nb - 1;
4287 static void j2k_read_crg(opj_j2k_t *j2k) {
4288 int len, i, Xcrg_i, Ycrg_i;
4290 opj_cio_t *cio = j2k->cio;
4291 int numcomps = j2k->image->numcomps;
4293 len = cio_read(cio, 2); /* Lcrg */
4294 for (i = 0; i < numcomps; i++) {
4295 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
4296 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4301 * Reads a CRG marker (Component registration)
4303 * @param p_header_data the data contained in the TLM box.
4304 * @param p_j2k the jpeg2000 codec.
4305 * @param p_header_size the size of the data contained in the TLM marker.
4306 * @param p_manager the user event manager.
4308 opj_bool j2k_read_crg_v2 (
4309 opj_j2k_v2_t *p_j2k,
4310 OPJ_BYTE * p_header_data,
4311 OPJ_UINT32 p_header_size,
4312 struct opj_event_mgr * p_manager
4315 OPJ_UINT32 l_nb_comp;
4317 assert(p_header_data != 00);
4318 assert(p_j2k != 00);
4319 assert(p_manager != 00);
4321 l_nb_comp = p_j2k->m_private_image->numcomps;
4323 if (p_header_size != l_nb_comp *4) {
4324 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4327 /* Do not care of this at the moment since only local variables are set here */
4330 (i = 0; i < l_nb_comp; ++i)
4332 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4334 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4341 static void j2k_read_tlm(opj_j2k_t *j2k) {
4342 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4343 long int Ttlm_i, Ptlm_i;
4345 opj_cio_t *cio = j2k->cio;
4347 len = cio_read(cio, 2); /* Ltlm */
4348 Ztlm = cio_read(cio, 1); /* Ztlm */
4349 Stlm = cio_read(cio, 1); /* Stlm */
4350 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4351 SP = (Stlm >> 6) & 0x01;
4352 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4353 for (i = 0; i < tile_tlm; i++) {
4354 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4355 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4360 * Reads a TLM marker (Tile Length Marker)
4362 * @param p_header_data the data contained in the TLM box.
4363 * @param p_j2k the jpeg2000 codec.
4364 * @param p_header_size the size of the data contained in the TLM marker.
4365 * @param p_manager the user event manager.
4367 opj_bool j2k_read_tlm_v2 (
4368 opj_j2k_v2_t *p_j2k,
4369 OPJ_BYTE * p_header_data,
4370 OPJ_UINT32 p_header_size,
4371 struct opj_event_mgr * p_manager
4374 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4376 assert(p_header_data != 00);
4377 assert(p_j2k != 00);
4378 assert(p_manager != 00);
4380 if (p_header_size < 2) {
4381 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4386 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4388 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4391 l_ST = ((l_Stlm >> 4) & 0x3);
4392 l_SP = (l_Stlm >> 6) & 0x1;
4394 l_Ptlm_size = (l_SP + 1) * 2;
4395 l_quotient = l_Ptlm_size + l_ST;
4397 l_tot_num_tp = p_header_size / l_quotient;
4398 l_tot_num_tp_remaining = p_header_size % l_quotient;
4400 if (l_tot_num_tp_remaining != 0) {
4401 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4404 /* FIXME Do not care of this at the moment since only local variables are set here */
4407 (i = 0; i < l_tot_num_tp; ++i)
4409 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4410 p_header_data += l_ST;
4411 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4412 p_header_data += l_Ptlm_size;
4417 static void j2k_read_plm(opj_j2k_t *j2k) {
4418 int len, i, Zplm, Nplm, add, packet_len = 0;
4420 opj_cio_t *cio = j2k->cio;
4422 len = cio_read(cio, 2); /* Lplm */
4423 Zplm = cio_read(cio, 1); /* Zplm */
4426 Nplm = cio_read(cio, 4); /* Nplm */
4428 for (i = Nplm; i > 0; i--) {
4429 add = cio_read(cio, 1);
4431 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4432 if ((add & 0x80) == 0) {
4443 * Reads a PLM marker (Packet length, main header marker)
4445 * @param p_header_data the data contained in the TLM box.
4446 * @param p_j2k the jpeg2000 codec.
4447 * @param p_header_size the size of the data contained in the TLM marker.
4448 * @param p_manager the user event manager.
4450 opj_bool j2k_read_plm_v2 (
4451 opj_j2k_v2_t *p_j2k,
4452 OPJ_BYTE * p_header_data,
4453 OPJ_UINT32 p_header_size,
4454 struct opj_event_mgr * p_manager
4458 assert(p_header_data != 00);
4459 assert(p_j2k != 00);
4460 assert(p_manager != 00);
4462 if (p_header_size < 1) {
4463 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4466 /* Do not care of this at the moment since only local variables are set here */
4468 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4475 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4477 p_header_size -= (1+l_Nplm);
4481 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4485 (i = 0; i < l_Nplm; ++i)
4487 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4489 // take only the last seven bytes
4490 l_packet_len |= (l_tmp & 0x7f);
4498 // store packet length and proceed to next packet
4505 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4513 static void j2k_read_plt(opj_j2k_t *j2k) {
4514 int len, i, Zplt, packet_len = 0, add;
4516 opj_cio_t *cio = j2k->cio;
4518 len = cio_read(cio, 2); /* Lplt */
4519 Zplt = cio_read(cio, 1); /* Zplt */
4520 for (i = len - 3; i > 0; i--) {
4521 add = cio_read(cio, 1);
4522 packet_len = (packet_len << 7) + add; /* Iplt_i */
4523 if ((add & 0x80) == 0) {
4531 * Reads a PLT marker (Packet length, tile-part header)
4533 * @param p_header_data the data contained in the PLT box.
4534 * @param p_j2k the jpeg2000 codec.
4535 * @param p_header_size the size of the data contained in the PLT marker.
4536 * @param p_manager the user event manager.
4538 opj_bool j2k_read_plt_v2 (
4539 opj_j2k_v2_t *p_j2k,
4540 OPJ_BYTE * p_header_data,
4541 OPJ_UINT32 p_header_size,
4542 struct opj_event_mgr * p_manager
4545 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4548 assert(p_header_data != 00);
4549 assert(p_j2k != 00);
4550 assert(p_manager != 00);
4552 if (p_header_size < 1) {
4553 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4557 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4561 for (i = 0; i < p_header_size; ++i) {
4562 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4564 /* take only the last seven bytes */
4565 l_packet_len |= (l_tmp & 0x7f);
4570 /* store packet length and proceed to next packet */
4575 if (l_packet_len != 0) {
4576 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4583 static void j2k_read_ppm(opj_j2k_t *j2k) {
4584 int len, Z_ppm, i, j;
4587 opj_cp_t *cp = j2k->cp;
4588 opj_cio_t *cio = j2k->cio;
4590 len = cio_read(cio, 2);
4593 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4596 if (cp->ppm_previous == 0) {
4597 N_ppm = cio_read(cio, 4); /* N_ppm */
4600 N_ppm = cp->ppm_previous;
4603 if (Z_ppm == 0) { /* First PPM marker */
4604 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4605 cp->ppm_data_first = cp->ppm_data;
4606 cp->ppm_len = N_ppm;
4607 } else { /* NON-first PPM marker */
4608 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4611 /* this memory allocation check could be done even in non-JPWL cases */
4613 if (!cp->ppm_data) {
4614 opj_event_msg(j2k->cinfo, EVT_ERROR,
4615 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4617 if (!JPWL_ASSUME || JPWL_ASSUME) {
4618 opj_free(cp->ppm_data);
4619 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4626 cp->ppm_data_first = cp->ppm_data;
4627 cp->ppm_len = N_ppm + cp->ppm_store;
4629 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4630 cp->ppm_data[j] = cio_read(cio, 1);
4634 break; /* Case of non-finished packet header in present marker but finished in next one */
4636 cp->ppm_previous = i - 1;
4641 * Reads a PPM marker (Packed packet headers, main header)
4643 * @param p_header_data the data contained in the POC box.
4644 * @param p_j2k the jpeg2000 codec.
4645 * @param p_header_size the size of the data contained in the POC marker.
4646 * @param p_manager the user event manager.
4649 opj_bool j2k_read_ppm_v2 (
4650 opj_j2k_v2_t *p_j2k,
4651 OPJ_BYTE * p_header_data,
4652 OPJ_UINT32 p_header_size,
4653 struct opj_event_mgr * p_manager
4657 opj_cp_v2_t *l_cp = 00;
4658 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4661 assert(p_header_data != 00);
4662 assert(p_j2k != 00);
4663 assert(p_manager != 00);
4665 if (p_header_size < 1) {
4666 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4670 l_cp = &(p_j2k->m_cp);
4673 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4677 /* First PPM marker */
4679 if (p_header_size < 4) {
4680 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4684 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4688 /* First PPM marker: Initialization */
4689 l_cp->ppm_len = l_N_ppm;
4690 l_cp->ppm_data_size = 0;
4692 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4693 if (l_cp->ppm_buffer == 00) {
4694 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4697 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4699 l_cp->ppm_data = l_cp->ppm_buffer;
4703 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4704 if (p_header_size >= 4) {
4706 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4709 l_cp->ppm_len += l_N_ppm ;
4711 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4712 if (l_cp->ppm_buffer == 00) {
4713 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4716 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4718 l_cp->ppm_data = l_cp->ppm_buffer;
4725 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4727 if (l_remaining_data <= p_header_size) {
4728 /* we must store less information than available in the packet */
4729 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4730 l_cp->ppm_data_size = l_cp->ppm_len;
4731 p_header_size -= l_remaining_data;
4732 p_header_data += l_remaining_data;
4735 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4736 l_cp->ppm_data_size += p_header_size;
4737 p_header_data += p_header_size;
4750 * Reads a PPM marker (Packed packet headers, main header)
4752 * @param p_header_data the data contained in the POC box.
4753 * @param p_j2k the jpeg2000 codec.
4754 * @param p_header_size the size of the data contained in the POC marker.
4755 * @param p_manager the user event manager.
4757 opj_bool j2k_read_ppm_v3 (
4758 opj_j2k_v2_t *p_j2k,
4759 OPJ_BYTE * p_header_data,
4760 OPJ_UINT32 p_header_size,
4761 struct opj_event_mgr * p_manager
4764 opj_cp_v2_t *l_cp = 00;
4765 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4768 assert(p_header_data != 00);
4769 assert(p_j2k != 00);
4770 assert(p_manager != 00);
4772 /* Minimum size of PPM marker is equal to the size of Zppm element */
4773 if (p_header_size < 1) {
4774 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4778 l_cp = &(p_j2k->m_cp);
4781 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4785 /* First PPM marker */
4787 /* We need now at least the Nppm^0 element */
4788 if (p_header_size < 4) {
4789 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4793 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4797 /* First PPM marker: Initialization */
4798 l_cp->ppm_len = l_N_ppm;
4799 l_cp->ppm_data_read = 0;
4801 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4802 if (l_cp->ppm_data == 00) {
4803 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4806 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4808 l_cp->ppm_data_current = l_cp->ppm_data;
4810 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4813 if (p_header_size < 4) {
4814 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4818 /* Uncompleted Ippm series in the previous PPM marker?*/
4819 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4820 /* Get the place where add the remaining Ippm series*/
4821 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4822 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4825 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4829 /* Increase the size of ppm_data to add the new Ippm series*/
4830 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4832 /* Keep the position of the place where concatenate the new series*/
4833 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4834 l_cp->ppm_len += l_N_ppm;
4839 l_remaining_data = p_header_size;
4841 while (l_remaining_data >= l_N_ppm) {
4842 /* read a complete Ippm series*/
4843 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4844 p_header_size -= l_N_ppm;
4845 p_header_data += l_N_ppm;
4847 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4851 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4856 l_remaining_data = p_header_size;
4860 l_remaining_data = p_header_size;
4862 /* Next Ippm series is a complete series ?*/
4863 if (l_remaining_data > l_N_ppm) {
4864 /* Increase the size of ppm_data to add the new Ippm series*/
4865 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4867 /* Keep the position of the place where concatenate the new series */
4868 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4869 l_cp->ppm_len += l_N_ppm;
4874 /* Need to read an incomplete Ippm series*/
4875 if (l_remaining_data) {
4876 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4878 /* Keep the position of the place where concatenate the new series*/
4879 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4880 l_cp->ppm_len += l_N_ppm;
4882 /* Read incomplete Ippm series*/
4883 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4884 p_header_size -= l_remaining_data;
4885 p_header_data += l_remaining_data;
4887 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4892 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4893 if (p_header_size >= 4) {
4895 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4898 l_cp->ppm_len += l_N_ppm ;
4900 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4901 if (l_cp->ppm_buffer == 00) {
4902 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4905 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4907 l_cp->ppm_data = l_cp->ppm_buffer;
4914 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4916 if (l_remaining_data <= p_header_size) {
4917 /* we must store less information than available in the packet */
4918 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4919 l_cp->ppm_data_size = l_cp->ppm_len;
4920 p_header_size -= l_remaining_data;
4921 p_header_data += l_remaining_data;
4924 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4925 l_cp->ppm_data_size += p_header_size;
4926 p_header_data += p_header_size;
4935 static void j2k_read_ppt(opj_j2k_t *j2k) {
4936 int len, Z_ppt, i, j = 0;
4938 opj_cp_t *cp = j2k->cp;
4939 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4940 opj_cio_t *cio = j2k->cio;
4942 len = cio_read(cio, 2);
4943 Z_ppt = cio_read(cio, 1);
4945 if (Z_ppt == 0) { /* First PPT marker */
4946 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4947 tcp->ppt_data_first = tcp->ppt_data;
4949 tcp->ppt_len = len - 3;
4950 } else { /* NON-first PPT marker */
4951 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4952 tcp->ppt_data_first = tcp->ppt_data;
4953 tcp->ppt_len = len - 3 + tcp->ppt_store;
4956 for (i = len - 3; i > 0; i--) {
4957 tcp->ppt_data[j] = cio_read(cio, 1);
4964 * Reads a PPT marker (Packed packet headers, tile-part header)
4966 * @param p_header_data the data contained in the PPT box.
4967 * @param p_j2k the jpeg2000 codec.
4968 * @param p_header_size the size of the data contained in the PPT marker.
4969 * @param p_manager the user event manager.
4971 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4972 OPJ_BYTE * p_header_data,
4973 OPJ_UINT32 p_header_size,
4974 struct opj_event_mgr * p_manager )
4976 opj_cp_v2_t *l_cp = 00;
4977 opj_tcp_v2_t *l_tcp = 00;
4981 assert(p_header_data != 00);
4982 assert(p_j2k != 00);
4983 assert(p_manager != 00);
4985 /* We need to have the Z_ppt element at minimum */
4986 if (p_header_size < 1) {
4987 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4991 l_cp = &(p_j2k->m_cp);
4993 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");
4997 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
5000 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
5004 /* Allocate buffer to read the packet header */
5006 /* First PPT marker */
5007 l_tcp->ppt_data_size = 0;
5008 l_tcp->ppt_len = p_header_size;
5010 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
5011 if (l_tcp->ppt_buffer == 00) {
5012 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5015 l_tcp->ppt_data = l_tcp->ppt_buffer;
5017 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
5020 l_tcp->ppt_len += p_header_size;
5022 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
5023 if (l_tcp->ppt_buffer == 00) {
5024 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5027 l_tcp->ppt_data = l_tcp->ppt_buffer;
5029 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
5032 /* Read packet header from buffer */
5033 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
5035 l_tcp->ppt_data_size += p_header_size;
5041 * Writes the TLM marker (Tile Length Marker)
5043 * @param p_stream the stream to write data to.
5044 * @param p_j2k J2K codec.
5045 * @param p_manager the user event manager.
5047 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
5048 struct opj_stream_private *p_stream,
5049 struct opj_event_mgr * p_manager )
5051 OPJ_BYTE * l_current_data = 00;
5052 OPJ_UINT32 l_tlm_size;
5055 assert(p_j2k != 00);
5056 assert(p_manager != 00);
5057 assert(p_stream != 00);
5059 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5061 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5062 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5063 = (OPJ_BYTE*)opj_realloc(
5064 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5067 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5071 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
5074 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5076 /* change the way data is written to avoid seeking if possible */
5078 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
5080 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
5081 l_current_data += 2;
5083 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
5084 l_current_data += 2;
5086 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
5089 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5092 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
5093 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) {
5100 static void j2k_write_tlm(opj_j2k_t *j2k){
5102 opj_cio_t *cio = j2k->cio;
5103 j2k->tlm_start = cio_tell(cio);
5104 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
5105 lenp = 4 + (5*j2k->totnum_tp);
5106 cio_write(cio,lenp,2); /* Ltlm */
5107 cio_write(cio, 0,1); /* Ztlm=0*/
5108 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5109 cio_skip(cio,5*j2k->totnum_tp);
5112 static void j2k_write_sot(opj_j2k_t *j2k) {
5115 opj_cio_t *cio = j2k->cio;
5117 j2k->sot_start = cio_tell(cio);
5118 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
5119 lenp = cio_tell(cio);
5120 cio_skip(cio, 2); /* Lsot (further) */
5121 cio_write(cio, j2k->curtileno, 2); /* Isot */
5122 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
5123 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
5124 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
5125 len = cio_tell(cio) - lenp;
5126 cio_seek(cio, lenp);
5127 cio_write(cio, len, 2); /* Lsot */
5128 cio_seek(cio, lenp + len);
5132 /* update markers struct */
5133 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
5134 #endif /* USE_JPWL */
5137 if( j2k->cstr_info && j2k->cur_tp_num==0){
5138 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
5143 * Writes the SOT marker (Start of tile-part)
5145 * @param p_stream the stream to write data to.
5146 * @param p_j2k J2K codec.
5147 * @param p_manager the user event manager.
5149 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
5151 OPJ_UINT32 * p_data_written,
5152 const struct opj_stream_private *p_stream,
5153 struct opj_event_mgr * p_manager )
5156 assert(p_j2k != 00);
5157 assert(p_manager != 00);
5158 assert(p_stream != 00);
5160 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
5163 opj_write_bytes(p_data,10,2); /* Lsot */
5166 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
5172 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
5175 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
5180 /* update markers struct */
5182 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
5184 assert( 0 && "TODO" );
5185 #endif /* USE_JPWL */
5187 * p_data_written = 12;
5196 * Reads a PPT marker (Packed packet headers, tile-part header)
5198 * @param p_header_data the data contained in the PPT box.
5199 * @param p_j2k the jpeg2000 codec.
5200 * @param p_header_size the size of the data contained in the PPT marker.
5201 * @param p_manager the user event manager.
5203 opj_bool j2k_read_sot_v2 (
5204 opj_j2k_v2_t *p_j2k,
5205 OPJ_BYTE * p_header_data,
5206 OPJ_UINT32 p_header_size,
5207 struct opj_event_mgr * p_manager
5211 opj_cp_v2_t *l_cp = 00;
5212 opj_tcp_v2_t *l_tcp = 00;
5213 OPJ_UINT32 l_tot_len, l_num_parts = 0;
5214 OPJ_UINT32 l_current_part;
5215 OPJ_UINT32 l_tile_x,l_tile_y;
5218 assert(p_header_data != 00);
5219 assert(p_j2k != 00);
5220 assert(p_manager != 00);
5222 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
5223 if (p_header_size != 8) {
5224 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
5228 l_cp = &(p_j2k->m_cp);
5229 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
5232 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5233 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
5234 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
5237 if (l_cp->correct) {
5239 int tileno = p_j2k->m_current_tile_number;
5240 static int backup_tileno = 0;
5242 /* tileno is negative or larger than the number of tiles!!! */
5243 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
5244 opj_event_msg_v2(p_manager, EVT_ERROR,
5245 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5246 tileno, (l_cp->tw * l_cp->th));
5248 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5251 /* we try to correct */
5252 tileno = backup_tileno;
5253 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5254 "- setting tile number to %d\n",
5258 /* keep your private count of tiles */
5261 #endif /* USE_JPWL */
5263 /* look for the tile in the list of already processed tile (in parts). */
5264 /* Optimization possible here with a more complex data structure and with the removing of tiles */
5265 /* since the time taken by this function can only grow at the time */
5267 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
5270 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
5271 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
5273 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5279 if (l_cp->correct) {
5281 /* totlen is negative or larger than the bytes left!!! */
5282 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))) { */
5283 opj_event_msg_v2(p_manager, EVT_ERROR,
5284 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5285 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5287 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5290 /* we try to correct */
5292 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5293 "- setting Psot to %d => assuming it is the last tile\n",
5297 #endif /* USE_JPWL */
5299 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5301 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5302 "we assuming it is the last tile-part of the codestream.\n");
5303 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5306 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5309 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5312 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5313 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5314 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5315 if (l_tcp->m_nb_tile_parts) {
5316 if (l_current_part >= l_tcp->m_nb_tile_parts){
5317 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5318 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5319 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5323 l_tcp->m_nb_tile_parts = l_num_parts;
5326 /* If know the number of tile part header we will check if we didn't read the last*/
5327 if (l_tcp->m_nb_tile_parts) {
5328 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5329 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5333 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5334 /* Keep the size of data to skip after this marker */
5335 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5338 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5339 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5342 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5344 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5345 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5346 p_j2k->m_specific_param.m_decoder.m_skip_data =
5347 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5348 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5349 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5350 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5353 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5354 p_j2k->m_specific_param.m_decoder.m_skip_data =
5355 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5359 if (p_j2k->cstr_index)
5361 assert(p_j2k->cstr_index->tile_index != 00);
5362 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5363 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5365 if (l_num_parts != 0){
5366 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5367 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5370 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5371 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5372 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5374 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5375 (opj_tp_index_t*)opj_realloc(
5376 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5377 l_num_parts* sizeof(opj_tp_index_t));
5380 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5382 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5383 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5384 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5385 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5386 sizeof(opj_tp_index_t));
5389 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5390 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5391 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5392 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5393 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5394 * sizeof(opj_tp_index_t));
5403 /* 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 */
5404 /* if (p_j2k->cstr_info) {
5407 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5410 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5411 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5412 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5413 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5416 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5419 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5423 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5426 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5427 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5428 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5433 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5436 opj_tcp_t *tcp = NULL;
5437 opj_codestream_info_t *cstr_info = NULL;
5439 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5440 opj_cp_t *cp = j2k->cp;
5441 opj_cio_t *cio = j2k->cio;
5443 tcd->tp_num = j2k->tp_num ;
5444 tcd->cur_tp_num = j2k->cur_tp_num;
5446 cio_write(cio, J2K_MS_SOD, 2);
5448 if( j2k->cstr_info && j2k->cur_tp_num==0){
5449 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5452 if (j2k->curtileno == 0) {
5453 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5457 cstr_info = j2k->cstr_info;
5459 if (!j2k->cur_tp_num ) {
5460 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5461 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5464 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5465 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5469 /* update markers struct */
5470 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5471 #endif /* USE_JPWL */
5476 tcp = &cp->tcps[j2k->curtileno];
5477 for (layno = 0; layno < tcp->numlayers; layno++) {
5478 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5479 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5480 } else if (tcp->rates[layno]) {
5481 tcp->rates[layno]=1;
5484 if(j2k->cur_tp_num == 0){
5485 tcd->tcd_image->tiles->packno = 0;
5487 cstr_info->packno = 0;
5490 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5492 /* Writing Psot in SOT marker */
5493 totlen = cio_tell(cio) + l - j2k->sot_start;
5494 cio_seek(cio, j2k->sot_start + 6);
5495 cio_write(cio, totlen, 4);
5496 cio_seek(cio, j2k->sot_start + totlen);
5497 /* Writing Ttlm and Ptlm in TLM marker */
5499 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5500 cio_write(cio, j2k->curtileno, 1);
5501 cio_write(cio, totlen, 4);
5503 cio_seek(cio, j2k->sot_start + totlen);
5507 * Writes the SOD marker (Start of data)
5509 * @param p_stream the stream to write data to.
5510 * @param p_j2k J2K codec.
5511 * @param p_manager the user event manager.
5513 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5514 struct opj_tcd_v2 * p_tile_coder,
5516 OPJ_UINT32 * p_data_written,
5517 OPJ_UINT32 p_total_data_size,
5518 const struct opj_stream_private *p_stream,
5519 struct opj_event_mgr * p_manager )
5521 opj_tcp_v2_t *l_tcp = 00;
5522 opj_codestream_info_t *l_cstr_info = 00;
5523 opj_cp_v2_t *l_cp = 00;
5525 OPJ_UINT32 l_size_tile;
5526 OPJ_UINT32 l_remaining_data;
5529 assert(p_j2k != 00);
5530 assert(p_manager != 00);
5531 assert(p_stream != 00);
5533 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5536 /* make room for the EOF marker */
5537 l_remaining_data = p_total_data_size - 4;
5539 l_cp = &(p_j2k->m_cp);
5540 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5543 /* update tile coder */
5544 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5545 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5547 l_size_tile = l_cp->th * l_cp->tw;
5550 /* TODO mergeV2: check this part which use cstr_info */
5551 /*l_cstr_info = p_j2k->cstr_info;
5553 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5554 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5555 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5561 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5563 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5568 /* update markers struct */
5569 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5571 assert( 0 && "TODO" );
5572 #endif /* USE_JPWL */
5577 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5578 p_tile_coder->tcd_image->tiles->packno = 0;
5580 l_cstr_info->packno = 0;
5584 *p_data_written = 0;
5586 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)) {
5587 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5591 *p_data_written += 2;
5597 static void j2k_read_sod(opj_j2k_t *j2k) {
5598 int len, truncate = 0, i;
5599 unsigned char *data = NULL, *data_ptr = NULL;
5601 opj_cio_t *cio = j2k->cio;
5602 int curtileno = j2k->curtileno;
5605 if (j2k->cstr_info) {
5606 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5607 cio_tell(cio) + j2k->pos_correction - 1;
5608 if (j2k->cur_tp_num == 0)
5609 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5610 j2k->cstr_info->packno = 0;
5613 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
5615 if (len == cio_numbytesleft(cio) + 1) {
5616 truncate = 1; /* Case of a truncate codestream */
5619 data = j2k->tile_data[curtileno];
5620 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
5622 data_ptr = data + j2k->tile_len[curtileno];
5623 for (i = 0; i < len; i++) {
5624 data_ptr[i] = cio_read(cio, 1);
5627 j2k->tile_len[curtileno] += len;
5628 j2k->tile_data[curtileno] = data;
5631 j2k->state = J2K_STATE_TPHSOT;
5633 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
5639 * Reads a SOD marker (Start Of Data)
5641 * @param p_header_data the data contained in the SOD box.
5642 * @param p_j2k the jpeg2000 codec.
5643 * @param p_header_size the size of the data contained in the SOD marker.
5644 * @param p_manager the user event manager.
5646 opj_bool j2k_read_sod_v2 (
5647 opj_j2k_v2_t *p_j2k,
5648 struct opj_stream_private *p_stream,
5649 struct opj_event_mgr * p_manager
5652 OPJ_UINT32 l_current_read_size;
5653 opj_codestream_index_t * l_cstr_index = 00;
5654 OPJ_BYTE ** l_current_data = 00;
5655 opj_tcp_v2_t * l_tcp = 00;
5656 OPJ_UINT32 * l_tile_len = 00;
5659 assert(p_j2k != 00);
5660 assert(p_manager != 00);
5661 assert(p_stream != 00);
5663 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5665 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5666 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5667 // but we are in the last tile part,
5668 // so its result will fit on OPJ_UINT32 unless we find
5669 // a file with a single tile part of more than 4 GB...*/
5670 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5673 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5675 l_current_data = &(l_tcp->m_data);
5676 l_tile_len = &l_tcp->m_data_size;
5678 if (! *l_current_data) {
5679 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5682 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5685 if (*l_current_data == 00) {
5686 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5692 l_cstr_index = p_j2k->cstr_index;
5694 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5696 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5697 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5699 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5700 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5702 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5706 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5708 /*l_cstr_index->packno = 0;*/
5711 l_current_read_size = opj_stream_read_data( p_stream,
5712 *l_current_data + *l_tile_len,
5713 p_j2k->m_specific_param.m_decoder.m_sot_length,
5716 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5717 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5720 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5723 *l_tile_len += l_current_read_size;
5729 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5730 opj_cp_t *cp = j2k->cp;
5731 opj_tcp_t *tcp = &cp->tcps[tileno];
5732 opj_cio_t *cio = j2k->cio;
5733 int numcomps = j2k->image->numcomps;
5735 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5736 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5737 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5738 cio_write(cio, 0, 1); /* Srgn */
5739 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5743 * Writes the RGN marker (Region Of Interest)
5745 * @param p_tile_no the tile to output
5746 * @param p_comp_no the component to output
5747 * @param p_stream the stream to write data to.
5748 * @param p_j2k J2K codec.
5749 * @param p_manager the user event manager.
5751 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5752 OPJ_UINT32 p_tile_no,
5753 OPJ_UINT32 p_comp_no,
5754 struct opj_stream_private *p_stream,
5755 struct opj_event_mgr * p_manager )
5757 OPJ_BYTE * l_current_data = 00;
5758 OPJ_UINT32 l_nb_comp;
5759 OPJ_UINT32 l_rgn_size;
5760 opj_image_t *l_image = 00;
5761 opj_cp_v2_t *l_cp = 00;
5762 opj_tcp_v2_t *l_tcp = 00;
5763 opj_tccp_t *l_tccp = 00;
5764 OPJ_UINT32 l_comp_room;
5767 assert(p_j2k != 00);
5768 assert(p_manager != 00);
5769 assert(p_stream != 00);
5771 l_cp = &(p_j2k->m_cp);
5772 l_tcp = &l_cp->tcps[p_tile_no];
5773 l_tccp = &l_tcp->tccps[p_comp_no];
5775 l_nb_comp = l_image->numcomps;
5777 if (l_nb_comp <= 256) {
5784 l_rgn_size = 6 + l_comp_room;
5786 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5788 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5789 l_current_data += 2;
5791 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5792 l_current_data += 2;
5794 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5795 l_current_data+=l_comp_room;
5797 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5800 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5803 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) {
5810 static void j2k_read_rgn(opj_j2k_t *j2k) {
5811 int len, compno, roisty;
5813 opj_cp_t *cp = j2k->cp;
5814 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5815 opj_cio_t *cio = j2k->cio;
5816 int numcomps = j2k->image->numcomps;
5818 len = cio_read(cio, 2); /* Lrgn */
5819 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
5820 roisty = cio_read(cio, 1); /* Srgn */
5823 if (j2k->cp->correct) {
5824 /* totlen is negative or larger than the bytes left!!! */
5825 if (compno >= numcomps) {
5826 opj_event_msg(j2k->cinfo, EVT_ERROR,
5827 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5829 if (!JPWL_ASSUME || JPWL_ASSUME) {
5830 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5835 #endif /* USE_JPWL */
5837 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
5840 static void j2k_write_eoc(opj_j2k_t *j2k) {
5841 opj_cio_t *cio = j2k->cio;
5842 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5843 cio_write(cio, J2K_MS_EOC, 2);
5847 /* update markers struct */
5848 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5849 #endif /* USE_JPWL */
5854 * Writes the EOC marker (End of Codestream)
5856 * @param p_stream the stream to write data to.
5857 * @param p_j2k J2K codec.
5858 * @param p_manager the user event manager.
5860 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5861 struct opj_stream_private *p_stream,
5862 struct opj_event_mgr * p_manager )
5865 assert(p_j2k != 00);
5866 assert(p_manager != 00);
5867 assert(p_stream != 00);
5869 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5874 /* update markers struct */
5876 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5878 #endif /* USE_JPWL */
5880 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5884 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5893 * Reads a RGN marker (Region Of Interest)
5895 * @param p_header_data the data contained in the POC box.
5896 * @param p_j2k the jpeg2000 codec.
5897 * @param p_header_size the size of the data contained in the POC marker.
5898 * @param p_manager the user event manager.
5900 opj_bool j2k_read_rgn_v2 (
5901 opj_j2k_v2_t *p_j2k,
5902 OPJ_BYTE * p_header_data,
5903 OPJ_UINT32 p_header_size,
5904 struct opj_event_mgr * p_manager
5907 OPJ_UINT32 l_nb_comp;
5908 opj_image_t * l_image = 00;
5910 opj_cp_v2_t *l_cp = 00;
5911 opj_tcp_v2_t *l_tcp = 00;
5912 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5915 assert(p_header_data != 00);
5916 assert(p_j2k != 00);
5917 assert(p_manager != 00);
5919 l_image = p_j2k->m_private_image;
5920 l_nb_comp = l_image->numcomps;
5922 if (l_nb_comp <= 256) {
5927 if (p_header_size != 2 + l_comp_room) {
5928 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5932 l_cp = &(p_j2k->m_cp);
5933 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5934 &l_cp->tcps[p_j2k->m_current_tile_number] :
5935 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5937 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5938 p_header_data+=l_comp_room;
5939 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5943 if (l_cp->correct) {
5944 /* totlen is negative or larger than the bytes left!!! */
5945 if (l_comp_room >= l_nb_comp) {
5946 opj_event_msg_v2(p_manager, EVT_ERROR,
5947 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5948 l_comp_room, l_nb_comp);
5949 if (!JPWL_ASSUME || JPWL_ASSUME) {
5950 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5955 #endif /* USE_JPWL */
5957 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5964 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5966 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5969 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5976 * Updates the rates of the tcp.
5978 * @param p_stream the stream to write data to.
5979 * @param p_j2k J2K codec.
5980 * @param p_manager the user event manager.
5982 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5983 struct opj_stream_private *p_stream,
5984 struct opj_event_mgr * p_manager )
5986 opj_cp_v2_t * l_cp = 00;
5987 opj_image_t * l_image = 00;
5988 opj_tcp_v2_t * l_tcp = 00;
5989 opj_image_comp_t * l_img_comp = 00;
5992 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5993 OPJ_FLOAT32 * l_rates = 0;
5994 OPJ_FLOAT32 l_sot_remove;
5995 OPJ_UINT32 l_bits_empty, l_size_pixel;
5996 OPJ_UINT32 l_tile_size = 0;
5997 OPJ_UINT32 l_last_res;
5998 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
6001 assert(p_j2k != 00);
6002 assert(p_manager != 00);
6003 assert(p_stream != 00);
6006 l_cp = &(p_j2k->m_cp);
6007 l_image = p_j2k->m_private_image;
6010 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
6011 l_size_pixel = l_image->numcomps * l_image->comps->prec;
6012 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
6014 if (l_cp->m_specific_param.m_enc.m_tp_on) {
6015 l_tp_stride_func = get_tp_stride;
6018 l_tp_stride_func = get_default_stride;
6021 for (i=0;i<l_cp->th;++i) {
6022 for (j=0;j<l_cp->tw;++j) {
6023 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
6025 /* 4 borders of the tile rescale on the image if necessary */
6026 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
6027 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
6028 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
6029 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
6031 l_rates = l_tcp->rates;
6033 /* Modification of the RATE >> */
6035 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6037 ((*l_rates) * l_bits_empty)
6045 for (k = 1; k < l_tcp->numlayers; ++k) {
6047 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6049 ((*l_rates) * l_bits_empty)
6065 for (i=0;i<l_cp->th;++i) {
6066 for (j=0;j<l_cp->tw;++j) {
6067 l_rates = l_tcp->rates;
6070 *l_rates -= l_sot_remove;
6072 if (*l_rates < 30) {
6079 l_last_res = l_tcp->numlayers - 1;
6081 for (k = 1; k < l_last_res; ++k) {
6084 *l_rates -= l_sot_remove;
6086 if (*l_rates < *(l_rates - 1) + 10) {
6087 *l_rates = (*(l_rates - 1)) + 20;
6095 *l_rates -= (l_sot_remove + 2.f);
6097 if (*l_rates < *(l_rates - 1) + 10) {
6098 *l_rates = (*(l_rates - 1)) + 20;
6106 l_img_comp = l_image->comps;
6109 for (i=0;i<l_image->numcomps;++i) {
6110 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
6112 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
6120 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
6122 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
6124 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
6125 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
6126 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
6127 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
6131 if (l_cp->m_specific_param.m_enc.m_cinema) {
6132 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
6133 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
6134 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6138 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
6139 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
6145 static void j2k_read_eoc(opj_j2k_t *j2k) {
6147 opj_bool success = OPJ_FALSE;
6149 /* if packets should be decoded */
6150 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
6151 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
6152 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
6153 for (i = 0; i < j2k->cp->tileno_size; i++) {
6154 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
6155 if (j2k->cp->tileno[i] != -1)
6157 tileno = j2k->cp->tileno[i];
6158 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
6159 opj_free(j2k->tile_data[tileno]);
6160 j2k->tile_data[tileno] = NULL;
6161 tcd_free_decode_tile(tcd, i);
6164 success = OPJ_FALSE;
6165 if (success == OPJ_FALSE) {
6166 j2k->state |= J2K_STATE_ERR;
6170 tcd_free_decode(tcd);
6173 /* if packets should not be decoded */
6175 for (i = 0; i < j2k->cp->tileno_size; i++) {
6176 tileno = j2k->cp->tileno[i];
6177 opj_free(j2k->tile_data[tileno]);
6178 j2k->tile_data[tileno] = NULL;
6181 if (j2k->state & J2K_STATE_ERR)
6182 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
6184 j2k->state = J2K_STATE_MT;
6188 * Reads a EOC marker (End Of Codestream)
6190 * @param p_header_data the data contained in the SOD box.
6191 * @param p_j2k the jpeg2000 codec.
6192 * @param p_header_size the size of the data contained in the SOD marker.
6193 * @param p_manager the user event manager.
6196 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
6197 struct opj_stream_private *p_stream,
6198 struct opj_event_mgr * p_manager )
6201 opj_tcd_v2_t * l_tcd = 00;
6202 OPJ_UINT32 l_nb_tiles;
6203 opj_tcp_v2_t * l_tcp = 00;
6207 assert(p_j2k != 00);
6208 assert(p_manager != 00);
6209 assert(p_stream != 00);
6211 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6212 l_tcp = p_j2k->m_cp.tcps;
6214 l_tcd = tcd_create_v2(OPJ_TRUE);
6216 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6220 for (i = 0; i < l_nb_tiles; ++i) {
6221 if (l_tcp->m_data) {
6222 if (! tcd_init_decode_tile(l_tcd, i)) {
6223 tcd_destroy_v2(l_tcd);
6224 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6228 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
6232 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
6237 j2k_tcp_destroy(l_tcp);
6241 tcd_destroy_v2(l_tcd);
6247 * Gets the offset of the header.
6249 * @param p_stream the stream to write data to.
6250 * @param p_j2k J2K codec.
6251 * @param p_manager the user event manager.
6253 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
6254 struct opj_stream_private *p_stream,
6255 struct opj_event_mgr * p_manager )
6258 assert(p_j2k != 00);
6259 assert(p_manager != 00);
6260 assert(p_stream != 00);
6262 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
6268 * Writes the MCT marker (Multiple Component Transform)
6270 * @param p_stream the stream to write data to.
6271 * @param p_j2k J2K codec.
6272 * @param p_manager the user event manager.
6274 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
6275 struct opj_stream_private *p_stream,
6276 struct opj_event_mgr * p_manager )
6279 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6280 opj_mct_data_t * l_mct_record;
6281 opj_tcp_v2_t * l_tcp;
6284 assert(p_j2k != 00);
6285 assert(p_stream != 00);
6286 assert(p_manager != 00);
6288 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
6292 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6293 l_mct_record = l_tcp->m_mct_records;
6295 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6297 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
6304 l_mcc_record = l_tcp->m_mcc_records;
6306 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6308 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6315 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6323 * Writes the image components.
6325 * @param p_stream the stream to write data to.
6326 * @param p_j2k J2K codec.
6327 * @param p_manager the user event manager.
6329 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6330 struct opj_stream_private *p_stream,
6331 struct opj_event_mgr * p_manager )
6336 assert(p_j2k != 00);
6337 assert(p_manager != 00);
6338 assert(p_stream != 00);
6340 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6342 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6346 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6355 * Writes regions of interests.
6357 * @param p_stream the stream to write data to.
6358 * @param p_j2k J2K codec.
6359 * @param p_manager the user event manager.
6361 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
6362 struct opj_stream_private *p_stream,
6363 struct opj_event_mgr * p_manager )
6366 const opj_tccp_t *l_tccp = 00;
6369 assert(p_j2k != 00);
6370 assert(p_manager != 00);
6371 assert(p_stream != 00);
6373 l_tccp = p_j2k->m_cp.tcps->tccps;
6375 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6376 if (l_tccp->roishift) {
6378 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6392 * @param p_stream the stream to write data to.
6393 * @param p_j2k J2K codec.
6394 * @param p_manager the user event manager.
6396 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6397 struct opj_stream_private *p_stream,
6398 struct opj_event_mgr * p_manager )
6400 opj_codestream_index_t * l_cstr_index = 00;
6403 assert(p_j2k != 00);
6404 assert(p_manager != 00);
6405 assert(p_stream != 00);
6407 l_cstr_index = p_j2k->cstr_index;
6409 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6411 /* The following adjustment is done to adjust the codestream size */
6412 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6413 /* the first bunch of bytes is not in the codestream */
6414 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6419 /* preparation of JPWL marker segments */
6423 /* encode according to JPWL */
6424 jpwl_encode(p_j2k, p_stream, image);
6428 assert( 0 && "TODO" );
6429 #endif /* USE_JPWL */
6434 typedef struct opj_dec_mstabent {
6437 /** value of the state when the marker can appear */
6439 /** action linked to the marker */
6440 void (*handler) (opj_j2k_t *j2k);
6441 } opj_dec_mstabent_t;
6443 opj_dec_mstabent_t j2k_dec_mstab[] = {
6444 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
6445 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
6446 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
6447 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6448 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
6449 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
6450 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6451 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6452 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6453 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6454 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6455 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6456 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6457 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6458 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6459 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6461 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6462 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6465 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6466 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6467 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6468 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6469 #endif /* USE_JPWL */
6471 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6472 {J2K_MS_INSEC, 0, j2k_read_insec},
6473 #endif /* USE_JPSEC */
6475 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6478 static void j2k_read_unk(opj_j2k_t *j2k) {
6479 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6482 if (j2k->cp->correct) {
6484 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6485 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6486 id = cio_read(j2k->cio, 2);
6487 opj_event_msg(j2k->cinfo, EVT_ERROR,
6488 "JPWL: really don't know this marker %x\n",
6491 opj_event_msg(j2k->cinfo, EVT_ERROR,
6492 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6495 /* OK, activate this at your own risk!!! */
6496 /* we look for the marker at the minimum hamming distance from this */
6497 while (j2k_dec_mstab[m].id) {
6499 /* 1's where they differ */
6500 tmp_id = j2k_dec_mstab[m].id ^ id;
6502 /* compute the hamming distance between our id and the current */
6504 for (i = 0; i < 16; i++) {
6505 if ((tmp_id >> i) & 0x0001) {
6510 /* if current distance is smaller, set the minimum */
6511 if (cur_dist < min_dist) {
6512 min_dist = cur_dist;
6513 min_id = j2k_dec_mstab[m].id;
6516 /* jump to the next marker */
6520 /* do we substitute the marker? */
6521 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6522 opj_event_msg(j2k->cinfo, EVT_ERROR,
6523 "- marker %x is at distance %d from the read %x\n",
6524 min_id, min_dist, id);
6525 opj_event_msg(j2k->cinfo, EVT_ERROR,
6526 "- trying to substitute in place and crossing fingers!\n");
6527 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6528 cio_write(j2k->cio, min_id, 2);
6531 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6536 #endif /* USE_JPWL */
6541 * Reads an unknown marker
6543 * @param p_stream the stream object to read from.
6544 * @param p_j2k the jpeg2000 codec.
6545 * @param p_manager the user event manager.
6547 * @return true if the marker could be deduced.
6549 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6550 struct opj_stream_private *p_stream,
6551 OPJ_UINT32 *output_marker,
6552 struct opj_event_mgr * p_manager
6555 OPJ_UINT32 l_unknown_marker;
6556 const opj_dec_memory_marker_handler_t * l_marker_handler;
6557 OPJ_UINT32 l_size_unk = 2;
6560 assert(p_j2k != 00);
6561 assert(p_manager != 00);
6562 assert(p_stream != 00);
6564 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6567 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6568 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6569 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6573 /* read 2 bytes as the new marker ID*/
6574 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6576 if (!(l_unknown_marker < 0xff00)) {
6578 /* Get the marker handler from the marker ID*/
6579 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6581 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6582 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6586 if (l_marker_handler->id != J2K_MS_UNK) {
6587 /* Add the marker to the codestream index*/
6588 if (l_marker_handler->id != J2K_MS_SOT)
6589 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6590 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6592 break; /* next marker is known and well located */
6600 *output_marker = l_marker_handler->id ;
6606 Read the lookup table containing all the marker, status and action
6607 @param id Marker value
6609 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6610 opj_dec_mstabent_t *e;
6611 for (e = j2k_dec_mstab; e->id != 0; e++) {
6620 * Writes the MCT marker (Multiple Component Transform)
6622 * @param p_stream the stream to write data to.
6623 * @param p_j2k J2K codec.
6624 * @param p_manager the user event manager.
6626 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6627 opj_mct_data_t * p_mct_record,
6628 struct opj_stream_private *p_stream,
6629 struct opj_event_mgr * p_manager )
6631 OPJ_UINT32 l_mct_size;
6632 OPJ_BYTE * l_current_data = 00;
6636 assert(p_j2k != 00);
6637 assert(p_manager != 00);
6638 assert(p_stream != 00);
6640 l_mct_size = 10 + p_mct_record->m_data_size;
6642 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6643 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6644 = (OPJ_BYTE*)opj_realloc(
6645 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6648 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6652 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6655 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6657 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6658 l_current_data += 2;
6660 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6661 l_current_data += 2;
6663 opj_write_bytes(l_current_data,0,2); /* Zmct */
6664 l_current_data += 2;
6666 /* only one marker atm */
6667 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6669 opj_write_bytes(l_current_data,l_tmp,2);
6670 l_current_data += 2;
6672 opj_write_bytes(l_current_data,0,2); /* Ymct */
6675 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6677 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) {
6685 * Reads a MCT marker (Multiple Component Transform)
6687 * @param p_header_data the data contained in the MCT box.
6688 * @param p_j2k the jpeg2000 codec.
6689 * @param p_header_size the size of the data contained in the MCT marker.
6690 * @param p_manager the user event manager.
6692 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6693 OPJ_BYTE * p_header_data,
6694 OPJ_UINT32 p_header_size,
6695 struct opj_event_mgr * p_manager )
6698 opj_tcp_v2_t *l_tcp = 00;
6701 opj_mct_data_t * l_mct_data;
6704 assert(p_header_data != 00);
6705 assert(p_j2k != 00);
6707 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6708 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6709 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6711 if (p_header_size < 2) {
6712 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6717 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6720 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6724 if(p_header_size <= 6) {
6725 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6729 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6730 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6733 l_indix = l_tmp & 0xff;
6734 l_mct_data = l_tcp->m_mct_records;
6736 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6737 if (l_mct_data->m_index == l_indix) {
6744 if (i == l_tcp->m_nb_mct_records) {
6745 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6746 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6748 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));
6749 if(! l_tcp->m_mct_records) {
6750 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6754 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6755 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6758 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6761 if (l_mct_data->m_data) {
6762 opj_free(l_mct_data->m_data);
6763 l_mct_data->m_data = 00;
6766 l_mct_data->m_index = l_indix;
6767 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6768 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6770 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6773 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6779 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6780 if (! l_mct_data->m_data) {
6781 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6784 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6786 l_mct_data->m_data_size = p_header_size;
6787 ++l_tcp->m_nb_mct_records;
6793 * Writes the MCC marker (Multiple Component Collection)
6795 * @param p_stream the stream to write data to.
6796 * @param p_j2k J2K codec.
6797 * @param p_manager the user event manager.
6799 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6800 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6801 struct opj_stream_private *p_stream,
6802 struct opj_event_mgr * p_manager )
6805 OPJ_UINT32 l_mcc_size;
6806 OPJ_BYTE * l_current_data = 00;
6807 OPJ_UINT32 l_nb_bytes_for_comp;
6812 assert(p_j2k != 00);
6813 assert(p_manager != 00);
6814 assert(p_stream != 00);
6816 if (p_mcc_record->m_nb_comps > 255 ) {
6817 l_nb_bytes_for_comp = 2;
6821 l_nb_bytes_for_comp = 1;
6825 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6826 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6828 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6829 = (OPJ_BYTE*)opj_realloc(
6830 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6832 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6836 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6839 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6841 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6842 l_current_data += 2;
6844 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6845 l_current_data += 2;
6848 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6849 l_current_data += 2;
6851 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6854 /* only one marker atm */
6855 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6858 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6861 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6864 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 */
6867 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6868 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6869 l_current_data+=l_nb_bytes_for_comp;
6872 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 */
6875 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6877 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6878 l_current_data+=l_nb_bytes_for_comp;
6881 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6883 if (p_mcc_record->m_decorrelation_array) {
6884 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6887 if (p_mcc_record->m_offset_array) {
6888 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6891 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6894 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) {
6902 * Reads a MCC marker (Multiple Component Collection)
6904 * @param p_header_data the data contained in the MCC box.
6905 * @param p_j2k the jpeg2000 codec.
6906 * @param p_header_size the size of the data contained in the MCC marker.
6907 * @param p_manager the user event manager.
6909 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6910 OPJ_BYTE * p_header_data,
6911 OPJ_UINT32 p_header_size,
6912 struct opj_event_mgr * p_manager )
6917 opj_tcp_v2_t * l_tcp;
6918 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6919 opj_mct_data_t * l_mct_data;
6920 OPJ_UINT32 l_nb_collections;
6921 OPJ_UINT32 l_nb_comps;
6922 OPJ_UINT32 l_nb_bytes_by_comp;
6926 assert(p_header_data != 00);
6927 assert(p_j2k != 00);
6928 assert(p_manager != 00);
6930 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6931 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6932 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6934 if (p_header_size < 2) {
6935 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6940 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6943 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6947 if (p_header_size < 7) {
6948 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6952 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6955 l_mcc_record = l_tcp->m_mcc_records;
6957 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6958 if (l_mcc_record->m_index == l_indix) {
6965 if (i == l_tcp->m_nb_mcc_records) {
6966 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6967 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6969 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6970 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6971 if (! l_tcp->m_mcc_records) {
6972 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6975 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6976 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));
6978 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6980 l_mcc_record->m_index = l_indix;
6982 /* only one marker atm */
6983 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6986 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6990 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6993 if (l_nb_collections > 1) {
6994 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
7000 for (i=0;i<l_nb_collections;++i) {
7001 if (p_header_size < 3) {
7002 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7006 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
7010 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
7014 opj_read_bytes(p_header_data,&l_nb_comps,2);
7019 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7020 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
7022 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
7023 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7027 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
7029 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7030 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
7031 p_header_data+=l_nb_bytes_by_comp;
7034 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7039 opj_read_bytes(p_header_data,&l_nb_comps,2);
7042 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7043 l_nb_comps &= 0x7fff;
7045 if (l_nb_comps != l_mcc_record->m_nb_comps) {
7046 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
7050 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
7051 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7055 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
7057 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7058 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
7059 p_header_data+=l_nb_bytes_by_comp;
7062 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7067 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
7070 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
7071 l_mcc_record->m_decorrelation_array = 00;
7072 l_mcc_record->m_offset_array = 00;
7074 l_indix = l_tmp & 0xff;
7076 l_mct_data = l_tcp->m_mct_records;
7077 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7078 if (l_mct_data->m_index == l_indix) {
7079 l_mcc_record->m_decorrelation_array = l_mct_data;
7085 if (l_mcc_record->m_decorrelation_array == 00) {
7086 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7091 l_indix = (l_tmp >> 8) & 0xff;
7093 l_mct_data = l_tcp->m_mct_records;
7094 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7095 if (l_mct_data->m_index == l_indix) {
7096 l_mcc_record->m_offset_array = l_mct_data;
7102 if (l_mcc_record->m_offset_array == 00) {
7103 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7109 if (p_header_size != 0) {
7110 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7114 ++l_tcp->m_nb_mcc_records;
7121 * Writes the MCO marker (Multiple component transformation ordering)
7123 * @param p_stream the stream to write data to.
7124 * @param p_j2k J2K codec.
7125 * @param p_manager the user event manager.
7127 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
7128 struct opj_stream_private *p_stream,
7129 struct opj_event_mgr * p_manager
7132 OPJ_BYTE * l_current_data = 00;
7133 OPJ_UINT32 l_mco_size;
7134 opj_tcp_v2_t * l_tcp = 00;
7135 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7139 assert(p_j2k != 00);
7140 assert(p_manager != 00);
7141 assert(p_stream != 00);
7143 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
7144 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7146 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
7147 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7149 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7150 = (OPJ_BYTE*)opj_realloc(
7151 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7153 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
7158 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
7161 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
7162 l_current_data += 2;
7164 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
7165 l_current_data += 2;
7167 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
7170 l_mcc_record = l_tcp->m_mcc_records;
7171 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
7172 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
7178 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) {
7186 * Reads a MCO marker (Multiple Component Transform Ordering)
7188 * @param p_header_data the data contained in the MCO box.
7189 * @param p_j2k the jpeg2000 codec.
7190 * @param p_header_size the size of the data contained in the MCO marker.
7191 * @param p_manager the user event manager.
7193 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
7194 OPJ_BYTE * p_header_data,
7195 OPJ_UINT32 p_header_size,
7196 struct opj_event_mgr * p_manager )
7198 OPJ_UINT32 l_tmp, i;
7199 OPJ_UINT32 l_nb_stages;
7200 opj_tcp_v2_t * l_tcp;
7201 opj_tccp_t * l_tccp;
7202 opj_image_t * l_image;
7203 opj_image_comp_t * l_img_comp;
7206 assert(p_header_data != 00);
7207 assert(p_j2k != 00);
7208 assert(p_manager != 00);
7210 l_image = p_j2k->m_private_image;
7211 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7212 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7213 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7215 if (p_header_size < 1) {
7216 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
7220 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
7223 if (l_nb_stages > 1) {
7224 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
7228 if (p_header_size != l_nb_stages + 1) {
7229 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
7233 l_tccp = l_tcp->tccps;
7234 l_img_comp = l_image->comps;
7236 for (i=0;i<l_image->numcomps;++i) {
7237 l_tccp->m_dc_level_shift = 0;
7241 if (l_tcp->m_mct_decoding_matrix) {
7242 opj_free(l_tcp->m_mct_decoding_matrix);
7243 l_tcp->m_mct_decoding_matrix = 00;
7246 for (i=0;i<l_nb_stages;++i) {
7247 opj_read_bytes(p_header_data,&l_tmp,1);
7250 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
7258 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
7261 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7262 opj_mct_data_t * l_deco_array, * l_offset_array;
7263 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
7264 OPJ_UINT32 l_nb_elem;
7265 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
7266 opj_tccp_t * l_tccp;
7269 assert(p_tcp != 00);
7271 l_mcc_record = p_tcp->m_mcc_records;
7273 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
7274 if (l_mcc_record->m_index == p_index) {
7279 if (i==p_tcp->m_nb_mcc_records) {
7280 /** element discarded **/
7284 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
7285 /** do not support number of comps != image */
7289 l_deco_array = l_mcc_record->m_decorrelation_array;
7292 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
7293 if (l_deco_array->m_data_size != l_data_size) {
7297 l_nb_elem = p_image->numcomps * p_image->numcomps;
7298 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
7299 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7301 if (! p_tcp->m_mct_decoding_matrix ) {
7305 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);
7308 l_offset_array = l_mcc_record->m_offset_array;
7310 if (l_offset_array) {
7311 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
7312 if (l_offset_array->m_data_size != l_data_size) {
7316 l_nb_elem = p_image->numcomps;
7317 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7318 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7320 if (! l_offset_data ) {
7324 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7326 l_tccp = p_tcp->tccps;
7327 l_current_offset_data = l_offset_data;
7329 for (i=0;i<p_image->numcomps;++i) {
7330 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7334 opj_free(l_offset_data);
7341 * Writes the CBD marker (Component bit depth definition)
7343 * @param p_stream the stream to write data to.
7344 * @param p_j2k J2K codec.
7345 * @param p_manager the user event manager.
7347 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7348 struct opj_stream_private *p_stream,
7349 struct opj_event_mgr * p_manager )
7352 OPJ_UINT32 l_cbd_size;
7353 OPJ_BYTE * l_current_data = 00;
7354 opj_image_t *l_image = 00;
7355 opj_image_comp_t * l_comp = 00;
7358 assert(p_j2k != 00);
7359 assert(p_manager != 00);
7360 assert(p_stream != 00);
7362 l_image = p_j2k->m_private_image;
7363 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7365 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7366 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7367 = (OPJ_BYTE*)opj_realloc(
7368 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7371 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7375 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7378 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7380 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7381 l_current_data += 2;
7383 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7384 l_current_data += 2;
7386 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7389 l_comp = l_image->comps;
7391 for (i=0;i<l_image->numcomps;++i) {
7392 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7398 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) {
7406 * Reads a CBD marker (Component bit depth definition)
7407 * @param p_header_data the data contained in the CBD box.
7408 * @param p_j2k the jpeg2000 codec.
7409 * @param p_header_size the size of the data contained in the CBD marker.
7410 * @param p_manager the user event manager.
7412 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7413 OPJ_BYTE * p_header_data,
7414 OPJ_UINT32 p_header_size,
7415 struct opj_event_mgr * p_manager)
7417 OPJ_UINT32 l_nb_comp,l_num_comp;
7418 OPJ_UINT32 l_comp_def;
7420 opj_image_comp_t * l_comp = 00;
7423 assert(p_header_data != 00);
7424 assert(p_j2k != 00);
7425 assert(p_manager != 00);
7427 l_num_comp = p_j2k->m_private_image->numcomps;
7429 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7430 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7434 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7437 if (l_nb_comp != l_num_comp) {
7438 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7442 l_comp = p_j2k->m_private_image->comps;
7443 for (i=0;i<l_num_comp;++i) {
7444 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7446 l_comp->sgnd = (l_comp_def>>7) & 1;
7447 l_comp->prec = (l_comp_def&0x7f) + 1;
7455 /* ----------------------------------------------------------------------- */
7456 /* J2K / JPT decoder interface */
7457 /* ----------------------------------------------------------------------- */
7461 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7464 if(j2k->tile_len != NULL) {
7465 opj_free(j2k->tile_len);
7467 if(j2k->tile_data != NULL) {
7468 opj_free(j2k->tile_data);
7470 if(j2k->default_tcp != NULL) {
7471 opj_tcp_t *default_tcp = j2k->default_tcp;
7472 if(default_tcp->ppt_data_first != NULL) {
7473 opj_free(default_tcp->ppt_data_first);
7475 if(j2k->default_tcp->tccps != NULL) {
7476 opj_free(j2k->default_tcp->tccps);
7478 opj_free(j2k->default_tcp);
7480 if(j2k->cp != NULL) {
7481 opj_cp_t *cp = j2k->cp;
7482 if(cp->tcps != NULL) {
7483 for(i = 0; i < cp->tw * cp->th; i++) {
7484 if(cp->tcps[i].ppt_data_first != NULL) {
7485 opj_free(cp->tcps[i].ppt_data_first);
7487 if(cp->tcps[i].tccps != NULL) {
7488 opj_free(cp->tcps[i].tccps);
7493 if(cp->ppm_data_first != NULL) {
7494 opj_free(cp->ppm_data_first);
7496 if(cp->tileno != NULL) {
7497 opj_free(cp->tileno);
7499 if(cp->comment != NULL) {
7500 opj_free(cp->comment);
7509 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7511 if(j2k && parameters) {
7512 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7513 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7516 j2k->m_cp.correct = parameters->jpwl_correct;
7517 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7518 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7519 #endif /* USE_JPWL */
7526 * Read a JPT-stream and decode file
7529 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7530 opj_image_t *image = NULL;
7531 opj_jpt_msg_header_t header;
7533 opj_common_ptr cinfo = j2k->cinfo;
7535 OPJ_ARG_NOT_USED(cstr_info);
7539 /* create an empty image */
7540 image = opj_image_create0();
7543 j2k->state = J2K_STATE_MHSOC;
7545 /* Initialize the header */
7546 jpt_init_msg_header(&header);
7547 /* Read the first header of the message */
7548 jpt_read_msg_header(cinfo, cio, &header);
7550 position = cio_tell(cio);
7551 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7552 opj_image_destroy(image);
7553 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7558 opj_dec_mstabent_t *e = NULL;
7561 if (!cio_numbytesleft(cio)) {
7565 /* data-bin read -> need to read a new header */
7566 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7567 jpt_read_msg_header(cinfo, cio, &header);
7568 position = cio_tell(cio);
7569 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7570 opj_image_destroy(image);
7571 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7576 id = cio_read(cio, 2);
7577 if (id >> 8 != 0xff) {
7578 opj_image_destroy(image);
7579 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7582 e = j2k_dec_mstab_lookup(id);
7583 if (!(j2k->state & e->states)) {
7584 opj_image_destroy(image);
7585 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7591 if (j2k->state == J2K_STATE_MT) {
7594 if (j2k->state == J2K_STATE_NEOC) {
7598 if (j2k->state == J2K_STATE_NEOC) {
7602 if (j2k->state != J2K_STATE_MT) {
7603 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7609 /* ----------------------------------------------------------------------- */
7610 /* J2K encoder interface */
7611 /* ----------------------------------------------------------------------- */
7613 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7614 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7621 opj_j2k_v2_t* j2k_create_compress_v2(void)
7623 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7628 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7630 l_j2k->m_is_decoder = 0;
7631 l_j2k->m_cp.m_is_decoder = 0;
7633 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7634 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7639 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7641 /* validation list creation*/
7642 l_j2k->m_validation_list = opj_procedure_list_create();
7643 if (! l_j2k->m_validation_list) {
7648 /* execution list creation*/
7649 l_j2k->m_procedure_list = opj_procedure_list_create();
7650 if (! l_j2k->m_procedure_list) {
7658 void j2k_destroy_compress(opj_j2k_t *j2k) {
7662 if(j2k->cp != NULL) {
7663 opj_cp_t *cp = j2k->cp;
7666 opj_free(cp->comment);
7669 opj_free(cp->matrice);
7671 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7672 opj_free(cp->tcps[tileno].tccps);
7681 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7683 int tileno, numpocs_tile;
7684 opj_cp_t *cp = NULL;
7686 if(!j2k || !parameters || ! image) {
7690 /* create and initialize the coding parameters structure */
7691 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7693 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7696 /* set default values for cp */
7701 copy user encoding parameters
7703 cp->cinema = parameters->cp_cinema;
7704 cp->max_comp_size = parameters->max_comp_size;
7705 cp->rsiz = parameters->cp_rsiz;
7706 cp->disto_alloc = parameters->cp_disto_alloc;
7707 cp->fixed_alloc = parameters->cp_fixed_alloc;
7708 cp->fixed_quality = parameters->cp_fixed_quality;
7710 /* mod fixed_quality */
7711 if(parameters->cp_matrice) {
7712 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7713 cp->matrice = (int *) opj_malloc(array_size);
7714 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7718 cp->tdx = parameters->cp_tdx;
7719 cp->tdy = parameters->cp_tdy;
7722 cp->tx0 = parameters->cp_tx0;
7723 cp->ty0 = parameters->cp_ty0;
7725 /* comment string */
7726 if(parameters->cp_comment) {
7727 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7729 strcpy(cp->comment, parameters->cp_comment);
7734 calculate other encoding parameters
7737 if (parameters->tile_size_on) {
7738 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7739 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7741 cp->tdx = image->x1 - cp->tx0;
7742 cp->tdy = image->y1 - cp->ty0;
7745 if(parameters->tp_on){
7746 cp->tp_flag = parameters->tp_flag;
7751 for(i=0;i<image->numcomps ;i++){
7752 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7758 calculate JPWL encoding parameters
7761 if (parameters->jpwl_epc_on) {
7765 cp->epc_on = OPJ_TRUE;
7766 cp->info_on = OPJ_FALSE; /* no informative technique */
7769 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7770 cp->epb_on = OPJ_TRUE;
7772 cp->hprot_MH = parameters->jpwl_hprot_MH;
7773 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7774 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7775 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7777 /* if tile specs are not specified, copy MH specs */
7778 if (cp->hprot_TPH[0] == -1) {
7779 cp->hprot_TPH_tileno[0] = 0;
7780 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7782 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7783 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7784 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7785 cp->pprot[i] = parameters->jpwl_pprot[i];
7789 /* set ESD writing */
7790 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7791 cp->esd_on = OPJ_TRUE;
7793 cp->sens_size = parameters->jpwl_sens_size;
7794 cp->sens_addr = parameters->jpwl_sens_addr;
7795 cp->sens_range = parameters->jpwl_sens_range;
7797 cp->sens_MH = parameters->jpwl_sens_MH;
7798 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7799 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7800 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7804 /* always set RED writing to false: we are at the encoder */
7805 cp->red_on = OPJ_FALSE;
7808 cp->epc_on = OPJ_FALSE;
7810 #endif /* USE_JPWL */
7813 /* initialize the mutiple tiles */
7814 /* ---------------------------- */
7815 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7817 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7818 opj_tcp_t *tcp = &cp->tcps[tileno];
7819 tcp->numlayers = parameters->tcp_numlayers;
7820 assert ( tcp->numlayers >= 0 );
7821 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7823 if (cp->fixed_quality) {
7824 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7826 tcp->rates[j] = parameters->tcp_rates[j];
7828 if (cp->fixed_quality) { /* add fixed_quality */
7829 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7831 tcp->rates[j] = parameters->tcp_rates[j];
7835 tcp->csty = parameters->csty;
7836 tcp->prg = parameters->prog_order;
7837 tcp->mct = parameters->tcp_mct;
7841 if (parameters->numpocs) {
7842 /* initialisation of POC */
7844 assert( parameters->numpocs >= 0 );
7845 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7846 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7847 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7848 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7849 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7850 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7851 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7852 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7853 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7854 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7858 tcp->numpocs = numpocs_tile -1 ;
7863 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7865 for (i = 0; i < image->numcomps; i++) {
7866 opj_tccp_t *tccp = &tcp->tccps[i];
7867 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7868 tccp->numresolutions = parameters->numresolution;
7869 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7870 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7871 tccp->cblksty = parameters->mode;
7872 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7873 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7875 assert(parameters->roi_compno >= 0);
7876 if (i == (OPJ_UINT32)parameters->roi_compno) {
7877 tccp->roishift = parameters->roi_shift;
7882 if(parameters->cp_cinema)
7884 /*Precinct size for lowest frequency subband=128*/
7887 /*Precinct size at all other resolutions = 256*/
7888 for (j = 1; j < tccp->numresolutions; j++) {
7893 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7895 assert(tccp->numresolutions > 0);
7896 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7897 if (p < parameters->res_spec) {
7899 if (parameters->prcw_init[p] < 1) {
7902 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7905 if (parameters->prch_init[p] < 1) {
7908 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7912 int res_spec = parameters->res_spec;
7913 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7914 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7916 if (size_prcw < 1) {
7919 tccp->prcw[j] = int_floorlog2(size_prcw);
7922 if (size_prch < 1) {
7925 tccp->prch[j] = int_floorlog2(size_prch);
7929 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7932 for (j = 0; j < tccp->numresolutions; j++) {
7939 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7944 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7945 opj_cparameters_t *parameters,
7947 struct opj_event_mgr * p_manager)
7949 OPJ_UINT32 i, j, tileno, numpocs_tile;
7950 opj_cp_v2_t *cp = 00;
7953 if(!p_j2k || !parameters || ! image) {
7957 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7958 cp = &(p_j2k->m_cp);
7960 /* set default values for cp */
7965 copy user encoding parameters
7967 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7968 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7969 cp->rsiz = parameters->cp_rsiz;
7970 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7971 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7972 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7974 /* mod fixed_quality */
7975 if (parameters->cp_matrice) {
7976 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7977 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7978 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7982 cp->tdx = parameters->cp_tdx;
7983 cp->tdy = parameters->cp_tdy;
7986 cp->tx0 = parameters->cp_tx0;
7987 cp->ty0 = parameters->cp_ty0;
7989 /* comment string */
7990 if(parameters->cp_comment) {
7991 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7993 strcpy(cp->comment, parameters->cp_comment);
7998 calculate other encoding parameters
8001 if (parameters->tile_size_on) {
8002 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
8003 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8005 cp->tdx = image->x1 - cp->tx0;
8006 cp->tdy = image->y1 - cp->ty0;
8009 if (parameters->tp_on) {
8010 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
8011 cp->m_specific_param.m_enc.m_tp_on = 1;
8016 calculate JPWL encoding parameters
8019 if (parameters->jpwl_epc_on) {
8023 cp->epc_on = OPJ_TRUE;
8024 cp->info_on = OPJ_FALSE; /* no informative technique */
8027 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8028 cp->epb_on = OPJ_TRUE;
8030 cp->hprot_MH = parameters->jpwl_hprot_MH;
8031 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8032 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8033 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8035 /* if tile specs are not specified, copy MH specs */
8036 if (cp->hprot_TPH[0] == -1) {
8037 cp->hprot_TPH_tileno[0] = 0;
8038 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8040 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8041 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8042 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8043 cp->pprot[i] = parameters->jpwl_pprot[i];
8047 /* set ESD writing */
8048 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8049 cp->esd_on = OPJ_TRUE;
8051 cp->sens_size = parameters->jpwl_sens_size;
8052 cp->sens_addr = parameters->jpwl_sens_addr;
8053 cp->sens_range = parameters->jpwl_sens_range;
8055 cp->sens_MH = parameters->jpwl_sens_MH;
8056 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8057 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8058 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8062 /* always set RED writing to false: we are at the encoder */
8063 cp->red_on = OPJ_FALSE;
8066 cp->epc_on = OPJ_FALSE;
8068 #endif /* USE_JPWL */
8071 /* initialize the mutiple tiles */
8072 /* ---------------------------- */
8073 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
8074 if (parameters->numpocs) {
8075 /* initialisation of POC */
8076 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
8080 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8081 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
8082 tcp->numlayers = parameters->tcp_numlayers;
8084 for (j = 0; j < tcp->numlayers; j++) {
8085 if(cp->m_specific_param.m_enc.m_cinema){
8086 if (cp->m_specific_param.m_enc.m_fixed_quality) {
8087 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8089 tcp->rates[j] = parameters->tcp_rates[j];
8091 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
8092 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8094 tcp->rates[j] = parameters->tcp_rates[j];
8099 tcp->csty = parameters->csty;
8100 tcp->prg = parameters->prog_order;
8101 tcp->mct = parameters->tcp_mct;
8106 if (parameters->numpocs) {
8107 /* initialisation of POC */
8110 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
8111 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8112 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8114 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
8115 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
8116 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
8117 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
8118 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
8119 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
8120 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
8126 tcp->numpocs = numpocs_tile -1 ;
8131 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8133 if (parameters->mct_data) {
8135 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
8138 /* TODO MSD : merge v2 add invert.c or used a external lib ?
8139 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
8140 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8141 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
8144 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8145 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
8146 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
8148 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8149 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
8151 tcp->mct_norms = (OPJ_FLOAT64*)
8152 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
8154 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
8157 for (i = 0; i < image->numcomps; i++) {
8158 opj_tccp_t *tccp = &tcp->tccps[i];
8159 tccp->m_dc_level_shift = l_dc_shift[i];
8162 j2k_setup_mct_encoding(tcp,image);
8166 for (i = 0; i < image->numcomps; i++) {
8167 opj_tccp_t *tccp = &tcp->tccps[i];
8168 opj_image_comp_t * l_comp = &(image->comps[i]);
8170 if (! l_comp->sgnd) {
8171 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
8176 for (i = 0; i < image->numcomps; i++) {
8177 opj_tccp_t *tccp = &tcp->tccps[i];
8179 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
8180 tccp->numresolutions = parameters->numresolution;
8181 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8182 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8183 tccp->cblksty = parameters->mode;
8184 tccp->qmfbid = parameters->irreversible ? 0 : 1;
8185 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8188 if (i == parameters->roi_compno) {
8189 tccp->roishift = parameters->roi_shift;
8194 if(parameters->cp_cinema) {
8195 //Precinct size for lowest frequency subband=128
8198 //Precinct size at all other resolutions = 256
8199 for (j = 1; j < tccp->numresolutions; j++) {
8204 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8205 OPJ_INT32 p = 0, it_res;
8206 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
8207 if (p < parameters->res_spec) {
8209 if (parameters->prcw_init[p] < 1) {
8210 tccp->prcw[it_res] = 1;
8212 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
8215 if (parameters->prch_init[p] < 1) {
8216 tccp->prch[it_res] = 1;
8218 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
8222 int res_spec = parameters->res_spec;
8223 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8224 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8226 if (size_prcw < 1) {
8227 tccp->prcw[it_res] = 1;
8229 tccp->prcw[it_res] = int_floorlog2(size_prcw);
8232 if (size_prch < 1) {
8233 tccp->prch[it_res] = 1;
8235 tccp->prch[it_res] = int_floorlog2(size_prch);
8239 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
8242 for (j = 0; j < tccp->numresolutions; j++) {
8249 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8253 if (parameters->mct_data) {
8254 opj_free(parameters->mct_data);
8255 parameters->mct_data = 00;
8260 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
8263 opj_cp_t *cp = NULL;
8265 opj_tcd_t *tcd = NULL; /* TCD component */
8273 j2k->cstr_info = cstr_info;
8276 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
8277 cstr_info->image_w = image->x1 - image->x0;
8278 cstr_info->image_h = image->y1 - image->y0;
8279 cstr_info->prog = (&cp->tcps[0])->prg;
8280 cstr_info->tw = cp->tw;
8281 cstr_info->th = cp->th;
8282 cstr_info->tile_x = cp->tdx; /* new version parser */
8283 cstr_info->tile_y = cp->tdy; /* new version parser */
8284 cstr_info->tile_Ox = cp->tx0; /* new version parser */
8285 cstr_info->tile_Oy = cp->ty0; /* new version parser */
8286 cstr_info->numcomps = image->numcomps;
8287 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
8288 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
8289 for (compno=0; compno < image->numcomps; compno++) {
8290 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
8292 cstr_info->D_max = 0.0; /* ADD Marcela */
8293 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
8294 cstr_info->maxmarknum = 100;
8295 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
8296 cstr_info->marknum = 0;
8306 for (compno = 1; compno < image->numcomps; compno++) {
8307 j2k_write_coc(j2k, compno);
8308 j2k_write_qcc(j2k, compno);
8312 for (compno = 0; compno < image->numcomps; compno++) {
8313 opj_tcp_t *tcp = &cp->tcps[0];
8314 if (tcp->tccps[compno].roishift)
8315 j2k_write_rgn(j2k, compno, 0);
8317 if (cp->comment != NULL) {
8321 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
8325 if (cp->cinema == CINEMA4K_24) {
8330 /* uncomment only for testing JPSEC marker writing */
8331 /* j2k_write_sec(j2k); */
8335 cstr_info->main_head_end = cio_tell(cio) - 1;
8338 /**** Main Header ENDS here ***/
8340 /* create the tile encoder */
8341 tcd = tcd_create(j2k->cinfo);
8343 /* encode each tile */
8344 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8348 int acc_pack_num = 0;
8352 opj_tcp_t *tcp = &cp->tcps[tileno];
8353 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8355 j2k->curtileno = tileno;
8356 j2k->cur_tp_num = 0;
8357 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8358 /* initialisation before tile encoding */
8360 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8362 tcd_init_encode(tcd, image, cp, j2k->curtileno);
8367 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8368 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8369 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8370 cstr_info->tile[j2k->curtileno].marknum = 0;
8374 for(pino = 0; pino <= tcp->numpocs; pino++) {
8378 /*Get number of tile parts*/
8379 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8380 tcd->tp_pos = cp->tp_pos;
8382 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8383 j2k->tp_num = tilepartno;
8386 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8387 cio_tell(cio) + j2k->pos_correction;
8391 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8392 for (compno = 1; compno < image->numcomps; compno++) {
8393 j2k_write_coc(j2k, compno);
8394 j2k_write_qcc(j2k, compno);
8396 if (cp->tcps[tileno].numpocs) {
8403 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8404 cio_tell(cio) + j2k->pos_correction + 1;
8407 j2k_write_sod(j2k, tcd);
8411 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8412 cio_tell(cio) + j2k->pos_correction - 1;
8413 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8415 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8416 cstr_info->packno - acc_pack_num;
8417 acc_pack_num = cstr_info->packno;
8425 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8430 if (tile->PPT) { // BAD PPT !!!
8433 PPT_file=fopen("PPT","rb");
8434 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8435 for (i=0;i<tile->len_ppt;i++) {
8437 fread(&elmt, 1, 1, PPT_file);
8438 fwrite(&elmt,1,1,f);
8447 /* destroy the tile encoder */
8448 tcd_free_encode(tcd);
8451 opj_free(j2k->cur_totnum_tp);
8456 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8458 /* The following adjustment is done to adjust the codestream size */
8459 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8460 /* the first bunch of bytes is not in the codestream */
8461 cstr_info->codestream_size -= cstr_info->main_head_start;
8467 preparation of JPWL marker segments
8471 /* encode according to JPWL */
8472 jpwl_encode(j2k, cio, image);
8475 #endif /* USE_JPWL */
8480 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8482 assert(cstr_info != 00);
8484 /* expand the list? */
8485 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8486 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8487 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8490 /* add the marker */
8491 cstr_info->marker[cstr_info->marknum].type = type;
8492 cstr_info->marker[cstr_info->marknum].pos = pos;
8493 cstr_info->marker[cstr_info->marknum].len = len;
8494 cstr_info->marknum++;
8498 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8500 assert(cstr_index != 00);
8502 /* expand the list? */
8503 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8504 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8505 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8508 /* add the marker */
8509 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8510 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8511 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8512 cstr_index->marknum++;
8516 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8518 opj_marker_info_t *marker;
8520 assert(cstr_info != 00);
8522 /* expand the list? */
8523 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8524 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8525 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8528 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8530 /* add the marker */
8531 marker->type = type;
8534 cstr_info->tile[tileno].marknum++;
8537 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)
8539 assert(cstr_index != 00);
8540 assert(cstr_index->tile_index != 00);
8542 /* expand the list? */
8543 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8544 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8545 cstr_index->tile_index[tileno].marker =
8546 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8547 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8550 /* add the marker */
8551 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8552 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8553 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8554 cstr_index->tile_index[tileno].marknum++;
8556 if (type == J2K_MS_SOT) {
8557 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8559 if (cstr_index->tile_index[tileno].tp_index)
8560 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8567 * -----------------------------------------------------------------------
8568 * -----------------------------------------------------------------------
8569 * -----------------------------------------------------------------------
8573 * Ends the decompression procedures and possibiliy add data to be read after the
8576 opj_bool j2k_end_decompress(
8577 opj_j2k_v2_t *p_j2k,
8578 opj_stream_private_t *p_stream,
8579 opj_event_mgr_t * p_manager)
8588 * Reads a jpeg2000 codestream header structure.
8591 * @param p_stream the stream to read data from.
8592 * @param p_j2k the jpeg2000 codec.
8593 * @param p_manager the user event manager.
8595 * @return true if the box is valid.
8597 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8598 opj_j2k_v2_t* p_j2k,
8599 opj_image_t** p_image,
8600 struct opj_event_mgr* p_manager )
8603 assert(p_j2k != 00);
8604 assert(p_stream != 00);
8605 assert(p_manager != 00);
8607 /* create an empty image header */
8608 p_j2k->m_private_image = opj_image_create0();
8609 if (! p_j2k->m_private_image) {
8613 /* customization of the validation */
8614 j2k_setup_decoding_validation(p_j2k);
8616 /* validation of the parameters codec */
8617 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8618 opj_image_destroy(p_j2k->m_private_image);
8619 p_j2k->m_private_image = NULL;
8623 /* customization of the encoding */
8624 j2k_setup_header_reading(p_j2k);
8627 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8628 opj_image_destroy(p_j2k->m_private_image);
8629 p_j2k->m_private_image = NULL;
8633 *p_image = opj_image_create0();
8638 /* Copy codestream image information to the output image */
8639 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8641 /*Allocate and initialize some elements of codestrem index*/
8642 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8650 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8652 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8655 assert(p_j2k != 00);
8657 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8659 /* DEVELOPER CORNER, add your custom procedures */
8660 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8665 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8666 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8668 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8671 assert(p_j2k != 00);
8673 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8674 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8675 /* DEVELOPER CORNER, add your custom validation procedure */
8681 * The mct encoding validation procedure.
8683 * @param p_j2k the jpeg2000 codec to validate.
8684 * @param p_stream the input stream to validate.
8685 * @param p_manager the user event manager.
8687 * @return true if the parameters are correct.
8689 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8690 opj_stream_private_t *p_stream,
8691 opj_event_mgr_t * p_manager )
8693 opj_bool l_is_valid = OPJ_TRUE;
8697 assert(p_j2k != 00);
8698 assert(p_stream != 00);
8699 assert(p_manager != 00);
8701 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8702 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8703 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8705 for (i=0;i<l_nb_tiles;++i) {
8706 if (l_tcp->mct == 2) {
8707 opj_tccp_t * l_tccp = l_tcp->tccps;
8708 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8710 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8711 l_is_valid &= ! (l_tccp->qmfbid & 1);
8722 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8725 OPJ_UINT32 l_indix = 1;
8726 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8727 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8728 OPJ_UINT32 l_mct_size,l_nb_elem;
8729 OPJ_FLOAT32 * l_data, * l_current_data;
8730 opj_tccp_t * l_tccp;
8733 assert(p_tcp != 00);
8735 if (p_tcp->mct != 2) {
8739 if (p_tcp->m_mct_decoding_matrix) {
8740 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8741 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8743 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));
8744 if (! p_tcp->m_mct_records) {
8747 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8749 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8751 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8753 if (l_mct_deco_data->m_data) {
8754 opj_free(l_mct_deco_data->m_data);
8755 l_mct_deco_data->m_data = 00;
8758 l_mct_deco_data->m_index = l_indix++;
8759 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8760 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8761 l_nb_elem = p_image->numcomps * p_image->numcomps;
8762 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8763 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8765 if (! l_mct_deco_data->m_data) {
8769 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);
8771 l_mct_deco_data->m_data_size = l_mct_size;
8772 ++p_tcp->m_nb_mct_records;
8775 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8776 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8777 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));
8779 if (! p_tcp->m_mct_records) {
8783 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8784 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8786 if (l_mct_deco_data) {
8787 l_mct_deco_data = l_mct_offset_data - 1;
8791 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8793 if (l_mct_offset_data->m_data) {
8794 opj_free(l_mct_offset_data->m_data);
8795 l_mct_offset_data->m_data = 00;
8798 l_mct_offset_data->m_index = l_indix++;
8799 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8800 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8801 l_nb_elem = p_image->numcomps;
8802 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8803 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8805 if (! l_mct_offset_data->m_data) {
8809 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8811 opj_free(l_mct_offset_data->m_data);
8812 l_mct_offset_data->m_data = 00;
8816 l_tccp = p_tcp->tccps;
8817 l_current_data = l_data;
8819 for (i=0;i<l_nb_elem;++i) {
8820 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8824 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8828 l_mct_offset_data->m_data_size = l_mct_size;
8830 ++p_tcp->m_nb_mct_records;
8832 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8833 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8834 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8835 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8837 if (! p_tcp->m_mcc_records) {
8840 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8841 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));
8845 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8846 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8847 l_mcc_data->m_is_irreversible = 1;
8848 l_mcc_data->m_nb_comps = p_image->numcomps;
8849 l_mcc_data->m_index = l_indix++;
8850 l_mcc_data->m_offset_array = l_mct_offset_data;
8851 ++p_tcp->m_nb_mcc_records;
8857 * Builds the cp decoder parameters to use to decode tile.
8859 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8860 opj_stream_private_t *p_stream,
8861 opj_event_mgr_t * p_manager )
8863 /* add here initialization of cp
8864 copy paste of setup_decoder */
8872 * Builds the cp encoder parameters to use to encode tile.
8874 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8875 opj_stream_private_t *p_stream,
8876 opj_event_mgr_t * p_manager )
8878 /* add here initialization of cp
8879 copy paste of setup_encoder */
8887 * The default encoding validation procedure without any extension.
8889 * @param p_j2k the jpeg2000 codec to validate.
8890 * @param p_stream the input stream to validate.
8891 * @param p_manager the user event manager.
8893 * @return true if the parameters are correct.
8895 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8896 opj_stream_private_t *p_stream,
8897 opj_event_mgr_t * p_manager )
8899 opj_bool l_is_valid = OPJ_TRUE;
8902 assert(p_j2k != 00);
8903 assert(p_stream != 00);
8904 assert(p_manager != 00);
8906 /* STATE checking */
8907 /* make sure the state is at 0 */
8908 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8910 /* POINTER validation */
8911 /* make sure a p_j2k codec is present */
8912 l_is_valid &= (p_j2k->m_procedure_list != 00);
8913 /* make sure a validation list is present */
8914 l_is_valid &= (p_j2k->m_validation_list != 00);
8916 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8917 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8921 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8922 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8926 /* PARAMETER VALIDATION */
8931 * The default decoding validation procedure without any extension.
8933 * @param p_j2k the jpeg2000 codec to validate.
8934 * @param p_stream the input stream to validate.
8935 * @param p_manager the user event manager.
8937 * @return true if the parameters are correct.
8939 opj_bool j2k_decoding_validation (
8940 opj_j2k_v2_t *p_j2k,
8941 opj_stream_private_t *p_stream,
8942 opj_event_mgr_t * p_manager
8945 opj_bool l_is_valid = OPJ_TRUE;
8948 assert(p_j2k != 00);
8949 assert(p_stream != 00);
8950 assert(p_manager != 00);
8953 /* STATE checking */
8954 /* make sure the state is at 0 */
8956 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8958 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8960 /* POINTER validation */
8961 /* make sure a p_j2k codec is present */
8962 /* make sure a procedure list is present */
8963 l_is_valid &= (p_j2k->m_procedure_list != 00);
8964 /* make sure a validation list is present */
8965 l_is_valid &= (p_j2k->m_validation_list != 00);
8967 /* PARAMETER VALIDATION */
8971 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8972 struct opj_stream_private *p_stream,
8973 struct opj_event_mgr * p_manager)
8975 OPJ_UINT32 l_current_marker;
8976 OPJ_UINT32 l_marker_size;
8977 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8980 assert(p_stream != 00);
8981 assert(p_j2k != 00);
8982 assert(p_manager != 00);
8984 /* We enter in the main header */
8985 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8987 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8988 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8989 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8993 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8994 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8995 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8999 /* Read 2 bytes as the new marker ID */
9000 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9002 /* Try to read until the SOT is detected */
9003 while (l_current_marker != J2K_MS_SOT) {
9005 /* Check if the current marker ID is valid */
9006 if (l_current_marker < 0xff00) {
9007 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
9011 /* Get the marker handler from the marker ID */
9012 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9014 /* Manage case where marker is unknown */
9015 if (l_marker_handler->id == J2K_MS_UNK) {
9016 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
9017 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
9021 if (l_current_marker == J2K_MS_SOT)
9022 break; /* SOT marker is detected main header is completely read */
9023 else /* Get the marker handler from the marker ID */
9024 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9027 /* Check if the marker is known and if it is the right place to find it */
9028 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9029 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9033 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9034 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9035 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9039 /* read 2 bytes as the marker size */
9040 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9041 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9043 /* Check if the marker size is compatible with the header data size */
9044 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9045 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9046 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9047 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9050 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9053 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9054 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) {
9055 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9059 /* Read the marker segment with the correct marker handler */
9060 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9061 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
9065 /* Add the marker to the codestream index*/
9066 j2k_add_mhmarker_v2(p_j2k->cstr_index,
9067 l_marker_handler->id,
9068 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9069 l_marker_size + 4 );
9071 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9072 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9073 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9077 /* read 2 bytes as the new marker ID */
9078 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9081 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
9083 /* Position of the last element if the main header */
9084 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
9086 /* Next step: read a tile-part header */
9087 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9093 * Excutes the given procedures on the given codec.
9095 * @param p_procedure_list the list of procedures to execute
9096 * @param p_j2k the jpeg2000 codec to execute the procedures on.
9097 * @param p_stream the stream to execute the procedures on.
9098 * @param p_manager the user manager.
9100 * @return true if all the procedures were successfully executed.
9102 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
9103 opj_procedure_list_t * p_procedure_list,
9104 opj_stream_private_t *p_stream,
9105 opj_event_mgr_t * p_manager )
9107 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
9108 opj_bool l_result = OPJ_TRUE;
9109 OPJ_UINT32 l_nb_proc, i;
9112 assert(p_procedure_list != 00);
9113 assert(p_j2k != 00);
9114 assert(p_stream != 00);
9115 assert(p_manager != 00);
9118 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
9119 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);
9121 for (i=0;i<l_nb_proc;++i) {
9122 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
9126 /* and clear the procedure list at the end.*/
9127 opj_procedure_list_clear(p_procedure_list);
9132 opj_bool j2k_copy_default_tcp_and_create_tcd
9134 opj_j2k_v2_t * p_j2k,
9135 opj_stream_private_t *p_stream,
9136 opj_event_mgr_t * p_manager
9139 opj_tcp_v2_t * l_tcp = 00;
9140 opj_tcp_v2_t * l_default_tcp = 00;
9141 OPJ_UINT32 l_nb_tiles;
9143 opj_tccp_t *l_current_tccp = 00;
9144 OPJ_UINT32 l_tccp_size;
9145 OPJ_UINT32 l_mct_size;
9146 opj_image_t * l_image;
9147 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
9148 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
9149 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
9150 OPJ_UINT32 l_offset;
9153 assert(p_j2k != 00);
9154 assert(p_stream != 00);
9155 assert(p_manager != 00);
9157 l_image = p_j2k->m_private_image;
9158 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9159 l_tcp = p_j2k->m_cp.tcps;
9160 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
9161 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9162 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
9165 for (i=0; i<l_nb_tiles; ++i) {
9166 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
9167 l_current_tccp = l_tcp->tccps;
9168 /*Copy default coding parameters into the current tile coding parameters*/
9169 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
9170 /* Initialize some values of the current tile coding parameters*/
9172 l_tcp->ppt_data = 00;
9173 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
9174 l_tcp->tccps = l_current_tccp;
9176 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
9177 if (l_default_tcp->m_mct_decoding_matrix) {
9178 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
9179 if (! l_tcp->m_mct_decoding_matrix ) {
9182 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
9185 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
9186 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
9187 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
9188 if (! l_tcp->m_mct_records) {
9191 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
9193 /* Copy the mct record data from dflt_tile_cp to the current tile*/
9194 l_src_mct_rec = l_default_tcp->m_mct_records;
9195 l_dest_mct_rec = l_tcp->m_mct_records;
9197 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
9199 if (l_src_mct_rec->m_data) {
9201 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
9202 if(! l_dest_mct_rec->m_data) {
9205 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
9212 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
9213 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
9214 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
9215 if (! l_tcp->m_mcc_records) {
9218 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
9220 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
9221 l_src_mcc_rec = l_default_tcp->m_mcc_records;
9222 l_dest_mcc_rec = l_tcp->m_mcc_records;
9224 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
9226 if (l_src_mcc_rec->m_decorrelation_array) {
9227 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
9228 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
9231 if (l_src_mcc_rec->m_offset_array) {
9232 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
9233 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
9240 /* Copy all the dflt_tile_compo_cp to the current tile cp */
9241 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
9243 /* Move to next tile cp*/
9247 /* Create the current tile decoder*/
9248 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
9249 if (! p_j2k->m_tcd ) {
9253 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
9254 tcd_destroy_v2(p_j2k->m_tcd);
9256 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9264 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
9265 * with the marker value.
9266 * @param p_id Marker value to look up
9268 * @return the handler associated with the id.
9270 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
9272 const opj_dec_memory_marker_handler_t *e;
9273 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
9274 if (e->id == p_id) {
9275 break; /* we find a handler corresponding to the marker ID*/
9283 * Destroys a jpeg2000 codec.
9285 * @param p_j2k the jpeg20000 structure to destroy.
9287 void j2k_destroy (opj_j2k_v2_t *p_j2k)
9293 if (p_j2k->m_is_decoder) {
9295 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
9296 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9297 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9298 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
9301 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
9302 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
9303 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
9304 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
9309 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
9310 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9311 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
9314 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
9315 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
9316 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
9317 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
9320 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
9321 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
9322 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
9323 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
9327 tcd_destroy_v2(p_j2k->m_tcd);
9329 j2k_cp_destroy(&(p_j2k->m_cp));
9330 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
9332 opj_procedure_list_destroy(p_j2k->m_procedure_list);
9333 p_j2k->m_procedure_list = 00;
9335 opj_procedure_list_destroy(p_j2k->m_validation_list);
9336 p_j2k->m_procedure_list = 00;
9338 j2k_destroy_cstr_index(p_j2k->cstr_index);
9339 p_j2k->cstr_index = NULL;
9341 opj_image_destroy(p_j2k->m_private_image);
9342 p_j2k->m_private_image = NULL;
9344 opj_image_destroy(p_j2k->m_output_image);
9345 p_j2k->m_output_image = NULL;
9350 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9354 if (p_cstr_ind->marker) {
9355 opj_free(p_cstr_ind->marker);
9356 p_cstr_ind->marker = NULL;
9359 if (p_cstr_ind->tile_index) {
9360 OPJ_UINT32 it_tile = 0;
9362 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9364 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9365 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9366 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9369 if(p_cstr_ind->tile_index[it_tile].tp_index){
9370 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9371 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9374 if(p_cstr_ind->tile_index[it_tile].marker){
9375 opj_free(p_cstr_ind->tile_index[it_tile].marker);
9376 p_cstr_ind->tile_index[it_tile].marker = NULL;
9381 opj_free( p_cstr_ind->tile_index);
9382 p_cstr_ind->tile_index = NULL;
9385 opj_free(p_cstr_ind);
9392 * Destroys a tile coding parameter structure.
9394 * @param p_tcp the tile coding parameter to destroy.
9396 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9402 if (p_tcp->ppt_buffer != 00) {
9403 opj_free(p_tcp->ppt_buffer);
9404 p_tcp->ppt_buffer = 00;
9407 if (p_tcp->tccps != 00) {
9408 opj_free(p_tcp->tccps);
9412 if (p_tcp->m_mct_coding_matrix != 00) {
9413 opj_free(p_tcp->m_mct_coding_matrix);
9414 p_tcp->m_mct_coding_matrix = 00;
9417 if (p_tcp->m_mct_decoding_matrix != 00) {
9418 opj_free(p_tcp->m_mct_decoding_matrix);
9419 p_tcp->m_mct_decoding_matrix = 00;
9422 if (p_tcp->m_mcc_records) {
9423 opj_free(p_tcp->m_mcc_records);
9424 p_tcp->m_mcc_records = 00;
9425 p_tcp->m_nb_max_mcc_records = 0;
9426 p_tcp->m_nb_mcc_records = 0;
9429 if (p_tcp->m_mct_records) {
9430 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9433 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9434 if (l_mct_data->m_data) {
9435 opj_free(l_mct_data->m_data);
9436 l_mct_data->m_data = 00;
9442 opj_free(p_tcp->m_mct_records);
9443 p_tcp->m_mct_records = 00;
9446 if (p_tcp->mct_norms != 00) {
9447 opj_free(p_tcp->mct_norms);
9448 p_tcp->mct_norms = 00;
9451 j2k_tcp_data_destroy(p_tcp);
9456 * Destroys the data inside a tile coding parameter structure.
9458 * @param p_tcp the tile coding parameter which contain data to destroy.
9460 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9462 if (p_tcp->m_data) {
9463 opj_free(p_tcp->m_data);
9464 p_tcp->m_data = NULL;
9465 p_tcp->m_data_size = 0;
9470 * Destroys a coding parameter structure.
9472 * @param p_cp the coding parameter to destroy.
9474 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9476 OPJ_UINT32 l_nb_tiles;
9477 opj_tcp_v2_t * l_current_tile = 00;
9488 l_current_tile = p_cp->tcps;
9489 l_nb_tiles = p_cp->th * p_cp->tw;
9492 (i = 0; i < l_nb_tiles; ++i)
9494 j2k_tcp_destroy(l_current_tile);
9497 opj_free(p_cp->tcps);
9501 (p_cp->ppm_buffer != 00)
9503 opj_free(p_cp->ppm_buffer);
9504 p_cp->ppm_buffer = 00;
9507 (p_cp->comment != 00)
9509 opj_free(p_cp->comment);
9513 (! p_cp->m_is_decoder)
9516 (p_cp->m_specific_param.m_enc.m_matrice)
9518 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9519 p_cp->m_specific_param.m_enc.m_matrice = 00;
9527 * Reads a tile header.
9528 * @param p_j2k the jpeg2000 codec.
9529 * @param p_stream the stream to write data to.
9530 * @param p_manager the user event manager.
9532 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9533 OPJ_UINT32 * p_tile_index,
9534 OPJ_UINT32 * p_data_size,
9535 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9536 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9537 OPJ_UINT32 * p_nb_comps,
9539 opj_stream_private_t *p_stream,
9540 opj_event_mgr_t * p_manager )
9542 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9543 OPJ_UINT32 l_marker_size;
9544 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9545 opj_tcp_v2_t * l_tcp = NULL;
9546 OPJ_UINT32 l_nb_tiles;
9549 assert(p_stream != 00);
9550 assert(p_j2k != 00);
9551 assert(p_manager != 00);
9553 /* Reach the End Of Codestream ?*/
9554 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9555 l_current_marker = J2K_MS_EOC;
9557 /* We need to encounter a SOT marker (a new tile-part header) */
9558 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9562 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9563 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9565 /* Try to read until the Start Of Data is detected */
9566 while (l_current_marker != J2K_MS_SOD) {
9568 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9569 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9570 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9574 /* Read 2 bytes from the buffer as the marker size */
9575 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9577 /* Why this condition? FIXME */
9578 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9579 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9581 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9583 /* Get the marker handler from the marker ID */
9584 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9586 /* Check if the marker is known and if it is the right place to find it */
9587 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9588 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9591 /* FIXME manage case of unknown marker as in the main header ? */
9593 /* Check if the marker size is compatible with the header data size */
9594 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9595 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9596 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9597 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9600 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9603 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9604 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) {
9605 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9609 /* Read the marker segment with the correct marker handler */
9610 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9611 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9615 /* Add the marker to the codestream index*/
9616 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9618 l_marker_handler->id,
9619 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9620 l_marker_size + 4 );
9622 /* Keep the position of the last SOT marker read */
9623 if ( l_marker_handler->id == J2K_MS_SOT ) {
9624 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9625 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9627 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9632 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9633 /* Skip the rest of the tile part header*/
9634 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) {
9635 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9638 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9641 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9642 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9643 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9646 /* Read 2 bytes from the buffer as the new marker ID */
9647 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9651 /* If we didn't skip data before, we need to read the SOD marker*/
9652 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9653 /* Try to read the SOD marker and skip data ? FIXME */
9654 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
9660 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9661 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9662 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9663 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9667 /* Read 2 bytes from buffer as the new marker ID */
9668 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9672 /* Indicate we will try to read a new tile-part header*/
9673 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9674 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9675 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9677 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9678 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9679 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9683 /* Read 2 bytes from buffer as the new marker ID */
9684 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9688 /* Current marker is the EOC marker ?*/
9689 if (l_current_marker == J2K_MS_EOC) {
9690 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9691 p_j2k->m_current_tile_number = 0;
9692 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9697 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9698 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9699 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9701 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9702 ++p_j2k->m_current_tile_number;
9706 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9707 *p_go_on = OPJ_FALSE;
9713 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9714 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9718 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9719 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9721 *p_tile_index = p_j2k->m_current_tile_number;
9722 *p_go_on = OPJ_TRUE;
9723 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9724 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9725 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9726 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9727 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9728 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9730 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9736 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9737 OPJ_UINT32 p_tile_index,
9739 OPJ_UINT32 p_data_size,
9740 opj_stream_private_t *p_stream,
9741 opj_event_mgr_t * p_manager )
9743 OPJ_UINT32 l_current_marker;
9744 OPJ_BYTE l_data [2];
9745 opj_tcp_v2_t * l_tcp;
9748 assert(p_stream != 00);
9749 assert(p_j2k != 00);
9750 assert(p_manager != 00);
9752 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9753 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9757 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9758 if (! l_tcp->m_data) {
9759 j2k_tcp_destroy(l_tcp);
9763 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9767 p_j2k->cstr_index) ) {
9768 j2k_tcp_destroy(l_tcp);
9769 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9773 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9777 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9778 * we destroy just the data which will be re-read in read_tile_header*/
9779 /*j2k_tcp_destroy(l_tcp);
9780 p_j2k->m_tcd->tcp = 0;*/
9781 j2k_tcp_data_destroy(l_tcp);
9783 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9784 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9786 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9787 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9788 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9792 opj_read_bytes(l_data,&l_current_marker,2);
9794 if (l_current_marker == J2K_MS_EOC) {
9795 p_j2k->m_current_tile_number = 0;
9796 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9798 else if (l_current_marker != J2K_MS_SOT)
9800 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9809 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9811 OPJ_UINT32 i,j,k = 0;
9812 OPJ_UINT32 l_width_src,l_height_src;
9813 OPJ_UINT32 l_width_dest,l_height_dest;
9814 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9815 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9816 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9817 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9818 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9820 opj_image_comp_t * l_img_comp_src = 00;
9821 opj_image_comp_t * l_img_comp_dest = 00;
9823 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9824 opj_image_t * l_image_src = 00;
9825 OPJ_UINT32 l_size_comp, l_remaining;
9826 OPJ_INT32 * l_dest_ptr;
9827 opj_tcd_resolution_v2_t* l_res= 00;
9829 l_tilec = p_tcd->tcd_image->tiles->comps;
9830 l_image_src = p_tcd->image;
9831 l_img_comp_src = l_image_src->comps;
9833 l_img_comp_dest = p_output_image->comps;
9835 for (i=0; i<l_image_src->numcomps; i++) {
9837 /* Allocate output component buffer if necessary */
9838 if (!l_img_comp_dest->data) {
9840 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9841 if (! l_img_comp_dest->data) {
9846 /* Copy info from decoded comp image to output image */
9847 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9850 /* Compute the precision of the output buffer */
9851 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9852 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9853 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9859 if (l_size_comp == 3) {
9864 /* Current tile component size*/
9866 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9867 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9870 l_width_src = (l_res->x1 - l_res->x0);
9871 l_height_src = (l_res->y1 - l_res->y0);
9873 /* Border of the current output component*/
9874 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9875 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9876 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9877 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9880 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9881 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9885 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9886 * of the input buffer (decoded tile component) which will be move
9887 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9888 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9889 * by this input area.
9891 assert( l_res->x0 >= 0);
9892 assert( l_res->x1 >= 0);
9893 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9894 l_start_x_dest = l_res->x0 - l_x0_dest;
9895 l_offset_x0_src = 0;
9897 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9898 l_width_dest = l_width_src;
9899 l_offset_x1_src = 0;
9902 l_width_dest = l_x1_dest - l_res->x0 ;
9903 l_offset_x1_src = l_width_src - l_width_dest;
9907 l_start_x_dest = 0 ;
9908 l_offset_x0_src = l_x0_dest - l_res->x0;
9910 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9911 l_width_dest = l_width_src - l_offset_x0_src;
9912 l_offset_x1_src = 0;
9915 l_width_dest = l_img_comp_dest->w ;
9916 l_offset_x1_src = l_res->x1 - l_x1_dest;
9920 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9921 l_start_y_dest = l_res->y0 - l_y0_dest;
9922 l_offset_y0_src = 0;
9924 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9925 l_height_dest = l_height_src;
9926 l_offset_y1_src = 0;
9929 l_height_dest = l_y1_dest - l_res->y0 ;
9930 l_offset_y1_src = l_height_src - l_height_dest;
9934 l_start_y_dest = 0 ;
9935 l_offset_y0_src = l_y0_dest - l_res->y0;
9937 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9938 l_height_dest = l_height_src - l_offset_y0_src;
9939 l_offset_y1_src = 0;
9942 l_height_dest = l_img_comp_dest->h ;
9943 l_offset_y1_src = l_res->y1 - l_y1_dest;
9947 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9952 /* Compute the input buffer offset */
9953 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9954 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9955 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9957 /* Compute the output buffer offset */
9958 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9959 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9961 /* Move the output buffer to the first place where we will write*/
9962 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9965 fprintf(stdout, "COMPO[%d]:\n",i);
9966 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9967 "\t tile offset:%d, %d, %d, %d\n"
9968 "\t buffer offset: %d; %d, %d\n",
9969 l_res->x0, l_res->y0, l_width_src, l_height_src,
9970 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9971 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9973 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9974 "\t start offset: %d, line offset= %d\n",
9975 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9979 switch (l_size_comp) {
9982 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9983 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9985 if (l_img_comp_src->sgnd) {
9986 for (j = 0 ; j < l_height_dest ; ++j) {
9987 for ( k = 0 ; k < l_width_dest ; ++k) {
9988 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9991 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9992 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9996 for ( j = 0 ; j < l_height_dest ; ++j ) {
9997 for ( k = 0 ; k < l_width_dest ; ++k) {
9998 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
10001 l_dest_ptr+= l_line_offset_dest;
10002 l_src_ptr += l_line_offset_src;
10006 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
10007 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
10012 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
10013 l_src_ptr += l_start_offset_src;
10015 if (l_img_comp_src->sgnd) {
10016 for (j=0;j<l_height_dest;++j) {
10017 for (k=0;k<l_width_dest;++k) {
10018 *(l_dest_ptr++) = *(l_src_ptr++);
10021 l_dest_ptr+= l_line_offset_dest;
10022 l_src_ptr += l_line_offset_src ;
10026 for (j=0;j<l_height_dest;++j) {
10027 for (k=0;k<l_width_dest;++k) {
10028 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10031 l_dest_ptr+= l_line_offset_dest;
10032 l_src_ptr += l_line_offset_src ;
10036 l_src_ptr += l_end_offset_src;
10037 p_data = (OPJ_BYTE*) l_src_ptr;
10042 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
10043 l_src_ptr += l_start_offset_src;
10045 for (j=0;j<l_height_dest;++j) {
10046 for (k=0;k<l_width_dest;++k) {
10047 *(l_dest_ptr++) = (*(l_src_ptr++));
10050 l_dest_ptr+= l_line_offset_dest;
10051 l_src_ptr += l_line_offset_src ;
10054 l_src_ptr += l_end_offset_src;
10055 p_data = (OPJ_BYTE*) l_src_ptr;
10069 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
10071 * @param p_j2k the jpeg2000 codec.
10072 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
10073 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
10074 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
10075 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
10076 * @param p_manager the user event manager
10078 * @return true if the area could be set.
10080 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
10081 opj_image_t* p_image,
10082 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
10083 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
10084 struct opj_event_mgr * p_manager )
10086 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
10087 opj_image_t * l_image = p_j2k->m_private_image;
10089 OPJ_UINT32 it_comp;
10090 OPJ_INT32 l_comp_x1, l_comp_y1;
10091 opj_image_comp_t* l_img_comp = NULL;
10093 /* Check if we are read the main header */
10094 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
10095 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
10099 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
10100 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
10102 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10103 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10104 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10105 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10111 /* Check if the positions provided by the user are correct */
10114 assert(p_start_x >= 0 );
10115 assert(p_start_y >= 0 );
10117 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
10118 opj_event_msg_v2(p_manager, EVT_ERROR,
10119 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
10120 p_start_x, l_image->x1);
10123 else if ((OPJ_UINT32)p_start_x < l_image->x0){
10124 opj_event_msg_v2(p_manager, EVT_WARNING,
10125 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
10126 p_start_x, l_image->x0);
10127 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10128 p_image->x0 = l_image->x0;
10131 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
10132 p_image->x0 = p_start_x;
10136 if ((OPJ_UINT32)p_start_y > l_image->y1){
10137 opj_event_msg_v2(p_manager, EVT_ERROR,
10138 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
10139 p_start_y, l_image->y1);
10142 else if ((OPJ_UINT32)p_start_y < l_image->y0){
10143 opj_event_msg_v2(p_manager, EVT_WARNING,
10144 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
10145 p_start_y, l_image->y0);
10146 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10147 p_image->y0 = l_image->y0;
10150 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
10151 p_image->y0 = p_start_y;
10155 assert((OPJ_UINT32)p_end_x > 0);
10156 assert((OPJ_UINT32)p_end_y > 0);
10157 if ((OPJ_UINT32)p_end_x < l_image->x0) {
10158 opj_event_msg_v2(p_manager, EVT_ERROR,
10159 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
10160 p_end_x, l_image->x0);
10163 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
10164 opj_event_msg_v2(p_manager, EVT_WARNING,
10165 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
10166 p_end_x, l_image->x1);
10167 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10168 p_image->x1 = l_image->x1;
10171 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
10172 p_image->x1 = p_end_x;
10176 if ((OPJ_UINT32)p_end_y < l_image->y0) {
10177 opj_event_msg_v2(p_manager, EVT_ERROR,
10178 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
10179 p_end_y, l_image->y0);
10182 if ((OPJ_UINT32)p_end_y > l_image->y1){
10183 opj_event_msg_v2(p_manager, EVT_WARNING,
10184 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
10185 p_end_y, l_image->y1);
10186 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10187 p_image->y1 = l_image->y1;
10190 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
10191 p_image->y1 = p_end_y;
10195 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
10197 l_img_comp = p_image->comps;
10198 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
10202 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10203 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10204 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10205 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10207 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
10208 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10210 opj_event_msg_v2(p_manager, EVT_ERROR,
10211 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
10215 l_img_comp->w = l_w;
10217 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
10218 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10220 opj_event_msg_v2(p_manager, EVT_ERROR,
10221 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
10225 l_img_comp->h = l_h;
10230 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
10231 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
10238 /* ----------------------------------------------------------------------- */
10239 /* J2K / JPT decoder interface */
10240 /* ----------------------------------------------------------------------- */
10242 * Creates a J2K decompression structure.
10244 * @return a handle to a J2K decompressor if successful, NULL otherwise.
10246 opj_j2k_v2_t* opj_j2k_create_decompress(void)
10248 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
10252 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
10254 l_j2k->m_is_decoder = 1;
10255 l_j2k->m_cp.m_is_decoder = 1;
10257 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
10258 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
10259 j2k_destroy(l_j2k);
10262 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
10264 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
10265 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
10266 j2k_destroy(l_j2k);
10270 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
10272 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
10274 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
10276 /* codestream index creation */
10277 l_j2k->cstr_index = j2k_create_cstr_index();
10279 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
10280 if (!l_j2k->cstr_index){
10281 j2k_destroy(l_j2k);
10285 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
10288 /* validation list creation */
10289 l_j2k->m_validation_list = opj_procedure_list_create();
10290 if (! l_j2k->m_validation_list) {
10291 j2k_destroy(l_j2k);
10295 /* execution list creation */
10296 l_j2k->m_procedure_list = opj_procedure_list_create();
10297 if (! l_j2k->m_procedure_list) {
10298 j2k_destroy(l_j2k);
10306 opj_codestream_index_t* j2k_create_cstr_index(void)
10308 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
10309 opj_calloc(1,sizeof(opj_codestream_index_t));
10313 cstr_index->maxmarknum = 100;
10314 cstr_index->marknum = 0;
10315 cstr_index->marker = (opj_marker_info_t*)
10316 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
10317 if (!cstr_index-> marker)
10320 cstr_index->tile_index = NULL;
10327 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
10329 * @param p_tile_no the tile index.
10330 * @param p_comp_no the component being outputted.
10331 * @param p_j2k the J2K codec.
10333 * @return the number of bytes taken by the SPCod element.
10335 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
10336 OPJ_UINT32 p_tile_no,
10337 OPJ_UINT32 p_comp_no )
10339 opj_cp_v2_t *l_cp = 00;
10340 opj_tcp_v2_t *l_tcp = 00;
10341 opj_tccp_t *l_tccp = 00;
10343 /* preconditions */
10344 assert(p_j2k != 00);
10346 l_cp = &(p_j2k->m_cp);
10347 l_tcp = &l_cp->tcps[p_tile_no];
10348 l_tccp = &l_tcp->tccps[p_comp_no];
10350 /* preconditions again */
10351 assert(p_tile_no < (l_cp->tw * l_cp->th));
10352 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10354 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10355 return 5 + l_tccp->numresolutions;
10363 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10365 * @param p_comp_no the component number to output.
10366 * @param p_stream the stream to write data to.
10367 * @param p_j2k J2K codec.
10368 * @param p_manager the user event manager.
10371 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10372 OPJ_UINT32 p_tile_no,
10373 OPJ_UINT32 p_comp_no,
10375 OPJ_UINT32 * p_header_size,
10376 struct opj_event_mgr * p_manager )
10379 opj_cp_v2_t *l_cp = 00;
10380 opj_tcp_v2_t *l_tcp = 00;
10381 opj_tccp_t *l_tccp = 00;
10383 /* preconditions */
10384 assert(p_j2k != 00);
10385 assert(p_header_size != 00);
10386 assert(p_manager != 00);
10387 assert(p_data != 00);
10389 l_cp = &(p_j2k->m_cp);
10390 l_tcp = &l_cp->tcps[p_tile_no];
10391 l_tccp = &l_tcp->tccps[p_comp_no];
10393 /* preconditions again */
10394 assert(p_tile_no < (l_cp->tw * l_cp->th));
10395 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10397 if (*p_header_size < 5) {
10398 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10402 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
10405 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
10408 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
10411 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
10414 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
10417 *p_header_size = *p_header_size - 5;
10419 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10421 if (*p_header_size < l_tccp->numresolutions) {
10422 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10426 for (i = 0; i < l_tccp->numresolutions; ++i) {
10427 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
10431 *p_header_size = *p_header_size - l_tccp->numresolutions;
10438 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10439 * @param p_header_data the data contained in the COM box.
10440 * @param p_j2k the jpeg2000 codec.
10441 * @param p_header_size the size of the data contained in the COM marker.
10442 * @param p_manager the user event manager.
10444 opj_bool j2k_read_SPCod_SPCoc(
10445 opj_j2k_v2_t *p_j2k,
10447 OPJ_BYTE * p_header_data,
10448 OPJ_UINT32 * p_header_size,
10449 struct opj_event_mgr * p_manager
10452 OPJ_UINT32 i, l_tmp;
10453 opj_cp_v2_t *l_cp = NULL;
10454 opj_tcp_v2_t *l_tcp = NULL;
10455 opj_tccp_t *l_tccp = NULL;
10456 OPJ_BYTE * l_current_ptr = NULL;
10458 /* preconditions */
10459 assert(p_j2k != 00);
10460 assert(p_manager != 00);
10461 assert(p_header_data != 00);
10463 l_cp = &(p_j2k->m_cp);
10464 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10465 &l_cp->tcps[p_j2k->m_current_tile_number] :
10466 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10468 /* precondition again */
10469 assert(compno < p_j2k->m_private_image->numcomps);
10471 l_tccp = &l_tcp->tccps[compno];
10472 l_current_ptr = p_header_data;
10474 /* make sure room is sufficient */
10475 if (*p_header_size < 5) {
10476 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10480 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10481 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10484 /* If user wants to remove more resolutions than the codestream contains, return error */
10485 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10486 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10487 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10488 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10492 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10494 l_tccp->cblkw += 2;
10496 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10498 l_tccp->cblkh += 2;
10500 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10503 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10506 *p_header_size = *p_header_size - 5;
10508 /* use custom precinct size ? */
10509 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10510 if (*p_header_size < l_tccp->numresolutions) {
10511 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10515 for (i = 0; i < l_tccp->numresolutions; ++i) {
10516 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10518 l_tccp->prcw[i] = l_tmp & 0xf;
10519 l_tccp->prch[i] = l_tmp >> 4;
10522 *p_header_size = *p_header_size - l_tccp->numresolutions;
10525 /* set default size for the precinct width and height */
10526 for (i = 0; i < l_tccp->numresolutions; ++i) {
10527 l_tccp->prcw[i] = 15;
10528 l_tccp->prch[i] = 15;
10532 #ifdef WIP_REMOVE_MSD
10534 if (p_j2k->cstr_info && compno == 0) {
10535 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10537 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10538 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10539 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10540 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10541 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10544 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10545 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10554 * Copies the tile component parameters of all the component from the first tile component.
10556 * @param p_j2k the J2k codec.
10558 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10562 opj_cp_v2_t *l_cp = NULL;
10563 opj_tcp_v2_t *l_tcp = NULL;
10564 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10565 OPJ_UINT32 l_prc_size;
10567 /* preconditions */
10568 assert(p_j2k != 00);
10570 l_cp = &(p_j2k->m_cp);
10571 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10572 &l_cp->tcps[p_j2k->m_current_tile_number] :
10573 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10575 l_ref_tccp = &l_tcp->tccps[0];
10576 l_copied_tccp = l_ref_tccp + 1;
10577 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10579 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10580 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10581 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10582 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10583 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10584 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10585 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10586 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10592 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10594 * @param p_tile_no the tile index.
10595 * @param p_comp_no the component being outputted.
10596 * @param p_j2k the J2K codec.
10598 * @return the number of bytes taken by the SPCod element.
10600 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10601 OPJ_UINT32 p_tile_no,
10602 OPJ_UINT32 p_comp_no )
10604 OPJ_UINT32 l_num_bands;
10606 opj_cp_v2_t *l_cp = 00;
10607 opj_tcp_v2_t *l_tcp = 00;
10608 opj_tccp_t *l_tccp = 00;
10610 /* preconditions */
10611 assert(p_j2k != 00);
10613 l_cp = &(p_j2k->m_cp);
10614 l_tcp = &l_cp->tcps[p_tile_no];
10615 l_tccp = &l_tcp->tccps[p_comp_no];
10617 /* preconditions again */
10618 assert(p_tile_no < l_cp->tw * l_cp->th);
10619 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10621 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10623 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10624 return 1 + l_num_bands;
10627 return 1 + 2*l_num_bands;
10632 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10634 * @param p_tile_no the tile to output.
10635 * @param p_comp_no the component number to output.
10636 * @param p_data the data buffer.
10637 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10638 * @param p_j2k J2K codec.
10639 * @param p_manager the user event manager.
10642 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10643 OPJ_UINT32 p_tile_no,
10644 OPJ_UINT32 p_comp_no,
10646 OPJ_UINT32 * p_header_size,
10647 struct opj_event_mgr * p_manager )
10649 OPJ_UINT32 l_header_size;
10650 OPJ_UINT32 l_band_no, l_num_bands;
10651 OPJ_UINT32 l_expn,l_mant;
10653 opj_cp_v2_t *l_cp = 00;
10654 opj_tcp_v2_t *l_tcp = 00;
10655 opj_tccp_t *l_tccp = 00;
10657 /* preconditions */
10658 assert(p_j2k != 00);
10659 assert(p_header_size != 00);
10660 assert(p_manager != 00);
10661 assert(p_data != 00);
10663 l_cp = &(p_j2k->m_cp);
10664 l_tcp = &l_cp->tcps[p_tile_no];
10665 l_tccp = &l_tcp->tccps[p_comp_no];
10667 /* preconditions again */
10668 assert(p_tile_no < l_cp->tw * l_cp->th);
10669 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10671 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10673 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10674 l_header_size = 1 + l_num_bands;
10676 if (*p_header_size < l_header_size) {
10677 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10681 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10684 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10685 l_expn = l_tccp->stepsizes[l_band_no].expn;
10686 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10691 l_header_size = 1 + 2*l_num_bands;
10693 if (*p_header_size < l_header_size) {
10694 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10698 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10701 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10702 l_expn = l_tccp->stepsizes[l_band_no].expn;
10703 l_mant = l_tccp->stepsizes[l_band_no].mant;
10705 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10710 *p_header_size = *p_header_size - l_header_size;
10716 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10718 * @param p_comp_no the component being targeted.
10719 * @param p_header_data the data contained in the COM box.
10720 * @param p_j2k the jpeg2000 codec.
10721 * @param p_header_size the size of the data contained in the COM marker.
10722 * @param p_manager the user event manager.
10724 opj_bool j2k_read_SQcd_SQcc(
10725 opj_j2k_v2_t *p_j2k,
10726 OPJ_UINT32 p_comp_no,
10727 OPJ_BYTE* p_header_data,
10728 OPJ_UINT32 * p_header_size,
10729 struct opj_event_mgr * p_manager
10733 OPJ_UINT32 l_band_no;
10734 opj_cp_v2_t *l_cp = 00;
10735 opj_tcp_v2_t *l_tcp = 00;
10736 opj_tccp_t *l_tccp = 00;
10737 OPJ_BYTE * l_current_ptr = 00;
10738 OPJ_UINT32 l_tmp, l_num_band;
10741 assert(p_j2k != 00);
10742 assert(p_manager != 00);
10743 assert(p_header_data != 00);
10745 l_cp = &(p_j2k->m_cp);
10746 /* come from tile part header or main header ?*/
10747 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10748 &l_cp->tcps[p_j2k->m_current_tile_number] :
10749 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10751 /* precondition again*/
10752 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10754 l_tccp = &l_tcp->tccps[p_comp_no];
10755 l_current_ptr = p_header_data;
10757 if (*p_header_size < 1) {
10758 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10761 *p_header_size -= 1;
10763 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10766 l_tccp->qntsty = l_tmp & 0x1f;
10767 l_tccp->numgbits = l_tmp >> 5;
10768 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10772 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10774 (*p_header_size) / 2;
10776 if( l_num_band > J2K_MAXBANDS ) {
10777 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10778 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10779 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10780 /*return OPJ_FALSE;*/
10785 if (l_cp->correct) {
10787 /* if JPWL is on, we check whether there are too many subbands */
10788 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10789 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10790 "JPWL: bad number of subbands in Sqcx (%d)\n",
10792 if (!JPWL_ASSUME) {
10793 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10796 /* we try to correct */
10798 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10799 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10804 #endif /* USE_JPWL */
10806 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10807 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10808 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10810 if (l_band_no < J2K_MAXBANDS){
10811 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10812 l_tccp->stepsizes[l_band_no].mant = 0;
10815 *p_header_size = *p_header_size - l_num_band;
10818 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10819 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10821 if (l_band_no < J2K_MAXBANDS){
10822 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10823 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10826 *p_header_size = *p_header_size - 2*l_num_band;
10829 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10830 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10831 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10832 l_tccp->stepsizes[l_band_no].expn =
10833 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10834 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10835 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10843 * Copies the tile component parameters of all the component from the first tile component.
10845 * @param p_j2k the J2k codec.
10847 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10850 opj_cp_v2_t *l_cp = NULL;
10851 opj_tcp_v2_t *l_tcp = NULL;
10852 opj_tccp_t *l_ref_tccp = NULL;
10853 opj_tccp_t *l_copied_tccp = NULL;
10856 /* preconditions */
10857 assert(p_j2k != 00);
10859 l_cp = &(p_j2k->m_cp);
10860 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10861 &l_cp->tcps[p_j2k->m_current_tile_number] :
10862 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10864 l_ref_tccp = &l_tcp->tccps[0];
10865 l_copied_tccp = l_ref_tccp + 1;
10866 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10868 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10869 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10870 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10871 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10877 * Dump some elements from the J2K decompression structure .
10879 *@param p_j2k the jpeg2000 codec.
10880 *@param flag flag to describe what elments are dump.
10881 *@param out_stream output stream where dump the elements.
10884 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10886 /* Check if the flag is compatible with j2k file*/
10887 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10888 fprintf(out_stream, "Wrong flag\n");
10892 /* Dump the image_header */
10893 if (flag & OPJ_IMG_INFO){
10894 if (p_j2k->m_private_image)
10895 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10898 /* Dump the codestream info from main header */
10899 if (flag & OPJ_J2K_MH_INFO){
10900 j2k_dump_MH_info(p_j2k, out_stream);
10904 /* Dump the codestream info of the current tile */
10905 if (flag & OPJ_J2K_TH_INFO){
10909 /* Dump the codestream index from main header */
10910 if (flag & OPJ_J2K_MH_IND){
10911 j2k_dump_MH_index(p_j2k, out_stream);
10914 /* Dump the codestream index of the current tile */
10915 if (flag & OPJ_J2K_TH_IND){
10922 * Dump index elements of the codestream extract from the main header.
10924 *@param p_j2k the jpeg2000 codec.
10925 *@param out_stream output stream where dump the elements.
10928 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10930 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10931 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10933 fprintf(out_stream, "Codestream index from main header: {\n");
10935 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10936 "\t Main header end position=%" PRIi64 "\n",
10937 cstr_index->main_head_start, cstr_index->main_head_end);
10939 fprintf(out_stream, "\t Marker list: {\n");
10941 if (cstr_index->marker){
10942 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10943 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10944 cstr_index->marker[it_marker].type,
10945 cstr_index->marker[it_marker].pos,
10946 cstr_index->marker[it_marker].len );
10950 fprintf(out_stream, "\t }\n");
10953 if (cstr_index->tile_index){
10955 /* Simple test to avoid to write empty information*/
10956 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10957 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10958 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10961 if (l_acc_nb_of_tile_part)
10963 fprintf(out_stream, "\t Tile index: {\n");
10965 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10966 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10968 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10970 if (cstr_index->tile_index[it_tile].tp_index){
10971 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10972 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10974 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10975 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10976 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10980 if (cstr_index->tile_index[it_tile].marker){
10981 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10982 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10983 cstr_index->tile_index[it_tile].marker[it_marker].type,
10984 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10985 cstr_index->tile_index[it_tile].marker[it_marker].len );
10989 fprintf(out_stream,"\t }\n");
10993 fprintf(out_stream,"}\n");
10998 * Dump info elements of the codestream extract from the main header.
11000 *@param p_j2k the jpeg2000 codec.
11001 *@param out_stream output stream where dump the elements.
11004 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11006 opj_tcp_v2_t * l_default_tile=NULL;
11008 fprintf(out_stream, "Codestream info from main header: {\n");
11010 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
11011 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
11012 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
11014 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11015 if (l_default_tile)
11018 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
11020 fprintf(out_stream, "\t default tile {\n");
11021 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
11022 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
11023 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
11024 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
11026 for (compno = 0; compno < numcomps; compno++) {
11027 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11029 OPJ_INT32 bandno, numbands;
11032 fprintf(out_stream, "\t\t comp %d {\n", compno);
11033 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
11034 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
11035 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
11036 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
11037 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
11038 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
11040 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
11041 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
11042 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
11044 fprintf(out_stream, "\n");
11046 /* quantization style*/
11047 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
11048 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
11049 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
11050 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11051 for (bandno = 0; bandno < numbands; bandno++) {
11052 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
11053 l_tccp->stepsizes[bandno].expn);
11055 fprintf(out_stream, "\n");
11058 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
11060 fprintf(out_stream, "\t\t }\n");
11061 } /*end of component of default tile*/
11062 fprintf(out_stream, "\t }\n"); /*end of default tile*/
11066 fprintf(out_stream, "}\n");
11071 * Dump an image header structure.
11073 *@param img_header the image header to dump.
11074 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11075 *@param out_stream output stream where dump the elements.
11077 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
11081 if (dev_dump_flag){
11082 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
11086 fprintf(out_stream, "Image info {\n");
11087 tab[0] = '\t';tab[1] = '\0';
11090 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
11091 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
11092 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
11094 if (img_header->comps){
11096 for (compno = 0; compno < img_header->numcomps; compno++) {
11097 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
11098 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
11099 fprintf(out_stream,"%s}\n",tab);
11103 fprintf(out_stream, "}\n");
11107 * Dump a component image header structure.
11109 *@param comp_header the component image header to dump.
11110 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11111 *@param out_stream output stream where dump the elements.
11113 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
11117 if (dev_dump_flag){
11118 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
11121 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
11124 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
11125 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
11126 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
11129 fprintf(out_stream, "}\n");
11134 * Get the codestream info from a JPEG2000 codec.
11136 *@param p_j2k the component image header to dump.
11138 *@return the codestream information extract from the jpg2000 codec
11140 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
11143 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
11144 opj_tcp_v2_t *l_default_tile;
11145 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
11147 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
11149 cstr_info->tx0 = p_j2k->m_cp.tx0;
11150 cstr_info->ty0 = p_j2k->m_cp.ty0;
11151 cstr_info->tdx = p_j2k->m_cp.tdx;
11152 cstr_info->tdy = p_j2k->m_cp.tdy;
11153 cstr_info->tw = p_j2k->m_cp.tw;
11154 cstr_info->th = p_j2k->m_cp.th;
11156 cstr_info->tile_info = NULL; /* Not fill from the main header*/
11158 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11160 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
11161 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
11162 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
11163 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
11165 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
11167 for (compno = 0; compno < numcomps; compno++) {
11168 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11169 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
11170 OPJ_INT32 bandno, numbands;
11173 l_tccp_info->csty = l_tccp->csty;
11174 l_tccp_info->numresolutions = l_tccp->numresolutions;
11175 l_tccp_info->cblkw = l_tccp->cblkw;
11176 l_tccp_info->cblkh = l_tccp->cblkh;
11177 l_tccp_info->cblksty = l_tccp->cblksty;
11178 l_tccp_info->qmfbid = l_tccp->qmfbid;
11179 if (l_tccp->numresolutions < J2K_MAXRLVLS)
11181 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
11182 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
11185 /* quantization style*/
11186 l_tccp_info->qntsty = l_tccp->qntsty;
11187 l_tccp_info->numgbits = l_tccp->numgbits;
11189 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11190 if (numbands < J2K_MAXBANDS) {
11191 for (bandno = 0; bandno < numbands; bandno++) {
11192 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
11193 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
11198 l_tccp_info->roishift = l_tccp->roishift;
11206 * Get the codestream index from a JPEG2000 codec.
11208 *@param p_j2k the component image header to dump.
11210 *@return the codestream index extract from the jpg2000 codec
11212 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
11214 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
11215 opj_calloc(1,sizeof(opj_codestream_index_t));
11219 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
11220 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
11221 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
11223 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
11224 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
11225 if (!l_cstr_index->marker){
11226 opj_free( l_cstr_index);
11230 if (p_j2k->cstr_index->marker)
11231 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
11233 opj_free(l_cstr_index->marker);
11234 l_cstr_index->marker = NULL;
11237 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
11238 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
11239 if (!l_cstr_index->tile_index){
11240 opj_free( l_cstr_index->marker);
11241 opj_free( l_cstr_index);
11245 if (!p_j2k->cstr_index->tile_index){
11246 opj_free(l_cstr_index->tile_index);
11247 l_cstr_index->tile_index = NULL;
11250 OPJ_UINT32 it_tile = 0;
11251 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
11254 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
11256 l_cstr_index->tile_index[it_tile].marker =
11257 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
11259 if (!l_cstr_index->tile_index[it_tile].marker) {
11260 OPJ_UINT32 it_tile_free;
11262 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11263 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11266 opj_free( l_cstr_index->tile_index);
11267 opj_free( l_cstr_index->marker);
11268 opj_free( l_cstr_index);
11272 if (p_j2k->cstr_index->tile_index[it_tile].marker)
11273 memcpy( l_cstr_index->tile_index[it_tile].marker,
11274 p_j2k->cstr_index->tile_index[it_tile].marker,
11275 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
11277 opj_free(l_cstr_index->tile_index[it_tile].marker);
11278 l_cstr_index->tile_index[it_tile].marker = NULL;
11281 /* Tile part index*/
11282 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
11284 l_cstr_index->tile_index[it_tile].tp_index =
11285 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
11287 if(!l_cstr_index->tile_index[it_tile].tp_index){
11288 OPJ_UINT32 it_tile_free;
11290 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11291 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11292 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
11295 opj_free( l_cstr_index->tile_index);
11296 opj_free( l_cstr_index->marker);
11297 opj_free( l_cstr_index);
11301 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
11302 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
11303 p_j2k->cstr_index->tile_index[it_tile].tp_index,
11304 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
11307 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
11308 l_cstr_index->tile_index[it_tile].tp_index = NULL;
11311 /* Packet index (NOT USED)*/
11312 l_cstr_index->tile_index[it_tile].nb_packet = 0;
11313 l_cstr_index->tile_index[it_tile].packet_index = NULL;
11318 return l_cstr_index;
11321 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
11323 OPJ_UINT32 it_tile=0;
11325 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
11326 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
11327 if (!p_j2k->cstr_index->tile_index)
11330 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
11331 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
11332 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
11333 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
11334 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
11335 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11345 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
11346 opj_stream_private_t *p_stream,
11347 opj_event_mgr_t * p_manager)
11349 opj_bool l_go_on = OPJ_TRUE;
11350 OPJ_UINT32 l_current_tile_no;
11351 OPJ_UINT32 l_data_size,l_max_data_size;
11352 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11353 OPJ_UINT32 l_nb_comps;
11354 OPJ_BYTE * l_current_data;
11356 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11357 if (! l_current_data) {
11360 l_max_data_size = 1000;
11365 if (! j2k_read_tile_header( p_j2k,
11366 &l_current_tile_no,
11368 &l_tile_x0, &l_tile_y0,
11369 &l_tile_x1, &l_tile_y1,
11374 opj_free(l_current_data);
11382 if (l_data_size > l_max_data_size) {
11383 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11384 if (! l_current_data) {
11385 opj_free(l_current_data);
11389 l_max_data_size = l_data_size;
11392 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11393 opj_free(l_current_data);
11396 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);
11398 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11399 opj_free(l_current_data);
11402 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11406 opj_free(l_current_data);
11412 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11414 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11417 assert(p_j2k != 00);
11419 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11420 /* DEVELOPER CORNER, add your custom procedures */
11425 * Read and decode one tile.
11427 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
11428 opj_stream_private_t *p_stream,
11429 opj_event_mgr_t * p_manager)
11431 opj_bool l_go_on = OPJ_TRUE;
11432 OPJ_UINT32 l_current_tile_no;
11433 OPJ_UINT32 l_tile_no_to_dec;
11434 OPJ_UINT32 l_data_size,l_max_data_size;
11435 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11436 OPJ_UINT32 l_nb_comps;
11437 OPJ_BYTE * l_current_data;
11439 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11440 if (! l_current_data) {
11443 l_max_data_size = 1000;
11445 /*Allocate and initialize some elements of codestrem index if not already done*/
11446 if( !p_j2k->cstr_index->tile_index)
11448 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11449 opj_free(l_current_data);
11453 /* Move into the codestream to the first SOT used to decode the desired tile */
11454 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11455 if (p_j2k->cstr_index->tile_index)
11456 if(p_j2k->cstr_index->tile_index->tp_index)
11458 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11459 /* the index for this tile has not been built,
11460 * so move to the last SOT read */
11461 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11462 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11467 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)) {
11468 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11472 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11473 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11474 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11478 if (! j2k_read_tile_header( p_j2k,
11479 &l_current_tile_no,
11481 &l_tile_x0, &l_tile_y0,
11482 &l_tile_x1, &l_tile_y1,
11487 opj_free(l_current_data);
11496 if (l_data_size > l_max_data_size) {
11497 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11498 if (! l_current_data) {
11499 opj_free(l_current_data);
11503 l_max_data_size = l_data_size;
11508 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11509 opj_free(l_current_data);
11512 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);
11514 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11515 opj_free(l_current_data);
11518 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11520 if(l_current_tile_no == l_tile_no_to_dec)
11522 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11523 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11524 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11530 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);
11535 opj_free(l_current_data);
11542 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11544 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11547 assert(p_j2k != 00);
11549 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11550 /* DEVELOPER CORNER, add your custom procedures */
11556 * Decodes the tiles of the stream.
11558 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
11559 opj_stream_private_t * p_stream,
11560 opj_image_t * p_image,
11561 opj_event_mgr_t * p_manager)
11568 p_j2k->m_output_image = opj_image_create0();
11569 if (! (p_j2k->m_output_image)) {
11572 opj_copy_image_header(p_image, p_j2k->m_output_image);
11574 /* customization of the decoding */
11575 j2k_setup_decoding(p_j2k);
11577 /* Decode the codestream */
11578 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11579 opj_image_destroy(p_j2k->m_private_image);
11580 p_j2k->m_private_image = NULL;
11584 /* Move data and copy one information from codec to output image*/
11585 for (compno = 0; compno < p_image->numcomps; compno++) {
11586 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11587 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11588 p_j2k->m_output_image->comps[compno].data = NULL;
11596 * Get the decoded tile.
11598 * @param p_j2k the jpeg2000 codestream codec.
11599 * @param p_stream input_stream
11600 * @param p_image output image. .
11601 * @param p_manager the user event manager
11602 * @param tile_index index of the tile we want decode
11604 * @return true if succeed.
11606 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11607 opj_stream_private_t *p_stream,
11608 opj_image_t* p_image,
11609 struct opj_event_mgr * p_manager,
11610 OPJ_UINT32 tile_index )
11613 OPJ_UINT32 l_tile_x, l_tile_y;
11614 opj_image_comp_t* l_img_comp;
11617 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11621 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11622 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);
11626 /* Compute the dimension of the desired tile*/
11627 l_tile_x = tile_index % p_j2k->m_cp.tw;
11628 l_tile_y = tile_index / p_j2k->m_cp.tw;
11630 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11631 if (p_image->x0 < p_j2k->m_private_image->x0)
11632 p_image->x0 = p_j2k->m_private_image->x0;
11633 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11634 if (p_image->x1 > p_j2k->m_private_image->x1)
11635 p_image->x1 = p_j2k->m_private_image->x1;
11637 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11638 if (p_image->y0 < p_j2k->m_private_image->y0)
11639 p_image->y0 = p_j2k->m_private_image->y0;
11640 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11641 if (p_image->y1 > p_j2k->m_private_image->y1)
11642 p_image->y1 = p_j2k->m_private_image->y1;
11644 l_img_comp = p_image->comps;
11645 for (compno=0; compno < p_image->numcomps; ++compno)
11647 OPJ_INT32 l_comp_x1, l_comp_y1;
11649 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11651 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11652 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11653 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11654 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11656 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11657 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11662 /* Destroy the previous output image*/
11663 if (p_j2k->m_output_image)
11664 opj_image_destroy(p_j2k->m_output_image);
11666 /* Create the ouput image from the information previously computed*/
11667 p_j2k->m_output_image = opj_image_create0();
11668 if (! (p_j2k->m_output_image)) {
11671 opj_copy_image_header(p_image, p_j2k->m_output_image);
11673 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11675 /* customization of the decoding */
11676 j2k_setup_decoding_tile(p_j2k);
11678 /* Decode the codestream */
11679 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11680 opj_image_destroy(p_j2k->m_private_image);
11681 p_j2k->m_private_image = NULL;
11685 /* Move data and copy one information from codec to output image*/
11686 for (compno = 0; compno < p_image->numcomps; compno++) {
11687 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11689 if (p_image->comps[compno].data)
11690 opj_free(p_image->comps[compno].data);
11692 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11694 p_j2k->m_output_image->comps[compno].data = NULL;
11700 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11702 OPJ_UINT32 it_comp;
11704 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11706 if (p_j2k->m_private_image) {
11707 if (p_j2k->m_private_image->comps) {
11708 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11709 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11710 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11711 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11712 if ( res_factor >= max_res){
11713 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11716 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11729 * Encodes all the tiles in a row.
11731 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11732 opj_stream_private_t *p_stream,
11733 opj_event_mgr_t * p_manager )
11736 OPJ_UINT32 l_nb_tiles;
11737 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11738 OPJ_BYTE * l_current_data;
11740 /* preconditions */
11741 assert(p_j2k != 00);
11742 assert(p_stream != 00);
11743 assert(p_manager != 00);
11745 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11746 if (! l_current_data) {
11749 l_max_tile_size = 1000;
11751 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11752 for (i=0;i<l_nb_tiles;++i) {
11753 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11754 opj_free(l_current_data);
11758 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11759 if (l_current_tile_size > l_max_tile_size) {
11760 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11761 if (! l_current_data) {
11764 l_max_tile_size = l_current_tile_size;
11767 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11769 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11774 opj_free(l_current_data);
11779 * Ends the compression procedures and possibility add data to be read after the
11782 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11783 opj_stream_private_t *p_stream,
11784 struct opj_event_mgr * p_manager)
11786 /* customization of the encoding */
11787 j2k_setup_end_compress(p_j2k);
11789 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11799 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11801 * @param p_j2k the jpeg2000 codec.
11802 * @param p_stream the stream object.
11803 * @param p_manager the user event manager.
11805 * @return true if the codec is valid.
11807 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11808 opj_stream_private_t *p_stream,
11809 opj_image_t * p_image,
11810 opj_event_mgr_t * p_manager)
11812 /* preconditions */
11813 assert(p_j2k != 00);
11814 assert(p_stream != 00);
11815 assert(p_manager != 00);
11817 p_j2k->m_private_image = opj_image_create0();
11818 opj_copy_image_header(p_image, p_j2k->m_private_image);
11820 // TODO_MSD: Find a better way
11821 if (p_image->comps) {
11822 OPJ_UINT32 it_comp;
11823 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11824 if (p_image->comps[it_comp].data) {
11825 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11826 p_image->comps[it_comp].data = NULL;
11832 /* customization of the validation */
11833 j2k_setup_encoding_validation (p_j2k);
11835 /* validation of the parameters codec */
11836 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11840 /* customization of the encoding */
11841 j2k_setup_header_writting(p_j2k);
11844 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11854 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11855 OPJ_UINT32 p_tile_index,
11856 opj_stream_private_t *p_stream,
11857 opj_event_mgr_t * p_manager )
11860 if (p_tile_index != p_j2k->m_current_tile_number) {
11861 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11865 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);
11867 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11868 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11869 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11871 /* initialisation before tile encoding */
11872 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11882 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11884 OPJ_UINT32 i,j,k = 0;
11885 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11886 opj_image_comp_t * l_img_comp = 00;
11887 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11888 opj_image_t * l_image = 00;
11889 OPJ_UINT32 l_size_comp, l_remaining;
11890 OPJ_INT32 * l_src_ptr;
11891 l_tilec = p_tcd->tcd_image->tiles->comps;
11892 l_image = p_tcd->image;
11893 l_img_comp = l_image->comps;
11895 for (i=0;i<p_tcd->image->numcomps;++i) {
11896 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11897 l_remaining = l_img_comp->prec & 7; /* (%8) */
11902 if (l_size_comp == 3) {
11906 l_width = (l_tilec->x1 - l_tilec->x0);
11907 l_height = (l_tilec->y1 - l_tilec->y0);
11908 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11909 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11910 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11911 l_stride = l_image_width - l_width;
11912 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11914 switch (l_size_comp) {
11917 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11918 if (l_img_comp->sgnd) {
11919 for (j=0;j<l_height;++j) {
11920 for (k=0;k<l_width;++k) {
11921 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11925 l_src_ptr += l_stride;
11929 for (j=0;j<l_height;++j) {
11930 for (k=0;k<l_width;++k) {
11931 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11935 l_src_ptr += l_stride;
11939 p_data = (OPJ_BYTE*) l_dest_ptr;
11944 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11945 if (l_img_comp->sgnd) {
11946 for (j=0;j<l_height;++j) {
11947 for (k=0;k<l_width;++k) {
11948 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11950 l_src_ptr += l_stride;
11954 for (j=0;j<l_height;++j) {
11955 for (k=0;k<l_width;++k) {
11956 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11958 l_src_ptr += l_stride;
11962 p_data = (OPJ_BYTE*) l_dest_ptr;
11967 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11968 for (j=0;j<l_height;++j) {
11969 for (k=0;k<l_width;++k) {
11970 *(l_dest_ptr++) = *(l_src_ptr++);
11972 l_src_ptr += l_stride;
11975 p_data = (OPJ_BYTE*) l_dest_ptr;
11988 * @param p_j2k the jpeg2000 codec.
11989 * @param p_stream the stream to write data to.
11990 * @param p_manager the user event manager.
11992 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11994 OPJ_UINT32 p_data_size,
11995 opj_stream_private_t *p_stream,
11996 opj_event_mgr_t * p_manager )
11998 opj_tcd_v2_t * l_tcd = 00;
11999 opj_cp_v2_t * l_cp = 00;
12000 opj_tcp_v2_t * l_tcp = 00;
12001 OPJ_UINT32 l_nb_bytes_written;
12002 OPJ_BYTE * l_current_data = 00;
12003 OPJ_UINT32 l_tile_size = 0;
12004 OPJ_UINT32 l_available_data;
12006 /* preconditions */
12007 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12009 l_tcd = p_j2k->m_tcd;
12010 l_cp = &(p_j2k->m_cp);
12011 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12013 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
12014 l_available_data = l_tile_size;
12015 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
12017 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
12018 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
12022 l_nb_bytes_written = 0;
12023 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12026 l_current_data += l_nb_bytes_written;
12027 l_available_data -= l_nb_bytes_written;
12029 l_nb_bytes_written = 0;
12030 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12034 l_available_data -= l_nb_bytes_written;
12035 l_nb_bytes_written = l_tile_size - l_available_data;
12037 if ( opj_stream_write_data( p_stream,
12038 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
12039 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
12043 ++p_j2k->m_current_tile_number;
12050 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12051 * are valid. Developers wanting to extend the library can add their own validation procedures.
12053 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
12055 /* preconditions */
12056 assert(p_j2k != 00);
12058 /* DEVELOPER CORNER, insert your custom procedures */
12059 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
12061 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12062 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
12065 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
12066 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
12067 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
12071 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12072 * are valid. Developers wanting to extend the library can add their own validation procedures.
12074 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
12076 /* preconditions */
12077 assert(p_j2k != 00);
12079 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
12080 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
12082 /* DEVELOPER CORNER, add your custom validation procedure */
12083 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
12088 * Sets up the procedures to do on writing header.
12089 * Developers wanting to extend the library can add their own writing procedures.
12091 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
12093 /* preconditions */
12094 assert(p_j2k != 00);
12096 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
12097 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
12098 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
12099 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
12100 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
12103 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12104 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
12105 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
12107 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
12108 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
12112 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
12114 if (p_j2k->m_cp.comment != 00) {
12115 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
12118 /* DEVELOPER CORNER, insert your custom procedures */
12119 if (p_j2k->m_cp.rsiz & MCT) {
12120 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
12122 /* End of Developer Corner */
12124 if (p_j2k->cstr_index) {
12125 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
12128 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
12129 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
12133 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
12135 OPJ_UINT32 * p_data_written,
12136 OPJ_UINT32 p_total_data_size,
12137 opj_stream_private_t *p_stream,
12138 struct opj_event_mgr * p_manager )
12141 OPJ_UINT32 l_nb_bytes_written = 0;
12142 OPJ_UINT32 l_current_nb_bytes_written;
12143 OPJ_BYTE * l_begin_data = 00;
12145 opj_tcp_v2_t *l_tcp = 00;
12146 opj_tcd_v2_t * l_tcd = 00;
12147 opj_cp_v2_t * l_cp = 00;
12149 l_tcd = p_j2k->m_tcd;
12150 l_cp = &(p_j2k->m_cp);
12151 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12153 l_tcd->cur_pino = 0;
12155 /*Get number of tile parts*/
12156 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12161 l_current_nb_bytes_written = 0;
12162 l_begin_data = p_data;
12163 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
12168 l_nb_bytes_written += l_current_nb_bytes_written;
12169 p_data += l_current_nb_bytes_written;
12170 p_total_data_size -= l_current_nb_bytes_written;
12172 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
12173 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
12174 l_current_nb_bytes_written = 0;
12175 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12176 l_nb_bytes_written += l_current_nb_bytes_written;
12177 p_data += l_current_nb_bytes_written;
12178 p_total_data_size -= l_current_nb_bytes_written;
12180 l_current_nb_bytes_written = 0;
12181 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12182 l_nb_bytes_written += l_current_nb_bytes_written;
12183 p_data += l_current_nb_bytes_written;
12184 p_total_data_size -= l_current_nb_bytes_written;
12187 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
12188 l_current_nb_bytes_written = 0;
12189 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
12190 l_nb_bytes_written += l_current_nb_bytes_written;
12191 p_data += l_current_nb_bytes_written;
12192 p_total_data_size -= l_current_nb_bytes_written;
12196 l_current_nb_bytes_written = 0;
12197 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12201 l_nb_bytes_written += l_current_nb_bytes_written;
12202 * p_data_written = l_nb_bytes_written;
12204 /* Writing Psot in SOT marker */
12205 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
12207 if (l_cp->m_specific_param.m_enc.m_cinema){
12208 j2k_update_tlm(p_j2k,l_nb_bytes_written);
12214 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
12216 OPJ_UINT32 * p_data_written,
12217 OPJ_UINT32 p_total_data_size,
12218 opj_stream_private_t *p_stream,
12219 struct opj_event_mgr * p_manager
12222 OPJ_UINT32 tilepartno=0;
12223 OPJ_UINT32 l_nb_bytes_written = 0;
12224 OPJ_UINT32 l_current_nb_bytes_written;
12225 OPJ_UINT32 l_part_tile_size;
12226 OPJ_UINT32 tot_num_tp;
12229 OPJ_BYTE * l_begin_data;
12230 opj_tcp_v2_t *l_tcp = 00;
12231 opj_tcd_v2_t * l_tcd = 00;
12232 opj_cp_v2_t * l_cp = 00;
12235 l_tcd = p_j2k->m_tcd;
12236 l_cp = &(p_j2k->m_cp);
12237 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12239 /*Get number of tile parts*/
12240 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
12242 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
12243 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12244 l_current_nb_bytes_written = 0;
12245 l_part_tile_size = 0;
12246 l_begin_data = p_data;
12248 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12252 l_nb_bytes_written += l_current_nb_bytes_written;
12253 p_data += l_current_nb_bytes_written;
12254 p_total_data_size -= l_current_nb_bytes_written;
12255 l_part_tile_size += l_nb_bytes_written;
12257 l_current_nb_bytes_written = 0;
12258 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12262 p_data += l_current_nb_bytes_written;
12263 l_nb_bytes_written += l_current_nb_bytes_written;
12264 p_total_data_size -= l_current_nb_bytes_written;
12265 l_part_tile_size += l_nb_bytes_written;
12267 /* Writing Psot in SOT marker */
12268 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12270 if (l_cp->m_specific_param.m_enc.m_cinema) {
12271 j2k_update_tlm(p_j2k,l_part_tile_size);
12274 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12277 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
12278 l_tcd->cur_pino = pino;
12280 /*Get number of tile parts*/
12281 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
12282 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
12283 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12284 l_current_nb_bytes_written = 0;
12285 l_part_tile_size = 0;
12286 l_begin_data = p_data;
12288 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12292 l_nb_bytes_written += l_current_nb_bytes_written;
12293 p_data += l_current_nb_bytes_written;
12294 p_total_data_size -= l_current_nb_bytes_written;
12295 l_part_tile_size += l_current_nb_bytes_written;
12297 l_current_nb_bytes_written = 0;
12299 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12303 l_nb_bytes_written += l_current_nb_bytes_written;
12304 p_data += l_current_nb_bytes_written;
12305 p_total_data_size -= l_current_nb_bytes_written;
12306 l_part_tile_size += l_current_nb_bytes_written;
12308 /* Writing Psot in SOT marker */
12309 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12311 if (l_cp->m_specific_param.m_enc.m_cinema) {
12312 j2k_update_tlm(p_j2k,l_part_tile_size);
12315 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12319 *p_data_written = l_nb_bytes_written;
12325 * Writes the updated tlm.
12327 * @param p_stream the stream to write data to.
12328 * @param p_j2k J2K codec.
12329 * @param p_manager the user event manager.
12331 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
12332 struct opj_stream_private *p_stream,
12333 struct opj_event_mgr * p_manager )
12335 OPJ_UINT32 l_tlm_size;
12336 OPJ_SIZE_T l_tlm_position, l_current_position;
12338 /* preconditions */
12339 assert(p_j2k != 00);
12340 assert(p_manager != 00);
12341 assert(p_stream != 00);
12343 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12344 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12345 l_current_position = opj_stream_tell(p_stream);
12347 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12351 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) {
12355 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12364 * Ends the encoding, i.e. frees memory.
12366 * @param p_stream the stream to write data to.
12367 * @param p_j2k J2K codec.
12368 * @param p_manager the user event manager.
12370 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
12371 struct opj_stream_private *p_stream,
12372 struct opj_event_mgr * p_manager )
12374 /* preconditions */
12375 assert(p_j2k != 00);
12376 assert(p_manager != 00);
12377 assert(p_stream != 00);
12379 tcd_destroy_v2(p_j2k->m_tcd);
12382 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12383 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12384 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12385 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12388 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12389 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12390 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12393 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12399 * Destroys the memory associated with the decoding of headers.
12401 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12402 opj_stream_private_t *p_stream,
12403 opj_event_mgr_t * p_manager )
12405 /* preconditions */
12406 assert(p_j2k != 00);
12407 assert(p_stream != 00);
12408 assert(p_manager != 00);
12410 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12411 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12412 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12415 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12424 * @param p_stream the stream to write data to.
12425 * @param p_j2k J2K codec.
12426 * @param p_manager the user event manager.
12428 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12429 struct opj_stream_private *p_stream,
12430 struct opj_event_mgr * p_manager )
12432 opj_codestream_info_t * l_cstr_info = 00;
12434 /* preconditions */
12435 assert(p_j2k != 00);
12436 assert(p_manager != 00);
12437 assert(p_stream != 00);
12440 /* TODO mergeV2: check this part which use cstr_info */
12441 /*l_cstr_info = p_j2k->cstr_info;
12445 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));
12447 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12448 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12450 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12452 l_cstr_info->tw = p_j2k->m_cp.tw;
12453 l_cstr_info->th = p_j2k->m_cp.th;
12455 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12456 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12457 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12458 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12460 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12462 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12464 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12466 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12467 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12470 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12472 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12474 /*l_cstr_info->maxmarknum = 100;
12475 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12476 l_cstr_info->marknum = 0;
12479 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);
12483 * Creates a tile-coder decoder.
12485 * @param p_stream the stream to write data to.
12486 * @param p_j2k J2K codec.
12487 * @param p_manager the user event manager.
12489 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12490 struct opj_stream_private *p_stream,
12491 struct opj_event_mgr * p_manager )
12493 /* preconditions */
12494 assert(p_j2k != 00);
12495 assert(p_manager != 00);
12496 assert(p_stream != 00);
12498 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12500 if (! p_j2k->m_tcd) {
12501 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12505 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12506 tcd_destroy_v2(p_j2k->m_tcd);
12517 * @param p_j2k the jpeg2000 codec.
12518 * @param p_stream the stream to write data to.
12519 * @param p_manager the user event manager.
12521 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12522 OPJ_UINT32 p_tile_index,
12524 OPJ_UINT32 p_data_size,
12525 opj_stream_private_t *p_stream,
12526 opj_event_mgr_t * p_manager )
12528 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12529 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12533 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12534 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);