2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9 * Copyright (c) 2006-2007, Parvatha Elangovan
10 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Copyright (c) 2011-2012, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
41 /** @name Local static functions */
45 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
47 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 static opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
53 opj_stream_private_t *p_stream,
54 opj_event_mgr_t * p_manager);
57 * The default encoding validation procedure without any extension.
59 * @param p_j2k the jpeg2000 codec to validate.
60 * @param p_stream the input stream to validate.
61 * @param p_manager the user event manager.
63 * @return true if the parameters are correct.
65 static opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
66 opj_stream_private_t *p_stream,
67 opj_event_mgr_t * p_manager );
70 * The default decoding validation procedure without any extension.
72 * @param p_j2k the jpeg2000 codec to validate.
73 * @param p_stream the input stream to validate.
74 * @param p_manager the user event manager.
76 * @return true if the parameters are correct.
78 static opj_bool j2k_decoding_validation ( opj_j2k_v2_t * p_j2k,
79 opj_stream_private_t *p_stream,
80 opj_event_mgr_t * p_manager );
83 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
84 * are valid. Developpers wanting to extend the library can add their own validation procedures.
86 static void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
89 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
90 * are valid. Developpers wanting to extend the library can add their own validation procedures.
92 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
95 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
96 * are valid. Developpers wanting to extend the library can add their own validation procedures.
98 static void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
101 * The mct encoding validation procedure.
103 * @param p_j2k the jpeg2000 codec to validate.
104 * @param p_stream the input stream to validate.
105 * @param p_manager the user event manager.
107 * @return true if the parameters are correct.
109 static opj_bool j2k_mct_validation (opj_j2k_v2_t * p_j2k,
110 opj_stream_private_t *p_stream,
111 opj_event_mgr_t * p_manager );
114 * Builds the tcd decoder to use to decode tile.
116 static opj_bool j2k_build_decoder ( opj_j2k_v2_t * p_j2k,
117 opj_stream_private_t *p_stream,
118 opj_event_mgr_t * p_manager );
120 * Builds the tcd encoder to use to encode tile.
122 static opj_bool j2k_build_encoder ( opj_j2k_v2_t * p_j2k,
123 opj_stream_private_t *p_stream,
124 opj_event_mgr_t * p_manager );
127 * Creates a tile-coder decoder.
129 * @param p_stream the stream to write data to.
130 * @param p_j2k J2K codec.
131 * @param p_manager the user event manager.
133 static opj_bool j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134 struct opj_stream_private *p_stream,
135 struct opj_event_mgr * p_manager );
138 * Excutes the given procedures on the given codec.
140 * @param p_procedure_list the list of procedures to execute
141 * @param p_j2k the jpeg2000 codec to execute the procedures on.
142 * @param p_stream the stream to execute the procedures on.
143 * @param p_manager the user manager.
145 * @return true if all the procedures were successfully executed.
147 static opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
148 opj_procedure_list_t * p_procedure_list,
149 opj_stream_private_t *p_stream,
150 opj_event_mgr_t * p_manager);
153 * Updates the rates of the tcp.
155 * @param p_stream the stream to write data to.
156 * @param p_j2k J2K codec.
157 * @param p_manager the user event manager.
159 static opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
160 opj_stream_private_t *p_stream,
161 opj_event_mgr_t * p_manager );
164 * Copies the decoding tile parameters onto all the tile parameters.
165 * Creates also the tile decoder.
167 static opj_bool j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
168 opj_stream_private_t *p_stream,
169 opj_event_mgr_t * p_manager );
172 * Destroys the memory associated with the decoding of headers.
174 static opj_bool j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
175 opj_stream_private_t *p_stream,
176 opj_event_mgr_t * p_manager );
179 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
180 * with the marker value.
181 * @param p_id Marker value to look up
183 * @return the handler associated with the id.
185 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
188 * Destroys a tile coding parameter structure.
190 * @param p_tcp the tile coding parameter to destroy.
192 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
195 * Destroys the data inside a tile coding parameter structure.
197 * @param p_tcp the tile coding parameter which contain data to destroy.
199 static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
202 * Destroys a coding parameter structure.
204 * @param p_cp the coding parameter to destroy.
206 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
210 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
212 * @param p_comp_no the component number to output.
213 * @param p_stream the stream to write data to.
214 * @param p_j2k J2K codec.
215 * @param p_manager the user event manager.
218 static opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
219 OPJ_UINT32 p_tile_no,
220 OPJ_UINT32 p_comp_no,
222 OPJ_UINT32 * p_header_size,
223 struct opj_event_mgr * p_manager );
226 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
228 * @param p_tile_no the tile index.
229 * @param p_comp_no the component being outputted.
230 * @param p_j2k the J2K codec.
232 * @return the number of bytes taken by the SPCod element.
234 static OPJ_UINT32 j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
235 OPJ_UINT32 p_tile_no,
236 OPJ_UINT32 p_comp_no );
239 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
240 * @param p_header_data the data contained in the COM box.
241 * @param p_j2k the jpeg2000 codec.
242 * @param p_header_size the size of the data contained in the COM marker.
243 * @param p_manager the user event manager.
245 static opj_bool j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
247 OPJ_BYTE * p_header_data,
248 OPJ_UINT32 * p_header_size,
249 opj_event_mgr_t * p_manager );
252 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
254 * @param p_tile_no the tile index.
255 * @param p_comp_no the component being outputted.
256 * @param p_j2k the J2K codec.
258 * @return the number of bytes taken by the SPCod element.
260 static OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
261 OPJ_UINT32 p_tile_no,
262 OPJ_UINT32 p_comp_no );
265 * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
267 * @param p_tile_no the tile to output.
268 * @param p_comp_no the component number to output.
269 * @param p_data the data buffer.
270 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
271 * @param p_j2k J2K codec.
272 * @param p_manager the user event manager.
275 static opj_bool j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
276 OPJ_UINT32 p_tile_no,
277 OPJ_UINT32 p_comp_no,
279 OPJ_UINT32 * p_header_size,
280 opj_event_mgr_t * p_manager);
283 * Updates the Tile Length Marker.
285 static void j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
288 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
290 * @param p_tile_no the tile to output.
291 * @param p_comp_no the component number to output.
292 * @param p_data the data buffer.
293 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
294 * @param p_j2k J2K codec.
295 * @param p_manager the user event manager.
298 static opj_bool j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
300 OPJ_BYTE * p_header_data,
301 OPJ_UINT32 * p_header_size,
302 opj_event_mgr_t * p_manager );
305 * Copies the tile component parameters of all the component from the first tile component.
307 * @param p_j2k the J2k codec.
309 static void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k );
312 * Copies the tile quantization parameters of all the component from the first tile component.
314 * @param p_j2k the J2k codec.
316 static void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
321 static opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
322 opj_stream_private_t *p_stream,
323 opj_event_mgr_t * p_manager);
326 static opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
327 OPJ_UINT32 p_tile_index,
328 opj_stream_private_t *p_stream,
329 opj_event_mgr_t * p_manager );
331 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
333 static void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
335 static opj_bool j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
337 OPJ_UINT32 p_data_size,
338 opj_stream_private_t *p_stream,
339 opj_event_mgr_t * p_manager );
342 * Sets up the procedures to do on writing header.
343 * Developers wanting to extend the library can add their own writing procedures.
345 static void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
347 static opj_bool j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k,
349 OPJ_UINT32 * p_data_written,
350 OPJ_UINT32 p_total_data_size,
351 opj_stream_private_t *p_stream,
352 struct opj_event_mgr * p_manager );
354 static opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
356 OPJ_UINT32 * p_data_written,
357 OPJ_UINT32 p_total_data_size,
358 opj_stream_private_t *p_stream,
359 struct opj_event_mgr * p_manager );
362 * Gets the offset of the header.
364 * @param p_stream the stream to write data to.
365 * @param p_j2k J2K codec.
366 * @param p_manager the user event manager.
368 static opj_bool j2k_get_end_header( opj_j2k_v2_t *p_j2k,
369 struct opj_stream_private *p_stream,
370 struct opj_event_mgr * p_manager );
373 * -----------------------------------------------------------------------
374 * -----------------------------------------------------------------------
375 * -----------------------------------------------------------------------
379 Write the SOC marker (Start Of Codestream)
380 @param j2k J2K handle
382 static void j2k_write_soc(opj_j2k_t *j2k);
385 * Writes the SOC marker (Start Of Codestream)
387 * @param p_stream the stream to write data to.
388 * @param p_j2k J2K codec.
389 * @param p_manager the user event manager.
391 static opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
392 struct opj_stream_private *p_stream,
393 struct opj_event_mgr * p_manager );
396 * Reads a SOC marker (Start of Codestream)
397 * @param p_header_data the data contained in the SOC box.
398 * @param jp2 the jpeg2000 file codec.
399 * @param p_header_size the size of the data contained in the SOC marker.
400 * @param p_manager the user event manager.
402 static opj_bool opj_j2k_read_soc(
404 struct opj_stream_private *p_stream,
405 struct opj_event_mgr * p_manager
409 Write the SIZ marker (image and tile size)
410 @param j2k J2K handle
412 static void j2k_write_siz(opj_j2k_t *j2k);
415 * Writes the SIZ marker (image and tile size)
417 * @param p_stream the stream to write data to.
418 * @param p_j2k J2K codec.
419 * @param p_manager the user event manager.
421 static opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
422 struct opj_stream_private *p_stream,
423 struct opj_event_mgr * p_manager );
426 Read the SIZ marker (image and tile size)
427 @param j2k J2K handle
429 static void j2k_read_siz(opj_j2k_t *j2k);
432 * Reads a SIZ marker (image and tile size)
433 * @param p_header_data the data contained in the SIZ box.
434 * @param jp2 the jpeg2000 file codec.
435 * @param p_header_size the size of the data contained in the SIZ marker.
436 * @param p_manager the user event manager.
438 static opj_bool j2k_read_siz_v2 (
440 OPJ_BYTE * p_header_data,
441 OPJ_UINT32 p_header_size,
442 struct opj_event_mgr * p_manager
446 Write the COM marker (comment)
447 @param j2k J2K handle
449 static void j2k_write_com(opj_j2k_t *j2k);
452 * Writes the COM marker (comment)
454 * @param p_stream the stream to write data to.
455 * @param p_j2k J2K codec.
456 * @param p_manager the user event manager.
458 static opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
459 struct opj_stream_private *p_stream,
460 struct opj_event_mgr * p_manager );
463 Read the COM marker (comment)
464 @param j2k J2K handle
466 static void j2k_read_com(opj_j2k_t *j2k);
468 * Reads a COM marker (comments)
469 * @param p_header_data the data contained in the COM box.
470 * @param jp2 the jpeg2000 file codec.
471 * @param p_header_size the size of the data contained in the COM marker.
472 * @param p_manager the user event manager.
474 static opj_bool j2k_read_com_v2 (
476 OPJ_BYTE * p_header_data,
477 OPJ_UINT32 p_header_size,
478 struct opj_event_mgr * p_manager
481 Write the value concerning the specified component in the marker COD and COC
482 @param j2k J2K handle
483 @param compno Number of the component concerned by the information written
485 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
487 Read the value concerning the specified component in the marker COD and COC
488 @param j2k J2K handle
489 @param compno Number of the component concerned by the information read
491 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
493 Write the COD marker (coding style default)
494 @param j2k J2K handle
496 static void j2k_write_cod(opj_j2k_t *j2k);
499 * Writes the COD marker (Coding style default)
501 * @param p_stream the stream to write data to.
502 * @param p_j2k J2K codec.
503 * @param p_manager the user event manager.
505 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
506 struct opj_stream_private *p_stream,
507 struct opj_event_mgr * p_manager );
510 Read the COD marker (coding style default)
511 @param j2k J2K handle
513 static void j2k_read_cod(opj_j2k_t *j2k);
516 * Reads a COD marker (Coding Styke defaults)
517 * @param p_header_data the data contained in the COD box.
518 * @param p_j2k the jpeg2000 codec.
519 * @param p_header_size the size of the data contained in the COD marker.
520 * @param p_manager the user event manager.
522 static opj_bool j2k_read_cod_v2 (
524 OPJ_BYTE * p_header_data,
525 OPJ_UINT32 p_header_size,
526 struct opj_event_mgr * p_manager
530 Write the COC marker (coding style component)
531 @param j2k J2K handle
532 @param compno Number of the component concerned by the information written
534 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
537 * Writes the COC marker (Coding style component)
539 * @param p_comp_number the index of the component to output.
540 * @param p_stream the stream to write data to.
541 * @param p_j2k J2K codec.
542 * @param p_manager the user event manager.
544 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
545 OPJ_UINT32 p_comp_number,
546 struct opj_stream_private *p_stream,
547 struct opj_event_mgr * p_manager
550 * Writes the COC marker (Coding style component)
552 * @param p_comp_no the index of the component to output.
553 * @param p_stream the stream to write data to.
554 * @param p_j2k J2K codec.
555 * @param p_manager the user event manager.
557 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
558 OPJ_UINT32 p_comp_no,
560 OPJ_UINT32 * p_data_written,
561 struct opj_event_mgr * p_manager );
564 * Gets the maximum size taken by a coc.
566 * @param p_j2k the jpeg2000 codec to use.
568 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
571 Read the COC marker (coding style component)
572 @param j2k J2K handle
574 static void j2k_read_coc(opj_j2k_t *j2k);
577 * Reads a COC marker (Coding Style Component)
578 * @param p_header_data the data contained in the COC box.
579 * @param p_j2k the jpeg2000 codec.
580 * @param p_header_size the size of the data contained in the COC marker.
581 * @param p_manager the user event manager.
583 static opj_bool j2k_read_coc_v2 (
585 OPJ_BYTE * p_header_data,
586 OPJ_UINT32 p_header_size,
587 struct opj_event_mgr * p_manager
591 Write the value concerning the specified component in the marker QCD and QCC
592 @param j2k J2K handle
593 @param compno Number of the component concerned by the information written
595 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
597 Read the value concerning the specified component in the marker QCD and QCC
598 @param j2k J2K handle
599 @param compno Number of the component concern by the information read
600 @param len Length of the information in the QCX part of the marker QCD/QCC
602 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
604 Write the QCD marker (quantization default)
605 @param j2k J2K handle
607 static void j2k_write_qcd(opj_j2k_t *j2k);
610 * Writes the QCD marker (quantization default)
612 * @param p_comp_number the index of the component to output.
613 * @param p_stream the stream to write data to.
614 * @param p_j2k J2K codec.
615 * @param p_manager the user event manager.
617 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
618 struct opj_stream_private *p_stream,
619 struct opj_event_mgr * p_manager );
622 Read the QCD marker (quantization default)
623 @param j2k J2K handle
625 static void j2k_read_qcd(opj_j2k_t *j2k);
628 * Reads a QCD marker (Quantization defaults)
629 * @param p_header_data the data contained in the QCD box.
630 * @param p_j2k the jpeg2000 codec.
631 * @param p_header_size the size of the data contained in the QCD marker.
632 * @param p_manager the user event manager.
634 static opj_bool j2k_read_qcd_v2 (
636 OPJ_BYTE * p_header_data,
637 OPJ_UINT32 p_header_size,
638 struct opj_event_mgr * p_manager
642 Write the QCC marker (quantization component)
643 @param j2k J2K handle
644 @param compno Number of the component concerned by the information written
646 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
649 * Writes the QCC marker (quantization component)
651 * @param p_comp_no the index of the component to output.
652 * @param p_stream the stream to write data to.
653 * @param p_j2k J2K codec.
654 * @param p_manager the user event manager.
656 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
657 OPJ_UINT32 p_comp_no,
658 struct opj_stream_private *p_stream,
659 struct opj_event_mgr * p_manager );
662 * Writes the QCC marker (quantization component)
664 * @param p_comp_no the index of the component to output.
665 * @param p_stream the stream to write data to.
666 * @param p_j2k J2K codec.
667 * @param p_manager the user event manager.
669 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
670 OPJ_UINT32 p_comp_no,
672 OPJ_UINT32 * p_data_written,
673 struct opj_event_mgr * p_manager );
676 * Gets the maximum size taken by a qcc.
678 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
681 Read the QCC marker (quantization component)
682 @param j2k J2K handle
684 static void j2k_read_qcc(opj_j2k_t *j2k);
686 * Reads a QCC marker (Quantization component)
687 * @param p_header_data the data contained in the QCC box.
688 * @param p_j2k the jpeg2000 codec.
689 * @param p_header_size the size of the data contained in the QCC marker.
690 * @param p_manager the user event manager.
692 static opj_bool j2k_read_qcc_v2(
694 OPJ_BYTE * p_header_data,
695 OPJ_UINT32 p_header_size,
696 struct opj_event_mgr * p_manager);
699 Write the POC marker (progression order change)
700 @param j2k J2K handle
702 static void j2k_write_poc(opj_j2k_t *j2k);
705 * Writes the POC marker (Progression Order Change)
707 * @param p_stream the stream to write data to.
708 * @param p_j2k J2K codec.
709 * @param p_manager the user event manager.
711 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
712 struct opj_stream_private *p_stream,
713 struct opj_event_mgr * p_manager );
716 * Writes the POC marker (Progression Order Change)
718 * @param p_stream the stream to write data to.
719 * @param p_j2k J2K codec.
720 * @param p_manager the user event manager.
722 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
724 OPJ_UINT32 * p_data_written,
725 struct opj_event_mgr * p_manager );
728 * Gets the maximum size taken by the writting of a POC.
730 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
733 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
735 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
738 * Gets the maximum size taken by the headers of the SOT.
740 * @param p_j2k the jpeg2000 codec to use.
742 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
745 Read the POC marker (progression order change)
746 @param j2k J2K handle
748 static void j2k_read_poc(opj_j2k_t *j2k);
750 * Reads a POC marker (Progression Order Change)
752 * @param p_header_data the data contained in the POC box.
753 * @param p_j2k the jpeg2000 codec.
754 * @param p_header_size the size of the data contained in the POC marker.
755 * @param p_manager the user event manager.
757 static opj_bool j2k_read_poc_v2 (
759 OPJ_BYTE * p_header_data,
760 OPJ_UINT32 p_header_size,
761 struct opj_event_mgr * p_manager
765 Read the CRG marker (component registration)
766 @param j2k J2K handle
768 static void j2k_read_crg(opj_j2k_t *j2k);
770 * Reads a CRG marker (Component registration)
772 * @param p_header_data the data contained in the TLM box.
773 * @param p_j2k the jpeg2000 codec.
774 * @param p_header_size the size of the data contained in the TLM marker.
775 * @param p_manager the user event manager.
777 static opj_bool j2k_read_crg_v2 (
779 OPJ_BYTE * p_header_data,
780 OPJ_UINT32 p_header_size,
781 struct opj_event_mgr * p_manager
784 Read the TLM marker (tile-part lengths)
785 @param j2k J2K handle
787 static void j2k_read_tlm(opj_j2k_t *j2k);
789 * Reads a TLM marker (Tile Length Marker)
791 * @param p_header_data the data contained in the TLM box.
792 * @param p_j2k the jpeg2000 codec.
793 * @param p_header_size the size of the data contained in the TLM marker.
794 * @param p_manager the user event manager.
796 static opj_bool j2k_read_tlm_v2 (
798 OPJ_BYTE * p_header_data,
799 OPJ_UINT32 p_header_size,
800 struct opj_event_mgr * p_manager
804 * Writes the updated tlm.
806 * @param p_stream the stream to write data to.
807 * @param p_j2k J2K codec.
808 * @param p_manager the user event manager.
810 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
811 struct opj_stream_private *p_stream,
812 struct opj_event_mgr * p_manager );
815 Read the PLM marker (packet length, main header)
816 @param j2k J2K handle
818 static void j2k_read_plm(opj_j2k_t *j2k);
821 * Reads a PLM marker (Packet length, main header marker)
823 * @param p_header_data the data contained in the TLM box.
824 * @param p_j2k the jpeg2000 codec.
825 * @param p_header_size the size of the data contained in the TLM marker.
826 * @param p_manager the user event manager.
828 static opj_bool j2k_read_plm_v2 (
830 OPJ_BYTE * p_header_data,
831 OPJ_UINT32 p_header_size,
832 struct opj_event_mgr * p_manager
835 Read the PLT marker (packet length, tile-part header)
836 @param j2k J2K handle
838 static void j2k_read_plt(opj_j2k_t *j2k);
840 * Reads a PLT marker (Packet length, tile-part header)
842 * @param p_header_data the data contained in the PLT box.
843 * @param p_j2k the jpeg2000 codec.
844 * @param p_header_size the size of the data contained in the PLT marker.
845 * @param p_manager the user event manager.
847 static opj_bool j2k_read_plt_v2 (
849 OPJ_BYTE * p_header_data,
850 OPJ_UINT32 p_header_size,
851 struct opj_event_mgr * p_manager
854 Read the PPM marker (packet packet headers, main header)
855 @param j2k J2K handle
857 static void j2k_read_ppm(opj_j2k_t *j2k);
859 * Reads a PPM marker (Packed packet headers, main header)
861 * @param p_header_data the data contained in the POC box.
862 * @param p_j2k the jpeg2000 codec.
863 * @param p_header_size the size of the data contained in the POC marker.
864 * @param p_manager the user event manager.
867 static opj_bool j2k_read_ppm_v2 (
869 OPJ_BYTE * p_header_data,
870 OPJ_UINT32 p_header_size,
871 struct opj_event_mgr * p_manager
875 static opj_bool j2k_read_ppm_v3 (
877 OPJ_BYTE * p_header_data,
878 OPJ_UINT32 p_header_size,
879 struct opj_event_mgr * p_manager
883 Read the PPT marker (packet packet headers, tile-part header)
884 @param j2k J2K handle
886 static void j2k_read_ppt(opj_j2k_t *j2k);
888 * Reads a PPT marker (Packed packet headers, tile-part header)
890 * @param p_header_data the data contained in the PPT box.
891 * @param p_j2k the jpeg2000 codec.
892 * @param p_header_size the size of the data contained in the PPT marker.
893 * @param p_manager the user event manager.
895 static opj_bool j2k_read_ppt_v2 (
897 OPJ_BYTE * p_header_data,
898 OPJ_UINT32 p_header_size,
899 struct opj_event_mgr * p_manager
902 Write the TLM marker (Mainheader)
903 @param j2k J2K handle
905 static void j2k_write_tlm(opj_j2k_t *j2k);
908 * Writes the TLM marker (Tile Length Marker)
910 * @param p_stream the stream to write data to.
911 * @param p_j2k J2K codec.
912 * @param p_manager the user event manager.
914 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
915 struct opj_stream_private *p_stream,
916 struct opj_event_mgr * p_manager );
919 Write the SOT marker (start of tile-part)
920 @param j2k J2K handle
922 static void j2k_write_sot(opj_j2k_t *j2k);
925 * Writes the SOT marker (Start of tile-part)
927 * @param p_stream the stream to write data to.
928 * @param p_j2k J2K codec.
929 * @param p_manager the user event manager.
931 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
933 OPJ_UINT32 * p_data_written,
934 const struct opj_stream_private *p_stream,
935 struct opj_event_mgr * p_manager );
938 * Reads a PPT marker (Packed packet headers, tile-part header)
940 * @param p_header_data the data contained in the PPT box.
941 * @param p_j2k the jpeg2000 codec.
942 * @param p_header_size the size of the data contained in the PPT marker.
943 * @param p_manager the user event manager.
945 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
946 OPJ_BYTE * p_header_data,
947 OPJ_UINT32 p_header_size,
948 opj_event_mgr_t * p_manager );
951 Write the SOD marker (start of data)
952 @param j2k J2K handle
953 @param tile_coder Pointer to a TCD handle
955 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
958 * Writes the SOD marker (Start of data)
960 * @param p_stream the stream to write data to.
961 * @param p_j2k J2K codec.
962 * @param p_manager the user event manager.
964 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
965 struct opj_tcd_v2 * p_tile_coder,
967 OPJ_UINT32 * p_data_written,
968 OPJ_UINT32 p_total_data_size,
969 const struct opj_stream_private *p_stream,
970 struct opj_event_mgr * p_manager );
973 * Reads a SOD marker (Start Of Data)
975 * @param p_header_data the data contained in the SOD box.
976 * @param p_j2k the jpeg2000 codec.
977 * @param p_header_size the size of the data contained in the SOD marker.
978 * @param p_manager the user event manager.
980 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
981 opj_stream_private_t *p_stream,
982 opj_event_mgr_t * p_manager );
985 * Updates the Tile Length Marker.
987 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
989 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
990 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
992 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
993 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
997 Write the RGN marker (region-of-interest)
998 @param j2k J2K handle
999 @param compno Number of the component concerned by the information written
1000 @param tileno Number of the tile concerned by the information written
1002 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
1005 * Writes the RGN marker (Region Of Interest)
1007 * @param p_tile_no the tile to output
1008 * @param p_comp_no the component to output
1009 * @param p_stream the stream to write data to.
1010 * @param p_j2k J2K codec.
1011 * @param p_manager the user event manager.
1013 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
1014 OPJ_UINT32 p_tile_no,
1015 OPJ_UINT32 p_comp_no,
1016 struct opj_stream_private *p_stream,
1017 struct opj_event_mgr * p_manager );
1020 Read the RGN marker (region-of-interest)
1021 @param j2k J2K handle
1023 static void j2k_read_rgn(opj_j2k_t *j2k);
1026 * Reads a RGN marker (Region Of Interest)
1028 * @param p_header_data the data contained in the POC box.
1029 * @param p_j2k the jpeg2000 codec.
1030 * @param p_header_size the size of the data contained in the POC marker.
1031 * @param p_manager the user event manager.
1033 static opj_bool j2k_read_rgn_v2 (
1034 opj_j2k_v2_t *p_j2k,
1035 OPJ_BYTE * p_header_data,
1036 OPJ_UINT32 p_header_size,
1037 struct opj_event_mgr * p_manager
1041 * Writes the EOC marker (End of Codestream)
1043 * @param p_stream the stream to write data to.
1044 * @param p_j2k J2K codec.
1045 * @param p_manager the user event manager.
1047 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
1048 struct opj_stream_private *p_stream,
1049 struct opj_event_mgr * p_manager );
1052 Write the EOC marker (end of codestream)
1053 @param j2k J2K handle
1055 static void j2k_write_eoc(opj_j2k_t *j2k);
1057 Read the EOC marker (end of codestream)
1058 @param j2k J2K handle
1060 static void j2k_read_eoc(opj_j2k_t *j2k);
1063 * Reads a EOC marker (End Of Codestream)
1065 * @param p_header_data the data contained in the SOD box.
1066 * @param p_j2k the jpeg2000 codec.
1067 * @param p_header_size the size of the data contained in the SOD marker.
1068 * @param p_manager the user event manager.
1071 static opj_bool j2k_read_eoc_v2 (
1072 opj_j2k_v2_t *p_j2k,
1073 struct opj_stream_private *p_stream,
1074 struct opj_event_mgr * p_manager
1081 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1083 * @param p_stream the stream to write data to.
1084 * @param p_j2k J2K codec.
1085 * @param p_manager the user event manager.
1087 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1088 struct opj_stream_private *p_stream,
1089 struct opj_event_mgr * p_manager );
1094 * @param p_stream the stream to write data to.
1095 * @param p_j2k J2K codec.
1096 * @param p_manager the user event manager.
1098 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1099 struct opj_stream_private *p_stream,
1100 struct opj_event_mgr * p_manager );
1103 Read an unknown marker
1104 @param j2k J2K handle
1106 static void j2k_read_unk(opj_j2k_t *j2k);
1108 Add main header marker information
1109 @param cstr_info Codestream information structure
1110 @param type marker type
1111 @param pos byte offset of marker segment
1112 @param len length of marker segment
1114 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1116 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1118 Add tile header marker information
1119 @param tileno tile index number
1120 @param cstr_info Codestream information structure
1121 @param type marker type
1122 @param pos byte offset of marker segment
1123 @param len length of marker segment
1125 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1127 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);
1130 * Reads an unknown marker
1132 * @param p_stream the stream object to read from.
1133 * @param p_j2k the jpeg2000 codec.
1134 * @param p_manager the user event manager.
1136 * @return true if the marker could be deduced.
1138 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1139 struct opj_stream_private *p_stream,
1140 OPJ_UINT32 *output_marker,
1141 struct opj_event_mgr * p_manager );
1144 * Writes the MCT marker (Multiple Component Transform)
1146 * @param p_stream the stream to write data to.
1147 * @param p_j2k J2K codec.
1148 * @param p_manager the user event manager.
1150 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1151 opj_mct_data_t * p_mct_record,
1152 struct opj_stream_private *p_stream,
1153 struct opj_event_mgr * p_manager );
1156 * Reads a MCT marker (Multiple Component Transform)
1158 * @param p_header_data the data contained in the MCT box.
1159 * @param p_j2k the jpeg2000 codec.
1160 * @param p_header_size the size of the data contained in the MCT marker.
1161 * @param p_manager the user event manager.
1163 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1164 OPJ_BYTE * p_header_data,
1165 OPJ_UINT32 p_header_size,
1166 struct opj_event_mgr * p_manager );
1169 * Writes the MCC marker (Multiple Component Collection)
1171 * @param p_stream the stream to write data to.
1172 * @param p_j2k J2K codec.
1173 * @param p_manager the user event manager.
1175 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1176 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1177 struct opj_stream_private *p_stream,
1178 struct opj_event_mgr * p_manager );
1182 * Reads a MCC marker (Multiple Component Collection)
1184 * @param p_header_data the data contained in the MCC box.
1185 * @param p_j2k the jpeg2000 codec.
1186 * @param p_header_size the size of the data contained in the MCC marker.
1187 * @param p_manager the user event manager.
1189 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1190 OPJ_BYTE * p_header_data,
1191 OPJ_UINT32 p_header_size,
1192 struct opj_event_mgr * p_manager );
1195 * Writes the MCO marker (Multiple component transformation ordering)
1197 * @param p_stream the stream to write data to.
1198 * @param p_j2k J2K codec.
1199 * @param p_manager the user event manager.
1201 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1202 struct opj_stream_private *p_stream,
1203 struct opj_event_mgr * p_manager );
1206 * Reads a MCO marker (Multiple Component Transform Ordering)
1208 * @param p_header_data the data contained in the MCO box.
1209 * @param p_j2k the jpeg2000 codec.
1210 * @param p_header_size the size of the data contained in the MCO marker.
1211 * @param p_manager the user event manager.
1213 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1214 OPJ_BYTE * p_header_data,
1215 OPJ_UINT32 p_header_size,
1216 struct opj_event_mgr * p_manager );
1218 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1220 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1221 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1222 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1223 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1225 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1226 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1227 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1228 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1230 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1231 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1232 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1233 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1237 * Ends the encoding, i.e. frees memory.
1239 * @param p_stream the stream to write data to.
1240 * @param p_j2k J2K codec.
1241 * @param p_manager the user event manager.
1243 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1244 struct opj_stream_private *p_stream,
1245 struct opj_event_mgr * p_manager );
1248 * Writes the CBD marker (Component bit depth definition)
1250 * @param p_stream the stream to write data to.
1251 * @param p_j2k J2K codec.
1252 * @param p_manager the user event manager.
1254 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1255 struct opj_stream_private *p_stream,
1256 struct opj_event_mgr * p_manager );
1259 * Reads a CBD marker (Component bit depth definition)
1260 * @param p_header_data the data contained in the CBD box.
1261 * @param p_j2k the jpeg2000 codec.
1262 * @param p_header_size the size of the data contained in the CBD marker.
1263 * @param p_manager the user event manager.
1265 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1266 OPJ_BYTE * p_header_data,
1267 OPJ_UINT32 p_header_size,
1268 struct opj_event_mgr * p_manager);
1271 * Writes the image components.
1273 * @param p_stream the stream to write data to.
1274 * @param p_j2k J2K codec.
1275 * @param p_manager the user event manager.
1277 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1278 struct opj_stream_private *p_stream,
1279 struct opj_event_mgr * p_manager );
1282 * Writes regions of interests.
1284 * @param p_stream the stream to write data to.
1285 * @param p_j2k J2K codec.
1286 * @param p_manager the user event manager.
1288 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1289 struct opj_stream_private *p_stream,
1290 struct opj_event_mgr * p_manager );
1295 * @param p_stream the stream to write data to.
1296 * @param p_j2k J2K codec.
1297 * @param p_manager the user event manager.
1299 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1300 struct opj_stream_private *p_stream,
1301 struct opj_event_mgr * p_manager );
1304 * Checks the progression order changes values. Tells of the poc given as input are valid.
1305 * A nice message is outputted at errors.
1307 * @param p_pocs the progression order changes.
1308 * @param p_nb_pocs the number of progression order changes.
1309 * @param p_nb_resolutions the number of resolutions.
1310 * @param numcomps the number of components
1311 * @param numlayers the number of layers.
1313 * @return true if the pocs are valid.
1315 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1316 OPJ_UINT32 p_nb_pocs,
1317 OPJ_UINT32 p_nb_resolutions,
1318 OPJ_UINT32 numcomps,
1319 OPJ_UINT32 numlayers,
1320 opj_event_mgr_t * p_manager);
1323 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1325 * @param cp the coding parameters.
1326 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1327 * @param tileno the given tile.
1329 * @return the number of tile parts.
1331 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1333 /** mem allocation for TLM marker*/
1334 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1337 * Calculates the total number of tile parts needed by the encoder to
1338 * encode such an image. If not enough memory is available, then the function return false.
1340 * @param p_nb_tiles pointer that will hold the number of tile parts.
1341 * @param cp the coding parameters for the image.
1342 * @param image the image to encode.
1343 * @param p_j2k the p_j2k encoder.
1344 * @param p_manager the user event manager.
1346 * @return true if the function was successful, false else.
1348 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1350 OPJ_UINT32 * p_nb_tiles,
1352 opj_event_mgr_t * p_manager);
1354 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1356 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1358 static opj_codestream_index_t* j2k_create_cstr_index(void);
1364 /* ----------------------------------------------------------------------- */
1365 typedef struct j2k_prog_order{
1366 OPJ_PROG_ORDER enum_prog;
1370 j2k_prog_order_t j2k_prog_order_list[] = {
1376 {(OPJ_PROG_ORDER)-1, ""}
1384 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1392 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1394 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1396 j2k_read_int16_to_float,
1397 j2k_read_int32_to_float,
1398 j2k_read_float32_to_float,
1399 j2k_read_float64_to_float
1402 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1404 j2k_read_int16_to_int32,
1405 j2k_read_int32_to_int32,
1406 j2k_read_float32_to_int32,
1407 j2k_read_float64_to_int32
1410 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1412 j2k_write_float_to_int16,
1413 j2k_write_float_to_int32,
1414 j2k_write_float_to_float,
1415 j2k_write_float_to_float64
1418 typedef struct opj_dec_memory_marker_handler
1422 /** value of the state when the marker can appear */
1424 /** action linked to the marker */
1425 opj_bool (*handler) (
1426 opj_j2k_v2_t *p_j2k,
1427 OPJ_BYTE * p_header_data,
1428 OPJ_UINT32 p_header_size,
1429 struct opj_event_mgr * p_manager
1432 opj_dec_memory_marker_handler_t;
1434 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1437 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1438 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1439 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1440 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1441 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1442 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1443 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1444 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1445 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1446 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1447 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1448 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1449 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1451 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1452 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1453 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1454 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1455 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1456 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1458 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1459 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1460 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1461 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1462 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1463 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1464 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1465 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
1466 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1467 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1468 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1469 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1470 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1472 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1473 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1474 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1475 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1476 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1477 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1479 #ifdef TODO_MS /* FIXME */
1480 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1481 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1482 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1483 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1485 #endif /* USE_JPWL */
1487 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1488 {J2K_MS_INSEC, 0, j2k_read_insec}
1489 #endif /* USE_JPSEC */
1490 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1495 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1497 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1498 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1502 for (i=0;i<p_nb_elem;++i) {
1503 opj_read_bytes(l_src_data,&l_temp,2);
1505 l_src_data+=sizeof(OPJ_INT16);
1507 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1511 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1513 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1514 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1518 for (i=0;i<p_nb_elem;++i) {
1519 opj_read_bytes(l_src_data,&l_temp,4);
1521 l_src_data+=sizeof(OPJ_INT32);
1523 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1527 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1529 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1530 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1534 for (i=0;i<p_nb_elem;++i) {
1535 opj_read_float(l_src_data,&l_temp);
1537 l_src_data+=sizeof(OPJ_FLOAT32);
1539 *(l_dest_data++) = l_temp;
1543 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1545 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1546 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1550 for (i=0;i<p_nb_elem;++i) {
1551 opj_read_double(l_src_data,&l_temp);
1553 l_src_data+=sizeof(OPJ_FLOAT64);
1555 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1559 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1561 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1562 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1566 for (i=0;i<p_nb_elem;++i) {
1567 opj_read_bytes(l_src_data,&l_temp,2);
1569 l_src_data+=sizeof(OPJ_INT16);
1571 *(l_dest_data++) = (OPJ_INT32) l_temp;
1575 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1577 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1578 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1582 for (i=0;i<p_nb_elem;++i) {
1583 opj_read_bytes(l_src_data,&l_temp,4);
1585 l_src_data+=sizeof(OPJ_INT32);
1587 *(l_dest_data++) = (OPJ_INT32) l_temp;
1591 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1593 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1594 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1598 for (i=0;i<p_nb_elem;++i) {
1599 opj_read_float(l_src_data,&l_temp);
1601 l_src_data+=sizeof(OPJ_FLOAT32);
1603 *(l_dest_data++) = (OPJ_INT32) l_temp;
1607 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1609 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1610 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1614 for (i=0;i<p_nb_elem;++i) {
1615 opj_read_double(l_src_data,&l_temp);
1617 l_src_data+=sizeof(OPJ_FLOAT64);
1619 *(l_dest_data++) = (OPJ_INT32) l_temp;
1623 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1625 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1626 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1630 for (i=0;i<p_nb_elem;++i) {
1631 l_temp = (OPJ_UINT32) *(l_src_data++);
1633 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1635 l_dest_data+=sizeof(OPJ_INT16);
1639 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1641 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1642 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1646 for (i=0;i<p_nb_elem;++i) {
1647 l_temp = (OPJ_UINT32) *(l_src_data++);
1649 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1651 l_dest_data+=sizeof(OPJ_INT32);
1655 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1657 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1658 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1662 for (i=0;i<p_nb_elem;++i) {
1663 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1665 opj_write_float(l_dest_data,l_temp);
1667 l_dest_data+=sizeof(OPJ_FLOAT32);
1671 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1673 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1674 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1678 for (i=0;i<p_nb_elem;++i) {
1679 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1681 opj_write_double(l_dest_data,l_temp);
1683 l_dest_data+=sizeof(OPJ_FLOAT64);
1689 * Converts an enum type progression order to string type.
1691 * @param prg_order the progression order to get.
1693 * @return the string representation of the given progression order.
1695 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1696 j2k_prog_order_t *po;
1697 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1698 if(po->enum_prog == prg_order){
1699 return po->str_prog;
1702 return po->str_prog;
1706 * Checks the progression order changes values. Tells if the poc given as input are valid.
1708 * @param p_pocs the progression order changes.
1709 * @param p_nb_pocs the number of progression order changes.
1710 * @param p_nb_resolutions the number of resolutions.
1711 * @param numcomps the number of components
1712 * @param numlayers the number of layers.
1713 * @param p_manager the user event manager.
1715 * @return true if the pocs are valid.
1717 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1718 OPJ_UINT32 p_nb_pocs,
1719 OPJ_UINT32 p_nb_resolutions,
1720 OPJ_UINT32 p_num_comps,
1721 OPJ_UINT32 p_num_layers,
1722 opj_event_mgr_t * p_manager)
1724 OPJ_UINT32* packet_array;
1725 OPJ_UINT32 index , resno, compno, layno;
1727 OPJ_UINT32 step_c = 1;
1728 OPJ_UINT32 step_r = p_num_comps * step_c;
1729 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1730 opj_bool loss = OPJ_FALSE;
1731 OPJ_UINT32 layno0 = 0;
1733 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1734 if (packet_array == 00) {
1735 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1738 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1740 if (p_nb_pocs == 0) {
1744 index = step_r * p_pocs->resno0;
1745 // take each resolution for each poc
1746 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1748 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1750 // take each comp of each resolution for each poc
1751 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1752 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1754 // and finally take each layer of each res of ...
1755 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1756 //index = step_r * resno + step_c * compno + step_l * layno;
1757 packet_array[comp_index] = 1;
1758 comp_index += step_l;
1761 res_index += step_c;
1768 // iterate through all the pocs
1769 for (i = 1; i < p_nb_pocs ; ++i) {
1770 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1772 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1773 index = step_r * p_pocs->resno0;
1775 // take each resolution for each poc
1776 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1777 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1779 // take each comp of each resolution for each poc
1780 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1781 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1783 // and finally take each layer of each res of ...
1784 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1785 //index = step_r * resno + step_c * compno + step_l * layno;
1786 packet_array[comp_index] = 1;
1787 comp_index += step_l;
1790 res_index += step_c;
1800 for (layno = 0; layno < p_num_layers ; ++layno) {
1801 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1802 for (compno = 0; compno < p_num_comps; ++compno) {
1803 loss |= (packet_array[index]!=1);
1804 //index = step_r * resno + step_c * compno + step_l * layno;
1811 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1814 opj_free(packet_array);
1819 /* ----------------------------------------------------------------------- */
1820 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1823 int tpnum=1,tpend=0;
1824 opj_tcp_t *tcp = &cp->tcps[tileno];
1825 prog = j2k_convert_progression_order(tcp->prg);
1830 if( cp->tp_flag == prog[i] ){
1831 tpend=1;cp->tp_pos=i;
1835 tpnum= tpnum * tcp->pocs[pino].compE;
1838 tpnum= tpnum * tcp->pocs[pino].resE;
1841 tpnum= tpnum * tcp->pocs[pino].prcE;
1844 tpnum= tpnum * tcp->pocs[pino].layE;
1856 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1858 * @param cp the coding parameters.
1859 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1860 * @param tileno the given tile.
1862 * @return the number of tile parts.
1864 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1866 const OPJ_CHAR *prog = 00;
1868 OPJ_UINT32 tpnum = 1;
1869 opj_tcp_v2_t *tcp = 00;
1870 opj_poc_t * l_current_poc = 00;
1873 assert(tileno < (cp->tw * cp->th));
1874 assert(pino < (cp->tcps[tileno].numpocs + 1));
1876 /* get the given tile coding parameter */
1877 tcp = &cp->tcps[tileno];
1880 l_current_poc = &(tcp->pocs[pino]);
1881 assert(l_current_poc != 0);
1883 /* get the progression order as a character string */
1884 prog = j2k_convert_progression_order(tcp->prg);
1885 assert(strlen(prog) > 0);
1887 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1891 /* component wise */
1893 tpnum *= l_current_poc->compE;
1895 /* resolution wise */
1897 tpnum *= l_current_poc->resE;
1901 tpnum *= l_current_poc->prcE;
1905 tpnum *= l_current_poc->layE;
1908 /* whould we split here ? */
1909 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1910 cp->m_specific_param.m_enc.m_tp_pos=i;
1922 /** mem allocation for TLM marker*/
1923 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1924 int pino,tileno,totnum_tp=0;
1926 OPJ_ARG_NOT_USED(img_numcomp);
1928 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1929 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1930 int cur_totnum_tp = 0;
1931 opj_tcp_t *tcp = &cp->tcps[tileno];
1932 for(pino = 0; pino <= tcp->numpocs; pino++) {
1934 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1935 if(!pi) { return -1;}
1936 tp_num = j2k_get_num_tp(cp,pino,tileno);
1937 totnum_tp = totnum_tp + tp_num;
1938 cur_totnum_tp = cur_totnum_tp + tp_num;
1939 pi_destroy(pi, cp, tileno);
1941 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1943 if (j2k->cstr_info) {
1944 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1945 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1953 * Calculates the total number of tile parts needed by the encoder to
1954 * encode such an image. If not enough memory is available, then the function return false.
1956 * @param p_nb_tiles pointer that will hold the number of tile parts.
1957 * @param cp the coding parameters for the image.
1958 * @param image the image to encode.
1959 * @param p_j2k the p_j2k encoder.
1960 * @param p_manager the user event manager.
1962 * @return true if the function was successful, false else.
1964 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1966 OPJ_UINT32 * p_nb_tiles,
1968 opj_event_mgr_t * p_manager)
1970 OPJ_UINT32 pino,tileno;
1971 OPJ_UINT32 l_nb_tiles;
1975 assert(p_nb_tiles != 00);
1977 assert(image != 00);
1978 assert(p_j2k != 00);
1979 assert(p_manager != 00);
1981 l_nb_tiles = cp->tw * cp->th;
1986 /* TODO mergeV2: check this part which use cstr_info */
1987 /*if (p_j2k->cstr_info) {
1988 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1990 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1991 OPJ_UINT32 cur_totnum_tp = 0;
1993 pi_update_encoding_parameters(image,cp,tileno);
1995 for (pino = 0; pino <= tcp->numpocs; ++pino)
1997 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1999 *p_nb_tiles = *p_nb_tiles + tp_num;
2001 cur_totnum_tp += tp_num;
2004 tcp->m_nb_tile_parts = cur_totnum_tp;
2006 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
2007 if (l_info_tile_ptr->tp == 00) {
2011 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
2013 l_info_tile_ptr->num_tps = cur_totnum_tp;
2020 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2021 OPJ_UINT32 cur_totnum_tp = 0;
2023 pi_update_encoding_parameters(image,cp,tileno);
2025 for (pino = 0; pino <= tcp->numpocs; ++pino) {
2026 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2028 *p_nb_tiles = *p_nb_tiles + tp_num;
2030 cur_totnum_tp += tp_num;
2032 tcp->m_nb_tile_parts = cur_totnum_tp;
2041 static void j2k_write_soc(opj_j2k_t *j2k) {
2042 opj_cio_t *cio = j2k->cio;
2043 cio_write(cio, J2K_MS_SOC, 2);
2046 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2051 /* update markers struct */
2052 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2053 #endif /* USE_JPWL */
2058 * Writes the SOC marker (Start Of Codestream)
2060 * @param p_stream the stream to write data to.
2061 * @param p_j2k J2K codec.
2062 * @param p_manager the user event manager.
2064 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2065 struct opj_stream_private *p_stream,
2066 struct opj_event_mgr * p_manager )
2068 /* 2 bytes will be written */
2069 OPJ_BYTE * l_start_stream = 00;
2072 assert(p_stream != 00);
2073 assert(p_j2k != 00);
2074 assert(p_manager != 00);
2076 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2078 /* write SOC identifier */
2079 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2081 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2087 /* update markers struct */
2089 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2091 assert( 0 && "TODO" );
2092 #endif /* USE_JPWL */
2101 * Reads a SOC marker (Start of Codestream)
2102 * @param p_header_data the data contained in the SOC box.
2103 * @param jp2 the jpeg2000 file codec.
2104 * @param p_header_size the size of the data contained in the SOC marker.
2105 * @param p_manager the user event manager.
2107 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2108 struct opj_stream_private *p_stream,
2109 struct opj_event_mgr * p_manager )
2111 OPJ_BYTE l_data [2];
2112 OPJ_UINT32 l_marker;
2115 assert(p_j2k != 00);
2116 assert(p_manager != 00);
2117 assert(p_stream != 00);
2119 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2123 opj_read_bytes(l_data,&l_marker,2);
2124 if (l_marker != J2K_MS_SOC) {
2128 /* Next marker should be a SIZ marker in the main header */
2129 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2131 /* FIXME move it in a index structure included in p_j2k*/
2132 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2134 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2136 /* Add the marker to the codestream index*/
2137 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2142 static void j2k_write_siz(opj_j2k_t *j2k) {
2146 opj_cio_t *cio = j2k->cio;
2147 opj_image_t *image = j2k->image;
2148 opj_cp_t *cp = j2k->cp;
2150 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2151 lenp = cio_tell(cio);
2153 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2154 cio_write(cio, image->x1, 4); /* Xsiz */
2155 cio_write(cio, image->y1, 4); /* Ysiz */
2156 cio_write(cio, image->x0, 4); /* X0siz */
2157 cio_write(cio, image->y0, 4); /* Y0siz */
2158 cio_write(cio, cp->tdx, 4); /* XTsiz */
2159 cio_write(cio, cp->tdy, 4); /* YTsiz */
2160 cio_write(cio, cp->tx0, 4); /* XT0siz */
2161 cio_write(cio, cp->ty0, 4); /* YT0siz */
2162 cio_write(cio, image->numcomps, 2); /* Csiz */
2163 for (i = 0; i < image->numcomps; i++) {
2164 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2165 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2166 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2168 len = cio_tell(cio) - lenp;
2169 cio_seek(cio, lenp);
2170 cio_write(cio, len, 2); /* Lsiz */
2171 cio_seek(cio, lenp + len);
2174 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2178 * Writes the SIZ marker (image and tile size)
2180 * @param p_stream the stream to write data to.
2181 * @param p_j2k J2K codec.
2182 * @param p_manager the user event manager.
2184 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2185 struct opj_stream_private *p_stream,
2186 struct opj_event_mgr * p_manager )
2189 OPJ_UINT32 l_size_len;
2190 OPJ_BYTE * l_current_ptr;
2191 opj_image_t * l_image = 00;
2192 opj_cp_v2_t *cp = 00;
2193 opj_image_comp_t * l_img_comp = 00;
2196 assert(p_stream != 00);
2197 assert(p_j2k != 00);
2198 assert(p_manager != 00);
2200 l_image = p_j2k->m_private_image;
2201 cp = &(p_j2k->m_cp);
2202 l_size_len = 40 + 3 * l_image->numcomps;
2203 l_img_comp = l_image->comps;
2205 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2207 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2208 = (OPJ_BYTE*)opj_realloc(
2209 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2211 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2215 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2218 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2220 /* write SOC identifier */
2221 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2224 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2227 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2230 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2233 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2236 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2239 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2242 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2245 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2248 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2251 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2254 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2257 for (i = 0; i < l_image->numcomps; ++i) {
2258 /* TODO here with MCT ? */
2259 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2262 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2265 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2271 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) {
2278 static void j2k_read_siz(opj_j2k_t *j2k) {
2282 opj_cio_t *cio = j2k->cio;
2283 opj_image_t *image = j2k->image;
2284 opj_cp_t *cp = j2k->cp;
2286 len = cio_read(cio, 2); /* Lsiz */
2287 cio_read(cio, 2); /* Rsiz (capabilities) */
2288 image->x1 = cio_read(cio, 4); /* Xsiz */
2289 image->y1 = cio_read(cio, 4); /* Ysiz */
2290 image->x0 = cio_read(cio, 4); /* X0siz */
2291 image->y0 = cio_read(cio, 4); /* Y0siz */
2292 cp->tdx = cio_read(cio, 4); /* XTsiz */
2293 cp->tdy = cio_read(cio, 4); /* YTsiz */
2294 cp->tx0 = cio_read(cio, 4); /* XT0siz */
2295 cp->ty0 = cio_read(cio, 4); /* YT0siz */
2297 /* the following code triggers: */
2298 /* warning: comparison of unsigned expression < 0 is always false */
2300 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
2301 opj_event_msg(j2k->cinfo, EVT_ERROR,
2302 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
2303 image->x0,image->x1,image->y0,image->y1);
2308 image->numcomps = cio_read(cio, 2); /* Csiz */
2311 if (j2k->cp->correct) {
2312 /* if JPWL is on, we check whether TX errors have damaged
2313 too much the SIZ parameters */
2314 if (!(image->x1 * image->y1)) {
2315 opj_event_msg(j2k->cinfo, EVT_ERROR,
2316 "JPWL: bad image size (%d x %d)\n",
2317 image->x1, image->y1);
2318 if (!JPWL_ASSUME || JPWL_ASSUME) {
2319 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2323 assert( len >= 38 );
2324 if (image->numcomps != (OPJ_UINT32)((len - 38) / 3)) {
2325 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2326 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2327 image->numcomps, ((len - 38) / 3));
2329 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2332 /* we try to correct */
2333 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
2334 if (image->numcomps < (OPJ_UINT32)((len - 38) / 3)) {
2335 len = 38 + 3 * image->numcomps;
2336 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2339 image->numcomps = ((len - 38) / 3);
2340 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2345 /* update components number in the jpwl_exp_comps filed */
2346 cp->exp_comps = image->numcomps;
2348 #endif /* USE_JPWL */
2350 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
2351 for (i = 0; i < image->numcomps; i++) {
2353 tmp = cio_read(cio, 1); /* Ssiz_i */
2354 image->comps[i].prec = (tmp & 0x7f) + 1;
2355 image->comps[i].sgnd = tmp >> 7;
2356 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
2357 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
2360 if (j2k->cp->correct) {
2361 /* if JPWL is on, we check whether TX errors have damaged
2362 too much the SIZ parameters, again */
2363 if (!(image->comps[i].dx * image->comps[i].dy)) {
2364 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2365 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2366 i, i, image->comps[i].dx, image->comps[i].dy);
2368 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2371 /* we try to correct */
2372 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2373 if (!image->comps[i].dx) {
2374 image->comps[i].dx = 1;
2375 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2376 i, image->comps[i].dx);
2378 if (!image->comps[i].dy) {
2379 image->comps[i].dy = 1;
2380 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2381 i, image->comps[i].dy);
2386 #endif /* USE_JPWL */
2388 /* TODO: unused ? */
2389 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
2390 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
2392 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
2393 image->comps[i].factor = cp->reduce; /* reducing factor per component */
2396 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2397 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2400 if (j2k->cp->correct) {
2401 /* if JPWL is on, we check whether TX errors have damaged
2402 too much the SIZ parameters */
2403 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
2404 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2405 "JPWL: bad number of tiles (%d x %d)\n",
2408 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2411 /* we try to correct */
2412 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2415 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2418 if (cp->tw > cp->max_tiles) {
2420 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
2421 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2422 cp->max_tiles, cp->tw);
2426 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2429 if (cp->th > cp->max_tiles) {
2431 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2432 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2433 cp->max_tiles, cp->th);
2437 #endif /* USE_JPWL */
2439 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2440 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
2441 cp->tileno_size = 0;
2444 if (j2k->cp->correct) {
2446 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2447 "JPWL: could not alloc tcps field of cp\n");
2448 if (!JPWL_ASSUME || JPWL_ASSUME) {
2449 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2454 #endif /* USE_JPWL */
2456 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2457 cp->tcps[i].POC = 0;
2458 cp->tcps[i].numpocs = 0;
2459 cp->tcps[i].first = 1;
2462 /* Initialization for PPM marker */
2464 cp->ppm_data = NULL;
2465 cp->ppm_data_first = NULL;
2466 cp->ppm_previous = 0;
2469 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2470 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2471 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2473 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
2474 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
2475 j2k->state = J2K_STATE_MH;
2478 if (j2k->cstr_info) {
2479 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2480 cstr_info->image_w = image->x1 - image->x0;
2481 cstr_info->image_h = image->y1 - image->y0;
2482 cstr_info->numcomps = image->numcomps;
2483 cstr_info->tw = cp->tw;
2484 cstr_info->th = cp->th;
2485 cstr_info->tile_x = cp->tdx;
2486 cstr_info->tile_y = cp->tdy;
2487 cstr_info->tile_Ox = cp->tx0;
2488 cstr_info->tile_Oy = cp->ty0;
2489 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
2495 * Reads a SIZ marker (image and tile size)
2496 * @param p_header_data the data contained in the SIZ box.
2497 * @param jp2 the jpeg2000 file codec.
2498 * @param p_header_size the size of the data contained in the SIZ marker.
2499 * @param p_manager the user event manager.
2501 opj_bool j2k_read_siz_v2 (
2502 opj_j2k_v2_t *p_j2k,
2503 OPJ_BYTE * p_header_data,
2504 OPJ_UINT32 p_header_size,
2505 struct opj_event_mgr * p_manager
2508 OPJ_UINT32 l_size, i;
2509 OPJ_UINT32 l_nb_comp;
2510 OPJ_UINT32 l_nb_comp_remain;
2511 OPJ_UINT32 l_remaining_size;
2512 OPJ_UINT32 l_nb_tiles;
2514 opj_image_t *l_image = 00;
2515 opj_cp_v2_t *l_cp = 00;
2516 opj_image_comp_t * l_img_comp = 00;
2517 opj_tcp_v2_t * l_current_tile_param = 00;
2520 assert(p_j2k != 00);
2521 assert(p_manager != 00);
2522 assert(p_header_data != 00);
2524 l_image = p_j2k->m_private_image;
2525 l_cp = &(p_j2k->m_cp);
2527 /* minimum size == 39 - 3 (= minimum component parameter) */
2528 if (p_header_size < 36) {
2529 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2533 l_remaining_size = p_header_size - 36;
2534 l_nb_comp = l_remaining_size / 3;
2535 l_nb_comp_remain = l_remaining_size % 3;
2536 if (l_nb_comp_remain != 0){
2537 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2541 l_size = p_header_size + 2; /* Lsiz */
2543 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2545 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2546 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2548 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2550 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2552 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2554 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2556 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2558 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2560 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2562 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2565 l_image->numcomps = (OPJ_UINT16) l_tmp;
2567 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2571 if (l_image->numcomps != l_nb_comp) {
2572 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);
2577 if (l_cp->correct) {
2578 /* if JPWL is on, we check whether TX errors have damaged
2579 too much the SIZ parameters */
2580 if (!(l_image->x1 * l_image->y1)) {
2581 opj_event_msg_v2(p_manager, EVT_ERROR,
2582 "JPWL: bad image size (%d x %d)\n",
2583 l_image->x1, l_image->y1);
2584 if (!JPWL_ASSUME || JPWL_ASSUME) {
2585 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2590 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2591 if (l_image->numcomps != ((len - 38) / 3)) {
2592 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2593 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2594 l_image->numcomps, ((len - 38) / 3));
2596 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2599 */ /* we try to correct */
2600 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2601 if (l_image->numcomps < ((len - 38) / 3)) {
2602 len = 38 + 3 * l_image->numcomps;
2603 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2606 l_image->numcomps = ((len - 38) / 3);
2607 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2613 /* update components number in the jpwl_exp_comps filed */
2614 l_cp->exp_comps = l_image->numcomps;
2616 #endif /* USE_JPWL */
2618 /* Allocate the resulting image components */
2619 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2620 if (l_image->comps == 00){
2621 l_image->numcomps = 0;
2622 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2626 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2627 l_img_comp = l_image->comps;
2629 /* Read the component information */
2630 for (i = 0; i < l_image->numcomps; ++i){
2632 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2634 l_img_comp->prec = (tmp & 0x7f) + 1;
2635 l_img_comp->sgnd = tmp >> 7;
2636 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2638 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2639 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2641 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2644 if (l_cp->correct) {
2645 /* if JPWL is on, we check whether TX errors have damaged
2646 too much the SIZ parameters, again */
2647 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2648 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2649 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2650 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2652 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2655 /* we try to correct */
2656 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2657 if (!l_image->comps[i].dx) {
2658 l_image->comps[i].dx = 1;
2659 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2660 i, l_image->comps[i].dx);
2662 if (!l_image->comps[i].dy) {
2663 l_image->comps[i].dy = 1;
2664 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2665 i, l_image->comps[i].dy);
2669 #endif /* USE_JPWL */
2670 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2671 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2675 /* Compute the number of tiles */
2676 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2677 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2678 l_nb_tiles = l_cp->tw * l_cp->th;
2680 /* Define the tiles which will be decoded */
2681 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2682 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;
2683 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;
2684 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);
2685 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);
2688 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2689 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2690 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2691 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2695 if (l_cp->correct) {
2696 /* if JPWL is on, we check whether TX errors have damaged
2697 too much the SIZ parameters */
2698 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2699 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2700 "JPWL: bad number of tiles (%d x %d)\n",
2701 l_cp->tw, l_cp->th);
2703 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2706 /* we try to correct */
2707 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2710 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2713 if (l_cp->tw > l_cp->max_tiles) {
2715 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2716 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2717 l_cp->max_tiles, l_cp->tw);
2721 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2724 if (l_cp->th > l_cp->max_tiles) {
2726 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2727 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2728 l_cp->max_tiles, l_cp->th);
2732 #endif /* USE_JPWL */
2734 /* memory allocations */
2735 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2736 if (l_cp->tcps == 00) {
2737 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2740 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2743 if (l_cp->correct) {
2745 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2746 "JPWL: could not alloc tcps field of cp\n");
2747 if (!JPWL_ASSUME || JPWL_ASSUME) {
2748 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2753 #endif /* USE_JPWL */
2755 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2756 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2757 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2758 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2761 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2763 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2764 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2766 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
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->m_mct_records,0,J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2771 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2773 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2774 (opj_simple_mcc_decorrelation_data_t*)
2775 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2777 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2778 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2781 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));
2782 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2784 /* set up default dc level shift */
2785 for (i=0;i<l_image->numcomps;++i) {
2786 if (! l_image->comps[i].sgnd) {
2787 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2791 l_current_tile_param = l_cp->tcps;
2792 for (i = 0; i < l_nb_tiles; ++i) {
2793 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2794 if (l_current_tile_param->tccps == 00) {
2795 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2798 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2800 ++l_current_tile_param;
2803 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2804 opj_image_comp_header_update(l_image,l_cp);
2811 static void j2k_write_com(opj_j2k_t *j2k) {
2815 if(j2k->cp->comment) {
2816 opj_cio_t *cio = j2k->cio;
2817 char *comment = j2k->cp->comment;
2819 cio_write(cio, J2K_MS_COM, 2);
2820 lenp = cio_tell(cio);
2822 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2823 for (i = 0; i < strlen(comment); i++) {
2824 cio_write(cio, comment[i], 1);
2826 len = cio_tell(cio) - lenp;
2827 cio_seek(cio, lenp);
2828 cio_write(cio, len, 2);
2829 cio_seek(cio, lenp + len);
2833 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2839 * Writes the COM marker (comment)
2841 * @param p_stream the stream to write data to.
2842 * @param p_j2k J2K codec.
2843 * @param p_manager the user event manager.
2845 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2846 struct opj_stream_private *p_stream,
2847 struct opj_event_mgr * p_manager )
2849 OPJ_UINT32 l_comment_size;
2850 OPJ_UINT32 l_total_com_size;
2851 const OPJ_CHAR *l_comment;
2852 OPJ_BYTE * l_current_ptr = 00;
2855 assert(p_j2k != 00);
2856 assert(p_stream != 00);
2857 assert(p_manager != 00);
2859 l_comment = p_j2k->m_cp.comment;
2860 l_comment_size = strlen(l_comment);
2861 l_total_com_size = l_comment_size + 6;
2863 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2864 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2865 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2868 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2872 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2875 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2877 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2880 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2883 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2886 memcpy( l_current_ptr,l_comment,l_comment_size);
2888 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) {
2895 static void j2k_read_com(opj_j2k_t *j2k) {
2898 opj_cio_t *cio = j2k->cio;
2900 len = cio_read(cio, 2);
2901 cio_skip(cio, len - 2);
2904 * Reads a COM marker (comments)
2905 * @param p_header_data the data contained in the COM box.
2906 * @param jp2 the jpeg2000 file codec.
2907 * @param p_header_size the size of the data contained in the COM marker.
2908 * @param p_manager the user event manager.
2910 opj_bool j2k_read_com_v2 (
2911 opj_j2k_v2_t *p_j2k,
2912 OPJ_BYTE * p_header_data,
2913 OPJ_UINT32 p_header_size,
2914 struct opj_event_mgr * p_manager
2918 assert(p_j2k != 00);
2919 assert(p_manager != 00);
2920 assert(p_header_data != 00);
2921 (void)p_header_size;
2926 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2929 opj_cp_t *cp = j2k->cp;
2930 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2931 opj_tccp_t *tccp = &tcp->tccps[compno];
2932 opj_cio_t *cio = j2k->cio;
2934 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2935 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2936 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2937 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2938 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2940 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2941 for (i = 0; i < tccp->numresolutions; i++) {
2942 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2947 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2950 opj_cp_t *cp = j2k->cp;
2951 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2952 opj_tccp_t *tccp = &tcp->tccps[compno];
2953 opj_cio_t *cio = j2k->cio;
2955 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2957 /* If user wants to remove more resolutions than the codestream contains, return error*/
2958 assert(cp->reduce >= 0);
2959 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2960 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2961 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2962 j2k->state |= J2K_STATE_ERR;
2965 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2966 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2967 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2968 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2969 if (tccp->csty & J2K_CP_CSTY_PRT) {
2970 for (i = 0; i < tccp->numresolutions; i++) {
2971 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2972 tccp->prcw[i] = tmp & 0xf;
2973 tccp->prch[i] = tmp >> 4;
2978 if(j2k->cstr_info && compno == 0) {
2979 for (i = 0; i < tccp->numresolutions; i++) {
2980 if (tccp->csty & J2K_CP_CSTY_PRT) {
2981 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2982 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2985 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2986 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2993 static void j2k_write_cod(opj_j2k_t *j2k) {
2994 opj_cp_t *cp = NULL;
2995 opj_tcp_t *tcp = NULL;
2998 opj_cio_t *cio = j2k->cio;
3000 cio_write(cio, J2K_MS_COD, 2); /* COD */
3002 lenp = cio_tell(cio);
3006 tcp = &cp->tcps[j2k->curtileno];
3008 cio_write(cio, tcp->csty, 1); /* Scod */
3009 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
3010 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
3011 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
3013 j2k_write_cox(j2k, 0);
3014 len = cio_tell(cio) - lenp;
3015 cio_seek(cio, lenp);
3016 cio_write(cio, len, 2); /* Lcod */
3017 cio_seek(cio, lenp + len);
3020 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
3025 * Writes the COD marker (Coding style default)
3027 * @param p_stream the stream to write data to.
3028 * @param p_j2k J2K codec.
3029 * @param p_manager the user event manager.
3031 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
3032 struct opj_stream_private *p_stream,
3033 struct opj_event_mgr * p_manager )
3035 opj_cp_v2_t *l_cp = 00;
3036 opj_tcp_v2_t *l_tcp = 00;
3037 OPJ_UINT32 l_code_size,l_remaining_size;
3038 OPJ_BYTE * l_current_data = 00;
3041 assert(p_j2k != 00);
3042 assert(p_manager != 00);
3043 assert(p_stream != 00);
3045 l_cp = &(p_j2k->m_cp);
3046 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3047 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
3048 l_remaining_size = l_code_size;
3050 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3051 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3052 = (OPJ_BYTE*)opj_realloc(
3053 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3056 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3060 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
3063 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3065 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
3066 l_current_data += 2;
3068 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
3069 l_current_data += 2;
3071 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
3074 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
3077 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
3080 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
3083 l_remaining_size -= 9;
3085 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3086 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3090 if (l_remaining_size != 0) {
3091 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3095 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) {
3102 static void j2k_read_cod(opj_j2k_t *j2k) {
3106 opj_cio_t *cio = j2k->cio;
3107 opj_cp_t *cp = j2k->cp;
3108 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3109 opj_image_t *image = j2k->image;
3111 len = cio_read(cio, 2); /* Lcod */
3112 tcp->csty = cio_read(cio, 1); /* Scod */
3113 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
3114 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
3115 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
3117 pos = cio_tell(cio);
3118 for (i = 0; i < image->numcomps; i++) {
3119 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
3121 j2k_read_cox(j2k, i);
3125 if (j2k->cstr_info) {
3126 opj_codestream_info_t *cstr_info = j2k->cstr_info;
3127 cstr_info->prog = tcp->prg;
3128 cstr_info->numlayers = tcp->numlayers;
3129 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
3130 for (i = 0; i < image->numcomps; i++) {
3131 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
3137 * Reads a COD marker (Coding Styke defaults)
3138 * @param p_header_data the data contained in the COD box.
3139 * @param p_j2k the jpeg2000 codec.
3140 * @param p_header_size the size of the data contained in the COD marker.
3141 * @param p_manager the user event manager.
3143 opj_bool j2k_read_cod_v2 (
3144 opj_j2k_v2_t *p_j2k,
3145 OPJ_BYTE * p_header_data,
3146 OPJ_UINT32 p_header_size,
3147 struct opj_event_mgr * p_manager
3153 opj_cp_v2_t *l_cp = 00;
3154 opj_tcp_v2_t *l_tcp = 00;
3155 opj_image_t *l_image = 00;
3158 assert(p_header_data != 00);
3159 assert(p_j2k != 00);
3160 assert(p_manager != 00);
3162 l_image = p_j2k->m_private_image;
3163 l_cp = &(p_j2k->m_cp);
3165 /* If we are in the first tile-part header of the current tile */
3166 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3167 &l_cp->tcps[p_j2k->m_current_tile_number] :
3168 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3170 /* Make sure room is sufficient */
3171 if (p_header_size < 5) {
3172 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3176 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
3178 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
3180 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
3181 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
3184 /* If user didn't set a number layer to decode take the max specify in the codestream. */
3185 if (l_cp->m_specific_param.m_dec.m_layer) {
3186 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
3189 l_tcp->num_layers_to_decode = l_tcp->numlayers;
3192 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
3196 for (i = 0; i < l_image->numcomps; ++i) {
3197 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
3200 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3201 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3205 if (p_header_size != 0) {
3206 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3210 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
3211 j2k_copy_tile_component_parameters(p_j2k);
3214 #ifdef WIP_REMOVE_MSD
3215 if (p_j2k->cstr_info) {
3216 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
3217 p_j2k->cstr_info->prog = l_tcp->prg;
3218 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
3219 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
3220 for (i = 0; i < l_image->numcomps; ++i) {
3221 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
3229 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
3232 opj_cp_t *cp = j2k->cp;
3233 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3234 opj_image_t *image = j2k->image;
3235 opj_cio_t *cio = j2k->cio;
3237 cio_write(cio, J2K_MS_COC, 2); /* COC */
3238 lenp = cio_tell(cio);
3240 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3241 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
3242 j2k_write_cox(j2k, compno);
3243 len = cio_tell(cio) - lenp;
3244 cio_seek(cio, lenp);
3245 cio_write(cio, len, 2); /* Lcoc */
3246 cio_seek(cio, lenp + len);
3250 * Writes the COC marker (Coding style component)
3252 * @param p_comp_no the index of the component to output.
3253 * @param p_stream the stream to write data to.
3254 * @param p_j2k J2K codec.
3255 * @param p_manager the user event manager.
3257 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
3258 OPJ_UINT32 p_comp_no,
3259 struct opj_stream_private *p_stream,
3260 struct opj_event_mgr * p_manager )
3262 OPJ_UINT32 l_coc_size,l_remaining_size;
3263 OPJ_UINT32 l_comp_room;
3266 assert(p_j2k != 00);
3267 assert(p_manager != 00);
3268 assert(p_stream != 00);
3270 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3272 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3274 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3275 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3276 = (OPJ_BYTE*)opj_realloc(
3277 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3279 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3283 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3286 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);
3288 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) {
3296 * Writes the COC marker (Coding style component)
3298 * @param p_comp_no the index of the component to output.
3299 * @param p_stream the stream to write data to.
3300 * @param p_j2k J2K codec.
3301 * @param p_manager the user event manager.
3303 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3304 OPJ_UINT32 p_comp_no,
3306 OPJ_UINT32 * p_data_written,
3307 struct opj_event_mgr * p_manager )
3309 opj_cp_v2_t *l_cp = 00;
3310 opj_tcp_v2_t *l_tcp = 00;
3311 OPJ_UINT32 l_coc_size,l_remaining_size;
3312 OPJ_BYTE * l_current_data = 00;
3313 opj_image_t *l_image = 00;
3314 OPJ_UINT32 l_comp_room;
3317 assert(p_j2k != 00);
3318 assert(p_manager != 00);
3320 l_cp = &(p_j2k->m_cp);
3321 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3322 l_image = p_j2k->m_private_image;
3323 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3325 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3326 l_remaining_size = l_coc_size;
3328 l_current_data = p_data;
3330 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3331 l_current_data += 2;
3333 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3334 l_current_data += 2;
3336 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3337 l_current_data+=l_comp_room;
3339 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3342 l_remaining_size -= (5 + l_comp_room);
3343 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3344 * p_data_written = l_coc_size;
3348 * Gets the maximum size taken by a coc.
3350 * @param p_j2k the jpeg2000 codec to use.
3352 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3355 OPJ_UINT32 l_nb_comp;
3356 OPJ_UINT32 l_nb_tiles;
3357 OPJ_UINT32 l_max = 0;
3361 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3362 l_nb_comp = p_j2k->m_private_image->numcomps;
3364 for (i=0;i<l_nb_tiles;++i) {
3365 for (j=0;j<l_nb_comp;++j) {
3366 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3373 static void j2k_read_coc(opj_j2k_t *j2k) {
3376 opj_cp_t *cp = j2k->cp;
3377 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3378 opj_image_t *image = j2k->image;
3379 opj_cio_t *cio = j2k->cio;
3381 len = cio_read(cio, 2); /* Lcoc */
3382 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3383 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
3384 j2k_read_cox(j2k, compno);
3388 * Reads a COC marker (Coding Style Component)
3389 * @param p_header_data the data contained in the COC box.
3390 * @param p_j2k the jpeg2000 codec.
3391 * @param p_header_size the size of the data contained in the COC marker.
3392 * @param p_manager the user event manager.
3394 opj_bool j2k_read_coc_v2 (
3395 opj_j2k_v2_t *p_j2k,
3396 OPJ_BYTE * p_header_data,
3397 OPJ_UINT32 p_header_size,
3398 struct opj_event_mgr * p_manager
3401 opj_cp_v2_t *l_cp = NULL;
3402 opj_tcp_v2_t *l_tcp = NULL;
3403 opj_image_t *l_image = NULL;
3404 OPJ_UINT32 l_comp_room;
3405 OPJ_UINT32 l_comp_no;
3408 assert(p_header_data != 00);
3409 assert(p_j2k != 00);
3410 assert(p_manager != 00);
3412 l_cp = &(p_j2k->m_cp);
3413 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3414 &l_cp->tcps[p_j2k->m_current_tile_number] :
3415 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3416 l_image = p_j2k->m_private_image;
3418 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3420 /* make sure room is sufficient*/
3421 if (p_header_size < l_comp_room + 1) {
3422 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3425 p_header_size -= l_comp_room + 1;
3427 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3428 p_header_data += l_comp_room;
3429 if (l_comp_no >= l_image->numcomps) {
3430 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3434 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3437 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3438 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3442 if (p_header_size != 0) {
3443 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3449 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3450 int bandno, numbands;
3453 opj_cp_t *cp = j2k->cp;
3454 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3455 opj_tccp_t *tccp = &tcp->tccps[compno];
3456 opj_cio_t *cio = j2k->cio;
3458 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3459 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3461 for (bandno = 0; bandno < numbands; bandno++) {
3462 expn = tccp->stepsizes[bandno].expn;
3463 mant = tccp->stepsizes[bandno].mant;
3465 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3466 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3468 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3473 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3475 int bandno, numbands;
3477 opj_cp_t *cp = j2k->cp;
3478 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3479 opj_tccp_t *tccp = &tcp->tccps[compno];
3480 opj_cio_t *cio = j2k->cio;
3482 tmp = cio_read(cio, 1); /* Sqcx */
3483 tccp->qntsty = tmp & 0x1f;
3484 tccp->numgbits = tmp >> 5;
3485 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3486 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3489 if (j2k->cp->correct) {
3491 /* if JPWL is on, we check whether there are too many subbands */
3492 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3493 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3494 "JPWL: bad number of subbands in Sqcx (%d)\n",
3497 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3500 /* we try to correct */
3502 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3503 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3510 /* We check whether there are too many subbands */
3511 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3512 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3513 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3514 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3517 #endif /* USE_JPWL */
3519 for (bandno = 0; bandno < numbands; bandno++) {
3521 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3522 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3525 tmp = cio_read(cio, 2); /* SPqcx_i */
3529 if (bandno < J2K_MAXBANDS){
3530 tccp->stepsizes[bandno].expn = expn;
3531 tccp->stepsizes[bandno].mant = mant;
3535 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3536 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3537 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3538 tccp->stepsizes[bandno].expn =
3539 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3540 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3541 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3547 static void j2k_write_qcd(opj_j2k_t *j2k) {
3550 opj_cio_t *cio = j2k->cio;
3552 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3553 lenp = cio_tell(cio);
3555 j2k_write_qcx(j2k, 0);
3556 len = cio_tell(cio) - lenp;
3557 cio_seek(cio, lenp);
3558 cio_write(cio, len, 2); /* Lqcd */
3559 cio_seek(cio, lenp + len);
3562 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3566 * Writes the QCD marker (quantization default)
3568 * @param p_comp_number the index of the component to output.
3569 * @param p_stream the stream to write data to.
3570 * @param p_j2k J2K codec.
3571 * @param p_manager the user event manager.
3573 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3574 struct opj_stream_private *p_stream,
3575 struct opj_event_mgr * p_manager )
3577 opj_cp_v2_t *l_cp = 00;
3578 opj_tcp_v2_t *l_tcp = 00;
3579 OPJ_UINT32 l_qcd_size,l_remaining_size;
3580 OPJ_BYTE * l_current_data = 00;
3583 assert(p_j2k != 00);
3584 assert(p_manager != 00);
3585 assert(p_stream != 00);
3587 l_cp = &(p_j2k->m_cp);
3588 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3589 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3590 l_remaining_size = l_qcd_size;
3592 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3593 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3594 = (OPJ_BYTE*)opj_realloc(
3595 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3598 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3602 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3605 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3607 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3608 l_current_data += 2;
3610 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3611 l_current_data += 2;
3613 l_remaining_size -= 4;
3615 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3616 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3620 if (l_remaining_size != 0) {
3621 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3625 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) {
3633 static void j2k_read_qcd(opj_j2k_t *j2k) {
3637 opj_cio_t *cio = j2k->cio;
3638 opj_image_t *image = j2k->image;
3640 len = cio_read(cio, 2); /* Lqcd */
3641 pos = cio_tell(cio);
3642 for (i = 0; i < image->numcomps; i++) {
3644 j2k_read_qcx(j2k, i, len - 2);
3649 * Reads a QCD marker (Quantization defaults)
3650 * @param p_header_data the data contained in the QCD box.
3651 * @param p_j2k the jpeg2000 codec.
3652 * @param p_header_size the size of the data contained in the QCD marker.
3653 * @param p_manager the user event manager.
3655 opj_bool j2k_read_qcd_v2 (
3656 opj_j2k_v2_t *p_j2k,
3657 OPJ_BYTE * p_header_data,
3658 OPJ_UINT32 p_header_size,
3659 struct opj_event_mgr * p_manager
3663 assert(p_header_data != 00);
3664 assert(p_j2k != 00);
3665 assert(p_manager != 00);
3667 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3668 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3672 if (p_header_size != 0) {
3673 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3677 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3678 j2k_copy_tile_quantization_parameters(p_j2k);
3683 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3686 opj_cio_t *cio = j2k->cio;
3688 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3689 lenp = cio_tell(cio);
3691 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3692 j2k_write_qcx(j2k, compno);
3693 len = cio_tell(cio) - lenp;
3694 cio_seek(cio, lenp);
3695 cio_write(cio, len, 2); /* Lqcc */
3696 cio_seek(cio, lenp + len);
3700 * Writes the QCC marker (quantization component)
3702 * @param p_comp_no the index of the component to output.
3703 * @param p_stream the stream to write data to.
3704 * @param p_j2k J2K codec.
3705 * @param p_manager the user event manager.
3707 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3708 OPJ_UINT32 p_comp_no,
3709 struct opj_stream_private *p_stream,
3710 struct opj_event_mgr * p_manager )
3712 OPJ_UINT32 l_qcc_size,l_remaining_size;
3715 assert(p_j2k != 00);
3716 assert(p_manager != 00);
3717 assert(p_stream != 00);
3719 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3720 l_remaining_size = l_qcc_size;
3722 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3723 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3724 = (OPJ_BYTE*)opj_realloc(
3725 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3727 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3731 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3734 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);
3736 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) {
3744 * Writes the QCC marker (quantization component)
3746 * @param p_comp_no the index of the component to output.
3747 * @param p_stream the stream to write data to.
3748 * @param p_j2k J2K codec.
3749 * @param p_manager the user event manager.
3751 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3752 OPJ_UINT32 p_comp_no,
3754 OPJ_UINT32 * p_data_written,
3755 struct opj_event_mgr * p_manager )
3757 OPJ_UINT32 l_qcc_size,l_remaining_size;
3758 OPJ_BYTE * l_current_data = 00;
3761 assert(p_j2k != 00);
3762 assert(p_manager != 00);
3764 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3765 l_remaining_size = l_qcc_size;
3767 l_current_data = p_data;
3769 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3770 l_current_data += 2;
3772 if (p_j2k->m_private_image->numcomps <= 256) {
3775 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3776 l_current_data += 2;
3778 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3781 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3782 l_remaining_size -= 6;
3785 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3786 l_current_data += 2;
3788 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3791 l_remaining_size -= 6;
3794 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3796 *p_data_written = l_qcc_size;
3800 * Gets the maximum size taken by a qcc.
3802 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3804 return j2k_get_max_coc_size(p_j2k);
3807 static void j2k_read_qcc(opj_j2k_t *j2k) {
3809 int numcomp = j2k->image->numcomps;
3810 opj_cio_t *cio = j2k->cio;
3812 len = cio_read(cio, 2); /* Lqcc */
3813 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3816 if (j2k->cp->correct) {
3818 static int backup_compno = 0;
3820 /* compno is negative or larger than the number of components!!! */
3821 if ((compno < 0) || (compno >= numcomp)) {
3822 opj_event_msg(j2k->cinfo, EVT_ERROR,
3823 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3826 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3829 /* we try to correct */
3830 compno = backup_compno % numcomp;
3831 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3832 "- setting component number to %d\n",
3836 /* keep your private count of tiles */
3839 #endif /* USE_JPWL */
3841 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3845 * Reads a QCC marker (Quantization component)
3846 * @param p_header_data the data contained in the QCC box.
3847 * @param p_j2k the jpeg2000 codec.
3848 * @param p_header_size the size of the data contained in the QCC marker.
3849 * @param p_manager the user event manager.
3851 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3852 OPJ_BYTE * p_header_data,
3853 OPJ_UINT32 p_header_size,
3854 struct opj_event_mgr * p_manager)
3856 OPJ_UINT32 l_num_comp,l_comp_no;
3859 assert(p_header_data != 00);
3860 assert(p_j2k != 00);
3861 assert(p_manager != 00);
3863 l_num_comp = p_j2k->m_private_image->numcomps;
3865 if (l_num_comp <= 256) {
3866 if (p_header_size < 1) {
3867 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3870 opj_read_bytes(p_header_data,&l_comp_no,1);
3875 if (p_header_size < 2) {
3876 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3879 opj_read_bytes(p_header_data,&l_comp_no,2);
3885 if (p_j2k->m_cp.correct) {
3887 static OPJ_UINT32 backup_compno = 0;
3889 /* compno is negative or larger than the number of components!!! */
3890 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3891 opj_event_msg_v2(p_manager, EVT_ERROR,
3892 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3893 l_comp_no, l_num_comp);
3895 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3898 /* we try to correct */
3899 l_comp_no = backup_compno % l_num_comp;
3900 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3901 "- setting component number to %d\n",
3905 /* keep your private count of tiles */
3908 #endif /* USE_JPWL */
3910 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3911 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3915 if (p_header_size != 0) {
3916 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3924 static void j2k_write_poc(opj_j2k_t *j2k) {
3925 int len, numpchgs, i;
3927 int numcomps = j2k->image->numcomps;
3929 opj_cp_t *cp = j2k->cp;
3930 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3931 opj_tccp_t *tccp = &tcp->tccps[0];
3932 opj_cio_t *cio = j2k->cio;
3934 numpchgs = 1 + tcp->numpocs;
3935 cio_write(cio, J2K_MS_POC, 2); /* POC */
3936 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3937 cio_write(cio, len, 2); /* Lpoc */
3938 for (i = 0; i < numpchgs; i++) {
3939 opj_poc_t *poc = &tcp->pocs[i];
3940 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3941 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3942 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3943 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3944 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3945 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3946 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3947 poc->compno1 = int_min(poc->compno1, numcomps);
3948 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3953 * Writes the POC marker (Progression Order Change)
3955 * @param p_stream the stream to write data to.
3956 * @param p_j2k J2K codec.
3957 * @param p_manager the user event manager.
3959 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3960 struct opj_stream_private *p_stream,
3961 struct opj_event_mgr * p_manager )
3963 OPJ_UINT32 l_nb_comp;
3964 OPJ_UINT32 l_nb_poc;
3965 OPJ_UINT32 l_poc_size;
3966 OPJ_UINT32 l_written_size = 0;
3967 opj_tcp_v2_t *l_tcp = 00;
3968 opj_tccp_t *l_tccp = 00;
3969 OPJ_UINT32 l_poc_room;
3972 assert(p_j2k != 00);
3973 assert(p_manager != 00);
3974 assert(p_stream != 00);
3976 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3977 l_tccp = &l_tcp->tccps[0];
3978 l_nb_comp = p_j2k->m_private_image->numcomps;
3979 l_nb_poc = 1 + l_tcp->numpocs;
3981 if (l_nb_comp <= 256) {
3987 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3989 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3990 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3991 = (OPJ_BYTE*)opj_realloc(
3992 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3995 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3999 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
4002 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
4004 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) {
4013 * Writes the POC marker (Progression Order Change)
4015 * @param p_stream the stream to write data to.
4016 * @param p_j2k J2K codec.
4017 * @param p_manager the user event manager.
4019 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
4021 OPJ_UINT32 * p_data_written,
4022 struct opj_event_mgr * p_manager )
4025 OPJ_BYTE * l_current_data = 00;
4026 OPJ_UINT32 l_nb_comp;
4027 OPJ_UINT32 l_nb_poc;
4028 OPJ_UINT32 l_poc_size;
4029 opj_image_t *l_image = 00;
4030 opj_tcp_v2_t *l_tcp = 00;
4031 opj_tccp_t *l_tccp = 00;
4032 opj_poc_t *l_current_poc = 00;
4033 OPJ_UINT32 l_poc_room;
4036 assert(p_j2k != 00);
4037 assert(p_manager != 00);
4039 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4040 l_tccp = &l_tcp->tccps[0];
4041 l_image = p_j2k->m_private_image;
4042 l_nb_comp = l_image->numcomps;
4043 l_nb_poc = 1 + l_tcp->numpocs;
4045 if (l_nb_comp <= 256) {
4052 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4054 l_current_data = p_data;
4056 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
4057 l_current_data += 2;
4059 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
4060 l_current_data += 2;
4062 l_current_poc = l_tcp->pocs;
4063 for (i = 0; i < l_nb_poc; ++i) {
4064 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
4067 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
4068 l_current_data+=l_poc_room;
4070 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
4073 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
4076 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
4077 l_current_data+=l_poc_room;
4079 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
4082 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
4083 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
4084 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
4085 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
4090 *p_data_written = l_poc_size;
4094 * Gets the maximum size taken by the writing of a POC.
4096 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
4098 opj_tcp_v2_t * l_tcp = 00;
4099 OPJ_UINT32 l_nb_tiles = 0;
4100 OPJ_UINT32 l_max_poc = 0;
4103 l_tcp = p_j2k->m_cp.tcps;
4104 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4106 for (i=0;i<l_nb_tiles;++i) {
4107 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
4113 return 4 + 9 * l_max_poc;
4117 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
4119 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
4122 OPJ_UINT32 l_nb_tiles;
4123 OPJ_UINT32 l_max = 0;
4124 opj_tcp_v2_t * l_tcp = 00;
4126 l_tcp = p_j2k->m_cp.tcps;
4127 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
4129 for (i=0;i<l_nb_tiles;++i) {
4130 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
4140 * Gets the maximum size taken by the headers of the SOT.
4142 * @param p_j2k the jpeg2000 codec to use.
4144 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
4146 OPJ_UINT32 l_nb_bytes = 0;
4147 OPJ_UINT32 l_nb_comps;
4148 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
4150 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
4151 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
4153 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
4154 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
4155 l_nb_bytes += l_nb_comps * l_coc_bytes;
4157 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
4158 l_nb_bytes += l_nb_comps * l_qcc_bytes;
4161 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
4163 /*** DEVELOPER CORNER, Add room for your headers ***/
4168 static void j2k_read_poc(opj_j2k_t *j2k) {
4169 int len, numpchgs, i, old_poc;
4171 int numcomps = j2k->image->numcomps;
4173 opj_cp_t *cp = j2k->cp;
4174 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
4175 opj_cio_t *cio = j2k->cio;
4177 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
4179 len = cio_read(cio, 2); /* Lpoc */
4180 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
4182 for (i = old_poc; i < numpchgs + old_poc; i++) {
4184 poc = &tcp->pocs[i];
4185 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
4186 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
4187 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
4188 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
4189 poc->compno1 = int_min(
4190 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
4191 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
4194 tcp->numpocs = numpchgs + old_poc - 1;
4198 * Reads a POC marker (Progression Order Change)
4200 * @param p_header_data the data contained in the POC box.
4201 * @param p_j2k the jpeg2000 codec.
4202 * @param p_header_size the size of the data contained in the POC marker.
4203 * @param p_manager the user event manager.
4205 opj_bool j2k_read_poc_v2 (
4206 opj_j2k_v2_t *p_j2k,
4207 OPJ_BYTE * p_header_data,
4208 OPJ_UINT32 p_header_size,
4209 struct opj_event_mgr * p_manager)
4211 OPJ_UINT32 i, l_nb_comp, l_tmp;
4212 opj_image_t * l_image = 00;
4213 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
4214 OPJ_UINT32 l_chunk_size, l_comp_room;
4216 opj_cp_v2_t *l_cp = 00;
4217 opj_tcp_v2_t *l_tcp = 00;
4218 opj_poc_t *l_current_poc = 00;
4221 assert(p_header_data != 00);
4222 assert(p_j2k != 00);
4223 assert(p_manager != 00);
4225 l_image = p_j2k->m_private_image;
4226 l_nb_comp = l_image->numcomps;
4227 if (l_nb_comp <= 256) {
4233 l_chunk_size = 5 + 2 * l_comp_room;
4234 l_current_poc_nb = p_header_size / l_chunk_size;
4235 l_current_poc_remaining = p_header_size % l_chunk_size;
4237 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
4238 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
4242 l_cp = &(p_j2k->m_cp);
4243 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4244 &l_cp->tcps[p_j2k->m_current_tile_number] :
4245 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4246 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
4247 l_current_poc_nb += l_old_poc_nb;
4249 assert(l_current_poc_nb < 32);
4251 /* now poc is in use.*/
4254 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
4255 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
4256 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
4258 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
4259 p_header_data+=l_comp_room;
4260 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
4262 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
4264 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
4265 p_header_data+=l_comp_room;
4266 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
4268 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4269 /* make sure comp is in acceptable bounds */
4270 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4274 l_tcp->numpocs = l_current_poc_nb - 1;
4278 static void j2k_read_crg(opj_j2k_t *j2k) {
4279 int len, i, Xcrg_i, Ycrg_i;
4281 opj_cio_t *cio = j2k->cio;
4282 int numcomps = j2k->image->numcomps;
4284 len = cio_read(cio, 2); /* Lcrg */
4285 for (i = 0; i < numcomps; i++) {
4286 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
4287 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4292 * Reads a CRG marker (Component registration)
4294 * @param p_header_data the data contained in the TLM box.
4295 * @param p_j2k the jpeg2000 codec.
4296 * @param p_header_size the size of the data contained in the TLM marker.
4297 * @param p_manager the user event manager.
4299 opj_bool j2k_read_crg_v2 (
4300 opj_j2k_v2_t *p_j2k,
4301 OPJ_BYTE * p_header_data,
4302 OPJ_UINT32 p_header_size,
4303 struct opj_event_mgr * p_manager
4306 OPJ_UINT32 l_nb_comp;
4308 assert(p_header_data != 00);
4309 assert(p_j2k != 00);
4310 assert(p_manager != 00);
4312 l_nb_comp = p_j2k->m_private_image->numcomps;
4314 if (p_header_size != l_nb_comp *4) {
4315 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4318 /* Do not care of this at the moment since only local variables are set here */
4321 (i = 0; i < l_nb_comp; ++i)
4323 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4325 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4332 static void j2k_read_tlm(opj_j2k_t *j2k) {
4333 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4334 long int Ttlm_i, Ptlm_i;
4336 opj_cio_t *cio = j2k->cio;
4338 len = cio_read(cio, 2); /* Ltlm */
4339 Ztlm = cio_read(cio, 1); /* Ztlm */
4340 Stlm = cio_read(cio, 1); /* Stlm */
4341 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4342 SP = (Stlm >> 6) & 0x01;
4343 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4344 for (i = 0; i < tile_tlm; i++) {
4345 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4346 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4351 * Reads a TLM marker (Tile Length Marker)
4353 * @param p_header_data the data contained in the TLM box.
4354 * @param p_j2k the jpeg2000 codec.
4355 * @param p_header_size the size of the data contained in the TLM marker.
4356 * @param p_manager the user event manager.
4358 opj_bool j2k_read_tlm_v2 (
4359 opj_j2k_v2_t *p_j2k,
4360 OPJ_BYTE * p_header_data,
4361 OPJ_UINT32 p_header_size,
4362 struct opj_event_mgr * p_manager
4365 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4367 assert(p_header_data != 00);
4368 assert(p_j2k != 00);
4369 assert(p_manager != 00);
4371 if (p_header_size < 2) {
4372 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4377 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4379 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4382 l_ST = ((l_Stlm >> 4) & 0x3);
4383 l_SP = (l_Stlm >> 6) & 0x1;
4385 l_Ptlm_size = (l_SP + 1) * 2;
4386 l_quotient = l_Ptlm_size + l_ST;
4388 l_tot_num_tp = p_header_size / l_quotient;
4389 l_tot_num_tp_remaining = p_header_size % l_quotient;
4391 if (l_tot_num_tp_remaining != 0) {
4392 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4395 /* FIXME Do not care of this at the moment since only local variables are set here */
4398 (i = 0; i < l_tot_num_tp; ++i)
4400 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4401 p_header_data += l_ST;
4402 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4403 p_header_data += l_Ptlm_size;
4408 static void j2k_read_plm(opj_j2k_t *j2k) {
4409 int len, i, Zplm, Nplm, add, packet_len = 0;
4411 opj_cio_t *cio = j2k->cio;
4413 len = cio_read(cio, 2); /* Lplm */
4414 Zplm = cio_read(cio, 1); /* Zplm */
4417 Nplm = cio_read(cio, 4); /* Nplm */
4419 for (i = Nplm; i > 0; i--) {
4420 add = cio_read(cio, 1);
4422 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4423 if ((add & 0x80) == 0) {
4434 * Reads a PLM marker (Packet length, main header marker)
4436 * @param p_header_data the data contained in the TLM box.
4437 * @param p_j2k the jpeg2000 codec.
4438 * @param p_header_size the size of the data contained in the TLM marker.
4439 * @param p_manager the user event manager.
4441 opj_bool j2k_read_plm_v2 (
4442 opj_j2k_v2_t *p_j2k,
4443 OPJ_BYTE * p_header_data,
4444 OPJ_UINT32 p_header_size,
4445 struct opj_event_mgr * p_manager
4449 assert(p_header_data != 00);
4450 assert(p_j2k != 00);
4451 assert(p_manager != 00);
4453 if (p_header_size < 1) {
4454 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4457 /* Do not care of this at the moment since only local variables are set here */
4459 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4466 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4468 p_header_size -= (1+l_Nplm);
4472 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4476 (i = 0; i < l_Nplm; ++i)
4478 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4480 // take only the last seven bytes
4481 l_packet_len |= (l_tmp & 0x7f);
4489 // store packet length and proceed to next packet
4496 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4504 static void j2k_read_plt(opj_j2k_t *j2k) {
4505 int len, i, Zplt, packet_len = 0, add;
4507 opj_cio_t *cio = j2k->cio;
4509 len = cio_read(cio, 2); /* Lplt */
4510 Zplt = cio_read(cio, 1); /* Zplt */
4511 for (i = len - 3; i > 0; i--) {
4512 add = cio_read(cio, 1);
4513 packet_len = (packet_len << 7) + add; /* Iplt_i */
4514 if ((add & 0x80) == 0) {
4522 * Reads a PLT marker (Packet length, tile-part header)
4524 * @param p_header_data the data contained in the PLT box.
4525 * @param p_j2k the jpeg2000 codec.
4526 * @param p_header_size the size of the data contained in the PLT marker.
4527 * @param p_manager the user event manager.
4529 opj_bool j2k_read_plt_v2 (
4530 opj_j2k_v2_t *p_j2k,
4531 OPJ_BYTE * p_header_data,
4532 OPJ_UINT32 p_header_size,
4533 struct opj_event_mgr * p_manager
4536 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4539 assert(p_header_data != 00);
4540 assert(p_j2k != 00);
4541 assert(p_manager != 00);
4543 if (p_header_size < 1) {
4544 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4548 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4552 for (i = 0; i < p_header_size; ++i) {
4553 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4555 /* take only the last seven bytes */
4556 l_packet_len |= (l_tmp & 0x7f);
4561 /* store packet length and proceed to next packet */
4566 if (l_packet_len != 0) {
4567 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4574 static void j2k_read_ppm(opj_j2k_t *j2k) {
4575 int len, Z_ppm, i, j;
4578 opj_cp_t *cp = j2k->cp;
4579 opj_cio_t *cio = j2k->cio;
4581 len = cio_read(cio, 2);
4584 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4587 if (cp->ppm_previous == 0) {
4588 N_ppm = cio_read(cio, 4); /* N_ppm */
4591 N_ppm = cp->ppm_previous;
4594 if (Z_ppm == 0) { /* First PPM marker */
4595 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4596 cp->ppm_data_first = cp->ppm_data;
4597 cp->ppm_len = N_ppm;
4598 } else { /* NON-first PPM marker */
4599 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4602 /* this memory allocation check could be done even in non-JPWL cases */
4604 if (!cp->ppm_data) {
4605 opj_event_msg(j2k->cinfo, EVT_ERROR,
4606 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4608 if (!JPWL_ASSUME || JPWL_ASSUME) {
4609 opj_free(cp->ppm_data);
4610 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4617 cp->ppm_data_first = cp->ppm_data;
4618 cp->ppm_len = N_ppm + cp->ppm_store;
4620 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4621 cp->ppm_data[j] = cio_read(cio, 1);
4625 break; /* Case of non-finished packet header in present marker but finished in next one */
4627 cp->ppm_previous = i - 1;
4632 * Reads a PPM marker (Packed packet headers, main header)
4634 * @param p_header_data the data contained in the POC box.
4635 * @param p_j2k the jpeg2000 codec.
4636 * @param p_header_size the size of the data contained in the POC marker.
4637 * @param p_manager the user event manager.
4640 opj_bool j2k_read_ppm_v2 (
4641 opj_j2k_v2_t *p_j2k,
4642 OPJ_BYTE * p_header_data,
4643 OPJ_UINT32 p_header_size,
4644 struct opj_event_mgr * p_manager
4648 opj_cp_v2_t *l_cp = 00;
4649 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4652 assert(p_header_data != 00);
4653 assert(p_j2k != 00);
4654 assert(p_manager != 00);
4656 if (p_header_size < 1) {
4657 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4661 l_cp = &(p_j2k->m_cp);
4664 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4668 /* First PPM marker */
4670 if (p_header_size < 4) {
4671 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4675 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4679 /* First PPM marker: Initialization */
4680 l_cp->ppm_len = l_N_ppm;
4681 l_cp->ppm_data_size = 0;
4683 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4684 if (l_cp->ppm_buffer == 00) {
4685 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4688 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4690 l_cp->ppm_data = l_cp->ppm_buffer;
4694 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4695 if (p_header_size >= 4) {
4697 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4700 l_cp->ppm_len += l_N_ppm ;
4702 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4703 if (l_cp->ppm_buffer == 00) {
4704 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4707 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4709 l_cp->ppm_data = l_cp->ppm_buffer;
4716 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4718 if (l_remaining_data <= p_header_size) {
4719 /* we must store less information than available in the packet */
4720 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4721 l_cp->ppm_data_size = l_cp->ppm_len;
4722 p_header_size -= l_remaining_data;
4723 p_header_data += l_remaining_data;
4726 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4727 l_cp->ppm_data_size += p_header_size;
4728 p_header_data += p_header_size;
4741 * Reads a PPM marker (Packed packet headers, main header)
4743 * @param p_header_data the data contained in the POC box.
4744 * @param p_j2k the jpeg2000 codec.
4745 * @param p_header_size the size of the data contained in the POC marker.
4746 * @param p_manager the user event manager.
4748 opj_bool j2k_read_ppm_v3 (
4749 opj_j2k_v2_t *p_j2k,
4750 OPJ_BYTE * p_header_data,
4751 OPJ_UINT32 p_header_size,
4752 struct opj_event_mgr * p_manager
4755 opj_cp_v2_t *l_cp = 00;
4756 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4759 assert(p_header_data != 00);
4760 assert(p_j2k != 00);
4761 assert(p_manager != 00);
4763 /* Minimum size of PPM marker is equal to the size of Zppm element */
4764 if (p_header_size < 1) {
4765 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4769 l_cp = &(p_j2k->m_cp);
4772 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4776 /* First PPM marker */
4778 /* We need now at least the Nppm^0 element */
4779 if (p_header_size < 4) {
4780 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4784 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4788 /* First PPM marker: Initialization */
4789 l_cp->ppm_len = l_N_ppm;
4790 l_cp->ppm_data_read = 0;
4792 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4793 if (l_cp->ppm_data == 00) {
4794 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4797 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4799 l_cp->ppm_data_current = l_cp->ppm_data;
4801 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4804 if (p_header_size < 4) {
4805 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4809 /* Uncompleted Ippm series in the previous PPM marker?*/
4810 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4811 /* Get the place where add the remaining Ippm series*/
4812 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4813 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4816 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4820 /* Increase the size of ppm_data to add the new Ippm series*/
4821 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4823 /* Keep the position of the place where concatenate the new series*/
4824 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4825 l_cp->ppm_len += l_N_ppm;
4830 l_remaining_data = p_header_size;
4832 while (l_remaining_data >= l_N_ppm) {
4833 /* read a complete Ippm series*/
4834 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4835 p_header_size -= l_N_ppm;
4836 p_header_data += l_N_ppm;
4838 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4842 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4847 l_remaining_data = p_header_size;
4851 l_remaining_data = p_header_size;
4853 /* Next Ippm series is a complete series ?*/
4854 if (l_remaining_data > l_N_ppm) {
4855 /* Increase the size of ppm_data to add the new Ippm series*/
4856 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4858 /* Keep the position of the place where concatenate the new series */
4859 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4860 l_cp->ppm_len += l_N_ppm;
4865 /* Need to read an incomplete Ippm series*/
4866 if (l_remaining_data) {
4867 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4869 /* Keep the position of the place where concatenate the new series*/
4870 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4871 l_cp->ppm_len += l_N_ppm;
4873 /* Read incomplete Ippm series*/
4874 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4875 p_header_size -= l_remaining_data;
4876 p_header_data += l_remaining_data;
4878 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4883 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4884 if (p_header_size >= 4) {
4886 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4889 l_cp->ppm_len += l_N_ppm ;
4891 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4892 if (l_cp->ppm_buffer == 00) {
4893 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4896 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4898 l_cp->ppm_data = l_cp->ppm_buffer;
4905 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4907 if (l_remaining_data <= p_header_size) {
4908 /* we must store less information than available in the packet */
4909 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4910 l_cp->ppm_data_size = l_cp->ppm_len;
4911 p_header_size -= l_remaining_data;
4912 p_header_data += l_remaining_data;
4915 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4916 l_cp->ppm_data_size += p_header_size;
4917 p_header_data += p_header_size;
4926 static void j2k_read_ppt(opj_j2k_t *j2k) {
4927 int len, Z_ppt, i, j = 0;
4929 opj_cp_t *cp = j2k->cp;
4930 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4931 opj_cio_t *cio = j2k->cio;
4933 len = cio_read(cio, 2);
4934 Z_ppt = cio_read(cio, 1);
4936 if (Z_ppt == 0) { /* First PPT marker */
4937 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4938 tcp->ppt_data_first = tcp->ppt_data;
4940 tcp->ppt_len = len - 3;
4941 } else { /* NON-first PPT marker */
4942 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4943 tcp->ppt_data_first = tcp->ppt_data;
4944 tcp->ppt_len = len - 3 + tcp->ppt_store;
4947 for (i = len - 3; i > 0; i--) {
4948 tcp->ppt_data[j] = cio_read(cio, 1);
4955 * Reads a PPT marker (Packed packet headers, tile-part header)
4957 * @param p_header_data the data contained in the PPT box.
4958 * @param p_j2k the jpeg2000 codec.
4959 * @param p_header_size the size of the data contained in the PPT marker.
4960 * @param p_manager the user event manager.
4962 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4963 OPJ_BYTE * p_header_data,
4964 OPJ_UINT32 p_header_size,
4965 struct opj_event_mgr * p_manager )
4967 opj_cp_v2_t *l_cp = 00;
4968 opj_tcp_v2_t *l_tcp = 00;
4972 assert(p_header_data != 00);
4973 assert(p_j2k != 00);
4974 assert(p_manager != 00);
4976 /* We need to have the Z_ppt element at minimum */
4977 if (p_header_size < 1) {
4978 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4982 l_cp = &(p_j2k->m_cp);
4984 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");
4988 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4991 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4995 /* Allocate buffer to read the packet header */
4997 /* First PPT marker */
4998 l_tcp->ppt_data_size = 0;
4999 l_tcp->ppt_len = p_header_size;
5001 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
5002 if (l_tcp->ppt_buffer == 00) {
5003 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5006 l_tcp->ppt_data = l_tcp->ppt_buffer;
5008 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
5011 l_tcp->ppt_len += p_header_size;
5013 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
5014 if (l_tcp->ppt_buffer == 00) {
5015 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5018 l_tcp->ppt_data = l_tcp->ppt_buffer;
5020 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
5023 /* Read packet header from buffer */
5024 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
5026 l_tcp->ppt_data_size += p_header_size;
5032 * Writes the TLM marker (Tile Length Marker)
5034 * @param p_stream the stream to write data to.
5035 * @param p_j2k J2K codec.
5036 * @param p_manager the user event manager.
5038 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
5039 struct opj_stream_private *p_stream,
5040 struct opj_event_mgr * p_manager )
5042 OPJ_BYTE * l_current_data = 00;
5043 OPJ_UINT32 l_tlm_size;
5046 assert(p_j2k != 00);
5047 assert(p_manager != 00);
5048 assert(p_stream != 00);
5050 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5052 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5053 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5054 = (OPJ_BYTE*)opj_realloc(
5055 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5058 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5062 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
5065 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5067 /* change the way data is written to avoid seeking if possible */
5069 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
5071 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
5072 l_current_data += 2;
5074 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
5075 l_current_data += 2;
5077 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
5080 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5083 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
5084 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) {
5091 static void j2k_write_tlm(opj_j2k_t *j2k){
5093 opj_cio_t *cio = j2k->cio;
5094 j2k->tlm_start = cio_tell(cio);
5095 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
5096 lenp = 4 + (5*j2k->totnum_tp);
5097 cio_write(cio,lenp,2); /* Ltlm */
5098 cio_write(cio, 0,1); /* Ztlm=0*/
5099 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5100 cio_skip(cio,5*j2k->totnum_tp);
5103 static void j2k_write_sot(opj_j2k_t *j2k) {
5106 opj_cio_t *cio = j2k->cio;
5108 j2k->sot_start = cio_tell(cio);
5109 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
5110 lenp = cio_tell(cio);
5111 cio_skip(cio, 2); /* Lsot (further) */
5112 cio_write(cio, j2k->curtileno, 2); /* Isot */
5113 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
5114 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
5115 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
5116 len = cio_tell(cio) - lenp;
5117 cio_seek(cio, lenp);
5118 cio_write(cio, len, 2); /* Lsot */
5119 cio_seek(cio, lenp + len);
5123 /* update markers struct */
5124 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
5125 #endif /* USE_JPWL */
5128 if( j2k->cstr_info && j2k->cur_tp_num==0){
5129 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
5134 * Writes the SOT marker (Start of tile-part)
5136 * @param p_stream the stream to write data to.
5137 * @param p_j2k J2K codec.
5138 * @param p_manager the user event manager.
5140 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
5142 OPJ_UINT32 * p_data_written,
5143 const struct opj_stream_private *p_stream,
5144 struct opj_event_mgr * p_manager )
5147 assert(p_j2k != 00);
5148 assert(p_manager != 00);
5149 assert(p_stream != 00);
5151 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
5154 opj_write_bytes(p_data,10,2); /* Lsot */
5157 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
5163 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
5166 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
5171 /* update markers struct */
5173 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
5175 assert( 0 && "TODO" );
5176 #endif /* USE_JPWL */
5178 * p_data_written = 12;
5187 * Reads a PPT marker (Packed packet headers, tile-part header)
5189 * @param p_header_data the data contained in the PPT box.
5190 * @param p_j2k the jpeg2000 codec.
5191 * @param p_header_size the size of the data contained in the PPT marker.
5192 * @param p_manager the user event manager.
5194 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
5195 OPJ_BYTE * p_header_data,
5196 OPJ_UINT32 p_header_size,
5197 opj_event_mgr_t * p_manager )
5199 opj_cp_v2_t *l_cp = 00;
5200 opj_tcp_v2_t *l_tcp = 00;
5201 OPJ_UINT32 l_tot_len, l_num_parts = 0;
5202 OPJ_UINT32 l_current_part;
5203 OPJ_UINT32 l_tile_x,l_tile_y;
5206 assert(p_header_data != 00);
5207 assert(p_j2k != 00);
5208 assert(p_manager != 00);
5210 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
5211 if (p_header_size != 8) {
5212 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
5216 l_cp = &(p_j2k->m_cp);
5217 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
5220 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5221 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
5222 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
5225 if (l_cp->correct) {
5227 int tileno = p_j2k->m_current_tile_number;
5228 static int backup_tileno = 0;
5230 /* tileno is negative or larger than the number of tiles!!! */
5231 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
5232 opj_event_msg_v2(p_manager, EVT_ERROR,
5233 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5234 tileno, (l_cp->tw * l_cp->th));
5236 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5239 /* we try to correct */
5240 tileno = backup_tileno;
5241 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5242 "- setting tile number to %d\n",
5246 /* keep your private count of tiles */
5249 #endif /* USE_JPWL */
5251 /* look for the tile in the list of already processed tile (in parts). */
5252 /* Optimization possible here with a more complex data structure and with the removing of tiles */
5253 /* since the time taken by this function can only grow at the time */
5255 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
5258 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
5259 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
5261 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5267 if (l_cp->correct) {
5269 /* totlen is negative or larger than the bytes left!!! */
5270 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))) { */
5271 opj_event_msg_v2(p_manager, EVT_ERROR,
5272 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5273 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5275 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5278 /* we try to correct */
5280 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5281 "- setting Psot to %d => assuming it is the last tile\n",
5285 #endif /* USE_JPWL */
5287 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5289 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5290 "we assuming it is the last tile-part of the codestream.\n");
5291 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5294 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5297 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5300 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5301 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5302 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5303 if (l_tcp->m_nb_tile_parts) {
5304 if (l_current_part >= l_tcp->m_nb_tile_parts){
5305 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5306 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5307 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5311 l_tcp->m_nb_tile_parts = l_num_parts;
5314 /* If know the number of tile part header we will check if we didn't read the last*/
5315 if (l_tcp->m_nb_tile_parts) {
5316 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5317 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5321 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5322 /* Keep the size of data to skip after this marker */
5323 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5326 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5327 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5330 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5332 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5333 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5334 p_j2k->m_specific_param.m_decoder.m_skip_data =
5335 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5336 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5337 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5338 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5341 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5342 p_j2k->m_specific_param.m_decoder.m_skip_data =
5343 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5347 if (p_j2k->cstr_index)
5349 assert(p_j2k->cstr_index->tile_index != 00);
5350 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5351 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5353 if (l_num_parts != 0){
5354 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5355 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5358 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5359 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5360 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5362 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5363 (opj_tp_index_t*)opj_realloc(
5364 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5365 l_num_parts* sizeof(opj_tp_index_t));
5368 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
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].current_nb_tps = 10;
5372 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5373 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5374 sizeof(opj_tp_index_t));
5377 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5378 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5379 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5380 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5381 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5382 * sizeof(opj_tp_index_t));
5391 /* 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 */
5392 /* if (p_j2k->cstr_info) {
5395 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5398 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5399 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5400 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5401 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5404 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5407 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5411 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5414 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5415 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5416 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5421 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5424 opj_tcp_t *tcp = NULL;
5425 opj_codestream_info_t *cstr_info = NULL;
5427 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5428 opj_cp_t *cp = j2k->cp;
5429 opj_cio_t *cio = j2k->cio;
5431 tcd->tp_num = j2k->tp_num ;
5432 tcd->cur_tp_num = j2k->cur_tp_num;
5434 cio_write(cio, J2K_MS_SOD, 2);
5436 if( j2k->cstr_info && j2k->cur_tp_num==0){
5437 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5440 if (j2k->curtileno == 0) {
5441 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5445 cstr_info = j2k->cstr_info;
5447 if (!j2k->cur_tp_num ) {
5448 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5449 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5452 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5453 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5457 /* update markers struct */
5458 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5459 #endif /* USE_JPWL */
5464 tcp = &cp->tcps[j2k->curtileno];
5465 for (layno = 0; layno < tcp->numlayers; layno++) {
5466 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5467 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5468 } else if (tcp->rates[layno]) {
5469 tcp->rates[layno]=1;
5472 if(j2k->cur_tp_num == 0){
5473 tcd->tcd_image->tiles->packno = 0;
5475 cstr_info->packno = 0;
5478 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5480 /* Writing Psot in SOT marker */
5481 totlen = cio_tell(cio) + l - j2k->sot_start;
5482 cio_seek(cio, j2k->sot_start + 6);
5483 cio_write(cio, totlen, 4);
5484 cio_seek(cio, j2k->sot_start + totlen);
5485 /* Writing Ttlm and Ptlm in TLM marker */
5487 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5488 cio_write(cio, j2k->curtileno, 1);
5489 cio_write(cio, totlen, 4);
5491 cio_seek(cio, j2k->sot_start + totlen);
5495 * Writes the SOD marker (Start of data)
5497 * @param p_stream the stream to write data to.
5498 * @param p_j2k J2K codec.
5499 * @param p_manager the user event manager.
5501 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5502 struct opj_tcd_v2 * p_tile_coder,
5504 OPJ_UINT32 * p_data_written,
5505 OPJ_UINT32 p_total_data_size,
5506 const struct opj_stream_private *p_stream,
5507 struct opj_event_mgr * p_manager )
5509 opj_tcp_v2_t *l_tcp = 00;
5510 opj_codestream_info_t *l_cstr_info = 00;
5511 opj_cp_v2_t *l_cp = 00;
5513 OPJ_UINT32 l_size_tile;
5514 OPJ_UINT32 l_remaining_data;
5517 assert(p_j2k != 00);
5518 assert(p_manager != 00);
5519 assert(p_stream != 00);
5521 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5524 /* make room for the EOF marker */
5525 l_remaining_data = p_total_data_size - 4;
5527 l_cp = &(p_j2k->m_cp);
5528 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5531 /* update tile coder */
5532 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5533 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5535 l_size_tile = l_cp->th * l_cp->tw;
5538 /* TODO mergeV2: check this part which use cstr_info */
5539 /*l_cstr_info = p_j2k->cstr_info;
5541 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5542 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5543 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5549 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5551 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5556 /* update markers struct */
5557 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5559 assert( 0 && "TODO" );
5560 #endif /* USE_JPWL */
5565 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5566 p_tile_coder->tcd_image->tiles->packno = 0;
5568 l_cstr_info->packno = 0;
5572 *p_data_written = 0;
5574 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)) {
5575 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5579 *p_data_written += 2;
5588 * Reads a SOD marker (Start Of Data)
5590 * @param p_header_data the data contained in the SOD box.
5591 * @param p_j2k the jpeg2000 codec.
5592 * @param p_header_size the size of the data contained in the SOD marker.
5593 * @param p_manager the user event manager.
5595 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
5596 opj_stream_private_t *p_stream,
5597 opj_event_mgr_t * p_manager
5600 OPJ_UINT32 l_current_read_size;
5601 opj_codestream_index_t * l_cstr_index = 00;
5602 OPJ_BYTE ** l_current_data = 00;
5603 opj_tcp_v2_t * l_tcp = 00;
5604 OPJ_UINT32 * l_tile_len = 00;
5607 assert(p_j2k != 00);
5608 assert(p_manager != 00);
5609 assert(p_stream != 00);
5611 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5613 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5614 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5615 // but we are in the last tile part,
5616 // so its result will fit on OPJ_UINT32 unless we find
5617 // a file with a single tile part of more than 4 GB...*/
5618 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5621 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5623 l_current_data = &(l_tcp->m_data);
5624 l_tile_len = &l_tcp->m_data_size;
5626 if (! *l_current_data) {
5627 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5630 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5633 if (*l_current_data == 00) {
5634 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5640 l_cstr_index = p_j2k->cstr_index;
5642 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5644 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5645 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5647 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5648 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5650 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5654 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5656 /*l_cstr_index->packno = 0;*/
5659 l_current_read_size = opj_stream_read_data( p_stream,
5660 *l_current_data + *l_tile_len,
5661 p_j2k->m_specific_param.m_decoder.m_sot_length,
5664 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5665 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5668 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5671 *l_tile_len += l_current_read_size;
5677 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5678 opj_cp_t *cp = j2k->cp;
5679 opj_tcp_t *tcp = &cp->tcps[tileno];
5680 opj_cio_t *cio = j2k->cio;
5681 int numcomps = j2k->image->numcomps;
5683 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5684 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5685 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5686 cio_write(cio, 0, 1); /* Srgn */
5687 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5691 * Writes the RGN marker (Region Of Interest)
5693 * @param p_tile_no the tile to output
5694 * @param p_comp_no the component to output
5695 * @param p_stream the stream to write data to.
5696 * @param p_j2k J2K codec.
5697 * @param p_manager the user event manager.
5699 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5700 OPJ_UINT32 p_tile_no,
5701 OPJ_UINT32 p_comp_no,
5702 struct opj_stream_private *p_stream,
5703 struct opj_event_mgr * p_manager )
5705 OPJ_BYTE * l_current_data = 00;
5706 OPJ_UINT32 l_nb_comp;
5707 OPJ_UINT32 l_rgn_size;
5708 opj_image_t *l_image = 00;
5709 opj_cp_v2_t *l_cp = 00;
5710 opj_tcp_v2_t *l_tcp = 00;
5711 opj_tccp_t *l_tccp = 00;
5712 OPJ_UINT32 l_comp_room;
5715 assert(p_j2k != 00);
5716 assert(p_manager != 00);
5717 assert(p_stream != 00);
5719 l_cp = &(p_j2k->m_cp);
5720 l_tcp = &l_cp->tcps[p_tile_no];
5721 l_tccp = &l_tcp->tccps[p_comp_no];
5723 l_nb_comp = l_image->numcomps;
5725 if (l_nb_comp <= 256) {
5732 l_rgn_size = 6 + l_comp_room;
5734 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5736 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5737 l_current_data += 2;
5739 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5740 l_current_data += 2;
5742 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5743 l_current_data+=l_comp_room;
5745 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5748 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5751 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) {
5758 static void j2k_read_rgn(opj_j2k_t *j2k) {
5759 int len, compno, roisty;
5761 opj_cp_t *cp = j2k->cp;
5762 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5763 opj_cio_t *cio = j2k->cio;
5764 int numcomps = j2k->image->numcomps;
5766 len = cio_read(cio, 2); /* Lrgn */
5767 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
5768 roisty = cio_read(cio, 1); /* Srgn */
5771 if (j2k->cp->correct) {
5772 /* totlen is negative or larger than the bytes left!!! */
5773 if (compno >= numcomps) {
5774 opj_event_msg(j2k->cinfo, EVT_ERROR,
5775 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5777 if (!JPWL_ASSUME || JPWL_ASSUME) {
5778 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5783 #endif /* USE_JPWL */
5785 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
5788 static void j2k_write_eoc(opj_j2k_t *j2k) {
5789 opj_cio_t *cio = j2k->cio;
5790 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5791 cio_write(cio, J2K_MS_EOC, 2);
5795 /* update markers struct */
5796 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5797 #endif /* USE_JPWL */
5802 * Writes the EOC marker (End of Codestream)
5804 * @param p_stream the stream to write data to.
5805 * @param p_j2k J2K codec.
5806 * @param p_manager the user event manager.
5808 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5809 struct opj_stream_private *p_stream,
5810 struct opj_event_mgr * p_manager )
5813 assert(p_j2k != 00);
5814 assert(p_manager != 00);
5815 assert(p_stream != 00);
5817 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5822 /* update markers struct */
5824 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5826 #endif /* USE_JPWL */
5828 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5832 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5841 * Reads a RGN marker (Region Of Interest)
5843 * @param p_header_data the data contained in the POC box.
5844 * @param p_j2k the jpeg2000 codec.
5845 * @param p_header_size the size of the data contained in the POC marker.
5846 * @param p_manager the user event manager.
5848 opj_bool j2k_read_rgn_v2 (
5849 opj_j2k_v2_t *p_j2k,
5850 OPJ_BYTE * p_header_data,
5851 OPJ_UINT32 p_header_size,
5852 struct opj_event_mgr * p_manager
5855 OPJ_UINT32 l_nb_comp;
5856 opj_image_t * l_image = 00;
5858 opj_cp_v2_t *l_cp = 00;
5859 opj_tcp_v2_t *l_tcp = 00;
5860 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5863 assert(p_header_data != 00);
5864 assert(p_j2k != 00);
5865 assert(p_manager != 00);
5867 l_image = p_j2k->m_private_image;
5868 l_nb_comp = l_image->numcomps;
5870 if (l_nb_comp <= 256) {
5875 if (p_header_size != 2 + l_comp_room) {
5876 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5880 l_cp = &(p_j2k->m_cp);
5881 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5882 &l_cp->tcps[p_j2k->m_current_tile_number] :
5883 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5885 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5886 p_header_data+=l_comp_room;
5887 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5891 if (l_cp->correct) {
5892 /* totlen is negative or larger than the bytes left!!! */
5893 if (l_comp_room >= l_nb_comp) {
5894 opj_event_msg_v2(p_manager, EVT_ERROR,
5895 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5896 l_comp_room, l_nb_comp);
5897 if (!JPWL_ASSUME || JPWL_ASSUME) {
5898 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5903 #endif /* USE_JPWL */
5905 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5912 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5914 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5917 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5924 * Updates the rates of the tcp.
5926 * @param p_stream the stream to write data to.
5927 * @param p_j2k J2K codec.
5928 * @param p_manager the user event manager.
5930 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5931 struct opj_stream_private *p_stream,
5932 struct opj_event_mgr * p_manager )
5934 opj_cp_v2_t * l_cp = 00;
5935 opj_image_t * l_image = 00;
5936 opj_tcp_v2_t * l_tcp = 00;
5937 opj_image_comp_t * l_img_comp = 00;
5940 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5941 OPJ_FLOAT32 * l_rates = 0;
5942 OPJ_FLOAT32 l_sot_remove;
5943 OPJ_UINT32 l_bits_empty, l_size_pixel;
5944 OPJ_UINT32 l_tile_size = 0;
5945 OPJ_UINT32 l_last_res;
5946 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5949 assert(p_j2k != 00);
5950 assert(p_manager != 00);
5951 assert(p_stream != 00);
5954 l_cp = &(p_j2k->m_cp);
5955 l_image = p_j2k->m_private_image;
5958 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5959 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5960 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5962 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5963 l_tp_stride_func = get_tp_stride;
5966 l_tp_stride_func = get_default_stride;
5969 for (i=0;i<l_cp->th;++i) {
5970 for (j=0;j<l_cp->tw;++j) {
5971 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5973 /* 4 borders of the tile rescale on the image if necessary */
5974 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5975 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5976 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5977 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5979 l_rates = l_tcp->rates;
5981 /* Modification of the RATE >> */
5983 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5985 ((*l_rates) * l_bits_empty)
5993 for (k = 1; k < l_tcp->numlayers; ++k) {
5995 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5997 ((*l_rates) * l_bits_empty)
6013 for (i=0;i<l_cp->th;++i) {
6014 for (j=0;j<l_cp->tw;++j) {
6015 l_rates = l_tcp->rates;
6018 *l_rates -= l_sot_remove;
6020 if (*l_rates < 30) {
6027 l_last_res = l_tcp->numlayers - 1;
6029 for (k = 1; k < l_last_res; ++k) {
6032 *l_rates -= l_sot_remove;
6034 if (*l_rates < *(l_rates - 1) + 10) {
6035 *l_rates = (*(l_rates - 1)) + 20;
6043 *l_rates -= (l_sot_remove + 2.f);
6045 if (*l_rates < *(l_rates - 1) + 10) {
6046 *l_rates = (*(l_rates - 1)) + 20;
6054 l_img_comp = l_image->comps;
6057 for (i=0;i<l_image->numcomps;++i) {
6058 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
6060 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
6068 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
6070 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
6072 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
6073 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
6074 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
6075 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
6079 if (l_cp->m_specific_param.m_enc.m_cinema) {
6080 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
6081 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
6082 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6086 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
6087 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
6093 static void j2k_read_eoc(opj_j2k_t *j2k) {
6095 opj_bool success = OPJ_FALSE;
6097 /* if packets should be decoded */
6098 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
6099 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
6100 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
6101 for (i = 0; i < j2k->cp->tileno_size; i++) {
6102 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
6103 if (j2k->cp->tileno[i] != -1)
6105 tileno = j2k->cp->tileno[i];
6106 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
6107 opj_free(j2k->tile_data[tileno]);
6108 j2k->tile_data[tileno] = NULL;
6109 tcd_free_decode_tile(tcd, i);
6112 success = OPJ_FALSE;
6113 if (success == OPJ_FALSE) {
6114 j2k->state |= J2K_STATE_ERR;
6118 tcd_free_decode(tcd);
6121 /* if packets should not be decoded */
6123 for (i = 0; i < j2k->cp->tileno_size; i++) {
6124 tileno = j2k->cp->tileno[i];
6125 opj_free(j2k->tile_data[tileno]);
6126 j2k->tile_data[tileno] = NULL;
6129 if (j2k->state & J2K_STATE_ERR)
6130 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
6132 j2k->state = J2K_STATE_MT;
6136 * Reads a EOC marker (End Of Codestream)
6138 * @param p_header_data the data contained in the SOD box.
6139 * @param p_j2k the jpeg2000 codec.
6140 * @param p_header_size the size of the data contained in the SOD marker.
6141 * @param p_manager the user event manager.
6144 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
6145 struct opj_stream_private *p_stream,
6146 struct opj_event_mgr * p_manager )
6149 opj_tcd_v2_t * l_tcd = 00;
6150 OPJ_UINT32 l_nb_tiles;
6151 opj_tcp_v2_t * l_tcp = 00;
6155 assert(p_j2k != 00);
6156 assert(p_manager != 00);
6157 assert(p_stream != 00);
6159 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6160 l_tcp = p_j2k->m_cp.tcps;
6162 l_tcd = tcd_create_v2(OPJ_TRUE);
6164 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6168 for (i = 0; i < l_nb_tiles; ++i) {
6169 if (l_tcp->m_data) {
6170 if (! tcd_init_decode_tile(l_tcd, i)) {
6171 tcd_destroy_v2(l_tcd);
6172 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6176 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
6180 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
6185 j2k_tcp_destroy(l_tcp);
6189 tcd_destroy_v2(l_tcd);
6195 * Gets the offset of the header.
6197 * @param p_stream the stream to write data to.
6198 * @param p_j2k J2K codec.
6199 * @param p_manager the user event manager.
6201 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
6202 struct opj_stream_private *p_stream,
6203 struct opj_event_mgr * p_manager )
6206 assert(p_j2k != 00);
6207 assert(p_manager != 00);
6208 assert(p_stream != 00);
6210 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
6216 * Writes the MCT marker (Multiple Component Transform)
6218 * @param p_stream the stream to write data to.
6219 * @param p_j2k J2K codec.
6220 * @param p_manager the user event manager.
6222 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
6223 struct opj_stream_private *p_stream,
6224 struct opj_event_mgr * p_manager )
6227 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6228 opj_mct_data_t * l_mct_record;
6229 opj_tcp_v2_t * l_tcp;
6232 assert(p_j2k != 00);
6233 assert(p_stream != 00);
6234 assert(p_manager != 00);
6236 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
6240 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6241 l_mct_record = l_tcp->m_mct_records;
6243 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6245 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
6252 l_mcc_record = l_tcp->m_mcc_records;
6254 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6256 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6263 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6271 * Writes the image components.
6273 * @param p_stream the stream to write data to.
6274 * @param p_j2k J2K codec.
6275 * @param p_manager the user event manager.
6277 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6278 struct opj_stream_private *p_stream,
6279 struct opj_event_mgr * p_manager )
6284 assert(p_j2k != 00);
6285 assert(p_manager != 00);
6286 assert(p_stream != 00);
6288 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6290 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6294 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6303 * Writes regions of interests.
6305 * @param p_stream the stream to write data to.
6306 * @param p_j2k J2K codec.
6307 * @param p_manager the user event manager.
6309 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
6310 struct opj_stream_private *p_stream,
6311 struct opj_event_mgr * p_manager )
6314 const opj_tccp_t *l_tccp = 00;
6317 assert(p_j2k != 00);
6318 assert(p_manager != 00);
6319 assert(p_stream != 00);
6321 l_tccp = p_j2k->m_cp.tcps->tccps;
6323 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6324 if (l_tccp->roishift) {
6326 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6340 * @param p_stream the stream to write data to.
6341 * @param p_j2k J2K codec.
6342 * @param p_manager the user event manager.
6344 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6345 struct opj_stream_private *p_stream,
6346 struct opj_event_mgr * p_manager )
6348 opj_codestream_index_t * l_cstr_index = 00;
6351 assert(p_j2k != 00);
6352 assert(p_manager != 00);
6353 assert(p_stream != 00);
6355 l_cstr_index = p_j2k->cstr_index;
6357 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6359 /* The following adjustment is done to adjust the codestream size */
6360 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6361 /* the first bunch of bytes is not in the codestream */
6362 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6367 /* preparation of JPWL marker segments */
6371 /* encode according to JPWL */
6372 jpwl_encode(p_j2k, p_stream, image);
6376 assert( 0 && "TODO" );
6377 #endif /* USE_JPWL */
6382 typedef struct opj_dec_mstabent {
6385 /** value of the state when the marker can appear */
6387 /** action linked to the marker */
6388 void (*handler) (opj_j2k_t *j2k);
6389 } opj_dec_mstabent_t;
6391 opj_dec_mstabent_t j2k_dec_mstab[] = {
6392 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
6393 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
6394 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
6395 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6396 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
6397 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
6398 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6399 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6400 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6401 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6402 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6403 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6404 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6405 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6406 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6407 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6409 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6410 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6413 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6414 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6415 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6416 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6417 #endif /* USE_JPWL */
6419 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6420 {J2K_MS_INSEC, 0, j2k_read_insec},
6421 #endif /* USE_JPSEC */
6423 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6426 static void j2k_read_unk(opj_j2k_t *j2k) {
6427 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6430 if (j2k->cp->correct) {
6432 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6433 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6434 id = cio_read(j2k->cio, 2);
6435 opj_event_msg(j2k->cinfo, EVT_ERROR,
6436 "JPWL: really don't know this marker %x\n",
6439 opj_event_msg(j2k->cinfo, EVT_ERROR,
6440 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6443 /* OK, activate this at your own risk!!! */
6444 /* we look for the marker at the minimum hamming distance from this */
6445 while (j2k_dec_mstab[m].id) {
6447 /* 1's where they differ */
6448 tmp_id = j2k_dec_mstab[m].id ^ id;
6450 /* compute the hamming distance between our id and the current */
6452 for (i = 0; i < 16; i++) {
6453 if ((tmp_id >> i) & 0x0001) {
6458 /* if current distance is smaller, set the minimum */
6459 if (cur_dist < min_dist) {
6460 min_dist = cur_dist;
6461 min_id = j2k_dec_mstab[m].id;
6464 /* jump to the next marker */
6468 /* do we substitute the marker? */
6469 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6470 opj_event_msg(j2k->cinfo, EVT_ERROR,
6471 "- marker %x is at distance %d from the read %x\n",
6472 min_id, min_dist, id);
6473 opj_event_msg(j2k->cinfo, EVT_ERROR,
6474 "- trying to substitute in place and crossing fingers!\n");
6475 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6476 cio_write(j2k->cio, min_id, 2);
6479 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6484 #endif /* USE_JPWL */
6489 * Reads an unknown marker
6491 * @param p_stream the stream object to read from.
6492 * @param p_j2k the jpeg2000 codec.
6493 * @param p_manager the user event manager.
6495 * @return true if the marker could be deduced.
6497 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6498 struct opj_stream_private *p_stream,
6499 OPJ_UINT32 *output_marker,
6500 struct opj_event_mgr * p_manager
6503 OPJ_UINT32 l_unknown_marker;
6504 const opj_dec_memory_marker_handler_t * l_marker_handler;
6505 OPJ_UINT32 l_size_unk = 2;
6508 assert(p_j2k != 00);
6509 assert(p_manager != 00);
6510 assert(p_stream != 00);
6512 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6515 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6516 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6517 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6521 /* read 2 bytes as the new marker ID*/
6522 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6524 if (!(l_unknown_marker < 0xff00)) {
6526 /* Get the marker handler from the marker ID*/
6527 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6529 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6530 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6534 if (l_marker_handler->id != J2K_MS_UNK) {
6535 /* Add the marker to the codestream index*/
6536 if (l_marker_handler->id != J2K_MS_SOT)
6537 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6538 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6540 break; /* next marker is known and well located */
6548 *output_marker = l_marker_handler->id ;
6554 Read the lookup table containing all the marker, status and action
6555 @param id Marker value
6557 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6558 opj_dec_mstabent_t *e;
6559 for (e = j2k_dec_mstab; e->id != 0; e++) {
6568 * Writes the MCT marker (Multiple Component Transform)
6570 * @param p_stream the stream to write data to.
6571 * @param p_j2k J2K codec.
6572 * @param p_manager the user event manager.
6574 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6575 opj_mct_data_t * p_mct_record,
6576 struct opj_stream_private *p_stream,
6577 struct opj_event_mgr * p_manager )
6579 OPJ_UINT32 l_mct_size;
6580 OPJ_BYTE * l_current_data = 00;
6584 assert(p_j2k != 00);
6585 assert(p_manager != 00);
6586 assert(p_stream != 00);
6588 l_mct_size = 10 + p_mct_record->m_data_size;
6590 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6591 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6592 = (OPJ_BYTE*)opj_realloc(
6593 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6596 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6600 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6603 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6605 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6606 l_current_data += 2;
6608 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6609 l_current_data += 2;
6611 opj_write_bytes(l_current_data,0,2); /* Zmct */
6612 l_current_data += 2;
6614 /* only one marker atm */
6615 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6617 opj_write_bytes(l_current_data,l_tmp,2);
6618 l_current_data += 2;
6620 opj_write_bytes(l_current_data,0,2); /* Ymct */
6623 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6625 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) {
6633 * Reads a MCT marker (Multiple Component Transform)
6635 * @param p_header_data the data contained in the MCT box.
6636 * @param p_j2k the jpeg2000 codec.
6637 * @param p_header_size the size of the data contained in the MCT marker.
6638 * @param p_manager the user event manager.
6640 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6641 OPJ_BYTE * p_header_data,
6642 OPJ_UINT32 p_header_size,
6643 struct opj_event_mgr * p_manager )
6646 opj_tcp_v2_t *l_tcp = 00;
6649 opj_mct_data_t * l_mct_data;
6652 assert(p_header_data != 00);
6653 assert(p_j2k != 00);
6655 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6656 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6657 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6659 if (p_header_size < 2) {
6660 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6665 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6668 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6672 if(p_header_size <= 6) {
6673 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6677 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6678 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6681 l_indix = l_tmp & 0xff;
6682 l_mct_data = l_tcp->m_mct_records;
6684 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6685 if (l_mct_data->m_index == l_indix) {
6692 if (i == l_tcp->m_nb_mct_records) {
6693 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6694 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6696 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));
6697 if(! l_tcp->m_mct_records) {
6698 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6702 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6703 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6706 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6709 if (l_mct_data->m_data) {
6710 opj_free(l_mct_data->m_data);
6711 l_mct_data->m_data = 00;
6714 l_mct_data->m_index = l_indix;
6715 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6716 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6718 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6721 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6727 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6728 if (! l_mct_data->m_data) {
6729 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6732 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6734 l_mct_data->m_data_size = p_header_size;
6735 ++l_tcp->m_nb_mct_records;
6741 * Writes the MCC marker (Multiple Component Collection)
6743 * @param p_stream the stream to write data to.
6744 * @param p_j2k J2K codec.
6745 * @param p_manager the user event manager.
6747 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6748 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6749 struct opj_stream_private *p_stream,
6750 struct opj_event_mgr * p_manager )
6753 OPJ_UINT32 l_mcc_size;
6754 OPJ_BYTE * l_current_data = 00;
6755 OPJ_UINT32 l_nb_bytes_for_comp;
6760 assert(p_j2k != 00);
6761 assert(p_manager != 00);
6762 assert(p_stream != 00);
6764 if (p_mcc_record->m_nb_comps > 255 ) {
6765 l_nb_bytes_for_comp = 2;
6769 l_nb_bytes_for_comp = 1;
6773 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6774 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6776 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6777 = (OPJ_BYTE*)opj_realloc(
6778 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6780 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6784 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6787 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6789 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6790 l_current_data += 2;
6792 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6793 l_current_data += 2;
6796 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6797 l_current_data += 2;
6799 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6802 /* only one marker atm */
6803 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6806 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6809 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6812 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 */
6815 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6816 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6817 l_current_data+=l_nb_bytes_for_comp;
6820 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 */
6823 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6825 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6826 l_current_data+=l_nb_bytes_for_comp;
6829 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6831 if (p_mcc_record->m_decorrelation_array) {
6832 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6835 if (p_mcc_record->m_offset_array) {
6836 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6839 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6842 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) {
6850 * Reads a MCC marker (Multiple Component Collection)
6852 * @param p_header_data the data contained in the MCC box.
6853 * @param p_j2k the jpeg2000 codec.
6854 * @param p_header_size the size of the data contained in the MCC marker.
6855 * @param p_manager the user event manager.
6857 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6858 OPJ_BYTE * p_header_data,
6859 OPJ_UINT32 p_header_size,
6860 struct opj_event_mgr * p_manager )
6865 opj_tcp_v2_t * l_tcp;
6866 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6867 opj_mct_data_t * l_mct_data;
6868 OPJ_UINT32 l_nb_collections;
6869 OPJ_UINT32 l_nb_comps;
6870 OPJ_UINT32 l_nb_bytes_by_comp;
6874 assert(p_header_data != 00);
6875 assert(p_j2k != 00);
6876 assert(p_manager != 00);
6878 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6879 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6880 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6882 if (p_header_size < 2) {
6883 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6888 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6891 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6895 if (p_header_size < 7) {
6896 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6900 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6903 l_mcc_record = l_tcp->m_mcc_records;
6905 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6906 if (l_mcc_record->m_index == l_indix) {
6913 if (i == l_tcp->m_nb_mcc_records) {
6914 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6915 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6917 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6918 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6919 if (! l_tcp->m_mcc_records) {
6920 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6923 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6924 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));
6926 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6928 l_mcc_record->m_index = l_indix;
6930 /* only one marker atm */
6931 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6934 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6938 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6941 if (l_nb_collections > 1) {
6942 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6948 for (i=0;i<l_nb_collections;++i) {
6949 if (p_header_size < 3) {
6950 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6954 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6958 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6962 opj_read_bytes(p_header_data,&l_nb_comps,2);
6967 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6968 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6970 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6971 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6975 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6977 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6978 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6979 p_header_data+=l_nb_bytes_by_comp;
6982 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6987 opj_read_bytes(p_header_data,&l_nb_comps,2);
6990 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6991 l_nb_comps &= 0x7fff;
6993 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6994 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6998 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6999 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7003 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
7005 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7006 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
7007 p_header_data+=l_nb_bytes_by_comp;
7010 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7015 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
7018 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
7019 l_mcc_record->m_decorrelation_array = 00;
7020 l_mcc_record->m_offset_array = 00;
7022 l_indix = l_tmp & 0xff;
7024 l_mct_data = l_tcp->m_mct_records;
7025 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7026 if (l_mct_data->m_index == l_indix) {
7027 l_mcc_record->m_decorrelation_array = l_mct_data;
7033 if (l_mcc_record->m_decorrelation_array == 00) {
7034 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7039 l_indix = (l_tmp >> 8) & 0xff;
7041 l_mct_data = l_tcp->m_mct_records;
7042 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7043 if (l_mct_data->m_index == l_indix) {
7044 l_mcc_record->m_offset_array = l_mct_data;
7050 if (l_mcc_record->m_offset_array == 00) {
7051 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7057 if (p_header_size != 0) {
7058 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7062 ++l_tcp->m_nb_mcc_records;
7069 * Writes the MCO marker (Multiple component transformation ordering)
7071 * @param p_stream the stream to write data to.
7072 * @param p_j2k J2K codec.
7073 * @param p_manager the user event manager.
7075 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
7076 struct opj_stream_private *p_stream,
7077 struct opj_event_mgr * p_manager
7080 OPJ_BYTE * l_current_data = 00;
7081 OPJ_UINT32 l_mco_size;
7082 opj_tcp_v2_t * l_tcp = 00;
7083 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7087 assert(p_j2k != 00);
7088 assert(p_manager != 00);
7089 assert(p_stream != 00);
7091 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
7092 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7094 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
7095 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7097 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7098 = (OPJ_BYTE*)opj_realloc(
7099 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7101 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
7106 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
7109 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
7110 l_current_data += 2;
7112 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
7113 l_current_data += 2;
7115 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
7118 l_mcc_record = l_tcp->m_mcc_records;
7119 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
7120 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
7126 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) {
7134 * Reads a MCO marker (Multiple Component Transform Ordering)
7136 * @param p_header_data the data contained in the MCO box.
7137 * @param p_j2k the jpeg2000 codec.
7138 * @param p_header_size the size of the data contained in the MCO marker.
7139 * @param p_manager the user event manager.
7141 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
7142 OPJ_BYTE * p_header_data,
7143 OPJ_UINT32 p_header_size,
7144 struct opj_event_mgr * p_manager )
7146 OPJ_UINT32 l_tmp, i;
7147 OPJ_UINT32 l_nb_stages;
7148 opj_tcp_v2_t * l_tcp;
7149 opj_tccp_t * l_tccp;
7150 opj_image_t * l_image;
7151 opj_image_comp_t * l_img_comp;
7154 assert(p_header_data != 00);
7155 assert(p_j2k != 00);
7156 assert(p_manager != 00);
7158 l_image = p_j2k->m_private_image;
7159 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7160 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7161 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7163 if (p_header_size < 1) {
7164 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
7168 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
7171 if (l_nb_stages > 1) {
7172 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
7176 if (p_header_size != l_nb_stages + 1) {
7177 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
7181 l_tccp = l_tcp->tccps;
7182 l_img_comp = l_image->comps;
7184 for (i=0;i<l_image->numcomps;++i) {
7185 l_tccp->m_dc_level_shift = 0;
7189 if (l_tcp->m_mct_decoding_matrix) {
7190 opj_free(l_tcp->m_mct_decoding_matrix);
7191 l_tcp->m_mct_decoding_matrix = 00;
7194 for (i=0;i<l_nb_stages;++i) {
7195 opj_read_bytes(p_header_data,&l_tmp,1);
7198 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
7206 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
7209 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7210 opj_mct_data_t * l_deco_array, * l_offset_array;
7211 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
7212 OPJ_UINT32 l_nb_elem;
7213 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
7214 opj_tccp_t * l_tccp;
7217 assert(p_tcp != 00);
7219 l_mcc_record = p_tcp->m_mcc_records;
7221 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
7222 if (l_mcc_record->m_index == p_index) {
7227 if (i==p_tcp->m_nb_mcc_records) {
7228 /** element discarded **/
7232 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
7233 /** do not support number of comps != image */
7237 l_deco_array = l_mcc_record->m_decorrelation_array;
7240 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
7241 if (l_deco_array->m_data_size != l_data_size) {
7245 l_nb_elem = p_image->numcomps * p_image->numcomps;
7246 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
7247 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7249 if (! p_tcp->m_mct_decoding_matrix ) {
7253 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);
7256 l_offset_array = l_mcc_record->m_offset_array;
7258 if (l_offset_array) {
7259 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
7260 if (l_offset_array->m_data_size != l_data_size) {
7264 l_nb_elem = p_image->numcomps;
7265 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7266 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7268 if (! l_offset_data ) {
7272 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7274 l_tccp = p_tcp->tccps;
7275 l_current_offset_data = l_offset_data;
7277 for (i=0;i<p_image->numcomps;++i) {
7278 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7282 opj_free(l_offset_data);
7289 * Writes the CBD marker (Component bit depth definition)
7291 * @param p_stream the stream to write data to.
7292 * @param p_j2k J2K codec.
7293 * @param p_manager the user event manager.
7295 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7296 struct opj_stream_private *p_stream,
7297 struct opj_event_mgr * p_manager )
7300 OPJ_UINT32 l_cbd_size;
7301 OPJ_BYTE * l_current_data = 00;
7302 opj_image_t *l_image = 00;
7303 opj_image_comp_t * l_comp = 00;
7306 assert(p_j2k != 00);
7307 assert(p_manager != 00);
7308 assert(p_stream != 00);
7310 l_image = p_j2k->m_private_image;
7311 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7313 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7314 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7315 = (OPJ_BYTE*)opj_realloc(
7316 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7319 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7323 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7326 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7328 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7329 l_current_data += 2;
7331 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7332 l_current_data += 2;
7334 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7337 l_comp = l_image->comps;
7339 for (i=0;i<l_image->numcomps;++i) {
7340 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7346 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) {
7354 * Reads a CBD marker (Component bit depth definition)
7355 * @param p_header_data the data contained in the CBD box.
7356 * @param p_j2k the jpeg2000 codec.
7357 * @param p_header_size the size of the data contained in the CBD marker.
7358 * @param p_manager the user event manager.
7360 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7361 OPJ_BYTE * p_header_data,
7362 OPJ_UINT32 p_header_size,
7363 struct opj_event_mgr * p_manager)
7365 OPJ_UINT32 l_nb_comp,l_num_comp;
7366 OPJ_UINT32 l_comp_def;
7368 opj_image_comp_t * l_comp = 00;
7371 assert(p_header_data != 00);
7372 assert(p_j2k != 00);
7373 assert(p_manager != 00);
7375 l_num_comp = p_j2k->m_private_image->numcomps;
7377 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7378 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7382 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7385 if (l_nb_comp != l_num_comp) {
7386 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7390 l_comp = p_j2k->m_private_image->comps;
7391 for (i=0;i<l_num_comp;++i) {
7392 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7394 l_comp->sgnd = (l_comp_def>>7) & 1;
7395 l_comp->prec = (l_comp_def&0x7f) + 1;
7403 /* ----------------------------------------------------------------------- */
7404 /* J2K / JPT decoder interface */
7405 /* ----------------------------------------------------------------------- */
7409 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7412 if(j2k->tile_len != NULL) {
7413 opj_free(j2k->tile_len);
7415 if(j2k->tile_data != NULL) {
7416 opj_free(j2k->tile_data);
7418 if(j2k->default_tcp != NULL) {
7419 opj_tcp_t *default_tcp = j2k->default_tcp;
7420 if(default_tcp->ppt_data_first != NULL) {
7421 opj_free(default_tcp->ppt_data_first);
7423 if(j2k->default_tcp->tccps != NULL) {
7424 opj_free(j2k->default_tcp->tccps);
7426 opj_free(j2k->default_tcp);
7428 if(j2k->cp != NULL) {
7429 opj_cp_t *cp = j2k->cp;
7430 if(cp->tcps != NULL) {
7431 for(i = 0; i < cp->tw * cp->th; i++) {
7432 if(cp->tcps[i].ppt_data_first != NULL) {
7433 opj_free(cp->tcps[i].ppt_data_first);
7435 if(cp->tcps[i].tccps != NULL) {
7436 opj_free(cp->tcps[i].tccps);
7441 if(cp->ppm_data_first != NULL) {
7442 opj_free(cp->ppm_data_first);
7444 if(cp->tileno != NULL) {
7445 opj_free(cp->tileno);
7447 if(cp->comment != NULL) {
7448 opj_free(cp->comment);
7457 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7459 if(j2k && parameters) {
7460 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7461 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7464 j2k->m_cp.correct = parameters->jpwl_correct;
7465 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7466 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7467 #endif /* USE_JPWL */
7474 * Read a JPT-stream and decode file
7477 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7478 opj_image_t *image = NULL;
7479 opj_jpt_msg_header_t header;
7481 opj_common_ptr cinfo = j2k->cinfo;
7483 OPJ_ARG_NOT_USED(cstr_info);
7487 /* create an empty image */
7488 image = opj_image_create0();
7491 j2k->state = J2K_STATE_MHSOC;
7493 /* Initialize the header */
7494 jpt_init_msg_header(&header);
7495 /* Read the first header of the message */
7496 jpt_read_msg_header(cinfo, cio, &header);
7498 position = cio_tell(cio);
7499 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7500 opj_image_destroy(image);
7501 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7506 opj_dec_mstabent_t *e = NULL;
7509 if (!cio_numbytesleft(cio)) {
7513 /* data-bin read -> need to read a new header */
7514 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7515 jpt_read_msg_header(cinfo, cio, &header);
7516 position = cio_tell(cio);
7517 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7518 opj_image_destroy(image);
7519 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7524 id = cio_read(cio, 2);
7525 if (id >> 8 != 0xff) {
7526 opj_image_destroy(image);
7527 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7530 e = j2k_dec_mstab_lookup(id);
7531 if (!(j2k->state & e->states)) {
7532 opj_image_destroy(image);
7533 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7539 if (j2k->state == J2K_STATE_MT) {
7542 if (j2k->state == J2K_STATE_NEOC) {
7546 if (j2k->state == J2K_STATE_NEOC) {
7550 if (j2k->state != J2K_STATE_MT) {
7551 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7557 /* ----------------------------------------------------------------------- */
7558 /* J2K encoder interface */
7559 /* ----------------------------------------------------------------------- */
7561 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7562 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7569 opj_j2k_v2_t* j2k_create_compress_v2(void)
7571 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7576 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7578 l_j2k->m_is_decoder = 0;
7579 l_j2k->m_cp.m_is_decoder = 0;
7581 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7582 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7587 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7589 /* validation list creation*/
7590 l_j2k->m_validation_list = opj_procedure_list_create();
7591 if (! l_j2k->m_validation_list) {
7596 /* execution list creation*/
7597 l_j2k->m_procedure_list = opj_procedure_list_create();
7598 if (! l_j2k->m_procedure_list) {
7606 void j2k_destroy_compress(opj_j2k_t *j2k) {
7610 if(j2k->cp != NULL) {
7611 opj_cp_t *cp = j2k->cp;
7614 opj_free(cp->comment);
7617 opj_free(cp->matrice);
7619 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7620 opj_free(cp->tcps[tileno].tccps);
7629 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7631 int tileno, numpocs_tile;
7632 opj_cp_t *cp = NULL;
7634 if(!j2k || !parameters || ! image) {
7638 /* create and initialize the coding parameters structure */
7639 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7641 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7644 /* set default values for cp */
7649 copy user encoding parameters
7651 cp->cinema = parameters->cp_cinema;
7652 cp->max_comp_size = parameters->max_comp_size;
7653 cp->rsiz = parameters->cp_rsiz;
7654 cp->disto_alloc = parameters->cp_disto_alloc;
7655 cp->fixed_alloc = parameters->cp_fixed_alloc;
7656 cp->fixed_quality = parameters->cp_fixed_quality;
7658 /* mod fixed_quality */
7659 if(parameters->cp_matrice) {
7660 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7661 cp->matrice = (int *) opj_malloc(array_size);
7662 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7666 cp->tdx = parameters->cp_tdx;
7667 cp->tdy = parameters->cp_tdy;
7670 cp->tx0 = parameters->cp_tx0;
7671 cp->ty0 = parameters->cp_ty0;
7673 /* comment string */
7674 if(parameters->cp_comment) {
7675 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7677 strcpy(cp->comment, parameters->cp_comment);
7682 calculate other encoding parameters
7685 if (parameters->tile_size_on) {
7686 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7687 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7689 cp->tdx = image->x1 - cp->tx0;
7690 cp->tdy = image->y1 - cp->ty0;
7693 if(parameters->tp_on){
7694 cp->tp_flag = parameters->tp_flag;
7699 for(i=0;i<image->numcomps ;i++){
7700 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7706 calculate JPWL encoding parameters
7709 if (parameters->jpwl_epc_on) {
7713 cp->epc_on = OPJ_TRUE;
7714 cp->info_on = OPJ_FALSE; /* no informative technique */
7717 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7718 cp->epb_on = OPJ_TRUE;
7720 cp->hprot_MH = parameters->jpwl_hprot_MH;
7721 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7722 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7723 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7725 /* if tile specs are not specified, copy MH specs */
7726 if (cp->hprot_TPH[0] == -1) {
7727 cp->hprot_TPH_tileno[0] = 0;
7728 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7730 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7731 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7732 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7733 cp->pprot[i] = parameters->jpwl_pprot[i];
7737 /* set ESD writing */
7738 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7739 cp->esd_on = OPJ_TRUE;
7741 cp->sens_size = parameters->jpwl_sens_size;
7742 cp->sens_addr = parameters->jpwl_sens_addr;
7743 cp->sens_range = parameters->jpwl_sens_range;
7745 cp->sens_MH = parameters->jpwl_sens_MH;
7746 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7747 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7748 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7752 /* always set RED writing to false: we are at the encoder */
7753 cp->red_on = OPJ_FALSE;
7756 cp->epc_on = OPJ_FALSE;
7758 #endif /* USE_JPWL */
7761 /* initialize the mutiple tiles */
7762 /* ---------------------------- */
7763 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7765 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7766 opj_tcp_t *tcp = &cp->tcps[tileno];
7767 tcp->numlayers = parameters->tcp_numlayers;
7768 assert ( tcp->numlayers >= 0 );
7769 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7771 if (cp->fixed_quality) {
7772 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7774 tcp->rates[j] = parameters->tcp_rates[j];
7776 if (cp->fixed_quality) { /* add fixed_quality */
7777 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7779 tcp->rates[j] = parameters->tcp_rates[j];
7783 tcp->csty = parameters->csty;
7784 tcp->prg = parameters->prog_order;
7785 tcp->mct = parameters->tcp_mct;
7789 if (parameters->numpocs) {
7790 /* initialisation of POC */
7792 assert( parameters->numpocs >= 0 );
7793 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7794 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7795 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7796 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7797 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7798 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7799 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7800 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7801 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7802 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7806 tcp->numpocs = numpocs_tile -1 ;
7811 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7813 for (i = 0; i < image->numcomps; i++) {
7814 opj_tccp_t *tccp = &tcp->tccps[i];
7815 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7816 tccp->numresolutions = parameters->numresolution;
7817 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7818 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7819 tccp->cblksty = parameters->mode;
7820 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7821 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7823 assert(parameters->roi_compno >= 0);
7824 if (i == (OPJ_UINT32)parameters->roi_compno) {
7825 tccp->roishift = parameters->roi_shift;
7830 if(parameters->cp_cinema)
7832 /*Precinct size for lowest frequency subband=128*/
7835 /*Precinct size at all other resolutions = 256*/
7836 for (j = 1; j < tccp->numresolutions; j++) {
7841 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7843 assert(tccp->numresolutions > 0);
7844 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7845 if (p < parameters->res_spec) {
7847 if (parameters->prcw_init[p] < 1) {
7850 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7853 if (parameters->prch_init[p] < 1) {
7856 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7860 int res_spec = parameters->res_spec;
7861 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7862 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7864 if (size_prcw < 1) {
7867 tccp->prcw[j] = int_floorlog2(size_prcw);
7870 if (size_prch < 1) {
7873 tccp->prch[j] = int_floorlog2(size_prch);
7877 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7880 for (j = 0; j < tccp->numresolutions; j++) {
7887 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7892 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7893 opj_cparameters_t *parameters,
7895 struct opj_event_mgr * p_manager)
7897 OPJ_UINT32 i, j, tileno, numpocs_tile;
7898 opj_cp_v2_t *cp = 00;
7901 if(!p_j2k || !parameters || ! image) {
7905 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7906 cp = &(p_j2k->m_cp);
7908 /* set default values for cp */
7913 copy user encoding parameters
7915 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7916 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7917 cp->rsiz = parameters->cp_rsiz;
7918 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7919 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7920 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7922 /* mod fixed_quality */
7923 if (parameters->cp_matrice) {
7924 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7925 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7926 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7930 cp->tdx = parameters->cp_tdx;
7931 cp->tdy = parameters->cp_tdy;
7934 cp->tx0 = parameters->cp_tx0;
7935 cp->ty0 = parameters->cp_ty0;
7937 /* comment string */
7938 if(parameters->cp_comment) {
7939 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7941 strcpy(cp->comment, parameters->cp_comment);
7946 calculate other encoding parameters
7949 if (parameters->tile_size_on) {
7950 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7951 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7953 cp->tdx = image->x1 - cp->tx0;
7954 cp->tdy = image->y1 - cp->ty0;
7957 if (parameters->tp_on) {
7958 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7959 cp->m_specific_param.m_enc.m_tp_on = 1;
7964 calculate JPWL encoding parameters
7967 if (parameters->jpwl_epc_on) {
7971 cp->epc_on = OPJ_TRUE;
7972 cp->info_on = OPJ_FALSE; /* no informative technique */
7975 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7976 cp->epb_on = OPJ_TRUE;
7978 cp->hprot_MH = parameters->jpwl_hprot_MH;
7979 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7980 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7981 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7983 /* if tile specs are not specified, copy MH specs */
7984 if (cp->hprot_TPH[0] == -1) {
7985 cp->hprot_TPH_tileno[0] = 0;
7986 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7988 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7989 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7990 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7991 cp->pprot[i] = parameters->jpwl_pprot[i];
7995 /* set ESD writing */
7996 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7997 cp->esd_on = OPJ_TRUE;
7999 cp->sens_size = parameters->jpwl_sens_size;
8000 cp->sens_addr = parameters->jpwl_sens_addr;
8001 cp->sens_range = parameters->jpwl_sens_range;
8003 cp->sens_MH = parameters->jpwl_sens_MH;
8004 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8005 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8006 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8010 /* always set RED writing to false: we are at the encoder */
8011 cp->red_on = OPJ_FALSE;
8014 cp->epc_on = OPJ_FALSE;
8016 #endif /* USE_JPWL */
8019 /* initialize the mutiple tiles */
8020 /* ---------------------------- */
8021 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
8022 if (parameters->numpocs) {
8023 /* initialisation of POC */
8024 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
8028 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8029 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
8030 tcp->numlayers = parameters->tcp_numlayers;
8032 for (j = 0; j < tcp->numlayers; j++) {
8033 if(cp->m_specific_param.m_enc.m_cinema){
8034 if (cp->m_specific_param.m_enc.m_fixed_quality) {
8035 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8037 tcp->rates[j] = parameters->tcp_rates[j];
8039 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
8040 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8042 tcp->rates[j] = parameters->tcp_rates[j];
8047 tcp->csty = parameters->csty;
8048 tcp->prg = parameters->prog_order;
8049 tcp->mct = parameters->tcp_mct;
8054 if (parameters->numpocs) {
8055 /* initialisation of POC */
8058 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
8059 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8060 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8062 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
8063 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
8064 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
8065 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
8066 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
8067 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
8068 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
8074 tcp->numpocs = numpocs_tile -1 ;
8079 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8081 if (parameters->mct_data) {
8083 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
8086 /* TODO MSD : merge v2 add invert.c or used a external lib ?
8087 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
8088 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8089 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
8092 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8093 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
8094 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
8096 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8097 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
8099 tcp->mct_norms = (OPJ_FLOAT64*)
8100 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
8102 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
8105 for (i = 0; i < image->numcomps; i++) {
8106 opj_tccp_t *tccp = &tcp->tccps[i];
8107 tccp->m_dc_level_shift = l_dc_shift[i];
8110 j2k_setup_mct_encoding(tcp,image);
8114 for (i = 0; i < image->numcomps; i++) {
8115 opj_tccp_t *tccp = &tcp->tccps[i];
8116 opj_image_comp_t * l_comp = &(image->comps[i]);
8118 if (! l_comp->sgnd) {
8119 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
8124 for (i = 0; i < image->numcomps; i++) {
8125 opj_tccp_t *tccp = &tcp->tccps[i];
8127 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
8128 tccp->numresolutions = parameters->numresolution;
8129 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8130 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8131 tccp->cblksty = parameters->mode;
8132 tccp->qmfbid = parameters->irreversible ? 0 : 1;
8133 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8136 if (i == parameters->roi_compno) {
8137 tccp->roishift = parameters->roi_shift;
8142 if(parameters->cp_cinema) {
8143 //Precinct size for lowest frequency subband=128
8146 //Precinct size at all other resolutions = 256
8147 for (j = 1; j < tccp->numresolutions; j++) {
8152 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8153 OPJ_INT32 p = 0, it_res;
8154 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
8155 if (p < parameters->res_spec) {
8157 if (parameters->prcw_init[p] < 1) {
8158 tccp->prcw[it_res] = 1;
8160 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
8163 if (parameters->prch_init[p] < 1) {
8164 tccp->prch[it_res] = 1;
8166 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
8170 int res_spec = parameters->res_spec;
8171 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8172 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8174 if (size_prcw < 1) {
8175 tccp->prcw[it_res] = 1;
8177 tccp->prcw[it_res] = int_floorlog2(size_prcw);
8180 if (size_prch < 1) {
8181 tccp->prch[it_res] = 1;
8183 tccp->prch[it_res] = int_floorlog2(size_prch);
8187 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
8190 for (j = 0; j < tccp->numresolutions; j++) {
8197 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8201 if (parameters->mct_data) {
8202 opj_free(parameters->mct_data);
8203 parameters->mct_data = 00;
8208 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
8211 opj_cp_t *cp = NULL;
8213 opj_tcd_t *tcd = NULL; /* TCD component */
8221 j2k->cstr_info = cstr_info;
8224 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
8225 cstr_info->image_w = image->x1 - image->x0;
8226 cstr_info->image_h = image->y1 - image->y0;
8227 cstr_info->prog = (&cp->tcps[0])->prg;
8228 cstr_info->tw = cp->tw;
8229 cstr_info->th = cp->th;
8230 cstr_info->tile_x = cp->tdx; /* new version parser */
8231 cstr_info->tile_y = cp->tdy; /* new version parser */
8232 cstr_info->tile_Ox = cp->tx0; /* new version parser */
8233 cstr_info->tile_Oy = cp->ty0; /* new version parser */
8234 cstr_info->numcomps = image->numcomps;
8235 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
8236 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
8237 for (compno=0; compno < image->numcomps; compno++) {
8238 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
8240 cstr_info->D_max = 0.0; /* ADD Marcela */
8241 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
8242 cstr_info->maxmarknum = 100;
8243 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
8244 cstr_info->marknum = 0;
8254 for (compno = 1; compno < image->numcomps; compno++) {
8255 j2k_write_coc(j2k, compno);
8256 j2k_write_qcc(j2k, compno);
8260 for (compno = 0; compno < image->numcomps; compno++) {
8261 opj_tcp_t *tcp = &cp->tcps[0];
8262 if (tcp->tccps[compno].roishift)
8263 j2k_write_rgn(j2k, compno, 0);
8265 if (cp->comment != NULL) {
8269 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
8273 if (cp->cinema == CINEMA4K_24) {
8278 /* uncomment only for testing JPSEC marker writing */
8279 /* j2k_write_sec(j2k); */
8283 cstr_info->main_head_end = cio_tell(cio) - 1;
8286 /**** Main Header ENDS here ***/
8288 /* create the tile encoder */
8289 tcd = tcd_create(j2k->cinfo);
8291 /* encode each tile */
8292 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8296 int acc_pack_num = 0;
8300 opj_tcp_t *tcp = &cp->tcps[tileno];
8301 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8303 j2k->curtileno = tileno;
8304 j2k->cur_tp_num = 0;
8305 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8306 /* initialisation before tile encoding */
8308 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8310 tcd_init_encode(tcd, image, cp, j2k->curtileno);
8315 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8316 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8317 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8318 cstr_info->tile[j2k->curtileno].marknum = 0;
8322 for(pino = 0; pino <= tcp->numpocs; pino++) {
8326 /*Get number of tile parts*/
8327 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8328 tcd->tp_pos = cp->tp_pos;
8330 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8331 j2k->tp_num = tilepartno;
8334 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8335 cio_tell(cio) + j2k->pos_correction;
8339 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8340 for (compno = 1; compno < image->numcomps; compno++) {
8341 j2k_write_coc(j2k, compno);
8342 j2k_write_qcc(j2k, compno);
8344 if (cp->tcps[tileno].numpocs) {
8351 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8352 cio_tell(cio) + j2k->pos_correction + 1;
8355 j2k_write_sod(j2k, tcd);
8359 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8360 cio_tell(cio) + j2k->pos_correction - 1;
8361 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8363 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8364 cstr_info->packno - acc_pack_num;
8365 acc_pack_num = cstr_info->packno;
8373 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8378 if (tile->PPT) { // BAD PPT !!!
8381 PPT_file=fopen("PPT","rb");
8382 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8383 for (i=0;i<tile->len_ppt;i++) {
8385 fread(&elmt, 1, 1, PPT_file);
8386 fwrite(&elmt,1,1,f);
8395 /* destroy the tile encoder */
8396 tcd_free_encode(tcd);
8399 opj_free(j2k->cur_totnum_tp);
8404 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8406 /* The following adjustment is done to adjust the codestream size */
8407 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8408 /* the first bunch of bytes is not in the codestream */
8409 cstr_info->codestream_size -= cstr_info->main_head_start;
8415 preparation of JPWL marker segments
8419 /* encode according to JPWL */
8420 jpwl_encode(j2k, cio, image);
8423 #endif /* USE_JPWL */
8428 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8430 assert(cstr_info != 00);
8432 /* expand the list? */
8433 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8434 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8435 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8438 /* add the marker */
8439 cstr_info->marker[cstr_info->marknum].type = type;
8440 cstr_info->marker[cstr_info->marknum].pos = pos;
8441 cstr_info->marker[cstr_info->marknum].len = len;
8442 cstr_info->marknum++;
8446 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8448 assert(cstr_index != 00);
8450 /* expand the list? */
8451 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8452 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8453 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8456 /* add the marker */
8457 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8458 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8459 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8460 cstr_index->marknum++;
8464 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8466 opj_marker_info_t *marker;
8468 assert(cstr_info != 00);
8470 /* expand the list? */
8471 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8472 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8473 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8476 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8478 /* add the marker */
8479 marker->type = type;
8482 cstr_info->tile[tileno].marknum++;
8485 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)
8487 assert(cstr_index != 00);
8488 assert(cstr_index->tile_index != 00);
8490 /* expand the list? */
8491 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8492 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8493 cstr_index->tile_index[tileno].marker =
8494 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8495 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8498 /* add the marker */
8499 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8500 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8501 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8502 cstr_index->tile_index[tileno].marknum++;
8504 if (type == J2K_MS_SOT) {
8505 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8507 if (cstr_index->tile_index[tileno].tp_index)
8508 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8515 * -----------------------------------------------------------------------
8516 * -----------------------------------------------------------------------
8517 * -----------------------------------------------------------------------
8521 * Ends the decompression procedures and possibiliy add data to be read after the
8524 opj_bool j2k_end_decompress(
8525 opj_j2k_v2_t *p_j2k,
8526 opj_stream_private_t *p_stream,
8527 opj_event_mgr_t * p_manager)
8536 * Reads a jpeg2000 codestream header structure.
8539 * @param p_stream the stream to read data from.
8540 * @param p_j2k the jpeg2000 codec.
8541 * @param p_manager the user event manager.
8543 * @return true if the box is valid.
8545 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8546 opj_j2k_v2_t* p_j2k,
8547 opj_image_t** p_image,
8548 struct opj_event_mgr* p_manager )
8551 assert(p_j2k != 00);
8552 assert(p_stream != 00);
8553 assert(p_manager != 00);
8555 /* create an empty image header */
8556 p_j2k->m_private_image = opj_image_create0();
8557 if (! p_j2k->m_private_image) {
8561 /* customization of the validation */
8562 j2k_setup_decoding_validation(p_j2k);
8564 /* validation of the parameters codec */
8565 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8566 opj_image_destroy(p_j2k->m_private_image);
8567 p_j2k->m_private_image = NULL;
8571 /* customization of the encoding */
8572 j2k_setup_header_reading(p_j2k);
8575 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8576 opj_image_destroy(p_j2k->m_private_image);
8577 p_j2k->m_private_image = NULL;
8581 *p_image = opj_image_create0();
8586 /* Copy codestream image information to the output image */
8587 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8589 /*Allocate and initialize some elements of codestrem index*/
8590 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8598 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8600 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8603 assert(p_j2k != 00);
8605 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8607 /* DEVELOPER CORNER, add your custom procedures */
8608 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8613 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8614 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8616 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8619 assert(p_j2k != 00);
8621 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8622 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8623 /* DEVELOPER CORNER, add your custom validation procedure */
8629 * The mct encoding validation procedure.
8631 * @param p_j2k the jpeg2000 codec to validate.
8632 * @param p_stream the input stream to validate.
8633 * @param p_manager the user event manager.
8635 * @return true if the parameters are correct.
8637 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8638 opj_stream_private_t *p_stream,
8639 opj_event_mgr_t * p_manager )
8641 opj_bool l_is_valid = OPJ_TRUE;
8645 assert(p_j2k != 00);
8646 assert(p_stream != 00);
8647 assert(p_manager != 00);
8649 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8650 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8651 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8653 for (i=0;i<l_nb_tiles;++i) {
8654 if (l_tcp->mct == 2) {
8655 opj_tccp_t * l_tccp = l_tcp->tccps;
8656 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8658 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8659 l_is_valid &= ! (l_tccp->qmfbid & 1);
8670 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8673 OPJ_UINT32 l_indix = 1;
8674 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8675 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8676 OPJ_UINT32 l_mct_size,l_nb_elem;
8677 OPJ_FLOAT32 * l_data, * l_current_data;
8678 opj_tccp_t * l_tccp;
8681 assert(p_tcp != 00);
8683 if (p_tcp->mct != 2) {
8687 if (p_tcp->m_mct_decoding_matrix) {
8688 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8689 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8691 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));
8692 if (! p_tcp->m_mct_records) {
8695 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8697 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8699 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8701 if (l_mct_deco_data->m_data) {
8702 opj_free(l_mct_deco_data->m_data);
8703 l_mct_deco_data->m_data = 00;
8706 l_mct_deco_data->m_index = l_indix++;
8707 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8708 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8709 l_nb_elem = p_image->numcomps * p_image->numcomps;
8710 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8711 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8713 if (! l_mct_deco_data->m_data) {
8717 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);
8719 l_mct_deco_data->m_data_size = l_mct_size;
8720 ++p_tcp->m_nb_mct_records;
8723 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8724 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8725 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));
8727 if (! p_tcp->m_mct_records) {
8731 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8732 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8734 if (l_mct_deco_data) {
8735 l_mct_deco_data = l_mct_offset_data - 1;
8739 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8741 if (l_mct_offset_data->m_data) {
8742 opj_free(l_mct_offset_data->m_data);
8743 l_mct_offset_data->m_data = 00;
8746 l_mct_offset_data->m_index = l_indix++;
8747 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8748 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8749 l_nb_elem = p_image->numcomps;
8750 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8751 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8753 if (! l_mct_offset_data->m_data) {
8757 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8759 opj_free(l_mct_offset_data->m_data);
8760 l_mct_offset_data->m_data = 00;
8764 l_tccp = p_tcp->tccps;
8765 l_current_data = l_data;
8767 for (i=0;i<l_nb_elem;++i) {
8768 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8772 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8776 l_mct_offset_data->m_data_size = l_mct_size;
8778 ++p_tcp->m_nb_mct_records;
8780 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8781 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8782 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8783 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8785 if (! p_tcp->m_mcc_records) {
8788 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8789 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));
8793 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8794 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8795 l_mcc_data->m_is_irreversible = 1;
8796 l_mcc_data->m_nb_comps = p_image->numcomps;
8797 l_mcc_data->m_index = l_indix++;
8798 l_mcc_data->m_offset_array = l_mct_offset_data;
8799 ++p_tcp->m_nb_mcc_records;
8805 * Builds the cp decoder parameters to use to decode tile.
8807 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8808 opj_stream_private_t *p_stream,
8809 opj_event_mgr_t * p_manager )
8811 /* add here initialization of cp
8812 copy paste of setup_decoder */
8820 * Builds the cp encoder parameters to use to encode tile.
8822 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8823 opj_stream_private_t *p_stream,
8824 opj_event_mgr_t * p_manager )
8826 /* add here initialization of cp
8827 copy paste of setup_encoder */
8835 * The default encoding validation procedure without any extension.
8837 * @param p_j2k the jpeg2000 codec to validate.
8838 * @param p_stream the input stream to validate.
8839 * @param p_manager the user event manager.
8841 * @return true if the parameters are correct.
8843 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8844 opj_stream_private_t *p_stream,
8845 opj_event_mgr_t * p_manager )
8847 opj_bool l_is_valid = OPJ_TRUE;
8850 assert(p_j2k != 00);
8851 assert(p_stream != 00);
8852 assert(p_manager != 00);
8854 /* STATE checking */
8855 /* make sure the state is at 0 */
8856 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8858 /* POINTER validation */
8859 /* make sure a p_j2k codec is present */
8860 l_is_valid &= (p_j2k->m_procedure_list != 00);
8861 /* make sure a validation list is present */
8862 l_is_valid &= (p_j2k->m_validation_list != 00);
8864 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8865 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8869 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8870 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8874 /* PARAMETER VALIDATION */
8879 * The default decoding validation procedure without any extension.
8881 * @param p_j2k the jpeg2000 codec to validate.
8882 * @param p_stream the input stream to validate.
8883 * @param p_manager the user event manager.
8885 * @return true if the parameters are correct.
8887 opj_bool j2k_decoding_validation (
8888 opj_j2k_v2_t *p_j2k,
8889 opj_stream_private_t *p_stream,
8890 opj_event_mgr_t * p_manager
8893 opj_bool l_is_valid = OPJ_TRUE;
8896 assert(p_j2k != 00);
8897 assert(p_stream != 00);
8898 assert(p_manager != 00);
8901 /* STATE checking */
8902 /* make sure the state is at 0 */
8904 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8906 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8908 /* POINTER validation */
8909 /* make sure a p_j2k codec is present */
8910 /* make sure a procedure list is present */
8911 l_is_valid &= (p_j2k->m_procedure_list != 00);
8912 /* make sure a validation list is present */
8913 l_is_valid &= (p_j2k->m_validation_list != 00);
8915 /* PARAMETER VALIDATION */
8919 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8920 struct opj_stream_private *p_stream,
8921 struct opj_event_mgr * p_manager)
8923 OPJ_UINT32 l_current_marker;
8924 OPJ_UINT32 l_marker_size;
8925 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8928 assert(p_stream != 00);
8929 assert(p_j2k != 00);
8930 assert(p_manager != 00);
8932 /* We enter in the main header */
8933 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8935 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8936 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8937 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8941 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8942 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8943 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8947 /* Read 2 bytes as the new marker ID */
8948 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8950 /* Try to read until the SOT is detected */
8951 while (l_current_marker != J2K_MS_SOT) {
8953 /* Check if the current marker ID is valid */
8954 if (l_current_marker < 0xff00) {
8955 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8959 /* Get the marker handler from the marker ID */
8960 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8962 /* Manage case where marker is unknown */
8963 if (l_marker_handler->id == J2K_MS_UNK) {
8964 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8965 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8969 if (l_current_marker == J2K_MS_SOT)
8970 break; /* SOT marker is detected main header is completely read */
8971 else /* Get the marker handler from the marker ID */
8972 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8975 /* Check if the marker is known and if it is the right place to find it */
8976 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8977 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8981 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8982 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8983 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8987 /* read 2 bytes as the marker size */
8988 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8989 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8991 /* Check if the marker size is compatible with the header data size */
8992 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8993 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8994 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8995 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8998 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9001 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9002 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) {
9003 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9007 /* Read the marker segment with the correct marker handler */
9008 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9009 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
9013 /* Add the marker to the codestream index*/
9014 j2k_add_mhmarker_v2(p_j2k->cstr_index,
9015 l_marker_handler->id,
9016 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9017 l_marker_size + 4 );
9019 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9020 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9021 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9025 /* read 2 bytes as the new marker ID */
9026 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9029 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
9031 /* Position of the last element if the main header */
9032 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
9034 /* Next step: read a tile-part header */
9035 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9041 * Excutes the given procedures on the given codec.
9043 * @param p_procedure_list the list of procedures to execute
9044 * @param p_j2k the jpeg2000 codec to execute the procedures on.
9045 * @param p_stream the stream to execute the procedures on.
9046 * @param p_manager the user manager.
9048 * @return true if all the procedures were successfully executed.
9050 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
9051 opj_procedure_list_t * p_procedure_list,
9052 opj_stream_private_t *p_stream,
9053 opj_event_mgr_t * p_manager )
9055 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
9056 opj_bool l_result = OPJ_TRUE;
9057 OPJ_UINT32 l_nb_proc, i;
9060 assert(p_procedure_list != 00);
9061 assert(p_j2k != 00);
9062 assert(p_stream != 00);
9063 assert(p_manager != 00);
9066 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
9067 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);
9069 for (i=0;i<l_nb_proc;++i) {
9070 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
9074 /* and clear the procedure list at the end.*/
9075 opj_procedure_list_clear(p_procedure_list);
9080 opj_bool j2k_copy_default_tcp_and_create_tcd
9082 opj_j2k_v2_t * p_j2k,
9083 opj_stream_private_t *p_stream,
9084 opj_event_mgr_t * p_manager
9087 opj_tcp_v2_t * l_tcp = 00;
9088 opj_tcp_v2_t * l_default_tcp = 00;
9089 OPJ_UINT32 l_nb_tiles;
9091 opj_tccp_t *l_current_tccp = 00;
9092 OPJ_UINT32 l_tccp_size;
9093 OPJ_UINT32 l_mct_size;
9094 opj_image_t * l_image;
9095 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
9096 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
9097 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
9098 OPJ_UINT32 l_offset;
9101 assert(p_j2k != 00);
9102 assert(p_stream != 00);
9103 assert(p_manager != 00);
9105 l_image = p_j2k->m_private_image;
9106 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9107 l_tcp = p_j2k->m_cp.tcps;
9108 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
9109 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9110 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
9113 for (i=0; i<l_nb_tiles; ++i) {
9114 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
9115 l_current_tccp = l_tcp->tccps;
9116 /*Copy default coding parameters into the current tile coding parameters*/
9117 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
9118 /* Initialize some values of the current tile coding parameters*/
9120 l_tcp->ppt_data = 00;
9121 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
9122 l_tcp->tccps = l_current_tccp;
9124 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
9125 if (l_default_tcp->m_mct_decoding_matrix) {
9126 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
9127 if (! l_tcp->m_mct_decoding_matrix ) {
9130 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
9133 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
9134 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
9135 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
9136 if (! l_tcp->m_mct_records) {
9139 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
9141 /* Copy the mct record data from dflt_tile_cp to the current tile*/
9142 l_src_mct_rec = l_default_tcp->m_mct_records;
9143 l_dest_mct_rec = l_tcp->m_mct_records;
9145 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
9147 if (l_src_mct_rec->m_data) {
9149 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
9150 if(! l_dest_mct_rec->m_data) {
9153 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
9160 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
9161 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
9162 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
9163 if (! l_tcp->m_mcc_records) {
9166 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
9168 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
9169 l_src_mcc_rec = l_default_tcp->m_mcc_records;
9170 l_dest_mcc_rec = l_tcp->m_mcc_records;
9172 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
9174 if (l_src_mcc_rec->m_decorrelation_array) {
9175 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
9176 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
9179 if (l_src_mcc_rec->m_offset_array) {
9180 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
9181 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
9188 /* Copy all the dflt_tile_compo_cp to the current tile cp */
9189 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
9191 /* Move to next tile cp*/
9195 /* Create the current tile decoder*/
9196 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
9197 if (! p_j2k->m_tcd ) {
9201 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
9202 tcd_destroy_v2(p_j2k->m_tcd);
9204 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9212 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
9213 * with the marker value.
9214 * @param p_id Marker value to look up
9216 * @return the handler associated with the id.
9218 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
9220 const opj_dec_memory_marker_handler_t *e;
9221 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
9222 if (e->id == p_id) {
9223 break; /* we find a handler corresponding to the marker ID*/
9231 * Destroys a jpeg2000 codec.
9233 * @param p_j2k the jpeg20000 structure to destroy.
9235 void j2k_destroy (opj_j2k_v2_t *p_j2k)
9241 if (p_j2k->m_is_decoder) {
9243 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
9244 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9245 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9246 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
9249 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
9250 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
9251 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
9252 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
9257 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
9258 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9259 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
9262 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
9263 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
9264 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
9265 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
9268 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
9269 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
9270 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
9271 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
9275 tcd_destroy_v2(p_j2k->m_tcd);
9277 j2k_cp_destroy(&(p_j2k->m_cp));
9278 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
9280 opj_procedure_list_destroy(p_j2k->m_procedure_list);
9281 p_j2k->m_procedure_list = 00;
9283 opj_procedure_list_destroy(p_j2k->m_validation_list);
9284 p_j2k->m_procedure_list = 00;
9286 j2k_destroy_cstr_index(p_j2k->cstr_index);
9287 p_j2k->cstr_index = NULL;
9289 opj_image_destroy(p_j2k->m_private_image);
9290 p_j2k->m_private_image = NULL;
9292 opj_image_destroy(p_j2k->m_output_image);
9293 p_j2k->m_output_image = NULL;
9298 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9302 if (p_cstr_ind->marker) {
9303 opj_free(p_cstr_ind->marker);
9304 p_cstr_ind->marker = NULL;
9307 if (p_cstr_ind->tile_index) {
9308 OPJ_UINT32 it_tile = 0;
9310 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9312 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9313 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9314 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9317 if(p_cstr_ind->tile_index[it_tile].tp_index){
9318 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9319 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9322 if(p_cstr_ind->tile_index[it_tile].marker){
9323 opj_free(p_cstr_ind->tile_index[it_tile].marker);
9324 p_cstr_ind->tile_index[it_tile].marker = NULL;
9329 opj_free( p_cstr_ind->tile_index);
9330 p_cstr_ind->tile_index = NULL;
9333 opj_free(p_cstr_ind);
9340 * Destroys a tile coding parameter structure.
9342 * @param p_tcp the tile coding parameter to destroy.
9344 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9350 if (p_tcp->ppt_buffer != 00) {
9351 opj_free(p_tcp->ppt_buffer);
9352 p_tcp->ppt_buffer = 00;
9355 if (p_tcp->tccps != 00) {
9356 opj_free(p_tcp->tccps);
9360 if (p_tcp->m_mct_coding_matrix != 00) {
9361 opj_free(p_tcp->m_mct_coding_matrix);
9362 p_tcp->m_mct_coding_matrix = 00;
9365 if (p_tcp->m_mct_decoding_matrix != 00) {
9366 opj_free(p_tcp->m_mct_decoding_matrix);
9367 p_tcp->m_mct_decoding_matrix = 00;
9370 if (p_tcp->m_mcc_records) {
9371 opj_free(p_tcp->m_mcc_records);
9372 p_tcp->m_mcc_records = 00;
9373 p_tcp->m_nb_max_mcc_records = 0;
9374 p_tcp->m_nb_mcc_records = 0;
9377 if (p_tcp->m_mct_records) {
9378 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9381 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9382 if (l_mct_data->m_data) {
9383 opj_free(l_mct_data->m_data);
9384 l_mct_data->m_data = 00;
9390 opj_free(p_tcp->m_mct_records);
9391 p_tcp->m_mct_records = 00;
9394 if (p_tcp->mct_norms != 00) {
9395 opj_free(p_tcp->mct_norms);
9396 p_tcp->mct_norms = 00;
9399 j2k_tcp_data_destroy(p_tcp);
9404 * Destroys the data inside a tile coding parameter structure.
9406 * @param p_tcp the tile coding parameter which contain data to destroy.
9408 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9410 if (p_tcp->m_data) {
9411 opj_free(p_tcp->m_data);
9412 p_tcp->m_data = NULL;
9413 p_tcp->m_data_size = 0;
9418 * Destroys a coding parameter structure.
9420 * @param p_cp the coding parameter to destroy.
9422 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9424 OPJ_UINT32 l_nb_tiles;
9425 opj_tcp_v2_t * l_current_tile = 00;
9436 l_current_tile = p_cp->tcps;
9437 l_nb_tiles = p_cp->th * p_cp->tw;
9440 (i = 0; i < l_nb_tiles; ++i)
9442 j2k_tcp_destroy(l_current_tile);
9445 opj_free(p_cp->tcps);
9449 (p_cp->ppm_buffer != 00)
9451 opj_free(p_cp->ppm_buffer);
9452 p_cp->ppm_buffer = 00;
9455 (p_cp->comment != 00)
9457 opj_free(p_cp->comment);
9461 (! p_cp->m_is_decoder)
9464 (p_cp->m_specific_param.m_enc.m_matrice)
9466 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9467 p_cp->m_specific_param.m_enc.m_matrice = 00;
9475 * Reads a tile header.
9476 * @param p_j2k the jpeg2000 codec.
9477 * @param p_stream the stream to write data to.
9478 * @param p_manager the user event manager.
9480 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9481 OPJ_UINT32 * p_tile_index,
9482 OPJ_UINT32 * p_data_size,
9483 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9484 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9485 OPJ_UINT32 * p_nb_comps,
9487 opj_stream_private_t *p_stream,
9488 opj_event_mgr_t * p_manager )
9490 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9491 OPJ_UINT32 l_marker_size;
9492 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9493 opj_tcp_v2_t * l_tcp = NULL;
9494 OPJ_UINT32 l_nb_tiles;
9497 assert(p_stream != 00);
9498 assert(p_j2k != 00);
9499 assert(p_manager != 00);
9501 /* Reach the End Of Codestream ?*/
9502 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9503 l_current_marker = J2K_MS_EOC;
9505 /* We need to encounter a SOT marker (a new tile-part header) */
9506 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9510 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9511 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9513 /* Try to read until the Start Of Data is detected */
9514 while (l_current_marker != J2K_MS_SOD) {
9516 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9517 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9518 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9522 /* Read 2 bytes from the buffer as the marker size */
9523 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9525 /* Why this condition? FIXME */
9526 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9527 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9529 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9531 /* Get the marker handler from the marker ID */
9532 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9534 /* Check if the marker is known and if it is the right place to find it */
9535 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9536 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9539 /* FIXME manage case of unknown marker as in the main header ? */
9541 /* Check if the marker size is compatible with the header data size */
9542 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9543 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9544 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9545 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9548 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9551 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9552 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) {
9553 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9557 /* Read the marker segment with the correct marker handler */
9558 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9559 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9563 /* Add the marker to the codestream index*/
9564 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9566 l_marker_handler->id,
9567 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9568 l_marker_size + 4 );
9570 /* Keep the position of the last SOT marker read */
9571 if ( l_marker_handler->id == J2K_MS_SOT ) {
9572 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9573 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9575 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9580 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9581 /* Skip the rest of the tile part header*/
9582 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) {
9583 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9586 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9589 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9590 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9591 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9594 /* Read 2 bytes from the buffer as the new marker ID */
9595 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9599 /* If we didn't skip data before, we need to read the SOD marker*/
9600 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9601 /* Try to read the SOD marker and skip data ? FIXME */
9602 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
9608 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9609 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9610 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9611 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9615 /* Read 2 bytes from buffer as the new marker ID */
9616 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9620 /* Indicate we will try to read a new tile-part header*/
9621 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9622 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9623 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9625 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9626 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9627 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9631 /* Read 2 bytes from buffer as the new marker ID */
9632 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9636 /* Current marker is the EOC marker ?*/
9637 if (l_current_marker == J2K_MS_EOC) {
9638 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9639 p_j2k->m_current_tile_number = 0;
9640 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9645 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9646 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9647 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9649 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9650 ++p_j2k->m_current_tile_number;
9654 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9655 *p_go_on = OPJ_FALSE;
9661 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9662 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9666 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9667 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9669 *p_tile_index = p_j2k->m_current_tile_number;
9670 *p_go_on = OPJ_TRUE;
9671 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9672 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9673 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9674 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9675 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9676 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9678 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9684 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9685 OPJ_UINT32 p_tile_index,
9687 OPJ_UINT32 p_data_size,
9688 opj_stream_private_t *p_stream,
9689 opj_event_mgr_t * p_manager )
9691 OPJ_UINT32 l_current_marker;
9692 OPJ_BYTE l_data [2];
9693 opj_tcp_v2_t * l_tcp;
9696 assert(p_stream != 00);
9697 assert(p_j2k != 00);
9698 assert(p_manager != 00);
9700 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9701 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9705 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9706 if (! l_tcp->m_data) {
9707 j2k_tcp_destroy(l_tcp);
9711 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9715 p_j2k->cstr_index) ) {
9716 j2k_tcp_destroy(l_tcp);
9717 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9721 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9725 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9726 * we destroy just the data which will be re-read in read_tile_header*/
9727 /*j2k_tcp_destroy(l_tcp);
9728 p_j2k->m_tcd->tcp = 0;*/
9729 j2k_tcp_data_destroy(l_tcp);
9731 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9732 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9734 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9735 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9736 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9740 opj_read_bytes(l_data,&l_current_marker,2);
9742 if (l_current_marker == J2K_MS_EOC) {
9743 p_j2k->m_current_tile_number = 0;
9744 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9746 else if (l_current_marker != J2K_MS_SOT)
9748 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9757 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9759 OPJ_UINT32 i,j,k = 0;
9760 OPJ_UINT32 l_width_src,l_height_src;
9761 OPJ_UINT32 l_width_dest,l_height_dest;
9762 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9763 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9764 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9765 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9766 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9768 opj_image_comp_t * l_img_comp_src = 00;
9769 opj_image_comp_t * l_img_comp_dest = 00;
9771 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9772 opj_image_t * l_image_src = 00;
9773 OPJ_UINT32 l_size_comp, l_remaining;
9774 OPJ_INT32 * l_dest_ptr;
9775 opj_tcd_resolution_v2_t* l_res= 00;
9777 l_tilec = p_tcd->tcd_image->tiles->comps;
9778 l_image_src = p_tcd->image;
9779 l_img_comp_src = l_image_src->comps;
9781 l_img_comp_dest = p_output_image->comps;
9783 for (i=0; i<l_image_src->numcomps; i++) {
9785 /* Allocate output component buffer if necessary */
9786 if (!l_img_comp_dest->data) {
9788 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9789 if (! l_img_comp_dest->data) {
9794 /* Copy info from decoded comp image to output image */
9795 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9798 /* Compute the precision of the output buffer */
9799 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9800 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9801 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9807 if (l_size_comp == 3) {
9812 /* Current tile component size*/
9814 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9815 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9818 l_width_src = (l_res->x1 - l_res->x0);
9819 l_height_src = (l_res->y1 - l_res->y0);
9821 /* Border of the current output component*/
9822 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9823 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9824 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9825 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9828 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9829 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9833 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9834 * of the input buffer (decoded tile component) which will be move
9835 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9836 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9837 * by this input area.
9839 assert( l_res->x0 >= 0);
9840 assert( l_res->x1 >= 0);
9841 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9842 l_start_x_dest = l_res->x0 - l_x0_dest;
9843 l_offset_x0_src = 0;
9845 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9846 l_width_dest = l_width_src;
9847 l_offset_x1_src = 0;
9850 l_width_dest = l_x1_dest - l_res->x0 ;
9851 l_offset_x1_src = l_width_src - l_width_dest;
9855 l_start_x_dest = 0 ;
9856 l_offset_x0_src = l_x0_dest - l_res->x0;
9858 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9859 l_width_dest = l_width_src - l_offset_x0_src;
9860 l_offset_x1_src = 0;
9863 l_width_dest = l_img_comp_dest->w ;
9864 l_offset_x1_src = l_res->x1 - l_x1_dest;
9868 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9869 l_start_y_dest = l_res->y0 - l_y0_dest;
9870 l_offset_y0_src = 0;
9872 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9873 l_height_dest = l_height_src;
9874 l_offset_y1_src = 0;
9877 l_height_dest = l_y1_dest - l_res->y0 ;
9878 l_offset_y1_src = l_height_src - l_height_dest;
9882 l_start_y_dest = 0 ;
9883 l_offset_y0_src = l_y0_dest - l_res->y0;
9885 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9886 l_height_dest = l_height_src - l_offset_y0_src;
9887 l_offset_y1_src = 0;
9890 l_height_dest = l_img_comp_dest->h ;
9891 l_offset_y1_src = l_res->y1 - l_y1_dest;
9895 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9900 /* Compute the input buffer offset */
9901 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9902 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9903 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9905 /* Compute the output buffer offset */
9906 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9907 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9909 /* Move the output buffer to the first place where we will write*/
9910 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9913 fprintf(stdout, "COMPO[%d]:\n",i);
9914 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9915 "\t tile offset:%d, %d, %d, %d\n"
9916 "\t buffer offset: %d; %d, %d\n",
9917 l_res->x0, l_res->y0, l_width_src, l_height_src,
9918 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9919 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9921 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9922 "\t start offset: %d, line offset= %d\n",
9923 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9927 switch (l_size_comp) {
9930 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9931 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9933 if (l_img_comp_src->sgnd) {
9934 for (j = 0 ; j < l_height_dest ; ++j) {
9935 for ( k = 0 ; k < l_width_dest ; ++k) {
9936 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9939 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9940 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9944 for ( j = 0 ; j < l_height_dest ; ++j ) {
9945 for ( k = 0 ; k < l_width_dest ; ++k) {
9946 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9949 l_dest_ptr+= l_line_offset_dest;
9950 l_src_ptr += l_line_offset_src;
9954 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9955 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9960 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9961 l_src_ptr += l_start_offset_src;
9963 if (l_img_comp_src->sgnd) {
9964 for (j=0;j<l_height_dest;++j) {
9965 for (k=0;k<l_width_dest;++k) {
9966 *(l_dest_ptr++) = *(l_src_ptr++);
9969 l_dest_ptr+= l_line_offset_dest;
9970 l_src_ptr += l_line_offset_src ;
9974 for (j=0;j<l_height_dest;++j) {
9975 for (k=0;k<l_width_dest;++k) {
9976 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9979 l_dest_ptr+= l_line_offset_dest;
9980 l_src_ptr += l_line_offset_src ;
9984 l_src_ptr += l_end_offset_src;
9985 p_data = (OPJ_BYTE*) l_src_ptr;
9990 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9991 l_src_ptr += l_start_offset_src;
9993 for (j=0;j<l_height_dest;++j) {
9994 for (k=0;k<l_width_dest;++k) {
9995 *(l_dest_ptr++) = (*(l_src_ptr++));
9998 l_dest_ptr+= l_line_offset_dest;
9999 l_src_ptr += l_line_offset_src ;
10002 l_src_ptr += l_end_offset_src;
10003 p_data = (OPJ_BYTE*) l_src_ptr;
10017 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
10019 * @param p_j2k the jpeg2000 codec.
10020 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
10021 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
10022 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
10023 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
10024 * @param p_manager the user event manager
10026 * @return true if the area could be set.
10028 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
10029 opj_image_t* p_image,
10030 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
10031 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
10032 struct opj_event_mgr * p_manager )
10034 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
10035 opj_image_t * l_image = p_j2k->m_private_image;
10037 OPJ_UINT32 it_comp;
10038 OPJ_INT32 l_comp_x1, l_comp_y1;
10039 opj_image_comp_t* l_img_comp = NULL;
10041 /* Check if we are read the main header */
10042 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
10043 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
10047 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
10048 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
10050 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10051 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10052 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10053 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10059 /* Check if the positions provided by the user are correct */
10062 assert(p_start_x >= 0 );
10063 assert(p_start_y >= 0 );
10065 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
10066 opj_event_msg_v2(p_manager, EVT_ERROR,
10067 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
10068 p_start_x, l_image->x1);
10071 else if ((OPJ_UINT32)p_start_x < l_image->x0){
10072 opj_event_msg_v2(p_manager, EVT_WARNING,
10073 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
10074 p_start_x, l_image->x0);
10075 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10076 p_image->x0 = l_image->x0;
10079 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
10080 p_image->x0 = p_start_x;
10084 if ((OPJ_UINT32)p_start_y > l_image->y1){
10085 opj_event_msg_v2(p_manager, EVT_ERROR,
10086 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
10087 p_start_y, l_image->y1);
10090 else if ((OPJ_UINT32)p_start_y < l_image->y0){
10091 opj_event_msg_v2(p_manager, EVT_WARNING,
10092 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
10093 p_start_y, l_image->y0);
10094 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10095 p_image->y0 = l_image->y0;
10098 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
10099 p_image->y0 = p_start_y;
10103 assert((OPJ_UINT32)p_end_x > 0);
10104 assert((OPJ_UINT32)p_end_y > 0);
10105 if ((OPJ_UINT32)p_end_x < l_image->x0) {
10106 opj_event_msg_v2(p_manager, EVT_ERROR,
10107 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
10108 p_end_x, l_image->x0);
10111 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
10112 opj_event_msg_v2(p_manager, EVT_WARNING,
10113 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
10114 p_end_x, l_image->x1);
10115 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10116 p_image->x1 = l_image->x1;
10119 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
10120 p_image->x1 = p_end_x;
10124 if ((OPJ_UINT32)p_end_y < l_image->y0) {
10125 opj_event_msg_v2(p_manager, EVT_ERROR,
10126 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
10127 p_end_y, l_image->y0);
10130 if ((OPJ_UINT32)p_end_y > l_image->y1){
10131 opj_event_msg_v2(p_manager, EVT_WARNING,
10132 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
10133 p_end_y, l_image->y1);
10134 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10135 p_image->y1 = l_image->y1;
10138 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
10139 p_image->y1 = p_end_y;
10143 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
10145 l_img_comp = p_image->comps;
10146 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
10150 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10151 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10152 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10153 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10155 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
10156 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10158 opj_event_msg_v2(p_manager, EVT_ERROR,
10159 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
10163 l_img_comp->w = l_w;
10165 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
10166 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10168 opj_event_msg_v2(p_manager, EVT_ERROR,
10169 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
10173 l_img_comp->h = l_h;
10178 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
10179 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
10186 /* ----------------------------------------------------------------------- */
10187 /* J2K / JPT decoder interface */
10188 /* ----------------------------------------------------------------------- */
10190 * Creates a J2K decompression structure.
10192 * @return a handle to a J2K decompressor if successful, NULL otherwise.
10194 opj_j2k_v2_t* opj_j2k_create_decompress(void)
10196 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
10200 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
10202 l_j2k->m_is_decoder = 1;
10203 l_j2k->m_cp.m_is_decoder = 1;
10205 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
10206 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
10207 j2k_destroy(l_j2k);
10210 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
10212 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
10213 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
10214 j2k_destroy(l_j2k);
10218 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
10220 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
10222 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
10224 /* codestream index creation */
10225 l_j2k->cstr_index = j2k_create_cstr_index();
10227 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
10228 if (!l_j2k->cstr_index){
10229 j2k_destroy(l_j2k);
10233 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
10236 /* validation list creation */
10237 l_j2k->m_validation_list = opj_procedure_list_create();
10238 if (! l_j2k->m_validation_list) {
10239 j2k_destroy(l_j2k);
10243 /* execution list creation */
10244 l_j2k->m_procedure_list = opj_procedure_list_create();
10245 if (! l_j2k->m_procedure_list) {
10246 j2k_destroy(l_j2k);
10254 opj_codestream_index_t* j2k_create_cstr_index(void)
10256 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
10257 opj_calloc(1,sizeof(opj_codestream_index_t));
10261 cstr_index->maxmarknum = 100;
10262 cstr_index->marknum = 0;
10263 cstr_index->marker = (opj_marker_info_t*)
10264 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
10265 if (!cstr_index-> marker)
10268 cstr_index->tile_index = NULL;
10275 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
10277 * @param p_tile_no the tile index.
10278 * @param p_comp_no the component being outputted.
10279 * @param p_j2k the J2K codec.
10281 * @return the number of bytes taken by the SPCod element.
10283 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
10284 OPJ_UINT32 p_tile_no,
10285 OPJ_UINT32 p_comp_no )
10287 opj_cp_v2_t *l_cp = 00;
10288 opj_tcp_v2_t *l_tcp = 00;
10289 opj_tccp_t *l_tccp = 00;
10291 /* preconditions */
10292 assert(p_j2k != 00);
10294 l_cp = &(p_j2k->m_cp);
10295 l_tcp = &l_cp->tcps[p_tile_no];
10296 l_tccp = &l_tcp->tccps[p_comp_no];
10298 /* preconditions again */
10299 assert(p_tile_no < (l_cp->tw * l_cp->th));
10300 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10302 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10303 return 5 + l_tccp->numresolutions;
10311 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10313 * @param p_comp_no the component number to output.
10314 * @param p_stream the stream to write data to.
10315 * @param p_j2k J2K codec.
10316 * @param p_manager the user event manager.
10319 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10320 OPJ_UINT32 p_tile_no,
10321 OPJ_UINT32 p_comp_no,
10323 OPJ_UINT32 * p_header_size,
10324 struct opj_event_mgr * p_manager )
10327 opj_cp_v2_t *l_cp = 00;
10328 opj_tcp_v2_t *l_tcp = 00;
10329 opj_tccp_t *l_tccp = 00;
10331 /* preconditions */
10332 assert(p_j2k != 00);
10333 assert(p_header_size != 00);
10334 assert(p_manager != 00);
10335 assert(p_data != 00);
10337 l_cp = &(p_j2k->m_cp);
10338 l_tcp = &l_cp->tcps[p_tile_no];
10339 l_tccp = &l_tcp->tccps[p_comp_no];
10341 /* preconditions again */
10342 assert(p_tile_no < (l_cp->tw * l_cp->th));
10343 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10345 if (*p_header_size < 5) {
10346 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10350 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
10353 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
10356 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
10359 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
10362 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
10365 *p_header_size = *p_header_size - 5;
10367 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10369 if (*p_header_size < l_tccp->numresolutions) {
10370 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10374 for (i = 0; i < l_tccp->numresolutions; ++i) {
10375 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
10379 *p_header_size = *p_header_size - l_tccp->numresolutions;
10386 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10387 * @param p_header_data the data contained in the COM box.
10388 * @param p_j2k the jpeg2000 codec.
10389 * @param p_header_size the size of the data contained in the COM marker.
10390 * @param p_manager the user event manager.
10392 opj_bool j2k_read_SPCod_SPCoc(
10393 opj_j2k_v2_t *p_j2k,
10395 OPJ_BYTE * p_header_data,
10396 OPJ_UINT32 * p_header_size,
10397 struct opj_event_mgr * p_manager
10400 OPJ_UINT32 i, l_tmp;
10401 opj_cp_v2_t *l_cp = NULL;
10402 opj_tcp_v2_t *l_tcp = NULL;
10403 opj_tccp_t *l_tccp = NULL;
10404 OPJ_BYTE * l_current_ptr = NULL;
10406 /* preconditions */
10407 assert(p_j2k != 00);
10408 assert(p_manager != 00);
10409 assert(p_header_data != 00);
10411 l_cp = &(p_j2k->m_cp);
10412 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10413 &l_cp->tcps[p_j2k->m_current_tile_number] :
10414 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10416 /* precondition again */
10417 assert(compno < p_j2k->m_private_image->numcomps);
10419 l_tccp = &l_tcp->tccps[compno];
10420 l_current_ptr = p_header_data;
10422 /* make sure room is sufficient */
10423 if (*p_header_size < 5) {
10424 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10428 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10429 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10432 /* If user wants to remove more resolutions than the codestream contains, return error */
10433 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10434 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10435 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10436 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10440 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10442 l_tccp->cblkw += 2;
10444 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10446 l_tccp->cblkh += 2;
10448 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10451 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10454 *p_header_size = *p_header_size - 5;
10456 /* use custom precinct size ? */
10457 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10458 if (*p_header_size < l_tccp->numresolutions) {
10459 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10463 for (i = 0; i < l_tccp->numresolutions; ++i) {
10464 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10466 l_tccp->prcw[i] = l_tmp & 0xf;
10467 l_tccp->prch[i] = l_tmp >> 4;
10470 *p_header_size = *p_header_size - l_tccp->numresolutions;
10473 /* set default size for the precinct width and height */
10474 for (i = 0; i < l_tccp->numresolutions; ++i) {
10475 l_tccp->prcw[i] = 15;
10476 l_tccp->prch[i] = 15;
10480 #ifdef WIP_REMOVE_MSD
10482 if (p_j2k->cstr_info && compno == 0) {
10483 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10485 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10486 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10487 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10488 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10489 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10492 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10493 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10502 * Copies the tile component parameters of all the component from the first tile component.
10504 * @param p_j2k the J2k codec.
10506 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10510 opj_cp_v2_t *l_cp = NULL;
10511 opj_tcp_v2_t *l_tcp = NULL;
10512 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10513 OPJ_UINT32 l_prc_size;
10515 /* preconditions */
10516 assert(p_j2k != 00);
10518 l_cp = &(p_j2k->m_cp);
10519 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10520 &l_cp->tcps[p_j2k->m_current_tile_number] :
10521 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10523 l_ref_tccp = &l_tcp->tccps[0];
10524 l_copied_tccp = l_ref_tccp + 1;
10525 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10527 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10528 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10529 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10530 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10531 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10532 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10533 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10534 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10540 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10542 * @param p_tile_no the tile index.
10543 * @param p_comp_no the component being outputted.
10544 * @param p_j2k the J2K codec.
10546 * @return the number of bytes taken by the SPCod element.
10548 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10549 OPJ_UINT32 p_tile_no,
10550 OPJ_UINT32 p_comp_no )
10552 OPJ_UINT32 l_num_bands;
10554 opj_cp_v2_t *l_cp = 00;
10555 opj_tcp_v2_t *l_tcp = 00;
10556 opj_tccp_t *l_tccp = 00;
10558 /* preconditions */
10559 assert(p_j2k != 00);
10561 l_cp = &(p_j2k->m_cp);
10562 l_tcp = &l_cp->tcps[p_tile_no];
10563 l_tccp = &l_tcp->tccps[p_comp_no];
10565 /* preconditions again */
10566 assert(p_tile_no < l_cp->tw * l_cp->th);
10567 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10569 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10571 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10572 return 1 + l_num_bands;
10575 return 1 + 2*l_num_bands;
10580 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10582 * @param p_tile_no the tile to output.
10583 * @param p_comp_no the component number to output.
10584 * @param p_data the data buffer.
10585 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10586 * @param p_j2k J2K codec.
10587 * @param p_manager the user event manager.
10590 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10591 OPJ_UINT32 p_tile_no,
10592 OPJ_UINT32 p_comp_no,
10594 OPJ_UINT32 * p_header_size,
10595 struct opj_event_mgr * p_manager )
10597 OPJ_UINT32 l_header_size;
10598 OPJ_UINT32 l_band_no, l_num_bands;
10599 OPJ_UINT32 l_expn,l_mant;
10601 opj_cp_v2_t *l_cp = 00;
10602 opj_tcp_v2_t *l_tcp = 00;
10603 opj_tccp_t *l_tccp = 00;
10605 /* preconditions */
10606 assert(p_j2k != 00);
10607 assert(p_header_size != 00);
10608 assert(p_manager != 00);
10609 assert(p_data != 00);
10611 l_cp = &(p_j2k->m_cp);
10612 l_tcp = &l_cp->tcps[p_tile_no];
10613 l_tccp = &l_tcp->tccps[p_comp_no];
10615 /* preconditions again */
10616 assert(p_tile_no < l_cp->tw * l_cp->th);
10617 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10619 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10621 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10622 l_header_size = 1 + l_num_bands;
10624 if (*p_header_size < l_header_size) {
10625 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10629 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10632 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10633 l_expn = l_tccp->stepsizes[l_band_no].expn;
10634 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10639 l_header_size = 1 + 2*l_num_bands;
10641 if (*p_header_size < l_header_size) {
10642 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10646 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10649 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10650 l_expn = l_tccp->stepsizes[l_band_no].expn;
10651 l_mant = l_tccp->stepsizes[l_band_no].mant;
10653 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10658 *p_header_size = *p_header_size - l_header_size;
10664 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10666 * @param p_comp_no the component being targeted.
10667 * @param p_header_data the data contained in the COM box.
10668 * @param p_j2k the jpeg2000 codec.
10669 * @param p_header_size the size of the data contained in the COM marker.
10670 * @param p_manager the user event manager.
10672 opj_bool j2k_read_SQcd_SQcc(
10673 opj_j2k_v2_t *p_j2k,
10674 OPJ_UINT32 p_comp_no,
10675 OPJ_BYTE* p_header_data,
10676 OPJ_UINT32 * p_header_size,
10677 struct opj_event_mgr * p_manager
10681 OPJ_UINT32 l_band_no;
10682 opj_cp_v2_t *l_cp = 00;
10683 opj_tcp_v2_t *l_tcp = 00;
10684 opj_tccp_t *l_tccp = 00;
10685 OPJ_BYTE * l_current_ptr = 00;
10686 OPJ_UINT32 l_tmp, l_num_band;
10689 assert(p_j2k != 00);
10690 assert(p_manager != 00);
10691 assert(p_header_data != 00);
10693 l_cp = &(p_j2k->m_cp);
10694 /* come from tile part header or main header ?*/
10695 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10696 &l_cp->tcps[p_j2k->m_current_tile_number] :
10697 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10699 /* precondition again*/
10700 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10702 l_tccp = &l_tcp->tccps[p_comp_no];
10703 l_current_ptr = p_header_data;
10705 if (*p_header_size < 1) {
10706 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10709 *p_header_size -= 1;
10711 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10714 l_tccp->qntsty = l_tmp & 0x1f;
10715 l_tccp->numgbits = l_tmp >> 5;
10716 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10720 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10722 (*p_header_size) / 2;
10724 if( l_num_band > J2K_MAXBANDS ) {
10725 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10726 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10727 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10728 /*return OPJ_FALSE;*/
10733 if (l_cp->correct) {
10735 /* if JPWL is on, we check whether there are too many subbands */
10736 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10737 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10738 "JPWL: bad number of subbands in Sqcx (%d)\n",
10740 if (!JPWL_ASSUME) {
10741 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10744 /* we try to correct */
10746 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10747 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10752 #endif /* USE_JPWL */
10754 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10755 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10756 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10758 if (l_band_no < J2K_MAXBANDS){
10759 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10760 l_tccp->stepsizes[l_band_no].mant = 0;
10763 *p_header_size = *p_header_size - l_num_band;
10766 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10767 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10769 if (l_band_no < J2K_MAXBANDS){
10770 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10771 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10774 *p_header_size = *p_header_size - 2*l_num_band;
10777 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10778 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10779 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10780 l_tccp->stepsizes[l_band_no].expn =
10781 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10782 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10783 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10791 * Copies the tile component parameters of all the component from the first tile component.
10793 * @param p_j2k the J2k codec.
10795 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10798 opj_cp_v2_t *l_cp = NULL;
10799 opj_tcp_v2_t *l_tcp = NULL;
10800 opj_tccp_t *l_ref_tccp = NULL;
10801 opj_tccp_t *l_copied_tccp = NULL;
10804 /* preconditions */
10805 assert(p_j2k != 00);
10807 l_cp = &(p_j2k->m_cp);
10808 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10809 &l_cp->tcps[p_j2k->m_current_tile_number] :
10810 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10812 l_ref_tccp = &l_tcp->tccps[0];
10813 l_copied_tccp = l_ref_tccp + 1;
10814 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10816 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10817 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10818 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10819 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10825 * Dump some elements from the J2K decompression structure .
10827 *@param p_j2k the jpeg2000 codec.
10828 *@param flag flag to describe what elments are dump.
10829 *@param out_stream output stream where dump the elements.
10832 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10834 /* Check if the flag is compatible with j2k file*/
10835 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10836 fprintf(out_stream, "Wrong flag\n");
10840 /* Dump the image_header */
10841 if (flag & OPJ_IMG_INFO){
10842 if (p_j2k->m_private_image)
10843 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10846 /* Dump the codestream info from main header */
10847 if (flag & OPJ_J2K_MH_INFO){
10848 j2k_dump_MH_info(p_j2k, out_stream);
10852 /* Dump the codestream info of the current tile */
10853 if (flag & OPJ_J2K_TH_INFO){
10857 /* Dump the codestream index from main header */
10858 if (flag & OPJ_J2K_MH_IND){
10859 j2k_dump_MH_index(p_j2k, out_stream);
10862 /* Dump the codestream index of the current tile */
10863 if (flag & OPJ_J2K_TH_IND){
10870 * Dump index elements of the codestream extract from the main header.
10872 *@param p_j2k the jpeg2000 codec.
10873 *@param out_stream output stream where dump the elements.
10876 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10878 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10879 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10881 fprintf(out_stream, "Codestream index from main header: {\n");
10883 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10884 "\t Main header end position=%" PRIi64 "\n",
10885 cstr_index->main_head_start, cstr_index->main_head_end);
10887 fprintf(out_stream, "\t Marker list: {\n");
10889 if (cstr_index->marker){
10890 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10891 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10892 cstr_index->marker[it_marker].type,
10893 cstr_index->marker[it_marker].pos,
10894 cstr_index->marker[it_marker].len );
10898 fprintf(out_stream, "\t }\n");
10901 if (cstr_index->tile_index){
10903 /* Simple test to avoid to write empty information*/
10904 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10905 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10906 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10909 if (l_acc_nb_of_tile_part)
10911 fprintf(out_stream, "\t Tile index: {\n");
10913 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10914 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10916 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10918 if (cstr_index->tile_index[it_tile].tp_index){
10919 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10920 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10922 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10923 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10924 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10928 if (cstr_index->tile_index[it_tile].marker){
10929 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10930 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10931 cstr_index->tile_index[it_tile].marker[it_marker].type,
10932 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10933 cstr_index->tile_index[it_tile].marker[it_marker].len );
10937 fprintf(out_stream,"\t }\n");
10941 fprintf(out_stream,"}\n");
10946 * Dump info elements of the codestream extract from the main header.
10948 *@param p_j2k the jpeg2000 codec.
10949 *@param out_stream output stream where dump the elements.
10952 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10954 opj_tcp_v2_t * l_default_tile=NULL;
10956 fprintf(out_stream, "Codestream info from main header: {\n");
10958 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10959 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10960 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10962 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10963 if (l_default_tile)
10966 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10968 fprintf(out_stream, "\t default tile {\n");
10969 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10970 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10971 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10972 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10974 for (compno = 0; compno < numcomps; compno++) {
10975 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10977 OPJ_INT32 bandno, numbands;
10980 fprintf(out_stream, "\t\t comp %d {\n", compno);
10981 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10982 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10983 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10984 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10985 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10986 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10988 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10989 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10990 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10992 fprintf(out_stream, "\n");
10994 /* quantization style*/
10995 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10996 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10997 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10998 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10999 for (bandno = 0; bandno < numbands; bandno++) {
11000 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
11001 l_tccp->stepsizes[bandno].expn);
11003 fprintf(out_stream, "\n");
11006 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
11008 fprintf(out_stream, "\t\t }\n");
11009 } /*end of component of default tile*/
11010 fprintf(out_stream, "\t }\n"); /*end of default tile*/
11014 fprintf(out_stream, "}\n");
11019 * Dump an image header structure.
11021 *@param img_header the image header to dump.
11022 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11023 *@param out_stream output stream where dump the elements.
11025 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
11029 if (dev_dump_flag){
11030 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
11034 fprintf(out_stream, "Image info {\n");
11035 tab[0] = '\t';tab[1] = '\0';
11038 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
11039 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
11040 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
11042 if (img_header->comps){
11044 for (compno = 0; compno < img_header->numcomps; compno++) {
11045 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
11046 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
11047 fprintf(out_stream,"%s}\n",tab);
11051 fprintf(out_stream, "}\n");
11055 * Dump a component image header structure.
11057 *@param comp_header the component image header to dump.
11058 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11059 *@param out_stream output stream where dump the elements.
11061 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
11065 if (dev_dump_flag){
11066 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
11069 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
11072 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
11073 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
11074 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
11077 fprintf(out_stream, "}\n");
11082 * Get the codestream info from a JPEG2000 codec.
11084 *@param p_j2k the component image header to dump.
11086 *@return the codestream information extract from the jpg2000 codec
11088 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
11091 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
11092 opj_tcp_v2_t *l_default_tile;
11093 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
11095 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
11097 cstr_info->tx0 = p_j2k->m_cp.tx0;
11098 cstr_info->ty0 = p_j2k->m_cp.ty0;
11099 cstr_info->tdx = p_j2k->m_cp.tdx;
11100 cstr_info->tdy = p_j2k->m_cp.tdy;
11101 cstr_info->tw = p_j2k->m_cp.tw;
11102 cstr_info->th = p_j2k->m_cp.th;
11104 cstr_info->tile_info = NULL; /* Not fill from the main header*/
11106 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11108 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
11109 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
11110 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
11111 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
11113 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
11115 for (compno = 0; compno < numcomps; compno++) {
11116 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11117 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
11118 OPJ_INT32 bandno, numbands;
11121 l_tccp_info->csty = l_tccp->csty;
11122 l_tccp_info->numresolutions = l_tccp->numresolutions;
11123 l_tccp_info->cblkw = l_tccp->cblkw;
11124 l_tccp_info->cblkh = l_tccp->cblkh;
11125 l_tccp_info->cblksty = l_tccp->cblksty;
11126 l_tccp_info->qmfbid = l_tccp->qmfbid;
11127 if (l_tccp->numresolutions < J2K_MAXRLVLS)
11129 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
11130 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
11133 /* quantization style*/
11134 l_tccp_info->qntsty = l_tccp->qntsty;
11135 l_tccp_info->numgbits = l_tccp->numgbits;
11137 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11138 if (numbands < J2K_MAXBANDS) {
11139 for (bandno = 0; bandno < numbands; bandno++) {
11140 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
11141 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
11146 l_tccp_info->roishift = l_tccp->roishift;
11154 * Get the codestream index from a JPEG2000 codec.
11156 *@param p_j2k the component image header to dump.
11158 *@return the codestream index extract from the jpg2000 codec
11160 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
11162 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
11163 opj_calloc(1,sizeof(opj_codestream_index_t));
11167 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
11168 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
11169 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
11171 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
11172 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
11173 if (!l_cstr_index->marker){
11174 opj_free( l_cstr_index);
11178 if (p_j2k->cstr_index->marker)
11179 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
11181 opj_free(l_cstr_index->marker);
11182 l_cstr_index->marker = NULL;
11185 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
11186 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
11187 if (!l_cstr_index->tile_index){
11188 opj_free( l_cstr_index->marker);
11189 opj_free( l_cstr_index);
11193 if (!p_j2k->cstr_index->tile_index){
11194 opj_free(l_cstr_index->tile_index);
11195 l_cstr_index->tile_index = NULL;
11198 OPJ_UINT32 it_tile = 0;
11199 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
11202 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
11204 l_cstr_index->tile_index[it_tile].marker =
11205 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
11207 if (!l_cstr_index->tile_index[it_tile].marker) {
11208 OPJ_UINT32 it_tile_free;
11210 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11211 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11214 opj_free( l_cstr_index->tile_index);
11215 opj_free( l_cstr_index->marker);
11216 opj_free( l_cstr_index);
11220 if (p_j2k->cstr_index->tile_index[it_tile].marker)
11221 memcpy( l_cstr_index->tile_index[it_tile].marker,
11222 p_j2k->cstr_index->tile_index[it_tile].marker,
11223 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
11225 opj_free(l_cstr_index->tile_index[it_tile].marker);
11226 l_cstr_index->tile_index[it_tile].marker = NULL;
11229 /* Tile part index*/
11230 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
11232 l_cstr_index->tile_index[it_tile].tp_index =
11233 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
11235 if(!l_cstr_index->tile_index[it_tile].tp_index){
11236 OPJ_UINT32 it_tile_free;
11238 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11239 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11240 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
11243 opj_free( l_cstr_index->tile_index);
11244 opj_free( l_cstr_index->marker);
11245 opj_free( l_cstr_index);
11249 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
11250 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
11251 p_j2k->cstr_index->tile_index[it_tile].tp_index,
11252 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
11255 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
11256 l_cstr_index->tile_index[it_tile].tp_index = NULL;
11259 /* Packet index (NOT USED)*/
11260 l_cstr_index->tile_index[it_tile].nb_packet = 0;
11261 l_cstr_index->tile_index[it_tile].packet_index = NULL;
11266 return l_cstr_index;
11269 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
11271 OPJ_UINT32 it_tile=0;
11273 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
11274 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
11275 if (!p_j2k->cstr_index->tile_index)
11278 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
11279 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
11280 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
11281 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
11282 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
11283 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11293 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
11294 opj_stream_private_t *p_stream,
11295 opj_event_mgr_t * p_manager)
11297 opj_bool l_go_on = OPJ_TRUE;
11298 OPJ_UINT32 l_current_tile_no;
11299 OPJ_UINT32 l_data_size,l_max_data_size;
11300 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11301 OPJ_UINT32 l_nb_comps;
11302 OPJ_BYTE * l_current_data;
11304 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11305 if (! l_current_data) {
11308 l_max_data_size = 1000;
11313 if (! j2k_read_tile_header( p_j2k,
11314 &l_current_tile_no,
11316 &l_tile_x0, &l_tile_y0,
11317 &l_tile_x1, &l_tile_y1,
11322 opj_free(l_current_data);
11330 if (l_data_size > l_max_data_size) {
11331 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11332 if (! l_current_data) {
11333 opj_free(l_current_data);
11337 l_max_data_size = l_data_size;
11340 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11341 opj_free(l_current_data);
11344 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);
11346 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11347 opj_free(l_current_data);
11350 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11354 opj_free(l_current_data);
11360 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11362 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11365 assert(p_j2k != 00);
11367 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11368 /* DEVELOPER CORNER, add your custom procedures */
11373 * Read and decode one tile.
11375 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
11376 opj_stream_private_t *p_stream,
11377 opj_event_mgr_t * p_manager)
11379 opj_bool l_go_on = OPJ_TRUE;
11380 OPJ_UINT32 l_current_tile_no;
11381 OPJ_UINT32 l_tile_no_to_dec;
11382 OPJ_UINT32 l_data_size,l_max_data_size;
11383 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11384 OPJ_UINT32 l_nb_comps;
11385 OPJ_BYTE * l_current_data;
11387 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11388 if (! l_current_data) {
11391 l_max_data_size = 1000;
11393 /*Allocate and initialize some elements of codestrem index if not already done*/
11394 if( !p_j2k->cstr_index->tile_index)
11396 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11397 opj_free(l_current_data);
11401 /* Move into the codestream to the first SOT used to decode the desired tile */
11402 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11403 if (p_j2k->cstr_index->tile_index)
11404 if(p_j2k->cstr_index->tile_index->tp_index)
11406 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11407 /* the index for this tile has not been built,
11408 * so move to the last SOT read */
11409 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11410 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11415 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)) {
11416 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11420 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11421 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11422 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11426 if (! j2k_read_tile_header( p_j2k,
11427 &l_current_tile_no,
11429 &l_tile_x0, &l_tile_y0,
11430 &l_tile_x1, &l_tile_y1,
11435 opj_free(l_current_data);
11444 if (l_data_size > l_max_data_size) {
11445 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11446 if (! l_current_data) {
11447 opj_free(l_current_data);
11451 l_max_data_size = l_data_size;
11456 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11457 opj_free(l_current_data);
11460 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);
11462 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11463 opj_free(l_current_data);
11466 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11468 if(l_current_tile_no == l_tile_no_to_dec)
11470 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11471 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11472 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11478 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);
11483 opj_free(l_current_data);
11490 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11492 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11495 assert(p_j2k != 00);
11497 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11498 /* DEVELOPER CORNER, add your custom procedures */
11504 * Decodes the tiles of the stream.
11506 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
11507 opj_stream_private_t * p_stream,
11508 opj_image_t * p_image,
11509 opj_event_mgr_t * p_manager)
11516 p_j2k->m_output_image = opj_image_create0();
11517 if (! (p_j2k->m_output_image)) {
11520 opj_copy_image_header(p_image, p_j2k->m_output_image);
11522 /* customization of the decoding */
11523 j2k_setup_decoding(p_j2k);
11525 /* Decode the codestream */
11526 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11527 opj_image_destroy(p_j2k->m_private_image);
11528 p_j2k->m_private_image = NULL;
11532 /* Move data and copy one information from codec to output image*/
11533 for (compno = 0; compno < p_image->numcomps; compno++) {
11534 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11535 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11536 p_j2k->m_output_image->comps[compno].data = NULL;
11544 * Get the decoded tile.
11546 * @param p_j2k the jpeg2000 codestream codec.
11547 * @param p_stream input_stream
11548 * @param p_image output image. .
11549 * @param p_manager the user event manager
11550 * @param tile_index index of the tile we want decode
11552 * @return true if succeed.
11554 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11555 opj_stream_private_t *p_stream,
11556 opj_image_t* p_image,
11557 struct opj_event_mgr * p_manager,
11558 OPJ_UINT32 tile_index )
11561 OPJ_UINT32 l_tile_x, l_tile_y;
11562 opj_image_comp_t* l_img_comp;
11565 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11569 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11570 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);
11574 /* Compute the dimension of the desired tile*/
11575 l_tile_x = tile_index % p_j2k->m_cp.tw;
11576 l_tile_y = tile_index / p_j2k->m_cp.tw;
11578 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11579 if (p_image->x0 < p_j2k->m_private_image->x0)
11580 p_image->x0 = p_j2k->m_private_image->x0;
11581 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11582 if (p_image->x1 > p_j2k->m_private_image->x1)
11583 p_image->x1 = p_j2k->m_private_image->x1;
11585 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11586 if (p_image->y0 < p_j2k->m_private_image->y0)
11587 p_image->y0 = p_j2k->m_private_image->y0;
11588 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11589 if (p_image->y1 > p_j2k->m_private_image->y1)
11590 p_image->y1 = p_j2k->m_private_image->y1;
11592 l_img_comp = p_image->comps;
11593 for (compno=0; compno < p_image->numcomps; ++compno)
11595 OPJ_INT32 l_comp_x1, l_comp_y1;
11597 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11599 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11600 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11601 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11602 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11604 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11605 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11610 /* Destroy the previous output image*/
11611 if (p_j2k->m_output_image)
11612 opj_image_destroy(p_j2k->m_output_image);
11614 /* Create the ouput image from the information previously computed*/
11615 p_j2k->m_output_image = opj_image_create0();
11616 if (! (p_j2k->m_output_image)) {
11619 opj_copy_image_header(p_image, p_j2k->m_output_image);
11621 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11623 /* customization of the decoding */
11624 j2k_setup_decoding_tile(p_j2k);
11626 /* Decode the codestream */
11627 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11628 opj_image_destroy(p_j2k->m_private_image);
11629 p_j2k->m_private_image = NULL;
11633 /* Move data and copy one information from codec to output image*/
11634 for (compno = 0; compno < p_image->numcomps; compno++) {
11635 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11637 if (p_image->comps[compno].data)
11638 opj_free(p_image->comps[compno].data);
11640 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11642 p_j2k->m_output_image->comps[compno].data = NULL;
11648 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11650 OPJ_UINT32 it_comp;
11652 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11654 if (p_j2k->m_private_image) {
11655 if (p_j2k->m_private_image->comps) {
11656 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11657 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11658 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11659 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11660 if ( res_factor >= max_res){
11661 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11664 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11677 * Encodes all the tiles in a row.
11679 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11680 opj_stream_private_t *p_stream,
11681 opj_event_mgr_t * p_manager )
11684 OPJ_UINT32 l_nb_tiles;
11685 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11686 OPJ_BYTE * l_current_data;
11688 /* preconditions */
11689 assert(p_j2k != 00);
11690 assert(p_stream != 00);
11691 assert(p_manager != 00);
11693 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11694 if (! l_current_data) {
11697 l_max_tile_size = 1000;
11699 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11700 for (i=0;i<l_nb_tiles;++i) {
11701 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11702 opj_free(l_current_data);
11706 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11707 if (l_current_tile_size > l_max_tile_size) {
11708 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11709 if (! l_current_data) {
11712 l_max_tile_size = l_current_tile_size;
11715 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11717 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11722 opj_free(l_current_data);
11727 * Ends the compression procedures and possibility add data to be read after the
11730 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11731 opj_stream_private_t *p_stream,
11732 struct opj_event_mgr * p_manager)
11734 /* customization of the encoding */
11735 j2k_setup_end_compress(p_j2k);
11737 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11747 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11749 * @param p_j2k the jpeg2000 codec.
11750 * @param p_stream the stream object.
11751 * @param p_manager the user event manager.
11753 * @return true if the codec is valid.
11755 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11756 opj_stream_private_t *p_stream,
11757 opj_image_t * p_image,
11758 opj_event_mgr_t * p_manager)
11760 /* preconditions */
11761 assert(p_j2k != 00);
11762 assert(p_stream != 00);
11763 assert(p_manager != 00);
11765 p_j2k->m_private_image = opj_image_create0();
11766 opj_copy_image_header(p_image, p_j2k->m_private_image);
11768 // TODO_MSD: Find a better way
11769 if (p_image->comps) {
11770 OPJ_UINT32 it_comp;
11771 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11772 if (p_image->comps[it_comp].data) {
11773 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11774 p_image->comps[it_comp].data = NULL;
11780 /* customization of the validation */
11781 j2k_setup_encoding_validation (p_j2k);
11783 /* validation of the parameters codec */
11784 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11788 /* customization of the encoding */
11789 j2k_setup_header_writting(p_j2k);
11792 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11802 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11803 OPJ_UINT32 p_tile_index,
11804 opj_stream_private_t *p_stream,
11805 opj_event_mgr_t * p_manager )
11808 if (p_tile_index != p_j2k->m_current_tile_number) {
11809 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11813 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);
11815 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11816 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11817 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11819 /* initialisation before tile encoding */
11820 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11830 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11832 OPJ_UINT32 i,j,k = 0;
11833 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11834 opj_image_comp_t * l_img_comp = 00;
11835 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11836 opj_image_t * l_image = 00;
11837 OPJ_UINT32 l_size_comp, l_remaining;
11838 OPJ_INT32 * l_src_ptr;
11839 l_tilec = p_tcd->tcd_image->tiles->comps;
11840 l_image = p_tcd->image;
11841 l_img_comp = l_image->comps;
11843 for (i=0;i<p_tcd->image->numcomps;++i) {
11844 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11845 l_remaining = l_img_comp->prec & 7; /* (%8) */
11850 if (l_size_comp == 3) {
11854 l_width = (l_tilec->x1 - l_tilec->x0);
11855 l_height = (l_tilec->y1 - l_tilec->y0);
11856 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11857 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11858 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11859 l_stride = l_image_width - l_width;
11860 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11862 switch (l_size_comp) {
11865 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11866 if (l_img_comp->sgnd) {
11867 for (j=0;j<l_height;++j) {
11868 for (k=0;k<l_width;++k) {
11869 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11873 l_src_ptr += l_stride;
11877 for (j=0;j<l_height;++j) {
11878 for (k=0;k<l_width;++k) {
11879 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11883 l_src_ptr += l_stride;
11887 p_data = (OPJ_BYTE*) l_dest_ptr;
11892 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11893 if (l_img_comp->sgnd) {
11894 for (j=0;j<l_height;++j) {
11895 for (k=0;k<l_width;++k) {
11896 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11898 l_src_ptr += l_stride;
11902 for (j=0;j<l_height;++j) {
11903 for (k=0;k<l_width;++k) {
11904 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11906 l_src_ptr += l_stride;
11910 p_data = (OPJ_BYTE*) l_dest_ptr;
11915 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11916 for (j=0;j<l_height;++j) {
11917 for (k=0;k<l_width;++k) {
11918 *(l_dest_ptr++) = *(l_src_ptr++);
11920 l_src_ptr += l_stride;
11923 p_data = (OPJ_BYTE*) l_dest_ptr;
11936 * @param p_j2k the jpeg2000 codec.
11937 * @param p_stream the stream to write data to.
11938 * @param p_manager the user event manager.
11940 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11942 OPJ_UINT32 p_data_size,
11943 opj_stream_private_t *p_stream,
11944 opj_event_mgr_t * p_manager )
11946 opj_tcd_v2_t * l_tcd = 00;
11947 opj_cp_v2_t * l_cp = 00;
11948 opj_tcp_v2_t * l_tcp = 00;
11949 OPJ_UINT32 l_nb_bytes_written;
11950 OPJ_BYTE * l_current_data = 00;
11951 OPJ_UINT32 l_tile_size = 0;
11952 OPJ_UINT32 l_available_data;
11954 /* preconditions */
11955 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11957 l_tcd = p_j2k->m_tcd;
11958 l_cp = &(p_j2k->m_cp);
11959 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11961 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11962 l_available_data = l_tile_size;
11963 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11965 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11966 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11970 l_nb_bytes_written = 0;
11971 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11974 l_current_data += l_nb_bytes_written;
11975 l_available_data -= l_nb_bytes_written;
11977 l_nb_bytes_written = 0;
11978 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11982 l_available_data -= l_nb_bytes_written;
11983 l_nb_bytes_written = l_tile_size - l_available_data;
11985 if ( opj_stream_write_data( p_stream,
11986 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11987 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11991 ++p_j2k->m_current_tile_number;
11998 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11999 * are valid. Developers wanting to extend the library can add their own validation procedures.
12001 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
12003 /* preconditions */
12004 assert(p_j2k != 00);
12006 /* DEVELOPER CORNER, insert your custom procedures */
12007 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
12009 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12010 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
12013 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
12014 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
12015 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
12019 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12020 * are valid. Developers wanting to extend the library can add their own validation procedures.
12022 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
12024 /* preconditions */
12025 assert(p_j2k != 00);
12027 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
12028 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
12030 /* DEVELOPER CORNER, add your custom validation procedure */
12031 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
12036 * Sets up the procedures to do on writing header.
12037 * Developers wanting to extend the library can add their own writing procedures.
12039 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
12041 /* preconditions */
12042 assert(p_j2k != 00);
12044 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
12045 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
12046 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
12047 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
12048 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
12051 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12052 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
12053 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
12055 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
12056 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
12060 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
12062 if (p_j2k->m_cp.comment != 00) {
12063 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
12066 /* DEVELOPER CORNER, insert your custom procedures */
12067 if (p_j2k->m_cp.rsiz & MCT) {
12068 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
12070 /* End of Developer Corner */
12072 if (p_j2k->cstr_index) {
12073 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
12076 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
12077 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
12081 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
12083 OPJ_UINT32 * p_data_written,
12084 OPJ_UINT32 p_total_data_size,
12085 opj_stream_private_t *p_stream,
12086 struct opj_event_mgr * p_manager )
12089 OPJ_UINT32 l_nb_bytes_written = 0;
12090 OPJ_UINT32 l_current_nb_bytes_written;
12091 OPJ_BYTE * l_begin_data = 00;
12093 opj_tcp_v2_t *l_tcp = 00;
12094 opj_tcd_v2_t * l_tcd = 00;
12095 opj_cp_v2_t * l_cp = 00;
12097 l_tcd = p_j2k->m_tcd;
12098 l_cp = &(p_j2k->m_cp);
12099 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12101 l_tcd->cur_pino = 0;
12103 /*Get number of tile parts*/
12104 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12109 l_current_nb_bytes_written = 0;
12110 l_begin_data = p_data;
12111 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
12116 l_nb_bytes_written += l_current_nb_bytes_written;
12117 p_data += l_current_nb_bytes_written;
12118 p_total_data_size -= l_current_nb_bytes_written;
12120 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
12121 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
12122 l_current_nb_bytes_written = 0;
12123 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12124 l_nb_bytes_written += l_current_nb_bytes_written;
12125 p_data += l_current_nb_bytes_written;
12126 p_total_data_size -= l_current_nb_bytes_written;
12128 l_current_nb_bytes_written = 0;
12129 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12130 l_nb_bytes_written += l_current_nb_bytes_written;
12131 p_data += l_current_nb_bytes_written;
12132 p_total_data_size -= l_current_nb_bytes_written;
12135 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
12136 l_current_nb_bytes_written = 0;
12137 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
12138 l_nb_bytes_written += l_current_nb_bytes_written;
12139 p_data += l_current_nb_bytes_written;
12140 p_total_data_size -= l_current_nb_bytes_written;
12144 l_current_nb_bytes_written = 0;
12145 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12149 l_nb_bytes_written += l_current_nb_bytes_written;
12150 * p_data_written = l_nb_bytes_written;
12152 /* Writing Psot in SOT marker */
12153 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
12155 if (l_cp->m_specific_param.m_enc.m_cinema){
12156 j2k_update_tlm(p_j2k,l_nb_bytes_written);
12162 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
12164 OPJ_UINT32 * p_data_written,
12165 OPJ_UINT32 p_total_data_size,
12166 opj_stream_private_t *p_stream,
12167 struct opj_event_mgr * p_manager
12170 OPJ_UINT32 tilepartno=0;
12171 OPJ_UINT32 l_nb_bytes_written = 0;
12172 OPJ_UINT32 l_current_nb_bytes_written;
12173 OPJ_UINT32 l_part_tile_size;
12174 OPJ_UINT32 tot_num_tp;
12177 OPJ_BYTE * l_begin_data;
12178 opj_tcp_v2_t *l_tcp = 00;
12179 opj_tcd_v2_t * l_tcd = 00;
12180 opj_cp_v2_t * l_cp = 00;
12183 l_tcd = p_j2k->m_tcd;
12184 l_cp = &(p_j2k->m_cp);
12185 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12187 /*Get number of tile parts*/
12188 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
12190 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
12191 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12192 l_current_nb_bytes_written = 0;
12193 l_part_tile_size = 0;
12194 l_begin_data = p_data;
12196 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12200 l_nb_bytes_written += l_current_nb_bytes_written;
12201 p_data += l_current_nb_bytes_written;
12202 p_total_data_size -= l_current_nb_bytes_written;
12203 l_part_tile_size += l_nb_bytes_written;
12205 l_current_nb_bytes_written = 0;
12206 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12210 p_data += l_current_nb_bytes_written;
12211 l_nb_bytes_written += l_current_nb_bytes_written;
12212 p_total_data_size -= l_current_nb_bytes_written;
12213 l_part_tile_size += l_nb_bytes_written;
12215 /* Writing Psot in SOT marker */
12216 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12218 if (l_cp->m_specific_param.m_enc.m_cinema) {
12219 j2k_update_tlm(p_j2k,l_part_tile_size);
12222 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12225 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
12226 l_tcd->cur_pino = pino;
12228 /*Get number of tile parts*/
12229 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
12230 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
12231 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12232 l_current_nb_bytes_written = 0;
12233 l_part_tile_size = 0;
12234 l_begin_data = p_data;
12236 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12240 l_nb_bytes_written += l_current_nb_bytes_written;
12241 p_data += l_current_nb_bytes_written;
12242 p_total_data_size -= l_current_nb_bytes_written;
12243 l_part_tile_size += l_current_nb_bytes_written;
12245 l_current_nb_bytes_written = 0;
12247 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12251 l_nb_bytes_written += l_current_nb_bytes_written;
12252 p_data += l_current_nb_bytes_written;
12253 p_total_data_size -= l_current_nb_bytes_written;
12254 l_part_tile_size += l_current_nb_bytes_written;
12256 /* Writing Psot in SOT marker */
12257 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12259 if (l_cp->m_specific_param.m_enc.m_cinema) {
12260 j2k_update_tlm(p_j2k,l_part_tile_size);
12263 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12267 *p_data_written = l_nb_bytes_written;
12273 * Writes the updated tlm.
12275 * @param p_stream the stream to write data to.
12276 * @param p_j2k J2K codec.
12277 * @param p_manager the user event manager.
12279 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
12280 struct opj_stream_private *p_stream,
12281 struct opj_event_mgr * p_manager )
12283 OPJ_UINT32 l_tlm_size;
12284 OPJ_SIZE_T l_tlm_position, l_current_position;
12286 /* preconditions */
12287 assert(p_j2k != 00);
12288 assert(p_manager != 00);
12289 assert(p_stream != 00);
12291 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12292 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12293 l_current_position = opj_stream_tell(p_stream);
12295 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12299 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) {
12303 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12312 * Ends the encoding, i.e. frees memory.
12314 * @param p_stream the stream to write data to.
12315 * @param p_j2k J2K codec.
12316 * @param p_manager the user event manager.
12318 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
12319 struct opj_stream_private *p_stream,
12320 struct opj_event_mgr * p_manager )
12322 /* preconditions */
12323 assert(p_j2k != 00);
12324 assert(p_manager != 00);
12325 assert(p_stream != 00);
12327 tcd_destroy_v2(p_j2k->m_tcd);
12330 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12331 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12332 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12333 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12336 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12337 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12338 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12341 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12347 * Destroys the memory associated with the decoding of headers.
12349 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12350 opj_stream_private_t *p_stream,
12351 opj_event_mgr_t * p_manager )
12353 /* preconditions */
12354 assert(p_j2k != 00);
12355 assert(p_stream != 00);
12356 assert(p_manager != 00);
12358 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12359 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12360 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12363 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12372 * @param p_stream the stream to write data to.
12373 * @param p_j2k J2K codec.
12374 * @param p_manager the user event manager.
12376 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12377 struct opj_stream_private *p_stream,
12378 struct opj_event_mgr * p_manager )
12380 opj_codestream_info_t * l_cstr_info = 00;
12382 /* preconditions */
12383 assert(p_j2k != 00);
12384 assert(p_manager != 00);
12385 assert(p_stream != 00);
12388 /* TODO mergeV2: check this part which use cstr_info */
12389 /*l_cstr_info = p_j2k->cstr_info;
12393 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));
12395 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12396 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12398 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12400 l_cstr_info->tw = p_j2k->m_cp.tw;
12401 l_cstr_info->th = p_j2k->m_cp.th;
12403 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12404 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12405 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12406 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12408 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12410 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12412 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12414 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12415 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12418 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12420 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12422 /*l_cstr_info->maxmarknum = 100;
12423 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12424 l_cstr_info->marknum = 0;
12427 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);
12431 * Creates a tile-coder decoder.
12433 * @param p_stream the stream to write data to.
12434 * @param p_j2k J2K codec.
12435 * @param p_manager the user event manager.
12437 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12438 struct opj_stream_private *p_stream,
12439 struct opj_event_mgr * p_manager )
12441 /* preconditions */
12442 assert(p_j2k != 00);
12443 assert(p_manager != 00);
12444 assert(p_stream != 00);
12446 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12448 if (! p_j2k->m_tcd) {
12449 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12453 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12454 tcd_destroy_v2(p_j2k->m_tcd);
12465 * @param p_j2k the jpeg2000 codec.
12466 * @param p_stream the stream to write data to.
12467 * @param p_manager the user event manager.
12469 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12470 OPJ_UINT32 p_tile_index,
12472 OPJ_UINT32 p_data_size,
12473 opj_stream_private_t *p_stream,
12474 opj_event_mgr_t * p_manager )
12476 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12477 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12481 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12482 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);