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);
127 * Destroys a codestream index structure.
129 * @param p_cstr_ind the codestream index parameter to destroy.
131 static void j2k_destroy_cstr_index (opj_codestream_index_t* p_cstr_ind);
134 * Destroys a coding parameter structure.
136 * @param p_cp the coding parameter to destroy.
138 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
142 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
143 * @param p_header_data the data contained in the COM box.
144 * @param p_j2k the jpeg2000 codec.
145 * @param p_header_size the size of the data contained in the COM marker.
146 * @param p_manager the user event manager.
148 static opj_bool j2k_read_SPCod_SPCoc(
151 OPJ_BYTE * p_header_data,
152 OPJ_UINT32 * p_header_size,
153 struct opj_event_mgr * p_manager
157 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
159 * @param p_tile_no the tile to output.
160 * @param p_comp_no the component number to output.
161 * @param p_data the data buffer.
162 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
163 * @param p_j2k J2K codec.
164 * @param p_manager the user event manager.
167 static opj_bool j2k_read_SQcd_SQcc(
170 OPJ_BYTE * p_header_data,
171 OPJ_UINT32 * p_header_size,
172 struct opj_event_mgr * p_manager
176 * Copies the tile component parameters of all the component from the first tile component.
178 * @param p_j2k the J2k codec.
180 static void j2k_copy_tile_component_parameters(
185 * Copies the tile quantization parameters of all the component from the first tile component.
187 * @param p_j2k the J2k codec.
189 static void j2k_copy_tile_quantization_parameters(
196 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
197 opj_stream_private_t *p_stream,
198 opj_event_mgr_t * p_manager);
200 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
204 * -----------------------------------------------------------------------
205 * -----------------------------------------------------------------------
206 * -----------------------------------------------------------------------
210 Write the SOC marker (Start Of Codestream)
211 @param j2k J2K handle
213 static void j2k_write_soc(opj_j2k_t *j2k);
215 Read the SOC marker (Start of Codestream)
216 @param j2k J2K handle
218 static void j2k_read_soc(opj_j2k_t *j2k);
221 * Reads a SOC marker (Start of Codestream)
222 * @param p_header_data the data contained in the SOC box.
223 * @param jp2 the jpeg2000 file codec.
224 * @param p_header_size the size of the data contained in the SOC marker.
225 * @param p_manager the user event manager.
227 static opj_bool j2k_read_soc_v2(
229 struct opj_stream_private *p_stream,
230 struct opj_event_mgr * p_manager
234 Write the SIZ marker (image and tile size)
235 @param j2k J2K handle
237 static void j2k_write_siz(opj_j2k_t *j2k);
239 Read the SIZ marker (image and tile size)
240 @param j2k J2K handle
242 static void j2k_read_siz(opj_j2k_t *j2k);
245 * Reads a SIZ marker (image and tile size)
246 * @param p_header_data the data contained in the SIZ box.
247 * @param jp2 the jpeg2000 file codec.
248 * @param p_header_size the size of the data contained in the SIZ marker.
249 * @param p_manager the user event manager.
251 static opj_bool j2k_read_siz_v2 (
253 OPJ_BYTE * p_header_data,
254 OPJ_UINT32 p_header_size,
255 struct opj_event_mgr * p_manager
259 Write the COM marker (comment)
260 @param j2k J2K handle
262 static void j2k_write_com(opj_j2k_t *j2k);
264 Read the COM marker (comment)
265 @param j2k J2K handle
267 static void j2k_read_com(opj_j2k_t *j2k);
269 * Reads a COM marker (comments)
270 * @param p_header_data the data contained in the COM box.
271 * @param jp2 the jpeg2000 file codec.
272 * @param p_header_size the size of the data contained in the COM marker.
273 * @param p_manager the user event manager.
275 static opj_bool j2k_read_com_v2 (
277 OPJ_BYTE * p_header_data,
278 OPJ_UINT32 p_header_size,
279 struct opj_event_mgr * p_manager
282 Write 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 written
286 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
288 Read the value concerning the specified component in the marker COD and COC
289 @param j2k J2K handle
290 @param compno Number of the component concerned by the information read
292 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
294 Write the COD marker (coding style default)
295 @param j2k J2K handle
297 static void j2k_write_cod(opj_j2k_t *j2k);
299 Read the COD marker (coding style default)
300 @param j2k J2K handle
302 static void j2k_read_cod(opj_j2k_t *j2k);
305 * Reads a COD marker (Coding Styke defaults)
306 * @param p_header_data the data contained in the COD box.
307 * @param p_j2k the jpeg2000 codec.
308 * @param p_header_size the size of the data contained in the COD marker.
309 * @param p_manager the user event manager.
311 static opj_bool j2k_read_cod_v2 (
313 OPJ_BYTE * p_header_data,
314 OPJ_UINT32 p_header_size,
315 struct opj_event_mgr * p_manager
319 Write the COC marker (coding style component)
320 @param j2k J2K handle
321 @param compno Number of the component concerned by the information written
323 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
325 Read the COC marker (coding style component)
326 @param j2k J2K handle
328 static void j2k_read_coc(opj_j2k_t *j2k);
331 * Reads a COC marker (Coding Style Component)
332 * @param p_header_data the data contained in the COC box.
333 * @param p_j2k the jpeg2000 codec.
334 * @param p_header_size the size of the data contained in the COC marker.
335 * @param p_manager the user event manager.
337 static opj_bool j2k_read_coc_v2 (
339 OPJ_BYTE * p_header_data,
340 OPJ_UINT32 p_header_size,
341 struct opj_event_mgr * p_manager
345 Write the value concerning the specified component in the marker QCD and QCC
346 @param j2k J2K handle
347 @param compno Number of the component concerned by the information written
349 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
351 Read the value concerning the specified component in the marker QCD and QCC
352 @param j2k J2K handle
353 @param compno Number of the component concern by the information read
354 @param len Length of the information in the QCX part of the marker QCD/QCC
356 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
358 Write the QCD marker (quantization default)
359 @param j2k J2K handle
361 static void j2k_write_qcd(opj_j2k_t *j2k);
363 Read the QCD marker (quantization default)
364 @param j2k J2K handle
366 static void j2k_read_qcd(opj_j2k_t *j2k);
369 * Reads a QCD marker (Quantization defaults)
370 * @param p_header_data the data contained in the QCD box.
371 * @param p_j2k the jpeg2000 codec.
372 * @param p_header_size the size of the data contained in the QCD marker.
373 * @param p_manager the user event manager.
375 static opj_bool j2k_read_qcd_v2 (
377 OPJ_BYTE * p_header_data,
378 OPJ_UINT32 p_header_size,
379 struct opj_event_mgr * p_manager
383 Write the QCC marker (quantization component)
384 @param j2k J2K handle
385 @param compno Number of the component concerned by the information written
387 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
389 Read the QCC marker (quantization component)
390 @param j2k J2K handle
392 static void j2k_read_qcc(opj_j2k_t *j2k);
394 * Reads a QCC marker (Quantization component)
395 * @param p_header_data the data contained in the QCC box.
396 * @param p_j2k the jpeg2000 codec.
397 * @param p_header_size the size of the data contained in the QCC marker.
398 * @param p_manager the user event manager.
400 static opj_bool j2k_read_qcc_v2(
402 OPJ_BYTE * p_header_data,
403 OPJ_UINT32 p_header_size,
404 struct opj_event_mgr * p_manager);
407 Write the POC marker (progression order change)
408 @param j2k J2K handle
410 static void j2k_write_poc(opj_j2k_t *j2k);
412 Read the POC marker (progression order change)
413 @param j2k J2K handle
415 static void j2k_read_poc(opj_j2k_t *j2k);
417 * Reads a POC marker (Progression Order Change)
419 * @param p_header_data the data contained in the POC box.
420 * @param p_j2k the jpeg2000 codec.
421 * @param p_header_size the size of the data contained in the POC marker.
422 * @param p_manager the user event manager.
424 static opj_bool j2k_read_poc_v2 (
426 OPJ_BYTE * p_header_data,
427 OPJ_UINT32 p_header_size,
428 struct opj_event_mgr * p_manager
431 Read the CRG marker (component registration)
432 @param j2k J2K handle
434 static void j2k_read_crg(opj_j2k_t *j2k);
436 * Reads a CRG marker (Component registration)
438 * @param p_header_data the data contained in the TLM box.
439 * @param p_j2k the jpeg2000 codec.
440 * @param p_header_size the size of the data contained in the TLM marker.
441 * @param p_manager the user event manager.
443 static opj_bool j2k_read_crg_v2 (
445 OPJ_BYTE * p_header_data,
446 OPJ_UINT32 p_header_size,
447 struct opj_event_mgr * p_manager
450 Read the TLM marker (tile-part lengths)
451 @param j2k J2K handle
453 static void j2k_read_tlm(opj_j2k_t *j2k);
455 * Reads a TLM marker (Tile Length Marker)
457 * @param p_header_data the data contained in the TLM box.
458 * @param p_j2k the jpeg2000 codec.
459 * @param p_header_size the size of the data contained in the TLM marker.
460 * @param p_manager the user event manager.
462 static opj_bool j2k_read_tlm_v2 (
464 OPJ_BYTE * p_header_data,
465 OPJ_UINT32 p_header_size,
466 struct opj_event_mgr * p_manager
469 Read the PLM marker (packet length, main header)
470 @param j2k J2K handle
472 static void j2k_read_plm(opj_j2k_t *j2k);
475 * Reads a PLM marker (Packet length, main header marker)
477 * @param p_header_data the data contained in the TLM box.
478 * @param p_j2k the jpeg2000 codec.
479 * @param p_header_size the size of the data contained in the TLM marker.
480 * @param p_manager the user event manager.
482 static opj_bool j2k_read_plm_v2 (
484 OPJ_BYTE * p_header_data,
485 OPJ_UINT32 p_header_size,
486 struct opj_event_mgr * p_manager
489 Read the PLT marker (packet length, tile-part header)
490 @param j2k J2K handle
492 static void j2k_read_plt(opj_j2k_t *j2k);
494 * Reads a PLT marker (Packet length, tile-part header)
496 * @param p_header_data the data contained in the PLT box.
497 * @param p_j2k the jpeg2000 codec.
498 * @param p_header_size the size of the data contained in the PLT marker.
499 * @param p_manager the user event manager.
501 static opj_bool j2k_read_plt_v2 (
503 OPJ_BYTE * p_header_data,
504 OPJ_UINT32 p_header_size,
505 struct opj_event_mgr * p_manager
508 Read the PPM marker (packet packet headers, main header)
509 @param j2k J2K handle
511 static void j2k_read_ppm(opj_j2k_t *j2k);
513 * Reads a PPM marker (Packed packet headers, main header)
515 * @param p_header_data the data contained in the POC box.
516 * @param p_j2k the jpeg2000 codec.
517 * @param p_header_size the size of the data contained in the POC marker.
518 * @param p_manager the user event manager.
520 static opj_bool j2k_read_ppm_v2 (
522 OPJ_BYTE * p_header_data,
523 OPJ_UINT32 p_header_size,
524 struct opj_event_mgr * p_manager
527 static opj_bool j2k_read_ppm_v3 (
529 OPJ_BYTE * p_header_data,
530 OPJ_UINT32 p_header_size,
531 struct opj_event_mgr * p_manager
535 Read the PPT marker (packet packet headers, tile-part header)
536 @param j2k J2K handle
538 static void j2k_read_ppt(opj_j2k_t *j2k);
540 * Reads a PPT marker (Packed packet headers, tile-part header)
542 * @param p_header_data the data contained in the PPT box.
543 * @param p_j2k the jpeg2000 codec.
544 * @param p_header_size the size of the data contained in the PPT marker.
545 * @param p_manager the user event manager.
547 static opj_bool j2k_read_ppt_v2 (
549 OPJ_BYTE * p_header_data,
550 OPJ_UINT32 p_header_size,
551 struct opj_event_mgr * p_manager
554 Write the TLM marker (Mainheader)
555 @param j2k J2K handle
557 static void j2k_write_tlm(opj_j2k_t *j2k);
559 Write the SOT marker (start of tile-part)
560 @param j2k J2K handle
562 static void j2k_write_sot(opj_j2k_t *j2k);
564 Read the SOT marker (start of tile-part)
565 @param j2k J2K handle
567 static void j2k_read_sot(opj_j2k_t *j2k);
570 * Reads a PPT marker (Packed packet headers, tile-part header)
572 * @param p_header_data the data contained in the PPT box.
573 * @param p_j2k the jpeg2000 codec.
574 * @param p_header_size the size of the data contained in the PPT marker.
575 * @param p_manager the user event manager.
577 static opj_bool j2k_read_sot_v2 (
579 OPJ_BYTE * p_header_data,
580 OPJ_UINT32 p_header_size,
581 struct opj_event_mgr * p_manager
584 Write the SOD marker (start of data)
585 @param j2k J2K handle
586 @param tile_coder Pointer to a TCD handle
588 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
590 Read the SOD marker (start of data)
591 @param j2k J2K handle
593 static void j2k_read_sod(opj_j2k_t *j2k);
596 * Reads a SOD marker (Start Of Data)
598 * @param p_header_data the data contained in the SOD box.
599 * @param p_j2k the jpeg2000 codec.
600 * @param p_header_size the size of the data contained in the SOD marker.
601 * @param p_manager the user event manager.
603 static opj_bool j2k_read_sod_v2 (
605 struct opj_stream_private *p_stream,
606 struct opj_event_mgr * p_manager
610 Write the RGN marker (region-of-interest)
611 @param j2k J2K handle
612 @param compno Number of the component concerned by the information written
613 @param tileno Number of the tile concerned by the information written
615 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
617 Read the RGN marker (region-of-interest)
618 @param j2k J2K handle
620 static void j2k_read_rgn(opj_j2k_t *j2k);
623 * Reads a RGN marker (Region Of Interest)
625 * @param p_header_data the data contained in the POC box.
626 * @param p_j2k the jpeg2000 codec.
627 * @param p_header_size the size of the data contained in the POC marker.
628 * @param p_manager the user event manager.
630 static opj_bool j2k_read_rgn_v2 (
632 OPJ_BYTE * p_header_data,
633 OPJ_UINT32 p_header_size,
634 struct opj_event_mgr * p_manager
638 Write the EOC marker (end of codestream)
639 @param j2k J2K handle
641 static void j2k_write_eoc(opj_j2k_t *j2k);
643 Read the EOC marker (end of codestream)
644 @param j2k J2K handle
646 static void j2k_read_eoc(opj_j2k_t *j2k);
649 * Reads a EOC marker (End Of Codestream)
651 * @param p_header_data the data contained in the SOD box.
652 * @param p_j2k the jpeg2000 codec.
653 * @param p_header_size the size of the data contained in the SOD marker.
654 * @param p_manager the user event manager.
656 static opj_bool j2k_read_eoc_v2 (
658 struct opj_stream_private *p_stream,
659 struct opj_event_mgr * p_manager
663 Read an unknown marker
664 @param j2k J2K handle
666 static void j2k_read_unk(opj_j2k_t *j2k);
668 Add main header marker information
669 @param cstr_info Codestream information structure
670 @param type marker type
671 @param pos byte offset of marker segment
672 @param len length of marker segment
674 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
676 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) ;
678 Add tile header marker information
679 @param tileno tile index number
680 @param cstr_info Codestream information structure
681 @param type marker type
682 @param pos byte offset of marker segment
683 @param len length of marker segment
685 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
689 * Reads an unknown marker
691 * @param p_stream the stream object to read from.
692 * @param p_j2k the jpeg2000 codec.
693 * @param p_manager the user event manager.
695 * @return true if the marker could be deduced.
697 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
698 struct opj_stream_private *p_stream,
699 OPJ_UINT32 *output_marker,
700 struct opj_event_mgr * p_manager );
703 * Reads a MCT marker (Multiple Component Transform)
705 * @param p_header_data the data contained in the MCT box.
706 * @param p_j2k the jpeg2000 codec.
707 * @param p_header_size the size of the data contained in the MCT marker.
708 * @param p_manager the user event manager.
710 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
711 OPJ_BYTE * p_header_data,
712 OPJ_UINT32 p_header_size,
713 struct opj_event_mgr * p_manager );
716 * Reads a MCC marker (Multiple Component Collection)
718 * @param p_header_data the data contained in the MCC box.
719 * @param p_j2k the jpeg2000 codec.
720 * @param p_header_size the size of the data contained in the MCC marker.
721 * @param p_manager the user event manager.
723 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
724 OPJ_BYTE * p_header_data,
725 OPJ_UINT32 p_header_size,
726 struct opj_event_mgr * p_manager );
729 * Reads a MCO marker (Multiple Component Transform Ordering)
731 * @param p_header_data the data contained in the MCO box.
732 * @param p_j2k the jpeg2000 codec.
733 * @param p_header_size the size of the data contained in the MCO marker.
734 * @param p_manager the user event manager.
736 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
737 OPJ_BYTE * p_header_data,
738 OPJ_UINT32 p_header_size,
739 struct opj_event_mgr * p_manager );
741 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
743 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
744 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
745 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
746 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
748 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
749 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
750 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
751 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
753 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
754 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
755 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
756 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
759 * Reads a CBD marker (Component bit depth definition)
760 * @param p_header_data the data contained in the CBD box.
761 * @param p_j2k the jpeg2000 codec.
762 * @param p_header_size the size of the data contained in the CBD marker.
763 * @param p_manager the user event manager.
765 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
766 OPJ_BYTE * p_header_data,
767 OPJ_UINT32 p_header_size,
768 struct opj_event_mgr * p_manager);
772 * Copy the image header from the jpeg2000 codec into an external image_header
774 * @param p_j2k the jpeg2000 codec.
775 * @param p_image_header the external empty image header
777 * @return true if the image header is correctly copy.
779 static opj_bool j2k_copy_img_header(opj_j2k_v2_t* p_j2k, opj_image_t** p_image);
782 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
784 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
786 static opj_codestream_index_t* j2k_create_cstr_index(void);
792 /* ----------------------------------------------------------------------- */
793 typedef struct j2k_prog_order{
794 OPJ_PROG_ORDER enum_prog;
798 j2k_prog_order_t j2k_prog_order_list[] = {
804 {(OPJ_PROG_ORDER)-1, ""}
810 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
818 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
820 const j2k_mct_function j2k_mct_read_functions_to_float [] =
822 j2k_read_int16_to_float,
823 j2k_read_int32_to_float,
824 j2k_read_float32_to_float,
825 j2k_read_float64_to_float
828 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
830 j2k_read_int16_to_int32,
831 j2k_read_int32_to_int32,
832 j2k_read_float32_to_int32,
833 j2k_read_float64_to_int32
836 const j2k_mct_function j2k_mct_write_functions_from_float [] =
838 j2k_write_float_to_int16,
839 j2k_write_float_to_int32,
840 j2k_write_float_to_float,
841 j2k_write_float_to_float64
844 typedef struct opj_dec_memory_marker_handler
848 /** value of the state when the marker can appear */
850 /** action linked to the marker */
851 opj_bool (*handler) (
853 OPJ_BYTE * p_header_data,
854 OPJ_UINT32 p_header_size,
855 struct opj_event_mgr * p_manager
858 opj_dec_memory_marker_handler_t;
860 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
863 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
864 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
865 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
866 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
867 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
868 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
869 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
870 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
871 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
872 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
873 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
874 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
875 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
877 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
878 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
879 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
880 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
881 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
882 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
884 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
885 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
886 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
887 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
888 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
889 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
890 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
891 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
892 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
893 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
894 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
895 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
896 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
898 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
899 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
900 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
901 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
902 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
903 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
905 #ifdef TODO_MS /* FIXME */
906 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
907 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
908 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
909 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
911 #endif /* USE_JPWL */
913 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
914 {J2K_MS_INSEC, 0, j2k_read_insec}
915 #endif /* USE_JPSEC */
916 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}//j2k_read_unk_v2}
921 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
923 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
924 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
928 for (i=0;i<p_nb_elem;++i) {
929 opj_read_bytes(l_src_data,&l_temp,2);
931 l_src_data+=sizeof(OPJ_INT16);
933 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
937 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
939 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
940 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
944 for (i=0;i<p_nb_elem;++i) {
945 opj_read_bytes(l_src_data,&l_temp,4);
947 l_src_data+=sizeof(OPJ_INT32);
949 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
953 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
955 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
956 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
960 for (i=0;i<p_nb_elem;++i) {
961 opj_read_float(l_src_data,&l_temp);
963 l_src_data+=sizeof(OPJ_FLOAT32);
965 *(l_dest_data++) = l_temp;
969 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
971 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
972 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
976 for (i=0;i<p_nb_elem;++i) {
977 opj_read_double(l_src_data,&l_temp);
979 l_src_data+=sizeof(OPJ_FLOAT64);
981 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
985 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
987 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
988 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
992 for (i=0;i<p_nb_elem;++i) {
993 opj_read_bytes(l_src_data,&l_temp,2);
995 l_src_data+=sizeof(OPJ_INT16);
997 *(l_dest_data++) = (OPJ_INT32) l_temp;
1001 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1003 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1004 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1008 for (i=0;i<p_nb_elem;++i) {
1009 opj_read_bytes(l_src_data,&l_temp,4);
1011 l_src_data+=sizeof(OPJ_INT32);
1013 *(l_dest_data++) = (OPJ_INT32) l_temp;
1017 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1019 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1020 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1024 for (i=0;i<p_nb_elem;++i) {
1025 opj_read_float(l_src_data,&l_temp);
1027 l_src_data+=sizeof(OPJ_FLOAT32);
1029 *(l_dest_data++) = (OPJ_INT32) l_temp;
1033 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1035 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1036 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1040 for (i=0;i<p_nb_elem;++i) {
1041 opj_read_double(l_src_data,&l_temp);
1043 l_src_data+=sizeof(OPJ_FLOAT64);
1045 *(l_dest_data++) = (OPJ_INT32) l_temp;
1049 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1051 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1052 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1056 for (i=0;i<p_nb_elem;++i) {
1057 l_temp = (OPJ_UINT32) *(l_src_data++);
1059 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1061 l_dest_data+=sizeof(OPJ_INT16);
1065 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1067 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1068 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1072 for (i=0;i<p_nb_elem;++i) {
1073 l_temp = (OPJ_UINT32) *(l_src_data++);
1075 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1077 l_dest_data+=sizeof(OPJ_INT32);
1081 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1083 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1084 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1088 for (i=0;i<p_nb_elem;++i) {
1089 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1091 opj_write_float(l_dest_data,l_temp);
1093 l_dest_data+=sizeof(OPJ_FLOAT32);
1097 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1099 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1100 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1104 for (i=0;i<p_nb_elem;++i) {
1105 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1107 opj_write_double(l_dest_data,l_temp);
1109 l_dest_data+=sizeof(OPJ_FLOAT64);
1115 * Converts an enum type progression order to string type.
1117 * @param prg_order the progression order to get.
1119 * @return the string representation of the given progression order.
1121 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1122 j2k_prog_order_t *po;
1123 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1124 if(po->enum_prog == prg_order){
1125 return po->str_prog;
1128 return po->str_prog;
1131 /* ----------------------------------------------------------------------- */
1132 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1135 int tpnum=1,tpend=0;
1136 opj_tcp_t *tcp = &cp->tcps[tileno];
1137 prog = j2k_convert_progression_order(tcp->prg);
1142 if( cp->tp_flag == prog[i] ){
1143 tpend=1;cp->tp_pos=i;
1147 tpnum= tpnum * tcp->pocs[pino].compE;
1150 tpnum= tpnum * tcp->pocs[pino].resE;
1153 tpnum= tpnum * tcp->pocs[pino].prcE;
1156 tpnum= tpnum * tcp->pocs[pino].layE;
1167 /** mem allocation for TLM marker*/
1168 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1169 int pino,tileno,totnum_tp=0;
1171 OPJ_ARG_NOT_USED(img_numcomp);
1173 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1174 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1175 int cur_totnum_tp = 0;
1176 opj_tcp_t *tcp = &cp->tcps[tileno];
1177 for(pino = 0; pino <= tcp->numpocs; pino++) {
1179 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1180 if(!pi) { return -1;}
1181 tp_num = j2k_get_num_tp(cp,pino,tileno);
1182 totnum_tp = totnum_tp + tp_num;
1183 cur_totnum_tp = cur_totnum_tp + tp_num;
1184 pi_destroy(pi, cp, tileno);
1186 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1188 if (j2k->cstr_info) {
1189 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1190 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1197 static void j2k_write_soc(opj_j2k_t *j2k) {
1198 opj_cio_t *cio = j2k->cio;
1199 cio_write(cio, J2K_MS_SOC, 2);
1202 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1207 /* update markers struct */
1208 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1209 #endif /* USE_JPWL */
1213 static void j2k_read_soc(opj_j2k_t *j2k) {
1214 j2k->state = J2K_STATE_MHSIZ;
1216 if (j2k->cstr_info) {
1217 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
1218 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
1223 * Reads a SOC marker (Start of Codestream)
1224 * @param p_header_data the data contained in the SOC box.
1225 * @param jp2 the jpeg2000 file codec.
1226 * @param p_header_size the size of the data contained in the SOC marker.
1227 * @param p_manager the user event manager.
1229 static opj_bool j2k_read_soc_v2( opj_j2k_v2_t *p_j2k,
1230 struct opj_stream_private *p_stream,
1231 struct opj_event_mgr * p_manager )
1233 OPJ_BYTE l_data [2];
1234 OPJ_UINT32 l_marker;
1237 assert(p_j2k != 00);
1238 assert(p_manager != 00);
1239 assert(p_stream != 00);
1241 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1245 opj_read_bytes(l_data,&l_marker,2);
1246 if (l_marker != J2K_MS_SOC) {
1250 /* Next marker should be a SIZ marker in the main header */
1251 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1253 /* FIXME move it in a index structure included in p_j2k*/
1254 p_j2k->cstr_index->main_head_start = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
1256 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1258 /* Add the marker to the codestream index*/
1259 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1264 static void j2k_write_siz(opj_j2k_t *j2k) {
1268 opj_cio_t *cio = j2k->cio;
1269 opj_image_t *image = j2k->image;
1270 opj_cp_t *cp = j2k->cp;
1272 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
1273 lenp = cio_tell(cio);
1275 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
1276 cio_write(cio, image->x1, 4); /* Xsiz */
1277 cio_write(cio, image->y1, 4); /* Ysiz */
1278 cio_write(cio, image->x0, 4); /* X0siz */
1279 cio_write(cio, image->y0, 4); /* Y0siz */
1280 cio_write(cio, cp->tdx, 4); /* XTsiz */
1281 cio_write(cio, cp->tdy, 4); /* YTsiz */
1282 cio_write(cio, cp->tx0, 4); /* XT0siz */
1283 cio_write(cio, cp->ty0, 4); /* YT0siz */
1284 cio_write(cio, image->numcomps, 2); /* Csiz */
1285 for (i = 0; i < image->numcomps; i++) {
1286 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
1287 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
1288 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
1290 len = cio_tell(cio) - lenp;
1291 cio_seek(cio, lenp);
1292 cio_write(cio, len, 2); /* Lsiz */
1293 cio_seek(cio, lenp + len);
1296 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
1299 static void j2k_read_siz(opj_j2k_t *j2k) {
1302 opj_cio_t *cio = j2k->cio;
1303 opj_image_t *image = j2k->image;
1304 opj_cp_t *cp = j2k->cp;
1306 len = cio_read(cio, 2); /* Lsiz */
1307 cio_read(cio, 2); /* Rsiz (capabilities) */
1308 image->x1 = cio_read(cio, 4); /* Xsiz */
1309 image->y1 = cio_read(cio, 4); /* Ysiz */
1310 image->x0 = cio_read(cio, 4); /* X0siz */
1311 image->y0 = cio_read(cio, 4); /* Y0siz */
1312 cp->tdx = cio_read(cio, 4); /* XTsiz */
1313 cp->tdy = cio_read(cio, 4); /* YTsiz */
1314 cp->tx0 = cio_read(cio, 4); /* XT0siz */
1315 cp->ty0 = cio_read(cio, 4); /* YT0siz */
1317 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
1318 opj_event_msg(j2k->cinfo, EVT_ERROR,
1319 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
1320 image->x0,image->x1,image->y0,image->y1);
1324 image->numcomps = cio_read(cio, 2); /* Csiz */
1327 if (j2k->cp->correct) {
1328 /* if JPWL is on, we check whether TX errors have damaged
1329 too much the SIZ parameters */
1330 if (!(image->x1 * image->y1)) {
1331 opj_event_msg(j2k->cinfo, EVT_ERROR,
1332 "JPWL: bad image size (%d x %d)\n",
1333 image->x1, image->y1);
1334 if (!JPWL_ASSUME || JPWL_ASSUME) {
1335 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1339 if (image->numcomps != ((len - 38) / 3)) {
1340 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1341 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1342 image->numcomps, ((len - 38) / 3));
1344 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1347 /* we try to correct */
1348 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
1349 if (image->numcomps < ((len - 38) / 3)) {
1350 len = 38 + 3 * image->numcomps;
1351 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1354 image->numcomps = ((len - 38) / 3);
1355 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1360 /* update components number in the jpwl_exp_comps filed */
1361 cp->exp_comps = image->numcomps;
1363 #endif /* USE_JPWL */
1365 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
1366 for (i = 0; i < image->numcomps; i++) {
1368 tmp = cio_read(cio, 1); /* Ssiz_i */
1369 image->comps[i].prec = (tmp & 0x7f) + 1;
1370 image->comps[i].sgnd = tmp >> 7;
1371 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
1372 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
1375 if (j2k->cp->correct) {
1376 /* if JPWL is on, we check whether TX errors have damaged
1377 too much the SIZ parameters, again */
1378 if (!(image->comps[i].dx * image->comps[i].dy)) {
1379 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1380 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1381 i, i, image->comps[i].dx, image->comps[i].dy);
1383 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1386 /* we try to correct */
1387 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1388 if (!image->comps[i].dx) {
1389 image->comps[i].dx = 1;
1390 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1391 i, image->comps[i].dx);
1393 if (!image->comps[i].dy) {
1394 image->comps[i].dy = 1;
1395 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1396 i, image->comps[i].dy);
1401 #endif /* USE_JPWL */
1403 /* TODO: unused ? */
1404 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
1405 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
1407 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
1408 image->comps[i].factor = cp->reduce; /* reducing factor per component */
1411 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
1412 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
1415 if (j2k->cp->correct) {
1416 /* if JPWL is on, we check whether TX errors have damaged
1417 too much the SIZ parameters */
1418 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
1419 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1420 "JPWL: bad number of tiles (%d x %d)\n",
1423 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1426 /* we try to correct */
1427 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1430 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1433 if (cp->tw > cp->max_tiles) {
1435 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
1436 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1437 cp->max_tiles, cp->tw);
1441 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1444 if (cp->th > cp->max_tiles) {
1446 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1447 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1448 cp->max_tiles, cp->th);
1452 #endif /* USE_JPWL */
1454 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
1455 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
1456 cp->tileno_size = 0;
1459 if (j2k->cp->correct) {
1461 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1462 "JPWL: could not alloc tcps field of cp\n");
1463 if (!JPWL_ASSUME || JPWL_ASSUME) {
1464 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1469 #endif /* USE_JPWL */
1471 for (i = 0; i < cp->tw * cp->th; i++) {
1472 cp->tcps[i].POC = 0;
1473 cp->tcps[i].numpocs = 0;
1474 cp->tcps[i].first = 1;
1477 /* Initialization for PPM marker */
1479 cp->ppm_data = NULL;
1480 cp->ppm_data_first = NULL;
1481 cp->ppm_previous = 0;
1484 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
1485 for (i = 0; i < cp->tw * cp->th; i++) {
1486 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
1488 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
1489 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
1490 j2k->state = J2K_STATE_MH;
1493 if (j2k->cstr_info) {
1494 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1495 cstr_info->image_w = image->x1 - image->x0;
1496 cstr_info->image_h = image->y1 - image->y0;
1497 cstr_info->numcomps = image->numcomps;
1498 cstr_info->tw = cp->tw;
1499 cstr_info->th = cp->th;
1500 cstr_info->tile_x = cp->tdx;
1501 cstr_info->tile_y = cp->tdy;
1502 cstr_info->tile_Ox = cp->tx0;
1503 cstr_info->tile_Oy = cp->ty0;
1504 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
1510 * Reads a SIZ marker (image and tile size)
1511 * @param p_header_data the data contained in the SIZ box.
1512 * @param jp2 the jpeg2000 file codec.
1513 * @param p_header_size the size of the data contained in the SIZ marker.
1514 * @param p_manager the user event manager.
1516 opj_bool j2k_read_siz_v2 (
1517 opj_j2k_v2_t *p_j2k,
1518 OPJ_BYTE * p_header_data,
1519 OPJ_UINT32 p_header_size,
1520 struct opj_event_mgr * p_manager
1523 OPJ_UINT32 l_size, i;
1524 OPJ_UINT32 l_nb_comp;
1525 OPJ_UINT32 l_nb_comp_remain;
1526 OPJ_UINT32 l_remaining_size;
1527 OPJ_UINT32 l_nb_tiles;
1529 opj_image_t *l_image = 00;
1530 opj_cp_v2_t *l_cp = 00;
1531 opj_image_comp_t * l_img_comp = 00;
1532 opj_tcp_v2_t * l_current_tile_param = 00;
1535 assert(p_j2k != 00);
1536 assert(p_manager != 00);
1537 assert(p_header_data != 00);
1539 l_image = p_j2k->m_image;
1540 l_cp = &(p_j2k->m_cp);
1542 // minimum size == 39 - 3 (= minimum component parameter)
1543 if (p_header_size < 36) {
1544 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1548 l_remaining_size = p_header_size - 36;
1549 l_nb_comp = l_remaining_size / 3;
1550 l_nb_comp_remain = l_remaining_size % 3;
1551 if (l_nb_comp_remain != 0){
1552 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1556 l_size = p_header_size + 2; /* Lsiz */
1558 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
1560 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1561 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
1563 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
1565 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
1567 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
1569 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
1571 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
1573 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
1575 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
1577 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
1580 l_image->numcomps = (OPJ_UINT16) l_tmp;
1582 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
1586 if (l_image->numcomps != l_nb_comp) {
1587 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);
1592 if (l_cp->correct) {
1593 /* if JPWL is on, we check whether TX errors have damaged
1594 too much the SIZ parameters */
1595 if (!(l_image->x1 * l_image->y1)) {
1596 opj_event_msg_v2(p_manager, EVT_ERROR,
1597 "JPWL: bad image size (%d x %d)\n",
1598 l_image->x1, l_image->y1);
1599 if (!JPWL_ASSUME || JPWL_ASSUME) {
1600 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1605 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
1606 if (l_image->numcomps != ((len - 38) / 3)) {
1607 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1608 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1609 l_image->numcomps, ((len - 38) / 3));
1611 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1614 */ /* we try to correct */
1615 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
1616 if (l_image->numcomps < ((len - 38) / 3)) {
1617 len = 38 + 3 * l_image->numcomps;
1618 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1621 l_image->numcomps = ((len - 38) / 3);
1622 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1628 /* update components number in the jpwl_exp_comps filed */
1629 l_cp->exp_comps = l_image->numcomps;
1631 #endif /* USE_JPWL */
1633 // Allocate the resulting image components
1634 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
1635 if (l_image->comps == 00){
1636 l_image->numcomps = 0;
1637 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1641 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
1642 l_img_comp = l_image->comps;
1644 // Read the component information
1645 for (i = 0; i < l_image->numcomps; ++i){
1647 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
1649 l_img_comp->prec = (tmp & 0x7f) + 1;
1650 l_img_comp->sgnd = tmp >> 7;
1651 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
1653 l_img_comp->dx = (OPJ_INT32)tmp; // should be between 1 and 255
1654 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
1656 l_img_comp->dy = (OPJ_INT32)tmp; // should be between 1 and 255
1659 if (l_cp->correct) {
1660 /* if JPWL is on, we check whether TX errors have damaged
1661 too much the SIZ parameters, again */
1662 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
1663 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1664 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1665 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
1667 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1670 /* we try to correct */
1671 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1672 if (!l_image->comps[i].dx) {
1673 l_image->comps[i].dx = 1;
1674 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1675 i, l_image->comps[i].dx);
1677 if (!l_image->comps[i].dy) {
1678 l_image->comps[i].dy = 1;
1679 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1680 i, l_image->comps[i].dy);
1684 #endif /* USE_JPWL */
1685 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
1686 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
1690 // Compute the number of tiles
1691 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
1692 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
1693 l_nb_tiles = l_cp->tw * l_cp->th;
1695 // Define the tiles which will be decoded
1696 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
1697 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;
1698 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;
1699 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);
1700 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);
1703 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
1704 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
1705 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
1706 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
1710 if (l_cp->correct) {
1711 /* if JPWL is on, we check whether TX errors have damaged
1712 too much the SIZ parameters */
1713 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
1714 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1715 "JPWL: bad number of tiles (%d x %d)\n",
1716 l_cp->tw, l_cp->th);
1718 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1721 /* we try to correct */
1722 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1725 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1728 if (l_cp->tw > l_cp->max_tiles) {
1730 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
1731 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1732 l_cp->max_tiles, l_cp->tw);
1736 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1739 if (l_cp->th > l_cp->max_tiles) {
1741 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1742 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1743 l_cp->max_tiles, l_cp->th);
1747 #endif /* USE_JPWL */
1749 /* memory allocations */
1750 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
1751 if (l_cp->tcps == 00) {
1752 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1755 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
1758 if (l_cp->correct) {
1760 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1761 "JPWL: could not alloc tcps field of cp\n");
1762 if (!JPWL_ASSUME || JPWL_ASSUME) {
1763 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1768 #endif /* USE_JPWL */
1770 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
1771 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
1772 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
1773 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1776 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
1778 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
1779 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
1781 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
1782 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1785 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));
1786 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
1788 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
1789 (opj_simple_mcc_decorrelation_data_t*)
1790 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
1792 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
1793 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1796 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));
1797 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
1799 /* set up default dc level shift */
1800 for (i=0;i<l_image->numcomps;++i) {
1801 if (! l_image->comps[i].sgnd) {
1802 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
1806 l_current_tile_param = l_cp->tcps;
1807 for (i = 0; i < l_nb_tiles; ++i) {
1808 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
1809 if (l_current_tile_param->tccps == 00) {
1810 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1813 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
1815 ++l_current_tile_param;
1818 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; // FIXME J2K_DEC_STATE_MH;
1819 opj_image_comp_header_update(l_image,l_cp);
1826 static void j2k_write_com(opj_j2k_t *j2k) {
1830 if(j2k->cp->comment) {
1831 opj_cio_t *cio = j2k->cio;
1832 char *comment = j2k->cp->comment;
1834 cio_write(cio, J2K_MS_COM, 2);
1835 lenp = cio_tell(cio);
1837 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
1838 for (i = 0; i < strlen(comment); i++) {
1839 cio_write(cio, comment[i], 1);
1841 len = cio_tell(cio) - lenp;
1842 cio_seek(cio, lenp);
1843 cio_write(cio, len, 2);
1844 cio_seek(cio, lenp + len);
1848 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
1853 static void j2k_read_com(opj_j2k_t *j2k) {
1856 opj_cio_t *cio = j2k->cio;
1858 len = cio_read(cio, 2);
1859 cio_skip(cio, len - 2);
1862 * Reads a COM marker (comments)
1863 * @param p_header_data the data contained in the COM box.
1864 * @param jp2 the jpeg2000 file codec.
1865 * @param p_header_size the size of the data contained in the COM marker.
1866 * @param p_manager the user event manager.
1868 opj_bool j2k_read_com_v2 (
1869 opj_j2k_v2_t *p_j2k,
1870 OPJ_BYTE * p_header_data,
1871 OPJ_UINT32 p_header_size,
1872 struct opj_event_mgr * p_manager
1876 assert(p_j2k != 00);
1877 assert(p_manager != 00);
1878 assert(p_header_data != 00);
1883 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
1886 opj_cp_t *cp = j2k->cp;
1887 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1888 opj_tccp_t *tccp = &tcp->tccps[compno];
1889 opj_cio_t *cio = j2k->cio;
1891 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
1892 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
1893 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
1894 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
1895 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
1897 if (tccp->csty & J2K_CCP_CSTY_PRT) {
1898 for (i = 0; i < tccp->numresolutions; i++) {
1899 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
1904 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
1907 opj_cp_t *cp = j2k->cp;
1908 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1909 opj_tccp_t *tccp = &tcp->tccps[compno];
1910 opj_cio_t *cio = j2k->cio;
1912 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
1914 // If user wants to remove more resolutions than the codestream contains, return error
1915 if (cp->reduce >= tccp->numresolutions) {
1916 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
1917 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
1918 j2k->state |= J2K_STATE_ERR;
1921 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
1922 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
1923 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
1924 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
1925 if (tccp->csty & J2K_CP_CSTY_PRT) {
1926 for (i = 0; i < tccp->numresolutions; i++) {
1927 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
1928 tccp->prcw[i] = tmp & 0xf;
1929 tccp->prch[i] = tmp >> 4;
1934 if(j2k->cstr_info && compno == 0) {
1935 for (i = 0; i < tccp->numresolutions; i++) {
1936 if (tccp->csty & J2K_CP_CSTY_PRT) {
1937 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
1938 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
1941 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1942 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1949 static void j2k_write_cod(opj_j2k_t *j2k) {
1950 opj_cp_t *cp = NULL;
1951 opj_tcp_t *tcp = NULL;
1954 opj_cio_t *cio = j2k->cio;
1956 cio_write(cio, J2K_MS_COD, 2); /* COD */
1958 lenp = cio_tell(cio);
1962 tcp = &cp->tcps[j2k->curtileno];
1964 cio_write(cio, tcp->csty, 1); /* Scod */
1965 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
1966 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
1967 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
1969 j2k_write_cox(j2k, 0);
1970 len = cio_tell(cio) - lenp;
1971 cio_seek(cio, lenp);
1972 cio_write(cio, len, 2); /* Lcod */
1973 cio_seek(cio, lenp + len);
1976 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
1980 static void j2k_read_cod(opj_j2k_t *j2k) {
1983 opj_cio_t *cio = j2k->cio;
1984 opj_cp_t *cp = j2k->cp;
1985 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1986 opj_image_t *image = j2k->image;
1988 len = cio_read(cio, 2); /* Lcod */
1989 tcp->csty = cio_read(cio, 1); /* Scod */
1990 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
1991 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
1992 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
1994 pos = cio_tell(cio);
1995 for (i = 0; i < image->numcomps; i++) {
1996 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
1998 j2k_read_cox(j2k, i);
2002 if (j2k->cstr_info) {
2003 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2004 cstr_info->prog = tcp->prg;
2005 cstr_info->numlayers = tcp->numlayers;
2006 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
2007 for (i = 0; i < image->numcomps; i++) {
2008 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
2014 * Reads a COD marker (Coding Styke defaults)
2015 * @param p_header_data the data contained in the COD box.
2016 * @param p_j2k the jpeg2000 codec.
2017 * @param p_header_size the size of the data contained in the COD marker.
2018 * @param p_manager the user event manager.
2020 opj_bool j2k_read_cod_v2 (
2021 opj_j2k_v2_t *p_j2k,
2022 OPJ_BYTE * p_header_data,
2023 OPJ_UINT32 p_header_size,
2024 struct opj_event_mgr * p_manager
2030 opj_cp_v2_t *l_cp = 00;
2031 opj_tcp_v2_t *l_tcp = 00;
2032 opj_image_t *l_image = 00;
2035 assert(p_header_data != 00);
2036 assert(p_j2k != 00);
2037 assert(p_manager != 00);
2039 l_image = p_j2k->m_image;
2040 l_cp = &(p_j2k->m_cp);
2042 /* If we are in the first tile-part header of the current tile */
2043 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2044 &l_cp->tcps[p_j2k->m_current_tile_number] :
2045 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2047 /* Make sure room is sufficient */
2048 if (p_header_size < 5) {
2049 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2053 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2055 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2057 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2058 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2061 // If user didn't set a number layer to decode take the max specify in the codestream.
2062 if (l_cp->m_specific_param.m_dec.m_layer) {
2063 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2066 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2069 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2073 for (i = 0; i < l_image->numcomps; ++i) {
2074 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2077 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2078 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2082 if (p_header_size != 0) {
2083 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2087 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2088 j2k_copy_tile_component_parameters(p_j2k);
2091 #ifdef WIP_REMOVE_MSD
2092 if (p_j2k->cstr_info) {
2093 //opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;
2094 p_j2k->cstr_info->prog = l_tcp->prg;
2095 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2096 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2097 for (i = 0; i < l_image->numcomps; ++i) {
2098 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2106 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2109 opj_cp_t *cp = j2k->cp;
2110 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2111 opj_image_t *image = j2k->image;
2112 opj_cio_t *cio = j2k->cio;
2114 cio_write(cio, J2K_MS_COC, 2); /* COC */
2115 lenp = cio_tell(cio);
2117 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2118 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2119 j2k_write_cox(j2k, compno);
2120 len = cio_tell(cio) - lenp;
2121 cio_seek(cio, lenp);
2122 cio_write(cio, len, 2); /* Lcoc */
2123 cio_seek(cio, lenp + len);
2126 static void j2k_read_coc(opj_j2k_t *j2k) {
2129 opj_cp_t *cp = j2k->cp;
2130 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2131 opj_image_t *image = j2k->image;
2132 opj_cio_t *cio = j2k->cio;
2134 len = cio_read(cio, 2); /* Lcoc */
2135 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2136 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
2137 j2k_read_cox(j2k, compno);
2141 * Reads a COC marker (Coding Style Component)
2142 * @param p_header_data the data contained in the COC box.
2143 * @param p_j2k the jpeg2000 codec.
2144 * @param p_header_size the size of the data contained in the COC marker.
2145 * @param p_manager the user event manager.
2147 opj_bool j2k_read_coc_v2 (
2148 opj_j2k_v2_t *p_j2k,
2149 OPJ_BYTE * p_header_data,
2150 OPJ_UINT32 p_header_size,
2151 struct opj_event_mgr * p_manager
2154 opj_cp_v2_t *l_cp = NULL;
2155 opj_tcp_v2_t *l_tcp = NULL;
2156 opj_image_t *l_image = NULL;
2157 OPJ_UINT32 l_comp_room;
2158 OPJ_UINT32 l_comp_no;
2161 assert(p_header_data != 00);
2162 assert(p_j2k != 00);
2163 assert(p_manager != 00);
2165 l_cp = &(p_j2k->m_cp);
2166 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2167 &l_cp->tcps[p_j2k->m_current_tile_number] :
2168 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2169 l_image = p_j2k->m_image;
2171 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2173 // make sure room is sufficient
2174 if (p_header_size < l_comp_room + 1) {
2175 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2178 p_header_size -= l_comp_room + 1;
2180 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2181 p_header_data += l_comp_room;
2182 if (l_comp_no >= l_image->numcomps) {
2183 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2187 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2190 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2191 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2195 if (p_header_size != 0) {
2196 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2202 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
2203 int bandno, numbands;
2206 opj_cp_t *cp = j2k->cp;
2207 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2208 opj_tccp_t *tccp = &tcp->tccps[compno];
2209 opj_cio_t *cio = j2k->cio;
2211 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
2212 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
2214 for (bandno = 0; bandno < numbands; bandno++) {
2215 expn = tccp->stepsizes[bandno].expn;
2216 mant = tccp->stepsizes[bandno].mant;
2218 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
2219 cio_write(cio, expn << 3, 1); /* SPqcx_i */
2221 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
2226 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
2228 int bandno, numbands;
2230 opj_cp_t *cp = j2k->cp;
2231 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2232 opj_tccp_t *tccp = &tcp->tccps[compno];
2233 opj_cio_t *cio = j2k->cio;
2235 tmp = cio_read(cio, 1); /* Sqcx */
2236 tccp->qntsty = tmp & 0x1f;
2237 tccp->numgbits = tmp >> 5;
2238 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
2239 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
2242 if (j2k->cp->correct) {
2244 /* if JPWL is on, we check whether there are too many subbands */
2245 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
2246 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2247 "JPWL: bad number of subbands in Sqcx (%d)\n",
2250 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2253 /* we try to correct */
2255 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
2256 "- setting number of bands to %d => HYPOTHESIS!!!\n",
2263 /* We check whether there are too many subbands */
2264 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
2265 opj_event_msg(j2k->cinfo, EVT_WARNING ,
2266 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
2267 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
2270 #endif /* USE_JPWL */
2272 for (bandno = 0; bandno < numbands; bandno++) {
2274 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
2275 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
2278 tmp = cio_read(cio, 2); /* SPqcx_i */
2282 if (bandno < J2K_MAXBANDS){
2283 tccp->stepsizes[bandno].expn = expn;
2284 tccp->stepsizes[bandno].mant = mant;
2288 /* Add Antonin : if scalar_derived -> compute other stepsizes */
2289 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
2290 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
2291 tccp->stepsizes[bandno].expn =
2292 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
2293 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
2294 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
2300 static void j2k_write_qcd(opj_j2k_t *j2k) {
2303 opj_cio_t *cio = j2k->cio;
2305 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
2306 lenp = cio_tell(cio);
2308 j2k_write_qcx(j2k, 0);
2309 len = cio_tell(cio) - lenp;
2310 cio_seek(cio, lenp);
2311 cio_write(cio, len, 2); /* Lqcd */
2312 cio_seek(cio, lenp + len);
2315 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
2318 static void j2k_read_qcd(opj_j2k_t *j2k) {
2321 opj_cio_t *cio = j2k->cio;
2322 opj_image_t *image = j2k->image;
2324 len = cio_read(cio, 2); /* Lqcd */
2325 pos = cio_tell(cio);
2326 for (i = 0; i < image->numcomps; i++) {
2328 j2k_read_qcx(j2k, i, len - 2);
2333 * Reads a QCD marker (Quantization defaults)
2334 * @param p_header_data the data contained in the QCD box.
2335 * @param p_j2k the jpeg2000 codec.
2336 * @param p_header_size the size of the data contained in the QCD marker.
2337 * @param p_manager the user event manager.
2339 opj_bool j2k_read_qcd_v2 (
2340 opj_j2k_v2_t *p_j2k,
2341 OPJ_BYTE * p_header_data,
2342 OPJ_UINT32 p_header_size,
2343 struct opj_event_mgr * p_manager
2347 assert(p_header_data != 00);
2348 assert(p_j2k != 00);
2349 assert(p_manager != 00);
2351 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2352 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2356 if (p_header_size != 0) {
2357 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2361 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2362 j2k_copy_tile_quantization_parameters(p_j2k);
2367 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
2370 opj_cio_t *cio = j2k->cio;
2372 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
2373 lenp = cio_tell(cio);
2375 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
2376 j2k_write_qcx(j2k, compno);
2377 len = cio_tell(cio) - lenp;
2378 cio_seek(cio, lenp);
2379 cio_write(cio, len, 2); /* Lqcc */
2380 cio_seek(cio, lenp + len);
2383 static void j2k_read_qcc(opj_j2k_t *j2k) {
2385 int numcomp = j2k->image->numcomps;
2386 opj_cio_t *cio = j2k->cio;
2388 len = cio_read(cio, 2); /* Lqcc */
2389 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
2392 if (j2k->cp->correct) {
2394 static int backup_compno = 0;
2396 /* compno is negative or larger than the number of components!!! */
2397 if ((compno < 0) || (compno >= numcomp)) {
2398 opj_event_msg(j2k->cinfo, EVT_ERROR,
2399 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2402 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2405 /* we try to correct */
2406 compno = backup_compno % numcomp;
2407 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
2408 "- setting component number to %d\n",
2412 /* keep your private count of tiles */
2415 #endif /* USE_JPWL */
2417 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
2421 * Reads a QCC marker (Quantization component)
2422 * @param p_header_data the data contained in the QCC box.
2423 * @param p_j2k the jpeg2000 codec.
2424 * @param p_header_size the size of the data contained in the QCC marker.
2425 * @param p_manager the user event manager.
2427 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
2428 OPJ_BYTE * p_header_data,
2429 OPJ_UINT32 p_header_size,
2430 struct opj_event_mgr * p_manager)
2432 OPJ_UINT32 l_num_comp,l_comp_no;
2435 assert(p_header_data != 00);
2436 assert(p_j2k != 00);
2437 assert(p_manager != 00);
2439 l_num_comp = p_j2k->m_image->numcomps;
2441 if (l_num_comp <= 256) {
2442 if (p_header_size < 1) {
2443 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2446 opj_read_bytes(p_header_data,&l_comp_no,1);
2451 if (p_header_size < 2) {
2452 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2455 opj_read_bytes(p_header_data,&l_comp_no,2);
2461 if (p_j2k->m_cp.correct) {
2463 static OPJ_UINT32 backup_compno = 0;
2465 /* compno is negative or larger than the number of components!!! */
2466 if ((l_comp_no < 0) || (l_comp_no >= l_num_comp)) {
2467 opj_event_msg_v2(p_manager, EVT_ERROR,
2468 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2469 l_comp_no, l_num_comp);
2471 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2474 /* we try to correct */
2475 l_comp_no = backup_compno % l_num_comp;
2476 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
2477 "- setting component number to %d\n",
2481 /* keep your private count of tiles */
2484 #endif /* USE_JPWL */
2486 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2487 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2491 if (p_header_size != 0) {
2492 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2500 static void j2k_write_poc(opj_j2k_t *j2k) {
2501 int len, numpchgs, i;
2503 int numcomps = j2k->image->numcomps;
2505 opj_cp_t *cp = j2k->cp;
2506 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2507 opj_tccp_t *tccp = &tcp->tccps[0];
2508 opj_cio_t *cio = j2k->cio;
2510 numpchgs = 1 + tcp->numpocs;
2511 cio_write(cio, J2K_MS_POC, 2); /* POC */
2512 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
2513 cio_write(cio, len, 2); /* Lpoc */
2514 for (i = 0; i < numpchgs; i++) {
2515 opj_poc_t *poc = &tcp->pocs[i];
2516 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
2517 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
2518 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
2519 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
2520 cio_write(cio, poc->resno1, 1); /* REpoc_i */
2521 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
2522 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
2523 poc->compno1 = int_min(poc->compno1, numcomps);
2524 cio_write(cio, poc->prg, 1); /* Ppoc_i */
2528 static void j2k_read_poc(opj_j2k_t *j2k) {
2529 int len, numpchgs, i, old_poc;
2531 int numcomps = j2k->image->numcomps;
2533 opj_cp_t *cp = j2k->cp;
2534 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2535 opj_cio_t *cio = j2k->cio;
2537 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
2539 len = cio_read(cio, 2); /* Lpoc */
2540 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
2542 for (i = old_poc; i < numpchgs + old_poc; i++) {
2544 poc = &tcp->pocs[i];
2545 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
2546 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
2547 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
2548 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
2549 poc->compno1 = int_min(
2550 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
2551 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
2554 tcp->numpocs = numpchgs + old_poc - 1;
2558 * Reads a POC marker (Progression Order Change)
2560 * @param p_header_data the data contained in the POC box.
2561 * @param p_j2k the jpeg2000 codec.
2562 * @param p_header_size the size of the data contained in the POC marker.
2563 * @param p_manager the user event manager.
2565 opj_bool j2k_read_poc_v2 (
2566 opj_j2k_v2_t *p_j2k,
2567 OPJ_BYTE * p_header_data,
2568 OPJ_UINT32 p_header_size,
2569 struct opj_event_mgr * p_manager)
2571 OPJ_UINT32 i, l_nb_comp, l_tmp;
2572 opj_image_t * l_image = 00;
2573 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
2574 OPJ_UINT32 l_chunk_size, l_comp_room;
2576 opj_cp_v2_t *l_cp = 00;
2577 opj_tcp_v2_t *l_tcp = 00;
2578 opj_poc_t *l_current_poc = 00;
2581 assert(p_header_data != 00);
2582 assert(p_j2k != 00);
2583 assert(p_manager != 00);
2585 l_image = p_j2k->m_image;
2586 l_nb_comp = l_image->numcomps;
2587 if (l_nb_comp <= 256) {
2593 l_chunk_size = 5 + 2 * l_comp_room;
2594 l_current_poc_nb = p_header_size / l_chunk_size;
2595 l_current_poc_remaining = p_header_size % l_chunk_size;
2597 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
2598 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
2602 l_cp = &(p_j2k->m_cp);
2603 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2604 &l_cp->tcps[p_j2k->m_current_tile_number] :
2605 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2606 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
2607 l_current_poc_nb += l_old_poc_nb;
2609 assert(l_current_poc_nb < 32);
2611 /* now poc is in use.*/
2614 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
2615 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
2616 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
2618 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
2619 p_header_data+=l_comp_room;
2620 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
2622 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
2624 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
2625 p_header_data+=l_comp_room;
2626 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
2628 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
2629 /* make sure comp is in acceptable bounds */
2630 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
2634 l_tcp->numpocs = l_current_poc_nb - 1;
2638 static void j2k_read_crg(opj_j2k_t *j2k) {
2639 int len, i, Xcrg_i, Ycrg_i;
2641 opj_cio_t *cio = j2k->cio;
2642 int numcomps = j2k->image->numcomps;
2644 len = cio_read(cio, 2); /* Lcrg */
2645 for (i = 0; i < numcomps; i++) {
2646 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
2647 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
2652 * Reads a CRG marker (Component registration)
2654 * @param p_header_data the data contained in the TLM box.
2655 * @param p_j2k the jpeg2000 codec.
2656 * @param p_header_size the size of the data contained in the TLM marker.
2657 * @param p_manager the user event manager.
2659 opj_bool j2k_read_crg_v2 (
2660 opj_j2k_v2_t *p_j2k,
2661 OPJ_BYTE * p_header_data,
2662 OPJ_UINT32 p_header_size,
2663 struct opj_event_mgr * p_manager
2666 OPJ_UINT32 l_nb_comp;
2668 assert(p_header_data != 00);
2669 assert(p_j2k != 00);
2670 assert(p_manager != 00);
2672 l_nb_comp = p_j2k->m_image->numcomps;
2674 if (p_header_size != l_nb_comp *4) {
2675 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
2678 /* Do not care of this at the moment since only local variables are set here */
2681 (i = 0; i < l_nb_comp; ++i)
2683 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
2685 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
2692 static void j2k_read_tlm(opj_j2k_t *j2k) {
2693 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
2694 long int Ttlm_i, Ptlm_i;
2696 opj_cio_t *cio = j2k->cio;
2698 len = cio_read(cio, 2); /* Ltlm */
2699 Ztlm = cio_read(cio, 1); /* Ztlm */
2700 Stlm = cio_read(cio, 1); /* Stlm */
2701 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
2702 SP = (Stlm >> 6) & 0x01;
2703 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
2704 for (i = 0; i < tile_tlm; i++) {
2705 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
2706 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
2711 * Reads a TLM marker (Tile Length Marker)
2713 * @param p_header_data the data contained in the TLM box.
2714 * @param p_j2k the jpeg2000 codec.
2715 * @param p_header_size the size of the data contained in the TLM marker.
2716 * @param p_manager the user event manager.
2718 opj_bool j2k_read_tlm_v2 (
2719 opj_j2k_v2_t *p_j2k,
2720 OPJ_BYTE * p_header_data,
2721 OPJ_UINT32 p_header_size,
2722 struct opj_event_mgr * p_manager
2725 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
2727 assert(p_header_data != 00);
2728 assert(p_j2k != 00);
2729 assert(p_manager != 00);
2731 if (p_header_size < 2) {
2732 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2737 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
2739 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
2742 l_ST = ((l_Stlm >> 4) & 0x3);
2743 l_SP = (l_Stlm >> 6) & 0x1;
2745 l_Ptlm_size = (l_SP + 1) * 2;
2746 l_quotient = l_Ptlm_size + l_ST;
2748 l_tot_num_tp = p_header_size / l_quotient;
2749 l_tot_num_tp_remaining = p_header_size % l_quotient;
2751 if (l_tot_num_tp_remaining != 0) {
2752 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2755 /* FIXME Do not care of this at the moment since only local variables are set here */
2758 (i = 0; i < l_tot_num_tp; ++i)
2760 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
2761 p_header_data += l_ST;
2762 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
2763 p_header_data += l_Ptlm_size;
2768 static void j2k_read_plm(opj_j2k_t *j2k) {
2769 int len, i, Zplm, Nplm, add, packet_len = 0;
2771 opj_cio_t *cio = j2k->cio;
2773 len = cio_read(cio, 2); /* Lplm */
2774 Zplm = cio_read(cio, 1); /* Zplm */
2777 Nplm = cio_read(cio, 4); /* Nplm */
2779 for (i = Nplm; i > 0; i--) {
2780 add = cio_read(cio, 1);
2782 packet_len = (packet_len << 7) + add; /* Iplm_ij */
2783 if ((add & 0x80) == 0) {
2794 * Reads a PLM marker (Packet length, main header marker)
2796 * @param p_header_data the data contained in the TLM box.
2797 * @param p_j2k the jpeg2000 codec.
2798 * @param p_header_size the size of the data contained in the TLM marker.
2799 * @param p_manager the user event manager.
2801 opj_bool j2k_read_plm_v2 (
2802 opj_j2k_v2_t *p_j2k,
2803 OPJ_BYTE * p_header_data,
2804 OPJ_UINT32 p_header_size,
2805 struct opj_event_mgr * p_manager
2809 assert(p_header_data != 00);
2810 assert(p_j2k != 00);
2811 assert(p_manager != 00);
2813 if (p_header_size < 1) {
2814 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2817 /* Do not care of this at the moment since only local variables are set here */
2819 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
2826 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
2828 p_header_size -= (1+l_Nplm);
2832 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2836 (i = 0; i < l_Nplm; ++i)
2838 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
2840 // take only the last seven bytes
2841 l_packet_len |= (l_tmp & 0x7f);
2849 // store packet length and proceed to next packet
2856 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2864 static void j2k_read_plt(opj_j2k_t *j2k) {
2865 int len, i, Zplt, packet_len = 0, add;
2867 opj_cio_t *cio = j2k->cio;
2869 len = cio_read(cio, 2); /* Lplt */
2870 Zplt = cio_read(cio, 1); /* Zplt */
2871 for (i = len - 3; i > 0; i--) {
2872 add = cio_read(cio, 1);
2873 packet_len = (packet_len << 7) + add; /* Iplt_i */
2874 if ((add & 0x80) == 0) {
2882 * Reads a PLT marker (Packet length, tile-part header)
2884 * @param p_header_data the data contained in the PLT box.
2885 * @param p_j2k the jpeg2000 codec.
2886 * @param p_header_size the size of the data contained in the PLT marker.
2887 * @param p_manager the user event manager.
2889 opj_bool j2k_read_plt_v2 (
2890 opj_j2k_v2_t *p_j2k,
2891 OPJ_BYTE * p_header_data,
2892 OPJ_UINT32 p_header_size,
2893 struct opj_event_mgr * p_manager
2896 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
2899 assert(p_header_data != 00);
2900 assert(p_j2k != 00);
2901 assert(p_manager != 00);
2903 if (p_header_size < 1) {
2904 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2908 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
2912 for (i = 0; i < p_header_size; ++i) {
2913 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
2915 // take only the last seven bytes
2916 l_packet_len |= (l_tmp & 0x7f);
2921 // store packet length and proceed to next packet
2926 if (l_packet_len != 0) {
2927 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2934 static void j2k_read_ppm(opj_j2k_t *j2k) {
2935 int len, Z_ppm, i, j;
2938 opj_cp_t *cp = j2k->cp;
2939 opj_cio_t *cio = j2k->cio;
2941 len = cio_read(cio, 2);
2944 Z_ppm = cio_read(cio, 1); /* Z_ppm */
2947 if (cp->ppm_previous == 0) {
2948 N_ppm = cio_read(cio, 4); /* N_ppm */
2951 N_ppm = cp->ppm_previous;
2954 if (Z_ppm == 0) { /* First PPM marker */
2955 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
2956 cp->ppm_data_first = cp->ppm_data;
2957 cp->ppm_len = N_ppm;
2958 } else { /* NON-first PPM marker */
2959 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
2962 /* this memory allocation check could be done even in non-JPWL cases */
2964 if (!cp->ppm_data) {
2965 opj_event_msg(j2k->cinfo, EVT_ERROR,
2966 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
2968 if (!JPWL_ASSUME || JPWL_ASSUME) {
2969 opj_free(cp->ppm_data);
2970 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2977 cp->ppm_data_first = cp->ppm_data;
2978 cp->ppm_len = N_ppm + cp->ppm_store;
2980 for (i = N_ppm; i > 0; i--) { /* Read packet header */
2981 cp->ppm_data[j] = cio_read(cio, 1);
2985 break; /* Case of non-finished packet header in present marker but finished in next one */
2987 cp->ppm_previous = i - 1;
2992 * Reads a PPM marker (Packed packet headers, main header)
2994 * @param p_header_data the data contained in the POC box.
2995 * @param p_j2k the jpeg2000 codec.
2996 * @param p_header_size the size of the data contained in the POC marker.
2997 * @param p_manager the user event manager.
2999 opj_bool j2k_read_ppm_v2 (
3000 opj_j2k_v2_t *p_j2k,
3001 OPJ_BYTE * p_header_data,
3002 OPJ_UINT32 p_header_size,
3003 struct opj_event_mgr * p_manager
3007 opj_cp_v2_t *l_cp = 00;
3008 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3011 assert(p_header_data != 00);
3012 assert(p_j2k != 00);
3013 assert(p_manager != 00);
3015 if (p_header_size < 1) {
3016 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3020 l_cp = &(p_j2k->m_cp);
3023 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3029 if (p_header_size < 4) {
3030 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3034 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3038 /* First PPM marker: Initialization */
3039 l_cp->ppm_len = l_N_ppm;
3040 l_cp->ppm_data_size = 0;
3042 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3043 if (l_cp->ppm_buffer == 00) {
3044 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3047 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3049 l_cp->ppm_data = l_cp->ppm_buffer;
3053 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3054 if (p_header_size >= 4) {
3056 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3059 l_cp->ppm_len += l_N_ppm ;
3061 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3062 if (l_cp->ppm_buffer == 00) {
3063 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3066 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3068 l_cp->ppm_data = l_cp->ppm_buffer;
3075 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3077 if (l_remaining_data <= p_header_size) {
3078 /* we must store less information than available in the packet */
3079 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3080 l_cp->ppm_data_size = l_cp->ppm_len;
3081 p_header_size -= l_remaining_data;
3082 p_header_data += l_remaining_data;
3085 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3086 l_cp->ppm_data_size += p_header_size;
3087 p_header_data += p_header_size;
3099 * Reads a PPM marker (Packed packet headers, main header)
3101 * @param p_header_data the data contained in the POC box.
3102 * @param p_j2k the jpeg2000 codec.
3103 * @param p_header_size the size of the data contained in the POC marker.
3104 * @param p_manager the user event manager.
3106 opj_bool j2k_read_ppm_v3 (
3107 opj_j2k_v2_t *p_j2k,
3108 OPJ_BYTE * p_header_data,
3109 OPJ_UINT32 p_header_size,
3110 struct opj_event_mgr * p_manager
3113 opj_cp_v2_t *l_cp = 00;
3114 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3117 assert(p_header_data != 00);
3118 assert(p_j2k != 00);
3119 assert(p_manager != 00);
3121 // Minimum size of PPM marker is equal to the size of Zppm element
3122 if (p_header_size < 1) {
3123 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3127 l_cp = &(p_j2k->m_cp);
3130 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3136 // We need now at least the Nppm^0 element
3137 if (p_header_size < 4) {
3138 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3142 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3146 /* First PPM marker: Initialization */
3147 l_cp->ppm_len = l_N_ppm;
3148 l_cp->ppm_data_read = 0;
3150 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3151 if (l_cp->ppm_data == 00) {
3152 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3155 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3157 l_cp->ppm_data_current = l_cp->ppm_data;
3159 //l_cp->ppm_data = l_cp->ppm_buffer;
3162 if (p_header_size < 4) {
3163 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3167 // Uncompleted Ippm series in the previous PPM marker?
3168 if (l_cp->ppm_data_read < l_cp->ppm_len) {
3169 // Get the place where add the remaining Ippm series
3170 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3171 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3174 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3178 // Increase the size of ppm_data to add the new Ippm series
3179 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3181 // Keep the position of the place where concatenate the new series
3182 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3183 l_cp->ppm_len += l_N_ppm;
3188 l_remaining_data = p_header_size;
3190 while (l_remaining_data >= l_N_ppm) {
3191 // read a complete Ippm series
3192 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3193 p_header_size -= l_N_ppm;
3194 p_header_data += l_N_ppm;
3196 l_cp->ppm_data_read += l_N_ppm; // Increase the number of data read
3200 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
3205 l_remaining_data = p_header_size;
3209 l_remaining_data = p_header_size;
3211 // Next Ippm series is a complete series ?
3212 if (l_remaining_data > l_N_ppm) {
3213 // Increase the size of ppm_data to add the new Ippm series
3214 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3216 // Keep the position of the place where concatenate the new series
3217 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3218 l_cp->ppm_len += l_N_ppm;
3223 // Need to read an incomplete Ippm series
3224 if (l_remaining_data) {
3225 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3227 // Keep the position of the place where concatenate the new series
3228 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3229 l_cp->ppm_len += l_N_ppm;
3231 // Read incomplete Ippm series
3232 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3233 p_header_size -= l_remaining_data;
3234 p_header_data += l_remaining_data;
3236 l_cp->ppm_data_read += l_remaining_data; // Increase the number of data read
3241 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3242 if (p_header_size >= 4) {
3244 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3247 l_cp->ppm_len += l_N_ppm ;
3249 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3250 if (l_cp->ppm_buffer == 00) {
3251 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3254 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3256 l_cp->ppm_data = l_cp->ppm_buffer;
3263 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3265 if (l_remaining_data <= p_header_size) {
3266 /* we must store less information than available in the packet */
3267 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3268 l_cp->ppm_data_size = l_cp->ppm_len;
3269 p_header_size -= l_remaining_data;
3270 p_header_data += l_remaining_data;
3273 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3274 l_cp->ppm_data_size += p_header_size;
3275 p_header_data += p_header_size;
3284 static void j2k_read_ppt(opj_j2k_t *j2k) {
3285 int len, Z_ppt, i, j = 0;
3287 opj_cp_t *cp = j2k->cp;
3288 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
3289 opj_cio_t *cio = j2k->cio;
3291 len = cio_read(cio, 2);
3292 Z_ppt = cio_read(cio, 1);
3294 if (Z_ppt == 0) { /* First PPT marker */
3295 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
3296 tcp->ppt_data_first = tcp->ppt_data;
3298 tcp->ppt_len = len - 3;
3299 } else { /* NON-first PPT marker */
3300 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
3301 tcp->ppt_data_first = tcp->ppt_data;
3302 tcp->ppt_len = len - 3 + tcp->ppt_store;
3305 for (i = len - 3; i > 0; i--) {
3306 tcp->ppt_data[j] = cio_read(cio, 1);
3313 * Reads a PPT marker (Packed packet headers, tile-part header)
3315 * @param p_header_data the data contained in the PPT box.
3316 * @param p_j2k the jpeg2000 codec.
3317 * @param p_header_size the size of the data contained in the PPT marker.
3318 * @param p_manager the user event manager.
3320 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
3321 OPJ_BYTE * p_header_data,
3322 OPJ_UINT32 p_header_size,
3323 struct opj_event_mgr * p_manager )
3325 opj_cp_v2_t *l_cp = 00;
3326 opj_tcp_v2_t *l_tcp = 00;
3330 assert(p_header_data != 00);
3331 assert(p_j2k != 00);
3332 assert(p_manager != 00);
3334 /* We need to have the Z_ppt element at minimum */
3335 if (p_header_size < 1) {
3336 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3340 l_cp = &(p_j2k->m_cp);
3342 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");
3346 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3349 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3353 /* Allocate buffer to read the packet header */
3355 /* First PPT marker */
3356 l_tcp->ppt_data_size = 0;
3357 l_tcp->ppt_len = p_header_size;
3359 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3360 if (l_tcp->ppt_buffer == 00) {
3361 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3364 l_tcp->ppt_data = l_tcp->ppt_buffer;
3366 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3369 l_tcp->ppt_len += p_header_size;
3371 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3372 if (l_tcp->ppt_buffer == 00) {
3373 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3376 l_tcp->ppt_data = l_tcp->ppt_buffer;
3378 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3381 /* Read packet header from buffer */
3382 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3384 l_tcp->ppt_data_size += p_header_size;
3389 static void j2k_write_tlm(opj_j2k_t *j2k){
3391 opj_cio_t *cio = j2k->cio;
3392 j2k->tlm_start = cio_tell(cio);
3393 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
3394 lenp = 4 + (5*j2k->totnum_tp);
3395 cio_write(cio,lenp,2); /* Ltlm */
3396 cio_write(cio, 0,1); /* Ztlm=0*/
3397 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3398 cio_skip(cio,5*j2k->totnum_tp);
3401 static void j2k_write_sot(opj_j2k_t *j2k) {
3404 opj_cio_t *cio = j2k->cio;
3406 j2k->sot_start = cio_tell(cio);
3407 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
3408 lenp = cio_tell(cio);
3409 cio_skip(cio, 2); /* Lsot (further) */
3410 cio_write(cio, j2k->curtileno, 2); /* Isot */
3411 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
3412 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
3413 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
3414 len = cio_tell(cio) - lenp;
3415 cio_seek(cio, lenp);
3416 cio_write(cio, len, 2); /* Lsot */
3417 cio_seek(cio, lenp + len);
3421 /* update markers struct */
3422 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
3423 #endif /* USE_JPWL */
3426 if( j2k->cstr_info && j2k->cur_tp_num==0){
3427 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
3431 static void j2k_read_sot(opj_j2k_t *j2k) {
3432 int len, tileno, totlen, partno, numparts, i;
3433 opj_tcp_t *tcp = NULL;
3436 opj_cp_t *cp = j2k->cp;
3437 opj_cio_t *cio = j2k->cio;
3439 len = cio_read(cio, 2);
3440 tileno = cio_read(cio, 2);
3443 if (j2k->cp->correct) {
3445 static int backup_tileno = 0;
3447 /* tileno is negative or larger than the number of tiles!!! */
3448 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3449 opj_event_msg(j2k->cinfo, EVT_ERROR,
3450 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3451 tileno, (cp->tw * cp->th));
3453 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3456 /* we try to correct */
3457 tileno = backup_tileno;
3458 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3459 "- setting tile number to %d\n",
3463 /* keep your private count of tiles */
3466 #endif /* USE_JPWL */
3468 if (cp->tileno_size == 0) {
3469 cp->tileno[cp->tileno_size] = tileno;
3473 while (i < cp->tileno_size && status == 0) {
3474 status = cp->tileno[i] == tileno ? 1 : 0;
3478 cp->tileno[cp->tileno_size] = tileno;
3483 totlen = cio_read(cio, 4);
3486 if (j2k->cp->correct) {
3488 /* totlen is negative or larger than the bytes left!!! */
3489 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
3490 opj_event_msg(j2k->cinfo, EVT_ERROR,
3491 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3492 totlen, cio_numbytesleft(cio) + 8);
3494 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3497 /* we try to correct */
3499 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3500 "- setting Psot to %d => assuming it is the last tile\n",
3505 #endif /* USE_JPWL */
3508 totlen = cio_numbytesleft(cio) + 8;
3510 partno = cio_read(cio, 1);
3511 numparts = cio_read(cio, 1);
3513 if (partno >= numparts) {
3514 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);
3515 numparts = partno+1;
3518 j2k->curtileno = tileno;
3519 j2k->cur_tp_num = partno;
3520 j2k->eot = cio_getbp(cio) - 12 + totlen;
3521 j2k->state = J2K_STATE_TPH;
3522 tcp = &cp->tcps[j2k->curtileno];
3525 if (j2k->cstr_info) {
3528 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
3529 j2k->cstr_info->tile[tileno].tileno = tileno;
3530 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
3531 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3533 j2k->cstr_info->tile[tileno].end_pos += totlen;
3535 j2k->cstr_info->tile[tileno].num_tps = numparts;
3537 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
3539 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)
3540 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
3541 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3542 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3545 if (tcp->first == 1) {
3546 /* Initialization PPT */
3547 opj_tccp_t *tmp = tcp->tccps;
3548 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
3550 tcp->ppt_data = NULL;
3551 tcp->ppt_data_first = NULL;
3554 for (i = 0; i < j2k->image->numcomps; i++) {
3555 tcp->tccps[i] = j2k->default_tcp->tccps[i];
3557 cp->tcps[j2k->curtileno].first = 0;
3562 * Reads a PPT marker (Packed packet headers, tile-part header)
3564 * @param p_header_data the data contained in the PPT box.
3565 * @param p_j2k the jpeg2000 codec.
3566 * @param p_header_size the size of the data contained in the PPT marker.
3567 * @param p_manager the user event manager.
3569 opj_bool j2k_read_sot_v2 (
3570 opj_j2k_v2_t *p_j2k,
3571 OPJ_BYTE * p_header_data,
3572 OPJ_UINT32 p_header_size,
3573 struct opj_event_mgr * p_manager
3577 opj_cp_v2_t *l_cp = 00;
3578 opj_tcp_v2_t *l_tcp = 00;
3579 OPJ_UINT32 l_tot_len, l_num_parts = 0;
3580 OPJ_UINT32 l_current_part;
3581 OPJ_UINT32 l_tile_x,l_tile_y;
3584 assert(p_header_data != 00);
3585 assert(p_j2k != 00);
3586 assert(p_manager != 00);
3588 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
3589 if (p_header_size != 8) {
3590 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3594 l_cp = &(p_j2k->m_cp);
3595 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
3598 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3599 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
3600 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
3603 if (l_cp->correct) {
3605 int tileno = p_j2k->m_current_tile_number;
3606 static int backup_tileno = 0;
3608 /* tileno is negative or larger than the number of tiles!!! */
3609 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
3610 opj_event_msg_v2(p_manager, EVT_ERROR,
3611 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3612 tileno, (l_cp->tw * l_cp->th));
3614 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3617 /* we try to correct */
3618 tileno = backup_tileno;
3619 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3620 "- setting tile number to %d\n",
3624 /* keep your private count of tiles */
3627 #endif /* USE_JPWL */
3629 /* look for the tile in the list of already processed tile (in parts). */
3630 /* Optimization possible here with a more complex data structure and with the removing of tiles */
3631 /* since the time taken by this function can only grow at the time */
3633 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
3637 if (l_cp->correct) {
3639 /* totlen is negative or larger than the bytes left!!! */
3640 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))) { */
3641 opj_event_msg_v2(p_manager, EVT_ERROR,
3642 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3643 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
3645 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3648 /* we try to correct */
3650 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3651 "- setting Psot to %d => assuming it is the last tile\n",
3655 #endif /* USE_JPWL */
3657 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
3659 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
3660 "we assuming it is the last tile-part of the codestream.\n");
3661 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3664 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
3667 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
3670 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
3671 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
3672 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
3673 if (l_tcp->m_nb_tile_parts) {
3674 if (l_current_part >= l_tcp->m_nb_tile_parts){
3675 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
3676 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
3677 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3681 l_tcp->m_nb_tile_parts = l_num_parts;
3684 /* If know the number of tile part header we will check if we didn't read the last*/
3685 if (l_tcp->m_nb_tile_parts) {
3686 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
3687 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
3691 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
3692 /* Keep the size of data to skip after this marker */
3693 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
3696 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
3697 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
3700 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
3702 /* Check if the current tile is outside the area we want decode (in tile index)*/
3703 p_j2k->m_specific_param.m_decoder.m_skip_data =
3704 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
3705 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
3706 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
3707 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
3711 /* 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 */
3712 /* if (p_j2k->cstr_info) {
3715 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
3718 p_j2k->cstr_info->tile[tileno].tileno = tileno;
3719 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
3720 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3721 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
3724 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
3727 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
3731 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
3734 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
3735 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3736 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3741 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
3744 opj_tcp_t *tcp = NULL;
3745 opj_codestream_info_t *cstr_info = NULL;
3747 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
3748 opj_cp_t *cp = j2k->cp;
3749 opj_cio_t *cio = j2k->cio;
3751 tcd->tp_num = j2k->tp_num ;
3752 tcd->cur_tp_num = j2k->cur_tp_num;
3754 cio_write(cio, J2K_MS_SOD, 2);
3756 if( j2k->cstr_info && j2k->cur_tp_num==0){
3757 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
3760 if (j2k->curtileno == 0) {
3761 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
3765 cstr_info = j2k->cstr_info;
3767 if (!j2k->cur_tp_num ) {
3768 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3769 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
3772 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
3773 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
3777 /* update markers struct */
3778 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
3779 #endif /* USE_JPWL */
3784 tcp = &cp->tcps[j2k->curtileno];
3785 for (layno = 0; layno < tcp->numlayers; layno++) {
3786 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
3787 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
3788 } else if (tcp->rates[layno]) {
3789 tcp->rates[layno]=1;
3792 if(j2k->cur_tp_num == 0){
3793 tcd->tcd_image->tiles->packno = 0;
3795 cstr_info->packno = 0;
3798 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
3800 /* Writing Psot in SOT marker */
3801 totlen = cio_tell(cio) + l - j2k->sot_start;
3802 cio_seek(cio, j2k->sot_start + 6);
3803 cio_write(cio, totlen, 4);
3804 cio_seek(cio, j2k->sot_start + totlen);
3805 /* Writing Ttlm and Ptlm in TLM marker */
3807 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
3808 cio_write(cio, j2k->curtileno, 1);
3809 cio_write(cio, totlen, 4);
3811 cio_seek(cio, j2k->sot_start + totlen);
3814 static void j2k_read_sod(opj_j2k_t *j2k) {
3815 int len, truncate = 0, i;
3816 unsigned char *data = NULL, *data_ptr = NULL;
3818 opj_cio_t *cio = j2k->cio;
3819 int curtileno = j2k->curtileno;
3822 if (j2k->cstr_info) {
3823 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
3824 cio_tell(cio) + j2k->pos_correction - 1;
3825 if (j2k->cur_tp_num == 0)
3826 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3827 j2k->cstr_info->packno = 0;
3830 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
3832 if (len == cio_numbytesleft(cio) + 1) {
3833 truncate = 1; /* Case of a truncate codestream */
3836 data = j2k->tile_data[curtileno];
3837 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
3839 data_ptr = data + j2k->tile_len[curtileno];
3840 for (i = 0; i < len; i++) {
3841 data_ptr[i] = cio_read(cio, 1);
3844 j2k->tile_len[curtileno] += len;
3845 j2k->tile_data[curtileno] = data;
3848 j2k->state = J2K_STATE_TPHSOT;
3850 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
3856 * Reads a SOD marker (Start Of Data)
3858 * @param p_header_data the data contained in the SOD box.
3859 * @param p_j2k the jpeg2000 codec.
3860 * @param p_header_size the size of the data contained in the SOD marker.
3861 * @param p_manager the user event manager.
3863 opj_bool j2k_read_sod_v2 (
3864 opj_j2k_v2_t *p_j2k,
3865 struct opj_stream_private *p_stream,
3866 struct opj_event_mgr * p_manager
3869 OPJ_UINT32 l_current_read_size;
3870 opj_codestream_index_t * l_cstr_index = 00;
3871 OPJ_BYTE ** l_current_data = 00;
3872 opj_tcp_v2_t * l_tcp = 00;
3873 OPJ_UINT32 * l_tile_len = 00;
3876 assert(p_j2k != 00);
3877 assert(p_manager != 00);
3878 assert(p_stream != 00);
3880 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
3882 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part)
3883 p_j2k->m_specific_param.m_decoder.m_sot_length = opj_stream_get_number_byte_left(p_stream) - 2;
3885 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
3887 l_current_data = &(l_tcp->m_data);
3888 l_tile_len = &l_tcp->m_data_size;
3890 if (! *l_current_data) {
3891 *l_current_data = (OPJ_BYTE*) opj_malloc/*FIXME V2 -> my_opj_malloc*/(p_j2k->m_specific_param.m_decoder.m_sot_length);
3894 *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);
3897 if (*l_current_data == 00) {
3898 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
3902 #ifdef TODO_MSD /* FIXME */
3903 l_cstr_index = p_j2k->cstr_index;
3907 OPJ_SIZE_T l_current_pos = opj_stream_tell(p_stream)-1;
3908 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[p_j2k->m_specific_param.m_encoder.m_current_tile_part_number].tp_end_header = l_current_pos;
3910 l_cstr_index->packno = 0;
3914 l_current_read_size = opj_stream_read_data( p_stream,
3915 *l_current_data + *l_tile_len,
3916 p_j2k->m_specific_param.m_decoder.m_sot_length,
3919 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
3920 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
3923 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
3926 *l_tile_len += l_current_read_size;
3932 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
3933 opj_cp_t *cp = j2k->cp;
3934 opj_tcp_t *tcp = &cp->tcps[tileno];
3935 opj_cio_t *cio = j2k->cio;
3936 int numcomps = j2k->image->numcomps;
3938 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
3939 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
3940 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
3941 cio_write(cio, 0, 1); /* Srgn */
3942 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
3945 static void j2k_read_rgn(opj_j2k_t *j2k) {
3946 int len, compno, roisty;
3948 opj_cp_t *cp = j2k->cp;
3949 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3950 opj_cio_t *cio = j2k->cio;
3951 int numcomps = j2k->image->numcomps;
3953 len = cio_read(cio, 2); /* Lrgn */
3954 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
3955 roisty = cio_read(cio, 1); /* Srgn */
3958 if (j2k->cp->correct) {
3959 /* totlen is negative or larger than the bytes left!!! */
3960 if (compno >= numcomps) {
3961 opj_event_msg(j2k->cinfo, EVT_ERROR,
3962 "JPWL: bad component number in RGN (%d when there are only %d)\n",
3964 if (!JPWL_ASSUME || JPWL_ASSUME) {
3965 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3970 #endif /* USE_JPWL */
3972 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
3975 static void j2k_write_eoc(opj_j2k_t *j2k) {
3976 opj_cio_t *cio = j2k->cio;
3977 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
3978 cio_write(cio, J2K_MS_EOC, 2);
3982 /* update markers struct */
3983 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
3984 #endif /* USE_JPWL */
3989 * Reads a RGN marker (Region Of Interest)
3991 * @param p_header_data the data contained in the POC box.
3992 * @param p_j2k the jpeg2000 codec.
3993 * @param p_header_size the size of the data contained in the POC marker.
3994 * @param p_manager the user event manager.
3996 opj_bool j2k_read_rgn_v2 (
3997 opj_j2k_v2_t *p_j2k,
3998 OPJ_BYTE * p_header_data,
3999 OPJ_UINT32 p_header_size,
4000 struct opj_event_mgr * p_manager
4003 OPJ_UINT32 l_nb_comp;
4004 opj_image_t * l_image = 00;
4006 opj_cp_v2_t *l_cp = 00;
4007 opj_tcp_v2_t *l_tcp = 00;
4008 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4011 assert(p_header_data != 00);
4012 assert(p_j2k != 00);
4013 assert(p_manager != 00);
4015 l_image = p_j2k->m_image;
4016 l_nb_comp = l_image->numcomps;
4018 if (l_nb_comp <= 256) {
4023 if (p_header_size != 2 + l_comp_room) {
4024 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4028 l_cp = &(p_j2k->m_cp);
4029 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4030 &l_cp->tcps[p_j2k->m_current_tile_number] :
4031 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4033 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4034 p_header_data+=l_comp_room;
4035 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4039 if (l_cp->correct) {
4040 /* totlen is negative or larger than the bytes left!!! */
4041 if (l_comp_room >= l_nb_comp) {
4042 opj_event_msg_v2(p_manager, EVT_ERROR,
4043 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4044 l_comp_room, l_nb_comp);
4045 if (!JPWL_ASSUME || JPWL_ASSUME) {
4046 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4051 #endif /* USE_JPWL */
4053 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4060 static void j2k_read_eoc(opj_j2k_t *j2k) {
4064 /* if packets should be decoded */
4065 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
4066 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
4067 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
4068 for (i = 0; i < j2k->cp->tileno_size; i++) {
4069 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
4070 tileno = j2k->cp->tileno[i];
4071 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
4072 opj_free(j2k->tile_data[tileno]);
4073 j2k->tile_data[tileno] = NULL;
4074 tcd_free_decode_tile(tcd, i);
4075 if (success == OPJ_FALSE) {
4076 j2k->state |= J2K_STATE_ERR;
4080 tcd_free_decode(tcd);
4083 /* if packets should not be decoded */
4085 for (i = 0; i < j2k->cp->tileno_size; i++) {
4086 tileno = j2k->cp->tileno[i];
4087 opj_free(j2k->tile_data[tileno]);
4088 j2k->tile_data[tileno] = NULL;
4091 if (j2k->state & J2K_STATE_ERR)
4092 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
4094 j2k->state = J2K_STATE_MT;
4098 * Reads a EOC marker (End Of Codestream)
4100 * @param p_header_data the data contained in the SOD box.
4101 * @param p_j2k the jpeg2000 codec.
4102 * @param p_header_size the size of the data contained in the SOD marker.
4103 * @param p_manager the user event manager.
4105 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
4106 struct opj_stream_private *p_stream,
4107 struct opj_event_mgr * p_manager )
4110 opj_tcd_v2_t * l_tcd = 00;
4111 OPJ_UINT32 l_nb_tiles;
4112 opj_tcp_v2_t * l_tcp = 00;
4116 assert(p_j2k != 00);
4117 assert(p_manager != 00);
4118 assert(p_stream != 00);
4120 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4121 l_tcp = p_j2k->m_cp.tcps;
4123 l_tcd = tcd_create_v2(OPJ_TRUE);
4125 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4129 for (i = 0; i < l_nb_tiles; ++i) {
4130 if (l_tcp->m_data) {
4131 if (! tcd_init_decode_tile(l_tcd, i)) {
4132 tcd_destroy_v2(l_tcd);
4133 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4137 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4141 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4146 j2k_tcp_destroy(l_tcp);
4150 tcd_destroy_v2(l_tcd);
4154 typedef struct opj_dec_mstabent {
4157 /** value of the state when the marker can appear */
4159 /** action linked to the marker */
4160 void (*handler) (opj_j2k_t *j2k);
4161 } opj_dec_mstabent_t;
4163 opj_dec_mstabent_t j2k_dec_mstab[] = {
4164 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
4165 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
4166 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
4167 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
4168 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
4169 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
4170 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
4171 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
4172 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
4173 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
4174 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
4175 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
4176 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
4177 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
4178 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
4179 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
4181 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
4182 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
4185 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
4186 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
4187 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
4188 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
4189 #endif /* USE_JPWL */
4191 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
4192 {J2K_MS_INSEC, 0, j2k_read_insec},
4193 #endif /* USE_JPSEC */
4195 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
4198 static void j2k_read_unk(opj_j2k_t *j2k) {
4199 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
4202 if (j2k->cp->correct) {
4204 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
4205 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4206 id = cio_read(j2k->cio, 2);
4207 opj_event_msg(j2k->cinfo, EVT_ERROR,
4208 "JPWL: really don't know this marker %x\n",
4211 opj_event_msg(j2k->cinfo, EVT_ERROR,
4212 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
4215 /* OK, activate this at your own risk!!! */
4216 /* we look for the marker at the minimum hamming distance from this */
4217 while (j2k_dec_mstab[m].id) {
4219 /* 1's where they differ */
4220 tmp_id = j2k_dec_mstab[m].id ^ id;
4222 /* compute the hamming distance between our id and the current */
4224 for (i = 0; i < 16; i++) {
4225 if ((tmp_id >> i) & 0x0001) {
4230 /* if current distance is smaller, set the minimum */
4231 if (cur_dist < min_dist) {
4232 min_dist = cur_dist;
4233 min_id = j2k_dec_mstab[m].id;
4236 /* jump to the next marker */
4240 /* do we substitute the marker? */
4241 if (min_dist < JPWL_MAXIMUM_HAMMING) {
4242 opj_event_msg(j2k->cinfo, EVT_ERROR,
4243 "- marker %x is at distance %d from the read %x\n",
4244 min_id, min_dist, id);
4245 opj_event_msg(j2k->cinfo, EVT_ERROR,
4246 "- trying to substitute in place and crossing fingers!\n");
4247 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4248 cio_write(j2k->cio, min_id, 2);
4251 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4256 #endif /* USE_JPWL */
4261 * Reads an unknown marker
4263 * @param p_stream the stream object to read from.
4264 * @param p_j2k the jpeg2000 codec.
4265 * @param p_manager the user event manager.
4267 * @return true if the marker could be deduced.
4269 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
4270 struct opj_stream_private *p_stream,
4271 OPJ_UINT32 *output_marker,
4272 struct opj_event_mgr * p_manager
4275 OPJ_UINT32 l_unknown_marker;
4276 const opj_dec_memory_marker_handler_t * l_marker_handler;
4277 OPJ_UINT32 l_size_unk = 2;
4280 assert(p_j2k != 00);
4281 assert(p_manager != 00);
4282 assert(p_stream != 00);
4284 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
4287 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
4288 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
4289 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
4293 // read 2 bytes as the new marker ID
4294 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
4296 if (!(l_unknown_marker < 0xff00)) {
4298 // Get the marker handler from the marker ID
4299 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
4301 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
4302 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
4306 if (l_marker_handler->id != J2K_MS_UNK) {
4307 /* Add the marker to the codestream index*/
4308 if (l_marker_handler->id != J2K_MS_SOT)
4309 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
4310 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
4312 break; /* next marker is known and well located */
4320 *output_marker = l_marker_handler->id ;
4326 Read the lookup table containing all the marker, status and action
4327 @param id Marker value
4329 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
4330 opj_dec_mstabent_t *e;
4331 for (e = j2k_dec_mstab; e->id != 0; e++) {
4341 * Reads a MCT marker (Multiple Component Transform)
4343 * @param p_header_data the data contained in the MCT box.
4344 * @param p_j2k the jpeg2000 codec.
4345 * @param p_header_size the size of the data contained in the MCT marker.
4346 * @param p_manager the user event manager.
4348 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
4349 OPJ_BYTE * p_header_data,
4350 OPJ_UINT32 p_header_size,
4351 struct opj_event_mgr * p_manager )
4354 opj_tcp_v2_t *l_tcp = 00;
4357 opj_mct_data_t * l_mct_data;
4360 assert(p_header_data != 00);
4361 assert(p_j2k != 00);
4363 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4364 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4365 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4367 if (p_header_size < 2) {
4368 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4373 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
4376 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
4380 if(p_header_size <= 6) {
4381 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4385 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
4386 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
4389 l_indix = l_tmp & 0xff;
4390 l_mct_data = l_tcp->m_mct_records;
4392 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
4393 if (l_mct_data->m_index == l_indix) {
4400 if (i == l_tcp->m_nb_mct_records) {
4401 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
4402 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
4404 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));
4405 if(! l_tcp->m_mct_records) {
4406 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4410 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
4411 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
4414 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
4417 if (l_mct_data->m_data) {
4418 opj_free(l_mct_data->m_data);
4419 l_mct_data->m_data = 00;
4422 l_mct_data->m_index = l_indix;
4423 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
4424 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
4426 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
4429 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
4435 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
4436 if (! l_mct_data->m_data) {
4437 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4440 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
4442 l_mct_data->m_data_size = p_header_size;
4443 ++l_tcp->m_nb_mct_records;
4449 * Reads a MCC marker (Multiple Component Collection)
4451 * @param p_header_data the data contained in the MCC box.
4452 * @param p_j2k the jpeg2000 codec.
4453 * @param p_header_size the size of the data contained in the MCC marker.
4454 * @param p_manager the user event manager.
4456 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
4457 OPJ_BYTE * p_header_data,
4458 OPJ_UINT32 p_header_size,
4459 struct opj_event_mgr * p_manager )
4464 opj_tcp_v2_t * l_tcp;
4465 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4466 opj_mct_data_t * l_mct_data;
4467 OPJ_UINT32 l_nb_collections;
4468 OPJ_UINT32 l_nb_comps;
4469 OPJ_UINT32 l_nb_bytes_by_comp;
4473 assert(p_header_data != 00);
4474 assert(p_j2k != 00);
4475 assert(p_manager != 00);
4477 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4478 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4479 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4481 if (p_header_size < 2) {
4482 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4487 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
4490 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
4494 if (p_header_size < 7) {
4495 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4499 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
4502 l_mcc_record = l_tcp->m_mcc_records;
4504 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
4505 if (l_mcc_record->m_index == l_indix) {
4512 if (i == l_tcp->m_nb_mcc_records) {
4513 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
4514 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
4516 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
4517 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
4518 if (! l_tcp->m_mcc_records) {
4519 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4522 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
4523 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));
4525 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
4527 l_mcc_record->m_index = l_indix;
4529 /* only one marker atm */
4530 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
4533 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
4537 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
4540 if (l_nb_collections > 1) {
4541 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
4547 for (i=0;i<l_nb_collections;++i) {
4548 if (p_header_size < 3) {
4549 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4553 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
4557 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
4561 opj_read_bytes(p_header_data,&l_nb_comps,2);
4566 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
4567 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
4569 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
4570 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4574 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
4576 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
4577 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
4578 p_header_data+=l_nb_bytes_by_comp;
4581 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
4586 opj_read_bytes(p_header_data,&l_nb_comps,2);
4589 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
4590 l_nb_comps &= 0x7fff;
4592 if (l_nb_comps != l_mcc_record->m_nb_comps) {
4593 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
4597 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
4598 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4602 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
4604 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
4605 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
4606 p_header_data+=l_nb_bytes_by_comp;
4609 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
4614 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
4617 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
4618 l_mcc_record->m_decorrelation_array = 00;
4619 l_mcc_record->m_offset_array = 00;
4621 l_indix = l_tmp & 0xff;
4623 l_mct_data = l_tcp->m_mct_records;
4624 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
4625 if (l_mct_data->m_index == l_indix) {
4626 l_mcc_record->m_decorrelation_array = l_mct_data;
4632 if (l_mcc_record->m_decorrelation_array == 00) {
4633 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4638 l_indix = (l_tmp >> 8) & 0xff;
4640 l_mct_data = l_tcp->m_mct_records;
4641 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
4642 if (l_mct_data->m_index == l_indix) {
4643 l_mcc_record->m_offset_array = l_mct_data;
4649 if (l_mcc_record->m_offset_array == 00) {
4650 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4656 if (p_header_size != 0) {
4657 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4661 ++l_tcp->m_nb_mcc_records;
4667 * Reads a MCO marker (Multiple Component Transform Ordering)
4669 * @param p_header_data the data contained in the MCO box.
4670 * @param p_j2k the jpeg2000 codec.
4671 * @param p_header_size the size of the data contained in the MCO marker.
4672 * @param p_manager the user event manager.
4674 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
4675 OPJ_BYTE * p_header_data,
4676 OPJ_UINT32 p_header_size,
4677 struct opj_event_mgr * p_manager )
4679 OPJ_UINT32 l_tmp, i;
4680 OPJ_UINT32 l_nb_stages;
4681 opj_tcp_v2_t * l_tcp;
4682 opj_tccp_t * l_tccp;
4683 opj_image_t * l_image;
4684 opj_image_comp_t * l_img_comp;
4687 assert(p_header_data != 00);
4688 assert(p_j2k != 00);
4689 assert(p_manager != 00);
4691 l_image = p_j2k->m_image;
4692 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4693 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4694 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4696 if (p_header_size < 1) {
4697 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
4701 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
4704 if (l_nb_stages > 1) {
4705 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
4709 if (p_header_size != l_nb_stages + 1) {
4710 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
4714 l_tccp = l_tcp->tccps;
4715 l_img_comp = l_image->comps;
4717 for (i=0;i<l_image->numcomps;++i) {
4718 l_tccp->m_dc_level_shift = 0;
4722 if (l_tcp->m_mct_decoding_matrix) {
4723 opj_free(l_tcp->m_mct_decoding_matrix);
4724 l_tcp->m_mct_decoding_matrix = 00;
4727 for (i=0;i<l_nb_stages;++i) {
4728 opj_read_bytes(p_header_data,&l_tmp,1);
4731 if (! j2k_add_mct(l_tcp,p_j2k->m_image,l_tmp)) {
4739 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
4742 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4743 opj_mct_data_t * l_deco_array, * l_offset_array;
4744 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
4745 OPJ_UINT32 l_nb_elem;
4746 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
4747 opj_tccp_t * l_tccp;
4750 assert(p_tcp != 00);
4752 l_mcc_record = p_tcp->m_mcc_records;
4754 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
4755 if (l_mcc_record->m_index == p_index) {
4760 if (i==p_tcp->m_nb_mcc_records) {
4761 /** element discarded **/
4765 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
4766 /** do not support number of comps != image */
4770 l_deco_array = l_mcc_record->m_decorrelation_array;
4773 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
4774 if (l_deco_array->m_data_size != l_data_size) {
4778 l_nb_elem = p_image->numcomps * p_image->numcomps;
4779 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
4780 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
4782 if (! p_tcp->m_mct_decoding_matrix ) {
4786 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);
4789 l_offset_array = l_mcc_record->m_offset_array;
4791 if (l_offset_array) {
4792 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
4793 if (l_offset_array->m_data_size != l_data_size) {
4797 l_nb_elem = p_image->numcomps;
4798 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
4799 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
4801 if (! l_offset_data ) {
4805 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
4807 l_tccp = p_tcp->tccps;
4808 l_current_offset_data = l_offset_data;
4810 for (i=0;i<p_image->numcomps;++i) {
4811 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
4815 opj_free(l_offset_data);
4822 * Reads a CBD marker (Component bit depth definition)
4823 * @param p_header_data the data contained in the CBD box.
4824 * @param p_j2k the jpeg2000 codec.
4825 * @param p_header_size the size of the data contained in the CBD marker.
4826 * @param p_manager the user event manager.
4828 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
4829 OPJ_BYTE * p_header_data,
4830 OPJ_UINT32 p_header_size,
4831 struct opj_event_mgr * p_manager)
4833 OPJ_UINT32 l_nb_comp,l_num_comp;
4834 OPJ_UINT32 l_comp_def;
4836 opj_image_comp_t * l_comp = 00;
4839 assert(p_header_data != 00);
4840 assert(p_j2k != 00);
4841 assert(p_manager != 00);
4843 l_num_comp = p_j2k->m_image->numcomps;
4845 if (p_header_size != (p_j2k->m_image->numcomps + 2)) {
4846 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
4850 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
4853 if (l_nb_comp != l_num_comp) {
4854 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
4858 l_comp = p_j2k->m_image->comps;
4859 for (i=0;i<l_num_comp;++i) {
4860 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
4862 l_comp->sgnd = (l_comp_def>>7) & 1;
4863 l_comp->prec = (l_comp_def&0x7f) + 1;
4871 /* ----------------------------------------------------------------------- */
4872 /* J2K / JPT decoder interface */
4873 /* ----------------------------------------------------------------------- */
4875 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
4876 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4880 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
4881 if(!j2k->default_tcp) {
4887 j2k->tile_data = NULL;
4892 void j2k_destroy_decompress(opj_j2k_t *j2k) {
4895 if(j2k->tile_len != NULL) {
4896 opj_free(j2k->tile_len);
4898 if(j2k->tile_data != NULL) {
4899 opj_free(j2k->tile_data);
4901 if(j2k->default_tcp != NULL) {
4902 opj_tcp_t *default_tcp = j2k->default_tcp;
4903 if(default_tcp->ppt_data_first != NULL) {
4904 opj_free(default_tcp->ppt_data_first);
4906 if(j2k->default_tcp->tccps != NULL) {
4907 opj_free(j2k->default_tcp->tccps);
4909 opj_free(j2k->default_tcp);
4911 if(j2k->cp != NULL) {
4912 opj_cp_t *cp = j2k->cp;
4913 if(cp->tcps != NULL) {
4914 for(i = 0; i < cp->tw * cp->th; i++) {
4915 if(cp->tcps[i].ppt_data_first != NULL) {
4916 opj_free(cp->tcps[i].ppt_data_first);
4918 if(cp->tcps[i].tccps != NULL) {
4919 opj_free(cp->tcps[i].tccps);
4924 if(cp->ppm_data_first != NULL) {
4925 opj_free(cp->ppm_data_first);
4927 if(cp->tileno != NULL) {
4928 opj_free(cp->tileno);
4930 if(cp->comment != NULL) {
4931 opj_free(cp->comment);
4939 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
4940 if(j2k && parameters) {
4941 /* create and initialize the coding parameters structure */
4942 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4943 cp->reduce = parameters->cp_reduce;
4944 cp->layer = parameters->cp_layer;
4945 cp->limit_decoding = parameters->cp_limit_decoding;
4948 cp->correct = parameters->jpwl_correct;
4949 cp->exp_comps = parameters->jpwl_exp_comps;
4950 cp->max_tiles = parameters->jpwl_max_tiles;
4951 #endif /* USE_JPWL */
4954 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
4959 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
4961 if(j2k && parameters) {
4962 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
4963 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
4966 j2k->m_cp.correct = parameters->jpwl_correct;
4967 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
4968 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
4969 #endif /* USE_JPWL */
4973 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
4974 opj_image_t *image = NULL;
4976 opj_common_ptr cinfo = j2k->cinfo;
4979 j2k->cstr_info = cstr_info;
4981 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
4983 /* create an empty image */
4984 image = opj_image_create0();
4987 j2k->state = J2K_STATE_MHSOC;
4990 opj_dec_mstabent_t *e;
4991 int id = cio_read(cio, 2);
4994 /* we try to honor JPWL correction power */
4995 if (j2k->cp->correct) {
4997 int orig_pos = cio_tell(cio);
5000 /* call the corrector */
5001 status = jpwl_correct(j2k);
5003 /* go back to where you were */
5004 cio_seek(cio, orig_pos - 2);
5006 /* re-read the marker */
5007 id = cio_read(cio, 2);
5009 /* check whether it begins with ff */
5010 if (id >> 8 != 0xff) {
5011 opj_event_msg(cinfo, EVT_ERROR,
5012 "JPWL: possible bad marker %x at %d\n",
5013 id, cio_tell(cio) - 2);
5015 opj_image_destroy(image);
5016 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
5019 /* we try to correct */
5021 cio_seek(cio, cio_tell(cio) - 2);
5022 cio_write(cio, id, 2);
5023 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
5024 "- setting marker to %x\n",
5029 #endif /* USE_JPWL */
5031 if (id >> 8 != 0xff) {
5032 opj_image_destroy(image);
5033 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
5036 e = j2k_dec_mstab_lookup(id);
5037 // Check if the marker is known
5038 if (!(j2k->state & e->states)) {
5039 opj_image_destroy(image);
5040 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
5043 // Check if the decoding is limited to the main header
5044 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
5045 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
5052 if (j2k->state & J2K_STATE_ERR)
5055 if (j2k->state == J2K_STATE_MT) {
5058 if (j2k->state == J2K_STATE_NEOC) {
5062 if (j2k->state == J2K_STATE_NEOC) {
5066 if (j2k->state != J2K_STATE_MT) {
5067 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
5073 * Read a JPT-stream and decode file
5076 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
5077 opj_image_t *image = NULL;
5078 opj_jpt_msg_header_t header;
5080 opj_common_ptr cinfo = j2k->cinfo;
5082 OPJ_ARG_NOT_USED(cstr_info);
5086 /* create an empty image */
5087 image = opj_image_create0();
5090 j2k->state = J2K_STATE_MHSOC;
5092 /* Initialize the header */
5093 jpt_init_msg_header(&header);
5094 /* Read the first header of the message */
5095 jpt_read_msg_header(cinfo, cio, &header);
5097 position = cio_tell(cio);
5098 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
5099 opj_image_destroy(image);
5100 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
5105 opj_dec_mstabent_t *e = NULL;
5108 if (!cio_numbytesleft(cio)) {
5112 /* data-bin read -> need to read a new header */
5113 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
5114 jpt_read_msg_header(cinfo, cio, &header);
5115 position = cio_tell(cio);
5116 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
5117 opj_image_destroy(image);
5118 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
5123 id = cio_read(cio, 2);
5124 if (id >> 8 != 0xff) {
5125 opj_image_destroy(image);
5126 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
5129 e = j2k_dec_mstab_lookup(id);
5130 if (!(j2k->state & e->states)) {
5131 opj_image_destroy(image);
5132 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
5138 if (j2k->state == J2K_STATE_MT) {
5141 if (j2k->state == J2K_STATE_NEOC) {
5145 if (j2k->state == J2K_STATE_NEOC) {
5149 if (j2k->state != J2K_STATE_MT) {
5150 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
5156 /* ----------------------------------------------------------------------- */
5157 /* J2K encoder interface */
5158 /* ----------------------------------------------------------------------- */
5160 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
5161 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
5168 opj_j2k_v2_t* j2k_create_compress_v2()
5170 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
5175 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
5177 l_j2k->m_is_decoder = 0;
5178 l_j2k->m_cp.m_is_decoder = 0;
5180 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
5181 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5186 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
5188 // validation list creation
5189 l_j2k->m_validation_list = opj_procedure_list_create();
5190 if (! l_j2k->m_validation_list) {
5195 // execution list creation
5196 l_j2k->m_procedure_list = opj_procedure_list_create();
5197 if (! l_j2k->m_procedure_list) {
5205 void j2k_destroy_compress(opj_j2k_t *j2k) {
5209 if(j2k->cp != NULL) {
5210 opj_cp_t *cp = j2k->cp;
5213 opj_free(cp->comment);
5216 opj_free(cp->matrice);
5218 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5219 opj_free(cp->tcps[tileno].tccps);
5228 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
5229 int i, j, tileno, numpocs_tile;
5230 opj_cp_t *cp = NULL;
5232 if(!j2k || !parameters || ! image) {
5236 /* create and initialize the coding parameters structure */
5237 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
5239 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
5242 /* set default values for cp */
5247 copy user encoding parameters
5249 cp->cinema = parameters->cp_cinema;
5250 cp->max_comp_size = parameters->max_comp_size;
5251 cp->rsiz = parameters->cp_rsiz;
5252 cp->disto_alloc = parameters->cp_disto_alloc;
5253 cp->fixed_alloc = parameters->cp_fixed_alloc;
5254 cp->fixed_quality = parameters->cp_fixed_quality;
5256 /* mod fixed_quality */
5257 if(parameters->cp_matrice) {
5258 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
5259 cp->matrice = (int *) opj_malloc(array_size);
5260 memcpy(cp->matrice, parameters->cp_matrice, array_size);
5264 cp->tdx = parameters->cp_tdx;
5265 cp->tdy = parameters->cp_tdy;
5268 cp->tx0 = parameters->cp_tx0;
5269 cp->ty0 = parameters->cp_ty0;
5271 /* comment string */
5272 if(parameters->cp_comment) {
5273 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
5275 strcpy(cp->comment, parameters->cp_comment);
5280 calculate other encoding parameters
5283 if (parameters->tile_size_on) {
5284 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
5285 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
5287 cp->tdx = image->x1 - cp->tx0;
5288 cp->tdy = image->y1 - cp->ty0;
5291 if(parameters->tp_on){
5292 cp->tp_flag = parameters->tp_flag;
5297 for(i=0;i<image->numcomps ;i++){
5298 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
5304 calculate JPWL encoding parameters
5307 if (parameters->jpwl_epc_on) {
5311 cp->epc_on = OPJ_TRUE;
5312 cp->info_on = OPJ_FALSE; /* no informative technique */
5315 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
5316 cp->epb_on = OPJ_TRUE;
5318 cp->hprot_MH = parameters->jpwl_hprot_MH;
5319 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
5320 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
5321 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
5323 /* if tile specs are not specified, copy MH specs */
5324 if (cp->hprot_TPH[0] == -1) {
5325 cp->hprot_TPH_tileno[0] = 0;
5326 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
5328 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
5329 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
5330 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
5331 cp->pprot[i] = parameters->jpwl_pprot[i];
5335 /* set ESD writing */
5336 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
5337 cp->esd_on = OPJ_TRUE;
5339 cp->sens_size = parameters->jpwl_sens_size;
5340 cp->sens_addr = parameters->jpwl_sens_addr;
5341 cp->sens_range = parameters->jpwl_sens_range;
5343 cp->sens_MH = parameters->jpwl_sens_MH;
5344 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
5345 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
5346 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
5350 /* always set RED writing to false: we are at the encoder */
5351 cp->red_on = OPJ_FALSE;
5354 cp->epc_on = OPJ_FALSE;
5356 #endif /* USE_JPWL */
5359 /* initialize the mutiple tiles */
5360 /* ---------------------------- */
5361 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
5363 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5364 opj_tcp_t *tcp = &cp->tcps[tileno];
5365 tcp->numlayers = parameters->tcp_numlayers;
5366 for (j = 0; j < tcp->numlayers; j++) {
5368 if (cp->fixed_quality) {
5369 tcp->distoratio[j] = parameters->tcp_distoratio[j];
5371 tcp->rates[j] = parameters->tcp_rates[j];
5373 if (cp->fixed_quality) { /* add fixed_quality */
5374 tcp->distoratio[j] = parameters->tcp_distoratio[j];
5376 tcp->rates[j] = parameters->tcp_rates[j];
5380 tcp->csty = parameters->csty;
5381 tcp->prg = parameters->prog_order;
5382 tcp->mct = parameters->tcp_mct;
5386 if (parameters->numpocs) {
5387 /* initialisation of POC */
5389 for (i = 0; i < parameters->numpocs; i++) {
5390 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
5391 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
5392 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
5393 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
5394 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
5395 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
5396 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
5397 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
5398 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
5402 tcp->numpocs = numpocs_tile -1 ;
5407 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
5409 for (i = 0; i < image->numcomps; i++) {
5410 opj_tccp_t *tccp = &tcp->tccps[i];
5411 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
5412 tccp->numresolutions = parameters->numresolution;
5413 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
5414 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
5415 tccp->cblksty = parameters->mode;
5416 tccp->qmfbid = parameters->irreversible ? 0 : 1;
5417 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
5419 if (i == parameters->roi_compno) {
5420 tccp->roishift = parameters->roi_shift;
5425 if(parameters->cp_cinema)
5427 //Precinct size for lowest frequency subband=128
5430 //Precinct size at all other resolutions = 256
5431 for (j = 1; j < tccp->numresolutions; j++) {
5436 if (parameters->csty & J2K_CCP_CSTY_PRT) {
5438 for (j = tccp->numresolutions - 1; j >= 0; j--) {
5439 if (p < parameters->res_spec) {
5441 if (parameters->prcw_init[p] < 1) {
5444 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
5447 if (parameters->prch_init[p] < 1) {
5450 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
5454 int res_spec = parameters->res_spec;
5455 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
5456 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
5458 if (size_prcw < 1) {
5461 tccp->prcw[j] = int_floorlog2(size_prcw);
5464 if (size_prch < 1) {
5467 tccp->prch[j] = int_floorlog2(size_prch);
5471 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
5474 for (j = 0; j < tccp->numresolutions; j++) {
5481 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
5486 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
5488 opj_cp_t *cp = NULL;
5490 opj_tcd_t *tcd = NULL; /* TCD component */
5498 j2k->cstr_info = cstr_info;
5501 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
5502 cstr_info->image_w = image->x1 - image->x0;
5503 cstr_info->image_h = image->y1 - image->y0;
5504 cstr_info->prog = (&cp->tcps[0])->prg;
5505 cstr_info->tw = cp->tw;
5506 cstr_info->th = cp->th;
5507 cstr_info->tile_x = cp->tdx; /* new version parser */
5508 cstr_info->tile_y = cp->tdy; /* new version parser */
5509 cstr_info->tile_Ox = cp->tx0; /* new version parser */
5510 cstr_info->tile_Oy = cp->ty0; /* new version parser */
5511 cstr_info->numcomps = image->numcomps;
5512 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
5513 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
5514 for (compno=0; compno < image->numcomps; compno++) {
5515 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
5517 cstr_info->D_max = 0.0; /* ADD Marcela */
5518 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
5519 cstr_info->maxmarknum = 100;
5520 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
5521 cstr_info->marknum = 0;
5531 for (compno = 1; compno < image->numcomps; compno++) {
5532 j2k_write_coc(j2k, compno);
5533 j2k_write_qcc(j2k, compno);
5537 for (compno = 0; compno < image->numcomps; compno++) {
5538 opj_tcp_t *tcp = &cp->tcps[0];
5539 if (tcp->tccps[compno].roishift)
5540 j2k_write_rgn(j2k, compno, 0);
5542 if (cp->comment != NULL) {
5546 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
5550 if (cp->cinema == CINEMA4K_24) {
5555 /* uncomment only for testing JPSEC marker writing */
5556 /* j2k_write_sec(j2k); */
5560 cstr_info->main_head_end = cio_tell(cio) - 1;
5563 /**** Main Header ENDS here ***/
5565 /* create the tile encoder */
5566 tcd = tcd_create(j2k->cinfo);
5568 /* encode each tile */
5569 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5573 int acc_pack_num = 0;
5577 opj_tcp_t *tcp = &cp->tcps[tileno];
5578 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
5580 j2k->curtileno = tileno;
5581 j2k->cur_tp_num = 0;
5582 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
5583 /* initialisation before tile encoding */
5585 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
5587 tcd_init_encode(tcd, image, cp, j2k->curtileno);
5592 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
5593 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
5594 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
5595 cstr_info->tile[j2k->curtileno].marknum = 0;
5599 for(pino = 0; pino <= tcp->numpocs; pino++) {
5603 /*Get number of tile parts*/
5604 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
5605 tcd->tp_pos = cp->tp_pos;
5607 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
5608 j2k->tp_num = tilepartno;
5611 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
5612 cio_tell(cio) + j2k->pos_correction;
5616 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
5617 for (compno = 1; compno < image->numcomps; compno++) {
5618 j2k_write_coc(j2k, compno);
5619 j2k_write_qcc(j2k, compno);
5621 if (cp->tcps[tileno].numpocs) {
5628 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5629 cio_tell(cio) + j2k->pos_correction + 1;
5632 j2k_write_sod(j2k, tcd);
5636 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
5637 cio_tell(cio) + j2k->pos_correction - 1;
5638 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
5640 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
5641 cstr_info->packno - acc_pack_num;
5642 acc_pack_num = cstr_info->packno;
5650 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
5655 if (tile->PPT) { // BAD PPT !!!
5658 PPT_file=fopen("PPT","rb");
5659 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
5660 for (i=0;i<tile->len_ppt;i++) {
5662 fread(&elmt, 1, 1, PPT_file);
5663 fwrite(&elmt,1,1,f);
5672 /* destroy the tile encoder */
5673 tcd_free_encode(tcd);
5676 opj_free(j2k->cur_totnum_tp);
5681 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
5683 /* The following adjustment is done to adjust the codestream size */
5684 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5685 /* the first bunch of bytes is not in the codestream */
5686 cstr_info->codestream_size -= cstr_info->main_head_start;
5692 preparation of JPWL marker segments
5696 /* encode according to JPWL */
5697 jpwl_encode(j2k, cio, image);
5700 #endif /* USE_JPWL */
5705 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
5710 /* expand the list? */
5711 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
5712 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
5713 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
5716 /* add the marker */
5717 cstr_info->marker[cstr_info->marknum].type = type;
5718 cstr_info->marker[cstr_info->marknum].pos = pos;
5719 cstr_info->marker[cstr_info->marknum].len = len;
5720 cstr_info->marknum++;
5724 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) {
5729 /* expand the list? */
5730 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
5731 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
5732 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
5735 /* add the marker */
5736 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
5737 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
5738 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
5739 cstr_index->marknum++;
5743 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
5746 opj_marker_info_t *marker;
5751 /* expand the list? */
5752 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
5753 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
5754 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
5757 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
5759 /* add the marker */
5760 marker->type = type;
5763 cstr_info->tile[tileno].marknum++;
5770 * -----------------------------------------------------------------------
5771 * -----------------------------------------------------------------------
5772 * -----------------------------------------------------------------------
5776 * Ends the decompression procedures and possibiliy add data to be read after the
5779 opj_bool j2k_end_decompress(
5780 opj_j2k_v2_t *p_j2k,
5781 opj_stream_private_t *p_stream,
5782 opj_event_mgr_t * p_manager)
5788 * Reads a jpeg2000 codestream header structure.
5791 * @param p_stream the stream to read data from.
5792 * @param p_j2k the jpeg2000 codec.
5793 * @param p_manager the user event manager.
5795 * @return true if the box is valid.
5797 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
5798 opj_j2k_v2_t* p_j2k,
5799 opj_image_t** p_image,
5800 struct opj_event_mgr* p_manager )
5803 assert(p_j2k != 00);
5804 assert(p_stream != 00);
5805 assert(p_manager != 00);
5807 /* create an empty image header */
5808 p_j2k->m_image = opj_image_create0();
5809 if (! p_j2k->m_image) {
5813 /* customization of the validation */
5814 j2k_setup_decoding_validation(p_j2k);
5816 /* validation of the parameters codec */
5817 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
5818 opj_image_destroy(p_j2k->m_image);
5819 p_j2k->m_image = NULL;
5823 /* customization of the encoding */
5824 j2k_setup_header_reading(p_j2k);
5827 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
5828 opj_image_destroy(p_j2k->m_image);
5829 p_j2k->m_image = NULL;
5833 *p_image = opj_image_create0();
5838 if (! j2k_copy_img_header(p_j2k, p_image)){
5839 opj_image_destroy(p_j2k->m_image);
5840 p_j2k->m_image = NULL;
5848 opj_bool j2k_copy_img_header(opj_j2k_v2_t* p_j2k, opj_image_t** p_image)
5853 assert(p_j2k != 00);
5855 (*p_image)->x0 = p_j2k->m_image->x0;
5856 (*p_image)->y0 = p_j2k->m_image->y0;
5857 (*p_image)->x1 = p_j2k->m_image->x1;
5858 (*p_image)->y1 = p_j2k->m_image->y1;
5860 (*p_image)->numcomps = p_j2k->m_image->numcomps;
5861 (*p_image)->comps = (opj_image_comp_t*)opj_malloc((*p_image)->numcomps * sizeof(opj_image_comp_t));
5862 if (!(*p_image)->comps)
5864 for (compno=0; compno < (*p_image)->numcomps; compno++){
5865 memcpy( &((*p_image)->comps[compno]),
5866 &(p_j2k->m_image->comps[compno]),
5867 sizeof(opj_image_comp_t));
5870 (*p_image)->color_space = p_j2k->m_image->color_space;
5871 (*p_image)->icc_profile_len = p_j2k->m_image->icc_profile_len;
5872 if (!(*p_image)->icc_profile_len) {
5874 (*p_image)->icc_profile_buf = (unsigned char*)opj_malloc((*p_image)->icc_profile_len);
5875 if (!(*p_image)->icc_profile_buf)
5877 memcpy( (*p_image)->icc_profile_buf,
5878 p_j2k->m_image->icc_profile_buf,
5879 p_j2k->m_image->icc_profile_len);
5882 (*p_image)->icc_profile_buf = NULL;
5890 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
5892 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
5895 assert(p_j2k != 00);
5897 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_read_header_procedure);
5899 /* DEVELOPER CORNER, add your custom procedures */
5900 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_copy_default_tcp_and_create_tcd);
5905 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
5906 * are valid. Developpers wanting to extend the library can add their own validation procedures.
5908 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
5911 assert(p_j2k != 00);
5913 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_decoder);
5914 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_decoding_validation);
5915 /* DEVELOPER CORNER, add your custom validation procedure */
5920 * Builds the cp decoder parameters to use to decode tile.
5922 opj_bool j2k_build_decoder (
5923 opj_j2k_v2_t * p_j2k,
5924 opj_stream_private_t *p_stream,
5925 opj_event_mgr_t * p_manager
5928 // add here initialization of cp
5929 // copy paste of setup_decoder
5934 * The default decoding validation procedure without any extension.
5936 * @param p_j2k the jpeg2000 codec to validate.
5937 * @param p_stream the input stream to validate.
5938 * @param p_manager the user event manager.
5940 * @return true if the parameters are correct.
5942 opj_bool j2k_decoding_validation (
5943 opj_j2k_v2_t *p_j2k,
5944 opj_stream_private_t *p_stream,
5945 opj_event_mgr_t * p_manager
5948 opj_bool l_is_valid = OPJ_TRUE;
5951 assert(p_j2k != 00);
5952 assert(p_stream != 00);
5953 assert(p_manager != 00);
5956 /* STATE checking */
5957 /* make sure the state is at 0 */
5959 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
5961 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
5963 /* POINTER validation */
5964 /* make sure a p_j2k codec is present */
5965 /* make sure a procedure list is present */
5966 l_is_valid &= (p_j2k->m_procedure_list != 00);
5967 /* make sure a validation list is present */
5968 l_is_valid &= (p_j2k->m_validation_list != 00);
5970 /* PARAMETER VALIDATION */
5974 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
5975 struct opj_stream_private *p_stream,
5976 struct opj_event_mgr * p_manager)
5978 OPJ_UINT32 l_current_marker;
5979 OPJ_UINT32 l_marker_size;
5980 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
5983 assert(p_stream != 00);
5984 assert(p_j2k != 00);
5985 assert(p_manager != 00);
5987 /* We enter in the main header */
5988 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
5990 /* Try to read the SOC marker, the codestream must begin with SOC marker */
5991 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
5992 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
5996 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
5997 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5998 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6002 /* Read 2 bytes as the new marker ID */
6003 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6005 /* Try to read until the SOT is detected */
6006 while (l_current_marker != J2K_MS_SOT) {
6008 /* Check if the current marker ID is valid */
6009 if (l_current_marker < 0xff00) {
6010 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
6014 /* Get the marker handler from the marker ID */
6015 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6017 /* Manage case where marker is unknown */
6018 if (l_marker_handler->id == J2K_MS_UNK) {
6019 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
6020 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
6024 if (l_current_marker == J2K_MS_SOT)
6025 break; /* SOT marker is detected main header is completely read */
6026 else /* Get the marker handler from the marker ID */
6027 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6030 /* Check if the marker is known and if it is the right place to find it */
6031 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6032 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6036 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6037 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6038 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6042 /* read 2 bytes as the marker size */
6043 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6044 l_marker_size -= 2; // Subtract the size of the marker ID already read
6046 /* Check if the marker size is compatible with the header data size */
6047 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6048 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
6049 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
6050 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
6053 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6056 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6057 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) {
6058 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6062 /* Read the marker segment with the correct marker handler */
6063 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6064 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
6068 /* Add the marker to the codestream index*/
6069 j2k_add_mhmarker_v2(p_j2k->cstr_index,
6070 l_marker_handler->id,
6071 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6072 l_marker_size + 4 );
6074 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6075 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6076 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6080 /* read 2 bytes as the new marker ID */
6081 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6084 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decode.\n");
6086 /* Position of the last element if the main header */
6087 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
6089 /* Next step: read a tile-part header */
6090 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6096 * Excutes the given procedures on the given codec.
6098 * @param p_procedure_list the list of procedures to execute
6099 * @param p_j2k the jpeg2000 codec to execute the procedures on.
6100 * @param p_stream the stream to execute the procedures on.
6101 * @param p_manager the user manager.
6103 * @return true if all the procedures were successfully executed.
6105 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
6106 opj_procedure_list_t * p_procedure_list,
6107 opj_stream_private_t *p_stream,
6108 opj_event_mgr_t * p_manager )
6110 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
6111 opj_bool l_result = OPJ_TRUE;
6112 OPJ_UINT32 l_nb_proc, i;
6115 assert(p_procedure_list != 00);
6116 assert(p_j2k != 00);
6117 assert(p_stream != 00);
6118 assert(p_manager != 00);
6121 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
6122 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);
6124 for (i=0;i<l_nb_proc;++i) {
6125 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
6129 // and clear the procedure list at the end.
6130 opj_procedure_list_clear(p_procedure_list);
6135 opj_bool j2k_copy_default_tcp_and_create_tcd
6137 opj_j2k_v2_t * p_j2k,
6138 opj_stream_private_t *p_stream,
6139 opj_event_mgr_t * p_manager
6142 opj_tcp_v2_t * l_tcp = 00;
6143 opj_tcp_v2_t * l_default_tcp = 00;
6144 OPJ_UINT32 l_nb_tiles;
6146 opj_tccp_t *l_current_tccp = 00;
6147 OPJ_UINT32 l_tccp_size;
6148 OPJ_UINT32 l_mct_size;
6149 opj_image_t * l_image;
6150 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
6151 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
6152 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
6153 OPJ_UINT32 l_offset;
6156 assert(p_j2k != 00);
6157 assert(p_stream != 00);
6158 assert(p_manager != 00);
6160 l_image = p_j2k->m_image;
6161 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6162 l_tcp = p_j2k->m_cp.tcps;
6163 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
6164 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
6165 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
6168 for (i=0; i<l_nb_tiles; ++i) {
6169 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
6170 l_current_tccp = l_tcp->tccps;
6171 /*Copy default coding parameters into the current tile coding parameters*/
6172 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
6173 /* Initialize some values of the current tile coding parameters*/
6175 l_tcp->ppt_data = 00;
6176 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
6177 l_tcp->tccps = l_current_tccp;
6179 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
6180 if (l_default_tcp->m_mct_decoding_matrix) {
6181 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6182 if (! l_tcp->m_mct_decoding_matrix ) {
6185 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
6188 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
6189 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
6190 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
6191 if (! l_tcp->m_mct_records) {
6194 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
6196 /* Copy the mct record data from dflt_tile_cp to the current tile*/
6197 l_src_mct_rec = l_default_tcp->m_mct_records;
6198 l_dest_mct_rec = l_tcp->m_mct_records;
6200 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
6202 if (l_src_mct_rec->m_data) {
6204 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
6205 if(! l_dest_mct_rec->m_data) {
6208 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
6215 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
6216 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
6217 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
6218 if (! l_tcp->m_mcc_records) {
6221 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
6223 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
6224 l_src_mcc_rec = l_default_tcp->m_mcc_records;
6225 l_dest_mcc_rec = l_tcp->m_mcc_records;
6227 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
6229 if (l_src_mcc_rec->m_decorrelation_array) {
6230 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
6231 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
6234 if (l_src_mcc_rec->m_offset_array) {
6235 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
6236 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
6243 /* Copy all the dflt_tile_compo_cp to the current tile cp */
6244 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
6246 /* Move to next tile cp*/
6250 /* Create the current tile decoder*/
6251 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); // FIXME why a cast ?
6252 if (! p_j2k->m_tcd ) {
6256 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
6257 tcd_destroy_v2(p_j2k->m_tcd);
6259 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6267 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
6268 * with the marker value.
6269 * @param p_id Marker value to look up
6271 * @return the handler associated with the id.
6273 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (const OPJ_UINT32 p_id)
6275 const opj_dec_memory_marker_handler_t *e;
6276 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
6277 if (e->id == p_id) {
6278 break; // we find a handler corresponding to the marker ID
6286 * Destroys a jpeg2000 codec.
6288 * @param p_j2k the jpeg20000 structure to destroy.
6290 void j2k_destroy (opj_j2k_v2_t *p_j2k)
6296 if (p_j2k->m_is_decoder) {
6298 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
6299 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
6300 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
6301 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
6304 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
6305 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
6306 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
6307 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
6312 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
6313 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
6314 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
6317 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6318 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
6319 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
6320 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
6323 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6324 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
6325 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
6326 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
6330 tcd_destroy_v2(p_j2k->m_tcd);
6332 j2k_cp_destroy(&(p_j2k->m_cp));
6333 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
6335 opj_procedure_list_destroy(p_j2k->m_procedure_list);
6336 p_j2k->m_procedure_list = 00;
6338 opj_procedure_list_destroy(p_j2k->m_validation_list);
6339 p_j2k->m_procedure_list = 00;
6341 j2k_destroy_cstr_index(p_j2k->cstr_index);
6342 p_j2k->cstr_index = NULL;
6344 opj_image_destroy(p_j2k->m_image);
6345 p_j2k->m_image = NULL;
6350 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
6354 if (p_cstr_ind->marker) {
6355 opj_free(p_cstr_ind->marker);
6356 p_cstr_ind->marker = NULL;
6359 if (p_cstr_ind->tile_index) {
6360 // FIXME not used for the moment
6363 opj_free(p_cstr_ind);
6370 * Destroys a tile coding parameter structure.
6372 * @param p_tcp the tile coding parameter to destroy.
6374 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
6380 if (p_tcp->ppt_buffer != 00) {
6381 opj_free(p_tcp->ppt_buffer);
6382 p_tcp->ppt_buffer = 00;
6385 if (p_tcp->tccps != 00) {
6386 opj_free(p_tcp->tccps);
6390 if (p_tcp->m_mct_coding_matrix != 00) {
6391 opj_free(p_tcp->m_mct_coding_matrix);
6392 p_tcp->m_mct_coding_matrix = 00;
6395 if (p_tcp->m_mct_decoding_matrix != 00) {
6396 opj_free(p_tcp->m_mct_decoding_matrix);
6397 p_tcp->m_mct_decoding_matrix = 00;
6400 if (p_tcp->m_mcc_records) {
6401 opj_free(p_tcp->m_mcc_records);
6402 p_tcp->m_mcc_records = 00;
6403 p_tcp->m_nb_max_mcc_records = 0;
6404 p_tcp->m_nb_mcc_records = 0;
6407 if (p_tcp->m_mct_records) {
6408 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
6411 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
6412 if (l_mct_data->m_data) {
6413 opj_free(l_mct_data->m_data);
6414 l_mct_data->m_data = 00;
6420 opj_free(p_tcp->m_mct_records);
6421 p_tcp->m_mct_records = 00;
6424 if (p_tcp->mct_norms != 00) {
6425 opj_free(p_tcp->mct_norms);
6426 p_tcp->mct_norms = 00;
6429 if (p_tcp->m_data) {
6430 opj_free(p_tcp->m_data);
6437 * Destroys a coding parameter structure.
6439 * @param p_cp the coding parameter to destroy.
6441 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
6443 OPJ_UINT32 l_nb_tiles;
6444 opj_tcp_v2_t * l_current_tile = 00;
6455 l_current_tile = p_cp->tcps;
6456 l_nb_tiles = p_cp->th * p_cp->tw;
6459 (i = 0; i < l_nb_tiles; ++i)
6461 j2k_tcp_destroy(l_current_tile);
6464 opj_free(p_cp->tcps);
6468 (p_cp->ppm_buffer != 00)
6470 opj_free(p_cp->ppm_buffer);
6471 p_cp->ppm_buffer = 00;
6474 (p_cp->comment != 00)
6476 opj_free(p_cp->comment);
6480 (! p_cp->m_is_decoder)
6483 (p_cp->m_specific_param.m_enc.m_matrice)
6485 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
6486 p_cp->m_specific_param.m_enc.m_matrice = 00;
6494 * Reads a tile header.
6495 * @param p_j2k the jpeg2000 codec.
6496 * @param p_stream the stream to write data to.
6497 * @param p_manager the user event manager.
6499 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
6500 OPJ_UINT32 * p_tile_index,
6501 OPJ_UINT32 * p_data_size,
6502 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
6503 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
6504 OPJ_UINT32 * p_nb_comps,
6506 opj_stream_private_t *p_stream,
6507 opj_event_mgr_t * p_manager )
6509 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
6510 OPJ_UINT32 l_marker_size;
6511 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6512 opj_tcp_v2_t * l_tcp = NULL;
6513 OPJ_UINT32 l_nb_tiles;
6516 assert(p_stream != 00);
6517 assert(p_j2k != 00);
6518 assert(p_manager != 00);
6520 /* Reach the End Of Codestream ?*/
6521 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
6522 l_current_marker = J2K_MS_EOC;
6524 /* We need to encounter a SOT marker (a new tile-part header) */
6525 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
6529 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
6530 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
6532 /* Try to read until the Start Of Data is detected */
6533 while (l_current_marker != J2K_MS_SOD) {
6535 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6536 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6537 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6541 /* Read 2 bytes from the buffer as the marker size */
6542 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6544 /* Why this condition? FIXME */
6545 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
6546 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
6548 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
6550 /* Get the marker handler from the marker ID */
6551 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6553 /* Check if the marker is known and if it is the right place to find it */
6554 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6555 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6558 /* FIXME manage case of unknown marker as in the main header ? */
6560 /* Check if the marker size is compatible with the header data size */
6561 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6562 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
6563 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
6564 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
6567 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6570 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6571 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) {
6572 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6576 /* Read the marker segment with the correct marker handler */
6577 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6578 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
6582 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
6583 /* Skip the rest of the tile part header*/
6584 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) {
6585 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6588 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
6591 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6592 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6593 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6596 /* Read 2 bytes from the buffer as the new marker ID */
6597 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6601 /* If we didn't skip data before, we need to read the SOD marker*/
6602 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
6603 /* Try to read the SOD marker and skip data ? FIXME */
6604 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
6608 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
6609 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6610 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6611 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6615 /* Read 2 bytes from buffer as the new marker ID */
6616 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6620 /* Indicate we will try to read a new tile-part header*/
6621 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
6622 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
6623 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6625 /* Try to read 2 bytes (the next marker ID) 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,2,p_manager) != 2) {
6627 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6631 /* Read 2 bytes from buffer as the new marker ID */
6632 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6636 /* Current marker is the EOC marker ?*/
6637 if (l_current_marker == J2K_MS_EOC) {
6638 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
6639 p_j2k->m_current_tile_number = 0;
6640 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
6645 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
6646 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
6647 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6649 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
6650 ++p_j2k->m_current_tile_number;
6654 if (p_j2k->m_current_tile_number == l_nb_tiles) {
6655 *p_go_on = OPJ_FALSE;
6661 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
6662 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6666 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
6667 p_j2k->m_current_tile_number +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
6669 *p_tile_index = p_j2k->m_current_tile_number;
6670 *p_go_on = OPJ_TRUE;
6671 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
6672 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
6673 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
6674 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
6675 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
6676 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
6678 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;// FIXME J2K_DEC_STATE_DATA;
6684 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
6685 OPJ_UINT32 p_tile_index,
6687 OPJ_UINT32 p_data_size,
6688 opj_stream_private_t *p_stream,
6689 opj_event_mgr_t * p_manager )
6691 OPJ_UINT32 l_current_marker;
6692 OPJ_BYTE l_data [2];
6693 opj_tcp_v2_t * l_tcp;
6696 assert(p_stream != 00);
6697 assert(p_j2k != 00);
6698 assert(p_manager != 00);
6700 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
6701 || (p_tile_index != p_j2k->m_current_tile_number) ) {
6705 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
6706 if (! l_tcp->m_data) {
6707 j2k_tcp_destroy(&(p_j2k->m_cp.tcps[p_tile_index]));
6711 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
6715 p_j2k->cstr_index) ) {
6716 j2k_tcp_destroy(l_tcp);
6717 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;//FIXME J2K_DEC_STATE_ERR;
6721 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
6725 j2k_tcp_destroy(l_tcp);
6726 p_j2k->m_tcd->tcp = 0;
6728 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
6729 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));// FIXME J2K_DEC_STATE_DATA);
6731 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ //FIXME J2K_DEC_STATE_EOC)
6732 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
6733 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6737 opj_read_bytes(l_data,&l_current_marker,2);
6739 if (l_current_marker == J2K_MS_EOC) {
6740 p_j2k->m_current_tile_number = 0;
6741 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;//FIXME J2K_DEC_STATE_EOC;
6743 else if (l_current_marker != J2K_MS_SOT)
6745 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
6754 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
6756 OPJ_UINT32 i,j,k = 0;
6757 OPJ_UINT32 l_width,l_height,l_offset_x,l_offset_y;
6758 opj_image_comp_t * l_img_comp = 00;
6759 opj_tcd_tilecomp_v2_t * l_tilec = 00;
6760 opj_image_t * l_image = 00;
6761 OPJ_UINT32 l_size_comp, l_remaining;
6762 OPJ_UINT32 l_dest_stride;
6763 OPJ_INT32 * l_dest_ptr;
6764 opj_tcd_resolution_v2_t* l_res= 00;
6767 l_tilec = p_tcd->tcd_image->tiles->comps;
6768 l_image = p_tcd->image;
6769 l_img_comp = l_image->comps;
6771 for (i=0;i<p_tcd->image->numcomps;++i) {
6773 if (!l_img_comp->data) {
6775 l_img_comp->data = (OPJ_INT32*) opj_malloc(l_img_comp->w * l_img_comp->h * sizeof(OPJ_INT32));
6776 if (! l_img_comp->data) {
6779 memset(l_img_comp->data,0,l_img_comp->w * l_img_comp->h * sizeof(OPJ_INT32));
6782 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
6783 l_remaining = l_img_comp->prec & 7; /* (%8) */
6784 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
6790 if (l_size_comp == 3) {
6794 l_width = (l_res->x1 - l_res->x0);
6795 l_height = (l_res->y1 - l_res->y0);
6797 l_dest_stride = (l_img_comp->w) - l_width;
6799 l_offset_x = int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
6800 l_offset_y = int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
6801 l_dest_ptr = l_img_comp->data + (l_res->x0 - l_offset_x) + (l_res->y0 - l_offset_y) * l_img_comp->w;
6803 switch (l_size_comp) {
6806 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
6807 if (l_img_comp->sgnd) {
6808 for (j=0;j<l_height;++j) {
6809 for (k=0;k<l_width;++k) {
6810 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
6813 l_dest_ptr += l_dest_stride;
6817 for (j=0;j<l_height;++j) {
6818 for (k=0;k<l_width;++k) {
6819 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
6822 l_dest_ptr += l_dest_stride;
6826 p_data = (OPJ_BYTE*) l_src_ptr;
6831 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
6833 if (l_img_comp->sgnd) {
6834 for (j=0;j<l_height;++j) {
6835 for (k=0;k<l_width;++k) {
6836 *(l_dest_ptr++) = *(l_src_ptr++);
6839 l_dest_ptr += l_dest_stride;
6843 for (j=0;j<l_height;++j) {
6844 for (k=0;k<l_width;++k) {
6845 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
6848 l_dest_ptr += l_dest_stride;
6852 p_data = (OPJ_BYTE*) l_src_ptr;
6857 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
6858 for (j=0;j<l_height;++j) {
6859 for (k=0;k<l_width;++k) {
6860 *(l_dest_ptr++) = (*(l_src_ptr++));
6863 l_dest_ptr += l_dest_stride;
6866 p_data = (OPJ_BYTE*) l_src_ptr;
6878 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
6880 * @param p_j2k the jpeg2000 codec.
6881 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
6882 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
6883 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
6884 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
6885 * @param p_manager the user event manager
6887 * @return true if the area could be set.
6889 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
6890 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
6891 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
6892 struct opj_event_mgr * p_manager )
6894 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
6896 /* Check if we are read the main header */
6897 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { // FIXME J2K_DEC_STATE_TPHSOT)
6898 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
6903 /* Check if the positions provided by the user are correct */
6906 if (p_start_x > l_cp->tx0 + l_cp->tw * l_cp->tdx) {
6907 opj_event_msg_v2(p_manager, EVT_ERROR,
6908 "Left position of the decoded area (ROI_x0=%d) is outside the tile area (XTOsiz + nb_tw x XTsiz=%d).\n",
6909 p_start_x, l_cp->tx0 + l_cp->tw * l_cp->tdx);
6912 else if (p_start_x < l_cp->tx0){
6913 opj_event_msg_v2(p_manager, EVT_WARNING,
6914 "Left position of the decoded area (ROI_x0=%d) is outside the tile area (XTOsiz=%d).\n",
6915 p_start_x, l_cp->tx0);
6916 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
6919 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
6922 if (p_start_y > l_cp->ty0 + l_cp->th * l_cp->tdy){
6923 opj_event_msg_v2(p_manager, EVT_ERROR,
6924 "Up position of the decoded area (ROI_y0=%d) is outside the tile area (YTOsiz + nb_th x YTsiz=%d).\n",
6925 p_start_y, l_cp->ty0 + l_cp->th * l_cp->tdy);
6928 else if (p_start_y < l_cp->ty0){
6929 opj_event_msg_v2(p_manager, EVT_WARNING,
6930 "Up position of the decoded area (ROI_y0=%d) is outside the tile area (YTOsiz=%d).\n",
6931 p_start_y, l_cp->ty0);
6932 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
6935 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
6938 if (p_end_x < l_cp->tx0) {
6939 opj_event_msg_v2(p_manager, EVT_ERROR,
6940 "Right position of the decoded area (ROI_x1=%d) is outside the tile area (XTOsiz=%d).\n",
6941 p_end_x, l_cp->tx0);
6944 else if (p_end_x > l_cp->tx0 + l_cp->tw * l_cp->tdx) {
6945 opj_event_msg_v2(p_manager, EVT_WARNING,
6946 "Right position of the decoded area (ROI_x1=%d) is outside the tile area (XTOsiz + nb_tw x XTsiz=%d).\n",
6947 p_end_x, l_cp->tx0 + l_cp->tw * l_cp->tdx);
6948 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; // FIXME (-1) ???
6951 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
6954 if (p_end_y < l_cp->ty0) {
6955 opj_event_msg_v2(p_manager, EVT_ERROR,
6956 "Right position of the decoded area (ROI_y1=%d) is outside the tile area (YTOsiz=%d).\n",
6957 p_end_y, l_cp->ty0);
6960 if (p_end_y > l_cp->ty0 + l_cp->th * l_cp->tdy){
6961 opj_event_msg_v2(p_manager, EVT_WARNING,
6962 "Bottom position of the decoded area (ROI_y1=%d) is outside the tile area (YTOsiz + nb_th x YTsiz=%d).\n",
6963 p_end_y, l_cp->ty0 + l_cp->th * l_cp->tdy);
6964 p_j2k->m_specific_param.m_decoder.m_start_tile_y = l_cp->th; // FIXME (-1) ???
6967 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
6970 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
6976 /* ----------------------------------------------------------------------- */
6977 /* J2K / JPT decoder interface */
6978 /* ----------------------------------------------------------------------- */
6980 * Creates a J2K decompression structure.
6982 * @return a handle to a J2K decompressor if successful, NULL otherwise.
6984 opj_j2k_v2_t* j2k_create_decompress_v2()
6986 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6990 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6992 l_j2k->m_is_decoder = 1;
6993 l_j2k->m_cp.m_is_decoder = 1;
6995 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
6996 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
7000 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
7002 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7003 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
7008 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
7010 /* codestream index creation */
7011 l_j2k->cstr_index = j2k_create_cstr_index();
7013 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
7014 if (!l_j2k->cstr_index){
7019 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
7022 /* validation list creation */
7023 l_j2k->m_validation_list = opj_procedure_list_create();
7024 if (! l_j2k->m_validation_list) {
7029 /* execution list creation */
7030 l_j2k->m_procedure_list = opj_procedure_list_create();
7031 if (! l_j2k->m_procedure_list) {
7040 opj_codestream_index_t* j2k_create_cstr_index(void)
7042 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
7043 opj_calloc(1,sizeof(opj_codestream_index_t));
7047 cstr_index->maxmarknum = 100;
7048 cstr_index->marknum = 0;
7049 cstr_index->marker = (opj_marker_info_t*)
7050 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
7051 if (!cstr_index-> marker)
7054 cstr_index->tile_index = NULL;
7060 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
7061 * @param p_header_data the data contained in the COM box.
7062 * @param p_j2k the jpeg2000 codec.
7063 * @param p_header_size the size of the data contained in the COM marker.
7064 * @param p_manager the user event manager.
7066 opj_bool j2k_read_SPCod_SPCoc(
7067 opj_j2k_v2_t *p_j2k,
7069 OPJ_BYTE * p_header_data,
7070 OPJ_UINT32 * p_header_size,
7071 struct opj_event_mgr * p_manager
7074 OPJ_UINT32 i, l_tmp;
7075 opj_cp_v2_t *l_cp = NULL;
7076 opj_tcp_v2_t *l_tcp = NULL;
7077 opj_tccp_t *l_tccp = NULL;
7078 OPJ_BYTE * l_current_ptr = NULL;
7081 assert(p_j2k != 00);
7082 assert(p_manager != 00);
7083 assert(p_header_data != 00);
7085 l_cp = &(p_j2k->m_cp);
7086 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
7087 &l_cp->tcps[p_j2k->m_current_tile_number] :
7088 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7090 /* precondition again */
7091 assert(compno < p_j2k->m_image->numcomps);
7093 l_tccp = &l_tcp->tccps[compno];
7094 l_current_ptr = p_header_data;
7096 /* make sure room is sufficient */
7097 if (*p_header_size < 5) {
7098 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
7102 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
7103 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
7106 /* If user wants to remove more resolutions than the codestream contains, return error */
7107 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
7108 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
7109 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
7110 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;// FIXME J2K_DEC_STATE_ERR;
7114 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
7118 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
7122 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
7125 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
7128 *p_header_size = *p_header_size - 5;
7130 /* use custom precinct size ? */
7131 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
7132 if (*p_header_size < l_tccp->numresolutions) {
7133 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
7137 for (i = 0; i < l_tccp->numresolutions; ++i) {
7138 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
7140 l_tccp->prcw[i] = l_tmp & 0xf;
7141 l_tccp->prch[i] = l_tmp >> 4;
7144 *p_header_size = *p_header_size - l_tccp->numresolutions;
7147 /* set default size for the precinct width and height */
7148 for (i = 0; i < l_tccp->numresolutions; ++i) {
7149 l_tccp->prcw[i] = 15;
7150 l_tccp->prch[i] = 15;
7154 #ifdef WIP_REMOVE_MSD
7156 if (p_j2k->cstr_info && compno == 0) {
7157 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
7159 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
7160 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
7161 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
7162 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
7163 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
7166 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
7167 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
7176 * Copies the tile component parameters of all the component from the first tile component.
7178 * @param p_j2k the J2k codec.
7180 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
7184 opj_cp_v2_t *l_cp = NULL;
7185 opj_tcp_v2_t *l_tcp = NULL;
7186 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
7187 OPJ_UINT32 l_prc_size;
7190 assert(p_j2k != 00);
7192 l_cp = &(p_j2k->m_cp);
7193 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
7194 &l_cp->tcps[p_j2k->m_current_tile_number] :
7195 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7197 l_ref_tccp = &l_tcp->tccps[0];
7198 l_copied_tccp = l_ref_tccp + 1;
7199 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
7201 for (i=1; i<p_j2k->m_image->numcomps; ++i) {
7202 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
7203 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
7204 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
7205 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
7206 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
7207 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
7208 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
7214 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
7216 * @param p_comp_no the component being targeted.
7217 * @param p_header_data the data contained in the COM box.
7218 * @param p_j2k the jpeg2000 codec.
7219 * @param p_header_size the size of the data contained in the COM marker.
7220 * @param p_manager the user event manager.
7222 opj_bool j2k_read_SQcd_SQcc(
7223 opj_j2k_v2_t *p_j2k,
7224 OPJ_UINT32 p_comp_no,
7225 OPJ_BYTE* p_header_data,
7226 OPJ_UINT32 * p_header_size,
7227 struct opj_event_mgr * p_manager
7231 OPJ_UINT32 l_band_no;
7232 opj_cp_v2_t *l_cp = 00;
7233 opj_tcp_v2_t *l_tcp = 00;
7234 opj_tccp_t *l_tccp = 00;
7235 OPJ_BYTE * l_current_ptr = 00;
7236 OPJ_UINT32 l_tmp, l_num_band;
7239 assert(p_j2k != 00);
7240 assert(p_manager != 00);
7241 assert(p_header_data != 00);
7243 l_cp = &(p_j2k->m_cp);
7244 // come from tile part header or main header ?
7245 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
7246 &l_cp->tcps[p_j2k->m_current_tile_number] :
7247 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7249 // precondition again
7250 assert(p_comp_no < p_j2k->m_image->numcomps);
7252 l_tccp = &l_tcp->tccps[p_comp_no];
7253 l_current_ptr = p_header_data;
7255 if (*p_header_size < 1) {
7256 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
7259 *p_header_size -= 1;
7261 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
7264 l_tccp->qntsty = l_tmp & 0x1f;
7265 l_tccp->numgbits = l_tmp >> 5;
7266 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
7270 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
7272 (*p_header_size) / 2;
7274 if( l_num_band > J2K_MAXBANDS ) {
7275 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
7276 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limiting the number of elements stored to "
7277 "J2K_MAXBANDS (%d) and skip the other. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
7283 if (l_cp->correct) {
7285 /* if JPWL is on, we check whether there are too many subbands */
7286 if ((l_num_band < 0) || (l_num_band >= J2K_MAXBANDS)) {
7287 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
7288 "JPWL: bad number of subbands in Sqcx (%d)\n",
7291 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
7294 /* we try to correct */
7296 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
7297 "- setting number of bands to %d => HYPOTHESIS!!!\n",
7302 #endif /* USE_JPWL */
7304 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
7305 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
7306 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
7308 if (l_band_no < J2K_MAXBANDS){
7309 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
7310 l_tccp->stepsizes[l_band_no].mant = 0;
7313 *p_header_size = *p_header_size - l_num_band;
7316 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
7317 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
7319 if (l_band_no < J2K_MAXBANDS){
7320 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
7321 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
7324 *p_header_size = *p_header_size - 2*l_num_band;
7327 /* Add Antonin : if scalar_derived -> compute other stepsizes */
7328 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
7329 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
7330 l_tccp->stepsizes[l_band_no].expn =
7331 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
7332 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
7333 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
7341 * Copies the tile component parameters of all the component from the first tile component.
7343 * @param p_j2k the J2k codec.
7345 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
7348 opj_cp_v2_t *l_cp = NULL;
7349 opj_tcp_v2_t *l_tcp = NULL;
7350 opj_tccp_t *l_ref_tccp = NULL;
7351 opj_tccp_t *l_copied_tccp = NULL;
7355 assert(p_j2k != 00);
7357 l_cp = &(p_j2k->m_cp);
7358 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7359 &l_cp->tcps[p_j2k->m_current_tile_number] :
7360 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7362 l_ref_tccp = &l_tcp->tccps[0];
7363 l_copied_tccp = l_ref_tccp + 1;
7364 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
7366 for (i=1;i<p_j2k->m_image->numcomps;++i) {
7367 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
7368 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
7369 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
7375 * Dump some elements from the J2K decompression structure .
7377 *@param p_j2k the jpeg2000 codec.
7378 *@param flag flag to describe what elments are dump.
7379 *@param out_stream output stream where dump the elements.
7382 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
7384 /* Check if the flag is compatible with j2k file*/
7385 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
7386 fprintf(out_stream, "Wrong flag\n");
7390 /* Dump the image_header */
7391 if (flag & OPJ_IMG_INFO){
7393 j2k_dump_image_header(p_j2k->m_image, 0, out_stream);
7396 /* Dump the codestream info from main header */
7397 if (flag & OPJ_J2K_MH_INFO){
7398 j2k_dump_MH_info(p_j2k, out_stream);
7402 /* Dump the codestream info of the current tile */
7403 if (flag & OPJ_J2K_TH_INFO){
7407 /* Dump the codestream index from main header */
7408 if (flag & OPJ_J2K_MH_IND){
7409 j2k_dump_MH_index(p_j2k, out_stream);
7412 /* Dump the codestream index of the current tile */
7413 if (flag & OPJ_J2K_TH_IND){
7420 * Dump index elements of the codestream extract from the main header.
7422 *@param p_j2k the jpeg2000 codec.
7423 *@param out_stream output stream where dump the elements.
7426 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
7428 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
7429 OPJ_UINT32 it_marker;
7431 fprintf(out_stream, "Codestream index from main header: {\n");
7433 fprintf(out_stream, "\t Main header start position=%d\n\t Main header end position=%d\n",
7434 cstr_index->main_head_start, cstr_index->main_head_end);
7436 fprintf(out_stream, "\t Marker list: {\n");
7438 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
7439 fprintf(out_stream, "\t\t type=%#x, pos=%d, len=%d\n",
7440 cstr_index->marker[it_marker].type,
7441 cstr_index->marker[it_marker].pos,
7442 cstr_index->marker[it_marker].len );
7445 fprintf(out_stream, "\t }\n}\n");
7450 * Dump info elements of the codestream extract from the main header.
7452 *@param p_j2k the jpeg2000 codec.
7453 *@param out_stream output stream where dump the elements.
7456 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
7458 opj_tcp_v2_t * l_default_tile=NULL;
7460 fprintf(out_stream, "Codestream info from main header: {\n");
7462 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
7463 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
7464 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
7466 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7470 OPJ_INT32 numcomps = p_j2k->m_image->numcomps;
7472 fprintf(out_stream, "\t default tile {\n");
7473 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
7474 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
7475 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
7476 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
7478 for (compno = 0; compno < numcomps; compno++) {
7479 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
7480 OPJ_INT32 resno, bandno, numbands;
7483 fprintf(out_stream, "\t\t comp %d {\n", compno);
7484 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
7485 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
7486 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
7487 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
7488 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
7489 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
7491 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
7492 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
7493 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
7495 fprintf(out_stream, "\n");
7497 /* quantization style*/
7498 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
7499 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
7500 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
7501 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
7502 for (bandno = 0; bandno < numbands; bandno++) {
7503 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
7504 l_tccp->stepsizes[bandno].expn);
7506 fprintf(out_stream, "\n");
7509 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
7511 fprintf(out_stream, "\t\t }\n");
7512 } /*end of component of default tile*/
7513 fprintf(out_stream, "\t }\n"); /*end of default tile*/
7517 fprintf(out_stream, "}\n");
7522 * Dump an image header structure.
7524 *@param img_header the image header to dump.
7525 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
7526 *@param out_stream output stream where dump the elements.
7528 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
7533 fprintf(stdout, "[DEV] Dump a image_header struct {\n");
7537 fprintf(out_stream, "Image info {\n");
7538 tab[0] = '\t';tab[1] = '\0';
7541 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
7542 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
7543 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
7545 if (img_header->comps){
7547 for (compno = 0; compno < img_header->numcomps; compno++) {
7548 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
7549 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
7550 fprintf(out_stream,"%s}\n",tab);
7554 fprintf(out_stream, "}\n");
7558 * Dump a component image header structure.
7560 *@param comp_header the component image header to dump.
7561 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
7562 *@param out_stream output stream where dump the elements.
7564 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
7569 fprintf(stdout, "[DEV] Dump a image_comp_header struct {\n");
7572 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
7575 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
7576 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
7577 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
7580 fprintf(out_stream, "}\n");
7585 * Get the codestream info from a JPEG2000 codec.
7587 *@param p_j2k the component image header to dump.
7589 *@return the codestream information extract from the jpg2000 codec
7591 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
7594 OPJ_UINT16 numcomps = p_j2k->m_image->numcomps;
7595 opj_tcp_v2_t *l_default_tile;
7596 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
7598 cstr_info->nbcomps = p_j2k->m_image->numcomps;
7600 cstr_info->tx0 = p_j2k->m_cp.tx0;
7601 cstr_info->ty0 = p_j2k->m_cp.ty0;
7602 cstr_info->tdx = p_j2k->m_cp.tdx;
7603 cstr_info->tdy = p_j2k->m_cp.tdy;
7604 cstr_info->tw = p_j2k->m_cp.tw;
7605 cstr_info->th = p_j2k->m_cp.th;
7607 cstr_info->tile_info = NULL; /* Not fill from the main header*/
7609 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7611 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
7612 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
7613 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
7614 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
7616 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
7618 for (compno = 0; compno < numcomps; compno++) {
7619 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
7620 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
7621 OPJ_INT32 bandno, numbands;
7624 l_tccp_info->csty = l_tccp->csty;
7625 l_tccp_info->numresolutions = l_tccp->numresolutions;
7626 l_tccp_info->cblkw = l_tccp->cblkw;
7627 l_tccp_info->cblkh = l_tccp->cblkh;
7628 l_tccp_info->cblksty = l_tccp->cblksty;
7629 l_tccp_info->qmfbid = l_tccp->qmfbid;
7630 if (l_tccp->numresolutions < J2K_MAXRLVLS)
7632 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
7633 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
7636 /* quantization style*/
7637 l_tccp_info->qntsty = l_tccp->qntsty;
7638 l_tccp_info->numgbits = l_tccp->numgbits;
7640 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
7641 if (numbands < J2K_MAXBANDS) {
7642 for (bandno = 0; bandno < numbands; bandno++) {
7643 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
7644 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
7649 l_tccp_info->roishift = l_tccp->roishift;
7657 * Get the codestream index from a JPEG2000 codec.
7659 *@param p_j2k the component image header to dump.
7661 *@return the codestream index extract from the jpg2000 codec
7663 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
7665 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
7666 opj_calloc(1,sizeof(opj_codestream_index_t));
7670 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
7671 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
7672 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
7674 l_cstr_index->maxmarknum = p_j2k->cstr_index->maxmarknum;
7675 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
7676 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
7677 if (!l_cstr_index->marker)
7680 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
7682 l_cstr_index->tile_index = NULL; /* FIXME not used for the moment*/
7684 return l_cstr_index;
7692 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
7693 opj_stream_private_t *p_stream,
7694 opj_event_mgr_t * p_manager)
7696 opj_bool l_go_on = OPJ_TRUE;
7697 OPJ_UINT32 l_current_tile_no;
7698 OPJ_UINT32 l_data_size,l_max_data_size;
7699 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
7700 OPJ_UINT32 l_nb_comps;
7701 OPJ_BYTE * l_current_data;
7703 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
7704 if (! l_current_data) {
7707 l_max_data_size = 1000;
7710 if (! j2k_read_tile_header( p_j2k,
7713 &l_tile_x0, &l_tile_y0,
7714 &l_tile_x1, &l_tile_y1,
7719 opj_free(l_current_data);
7727 if (l_data_size > l_max_data_size) {
7728 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
7729 if (! l_current_data) {
7733 l_max_data_size = l_data_size;
7736 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
7737 opj_free(l_current_data);
7740 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);
7742 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data)) {
7743 opj_free(l_current_data);
7746 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
7750 opj_free(l_current_data);
7756 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
7758 void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
7761 assert(p_j2k != 00);
7763 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_tiles);
7764 /* DEVELOPER CORNER, add your custom procedures */
7770 * Decodes the tiles of the stream.
7772 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
7773 opj_stream_private_t * p_stream,
7774 opj_image_t * p_image,
7775 opj_event_mgr_t * p_manager)
7782 /* customization of the decoding */
7783 j2k_setup_decoding(p_j2k);
7785 /* Decode the codestream */
7786 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
7787 opj_image_destroy(p_j2k->m_image);
7788 p_j2k->m_image = NULL;
7792 /* Copy data from codec to output image*/
7793 for (compno = 0; compno < p_image->numcomps; compno++) {
7794 p_image->comps[compno].data = p_j2k->m_image->comps[compno].data;
7795 p_j2k->m_image->comps[compno].data = NULL;