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, 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 opj_bool j2k_read_header_procedure(
54 struct opj_stream_private *p_stream,
55 struct opj_event_mgr * p_manager);
58 * The default decoding validation procedure without any extension.
60 * @param p_j2k the jpeg2000 codec to validate.
61 * @param p_stream the input stream to validate.
62 * @param p_manager the user event manager.
64 * @return true if the parameters are correct.
66 opj_bool j2k_decoding_validation (
68 opj_stream_private_t *p_stream,
69 opj_event_mgr_t * p_manager
73 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
74 * are valid. Developpers wanting to extend the library can add their own validation procedures.
76 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
79 * Builds the tcd decoder to use to decode tile.
81 opj_bool j2k_build_decoder (
83 opj_stream_private_t *p_stream,
84 opj_event_mgr_t * p_manager
88 * Excutes the given procedures on the given codec.
90 * @param p_procedure_list the list of procedures to execute
91 * @param p_j2k the jpeg2000 codec to execute the procedures on.
92 * @param p_stream the stream to execute the procedures on.
93 * @param p_manager the user manager.
95 * @return true if all the procedures were successfully executed.
97 static opj_bool j2k_exec (
99 opj_procedure_list_t * p_procedure_list,
100 opj_stream_private_t *p_stream,
101 opj_event_mgr_t * p_manager
105 * Copies the decoding tile parameters onto all the tile parameters.
106 * Creates also the tile decoder.
108 opj_bool j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
109 opj_stream_private_t *p_stream,
110 opj_event_mgr_t * p_manager );
113 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
114 * with the marker value.
115 * @param p_id Marker value to look up
117 * @return the handler associated with the id.
119 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
122 * Destroys a tile coding parameter structure.
124 * @param p_tcp the tile coding parameter to destroy.
126 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
129 * Destroys the data inside a tile coding parameter structure.
131 * @param p_tcp the tile coding parameter which contain data to destroy.
133 static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
136 * Destroys a coding parameter structure.
138 * @param p_cp the coding parameter to destroy.
140 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
144 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
145 * @param p_header_data the data contained in the COM box.
146 * @param p_j2k the jpeg2000 codec.
147 * @param p_header_size the size of the data contained in the COM marker.
148 * @param p_manager the user event manager.
150 static opj_bool j2k_read_SPCod_SPCoc(
153 OPJ_BYTE * p_header_data,
154 OPJ_UINT32 * p_header_size,
155 struct opj_event_mgr * p_manager
159 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
161 * @param p_tile_no the tile to output.
162 * @param p_comp_no the component number to output.
163 * @param p_data the data buffer.
164 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
165 * @param p_j2k J2K codec.
166 * @param p_manager the user event manager.
169 static opj_bool j2k_read_SQcd_SQcc(
172 OPJ_BYTE * p_header_data,
173 OPJ_UINT32 * p_header_size,
174 struct opj_event_mgr * p_manager
178 * Copies the tile component parameters of all the component from the first tile component.
180 * @param p_j2k the J2k codec.
182 static void j2k_copy_tile_component_parameters(
187 * Copies the tile quantization parameters of all the component from the first tile component.
189 * @param p_j2k the J2k codec.
191 static void j2k_copy_tile_quantization_parameters(
198 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
199 opj_stream_private_t *p_stream,
200 opj_event_mgr_t * p_manager);
202 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
206 * -----------------------------------------------------------------------
207 * -----------------------------------------------------------------------
208 * -----------------------------------------------------------------------
212 Write the SOC marker (Start Of Codestream)
213 @param j2k J2K handle
215 static void j2k_write_soc(opj_j2k_t *j2k);
217 Read the SOC marker (Start of Codestream)
218 @param j2k J2K handle
220 static void j2k_read_soc(opj_j2k_t *j2k);
223 * Reads a SOC marker (Start of Codestream)
224 * @param p_header_data the data contained in the SOC box.
225 * @param jp2 the jpeg2000 file codec.
226 * @param p_header_size the size of the data contained in the SOC marker.
227 * @param p_manager the user event manager.
229 static opj_bool j2k_read_soc_v2(
231 struct opj_stream_private *p_stream,
232 struct opj_event_mgr * p_manager
236 Write the SIZ marker (image and tile size)
237 @param j2k J2K handle
239 static void j2k_write_siz(opj_j2k_t *j2k);
241 Read the SIZ marker (image and tile size)
242 @param j2k J2K handle
244 static void j2k_read_siz(opj_j2k_t *j2k);
247 * Reads a SIZ marker (image and tile size)
248 * @param p_header_data the data contained in the SIZ box.
249 * @param jp2 the jpeg2000 file codec.
250 * @param p_header_size the size of the data contained in the SIZ marker.
251 * @param p_manager the user event manager.
253 static opj_bool j2k_read_siz_v2 (
255 OPJ_BYTE * p_header_data,
256 OPJ_UINT32 p_header_size,
257 struct opj_event_mgr * p_manager
261 Write the COM marker (comment)
262 @param j2k J2K handle
264 static void j2k_write_com(opj_j2k_t *j2k);
266 Read the COM marker (comment)
267 @param j2k J2K handle
269 static void j2k_read_com(opj_j2k_t *j2k);
271 * Reads a COM marker (comments)
272 * @param p_header_data the data contained in the COM box.
273 * @param jp2 the jpeg2000 file codec.
274 * @param p_header_size the size of the data contained in the COM marker.
275 * @param p_manager the user event manager.
277 static opj_bool j2k_read_com_v2 (
279 OPJ_BYTE * p_header_data,
280 OPJ_UINT32 p_header_size,
281 struct opj_event_mgr * p_manager
284 Write the value concerning the specified component in the marker COD and COC
285 @param j2k J2K handle
286 @param compno Number of the component concerned by the information written
288 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
290 Read the value concerning the specified component in the marker COD and COC
291 @param j2k J2K handle
292 @param compno Number of the component concerned by the information read
294 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
296 Write the COD marker (coding style default)
297 @param j2k J2K handle
299 static void j2k_write_cod(opj_j2k_t *j2k);
301 Read the COD marker (coding style default)
302 @param j2k J2K handle
304 static void j2k_read_cod(opj_j2k_t *j2k);
307 * Reads a COD marker (Coding Styke defaults)
308 * @param p_header_data the data contained in the COD box.
309 * @param p_j2k the jpeg2000 codec.
310 * @param p_header_size the size of the data contained in the COD marker.
311 * @param p_manager the user event manager.
313 static opj_bool j2k_read_cod_v2 (
315 OPJ_BYTE * p_header_data,
316 OPJ_UINT32 p_header_size,
317 struct opj_event_mgr * p_manager
321 Write the COC marker (coding style component)
322 @param j2k J2K handle
323 @param compno Number of the component concerned by the information written
325 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
327 Read the COC marker (coding style component)
328 @param j2k J2K handle
330 static void j2k_read_coc(opj_j2k_t *j2k);
333 * Reads a COC marker (Coding Style Component)
334 * @param p_header_data the data contained in the COC box.
335 * @param p_j2k the jpeg2000 codec.
336 * @param p_header_size the size of the data contained in the COC marker.
337 * @param p_manager the user event manager.
339 static opj_bool j2k_read_coc_v2 (
341 OPJ_BYTE * p_header_data,
342 OPJ_UINT32 p_header_size,
343 struct opj_event_mgr * p_manager
347 Write the value concerning the specified component in the marker QCD and QCC
348 @param j2k J2K handle
349 @param compno Number of the component concerned by the information written
351 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
353 Read the value concerning the specified component in the marker QCD and QCC
354 @param j2k J2K handle
355 @param compno Number of the component concern by the information read
356 @param len Length of the information in the QCX part of the marker QCD/QCC
358 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
360 Write the QCD marker (quantization default)
361 @param j2k J2K handle
363 static void j2k_write_qcd(opj_j2k_t *j2k);
365 Read the QCD marker (quantization default)
366 @param j2k J2K handle
368 static void j2k_read_qcd(opj_j2k_t *j2k);
371 * Reads a QCD marker (Quantization defaults)
372 * @param p_header_data the data contained in the QCD box.
373 * @param p_j2k the jpeg2000 codec.
374 * @param p_header_size the size of the data contained in the QCD marker.
375 * @param p_manager the user event manager.
377 static opj_bool j2k_read_qcd_v2 (
379 OPJ_BYTE * p_header_data,
380 OPJ_UINT32 p_header_size,
381 struct opj_event_mgr * p_manager
385 Write the QCC marker (quantization component)
386 @param j2k J2K handle
387 @param compno Number of the component concerned by the information written
389 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
391 Read the QCC marker (quantization component)
392 @param j2k J2K handle
394 static void j2k_read_qcc(opj_j2k_t *j2k);
396 * Reads a QCC marker (Quantization component)
397 * @param p_header_data the data contained in the QCC box.
398 * @param p_j2k the jpeg2000 codec.
399 * @param p_header_size the size of the data contained in the QCC marker.
400 * @param p_manager the user event manager.
402 static opj_bool j2k_read_qcc_v2(
404 OPJ_BYTE * p_header_data,
405 OPJ_UINT32 p_header_size,
406 struct opj_event_mgr * p_manager);
409 Write the POC marker (progression order change)
410 @param j2k J2K handle
412 static void j2k_write_poc(opj_j2k_t *j2k);
414 Read the POC marker (progression order change)
415 @param j2k J2K handle
417 static void j2k_read_poc(opj_j2k_t *j2k);
419 * Reads a POC marker (Progression Order Change)
421 * @param p_header_data the data contained in the POC box.
422 * @param p_j2k the jpeg2000 codec.
423 * @param p_header_size the size of the data contained in the POC marker.
424 * @param p_manager the user event manager.
426 static opj_bool j2k_read_poc_v2 (
428 OPJ_BYTE * p_header_data,
429 OPJ_UINT32 p_header_size,
430 struct opj_event_mgr * p_manager
433 Read the CRG marker (component registration)
434 @param j2k J2K handle
436 static void j2k_read_crg(opj_j2k_t *j2k);
438 * Reads a CRG marker (Component registration)
440 * @param p_header_data the data contained in the TLM box.
441 * @param p_j2k the jpeg2000 codec.
442 * @param p_header_size the size of the data contained in the TLM marker.
443 * @param p_manager the user event manager.
445 static opj_bool j2k_read_crg_v2 (
447 OPJ_BYTE * p_header_data,
448 OPJ_UINT32 p_header_size,
449 struct opj_event_mgr * p_manager
452 Read the TLM marker (tile-part lengths)
453 @param j2k J2K handle
455 static void j2k_read_tlm(opj_j2k_t *j2k);
457 * Reads a TLM marker (Tile Length Marker)
459 * @param p_header_data the data contained in the TLM box.
460 * @param p_j2k the jpeg2000 codec.
461 * @param p_header_size the size of the data contained in the TLM marker.
462 * @param p_manager the user event manager.
464 static opj_bool j2k_read_tlm_v2 (
466 OPJ_BYTE * p_header_data,
467 OPJ_UINT32 p_header_size,
468 struct opj_event_mgr * p_manager
471 Read the PLM marker (packet length, main header)
472 @param j2k J2K handle
474 static void j2k_read_plm(opj_j2k_t *j2k);
477 * Reads a PLM marker (Packet length, main header marker)
479 * @param p_header_data the data contained in the TLM box.
480 * @param p_j2k the jpeg2000 codec.
481 * @param p_header_size the size of the data contained in the TLM marker.
482 * @param p_manager the user event manager.
484 static opj_bool j2k_read_plm_v2 (
486 OPJ_BYTE * p_header_data,
487 OPJ_UINT32 p_header_size,
488 struct opj_event_mgr * p_manager
491 Read the PLT marker (packet length, tile-part header)
492 @param j2k J2K handle
494 static void j2k_read_plt(opj_j2k_t *j2k);
496 * Reads a PLT marker (Packet length, tile-part header)
498 * @param p_header_data the data contained in the PLT box.
499 * @param p_j2k the jpeg2000 codec.
500 * @param p_header_size the size of the data contained in the PLT marker.
501 * @param p_manager the user event manager.
503 static opj_bool j2k_read_plt_v2 (
505 OPJ_BYTE * p_header_data,
506 OPJ_UINT32 p_header_size,
507 struct opj_event_mgr * p_manager
510 Read the PPM marker (packet packet headers, main header)
511 @param j2k J2K handle
513 static void j2k_read_ppm(opj_j2k_t *j2k);
515 * Reads a PPM marker (Packed packet headers, main header)
517 * @param p_header_data the data contained in the POC box.
518 * @param p_j2k the jpeg2000 codec.
519 * @param p_header_size the size of the data contained in the POC marker.
520 * @param p_manager the user event manager.
522 static opj_bool j2k_read_ppm_v2 (
524 OPJ_BYTE * p_header_data,
525 OPJ_UINT32 p_header_size,
526 struct opj_event_mgr * p_manager
529 static opj_bool j2k_read_ppm_v3 (
531 OPJ_BYTE * p_header_data,
532 OPJ_UINT32 p_header_size,
533 struct opj_event_mgr * p_manager
537 Read the PPT marker (packet packet headers, tile-part header)
538 @param j2k J2K handle
540 static void j2k_read_ppt(opj_j2k_t *j2k);
542 * Reads a PPT marker (Packed packet headers, tile-part header)
544 * @param p_header_data the data contained in the PPT box.
545 * @param p_j2k the jpeg2000 codec.
546 * @param p_header_size the size of the data contained in the PPT marker.
547 * @param p_manager the user event manager.
549 static opj_bool j2k_read_ppt_v2 (
551 OPJ_BYTE * p_header_data,
552 OPJ_UINT32 p_header_size,
553 struct opj_event_mgr * p_manager
556 Write the TLM marker (Mainheader)
557 @param j2k J2K handle
559 static void j2k_write_tlm(opj_j2k_t *j2k);
561 Write the SOT marker (start of tile-part)
562 @param j2k J2K handle
564 static void j2k_write_sot(opj_j2k_t *j2k);
566 Read the SOT marker (start of tile-part)
567 @param j2k J2K handle
569 static void j2k_read_sot(opj_j2k_t *j2k);
572 * Reads a PPT marker (Packed packet headers, tile-part header)
574 * @param p_header_data the data contained in the PPT box.
575 * @param p_j2k the jpeg2000 codec.
576 * @param p_header_size the size of the data contained in the PPT marker.
577 * @param p_manager the user event manager.
579 static opj_bool j2k_read_sot_v2 (
581 OPJ_BYTE * p_header_data,
582 OPJ_UINT32 p_header_size,
583 struct opj_event_mgr * p_manager
586 Write the SOD marker (start of data)
587 @param j2k J2K handle
588 @param tile_coder Pointer to a TCD handle
590 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
592 Read the SOD marker (start of data)
593 @param j2k J2K handle
595 static void j2k_read_sod(opj_j2k_t *j2k);
598 * Reads a SOD marker (Start Of Data)
600 * @param p_header_data the data contained in the SOD box.
601 * @param p_j2k the jpeg2000 codec.
602 * @param p_header_size the size of the data contained in the SOD marker.
603 * @param p_manager the user event manager.
605 static opj_bool j2k_read_sod_v2 (
607 struct opj_stream_private *p_stream,
608 struct opj_event_mgr * p_manager
612 Write the RGN marker (region-of-interest)
613 @param j2k J2K handle
614 @param compno Number of the component concerned by the information written
615 @param tileno Number of the tile concerned by the information written
617 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
619 Read the RGN marker (region-of-interest)
620 @param j2k J2K handle
622 static void j2k_read_rgn(opj_j2k_t *j2k);
625 * Reads a RGN marker (Region Of Interest)
627 * @param p_header_data the data contained in the POC box.
628 * @param p_j2k the jpeg2000 codec.
629 * @param p_header_size the size of the data contained in the POC marker.
630 * @param p_manager the user event manager.
632 static opj_bool j2k_read_rgn_v2 (
634 OPJ_BYTE * p_header_data,
635 OPJ_UINT32 p_header_size,
636 struct opj_event_mgr * p_manager
640 Write the EOC marker (end of codestream)
641 @param j2k J2K handle
643 static void j2k_write_eoc(opj_j2k_t *j2k);
645 Read the EOC marker (end of codestream)
646 @param j2k J2K handle
648 static void j2k_read_eoc(opj_j2k_t *j2k);
651 * Reads a EOC marker (End Of Codestream)
653 * @param p_header_data the data contained in the SOD box.
654 * @param p_j2k the jpeg2000 codec.
655 * @param p_header_size the size of the data contained in the SOD marker.
656 * @param p_manager the user event manager.
658 static opj_bool j2k_read_eoc_v2 (
660 struct opj_stream_private *p_stream,
661 struct opj_event_mgr * p_manager
665 Read an unknown marker
666 @param j2k J2K handle
668 static void j2k_read_unk(opj_j2k_t *j2k);
670 Add main header marker information
671 @param cstr_info Codestream information structure
672 @param type marker type
673 @param pos byte offset of marker segment
674 @param len length of marker segment
676 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
678 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
680 Add tile header marker information
681 @param tileno tile index number
682 @param cstr_info Codestream information structure
683 @param type marker type
684 @param pos byte offset of marker segment
685 @param len length of marker segment
687 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
689 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);
692 * Reads an unknown marker
694 * @param p_stream the stream object to read from.
695 * @param p_j2k the jpeg2000 codec.
696 * @param p_manager the user event manager.
698 * @return true if the marker could be deduced.
700 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
701 struct opj_stream_private *p_stream,
702 OPJ_UINT32 *output_marker,
703 struct opj_event_mgr * p_manager );
706 * Reads a MCT marker (Multiple Component Transform)
708 * @param p_header_data the data contained in the MCT box.
709 * @param p_j2k the jpeg2000 codec.
710 * @param p_header_size the size of the data contained in the MCT marker.
711 * @param p_manager the user event manager.
713 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
714 OPJ_BYTE * p_header_data,
715 OPJ_UINT32 p_header_size,
716 struct opj_event_mgr * p_manager );
719 * Reads a MCC marker (Multiple Component Collection)
721 * @param p_header_data the data contained in the MCC box.
722 * @param p_j2k the jpeg2000 codec.
723 * @param p_header_size the size of the data contained in the MCC marker.
724 * @param p_manager the user event manager.
726 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
727 OPJ_BYTE * p_header_data,
728 OPJ_UINT32 p_header_size,
729 struct opj_event_mgr * p_manager );
732 * Reads a MCO marker (Multiple Component Transform Ordering)
734 * @param p_header_data the data contained in the MCO box.
735 * @param p_j2k the jpeg2000 codec.
736 * @param p_header_size the size of the data contained in the MCO marker.
737 * @param p_manager the user event manager.
739 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
740 OPJ_BYTE * p_header_data,
741 OPJ_UINT32 p_header_size,
742 struct opj_event_mgr * p_manager );
744 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
746 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
747 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
748 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
749 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
751 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
752 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
753 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
754 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
756 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
757 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
758 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
759 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
762 * Reads a CBD marker (Component bit depth definition)
763 * @param p_header_data the data contained in the CBD box.
764 * @param p_j2k the jpeg2000 codec.
765 * @param p_header_size the size of the data contained in the CBD marker.
766 * @param p_manager the user event manager.
768 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
769 OPJ_BYTE * p_header_data,
770 OPJ_UINT32 p_header_size,
771 struct opj_event_mgr * p_manager);
774 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
776 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
778 static opj_codestream_index_t* j2k_create_cstr_index(void);
784 /* ----------------------------------------------------------------------- */
785 typedef struct j2k_prog_order{
786 OPJ_PROG_ORDER enum_prog;
790 j2k_prog_order_t j2k_prog_order_list[] = {
796 {(OPJ_PROG_ORDER)-1, ""}
804 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
812 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
814 const j2k_mct_function j2k_mct_read_functions_to_float [] =
816 j2k_read_int16_to_float,
817 j2k_read_int32_to_float,
818 j2k_read_float32_to_float,
819 j2k_read_float64_to_float
822 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
824 j2k_read_int16_to_int32,
825 j2k_read_int32_to_int32,
826 j2k_read_float32_to_int32,
827 j2k_read_float64_to_int32
830 const j2k_mct_function j2k_mct_write_functions_from_float [] =
832 j2k_write_float_to_int16,
833 j2k_write_float_to_int32,
834 j2k_write_float_to_float,
835 j2k_write_float_to_float64
838 typedef struct opj_dec_memory_marker_handler
842 /** value of the state when the marker can appear */
844 /** action linked to the marker */
845 opj_bool (*handler) (
847 OPJ_BYTE * p_header_data,
848 OPJ_UINT32 p_header_size,
849 struct opj_event_mgr * p_manager
852 opj_dec_memory_marker_handler_t;
854 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
857 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
858 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
859 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
860 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
861 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
862 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
863 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
864 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
865 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
866 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
867 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
868 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
869 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
871 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
872 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
873 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
874 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
875 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
876 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
878 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
879 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
880 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
881 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
882 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
883 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
884 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
885 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
886 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
887 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
888 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
889 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
890 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
892 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
893 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
894 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
895 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
896 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
897 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
899 #ifdef TODO_MS /* FIXME */
900 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
901 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
902 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
903 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
905 #endif /* USE_JPWL */
907 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
908 {J2K_MS_INSEC, 0, j2k_read_insec}
909 #endif /* USE_JPSEC */
910 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}//j2k_read_unk_v2}
915 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
917 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
918 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
922 for (i=0;i<p_nb_elem;++i) {
923 opj_read_bytes(l_src_data,&l_temp,2);
925 l_src_data+=sizeof(OPJ_INT16);
927 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
931 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
933 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
934 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
938 for (i=0;i<p_nb_elem;++i) {
939 opj_read_bytes(l_src_data,&l_temp,4);
941 l_src_data+=sizeof(OPJ_INT32);
943 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
947 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
949 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
950 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
954 for (i=0;i<p_nb_elem;++i) {
955 opj_read_float(l_src_data,&l_temp);
957 l_src_data+=sizeof(OPJ_FLOAT32);
959 *(l_dest_data++) = l_temp;
963 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
965 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
966 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
970 for (i=0;i<p_nb_elem;++i) {
971 opj_read_double(l_src_data,&l_temp);
973 l_src_data+=sizeof(OPJ_FLOAT64);
975 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
979 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
981 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
982 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
986 for (i=0;i<p_nb_elem;++i) {
987 opj_read_bytes(l_src_data,&l_temp,2);
989 l_src_data+=sizeof(OPJ_INT16);
991 *(l_dest_data++) = (OPJ_INT32) l_temp;
995 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
997 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
998 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1002 for (i=0;i<p_nb_elem;++i) {
1003 opj_read_bytes(l_src_data,&l_temp,4);
1005 l_src_data+=sizeof(OPJ_INT32);
1007 *(l_dest_data++) = (OPJ_INT32) l_temp;
1011 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1013 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1014 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1018 for (i=0;i<p_nb_elem;++i) {
1019 opj_read_float(l_src_data,&l_temp);
1021 l_src_data+=sizeof(OPJ_FLOAT32);
1023 *(l_dest_data++) = (OPJ_INT32) l_temp;
1027 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1029 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1030 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1034 for (i=0;i<p_nb_elem;++i) {
1035 opj_read_double(l_src_data,&l_temp);
1037 l_src_data+=sizeof(OPJ_FLOAT64);
1039 *(l_dest_data++) = (OPJ_INT32) l_temp;
1043 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1045 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1046 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1050 for (i=0;i<p_nb_elem;++i) {
1051 l_temp = (OPJ_UINT32) *(l_src_data++);
1053 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1055 l_dest_data+=sizeof(OPJ_INT16);
1059 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1061 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1062 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1066 for (i=0;i<p_nb_elem;++i) {
1067 l_temp = (OPJ_UINT32) *(l_src_data++);
1069 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1071 l_dest_data+=sizeof(OPJ_INT32);
1075 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1077 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1078 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1082 for (i=0;i<p_nb_elem;++i) {
1083 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1085 opj_write_float(l_dest_data,l_temp);
1087 l_dest_data+=sizeof(OPJ_FLOAT32);
1091 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1093 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1094 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1098 for (i=0;i<p_nb_elem;++i) {
1099 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1101 opj_write_double(l_dest_data,l_temp);
1103 l_dest_data+=sizeof(OPJ_FLOAT64);
1109 * Converts an enum type progression order to string type.
1111 * @param prg_order the progression order to get.
1113 * @return the string representation of the given progression order.
1115 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1116 j2k_prog_order_t *po;
1117 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1118 if(po->enum_prog == prg_order){
1119 return po->str_prog;
1122 return po->str_prog;
1125 /* ----------------------------------------------------------------------- */
1126 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1129 int tpnum=1,tpend=0;
1130 opj_tcp_t *tcp = &cp->tcps[tileno];
1131 prog = j2k_convert_progression_order(tcp->prg);
1136 if( cp->tp_flag == prog[i] ){
1137 tpend=1;cp->tp_pos=i;
1141 tpnum= tpnum * tcp->pocs[pino].compE;
1144 tpnum= tpnum * tcp->pocs[pino].resE;
1147 tpnum= tpnum * tcp->pocs[pino].prcE;
1150 tpnum= tpnum * tcp->pocs[pino].layE;
1161 /** mem allocation for TLM marker*/
1162 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1163 int pino,tileno,totnum_tp=0;
1165 OPJ_ARG_NOT_USED(img_numcomp);
1167 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1168 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1169 int cur_totnum_tp = 0;
1170 opj_tcp_t *tcp = &cp->tcps[tileno];
1171 for(pino = 0; pino <= tcp->numpocs; pino++) {
1173 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1174 if(!pi) { return -1;}
1175 tp_num = j2k_get_num_tp(cp,pino,tileno);
1176 totnum_tp = totnum_tp + tp_num;
1177 cur_totnum_tp = cur_totnum_tp + tp_num;
1178 pi_destroy(pi, cp, tileno);
1180 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1182 if (j2k->cstr_info) {
1183 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1184 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1191 static void j2k_write_soc(opj_j2k_t *j2k) {
1192 opj_cio_t *cio = j2k->cio;
1193 cio_write(cio, J2K_MS_SOC, 2);
1196 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1201 /* update markers struct */
1202 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1203 #endif /* USE_JPWL */
1207 static void j2k_read_soc(opj_j2k_t *j2k) {
1208 j2k->state = J2K_STATE_MHSIZ;
1210 if (j2k->cstr_info) {
1211 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
1212 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
1217 * Reads a SOC marker (Start of Codestream)
1218 * @param p_header_data the data contained in the SOC box.
1219 * @param jp2 the jpeg2000 file codec.
1220 * @param p_header_size the size of the data contained in the SOC marker.
1221 * @param p_manager the user event manager.
1223 static opj_bool j2k_read_soc_v2( opj_j2k_v2_t *p_j2k,
1224 struct opj_stream_private *p_stream,
1225 struct opj_event_mgr * p_manager )
1227 OPJ_BYTE l_data [2];
1228 OPJ_UINT32 l_marker;
1231 assert(p_j2k != 00);
1232 assert(p_manager != 00);
1233 assert(p_stream != 00);
1235 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1239 opj_read_bytes(l_data,&l_marker,2);
1240 if (l_marker != J2K_MS_SOC) {
1244 /* Next marker should be a SIZ marker in the main header */
1245 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1247 /* FIXME move it in a index structure included in p_j2k*/
1248 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1250 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1252 /* Add the marker to the codestream index*/
1253 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1258 static void j2k_write_siz(opj_j2k_t *j2k) {
1262 opj_cio_t *cio = j2k->cio;
1263 opj_image_t *image = j2k->image;
1264 opj_cp_t *cp = j2k->cp;
1266 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
1267 lenp = cio_tell(cio);
1269 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
1270 cio_write(cio, image->x1, 4); /* Xsiz */
1271 cio_write(cio, image->y1, 4); /* Ysiz */
1272 cio_write(cio, image->x0, 4); /* X0siz */
1273 cio_write(cio, image->y0, 4); /* Y0siz */
1274 cio_write(cio, cp->tdx, 4); /* XTsiz */
1275 cio_write(cio, cp->tdy, 4); /* YTsiz */
1276 cio_write(cio, cp->tx0, 4); /* XT0siz */
1277 cio_write(cio, cp->ty0, 4); /* YT0siz */
1278 cio_write(cio, image->numcomps, 2); /* Csiz */
1279 for (i = 0; i < image->numcomps; i++) {
1280 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
1281 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
1282 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
1284 len = cio_tell(cio) - lenp;
1285 cio_seek(cio, lenp);
1286 cio_write(cio, len, 2); /* Lsiz */
1287 cio_seek(cio, lenp + len);
1290 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
1293 static void j2k_read_siz(opj_j2k_t *j2k) {
1296 opj_cio_t *cio = j2k->cio;
1297 opj_image_t *image = j2k->image;
1298 opj_cp_t *cp = j2k->cp;
1300 len = cio_read(cio, 2); /* Lsiz */
1301 cio_read(cio, 2); /* Rsiz (capabilities) */
1302 image->x1 = cio_read(cio, 4); /* Xsiz */
1303 image->y1 = cio_read(cio, 4); /* Ysiz */
1304 image->x0 = cio_read(cio, 4); /* X0siz */
1305 image->y0 = cio_read(cio, 4); /* Y0siz */
1306 cp->tdx = cio_read(cio, 4); /* XTsiz */
1307 cp->tdy = cio_read(cio, 4); /* YTsiz */
1308 cp->tx0 = cio_read(cio, 4); /* XT0siz */
1309 cp->ty0 = cio_read(cio, 4); /* YT0siz */
1311 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
1312 opj_event_msg(j2k->cinfo, EVT_ERROR,
1313 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
1314 image->x0,image->x1,image->y0,image->y1);
1318 image->numcomps = cio_read(cio, 2); /* Csiz */
1321 if (j2k->cp->correct) {
1322 /* if JPWL is on, we check whether TX errors have damaged
1323 too much the SIZ parameters */
1324 if (!(image->x1 * image->y1)) {
1325 opj_event_msg(j2k->cinfo, EVT_ERROR,
1326 "JPWL: bad image size (%d x %d)\n",
1327 image->x1, image->y1);
1328 if (!JPWL_ASSUME || JPWL_ASSUME) {
1329 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1333 if (image->numcomps != ((len - 38) / 3)) {
1334 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1335 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1336 image->numcomps, ((len - 38) / 3));
1338 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1341 /* we try to correct */
1342 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
1343 if (image->numcomps < ((len - 38) / 3)) {
1344 len = 38 + 3 * image->numcomps;
1345 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1348 image->numcomps = ((len - 38) / 3);
1349 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1354 /* update components number in the jpwl_exp_comps filed */
1355 cp->exp_comps = image->numcomps;
1357 #endif /* USE_JPWL */
1359 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
1360 for (i = 0; i < image->numcomps; i++) {
1362 tmp = cio_read(cio, 1); /* Ssiz_i */
1363 image->comps[i].prec = (tmp & 0x7f) + 1;
1364 image->comps[i].sgnd = tmp >> 7;
1365 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
1366 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
1369 if (j2k->cp->correct) {
1370 /* if JPWL is on, we check whether TX errors have damaged
1371 too much the SIZ parameters, again */
1372 if (!(image->comps[i].dx * image->comps[i].dy)) {
1373 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1374 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1375 i, i, image->comps[i].dx, image->comps[i].dy);
1377 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1380 /* we try to correct */
1381 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1382 if (!image->comps[i].dx) {
1383 image->comps[i].dx = 1;
1384 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1385 i, image->comps[i].dx);
1387 if (!image->comps[i].dy) {
1388 image->comps[i].dy = 1;
1389 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1390 i, image->comps[i].dy);
1395 #endif /* USE_JPWL */
1397 /* TODO: unused ? */
1398 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
1399 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
1401 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
1402 image->comps[i].factor = cp->reduce; /* reducing factor per component */
1405 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
1406 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
1409 if (j2k->cp->correct) {
1410 /* if JPWL is on, we check whether TX errors have damaged
1411 too much the SIZ parameters */
1412 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
1413 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1414 "JPWL: bad number of tiles (%d x %d)\n",
1417 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1420 /* we try to correct */
1421 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1424 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1427 if (cp->tw > cp->max_tiles) {
1429 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
1430 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1431 cp->max_tiles, cp->tw);
1435 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1438 if (cp->th > cp->max_tiles) {
1440 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1441 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1442 cp->max_tiles, cp->th);
1446 #endif /* USE_JPWL */
1448 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
1449 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
1450 cp->tileno_size = 0;
1453 if (j2k->cp->correct) {
1455 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1456 "JPWL: could not alloc tcps field of cp\n");
1457 if (!JPWL_ASSUME || JPWL_ASSUME) {
1458 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1463 #endif /* USE_JPWL */
1465 for (i = 0; i < cp->tw * cp->th; i++) {
1466 cp->tcps[i].POC = 0;
1467 cp->tcps[i].numpocs = 0;
1468 cp->tcps[i].first = 1;
1471 /* Initialization for PPM marker */
1473 cp->ppm_data = NULL;
1474 cp->ppm_data_first = NULL;
1475 cp->ppm_previous = 0;
1478 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
1479 for (i = 0; i < cp->tw * cp->th; i++) {
1480 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
1482 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
1483 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
1484 j2k->state = J2K_STATE_MH;
1487 if (j2k->cstr_info) {
1488 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1489 cstr_info->image_w = image->x1 - image->x0;
1490 cstr_info->image_h = image->y1 - image->y0;
1491 cstr_info->numcomps = image->numcomps;
1492 cstr_info->tw = cp->tw;
1493 cstr_info->th = cp->th;
1494 cstr_info->tile_x = cp->tdx;
1495 cstr_info->tile_y = cp->tdy;
1496 cstr_info->tile_Ox = cp->tx0;
1497 cstr_info->tile_Oy = cp->ty0;
1498 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
1504 * Reads a SIZ marker (image and tile size)
1505 * @param p_header_data the data contained in the SIZ box.
1506 * @param jp2 the jpeg2000 file codec.
1507 * @param p_header_size the size of the data contained in the SIZ marker.
1508 * @param p_manager the user event manager.
1510 opj_bool j2k_read_siz_v2 (
1511 opj_j2k_v2_t *p_j2k,
1512 OPJ_BYTE * p_header_data,
1513 OPJ_UINT32 p_header_size,
1514 struct opj_event_mgr * p_manager
1517 OPJ_UINT32 l_size, i;
1518 OPJ_UINT32 l_nb_comp;
1519 OPJ_UINT32 l_nb_comp_remain;
1520 OPJ_UINT32 l_remaining_size;
1521 OPJ_UINT32 l_nb_tiles;
1523 opj_image_t *l_image = 00;
1524 opj_cp_v2_t *l_cp = 00;
1525 opj_image_comp_t * l_img_comp = 00;
1526 opj_tcp_v2_t * l_current_tile_param = 00;
1529 assert(p_j2k != 00);
1530 assert(p_manager != 00);
1531 assert(p_header_data != 00);
1533 l_image = p_j2k->m_private_image;
1534 l_cp = &(p_j2k->m_cp);
1536 // minimum size == 39 - 3 (= minimum component parameter)
1537 if (p_header_size < 36) {
1538 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1542 l_remaining_size = p_header_size - 36;
1543 l_nb_comp = l_remaining_size / 3;
1544 l_nb_comp_remain = l_remaining_size % 3;
1545 if (l_nb_comp_remain != 0){
1546 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1550 l_size = p_header_size + 2; /* Lsiz */
1552 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
1554 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1555 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
1557 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
1559 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
1561 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
1563 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
1565 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
1567 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
1569 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
1571 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
1574 l_image->numcomps = (OPJ_UINT16) l_tmp;
1576 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
1580 if (l_image->numcomps != l_nb_comp) {
1581 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);
1586 if (l_cp->correct) {
1587 /* if JPWL is on, we check whether TX errors have damaged
1588 too much the SIZ parameters */
1589 if (!(l_image->x1 * l_image->y1)) {
1590 opj_event_msg_v2(p_manager, EVT_ERROR,
1591 "JPWL: bad image size (%d x %d)\n",
1592 l_image->x1, l_image->y1);
1593 if (!JPWL_ASSUME || JPWL_ASSUME) {
1594 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1599 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
1600 if (l_image->numcomps != ((len - 38) / 3)) {
1601 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1602 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1603 l_image->numcomps, ((len - 38) / 3));
1605 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1608 */ /* we try to correct */
1609 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
1610 if (l_image->numcomps < ((len - 38) / 3)) {
1611 len = 38 + 3 * l_image->numcomps;
1612 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1615 l_image->numcomps = ((len - 38) / 3);
1616 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1622 /* update components number in the jpwl_exp_comps filed */
1623 l_cp->exp_comps = l_image->numcomps;
1625 #endif /* USE_JPWL */
1627 // Allocate the resulting image components
1628 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
1629 if (l_image->comps == 00){
1630 l_image->numcomps = 0;
1631 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1635 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
1636 l_img_comp = l_image->comps;
1638 // Read the component information
1639 for (i = 0; i < l_image->numcomps; ++i){
1641 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
1643 l_img_comp->prec = (tmp & 0x7f) + 1;
1644 l_img_comp->sgnd = tmp >> 7;
1645 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
1647 l_img_comp->dx = (OPJ_INT32)tmp; // should be between 1 and 255
1648 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
1650 l_img_comp->dy = (OPJ_INT32)tmp; // should be between 1 and 255
1653 if (l_cp->correct) {
1654 /* if JPWL is on, we check whether TX errors have damaged
1655 too much the SIZ parameters, again */
1656 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
1657 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1658 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1659 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
1661 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1664 /* we try to correct */
1665 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1666 if (!l_image->comps[i].dx) {
1667 l_image->comps[i].dx = 1;
1668 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1669 i, l_image->comps[i].dx);
1671 if (!l_image->comps[i].dy) {
1672 l_image->comps[i].dy = 1;
1673 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1674 i, l_image->comps[i].dy);
1678 #endif /* USE_JPWL */
1679 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
1680 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
1684 // Compute the number of tiles
1685 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
1686 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
1687 l_nb_tiles = l_cp->tw * l_cp->th;
1689 // Define the tiles which will be decoded
1690 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
1691 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;
1692 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;
1693 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);
1694 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);
1697 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
1698 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
1699 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
1700 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
1704 if (l_cp->correct) {
1705 /* if JPWL is on, we check whether TX errors have damaged
1706 too much the SIZ parameters */
1707 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
1708 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1709 "JPWL: bad number of tiles (%d x %d)\n",
1710 l_cp->tw, l_cp->th);
1712 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1715 /* we try to correct */
1716 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1719 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1722 if (l_cp->tw > l_cp->max_tiles) {
1724 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
1725 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1726 l_cp->max_tiles, l_cp->tw);
1730 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1733 if (l_cp->th > l_cp->max_tiles) {
1735 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1736 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1737 l_cp->max_tiles, l_cp->th);
1741 #endif /* USE_JPWL */
1743 /* memory allocations */
1744 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
1745 if (l_cp->tcps == 00) {
1746 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1749 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
1752 if (l_cp->correct) {
1754 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1755 "JPWL: could not alloc tcps field of cp\n");
1756 if (!JPWL_ASSUME || JPWL_ASSUME) {
1757 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1762 #endif /* USE_JPWL */
1764 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
1765 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
1766 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
1767 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1770 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
1772 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
1773 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
1775 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
1776 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1779 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));
1780 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
1782 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
1783 (opj_simple_mcc_decorrelation_data_t*)
1784 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
1786 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
1787 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1790 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));
1791 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
1793 /* set up default dc level shift */
1794 for (i=0;i<l_image->numcomps;++i) {
1795 if (! l_image->comps[i].sgnd) {
1796 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
1800 l_current_tile_param = l_cp->tcps;
1801 for (i = 0; i < l_nb_tiles; ++i) {
1802 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
1803 if (l_current_tile_param->tccps == 00) {
1804 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1807 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
1809 ++l_current_tile_param;
1812 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; // FIXME J2K_DEC_STATE_MH;
1813 opj_image_comp_header_update(l_image,l_cp);
1820 static void j2k_write_com(opj_j2k_t *j2k) {
1824 if(j2k->cp->comment) {
1825 opj_cio_t *cio = j2k->cio;
1826 char *comment = j2k->cp->comment;
1828 cio_write(cio, J2K_MS_COM, 2);
1829 lenp = cio_tell(cio);
1831 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
1832 for (i = 0; i < strlen(comment); i++) {
1833 cio_write(cio, comment[i], 1);
1835 len = cio_tell(cio) - lenp;
1836 cio_seek(cio, lenp);
1837 cio_write(cio, len, 2);
1838 cio_seek(cio, lenp + len);
1842 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
1847 static void j2k_read_com(opj_j2k_t *j2k) {
1850 opj_cio_t *cio = j2k->cio;
1852 len = cio_read(cio, 2);
1853 cio_skip(cio, len - 2);
1856 * Reads a COM marker (comments)
1857 * @param p_header_data the data contained in the COM box.
1858 * @param jp2 the jpeg2000 file codec.
1859 * @param p_header_size the size of the data contained in the COM marker.
1860 * @param p_manager the user event manager.
1862 opj_bool j2k_read_com_v2 (
1863 opj_j2k_v2_t *p_j2k,
1864 OPJ_BYTE * p_header_data,
1865 OPJ_UINT32 p_header_size,
1866 struct opj_event_mgr * p_manager
1870 assert(p_j2k != 00);
1871 assert(p_manager != 00);
1872 assert(p_header_data != 00);
1877 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
1880 opj_cp_t *cp = j2k->cp;
1881 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1882 opj_tccp_t *tccp = &tcp->tccps[compno];
1883 opj_cio_t *cio = j2k->cio;
1885 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
1886 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
1887 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
1888 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
1889 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
1891 if (tccp->csty & J2K_CCP_CSTY_PRT) {
1892 for (i = 0; i < tccp->numresolutions; i++) {
1893 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
1898 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
1901 opj_cp_t *cp = j2k->cp;
1902 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1903 opj_tccp_t *tccp = &tcp->tccps[compno];
1904 opj_cio_t *cio = j2k->cio;
1906 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
1908 /* If user wants to remove more resolutions than the codestream contains, return error*/
1909 if (cp->reduce >= tccp->numresolutions) {
1910 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
1911 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
1912 j2k->state |= J2K_STATE_ERR;
1915 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
1916 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
1917 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
1918 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
1919 if (tccp->csty & J2K_CP_CSTY_PRT) {
1920 for (i = 0; i < tccp->numresolutions; i++) {
1921 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
1922 tccp->prcw[i] = tmp & 0xf;
1923 tccp->prch[i] = tmp >> 4;
1928 if(j2k->cstr_info && compno == 0) {
1929 for (i = 0; i < tccp->numresolutions; i++) {
1930 if (tccp->csty & J2K_CP_CSTY_PRT) {
1931 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
1932 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
1935 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1936 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1943 static void j2k_write_cod(opj_j2k_t *j2k) {
1944 opj_cp_t *cp = NULL;
1945 opj_tcp_t *tcp = NULL;
1948 opj_cio_t *cio = j2k->cio;
1950 cio_write(cio, J2K_MS_COD, 2); /* COD */
1952 lenp = cio_tell(cio);
1956 tcp = &cp->tcps[j2k->curtileno];
1958 cio_write(cio, tcp->csty, 1); /* Scod */
1959 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
1960 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
1961 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
1963 j2k_write_cox(j2k, 0);
1964 len = cio_tell(cio) - lenp;
1965 cio_seek(cio, lenp);
1966 cio_write(cio, len, 2); /* Lcod */
1967 cio_seek(cio, lenp + len);
1970 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
1974 static void j2k_read_cod(opj_j2k_t *j2k) {
1977 opj_cio_t *cio = j2k->cio;
1978 opj_cp_t *cp = j2k->cp;
1979 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1980 opj_image_t *image = j2k->image;
1982 len = cio_read(cio, 2); /* Lcod */
1983 tcp->csty = cio_read(cio, 1); /* Scod */
1984 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
1985 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
1986 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
1988 pos = cio_tell(cio);
1989 for (i = 0; i < image->numcomps; i++) {
1990 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
1992 j2k_read_cox(j2k, i);
1996 if (j2k->cstr_info) {
1997 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1998 cstr_info->prog = tcp->prg;
1999 cstr_info->numlayers = tcp->numlayers;
2000 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
2001 for (i = 0; i < image->numcomps; i++) {
2002 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
2008 * Reads a COD marker (Coding Styke defaults)
2009 * @param p_header_data the data contained in the COD box.
2010 * @param p_j2k the jpeg2000 codec.
2011 * @param p_header_size the size of the data contained in the COD marker.
2012 * @param p_manager the user event manager.
2014 opj_bool j2k_read_cod_v2 (
2015 opj_j2k_v2_t *p_j2k,
2016 OPJ_BYTE * p_header_data,
2017 OPJ_UINT32 p_header_size,
2018 struct opj_event_mgr * p_manager
2024 opj_cp_v2_t *l_cp = 00;
2025 opj_tcp_v2_t *l_tcp = 00;
2026 opj_image_t *l_image = 00;
2029 assert(p_header_data != 00);
2030 assert(p_j2k != 00);
2031 assert(p_manager != 00);
2033 l_image = p_j2k->m_private_image;
2034 l_cp = &(p_j2k->m_cp);
2036 /* If we are in the first tile-part header of the current tile */
2037 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2038 &l_cp->tcps[p_j2k->m_current_tile_number] :
2039 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2041 /* Make sure room is sufficient */
2042 if (p_header_size < 5) {
2043 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2047 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2049 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2051 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2052 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2055 // If user didn't set a number layer to decode take the max specify in the codestream.
2056 if (l_cp->m_specific_param.m_dec.m_layer) {
2057 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2060 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2063 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2067 for (i = 0; i < l_image->numcomps; ++i) {
2068 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2071 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2072 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2076 if (p_header_size != 0) {
2077 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2081 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2082 j2k_copy_tile_component_parameters(p_j2k);
2085 #ifdef WIP_REMOVE_MSD
2086 if (p_j2k->cstr_info) {
2087 //opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;
2088 p_j2k->cstr_info->prog = l_tcp->prg;
2089 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2090 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2091 for (i = 0; i < l_image->numcomps; ++i) {
2092 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2100 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2103 opj_cp_t *cp = j2k->cp;
2104 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2105 opj_image_t *image = j2k->image;
2106 opj_cio_t *cio = j2k->cio;
2108 cio_write(cio, J2K_MS_COC, 2); /* COC */
2109 lenp = cio_tell(cio);
2111 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2112 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2113 j2k_write_cox(j2k, compno);
2114 len = cio_tell(cio) - lenp;
2115 cio_seek(cio, lenp);
2116 cio_write(cio, len, 2); /* Lcoc */
2117 cio_seek(cio, lenp + len);
2120 static void j2k_read_coc(opj_j2k_t *j2k) {
2123 opj_cp_t *cp = j2k->cp;
2124 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2125 opj_image_t *image = j2k->image;
2126 opj_cio_t *cio = j2k->cio;
2128 len = cio_read(cio, 2); /* Lcoc */
2129 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2130 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
2131 j2k_read_cox(j2k, compno);
2135 * Reads a COC marker (Coding Style Component)
2136 * @param p_header_data the data contained in the COC box.
2137 * @param p_j2k the jpeg2000 codec.
2138 * @param p_header_size the size of the data contained in the COC marker.
2139 * @param p_manager the user event manager.
2141 opj_bool j2k_read_coc_v2 (
2142 opj_j2k_v2_t *p_j2k,
2143 OPJ_BYTE * p_header_data,
2144 OPJ_UINT32 p_header_size,
2145 struct opj_event_mgr * p_manager
2148 opj_cp_v2_t *l_cp = NULL;
2149 opj_tcp_v2_t *l_tcp = NULL;
2150 opj_image_t *l_image = NULL;
2151 OPJ_UINT32 l_comp_room;
2152 OPJ_UINT32 l_comp_no;
2155 assert(p_header_data != 00);
2156 assert(p_j2k != 00);
2157 assert(p_manager != 00);
2159 l_cp = &(p_j2k->m_cp);
2160 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2161 &l_cp->tcps[p_j2k->m_current_tile_number] :
2162 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2163 l_image = p_j2k->m_private_image;
2165 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2167 // make sure room is sufficient
2168 if (p_header_size < l_comp_room + 1) {
2169 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2172 p_header_size -= l_comp_room + 1;
2174 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2175 p_header_data += l_comp_room;
2176 if (l_comp_no >= l_image->numcomps) {
2177 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2181 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2184 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2185 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2189 if (p_header_size != 0) {
2190 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2196 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
2197 int bandno, numbands;
2200 opj_cp_t *cp = j2k->cp;
2201 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2202 opj_tccp_t *tccp = &tcp->tccps[compno];
2203 opj_cio_t *cio = j2k->cio;
2205 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
2206 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
2208 for (bandno = 0; bandno < numbands; bandno++) {
2209 expn = tccp->stepsizes[bandno].expn;
2210 mant = tccp->stepsizes[bandno].mant;
2212 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
2213 cio_write(cio, expn << 3, 1); /* SPqcx_i */
2215 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
2220 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
2222 int bandno, numbands;
2224 opj_cp_t *cp = j2k->cp;
2225 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2226 opj_tccp_t *tccp = &tcp->tccps[compno];
2227 opj_cio_t *cio = j2k->cio;
2229 tmp = cio_read(cio, 1); /* Sqcx */
2230 tccp->qntsty = tmp & 0x1f;
2231 tccp->numgbits = tmp >> 5;
2232 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
2233 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
2236 if (j2k->cp->correct) {
2238 /* if JPWL is on, we check whether there are too many subbands */
2239 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
2240 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2241 "JPWL: bad number of subbands in Sqcx (%d)\n",
2244 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2247 /* we try to correct */
2249 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
2250 "- setting number of bands to %d => HYPOTHESIS!!!\n",
2257 /* We check whether there are too many subbands */
2258 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
2259 opj_event_msg(j2k->cinfo, EVT_WARNING ,
2260 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
2261 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
2264 #endif /* USE_JPWL */
2266 for (bandno = 0; bandno < numbands; bandno++) {
2268 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
2269 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
2272 tmp = cio_read(cio, 2); /* SPqcx_i */
2276 if (bandno < J2K_MAXBANDS){
2277 tccp->stepsizes[bandno].expn = expn;
2278 tccp->stepsizes[bandno].mant = mant;
2282 /* Add Antonin : if scalar_derived -> compute other stepsizes */
2283 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
2284 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
2285 tccp->stepsizes[bandno].expn =
2286 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
2287 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
2288 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
2294 static void j2k_write_qcd(opj_j2k_t *j2k) {
2297 opj_cio_t *cio = j2k->cio;
2299 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
2300 lenp = cio_tell(cio);
2302 j2k_write_qcx(j2k, 0);
2303 len = cio_tell(cio) - lenp;
2304 cio_seek(cio, lenp);
2305 cio_write(cio, len, 2); /* Lqcd */
2306 cio_seek(cio, lenp + len);
2309 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
2312 static void j2k_read_qcd(opj_j2k_t *j2k) {
2315 opj_cio_t *cio = j2k->cio;
2316 opj_image_t *image = j2k->image;
2318 len = cio_read(cio, 2); /* Lqcd */
2319 pos = cio_tell(cio);
2320 for (i = 0; i < image->numcomps; i++) {
2322 j2k_read_qcx(j2k, i, len - 2);
2327 * Reads a QCD marker (Quantization defaults)
2328 * @param p_header_data the data contained in the QCD box.
2329 * @param p_j2k the jpeg2000 codec.
2330 * @param p_header_size the size of the data contained in the QCD marker.
2331 * @param p_manager the user event manager.
2333 opj_bool j2k_read_qcd_v2 (
2334 opj_j2k_v2_t *p_j2k,
2335 OPJ_BYTE * p_header_data,
2336 OPJ_UINT32 p_header_size,
2337 struct opj_event_mgr * p_manager
2341 assert(p_header_data != 00);
2342 assert(p_j2k != 00);
2343 assert(p_manager != 00);
2345 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2346 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2350 if (p_header_size != 0) {
2351 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2355 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2356 j2k_copy_tile_quantization_parameters(p_j2k);
2361 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
2364 opj_cio_t *cio = j2k->cio;
2366 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
2367 lenp = cio_tell(cio);
2369 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
2370 j2k_write_qcx(j2k, compno);
2371 len = cio_tell(cio) - lenp;
2372 cio_seek(cio, lenp);
2373 cio_write(cio, len, 2); /* Lqcc */
2374 cio_seek(cio, lenp + len);
2377 static void j2k_read_qcc(opj_j2k_t *j2k) {
2379 int numcomp = j2k->image->numcomps;
2380 opj_cio_t *cio = j2k->cio;
2382 len = cio_read(cio, 2); /* Lqcc */
2383 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
2386 if (j2k->cp->correct) {
2388 static int backup_compno = 0;
2390 /* compno is negative or larger than the number of components!!! */
2391 if ((compno < 0) || (compno >= numcomp)) {
2392 opj_event_msg(j2k->cinfo, EVT_ERROR,
2393 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2396 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2399 /* we try to correct */
2400 compno = backup_compno % numcomp;
2401 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
2402 "- setting component number to %d\n",
2406 /* keep your private count of tiles */
2409 #endif /* USE_JPWL */
2411 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
2415 * Reads a QCC marker (Quantization component)
2416 * @param p_header_data the data contained in the QCC box.
2417 * @param p_j2k the jpeg2000 codec.
2418 * @param p_header_size the size of the data contained in the QCC marker.
2419 * @param p_manager the user event manager.
2421 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
2422 OPJ_BYTE * p_header_data,
2423 OPJ_UINT32 p_header_size,
2424 struct opj_event_mgr * p_manager)
2426 OPJ_UINT32 l_num_comp,l_comp_no;
2429 assert(p_header_data != 00);
2430 assert(p_j2k != 00);
2431 assert(p_manager != 00);
2433 l_num_comp = p_j2k->m_private_image->numcomps;
2435 if (l_num_comp <= 256) {
2436 if (p_header_size < 1) {
2437 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2440 opj_read_bytes(p_header_data,&l_comp_no,1);
2445 if (p_header_size < 2) {
2446 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2449 opj_read_bytes(p_header_data,&l_comp_no,2);
2455 if (p_j2k->m_cp.correct) {
2457 static OPJ_UINT32 backup_compno = 0;
2459 /* compno is negative or larger than the number of components!!! */
2460 if ((l_comp_no < 0) || (l_comp_no >= l_num_comp)) {
2461 opj_event_msg_v2(p_manager, EVT_ERROR,
2462 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2463 l_comp_no, l_num_comp);
2465 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2468 /* we try to correct */
2469 l_comp_no = backup_compno % l_num_comp;
2470 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
2471 "- setting component number to %d\n",
2475 /* keep your private count of tiles */
2478 #endif /* USE_JPWL */
2480 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2481 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2485 if (p_header_size != 0) {
2486 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2494 static void j2k_write_poc(opj_j2k_t *j2k) {
2495 int len, numpchgs, i;
2497 int numcomps = j2k->image->numcomps;
2499 opj_cp_t *cp = j2k->cp;
2500 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2501 opj_tccp_t *tccp = &tcp->tccps[0];
2502 opj_cio_t *cio = j2k->cio;
2504 numpchgs = 1 + tcp->numpocs;
2505 cio_write(cio, J2K_MS_POC, 2); /* POC */
2506 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
2507 cio_write(cio, len, 2); /* Lpoc */
2508 for (i = 0; i < numpchgs; i++) {
2509 opj_poc_t *poc = &tcp->pocs[i];
2510 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
2511 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
2512 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
2513 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
2514 cio_write(cio, poc->resno1, 1); /* REpoc_i */
2515 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
2516 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
2517 poc->compno1 = int_min(poc->compno1, numcomps);
2518 cio_write(cio, poc->prg, 1); /* Ppoc_i */
2522 static void j2k_read_poc(opj_j2k_t *j2k) {
2523 int len, numpchgs, i, old_poc;
2525 int numcomps = j2k->image->numcomps;
2527 opj_cp_t *cp = j2k->cp;
2528 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2529 opj_cio_t *cio = j2k->cio;
2531 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
2533 len = cio_read(cio, 2); /* Lpoc */
2534 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
2536 for (i = old_poc; i < numpchgs + old_poc; i++) {
2538 poc = &tcp->pocs[i];
2539 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
2540 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
2541 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
2542 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
2543 poc->compno1 = int_min(
2544 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
2545 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
2548 tcp->numpocs = numpchgs + old_poc - 1;
2552 * Reads a POC marker (Progression Order Change)
2554 * @param p_header_data the data contained in the POC box.
2555 * @param p_j2k the jpeg2000 codec.
2556 * @param p_header_size the size of the data contained in the POC marker.
2557 * @param p_manager the user event manager.
2559 opj_bool j2k_read_poc_v2 (
2560 opj_j2k_v2_t *p_j2k,
2561 OPJ_BYTE * p_header_data,
2562 OPJ_UINT32 p_header_size,
2563 struct opj_event_mgr * p_manager)
2565 OPJ_UINT32 i, l_nb_comp, l_tmp;
2566 opj_image_t * l_image = 00;
2567 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
2568 OPJ_UINT32 l_chunk_size, l_comp_room;
2570 opj_cp_v2_t *l_cp = 00;
2571 opj_tcp_v2_t *l_tcp = 00;
2572 opj_poc_t *l_current_poc = 00;
2575 assert(p_header_data != 00);
2576 assert(p_j2k != 00);
2577 assert(p_manager != 00);
2579 l_image = p_j2k->m_private_image;
2580 l_nb_comp = l_image->numcomps;
2581 if (l_nb_comp <= 256) {
2587 l_chunk_size = 5 + 2 * l_comp_room;
2588 l_current_poc_nb = p_header_size / l_chunk_size;
2589 l_current_poc_remaining = p_header_size % l_chunk_size;
2591 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
2592 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
2596 l_cp = &(p_j2k->m_cp);
2597 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2598 &l_cp->tcps[p_j2k->m_current_tile_number] :
2599 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2600 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
2601 l_current_poc_nb += l_old_poc_nb;
2603 assert(l_current_poc_nb < 32);
2605 /* now poc is in use.*/
2608 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
2609 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
2610 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
2612 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
2613 p_header_data+=l_comp_room;
2614 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
2616 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
2618 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
2619 p_header_data+=l_comp_room;
2620 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
2622 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
2623 /* make sure comp is in acceptable bounds */
2624 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
2628 l_tcp->numpocs = l_current_poc_nb - 1;
2632 static void j2k_read_crg(opj_j2k_t *j2k) {
2633 int len, i, Xcrg_i, Ycrg_i;
2635 opj_cio_t *cio = j2k->cio;
2636 int numcomps = j2k->image->numcomps;
2638 len = cio_read(cio, 2); /* Lcrg */
2639 for (i = 0; i < numcomps; i++) {
2640 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
2641 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
2646 * Reads a CRG marker (Component registration)
2648 * @param p_header_data the data contained in the TLM box.
2649 * @param p_j2k the jpeg2000 codec.
2650 * @param p_header_size the size of the data contained in the TLM marker.
2651 * @param p_manager the user event manager.
2653 opj_bool j2k_read_crg_v2 (
2654 opj_j2k_v2_t *p_j2k,
2655 OPJ_BYTE * p_header_data,
2656 OPJ_UINT32 p_header_size,
2657 struct opj_event_mgr * p_manager
2660 OPJ_UINT32 l_nb_comp;
2662 assert(p_header_data != 00);
2663 assert(p_j2k != 00);
2664 assert(p_manager != 00);
2666 l_nb_comp = p_j2k->m_private_image->numcomps;
2668 if (p_header_size != l_nb_comp *4) {
2669 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
2672 /* Do not care of this at the moment since only local variables are set here */
2675 (i = 0; i < l_nb_comp; ++i)
2677 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
2679 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
2686 static void j2k_read_tlm(opj_j2k_t *j2k) {
2687 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
2688 long int Ttlm_i, Ptlm_i;
2690 opj_cio_t *cio = j2k->cio;
2692 len = cio_read(cio, 2); /* Ltlm */
2693 Ztlm = cio_read(cio, 1); /* Ztlm */
2694 Stlm = cio_read(cio, 1); /* Stlm */
2695 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
2696 SP = (Stlm >> 6) & 0x01;
2697 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
2698 for (i = 0; i < tile_tlm; i++) {
2699 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
2700 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
2705 * Reads a TLM marker (Tile Length Marker)
2707 * @param p_header_data the data contained in the TLM box.
2708 * @param p_j2k the jpeg2000 codec.
2709 * @param p_header_size the size of the data contained in the TLM marker.
2710 * @param p_manager the user event manager.
2712 opj_bool j2k_read_tlm_v2 (
2713 opj_j2k_v2_t *p_j2k,
2714 OPJ_BYTE * p_header_data,
2715 OPJ_UINT32 p_header_size,
2716 struct opj_event_mgr * p_manager
2719 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
2721 assert(p_header_data != 00);
2722 assert(p_j2k != 00);
2723 assert(p_manager != 00);
2725 if (p_header_size < 2) {
2726 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2731 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
2733 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
2736 l_ST = ((l_Stlm >> 4) & 0x3);
2737 l_SP = (l_Stlm >> 6) & 0x1;
2739 l_Ptlm_size = (l_SP + 1) * 2;
2740 l_quotient = l_Ptlm_size + l_ST;
2742 l_tot_num_tp = p_header_size / l_quotient;
2743 l_tot_num_tp_remaining = p_header_size % l_quotient;
2745 if (l_tot_num_tp_remaining != 0) {
2746 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2749 /* FIXME Do not care of this at the moment since only local variables are set here */
2752 (i = 0; i < l_tot_num_tp; ++i)
2754 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
2755 p_header_data += l_ST;
2756 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
2757 p_header_data += l_Ptlm_size;
2762 static void j2k_read_plm(opj_j2k_t *j2k) {
2763 int len, i, Zplm, Nplm, add, packet_len = 0;
2765 opj_cio_t *cio = j2k->cio;
2767 len = cio_read(cio, 2); /* Lplm */
2768 Zplm = cio_read(cio, 1); /* Zplm */
2771 Nplm = cio_read(cio, 4); /* Nplm */
2773 for (i = Nplm; i > 0; i--) {
2774 add = cio_read(cio, 1);
2776 packet_len = (packet_len << 7) + add; /* Iplm_ij */
2777 if ((add & 0x80) == 0) {
2788 * Reads a PLM marker (Packet length, main header marker)
2790 * @param p_header_data the data contained in the TLM box.
2791 * @param p_j2k the jpeg2000 codec.
2792 * @param p_header_size the size of the data contained in the TLM marker.
2793 * @param p_manager the user event manager.
2795 opj_bool j2k_read_plm_v2 (
2796 opj_j2k_v2_t *p_j2k,
2797 OPJ_BYTE * p_header_data,
2798 OPJ_UINT32 p_header_size,
2799 struct opj_event_mgr * p_manager
2803 assert(p_header_data != 00);
2804 assert(p_j2k != 00);
2805 assert(p_manager != 00);
2807 if (p_header_size < 1) {
2808 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2811 /* Do not care of this at the moment since only local variables are set here */
2813 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
2820 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
2822 p_header_size -= (1+l_Nplm);
2826 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2830 (i = 0; i < l_Nplm; ++i)
2832 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
2834 // take only the last seven bytes
2835 l_packet_len |= (l_tmp & 0x7f);
2843 // store packet length and proceed to next packet
2850 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2858 static void j2k_read_plt(opj_j2k_t *j2k) {
2859 int len, i, Zplt, packet_len = 0, add;
2861 opj_cio_t *cio = j2k->cio;
2863 len = cio_read(cio, 2); /* Lplt */
2864 Zplt = cio_read(cio, 1); /* Zplt */
2865 for (i = len - 3; i > 0; i--) {
2866 add = cio_read(cio, 1);
2867 packet_len = (packet_len << 7) + add; /* Iplt_i */
2868 if ((add & 0x80) == 0) {
2876 * Reads a PLT marker (Packet length, tile-part header)
2878 * @param p_header_data the data contained in the PLT box.
2879 * @param p_j2k the jpeg2000 codec.
2880 * @param p_header_size the size of the data contained in the PLT marker.
2881 * @param p_manager the user event manager.
2883 opj_bool j2k_read_plt_v2 (
2884 opj_j2k_v2_t *p_j2k,
2885 OPJ_BYTE * p_header_data,
2886 OPJ_UINT32 p_header_size,
2887 struct opj_event_mgr * p_manager
2890 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
2893 assert(p_header_data != 00);
2894 assert(p_j2k != 00);
2895 assert(p_manager != 00);
2897 if (p_header_size < 1) {
2898 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2902 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
2906 for (i = 0; i < p_header_size; ++i) {
2907 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
2909 // take only the last seven bytes
2910 l_packet_len |= (l_tmp & 0x7f);
2915 // store packet length and proceed to next packet
2920 if (l_packet_len != 0) {
2921 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2928 static void j2k_read_ppm(opj_j2k_t *j2k) {
2929 int len, Z_ppm, i, j;
2932 opj_cp_t *cp = j2k->cp;
2933 opj_cio_t *cio = j2k->cio;
2935 len = cio_read(cio, 2);
2938 Z_ppm = cio_read(cio, 1); /* Z_ppm */
2941 if (cp->ppm_previous == 0) {
2942 N_ppm = cio_read(cio, 4); /* N_ppm */
2945 N_ppm = cp->ppm_previous;
2948 if (Z_ppm == 0) { /* First PPM marker */
2949 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
2950 cp->ppm_data_first = cp->ppm_data;
2951 cp->ppm_len = N_ppm;
2952 } else { /* NON-first PPM marker */
2953 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
2956 /* this memory allocation check could be done even in non-JPWL cases */
2958 if (!cp->ppm_data) {
2959 opj_event_msg(j2k->cinfo, EVT_ERROR,
2960 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
2962 if (!JPWL_ASSUME || JPWL_ASSUME) {
2963 opj_free(cp->ppm_data);
2964 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2971 cp->ppm_data_first = cp->ppm_data;
2972 cp->ppm_len = N_ppm + cp->ppm_store;
2974 for (i = N_ppm; i > 0; i--) { /* Read packet header */
2975 cp->ppm_data[j] = cio_read(cio, 1);
2979 break; /* Case of non-finished packet header in present marker but finished in next one */
2981 cp->ppm_previous = i - 1;
2986 * Reads a PPM marker (Packed packet headers, main header)
2988 * @param p_header_data the data contained in the POC box.
2989 * @param p_j2k the jpeg2000 codec.
2990 * @param p_header_size the size of the data contained in the POC marker.
2991 * @param p_manager the user event manager.
2993 opj_bool j2k_read_ppm_v2 (
2994 opj_j2k_v2_t *p_j2k,
2995 OPJ_BYTE * p_header_data,
2996 OPJ_UINT32 p_header_size,
2997 struct opj_event_mgr * p_manager
3001 opj_cp_v2_t *l_cp = 00;
3002 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3005 assert(p_header_data != 00);
3006 assert(p_j2k != 00);
3007 assert(p_manager != 00);
3009 if (p_header_size < 1) {
3010 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3014 l_cp = &(p_j2k->m_cp);
3017 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3023 if (p_header_size < 4) {
3024 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3028 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3032 /* First PPM marker: Initialization */
3033 l_cp->ppm_len = l_N_ppm;
3034 l_cp->ppm_data_size = 0;
3036 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3037 if (l_cp->ppm_buffer == 00) {
3038 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3041 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3043 l_cp->ppm_data = l_cp->ppm_buffer;
3047 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3048 if (p_header_size >= 4) {
3050 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3053 l_cp->ppm_len += l_N_ppm ;
3055 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3056 if (l_cp->ppm_buffer == 00) {
3057 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3060 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3062 l_cp->ppm_data = l_cp->ppm_buffer;
3069 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3071 if (l_remaining_data <= p_header_size) {
3072 /* we must store less information than available in the packet */
3073 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3074 l_cp->ppm_data_size = l_cp->ppm_len;
3075 p_header_size -= l_remaining_data;
3076 p_header_data += l_remaining_data;
3079 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3080 l_cp->ppm_data_size += p_header_size;
3081 p_header_data += p_header_size;
3093 * Reads a PPM marker (Packed packet headers, main header)
3095 * @param p_header_data the data contained in the POC box.
3096 * @param p_j2k the jpeg2000 codec.
3097 * @param p_header_size the size of the data contained in the POC marker.
3098 * @param p_manager the user event manager.
3100 opj_bool j2k_read_ppm_v3 (
3101 opj_j2k_v2_t *p_j2k,
3102 OPJ_BYTE * p_header_data,
3103 OPJ_UINT32 p_header_size,
3104 struct opj_event_mgr * p_manager
3107 opj_cp_v2_t *l_cp = 00;
3108 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3111 assert(p_header_data != 00);
3112 assert(p_j2k != 00);
3113 assert(p_manager != 00);
3115 // Minimum size of PPM marker is equal to the size of Zppm element
3116 if (p_header_size < 1) {
3117 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3121 l_cp = &(p_j2k->m_cp);
3124 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3130 // We need now at least the Nppm^0 element
3131 if (p_header_size < 4) {
3132 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3136 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3140 /* First PPM marker: Initialization */
3141 l_cp->ppm_len = l_N_ppm;
3142 l_cp->ppm_data_read = 0;
3144 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3145 if (l_cp->ppm_data == 00) {
3146 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3149 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3151 l_cp->ppm_data_current = l_cp->ppm_data;
3153 //l_cp->ppm_data = l_cp->ppm_buffer;
3156 if (p_header_size < 4) {
3157 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3161 // Uncompleted Ippm series in the previous PPM marker?
3162 if (l_cp->ppm_data_read < l_cp->ppm_len) {
3163 // Get the place where add the remaining Ippm series
3164 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3165 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3168 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3172 // Increase the size of ppm_data to add the new Ippm series
3173 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3175 // Keep the position of the place where concatenate the new series
3176 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3177 l_cp->ppm_len += l_N_ppm;
3182 l_remaining_data = p_header_size;
3184 while (l_remaining_data >= l_N_ppm) {
3185 // read a complete Ippm series
3186 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3187 p_header_size -= l_N_ppm;
3188 p_header_data += l_N_ppm;
3190 l_cp->ppm_data_read += l_N_ppm; // Increase the number of data read
3194 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
3199 l_remaining_data = p_header_size;
3203 l_remaining_data = p_header_size;
3205 // Next Ippm series is a complete series ?
3206 if (l_remaining_data > l_N_ppm) {
3207 // Increase the size of ppm_data to add the new Ippm series
3208 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3210 // Keep the position of the place where concatenate the new series
3211 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3212 l_cp->ppm_len += l_N_ppm;
3217 // Need to read an incomplete Ippm series
3218 if (l_remaining_data) {
3219 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3221 // Keep the position of the place where concatenate the new series
3222 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3223 l_cp->ppm_len += l_N_ppm;
3225 // Read incomplete Ippm series
3226 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3227 p_header_size -= l_remaining_data;
3228 p_header_data += l_remaining_data;
3230 l_cp->ppm_data_read += l_remaining_data; // Increase the number of data read
3235 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3236 if (p_header_size >= 4) {
3238 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3241 l_cp->ppm_len += l_N_ppm ;
3243 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3244 if (l_cp->ppm_buffer == 00) {
3245 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3248 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3250 l_cp->ppm_data = l_cp->ppm_buffer;
3257 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3259 if (l_remaining_data <= p_header_size) {
3260 /* we must store less information than available in the packet */
3261 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3262 l_cp->ppm_data_size = l_cp->ppm_len;
3263 p_header_size -= l_remaining_data;
3264 p_header_data += l_remaining_data;
3267 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3268 l_cp->ppm_data_size += p_header_size;
3269 p_header_data += p_header_size;
3278 static void j2k_read_ppt(opj_j2k_t *j2k) {
3279 int len, Z_ppt, i, j = 0;
3281 opj_cp_t *cp = j2k->cp;
3282 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
3283 opj_cio_t *cio = j2k->cio;
3285 len = cio_read(cio, 2);
3286 Z_ppt = cio_read(cio, 1);
3288 if (Z_ppt == 0) { /* First PPT marker */
3289 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
3290 tcp->ppt_data_first = tcp->ppt_data;
3292 tcp->ppt_len = len - 3;
3293 } else { /* NON-first PPT marker */
3294 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
3295 tcp->ppt_data_first = tcp->ppt_data;
3296 tcp->ppt_len = len - 3 + tcp->ppt_store;
3299 for (i = len - 3; i > 0; i--) {
3300 tcp->ppt_data[j] = cio_read(cio, 1);
3307 * Reads a PPT marker (Packed packet headers, tile-part header)
3309 * @param p_header_data the data contained in the PPT box.
3310 * @param p_j2k the jpeg2000 codec.
3311 * @param p_header_size the size of the data contained in the PPT marker.
3312 * @param p_manager the user event manager.
3314 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
3315 OPJ_BYTE * p_header_data,
3316 OPJ_UINT32 p_header_size,
3317 struct opj_event_mgr * p_manager )
3319 opj_cp_v2_t *l_cp = 00;
3320 opj_tcp_v2_t *l_tcp = 00;
3324 assert(p_header_data != 00);
3325 assert(p_j2k != 00);
3326 assert(p_manager != 00);
3328 /* We need to have the Z_ppt element at minimum */
3329 if (p_header_size < 1) {
3330 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3334 l_cp = &(p_j2k->m_cp);
3336 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");
3340 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3343 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3347 /* Allocate buffer to read the packet header */
3349 /* First PPT marker */
3350 l_tcp->ppt_data_size = 0;
3351 l_tcp->ppt_len = p_header_size;
3353 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3354 if (l_tcp->ppt_buffer == 00) {
3355 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3358 l_tcp->ppt_data = l_tcp->ppt_buffer;
3360 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3363 l_tcp->ppt_len += p_header_size;
3365 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3366 if (l_tcp->ppt_buffer == 00) {
3367 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3370 l_tcp->ppt_data = l_tcp->ppt_buffer;
3372 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3375 /* Read packet header from buffer */
3376 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3378 l_tcp->ppt_data_size += p_header_size;
3383 static void j2k_write_tlm(opj_j2k_t *j2k){
3385 opj_cio_t *cio = j2k->cio;
3386 j2k->tlm_start = cio_tell(cio);
3387 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
3388 lenp = 4 + (5*j2k->totnum_tp);
3389 cio_write(cio,lenp,2); /* Ltlm */
3390 cio_write(cio, 0,1); /* Ztlm=0*/
3391 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3392 cio_skip(cio,5*j2k->totnum_tp);
3395 static void j2k_write_sot(opj_j2k_t *j2k) {
3398 opj_cio_t *cio = j2k->cio;
3400 j2k->sot_start = cio_tell(cio);
3401 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
3402 lenp = cio_tell(cio);
3403 cio_skip(cio, 2); /* Lsot (further) */
3404 cio_write(cio, j2k->curtileno, 2); /* Isot */
3405 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
3406 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
3407 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
3408 len = cio_tell(cio) - lenp;
3409 cio_seek(cio, lenp);
3410 cio_write(cio, len, 2); /* Lsot */
3411 cio_seek(cio, lenp + len);
3415 /* update markers struct */
3416 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
3417 #endif /* USE_JPWL */
3420 if( j2k->cstr_info && j2k->cur_tp_num==0){
3421 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
3425 static void j2k_read_sot(opj_j2k_t *j2k) {
3426 int len, tileno, totlen, partno, numparts, i;
3427 opj_tcp_t *tcp = NULL;
3430 opj_cp_t *cp = j2k->cp;
3431 opj_cio_t *cio = j2k->cio;
3433 len = cio_read(cio, 2);
3434 tileno = cio_read(cio, 2);
3437 if (j2k->cp->correct) {
3439 static int backup_tileno = 0;
3441 /* tileno is negative or larger than the number of tiles!!! */
3442 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3443 opj_event_msg(j2k->cinfo, EVT_ERROR,
3444 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3445 tileno, (cp->tw * cp->th));
3447 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3450 /* we try to correct */
3451 tileno = backup_tileno;
3452 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3453 "- setting tile number to %d\n",
3457 /* keep your private count of tiles */
3460 #endif /* USE_JPWL */
3462 if (cp->tileno_size == 0) {
3463 cp->tileno[cp->tileno_size] = tileno;
3467 while (i < cp->tileno_size && status == 0) {
3468 status = cp->tileno[i] == tileno ? 1 : 0;
3472 cp->tileno[cp->tileno_size] = tileno;
3477 totlen = cio_read(cio, 4);
3480 if (j2k->cp->correct) {
3482 /* totlen is negative or larger than the bytes left!!! */
3483 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
3484 opj_event_msg(j2k->cinfo, EVT_ERROR,
3485 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3486 totlen, cio_numbytesleft(cio) + 8);
3488 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3491 /* we try to correct */
3493 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3494 "- setting Psot to %d => assuming it is the last tile\n",
3499 #endif /* USE_JPWL */
3502 totlen = cio_numbytesleft(cio) + 8;
3504 partno = cio_read(cio, 1);
3505 numparts = cio_read(cio, 1);
3507 if (partno >= numparts) {
3508 opj_event_msg(j2k->cinfo, EVT_WARNING, "SOT marker inconsistency in tile %d: tile-part index greater (%d) than number of tile-parts (%d)\n", tileno, partno, numparts);
3509 numparts = partno+1;
3512 j2k->curtileno = tileno;
3513 j2k->cur_tp_num = partno;
3514 j2k->eot = cio_getbp(cio) - 12 + totlen;
3515 j2k->state = J2K_STATE_TPH;
3516 tcp = &cp->tcps[j2k->curtileno];
3519 if (j2k->cstr_info) {
3522 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
3523 j2k->cstr_info->tile[tileno].tileno = tileno;
3524 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
3525 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3527 j2k->cstr_info->tile[tileno].end_pos += totlen;
3529 j2k->cstr_info->tile[tileno].num_tps = numparts;
3531 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
3533 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, 10 * sizeof(opj_tp_info_t)); /* Fixme (10)*/
3534 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
3535 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3536 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3539 if (tcp->first == 1) {
3540 /* Initialization PPT */
3541 opj_tccp_t *tmp = tcp->tccps;
3542 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
3544 tcp->ppt_data = NULL;
3545 tcp->ppt_data_first = NULL;
3548 for (i = 0; i < j2k->image->numcomps; i++) {
3549 tcp->tccps[i] = j2k->default_tcp->tccps[i];
3551 cp->tcps[j2k->curtileno].first = 0;
3556 * Reads a PPT marker (Packed packet headers, tile-part header)
3558 * @param p_header_data the data contained in the PPT box.
3559 * @param p_j2k the jpeg2000 codec.
3560 * @param p_header_size the size of the data contained in the PPT marker.
3561 * @param p_manager the user event manager.
3563 opj_bool j2k_read_sot_v2 (
3564 opj_j2k_v2_t *p_j2k,
3565 OPJ_BYTE * p_header_data,
3566 OPJ_UINT32 p_header_size,
3567 struct opj_event_mgr * p_manager
3571 opj_cp_v2_t *l_cp = 00;
3572 opj_tcp_v2_t *l_tcp = 00;
3573 OPJ_UINT32 l_tot_len, l_num_parts = 0;
3574 OPJ_UINT32 l_current_part;
3575 OPJ_UINT32 l_tile_x,l_tile_y;
3578 assert(p_header_data != 00);
3579 assert(p_j2k != 00);
3580 assert(p_manager != 00);
3582 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
3583 if (p_header_size != 8) {
3584 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3588 l_cp = &(p_j2k->m_cp);
3589 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
3592 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3593 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
3594 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
3597 if (l_cp->correct) {
3599 int tileno = p_j2k->m_current_tile_number;
3600 static int backup_tileno = 0;
3602 /* tileno is negative or larger than the number of tiles!!! */
3603 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
3604 opj_event_msg_v2(p_manager, EVT_ERROR,
3605 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3606 tileno, (l_cp->tw * l_cp->th));
3608 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3611 /* we try to correct */
3612 tileno = backup_tileno;
3613 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3614 "- setting tile number to %d\n",
3618 /* keep your private count of tiles */
3621 #endif /* USE_JPWL */
3623 /* look for the tile in the list of already processed tile (in parts). */
3624 /* Optimization possible here with a more complex data structure and with the removing of tiles */
3625 /* since the time taken by this function can only grow at the time */
3627 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
3630 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
3631 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
3633 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
3639 if (l_cp->correct) {
3641 /* totlen is negative or larger than the bytes left!!! */
3642 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))) { */
3643 opj_event_msg_v2(p_manager, EVT_ERROR,
3644 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3645 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
3647 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3650 /* we try to correct */
3652 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3653 "- setting Psot to %d => assuming it is the last tile\n",
3657 #endif /* USE_JPWL */
3659 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
3661 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
3662 "we assuming it is the last tile-part of the codestream.\n");
3663 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3666 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
3669 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
3672 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
3673 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
3674 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
3675 if (l_tcp->m_nb_tile_parts) {
3676 if (l_current_part >= l_tcp->m_nb_tile_parts){
3677 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
3678 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
3679 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3683 l_tcp->m_nb_tile_parts = l_num_parts;
3686 /* If know the number of tile part header we will check if we didn't read the last*/
3687 if (l_tcp->m_nb_tile_parts) {
3688 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
3689 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
3693 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
3694 /* Keep the size of data to skip after this marker */
3695 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
3698 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
3699 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
3702 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
3704 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
3705 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
3706 p_j2k->m_specific_param.m_decoder.m_skip_data =
3707 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
3708 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
3709 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
3710 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
3713 p_j2k->m_specific_param.m_decoder.m_skip_data =
3714 (p_j2k->m_current_tile_number != p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
3717 if (p_j2k->cstr_index)
3719 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
3720 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
3722 if (l_num_parts != 0){
3723 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
3724 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
3727 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
3728 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3729 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
3731 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3732 (opj_tp_index_t*)opj_realloc(
3733 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
3734 l_num_parts* sizeof(opj_tp_index_t));
3737 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
3739 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
3740 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
3741 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3742 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
3743 sizeof(opj_tp_index_t));
3746 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
3747 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
3748 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3749 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
3750 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
3751 * sizeof(opj_tp_index_t));
3760 /* 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 */
3761 /* if (p_j2k->cstr_info) {
3764 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
3767 p_j2k->cstr_info->tile[tileno].tileno = tileno;
3768 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
3769 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3770 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
3773 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
3776 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
3780 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
3783 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
3784 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3785 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3790 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
3793 opj_tcp_t *tcp = NULL;
3794 opj_codestream_info_t *cstr_info = NULL;
3796 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
3797 opj_cp_t *cp = j2k->cp;
3798 opj_cio_t *cio = j2k->cio;
3800 tcd->tp_num = j2k->tp_num ;
3801 tcd->cur_tp_num = j2k->cur_tp_num;
3803 cio_write(cio, J2K_MS_SOD, 2);
3805 if( j2k->cstr_info && j2k->cur_tp_num==0){
3806 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
3809 if (j2k->curtileno == 0) {
3810 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
3814 cstr_info = j2k->cstr_info;
3816 if (!j2k->cur_tp_num ) {
3817 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3818 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
3821 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
3822 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
3826 /* update markers struct */
3827 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
3828 #endif /* USE_JPWL */
3833 tcp = &cp->tcps[j2k->curtileno];
3834 for (layno = 0; layno < tcp->numlayers; layno++) {
3835 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
3836 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
3837 } else if (tcp->rates[layno]) {
3838 tcp->rates[layno]=1;
3841 if(j2k->cur_tp_num == 0){
3842 tcd->tcd_image->tiles->packno = 0;
3844 cstr_info->packno = 0;
3847 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
3849 /* Writing Psot in SOT marker */
3850 totlen = cio_tell(cio) + l - j2k->sot_start;
3851 cio_seek(cio, j2k->sot_start + 6);
3852 cio_write(cio, totlen, 4);
3853 cio_seek(cio, j2k->sot_start + totlen);
3854 /* Writing Ttlm and Ptlm in TLM marker */
3856 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
3857 cio_write(cio, j2k->curtileno, 1);
3858 cio_write(cio, totlen, 4);
3860 cio_seek(cio, j2k->sot_start + totlen);
3863 static void j2k_read_sod(opj_j2k_t *j2k) {
3864 int len, truncate = 0, i;
3865 unsigned char *data = NULL, *data_ptr = NULL;
3867 opj_cio_t *cio = j2k->cio;
3868 int curtileno = j2k->curtileno;
3871 if (j2k->cstr_info) {
3872 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
3873 cio_tell(cio) + j2k->pos_correction - 1;
3874 if (j2k->cur_tp_num == 0)
3875 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3876 j2k->cstr_info->packno = 0;
3879 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
3881 if (len == cio_numbytesleft(cio) + 1) {
3882 truncate = 1; /* Case of a truncate codestream */
3885 data = j2k->tile_data[curtileno];
3886 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
3888 data_ptr = data + j2k->tile_len[curtileno];
3889 for (i = 0; i < len; i++) {
3890 data_ptr[i] = cio_read(cio, 1);
3893 j2k->tile_len[curtileno] += len;
3894 j2k->tile_data[curtileno] = data;
3897 j2k->state = J2K_STATE_TPHSOT;
3899 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
3905 * Reads a SOD marker (Start Of Data)
3907 * @param p_header_data the data contained in the SOD box.
3908 * @param p_j2k the jpeg2000 codec.
3909 * @param p_header_size the size of the data contained in the SOD marker.
3910 * @param p_manager the user event manager.
3912 opj_bool j2k_read_sod_v2 (
3913 opj_j2k_v2_t *p_j2k,
3914 struct opj_stream_private *p_stream,
3915 struct opj_event_mgr * p_manager
3918 OPJ_UINT32 l_current_read_size;
3919 opj_codestream_index_t * l_cstr_index = 00;
3920 OPJ_BYTE ** l_current_data = 00;
3921 opj_tcp_v2_t * l_tcp = 00;
3922 OPJ_UINT32 * l_tile_len = 00;
3925 assert(p_j2k != 00);
3926 assert(p_manager != 00);
3927 assert(p_stream != 00);
3929 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
3931 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
3932 // opj_stream_get_number_byte_left returns OPJ_OFF_T
3933 // but we are in the last tile part,
3934 // so its result will fit on OPJ_UINT32 unless we find
3935 // a file with a single tile part of more than 4 GB...
3936 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
3939 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
3941 l_current_data = &(l_tcp->m_data);
3942 l_tile_len = &l_tcp->m_data_size;
3944 if (! *l_current_data) {
3945 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
3948 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
3951 if (*l_current_data == 00) {
3952 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
3958 l_cstr_index = p_j2k->cstr_index;
3960 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
3962 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
3963 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
3965 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
3966 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
3968 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
3972 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
3974 /*l_cstr_index->packno = 0;*/
3977 l_current_read_size = opj_stream_read_data( p_stream,
3978 *l_current_data + *l_tile_len,
3979 p_j2k->m_specific_param.m_decoder.m_sot_length,
3982 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
3983 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
3986 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
3989 *l_tile_len += l_current_read_size;
3995 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
3996 opj_cp_t *cp = j2k->cp;
3997 opj_tcp_t *tcp = &cp->tcps[tileno];
3998 opj_cio_t *cio = j2k->cio;
3999 int numcomps = j2k->image->numcomps;
4001 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
4002 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
4003 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
4004 cio_write(cio, 0, 1); /* Srgn */
4005 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
4008 static void j2k_read_rgn(opj_j2k_t *j2k) {
4009 int len, compno, roisty;
4011 opj_cp_t *cp = j2k->cp;
4012 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
4013 opj_cio_t *cio = j2k->cio;
4014 int numcomps = j2k->image->numcomps;
4016 len = cio_read(cio, 2); /* Lrgn */
4017 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
4018 roisty = cio_read(cio, 1); /* Srgn */
4021 if (j2k->cp->correct) {
4022 /* totlen is negative or larger than the bytes left!!! */
4023 if (compno >= numcomps) {
4024 opj_event_msg(j2k->cinfo, EVT_ERROR,
4025 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4027 if (!JPWL_ASSUME || JPWL_ASSUME) {
4028 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4033 #endif /* USE_JPWL */
4035 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
4038 static void j2k_write_eoc(opj_j2k_t *j2k) {
4039 opj_cio_t *cio = j2k->cio;
4040 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
4041 cio_write(cio, J2K_MS_EOC, 2);
4045 /* update markers struct */
4046 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
4047 #endif /* USE_JPWL */
4052 * Reads a RGN marker (Region Of Interest)
4054 * @param p_header_data the data contained in the POC box.
4055 * @param p_j2k the jpeg2000 codec.
4056 * @param p_header_size the size of the data contained in the POC marker.
4057 * @param p_manager the user event manager.
4059 opj_bool j2k_read_rgn_v2 (
4060 opj_j2k_v2_t *p_j2k,
4061 OPJ_BYTE * p_header_data,
4062 OPJ_UINT32 p_header_size,
4063 struct opj_event_mgr * p_manager
4066 OPJ_UINT32 l_nb_comp;
4067 opj_image_t * l_image = 00;
4069 opj_cp_v2_t *l_cp = 00;
4070 opj_tcp_v2_t *l_tcp = 00;
4071 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4074 assert(p_header_data != 00);
4075 assert(p_j2k != 00);
4076 assert(p_manager != 00);
4078 l_image = p_j2k->m_private_image;
4079 l_nb_comp = l_image->numcomps;
4081 if (l_nb_comp <= 256) {
4086 if (p_header_size != 2 + l_comp_room) {
4087 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4091 l_cp = &(p_j2k->m_cp);
4092 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4093 &l_cp->tcps[p_j2k->m_current_tile_number] :
4094 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4096 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4097 p_header_data+=l_comp_room;
4098 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4102 if (l_cp->correct) {
4103 /* totlen is negative or larger than the bytes left!!! */
4104 if (l_comp_room >= l_nb_comp) {
4105 opj_event_msg_v2(p_manager, EVT_ERROR,
4106 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4107 l_comp_room, l_nb_comp);
4108 if (!JPWL_ASSUME || JPWL_ASSUME) {
4109 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4114 #endif /* USE_JPWL */
4116 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4123 static void j2k_read_eoc(opj_j2k_t *j2k) {
4127 /* if packets should be decoded */
4128 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
4129 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
4130 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
4131 for (i = 0; i < j2k->cp->tileno_size; i++) {
4132 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
4133 tileno = j2k->cp->tileno[i];
4134 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
4135 opj_free(j2k->tile_data[tileno]);
4136 j2k->tile_data[tileno] = NULL;
4137 tcd_free_decode_tile(tcd, i);
4138 if (success == OPJ_FALSE) {
4139 j2k->state |= J2K_STATE_ERR;
4143 tcd_free_decode(tcd);
4146 /* if packets should not be decoded */
4148 for (i = 0; i < j2k->cp->tileno_size; i++) {
4149 tileno = j2k->cp->tileno[i];
4150 opj_free(j2k->tile_data[tileno]);
4151 j2k->tile_data[tileno] = NULL;
4154 if (j2k->state & J2K_STATE_ERR)
4155 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
4157 j2k->state = J2K_STATE_MT;
4161 * Reads a EOC marker (End Of Codestream)
4163 * @param p_header_data the data contained in the SOD box.
4164 * @param p_j2k the jpeg2000 codec.
4165 * @param p_header_size the size of the data contained in the SOD marker.
4166 * @param p_manager the user event manager.
4168 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
4169 struct opj_stream_private *p_stream,
4170 struct opj_event_mgr * p_manager )
4173 opj_tcd_v2_t * l_tcd = 00;
4174 OPJ_UINT32 l_nb_tiles;
4175 opj_tcp_v2_t * l_tcp = 00;
4179 assert(p_j2k != 00);
4180 assert(p_manager != 00);
4181 assert(p_stream != 00);
4183 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4184 l_tcp = p_j2k->m_cp.tcps;
4186 l_tcd = tcd_create_v2(OPJ_TRUE);
4188 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4192 for (i = 0; i < l_nb_tiles; ++i) {
4193 if (l_tcp->m_data) {
4194 if (! tcd_init_decode_tile(l_tcd, i)) {
4195 tcd_destroy_v2(l_tcd);
4196 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4200 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4204 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4209 j2k_tcp_destroy(l_tcp);
4213 tcd_destroy_v2(l_tcd);
4217 typedef struct opj_dec_mstabent {
4220 /** value of the state when the marker can appear */
4222 /** action linked to the marker */
4223 void (*handler) (opj_j2k_t *j2k);
4224 } opj_dec_mstabent_t;
4226 opj_dec_mstabent_t j2k_dec_mstab[] = {
4227 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
4228 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
4229 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
4230 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
4231 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
4232 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
4233 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
4234 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
4235 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
4236 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
4237 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
4238 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
4239 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
4240 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
4241 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
4242 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
4244 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
4245 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
4248 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
4249 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
4250 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
4251 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
4252 #endif /* USE_JPWL */
4254 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
4255 {J2K_MS_INSEC, 0, j2k_read_insec},
4256 #endif /* USE_JPSEC */
4258 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
4261 static void j2k_read_unk(opj_j2k_t *j2k) {
4262 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
4265 if (j2k->cp->correct) {
4267 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
4268 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4269 id = cio_read(j2k->cio, 2);
4270 opj_event_msg(j2k->cinfo, EVT_ERROR,
4271 "JPWL: really don't know this marker %x\n",
4274 opj_event_msg(j2k->cinfo, EVT_ERROR,
4275 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
4278 /* OK, activate this at your own risk!!! */
4279 /* we look for the marker at the minimum hamming distance from this */
4280 while (j2k_dec_mstab[m].id) {
4282 /* 1's where they differ */
4283 tmp_id = j2k_dec_mstab[m].id ^ id;
4285 /* compute the hamming distance between our id and the current */
4287 for (i = 0; i < 16; i++) {
4288 if ((tmp_id >> i) & 0x0001) {
4293 /* if current distance is smaller, set the minimum */
4294 if (cur_dist < min_dist) {
4295 min_dist = cur_dist;
4296 min_id = j2k_dec_mstab[m].id;
4299 /* jump to the next marker */
4303 /* do we substitute the marker? */
4304 if (min_dist < JPWL_MAXIMUM_HAMMING) {
4305 opj_event_msg(j2k->cinfo, EVT_ERROR,
4306 "- marker %x is at distance %d from the read %x\n",
4307 min_id, min_dist, id);
4308 opj_event_msg(j2k->cinfo, EVT_ERROR,
4309 "- trying to substitute in place and crossing fingers!\n");
4310 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4311 cio_write(j2k->cio, min_id, 2);
4314 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4319 #endif /* USE_JPWL */
4324 * Reads an unknown marker
4326 * @param p_stream the stream object to read from.
4327 * @param p_j2k the jpeg2000 codec.
4328 * @param p_manager the user event manager.
4330 * @return true if the marker could be deduced.
4332 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
4333 struct opj_stream_private *p_stream,
4334 OPJ_UINT32 *output_marker,
4335 struct opj_event_mgr * p_manager
4338 OPJ_UINT32 l_unknown_marker;
4339 const opj_dec_memory_marker_handler_t * l_marker_handler;
4340 OPJ_UINT32 l_size_unk = 2;
4343 assert(p_j2k != 00);
4344 assert(p_manager != 00);
4345 assert(p_stream != 00);
4347 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
4350 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
4351 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
4352 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
4356 // read 2 bytes as the new marker ID
4357 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
4359 if (!(l_unknown_marker < 0xff00)) {
4361 // Get the marker handler from the marker ID
4362 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
4364 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
4365 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
4369 if (l_marker_handler->id != J2K_MS_UNK) {
4370 /* Add the marker to the codestream index*/
4371 if (l_marker_handler->id != J2K_MS_SOT)
4372 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
4373 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
4375 break; /* next marker is known and well located */
4383 *output_marker = l_marker_handler->id ;
4389 Read the lookup table containing all the marker, status and action
4390 @param id Marker value
4392 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
4393 opj_dec_mstabent_t *e;
4394 for (e = j2k_dec_mstab; e->id != 0; e++) {
4404 * Reads a MCT marker (Multiple Component Transform)
4406 * @param p_header_data the data contained in the MCT box.
4407 * @param p_j2k the jpeg2000 codec.
4408 * @param p_header_size the size of the data contained in the MCT marker.
4409 * @param p_manager the user event manager.
4411 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
4412 OPJ_BYTE * p_header_data,
4413 OPJ_UINT32 p_header_size,
4414 struct opj_event_mgr * p_manager )
4417 opj_tcp_v2_t *l_tcp = 00;
4420 opj_mct_data_t * l_mct_data;
4423 assert(p_header_data != 00);
4424 assert(p_j2k != 00);
4426 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4427 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4428 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4430 if (p_header_size < 2) {
4431 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4436 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
4439 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
4443 if(p_header_size <= 6) {
4444 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4448 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
4449 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
4452 l_indix = l_tmp & 0xff;
4453 l_mct_data = l_tcp->m_mct_records;
4455 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
4456 if (l_mct_data->m_index == l_indix) {
4463 if (i == l_tcp->m_nb_mct_records) {
4464 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
4465 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
4467 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));
4468 if(! l_tcp->m_mct_records) {
4469 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4473 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
4474 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
4477 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
4480 if (l_mct_data->m_data) {
4481 opj_free(l_mct_data->m_data);
4482 l_mct_data->m_data = 00;
4485 l_mct_data->m_index = l_indix;
4486 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
4487 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
4489 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
4492 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
4498 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
4499 if (! l_mct_data->m_data) {
4500 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4503 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
4505 l_mct_data->m_data_size = p_header_size;
4506 ++l_tcp->m_nb_mct_records;
4512 * Reads a MCC marker (Multiple Component Collection)
4514 * @param p_header_data the data contained in the MCC box.
4515 * @param p_j2k the jpeg2000 codec.
4516 * @param p_header_size the size of the data contained in the MCC marker.
4517 * @param p_manager the user event manager.
4519 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
4520 OPJ_BYTE * p_header_data,
4521 OPJ_UINT32 p_header_size,
4522 struct opj_event_mgr * p_manager )
4527 opj_tcp_v2_t * l_tcp;
4528 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4529 opj_mct_data_t * l_mct_data;
4530 OPJ_UINT32 l_nb_collections;
4531 OPJ_UINT32 l_nb_comps;
4532 OPJ_UINT32 l_nb_bytes_by_comp;
4536 assert(p_header_data != 00);
4537 assert(p_j2k != 00);
4538 assert(p_manager != 00);
4540 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4541 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4542 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4544 if (p_header_size < 2) {
4545 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4550 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
4553 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
4557 if (p_header_size < 7) {
4558 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4562 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
4565 l_mcc_record = l_tcp->m_mcc_records;
4567 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
4568 if (l_mcc_record->m_index == l_indix) {
4575 if (i == l_tcp->m_nb_mcc_records) {
4576 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
4577 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
4579 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
4580 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
4581 if (! l_tcp->m_mcc_records) {
4582 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4585 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
4586 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));
4588 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
4590 l_mcc_record->m_index = l_indix;
4592 /* only one marker atm */
4593 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
4596 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
4600 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
4603 if (l_nb_collections > 1) {
4604 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
4610 for (i=0;i<l_nb_collections;++i) {
4611 if (p_header_size < 3) {
4612 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4616 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
4620 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
4624 opj_read_bytes(p_header_data,&l_nb_comps,2);
4629 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
4630 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
4632 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
4633 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4637 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
4639 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
4640 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
4641 p_header_data+=l_nb_bytes_by_comp;
4644 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
4649 opj_read_bytes(p_header_data,&l_nb_comps,2);
4652 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
4653 l_nb_comps &= 0x7fff;
4655 if (l_nb_comps != l_mcc_record->m_nb_comps) {
4656 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
4660 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
4661 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4665 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
4667 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
4668 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
4669 p_header_data+=l_nb_bytes_by_comp;
4672 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
4677 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
4680 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
4681 l_mcc_record->m_decorrelation_array = 00;
4682 l_mcc_record->m_offset_array = 00;
4684 l_indix = l_tmp & 0xff;
4686 l_mct_data = l_tcp->m_mct_records;
4687 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
4688 if (l_mct_data->m_index == l_indix) {
4689 l_mcc_record->m_decorrelation_array = l_mct_data;
4695 if (l_mcc_record->m_decorrelation_array == 00) {
4696 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4701 l_indix = (l_tmp >> 8) & 0xff;
4703 l_mct_data = l_tcp->m_mct_records;
4704 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
4705 if (l_mct_data->m_index == l_indix) {
4706 l_mcc_record->m_offset_array = l_mct_data;
4712 if (l_mcc_record->m_offset_array == 00) {
4713 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4719 if (p_header_size != 0) {
4720 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4724 ++l_tcp->m_nb_mcc_records;
4730 * Reads a MCO marker (Multiple Component Transform Ordering)
4732 * @param p_header_data the data contained in the MCO box.
4733 * @param p_j2k the jpeg2000 codec.
4734 * @param p_header_size the size of the data contained in the MCO marker.
4735 * @param p_manager the user event manager.
4737 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
4738 OPJ_BYTE * p_header_data,
4739 OPJ_UINT32 p_header_size,
4740 struct opj_event_mgr * p_manager )
4742 OPJ_UINT32 l_tmp, i;
4743 OPJ_UINT32 l_nb_stages;
4744 opj_tcp_v2_t * l_tcp;
4745 opj_tccp_t * l_tccp;
4746 opj_image_t * l_image;
4747 opj_image_comp_t * l_img_comp;
4750 assert(p_header_data != 00);
4751 assert(p_j2k != 00);
4752 assert(p_manager != 00);
4754 l_image = p_j2k->m_private_image;
4755 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4756 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4757 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4759 if (p_header_size < 1) {
4760 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
4764 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
4767 if (l_nb_stages > 1) {
4768 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
4772 if (p_header_size != l_nb_stages + 1) {
4773 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
4777 l_tccp = l_tcp->tccps;
4778 l_img_comp = l_image->comps;
4780 for (i=0;i<l_image->numcomps;++i) {
4781 l_tccp->m_dc_level_shift = 0;
4785 if (l_tcp->m_mct_decoding_matrix) {
4786 opj_free(l_tcp->m_mct_decoding_matrix);
4787 l_tcp->m_mct_decoding_matrix = 00;
4790 for (i=0;i<l_nb_stages;++i) {
4791 opj_read_bytes(p_header_data,&l_tmp,1);
4794 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
4802 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
4805 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4806 opj_mct_data_t * l_deco_array, * l_offset_array;
4807 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
4808 OPJ_UINT32 l_nb_elem;
4809 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
4810 opj_tccp_t * l_tccp;
4813 assert(p_tcp != 00);
4815 l_mcc_record = p_tcp->m_mcc_records;
4817 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
4818 if (l_mcc_record->m_index == p_index) {
4823 if (i==p_tcp->m_nb_mcc_records) {
4824 /** element discarded **/
4828 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
4829 /** do not support number of comps != image */
4833 l_deco_array = l_mcc_record->m_decorrelation_array;
4836 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
4837 if (l_deco_array->m_data_size != l_data_size) {
4841 l_nb_elem = p_image->numcomps * p_image->numcomps;
4842 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
4843 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
4845 if (! p_tcp->m_mct_decoding_matrix ) {
4849 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);
4852 l_offset_array = l_mcc_record->m_offset_array;
4854 if (l_offset_array) {
4855 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
4856 if (l_offset_array->m_data_size != l_data_size) {
4860 l_nb_elem = p_image->numcomps;
4861 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
4862 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
4864 if (! l_offset_data ) {
4868 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
4870 l_tccp = p_tcp->tccps;
4871 l_current_offset_data = l_offset_data;
4873 for (i=0;i<p_image->numcomps;++i) {
4874 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
4878 opj_free(l_offset_data);
4885 * Reads a CBD marker (Component bit depth definition)
4886 * @param p_header_data the data contained in the CBD box.
4887 * @param p_j2k the jpeg2000 codec.
4888 * @param p_header_size the size of the data contained in the CBD marker.
4889 * @param p_manager the user event manager.
4891 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
4892 OPJ_BYTE * p_header_data,
4893 OPJ_UINT32 p_header_size,
4894 struct opj_event_mgr * p_manager)
4896 OPJ_UINT32 l_nb_comp,l_num_comp;
4897 OPJ_UINT32 l_comp_def;
4899 opj_image_comp_t * l_comp = 00;
4902 assert(p_header_data != 00);
4903 assert(p_j2k != 00);
4904 assert(p_manager != 00);
4906 l_num_comp = p_j2k->m_private_image->numcomps;
4908 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
4909 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
4913 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
4916 if (l_nb_comp != l_num_comp) {
4917 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
4921 l_comp = p_j2k->m_private_image->comps;
4922 for (i=0;i<l_num_comp;++i) {
4923 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
4925 l_comp->sgnd = (l_comp_def>>7) & 1;
4926 l_comp->prec = (l_comp_def&0x7f) + 1;
4934 /* ----------------------------------------------------------------------- */
4935 /* J2K / JPT decoder interface */
4936 /* ----------------------------------------------------------------------- */
4938 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
4939 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4943 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
4944 if(!j2k->default_tcp) {
4950 j2k->tile_data = NULL;
4955 void j2k_destroy_decompress(opj_j2k_t *j2k) {
4958 if(j2k->tile_len != NULL) {
4959 opj_free(j2k->tile_len);
4961 if(j2k->tile_data != NULL) {
4962 opj_free(j2k->tile_data);
4964 if(j2k->default_tcp != NULL) {
4965 opj_tcp_t *default_tcp = j2k->default_tcp;
4966 if(default_tcp->ppt_data_first != NULL) {
4967 opj_free(default_tcp->ppt_data_first);
4969 if(j2k->default_tcp->tccps != NULL) {
4970 opj_free(j2k->default_tcp->tccps);
4972 opj_free(j2k->default_tcp);
4974 if(j2k->cp != NULL) {
4975 opj_cp_t *cp = j2k->cp;
4976 if(cp->tcps != NULL) {
4977 for(i = 0; i < cp->tw * cp->th; i++) {
4978 if(cp->tcps[i].ppt_data_first != NULL) {
4979 opj_free(cp->tcps[i].ppt_data_first);
4981 if(cp->tcps[i].tccps != NULL) {
4982 opj_free(cp->tcps[i].tccps);
4987 if(cp->ppm_data_first != NULL) {
4988 opj_free(cp->ppm_data_first);
4990 if(cp->tileno != NULL) {
4991 opj_free(cp->tileno);
4993 if(cp->comment != NULL) {
4994 opj_free(cp->comment);
5002 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
5003 if(j2k && parameters) {
5004 /* create and initialize the coding parameters structure */
5005 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
5006 cp->reduce = parameters->cp_reduce;
5007 cp->layer = parameters->cp_layer;
5008 cp->limit_decoding = parameters->cp_limit_decoding;
5011 cp->correct = parameters->jpwl_correct;
5012 cp->exp_comps = parameters->jpwl_exp_comps;
5013 cp->max_tiles = parameters->jpwl_max_tiles;
5014 #endif /* USE_JPWL */
5017 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
5022 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
5024 if(j2k && parameters) {
5025 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
5026 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
5029 j2k->m_cp.correct = parameters->jpwl_correct;
5030 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
5031 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
5032 #endif /* USE_JPWL */
5036 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
5037 opj_image_t *image = NULL;
5039 opj_common_ptr cinfo = j2k->cinfo;
5042 j2k->cstr_info = cstr_info;
5044 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
5046 /* create an empty image */
5047 image = opj_image_create0();
5050 j2k->state = J2K_STATE_MHSOC;
5053 opj_dec_mstabent_t *e;
5054 int id = cio_read(cio, 2);
5057 /* we try to honor JPWL correction power */
5058 if (j2k->cp->correct) {
5060 int orig_pos = cio_tell(cio);
5063 /* call the corrector */
5064 status = jpwl_correct(j2k);
5066 /* go back to where you were */
5067 cio_seek(cio, orig_pos - 2);
5069 /* re-read the marker */
5070 id = cio_read(cio, 2);
5072 /* check whether it begins with ff */
5073 if (id >> 8 != 0xff) {
5074 opj_event_msg(cinfo, EVT_ERROR,
5075 "JPWL: possible bad marker %x at %d\n",
5076 id, cio_tell(cio) - 2);
5078 opj_image_destroy(image);
5079 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
5082 /* we try to correct */
5084 cio_seek(cio, cio_tell(cio) - 2);
5085 cio_write(cio, id, 2);
5086 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
5087 "- setting marker to %x\n",
5092 #endif /* USE_JPWL */
5094 if (id >> 8 != 0xff) {
5095 opj_image_destroy(image);
5096 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
5099 e = j2k_dec_mstab_lookup(id);
5100 /* Check if the marker is known*/
5101 if (!(j2k->state & e->states)) {
5102 opj_image_destroy(image);
5103 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
5106 /* Check if the decoding is limited to the main header*/
5107 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
5108 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
5115 if (j2k->state & J2K_STATE_ERR)
5118 if (j2k->state == J2K_STATE_MT) {
5121 if (j2k->state == J2K_STATE_NEOC) {
5125 if (j2k->state == J2K_STATE_NEOC) {
5129 if (j2k->state != J2K_STATE_MT) {
5130 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
5136 * Read a JPT-stream and decode file
5139 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
5140 opj_image_t *image = NULL;
5141 opj_jpt_msg_header_t header;
5143 opj_common_ptr cinfo = j2k->cinfo;
5145 OPJ_ARG_NOT_USED(cstr_info);
5149 /* create an empty image */
5150 image = opj_image_create0();
5153 j2k->state = J2K_STATE_MHSOC;
5155 /* Initialize the header */
5156 jpt_init_msg_header(&header);
5157 /* Read the first header of the message */
5158 jpt_read_msg_header(cinfo, cio, &header);
5160 position = cio_tell(cio);
5161 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
5162 opj_image_destroy(image);
5163 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
5168 opj_dec_mstabent_t *e = NULL;
5171 if (!cio_numbytesleft(cio)) {
5175 /* data-bin read -> need to read a new header */
5176 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
5177 jpt_read_msg_header(cinfo, cio, &header);
5178 position = cio_tell(cio);
5179 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
5180 opj_image_destroy(image);
5181 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
5186 id = cio_read(cio, 2);
5187 if (id >> 8 != 0xff) {
5188 opj_image_destroy(image);
5189 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
5192 e = j2k_dec_mstab_lookup(id);
5193 if (!(j2k->state & e->states)) {
5194 opj_image_destroy(image);
5195 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
5201 if (j2k->state == J2K_STATE_MT) {
5204 if (j2k->state == J2K_STATE_NEOC) {
5208 if (j2k->state == J2K_STATE_NEOC) {
5212 if (j2k->state != J2K_STATE_MT) {
5213 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
5219 /* ----------------------------------------------------------------------- */
5220 /* J2K encoder interface */
5221 /* ----------------------------------------------------------------------- */
5223 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
5224 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
5231 opj_j2k_v2_t* j2k_create_compress_v2()
5233 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
5238 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
5240 l_j2k->m_is_decoder = 0;
5241 l_j2k->m_cp.m_is_decoder = 0;
5243 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
5244 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5249 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
5251 // validation list creation
5252 l_j2k->m_validation_list = opj_procedure_list_create();
5253 if (! l_j2k->m_validation_list) {
5258 // execution list creation
5259 l_j2k->m_procedure_list = opj_procedure_list_create();
5260 if (! l_j2k->m_procedure_list) {
5268 void j2k_destroy_compress(opj_j2k_t *j2k) {
5272 if(j2k->cp != NULL) {
5273 opj_cp_t *cp = j2k->cp;
5276 opj_free(cp->comment);
5279 opj_free(cp->matrice);
5281 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5282 opj_free(cp->tcps[tileno].tccps);
5291 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
5292 int i, j, tileno, numpocs_tile;
5293 opj_cp_t *cp = NULL;
5295 if(!j2k || !parameters || ! image) {
5299 /* create and initialize the coding parameters structure */
5300 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
5302 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
5305 /* set default values for cp */
5310 copy user encoding parameters
5312 cp->cinema = parameters->cp_cinema;
5313 cp->max_comp_size = parameters->max_comp_size;
5314 cp->rsiz = parameters->cp_rsiz;
5315 cp->disto_alloc = parameters->cp_disto_alloc;
5316 cp->fixed_alloc = parameters->cp_fixed_alloc;
5317 cp->fixed_quality = parameters->cp_fixed_quality;
5319 /* mod fixed_quality */
5320 if(parameters->cp_matrice) {
5321 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
5322 cp->matrice = (int *) opj_malloc(array_size);
5323 memcpy(cp->matrice, parameters->cp_matrice, array_size);
5327 cp->tdx = parameters->cp_tdx;
5328 cp->tdy = parameters->cp_tdy;
5331 cp->tx0 = parameters->cp_tx0;
5332 cp->ty0 = parameters->cp_ty0;
5334 /* comment string */
5335 if(parameters->cp_comment) {
5336 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
5338 strcpy(cp->comment, parameters->cp_comment);
5343 calculate other encoding parameters
5346 if (parameters->tile_size_on) {
5347 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
5348 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
5350 cp->tdx = image->x1 - cp->tx0;
5351 cp->tdy = image->y1 - cp->ty0;
5354 if(parameters->tp_on){
5355 cp->tp_flag = parameters->tp_flag;
5360 for(i=0;i<image->numcomps ;i++){
5361 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
5367 calculate JPWL encoding parameters
5370 if (parameters->jpwl_epc_on) {
5374 cp->epc_on = OPJ_TRUE;
5375 cp->info_on = OPJ_FALSE; /* no informative technique */
5378 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
5379 cp->epb_on = OPJ_TRUE;
5381 cp->hprot_MH = parameters->jpwl_hprot_MH;
5382 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
5383 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
5384 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
5386 /* if tile specs are not specified, copy MH specs */
5387 if (cp->hprot_TPH[0] == -1) {
5388 cp->hprot_TPH_tileno[0] = 0;
5389 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
5391 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
5392 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
5393 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
5394 cp->pprot[i] = parameters->jpwl_pprot[i];
5398 /* set ESD writing */
5399 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
5400 cp->esd_on = OPJ_TRUE;
5402 cp->sens_size = parameters->jpwl_sens_size;
5403 cp->sens_addr = parameters->jpwl_sens_addr;
5404 cp->sens_range = parameters->jpwl_sens_range;
5406 cp->sens_MH = parameters->jpwl_sens_MH;
5407 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
5408 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
5409 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
5413 /* always set RED writing to false: we are at the encoder */
5414 cp->red_on = OPJ_FALSE;
5417 cp->epc_on = OPJ_FALSE;
5419 #endif /* USE_JPWL */
5422 /* initialize the mutiple tiles */
5423 /* ---------------------------- */
5424 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
5426 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5427 opj_tcp_t *tcp = &cp->tcps[tileno];
5428 tcp->numlayers = parameters->tcp_numlayers;
5429 for (j = 0; j < tcp->numlayers; j++) {
5431 if (cp->fixed_quality) {
5432 tcp->distoratio[j] = parameters->tcp_distoratio[j];
5434 tcp->rates[j] = parameters->tcp_rates[j];
5436 if (cp->fixed_quality) { /* add fixed_quality */
5437 tcp->distoratio[j] = parameters->tcp_distoratio[j];
5439 tcp->rates[j] = parameters->tcp_rates[j];
5443 tcp->csty = parameters->csty;
5444 tcp->prg = parameters->prog_order;
5445 tcp->mct = parameters->tcp_mct;
5449 if (parameters->numpocs) {
5450 /* initialisation of POC */
5452 for (i = 0; i < parameters->numpocs; i++) {
5453 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
5454 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
5455 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
5456 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
5457 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
5458 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
5459 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
5460 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
5461 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
5465 tcp->numpocs = numpocs_tile -1 ;
5470 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
5472 for (i = 0; i < image->numcomps; i++) {
5473 opj_tccp_t *tccp = &tcp->tccps[i];
5474 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
5475 tccp->numresolutions = parameters->numresolution;
5476 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
5477 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
5478 tccp->cblksty = parameters->mode;
5479 tccp->qmfbid = parameters->irreversible ? 0 : 1;
5480 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
5482 if (i == parameters->roi_compno) {
5483 tccp->roishift = parameters->roi_shift;
5488 if(parameters->cp_cinema)
5490 /*Precinct size for lowest frequency subband=128*/
5493 /*Precinct size at all other resolutions = 256*/
5494 for (j = 1; j < tccp->numresolutions; j++) {
5499 if (parameters->csty & J2K_CCP_CSTY_PRT) {
5501 for (j = tccp->numresolutions - 1; j >= 0; j--) {
5502 if (p < parameters->res_spec) {
5504 if (parameters->prcw_init[p] < 1) {
5507 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
5510 if (parameters->prch_init[p] < 1) {
5513 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
5517 int res_spec = parameters->res_spec;
5518 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
5519 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
5521 if (size_prcw < 1) {
5524 tccp->prcw[j] = int_floorlog2(size_prcw);
5527 if (size_prch < 1) {
5530 tccp->prch[j] = int_floorlog2(size_prch);
5534 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
5537 for (j = 0; j < tccp->numresolutions; j++) {
5544 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
5549 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
5551 opj_cp_t *cp = NULL;
5553 opj_tcd_t *tcd = NULL; /* TCD component */
5561 j2k->cstr_info = cstr_info;
5564 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
5565 cstr_info->image_w = image->x1 - image->x0;
5566 cstr_info->image_h = image->y1 - image->y0;
5567 cstr_info->prog = (&cp->tcps[0])->prg;
5568 cstr_info->tw = cp->tw;
5569 cstr_info->th = cp->th;
5570 cstr_info->tile_x = cp->tdx; /* new version parser */
5571 cstr_info->tile_y = cp->tdy; /* new version parser */
5572 cstr_info->tile_Ox = cp->tx0; /* new version parser */
5573 cstr_info->tile_Oy = cp->ty0; /* new version parser */
5574 cstr_info->numcomps = image->numcomps;
5575 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
5576 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
5577 for (compno=0; compno < image->numcomps; compno++) {
5578 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
5580 cstr_info->D_max = 0.0; /* ADD Marcela */
5581 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
5582 cstr_info->maxmarknum = 100;
5583 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
5584 cstr_info->marknum = 0;
5594 for (compno = 1; compno < image->numcomps; compno++) {
5595 j2k_write_coc(j2k, compno);
5596 j2k_write_qcc(j2k, compno);
5600 for (compno = 0; compno < image->numcomps; compno++) {
5601 opj_tcp_t *tcp = &cp->tcps[0];
5602 if (tcp->tccps[compno].roishift)
5603 j2k_write_rgn(j2k, compno, 0);
5605 if (cp->comment != NULL) {
5609 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
5613 if (cp->cinema == CINEMA4K_24) {
5618 /* uncomment only for testing JPSEC marker writing */
5619 /* j2k_write_sec(j2k); */
5623 cstr_info->main_head_end = cio_tell(cio) - 1;
5626 /**** Main Header ENDS here ***/
5628 /* create the tile encoder */
5629 tcd = tcd_create(j2k->cinfo);
5631 /* encode each tile */
5632 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5636 int acc_pack_num = 0;
5640 opj_tcp_t *tcp = &cp->tcps[tileno];
5641 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
5643 j2k->curtileno = tileno;
5644 j2k->cur_tp_num = 0;
5645 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
5646 /* initialisation before tile encoding */
5648 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
5650 tcd_init_encode(tcd, image, cp, j2k->curtileno);
5655 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
5656 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
5657 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
5658 cstr_info->tile[j2k->curtileno].marknum = 0;
5662 for(pino = 0; pino <= tcp->numpocs; pino++) {
5666 /*Get number of tile parts*/
5667 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
5668 tcd->tp_pos = cp->tp_pos;
5670 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
5671 j2k->tp_num = tilepartno;
5674 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
5675 cio_tell(cio) + j2k->pos_correction;
5679 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
5680 for (compno = 1; compno < image->numcomps; compno++) {
5681 j2k_write_coc(j2k, compno);
5682 j2k_write_qcc(j2k, compno);
5684 if (cp->tcps[tileno].numpocs) {
5691 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5692 cio_tell(cio) + j2k->pos_correction + 1;
5695 j2k_write_sod(j2k, tcd);
5699 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
5700 cio_tell(cio) + j2k->pos_correction - 1;
5701 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
5703 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
5704 cstr_info->packno - acc_pack_num;
5705 acc_pack_num = cstr_info->packno;
5713 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
5718 if (tile->PPT) { // BAD PPT !!!
5721 PPT_file=fopen("PPT","rb");
5722 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
5723 for (i=0;i<tile->len_ppt;i++) {
5725 fread(&elmt, 1, 1, PPT_file);
5726 fwrite(&elmt,1,1,f);
5735 /* destroy the tile encoder */
5736 tcd_free_encode(tcd);
5739 opj_free(j2k->cur_totnum_tp);
5744 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
5746 /* The following adjustment is done to adjust the codestream size */
5747 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5748 /* the first bunch of bytes is not in the codestream */
5749 cstr_info->codestream_size -= cstr_info->main_head_start;
5755 preparation of JPWL marker segments
5759 /* encode according to JPWL */
5760 jpwl_encode(j2k, cio, image);
5763 #endif /* USE_JPWL */
5768 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
5770 assert(cstr_info != 00);
5772 /* expand the list? */
5773 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
5774 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
5775 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
5778 /* add the marker */
5779 cstr_info->marker[cstr_info->marknum].type = type;
5780 cstr_info->marker[cstr_info->marknum].pos = pos;
5781 cstr_info->marker[cstr_info->marknum].len = len;
5782 cstr_info->marknum++;
5786 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
5788 assert(cstr_index != 00);
5790 /* expand the list? */
5791 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
5792 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
5793 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
5796 /* add the marker */
5797 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
5798 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
5799 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
5800 cstr_index->marknum++;
5804 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
5806 opj_marker_info_t *marker;
5808 assert(cstr_info != 00);
5810 /* expand the list? */
5811 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
5812 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
5813 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
5816 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
5818 /* add the marker */
5819 marker->type = type;
5822 cstr_info->tile[tileno].marknum++;
5825 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)
5827 assert(cstr_index != 00);
5828 assert(cstr_index->tile_index != 00);
5830 /* expand the list? */
5831 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
5832 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
5833 cstr_index->tile_index[tileno].marker =
5834 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
5835 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
5838 /* add the marker */
5839 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
5840 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
5841 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
5842 cstr_index->tile_index[tileno].marknum++;
5844 if (type == J2K_MS_SOT) {
5845 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
5847 if (cstr_index->tile_index[tileno].tp_index)
5848 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
5855 * -----------------------------------------------------------------------
5856 * -----------------------------------------------------------------------
5857 * -----------------------------------------------------------------------
5861 * Ends the decompression procedures and possibiliy add data to be read after the
5864 opj_bool j2k_end_decompress(
5865 opj_j2k_v2_t *p_j2k,
5866 opj_stream_private_t *p_stream,
5867 opj_event_mgr_t * p_manager)
5873 * Reads a jpeg2000 codestream header structure.
5876 * @param p_stream the stream to read data from.
5877 * @param p_j2k the jpeg2000 codec.
5878 * @param p_manager the user event manager.
5880 * @return true if the box is valid.
5882 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
5883 opj_j2k_v2_t* p_j2k,
5884 opj_image_t** p_image,
5885 struct opj_event_mgr* p_manager )
5888 assert(p_j2k != 00);
5889 assert(p_stream != 00);
5890 assert(p_manager != 00);
5892 /* create an empty image header */
5893 p_j2k->m_private_image = opj_image_create0();
5894 if (! p_j2k->m_private_image) {
5898 /* customization of the validation */
5899 j2k_setup_decoding_validation(p_j2k);
5901 /* validation of the parameters codec */
5902 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
5903 opj_image_destroy(p_j2k->m_private_image);
5904 p_j2k->m_private_image = NULL;
5908 /* customization of the encoding */
5909 j2k_setup_header_reading(p_j2k);
5912 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
5913 opj_image_destroy(p_j2k->m_private_image);
5914 p_j2k->m_private_image = NULL;
5918 *p_image = opj_image_create0();
5923 /* Copy codestream image information to the output image */
5924 opj_copy_image_header(p_j2k->m_private_image, *p_image);
5931 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
5933 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
5936 assert(p_j2k != 00);
5938 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_read_header_procedure);
5940 /* DEVELOPER CORNER, add your custom procedures */
5941 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_copy_default_tcp_and_create_tcd);
5946 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
5947 * are valid. Developpers wanting to extend the library can add their own validation procedures.
5949 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
5952 assert(p_j2k != 00);
5954 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_decoder);
5955 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_decoding_validation);
5956 /* DEVELOPER CORNER, add your custom validation procedure */
5961 * Builds the cp decoder parameters to use to decode tile.
5963 opj_bool j2k_build_decoder (
5964 opj_j2k_v2_t * p_j2k,
5965 opj_stream_private_t *p_stream,
5966 opj_event_mgr_t * p_manager
5969 // add here initialization of cp
5970 // copy paste of setup_decoder
5975 * The default decoding validation procedure without any extension.
5977 * @param p_j2k the jpeg2000 codec to validate.
5978 * @param p_stream the input stream to validate.
5979 * @param p_manager the user event manager.
5981 * @return true if the parameters are correct.
5983 opj_bool j2k_decoding_validation (
5984 opj_j2k_v2_t *p_j2k,
5985 opj_stream_private_t *p_stream,
5986 opj_event_mgr_t * p_manager
5989 opj_bool l_is_valid = OPJ_TRUE;
5992 assert(p_j2k != 00);
5993 assert(p_stream != 00);
5994 assert(p_manager != 00);
5997 /* STATE checking */
5998 /* make sure the state is at 0 */
6000 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
6002 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
6004 /* POINTER validation */
6005 /* make sure a p_j2k codec is present */
6006 /* make sure a procedure list is present */
6007 l_is_valid &= (p_j2k->m_procedure_list != 00);
6008 /* make sure a validation list is present */
6009 l_is_valid &= (p_j2k->m_validation_list != 00);
6011 /* PARAMETER VALIDATION */
6015 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
6016 struct opj_stream_private *p_stream,
6017 struct opj_event_mgr * p_manager)
6019 OPJ_UINT32 l_current_marker;
6020 OPJ_UINT32 l_marker_size;
6021 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6024 assert(p_stream != 00);
6025 assert(p_j2k != 00);
6026 assert(p_manager != 00);
6028 /* We enter in the main header */
6029 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
6031 /* Try to read the SOC marker, the codestream must begin with SOC marker */
6032 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
6033 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
6037 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6038 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6039 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6043 /* Read 2 bytes as the new marker ID */
6044 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6046 /* Try to read until the SOT is detected */
6047 while (l_current_marker != J2K_MS_SOT) {
6049 /* Check if the current marker ID is valid */
6050 if (l_current_marker < 0xff00) {
6051 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
6055 /* Get the marker handler from the marker ID */
6056 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6058 /* Manage case where marker is unknown */
6059 if (l_marker_handler->id == J2K_MS_UNK) {
6060 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
6061 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
6065 if (l_current_marker == J2K_MS_SOT)
6066 break; /* SOT marker is detected main header is completely read */
6067 else /* Get the marker handler from the marker ID */
6068 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6071 /* Check if the marker is known and if it is the right place to find it */
6072 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6073 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6077 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6078 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6079 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6083 /* read 2 bytes as the marker size */
6084 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6085 l_marker_size -= 2; // Subtract the size of the marker ID already read
6087 /* Check if the marker size is compatible with the header data size */
6088 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6089 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
6090 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
6091 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
6094 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6097 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6098 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) {
6099 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6103 /* Read the marker segment with the correct marker handler */
6104 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6105 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
6109 /* Add the marker to the codestream index*/
6110 j2k_add_mhmarker_v2(p_j2k->cstr_index,
6111 l_marker_handler->id,
6112 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6113 l_marker_size + 4 );
6115 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6116 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6117 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6121 /* read 2 bytes as the new marker ID */
6122 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6125 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
6127 /* Position of the last element if the main header */
6128 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
6130 /* Next step: read a tile-part header */
6131 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6137 * Excutes the given procedures on the given codec.
6139 * @param p_procedure_list the list of procedures to execute
6140 * @param p_j2k the jpeg2000 codec to execute the procedures on.
6141 * @param p_stream the stream to execute the procedures on.
6142 * @param p_manager the user manager.
6144 * @return true if all the procedures were successfully executed.
6146 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
6147 opj_procedure_list_t * p_procedure_list,
6148 opj_stream_private_t *p_stream,
6149 opj_event_mgr_t * p_manager )
6151 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
6152 opj_bool l_result = OPJ_TRUE;
6153 OPJ_UINT32 l_nb_proc, i;
6156 assert(p_procedure_list != 00);
6157 assert(p_j2k != 00);
6158 assert(p_stream != 00);
6159 assert(p_manager != 00);
6162 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
6163 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);
6165 for (i=0;i<l_nb_proc;++i) {
6166 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
6170 // and clear the procedure list at the end.
6171 opj_procedure_list_clear(p_procedure_list);
6176 opj_bool j2k_copy_default_tcp_and_create_tcd
6178 opj_j2k_v2_t * p_j2k,
6179 opj_stream_private_t *p_stream,
6180 opj_event_mgr_t * p_manager
6183 opj_tcp_v2_t * l_tcp = 00;
6184 opj_tcp_v2_t * l_default_tcp = 00;
6185 OPJ_UINT32 l_nb_tiles;
6187 opj_tccp_t *l_current_tccp = 00;
6188 OPJ_UINT32 l_tccp_size;
6189 OPJ_UINT32 l_mct_size;
6190 opj_image_t * l_image;
6191 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
6192 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
6193 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
6194 OPJ_UINT32 l_offset;
6197 assert(p_j2k != 00);
6198 assert(p_stream != 00);
6199 assert(p_manager != 00);
6201 l_image = p_j2k->m_private_image;
6202 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6203 l_tcp = p_j2k->m_cp.tcps;
6204 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
6205 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
6206 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
6209 for (i=0; i<l_nb_tiles; ++i) {
6210 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
6211 l_current_tccp = l_tcp->tccps;
6212 /*Copy default coding parameters into the current tile coding parameters*/
6213 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
6214 /* Initialize some values of the current tile coding parameters*/
6216 l_tcp->ppt_data = 00;
6217 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
6218 l_tcp->tccps = l_current_tccp;
6220 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
6221 if (l_default_tcp->m_mct_decoding_matrix) {
6222 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6223 if (! l_tcp->m_mct_decoding_matrix ) {
6226 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
6229 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
6230 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
6231 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
6232 if (! l_tcp->m_mct_records) {
6235 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
6237 /* Copy the mct record data from dflt_tile_cp to the current tile*/
6238 l_src_mct_rec = l_default_tcp->m_mct_records;
6239 l_dest_mct_rec = l_tcp->m_mct_records;
6241 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
6243 if (l_src_mct_rec->m_data) {
6245 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
6246 if(! l_dest_mct_rec->m_data) {
6249 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
6256 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
6257 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
6258 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
6259 if (! l_tcp->m_mcc_records) {
6262 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
6264 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
6265 l_src_mcc_rec = l_default_tcp->m_mcc_records;
6266 l_dest_mcc_rec = l_tcp->m_mcc_records;
6268 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
6270 if (l_src_mcc_rec->m_decorrelation_array) {
6271 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
6272 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
6275 if (l_src_mcc_rec->m_offset_array) {
6276 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
6277 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
6284 /* Copy all the dflt_tile_compo_cp to the current tile cp */
6285 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
6287 /* Move to next tile cp*/
6291 /* Create the current tile decoder*/
6292 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); // FIXME why a cast ?
6293 if (! p_j2k->m_tcd ) {
6297 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
6298 tcd_destroy_v2(p_j2k->m_tcd);
6300 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6308 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
6309 * with the marker value.
6310 * @param p_id Marker value to look up
6312 * @return the handler associated with the id.
6314 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
6316 const opj_dec_memory_marker_handler_t *e;
6317 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
6318 if (e->id == p_id) {
6319 break; // we find a handler corresponding to the marker ID
6327 * Destroys a jpeg2000 codec.
6329 * @param p_j2k the jpeg20000 structure to destroy.
6331 void j2k_destroy (opj_j2k_v2_t *p_j2k)
6337 if (p_j2k->m_is_decoder) {
6339 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
6340 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
6341 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
6342 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
6345 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
6346 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
6347 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
6348 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
6353 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
6354 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
6355 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
6358 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6359 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
6360 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
6361 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
6364 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6365 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
6366 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
6367 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
6371 tcd_destroy_v2(p_j2k->m_tcd);
6373 j2k_cp_destroy(&(p_j2k->m_cp));
6374 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
6376 opj_procedure_list_destroy(p_j2k->m_procedure_list);
6377 p_j2k->m_procedure_list = 00;
6379 opj_procedure_list_destroy(p_j2k->m_validation_list);
6380 p_j2k->m_procedure_list = 00;
6382 j2k_destroy_cstr_index(p_j2k->cstr_index);
6383 p_j2k->cstr_index = NULL;
6385 opj_image_destroy(p_j2k->m_private_image);
6386 p_j2k->m_private_image = NULL;
6388 opj_image_destroy(p_j2k->m_output_image);
6389 p_j2k->m_output_image = NULL;
6394 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
6398 if (p_cstr_ind->marker) {
6399 opj_free(p_cstr_ind->marker);
6400 p_cstr_ind->marker = NULL;
6403 if (p_cstr_ind->tile_index) {
6404 OPJ_UINT32 it_tile = 0;
6406 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
6408 if(p_cstr_ind->tile_index[it_tile].packet_index) {
6409 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
6410 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
6413 if(p_cstr_ind->tile_index[it_tile].tp_index){
6414 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
6415 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
6418 if(p_cstr_ind->tile_index[it_tile].marker){
6419 opj_free(p_cstr_ind->tile_index[it_tile].marker);
6420 p_cstr_ind->tile_index[it_tile].marker = NULL;
6425 opj_free( p_cstr_ind->tile_index);
6426 p_cstr_ind->tile_index = NULL;
6429 opj_free(p_cstr_ind);
6436 * Destroys a tile coding parameter structure.
6438 * @param p_tcp the tile coding parameter to destroy.
6440 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
6446 if (p_tcp->ppt_buffer != 00) {
6447 opj_free(p_tcp->ppt_buffer);
6448 p_tcp->ppt_buffer = 00;
6451 if (p_tcp->tccps != 00) {
6452 opj_free(p_tcp->tccps);
6456 if (p_tcp->m_mct_coding_matrix != 00) {
6457 opj_free(p_tcp->m_mct_coding_matrix);
6458 p_tcp->m_mct_coding_matrix = 00;
6461 if (p_tcp->m_mct_decoding_matrix != 00) {
6462 opj_free(p_tcp->m_mct_decoding_matrix);
6463 p_tcp->m_mct_decoding_matrix = 00;
6466 if (p_tcp->m_mcc_records) {
6467 opj_free(p_tcp->m_mcc_records);
6468 p_tcp->m_mcc_records = 00;
6469 p_tcp->m_nb_max_mcc_records = 0;
6470 p_tcp->m_nb_mcc_records = 0;
6473 if (p_tcp->m_mct_records) {
6474 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
6477 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
6478 if (l_mct_data->m_data) {
6479 opj_free(l_mct_data->m_data);
6480 l_mct_data->m_data = 00;
6486 opj_free(p_tcp->m_mct_records);
6487 p_tcp->m_mct_records = 00;
6490 if (p_tcp->mct_norms != 00) {
6491 opj_free(p_tcp->mct_norms);
6492 p_tcp->mct_norms = 00;
6495 j2k_tcp_data_destroy(p_tcp);
6500 * Destroys the data inside a tile coding parameter structure.
6502 * @param p_tcp the tile coding parameter which contain data to destroy.
6504 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
6506 if (p_tcp->m_data) {
6507 opj_free(p_tcp->m_data);
6508 p_tcp->m_data = NULL;
6509 p_tcp->m_data_size = 0;
6514 * Destroys a coding parameter structure.
6516 * @param p_cp the coding parameter to destroy.
6518 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
6520 OPJ_UINT32 l_nb_tiles;
6521 opj_tcp_v2_t * l_current_tile = 00;
6532 l_current_tile = p_cp->tcps;
6533 l_nb_tiles = p_cp->th * p_cp->tw;
6536 (i = 0; i < l_nb_tiles; ++i)
6538 j2k_tcp_destroy(l_current_tile);
6541 opj_free(p_cp->tcps);
6545 (p_cp->ppm_buffer != 00)
6547 opj_free(p_cp->ppm_buffer);
6548 p_cp->ppm_buffer = 00;
6551 (p_cp->comment != 00)
6553 opj_free(p_cp->comment);
6557 (! p_cp->m_is_decoder)
6560 (p_cp->m_specific_param.m_enc.m_matrice)
6562 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
6563 p_cp->m_specific_param.m_enc.m_matrice = 00;
6571 * Reads a tile header.
6572 * @param p_j2k the jpeg2000 codec.
6573 * @param p_stream the stream to write data to.
6574 * @param p_manager the user event manager.
6576 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
6577 OPJ_UINT32 * p_tile_index,
6578 OPJ_UINT32 * p_data_size,
6579 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
6580 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
6581 OPJ_UINT32 * p_nb_comps,
6583 opj_stream_private_t *p_stream,
6584 opj_event_mgr_t * p_manager )
6586 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
6587 OPJ_UINT32 l_marker_size;
6588 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6589 opj_tcp_v2_t * l_tcp = NULL;
6590 OPJ_UINT32 l_nb_tiles;
6593 assert(p_stream != 00);
6594 assert(p_j2k != 00);
6595 assert(p_manager != 00);
6597 /* Reach the End Of Codestream ?*/
6598 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
6599 l_current_marker = J2K_MS_EOC;
6601 /* We need to encounter a SOT marker (a new tile-part header) */
6602 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
6606 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
6607 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
6609 /* Try to read until the Start Of Data is detected */
6610 while (l_current_marker != J2K_MS_SOD) {
6612 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6613 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6614 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6618 /* Read 2 bytes from the buffer as the marker size */
6619 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6621 /* Why this condition? FIXME */
6622 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
6623 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
6625 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
6627 /* Get the marker handler from the marker ID */
6628 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6630 /* Check if the marker is known and if it is the right place to find it */
6631 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6632 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6635 /* FIXME manage case of unknown marker as in the main header ? */
6637 /* Check if the marker size is compatible with the header data size */
6638 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6639 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
6640 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
6641 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
6644 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6647 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6648 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) {
6649 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6653 /* Read the marker segment with the correct marker handler */
6654 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6655 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
6659 /* Add the marker to the codestream index*/
6660 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
6662 l_marker_handler->id,
6663 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6664 l_marker_size + 4 );
6666 /* Keep the position of the last SOT marker read */
6667 if ( l_marker_handler->id == J2K_MS_SOT ) {
6668 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
6669 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
6671 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
6676 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
6677 /* Skip the rest of the tile part header*/
6678 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) {
6679 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6682 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
6685 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6686 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6687 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6690 /* Read 2 bytes from the buffer as the new marker ID */
6691 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6695 /* If we didn't skip data before, we need to read the SOD marker*/
6696 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
6697 /* Try to read the SOD marker and skip data ? FIXME */
6698 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
6704 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
6705 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6706 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6707 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6711 /* Read 2 bytes from buffer as the new marker ID */
6712 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6716 /* Indicate we will try to read a new tile-part header*/
6717 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
6718 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
6719 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6721 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6722 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6723 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6727 /* Read 2 bytes from buffer as the new marker ID */
6728 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6732 /* Current marker is the EOC marker ?*/
6733 if (l_current_marker == J2K_MS_EOC) {
6734 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
6735 p_j2k->m_current_tile_number = 0;
6736 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
6741 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
6742 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
6743 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6745 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
6746 ++p_j2k->m_current_tile_number;
6750 if (p_j2k->m_current_tile_number == l_nb_tiles) {
6751 *p_go_on = OPJ_FALSE;
6757 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
6758 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6762 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
6763 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
6765 *p_tile_index = p_j2k->m_current_tile_number;
6766 *p_go_on = OPJ_TRUE;
6767 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
6768 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
6769 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
6770 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
6771 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
6772 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
6774 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;// FIXME J2K_DEC_STATE_DATA;
6780 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
6781 OPJ_UINT32 p_tile_index,
6783 OPJ_UINT32 p_data_size,
6784 opj_stream_private_t *p_stream,
6785 opj_event_mgr_t * p_manager )
6787 OPJ_UINT32 l_current_marker;
6788 OPJ_BYTE l_data [2];
6789 opj_tcp_v2_t * l_tcp;
6792 assert(p_stream != 00);
6793 assert(p_j2k != 00);
6794 assert(p_manager != 00);
6796 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
6797 || (p_tile_index != p_j2k->m_current_tile_number) ) {
6801 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
6802 if (! l_tcp->m_data) {
6803 j2k_tcp_destroy(l_tcp);
6807 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
6811 p_j2k->cstr_index) ) {
6812 j2k_tcp_destroy(l_tcp);
6813 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;//FIXME J2K_DEC_STATE_ERR;
6817 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
6821 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
6822 * we destroy just the data which will be re-read in read_tile_header*/
6823 /*j2k_tcp_destroy(l_tcp);
6824 p_j2k->m_tcd->tcp = 0;*/
6825 j2k_tcp_data_destroy(l_tcp);
6827 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
6828 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));// FIXME J2K_DEC_STATE_DATA);
6830 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ //FIXME J2K_DEC_STATE_EOC)
6831 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
6832 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6836 opj_read_bytes(l_data,&l_current_marker,2);
6838 if (l_current_marker == J2K_MS_EOC) {
6839 p_j2k->m_current_tile_number = 0;
6840 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;//FIXME J2K_DEC_STATE_EOC;
6842 else if (l_current_marker != J2K_MS_SOT)
6844 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
6853 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
6855 OPJ_UINT32 i,j,k = 0;
6856 OPJ_UINT32 l_width_src,l_height_src;
6857 OPJ_UINT32 l_width_dest,l_height_dest;
6858 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
6859 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
6860 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
6861 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
6862 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
6864 opj_image_comp_t * l_img_comp_src = 00;
6865 opj_image_comp_t * l_img_comp_dest = 00;
6867 opj_tcd_tilecomp_v2_t * l_tilec = 00;
6868 opj_image_t * l_image_src = 00;
6869 OPJ_UINT32 l_size_comp, l_remaining;
6870 OPJ_INT32 * l_dest_ptr;
6871 opj_tcd_resolution_v2_t* l_res= 00;
6873 l_tilec = p_tcd->tcd_image->tiles->comps;
6874 l_image_src = p_tcd->image;
6875 l_img_comp_src = l_image_src->comps;
6877 l_img_comp_dest = p_output_image->comps;
6879 for (i=0; i<l_image_src->numcomps; i++) {
6881 /* Allocate output component buffer if necessary */
6882 if (!l_img_comp_dest->data) {
6884 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
6885 if (! l_img_comp_dest->data) {
6890 /* Copy info from decoded comp image to output image */
6891 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
6894 /* Compute the precision of the output buffer */
6895 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
6896 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
6897 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
6903 if (l_size_comp == 3) {
6908 /* Current tile component size*/
6910 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
6911 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
6914 l_width_src = (l_res->x1 - l_res->x0);
6915 l_height_src = (l_res->y1 - l_res->y0);
6917 /* Border of the current output component*/
6918 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
6919 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
6920 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
6921 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
6924 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
6925 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
6929 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
6930 * of the input buffer (decoded tile component) which will be move
6931 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
6932 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
6933 * by this input area.
6935 if ( l_x0_dest < l_res->x0 ) {
6936 l_start_x_dest = l_res->x0 - l_x0_dest;
6937 l_offset_x0_src = 0;
6939 if ( l_x1_dest >= l_res->x1 ) {
6940 l_width_dest = l_width_src;
6941 l_offset_x1_src = 0;
6944 l_width_dest = l_x1_dest - l_res->x0 ;
6945 l_offset_x1_src = l_width_src - l_width_dest;
6949 l_start_x_dest = 0 ;
6950 l_offset_x0_src = l_x0_dest - l_res->x0;
6952 if ( l_x1_dest >= l_res->x1 ) {
6953 l_width_dest = l_width_src - l_offset_x0_src;
6954 l_offset_x1_src = 0;
6957 l_width_dest = l_img_comp_dest->w ;
6958 l_offset_x1_src = l_res->x1 - l_x1_dest;
6962 if ( l_y0_dest < l_res->y0 ) {
6963 l_start_y_dest = l_res->y0 - l_y0_dest;
6964 l_offset_y0_src = 0;
6966 if ( l_y1_dest >= l_res->y1 ) {
6967 l_height_dest = l_height_src;
6968 l_offset_y1_src = 0;
6971 l_height_dest = l_y1_dest - l_res->y0 ;
6972 l_offset_y1_src = l_height_src - l_height_dest;
6976 l_start_y_dest = 0 ;
6977 l_offset_y0_src = l_y0_dest - l_res->y0;
6979 if ( l_y1_dest >= l_res->y1 ) {
6980 l_height_dest = l_height_src - l_offset_y0_src;
6981 l_offset_y1_src = 0;
6984 l_height_dest = l_img_comp_dest->h ;
6985 l_offset_y1_src = l_res->y1 - l_y1_dest;
6989 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
6994 /* Compute the input buffer offset */
6995 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
6996 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
6997 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
6999 /* Compute the output buffer offset */
7000 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
7001 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
7003 /* Move the output buffer to the first place where we will write*/
7004 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
7007 fprintf(stdout, "COMPO[%d]:\n",i);
7008 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
7009 "\t tile offset:%d, %d, %d, %d\n"
7010 "\t buffer offset: %d; %d, %d\n",
7011 l_res->x0, l_res->y0, l_width_src, l_height_src,
7012 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
7013 l_start_offset_src, l_line_offset_src, l_end_offset_src);
7015 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
7016 "\t start offset: %d, line offset= %d\n",
7017 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
7021 switch (l_size_comp) {
7024 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
7025 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
7027 if (l_img_comp_src->sgnd) {
7028 for (j = 0 ; j < l_height_dest ; ++j) {
7029 for ( k = 0 ; k < l_width_dest ; ++k) {
7030 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
7033 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
7034 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
7038 for ( j = 0 ; j < l_height_dest ; ++j ) {
7039 for ( k = 0 ; k < l_width_dest ; ++k) {
7040 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
7043 l_dest_ptr+= l_line_offset_dest;
7044 l_src_ptr += l_line_offset_src;
7048 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
7049 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
7054 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
7055 l_src_ptr += l_start_offset_src;
7057 if (l_img_comp_src->sgnd) {
7058 for (j=0;j<l_height_dest;++j) {
7059 for (k=0;k<l_width_dest;++k) {
7060 *(l_dest_ptr++) = *(l_src_ptr++);
7063 l_dest_ptr+= l_line_offset_dest;
7064 l_src_ptr += l_line_offset_src ;
7068 for (j=0;j<l_height_dest;++j) {
7069 for (k=0;k<l_width_dest;++k) {
7070 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
7073 l_dest_ptr+= l_line_offset_dest;
7074 l_src_ptr += l_line_offset_src ;
7078 l_src_ptr += l_end_offset_src;
7079 p_data = (OPJ_BYTE*) l_src_ptr;
7084 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
7085 l_src_ptr += l_start_offset_src;
7087 for (j=0;j<l_height_dest;++j) {
7088 for (k=0;k<l_width_dest;++k) {
7089 *(l_dest_ptr++) = (*(l_src_ptr++));
7092 l_dest_ptr+= l_line_offset_dest;
7093 l_src_ptr += l_line_offset_src ;
7096 l_src_ptr += l_end_offset_src;
7097 p_data = (OPJ_BYTE*) l_src_ptr;
7111 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
7113 * @param p_j2k the jpeg2000 codec.
7114 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
7115 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
7116 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
7117 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
7118 * @param p_manager the user event manager
7120 * @return true if the area could be set.
7122 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
7123 opj_image_t* p_image,
7124 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
7125 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
7126 struct opj_event_mgr * p_manager )
7128 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
7129 opj_image_t * l_image = p_j2k->m_private_image;
7132 OPJ_INT32 l_comp_x1, l_comp_y1;
7133 opj_image_comp_t* l_img_comp = NULL;
7135 /* Check if we are read the main header */
7136 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { // FIXME J2K_DEC_STATE_TPHSOT)
7137 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
7141 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
7142 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
7144 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7145 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7146 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7147 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
7153 /* Check if the positions provided by the user are correct */
7156 if (p_start_x > l_image->x1 ) {
7157 opj_event_msg_v2(p_manager, EVT_ERROR,
7158 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
7159 p_start_x, l_image->x1);
7162 else if (p_start_x < l_image->x0){
7163 opj_event_msg_v2(p_manager, EVT_WARNING,
7164 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
7165 p_start_x, l_image->x0);
7166 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7167 p_image->x0 = l_image->x0;
7170 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
7171 p_image->x0 = p_start_x;
7175 if (p_start_y > l_image->y1){
7176 opj_event_msg_v2(p_manager, EVT_ERROR,
7177 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
7178 p_start_y, l_image->y1);
7181 else if (p_start_y < l_image->y0){
7182 opj_event_msg_v2(p_manager, EVT_WARNING,
7183 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
7184 p_start_y, l_image->y0);
7185 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7186 p_image->y0 = l_image->y0;
7189 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
7190 p_image->y0 = p_start_y;
7194 if (p_end_x < l_image->x0) {
7195 opj_event_msg_v2(p_manager, EVT_ERROR,
7196 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
7197 p_end_x, l_image->x0);
7200 else if (p_end_x > l_image->x1) {
7201 opj_event_msg_v2(p_manager, EVT_WARNING,
7202 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
7203 p_end_x, l_image->x1);
7204 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7205 p_image->x1 = l_image->x1;
7208 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
7209 p_image->x1 = p_end_x;
7213 if (p_end_y < l_image->y0) {
7214 opj_event_msg_v2(p_manager, EVT_ERROR,
7215 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
7216 p_end_y, l_image->y0);
7219 if (p_end_y > l_image->y1){
7220 opj_event_msg_v2(p_manager, EVT_WARNING,
7221 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
7222 p_end_y, l_image->y1);
7223 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
7224 p_image->y1 = l_image->y1;
7227 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
7228 p_image->y1 = p_end_y;
7232 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
7234 l_img_comp = p_image->comps;
7235 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
7239 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
7240 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
7241 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
7242 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
7244 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
7245 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
7247 opj_event_msg_v2(p_manager, EVT_ERROR,
7248 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
7252 l_img_comp->w = l_w;
7254 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
7255 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
7257 opj_event_msg_v2(p_manager, EVT_ERROR,
7258 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
7262 l_img_comp->h = l_h;
7267 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
7268 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
7275 /* ----------------------------------------------------------------------- */
7276 /* J2K / JPT decoder interface */
7277 /* ----------------------------------------------------------------------- */
7279 * Creates a J2K decompression structure.
7281 * @return a handle to a J2K decompressor if successful, NULL otherwise.
7283 opj_j2k_v2_t* j2k_create_decompress_v2()
7285 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7289 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7291 l_j2k->m_is_decoder = 1;
7292 l_j2k->m_cp.m_is_decoder = 1;
7294 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
7295 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
7299 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
7301 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7302 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
7307 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
7309 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
7311 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
7313 /* codestream index creation */
7314 l_j2k->cstr_index = j2k_create_cstr_index();
7316 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
7317 if (!l_j2k->cstr_index){
7322 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
7325 /* validation list creation */
7326 l_j2k->m_validation_list = opj_procedure_list_create();
7327 if (! l_j2k->m_validation_list) {
7332 /* execution list creation */
7333 l_j2k->m_procedure_list = opj_procedure_list_create();
7334 if (! l_j2k->m_procedure_list) {
7343 opj_codestream_index_t* j2k_create_cstr_index(void)
7345 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
7346 opj_calloc(1,sizeof(opj_codestream_index_t));
7350 cstr_index->maxmarknum = 100;
7351 cstr_index->marknum = 0;
7352 cstr_index->marker = (opj_marker_info_t*)
7353 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
7354 if (!cstr_index-> marker)
7357 cstr_index->tile_index = NULL;
7363 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
7364 * @param p_header_data the data contained in the COM box.
7365 * @param p_j2k the jpeg2000 codec.
7366 * @param p_header_size the size of the data contained in the COM marker.
7367 * @param p_manager the user event manager.
7369 opj_bool j2k_read_SPCod_SPCoc(
7370 opj_j2k_v2_t *p_j2k,
7372 OPJ_BYTE * p_header_data,
7373 OPJ_UINT32 * p_header_size,
7374 struct opj_event_mgr * p_manager
7377 OPJ_UINT32 i, l_tmp;
7378 opj_cp_v2_t *l_cp = NULL;
7379 opj_tcp_v2_t *l_tcp = NULL;
7380 opj_tccp_t *l_tccp = NULL;
7381 OPJ_BYTE * l_current_ptr = NULL;
7384 assert(p_j2k != 00);
7385 assert(p_manager != 00);
7386 assert(p_header_data != 00);
7388 l_cp = &(p_j2k->m_cp);
7389 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
7390 &l_cp->tcps[p_j2k->m_current_tile_number] :
7391 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7393 /* precondition again */
7394 assert(compno < p_j2k->m_private_image->numcomps);
7396 l_tccp = &l_tcp->tccps[compno];
7397 l_current_ptr = p_header_data;
7399 /* make sure room is sufficient */
7400 if (*p_header_size < 5) {
7401 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
7405 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
7406 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
7409 /* If user wants to remove more resolutions than the codestream contains, return error */
7410 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
7411 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
7412 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
7413 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;// FIXME J2K_DEC_STATE_ERR;
7417 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
7421 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
7425 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
7428 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
7431 *p_header_size = *p_header_size - 5;
7433 /* use custom precinct size ? */
7434 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
7435 if (*p_header_size < l_tccp->numresolutions) {
7436 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
7440 for (i = 0; i < l_tccp->numresolutions; ++i) {
7441 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
7443 l_tccp->prcw[i] = l_tmp & 0xf;
7444 l_tccp->prch[i] = l_tmp >> 4;
7447 *p_header_size = *p_header_size - l_tccp->numresolutions;
7450 /* set default size for the precinct width and height */
7451 for (i = 0; i < l_tccp->numresolutions; ++i) {
7452 l_tccp->prcw[i] = 15;
7453 l_tccp->prch[i] = 15;
7457 #ifdef WIP_REMOVE_MSD
7459 if (p_j2k->cstr_info && compno == 0) {
7460 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
7462 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
7463 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
7464 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
7465 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
7466 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
7469 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
7470 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
7479 * Copies the tile component parameters of all the component from the first tile component.
7481 * @param p_j2k the J2k codec.
7483 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
7487 opj_cp_v2_t *l_cp = NULL;
7488 opj_tcp_v2_t *l_tcp = NULL;
7489 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
7490 OPJ_UINT32 l_prc_size;
7493 assert(p_j2k != 00);
7495 l_cp = &(p_j2k->m_cp);
7496 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
7497 &l_cp->tcps[p_j2k->m_current_tile_number] :
7498 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7500 l_ref_tccp = &l_tcp->tccps[0];
7501 l_copied_tccp = l_ref_tccp + 1;
7502 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
7504 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
7505 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
7506 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
7507 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
7508 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
7509 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
7510 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
7511 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
7517 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
7519 * @param p_comp_no the component being targeted.
7520 * @param p_header_data the data contained in the COM box.
7521 * @param p_j2k the jpeg2000 codec.
7522 * @param p_header_size the size of the data contained in the COM marker.
7523 * @param p_manager the user event manager.
7525 opj_bool j2k_read_SQcd_SQcc(
7526 opj_j2k_v2_t *p_j2k,
7527 OPJ_UINT32 p_comp_no,
7528 OPJ_BYTE* p_header_data,
7529 OPJ_UINT32 * p_header_size,
7530 struct opj_event_mgr * p_manager
7534 OPJ_UINT32 l_band_no;
7535 opj_cp_v2_t *l_cp = 00;
7536 opj_tcp_v2_t *l_tcp = 00;
7537 opj_tccp_t *l_tccp = 00;
7538 OPJ_BYTE * l_current_ptr = 00;
7539 OPJ_UINT32 l_tmp, l_num_band;
7542 assert(p_j2k != 00);
7543 assert(p_manager != 00);
7544 assert(p_header_data != 00);
7546 l_cp = &(p_j2k->m_cp);
7547 // come from tile part header or main header ?
7548 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
7549 &l_cp->tcps[p_j2k->m_current_tile_number] :
7550 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7552 // precondition again
7553 assert(p_comp_no < p_j2k->m_private_image->numcomps);
7555 l_tccp = &l_tcp->tccps[p_comp_no];
7556 l_current_ptr = p_header_data;
7558 if (*p_header_size < 1) {
7559 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
7562 *p_header_size -= 1;
7564 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
7567 l_tccp->qntsty = l_tmp & 0x1f;
7568 l_tccp->numgbits = l_tmp >> 5;
7569 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
7573 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
7575 (*p_header_size) / 2;
7577 if( l_num_band > J2K_MAXBANDS ) {
7578 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
7579 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
7580 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
7586 if (l_cp->correct) {
7588 /* if JPWL is on, we check whether there are too many subbands */
7589 if ((l_num_band < 0) || (l_num_band >= J2K_MAXBANDS)) {
7590 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
7591 "JPWL: bad number of subbands in Sqcx (%d)\n",
7594 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
7597 /* we try to correct */
7599 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
7600 "- setting number of bands to %d => HYPOTHESIS!!!\n",
7605 #endif /* USE_JPWL */
7607 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
7608 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
7609 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
7611 if (l_band_no < J2K_MAXBANDS){
7612 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
7613 l_tccp->stepsizes[l_band_no].mant = 0;
7616 *p_header_size = *p_header_size - l_num_band;
7619 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
7620 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
7622 if (l_band_no < J2K_MAXBANDS){
7623 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
7624 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
7627 *p_header_size = *p_header_size - 2*l_num_band;
7630 /* Add Antonin : if scalar_derived -> compute other stepsizes */
7631 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
7632 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
7633 l_tccp->stepsizes[l_band_no].expn =
7634 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
7635 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
7636 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
7644 * Copies the tile component parameters of all the component from the first tile component.
7646 * @param p_j2k the J2k codec.
7648 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
7651 opj_cp_v2_t *l_cp = NULL;
7652 opj_tcp_v2_t *l_tcp = NULL;
7653 opj_tccp_t *l_ref_tccp = NULL;
7654 opj_tccp_t *l_copied_tccp = NULL;
7658 assert(p_j2k != 00);
7660 l_cp = &(p_j2k->m_cp);
7661 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7662 &l_cp->tcps[p_j2k->m_current_tile_number] :
7663 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7665 l_ref_tccp = &l_tcp->tccps[0];
7666 l_copied_tccp = l_ref_tccp + 1;
7667 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
7669 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
7670 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
7671 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
7672 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
7678 * Dump some elements from the J2K decompression structure .
7680 *@param p_j2k the jpeg2000 codec.
7681 *@param flag flag to describe what elments are dump.
7682 *@param out_stream output stream where dump the elements.
7685 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
7687 /* Check if the flag is compatible with j2k file*/
7688 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
7689 fprintf(out_stream, "Wrong flag\n");
7693 /* Dump the image_header */
7694 if (flag & OPJ_IMG_INFO){
7695 if (p_j2k->m_private_image)
7696 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
7699 /* Dump the codestream info from main header */
7700 if (flag & OPJ_J2K_MH_INFO){
7701 j2k_dump_MH_info(p_j2k, out_stream);
7705 /* Dump the codestream info of the current tile */
7706 if (flag & OPJ_J2K_TH_INFO){
7710 /* Dump the codestream index from main header */
7711 if (flag & OPJ_J2K_MH_IND){
7712 j2k_dump_MH_index(p_j2k, out_stream);
7715 /* Dump the codestream index of the current tile */
7716 if (flag & OPJ_J2K_TH_IND){
7723 * Dump index elements of the codestream extract from the main header.
7725 *@param p_j2k the jpeg2000 codec.
7726 *@param out_stream output stream where dump the elements.
7729 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
7731 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
7732 OPJ_UINT32 it_marker, it_tile, it_tile_part;
7734 fprintf(out_stream, "Codestream index from main header: {\n");
7736 fprintf(out_stream, "\t Main header start position=%" OPJ_OFF_F "d\n"
7737 "\t Main header end position=%" OPJ_OFF_F "d\n",
7738 cstr_index->main_head_start, cstr_index->main_head_end);
7740 fprintf(out_stream, "\t Marker list: {\n");
7742 if (cstr_index->marker){
7743 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
7744 fprintf(out_stream, "\t\t type=%#x, pos=%" OPJ_OFF_F "d, len=%d\n",
7745 cstr_index->marker[it_marker].type,
7746 cstr_index->marker[it_marker].pos,
7747 cstr_index->marker[it_marker].len );
7751 fprintf(out_stream, "\t }\n");
7754 if (cstr_index->tile_index){
7755 fprintf(out_stream, "\t Tile index: {\n");
7757 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
7758 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
7760 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
7762 if (cstr_index->tile_index[it_tile].tp_index){
7763 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
7764 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" OPJ_OFF_F "d, end_header=%" OPJ_OFF_F "d, end_pos=%" OPJ_OFF_F "d.\n",
7766 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
7767 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
7768 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
7772 if (cstr_index->tile_index[it_tile].marker){
7773 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
7774 fprintf(out_stream, "\t\t type=%#x, pos=%" OPJ_OFF_F "d, len=%d\n",
7775 cstr_index->tile_index[it_tile].marker[it_marker].type,
7776 cstr_index->tile_index[it_tile].marker[it_marker].pos,
7777 cstr_index->tile_index[it_tile].marker[it_marker].len );
7781 fprintf(out_stream,"\t }\n");
7784 fprintf(out_stream,"}\n");
7789 * Dump info elements of the codestream extract from the main header.
7791 *@param p_j2k the jpeg2000 codec.
7792 *@param out_stream output stream where dump the elements.
7795 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
7797 opj_tcp_v2_t * l_default_tile=NULL;
7799 fprintf(out_stream, "Codestream info from main header: {\n");
7801 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
7802 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
7803 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
7805 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7809 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
7811 fprintf(out_stream, "\t default tile {\n");
7812 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
7813 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
7814 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
7815 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
7817 for (compno = 0; compno < numcomps; compno++) {
7818 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
7819 OPJ_INT32 resno, bandno, numbands;
7822 fprintf(out_stream, "\t\t comp %d {\n", compno);
7823 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
7824 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
7825 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
7826 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
7827 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
7828 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
7830 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
7831 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
7832 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
7834 fprintf(out_stream, "\n");
7836 /* quantization style*/
7837 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
7838 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
7839 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
7840 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
7841 for (bandno = 0; bandno < numbands; bandno++) {
7842 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
7843 l_tccp->stepsizes[bandno].expn);
7845 fprintf(out_stream, "\n");
7848 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
7850 fprintf(out_stream, "\t\t }\n");
7851 } /*end of component of default tile*/
7852 fprintf(out_stream, "\t }\n"); /*end of default tile*/
7856 fprintf(out_stream, "}\n");
7861 * Dump an image header structure.
7863 *@param img_header the image header to dump.
7864 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
7865 *@param out_stream output stream where dump the elements.
7867 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
7872 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
7876 fprintf(out_stream, "Image info {\n");
7877 tab[0] = '\t';tab[1] = '\0';
7880 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
7881 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
7882 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
7884 if (img_header->comps){
7886 for (compno = 0; compno < img_header->numcomps; compno++) {
7887 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
7888 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
7889 fprintf(out_stream,"%s}\n",tab);
7893 fprintf(out_stream, "}\n");
7897 * Dump a component image header structure.
7899 *@param comp_header the component image header to dump.
7900 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
7901 *@param out_stream output stream where dump the elements.
7903 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
7908 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
7911 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
7914 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
7915 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
7916 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
7919 fprintf(out_stream, "}\n");
7924 * Get the codestream info from a JPEG2000 codec.
7926 *@param p_j2k the component image header to dump.
7928 *@return the codestream information extract from the jpg2000 codec
7930 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
7933 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
7934 opj_tcp_v2_t *l_default_tile;
7935 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
7937 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
7939 cstr_info->tx0 = p_j2k->m_cp.tx0;
7940 cstr_info->ty0 = p_j2k->m_cp.ty0;
7941 cstr_info->tdx = p_j2k->m_cp.tdx;
7942 cstr_info->tdy = p_j2k->m_cp.tdy;
7943 cstr_info->tw = p_j2k->m_cp.tw;
7944 cstr_info->th = p_j2k->m_cp.th;
7946 cstr_info->tile_info = NULL; /* Not fill from the main header*/
7948 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7950 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
7951 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
7952 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
7953 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
7955 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
7957 for (compno = 0; compno < numcomps; compno++) {
7958 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
7959 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
7960 OPJ_INT32 bandno, numbands;
7963 l_tccp_info->csty = l_tccp->csty;
7964 l_tccp_info->numresolutions = l_tccp->numresolutions;
7965 l_tccp_info->cblkw = l_tccp->cblkw;
7966 l_tccp_info->cblkh = l_tccp->cblkh;
7967 l_tccp_info->cblksty = l_tccp->cblksty;
7968 l_tccp_info->qmfbid = l_tccp->qmfbid;
7969 if (l_tccp->numresolutions < J2K_MAXRLVLS)
7971 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
7972 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
7975 /* quantization style*/
7976 l_tccp_info->qntsty = l_tccp->qntsty;
7977 l_tccp_info->numgbits = l_tccp->numgbits;
7979 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
7980 if (numbands < J2K_MAXBANDS) {
7981 for (bandno = 0; bandno < numbands; bandno++) {
7982 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
7983 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
7988 l_tccp_info->roishift = l_tccp->roishift;
7996 * Get the codestream index from a JPEG2000 codec.
7998 *@param p_j2k the component image header to dump.
8000 *@return the codestream index extract from the jpg2000 codec
8002 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
8004 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
8005 opj_calloc(1,sizeof(opj_codestream_index_t));
8009 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
8010 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
8011 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
8013 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
8014 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
8015 if (!l_cstr_index->marker){
8016 opj_free( l_cstr_index);
8020 if (p_j2k->cstr_index->marker)
8021 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
8023 opj_free(l_cstr_index->marker);
8024 l_cstr_index->marker = NULL;
8027 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
8028 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
8029 if (!l_cstr_index->tile_index){
8030 opj_free( l_cstr_index->marker);
8031 opj_free( l_cstr_index);
8035 if (!p_j2k->cstr_index->tile_index){
8036 opj_free(l_cstr_index->tile_index);
8037 l_cstr_index->tile_index = NULL;
8040 OPJ_UINT32 it_tile = 0;
8041 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
8044 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
8046 l_cstr_index->tile_index[it_tile].marker =
8047 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
8049 if (!l_cstr_index->tile_index[it_tile].marker) {
8050 OPJ_UINT32 it_tile_free;
8052 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
8053 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
8056 opj_free( l_cstr_index->tile_index);
8057 opj_free( l_cstr_index->marker);
8058 opj_free( l_cstr_index);
8062 if (p_j2k->cstr_index->tile_index[it_tile].marker)
8063 memcpy( l_cstr_index->tile_index[it_tile].marker,
8064 p_j2k->cstr_index->tile_index[it_tile].marker,
8065 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
8067 opj_free(l_cstr_index->tile_index[it_tile].marker);
8068 l_cstr_index->tile_index[it_tile].marker = NULL;
8071 /* Tile part index*/
8072 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
8074 l_cstr_index->tile_index[it_tile].tp_index =
8075 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
8077 if(!l_cstr_index->tile_index[it_tile].tp_index){
8078 OPJ_UINT32 it_tile_free;
8080 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
8081 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
8082 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
8085 opj_free( l_cstr_index->tile_index);
8086 opj_free( l_cstr_index->marker);
8087 opj_free( l_cstr_index);
8091 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
8092 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
8093 p_j2k->cstr_index->tile_index[it_tile].tp_index,
8094 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
8097 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
8098 l_cstr_index->tile_index[it_tile].tp_index = NULL;
8101 /* Packet index (NOT USED)*/
8102 l_cstr_index->tile_index[it_tile].nb_packet = 0;
8103 l_cstr_index->tile_index[it_tile].packet_index = NULL;
8108 return l_cstr_index;
8111 opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
8113 OPJ_UINT32 it_tile=0;
8115 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
8116 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
8117 if (!p_j2k->cstr_index->tile_index)
8120 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
8121 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
8122 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
8123 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
8124 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
8125 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
8135 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
8136 opj_stream_private_t *p_stream,
8137 opj_event_mgr_t * p_manager)
8139 opj_bool l_go_on = OPJ_TRUE;
8140 OPJ_UINT32 l_current_tile_no;
8141 OPJ_UINT32 l_data_size,l_max_data_size;
8142 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
8143 OPJ_UINT32 l_nb_comps;
8144 OPJ_BYTE * l_current_data;
8146 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
8147 if (! l_current_data) {
8150 l_max_data_size = 1000;
8152 /*Allocate and initialize some elements of codestrem index*/
8153 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8154 opj_free(l_current_data);
8159 if (! j2k_read_tile_header( p_j2k,
8162 &l_tile_x0, &l_tile_y0,
8163 &l_tile_x1, &l_tile_y1,
8168 opj_free(l_current_data);
8176 if (l_data_size > l_max_data_size) {
8177 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
8178 if (! l_current_data) {
8179 opj_free(l_current_data);
8183 l_max_data_size = l_data_size;
8186 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
8187 opj_free(l_current_data);
8190 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);
8192 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
8193 opj_free(l_current_data);
8196 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
8200 opj_free(l_current_data);
8206 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
8208 void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
8211 assert(p_j2k != 00);
8213 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_tiles);
8214 /* DEVELOPER CORNER, add your custom procedures */
8219 * Read and decode one tile.
8221 opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
8222 opj_stream_private_t *p_stream,
8223 opj_event_mgr_t * p_manager)
8225 opj_bool l_go_on = OPJ_TRUE;
8226 OPJ_UINT32 l_current_tile_no;
8227 OPJ_UINT32 l_tile_no_to_dec;
8228 OPJ_UINT32 l_data_size,l_max_data_size;
8229 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
8230 OPJ_UINT32 l_nb_comps;
8231 OPJ_BYTE * l_current_data;
8233 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
8234 if (! l_current_data) {
8237 l_max_data_size = 1000;
8239 /*Allocate and initialize some elements of codestrem index if not already done*/
8240 if( !p_j2k->cstr_index->tile_index)
8242 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8243 opj_free(l_current_data);
8247 /* Move into the codestream to the first SOT used to decode the desired tile */
8248 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
8249 if (p_j2k->cstr_index->tile_index)
8250 if(p_j2k->cstr_index->tile_index->tp_index)
8252 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
8253 /* the index for this tile has not been built,
8254 * so move to the last SOT read */
8255 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
8256 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
8261 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)) {
8262 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
8266 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
8267 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
8268 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8272 if (! j2k_read_tile_header( p_j2k,
8275 &l_tile_x0, &l_tile_y0,
8276 &l_tile_x1, &l_tile_y1,
8281 opj_free(l_current_data);
8290 if (l_data_size > l_max_data_size) {
8291 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
8292 if (! l_current_data) {
8293 opj_free(l_current_data);
8297 l_max_data_size = l_data_size;
8302 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
8303 opj_free(l_current_data);
8306 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);
8308 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
8309 opj_free(l_current_data);
8312 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
8314 if(l_current_tile_no == l_tile_no_to_dec)
8316 /* move into the codestream to the the first SOT (FIXME or not move?)*/
8317 if (opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) {
8318 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
8324 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);
8329 opj_free(l_current_data);
8336 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
8338 void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
8341 assert(p_j2k != 00);
8343 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_one_tile);
8344 /* DEVELOPER CORNER, add your custom procedures */
8350 * Decodes the tiles of the stream.
8352 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
8353 opj_stream_private_t * p_stream,
8354 opj_image_t * p_image,
8355 opj_event_mgr_t * p_manager)
8362 p_j2k->m_output_image = opj_image_create0();
8363 if (! (p_j2k->m_output_image)) {
8366 opj_copy_image_header(p_image, p_j2k->m_output_image);
8368 /* customization of the decoding */
8369 j2k_setup_decoding(p_j2k);
8371 /* Decode the codestream */
8372 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8373 opj_image_destroy(p_j2k->m_private_image);
8374 p_j2k->m_private_image = NULL;
8378 /* Move data and copy one information from codec to output image*/
8379 for (compno = 0; compno < p_image->numcomps; compno++) {
8380 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
8381 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
8382 p_j2k->m_output_image->comps[compno].data = NULL;
8390 * Get the decoded tile.
8392 * @param p_j2k the jpeg2000 codestream codec.
8393 * @param p_stream input_stream
8394 * @param p_image output image. .
8395 * @param p_manager the user event manager
8396 * @param tile_index index of the tile we want decode
8398 * @return true if succeed.
8400 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
8401 opj_stream_private_t *p_stream,
8402 opj_image_t* p_image,
8403 struct opj_event_mgr * p_manager,
8404 OPJ_UINT32 tile_index )
8407 OPJ_UINT32 l_tile_x, l_tile_y;
8408 opj_image_comp_t* l_img_comp;
8411 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
8415 if ( (tile_index < 0) && (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
8416 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);
8420 /* Compute the dimension of the desired tile*/
8421 l_tile_x = tile_index % p_j2k->m_cp.tw;
8422 l_tile_y = tile_index / p_j2k->m_cp.tw;
8424 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
8425 if (p_image->x0 < p_j2k->m_private_image->x0)
8426 p_image->x0 = p_j2k->m_private_image->x0;
8427 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
8428 if (p_image->x1 > p_j2k->m_private_image->x1)
8429 p_image->x1 = p_j2k->m_private_image->x1;
8431 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
8432 if (p_image->y0 < p_j2k->m_private_image->y0)
8433 p_image->y0 = p_j2k->m_private_image->y0;
8434 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
8435 if (p_image->y1 > p_j2k->m_private_image->y1)
8436 p_image->y1 = p_j2k->m_private_image->y1;
8438 l_img_comp = p_image->comps;
8439 for (compno=0; compno < p_image->numcomps; ++compno)
8441 OPJ_INT32 l_comp_x1, l_comp_y1;
8443 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
8445 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
8446 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
8447 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
8448 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
8450 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
8451 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
8456 /* Destroy the previous output image*/
8457 if (p_j2k->m_output_image)
8458 opj_image_destroy(p_j2k->m_output_image);
8460 /* Create the ouput image from the information previously computed*/
8461 p_j2k->m_output_image = opj_image_create0();
8462 if (! (p_j2k->m_output_image)) {
8465 opj_copy_image_header(p_image, p_j2k->m_output_image);
8467 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
8469 /* customization of the decoding */
8470 j2k_setup_decoding_tile(p_j2k);
8472 /* Decode the codestream */
8473 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8474 opj_image_destroy(p_j2k->m_private_image);
8475 p_j2k->m_private_image = NULL;
8479 /* Move data and copy one information from codec to output image*/
8480 for (compno = 0; compno < p_image->numcomps; compno++) {
8481 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
8483 if (p_image->comps[compno].data)
8484 opj_free(p_image->comps[compno].data);
8486 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
8488 p_j2k->m_output_image->comps[compno].data = NULL;
8494 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
8498 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
8500 if (p_j2k->m_private_image) {
8501 if (p_j2k->m_private_image->comps) {
8502 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
8503 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
8504 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
8505 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
8506 if ( res_factor >= max_res){
8507 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
8510 p_j2k->m_private_image->comps[it_comp].factor = res_factor;