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) 2006-2007, Parvatha Elangovan
9 * Copyright (c) 2010-2011, Kaori Hagihara
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include "opj_includes.h"
36 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
39 /** @name Local static functions */
43 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
45 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
48 * The read header procedure.
50 opj_bool j2k_read_header_procedure(
52 struct opj_stream_private *p_stream,
53 struct opj_event_mgr * p_manager);
56 * The default decoding validation procedure without any extension.
58 * @param p_j2k the jpeg2000 codec to validate.
59 * @param p_stream the input stream to validate.
60 * @param p_manager the user event manager.
62 * @return true if the parameters are correct.
64 opj_bool j2k_decoding_validation (
66 opj_stream_private_t *p_stream,
67 opj_event_mgr_t * p_manager
71 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
72 * are valid. Developpers wanting to extend the library can add their own validation procedures.
74 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
77 * Builds the tcd decoder to use to decode tile.
79 opj_bool j2k_build_decoder (
81 opj_stream_private_t *p_stream,
82 opj_event_mgr_t * p_manager
86 * Excutes the given procedures on the given codec.
88 * @param p_procedure_list the list of procedures to execute
89 * @param p_j2k the jpeg2000 codec to execute the procedures on.
90 * @param p_stream the stream to execute the procedures on.
91 * @param p_manager the user manager.
93 * @return true if all the procedures were successfully executed.
95 static opj_bool j2k_exec (
97 opj_procedure_list_t * p_procedure_list,
98 opj_stream_private_t *p_stream,
99 opj_event_mgr_t * p_manager
103 * Copies the decoding tile parameters onto all the tile parameters.
104 * Creates also the tile decoder.
106 opj_bool j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
107 opj_stream_private_t *p_stream,
108 opj_event_mgr_t * p_manager );
111 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
112 * with the marker value.
113 * @param p_id Marker value to look up
115 * @return the handler associated with the id.
117 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
120 * Destroys a tile coding parameter structure.
122 * @param p_tcp the tile coding parameter to destroy.
124 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
128 * Destroys a coding parameter structure.
130 * @param p_cp the coding parameter to destroy.
132 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
136 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
137 * @param p_header_data the data contained in the COM box.
138 * @param p_j2k the jpeg2000 codec.
139 * @param p_header_size the size of the data contained in the COM marker.
140 * @param p_manager the user event manager.
142 static opj_bool j2k_read_SPCod_SPCoc(
145 OPJ_BYTE * p_header_data,
146 OPJ_UINT32 * p_header_size,
147 struct opj_event_mgr * p_manager
151 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
153 * @param p_tile_no the tile to output.
154 * @param p_comp_no the component number to output.
155 * @param p_data the data buffer.
156 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
157 * @param p_j2k J2K codec.
158 * @param p_manager the user event manager.
161 static opj_bool j2k_read_SQcd_SQcc(
164 OPJ_BYTE * p_header_data,
165 OPJ_UINT32 * p_header_size,
166 struct opj_event_mgr * p_manager
170 * Copies the tile component parameters of all the component from the first tile component.
172 * @param p_j2k the J2k codec.
174 static void j2k_copy_tile_component_parameters(
179 * Copies the tile quantization parameters of all the component from the first tile component.
181 * @param p_j2k the J2k codec.
183 static void j2k_copy_tile_quantization_parameters(
190 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
191 opj_stream_private_t *p_stream,
192 opj_event_mgr_t * p_manager);
194 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
198 * -----------------------------------------------------------------------
199 * -----------------------------------------------------------------------
200 * -----------------------------------------------------------------------
204 Write the SOC marker (Start Of Codestream)
205 @param j2k J2K handle
207 static void j2k_write_soc(opj_j2k_t *j2k);
209 Read the SOC marker (Start of Codestream)
210 @param j2k J2K handle
212 static void j2k_read_soc(opj_j2k_t *j2k);
215 * Reads a SOC marker (Start of Codestream)
216 * @param p_header_data the data contained in the SOC box.
217 * @param jp2 the jpeg2000 file codec.
218 * @param p_header_size the size of the data contained in the SOC marker.
219 * @param p_manager the user event manager.
221 static opj_bool j2k_read_soc_v2(
223 struct opj_stream_private *p_stream,
224 struct opj_event_mgr * p_manager
228 Write the SIZ marker (image and tile size)
229 @param j2k J2K handle
231 static void j2k_write_siz(opj_j2k_t *j2k);
233 Read the SIZ marker (image and tile size)
234 @param j2k J2K handle
236 static void j2k_read_siz(opj_j2k_t *j2k);
239 * Reads a SIZ marker (image and tile size)
240 * @param p_header_data the data contained in the SIZ box.
241 * @param jp2 the jpeg2000 file codec.
242 * @param p_header_size the size of the data contained in the SIZ marker.
243 * @param p_manager the user event manager.
245 static opj_bool j2k_read_siz_v2 (
247 OPJ_BYTE * p_header_data,
248 OPJ_UINT32 p_header_size,
249 struct opj_event_mgr * p_manager
253 Write the COM marker (comment)
254 @param j2k J2K handle
256 static void j2k_write_com(opj_j2k_t *j2k);
258 Read the COM marker (comment)
259 @param j2k J2K handle
261 static void j2k_read_com(opj_j2k_t *j2k);
263 * Reads a COM marker (comments)
264 * @param p_header_data the data contained in the COM box.
265 * @param jp2 the jpeg2000 file codec.
266 * @param p_header_size the size of the data contained in the COM marker.
267 * @param p_manager the user event manager.
269 static opj_bool j2k_read_com_v2 (
271 OPJ_BYTE * p_header_data,
272 OPJ_UINT32 p_header_size,
273 struct opj_event_mgr * p_manager
276 Write the value concerning the specified component in the marker COD and COC
277 @param j2k J2K handle
278 @param compno Number of the component concerned by the information written
280 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
282 Read the value concerning the specified component in the marker COD and COC
283 @param j2k J2K handle
284 @param compno Number of the component concerned by the information read
286 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
288 Write the COD marker (coding style default)
289 @param j2k J2K handle
291 static void j2k_write_cod(opj_j2k_t *j2k);
293 Read the COD marker (coding style default)
294 @param j2k J2K handle
296 static void j2k_read_cod(opj_j2k_t *j2k);
299 * Reads a COD marker (Coding Styke defaults)
300 * @param p_header_data the data contained in the COD box.
301 * @param p_j2k the jpeg2000 codec.
302 * @param p_header_size the size of the data contained in the COD marker.
303 * @param p_manager the user event manager.
305 static opj_bool j2k_read_cod_v2 (
307 OPJ_BYTE * p_header_data,
308 OPJ_UINT32 p_header_size,
309 struct opj_event_mgr * p_manager
313 Write the COC marker (coding style component)
314 @param j2k J2K handle
315 @param compno Number of the component concerned by the information written
317 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
319 Read the COC marker (coding style component)
320 @param j2k J2K handle
322 static void j2k_read_coc(opj_j2k_t *j2k);
325 * Reads a COC marker (Coding Style Component)
326 * @param p_header_data the data contained in the COC box.
327 * @param p_j2k the jpeg2000 codec.
328 * @param p_header_size the size of the data contained in the COC marker.
329 * @param p_manager the user event manager.
331 static opj_bool j2k_read_coc_v2 (
333 OPJ_BYTE * p_header_data,
334 OPJ_UINT32 p_header_size,
335 struct opj_event_mgr * p_manager
339 Write the value concerning the specified component in the marker QCD and QCC
340 @param j2k J2K handle
341 @param compno Number of the component concerned by the information written
343 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
345 Read the value concerning the specified component in the marker QCD and QCC
346 @param j2k J2K handle
347 @param compno Number of the component concern by the information read
348 @param len Length of the information in the QCX part of the marker QCD/QCC
350 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
352 Write the QCD marker (quantization default)
353 @param j2k J2K handle
355 static void j2k_write_qcd(opj_j2k_t *j2k);
357 Read the QCD marker (quantization default)
358 @param j2k J2K handle
360 static void j2k_read_qcd(opj_j2k_t *j2k);
363 * Reads a QCD marker (Quantization defaults)
364 * @param p_header_data the data contained in the QCD box.
365 * @param p_j2k the jpeg2000 codec.
366 * @param p_header_size the size of the data contained in the QCD marker.
367 * @param p_manager the user event manager.
369 static opj_bool j2k_read_qcd_v2 (
371 OPJ_BYTE * p_header_data,
372 OPJ_UINT32 p_header_size,
373 struct opj_event_mgr * p_manager
377 Write the QCC marker (quantization component)
378 @param j2k J2K handle
379 @param compno Number of the component concerned by the information written
381 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
383 Read the QCC marker (quantization component)
384 @param j2k J2K handle
386 static void j2k_read_qcc(opj_j2k_t *j2k);
388 * Reads a QCC marker (Quantization component)
389 * @param p_header_data the data contained in the QCC box.
390 * @param p_j2k the jpeg2000 codec.
391 * @param p_header_size the size of the data contained in the QCC marker.
392 * @param p_manager the user event manager.
394 static opj_bool j2k_read_qcc_v2(
396 OPJ_BYTE * p_header_data,
397 OPJ_UINT32 p_header_size,
398 struct opj_event_mgr * p_manager);
401 Write the POC marker (progression order change)
402 @param j2k J2K handle
404 static void j2k_write_poc(opj_j2k_t *j2k);
406 Read the POC marker (progression order change)
407 @param j2k J2K handle
409 static void j2k_read_poc(opj_j2k_t *j2k);
411 * Reads a POC marker (Progression Order Change)
413 * @param p_header_data the data contained in the POC box.
414 * @param p_j2k the jpeg2000 codec.
415 * @param p_header_size the size of the data contained in the POC marker.
416 * @param p_manager the user event manager.
418 static opj_bool j2k_read_poc_v2 (
420 OPJ_BYTE * p_header_data,
421 OPJ_UINT32 p_header_size,
422 struct opj_event_mgr * p_manager
425 Read the CRG marker (component registration)
426 @param j2k J2K handle
428 static void j2k_read_crg(opj_j2k_t *j2k);
430 * Reads a CRG marker (Component registration)
432 * @param p_header_data the data contained in the TLM box.
433 * @param p_j2k the jpeg2000 codec.
434 * @param p_header_size the size of the data contained in the TLM marker.
435 * @param p_manager the user event manager.
437 static opj_bool j2k_read_crg_v2 (
439 OPJ_BYTE * p_header_data,
440 OPJ_UINT32 p_header_size,
441 struct opj_event_mgr * p_manager
444 Read the TLM marker (tile-part lengths)
445 @param j2k J2K handle
447 static void j2k_read_tlm(opj_j2k_t *j2k);
449 * Reads a TLM marker (Tile Length Marker)
451 * @param p_header_data the data contained in the TLM box.
452 * @param p_j2k the jpeg2000 codec.
453 * @param p_header_size the size of the data contained in the TLM marker.
454 * @param p_manager the user event manager.
456 static opj_bool j2k_read_tlm_v2 (
458 OPJ_BYTE * p_header_data,
459 OPJ_UINT32 p_header_size,
460 struct opj_event_mgr * p_manager
463 Read the PLM marker (packet length, main header)
464 @param j2k J2K handle
466 static void j2k_read_plm(opj_j2k_t *j2k);
469 * Reads a PLM marker (Packet length, main header marker)
471 * @param p_header_data the data contained in the TLM box.
472 * @param p_j2k the jpeg2000 codec.
473 * @param p_header_size the size of the data contained in the TLM marker.
474 * @param p_manager the user event manager.
476 static opj_bool j2k_read_plm_v2 (
478 OPJ_BYTE * p_header_data,
479 OPJ_UINT32 p_header_size,
480 struct opj_event_mgr * p_manager
483 Read the PLT marker (packet length, tile-part header)
484 @param j2k J2K handle
486 static void j2k_read_plt(opj_j2k_t *j2k);
488 * Reads a PLT marker (Packet length, tile-part header)
490 * @param p_header_data the data contained in the PLT box.
491 * @param p_j2k the jpeg2000 codec.
492 * @param p_header_size the size of the data contained in the PLT marker.
493 * @param p_manager the user event manager.
495 static opj_bool j2k_read_plt_v2 (
497 OPJ_BYTE * p_header_data,
498 OPJ_UINT32 p_header_size,
499 struct opj_event_mgr * p_manager
502 Read the PPM marker (packet packet headers, main header)
503 @param j2k J2K handle
505 static void j2k_read_ppm(opj_j2k_t *j2k);
507 * Reads a PPM marker (Packed packet headers, main header)
509 * @param p_header_data the data contained in the POC box.
510 * @param p_j2k the jpeg2000 codec.
511 * @param p_header_size the size of the data contained in the POC marker.
512 * @param p_manager the user event manager.
514 static opj_bool j2k_read_ppm_v2 (
516 OPJ_BYTE * p_header_data,
517 OPJ_UINT32 p_header_size,
518 struct opj_event_mgr * p_manager
521 static opj_bool j2k_read_ppm_v3 (
523 OPJ_BYTE * p_header_data,
524 OPJ_UINT32 p_header_size,
525 struct opj_event_mgr * p_manager
529 Read the PPT marker (packet packet headers, tile-part header)
530 @param j2k J2K handle
532 static void j2k_read_ppt(opj_j2k_t *j2k);
534 * Reads a PPT marker (Packed packet headers, tile-part header)
536 * @param p_header_data the data contained in the PPT box.
537 * @param p_j2k the jpeg2000 codec.
538 * @param p_header_size the size of the data contained in the PPT marker.
539 * @param p_manager the user event manager.
541 static opj_bool j2k_read_ppt_v2 (
543 OPJ_BYTE * p_header_data,
544 OPJ_UINT32 p_header_size,
545 struct opj_event_mgr * p_manager
548 Write the TLM marker (Mainheader)
549 @param j2k J2K handle
551 static void j2k_write_tlm(opj_j2k_t *j2k);
553 Write the SOT marker (start of tile-part)
554 @param j2k J2K handle
556 static void j2k_write_sot(opj_j2k_t *j2k);
558 Read the SOT marker (start of tile-part)
559 @param j2k J2K handle
561 static void j2k_read_sot(opj_j2k_t *j2k);
564 * Reads a PPT marker (Packed packet headers, tile-part header)
566 * @param p_header_data the data contained in the PPT box.
567 * @param p_j2k the jpeg2000 codec.
568 * @param p_header_size the size of the data contained in the PPT marker.
569 * @param p_manager the user event manager.
571 static opj_bool j2k_read_sot_v2 (
573 OPJ_BYTE * p_header_data,
574 OPJ_UINT32 p_header_size,
575 struct opj_event_mgr * p_manager
578 Write the SOD marker (start of data)
579 @param j2k J2K handle
580 @param tile_coder Pointer to a TCD handle
582 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
584 Read the SOD marker (start of data)
585 @param j2k J2K handle
587 static void j2k_read_sod(opj_j2k_t *j2k);
590 * Reads a SOD marker (Start Of Data)
592 * @param p_header_data the data contained in the SOD box.
593 * @param p_j2k the jpeg2000 codec.
594 * @param p_header_size the size of the data contained in the SOD marker.
595 * @param p_manager the user event manager.
597 static opj_bool j2k_read_sod_v2 (
599 struct opj_stream_private *p_stream,
600 struct opj_event_mgr * p_manager
604 Write the RGN marker (region-of-interest)
605 @param j2k J2K handle
606 @param compno Number of the component concerned by the information written
607 @param tileno Number of the tile concerned by the information written
609 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
611 Read the RGN marker (region-of-interest)
612 @param j2k J2K handle
614 static void j2k_read_rgn(opj_j2k_t *j2k);
617 * Reads a RGN marker (Region Of Interest)
619 * @param p_header_data the data contained in the POC box.
620 * @param p_j2k the jpeg2000 codec.
621 * @param p_header_size the size of the data contained in the POC marker.
622 * @param p_manager the user event manager.
624 static opj_bool j2k_read_rgn_v2 (
626 OPJ_BYTE * p_header_data,
627 OPJ_UINT32 p_header_size,
628 struct opj_event_mgr * p_manager
632 Write the EOC marker (end of codestream)
633 @param j2k J2K handle
635 static void j2k_write_eoc(opj_j2k_t *j2k);
637 Read the EOC marker (end of codestream)
638 @param j2k J2K handle
640 static void j2k_read_eoc(opj_j2k_t *j2k);
643 * Reads a EOC marker (End Of Codestream)
645 * @param p_header_data the data contained in the SOD box.
646 * @param p_j2k the jpeg2000 codec.
647 * @param p_header_size the size of the data contained in the SOD marker.
648 * @param p_manager the user event manager.
650 static opj_bool j2k_read_eoc_v2 (
652 struct opj_stream_private *p_stream,
653 struct opj_event_mgr * p_manager
657 Read an unknown marker
658 @param j2k J2K handle
660 static void j2k_read_unk(opj_j2k_t *j2k);
662 Add main header marker information
663 @param cstr_info Codestream information structure
664 @param type marker type
665 @param pos byte offset of marker segment
666 @param len length of marker segment
668 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
670 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) ;
672 Add tile header marker information
673 @param tileno tile index number
674 @param cstr_info Codestream information structure
675 @param type marker type
676 @param pos byte offset of marker segment
677 @param len length of marker segment
679 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
681 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len);
684 * Reads an unknown marker
686 * @param p_stream the stream object to read from.
687 * @param p_j2k the jpeg2000 codec.
688 * @param p_manager the user event manager.
690 * @return true if the marker could be deduced.
692 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
693 struct opj_stream_private *p_stream,
694 OPJ_UINT32 *output_marker,
695 struct opj_event_mgr * p_manager );
698 * Reads a MCT marker (Multiple Component Transform)
700 * @param p_header_data the data contained in the MCT box.
701 * @param p_j2k the jpeg2000 codec.
702 * @param p_header_size the size of the data contained in the MCT marker.
703 * @param p_manager the user event manager.
705 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
706 OPJ_BYTE * p_header_data,
707 OPJ_UINT32 p_header_size,
708 struct opj_event_mgr * p_manager );
711 * Reads a MCC marker (Multiple Component Collection)
713 * @param p_header_data the data contained in the MCC box.
714 * @param p_j2k the jpeg2000 codec.
715 * @param p_header_size the size of the data contained in the MCC marker.
716 * @param p_manager the user event manager.
718 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
719 OPJ_BYTE * p_header_data,
720 OPJ_UINT32 p_header_size,
721 struct opj_event_mgr * p_manager );
724 * Reads a MCO marker (Multiple Component Transform Ordering)
726 * @param p_header_data the data contained in the MCO box.
727 * @param p_j2k the jpeg2000 codec.
728 * @param p_header_size the size of the data contained in the MCO marker.
729 * @param p_manager the user event manager.
731 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
732 OPJ_BYTE * p_header_data,
733 OPJ_UINT32 p_header_size,
734 struct opj_event_mgr * p_manager );
736 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
738 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
739 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
740 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
741 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
743 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
744 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
745 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
746 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
748 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
749 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
750 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
751 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
754 * Reads a CBD marker (Component bit depth definition)
755 * @param p_header_data the data contained in the CBD box.
756 * @param p_j2k the jpeg2000 codec.
757 * @param p_header_size the size of the data contained in the CBD marker.
758 * @param p_manager the user event manager.
760 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
761 OPJ_BYTE * p_header_data,
762 OPJ_UINT32 p_header_size,
763 struct opj_event_mgr * p_manager);
766 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
768 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
770 static opj_codestream_index_t* j2k_create_cstr_index(void);
776 /* ----------------------------------------------------------------------- */
777 typedef struct j2k_prog_order{
778 OPJ_PROG_ORDER enum_prog;
782 j2k_prog_order_t j2k_prog_order_list[] = {
788 {(OPJ_PROG_ORDER)-1, ""}
796 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
804 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
806 const j2k_mct_function j2k_mct_read_functions_to_float [] =
808 j2k_read_int16_to_float,
809 j2k_read_int32_to_float,
810 j2k_read_float32_to_float,
811 j2k_read_float64_to_float
814 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
816 j2k_read_int16_to_int32,
817 j2k_read_int32_to_int32,
818 j2k_read_float32_to_int32,
819 j2k_read_float64_to_int32
822 const j2k_mct_function j2k_mct_write_functions_from_float [] =
824 j2k_write_float_to_int16,
825 j2k_write_float_to_int32,
826 j2k_write_float_to_float,
827 j2k_write_float_to_float64
830 typedef struct opj_dec_memory_marker_handler
834 /** value of the state when the marker can appear */
836 /** action linked to the marker */
837 opj_bool (*handler) (
839 OPJ_BYTE * p_header_data,
840 OPJ_UINT32 p_header_size,
841 struct opj_event_mgr * p_manager
844 opj_dec_memory_marker_handler_t;
846 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
849 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
850 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
851 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
852 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
853 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
854 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
855 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
856 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
857 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
858 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
859 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
860 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
861 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
863 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
864 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
865 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
866 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
867 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
868 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
870 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
871 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
872 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
873 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
874 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
875 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
876 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
877 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
878 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
879 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
880 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
881 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
882 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
884 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
885 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
886 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
887 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
888 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
889 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
891 #ifdef TODO_MS /* FIXME */
892 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
893 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
894 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
895 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
897 #endif /* USE_JPWL */
899 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
900 {J2K_MS_INSEC, 0, j2k_read_insec}
901 #endif /* USE_JPSEC */
902 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}//j2k_read_unk_v2}
907 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
909 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
910 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
914 for (i=0;i<p_nb_elem;++i) {
915 opj_read_bytes(l_src_data,&l_temp,2);
917 l_src_data+=sizeof(OPJ_INT16);
919 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
923 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
925 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
926 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
930 for (i=0;i<p_nb_elem;++i) {
931 opj_read_bytes(l_src_data,&l_temp,4);
933 l_src_data+=sizeof(OPJ_INT32);
935 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
939 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
941 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
942 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
946 for (i=0;i<p_nb_elem;++i) {
947 opj_read_float(l_src_data,&l_temp);
949 l_src_data+=sizeof(OPJ_FLOAT32);
951 *(l_dest_data++) = l_temp;
955 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
957 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
958 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
962 for (i=0;i<p_nb_elem;++i) {
963 opj_read_double(l_src_data,&l_temp);
965 l_src_data+=sizeof(OPJ_FLOAT64);
967 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
971 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
973 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
974 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
978 for (i=0;i<p_nb_elem;++i) {
979 opj_read_bytes(l_src_data,&l_temp,2);
981 l_src_data+=sizeof(OPJ_INT16);
983 *(l_dest_data++) = (OPJ_INT32) l_temp;
987 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
989 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
990 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
994 for (i=0;i<p_nb_elem;++i) {
995 opj_read_bytes(l_src_data,&l_temp,4);
997 l_src_data+=sizeof(OPJ_INT32);
999 *(l_dest_data++) = (OPJ_INT32) l_temp;
1003 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1005 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1006 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1010 for (i=0;i<p_nb_elem;++i) {
1011 opj_read_float(l_src_data,&l_temp);
1013 l_src_data+=sizeof(OPJ_FLOAT32);
1015 *(l_dest_data++) = (OPJ_INT32) l_temp;
1019 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1021 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1022 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1026 for (i=0;i<p_nb_elem;++i) {
1027 opj_read_double(l_src_data,&l_temp);
1029 l_src_data+=sizeof(OPJ_FLOAT64);
1031 *(l_dest_data++) = (OPJ_INT32) l_temp;
1035 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1037 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1038 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1042 for (i=0;i<p_nb_elem;++i) {
1043 l_temp = (OPJ_UINT32) *(l_src_data++);
1045 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1047 l_dest_data+=sizeof(OPJ_INT16);
1051 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1053 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1054 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1058 for (i=0;i<p_nb_elem;++i) {
1059 l_temp = (OPJ_UINT32) *(l_src_data++);
1061 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1063 l_dest_data+=sizeof(OPJ_INT32);
1067 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1069 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1070 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1074 for (i=0;i<p_nb_elem;++i) {
1075 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1077 opj_write_float(l_dest_data,l_temp);
1079 l_dest_data+=sizeof(OPJ_FLOAT32);
1083 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1085 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1086 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1090 for (i=0;i<p_nb_elem;++i) {
1091 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1093 opj_write_double(l_dest_data,l_temp);
1095 l_dest_data+=sizeof(OPJ_FLOAT64);
1101 * Converts an enum type progression order to string type.
1103 * @param prg_order the progression order to get.
1105 * @return the string representation of the given progression order.
1107 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1108 j2k_prog_order_t *po;
1109 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1110 if(po->enum_prog == prg_order){
1111 return po->str_prog;
1114 return po->str_prog;
1117 /* ----------------------------------------------------------------------- */
1118 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1121 int tpnum=1,tpend=0;
1122 opj_tcp_t *tcp = &cp->tcps[tileno];
1123 prog = j2k_convert_progression_order(tcp->prg);
1128 if( cp->tp_flag == prog[i] ){
1129 tpend=1;cp->tp_pos=i;
1133 tpnum= tpnum * tcp->pocs[pino].compE;
1136 tpnum= tpnum * tcp->pocs[pino].resE;
1139 tpnum= tpnum * tcp->pocs[pino].prcE;
1142 tpnum= tpnum * tcp->pocs[pino].layE;
1153 /** mem allocation for TLM marker*/
1154 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1155 int pino,tileno,totnum_tp=0;
1157 OPJ_ARG_NOT_USED(img_numcomp);
1159 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1160 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1161 int cur_totnum_tp = 0;
1162 opj_tcp_t *tcp = &cp->tcps[tileno];
1163 for(pino = 0; pino <= tcp->numpocs; pino++) {
1165 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1166 if(!pi) { return -1;}
1167 tp_num = j2k_get_num_tp(cp,pino,tileno);
1168 totnum_tp = totnum_tp + tp_num;
1169 cur_totnum_tp = cur_totnum_tp + tp_num;
1170 pi_destroy(pi, cp, tileno);
1172 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1174 if (j2k->cstr_info) {
1175 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1176 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1183 static void j2k_write_soc(opj_j2k_t *j2k) {
1184 opj_cio_t *cio = j2k->cio;
1185 cio_write(cio, J2K_MS_SOC, 2);
1188 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1193 /* update markers struct */
1194 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1195 #endif /* USE_JPWL */
1199 static void j2k_read_soc(opj_j2k_t *j2k) {
1200 j2k->state = J2K_STATE_MHSIZ;
1202 if (j2k->cstr_info) {
1203 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
1204 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
1209 * Reads a SOC marker (Start of Codestream)
1210 * @param p_header_data the data contained in the SOC box.
1211 * @param jp2 the jpeg2000 file codec.
1212 * @param p_header_size the size of the data contained in the SOC marker.
1213 * @param p_manager the user event manager.
1215 static opj_bool j2k_read_soc_v2( opj_j2k_v2_t *p_j2k,
1216 struct opj_stream_private *p_stream,
1217 struct opj_event_mgr * p_manager )
1219 OPJ_BYTE l_data [2];
1220 OPJ_UINT32 l_marker;
1223 assert(p_j2k != 00);
1224 assert(p_manager != 00);
1225 assert(p_stream != 00);
1227 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1231 opj_read_bytes(l_data,&l_marker,2);
1232 if (l_marker != J2K_MS_SOC) {
1236 /* Next marker should be a SIZ marker in the main header */
1237 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1239 /* FIXME move it in a index structure included in p_j2k*/
1240 p_j2k->cstr_index->main_head_start = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
1242 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1244 /* Add the marker to the codestream index*/
1245 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1250 static void j2k_write_siz(opj_j2k_t *j2k) {
1254 opj_cio_t *cio = j2k->cio;
1255 opj_image_t *image = j2k->image;
1256 opj_cp_t *cp = j2k->cp;
1258 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
1259 lenp = cio_tell(cio);
1261 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
1262 cio_write(cio, image->x1, 4); /* Xsiz */
1263 cio_write(cio, image->y1, 4); /* Ysiz */
1264 cio_write(cio, image->x0, 4); /* X0siz */
1265 cio_write(cio, image->y0, 4); /* Y0siz */
1266 cio_write(cio, cp->tdx, 4); /* XTsiz */
1267 cio_write(cio, cp->tdy, 4); /* YTsiz */
1268 cio_write(cio, cp->tx0, 4); /* XT0siz */
1269 cio_write(cio, cp->ty0, 4); /* YT0siz */
1270 cio_write(cio, image->numcomps, 2); /* Csiz */
1271 for (i = 0; i < image->numcomps; i++) {
1272 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
1273 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
1274 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
1276 len = cio_tell(cio) - lenp;
1277 cio_seek(cio, lenp);
1278 cio_write(cio, len, 2); /* Lsiz */
1279 cio_seek(cio, lenp + len);
1282 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
1285 static void j2k_read_siz(opj_j2k_t *j2k) {
1288 opj_cio_t *cio = j2k->cio;
1289 opj_image_t *image = j2k->image;
1290 opj_cp_t *cp = j2k->cp;
1292 len = cio_read(cio, 2); /* Lsiz */
1293 cio_read(cio, 2); /* Rsiz (capabilities) */
1294 image->x1 = cio_read(cio, 4); /* Xsiz */
1295 image->y1 = cio_read(cio, 4); /* Ysiz */
1296 image->x0 = cio_read(cio, 4); /* X0siz */
1297 image->y0 = cio_read(cio, 4); /* Y0siz */
1298 cp->tdx = cio_read(cio, 4); /* XTsiz */
1299 cp->tdy = cio_read(cio, 4); /* YTsiz */
1300 cp->tx0 = cio_read(cio, 4); /* XT0siz */
1301 cp->ty0 = cio_read(cio, 4); /* YT0siz */
1303 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
1304 opj_event_msg(j2k->cinfo, EVT_ERROR,
1305 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
1306 image->x0,image->x1,image->y0,image->y1);
1310 image->numcomps = cio_read(cio, 2); /* Csiz */
1313 if (j2k->cp->correct) {
1314 /* if JPWL is on, we check whether TX errors have damaged
1315 too much the SIZ parameters */
1316 if (!(image->x1 * image->y1)) {
1317 opj_event_msg(j2k->cinfo, EVT_ERROR,
1318 "JPWL: bad image size (%d x %d)\n",
1319 image->x1, image->y1);
1320 if (!JPWL_ASSUME || JPWL_ASSUME) {
1321 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1325 if (image->numcomps != ((len - 38) / 3)) {
1326 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1327 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1328 image->numcomps, ((len - 38) / 3));
1330 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1333 /* we try to correct */
1334 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
1335 if (image->numcomps < ((len - 38) / 3)) {
1336 len = 38 + 3 * image->numcomps;
1337 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1340 image->numcomps = ((len - 38) / 3);
1341 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1346 /* update components number in the jpwl_exp_comps filed */
1347 cp->exp_comps = image->numcomps;
1349 #endif /* USE_JPWL */
1351 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
1352 for (i = 0; i < image->numcomps; i++) {
1354 tmp = cio_read(cio, 1); /* Ssiz_i */
1355 image->comps[i].prec = (tmp & 0x7f) + 1;
1356 image->comps[i].sgnd = tmp >> 7;
1357 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
1358 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
1361 if (j2k->cp->correct) {
1362 /* if JPWL is on, we check whether TX errors have damaged
1363 too much the SIZ parameters, again */
1364 if (!(image->comps[i].dx * image->comps[i].dy)) {
1365 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1366 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1367 i, i, image->comps[i].dx, image->comps[i].dy);
1369 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1372 /* we try to correct */
1373 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1374 if (!image->comps[i].dx) {
1375 image->comps[i].dx = 1;
1376 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1377 i, image->comps[i].dx);
1379 if (!image->comps[i].dy) {
1380 image->comps[i].dy = 1;
1381 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1382 i, image->comps[i].dy);
1387 #endif /* USE_JPWL */
1389 /* TODO: unused ? */
1390 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
1391 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
1393 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
1394 image->comps[i].factor = cp->reduce; /* reducing factor per component */
1397 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
1398 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
1401 if (j2k->cp->correct) {
1402 /* if JPWL is on, we check whether TX errors have damaged
1403 too much the SIZ parameters */
1404 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
1405 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1406 "JPWL: bad number of tiles (%d x %d)\n",
1409 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1412 /* we try to correct */
1413 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1416 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1419 if (cp->tw > cp->max_tiles) {
1421 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
1422 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1423 cp->max_tiles, cp->tw);
1427 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1430 if (cp->th > cp->max_tiles) {
1432 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1433 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1434 cp->max_tiles, cp->th);
1438 #endif /* USE_JPWL */
1440 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
1441 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
1442 cp->tileno_size = 0;
1445 if (j2k->cp->correct) {
1447 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1448 "JPWL: could not alloc tcps field of cp\n");
1449 if (!JPWL_ASSUME || JPWL_ASSUME) {
1450 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1455 #endif /* USE_JPWL */
1457 for (i = 0; i < cp->tw * cp->th; i++) {
1458 cp->tcps[i].POC = 0;
1459 cp->tcps[i].numpocs = 0;
1460 cp->tcps[i].first = 1;
1463 /* Initialization for PPM marker */
1465 cp->ppm_data = NULL;
1466 cp->ppm_data_first = NULL;
1467 cp->ppm_previous = 0;
1470 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
1471 for (i = 0; i < cp->tw * cp->th; i++) {
1472 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
1474 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
1475 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
1476 j2k->state = J2K_STATE_MH;
1479 if (j2k->cstr_info) {
1480 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1481 cstr_info->image_w = image->x1 - image->x0;
1482 cstr_info->image_h = image->y1 - image->y0;
1483 cstr_info->numcomps = image->numcomps;
1484 cstr_info->tw = cp->tw;
1485 cstr_info->th = cp->th;
1486 cstr_info->tile_x = cp->tdx;
1487 cstr_info->tile_y = cp->tdy;
1488 cstr_info->tile_Ox = cp->tx0;
1489 cstr_info->tile_Oy = cp->ty0;
1490 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
1496 * Reads a SIZ marker (image and tile size)
1497 * @param p_header_data the data contained in the SIZ box.
1498 * @param jp2 the jpeg2000 file codec.
1499 * @param p_header_size the size of the data contained in the SIZ marker.
1500 * @param p_manager the user event manager.
1502 opj_bool j2k_read_siz_v2 (
1503 opj_j2k_v2_t *p_j2k,
1504 OPJ_BYTE * p_header_data,
1505 OPJ_UINT32 p_header_size,
1506 struct opj_event_mgr * p_manager
1509 OPJ_UINT32 l_size, i;
1510 OPJ_UINT32 l_nb_comp;
1511 OPJ_UINT32 l_nb_comp_remain;
1512 OPJ_UINT32 l_remaining_size;
1513 OPJ_UINT32 l_nb_tiles;
1515 opj_image_t *l_image = 00;
1516 opj_cp_v2_t *l_cp = 00;
1517 opj_image_comp_t * l_img_comp = 00;
1518 opj_tcp_v2_t * l_current_tile_param = 00;
1521 assert(p_j2k != 00);
1522 assert(p_manager != 00);
1523 assert(p_header_data != 00);
1525 l_image = p_j2k->m_private_image;
1526 l_cp = &(p_j2k->m_cp);
1528 // minimum size == 39 - 3 (= minimum component parameter)
1529 if (p_header_size < 36) {
1530 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1534 l_remaining_size = p_header_size - 36;
1535 l_nb_comp = l_remaining_size / 3;
1536 l_nb_comp_remain = l_remaining_size % 3;
1537 if (l_nb_comp_remain != 0){
1538 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1542 l_size = p_header_size + 2; /* Lsiz */
1544 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
1546 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1547 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
1549 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
1551 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
1553 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
1555 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
1557 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
1559 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
1561 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
1563 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
1566 l_image->numcomps = (OPJ_UINT16) l_tmp;
1568 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
1572 if (l_image->numcomps != l_nb_comp) {
1573 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);
1578 if (l_cp->correct) {
1579 /* if JPWL is on, we check whether TX errors have damaged
1580 too much the SIZ parameters */
1581 if (!(l_image->x1 * l_image->y1)) {
1582 opj_event_msg_v2(p_manager, EVT_ERROR,
1583 "JPWL: bad image size (%d x %d)\n",
1584 l_image->x1, l_image->y1);
1585 if (!JPWL_ASSUME || JPWL_ASSUME) {
1586 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1591 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
1592 if (l_image->numcomps != ((len - 38) / 3)) {
1593 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1594 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1595 l_image->numcomps, ((len - 38) / 3));
1597 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1600 */ /* we try to correct */
1601 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
1602 if (l_image->numcomps < ((len - 38) / 3)) {
1603 len = 38 + 3 * l_image->numcomps;
1604 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1607 l_image->numcomps = ((len - 38) / 3);
1608 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1614 /* update components number in the jpwl_exp_comps filed */
1615 l_cp->exp_comps = l_image->numcomps;
1617 #endif /* USE_JPWL */
1619 // Allocate the resulting image components
1620 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
1621 if (l_image->comps == 00){
1622 l_image->numcomps = 0;
1623 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1627 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
1628 l_img_comp = l_image->comps;
1630 // Read the component information
1631 for (i = 0; i < l_image->numcomps; ++i){
1633 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
1635 l_img_comp->prec = (tmp & 0x7f) + 1;
1636 l_img_comp->sgnd = tmp >> 7;
1637 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
1639 l_img_comp->dx = (OPJ_INT32)tmp; // should be between 1 and 255
1640 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
1642 l_img_comp->dy = (OPJ_INT32)tmp; // should be between 1 and 255
1645 if (l_cp->correct) {
1646 /* if JPWL is on, we check whether TX errors have damaged
1647 too much the SIZ parameters, again */
1648 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
1649 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1650 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1651 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
1653 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1656 /* we try to correct */
1657 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1658 if (!l_image->comps[i].dx) {
1659 l_image->comps[i].dx = 1;
1660 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1661 i, l_image->comps[i].dx);
1663 if (!l_image->comps[i].dy) {
1664 l_image->comps[i].dy = 1;
1665 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1666 i, l_image->comps[i].dy);
1670 #endif /* USE_JPWL */
1671 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
1672 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
1676 // Compute the number of tiles
1677 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
1678 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
1679 l_nb_tiles = l_cp->tw * l_cp->th;
1681 // Define the tiles which will be decoded
1682 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
1683 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;
1684 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;
1685 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);
1686 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);
1689 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
1690 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
1691 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
1692 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
1696 if (l_cp->correct) {
1697 /* if JPWL is on, we check whether TX errors have damaged
1698 too much the SIZ parameters */
1699 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
1700 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1701 "JPWL: bad number of tiles (%d x %d)\n",
1702 l_cp->tw, l_cp->th);
1704 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1707 /* we try to correct */
1708 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1711 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1714 if (l_cp->tw > l_cp->max_tiles) {
1716 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
1717 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1718 l_cp->max_tiles, l_cp->tw);
1722 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1725 if (l_cp->th > l_cp->max_tiles) {
1727 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1728 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1729 l_cp->max_tiles, l_cp->th);
1733 #endif /* USE_JPWL */
1735 /* memory allocations */
1736 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
1737 if (l_cp->tcps == 00) {
1738 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1741 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
1744 if (l_cp->correct) {
1746 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1747 "JPWL: could not alloc tcps field of cp\n");
1748 if (!JPWL_ASSUME || JPWL_ASSUME) {
1749 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1754 #endif /* USE_JPWL */
1756 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
1757 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
1758 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
1759 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1762 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
1764 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
1765 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
1767 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
1768 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1771 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));
1772 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
1774 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
1775 (opj_simple_mcc_decorrelation_data_t*)
1776 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
1778 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
1779 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1782 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));
1783 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
1785 /* set up default dc level shift */
1786 for (i=0;i<l_image->numcomps;++i) {
1787 if (! l_image->comps[i].sgnd) {
1788 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
1792 l_current_tile_param = l_cp->tcps;
1793 for (i = 0; i < l_nb_tiles; ++i) {
1794 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
1795 if (l_current_tile_param->tccps == 00) {
1796 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1799 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
1801 ++l_current_tile_param;
1804 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; // FIXME J2K_DEC_STATE_MH;
1805 opj_image_comp_header_update(l_image,l_cp);
1812 static void j2k_write_com(opj_j2k_t *j2k) {
1816 if(j2k->cp->comment) {
1817 opj_cio_t *cio = j2k->cio;
1818 char *comment = j2k->cp->comment;
1820 cio_write(cio, J2K_MS_COM, 2);
1821 lenp = cio_tell(cio);
1823 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
1824 for (i = 0; i < strlen(comment); i++) {
1825 cio_write(cio, comment[i], 1);
1827 len = cio_tell(cio) - lenp;
1828 cio_seek(cio, lenp);
1829 cio_write(cio, len, 2);
1830 cio_seek(cio, lenp + len);
1834 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
1839 static void j2k_read_com(opj_j2k_t *j2k) {
1842 opj_cio_t *cio = j2k->cio;
1844 len = cio_read(cio, 2);
1845 cio_skip(cio, len - 2);
1848 * Reads a COM marker (comments)
1849 * @param p_header_data the data contained in the COM box.
1850 * @param jp2 the jpeg2000 file codec.
1851 * @param p_header_size the size of the data contained in the COM marker.
1852 * @param p_manager the user event manager.
1854 opj_bool j2k_read_com_v2 (
1855 opj_j2k_v2_t *p_j2k,
1856 OPJ_BYTE * p_header_data,
1857 OPJ_UINT32 p_header_size,
1858 struct opj_event_mgr * p_manager
1862 assert(p_j2k != 00);
1863 assert(p_manager != 00);
1864 assert(p_header_data != 00);
1869 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
1872 opj_cp_t *cp = j2k->cp;
1873 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1874 opj_tccp_t *tccp = &tcp->tccps[compno];
1875 opj_cio_t *cio = j2k->cio;
1877 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
1878 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
1879 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
1880 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
1881 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
1883 if (tccp->csty & J2K_CCP_CSTY_PRT) {
1884 for (i = 0; i < tccp->numresolutions; i++) {
1885 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
1890 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
1893 opj_cp_t *cp = j2k->cp;
1894 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1895 opj_tccp_t *tccp = &tcp->tccps[compno];
1896 opj_cio_t *cio = j2k->cio;
1898 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
1900 // If user wants to remove more resolutions than the codestream contains, return error
1901 if (cp->reduce >= tccp->numresolutions) {
1902 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
1903 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
1904 j2k->state |= J2K_STATE_ERR;
1907 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
1908 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
1909 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
1910 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
1911 if (tccp->csty & J2K_CP_CSTY_PRT) {
1912 for (i = 0; i < tccp->numresolutions; i++) {
1913 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
1914 tccp->prcw[i] = tmp & 0xf;
1915 tccp->prch[i] = tmp >> 4;
1920 if(j2k->cstr_info && compno == 0) {
1921 for (i = 0; i < tccp->numresolutions; i++) {
1922 if (tccp->csty & J2K_CP_CSTY_PRT) {
1923 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
1924 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
1927 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1928 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1935 static void j2k_write_cod(opj_j2k_t *j2k) {
1936 opj_cp_t *cp = NULL;
1937 opj_tcp_t *tcp = NULL;
1940 opj_cio_t *cio = j2k->cio;
1942 cio_write(cio, J2K_MS_COD, 2); /* COD */
1944 lenp = cio_tell(cio);
1948 tcp = &cp->tcps[j2k->curtileno];
1950 cio_write(cio, tcp->csty, 1); /* Scod */
1951 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
1952 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
1953 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
1955 j2k_write_cox(j2k, 0);
1956 len = cio_tell(cio) - lenp;
1957 cio_seek(cio, lenp);
1958 cio_write(cio, len, 2); /* Lcod */
1959 cio_seek(cio, lenp + len);
1962 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
1966 static void j2k_read_cod(opj_j2k_t *j2k) {
1969 opj_cio_t *cio = j2k->cio;
1970 opj_cp_t *cp = j2k->cp;
1971 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1972 opj_image_t *image = j2k->image;
1974 len = cio_read(cio, 2); /* Lcod */
1975 tcp->csty = cio_read(cio, 1); /* Scod */
1976 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
1977 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
1978 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
1980 pos = cio_tell(cio);
1981 for (i = 0; i < image->numcomps; i++) {
1982 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
1984 j2k_read_cox(j2k, i);
1988 if (j2k->cstr_info) {
1989 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1990 cstr_info->prog = tcp->prg;
1991 cstr_info->numlayers = tcp->numlayers;
1992 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
1993 for (i = 0; i < image->numcomps; i++) {
1994 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
2000 * Reads a COD marker (Coding Styke defaults)
2001 * @param p_header_data the data contained in the COD box.
2002 * @param p_j2k the jpeg2000 codec.
2003 * @param p_header_size the size of the data contained in the COD marker.
2004 * @param p_manager the user event manager.
2006 opj_bool j2k_read_cod_v2 (
2007 opj_j2k_v2_t *p_j2k,
2008 OPJ_BYTE * p_header_data,
2009 OPJ_UINT32 p_header_size,
2010 struct opj_event_mgr * p_manager
2016 opj_cp_v2_t *l_cp = 00;
2017 opj_tcp_v2_t *l_tcp = 00;
2018 opj_image_t *l_image = 00;
2021 assert(p_header_data != 00);
2022 assert(p_j2k != 00);
2023 assert(p_manager != 00);
2025 l_image = p_j2k->m_private_image;
2026 l_cp = &(p_j2k->m_cp);
2028 /* If we are in the first tile-part header of the current tile */
2029 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2030 &l_cp->tcps[p_j2k->m_current_tile_number] :
2031 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2033 /* Make sure room is sufficient */
2034 if (p_header_size < 5) {
2035 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2039 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2041 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2043 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2044 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2047 // If user didn't set a number layer to decode take the max specify in the codestream.
2048 if (l_cp->m_specific_param.m_dec.m_layer) {
2049 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2052 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2055 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2059 for (i = 0; i < l_image->numcomps; ++i) {
2060 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2063 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2064 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2068 if (p_header_size != 0) {
2069 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2073 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2074 j2k_copy_tile_component_parameters(p_j2k);
2077 #ifdef WIP_REMOVE_MSD
2078 if (p_j2k->cstr_info) {
2079 //opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;
2080 p_j2k->cstr_info->prog = l_tcp->prg;
2081 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2082 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2083 for (i = 0; i < l_image->numcomps; ++i) {
2084 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2092 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2095 opj_cp_t *cp = j2k->cp;
2096 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2097 opj_image_t *image = j2k->image;
2098 opj_cio_t *cio = j2k->cio;
2100 cio_write(cio, J2K_MS_COC, 2); /* COC */
2101 lenp = cio_tell(cio);
2103 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2104 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2105 j2k_write_cox(j2k, compno);
2106 len = cio_tell(cio) - lenp;
2107 cio_seek(cio, lenp);
2108 cio_write(cio, len, 2); /* Lcoc */
2109 cio_seek(cio, lenp + len);
2112 static void j2k_read_coc(opj_j2k_t *j2k) {
2115 opj_cp_t *cp = j2k->cp;
2116 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2117 opj_image_t *image = j2k->image;
2118 opj_cio_t *cio = j2k->cio;
2120 len = cio_read(cio, 2); /* Lcoc */
2121 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2122 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
2123 j2k_read_cox(j2k, compno);
2127 * Reads a COC marker (Coding Style Component)
2128 * @param p_header_data the data contained in the COC box.
2129 * @param p_j2k the jpeg2000 codec.
2130 * @param p_header_size the size of the data contained in the COC marker.
2131 * @param p_manager the user event manager.
2133 opj_bool j2k_read_coc_v2 (
2134 opj_j2k_v2_t *p_j2k,
2135 OPJ_BYTE * p_header_data,
2136 OPJ_UINT32 p_header_size,
2137 struct opj_event_mgr * p_manager
2140 opj_cp_v2_t *l_cp = NULL;
2141 opj_tcp_v2_t *l_tcp = NULL;
2142 opj_image_t *l_image = NULL;
2143 OPJ_UINT32 l_comp_room;
2144 OPJ_UINT32 l_comp_no;
2147 assert(p_header_data != 00);
2148 assert(p_j2k != 00);
2149 assert(p_manager != 00);
2151 l_cp = &(p_j2k->m_cp);
2152 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2153 &l_cp->tcps[p_j2k->m_current_tile_number] :
2154 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2155 l_image = p_j2k->m_private_image;
2157 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2159 // make sure room is sufficient
2160 if (p_header_size < l_comp_room + 1) {
2161 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2164 p_header_size -= l_comp_room + 1;
2166 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2167 p_header_data += l_comp_room;
2168 if (l_comp_no >= l_image->numcomps) {
2169 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2173 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2176 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2177 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2181 if (p_header_size != 0) {
2182 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2188 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
2189 int bandno, numbands;
2192 opj_cp_t *cp = j2k->cp;
2193 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2194 opj_tccp_t *tccp = &tcp->tccps[compno];
2195 opj_cio_t *cio = j2k->cio;
2197 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
2198 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
2200 for (bandno = 0; bandno < numbands; bandno++) {
2201 expn = tccp->stepsizes[bandno].expn;
2202 mant = tccp->stepsizes[bandno].mant;
2204 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
2205 cio_write(cio, expn << 3, 1); /* SPqcx_i */
2207 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
2212 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
2214 int bandno, numbands;
2216 opj_cp_t *cp = j2k->cp;
2217 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2218 opj_tccp_t *tccp = &tcp->tccps[compno];
2219 opj_cio_t *cio = j2k->cio;
2221 tmp = cio_read(cio, 1); /* Sqcx */
2222 tccp->qntsty = tmp & 0x1f;
2223 tccp->numgbits = tmp >> 5;
2224 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
2225 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
2228 if (j2k->cp->correct) {
2230 /* if JPWL is on, we check whether there are too many subbands */
2231 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
2232 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2233 "JPWL: bad number of subbands in Sqcx (%d)\n",
2236 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2239 /* we try to correct */
2241 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
2242 "- setting number of bands to %d => HYPOTHESIS!!!\n",
2249 /* We check whether there are too many subbands */
2250 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
2251 opj_event_msg(j2k->cinfo, EVT_WARNING ,
2252 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
2253 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
2256 #endif /* USE_JPWL */
2258 for (bandno = 0; bandno < numbands; bandno++) {
2260 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
2261 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
2264 tmp = cio_read(cio, 2); /* SPqcx_i */
2268 if (bandno < J2K_MAXBANDS){
2269 tccp->stepsizes[bandno].expn = expn;
2270 tccp->stepsizes[bandno].mant = mant;
2274 /* Add Antonin : if scalar_derived -> compute other stepsizes */
2275 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
2276 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
2277 tccp->stepsizes[bandno].expn =
2278 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
2279 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
2280 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
2286 static void j2k_write_qcd(opj_j2k_t *j2k) {
2289 opj_cio_t *cio = j2k->cio;
2291 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
2292 lenp = cio_tell(cio);
2294 j2k_write_qcx(j2k, 0);
2295 len = cio_tell(cio) - lenp;
2296 cio_seek(cio, lenp);
2297 cio_write(cio, len, 2); /* Lqcd */
2298 cio_seek(cio, lenp + len);
2301 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
2304 static void j2k_read_qcd(opj_j2k_t *j2k) {
2307 opj_cio_t *cio = j2k->cio;
2308 opj_image_t *image = j2k->image;
2310 len = cio_read(cio, 2); /* Lqcd */
2311 pos = cio_tell(cio);
2312 for (i = 0; i < image->numcomps; i++) {
2314 j2k_read_qcx(j2k, i, len - 2);
2319 * Reads a QCD marker (Quantization defaults)
2320 * @param p_header_data the data contained in the QCD box.
2321 * @param p_j2k the jpeg2000 codec.
2322 * @param p_header_size the size of the data contained in the QCD marker.
2323 * @param p_manager the user event manager.
2325 opj_bool j2k_read_qcd_v2 (
2326 opj_j2k_v2_t *p_j2k,
2327 OPJ_BYTE * p_header_data,
2328 OPJ_UINT32 p_header_size,
2329 struct opj_event_mgr * p_manager
2333 assert(p_header_data != 00);
2334 assert(p_j2k != 00);
2335 assert(p_manager != 00);
2337 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2338 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2342 if (p_header_size != 0) {
2343 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2347 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2348 j2k_copy_tile_quantization_parameters(p_j2k);
2353 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
2356 opj_cio_t *cio = j2k->cio;
2358 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
2359 lenp = cio_tell(cio);
2361 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
2362 j2k_write_qcx(j2k, compno);
2363 len = cio_tell(cio) - lenp;
2364 cio_seek(cio, lenp);
2365 cio_write(cio, len, 2); /* Lqcc */
2366 cio_seek(cio, lenp + len);
2369 static void j2k_read_qcc(opj_j2k_t *j2k) {
2371 int numcomp = j2k->image->numcomps;
2372 opj_cio_t *cio = j2k->cio;
2374 len = cio_read(cio, 2); /* Lqcc */
2375 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
2378 if (j2k->cp->correct) {
2380 static int backup_compno = 0;
2382 /* compno is negative or larger than the number of components!!! */
2383 if ((compno < 0) || (compno >= numcomp)) {
2384 opj_event_msg(j2k->cinfo, EVT_ERROR,
2385 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2388 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2391 /* we try to correct */
2392 compno = backup_compno % numcomp;
2393 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
2394 "- setting component number to %d\n",
2398 /* keep your private count of tiles */
2401 #endif /* USE_JPWL */
2403 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
2407 * Reads a QCC marker (Quantization component)
2408 * @param p_header_data the data contained in the QCC box.
2409 * @param p_j2k the jpeg2000 codec.
2410 * @param p_header_size the size of the data contained in the QCC marker.
2411 * @param p_manager the user event manager.
2413 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
2414 OPJ_BYTE * p_header_data,
2415 OPJ_UINT32 p_header_size,
2416 struct opj_event_mgr * p_manager)
2418 OPJ_UINT32 l_num_comp,l_comp_no;
2421 assert(p_header_data != 00);
2422 assert(p_j2k != 00);
2423 assert(p_manager != 00);
2425 l_num_comp = p_j2k->m_private_image->numcomps;
2427 if (l_num_comp <= 256) {
2428 if (p_header_size < 1) {
2429 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2432 opj_read_bytes(p_header_data,&l_comp_no,1);
2437 if (p_header_size < 2) {
2438 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2441 opj_read_bytes(p_header_data,&l_comp_no,2);
2447 if (p_j2k->m_cp.correct) {
2449 static OPJ_UINT32 backup_compno = 0;
2451 /* compno is negative or larger than the number of components!!! */
2452 if ((l_comp_no < 0) || (l_comp_no >= l_num_comp)) {
2453 opj_event_msg_v2(p_manager, EVT_ERROR,
2454 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2455 l_comp_no, l_num_comp);
2457 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2460 /* we try to correct */
2461 l_comp_no = backup_compno % l_num_comp;
2462 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
2463 "- setting component number to %d\n",
2467 /* keep your private count of tiles */
2470 #endif /* USE_JPWL */
2472 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2473 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2477 if (p_header_size != 0) {
2478 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2486 static void j2k_write_poc(opj_j2k_t *j2k) {
2487 int len, numpchgs, i;
2489 int numcomps = j2k->image->numcomps;
2491 opj_cp_t *cp = j2k->cp;
2492 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2493 opj_tccp_t *tccp = &tcp->tccps[0];
2494 opj_cio_t *cio = j2k->cio;
2496 numpchgs = 1 + tcp->numpocs;
2497 cio_write(cio, J2K_MS_POC, 2); /* POC */
2498 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
2499 cio_write(cio, len, 2); /* Lpoc */
2500 for (i = 0; i < numpchgs; i++) {
2501 opj_poc_t *poc = &tcp->pocs[i];
2502 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
2503 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
2504 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
2505 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
2506 cio_write(cio, poc->resno1, 1); /* REpoc_i */
2507 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
2508 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
2509 poc->compno1 = int_min(poc->compno1, numcomps);
2510 cio_write(cio, poc->prg, 1); /* Ppoc_i */
2514 static void j2k_read_poc(opj_j2k_t *j2k) {
2515 int len, numpchgs, i, old_poc;
2517 int numcomps = j2k->image->numcomps;
2519 opj_cp_t *cp = j2k->cp;
2520 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2521 opj_cio_t *cio = j2k->cio;
2523 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
2525 len = cio_read(cio, 2); /* Lpoc */
2526 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
2528 for (i = old_poc; i < numpchgs + old_poc; i++) {
2530 poc = &tcp->pocs[i];
2531 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
2532 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
2533 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
2534 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
2535 poc->compno1 = int_min(
2536 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
2537 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
2540 tcp->numpocs = numpchgs + old_poc - 1;
2544 * Reads a POC marker (Progression Order Change)
2546 * @param p_header_data the data contained in the POC box.
2547 * @param p_j2k the jpeg2000 codec.
2548 * @param p_header_size the size of the data contained in the POC marker.
2549 * @param p_manager the user event manager.
2551 opj_bool j2k_read_poc_v2 (
2552 opj_j2k_v2_t *p_j2k,
2553 OPJ_BYTE * p_header_data,
2554 OPJ_UINT32 p_header_size,
2555 struct opj_event_mgr * p_manager)
2557 OPJ_UINT32 i, l_nb_comp, l_tmp;
2558 opj_image_t * l_image = 00;
2559 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
2560 OPJ_UINT32 l_chunk_size, l_comp_room;
2562 opj_cp_v2_t *l_cp = 00;
2563 opj_tcp_v2_t *l_tcp = 00;
2564 opj_poc_t *l_current_poc = 00;
2567 assert(p_header_data != 00);
2568 assert(p_j2k != 00);
2569 assert(p_manager != 00);
2571 l_image = p_j2k->m_private_image;
2572 l_nb_comp = l_image->numcomps;
2573 if (l_nb_comp <= 256) {
2579 l_chunk_size = 5 + 2 * l_comp_room;
2580 l_current_poc_nb = p_header_size / l_chunk_size;
2581 l_current_poc_remaining = p_header_size % l_chunk_size;
2583 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
2584 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
2588 l_cp = &(p_j2k->m_cp);
2589 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2590 &l_cp->tcps[p_j2k->m_current_tile_number] :
2591 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2592 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
2593 l_current_poc_nb += l_old_poc_nb;
2595 assert(l_current_poc_nb < 32);
2597 /* now poc is in use.*/
2600 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
2601 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
2602 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
2604 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
2605 p_header_data+=l_comp_room;
2606 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
2608 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
2610 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
2611 p_header_data+=l_comp_room;
2612 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
2614 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
2615 /* make sure comp is in acceptable bounds */
2616 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
2620 l_tcp->numpocs = l_current_poc_nb - 1;
2624 static void j2k_read_crg(opj_j2k_t *j2k) {
2625 int len, i, Xcrg_i, Ycrg_i;
2627 opj_cio_t *cio = j2k->cio;
2628 int numcomps = j2k->image->numcomps;
2630 len = cio_read(cio, 2); /* Lcrg */
2631 for (i = 0; i < numcomps; i++) {
2632 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
2633 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
2638 * Reads a CRG marker (Component registration)
2640 * @param p_header_data the data contained in the TLM box.
2641 * @param p_j2k the jpeg2000 codec.
2642 * @param p_header_size the size of the data contained in the TLM marker.
2643 * @param p_manager the user event manager.
2645 opj_bool j2k_read_crg_v2 (
2646 opj_j2k_v2_t *p_j2k,
2647 OPJ_BYTE * p_header_data,
2648 OPJ_UINT32 p_header_size,
2649 struct opj_event_mgr * p_manager
2652 OPJ_UINT32 l_nb_comp;
2654 assert(p_header_data != 00);
2655 assert(p_j2k != 00);
2656 assert(p_manager != 00);
2658 l_nb_comp = p_j2k->m_private_image->numcomps;
2660 if (p_header_size != l_nb_comp *4) {
2661 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
2664 /* Do not care of this at the moment since only local variables are set here */
2667 (i = 0; i < l_nb_comp; ++i)
2669 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
2671 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
2678 static void j2k_read_tlm(opj_j2k_t *j2k) {
2679 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
2680 long int Ttlm_i, Ptlm_i;
2682 opj_cio_t *cio = j2k->cio;
2684 len = cio_read(cio, 2); /* Ltlm */
2685 Ztlm = cio_read(cio, 1); /* Ztlm */
2686 Stlm = cio_read(cio, 1); /* Stlm */
2687 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
2688 SP = (Stlm >> 6) & 0x01;
2689 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
2690 for (i = 0; i < tile_tlm; i++) {
2691 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
2692 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
2697 * Reads a TLM marker (Tile Length Marker)
2699 * @param p_header_data the data contained in the TLM box.
2700 * @param p_j2k the jpeg2000 codec.
2701 * @param p_header_size the size of the data contained in the TLM marker.
2702 * @param p_manager the user event manager.
2704 opj_bool j2k_read_tlm_v2 (
2705 opj_j2k_v2_t *p_j2k,
2706 OPJ_BYTE * p_header_data,
2707 OPJ_UINT32 p_header_size,
2708 struct opj_event_mgr * p_manager
2711 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
2713 assert(p_header_data != 00);
2714 assert(p_j2k != 00);
2715 assert(p_manager != 00);
2717 if (p_header_size < 2) {
2718 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2723 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
2725 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
2728 l_ST = ((l_Stlm >> 4) & 0x3);
2729 l_SP = (l_Stlm >> 6) & 0x1;
2731 l_Ptlm_size = (l_SP + 1) * 2;
2732 l_quotient = l_Ptlm_size + l_ST;
2734 l_tot_num_tp = p_header_size / l_quotient;
2735 l_tot_num_tp_remaining = p_header_size % l_quotient;
2737 if (l_tot_num_tp_remaining != 0) {
2738 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2741 /* FIXME Do not care of this at the moment since only local variables are set here */
2744 (i = 0; i < l_tot_num_tp; ++i)
2746 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
2747 p_header_data += l_ST;
2748 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
2749 p_header_data += l_Ptlm_size;
2754 static void j2k_read_plm(opj_j2k_t *j2k) {
2755 int len, i, Zplm, Nplm, add, packet_len = 0;
2757 opj_cio_t *cio = j2k->cio;
2759 len = cio_read(cio, 2); /* Lplm */
2760 Zplm = cio_read(cio, 1); /* Zplm */
2763 Nplm = cio_read(cio, 4); /* Nplm */
2765 for (i = Nplm; i > 0; i--) {
2766 add = cio_read(cio, 1);
2768 packet_len = (packet_len << 7) + add; /* Iplm_ij */
2769 if ((add & 0x80) == 0) {
2780 * Reads a PLM marker (Packet length, main header marker)
2782 * @param p_header_data the data contained in the TLM box.
2783 * @param p_j2k the jpeg2000 codec.
2784 * @param p_header_size the size of the data contained in the TLM marker.
2785 * @param p_manager the user event manager.
2787 opj_bool j2k_read_plm_v2 (
2788 opj_j2k_v2_t *p_j2k,
2789 OPJ_BYTE * p_header_data,
2790 OPJ_UINT32 p_header_size,
2791 struct opj_event_mgr * p_manager
2795 assert(p_header_data != 00);
2796 assert(p_j2k != 00);
2797 assert(p_manager != 00);
2799 if (p_header_size < 1) {
2800 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2803 /* Do not care of this at the moment since only local variables are set here */
2805 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
2812 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
2814 p_header_size -= (1+l_Nplm);
2818 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2822 (i = 0; i < l_Nplm; ++i)
2824 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
2826 // take only the last seven bytes
2827 l_packet_len |= (l_tmp & 0x7f);
2835 // store packet length and proceed to next packet
2842 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2850 static void j2k_read_plt(opj_j2k_t *j2k) {
2851 int len, i, Zplt, packet_len = 0, add;
2853 opj_cio_t *cio = j2k->cio;
2855 len = cio_read(cio, 2); /* Lplt */
2856 Zplt = cio_read(cio, 1); /* Zplt */
2857 for (i = len - 3; i > 0; i--) {
2858 add = cio_read(cio, 1);
2859 packet_len = (packet_len << 7) + add; /* Iplt_i */
2860 if ((add & 0x80) == 0) {
2868 * Reads a PLT marker (Packet length, tile-part header)
2870 * @param p_header_data the data contained in the PLT box.
2871 * @param p_j2k the jpeg2000 codec.
2872 * @param p_header_size the size of the data contained in the PLT marker.
2873 * @param p_manager the user event manager.
2875 opj_bool j2k_read_plt_v2 (
2876 opj_j2k_v2_t *p_j2k,
2877 OPJ_BYTE * p_header_data,
2878 OPJ_UINT32 p_header_size,
2879 struct opj_event_mgr * p_manager
2882 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
2885 assert(p_header_data != 00);
2886 assert(p_j2k != 00);
2887 assert(p_manager != 00);
2889 if (p_header_size < 1) {
2890 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2894 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
2898 for (i = 0; i < p_header_size; ++i) {
2899 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
2901 // take only the last seven bytes
2902 l_packet_len |= (l_tmp & 0x7f);
2907 // store packet length and proceed to next packet
2912 if (l_packet_len != 0) {
2913 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2920 static void j2k_read_ppm(opj_j2k_t *j2k) {
2921 int len, Z_ppm, i, j;
2924 opj_cp_t *cp = j2k->cp;
2925 opj_cio_t *cio = j2k->cio;
2927 len = cio_read(cio, 2);
2930 Z_ppm = cio_read(cio, 1); /* Z_ppm */
2933 if (cp->ppm_previous == 0) {
2934 N_ppm = cio_read(cio, 4); /* N_ppm */
2937 N_ppm = cp->ppm_previous;
2940 if (Z_ppm == 0) { /* First PPM marker */
2941 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
2942 cp->ppm_data_first = cp->ppm_data;
2943 cp->ppm_len = N_ppm;
2944 } else { /* NON-first PPM marker */
2945 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
2948 /* this memory allocation check could be done even in non-JPWL cases */
2950 if (!cp->ppm_data) {
2951 opj_event_msg(j2k->cinfo, EVT_ERROR,
2952 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
2954 if (!JPWL_ASSUME || JPWL_ASSUME) {
2955 opj_free(cp->ppm_data);
2956 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2963 cp->ppm_data_first = cp->ppm_data;
2964 cp->ppm_len = N_ppm + cp->ppm_store;
2966 for (i = N_ppm; i > 0; i--) { /* Read packet header */
2967 cp->ppm_data[j] = cio_read(cio, 1);
2971 break; /* Case of non-finished packet header in present marker but finished in next one */
2973 cp->ppm_previous = i - 1;
2978 * Reads a PPM marker (Packed packet headers, main header)
2980 * @param p_header_data the data contained in the POC box.
2981 * @param p_j2k the jpeg2000 codec.
2982 * @param p_header_size the size of the data contained in the POC marker.
2983 * @param p_manager the user event manager.
2985 opj_bool j2k_read_ppm_v2 (
2986 opj_j2k_v2_t *p_j2k,
2987 OPJ_BYTE * p_header_data,
2988 OPJ_UINT32 p_header_size,
2989 struct opj_event_mgr * p_manager
2993 opj_cp_v2_t *l_cp = 00;
2994 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
2997 assert(p_header_data != 00);
2998 assert(p_j2k != 00);
2999 assert(p_manager != 00);
3001 if (p_header_size < 1) {
3002 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3006 l_cp = &(p_j2k->m_cp);
3009 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3015 if (p_header_size < 4) {
3016 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3020 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3024 /* First PPM marker: Initialization */
3025 l_cp->ppm_len = l_N_ppm;
3026 l_cp->ppm_data_size = 0;
3028 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3029 if (l_cp->ppm_buffer == 00) {
3030 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3033 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3035 l_cp->ppm_data = l_cp->ppm_buffer;
3039 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3040 if (p_header_size >= 4) {
3042 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3045 l_cp->ppm_len += l_N_ppm ;
3047 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3048 if (l_cp->ppm_buffer == 00) {
3049 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3052 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3054 l_cp->ppm_data = l_cp->ppm_buffer;
3061 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3063 if (l_remaining_data <= p_header_size) {
3064 /* we must store less information than available in the packet */
3065 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3066 l_cp->ppm_data_size = l_cp->ppm_len;
3067 p_header_size -= l_remaining_data;
3068 p_header_data += l_remaining_data;
3071 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3072 l_cp->ppm_data_size += p_header_size;
3073 p_header_data += p_header_size;
3085 * Reads a PPM marker (Packed packet headers, main header)
3087 * @param p_header_data the data contained in the POC box.
3088 * @param p_j2k the jpeg2000 codec.
3089 * @param p_header_size the size of the data contained in the POC marker.
3090 * @param p_manager the user event manager.
3092 opj_bool j2k_read_ppm_v3 (
3093 opj_j2k_v2_t *p_j2k,
3094 OPJ_BYTE * p_header_data,
3095 OPJ_UINT32 p_header_size,
3096 struct opj_event_mgr * p_manager
3099 opj_cp_v2_t *l_cp = 00;
3100 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3103 assert(p_header_data != 00);
3104 assert(p_j2k != 00);
3105 assert(p_manager != 00);
3107 // Minimum size of PPM marker is equal to the size of Zppm element
3108 if (p_header_size < 1) {
3109 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3113 l_cp = &(p_j2k->m_cp);
3116 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3122 // We need now at least the Nppm^0 element
3123 if (p_header_size < 4) {
3124 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3128 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3132 /* First PPM marker: Initialization */
3133 l_cp->ppm_len = l_N_ppm;
3134 l_cp->ppm_data_read = 0;
3136 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3137 if (l_cp->ppm_data == 00) {
3138 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3141 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3143 l_cp->ppm_data_current = l_cp->ppm_data;
3145 //l_cp->ppm_data = l_cp->ppm_buffer;
3148 if (p_header_size < 4) {
3149 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3153 // Uncompleted Ippm series in the previous PPM marker?
3154 if (l_cp->ppm_data_read < l_cp->ppm_len) {
3155 // Get the place where add the remaining Ippm series
3156 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3157 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3160 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3164 // Increase the size of ppm_data to add the new Ippm series
3165 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3167 // Keep the position of the place where concatenate the new series
3168 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3169 l_cp->ppm_len += l_N_ppm;
3174 l_remaining_data = p_header_size;
3176 while (l_remaining_data >= l_N_ppm) {
3177 // read a complete Ippm series
3178 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3179 p_header_size -= l_N_ppm;
3180 p_header_data += l_N_ppm;
3182 l_cp->ppm_data_read += l_N_ppm; // Increase the number of data read
3186 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
3191 l_remaining_data = p_header_size;
3195 l_remaining_data = p_header_size;
3197 // Next Ippm series is a complete series ?
3198 if (l_remaining_data > l_N_ppm) {
3199 // Increase the size of ppm_data to add the new Ippm series
3200 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3202 // Keep the position of the place where concatenate the new series
3203 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3204 l_cp->ppm_len += l_N_ppm;
3209 // Need to read an incomplete Ippm series
3210 if (l_remaining_data) {
3211 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3213 // Keep the position of the place where concatenate the new series
3214 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3215 l_cp->ppm_len += l_N_ppm;
3217 // Read incomplete Ippm series
3218 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3219 p_header_size -= l_remaining_data;
3220 p_header_data += l_remaining_data;
3222 l_cp->ppm_data_read += l_remaining_data; // Increase the number of data read
3227 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3228 if (p_header_size >= 4) {
3230 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3233 l_cp->ppm_len += l_N_ppm ;
3235 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3236 if (l_cp->ppm_buffer == 00) {
3237 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3240 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3242 l_cp->ppm_data = l_cp->ppm_buffer;
3249 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3251 if (l_remaining_data <= p_header_size) {
3252 /* we must store less information than available in the packet */
3253 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3254 l_cp->ppm_data_size = l_cp->ppm_len;
3255 p_header_size -= l_remaining_data;
3256 p_header_data += l_remaining_data;
3259 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3260 l_cp->ppm_data_size += p_header_size;
3261 p_header_data += p_header_size;
3270 static void j2k_read_ppt(opj_j2k_t *j2k) {
3271 int len, Z_ppt, i, j = 0;
3273 opj_cp_t *cp = j2k->cp;
3274 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
3275 opj_cio_t *cio = j2k->cio;
3277 len = cio_read(cio, 2);
3278 Z_ppt = cio_read(cio, 1);
3280 if (Z_ppt == 0) { /* First PPT marker */
3281 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
3282 tcp->ppt_data_first = tcp->ppt_data;
3284 tcp->ppt_len = len - 3;
3285 } else { /* NON-first PPT marker */
3286 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
3287 tcp->ppt_data_first = tcp->ppt_data;
3288 tcp->ppt_len = len - 3 + tcp->ppt_store;
3291 for (i = len - 3; i > 0; i--) {
3292 tcp->ppt_data[j] = cio_read(cio, 1);
3299 * Reads a PPT marker (Packed packet headers, tile-part header)
3301 * @param p_header_data the data contained in the PPT box.
3302 * @param p_j2k the jpeg2000 codec.
3303 * @param p_header_size the size of the data contained in the PPT marker.
3304 * @param p_manager the user event manager.
3306 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
3307 OPJ_BYTE * p_header_data,
3308 OPJ_UINT32 p_header_size,
3309 struct opj_event_mgr * p_manager )
3311 opj_cp_v2_t *l_cp = 00;
3312 opj_tcp_v2_t *l_tcp = 00;
3316 assert(p_header_data != 00);
3317 assert(p_j2k != 00);
3318 assert(p_manager != 00);
3320 /* We need to have the Z_ppt element at minimum */
3321 if (p_header_size < 1) {
3322 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3326 l_cp = &(p_j2k->m_cp);
3328 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");
3332 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3335 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3339 /* Allocate buffer to read the packet header */
3341 /* First PPT marker */
3342 l_tcp->ppt_data_size = 0;
3343 l_tcp->ppt_len = p_header_size;
3345 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3346 if (l_tcp->ppt_buffer == 00) {
3347 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3350 l_tcp->ppt_data = l_tcp->ppt_buffer;
3352 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3355 l_tcp->ppt_len += p_header_size;
3357 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3358 if (l_tcp->ppt_buffer == 00) {
3359 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3362 l_tcp->ppt_data = l_tcp->ppt_buffer;
3364 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3367 /* Read packet header from buffer */
3368 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3370 l_tcp->ppt_data_size += p_header_size;
3375 static void j2k_write_tlm(opj_j2k_t *j2k){
3377 opj_cio_t *cio = j2k->cio;
3378 j2k->tlm_start = cio_tell(cio);
3379 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
3380 lenp = 4 + (5*j2k->totnum_tp);
3381 cio_write(cio,lenp,2); /* Ltlm */
3382 cio_write(cio, 0,1); /* Ztlm=0*/
3383 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3384 cio_skip(cio,5*j2k->totnum_tp);
3387 static void j2k_write_sot(opj_j2k_t *j2k) {
3390 opj_cio_t *cio = j2k->cio;
3392 j2k->sot_start = cio_tell(cio);
3393 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
3394 lenp = cio_tell(cio);
3395 cio_skip(cio, 2); /* Lsot (further) */
3396 cio_write(cio, j2k->curtileno, 2); /* Isot */
3397 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
3398 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
3399 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
3400 len = cio_tell(cio) - lenp;
3401 cio_seek(cio, lenp);
3402 cio_write(cio, len, 2); /* Lsot */
3403 cio_seek(cio, lenp + len);
3407 /* update markers struct */
3408 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
3409 #endif /* USE_JPWL */
3412 if( j2k->cstr_info && j2k->cur_tp_num==0){
3413 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
3417 static void j2k_read_sot(opj_j2k_t *j2k) {
3418 int len, tileno, totlen, partno, numparts, i;
3419 opj_tcp_t *tcp = NULL;
3422 opj_cp_t *cp = j2k->cp;
3423 opj_cio_t *cio = j2k->cio;
3425 len = cio_read(cio, 2);
3426 tileno = cio_read(cio, 2);
3429 if (j2k->cp->correct) {
3431 static int backup_tileno = 0;
3433 /* tileno is negative or larger than the number of tiles!!! */
3434 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3435 opj_event_msg(j2k->cinfo, EVT_ERROR,
3436 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3437 tileno, (cp->tw * cp->th));
3439 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3442 /* we try to correct */
3443 tileno = backup_tileno;
3444 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3445 "- setting tile number to %d\n",
3449 /* keep your private count of tiles */
3452 #endif /* USE_JPWL */
3454 if (cp->tileno_size == 0) {
3455 cp->tileno[cp->tileno_size] = tileno;
3459 while (i < cp->tileno_size && status == 0) {
3460 status = cp->tileno[i] == tileno ? 1 : 0;
3464 cp->tileno[cp->tileno_size] = tileno;
3469 totlen = cio_read(cio, 4);
3472 if (j2k->cp->correct) {
3474 /* totlen is negative or larger than the bytes left!!! */
3475 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
3476 opj_event_msg(j2k->cinfo, EVT_ERROR,
3477 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3478 totlen, cio_numbytesleft(cio) + 8);
3480 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3483 /* we try to correct */
3485 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3486 "- setting Psot to %d => assuming it is the last tile\n",
3491 #endif /* USE_JPWL */
3494 totlen = cio_numbytesleft(cio) + 8;
3496 partno = cio_read(cio, 1);
3497 numparts = cio_read(cio, 1);
3499 if (partno >= numparts) {
3500 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);
3501 numparts = partno+1;
3504 j2k->curtileno = tileno;
3505 j2k->cur_tp_num = partno;
3506 j2k->eot = cio_getbp(cio) - 12 + totlen;
3507 j2k->state = J2K_STATE_TPH;
3508 tcp = &cp->tcps[j2k->curtileno];
3511 if (j2k->cstr_info) {
3514 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
3515 j2k->cstr_info->tile[tileno].tileno = tileno;
3516 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
3517 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3519 j2k->cstr_info->tile[tileno].end_pos += totlen;
3521 j2k->cstr_info->tile[tileno].num_tps = numparts;
3523 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
3525 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)
3526 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
3527 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3528 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3531 if (tcp->first == 1) {
3532 /* Initialization PPT */
3533 opj_tccp_t *tmp = tcp->tccps;
3534 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
3536 tcp->ppt_data = NULL;
3537 tcp->ppt_data_first = NULL;
3540 for (i = 0; i < j2k->image->numcomps; i++) {
3541 tcp->tccps[i] = j2k->default_tcp->tccps[i];
3543 cp->tcps[j2k->curtileno].first = 0;
3548 * Reads a PPT marker (Packed packet headers, tile-part header)
3550 * @param p_header_data the data contained in the PPT box.
3551 * @param p_j2k the jpeg2000 codec.
3552 * @param p_header_size the size of the data contained in the PPT marker.
3553 * @param p_manager the user event manager.
3555 opj_bool j2k_read_sot_v2 (
3556 opj_j2k_v2_t *p_j2k,
3557 OPJ_BYTE * p_header_data,
3558 OPJ_UINT32 p_header_size,
3559 struct opj_event_mgr * p_manager
3563 opj_cp_v2_t *l_cp = 00;
3564 opj_tcp_v2_t *l_tcp = 00;
3565 OPJ_UINT32 l_tot_len, l_num_parts = 0;
3566 OPJ_UINT32 l_current_part;
3567 OPJ_UINT32 l_tile_x,l_tile_y;
3570 assert(p_header_data != 00);
3571 assert(p_j2k != 00);
3572 assert(p_manager != 00);
3574 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
3575 if (p_header_size != 8) {
3576 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3580 l_cp = &(p_j2k->m_cp);
3581 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
3584 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3585 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
3586 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
3589 if (l_cp->correct) {
3591 int tileno = p_j2k->m_current_tile_number;
3592 static int backup_tileno = 0;
3594 /* tileno is negative or larger than the number of tiles!!! */
3595 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
3596 opj_event_msg_v2(p_manager, EVT_ERROR,
3597 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3598 tileno, (l_cp->tw * l_cp->th));
3600 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3603 /* we try to correct */
3604 tileno = backup_tileno;
3605 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3606 "- setting tile number to %d\n",
3610 /* keep your private count of tiles */
3613 #endif /* USE_JPWL */
3615 /* look for the tile in the list of already processed tile (in parts). */
3616 /* Optimization possible here with a more complex data structure and with the removing of tiles */
3617 /* since the time taken by this function can only grow at the time */
3619 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
3623 if (l_cp->correct) {
3625 /* totlen is negative or larger than the bytes left!!! */
3626 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))) { */
3627 opj_event_msg_v2(p_manager, EVT_ERROR,
3628 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3629 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
3631 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3634 /* we try to correct */
3636 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3637 "- setting Psot to %d => assuming it is the last tile\n",
3641 #endif /* USE_JPWL */
3643 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
3645 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
3646 "we assuming it is the last tile-part of the codestream.\n");
3647 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3650 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
3653 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
3656 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
3657 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
3658 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
3659 if (l_tcp->m_nb_tile_parts) {
3660 if (l_current_part >= l_tcp->m_nb_tile_parts){
3661 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
3662 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
3663 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3667 l_tcp->m_nb_tile_parts = l_num_parts;
3670 /* If know the number of tile part header we will check if we didn't read the last*/
3671 if (l_tcp->m_nb_tile_parts) {
3672 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
3673 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
3677 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
3678 /* Keep the size of data to skip after this marker */
3679 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
3682 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
3683 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
3686 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
3688 /* Check if the current tile is outside the area we want decode (in tile index)*/
3689 p_j2k->m_specific_param.m_decoder.m_skip_data =
3690 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
3691 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
3692 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
3693 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
3696 if (p_j2k->cstr_index)
3698 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
3699 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
3701 if (l_num_parts != 0){
3702 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
3703 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
3706 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
3707 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3708 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
3710 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3711 (opj_tp_index_t*)opj_realloc(
3712 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
3713 l_num_parts* sizeof(opj_tp_index_t));
3716 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
3718 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
3719 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
3720 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3721 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
3722 sizeof(opj_tp_index_t));
3725 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
3726 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
3727 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3728 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
3729 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
3730 * sizeof(opj_tp_index_t));
3739 /* 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 */
3740 /* if (p_j2k->cstr_info) {
3743 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
3746 p_j2k->cstr_info->tile[tileno].tileno = tileno;
3747 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
3748 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3749 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
3752 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
3755 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
3759 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
3762 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
3763 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3764 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3769 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
3772 opj_tcp_t *tcp = NULL;
3773 opj_codestream_info_t *cstr_info = NULL;
3775 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
3776 opj_cp_t *cp = j2k->cp;
3777 opj_cio_t *cio = j2k->cio;
3779 tcd->tp_num = j2k->tp_num ;
3780 tcd->cur_tp_num = j2k->cur_tp_num;
3782 cio_write(cio, J2K_MS_SOD, 2);
3784 if( j2k->cstr_info && j2k->cur_tp_num==0){
3785 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
3788 if (j2k->curtileno == 0) {
3789 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
3793 cstr_info = j2k->cstr_info;
3795 if (!j2k->cur_tp_num ) {
3796 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3797 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
3800 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
3801 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
3805 /* update markers struct */
3806 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
3807 #endif /* USE_JPWL */
3812 tcp = &cp->tcps[j2k->curtileno];
3813 for (layno = 0; layno < tcp->numlayers; layno++) {
3814 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
3815 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
3816 } else if (tcp->rates[layno]) {
3817 tcp->rates[layno]=1;
3820 if(j2k->cur_tp_num == 0){
3821 tcd->tcd_image->tiles->packno = 0;
3823 cstr_info->packno = 0;
3826 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
3828 /* Writing Psot in SOT marker */
3829 totlen = cio_tell(cio) + l - j2k->sot_start;
3830 cio_seek(cio, j2k->sot_start + 6);
3831 cio_write(cio, totlen, 4);
3832 cio_seek(cio, j2k->sot_start + totlen);
3833 /* Writing Ttlm and Ptlm in TLM marker */
3835 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
3836 cio_write(cio, j2k->curtileno, 1);
3837 cio_write(cio, totlen, 4);
3839 cio_seek(cio, j2k->sot_start + totlen);
3842 static void j2k_read_sod(opj_j2k_t *j2k) {
3843 int len, truncate = 0, i;
3844 unsigned char *data = NULL, *data_ptr = NULL;
3846 opj_cio_t *cio = j2k->cio;
3847 int curtileno = j2k->curtileno;
3850 if (j2k->cstr_info) {
3851 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
3852 cio_tell(cio) + j2k->pos_correction - 1;
3853 if (j2k->cur_tp_num == 0)
3854 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3855 j2k->cstr_info->packno = 0;
3858 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
3860 if (len == cio_numbytesleft(cio) + 1) {
3861 truncate = 1; /* Case of a truncate codestream */
3864 data = j2k->tile_data[curtileno];
3865 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
3867 data_ptr = data + j2k->tile_len[curtileno];
3868 for (i = 0; i < len; i++) {
3869 data_ptr[i] = cio_read(cio, 1);
3872 j2k->tile_len[curtileno] += len;
3873 j2k->tile_data[curtileno] = data;
3876 j2k->state = J2K_STATE_TPHSOT;
3878 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
3884 * Reads a SOD marker (Start Of Data)
3886 * @param p_header_data the data contained in the SOD box.
3887 * @param p_j2k the jpeg2000 codec.
3888 * @param p_header_size the size of the data contained in the SOD marker.
3889 * @param p_manager the user event manager.
3891 opj_bool j2k_read_sod_v2 (
3892 opj_j2k_v2_t *p_j2k,
3893 struct opj_stream_private *p_stream,
3894 struct opj_event_mgr * p_manager
3897 OPJ_UINT32 l_current_read_size;
3898 opj_codestream_index_t * l_cstr_index = 00;
3899 OPJ_BYTE ** l_current_data = 00;
3900 opj_tcp_v2_t * l_tcp = 00;
3901 OPJ_UINT32 * l_tile_len = 00;
3904 assert(p_j2k != 00);
3905 assert(p_manager != 00);
3906 assert(p_stream != 00);
3908 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
3910 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part)
3911 p_j2k->m_specific_param.m_decoder.m_sot_length = opj_stream_get_number_byte_left(p_stream) - 2;
3913 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
3915 l_current_data = &(l_tcp->m_data);
3916 l_tile_len = &l_tcp->m_data_size;
3918 if (! *l_current_data) {
3919 *l_current_data = (OPJ_BYTE*) opj_malloc/*FIXME V2 -> my_opj_malloc*/(p_j2k->m_specific_param.m_decoder.m_sot_length);
3922 *l_current_data = (OPJ_BYTE*) opj_realloc/*FIXME V2 -> my_opj_realloc*/(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
3925 if (*l_current_data == 00) {
3926 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
3932 l_cstr_index = p_j2k->cstr_index;
3934 OPJ_SIZE_T l_current_pos = opj_stream_tell(p_stream) - 2;
3935 OPJ_UINT32 l_current_tile_part =l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
3936 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
3938 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
3939 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
3941 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
3945 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
3947 /*l_cstr_index->packno = 0;*/
3956 l_current_read_size = opj_stream_read_data( p_stream,
3957 *l_current_data + *l_tile_len,
3958 p_j2k->m_specific_param.m_decoder.m_sot_length,
3961 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
3962 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
3965 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
3968 *l_tile_len += l_current_read_size;
3974 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
3975 opj_cp_t *cp = j2k->cp;
3976 opj_tcp_t *tcp = &cp->tcps[tileno];
3977 opj_cio_t *cio = j2k->cio;
3978 int numcomps = j2k->image->numcomps;
3980 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
3981 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
3982 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
3983 cio_write(cio, 0, 1); /* Srgn */
3984 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
3987 static void j2k_read_rgn(opj_j2k_t *j2k) {
3988 int len, compno, roisty;
3990 opj_cp_t *cp = j2k->cp;
3991 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3992 opj_cio_t *cio = j2k->cio;
3993 int numcomps = j2k->image->numcomps;
3995 len = cio_read(cio, 2); /* Lrgn */
3996 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
3997 roisty = cio_read(cio, 1); /* Srgn */
4000 if (j2k->cp->correct) {
4001 /* totlen is negative or larger than the bytes left!!! */
4002 if (compno >= numcomps) {
4003 opj_event_msg(j2k->cinfo, EVT_ERROR,
4004 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4006 if (!JPWL_ASSUME || JPWL_ASSUME) {
4007 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4012 #endif /* USE_JPWL */
4014 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
4017 static void j2k_write_eoc(opj_j2k_t *j2k) {
4018 opj_cio_t *cio = j2k->cio;
4019 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
4020 cio_write(cio, J2K_MS_EOC, 2);
4024 /* update markers struct */
4025 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
4026 #endif /* USE_JPWL */
4031 * Reads a RGN marker (Region Of Interest)
4033 * @param p_header_data the data contained in the POC box.
4034 * @param p_j2k the jpeg2000 codec.
4035 * @param p_header_size the size of the data contained in the POC marker.
4036 * @param p_manager the user event manager.
4038 opj_bool j2k_read_rgn_v2 (
4039 opj_j2k_v2_t *p_j2k,
4040 OPJ_BYTE * p_header_data,
4041 OPJ_UINT32 p_header_size,
4042 struct opj_event_mgr * p_manager
4045 OPJ_UINT32 l_nb_comp;
4046 opj_image_t * l_image = 00;
4048 opj_cp_v2_t *l_cp = 00;
4049 opj_tcp_v2_t *l_tcp = 00;
4050 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4053 assert(p_header_data != 00);
4054 assert(p_j2k != 00);
4055 assert(p_manager != 00);
4057 l_image = p_j2k->m_private_image;
4058 l_nb_comp = l_image->numcomps;
4060 if (l_nb_comp <= 256) {
4065 if (p_header_size != 2 + l_comp_room) {
4066 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4070 l_cp = &(p_j2k->m_cp);
4071 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4072 &l_cp->tcps[p_j2k->m_current_tile_number] :
4073 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4075 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4076 p_header_data+=l_comp_room;
4077 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4081 if (l_cp->correct) {
4082 /* totlen is negative or larger than the bytes left!!! */
4083 if (l_comp_room >= l_nb_comp) {
4084 opj_event_msg_v2(p_manager, EVT_ERROR,
4085 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4086 l_comp_room, l_nb_comp);
4087 if (!JPWL_ASSUME || JPWL_ASSUME) {
4088 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4093 #endif /* USE_JPWL */
4095 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4102 static void j2k_read_eoc(opj_j2k_t *j2k) {
4106 /* if packets should be decoded */
4107 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
4108 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
4109 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
4110 for (i = 0; i < j2k->cp->tileno_size; i++) {
4111 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
4112 tileno = j2k->cp->tileno[i];
4113 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
4114 opj_free(j2k->tile_data[tileno]);
4115 j2k->tile_data[tileno] = NULL;
4116 tcd_free_decode_tile(tcd, i);
4117 if (success == OPJ_FALSE) {
4118 j2k->state |= J2K_STATE_ERR;
4122 tcd_free_decode(tcd);
4125 /* if packets should not be decoded */
4127 for (i = 0; i < j2k->cp->tileno_size; i++) {
4128 tileno = j2k->cp->tileno[i];
4129 opj_free(j2k->tile_data[tileno]);
4130 j2k->tile_data[tileno] = NULL;
4133 if (j2k->state & J2K_STATE_ERR)
4134 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
4136 j2k->state = J2K_STATE_MT;
4140 * Reads a EOC marker (End Of Codestream)
4142 * @param p_header_data the data contained in the SOD box.
4143 * @param p_j2k the jpeg2000 codec.
4144 * @param p_header_size the size of the data contained in the SOD marker.
4145 * @param p_manager the user event manager.
4147 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
4148 struct opj_stream_private *p_stream,
4149 struct opj_event_mgr * p_manager )
4152 opj_tcd_v2_t * l_tcd = 00;
4153 OPJ_UINT32 l_nb_tiles;
4154 opj_tcp_v2_t * l_tcp = 00;
4158 assert(p_j2k != 00);
4159 assert(p_manager != 00);
4160 assert(p_stream != 00);
4162 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4163 l_tcp = p_j2k->m_cp.tcps;
4165 l_tcd = tcd_create_v2(OPJ_TRUE);
4167 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4171 for (i = 0; i < l_nb_tiles; ++i) {
4172 if (l_tcp->m_data) {
4173 if (! tcd_init_decode_tile(l_tcd, i)) {
4174 tcd_destroy_v2(l_tcd);
4175 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4179 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4183 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4188 j2k_tcp_destroy(l_tcp);
4192 tcd_destroy_v2(l_tcd);
4196 typedef struct opj_dec_mstabent {
4199 /** value of the state when the marker can appear */
4201 /** action linked to the marker */
4202 void (*handler) (opj_j2k_t *j2k);
4203 } opj_dec_mstabent_t;
4205 opj_dec_mstabent_t j2k_dec_mstab[] = {
4206 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
4207 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
4208 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
4209 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
4210 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
4211 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
4212 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
4213 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
4214 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
4215 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
4216 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
4217 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
4218 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
4219 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
4220 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
4221 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
4223 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
4224 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
4227 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
4228 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
4229 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
4230 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
4231 #endif /* USE_JPWL */
4233 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
4234 {J2K_MS_INSEC, 0, j2k_read_insec},
4235 #endif /* USE_JPSEC */
4237 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
4240 static void j2k_read_unk(opj_j2k_t *j2k) {
4241 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
4244 if (j2k->cp->correct) {
4246 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
4247 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4248 id = cio_read(j2k->cio, 2);
4249 opj_event_msg(j2k->cinfo, EVT_ERROR,
4250 "JPWL: really don't know this marker %x\n",
4253 opj_event_msg(j2k->cinfo, EVT_ERROR,
4254 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
4257 /* OK, activate this at your own risk!!! */
4258 /* we look for the marker at the minimum hamming distance from this */
4259 while (j2k_dec_mstab[m].id) {
4261 /* 1's where they differ */
4262 tmp_id = j2k_dec_mstab[m].id ^ id;
4264 /* compute the hamming distance between our id and the current */
4266 for (i = 0; i < 16; i++) {
4267 if ((tmp_id >> i) & 0x0001) {
4272 /* if current distance is smaller, set the minimum */
4273 if (cur_dist < min_dist) {
4274 min_dist = cur_dist;
4275 min_id = j2k_dec_mstab[m].id;
4278 /* jump to the next marker */
4282 /* do we substitute the marker? */
4283 if (min_dist < JPWL_MAXIMUM_HAMMING) {
4284 opj_event_msg(j2k->cinfo, EVT_ERROR,
4285 "- marker %x is at distance %d from the read %x\n",
4286 min_id, min_dist, id);
4287 opj_event_msg(j2k->cinfo, EVT_ERROR,
4288 "- trying to substitute in place and crossing fingers!\n");
4289 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4290 cio_write(j2k->cio, min_id, 2);
4293 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4298 #endif /* USE_JPWL */
4303 * Reads an unknown marker
4305 * @param p_stream the stream object to read from.
4306 * @param p_j2k the jpeg2000 codec.
4307 * @param p_manager the user event manager.
4309 * @return true if the marker could be deduced.
4311 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
4312 struct opj_stream_private *p_stream,
4313 OPJ_UINT32 *output_marker,
4314 struct opj_event_mgr * p_manager
4317 OPJ_UINT32 l_unknown_marker;
4318 const opj_dec_memory_marker_handler_t * l_marker_handler;
4319 OPJ_UINT32 l_size_unk = 2;
4322 assert(p_j2k != 00);
4323 assert(p_manager != 00);
4324 assert(p_stream != 00);
4326 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
4329 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
4330 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
4331 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
4335 // read 2 bytes as the new marker ID
4336 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
4338 if (!(l_unknown_marker < 0xff00)) {
4340 // Get the marker handler from the marker ID
4341 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
4343 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
4344 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
4348 if (l_marker_handler->id != J2K_MS_UNK) {
4349 /* Add the marker to the codestream index*/
4350 if (l_marker_handler->id != J2K_MS_SOT)
4351 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
4352 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
4354 break; /* next marker is known and well located */
4362 *output_marker = l_marker_handler->id ;
4368 Read the lookup table containing all the marker, status and action
4369 @param id Marker value
4371 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
4372 opj_dec_mstabent_t *e;
4373 for (e = j2k_dec_mstab; e->id != 0; e++) {
4383 * Reads a MCT marker (Multiple Component Transform)
4385 * @param p_header_data the data contained in the MCT box.
4386 * @param p_j2k the jpeg2000 codec.
4387 * @param p_header_size the size of the data contained in the MCT marker.
4388 * @param p_manager the user event manager.
4390 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
4391 OPJ_BYTE * p_header_data,
4392 OPJ_UINT32 p_header_size,
4393 struct opj_event_mgr * p_manager )
4396 opj_tcp_v2_t *l_tcp = 00;
4399 opj_mct_data_t * l_mct_data;
4402 assert(p_header_data != 00);
4403 assert(p_j2k != 00);
4405 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4406 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4407 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4409 if (p_header_size < 2) {
4410 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4415 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
4418 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
4422 if(p_header_size <= 6) {
4423 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4427 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
4428 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
4431 l_indix = l_tmp & 0xff;
4432 l_mct_data = l_tcp->m_mct_records;
4434 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
4435 if (l_mct_data->m_index == l_indix) {
4442 if (i == l_tcp->m_nb_mct_records) {
4443 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
4444 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
4446 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));
4447 if(! l_tcp->m_mct_records) {
4448 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4452 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
4453 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
4456 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
4459 if (l_mct_data->m_data) {
4460 opj_free(l_mct_data->m_data);
4461 l_mct_data->m_data = 00;
4464 l_mct_data->m_index = l_indix;
4465 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
4466 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
4468 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
4471 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
4477 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
4478 if (! l_mct_data->m_data) {
4479 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4482 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
4484 l_mct_data->m_data_size = p_header_size;
4485 ++l_tcp->m_nb_mct_records;
4491 * Reads a MCC marker (Multiple Component Collection)
4493 * @param p_header_data the data contained in the MCC box.
4494 * @param p_j2k the jpeg2000 codec.
4495 * @param p_header_size the size of the data contained in the MCC marker.
4496 * @param p_manager the user event manager.
4498 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
4499 OPJ_BYTE * p_header_data,
4500 OPJ_UINT32 p_header_size,
4501 struct opj_event_mgr * p_manager )
4506 opj_tcp_v2_t * l_tcp;
4507 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4508 opj_mct_data_t * l_mct_data;
4509 OPJ_UINT32 l_nb_collections;
4510 OPJ_UINT32 l_nb_comps;
4511 OPJ_UINT32 l_nb_bytes_by_comp;
4515 assert(p_header_data != 00);
4516 assert(p_j2k != 00);
4517 assert(p_manager != 00);
4519 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4520 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4521 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4523 if (p_header_size < 2) {
4524 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4529 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
4532 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
4536 if (p_header_size < 7) {
4537 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4541 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
4544 l_mcc_record = l_tcp->m_mcc_records;
4546 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
4547 if (l_mcc_record->m_index == l_indix) {
4554 if (i == l_tcp->m_nb_mcc_records) {
4555 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
4556 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
4558 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
4559 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
4560 if (! l_tcp->m_mcc_records) {
4561 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4564 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
4565 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));
4567 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
4569 l_mcc_record->m_index = l_indix;
4571 /* only one marker atm */
4572 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
4575 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
4579 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
4582 if (l_nb_collections > 1) {
4583 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
4589 for (i=0;i<l_nb_collections;++i) {
4590 if (p_header_size < 3) {
4591 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4595 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
4599 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
4603 opj_read_bytes(p_header_data,&l_nb_comps,2);
4608 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
4609 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
4611 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
4612 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4616 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
4618 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
4619 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
4620 p_header_data+=l_nb_bytes_by_comp;
4623 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
4628 opj_read_bytes(p_header_data,&l_nb_comps,2);
4631 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
4632 l_nb_comps &= 0x7fff;
4634 if (l_nb_comps != l_mcc_record->m_nb_comps) {
4635 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
4639 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
4640 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4644 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
4646 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
4647 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
4648 p_header_data+=l_nb_bytes_by_comp;
4651 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
4656 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
4659 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
4660 l_mcc_record->m_decorrelation_array = 00;
4661 l_mcc_record->m_offset_array = 00;
4663 l_indix = l_tmp & 0xff;
4665 l_mct_data = l_tcp->m_mct_records;
4666 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
4667 if (l_mct_data->m_index == l_indix) {
4668 l_mcc_record->m_decorrelation_array = l_mct_data;
4674 if (l_mcc_record->m_decorrelation_array == 00) {
4675 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4680 l_indix = (l_tmp >> 8) & 0xff;
4682 l_mct_data = l_tcp->m_mct_records;
4683 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
4684 if (l_mct_data->m_index == l_indix) {
4685 l_mcc_record->m_offset_array = l_mct_data;
4691 if (l_mcc_record->m_offset_array == 00) {
4692 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4698 if (p_header_size != 0) {
4699 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4703 ++l_tcp->m_nb_mcc_records;
4709 * Reads a MCO marker (Multiple Component Transform Ordering)
4711 * @param p_header_data the data contained in the MCO box.
4712 * @param p_j2k the jpeg2000 codec.
4713 * @param p_header_size the size of the data contained in the MCO marker.
4714 * @param p_manager the user event manager.
4716 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
4717 OPJ_BYTE * p_header_data,
4718 OPJ_UINT32 p_header_size,
4719 struct opj_event_mgr * p_manager )
4721 OPJ_UINT32 l_tmp, i;
4722 OPJ_UINT32 l_nb_stages;
4723 opj_tcp_v2_t * l_tcp;
4724 opj_tccp_t * l_tccp;
4725 opj_image_t * l_image;
4726 opj_image_comp_t * l_img_comp;
4729 assert(p_header_data != 00);
4730 assert(p_j2k != 00);
4731 assert(p_manager != 00);
4733 l_image = p_j2k->m_private_image;
4734 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4735 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4736 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4738 if (p_header_size < 1) {
4739 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
4743 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
4746 if (l_nb_stages > 1) {
4747 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
4751 if (p_header_size != l_nb_stages + 1) {
4752 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
4756 l_tccp = l_tcp->tccps;
4757 l_img_comp = l_image->comps;
4759 for (i=0;i<l_image->numcomps;++i) {
4760 l_tccp->m_dc_level_shift = 0;
4764 if (l_tcp->m_mct_decoding_matrix) {
4765 opj_free(l_tcp->m_mct_decoding_matrix);
4766 l_tcp->m_mct_decoding_matrix = 00;
4769 for (i=0;i<l_nb_stages;++i) {
4770 opj_read_bytes(p_header_data,&l_tmp,1);
4773 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
4781 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
4784 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4785 opj_mct_data_t * l_deco_array, * l_offset_array;
4786 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
4787 OPJ_UINT32 l_nb_elem;
4788 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
4789 opj_tccp_t * l_tccp;
4792 assert(p_tcp != 00);
4794 l_mcc_record = p_tcp->m_mcc_records;
4796 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
4797 if (l_mcc_record->m_index == p_index) {
4802 if (i==p_tcp->m_nb_mcc_records) {
4803 /** element discarded **/
4807 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
4808 /** do not support number of comps != image */
4812 l_deco_array = l_mcc_record->m_decorrelation_array;
4815 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
4816 if (l_deco_array->m_data_size != l_data_size) {
4820 l_nb_elem = p_image->numcomps * p_image->numcomps;
4821 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
4822 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
4824 if (! p_tcp->m_mct_decoding_matrix ) {
4828 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);
4831 l_offset_array = l_mcc_record->m_offset_array;
4833 if (l_offset_array) {
4834 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
4835 if (l_offset_array->m_data_size != l_data_size) {
4839 l_nb_elem = p_image->numcomps;
4840 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
4841 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
4843 if (! l_offset_data ) {
4847 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
4849 l_tccp = p_tcp->tccps;
4850 l_current_offset_data = l_offset_data;
4852 for (i=0;i<p_image->numcomps;++i) {
4853 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
4857 opj_free(l_offset_data);
4864 * Reads a CBD marker (Component bit depth definition)
4865 * @param p_header_data the data contained in the CBD box.
4866 * @param p_j2k the jpeg2000 codec.
4867 * @param p_header_size the size of the data contained in the CBD marker.
4868 * @param p_manager the user event manager.
4870 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
4871 OPJ_BYTE * p_header_data,
4872 OPJ_UINT32 p_header_size,
4873 struct opj_event_mgr * p_manager)
4875 OPJ_UINT32 l_nb_comp,l_num_comp;
4876 OPJ_UINT32 l_comp_def;
4878 opj_image_comp_t * l_comp = 00;
4881 assert(p_header_data != 00);
4882 assert(p_j2k != 00);
4883 assert(p_manager != 00);
4885 l_num_comp = p_j2k->m_private_image->numcomps;
4887 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
4888 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
4892 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
4895 if (l_nb_comp != l_num_comp) {
4896 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
4900 l_comp = p_j2k->m_private_image->comps;
4901 for (i=0;i<l_num_comp;++i) {
4902 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
4904 l_comp->sgnd = (l_comp_def>>7) & 1;
4905 l_comp->prec = (l_comp_def&0x7f) + 1;
4913 /* ----------------------------------------------------------------------- */
4914 /* J2K / JPT decoder interface */
4915 /* ----------------------------------------------------------------------- */
4917 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
4918 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4922 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
4923 if(!j2k->default_tcp) {
4929 j2k->tile_data = NULL;
4934 void j2k_destroy_decompress(opj_j2k_t *j2k) {
4937 if(j2k->tile_len != NULL) {
4938 opj_free(j2k->tile_len);
4940 if(j2k->tile_data != NULL) {
4941 opj_free(j2k->tile_data);
4943 if(j2k->default_tcp != NULL) {
4944 opj_tcp_t *default_tcp = j2k->default_tcp;
4945 if(default_tcp->ppt_data_first != NULL) {
4946 opj_free(default_tcp->ppt_data_first);
4948 if(j2k->default_tcp->tccps != NULL) {
4949 opj_free(j2k->default_tcp->tccps);
4951 opj_free(j2k->default_tcp);
4953 if(j2k->cp != NULL) {
4954 opj_cp_t *cp = j2k->cp;
4955 if(cp->tcps != NULL) {
4956 for(i = 0; i < cp->tw * cp->th; i++) {
4957 if(cp->tcps[i].ppt_data_first != NULL) {
4958 opj_free(cp->tcps[i].ppt_data_first);
4960 if(cp->tcps[i].tccps != NULL) {
4961 opj_free(cp->tcps[i].tccps);
4966 if(cp->ppm_data_first != NULL) {
4967 opj_free(cp->ppm_data_first);
4969 if(cp->tileno != NULL) {
4970 opj_free(cp->tileno);
4972 if(cp->comment != NULL) {
4973 opj_free(cp->comment);
4981 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
4982 if(j2k && parameters) {
4983 /* create and initialize the coding parameters structure */
4984 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4985 cp->reduce = parameters->cp_reduce;
4986 cp->layer = parameters->cp_layer;
4987 cp->limit_decoding = parameters->cp_limit_decoding;
4990 cp->correct = parameters->jpwl_correct;
4991 cp->exp_comps = parameters->jpwl_exp_comps;
4992 cp->max_tiles = parameters->jpwl_max_tiles;
4993 #endif /* USE_JPWL */
4996 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
5001 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
5003 if(j2k && parameters) {
5004 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
5005 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
5008 j2k->m_cp.correct = parameters->jpwl_correct;
5009 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
5010 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
5011 #endif /* USE_JPWL */
5015 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
5016 opj_image_t *image = NULL;
5018 opj_common_ptr cinfo = j2k->cinfo;
5021 j2k->cstr_info = cstr_info;
5023 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
5025 /* create an empty image */
5026 image = opj_image_create0();
5029 j2k->state = J2K_STATE_MHSOC;
5032 opj_dec_mstabent_t *e;
5033 int id = cio_read(cio, 2);
5036 /* we try to honor JPWL correction power */
5037 if (j2k->cp->correct) {
5039 int orig_pos = cio_tell(cio);
5042 /* call the corrector */
5043 status = jpwl_correct(j2k);
5045 /* go back to where you were */
5046 cio_seek(cio, orig_pos - 2);
5048 /* re-read the marker */
5049 id = cio_read(cio, 2);
5051 /* check whether it begins with ff */
5052 if (id >> 8 != 0xff) {
5053 opj_event_msg(cinfo, EVT_ERROR,
5054 "JPWL: possible bad marker %x at %d\n",
5055 id, cio_tell(cio) - 2);
5057 opj_image_destroy(image);
5058 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
5061 /* we try to correct */
5063 cio_seek(cio, cio_tell(cio) - 2);
5064 cio_write(cio, id, 2);
5065 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
5066 "- setting marker to %x\n",
5071 #endif /* USE_JPWL */
5073 if (id >> 8 != 0xff) {
5074 opj_image_destroy(image);
5075 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
5078 e = j2k_dec_mstab_lookup(id);
5079 // Check if the marker is known
5080 if (!(j2k->state & e->states)) {
5081 opj_image_destroy(image);
5082 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
5085 // Check if the decoding is limited to the main header
5086 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
5087 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
5094 if (j2k->state & J2K_STATE_ERR)
5097 if (j2k->state == J2K_STATE_MT) {
5100 if (j2k->state == J2K_STATE_NEOC) {
5104 if (j2k->state == J2K_STATE_NEOC) {
5108 if (j2k->state != J2K_STATE_MT) {
5109 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
5115 * Read a JPT-stream and decode file
5118 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
5119 opj_image_t *image = NULL;
5120 opj_jpt_msg_header_t header;
5122 opj_common_ptr cinfo = j2k->cinfo;
5124 OPJ_ARG_NOT_USED(cstr_info);
5128 /* create an empty image */
5129 image = opj_image_create0();
5132 j2k->state = J2K_STATE_MHSOC;
5134 /* Initialize the header */
5135 jpt_init_msg_header(&header);
5136 /* Read the first header of the message */
5137 jpt_read_msg_header(cinfo, cio, &header);
5139 position = cio_tell(cio);
5140 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
5141 opj_image_destroy(image);
5142 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
5147 opj_dec_mstabent_t *e = NULL;
5150 if (!cio_numbytesleft(cio)) {
5154 /* data-bin read -> need to read a new header */
5155 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
5156 jpt_read_msg_header(cinfo, cio, &header);
5157 position = cio_tell(cio);
5158 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
5159 opj_image_destroy(image);
5160 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
5165 id = cio_read(cio, 2);
5166 if (id >> 8 != 0xff) {
5167 opj_image_destroy(image);
5168 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
5171 e = j2k_dec_mstab_lookup(id);
5172 if (!(j2k->state & e->states)) {
5173 opj_image_destroy(image);
5174 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
5180 if (j2k->state == J2K_STATE_MT) {
5183 if (j2k->state == J2K_STATE_NEOC) {
5187 if (j2k->state == J2K_STATE_NEOC) {
5191 if (j2k->state != J2K_STATE_MT) {
5192 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
5198 /* ----------------------------------------------------------------------- */
5199 /* J2K encoder interface */
5200 /* ----------------------------------------------------------------------- */
5202 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
5203 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
5210 opj_j2k_v2_t* j2k_create_compress_v2()
5212 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
5217 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
5219 l_j2k->m_is_decoder = 0;
5220 l_j2k->m_cp.m_is_decoder = 0;
5222 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
5223 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5228 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
5230 // validation list creation
5231 l_j2k->m_validation_list = opj_procedure_list_create();
5232 if (! l_j2k->m_validation_list) {
5237 // execution list creation
5238 l_j2k->m_procedure_list = opj_procedure_list_create();
5239 if (! l_j2k->m_procedure_list) {
5247 void j2k_destroy_compress(opj_j2k_t *j2k) {
5251 if(j2k->cp != NULL) {
5252 opj_cp_t *cp = j2k->cp;
5255 opj_free(cp->comment);
5258 opj_free(cp->matrice);
5260 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5261 opj_free(cp->tcps[tileno].tccps);
5270 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
5271 int i, j, tileno, numpocs_tile;
5272 opj_cp_t *cp = NULL;
5274 if(!j2k || !parameters || ! image) {
5278 /* create and initialize the coding parameters structure */
5279 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
5281 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
5284 /* set default values for cp */
5289 copy user encoding parameters
5291 cp->cinema = parameters->cp_cinema;
5292 cp->max_comp_size = parameters->max_comp_size;
5293 cp->rsiz = parameters->cp_rsiz;
5294 cp->disto_alloc = parameters->cp_disto_alloc;
5295 cp->fixed_alloc = parameters->cp_fixed_alloc;
5296 cp->fixed_quality = parameters->cp_fixed_quality;
5298 /* mod fixed_quality */
5299 if(parameters->cp_matrice) {
5300 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
5301 cp->matrice = (int *) opj_malloc(array_size);
5302 memcpy(cp->matrice, parameters->cp_matrice, array_size);
5306 cp->tdx = parameters->cp_tdx;
5307 cp->tdy = parameters->cp_tdy;
5310 cp->tx0 = parameters->cp_tx0;
5311 cp->ty0 = parameters->cp_ty0;
5313 /* comment string */
5314 if(parameters->cp_comment) {
5315 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
5317 strcpy(cp->comment, parameters->cp_comment);
5322 calculate other encoding parameters
5325 if (parameters->tile_size_on) {
5326 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
5327 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
5329 cp->tdx = image->x1 - cp->tx0;
5330 cp->tdy = image->y1 - cp->ty0;
5333 if(parameters->tp_on){
5334 cp->tp_flag = parameters->tp_flag;
5339 for(i=0;i<image->numcomps ;i++){
5340 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
5346 calculate JPWL encoding parameters
5349 if (parameters->jpwl_epc_on) {
5353 cp->epc_on = OPJ_TRUE;
5354 cp->info_on = OPJ_FALSE; /* no informative technique */
5357 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
5358 cp->epb_on = OPJ_TRUE;
5360 cp->hprot_MH = parameters->jpwl_hprot_MH;
5361 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
5362 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
5363 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
5365 /* if tile specs are not specified, copy MH specs */
5366 if (cp->hprot_TPH[0] == -1) {
5367 cp->hprot_TPH_tileno[0] = 0;
5368 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
5370 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
5371 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
5372 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
5373 cp->pprot[i] = parameters->jpwl_pprot[i];
5377 /* set ESD writing */
5378 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
5379 cp->esd_on = OPJ_TRUE;
5381 cp->sens_size = parameters->jpwl_sens_size;
5382 cp->sens_addr = parameters->jpwl_sens_addr;
5383 cp->sens_range = parameters->jpwl_sens_range;
5385 cp->sens_MH = parameters->jpwl_sens_MH;
5386 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
5387 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
5388 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
5392 /* always set RED writing to false: we are at the encoder */
5393 cp->red_on = OPJ_FALSE;
5396 cp->epc_on = OPJ_FALSE;
5398 #endif /* USE_JPWL */
5401 /* initialize the mutiple tiles */
5402 /* ---------------------------- */
5403 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
5405 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5406 opj_tcp_t *tcp = &cp->tcps[tileno];
5407 tcp->numlayers = parameters->tcp_numlayers;
5408 for (j = 0; j < tcp->numlayers; j++) {
5410 if (cp->fixed_quality) {
5411 tcp->distoratio[j] = parameters->tcp_distoratio[j];
5413 tcp->rates[j] = parameters->tcp_rates[j];
5415 if (cp->fixed_quality) { /* add fixed_quality */
5416 tcp->distoratio[j] = parameters->tcp_distoratio[j];
5418 tcp->rates[j] = parameters->tcp_rates[j];
5422 tcp->csty = parameters->csty;
5423 tcp->prg = parameters->prog_order;
5424 tcp->mct = parameters->tcp_mct;
5428 if (parameters->numpocs) {
5429 /* initialisation of POC */
5431 for (i = 0; i < parameters->numpocs; i++) {
5432 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
5433 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
5434 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
5435 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
5436 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
5437 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
5438 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
5439 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
5440 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
5444 tcp->numpocs = numpocs_tile -1 ;
5449 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
5451 for (i = 0; i < image->numcomps; i++) {
5452 opj_tccp_t *tccp = &tcp->tccps[i];
5453 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
5454 tccp->numresolutions = parameters->numresolution;
5455 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
5456 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
5457 tccp->cblksty = parameters->mode;
5458 tccp->qmfbid = parameters->irreversible ? 0 : 1;
5459 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
5461 if (i == parameters->roi_compno) {
5462 tccp->roishift = parameters->roi_shift;
5467 if(parameters->cp_cinema)
5469 //Precinct size for lowest frequency subband=128
5472 //Precinct size at all other resolutions = 256
5473 for (j = 1; j < tccp->numresolutions; j++) {
5478 if (parameters->csty & J2K_CCP_CSTY_PRT) {
5480 for (j = tccp->numresolutions - 1; j >= 0; j--) {
5481 if (p < parameters->res_spec) {
5483 if (parameters->prcw_init[p] < 1) {
5486 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
5489 if (parameters->prch_init[p] < 1) {
5492 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
5496 int res_spec = parameters->res_spec;
5497 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
5498 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
5500 if (size_prcw < 1) {
5503 tccp->prcw[j] = int_floorlog2(size_prcw);
5506 if (size_prch < 1) {
5509 tccp->prch[j] = int_floorlog2(size_prch);
5513 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
5516 for (j = 0; j < tccp->numresolutions; j++) {
5523 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
5528 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
5530 opj_cp_t *cp = NULL;
5532 opj_tcd_t *tcd = NULL; /* TCD component */
5540 j2k->cstr_info = cstr_info;
5543 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
5544 cstr_info->image_w = image->x1 - image->x0;
5545 cstr_info->image_h = image->y1 - image->y0;
5546 cstr_info->prog = (&cp->tcps[0])->prg;
5547 cstr_info->tw = cp->tw;
5548 cstr_info->th = cp->th;
5549 cstr_info->tile_x = cp->tdx; /* new version parser */
5550 cstr_info->tile_y = cp->tdy; /* new version parser */
5551 cstr_info->tile_Ox = cp->tx0; /* new version parser */
5552 cstr_info->tile_Oy = cp->ty0; /* new version parser */
5553 cstr_info->numcomps = image->numcomps;
5554 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
5555 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
5556 for (compno=0; compno < image->numcomps; compno++) {
5557 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
5559 cstr_info->D_max = 0.0; /* ADD Marcela */
5560 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
5561 cstr_info->maxmarknum = 100;
5562 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
5563 cstr_info->marknum = 0;
5573 for (compno = 1; compno < image->numcomps; compno++) {
5574 j2k_write_coc(j2k, compno);
5575 j2k_write_qcc(j2k, compno);
5579 for (compno = 0; compno < image->numcomps; compno++) {
5580 opj_tcp_t *tcp = &cp->tcps[0];
5581 if (tcp->tccps[compno].roishift)
5582 j2k_write_rgn(j2k, compno, 0);
5584 if (cp->comment != NULL) {
5588 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
5592 if (cp->cinema == CINEMA4K_24) {
5597 /* uncomment only for testing JPSEC marker writing */
5598 /* j2k_write_sec(j2k); */
5602 cstr_info->main_head_end = cio_tell(cio) - 1;
5605 /**** Main Header ENDS here ***/
5607 /* create the tile encoder */
5608 tcd = tcd_create(j2k->cinfo);
5610 /* encode each tile */
5611 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5615 int acc_pack_num = 0;
5619 opj_tcp_t *tcp = &cp->tcps[tileno];
5620 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
5622 j2k->curtileno = tileno;
5623 j2k->cur_tp_num = 0;
5624 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
5625 /* initialisation before tile encoding */
5627 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
5629 tcd_init_encode(tcd, image, cp, j2k->curtileno);
5634 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
5635 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
5636 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
5637 cstr_info->tile[j2k->curtileno].marknum = 0;
5641 for(pino = 0; pino <= tcp->numpocs; pino++) {
5645 /*Get number of tile parts*/
5646 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
5647 tcd->tp_pos = cp->tp_pos;
5649 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
5650 j2k->tp_num = tilepartno;
5653 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
5654 cio_tell(cio) + j2k->pos_correction;
5658 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
5659 for (compno = 1; compno < image->numcomps; compno++) {
5660 j2k_write_coc(j2k, compno);
5661 j2k_write_qcc(j2k, compno);
5663 if (cp->tcps[tileno].numpocs) {
5670 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5671 cio_tell(cio) + j2k->pos_correction + 1;
5674 j2k_write_sod(j2k, tcd);
5678 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
5679 cio_tell(cio) + j2k->pos_correction - 1;
5680 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
5682 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
5683 cstr_info->packno - acc_pack_num;
5684 acc_pack_num = cstr_info->packno;
5692 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
5697 if (tile->PPT) { // BAD PPT !!!
5700 PPT_file=fopen("PPT","rb");
5701 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
5702 for (i=0;i<tile->len_ppt;i++) {
5704 fread(&elmt, 1, 1, PPT_file);
5705 fwrite(&elmt,1,1,f);
5714 /* destroy the tile encoder */
5715 tcd_free_encode(tcd);
5718 opj_free(j2k->cur_totnum_tp);
5723 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
5725 /* The following adjustment is done to adjust the codestream size */
5726 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5727 /* the first bunch of bytes is not in the codestream */
5728 cstr_info->codestream_size -= cstr_info->main_head_start;
5734 preparation of JPWL marker segments
5738 /* encode according to JPWL */
5739 jpwl_encode(j2k, cio, image);
5742 #endif /* USE_JPWL */
5747 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
5752 /* expand the list? */
5753 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
5754 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
5755 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
5758 /* add the marker */
5759 cstr_info->marker[cstr_info->marknum].type = type;
5760 cstr_info->marker[cstr_info->marknum].pos = pos;
5761 cstr_info->marker[cstr_info->marknum].len = len;
5762 cstr_info->marknum++;
5766 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) {
5771 /* expand the list? */
5772 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
5773 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
5774 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
5777 /* add the marker */
5778 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
5779 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
5780 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
5781 cstr_index->marknum++;
5785 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
5788 opj_marker_info_t *marker;
5793 /* expand the list? */
5794 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
5795 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
5796 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
5799 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
5801 /* add the marker */
5802 marker->type = type;
5805 cstr_info->tile[tileno].marknum++;
5808 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len)
5814 if (!cstr_index->tile_index)
5816 /* expand the list? */
5817 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
5818 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
5819 cstr_index->tile_index[tileno].marker =
5820 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
5821 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
5824 /* add the marker */
5825 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
5826 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
5827 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
5828 cstr_index->tile_index[tileno].marknum++;
5830 if (type == J2K_MS_SOT) {
5831 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
5833 if (cstr_index->tile_index[tileno].tp_index)
5834 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
5844 * -----------------------------------------------------------------------
5845 * -----------------------------------------------------------------------
5846 * -----------------------------------------------------------------------
5850 * Ends the decompression procedures and possibiliy add data to be read after the
5853 opj_bool j2k_end_decompress(
5854 opj_j2k_v2_t *p_j2k,
5855 opj_stream_private_t *p_stream,
5856 opj_event_mgr_t * p_manager)
5862 * Reads a jpeg2000 codestream header structure.
5865 * @param p_stream the stream to read data from.
5866 * @param p_j2k the jpeg2000 codec.
5867 * @param p_manager the user event manager.
5869 * @return true if the box is valid.
5871 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
5872 opj_j2k_v2_t* p_j2k,
5873 opj_image_t** p_image,
5874 struct opj_event_mgr* p_manager )
5877 assert(p_j2k != 00);
5878 assert(p_stream != 00);
5879 assert(p_manager != 00);
5881 /* create an empty image header */
5882 p_j2k->m_private_image = opj_image_create0();
5883 if (! p_j2k->m_private_image) {
5887 /* customization of the validation */
5888 j2k_setup_decoding_validation(p_j2k);
5890 /* validation of the parameters codec */
5891 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
5892 opj_image_destroy(p_j2k->m_private_image);
5893 p_j2k->m_private_image = NULL;
5897 /* customization of the encoding */
5898 j2k_setup_header_reading(p_j2k);
5901 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
5902 opj_image_destroy(p_j2k->m_private_image);
5903 p_j2k->m_private_image = NULL;
5907 *p_image = opj_image_create0();
5912 /* Copy codestream image information to the output image */
5913 opj_copy_image_header(p_j2k->m_private_image, *p_image);
5920 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
5922 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
5925 assert(p_j2k != 00);
5927 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_read_header_procedure);
5929 /* DEVELOPER CORNER, add your custom procedures */
5930 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_copy_default_tcp_and_create_tcd);
5935 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
5936 * are valid. Developpers wanting to extend the library can add their own validation procedures.
5938 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
5941 assert(p_j2k != 00);
5943 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_decoder);
5944 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_decoding_validation);
5945 /* DEVELOPER CORNER, add your custom validation procedure */
5950 * Builds the cp decoder parameters to use to decode tile.
5952 opj_bool j2k_build_decoder (
5953 opj_j2k_v2_t * p_j2k,
5954 opj_stream_private_t *p_stream,
5955 opj_event_mgr_t * p_manager
5958 // add here initialization of cp
5959 // copy paste of setup_decoder
5964 * The default decoding validation procedure without any extension.
5966 * @param p_j2k the jpeg2000 codec to validate.
5967 * @param p_stream the input stream to validate.
5968 * @param p_manager the user event manager.
5970 * @return true if the parameters are correct.
5972 opj_bool j2k_decoding_validation (
5973 opj_j2k_v2_t *p_j2k,
5974 opj_stream_private_t *p_stream,
5975 opj_event_mgr_t * p_manager
5978 opj_bool l_is_valid = OPJ_TRUE;
5981 assert(p_j2k != 00);
5982 assert(p_stream != 00);
5983 assert(p_manager != 00);
5986 /* STATE checking */
5987 /* make sure the state is at 0 */
5989 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
5991 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
5993 /* POINTER validation */
5994 /* make sure a p_j2k codec is present */
5995 /* make sure a procedure list is present */
5996 l_is_valid &= (p_j2k->m_procedure_list != 00);
5997 /* make sure a validation list is present */
5998 l_is_valid &= (p_j2k->m_validation_list != 00);
6000 /* PARAMETER VALIDATION */
6004 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
6005 struct opj_stream_private *p_stream,
6006 struct opj_event_mgr * p_manager)
6008 OPJ_UINT32 l_current_marker;
6009 OPJ_UINT32 l_marker_size;
6010 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6013 assert(p_stream != 00);
6014 assert(p_j2k != 00);
6015 assert(p_manager != 00);
6017 /* We enter in the main header */
6018 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
6020 /* Try to read the SOC marker, the codestream must begin with SOC marker */
6021 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
6022 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
6026 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6027 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6028 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6032 /* Read 2 bytes as the new marker ID */
6033 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6035 /* Try to read until the SOT is detected */
6036 while (l_current_marker != J2K_MS_SOT) {
6038 /* Check if the current marker ID is valid */
6039 if (l_current_marker < 0xff00) {
6040 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
6044 /* Get the marker handler from the marker ID */
6045 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6047 /* Manage case where marker is unknown */
6048 if (l_marker_handler->id == J2K_MS_UNK) {
6049 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
6050 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
6054 if (l_current_marker == J2K_MS_SOT)
6055 break; /* SOT marker is detected main header is completely read */
6056 else /* Get the marker handler from the marker ID */
6057 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6060 /* Check if the marker is known and if it is the right place to find it */
6061 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6062 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6066 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6067 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6068 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6072 /* read 2 bytes as the marker size */
6073 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6074 l_marker_size -= 2; // Subtract the size of the marker ID already read
6076 /* Check if the marker size is compatible with the header data size */
6077 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6078 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
6079 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
6080 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
6083 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6086 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6087 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) {
6088 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6092 /* Read the marker segment with the correct marker handler */
6093 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6094 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
6098 /* Add the marker to the codestream index*/
6099 j2k_add_mhmarker_v2(p_j2k->cstr_index,
6100 l_marker_handler->id,
6101 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6102 l_marker_size + 4 );
6104 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6105 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6106 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6110 /* read 2 bytes as the new marker ID */
6111 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6114 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decode.\n");
6116 /* Position of the last element if the main header */
6117 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
6119 /* Next step: read a tile-part header */
6120 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6126 * Excutes the given procedures on the given codec.
6128 * @param p_procedure_list the list of procedures to execute
6129 * @param p_j2k the jpeg2000 codec to execute the procedures on.
6130 * @param p_stream the stream to execute the procedures on.
6131 * @param p_manager the user manager.
6133 * @return true if all the procedures were successfully executed.
6135 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
6136 opj_procedure_list_t * p_procedure_list,
6137 opj_stream_private_t *p_stream,
6138 opj_event_mgr_t * p_manager )
6140 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
6141 opj_bool l_result = OPJ_TRUE;
6142 OPJ_UINT32 l_nb_proc, i;
6145 assert(p_procedure_list != 00);
6146 assert(p_j2k != 00);
6147 assert(p_stream != 00);
6148 assert(p_manager != 00);
6151 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
6152 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);
6154 for (i=0;i<l_nb_proc;++i) {
6155 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
6159 // and clear the procedure list at the end.
6160 opj_procedure_list_clear(p_procedure_list);
6165 opj_bool j2k_copy_default_tcp_and_create_tcd
6167 opj_j2k_v2_t * p_j2k,
6168 opj_stream_private_t *p_stream,
6169 opj_event_mgr_t * p_manager
6172 opj_tcp_v2_t * l_tcp = 00;
6173 opj_tcp_v2_t * l_default_tcp = 00;
6174 OPJ_UINT32 l_nb_tiles;
6176 opj_tccp_t *l_current_tccp = 00;
6177 OPJ_UINT32 l_tccp_size;
6178 OPJ_UINT32 l_mct_size;
6179 opj_image_t * l_image;
6180 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
6181 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
6182 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
6183 OPJ_UINT32 l_offset;
6186 assert(p_j2k != 00);
6187 assert(p_stream != 00);
6188 assert(p_manager != 00);
6190 l_image = p_j2k->m_private_image;
6191 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6192 l_tcp = p_j2k->m_cp.tcps;
6193 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
6194 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
6195 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
6198 for (i=0; i<l_nb_tiles; ++i) {
6199 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
6200 l_current_tccp = l_tcp->tccps;
6201 /*Copy default coding parameters into the current tile coding parameters*/
6202 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
6203 /* Initialize some values of the current tile coding parameters*/
6205 l_tcp->ppt_data = 00;
6206 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
6207 l_tcp->tccps = l_current_tccp;
6209 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
6210 if (l_default_tcp->m_mct_decoding_matrix) {
6211 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6212 if (! l_tcp->m_mct_decoding_matrix ) {
6215 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
6218 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
6219 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
6220 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
6221 if (! l_tcp->m_mct_records) {
6224 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
6226 /* Copy the mct record data from dflt_tile_cp to the current tile*/
6227 l_src_mct_rec = l_default_tcp->m_mct_records;
6228 l_dest_mct_rec = l_tcp->m_mct_records;
6230 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
6232 if (l_src_mct_rec->m_data) {
6234 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
6235 if(! l_dest_mct_rec->m_data) {
6238 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
6245 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
6246 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
6247 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
6248 if (! l_tcp->m_mcc_records) {
6251 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
6253 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
6254 l_src_mcc_rec = l_default_tcp->m_mcc_records;
6255 l_dest_mcc_rec = l_tcp->m_mcc_records;
6257 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
6259 if (l_src_mcc_rec->m_decorrelation_array) {
6260 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
6261 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
6264 if (l_src_mcc_rec->m_offset_array) {
6265 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
6266 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
6273 /* Copy all the dflt_tile_compo_cp to the current tile cp */
6274 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
6276 /* Move to next tile cp*/
6280 /* Create the current tile decoder*/
6281 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); // FIXME why a cast ?
6282 if (! p_j2k->m_tcd ) {
6286 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
6287 tcd_destroy_v2(p_j2k->m_tcd);
6289 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6297 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
6298 * with the marker value.
6299 * @param p_id Marker value to look up
6301 * @return the handler associated with the id.
6303 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (const OPJ_UINT32 p_id)
6305 const opj_dec_memory_marker_handler_t *e;
6306 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
6307 if (e->id == p_id) {
6308 break; // we find a handler corresponding to the marker ID
6316 * Destroys a jpeg2000 codec.
6318 * @param p_j2k the jpeg20000 structure to destroy.
6320 void j2k_destroy (opj_j2k_v2_t *p_j2k)
6326 if (p_j2k->m_is_decoder) {
6328 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
6329 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
6330 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
6331 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
6334 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
6335 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
6336 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
6337 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
6342 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
6343 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
6344 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
6347 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6348 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
6349 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
6350 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
6353 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6354 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
6355 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
6356 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
6360 tcd_destroy_v2(p_j2k->m_tcd);
6362 j2k_cp_destroy(&(p_j2k->m_cp));
6363 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
6365 opj_procedure_list_destroy(p_j2k->m_procedure_list);
6366 p_j2k->m_procedure_list = 00;
6368 opj_procedure_list_destroy(p_j2k->m_validation_list);
6369 p_j2k->m_procedure_list = 00;
6371 j2k_destroy_cstr_index(p_j2k->cstr_index);
6372 p_j2k->cstr_index = NULL;
6374 opj_image_destroy(p_j2k->m_private_image);
6375 p_j2k->m_private_image = NULL;
6377 opj_image_destroy(p_j2k->m_output_image);
6378 p_j2k->m_output_image = NULL;
6383 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
6387 if (p_cstr_ind->marker) {
6388 opj_free(p_cstr_ind->marker);
6389 p_cstr_ind->marker = NULL;
6392 if (p_cstr_ind->tile_index) {
6393 OPJ_UINT32 it_tile = 0;
6395 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
6397 if(p_cstr_ind->tile_index[it_tile].packet_index) {
6398 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
6399 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
6402 if(p_cstr_ind->tile_index[it_tile].tp_index){
6403 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
6404 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
6407 if(p_cstr_ind->tile_index[it_tile].marker){
6408 opj_free(p_cstr_ind->tile_index[it_tile].marker);
6409 p_cstr_ind->tile_index[it_tile].marker = NULL;
6414 opj_free( p_cstr_ind->tile_index);
6415 p_cstr_ind->tile_index = NULL;
6418 opj_free(p_cstr_ind);
6425 * Destroys a tile coding parameter structure.
6427 * @param p_tcp the tile coding parameter to destroy.
6429 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
6435 if (p_tcp->ppt_buffer != 00) {
6436 opj_free(p_tcp->ppt_buffer);
6437 p_tcp->ppt_buffer = 00;
6440 if (p_tcp->tccps != 00) {
6441 opj_free(p_tcp->tccps);
6445 if (p_tcp->m_mct_coding_matrix != 00) {
6446 opj_free(p_tcp->m_mct_coding_matrix);
6447 p_tcp->m_mct_coding_matrix = 00;
6450 if (p_tcp->m_mct_decoding_matrix != 00) {
6451 opj_free(p_tcp->m_mct_decoding_matrix);
6452 p_tcp->m_mct_decoding_matrix = 00;
6455 if (p_tcp->m_mcc_records) {
6456 opj_free(p_tcp->m_mcc_records);
6457 p_tcp->m_mcc_records = 00;
6458 p_tcp->m_nb_max_mcc_records = 0;
6459 p_tcp->m_nb_mcc_records = 0;
6462 if (p_tcp->m_mct_records) {
6463 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
6466 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
6467 if (l_mct_data->m_data) {
6468 opj_free(l_mct_data->m_data);
6469 l_mct_data->m_data = 00;
6475 opj_free(p_tcp->m_mct_records);
6476 p_tcp->m_mct_records = 00;
6479 if (p_tcp->mct_norms != 00) {
6480 opj_free(p_tcp->mct_norms);
6481 p_tcp->mct_norms = 00;
6484 if (p_tcp->m_data) {
6485 opj_free(p_tcp->m_data);
6492 * Destroys a coding parameter structure.
6494 * @param p_cp the coding parameter to destroy.
6496 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
6498 OPJ_UINT32 l_nb_tiles;
6499 opj_tcp_v2_t * l_current_tile = 00;
6510 l_current_tile = p_cp->tcps;
6511 l_nb_tiles = p_cp->th * p_cp->tw;
6514 (i = 0; i < l_nb_tiles; ++i)
6516 j2k_tcp_destroy(l_current_tile);
6519 opj_free(p_cp->tcps);
6523 (p_cp->ppm_buffer != 00)
6525 opj_free(p_cp->ppm_buffer);
6526 p_cp->ppm_buffer = 00;
6529 (p_cp->comment != 00)
6531 opj_free(p_cp->comment);
6535 (! p_cp->m_is_decoder)
6538 (p_cp->m_specific_param.m_enc.m_matrice)
6540 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
6541 p_cp->m_specific_param.m_enc.m_matrice = 00;
6549 * Reads a tile header.
6550 * @param p_j2k the jpeg2000 codec.
6551 * @param p_stream the stream to write data to.
6552 * @param p_manager the user event manager.
6554 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
6555 OPJ_UINT32 * p_tile_index,
6556 OPJ_UINT32 * p_data_size,
6557 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
6558 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
6559 OPJ_UINT32 * p_nb_comps,
6561 opj_stream_private_t *p_stream,
6562 opj_event_mgr_t * p_manager )
6564 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
6565 OPJ_UINT32 l_marker_size;
6566 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6567 opj_tcp_v2_t * l_tcp = NULL;
6568 OPJ_UINT32 l_nb_tiles;
6571 assert(p_stream != 00);
6572 assert(p_j2k != 00);
6573 assert(p_manager != 00);
6575 /* Reach the End Of Codestream ?*/
6576 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
6577 l_current_marker = J2K_MS_EOC;
6579 /* We need to encounter a SOT marker (a new tile-part header) */
6580 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
6584 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
6585 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
6587 /* Try to read until the Start Of Data is detected */
6588 while (l_current_marker != J2K_MS_SOD) {
6590 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6591 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6592 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6596 /* Read 2 bytes from the buffer as the marker size */
6597 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6599 /* Why this condition? FIXME */
6600 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
6601 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
6603 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
6605 /* Get the marker handler from the marker ID */
6606 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6608 /* Check if the marker is known and if it is the right place to find it */
6609 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6610 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6613 /* FIXME manage case of unknown marker as in the main header ? */
6615 /* Check if the marker size is compatible with the header data size */
6616 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6617 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
6618 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
6619 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
6622 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6625 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6626 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) {
6627 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6631 /* Read the marker segment with the correct marker handler */
6632 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6633 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
6637 /* if (l_current_marker == J2K_MS_SOT)
6638 j2k_add_tlmarker();*/
6640 /* Add the marker to the codestream index*/
6641 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
6643 l_marker_handler->id,
6644 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6645 l_marker_size + 4 );
6647 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
6648 /* Skip the rest of the tile part header*/
6649 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) {
6650 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6653 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
6656 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6657 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6658 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6661 /* Read 2 bytes from the buffer as the new marker ID */
6662 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6666 /* If we didn't skip data before, we need to read the SOD marker*/
6667 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
6668 /* Try to read the SOD marker and skip data ? FIXME */
6669 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
6675 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
6676 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6677 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6678 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6682 /* Read 2 bytes from buffer as the new marker ID */
6683 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6687 /* Indicate we will try to read a new tile-part header*/
6688 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
6689 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
6690 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6692 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6693 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6694 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6698 /* Read 2 bytes from buffer as the new marker ID */
6699 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6703 /* Current marker is the EOC marker ?*/
6704 if (l_current_marker == J2K_MS_EOC) {
6705 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
6706 p_j2k->m_current_tile_number = 0;
6707 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
6712 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
6713 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
6714 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6716 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
6717 ++p_j2k->m_current_tile_number;
6721 if (p_j2k->m_current_tile_number == l_nb_tiles) {
6722 *p_go_on = OPJ_FALSE;
6728 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
6729 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6733 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
6734 p_j2k->m_current_tile_number +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
6736 *p_tile_index = p_j2k->m_current_tile_number;
6737 *p_go_on = OPJ_TRUE;
6738 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
6739 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
6740 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
6741 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
6742 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
6743 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
6745 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;// FIXME J2K_DEC_STATE_DATA;
6751 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
6752 OPJ_UINT32 p_tile_index,
6754 OPJ_UINT32 p_data_size,
6755 opj_stream_private_t *p_stream,
6756 opj_event_mgr_t * p_manager )
6758 OPJ_UINT32 l_current_marker;
6759 OPJ_BYTE l_data [2];
6760 opj_tcp_v2_t * l_tcp;
6763 assert(p_stream != 00);
6764 assert(p_j2k != 00);
6765 assert(p_manager != 00);
6767 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
6768 || (p_tile_index != p_j2k->m_current_tile_number) ) {
6772 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
6773 if (! l_tcp->m_data) {
6774 j2k_tcp_destroy(&(p_j2k->m_cp.tcps[p_tile_index]));
6778 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
6782 p_j2k->cstr_index) ) {
6783 j2k_tcp_destroy(l_tcp);
6784 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;//FIXME J2K_DEC_STATE_ERR;
6788 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
6792 j2k_tcp_destroy(l_tcp);
6793 p_j2k->m_tcd->tcp = 0;
6795 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
6796 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));// FIXME J2K_DEC_STATE_DATA);
6798 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ //FIXME J2K_DEC_STATE_EOC)
6799 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
6800 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6804 opj_read_bytes(l_data,&l_current_marker,2);
6806 if (l_current_marker == J2K_MS_EOC) {
6807 p_j2k->m_current_tile_number = 0;
6808 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;//FIXME J2K_DEC_STATE_EOC;
6810 else if (l_current_marker != J2K_MS_SOT)
6812 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
6821 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
6823 OPJ_UINT32 i,j,k = 0;
6824 OPJ_UINT32 l_width_src,l_height_src;
6825 OPJ_UINT32 l_width_dest,l_height_dest;
6826 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
6827 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
6828 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
6829 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
6830 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
6832 opj_image_comp_t * l_img_comp_src = 00;
6833 opj_image_comp_t * l_img_comp_dest = 00;
6835 opj_tcd_tilecomp_v2_t * l_tilec = 00;
6836 opj_image_t * l_image_src = 00;
6837 OPJ_UINT32 l_size_comp, l_remaining;
6838 OPJ_INT32 * l_dest_ptr;
6839 opj_tcd_resolution_v2_t* l_res= 00;
6841 l_tilec = p_tcd->tcd_image->tiles->comps;
6842 l_image_src = p_tcd->image;
6843 l_img_comp_src = l_image_src->comps;
6845 l_img_comp_dest = p_output_image->comps;
6847 for (i=0; i<l_image_src->numcomps; i++) {
6849 /* Allocate output component buffer if necessary */
6850 if (!l_img_comp_dest->data) {
6852 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
6853 if (! l_img_comp_dest->data) {
6858 /* Copy info from decoded comp image to output image */
6859 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
6862 /* Compute the precision of the output buffer */
6863 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
6864 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
6865 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
6871 if (l_size_comp == 3) {
6876 /* Current tile component size*/
6877 l_width_src = (l_res->x1 - l_res->x0);
6878 l_height_src = (l_res->y1 - l_res->y0);
6880 /* Border of the current output component*/
6881 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
6882 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
6883 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
6884 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
6887 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
6888 * of the input buffer (decoded tile component) which will be move
6889 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
6890 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
6891 * by this input area.
6893 if ( l_x0_dest < l_res->x0 ) {
6894 l_start_x_dest = l_res->x0 - l_x0_dest;
6895 l_offset_x0_src = 0;
6897 if ( l_x1_dest >= l_res->x1 ) {
6898 l_width_dest = l_width_src;
6899 l_offset_x1_src = 0;
6902 l_width_dest = l_x1_dest - l_res->x0 ;
6903 l_offset_x1_src = l_width_src - l_width_dest;
6907 l_start_x_dest = 0 ;
6908 l_offset_x0_src = l_x0_dest - l_res->x0;
6910 if ( l_x1_dest >= l_res->x1 ) {
6911 l_width_dest = l_width_src - l_offset_x0_src;
6912 l_offset_x1_src = 0;
6915 l_width_dest = l_img_comp_dest->w ;
6916 l_offset_x1_src = l_res->x1 - l_x1_dest;
6920 if ( l_y0_dest < l_res->y0 ) {
6921 l_start_y_dest = l_res->y0 - l_y0_dest;
6922 l_offset_y0_src = 0;
6924 if ( l_y1_dest >= l_res->y1 ) {
6925 l_height_dest = l_height_src;
6926 l_offset_y1_src = 0;
6929 l_height_dest = l_y1_dest - l_res->y0 ;
6930 l_offset_y1_src = l_height_src - l_height_dest;
6934 l_start_y_dest = 0 ;
6935 l_offset_y0_src = l_y0_dest - l_res->y0;
6937 if ( l_y1_dest >= l_res->y1 ) {
6938 l_height_dest = l_height_src - l_offset_y0_src;
6939 l_offset_y1_src = 0;
6942 l_height_dest = l_img_comp_dest->h ;
6943 l_offset_y1_src = l_res->y1 - l_y1_dest;
6947 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
6952 /* Compute the input buffer offset */
6953 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
6954 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
6955 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
6957 /* Compute the output buffer offset */
6958 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
6959 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
6961 /* Move the output buffer to the first place where we will write*/
6962 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
6965 fprintf(stdout, "COMPO[%d]:\n",i);
6966 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
6967 "\t tile offset:%d, %d, %d, %d\n"
6968 "\t buffer offset: %d; %d, %d\n",
6969 l_res->x0, l_res->y0, l_width_src, l_height_src,
6970 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
6971 l_start_offset_src, l_line_offset_src, l_end_offset_src);
6973 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
6974 "\t start offset: %d, line offset= %d\n",
6975 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
6979 switch (l_size_comp) {
6982 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
6983 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
6985 if (l_img_comp_src->sgnd) {
6986 for (j = 0 ; j < l_height_dest ; ++j) {
6987 for ( k = 0 ; k < l_width_dest ; ++k) {
6988 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
6991 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
6992 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
6996 for ( j = 0 ; j < l_height_dest ; ++j ) {
6997 for ( k = 0 ; k < l_width_dest ; ++k) {
6998 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
7001 l_dest_ptr+= l_line_offset_dest;
7002 l_src_ptr += l_line_offset_src;
7006 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
7007 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
7012 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
7013 l_src_ptr += l_start_offset_src;
7015 if (l_img_comp_src->sgnd) {
7016 for (j=0;j<l_height_dest;++j) {
7017 for (k=0;k<l_width_dest;++k) {
7018 *(l_dest_ptr++) = *(l_src_ptr++);
7021 l_dest_ptr+= l_line_offset_dest;
7022 l_src_ptr += l_line_offset_src ;
7026 for (j=0;j<l_height_dest;++j) {
7027 for (k=0;k<l_width_dest;++k) {
7028 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
7031 l_dest_ptr+= l_line_offset_dest;
7032 l_src_ptr += l_line_offset_src ;
7036 l_src_ptr += l_end_offset_src;
7037 p_data = (OPJ_BYTE*) l_src_ptr;
7042 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
7043 l_src_ptr += l_start_offset_src;
7045 for (j=0;j<l_height_dest;++j) {
7046 for (k=0;k<l_width_dest;++k) {
7047 *(l_dest_ptr++) = (*(l_src_ptr++));
7050 l_dest_ptr+= l_line_offset_dest;
7051 l_src_ptr += l_line_offset_src ;
7054 l_src_ptr += l_end_offset_src;
7055 p_data = (OPJ_BYTE*) l_src_ptr;
7069 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
7071 * @param p_j2k the jpeg2000 codec.
7072 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
7073 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
7074 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
7075 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
7076 * @param p_manager the user event manager
7078 * @return true if the area could be set.
7080 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
7081 opj_image_t* p_image,
7082 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
7083 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
7084 struct opj_event_mgr * p_manager )
7086 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
7087 opj_image_t * l_image = p_j2k->m_private_image;
7090 OPJ_INT32 l_comp_x1, l_comp_y1;
7091 opj_image_comp_t* l_img_comp = NULL;
7093 /* Check if we are read the main header */
7094 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { // FIXME J2K_DEC_STATE_TPHSOT)
7095 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
7099 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
7100 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the all image\n");
7102 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7103 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7104 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7105 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
7111 /* Check if the positions provided by the user are correct */
7114 if (p_start_x > l_image->x1 ) {
7115 opj_event_msg_v2(p_manager, EVT_ERROR,
7116 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
7117 p_start_x, l_image->x1);
7120 else if (p_start_x < l_image->x0){
7121 opj_event_msg_v2(p_manager, EVT_WARNING,
7122 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
7123 p_start_x, l_image->x0);
7124 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7125 p_image->x0 = l_image->x0;
7128 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
7129 p_image->x0 = p_start_x;
7133 if (p_start_y > l_image->y1){
7134 opj_event_msg_v2(p_manager, EVT_ERROR,
7135 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
7136 p_start_y, l_image->y1);
7139 else if (p_start_y < l_image->y0){
7140 opj_event_msg_v2(p_manager, EVT_WARNING,
7141 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
7142 p_start_y, l_image->y0);
7143 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7144 p_image->y0 = l_image->y0;
7147 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
7148 p_image->y0 = p_start_y;
7152 if (p_end_x < l_image->x0) {
7153 opj_event_msg_v2(p_manager, EVT_ERROR,
7154 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
7155 p_end_x, l_image->x0);
7158 else if (p_end_x > l_image->x1) {
7159 opj_event_msg_v2(p_manager, EVT_WARNING,
7160 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
7161 p_end_x, l_image->x1);
7162 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7163 p_image->x1 = l_image->x1;
7166 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
7167 p_image->x1 = p_end_x;
7171 if (p_end_y < l_image->y0) {
7172 opj_event_msg_v2(p_manager, EVT_ERROR,
7173 "Right position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
7174 p_end_y, l_image->y0);
7177 if (p_end_y > l_image->y1){
7178 opj_event_msg_v2(p_manager, EVT_WARNING,
7179 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
7180 p_end_y, l_image->y1);
7181 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
7182 p_image->y1 = l_image->y1;
7185 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
7186 p_image->y1 = p_end_y;
7190 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
7192 l_img_comp = p_image->comps;
7193 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
7195 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
7196 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
7197 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
7198 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
7200 l_img_comp->w = int_ceildivpow2(l_comp_x1 - l_img_comp->x0, l_img_comp->factor);
7201 if (l_img_comp->w <= 0){
7202 opj_event_msg_v2(p_manager, EVT_ERROR,
7203 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
7204 it_comp, l_img_comp->w);
7208 l_img_comp->h = int_ceildivpow2(l_comp_y1 - l_img_comp->y0, l_img_comp->factor);
7209 if (l_img_comp->h <= 0){
7210 opj_event_msg_v2(p_manager, EVT_ERROR,
7211 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
7212 it_comp, l_img_comp->h);
7219 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
7220 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
7227 /* ----------------------------------------------------------------------- */
7228 /* J2K / JPT decoder interface */
7229 /* ----------------------------------------------------------------------- */
7231 * Creates a J2K decompression structure.
7233 * @return a handle to a J2K decompressor if successful, NULL otherwise.
7235 opj_j2k_v2_t* j2k_create_decompress_v2()
7237 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7241 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7243 l_j2k->m_is_decoder = 1;
7244 l_j2k->m_cp.m_is_decoder = 1;
7246 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
7247 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
7251 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
7253 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7254 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
7259 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
7261 /* codestream index creation */
7262 l_j2k->cstr_index = j2k_create_cstr_index();
7264 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
7265 if (!l_j2k->cstr_index){
7270 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
7273 /* validation list creation */
7274 l_j2k->m_validation_list = opj_procedure_list_create();
7275 if (! l_j2k->m_validation_list) {
7280 /* execution list creation */
7281 l_j2k->m_procedure_list = opj_procedure_list_create();
7282 if (! l_j2k->m_procedure_list) {
7291 opj_codestream_index_t* j2k_create_cstr_index(void)
7293 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
7294 opj_calloc(1,sizeof(opj_codestream_index_t));
7298 cstr_index->maxmarknum = 100;
7299 cstr_index->marknum = 0;
7300 cstr_index->marker = (opj_marker_info_t*)
7301 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
7302 if (!cstr_index-> marker)
7305 cstr_index->tile_index = NULL;
7311 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
7312 * @param p_header_data the data contained in the COM box.
7313 * @param p_j2k the jpeg2000 codec.
7314 * @param p_header_size the size of the data contained in the COM marker.
7315 * @param p_manager the user event manager.
7317 opj_bool j2k_read_SPCod_SPCoc(
7318 opj_j2k_v2_t *p_j2k,
7320 OPJ_BYTE * p_header_data,
7321 OPJ_UINT32 * p_header_size,
7322 struct opj_event_mgr * p_manager
7325 OPJ_UINT32 i, l_tmp;
7326 opj_cp_v2_t *l_cp = NULL;
7327 opj_tcp_v2_t *l_tcp = NULL;
7328 opj_tccp_t *l_tccp = NULL;
7329 OPJ_BYTE * l_current_ptr = NULL;
7332 assert(p_j2k != 00);
7333 assert(p_manager != 00);
7334 assert(p_header_data != 00);
7336 l_cp = &(p_j2k->m_cp);
7337 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
7338 &l_cp->tcps[p_j2k->m_current_tile_number] :
7339 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7341 /* precondition again */
7342 assert(compno < p_j2k->m_private_image->numcomps);
7344 l_tccp = &l_tcp->tccps[compno];
7345 l_current_ptr = p_header_data;
7347 /* make sure room is sufficient */
7348 if (*p_header_size < 5) {
7349 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
7353 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
7354 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
7357 /* If user wants to remove more resolutions than the codestream contains, return error */
7358 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
7359 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
7360 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
7361 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;// FIXME J2K_DEC_STATE_ERR;
7365 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
7369 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
7373 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
7376 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
7379 *p_header_size = *p_header_size - 5;
7381 /* use custom precinct size ? */
7382 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
7383 if (*p_header_size < l_tccp->numresolutions) {
7384 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
7388 for (i = 0; i < l_tccp->numresolutions; ++i) {
7389 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
7391 l_tccp->prcw[i] = l_tmp & 0xf;
7392 l_tccp->prch[i] = l_tmp >> 4;
7395 *p_header_size = *p_header_size - l_tccp->numresolutions;
7398 /* set default size for the precinct width and height */
7399 for (i = 0; i < l_tccp->numresolutions; ++i) {
7400 l_tccp->prcw[i] = 15;
7401 l_tccp->prch[i] = 15;
7405 #ifdef WIP_REMOVE_MSD
7407 if (p_j2k->cstr_info && compno == 0) {
7408 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
7410 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
7411 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
7412 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
7413 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
7414 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
7417 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
7418 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
7427 * Copies the tile component parameters of all the component from the first tile component.
7429 * @param p_j2k the J2k codec.
7431 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
7435 opj_cp_v2_t *l_cp = NULL;
7436 opj_tcp_v2_t *l_tcp = NULL;
7437 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
7438 OPJ_UINT32 l_prc_size;
7441 assert(p_j2k != 00);
7443 l_cp = &(p_j2k->m_cp);
7444 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
7445 &l_cp->tcps[p_j2k->m_current_tile_number] :
7446 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7448 l_ref_tccp = &l_tcp->tccps[0];
7449 l_copied_tccp = l_ref_tccp + 1;
7450 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
7452 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
7453 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
7454 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
7455 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
7456 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
7457 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
7458 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
7459 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
7465 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
7467 * @param p_comp_no the component being targeted.
7468 * @param p_header_data the data contained in the COM box.
7469 * @param p_j2k the jpeg2000 codec.
7470 * @param p_header_size the size of the data contained in the COM marker.
7471 * @param p_manager the user event manager.
7473 opj_bool j2k_read_SQcd_SQcc(
7474 opj_j2k_v2_t *p_j2k,
7475 OPJ_UINT32 p_comp_no,
7476 OPJ_BYTE* p_header_data,
7477 OPJ_UINT32 * p_header_size,
7478 struct opj_event_mgr * p_manager
7482 OPJ_UINT32 l_band_no;
7483 opj_cp_v2_t *l_cp = 00;
7484 opj_tcp_v2_t *l_tcp = 00;
7485 opj_tccp_t *l_tccp = 00;
7486 OPJ_BYTE * l_current_ptr = 00;
7487 OPJ_UINT32 l_tmp, l_num_band;
7490 assert(p_j2k != 00);
7491 assert(p_manager != 00);
7492 assert(p_header_data != 00);
7494 l_cp = &(p_j2k->m_cp);
7495 // come from tile part header or main header ?
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 // precondition again
7501 assert(p_comp_no < p_j2k->m_private_image->numcomps);
7503 l_tccp = &l_tcp->tccps[p_comp_no];
7504 l_current_ptr = p_header_data;
7506 if (*p_header_size < 1) {
7507 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
7510 *p_header_size -= 1;
7512 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
7515 l_tccp->qntsty = l_tmp & 0x1f;
7516 l_tccp->numgbits = l_tmp >> 5;
7517 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
7521 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
7523 (*p_header_size) / 2;
7525 if( l_num_band > J2K_MAXBANDS ) {
7526 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
7527 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limiting the number of elements stored to "
7528 "J2K_MAXBANDS (%d) and skip the other. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
7534 if (l_cp->correct) {
7536 /* if JPWL is on, we check whether there are too many subbands */
7537 if ((l_num_band < 0) || (l_num_band >= J2K_MAXBANDS)) {
7538 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
7539 "JPWL: bad number of subbands in Sqcx (%d)\n",
7542 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
7545 /* we try to correct */
7547 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
7548 "- setting number of bands to %d => HYPOTHESIS!!!\n",
7553 #endif /* USE_JPWL */
7555 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
7556 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
7557 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
7559 if (l_band_no < J2K_MAXBANDS){
7560 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
7561 l_tccp->stepsizes[l_band_no].mant = 0;
7564 *p_header_size = *p_header_size - l_num_band;
7567 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
7568 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
7570 if (l_band_no < J2K_MAXBANDS){
7571 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
7572 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
7575 *p_header_size = *p_header_size - 2*l_num_band;
7578 /* Add Antonin : if scalar_derived -> compute other stepsizes */
7579 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
7580 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
7581 l_tccp->stepsizes[l_band_no].expn =
7582 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
7583 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
7584 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
7592 * Copies the tile component parameters of all the component from the first tile component.
7594 * @param p_j2k the J2k codec.
7596 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
7599 opj_cp_v2_t *l_cp = NULL;
7600 opj_tcp_v2_t *l_tcp = NULL;
7601 opj_tccp_t *l_ref_tccp = NULL;
7602 opj_tccp_t *l_copied_tccp = NULL;
7606 assert(p_j2k != 00);
7608 l_cp = &(p_j2k->m_cp);
7609 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7610 &l_cp->tcps[p_j2k->m_current_tile_number] :
7611 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7613 l_ref_tccp = &l_tcp->tccps[0];
7614 l_copied_tccp = l_ref_tccp + 1;
7615 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
7617 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
7618 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
7619 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
7620 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
7626 * Dump some elements from the J2K decompression structure .
7628 *@param p_j2k the jpeg2000 codec.
7629 *@param flag flag to describe what elments are dump.
7630 *@param out_stream output stream where dump the elements.
7633 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
7635 /* Check if the flag is compatible with j2k file*/
7636 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
7637 fprintf(out_stream, "Wrong flag\n");
7641 /* Dump the image_header */
7642 if (flag & OPJ_IMG_INFO){
7643 if (p_j2k->m_private_image)
7644 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
7647 /* Dump the codestream info from main header */
7648 if (flag & OPJ_J2K_MH_INFO){
7649 j2k_dump_MH_info(p_j2k, out_stream);
7653 /* Dump the codestream info of the current tile */
7654 if (flag & OPJ_J2K_TH_INFO){
7658 /* Dump the codestream index from main header */
7659 if (flag & OPJ_J2K_MH_IND){
7660 j2k_dump_MH_index(p_j2k, out_stream);
7663 /* Dump the codestream index of the current tile */
7664 if (flag & OPJ_J2K_TH_IND){
7671 * Dump index elements of the codestream extract from the main header.
7673 *@param p_j2k the jpeg2000 codec.
7674 *@param out_stream output stream where dump the elements.
7677 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
7679 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
7680 OPJ_UINT32 it_marker, it_tile, it_tile_part;
7682 fprintf(out_stream, "Codestream index from main header: {\n");
7684 fprintf(out_stream, "\t Main header start position=%d\n\t Main header end position=%d\n",
7685 cstr_index->main_head_start, cstr_index->main_head_end);
7687 fprintf(out_stream, "\t Marker list: {\n");
7689 if (cstr_index->marker){
7690 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
7691 fprintf(out_stream, "\t\t type=%#x, pos=%d, len=%d\n",
7692 cstr_index->marker[it_marker].type,
7693 cstr_index->marker[it_marker].pos,
7694 cstr_index->marker[it_marker].len );
7698 fprintf(out_stream, "\t }\n");
7701 if (cstr_index->tile_index){
7702 fprintf(out_stream, "\t Tile index: {\n");
7704 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
7705 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
7707 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
7709 if (cstr_index->tile_index[it_tile].tp_index){
7710 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
7711 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%d, end_header=%d, end_pos=%d.\n",
7713 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
7714 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
7715 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
7719 if (cstr_index->tile_index[it_tile].marker){
7720 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
7721 fprintf(out_stream, "\t\t type=%#x, pos=%d, len=%d\n",
7722 cstr_index->tile_index[it_tile].marker[it_marker].type,
7723 cstr_index->tile_index[it_tile].marker[it_marker].pos,
7724 cstr_index->tile_index[it_tile].marker[it_marker].len );
7728 fprintf(out_stream,"\t }\n");
7731 fprintf(out_stream,"}\n");
7736 * Dump info elements of the codestream extract from the main header.
7738 *@param p_j2k the jpeg2000 codec.
7739 *@param out_stream output stream where dump the elements.
7742 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
7744 opj_tcp_v2_t * l_default_tile=NULL;
7746 fprintf(out_stream, "Codestream info from main header: {\n");
7748 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
7749 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
7750 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
7752 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7756 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
7758 fprintf(out_stream, "\t default tile {\n");
7759 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
7760 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
7761 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
7762 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
7764 for (compno = 0; compno < numcomps; compno++) {
7765 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
7766 OPJ_INT32 resno, bandno, numbands;
7769 fprintf(out_stream, "\t\t comp %d {\n", compno);
7770 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
7771 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
7772 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
7773 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
7774 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
7775 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
7777 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
7778 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
7779 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
7781 fprintf(out_stream, "\n");
7783 /* quantization style*/
7784 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
7785 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
7786 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
7787 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
7788 for (bandno = 0; bandno < numbands; bandno++) {
7789 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
7790 l_tccp->stepsizes[bandno].expn);
7792 fprintf(out_stream, "\n");
7795 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
7797 fprintf(out_stream, "\t\t }\n");
7798 } /*end of component of default tile*/
7799 fprintf(out_stream, "\t }\n"); /*end of default tile*/
7803 fprintf(out_stream, "}\n");
7808 * Dump an image header structure.
7810 *@param img_header the image header to dump.
7811 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
7812 *@param out_stream output stream where dump the elements.
7814 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
7819 fprintf(stdout, "[DEV] Dump a image_header struct {\n");
7823 fprintf(out_stream, "Image info {\n");
7824 tab[0] = '\t';tab[1] = '\0';
7827 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
7828 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
7829 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
7831 if (img_header->comps){
7833 for (compno = 0; compno < img_header->numcomps; compno++) {
7834 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
7835 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
7836 fprintf(out_stream,"%s}\n",tab);
7840 fprintf(out_stream, "}\n");
7844 * Dump a component image header structure.
7846 *@param comp_header the component image header to dump.
7847 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
7848 *@param out_stream output stream where dump the elements.
7850 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
7855 fprintf(stdout, "[DEV] Dump a image_comp_header struct {\n");
7858 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
7861 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
7862 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
7863 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
7866 fprintf(out_stream, "}\n");
7871 * Get the codestream info from a JPEG2000 codec.
7873 *@param p_j2k the component image header to dump.
7875 *@return the codestream information extract from the jpg2000 codec
7877 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
7880 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
7881 opj_tcp_v2_t *l_default_tile;
7882 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
7884 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
7886 cstr_info->tx0 = p_j2k->m_cp.tx0;
7887 cstr_info->ty0 = p_j2k->m_cp.ty0;
7888 cstr_info->tdx = p_j2k->m_cp.tdx;
7889 cstr_info->tdy = p_j2k->m_cp.tdy;
7890 cstr_info->tw = p_j2k->m_cp.tw;
7891 cstr_info->th = p_j2k->m_cp.th;
7893 cstr_info->tile_info = NULL; /* Not fill from the main header*/
7895 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7897 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
7898 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
7899 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
7900 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
7902 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
7904 for (compno = 0; compno < numcomps; compno++) {
7905 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
7906 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
7907 OPJ_INT32 bandno, numbands;
7910 l_tccp_info->csty = l_tccp->csty;
7911 l_tccp_info->numresolutions = l_tccp->numresolutions;
7912 l_tccp_info->cblkw = l_tccp->cblkw;
7913 l_tccp_info->cblkh = l_tccp->cblkh;
7914 l_tccp_info->cblksty = l_tccp->cblksty;
7915 l_tccp_info->qmfbid = l_tccp->qmfbid;
7916 if (l_tccp->numresolutions < J2K_MAXRLVLS)
7918 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
7919 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
7922 /* quantization style*/
7923 l_tccp_info->qntsty = l_tccp->qntsty;
7924 l_tccp_info->numgbits = l_tccp->numgbits;
7926 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
7927 if (numbands < J2K_MAXBANDS) {
7928 for (bandno = 0; bandno < numbands; bandno++) {
7929 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
7930 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
7935 l_tccp_info->roishift = l_tccp->roishift;
7943 * Get the codestream index from a JPEG2000 codec.
7945 *@param p_j2k the component image header to dump.
7947 *@return the codestream index extract from the jpg2000 codec
7949 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
7951 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
7952 opj_calloc(1,sizeof(opj_codestream_index_t));
7956 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
7957 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
7958 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
7960 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
7961 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
7962 if (!l_cstr_index->marker){
7963 opj_free( l_cstr_index);
7967 if (p_j2k->cstr_index->marker)
7968 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
7970 opj_free(l_cstr_index->marker);
7971 l_cstr_index->marker = NULL;
7974 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
7975 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
7976 if (!l_cstr_index->tile_index){
7977 opj_free( l_cstr_index->marker);
7978 opj_free( l_cstr_index);
7982 if (!p_j2k->cstr_index->tile_index){
7983 opj_free(l_cstr_index->tile_index);
7984 l_cstr_index->tile_index = NULL;
7987 OPJ_UINT32 it_tile = 0;
7988 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
7991 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
7993 l_cstr_index->tile_index[it_tile].marker =
7994 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
7996 if (!l_cstr_index->tile_index[it_tile].marker) {
7997 OPJ_UINT32 it_tile_free;
7999 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
8000 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
8003 opj_free( l_cstr_index->tile_index);
8004 opj_free( l_cstr_index->marker);
8005 opj_free( l_cstr_index);
8009 if (p_j2k->cstr_index->tile_index[it_tile].marker)
8010 memcpy( l_cstr_index->tile_index[it_tile].marker,
8011 p_j2k->cstr_index->tile_index[it_tile].marker,
8012 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
8014 opj_free(l_cstr_index->tile_index[it_tile].marker);
8015 l_cstr_index->tile_index[it_tile].marker = NULL;
8018 /* Tile part index*/
8019 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
8021 l_cstr_index->tile_index[it_tile].tp_index =
8022 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
8024 if(!l_cstr_index->tile_index[it_tile].tp_index){
8025 OPJ_UINT32 it_tile_free;
8027 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
8028 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
8029 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
8032 opj_free( l_cstr_index->tile_index);
8033 opj_free( l_cstr_index->marker);
8034 opj_free( l_cstr_index);
8038 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
8039 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
8040 p_j2k->cstr_index->tile_index[it_tile].tp_index,
8041 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
8044 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
8045 l_cstr_index->tile_index[it_tile].tp_index = NULL;
8048 /* Packet index (NOT USED)*/
8049 l_cstr_index->tile_index[it_tile].nb_packet = 0;
8050 l_cstr_index->tile_index[it_tile].packet_index = NULL;
8055 return l_cstr_index;
8058 opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
8060 OPJ_UINT32 it_tile=0;
8062 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.tw;
8063 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
8064 if (!p_j2k->cstr_index->tile_index)
8067 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
8068 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
8069 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
8070 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
8071 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
8072 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
8082 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
8083 opj_stream_private_t *p_stream,
8084 opj_event_mgr_t * p_manager)
8086 opj_bool l_go_on = OPJ_TRUE;
8087 OPJ_UINT32 l_current_tile_no;
8088 OPJ_UINT32 l_data_size,l_max_data_size;
8089 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
8090 OPJ_UINT32 l_nb_comps;
8091 OPJ_BYTE * l_current_data;
8093 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
8094 if (! l_current_data) {
8097 l_max_data_size = 1000;
8099 /*Allocate and initialize some elements of codestrem index*/
8100 if (!j2k_allocate_tile_element_cstr_index(p_j2k))
8104 if (! j2k_read_tile_header( p_j2k,
8107 &l_tile_x0, &l_tile_y0,
8108 &l_tile_x1, &l_tile_y1,
8113 opj_free(l_current_data);
8121 if (l_data_size > l_max_data_size) {
8122 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
8123 if (! l_current_data) {
8127 l_max_data_size = l_data_size;
8130 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
8131 opj_free(l_current_data);
8134 opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decode.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
8136 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
8137 opj_free(l_current_data);
8140 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
8144 opj_free(l_current_data);
8150 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
8152 void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
8155 assert(p_j2k != 00);
8157 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_tiles);
8158 /* DEVELOPER CORNER, add your custom procedures */
8164 * Decodes the tiles of the stream.
8166 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
8167 opj_stream_private_t * p_stream,
8168 opj_image_t * p_image,
8169 opj_event_mgr_t * p_manager)
8176 p_j2k->m_output_image = opj_image_create0();
8177 if (! (p_j2k->m_output_image)) {
8180 opj_copy_image_header(p_image, p_j2k->m_output_image);
8182 /* customization of the decoding */
8183 j2k_setup_decoding(p_j2k);
8185 /* Decode the codestream */
8186 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8187 opj_image_destroy(p_j2k->m_private_image);
8188 p_j2k->m_private_image = NULL;
8192 /* Move data and copy one information from codec to output image*/
8193 for (compno = 0; compno < p_image->numcomps; compno++) {
8194 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
8195 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
8196 p_j2k->m_output_image->comps[compno].data = NULL;