2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9 * Copyright (c) 2006-2007, Parvatha Elangovan
10 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Copyright (c) 2011, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
41 /** @name Local static functions */
45 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
47 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 opj_bool j2k_read_header_procedure(
54 struct opj_stream_private *p_stream,
55 struct opj_event_mgr * p_manager);
58 * The default decoding validation procedure without any extension.
60 * @param p_j2k the jpeg2000 codec to validate.
61 * @param p_stream the input stream to validate.
62 * @param p_manager the user event manager.
64 * @return true if the parameters are correct.
66 opj_bool j2k_decoding_validation (
68 opj_stream_private_t *p_stream,
69 opj_event_mgr_t * p_manager
73 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
74 * are valid. Developpers wanting to extend the library can add their own validation procedures.
76 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
79 * Builds the tcd decoder to use to decode tile.
81 opj_bool j2k_build_decoder (
83 opj_stream_private_t *p_stream,
84 opj_event_mgr_t * p_manager
88 * Excutes the given procedures on the given codec.
90 * @param p_procedure_list the list of procedures to execute
91 * @param p_j2k the jpeg2000 codec to execute the procedures on.
92 * @param p_stream the stream to execute the procedures on.
93 * @param p_manager the user manager.
95 * @return true if all the procedures were successfully executed.
97 static opj_bool j2k_exec (
99 opj_procedure_list_t * p_procedure_list,
100 opj_stream_private_t *p_stream,
101 opj_event_mgr_t * p_manager
105 * Copies the decoding tile parameters onto all the tile parameters.
106 * Creates also the tile decoder.
108 opj_bool j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
109 opj_stream_private_t *p_stream,
110 opj_event_mgr_t * p_manager );
113 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
114 * with the marker value.
115 * @param p_id Marker value to look up
117 * @return the handler associated with the id.
119 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
122 * Destroys a tile coding parameter structure.
124 * @param p_tcp the tile coding parameter to destroy.
126 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
130 * Destroys a coding parameter structure.
132 * @param p_cp the coding parameter to destroy.
134 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
138 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
139 * @param p_header_data the data contained in the COM box.
140 * @param p_j2k the jpeg2000 codec.
141 * @param p_header_size the size of the data contained in the COM marker.
142 * @param p_manager the user event manager.
144 static opj_bool j2k_read_SPCod_SPCoc(
147 OPJ_BYTE * p_header_data,
148 OPJ_UINT32 * p_header_size,
149 struct opj_event_mgr * p_manager
153 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
155 * @param p_tile_no the tile to output.
156 * @param p_comp_no the component number to output.
157 * @param p_data the data buffer.
158 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
159 * @param p_j2k J2K codec.
160 * @param p_manager the user event manager.
163 static opj_bool j2k_read_SQcd_SQcc(
166 OPJ_BYTE * p_header_data,
167 OPJ_UINT32 * p_header_size,
168 struct opj_event_mgr * p_manager
172 * Copies the tile component parameters of all the component from the first tile component.
174 * @param p_j2k the J2k codec.
176 static void j2k_copy_tile_component_parameters(
181 * Copies the tile quantization parameters of all the component from the first tile component.
183 * @param p_j2k the J2k codec.
185 static void j2k_copy_tile_quantization_parameters(
192 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
193 opj_stream_private_t *p_stream,
194 opj_event_mgr_t * p_manager);
196 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
200 * -----------------------------------------------------------------------
201 * -----------------------------------------------------------------------
202 * -----------------------------------------------------------------------
206 Write the SOC marker (Start Of Codestream)
207 @param j2k J2K handle
209 static void j2k_write_soc(opj_j2k_t *j2k);
211 Read the SOC marker (Start of Codestream)
212 @param j2k J2K handle
214 static void j2k_read_soc(opj_j2k_t *j2k);
217 * Reads a SOC marker (Start of Codestream)
218 * @param p_header_data the data contained in the SOC box.
219 * @param jp2 the jpeg2000 file codec.
220 * @param p_header_size the size of the data contained in the SOC marker.
221 * @param p_manager the user event manager.
223 static opj_bool j2k_read_soc_v2(
225 struct opj_stream_private *p_stream,
226 struct opj_event_mgr * p_manager
230 Write the SIZ marker (image and tile size)
231 @param j2k J2K handle
233 static void j2k_write_siz(opj_j2k_t *j2k);
235 Read the SIZ marker (image and tile size)
236 @param j2k J2K handle
238 static void j2k_read_siz(opj_j2k_t *j2k);
241 * Reads a SIZ marker (image and tile size)
242 * @param p_header_data the data contained in the SIZ box.
243 * @param jp2 the jpeg2000 file codec.
244 * @param p_header_size the size of the data contained in the SIZ marker.
245 * @param p_manager the user event manager.
247 static opj_bool j2k_read_siz_v2 (
249 OPJ_BYTE * p_header_data,
250 OPJ_UINT32 p_header_size,
251 struct opj_event_mgr * p_manager
255 Write the COM marker (comment)
256 @param j2k J2K handle
258 static void j2k_write_com(opj_j2k_t *j2k);
260 Read the COM marker (comment)
261 @param j2k J2K handle
263 static void j2k_read_com(opj_j2k_t *j2k);
265 * Reads a COM marker (comments)
266 * @param p_header_data the data contained in the COM box.
267 * @param jp2 the jpeg2000 file codec.
268 * @param p_header_size the size of the data contained in the COM marker.
269 * @param p_manager the user event manager.
271 static opj_bool j2k_read_com_v2 (
273 OPJ_BYTE * p_header_data,
274 OPJ_UINT32 p_header_size,
275 struct opj_event_mgr * p_manager
278 Write the value concerning the specified component in the marker COD and COC
279 @param j2k J2K handle
280 @param compno Number of the component concerned by the information written
282 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
284 Read the value concerning the specified component in the marker COD and COC
285 @param j2k J2K handle
286 @param compno Number of the component concerned by the information read
288 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
290 Write the COD marker (coding style default)
291 @param j2k J2K handle
293 static void j2k_write_cod(opj_j2k_t *j2k);
295 Read the COD marker (coding style default)
296 @param j2k J2K handle
298 static void j2k_read_cod(opj_j2k_t *j2k);
301 * Reads a COD marker (Coding Styke defaults)
302 * @param p_header_data the data contained in the COD box.
303 * @param p_j2k the jpeg2000 codec.
304 * @param p_header_size the size of the data contained in the COD marker.
305 * @param p_manager the user event manager.
307 static opj_bool j2k_read_cod_v2 (
309 OPJ_BYTE * p_header_data,
310 OPJ_UINT32 p_header_size,
311 struct opj_event_mgr * p_manager
315 Write the COC marker (coding style component)
316 @param j2k J2K handle
317 @param compno Number of the component concerned by the information written
319 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
321 Read the COC marker (coding style component)
322 @param j2k J2K handle
324 static void j2k_read_coc(opj_j2k_t *j2k);
327 * Reads a COC marker (Coding Style Component)
328 * @param p_header_data the data contained in the COC box.
329 * @param p_j2k the jpeg2000 codec.
330 * @param p_header_size the size of the data contained in the COC marker.
331 * @param p_manager the user event manager.
333 static opj_bool j2k_read_coc_v2 (
335 OPJ_BYTE * p_header_data,
336 OPJ_UINT32 p_header_size,
337 struct opj_event_mgr * p_manager
341 Write the value concerning the specified component in the marker QCD and QCC
342 @param j2k J2K handle
343 @param compno Number of the component concerned by the information written
345 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
347 Read the value concerning the specified component in the marker QCD and QCC
348 @param j2k J2K handle
349 @param compno Number of the component concern by the information read
350 @param len Length of the information in the QCX part of the marker QCD/QCC
352 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
354 Write the QCD marker (quantization default)
355 @param j2k J2K handle
357 static void j2k_write_qcd(opj_j2k_t *j2k);
359 Read the QCD marker (quantization default)
360 @param j2k J2K handle
362 static void j2k_read_qcd(opj_j2k_t *j2k);
365 * Reads a QCD marker (Quantization defaults)
366 * @param p_header_data the data contained in the QCD box.
367 * @param p_j2k the jpeg2000 codec.
368 * @param p_header_size the size of the data contained in the QCD marker.
369 * @param p_manager the user event manager.
371 static opj_bool j2k_read_qcd_v2 (
373 OPJ_BYTE * p_header_data,
374 OPJ_UINT32 p_header_size,
375 struct opj_event_mgr * p_manager
379 Write the QCC marker (quantization component)
380 @param j2k J2K handle
381 @param compno Number of the component concerned by the information written
383 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
385 Read the QCC marker (quantization component)
386 @param j2k J2K handle
388 static void j2k_read_qcc(opj_j2k_t *j2k);
390 * Reads a QCC marker (Quantization component)
391 * @param p_header_data the data contained in the QCC box.
392 * @param p_j2k the jpeg2000 codec.
393 * @param p_header_size the size of the data contained in the QCC marker.
394 * @param p_manager the user event manager.
396 static opj_bool j2k_read_qcc_v2(
398 OPJ_BYTE * p_header_data,
399 OPJ_UINT32 p_header_size,
400 struct opj_event_mgr * p_manager);
403 Write the POC marker (progression order change)
404 @param j2k J2K handle
406 static void j2k_write_poc(opj_j2k_t *j2k);
408 Read the POC marker (progression order change)
409 @param j2k J2K handle
411 static void j2k_read_poc(opj_j2k_t *j2k);
413 * Reads a POC marker (Progression Order Change)
415 * @param p_header_data the data contained in the POC box.
416 * @param p_j2k the jpeg2000 codec.
417 * @param p_header_size the size of the data contained in the POC marker.
418 * @param p_manager the user event manager.
420 static opj_bool j2k_read_poc_v2 (
422 OPJ_BYTE * p_header_data,
423 OPJ_UINT32 p_header_size,
424 struct opj_event_mgr * p_manager
427 Read the CRG marker (component registration)
428 @param j2k J2K handle
430 static void j2k_read_crg(opj_j2k_t *j2k);
432 * Reads a CRG marker (Component registration)
434 * @param p_header_data the data contained in the TLM box.
435 * @param p_j2k the jpeg2000 codec.
436 * @param p_header_size the size of the data contained in the TLM marker.
437 * @param p_manager the user event manager.
439 static opj_bool j2k_read_crg_v2 (
441 OPJ_BYTE * p_header_data,
442 OPJ_UINT32 p_header_size,
443 struct opj_event_mgr * p_manager
446 Read the TLM marker (tile-part lengths)
447 @param j2k J2K handle
449 static void j2k_read_tlm(opj_j2k_t *j2k);
451 * Reads a TLM marker (Tile Length Marker)
453 * @param p_header_data the data contained in the TLM box.
454 * @param p_j2k the jpeg2000 codec.
455 * @param p_header_size the size of the data contained in the TLM marker.
456 * @param p_manager the user event manager.
458 static opj_bool j2k_read_tlm_v2 (
460 OPJ_BYTE * p_header_data,
461 OPJ_UINT32 p_header_size,
462 struct opj_event_mgr * p_manager
465 Read the PLM marker (packet length, main header)
466 @param j2k J2K handle
468 static void j2k_read_plm(opj_j2k_t *j2k);
471 * Reads a PLM marker (Packet length, main header marker)
473 * @param p_header_data the data contained in the TLM box.
474 * @param p_j2k the jpeg2000 codec.
475 * @param p_header_size the size of the data contained in the TLM marker.
476 * @param p_manager the user event manager.
478 static opj_bool j2k_read_plm_v2 (
480 OPJ_BYTE * p_header_data,
481 OPJ_UINT32 p_header_size,
482 struct opj_event_mgr * p_manager
485 Read the PLT marker (packet length, tile-part header)
486 @param j2k J2K handle
488 static void j2k_read_plt(opj_j2k_t *j2k);
490 * Reads a PLT marker (Packet length, tile-part header)
492 * @param p_header_data the data contained in the PLT box.
493 * @param p_j2k the jpeg2000 codec.
494 * @param p_header_size the size of the data contained in the PLT marker.
495 * @param p_manager the user event manager.
497 static opj_bool j2k_read_plt_v2 (
499 OPJ_BYTE * p_header_data,
500 OPJ_UINT32 p_header_size,
501 struct opj_event_mgr * p_manager
504 Read the PPM marker (packet packet headers, main header)
505 @param j2k J2K handle
507 static void j2k_read_ppm(opj_j2k_t *j2k);
509 * Reads a PPM marker (Packed packet headers, main header)
511 * @param p_header_data the data contained in the POC box.
512 * @param p_j2k the jpeg2000 codec.
513 * @param p_header_size the size of the data contained in the POC marker.
514 * @param p_manager the user event manager.
516 static opj_bool j2k_read_ppm_v2 (
518 OPJ_BYTE * p_header_data,
519 OPJ_UINT32 p_header_size,
520 struct opj_event_mgr * p_manager
523 static opj_bool j2k_read_ppm_v3 (
525 OPJ_BYTE * p_header_data,
526 OPJ_UINT32 p_header_size,
527 struct opj_event_mgr * p_manager
531 Read the PPT marker (packet packet headers, tile-part header)
532 @param j2k J2K handle
534 static void j2k_read_ppt(opj_j2k_t *j2k);
536 * Reads a PPT marker (Packed packet headers, tile-part header)
538 * @param p_header_data the data contained in the PPT box.
539 * @param p_j2k the jpeg2000 codec.
540 * @param p_header_size the size of the data contained in the PPT marker.
541 * @param p_manager the user event manager.
543 static opj_bool j2k_read_ppt_v2 (
545 OPJ_BYTE * p_header_data,
546 OPJ_UINT32 p_header_size,
547 struct opj_event_mgr * p_manager
550 Write the TLM marker (Mainheader)
551 @param j2k J2K handle
553 static void j2k_write_tlm(opj_j2k_t *j2k);
555 Write the SOT marker (start of tile-part)
556 @param j2k J2K handle
558 static void j2k_write_sot(opj_j2k_t *j2k);
560 Read the SOT marker (start of tile-part)
561 @param j2k J2K handle
563 static void j2k_read_sot(opj_j2k_t *j2k);
566 * Reads a PPT marker (Packed packet headers, tile-part header)
568 * @param p_header_data the data contained in the PPT box.
569 * @param p_j2k the jpeg2000 codec.
570 * @param p_header_size the size of the data contained in the PPT marker.
571 * @param p_manager the user event manager.
573 static opj_bool j2k_read_sot_v2 (
575 OPJ_BYTE * p_header_data,
576 OPJ_UINT32 p_header_size,
577 struct opj_event_mgr * p_manager
580 Write the SOD marker (start of data)
581 @param j2k J2K handle
582 @param tile_coder Pointer to a TCD handle
584 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
586 Read the SOD marker (start of data)
587 @param j2k J2K handle
589 static void j2k_read_sod(opj_j2k_t *j2k);
592 * Reads a SOD marker (Start Of Data)
594 * @param p_header_data the data contained in the SOD box.
595 * @param p_j2k the jpeg2000 codec.
596 * @param p_header_size the size of the data contained in the SOD marker.
597 * @param p_manager the user event manager.
599 static opj_bool j2k_read_sod_v2 (
601 struct opj_stream_private *p_stream,
602 struct opj_event_mgr * p_manager
606 Write the RGN marker (region-of-interest)
607 @param j2k J2K handle
608 @param compno Number of the component concerned by the information written
609 @param tileno Number of the tile concerned by the information written
611 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
613 Read the RGN marker (region-of-interest)
614 @param j2k J2K handle
616 static void j2k_read_rgn(opj_j2k_t *j2k);
619 * Reads a RGN marker (Region Of Interest)
621 * @param p_header_data the data contained in the POC box.
622 * @param p_j2k the jpeg2000 codec.
623 * @param p_header_size the size of the data contained in the POC marker.
624 * @param p_manager the user event manager.
626 static opj_bool j2k_read_rgn_v2 (
628 OPJ_BYTE * p_header_data,
629 OPJ_UINT32 p_header_size,
630 struct opj_event_mgr * p_manager
634 Write the EOC marker (end of codestream)
635 @param j2k J2K handle
637 static void j2k_write_eoc(opj_j2k_t *j2k);
639 Read the EOC marker (end of codestream)
640 @param j2k J2K handle
642 static void j2k_read_eoc(opj_j2k_t *j2k);
645 * Reads a EOC marker (End Of Codestream)
647 * @param p_header_data the data contained in the SOD box.
648 * @param p_j2k the jpeg2000 codec.
649 * @param p_header_size the size of the data contained in the SOD marker.
650 * @param p_manager the user event manager.
652 static opj_bool j2k_read_eoc_v2 (
654 struct opj_stream_private *p_stream,
655 struct opj_event_mgr * p_manager
659 Read an unknown marker
660 @param j2k J2K handle
662 static void j2k_read_unk(opj_j2k_t *j2k);
664 Add main header marker information
665 @param cstr_info Codestream information structure
666 @param type marker type
667 @param pos byte offset of marker segment
668 @param len length of marker segment
670 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
672 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) ;
674 Add tile header marker information
675 @param tileno tile index number
676 @param cstr_info Codestream information structure
677 @param type marker type
678 @param pos byte offset of marker segment
679 @param len length of marker segment
681 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
683 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len);
686 * Reads an unknown marker
688 * @param p_stream the stream object to read from.
689 * @param p_j2k the jpeg2000 codec.
690 * @param p_manager the user event manager.
692 * @return true if the marker could be deduced.
694 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
695 struct opj_stream_private *p_stream,
696 OPJ_UINT32 *output_marker,
697 struct opj_event_mgr * p_manager );
700 * Reads a MCT marker (Multiple Component Transform)
702 * @param p_header_data the data contained in the MCT box.
703 * @param p_j2k the jpeg2000 codec.
704 * @param p_header_size the size of the data contained in the MCT marker.
705 * @param p_manager the user event manager.
707 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
708 OPJ_BYTE * p_header_data,
709 OPJ_UINT32 p_header_size,
710 struct opj_event_mgr * p_manager );
713 * Reads a MCC marker (Multiple Component Collection)
715 * @param p_header_data the data contained in the MCC box.
716 * @param p_j2k the jpeg2000 codec.
717 * @param p_header_size the size of the data contained in the MCC marker.
718 * @param p_manager the user event manager.
720 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
721 OPJ_BYTE * p_header_data,
722 OPJ_UINT32 p_header_size,
723 struct opj_event_mgr * p_manager );
726 * Reads a MCO marker (Multiple Component Transform Ordering)
728 * @param p_header_data the data contained in the MCO box.
729 * @param p_j2k the jpeg2000 codec.
730 * @param p_header_size the size of the data contained in the MCO marker.
731 * @param p_manager the user event manager.
733 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
734 OPJ_BYTE * p_header_data,
735 OPJ_UINT32 p_header_size,
736 struct opj_event_mgr * p_manager );
738 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
740 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
741 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
742 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
743 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
745 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
746 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
747 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
748 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
750 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
751 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
752 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
753 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
756 * Reads a CBD marker (Component bit depth definition)
757 * @param p_header_data the data contained in the CBD box.
758 * @param p_j2k the jpeg2000 codec.
759 * @param p_header_size the size of the data contained in the CBD marker.
760 * @param p_manager the user event manager.
762 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
763 OPJ_BYTE * p_header_data,
764 OPJ_UINT32 p_header_size,
765 struct opj_event_mgr * p_manager);
768 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
770 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
772 static opj_codestream_index_t* j2k_create_cstr_index(void);
778 /* ----------------------------------------------------------------------- */
779 typedef struct j2k_prog_order{
780 OPJ_PROG_ORDER enum_prog;
784 j2k_prog_order_t j2k_prog_order_list[] = {
790 {(OPJ_PROG_ORDER)-1, ""}
798 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
806 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
808 const j2k_mct_function j2k_mct_read_functions_to_float [] =
810 j2k_read_int16_to_float,
811 j2k_read_int32_to_float,
812 j2k_read_float32_to_float,
813 j2k_read_float64_to_float
816 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
818 j2k_read_int16_to_int32,
819 j2k_read_int32_to_int32,
820 j2k_read_float32_to_int32,
821 j2k_read_float64_to_int32
824 const j2k_mct_function j2k_mct_write_functions_from_float [] =
826 j2k_write_float_to_int16,
827 j2k_write_float_to_int32,
828 j2k_write_float_to_float,
829 j2k_write_float_to_float64
832 typedef struct opj_dec_memory_marker_handler
836 /** value of the state when the marker can appear */
838 /** action linked to the marker */
839 opj_bool (*handler) (
841 OPJ_BYTE * p_header_data,
842 OPJ_UINT32 p_header_size,
843 struct opj_event_mgr * p_manager
846 opj_dec_memory_marker_handler_t;
848 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
851 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
852 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
853 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
854 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
855 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
856 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
857 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
858 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
859 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
860 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
861 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
862 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
863 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
865 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
866 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
867 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
868 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
869 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
870 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
872 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
873 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
874 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
875 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
876 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
877 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
878 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
879 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
880 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
881 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
882 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
883 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
884 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
886 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
887 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
888 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
889 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
890 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
891 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
893 #ifdef TODO_MS /* FIXME */
894 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
895 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
896 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
897 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
899 #endif /* USE_JPWL */
901 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
902 {J2K_MS_INSEC, 0, j2k_read_insec}
903 #endif /* USE_JPSEC */
904 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}//j2k_read_unk_v2}
909 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
911 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
912 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
916 for (i=0;i<p_nb_elem;++i) {
917 opj_read_bytes(l_src_data,&l_temp,2);
919 l_src_data+=sizeof(OPJ_INT16);
921 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
925 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
927 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
928 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
932 for (i=0;i<p_nb_elem;++i) {
933 opj_read_bytes(l_src_data,&l_temp,4);
935 l_src_data+=sizeof(OPJ_INT32);
937 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
941 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
943 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
944 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
948 for (i=0;i<p_nb_elem;++i) {
949 opj_read_float(l_src_data,&l_temp);
951 l_src_data+=sizeof(OPJ_FLOAT32);
953 *(l_dest_data++) = l_temp;
957 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
959 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
960 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
964 for (i=0;i<p_nb_elem;++i) {
965 opj_read_double(l_src_data,&l_temp);
967 l_src_data+=sizeof(OPJ_FLOAT64);
969 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
973 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
975 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
976 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
980 for (i=0;i<p_nb_elem;++i) {
981 opj_read_bytes(l_src_data,&l_temp,2);
983 l_src_data+=sizeof(OPJ_INT16);
985 *(l_dest_data++) = (OPJ_INT32) l_temp;
989 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
991 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
992 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
996 for (i=0;i<p_nb_elem;++i) {
997 opj_read_bytes(l_src_data,&l_temp,4);
999 l_src_data+=sizeof(OPJ_INT32);
1001 *(l_dest_data++) = (OPJ_INT32) l_temp;
1005 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1007 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1008 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1012 for (i=0;i<p_nb_elem;++i) {
1013 opj_read_float(l_src_data,&l_temp);
1015 l_src_data+=sizeof(OPJ_FLOAT32);
1017 *(l_dest_data++) = (OPJ_INT32) l_temp;
1021 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1023 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1024 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1028 for (i=0;i<p_nb_elem;++i) {
1029 opj_read_double(l_src_data,&l_temp);
1031 l_src_data+=sizeof(OPJ_FLOAT64);
1033 *(l_dest_data++) = (OPJ_INT32) l_temp;
1037 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1039 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1040 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1044 for (i=0;i<p_nb_elem;++i) {
1045 l_temp = (OPJ_UINT32) *(l_src_data++);
1047 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1049 l_dest_data+=sizeof(OPJ_INT16);
1053 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1055 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1056 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1060 for (i=0;i<p_nb_elem;++i) {
1061 l_temp = (OPJ_UINT32) *(l_src_data++);
1063 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1065 l_dest_data+=sizeof(OPJ_INT32);
1069 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1071 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1072 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1076 for (i=0;i<p_nb_elem;++i) {
1077 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1079 opj_write_float(l_dest_data,l_temp);
1081 l_dest_data+=sizeof(OPJ_FLOAT32);
1085 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1087 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1088 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1092 for (i=0;i<p_nb_elem;++i) {
1093 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1095 opj_write_double(l_dest_data,l_temp);
1097 l_dest_data+=sizeof(OPJ_FLOAT64);
1103 * Converts an enum type progression order to string type.
1105 * @param prg_order the progression order to get.
1107 * @return the string representation of the given progression order.
1109 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1110 j2k_prog_order_t *po;
1111 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1112 if(po->enum_prog == prg_order){
1113 return po->str_prog;
1116 return po->str_prog;
1119 /* ----------------------------------------------------------------------- */
1120 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1123 int tpnum=1,tpend=0;
1124 opj_tcp_t *tcp = &cp->tcps[tileno];
1125 prog = j2k_convert_progression_order(tcp->prg);
1130 if( cp->tp_flag == prog[i] ){
1131 tpend=1;cp->tp_pos=i;
1135 tpnum= tpnum * tcp->pocs[pino].compE;
1138 tpnum= tpnum * tcp->pocs[pino].resE;
1141 tpnum= tpnum * tcp->pocs[pino].prcE;
1144 tpnum= tpnum * tcp->pocs[pino].layE;
1155 /** mem allocation for TLM marker*/
1156 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1157 int pino,tileno,totnum_tp=0;
1159 OPJ_ARG_NOT_USED(img_numcomp);
1161 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1162 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1163 int cur_totnum_tp = 0;
1164 opj_tcp_t *tcp = &cp->tcps[tileno];
1165 for(pino = 0; pino <= tcp->numpocs; pino++) {
1167 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1168 if(!pi) { return -1;}
1169 tp_num = j2k_get_num_tp(cp,pino,tileno);
1170 totnum_tp = totnum_tp + tp_num;
1171 cur_totnum_tp = cur_totnum_tp + tp_num;
1172 pi_destroy(pi, cp, tileno);
1174 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1176 if (j2k->cstr_info) {
1177 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1178 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1185 static void j2k_write_soc(opj_j2k_t *j2k) {
1186 opj_cio_t *cio = j2k->cio;
1187 cio_write(cio, J2K_MS_SOC, 2);
1190 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
1195 /* update markers struct */
1196 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
1197 #endif /* USE_JPWL */
1201 static void j2k_read_soc(opj_j2k_t *j2k) {
1202 j2k->state = J2K_STATE_MHSIZ;
1204 if (j2k->cstr_info) {
1205 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
1206 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
1211 * Reads a SOC marker (Start of Codestream)
1212 * @param p_header_data the data contained in the SOC box.
1213 * @param jp2 the jpeg2000 file codec.
1214 * @param p_header_size the size of the data contained in the SOC marker.
1215 * @param p_manager the user event manager.
1217 static opj_bool j2k_read_soc_v2( opj_j2k_v2_t *p_j2k,
1218 struct opj_stream_private *p_stream,
1219 struct opj_event_mgr * p_manager )
1221 OPJ_BYTE l_data [2];
1222 OPJ_UINT32 l_marker;
1225 assert(p_j2k != 00);
1226 assert(p_manager != 00);
1227 assert(p_stream != 00);
1229 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1233 opj_read_bytes(l_data,&l_marker,2);
1234 if (l_marker != J2K_MS_SOC) {
1238 /* Next marker should be a SIZ marker in the main header */
1239 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1241 /* FIXME move it in a index structure included in p_j2k*/
1242 p_j2k->cstr_index->main_head_start = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
1244 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1246 /* Add the marker to the codestream index*/
1247 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1252 static void j2k_write_siz(opj_j2k_t *j2k) {
1256 opj_cio_t *cio = j2k->cio;
1257 opj_image_t *image = j2k->image;
1258 opj_cp_t *cp = j2k->cp;
1260 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
1261 lenp = cio_tell(cio);
1263 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
1264 cio_write(cio, image->x1, 4); /* Xsiz */
1265 cio_write(cio, image->y1, 4); /* Ysiz */
1266 cio_write(cio, image->x0, 4); /* X0siz */
1267 cio_write(cio, image->y0, 4); /* Y0siz */
1268 cio_write(cio, cp->tdx, 4); /* XTsiz */
1269 cio_write(cio, cp->tdy, 4); /* YTsiz */
1270 cio_write(cio, cp->tx0, 4); /* XT0siz */
1271 cio_write(cio, cp->ty0, 4); /* YT0siz */
1272 cio_write(cio, image->numcomps, 2); /* Csiz */
1273 for (i = 0; i < image->numcomps; i++) {
1274 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
1275 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
1276 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
1278 len = cio_tell(cio) - lenp;
1279 cio_seek(cio, lenp);
1280 cio_write(cio, len, 2); /* Lsiz */
1281 cio_seek(cio, lenp + len);
1284 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
1287 static void j2k_read_siz(opj_j2k_t *j2k) {
1290 opj_cio_t *cio = j2k->cio;
1291 opj_image_t *image = j2k->image;
1292 opj_cp_t *cp = j2k->cp;
1294 len = cio_read(cio, 2); /* Lsiz */
1295 cio_read(cio, 2); /* Rsiz (capabilities) */
1296 image->x1 = cio_read(cio, 4); /* Xsiz */
1297 image->y1 = cio_read(cio, 4); /* Ysiz */
1298 image->x0 = cio_read(cio, 4); /* X0siz */
1299 image->y0 = cio_read(cio, 4); /* Y0siz */
1300 cp->tdx = cio_read(cio, 4); /* XTsiz */
1301 cp->tdy = cio_read(cio, 4); /* YTsiz */
1302 cp->tx0 = cio_read(cio, 4); /* XT0siz */
1303 cp->ty0 = cio_read(cio, 4); /* YT0siz */
1305 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
1306 opj_event_msg(j2k->cinfo, EVT_ERROR,
1307 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
1308 image->x0,image->x1,image->y0,image->y1);
1312 image->numcomps = cio_read(cio, 2); /* Csiz */
1315 if (j2k->cp->correct) {
1316 /* if JPWL is on, we check whether TX errors have damaged
1317 too much the SIZ parameters */
1318 if (!(image->x1 * image->y1)) {
1319 opj_event_msg(j2k->cinfo, EVT_ERROR,
1320 "JPWL: bad image size (%d x %d)\n",
1321 image->x1, image->y1);
1322 if (!JPWL_ASSUME || JPWL_ASSUME) {
1323 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1327 if (image->numcomps != ((len - 38) / 3)) {
1328 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1329 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1330 image->numcomps, ((len - 38) / 3));
1332 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1335 /* we try to correct */
1336 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
1337 if (image->numcomps < ((len - 38) / 3)) {
1338 len = 38 + 3 * image->numcomps;
1339 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1342 image->numcomps = ((len - 38) / 3);
1343 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1348 /* update components number in the jpwl_exp_comps filed */
1349 cp->exp_comps = image->numcomps;
1351 #endif /* USE_JPWL */
1353 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
1354 for (i = 0; i < image->numcomps; i++) {
1356 tmp = cio_read(cio, 1); /* Ssiz_i */
1357 image->comps[i].prec = (tmp & 0x7f) + 1;
1358 image->comps[i].sgnd = tmp >> 7;
1359 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
1360 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
1363 if (j2k->cp->correct) {
1364 /* if JPWL is on, we check whether TX errors have damaged
1365 too much the SIZ parameters, again */
1366 if (!(image->comps[i].dx * image->comps[i].dy)) {
1367 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1368 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1369 i, i, image->comps[i].dx, image->comps[i].dy);
1371 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1374 /* we try to correct */
1375 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1376 if (!image->comps[i].dx) {
1377 image->comps[i].dx = 1;
1378 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1379 i, image->comps[i].dx);
1381 if (!image->comps[i].dy) {
1382 image->comps[i].dy = 1;
1383 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1384 i, image->comps[i].dy);
1389 #endif /* USE_JPWL */
1391 /* TODO: unused ? */
1392 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
1393 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
1395 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
1396 image->comps[i].factor = cp->reduce; /* reducing factor per component */
1399 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
1400 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
1403 if (j2k->cp->correct) {
1404 /* if JPWL is on, we check whether TX errors have damaged
1405 too much the SIZ parameters */
1406 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
1407 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1408 "JPWL: bad number of tiles (%d x %d)\n",
1411 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1414 /* we try to correct */
1415 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1418 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1421 if (cp->tw > cp->max_tiles) {
1423 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
1424 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1425 cp->max_tiles, cp->tw);
1429 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1432 if (cp->th > cp->max_tiles) {
1434 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1435 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1436 cp->max_tiles, cp->th);
1440 #endif /* USE_JPWL */
1442 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
1443 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
1444 cp->tileno_size = 0;
1447 if (j2k->cp->correct) {
1449 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1450 "JPWL: could not alloc tcps field of cp\n");
1451 if (!JPWL_ASSUME || JPWL_ASSUME) {
1452 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1457 #endif /* USE_JPWL */
1459 for (i = 0; i < cp->tw * cp->th; i++) {
1460 cp->tcps[i].POC = 0;
1461 cp->tcps[i].numpocs = 0;
1462 cp->tcps[i].first = 1;
1465 /* Initialization for PPM marker */
1467 cp->ppm_data = NULL;
1468 cp->ppm_data_first = NULL;
1469 cp->ppm_previous = 0;
1472 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
1473 for (i = 0; i < cp->tw * cp->th; i++) {
1474 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
1476 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
1477 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
1478 j2k->state = J2K_STATE_MH;
1481 if (j2k->cstr_info) {
1482 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1483 cstr_info->image_w = image->x1 - image->x0;
1484 cstr_info->image_h = image->y1 - image->y0;
1485 cstr_info->numcomps = image->numcomps;
1486 cstr_info->tw = cp->tw;
1487 cstr_info->th = cp->th;
1488 cstr_info->tile_x = cp->tdx;
1489 cstr_info->tile_y = cp->tdy;
1490 cstr_info->tile_Ox = cp->tx0;
1491 cstr_info->tile_Oy = cp->ty0;
1492 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
1498 * Reads a SIZ marker (image and tile size)
1499 * @param p_header_data the data contained in the SIZ box.
1500 * @param jp2 the jpeg2000 file codec.
1501 * @param p_header_size the size of the data contained in the SIZ marker.
1502 * @param p_manager the user event manager.
1504 opj_bool j2k_read_siz_v2 (
1505 opj_j2k_v2_t *p_j2k,
1506 OPJ_BYTE * p_header_data,
1507 OPJ_UINT32 p_header_size,
1508 struct opj_event_mgr * p_manager
1511 OPJ_UINT32 l_size, i;
1512 OPJ_UINT32 l_nb_comp;
1513 OPJ_UINT32 l_nb_comp_remain;
1514 OPJ_UINT32 l_remaining_size;
1515 OPJ_UINT32 l_nb_tiles;
1517 opj_image_t *l_image = 00;
1518 opj_cp_v2_t *l_cp = 00;
1519 opj_image_comp_t * l_img_comp = 00;
1520 opj_tcp_v2_t * l_current_tile_param = 00;
1523 assert(p_j2k != 00);
1524 assert(p_manager != 00);
1525 assert(p_header_data != 00);
1527 l_image = p_j2k->m_private_image;
1528 l_cp = &(p_j2k->m_cp);
1530 // minimum size == 39 - 3 (= minimum component parameter)
1531 if (p_header_size < 36) {
1532 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1536 l_remaining_size = p_header_size - 36;
1537 l_nb_comp = l_remaining_size / 3;
1538 l_nb_comp_remain = l_remaining_size % 3;
1539 if (l_nb_comp_remain != 0){
1540 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1544 l_size = p_header_size + 2; /* Lsiz */
1546 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
1548 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1549 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
1551 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
1553 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
1555 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
1557 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
1559 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
1561 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
1563 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
1565 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
1568 l_image->numcomps = (OPJ_UINT16) l_tmp;
1570 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
1574 if (l_image->numcomps != l_nb_comp) {
1575 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);
1580 if (l_cp->correct) {
1581 /* if JPWL is on, we check whether TX errors have damaged
1582 too much the SIZ parameters */
1583 if (!(l_image->x1 * l_image->y1)) {
1584 opj_event_msg_v2(p_manager, EVT_ERROR,
1585 "JPWL: bad image size (%d x %d)\n",
1586 l_image->x1, l_image->y1);
1587 if (!JPWL_ASSUME || JPWL_ASSUME) {
1588 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1593 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
1594 if (l_image->numcomps != ((len - 38) / 3)) {
1595 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1596 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1597 l_image->numcomps, ((len - 38) / 3));
1599 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1602 */ /* we try to correct */
1603 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
1604 if (l_image->numcomps < ((len - 38) / 3)) {
1605 len = 38 + 3 * l_image->numcomps;
1606 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1609 l_image->numcomps = ((len - 38) / 3);
1610 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1616 /* update components number in the jpwl_exp_comps filed */
1617 l_cp->exp_comps = l_image->numcomps;
1619 #endif /* USE_JPWL */
1621 // Allocate the resulting image components
1622 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
1623 if (l_image->comps == 00){
1624 l_image->numcomps = 0;
1625 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1629 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
1630 l_img_comp = l_image->comps;
1632 // Read the component information
1633 for (i = 0; i < l_image->numcomps; ++i){
1635 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
1637 l_img_comp->prec = (tmp & 0x7f) + 1;
1638 l_img_comp->sgnd = tmp >> 7;
1639 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
1641 l_img_comp->dx = (OPJ_INT32)tmp; // should be between 1 and 255
1642 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
1644 l_img_comp->dy = (OPJ_INT32)tmp; // should be between 1 and 255
1647 if (l_cp->correct) {
1648 /* if JPWL is on, we check whether TX errors have damaged
1649 too much the SIZ parameters, again */
1650 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
1651 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1652 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1653 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
1655 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1658 /* we try to correct */
1659 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1660 if (!l_image->comps[i].dx) {
1661 l_image->comps[i].dx = 1;
1662 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1663 i, l_image->comps[i].dx);
1665 if (!l_image->comps[i].dy) {
1666 l_image->comps[i].dy = 1;
1667 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1668 i, l_image->comps[i].dy);
1672 #endif /* USE_JPWL */
1673 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
1674 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
1678 // Compute the number of tiles
1679 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
1680 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
1681 l_nb_tiles = l_cp->tw * l_cp->th;
1683 // Define the tiles which will be decoded
1684 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
1685 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;
1686 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;
1687 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);
1688 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);
1691 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
1692 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
1693 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
1694 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
1698 if (l_cp->correct) {
1699 /* if JPWL is on, we check whether TX errors have damaged
1700 too much the SIZ parameters */
1701 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
1702 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1703 "JPWL: bad number of tiles (%d x %d)\n",
1704 l_cp->tw, l_cp->th);
1706 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1709 /* we try to correct */
1710 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1713 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1716 if (l_cp->tw > l_cp->max_tiles) {
1718 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
1719 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1720 l_cp->max_tiles, l_cp->tw);
1724 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1727 if (l_cp->th > l_cp->max_tiles) {
1729 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1730 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1731 l_cp->max_tiles, l_cp->th);
1735 #endif /* USE_JPWL */
1737 /* memory allocations */
1738 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
1739 if (l_cp->tcps == 00) {
1740 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1743 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
1746 if (l_cp->correct) {
1748 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1749 "JPWL: could not alloc tcps field of cp\n");
1750 if (!JPWL_ASSUME || JPWL_ASSUME) {
1751 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1756 #endif /* USE_JPWL */
1758 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
1759 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
1760 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
1761 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1764 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
1766 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
1767 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
1769 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
1770 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1773 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));
1774 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
1776 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
1777 (opj_simple_mcc_decorrelation_data_t*)
1778 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
1780 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
1781 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1784 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));
1785 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
1787 /* set up default dc level shift */
1788 for (i=0;i<l_image->numcomps;++i) {
1789 if (! l_image->comps[i].sgnd) {
1790 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
1794 l_current_tile_param = l_cp->tcps;
1795 for (i = 0; i < l_nb_tiles; ++i) {
1796 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
1797 if (l_current_tile_param->tccps == 00) {
1798 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1801 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
1803 ++l_current_tile_param;
1806 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; // FIXME J2K_DEC_STATE_MH;
1807 opj_image_comp_header_update(l_image,l_cp);
1814 static void j2k_write_com(opj_j2k_t *j2k) {
1818 if(j2k->cp->comment) {
1819 opj_cio_t *cio = j2k->cio;
1820 char *comment = j2k->cp->comment;
1822 cio_write(cio, J2K_MS_COM, 2);
1823 lenp = cio_tell(cio);
1825 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
1826 for (i = 0; i < strlen(comment); i++) {
1827 cio_write(cio, comment[i], 1);
1829 len = cio_tell(cio) - lenp;
1830 cio_seek(cio, lenp);
1831 cio_write(cio, len, 2);
1832 cio_seek(cio, lenp + len);
1836 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
1841 static void j2k_read_com(opj_j2k_t *j2k) {
1844 opj_cio_t *cio = j2k->cio;
1846 len = cio_read(cio, 2);
1847 cio_skip(cio, len - 2);
1850 * Reads a COM marker (comments)
1851 * @param p_header_data the data contained in the COM box.
1852 * @param jp2 the jpeg2000 file codec.
1853 * @param p_header_size the size of the data contained in the COM marker.
1854 * @param p_manager the user event manager.
1856 opj_bool j2k_read_com_v2 (
1857 opj_j2k_v2_t *p_j2k,
1858 OPJ_BYTE * p_header_data,
1859 OPJ_UINT32 p_header_size,
1860 struct opj_event_mgr * p_manager
1864 assert(p_j2k != 00);
1865 assert(p_manager != 00);
1866 assert(p_header_data != 00);
1871 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
1874 opj_cp_t *cp = j2k->cp;
1875 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1876 opj_tccp_t *tccp = &tcp->tccps[compno];
1877 opj_cio_t *cio = j2k->cio;
1879 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
1880 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
1881 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
1882 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
1883 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
1885 if (tccp->csty & J2K_CCP_CSTY_PRT) {
1886 for (i = 0; i < tccp->numresolutions; i++) {
1887 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
1892 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
1895 opj_cp_t *cp = j2k->cp;
1896 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1897 opj_tccp_t *tccp = &tcp->tccps[compno];
1898 opj_cio_t *cio = j2k->cio;
1900 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
1902 // If user wants to remove more resolutions than the codestream contains, return error
1903 if (cp->reduce >= tccp->numresolutions) {
1904 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
1905 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
1906 j2k->state |= J2K_STATE_ERR;
1909 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
1910 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
1911 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
1912 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
1913 if (tccp->csty & J2K_CP_CSTY_PRT) {
1914 for (i = 0; i < tccp->numresolutions; i++) {
1915 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
1916 tccp->prcw[i] = tmp & 0xf;
1917 tccp->prch[i] = tmp >> 4;
1922 if(j2k->cstr_info && compno == 0) {
1923 for (i = 0; i < tccp->numresolutions; i++) {
1924 if (tccp->csty & J2K_CP_CSTY_PRT) {
1925 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
1926 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
1929 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1930 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1937 static void j2k_write_cod(opj_j2k_t *j2k) {
1938 opj_cp_t *cp = NULL;
1939 opj_tcp_t *tcp = NULL;
1942 opj_cio_t *cio = j2k->cio;
1944 cio_write(cio, J2K_MS_COD, 2); /* COD */
1946 lenp = cio_tell(cio);
1950 tcp = &cp->tcps[j2k->curtileno];
1952 cio_write(cio, tcp->csty, 1); /* Scod */
1953 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
1954 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
1955 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
1957 j2k_write_cox(j2k, 0);
1958 len = cio_tell(cio) - lenp;
1959 cio_seek(cio, lenp);
1960 cio_write(cio, len, 2); /* Lcod */
1961 cio_seek(cio, lenp + len);
1964 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
1968 static void j2k_read_cod(opj_j2k_t *j2k) {
1971 opj_cio_t *cio = j2k->cio;
1972 opj_cp_t *cp = j2k->cp;
1973 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1974 opj_image_t *image = j2k->image;
1976 len = cio_read(cio, 2); /* Lcod */
1977 tcp->csty = cio_read(cio, 1); /* Scod */
1978 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
1979 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
1980 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
1982 pos = cio_tell(cio);
1983 for (i = 0; i < image->numcomps; i++) {
1984 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
1986 j2k_read_cox(j2k, i);
1990 if (j2k->cstr_info) {
1991 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1992 cstr_info->prog = tcp->prg;
1993 cstr_info->numlayers = tcp->numlayers;
1994 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
1995 for (i = 0; i < image->numcomps; i++) {
1996 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
2002 * Reads a COD marker (Coding Styke defaults)
2003 * @param p_header_data the data contained in the COD box.
2004 * @param p_j2k the jpeg2000 codec.
2005 * @param p_header_size the size of the data contained in the COD marker.
2006 * @param p_manager the user event manager.
2008 opj_bool j2k_read_cod_v2 (
2009 opj_j2k_v2_t *p_j2k,
2010 OPJ_BYTE * p_header_data,
2011 OPJ_UINT32 p_header_size,
2012 struct opj_event_mgr * p_manager
2018 opj_cp_v2_t *l_cp = 00;
2019 opj_tcp_v2_t *l_tcp = 00;
2020 opj_image_t *l_image = 00;
2023 assert(p_header_data != 00);
2024 assert(p_j2k != 00);
2025 assert(p_manager != 00);
2027 l_image = p_j2k->m_private_image;
2028 l_cp = &(p_j2k->m_cp);
2030 /* If we are in the first tile-part header of the current tile */
2031 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2032 &l_cp->tcps[p_j2k->m_current_tile_number] :
2033 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2035 /* Make sure room is sufficient */
2036 if (p_header_size < 5) {
2037 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2041 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2043 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2045 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2046 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2049 // If user didn't set a number layer to decode take the max specify in the codestream.
2050 if (l_cp->m_specific_param.m_dec.m_layer) {
2051 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2054 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2057 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2061 for (i = 0; i < l_image->numcomps; ++i) {
2062 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2065 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2066 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2070 if (p_header_size != 0) {
2071 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2075 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2076 j2k_copy_tile_component_parameters(p_j2k);
2079 #ifdef WIP_REMOVE_MSD
2080 if (p_j2k->cstr_info) {
2081 //opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;
2082 p_j2k->cstr_info->prog = l_tcp->prg;
2083 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2084 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2085 for (i = 0; i < l_image->numcomps; ++i) {
2086 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2094 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2097 opj_cp_t *cp = j2k->cp;
2098 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2099 opj_image_t *image = j2k->image;
2100 opj_cio_t *cio = j2k->cio;
2102 cio_write(cio, J2K_MS_COC, 2); /* COC */
2103 lenp = cio_tell(cio);
2105 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2106 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2107 j2k_write_cox(j2k, compno);
2108 len = cio_tell(cio) - lenp;
2109 cio_seek(cio, lenp);
2110 cio_write(cio, len, 2); /* Lcoc */
2111 cio_seek(cio, lenp + len);
2114 static void j2k_read_coc(opj_j2k_t *j2k) {
2117 opj_cp_t *cp = j2k->cp;
2118 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2119 opj_image_t *image = j2k->image;
2120 opj_cio_t *cio = j2k->cio;
2122 len = cio_read(cio, 2); /* Lcoc */
2123 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2124 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
2125 j2k_read_cox(j2k, compno);
2129 * Reads a COC marker (Coding Style Component)
2130 * @param p_header_data the data contained in the COC box.
2131 * @param p_j2k the jpeg2000 codec.
2132 * @param p_header_size the size of the data contained in the COC marker.
2133 * @param p_manager the user event manager.
2135 opj_bool j2k_read_coc_v2 (
2136 opj_j2k_v2_t *p_j2k,
2137 OPJ_BYTE * p_header_data,
2138 OPJ_UINT32 p_header_size,
2139 struct opj_event_mgr * p_manager
2142 opj_cp_v2_t *l_cp = NULL;
2143 opj_tcp_v2_t *l_tcp = NULL;
2144 opj_image_t *l_image = NULL;
2145 OPJ_UINT32 l_comp_room;
2146 OPJ_UINT32 l_comp_no;
2149 assert(p_header_data != 00);
2150 assert(p_j2k != 00);
2151 assert(p_manager != 00);
2153 l_cp = &(p_j2k->m_cp);
2154 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2155 &l_cp->tcps[p_j2k->m_current_tile_number] :
2156 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2157 l_image = p_j2k->m_private_image;
2159 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2161 // make sure room is sufficient
2162 if (p_header_size < l_comp_room + 1) {
2163 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2166 p_header_size -= l_comp_room + 1;
2168 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2169 p_header_data += l_comp_room;
2170 if (l_comp_no >= l_image->numcomps) {
2171 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2175 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2178 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2179 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2183 if (p_header_size != 0) {
2184 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2190 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
2191 int bandno, numbands;
2194 opj_cp_t *cp = j2k->cp;
2195 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2196 opj_tccp_t *tccp = &tcp->tccps[compno];
2197 opj_cio_t *cio = j2k->cio;
2199 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
2200 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
2202 for (bandno = 0; bandno < numbands; bandno++) {
2203 expn = tccp->stepsizes[bandno].expn;
2204 mant = tccp->stepsizes[bandno].mant;
2206 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
2207 cio_write(cio, expn << 3, 1); /* SPqcx_i */
2209 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
2214 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
2216 int bandno, numbands;
2218 opj_cp_t *cp = j2k->cp;
2219 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2220 opj_tccp_t *tccp = &tcp->tccps[compno];
2221 opj_cio_t *cio = j2k->cio;
2223 tmp = cio_read(cio, 1); /* Sqcx */
2224 tccp->qntsty = tmp & 0x1f;
2225 tccp->numgbits = tmp >> 5;
2226 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
2227 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
2230 if (j2k->cp->correct) {
2232 /* if JPWL is on, we check whether there are too many subbands */
2233 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
2234 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2235 "JPWL: bad number of subbands in Sqcx (%d)\n",
2238 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2241 /* we try to correct */
2243 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
2244 "- setting number of bands to %d => HYPOTHESIS!!!\n",
2251 /* We check whether there are too many subbands */
2252 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
2253 opj_event_msg(j2k->cinfo, EVT_WARNING ,
2254 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
2255 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
2258 #endif /* USE_JPWL */
2260 for (bandno = 0; bandno < numbands; bandno++) {
2262 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
2263 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
2266 tmp = cio_read(cio, 2); /* SPqcx_i */
2270 if (bandno < J2K_MAXBANDS){
2271 tccp->stepsizes[bandno].expn = expn;
2272 tccp->stepsizes[bandno].mant = mant;
2276 /* Add Antonin : if scalar_derived -> compute other stepsizes */
2277 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
2278 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
2279 tccp->stepsizes[bandno].expn =
2280 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
2281 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
2282 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
2288 static void j2k_write_qcd(opj_j2k_t *j2k) {
2291 opj_cio_t *cio = j2k->cio;
2293 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
2294 lenp = cio_tell(cio);
2296 j2k_write_qcx(j2k, 0);
2297 len = cio_tell(cio) - lenp;
2298 cio_seek(cio, lenp);
2299 cio_write(cio, len, 2); /* Lqcd */
2300 cio_seek(cio, lenp + len);
2303 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
2306 static void j2k_read_qcd(opj_j2k_t *j2k) {
2309 opj_cio_t *cio = j2k->cio;
2310 opj_image_t *image = j2k->image;
2312 len = cio_read(cio, 2); /* Lqcd */
2313 pos = cio_tell(cio);
2314 for (i = 0; i < image->numcomps; i++) {
2316 j2k_read_qcx(j2k, i, len - 2);
2321 * Reads a QCD marker (Quantization defaults)
2322 * @param p_header_data the data contained in the QCD box.
2323 * @param p_j2k the jpeg2000 codec.
2324 * @param p_header_size the size of the data contained in the QCD marker.
2325 * @param p_manager the user event manager.
2327 opj_bool j2k_read_qcd_v2 (
2328 opj_j2k_v2_t *p_j2k,
2329 OPJ_BYTE * p_header_data,
2330 OPJ_UINT32 p_header_size,
2331 struct opj_event_mgr * p_manager
2335 assert(p_header_data != 00);
2336 assert(p_j2k != 00);
2337 assert(p_manager != 00);
2339 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2340 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2344 if (p_header_size != 0) {
2345 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2349 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2350 j2k_copy_tile_quantization_parameters(p_j2k);
2355 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
2358 opj_cio_t *cio = j2k->cio;
2360 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
2361 lenp = cio_tell(cio);
2363 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
2364 j2k_write_qcx(j2k, compno);
2365 len = cio_tell(cio) - lenp;
2366 cio_seek(cio, lenp);
2367 cio_write(cio, len, 2); /* Lqcc */
2368 cio_seek(cio, lenp + len);
2371 static void j2k_read_qcc(opj_j2k_t *j2k) {
2373 int numcomp = j2k->image->numcomps;
2374 opj_cio_t *cio = j2k->cio;
2376 len = cio_read(cio, 2); /* Lqcc */
2377 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
2380 if (j2k->cp->correct) {
2382 static int backup_compno = 0;
2384 /* compno is negative or larger than the number of components!!! */
2385 if ((compno < 0) || (compno >= numcomp)) {
2386 opj_event_msg(j2k->cinfo, EVT_ERROR,
2387 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2390 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2393 /* we try to correct */
2394 compno = backup_compno % numcomp;
2395 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
2396 "- setting component number to %d\n",
2400 /* keep your private count of tiles */
2403 #endif /* USE_JPWL */
2405 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
2409 * Reads a QCC marker (Quantization component)
2410 * @param p_header_data the data contained in the QCC box.
2411 * @param p_j2k the jpeg2000 codec.
2412 * @param p_header_size the size of the data contained in the QCC marker.
2413 * @param p_manager the user event manager.
2415 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
2416 OPJ_BYTE * p_header_data,
2417 OPJ_UINT32 p_header_size,
2418 struct opj_event_mgr * p_manager)
2420 OPJ_UINT32 l_num_comp,l_comp_no;
2423 assert(p_header_data != 00);
2424 assert(p_j2k != 00);
2425 assert(p_manager != 00);
2427 l_num_comp = p_j2k->m_private_image->numcomps;
2429 if (l_num_comp <= 256) {
2430 if (p_header_size < 1) {
2431 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2434 opj_read_bytes(p_header_data,&l_comp_no,1);
2439 if (p_header_size < 2) {
2440 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2443 opj_read_bytes(p_header_data,&l_comp_no,2);
2449 if (p_j2k->m_cp.correct) {
2451 static OPJ_UINT32 backup_compno = 0;
2453 /* compno is negative or larger than the number of components!!! */
2454 if ((l_comp_no < 0) || (l_comp_no >= l_num_comp)) {
2455 opj_event_msg_v2(p_manager, EVT_ERROR,
2456 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2457 l_comp_no, l_num_comp);
2459 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2462 /* we try to correct */
2463 l_comp_no = backup_compno % l_num_comp;
2464 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
2465 "- setting component number to %d\n",
2469 /* keep your private count of tiles */
2472 #endif /* USE_JPWL */
2474 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2475 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2479 if (p_header_size != 0) {
2480 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2488 static void j2k_write_poc(opj_j2k_t *j2k) {
2489 int len, numpchgs, i;
2491 int numcomps = j2k->image->numcomps;
2493 opj_cp_t *cp = j2k->cp;
2494 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2495 opj_tccp_t *tccp = &tcp->tccps[0];
2496 opj_cio_t *cio = j2k->cio;
2498 numpchgs = 1 + tcp->numpocs;
2499 cio_write(cio, J2K_MS_POC, 2); /* POC */
2500 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
2501 cio_write(cio, len, 2); /* Lpoc */
2502 for (i = 0; i < numpchgs; i++) {
2503 opj_poc_t *poc = &tcp->pocs[i];
2504 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
2505 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
2506 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
2507 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
2508 cio_write(cio, poc->resno1, 1); /* REpoc_i */
2509 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
2510 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
2511 poc->compno1 = int_min(poc->compno1, numcomps);
2512 cio_write(cio, poc->prg, 1); /* Ppoc_i */
2516 static void j2k_read_poc(opj_j2k_t *j2k) {
2517 int len, numpchgs, i, old_poc;
2519 int numcomps = j2k->image->numcomps;
2521 opj_cp_t *cp = j2k->cp;
2522 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2523 opj_cio_t *cio = j2k->cio;
2525 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
2527 len = cio_read(cio, 2); /* Lpoc */
2528 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
2530 for (i = old_poc; i < numpchgs + old_poc; i++) {
2532 poc = &tcp->pocs[i];
2533 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
2534 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
2535 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
2536 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
2537 poc->compno1 = int_min(
2538 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
2539 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
2542 tcp->numpocs = numpchgs + old_poc - 1;
2546 * Reads a POC marker (Progression Order Change)
2548 * @param p_header_data the data contained in the POC box.
2549 * @param p_j2k the jpeg2000 codec.
2550 * @param p_header_size the size of the data contained in the POC marker.
2551 * @param p_manager the user event manager.
2553 opj_bool j2k_read_poc_v2 (
2554 opj_j2k_v2_t *p_j2k,
2555 OPJ_BYTE * p_header_data,
2556 OPJ_UINT32 p_header_size,
2557 struct opj_event_mgr * p_manager)
2559 OPJ_UINT32 i, l_nb_comp, l_tmp;
2560 opj_image_t * l_image = 00;
2561 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
2562 OPJ_UINT32 l_chunk_size, l_comp_room;
2564 opj_cp_v2_t *l_cp = 00;
2565 opj_tcp_v2_t *l_tcp = 00;
2566 opj_poc_t *l_current_poc = 00;
2569 assert(p_header_data != 00);
2570 assert(p_j2k != 00);
2571 assert(p_manager != 00);
2573 l_image = p_j2k->m_private_image;
2574 l_nb_comp = l_image->numcomps;
2575 if (l_nb_comp <= 256) {
2581 l_chunk_size = 5 + 2 * l_comp_room;
2582 l_current_poc_nb = p_header_size / l_chunk_size;
2583 l_current_poc_remaining = p_header_size % l_chunk_size;
2585 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
2586 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
2590 l_cp = &(p_j2k->m_cp);
2591 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2592 &l_cp->tcps[p_j2k->m_current_tile_number] :
2593 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2594 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
2595 l_current_poc_nb += l_old_poc_nb;
2597 assert(l_current_poc_nb < 32);
2599 /* now poc is in use.*/
2602 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
2603 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
2604 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
2606 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
2607 p_header_data+=l_comp_room;
2608 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
2610 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
2612 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
2613 p_header_data+=l_comp_room;
2614 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
2616 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
2617 /* make sure comp is in acceptable bounds */
2618 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
2622 l_tcp->numpocs = l_current_poc_nb - 1;
2626 static void j2k_read_crg(opj_j2k_t *j2k) {
2627 int len, i, Xcrg_i, Ycrg_i;
2629 opj_cio_t *cio = j2k->cio;
2630 int numcomps = j2k->image->numcomps;
2632 len = cio_read(cio, 2); /* Lcrg */
2633 for (i = 0; i < numcomps; i++) {
2634 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
2635 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
2640 * Reads a CRG marker (Component registration)
2642 * @param p_header_data the data contained in the TLM box.
2643 * @param p_j2k the jpeg2000 codec.
2644 * @param p_header_size the size of the data contained in the TLM marker.
2645 * @param p_manager the user event manager.
2647 opj_bool j2k_read_crg_v2 (
2648 opj_j2k_v2_t *p_j2k,
2649 OPJ_BYTE * p_header_data,
2650 OPJ_UINT32 p_header_size,
2651 struct opj_event_mgr * p_manager
2654 OPJ_UINT32 l_nb_comp;
2656 assert(p_header_data != 00);
2657 assert(p_j2k != 00);
2658 assert(p_manager != 00);
2660 l_nb_comp = p_j2k->m_private_image->numcomps;
2662 if (p_header_size != l_nb_comp *4) {
2663 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
2666 /* Do not care of this at the moment since only local variables are set here */
2669 (i = 0; i < l_nb_comp; ++i)
2671 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
2673 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
2680 static void j2k_read_tlm(opj_j2k_t *j2k) {
2681 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
2682 long int Ttlm_i, Ptlm_i;
2684 opj_cio_t *cio = j2k->cio;
2686 len = cio_read(cio, 2); /* Ltlm */
2687 Ztlm = cio_read(cio, 1); /* Ztlm */
2688 Stlm = cio_read(cio, 1); /* Stlm */
2689 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
2690 SP = (Stlm >> 6) & 0x01;
2691 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
2692 for (i = 0; i < tile_tlm; i++) {
2693 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
2694 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
2699 * Reads a TLM marker (Tile Length Marker)
2701 * @param p_header_data the data contained in the TLM box.
2702 * @param p_j2k the jpeg2000 codec.
2703 * @param p_header_size the size of the data contained in the TLM marker.
2704 * @param p_manager the user event manager.
2706 opj_bool j2k_read_tlm_v2 (
2707 opj_j2k_v2_t *p_j2k,
2708 OPJ_BYTE * p_header_data,
2709 OPJ_UINT32 p_header_size,
2710 struct opj_event_mgr * p_manager
2713 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
2715 assert(p_header_data != 00);
2716 assert(p_j2k != 00);
2717 assert(p_manager != 00);
2719 if (p_header_size < 2) {
2720 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2725 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
2727 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
2730 l_ST = ((l_Stlm >> 4) & 0x3);
2731 l_SP = (l_Stlm >> 6) & 0x1;
2733 l_Ptlm_size = (l_SP + 1) * 2;
2734 l_quotient = l_Ptlm_size + l_ST;
2736 l_tot_num_tp = p_header_size / l_quotient;
2737 l_tot_num_tp_remaining = p_header_size % l_quotient;
2739 if (l_tot_num_tp_remaining != 0) {
2740 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2743 /* FIXME Do not care of this at the moment since only local variables are set here */
2746 (i = 0; i < l_tot_num_tp; ++i)
2748 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
2749 p_header_data += l_ST;
2750 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
2751 p_header_data += l_Ptlm_size;
2756 static void j2k_read_plm(opj_j2k_t *j2k) {
2757 int len, i, Zplm, Nplm, add, packet_len = 0;
2759 opj_cio_t *cio = j2k->cio;
2761 len = cio_read(cio, 2); /* Lplm */
2762 Zplm = cio_read(cio, 1); /* Zplm */
2765 Nplm = cio_read(cio, 4); /* Nplm */
2767 for (i = Nplm; i > 0; i--) {
2768 add = cio_read(cio, 1);
2770 packet_len = (packet_len << 7) + add; /* Iplm_ij */
2771 if ((add & 0x80) == 0) {
2782 * Reads a PLM marker (Packet length, main header marker)
2784 * @param p_header_data the data contained in the TLM box.
2785 * @param p_j2k the jpeg2000 codec.
2786 * @param p_header_size the size of the data contained in the TLM marker.
2787 * @param p_manager the user event manager.
2789 opj_bool j2k_read_plm_v2 (
2790 opj_j2k_v2_t *p_j2k,
2791 OPJ_BYTE * p_header_data,
2792 OPJ_UINT32 p_header_size,
2793 struct opj_event_mgr * p_manager
2797 assert(p_header_data != 00);
2798 assert(p_j2k != 00);
2799 assert(p_manager != 00);
2801 if (p_header_size < 1) {
2802 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2805 /* Do not care of this at the moment since only local variables are set here */
2807 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
2814 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
2816 p_header_size -= (1+l_Nplm);
2820 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2824 (i = 0; i < l_Nplm; ++i)
2826 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
2828 // take only the last seven bytes
2829 l_packet_len |= (l_tmp & 0x7f);
2837 // store packet length and proceed to next packet
2844 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2852 static void j2k_read_plt(opj_j2k_t *j2k) {
2853 int len, i, Zplt, packet_len = 0, add;
2855 opj_cio_t *cio = j2k->cio;
2857 len = cio_read(cio, 2); /* Lplt */
2858 Zplt = cio_read(cio, 1); /* Zplt */
2859 for (i = len - 3; i > 0; i--) {
2860 add = cio_read(cio, 1);
2861 packet_len = (packet_len << 7) + add; /* Iplt_i */
2862 if ((add & 0x80) == 0) {
2870 * Reads a PLT marker (Packet length, tile-part header)
2872 * @param p_header_data the data contained in the PLT box.
2873 * @param p_j2k the jpeg2000 codec.
2874 * @param p_header_size the size of the data contained in the PLT marker.
2875 * @param p_manager the user event manager.
2877 opj_bool j2k_read_plt_v2 (
2878 opj_j2k_v2_t *p_j2k,
2879 OPJ_BYTE * p_header_data,
2880 OPJ_UINT32 p_header_size,
2881 struct opj_event_mgr * p_manager
2884 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
2887 assert(p_header_data != 00);
2888 assert(p_j2k != 00);
2889 assert(p_manager != 00);
2891 if (p_header_size < 1) {
2892 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2896 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
2900 for (i = 0; i < p_header_size; ++i) {
2901 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
2903 // take only the last seven bytes
2904 l_packet_len |= (l_tmp & 0x7f);
2909 // store packet length and proceed to next packet
2914 if (l_packet_len != 0) {
2915 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2922 static void j2k_read_ppm(opj_j2k_t *j2k) {
2923 int len, Z_ppm, i, j;
2926 opj_cp_t *cp = j2k->cp;
2927 opj_cio_t *cio = j2k->cio;
2929 len = cio_read(cio, 2);
2932 Z_ppm = cio_read(cio, 1); /* Z_ppm */
2935 if (cp->ppm_previous == 0) {
2936 N_ppm = cio_read(cio, 4); /* N_ppm */
2939 N_ppm = cp->ppm_previous;
2942 if (Z_ppm == 0) { /* First PPM marker */
2943 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
2944 cp->ppm_data_first = cp->ppm_data;
2945 cp->ppm_len = N_ppm;
2946 } else { /* NON-first PPM marker */
2947 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
2950 /* this memory allocation check could be done even in non-JPWL cases */
2952 if (!cp->ppm_data) {
2953 opj_event_msg(j2k->cinfo, EVT_ERROR,
2954 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
2956 if (!JPWL_ASSUME || JPWL_ASSUME) {
2957 opj_free(cp->ppm_data);
2958 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2965 cp->ppm_data_first = cp->ppm_data;
2966 cp->ppm_len = N_ppm + cp->ppm_store;
2968 for (i = N_ppm; i > 0; i--) { /* Read packet header */
2969 cp->ppm_data[j] = cio_read(cio, 1);
2973 break; /* Case of non-finished packet header in present marker but finished in next one */
2975 cp->ppm_previous = i - 1;
2980 * Reads a PPM marker (Packed packet headers, main header)
2982 * @param p_header_data the data contained in the POC box.
2983 * @param p_j2k the jpeg2000 codec.
2984 * @param p_header_size the size of the data contained in the POC marker.
2985 * @param p_manager the user event manager.
2987 opj_bool j2k_read_ppm_v2 (
2988 opj_j2k_v2_t *p_j2k,
2989 OPJ_BYTE * p_header_data,
2990 OPJ_UINT32 p_header_size,
2991 struct opj_event_mgr * p_manager
2995 opj_cp_v2_t *l_cp = 00;
2996 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
2999 assert(p_header_data != 00);
3000 assert(p_j2k != 00);
3001 assert(p_manager != 00);
3003 if (p_header_size < 1) {
3004 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3008 l_cp = &(p_j2k->m_cp);
3011 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3017 if (p_header_size < 4) {
3018 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3022 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3026 /* First PPM marker: Initialization */
3027 l_cp->ppm_len = l_N_ppm;
3028 l_cp->ppm_data_size = 0;
3030 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3031 if (l_cp->ppm_buffer == 00) {
3032 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3035 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3037 l_cp->ppm_data = l_cp->ppm_buffer;
3041 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3042 if (p_header_size >= 4) {
3044 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3047 l_cp->ppm_len += l_N_ppm ;
3049 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3050 if (l_cp->ppm_buffer == 00) {
3051 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3054 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3056 l_cp->ppm_data = l_cp->ppm_buffer;
3063 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3065 if (l_remaining_data <= p_header_size) {
3066 /* we must store less information than available in the packet */
3067 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3068 l_cp->ppm_data_size = l_cp->ppm_len;
3069 p_header_size -= l_remaining_data;
3070 p_header_data += l_remaining_data;
3073 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3074 l_cp->ppm_data_size += p_header_size;
3075 p_header_data += p_header_size;
3087 * Reads a PPM marker (Packed packet headers, main header)
3089 * @param p_header_data the data contained in the POC box.
3090 * @param p_j2k the jpeg2000 codec.
3091 * @param p_header_size the size of the data contained in the POC marker.
3092 * @param p_manager the user event manager.
3094 opj_bool j2k_read_ppm_v3 (
3095 opj_j2k_v2_t *p_j2k,
3096 OPJ_BYTE * p_header_data,
3097 OPJ_UINT32 p_header_size,
3098 struct opj_event_mgr * p_manager
3101 opj_cp_v2_t *l_cp = 00;
3102 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3105 assert(p_header_data != 00);
3106 assert(p_j2k != 00);
3107 assert(p_manager != 00);
3109 // Minimum size of PPM marker is equal to the size of Zppm element
3110 if (p_header_size < 1) {
3111 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3115 l_cp = &(p_j2k->m_cp);
3118 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3124 // We need now at least the Nppm^0 element
3125 if (p_header_size < 4) {
3126 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3130 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3134 /* First PPM marker: Initialization */
3135 l_cp->ppm_len = l_N_ppm;
3136 l_cp->ppm_data_read = 0;
3138 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3139 if (l_cp->ppm_data == 00) {
3140 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3143 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3145 l_cp->ppm_data_current = l_cp->ppm_data;
3147 //l_cp->ppm_data = l_cp->ppm_buffer;
3150 if (p_header_size < 4) {
3151 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3155 // Uncompleted Ippm series in the previous PPM marker?
3156 if (l_cp->ppm_data_read < l_cp->ppm_len) {
3157 // Get the place where add the remaining Ippm series
3158 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3159 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3162 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3166 // Increase the size of ppm_data to add the new Ippm series
3167 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3169 // Keep the position of the place where concatenate the new series
3170 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3171 l_cp->ppm_len += l_N_ppm;
3176 l_remaining_data = p_header_size;
3178 while (l_remaining_data >= l_N_ppm) {
3179 // read a complete Ippm series
3180 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3181 p_header_size -= l_N_ppm;
3182 p_header_data += l_N_ppm;
3184 l_cp->ppm_data_read += l_N_ppm; // Increase the number of data read
3188 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
3193 l_remaining_data = p_header_size;
3197 l_remaining_data = p_header_size;
3199 // Next Ippm series is a complete series ?
3200 if (l_remaining_data > l_N_ppm) {
3201 // Increase the size of ppm_data to add the new Ippm series
3202 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3204 // Keep the position of the place where concatenate the new series
3205 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3206 l_cp->ppm_len += l_N_ppm;
3211 // Need to read an incomplete Ippm series
3212 if (l_remaining_data) {
3213 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3215 // Keep the position of the place where concatenate the new series
3216 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3217 l_cp->ppm_len += l_N_ppm;
3219 // Read incomplete Ippm series
3220 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3221 p_header_size -= l_remaining_data;
3222 p_header_data += l_remaining_data;
3224 l_cp->ppm_data_read += l_remaining_data; // Increase the number of data read
3229 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3230 if (p_header_size >= 4) {
3232 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3235 l_cp->ppm_len += l_N_ppm ;
3237 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3238 if (l_cp->ppm_buffer == 00) {
3239 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3242 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3244 l_cp->ppm_data = l_cp->ppm_buffer;
3251 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3253 if (l_remaining_data <= p_header_size) {
3254 /* we must store less information than available in the packet */
3255 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3256 l_cp->ppm_data_size = l_cp->ppm_len;
3257 p_header_size -= l_remaining_data;
3258 p_header_data += l_remaining_data;
3261 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3262 l_cp->ppm_data_size += p_header_size;
3263 p_header_data += p_header_size;
3272 static void j2k_read_ppt(opj_j2k_t *j2k) {
3273 int len, Z_ppt, i, j = 0;
3275 opj_cp_t *cp = j2k->cp;
3276 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
3277 opj_cio_t *cio = j2k->cio;
3279 len = cio_read(cio, 2);
3280 Z_ppt = cio_read(cio, 1);
3282 if (Z_ppt == 0) { /* First PPT marker */
3283 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
3284 tcp->ppt_data_first = tcp->ppt_data;
3286 tcp->ppt_len = len - 3;
3287 } else { /* NON-first PPT marker */
3288 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
3289 tcp->ppt_data_first = tcp->ppt_data;
3290 tcp->ppt_len = len - 3 + tcp->ppt_store;
3293 for (i = len - 3; i > 0; i--) {
3294 tcp->ppt_data[j] = cio_read(cio, 1);
3301 * Reads a PPT marker (Packed packet headers, tile-part header)
3303 * @param p_header_data the data contained in the PPT box.
3304 * @param p_j2k the jpeg2000 codec.
3305 * @param p_header_size the size of the data contained in the PPT marker.
3306 * @param p_manager the user event manager.
3308 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
3309 OPJ_BYTE * p_header_data,
3310 OPJ_UINT32 p_header_size,
3311 struct opj_event_mgr * p_manager )
3313 opj_cp_v2_t *l_cp = 00;
3314 opj_tcp_v2_t *l_tcp = 00;
3318 assert(p_header_data != 00);
3319 assert(p_j2k != 00);
3320 assert(p_manager != 00);
3322 /* We need to have the Z_ppt element at minimum */
3323 if (p_header_size < 1) {
3324 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3328 l_cp = &(p_j2k->m_cp);
3330 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");
3334 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3337 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3341 /* Allocate buffer to read the packet header */
3343 /* First PPT marker */
3344 l_tcp->ppt_data_size = 0;
3345 l_tcp->ppt_len = p_header_size;
3347 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3348 if (l_tcp->ppt_buffer == 00) {
3349 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3352 l_tcp->ppt_data = l_tcp->ppt_buffer;
3354 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3357 l_tcp->ppt_len += p_header_size;
3359 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
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+l_tcp->ppt_data_size,0,p_header_size);
3369 /* Read packet header from buffer */
3370 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3372 l_tcp->ppt_data_size += p_header_size;
3377 static void j2k_write_tlm(opj_j2k_t *j2k){
3379 opj_cio_t *cio = j2k->cio;
3380 j2k->tlm_start = cio_tell(cio);
3381 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
3382 lenp = 4 + (5*j2k->totnum_tp);
3383 cio_write(cio,lenp,2); /* Ltlm */
3384 cio_write(cio, 0,1); /* Ztlm=0*/
3385 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3386 cio_skip(cio,5*j2k->totnum_tp);
3389 static void j2k_write_sot(opj_j2k_t *j2k) {
3392 opj_cio_t *cio = j2k->cio;
3394 j2k->sot_start = cio_tell(cio);
3395 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
3396 lenp = cio_tell(cio);
3397 cio_skip(cio, 2); /* Lsot (further) */
3398 cio_write(cio, j2k->curtileno, 2); /* Isot */
3399 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
3400 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
3401 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
3402 len = cio_tell(cio) - lenp;
3403 cio_seek(cio, lenp);
3404 cio_write(cio, len, 2); /* Lsot */
3405 cio_seek(cio, lenp + len);
3409 /* update markers struct */
3410 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
3411 #endif /* USE_JPWL */
3414 if( j2k->cstr_info && j2k->cur_tp_num==0){
3415 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
3419 static void j2k_read_sot(opj_j2k_t *j2k) {
3420 int len, tileno, totlen, partno, numparts, i;
3421 opj_tcp_t *tcp = NULL;
3424 opj_cp_t *cp = j2k->cp;
3425 opj_cio_t *cio = j2k->cio;
3427 len = cio_read(cio, 2);
3428 tileno = cio_read(cio, 2);
3431 if (j2k->cp->correct) {
3433 static int backup_tileno = 0;
3435 /* tileno is negative or larger than the number of tiles!!! */
3436 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3437 opj_event_msg(j2k->cinfo, EVT_ERROR,
3438 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3439 tileno, (cp->tw * cp->th));
3441 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3444 /* we try to correct */
3445 tileno = backup_tileno;
3446 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3447 "- setting tile number to %d\n",
3451 /* keep your private count of tiles */
3454 #endif /* USE_JPWL */
3456 if (cp->tileno_size == 0) {
3457 cp->tileno[cp->tileno_size] = tileno;
3461 while (i < cp->tileno_size && status == 0) {
3462 status = cp->tileno[i] == tileno ? 1 : 0;
3466 cp->tileno[cp->tileno_size] = tileno;
3471 totlen = cio_read(cio, 4);
3474 if (j2k->cp->correct) {
3476 /* totlen is negative or larger than the bytes left!!! */
3477 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
3478 opj_event_msg(j2k->cinfo, EVT_ERROR,
3479 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3480 totlen, cio_numbytesleft(cio) + 8);
3482 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3485 /* we try to correct */
3487 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3488 "- setting Psot to %d => assuming it is the last tile\n",
3493 #endif /* USE_JPWL */
3496 totlen = cio_numbytesleft(cio) + 8;
3498 partno = cio_read(cio, 1);
3499 numparts = cio_read(cio, 1);
3501 if (partno >= numparts) {
3502 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);
3503 numparts = partno+1;
3506 j2k->curtileno = tileno;
3507 j2k->cur_tp_num = partno;
3508 j2k->eot = cio_getbp(cio) - 12 + totlen;
3509 j2k->state = J2K_STATE_TPH;
3510 tcp = &cp->tcps[j2k->curtileno];
3513 if (j2k->cstr_info) {
3516 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
3517 j2k->cstr_info->tile[tileno].tileno = tileno;
3518 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
3519 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3521 j2k->cstr_info->tile[tileno].end_pos += totlen;
3523 j2k->cstr_info->tile[tileno].num_tps = numparts;
3525 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
3527 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)
3528 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
3529 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3530 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3533 if (tcp->first == 1) {
3534 /* Initialization PPT */
3535 opj_tccp_t *tmp = tcp->tccps;
3536 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
3538 tcp->ppt_data = NULL;
3539 tcp->ppt_data_first = NULL;
3542 for (i = 0; i < j2k->image->numcomps; i++) {
3543 tcp->tccps[i] = j2k->default_tcp->tccps[i];
3545 cp->tcps[j2k->curtileno].first = 0;
3550 * Reads a PPT marker (Packed packet headers, tile-part header)
3552 * @param p_header_data the data contained in the PPT box.
3553 * @param p_j2k the jpeg2000 codec.
3554 * @param p_header_size the size of the data contained in the PPT marker.
3555 * @param p_manager the user event manager.
3557 opj_bool j2k_read_sot_v2 (
3558 opj_j2k_v2_t *p_j2k,
3559 OPJ_BYTE * p_header_data,
3560 OPJ_UINT32 p_header_size,
3561 struct opj_event_mgr * p_manager
3565 opj_cp_v2_t *l_cp = 00;
3566 opj_tcp_v2_t *l_tcp = 00;
3567 OPJ_UINT32 l_tot_len, l_num_parts = 0;
3568 OPJ_UINT32 l_current_part;
3569 OPJ_UINT32 l_tile_x,l_tile_y;
3572 assert(p_header_data != 00);
3573 assert(p_j2k != 00);
3574 assert(p_manager != 00);
3576 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
3577 if (p_header_size != 8) {
3578 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3582 l_cp = &(p_j2k->m_cp);
3583 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
3586 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3587 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
3588 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
3591 if (l_cp->correct) {
3593 int tileno = p_j2k->m_current_tile_number;
3594 static int backup_tileno = 0;
3596 /* tileno is negative or larger than the number of tiles!!! */
3597 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
3598 opj_event_msg_v2(p_manager, EVT_ERROR,
3599 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3600 tileno, (l_cp->tw * l_cp->th));
3602 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3605 /* we try to correct */
3606 tileno = backup_tileno;
3607 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3608 "- setting tile number to %d\n",
3612 /* keep your private count of tiles */
3615 #endif /* USE_JPWL */
3617 /* look for the tile in the list of already processed tile (in parts). */
3618 /* Optimization possible here with a more complex data structure and with the removing of tiles */
3619 /* since the time taken by this function can only grow at the time */
3621 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
3625 if (l_cp->correct) {
3627 /* totlen is negative or larger than the bytes left!!! */
3628 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))) { */
3629 opj_event_msg_v2(p_manager, EVT_ERROR,
3630 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3631 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
3633 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3636 /* we try to correct */
3638 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3639 "- setting Psot to %d => assuming it is the last tile\n",
3643 #endif /* USE_JPWL */
3645 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
3647 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
3648 "we assuming it is the last tile-part of the codestream.\n");
3649 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3652 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
3655 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
3658 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
3659 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
3660 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
3661 if (l_tcp->m_nb_tile_parts) {
3662 if (l_current_part >= l_tcp->m_nb_tile_parts){
3663 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
3664 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
3665 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3669 l_tcp->m_nb_tile_parts = l_num_parts;
3672 /* If know the number of tile part header we will check if we didn't read the last*/
3673 if (l_tcp->m_nb_tile_parts) {
3674 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
3675 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
3679 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
3680 /* Keep the size of data to skip after this marker */
3681 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
3684 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
3685 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
3688 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
3690 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
3691 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
3692 p_j2k->m_specific_param.m_decoder.m_skip_data =
3693 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
3694 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
3695 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
3696 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
3699 p_j2k->m_specific_param.m_decoder.m_skip_data =
3700 (p_j2k->m_current_tile_number != p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
3703 if (p_j2k->cstr_index)
3705 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
3706 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
3708 if (l_num_parts != 0){
3709 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
3710 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
3713 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
3714 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3715 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
3717 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3718 (opj_tp_index_t*)opj_realloc(
3719 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
3720 l_num_parts* sizeof(opj_tp_index_t));
3723 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
3725 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
3726 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
3727 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3728 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
3729 sizeof(opj_tp_index_t));
3732 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
3733 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
3734 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
3735 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
3736 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
3737 * sizeof(opj_tp_index_t));
3746 /* 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 */
3747 /* if (p_j2k->cstr_info) {
3750 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
3753 p_j2k->cstr_info->tile[tileno].tileno = tileno;
3754 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
3755 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3756 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
3759 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
3762 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
3766 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
3769 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
3770 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3771 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3776 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
3779 opj_tcp_t *tcp = NULL;
3780 opj_codestream_info_t *cstr_info = NULL;
3782 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
3783 opj_cp_t *cp = j2k->cp;
3784 opj_cio_t *cio = j2k->cio;
3786 tcd->tp_num = j2k->tp_num ;
3787 tcd->cur_tp_num = j2k->cur_tp_num;
3789 cio_write(cio, J2K_MS_SOD, 2);
3791 if( j2k->cstr_info && j2k->cur_tp_num==0){
3792 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
3795 if (j2k->curtileno == 0) {
3796 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
3800 cstr_info = j2k->cstr_info;
3802 if (!j2k->cur_tp_num ) {
3803 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3804 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
3807 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
3808 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
3812 /* update markers struct */
3813 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
3814 #endif /* USE_JPWL */
3819 tcp = &cp->tcps[j2k->curtileno];
3820 for (layno = 0; layno < tcp->numlayers; layno++) {
3821 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
3822 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
3823 } else if (tcp->rates[layno]) {
3824 tcp->rates[layno]=1;
3827 if(j2k->cur_tp_num == 0){
3828 tcd->tcd_image->tiles->packno = 0;
3830 cstr_info->packno = 0;
3833 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
3835 /* Writing Psot in SOT marker */
3836 totlen = cio_tell(cio) + l - j2k->sot_start;
3837 cio_seek(cio, j2k->sot_start + 6);
3838 cio_write(cio, totlen, 4);
3839 cio_seek(cio, j2k->sot_start + totlen);
3840 /* Writing Ttlm and Ptlm in TLM marker */
3842 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
3843 cio_write(cio, j2k->curtileno, 1);
3844 cio_write(cio, totlen, 4);
3846 cio_seek(cio, j2k->sot_start + totlen);
3849 static void j2k_read_sod(opj_j2k_t *j2k) {
3850 int len, truncate = 0, i;
3851 unsigned char *data = NULL, *data_ptr = NULL;
3853 opj_cio_t *cio = j2k->cio;
3854 int curtileno = j2k->curtileno;
3857 if (j2k->cstr_info) {
3858 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
3859 cio_tell(cio) + j2k->pos_correction - 1;
3860 if (j2k->cur_tp_num == 0)
3861 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3862 j2k->cstr_info->packno = 0;
3865 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
3867 if (len == cio_numbytesleft(cio) + 1) {
3868 truncate = 1; /* Case of a truncate codestream */
3871 data = j2k->tile_data[curtileno];
3872 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
3874 data_ptr = data + j2k->tile_len[curtileno];
3875 for (i = 0; i < len; i++) {
3876 data_ptr[i] = cio_read(cio, 1);
3879 j2k->tile_len[curtileno] += len;
3880 j2k->tile_data[curtileno] = data;
3883 j2k->state = J2K_STATE_TPHSOT;
3885 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
3891 * Reads a SOD marker (Start Of Data)
3893 * @param p_header_data the data contained in the SOD box.
3894 * @param p_j2k the jpeg2000 codec.
3895 * @param p_header_size the size of the data contained in the SOD marker.
3896 * @param p_manager the user event manager.
3898 opj_bool j2k_read_sod_v2 (
3899 opj_j2k_v2_t *p_j2k,
3900 struct opj_stream_private *p_stream,
3901 struct opj_event_mgr * p_manager
3904 OPJ_UINT32 l_current_read_size;
3905 opj_codestream_index_t * l_cstr_index = 00;
3906 OPJ_BYTE ** l_current_data = 00;
3907 opj_tcp_v2_t * l_tcp = 00;
3908 OPJ_UINT32 * l_tile_len = 00;
3911 assert(p_j2k != 00);
3912 assert(p_manager != 00);
3913 assert(p_stream != 00);
3915 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
3917 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part)
3918 p_j2k->m_specific_param.m_decoder.m_sot_length = opj_stream_get_number_byte_left(p_stream) - 2;
3920 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
3922 l_current_data = &(l_tcp->m_data);
3923 l_tile_len = &l_tcp->m_data_size;
3925 if (! *l_current_data) {
3926 *l_current_data = (OPJ_BYTE*) opj_malloc/*FIXME V2 -> my_opj_malloc*/(p_j2k->m_specific_param.m_decoder.m_sot_length);
3929 *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);
3932 if (*l_current_data == 00) {
3933 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
3939 l_cstr_index = p_j2k->cstr_index;
3941 OPJ_SIZE_T l_current_pos = opj_stream_tell(p_stream) - 2;
3942 OPJ_UINT32 l_current_tile_part =l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
3943 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
3945 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
3946 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
3948 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
3952 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
3954 /*l_cstr_index->packno = 0;*/
3963 l_current_read_size = opj_stream_read_data( p_stream,
3964 *l_current_data + *l_tile_len,
3965 p_j2k->m_specific_param.m_decoder.m_sot_length,
3968 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
3969 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
3972 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
3975 *l_tile_len += l_current_read_size;
3981 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
3982 opj_cp_t *cp = j2k->cp;
3983 opj_tcp_t *tcp = &cp->tcps[tileno];
3984 opj_cio_t *cio = j2k->cio;
3985 int numcomps = j2k->image->numcomps;
3987 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
3988 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
3989 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
3990 cio_write(cio, 0, 1); /* Srgn */
3991 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
3994 static void j2k_read_rgn(opj_j2k_t *j2k) {
3995 int len, compno, roisty;
3997 opj_cp_t *cp = j2k->cp;
3998 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3999 opj_cio_t *cio = j2k->cio;
4000 int numcomps = j2k->image->numcomps;
4002 len = cio_read(cio, 2); /* Lrgn */
4003 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
4004 roisty = cio_read(cio, 1); /* Srgn */
4007 if (j2k->cp->correct) {
4008 /* totlen is negative or larger than the bytes left!!! */
4009 if (compno >= numcomps) {
4010 opj_event_msg(j2k->cinfo, EVT_ERROR,
4011 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4013 if (!JPWL_ASSUME || JPWL_ASSUME) {
4014 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4019 #endif /* USE_JPWL */
4021 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
4024 static void j2k_write_eoc(opj_j2k_t *j2k) {
4025 opj_cio_t *cio = j2k->cio;
4026 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
4027 cio_write(cio, J2K_MS_EOC, 2);
4031 /* update markers struct */
4032 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
4033 #endif /* USE_JPWL */
4038 * Reads a RGN marker (Region Of Interest)
4040 * @param p_header_data the data contained in the POC box.
4041 * @param p_j2k the jpeg2000 codec.
4042 * @param p_header_size the size of the data contained in the POC marker.
4043 * @param p_manager the user event manager.
4045 opj_bool j2k_read_rgn_v2 (
4046 opj_j2k_v2_t *p_j2k,
4047 OPJ_BYTE * p_header_data,
4048 OPJ_UINT32 p_header_size,
4049 struct opj_event_mgr * p_manager
4052 OPJ_UINT32 l_nb_comp;
4053 opj_image_t * l_image = 00;
4055 opj_cp_v2_t *l_cp = 00;
4056 opj_tcp_v2_t *l_tcp = 00;
4057 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4060 assert(p_header_data != 00);
4061 assert(p_j2k != 00);
4062 assert(p_manager != 00);
4064 l_image = p_j2k->m_private_image;
4065 l_nb_comp = l_image->numcomps;
4067 if (l_nb_comp <= 256) {
4072 if (p_header_size != 2 + l_comp_room) {
4073 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4077 l_cp = &(p_j2k->m_cp);
4078 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4079 &l_cp->tcps[p_j2k->m_current_tile_number] :
4080 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4082 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4083 p_header_data+=l_comp_room;
4084 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4088 if (l_cp->correct) {
4089 /* totlen is negative or larger than the bytes left!!! */
4090 if (l_comp_room >= l_nb_comp) {
4091 opj_event_msg_v2(p_manager, EVT_ERROR,
4092 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4093 l_comp_room, l_nb_comp);
4094 if (!JPWL_ASSUME || JPWL_ASSUME) {
4095 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4100 #endif /* USE_JPWL */
4102 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4109 static void j2k_read_eoc(opj_j2k_t *j2k) {
4113 /* if packets should be decoded */
4114 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
4115 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
4116 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
4117 for (i = 0; i < j2k->cp->tileno_size; i++) {
4118 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
4119 tileno = j2k->cp->tileno[i];
4120 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
4121 opj_free(j2k->tile_data[tileno]);
4122 j2k->tile_data[tileno] = NULL;
4123 tcd_free_decode_tile(tcd, i);
4124 if (success == OPJ_FALSE) {
4125 j2k->state |= J2K_STATE_ERR;
4129 tcd_free_decode(tcd);
4132 /* if packets should not be decoded */
4134 for (i = 0; i < j2k->cp->tileno_size; i++) {
4135 tileno = j2k->cp->tileno[i];
4136 opj_free(j2k->tile_data[tileno]);
4137 j2k->tile_data[tileno] = NULL;
4140 if (j2k->state & J2K_STATE_ERR)
4141 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
4143 j2k->state = J2K_STATE_MT;
4147 * Reads a EOC marker (End Of Codestream)
4149 * @param p_header_data the data contained in the SOD box.
4150 * @param p_j2k the jpeg2000 codec.
4151 * @param p_header_size the size of the data contained in the SOD marker.
4152 * @param p_manager the user event manager.
4154 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
4155 struct opj_stream_private *p_stream,
4156 struct opj_event_mgr * p_manager )
4159 opj_tcd_v2_t * l_tcd = 00;
4160 OPJ_UINT32 l_nb_tiles;
4161 opj_tcp_v2_t * l_tcp = 00;
4165 assert(p_j2k != 00);
4166 assert(p_manager != 00);
4167 assert(p_stream != 00);
4169 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4170 l_tcp = p_j2k->m_cp.tcps;
4172 l_tcd = tcd_create_v2(OPJ_TRUE);
4174 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4178 for (i = 0; i < l_nb_tiles; ++i) {
4179 if (l_tcp->m_data) {
4180 if (! tcd_init_decode_tile(l_tcd, i)) {
4181 tcd_destroy_v2(l_tcd);
4182 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4186 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4190 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4195 j2k_tcp_destroy(l_tcp);
4199 tcd_destroy_v2(l_tcd);
4203 typedef struct opj_dec_mstabent {
4206 /** value of the state when the marker can appear */
4208 /** action linked to the marker */
4209 void (*handler) (opj_j2k_t *j2k);
4210 } opj_dec_mstabent_t;
4212 opj_dec_mstabent_t j2k_dec_mstab[] = {
4213 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
4214 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
4215 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
4216 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
4217 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
4218 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
4219 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
4220 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
4221 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
4222 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
4223 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
4224 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
4225 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
4226 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
4227 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
4228 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
4230 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
4231 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
4234 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
4235 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
4236 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
4237 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
4238 #endif /* USE_JPWL */
4240 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
4241 {J2K_MS_INSEC, 0, j2k_read_insec},
4242 #endif /* USE_JPSEC */
4244 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
4247 static void j2k_read_unk(opj_j2k_t *j2k) {
4248 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
4251 if (j2k->cp->correct) {
4253 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
4254 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4255 id = cio_read(j2k->cio, 2);
4256 opj_event_msg(j2k->cinfo, EVT_ERROR,
4257 "JPWL: really don't know this marker %x\n",
4260 opj_event_msg(j2k->cinfo, EVT_ERROR,
4261 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
4264 /* OK, activate this at your own risk!!! */
4265 /* we look for the marker at the minimum hamming distance from this */
4266 while (j2k_dec_mstab[m].id) {
4268 /* 1's where they differ */
4269 tmp_id = j2k_dec_mstab[m].id ^ id;
4271 /* compute the hamming distance between our id and the current */
4273 for (i = 0; i < 16; i++) {
4274 if ((tmp_id >> i) & 0x0001) {
4279 /* if current distance is smaller, set the minimum */
4280 if (cur_dist < min_dist) {
4281 min_dist = cur_dist;
4282 min_id = j2k_dec_mstab[m].id;
4285 /* jump to the next marker */
4289 /* do we substitute the marker? */
4290 if (min_dist < JPWL_MAXIMUM_HAMMING) {
4291 opj_event_msg(j2k->cinfo, EVT_ERROR,
4292 "- marker %x is at distance %d from the read %x\n",
4293 min_id, min_dist, id);
4294 opj_event_msg(j2k->cinfo, EVT_ERROR,
4295 "- trying to substitute in place and crossing fingers!\n");
4296 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4297 cio_write(j2k->cio, min_id, 2);
4300 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
4305 #endif /* USE_JPWL */
4310 * Reads an unknown marker
4312 * @param p_stream the stream object to read from.
4313 * @param p_j2k the jpeg2000 codec.
4314 * @param p_manager the user event manager.
4316 * @return true if the marker could be deduced.
4318 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
4319 struct opj_stream_private *p_stream,
4320 OPJ_UINT32 *output_marker,
4321 struct opj_event_mgr * p_manager
4324 OPJ_UINT32 l_unknown_marker;
4325 const opj_dec_memory_marker_handler_t * l_marker_handler;
4326 OPJ_UINT32 l_size_unk = 2;
4329 assert(p_j2k != 00);
4330 assert(p_manager != 00);
4331 assert(p_stream != 00);
4333 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
4336 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
4337 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
4338 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
4342 // read 2 bytes as the new marker ID
4343 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
4345 if (!(l_unknown_marker < 0xff00)) {
4347 // Get the marker handler from the marker ID
4348 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
4350 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
4351 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
4355 if (l_marker_handler->id != J2K_MS_UNK) {
4356 /* Add the marker to the codestream index*/
4357 if (l_marker_handler->id != J2K_MS_SOT)
4358 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
4359 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
4361 break; /* next marker is known and well located */
4369 *output_marker = l_marker_handler->id ;
4375 Read the lookup table containing all the marker, status and action
4376 @param id Marker value
4378 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
4379 opj_dec_mstabent_t *e;
4380 for (e = j2k_dec_mstab; e->id != 0; e++) {
4390 * Reads a MCT marker (Multiple Component Transform)
4392 * @param p_header_data the data contained in the MCT box.
4393 * @param p_j2k the jpeg2000 codec.
4394 * @param p_header_size the size of the data contained in the MCT marker.
4395 * @param p_manager the user event manager.
4397 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
4398 OPJ_BYTE * p_header_data,
4399 OPJ_UINT32 p_header_size,
4400 struct opj_event_mgr * p_manager )
4403 opj_tcp_v2_t *l_tcp = 00;
4406 opj_mct_data_t * l_mct_data;
4409 assert(p_header_data != 00);
4410 assert(p_j2k != 00);
4412 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4413 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4414 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4416 if (p_header_size < 2) {
4417 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4422 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
4425 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
4429 if(p_header_size <= 6) {
4430 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4434 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
4435 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
4438 l_indix = l_tmp & 0xff;
4439 l_mct_data = l_tcp->m_mct_records;
4441 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
4442 if (l_mct_data->m_index == l_indix) {
4449 if (i == l_tcp->m_nb_mct_records) {
4450 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
4451 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
4453 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));
4454 if(! l_tcp->m_mct_records) {
4455 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4459 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
4460 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
4463 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
4466 if (l_mct_data->m_data) {
4467 opj_free(l_mct_data->m_data);
4468 l_mct_data->m_data = 00;
4471 l_mct_data->m_index = l_indix;
4472 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
4473 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
4475 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
4478 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
4484 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
4485 if (! l_mct_data->m_data) {
4486 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
4489 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
4491 l_mct_data->m_data_size = p_header_size;
4492 ++l_tcp->m_nb_mct_records;
4498 * Reads a MCC marker (Multiple Component Collection)
4500 * @param p_header_data the data contained in the MCC box.
4501 * @param p_j2k the jpeg2000 codec.
4502 * @param p_header_size the size of the data contained in the MCC marker.
4503 * @param p_manager the user event manager.
4505 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
4506 OPJ_BYTE * p_header_data,
4507 OPJ_UINT32 p_header_size,
4508 struct opj_event_mgr * p_manager )
4513 opj_tcp_v2_t * l_tcp;
4514 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4515 opj_mct_data_t * l_mct_data;
4516 OPJ_UINT32 l_nb_collections;
4517 OPJ_UINT32 l_nb_comps;
4518 OPJ_UINT32 l_nb_bytes_by_comp;
4522 assert(p_header_data != 00);
4523 assert(p_j2k != 00);
4524 assert(p_manager != 00);
4526 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4527 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4528 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4530 if (p_header_size < 2) {
4531 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4536 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
4539 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
4543 if (p_header_size < 7) {
4544 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4548 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
4551 l_mcc_record = l_tcp->m_mcc_records;
4553 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
4554 if (l_mcc_record->m_index == l_indix) {
4561 if (i == l_tcp->m_nb_mcc_records) {
4562 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
4563 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
4565 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
4566 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
4567 if (! l_tcp->m_mcc_records) {
4568 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4571 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
4572 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));
4574 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
4576 l_mcc_record->m_index = l_indix;
4578 /* only one marker atm */
4579 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
4582 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
4586 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
4589 if (l_nb_collections > 1) {
4590 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
4596 for (i=0;i<l_nb_collections;++i) {
4597 if (p_header_size < 3) {
4598 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4602 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
4606 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
4610 opj_read_bytes(p_header_data,&l_nb_comps,2);
4615 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
4616 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
4618 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
4619 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4623 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
4625 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
4626 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
4627 p_header_data+=l_nb_bytes_by_comp;
4630 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
4635 opj_read_bytes(p_header_data,&l_nb_comps,2);
4638 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
4639 l_nb_comps &= 0x7fff;
4641 if (l_nb_comps != l_mcc_record->m_nb_comps) {
4642 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
4646 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
4647 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4651 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
4653 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
4654 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
4655 p_header_data+=l_nb_bytes_by_comp;
4658 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
4663 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
4666 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
4667 l_mcc_record->m_decorrelation_array = 00;
4668 l_mcc_record->m_offset_array = 00;
4670 l_indix = l_tmp & 0xff;
4672 l_mct_data = l_tcp->m_mct_records;
4673 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
4674 if (l_mct_data->m_index == l_indix) {
4675 l_mcc_record->m_decorrelation_array = l_mct_data;
4681 if (l_mcc_record->m_decorrelation_array == 00) {
4682 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4687 l_indix = (l_tmp >> 8) & 0xff;
4689 l_mct_data = l_tcp->m_mct_records;
4690 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
4691 if (l_mct_data->m_index == l_indix) {
4692 l_mcc_record->m_offset_array = l_mct_data;
4698 if (l_mcc_record->m_offset_array == 00) {
4699 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4705 if (p_header_size != 0) {
4706 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
4710 ++l_tcp->m_nb_mcc_records;
4716 * Reads a MCO marker (Multiple Component Transform Ordering)
4718 * @param p_header_data the data contained in the MCO box.
4719 * @param p_j2k the jpeg2000 codec.
4720 * @param p_header_size the size of the data contained in the MCO marker.
4721 * @param p_manager the user event manager.
4723 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
4724 OPJ_BYTE * p_header_data,
4725 OPJ_UINT32 p_header_size,
4726 struct opj_event_mgr * p_manager )
4728 OPJ_UINT32 l_tmp, i;
4729 OPJ_UINT32 l_nb_stages;
4730 opj_tcp_v2_t * l_tcp;
4731 opj_tccp_t * l_tccp;
4732 opj_image_t * l_image;
4733 opj_image_comp_t * l_img_comp;
4736 assert(p_header_data != 00);
4737 assert(p_j2k != 00);
4738 assert(p_manager != 00);
4740 l_image = p_j2k->m_private_image;
4741 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
4742 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
4743 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4745 if (p_header_size < 1) {
4746 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
4750 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
4753 if (l_nb_stages > 1) {
4754 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
4758 if (p_header_size != l_nb_stages + 1) {
4759 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
4763 l_tccp = l_tcp->tccps;
4764 l_img_comp = l_image->comps;
4766 for (i=0;i<l_image->numcomps;++i) {
4767 l_tccp->m_dc_level_shift = 0;
4771 if (l_tcp->m_mct_decoding_matrix) {
4772 opj_free(l_tcp->m_mct_decoding_matrix);
4773 l_tcp->m_mct_decoding_matrix = 00;
4776 for (i=0;i<l_nb_stages;++i) {
4777 opj_read_bytes(p_header_data,&l_tmp,1);
4780 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
4788 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
4791 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
4792 opj_mct_data_t * l_deco_array, * l_offset_array;
4793 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
4794 OPJ_UINT32 l_nb_elem;
4795 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
4796 opj_tccp_t * l_tccp;
4799 assert(p_tcp != 00);
4801 l_mcc_record = p_tcp->m_mcc_records;
4803 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
4804 if (l_mcc_record->m_index == p_index) {
4809 if (i==p_tcp->m_nb_mcc_records) {
4810 /** element discarded **/
4814 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
4815 /** do not support number of comps != image */
4819 l_deco_array = l_mcc_record->m_decorrelation_array;
4822 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
4823 if (l_deco_array->m_data_size != l_data_size) {
4827 l_nb_elem = p_image->numcomps * p_image->numcomps;
4828 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
4829 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
4831 if (! p_tcp->m_mct_decoding_matrix ) {
4835 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);
4838 l_offset_array = l_mcc_record->m_offset_array;
4840 if (l_offset_array) {
4841 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
4842 if (l_offset_array->m_data_size != l_data_size) {
4846 l_nb_elem = p_image->numcomps;
4847 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
4848 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
4850 if (! l_offset_data ) {
4854 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
4856 l_tccp = p_tcp->tccps;
4857 l_current_offset_data = l_offset_data;
4859 for (i=0;i<p_image->numcomps;++i) {
4860 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
4864 opj_free(l_offset_data);
4871 * Reads a CBD marker (Component bit depth definition)
4872 * @param p_header_data the data contained in the CBD box.
4873 * @param p_j2k the jpeg2000 codec.
4874 * @param p_header_size the size of the data contained in the CBD marker.
4875 * @param p_manager the user event manager.
4877 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
4878 OPJ_BYTE * p_header_data,
4879 OPJ_UINT32 p_header_size,
4880 struct opj_event_mgr * p_manager)
4882 OPJ_UINT32 l_nb_comp,l_num_comp;
4883 OPJ_UINT32 l_comp_def;
4885 opj_image_comp_t * l_comp = 00;
4888 assert(p_header_data != 00);
4889 assert(p_j2k != 00);
4890 assert(p_manager != 00);
4892 l_num_comp = p_j2k->m_private_image->numcomps;
4894 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
4895 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
4899 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
4902 if (l_nb_comp != l_num_comp) {
4903 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
4907 l_comp = p_j2k->m_private_image->comps;
4908 for (i=0;i<l_num_comp;++i) {
4909 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
4911 l_comp->sgnd = (l_comp_def>>7) & 1;
4912 l_comp->prec = (l_comp_def&0x7f) + 1;
4920 /* ----------------------------------------------------------------------- */
4921 /* J2K / JPT decoder interface */
4922 /* ----------------------------------------------------------------------- */
4924 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
4925 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4929 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
4930 if(!j2k->default_tcp) {
4936 j2k->tile_data = NULL;
4941 void j2k_destroy_decompress(opj_j2k_t *j2k) {
4944 if(j2k->tile_len != NULL) {
4945 opj_free(j2k->tile_len);
4947 if(j2k->tile_data != NULL) {
4948 opj_free(j2k->tile_data);
4950 if(j2k->default_tcp != NULL) {
4951 opj_tcp_t *default_tcp = j2k->default_tcp;
4952 if(default_tcp->ppt_data_first != NULL) {
4953 opj_free(default_tcp->ppt_data_first);
4955 if(j2k->default_tcp->tccps != NULL) {
4956 opj_free(j2k->default_tcp->tccps);
4958 opj_free(j2k->default_tcp);
4960 if(j2k->cp != NULL) {
4961 opj_cp_t *cp = j2k->cp;
4962 if(cp->tcps != NULL) {
4963 for(i = 0; i < cp->tw * cp->th; i++) {
4964 if(cp->tcps[i].ppt_data_first != NULL) {
4965 opj_free(cp->tcps[i].ppt_data_first);
4967 if(cp->tcps[i].tccps != NULL) {
4968 opj_free(cp->tcps[i].tccps);
4973 if(cp->ppm_data_first != NULL) {
4974 opj_free(cp->ppm_data_first);
4976 if(cp->tileno != NULL) {
4977 opj_free(cp->tileno);
4979 if(cp->comment != NULL) {
4980 opj_free(cp->comment);
4988 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
4989 if(j2k && parameters) {
4990 /* create and initialize the coding parameters structure */
4991 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4992 cp->reduce = parameters->cp_reduce;
4993 cp->layer = parameters->cp_layer;
4994 cp->limit_decoding = parameters->cp_limit_decoding;
4997 cp->correct = parameters->jpwl_correct;
4998 cp->exp_comps = parameters->jpwl_exp_comps;
4999 cp->max_tiles = parameters->jpwl_max_tiles;
5000 #endif /* USE_JPWL */
5003 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
5008 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
5010 if(j2k && parameters) {
5011 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
5012 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
5015 j2k->m_cp.correct = parameters->jpwl_correct;
5016 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
5017 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
5018 #endif /* USE_JPWL */
5022 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
5023 opj_image_t *image = NULL;
5025 opj_common_ptr cinfo = j2k->cinfo;
5028 j2k->cstr_info = cstr_info;
5030 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
5032 /* create an empty image */
5033 image = opj_image_create0();
5036 j2k->state = J2K_STATE_MHSOC;
5039 opj_dec_mstabent_t *e;
5040 int id = cio_read(cio, 2);
5043 /* we try to honor JPWL correction power */
5044 if (j2k->cp->correct) {
5046 int orig_pos = cio_tell(cio);
5049 /* call the corrector */
5050 status = jpwl_correct(j2k);
5052 /* go back to where you were */
5053 cio_seek(cio, orig_pos - 2);
5055 /* re-read the marker */
5056 id = cio_read(cio, 2);
5058 /* check whether it begins with ff */
5059 if (id >> 8 != 0xff) {
5060 opj_event_msg(cinfo, EVT_ERROR,
5061 "JPWL: possible bad marker %x at %d\n",
5062 id, cio_tell(cio) - 2);
5064 opj_image_destroy(image);
5065 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
5068 /* we try to correct */
5070 cio_seek(cio, cio_tell(cio) - 2);
5071 cio_write(cio, id, 2);
5072 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
5073 "- setting marker to %x\n",
5078 #endif /* USE_JPWL */
5080 if (id >> 8 != 0xff) {
5081 opj_image_destroy(image);
5082 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
5085 e = j2k_dec_mstab_lookup(id);
5086 // Check if the marker is known
5087 if (!(j2k->state & e->states)) {
5088 opj_image_destroy(image);
5089 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
5092 // Check if the decoding is limited to the main header
5093 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
5094 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
5101 if (j2k->state & J2K_STATE_ERR)
5104 if (j2k->state == J2K_STATE_MT) {
5107 if (j2k->state == J2K_STATE_NEOC) {
5111 if (j2k->state == J2K_STATE_NEOC) {
5115 if (j2k->state != J2K_STATE_MT) {
5116 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
5122 * Read a JPT-stream and decode file
5125 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
5126 opj_image_t *image = NULL;
5127 opj_jpt_msg_header_t header;
5129 opj_common_ptr cinfo = j2k->cinfo;
5131 OPJ_ARG_NOT_USED(cstr_info);
5135 /* create an empty image */
5136 image = opj_image_create0();
5139 j2k->state = J2K_STATE_MHSOC;
5141 /* Initialize the header */
5142 jpt_init_msg_header(&header);
5143 /* Read the first header of the message */
5144 jpt_read_msg_header(cinfo, cio, &header);
5146 position = cio_tell(cio);
5147 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
5148 opj_image_destroy(image);
5149 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
5154 opj_dec_mstabent_t *e = NULL;
5157 if (!cio_numbytesleft(cio)) {
5161 /* data-bin read -> need to read a new header */
5162 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
5163 jpt_read_msg_header(cinfo, cio, &header);
5164 position = cio_tell(cio);
5165 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
5166 opj_image_destroy(image);
5167 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
5172 id = cio_read(cio, 2);
5173 if (id >> 8 != 0xff) {
5174 opj_image_destroy(image);
5175 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
5178 e = j2k_dec_mstab_lookup(id);
5179 if (!(j2k->state & e->states)) {
5180 opj_image_destroy(image);
5181 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
5187 if (j2k->state == J2K_STATE_MT) {
5190 if (j2k->state == J2K_STATE_NEOC) {
5194 if (j2k->state == J2K_STATE_NEOC) {
5198 if (j2k->state != J2K_STATE_MT) {
5199 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
5205 /* ----------------------------------------------------------------------- */
5206 /* J2K encoder interface */
5207 /* ----------------------------------------------------------------------- */
5209 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
5210 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
5217 opj_j2k_v2_t* j2k_create_compress_v2()
5219 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
5224 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
5226 l_j2k->m_is_decoder = 0;
5227 l_j2k->m_cp.m_is_decoder = 0;
5229 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
5230 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5235 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
5237 // validation list creation
5238 l_j2k->m_validation_list = opj_procedure_list_create();
5239 if (! l_j2k->m_validation_list) {
5244 // execution list creation
5245 l_j2k->m_procedure_list = opj_procedure_list_create();
5246 if (! l_j2k->m_procedure_list) {
5254 void j2k_destroy_compress(opj_j2k_t *j2k) {
5258 if(j2k->cp != NULL) {
5259 opj_cp_t *cp = j2k->cp;
5262 opj_free(cp->comment);
5265 opj_free(cp->matrice);
5267 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5268 opj_free(cp->tcps[tileno].tccps);
5277 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
5278 int i, j, tileno, numpocs_tile;
5279 opj_cp_t *cp = NULL;
5281 if(!j2k || !parameters || ! image) {
5285 /* create and initialize the coding parameters structure */
5286 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
5288 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
5291 /* set default values for cp */
5296 copy user encoding parameters
5298 cp->cinema = parameters->cp_cinema;
5299 cp->max_comp_size = parameters->max_comp_size;
5300 cp->rsiz = parameters->cp_rsiz;
5301 cp->disto_alloc = parameters->cp_disto_alloc;
5302 cp->fixed_alloc = parameters->cp_fixed_alloc;
5303 cp->fixed_quality = parameters->cp_fixed_quality;
5305 /* mod fixed_quality */
5306 if(parameters->cp_matrice) {
5307 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
5308 cp->matrice = (int *) opj_malloc(array_size);
5309 memcpy(cp->matrice, parameters->cp_matrice, array_size);
5313 cp->tdx = parameters->cp_tdx;
5314 cp->tdy = parameters->cp_tdy;
5317 cp->tx0 = parameters->cp_tx0;
5318 cp->ty0 = parameters->cp_ty0;
5320 /* comment string */
5321 if(parameters->cp_comment) {
5322 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
5324 strcpy(cp->comment, parameters->cp_comment);
5329 calculate other encoding parameters
5332 if (parameters->tile_size_on) {
5333 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
5334 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
5336 cp->tdx = image->x1 - cp->tx0;
5337 cp->tdy = image->y1 - cp->ty0;
5340 if(parameters->tp_on){
5341 cp->tp_flag = parameters->tp_flag;
5346 for(i=0;i<image->numcomps ;i++){
5347 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
5353 calculate JPWL encoding parameters
5356 if (parameters->jpwl_epc_on) {
5360 cp->epc_on = OPJ_TRUE;
5361 cp->info_on = OPJ_FALSE; /* no informative technique */
5364 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
5365 cp->epb_on = OPJ_TRUE;
5367 cp->hprot_MH = parameters->jpwl_hprot_MH;
5368 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
5369 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
5370 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
5372 /* if tile specs are not specified, copy MH specs */
5373 if (cp->hprot_TPH[0] == -1) {
5374 cp->hprot_TPH_tileno[0] = 0;
5375 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
5377 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
5378 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
5379 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
5380 cp->pprot[i] = parameters->jpwl_pprot[i];
5384 /* set ESD writing */
5385 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
5386 cp->esd_on = OPJ_TRUE;
5388 cp->sens_size = parameters->jpwl_sens_size;
5389 cp->sens_addr = parameters->jpwl_sens_addr;
5390 cp->sens_range = parameters->jpwl_sens_range;
5392 cp->sens_MH = parameters->jpwl_sens_MH;
5393 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
5394 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
5395 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
5399 /* always set RED writing to false: we are at the encoder */
5400 cp->red_on = OPJ_FALSE;
5403 cp->epc_on = OPJ_FALSE;
5405 #endif /* USE_JPWL */
5408 /* initialize the mutiple tiles */
5409 /* ---------------------------- */
5410 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
5412 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5413 opj_tcp_t *tcp = &cp->tcps[tileno];
5414 tcp->numlayers = parameters->tcp_numlayers;
5415 for (j = 0; j < tcp->numlayers; j++) {
5417 if (cp->fixed_quality) {
5418 tcp->distoratio[j] = parameters->tcp_distoratio[j];
5420 tcp->rates[j] = parameters->tcp_rates[j];
5422 if (cp->fixed_quality) { /* add fixed_quality */
5423 tcp->distoratio[j] = parameters->tcp_distoratio[j];
5425 tcp->rates[j] = parameters->tcp_rates[j];
5429 tcp->csty = parameters->csty;
5430 tcp->prg = parameters->prog_order;
5431 tcp->mct = parameters->tcp_mct;
5435 if (parameters->numpocs) {
5436 /* initialisation of POC */
5438 for (i = 0; i < parameters->numpocs; i++) {
5439 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
5440 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
5441 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
5442 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
5443 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
5444 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
5445 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
5446 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
5447 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
5451 tcp->numpocs = numpocs_tile -1 ;
5456 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
5458 for (i = 0; i < image->numcomps; i++) {
5459 opj_tccp_t *tccp = &tcp->tccps[i];
5460 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
5461 tccp->numresolutions = parameters->numresolution;
5462 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
5463 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
5464 tccp->cblksty = parameters->mode;
5465 tccp->qmfbid = parameters->irreversible ? 0 : 1;
5466 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
5468 if (i == parameters->roi_compno) {
5469 tccp->roishift = parameters->roi_shift;
5474 if(parameters->cp_cinema)
5476 //Precinct size for lowest frequency subband=128
5479 //Precinct size at all other resolutions = 256
5480 for (j = 1; j < tccp->numresolutions; j++) {
5485 if (parameters->csty & J2K_CCP_CSTY_PRT) {
5487 for (j = tccp->numresolutions - 1; j >= 0; j--) {
5488 if (p < parameters->res_spec) {
5490 if (parameters->prcw_init[p] < 1) {
5493 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
5496 if (parameters->prch_init[p] < 1) {
5499 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
5503 int res_spec = parameters->res_spec;
5504 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
5505 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
5507 if (size_prcw < 1) {
5510 tccp->prcw[j] = int_floorlog2(size_prcw);
5513 if (size_prch < 1) {
5516 tccp->prch[j] = int_floorlog2(size_prch);
5520 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
5523 for (j = 0; j < tccp->numresolutions; j++) {
5530 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
5535 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
5537 opj_cp_t *cp = NULL;
5539 opj_tcd_t *tcd = NULL; /* TCD component */
5547 j2k->cstr_info = cstr_info;
5550 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
5551 cstr_info->image_w = image->x1 - image->x0;
5552 cstr_info->image_h = image->y1 - image->y0;
5553 cstr_info->prog = (&cp->tcps[0])->prg;
5554 cstr_info->tw = cp->tw;
5555 cstr_info->th = cp->th;
5556 cstr_info->tile_x = cp->tdx; /* new version parser */
5557 cstr_info->tile_y = cp->tdy; /* new version parser */
5558 cstr_info->tile_Ox = cp->tx0; /* new version parser */
5559 cstr_info->tile_Oy = cp->ty0; /* new version parser */
5560 cstr_info->numcomps = image->numcomps;
5561 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
5562 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
5563 for (compno=0; compno < image->numcomps; compno++) {
5564 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
5566 cstr_info->D_max = 0.0; /* ADD Marcela */
5567 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
5568 cstr_info->maxmarknum = 100;
5569 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
5570 cstr_info->marknum = 0;
5580 for (compno = 1; compno < image->numcomps; compno++) {
5581 j2k_write_coc(j2k, compno);
5582 j2k_write_qcc(j2k, compno);
5586 for (compno = 0; compno < image->numcomps; compno++) {
5587 opj_tcp_t *tcp = &cp->tcps[0];
5588 if (tcp->tccps[compno].roishift)
5589 j2k_write_rgn(j2k, compno, 0);
5591 if (cp->comment != NULL) {
5595 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
5599 if (cp->cinema == CINEMA4K_24) {
5604 /* uncomment only for testing JPSEC marker writing */
5605 /* j2k_write_sec(j2k); */
5609 cstr_info->main_head_end = cio_tell(cio) - 1;
5612 /**** Main Header ENDS here ***/
5614 /* create the tile encoder */
5615 tcd = tcd_create(j2k->cinfo);
5617 /* encode each tile */
5618 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
5622 int acc_pack_num = 0;
5626 opj_tcp_t *tcp = &cp->tcps[tileno];
5627 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
5629 j2k->curtileno = tileno;
5630 j2k->cur_tp_num = 0;
5631 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
5632 /* initialisation before tile encoding */
5634 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
5636 tcd_init_encode(tcd, image, cp, j2k->curtileno);
5641 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
5642 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
5643 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
5644 cstr_info->tile[j2k->curtileno].marknum = 0;
5648 for(pino = 0; pino <= tcp->numpocs; pino++) {
5652 /*Get number of tile parts*/
5653 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
5654 tcd->tp_pos = cp->tp_pos;
5656 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
5657 j2k->tp_num = tilepartno;
5660 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
5661 cio_tell(cio) + j2k->pos_correction;
5665 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
5666 for (compno = 1; compno < image->numcomps; compno++) {
5667 j2k_write_coc(j2k, compno);
5668 j2k_write_qcc(j2k, compno);
5670 if (cp->tcps[tileno].numpocs) {
5677 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5678 cio_tell(cio) + j2k->pos_correction + 1;
5681 j2k_write_sod(j2k, tcd);
5685 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
5686 cio_tell(cio) + j2k->pos_correction - 1;
5687 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
5689 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
5690 cstr_info->packno - acc_pack_num;
5691 acc_pack_num = cstr_info->packno;
5699 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
5704 if (tile->PPT) { // BAD PPT !!!
5707 PPT_file=fopen("PPT","rb");
5708 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
5709 for (i=0;i<tile->len_ppt;i++) {
5711 fread(&elmt, 1, 1, PPT_file);
5712 fwrite(&elmt,1,1,f);
5721 /* destroy the tile encoder */
5722 tcd_free_encode(tcd);
5725 opj_free(j2k->cur_totnum_tp);
5730 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
5732 /* The following adjustment is done to adjust the codestream size */
5733 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5734 /* the first bunch of bytes is not in the codestream */
5735 cstr_info->codestream_size -= cstr_info->main_head_start;
5741 preparation of JPWL marker segments
5745 /* encode according to JPWL */
5746 jpwl_encode(j2k, cio, image);
5749 #endif /* USE_JPWL */
5754 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
5759 /* expand the list? */
5760 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
5761 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
5762 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
5765 /* add the marker */
5766 cstr_info->marker[cstr_info->marknum].type = type;
5767 cstr_info->marker[cstr_info->marknum].pos = pos;
5768 cstr_info->marker[cstr_info->marknum].len = len;
5769 cstr_info->marknum++;
5773 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) {
5778 /* expand the list? */
5779 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
5780 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
5781 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
5784 /* add the marker */
5785 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
5786 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
5787 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
5788 cstr_index->marknum++;
5792 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
5795 opj_marker_info_t *marker;
5800 /* expand the list? */
5801 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
5802 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
5803 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
5806 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
5808 /* add the marker */
5809 marker->type = type;
5812 cstr_info->tile[tileno].marknum++;
5815 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len)
5821 if (!cstr_index->tile_index)
5823 /* expand the list? */
5824 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
5825 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
5826 cstr_index->tile_index[tileno].marker =
5827 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
5828 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
5831 /* add the marker */
5832 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
5833 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
5834 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
5835 cstr_index->tile_index[tileno].marknum++;
5837 if (type == J2K_MS_SOT) {
5838 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
5840 if (cstr_index->tile_index[tileno].tp_index)
5841 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
5851 * -----------------------------------------------------------------------
5852 * -----------------------------------------------------------------------
5853 * -----------------------------------------------------------------------
5857 * Ends the decompression procedures and possibiliy add data to be read after the
5860 opj_bool j2k_end_decompress(
5861 opj_j2k_v2_t *p_j2k,
5862 opj_stream_private_t *p_stream,
5863 opj_event_mgr_t * p_manager)
5869 * Reads a jpeg2000 codestream header structure.
5872 * @param p_stream the stream to read data from.
5873 * @param p_j2k the jpeg2000 codec.
5874 * @param p_manager the user event manager.
5876 * @return true if the box is valid.
5878 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
5879 opj_j2k_v2_t* p_j2k,
5880 opj_image_t** p_image,
5881 struct opj_event_mgr* p_manager )
5884 assert(p_j2k != 00);
5885 assert(p_stream != 00);
5886 assert(p_manager != 00);
5888 /* create an empty image header */
5889 p_j2k->m_private_image = opj_image_create0();
5890 if (! p_j2k->m_private_image) {
5894 /* customization of the validation */
5895 j2k_setup_decoding_validation(p_j2k);
5897 /* validation of the parameters codec */
5898 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
5899 opj_image_destroy(p_j2k->m_private_image);
5900 p_j2k->m_private_image = NULL;
5904 /* customization of the encoding */
5905 j2k_setup_header_reading(p_j2k);
5908 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
5909 opj_image_destroy(p_j2k->m_private_image);
5910 p_j2k->m_private_image = NULL;
5914 *p_image = opj_image_create0();
5919 /* Copy codestream image information to the output image */
5920 opj_copy_image_header(p_j2k->m_private_image, *p_image);
5927 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
5929 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
5932 assert(p_j2k != 00);
5934 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_read_header_procedure);
5936 /* DEVELOPER CORNER, add your custom procedures */
5937 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_copy_default_tcp_and_create_tcd);
5942 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
5943 * are valid. Developpers wanting to extend the library can add their own validation procedures.
5945 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
5948 assert(p_j2k != 00);
5950 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_decoder);
5951 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_decoding_validation);
5952 /* DEVELOPER CORNER, add your custom validation procedure */
5957 * Builds the cp decoder parameters to use to decode tile.
5959 opj_bool j2k_build_decoder (
5960 opj_j2k_v2_t * p_j2k,
5961 opj_stream_private_t *p_stream,
5962 opj_event_mgr_t * p_manager
5965 // add here initialization of cp
5966 // copy paste of setup_decoder
5971 * The default decoding validation procedure without any extension.
5973 * @param p_j2k the jpeg2000 codec to validate.
5974 * @param p_stream the input stream to validate.
5975 * @param p_manager the user event manager.
5977 * @return true if the parameters are correct.
5979 opj_bool j2k_decoding_validation (
5980 opj_j2k_v2_t *p_j2k,
5981 opj_stream_private_t *p_stream,
5982 opj_event_mgr_t * p_manager
5985 opj_bool l_is_valid = OPJ_TRUE;
5988 assert(p_j2k != 00);
5989 assert(p_stream != 00);
5990 assert(p_manager != 00);
5993 /* STATE checking */
5994 /* make sure the state is at 0 */
5996 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
5998 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
6000 /* POINTER validation */
6001 /* make sure a p_j2k codec is present */
6002 /* make sure a procedure list is present */
6003 l_is_valid &= (p_j2k->m_procedure_list != 00);
6004 /* make sure a validation list is present */
6005 l_is_valid &= (p_j2k->m_validation_list != 00);
6007 /* PARAMETER VALIDATION */
6011 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
6012 struct opj_stream_private *p_stream,
6013 struct opj_event_mgr * p_manager)
6015 OPJ_UINT32 l_current_marker;
6016 OPJ_UINT32 l_marker_size;
6017 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6020 assert(p_stream != 00);
6021 assert(p_j2k != 00);
6022 assert(p_manager != 00);
6024 /* We enter in the main header */
6025 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
6027 /* Try to read the SOC marker, the codestream must begin with SOC marker */
6028 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
6029 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
6033 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6034 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6035 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6039 /* Read 2 bytes as the new marker ID */
6040 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6042 /* Try to read until the SOT is detected */
6043 while (l_current_marker != J2K_MS_SOT) {
6045 /* Check if the current marker ID is valid */
6046 if (l_current_marker < 0xff00) {
6047 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
6051 /* Get the marker handler from the marker ID */
6052 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6054 /* Manage case where marker is unknown */
6055 if (l_marker_handler->id == J2K_MS_UNK) {
6056 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
6057 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
6061 if (l_current_marker == J2K_MS_SOT)
6062 break; /* SOT marker is detected main header is completely read */
6063 else /* Get the marker handler from the marker ID */
6064 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6067 /* Check if the marker is known and if it is the right place to find it */
6068 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6069 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6073 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6074 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6075 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6079 /* read 2 bytes as the marker size */
6080 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6081 l_marker_size -= 2; // Subtract the size of the marker ID already read
6083 /* Check if the marker size is compatible with the header data size */
6084 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6085 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
6086 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
6087 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
6090 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6093 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6094 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) {
6095 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6099 /* Read the marker segment with the correct marker handler */
6100 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6101 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
6105 /* Add the marker to the codestream index*/
6106 j2k_add_mhmarker_v2(p_j2k->cstr_index,
6107 l_marker_handler->id,
6108 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6109 l_marker_size + 4 );
6111 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6112 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6113 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6117 /* read 2 bytes as the new marker ID */
6118 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6121 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decode.\n");
6123 /* Position of the last element if the main header */
6124 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
6126 /* Next step: read a tile-part header */
6127 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6133 * Excutes the given procedures on the given codec.
6135 * @param p_procedure_list the list of procedures to execute
6136 * @param p_j2k the jpeg2000 codec to execute the procedures on.
6137 * @param p_stream the stream to execute the procedures on.
6138 * @param p_manager the user manager.
6140 * @return true if all the procedures were successfully executed.
6142 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
6143 opj_procedure_list_t * p_procedure_list,
6144 opj_stream_private_t *p_stream,
6145 opj_event_mgr_t * p_manager )
6147 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
6148 opj_bool l_result = OPJ_TRUE;
6149 OPJ_UINT32 l_nb_proc, i;
6152 assert(p_procedure_list != 00);
6153 assert(p_j2k != 00);
6154 assert(p_stream != 00);
6155 assert(p_manager != 00);
6158 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
6159 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);
6161 for (i=0;i<l_nb_proc;++i) {
6162 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
6166 // and clear the procedure list at the end.
6167 opj_procedure_list_clear(p_procedure_list);
6172 opj_bool j2k_copy_default_tcp_and_create_tcd
6174 opj_j2k_v2_t * p_j2k,
6175 opj_stream_private_t *p_stream,
6176 opj_event_mgr_t * p_manager
6179 opj_tcp_v2_t * l_tcp = 00;
6180 opj_tcp_v2_t * l_default_tcp = 00;
6181 OPJ_UINT32 l_nb_tiles;
6183 opj_tccp_t *l_current_tccp = 00;
6184 OPJ_UINT32 l_tccp_size;
6185 OPJ_UINT32 l_mct_size;
6186 opj_image_t * l_image;
6187 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
6188 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
6189 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
6190 OPJ_UINT32 l_offset;
6193 assert(p_j2k != 00);
6194 assert(p_stream != 00);
6195 assert(p_manager != 00);
6197 l_image = p_j2k->m_private_image;
6198 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6199 l_tcp = p_j2k->m_cp.tcps;
6200 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
6201 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
6202 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
6205 for (i=0; i<l_nb_tiles; ++i) {
6206 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
6207 l_current_tccp = l_tcp->tccps;
6208 /*Copy default coding parameters into the current tile coding parameters*/
6209 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
6210 /* Initialize some values of the current tile coding parameters*/
6212 l_tcp->ppt_data = 00;
6213 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
6214 l_tcp->tccps = l_current_tccp;
6216 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
6217 if (l_default_tcp->m_mct_decoding_matrix) {
6218 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6219 if (! l_tcp->m_mct_decoding_matrix ) {
6222 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
6225 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
6226 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
6227 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
6228 if (! l_tcp->m_mct_records) {
6231 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
6233 /* Copy the mct record data from dflt_tile_cp to the current tile*/
6234 l_src_mct_rec = l_default_tcp->m_mct_records;
6235 l_dest_mct_rec = l_tcp->m_mct_records;
6237 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
6239 if (l_src_mct_rec->m_data) {
6241 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
6242 if(! l_dest_mct_rec->m_data) {
6245 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
6252 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
6253 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
6254 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
6255 if (! l_tcp->m_mcc_records) {
6258 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
6260 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
6261 l_src_mcc_rec = l_default_tcp->m_mcc_records;
6262 l_dest_mcc_rec = l_tcp->m_mcc_records;
6264 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
6266 if (l_src_mcc_rec->m_decorrelation_array) {
6267 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
6268 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
6271 if (l_src_mcc_rec->m_offset_array) {
6272 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
6273 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
6280 /* Copy all the dflt_tile_compo_cp to the current tile cp */
6281 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
6283 /* Move to next tile cp*/
6287 /* Create the current tile decoder*/
6288 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); // FIXME why a cast ?
6289 if (! p_j2k->m_tcd ) {
6293 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
6294 tcd_destroy_v2(p_j2k->m_tcd);
6296 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6304 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
6305 * with the marker value.
6306 * @param p_id Marker value to look up
6308 * @return the handler associated with the id.
6310 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (const OPJ_UINT32 p_id)
6312 const opj_dec_memory_marker_handler_t *e;
6313 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
6314 if (e->id == p_id) {
6315 break; // we find a handler corresponding to the marker ID
6323 * Destroys a jpeg2000 codec.
6325 * @param p_j2k the jpeg20000 structure to destroy.
6327 void j2k_destroy (opj_j2k_v2_t *p_j2k)
6333 if (p_j2k->m_is_decoder) {
6335 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
6336 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
6337 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
6338 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
6341 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
6342 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
6343 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
6344 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
6349 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
6350 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
6351 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
6354 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6355 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
6356 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
6357 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
6360 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6361 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
6362 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
6363 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
6367 tcd_destroy_v2(p_j2k->m_tcd);
6369 j2k_cp_destroy(&(p_j2k->m_cp));
6370 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
6372 opj_procedure_list_destroy(p_j2k->m_procedure_list);
6373 p_j2k->m_procedure_list = 00;
6375 opj_procedure_list_destroy(p_j2k->m_validation_list);
6376 p_j2k->m_procedure_list = 00;
6378 j2k_destroy_cstr_index(p_j2k->cstr_index);
6379 p_j2k->cstr_index = NULL;
6381 opj_image_destroy(p_j2k->m_private_image);
6382 p_j2k->m_private_image = NULL;
6384 opj_image_destroy(p_j2k->m_output_image);
6385 p_j2k->m_output_image = NULL;
6390 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
6394 if (p_cstr_ind->marker) {
6395 opj_free(p_cstr_ind->marker);
6396 p_cstr_ind->marker = NULL;
6399 if (p_cstr_ind->tile_index) {
6400 OPJ_UINT32 it_tile = 0;
6402 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
6404 if(p_cstr_ind->tile_index[it_tile].packet_index) {
6405 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
6406 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
6409 if(p_cstr_ind->tile_index[it_tile].tp_index){
6410 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
6411 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
6414 if(p_cstr_ind->tile_index[it_tile].marker){
6415 opj_free(p_cstr_ind->tile_index[it_tile].marker);
6416 p_cstr_ind->tile_index[it_tile].marker = NULL;
6421 opj_free( p_cstr_ind->tile_index);
6422 p_cstr_ind->tile_index = NULL;
6425 opj_free(p_cstr_ind);
6432 * Destroys a tile coding parameter structure.
6434 * @param p_tcp the tile coding parameter to destroy.
6436 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
6442 if (p_tcp->ppt_buffer != 00) {
6443 opj_free(p_tcp->ppt_buffer);
6444 p_tcp->ppt_buffer = 00;
6447 if (p_tcp->tccps != 00) {
6448 opj_free(p_tcp->tccps);
6452 if (p_tcp->m_mct_coding_matrix != 00) {
6453 opj_free(p_tcp->m_mct_coding_matrix);
6454 p_tcp->m_mct_coding_matrix = 00;
6457 if (p_tcp->m_mct_decoding_matrix != 00) {
6458 opj_free(p_tcp->m_mct_decoding_matrix);
6459 p_tcp->m_mct_decoding_matrix = 00;
6462 if (p_tcp->m_mcc_records) {
6463 opj_free(p_tcp->m_mcc_records);
6464 p_tcp->m_mcc_records = 00;
6465 p_tcp->m_nb_max_mcc_records = 0;
6466 p_tcp->m_nb_mcc_records = 0;
6469 if (p_tcp->m_mct_records) {
6470 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
6473 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
6474 if (l_mct_data->m_data) {
6475 opj_free(l_mct_data->m_data);
6476 l_mct_data->m_data = 00;
6482 opj_free(p_tcp->m_mct_records);
6483 p_tcp->m_mct_records = 00;
6486 if (p_tcp->mct_norms != 00) {
6487 opj_free(p_tcp->mct_norms);
6488 p_tcp->mct_norms = 00;
6491 if (p_tcp->m_data) {
6492 opj_free(p_tcp->m_data);
6499 * Destroys a coding parameter structure.
6501 * @param p_cp the coding parameter to destroy.
6503 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
6505 OPJ_UINT32 l_nb_tiles;
6506 opj_tcp_v2_t * l_current_tile = 00;
6517 l_current_tile = p_cp->tcps;
6518 l_nb_tiles = p_cp->th * p_cp->tw;
6521 (i = 0; i < l_nb_tiles; ++i)
6523 j2k_tcp_destroy(l_current_tile);
6526 opj_free(p_cp->tcps);
6530 (p_cp->ppm_buffer != 00)
6532 opj_free(p_cp->ppm_buffer);
6533 p_cp->ppm_buffer = 00;
6536 (p_cp->comment != 00)
6538 opj_free(p_cp->comment);
6542 (! p_cp->m_is_decoder)
6545 (p_cp->m_specific_param.m_enc.m_matrice)
6547 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
6548 p_cp->m_specific_param.m_enc.m_matrice = 00;
6556 * Reads a tile header.
6557 * @param p_j2k the jpeg2000 codec.
6558 * @param p_stream the stream to write data to.
6559 * @param p_manager the user event manager.
6561 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
6562 OPJ_UINT32 * p_tile_index,
6563 OPJ_UINT32 * p_data_size,
6564 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
6565 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
6566 OPJ_UINT32 * p_nb_comps,
6568 opj_stream_private_t *p_stream,
6569 opj_event_mgr_t * p_manager )
6571 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
6572 OPJ_UINT32 l_marker_size;
6573 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
6574 opj_tcp_v2_t * l_tcp = NULL;
6575 OPJ_UINT32 l_nb_tiles;
6578 assert(p_stream != 00);
6579 assert(p_j2k != 00);
6580 assert(p_manager != 00);
6582 /* Reach the End Of Codestream ?*/
6583 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
6584 l_current_marker = J2K_MS_EOC;
6586 /* We need to encounter a SOT marker (a new tile-part header) */
6587 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
6591 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
6592 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
6594 /* Try to read until the Start Of Data is detected */
6595 while (l_current_marker != J2K_MS_SOD) {
6597 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
6598 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6599 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6603 /* Read 2 bytes from the buffer as the marker size */
6604 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
6606 /* Why this condition? FIXME */
6607 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
6608 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
6610 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
6612 /* Get the marker handler from the marker ID */
6613 l_marker_handler = j2k_get_marker_handler(l_current_marker);
6615 /* Check if the marker is known and if it is the right place to find it */
6616 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
6617 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6620 /* FIXME manage case of unknown marker as in the main header ? */
6622 /* Check if the marker size is compatible with the header data size */
6623 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
6624 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
6625 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
6626 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
6629 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
6632 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
6633 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) {
6634 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6638 /* Read the marker segment with the correct marker handler */
6639 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
6640 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
6644 /* Add the marker to the codestream index*/
6645 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
6647 l_marker_handler->id,
6648 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
6649 l_marker_size + 4 );
6651 /* Keep the position of the last SOT marker read */
6652 if ( l_marker_handler->id == J2K_MS_SOT ) {
6653 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
6654 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
6656 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
6661 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
6662 /* Skip the rest of the tile part header*/
6663 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) {
6664 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6667 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
6670 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6671 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6672 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6675 /* Read 2 bytes from the buffer as the new marker ID */
6676 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6680 /* If we didn't skip data before, we need to read the SOD marker*/
6681 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
6682 /* Try to read the SOD marker and skip data ? FIXME */
6683 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
6689 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
6690 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6691 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6692 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6696 /* Read 2 bytes from buffer as the new marker ID */
6697 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6701 /* Indicate we will try to read a new tile-part header*/
6702 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
6703 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
6704 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
6706 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
6707 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6708 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6712 /* Read 2 bytes from buffer as the new marker ID */
6713 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
6717 /* Current marker is the EOC marker ?*/
6718 if (l_current_marker == J2K_MS_EOC) {
6719 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
6720 p_j2k->m_current_tile_number = 0;
6721 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
6726 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
6727 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
6728 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6730 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
6731 ++p_j2k->m_current_tile_number;
6735 if (p_j2k->m_current_tile_number == l_nb_tiles) {
6736 *p_go_on = OPJ_FALSE;
6742 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
6743 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6747 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
6748 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
6750 *p_tile_index = p_j2k->m_current_tile_number;
6751 *p_go_on = OPJ_TRUE;
6752 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
6753 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
6754 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
6755 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
6756 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
6757 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
6759 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;// FIXME J2K_DEC_STATE_DATA;
6765 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
6766 OPJ_UINT32 p_tile_index,
6768 OPJ_UINT32 p_data_size,
6769 opj_stream_private_t *p_stream,
6770 opj_event_mgr_t * p_manager )
6772 OPJ_UINT32 l_current_marker;
6773 OPJ_BYTE l_data [2];
6774 opj_tcp_v2_t * l_tcp;
6777 assert(p_stream != 00);
6778 assert(p_j2k != 00);
6779 assert(p_manager != 00);
6781 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
6782 || (p_tile_index != p_j2k->m_current_tile_number) ) {
6786 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
6787 if (! l_tcp->m_data) {
6788 j2k_tcp_destroy(&(p_j2k->m_cp.tcps[p_tile_index]));
6792 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
6796 p_j2k->cstr_index) ) {
6797 j2k_tcp_destroy(l_tcp);
6798 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;//FIXME J2K_DEC_STATE_ERR;
6802 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
6806 j2k_tcp_destroy(l_tcp);
6807 p_j2k->m_tcd->tcp = 0;
6809 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
6810 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));// FIXME J2K_DEC_STATE_DATA);
6812 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ //FIXME J2K_DEC_STATE_EOC)
6813 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
6814 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6818 opj_read_bytes(l_data,&l_current_marker,2);
6820 if (l_current_marker == J2K_MS_EOC) {
6821 p_j2k->m_current_tile_number = 0;
6822 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;//FIXME J2K_DEC_STATE_EOC;
6824 else if (l_current_marker != J2K_MS_SOT)
6826 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
6835 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
6837 OPJ_UINT32 i,j,k = 0;
6838 OPJ_UINT32 l_width_src,l_height_src;
6839 OPJ_UINT32 l_width_dest,l_height_dest;
6840 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
6841 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
6842 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
6843 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
6844 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
6846 opj_image_comp_t * l_img_comp_src = 00;
6847 opj_image_comp_t * l_img_comp_dest = 00;
6849 opj_tcd_tilecomp_v2_t * l_tilec = 00;
6850 opj_image_t * l_image_src = 00;
6851 OPJ_UINT32 l_size_comp, l_remaining;
6852 OPJ_INT32 * l_dest_ptr;
6853 opj_tcd_resolution_v2_t* l_res= 00;
6855 l_tilec = p_tcd->tcd_image->tiles->comps;
6856 l_image_src = p_tcd->image;
6857 l_img_comp_src = l_image_src->comps;
6859 l_img_comp_dest = p_output_image->comps;
6861 for (i=0; i<l_image_src->numcomps; i++) {
6863 /* Allocate output component buffer if necessary */
6864 if (!l_img_comp_dest->data) {
6866 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
6867 if (! l_img_comp_dest->data) {
6872 /* Copy info from decoded comp image to output image */
6873 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
6876 /* Compute the precision of the output buffer */
6877 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
6878 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
6879 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
6885 if (l_size_comp == 3) {
6890 /* Current tile component size*/
6891 l_width_src = (l_res->x1 - l_res->x0);
6892 l_height_src = (l_res->y1 - l_res->y0);
6894 /* Border of the current output component*/
6895 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
6896 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
6897 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
6898 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
6901 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
6902 * of the input buffer (decoded tile component) which will be move
6903 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
6904 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
6905 * by this input area.
6907 if ( l_x0_dest < l_res->x0 ) {
6908 l_start_x_dest = l_res->x0 - l_x0_dest;
6909 l_offset_x0_src = 0;
6911 if ( l_x1_dest >= l_res->x1 ) {
6912 l_width_dest = l_width_src;
6913 l_offset_x1_src = 0;
6916 l_width_dest = l_x1_dest - l_res->x0 ;
6917 l_offset_x1_src = l_width_src - l_width_dest;
6921 l_start_x_dest = 0 ;
6922 l_offset_x0_src = l_x0_dest - l_res->x0;
6924 if ( l_x1_dest >= l_res->x1 ) {
6925 l_width_dest = l_width_src - l_offset_x0_src;
6926 l_offset_x1_src = 0;
6929 l_width_dest = l_img_comp_dest->w ;
6930 l_offset_x1_src = l_res->x1 - l_x1_dest;
6934 if ( l_y0_dest < l_res->y0 ) {
6935 l_start_y_dest = l_res->y0 - l_y0_dest;
6936 l_offset_y0_src = 0;
6938 if ( l_y1_dest >= l_res->y1 ) {
6939 l_height_dest = l_height_src;
6940 l_offset_y1_src = 0;
6943 l_height_dest = l_y1_dest - l_res->y0 ;
6944 l_offset_y1_src = l_height_src - l_height_dest;
6948 l_start_y_dest = 0 ;
6949 l_offset_y0_src = l_y0_dest - l_res->y0;
6951 if ( l_y1_dest >= l_res->y1 ) {
6952 l_height_dest = l_height_src - l_offset_y0_src;
6953 l_offset_y1_src = 0;
6956 l_height_dest = l_img_comp_dest->h ;
6957 l_offset_y1_src = l_res->y1 - l_y1_dest;
6961 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
6966 /* Compute the input buffer offset */
6967 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
6968 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
6969 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
6971 /* Compute the output buffer offset */
6972 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
6973 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
6975 /* Move the output buffer to the first place where we will write*/
6976 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
6979 fprintf(stdout, "COMPO[%d]:\n",i);
6980 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
6981 "\t tile offset:%d, %d, %d, %d\n"
6982 "\t buffer offset: %d; %d, %d\n",
6983 l_res->x0, l_res->y0, l_width_src, l_height_src,
6984 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
6985 l_start_offset_src, l_line_offset_src, l_end_offset_src);
6987 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
6988 "\t start offset: %d, line offset= %d\n",
6989 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
6993 switch (l_size_comp) {
6996 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
6997 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
6999 if (l_img_comp_src->sgnd) {
7000 for (j = 0 ; j < l_height_dest ; ++j) {
7001 for ( k = 0 ; k < l_width_dest ; ++k) {
7002 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
7005 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
7006 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
7010 for ( j = 0 ; j < l_height_dest ; ++j ) {
7011 for ( k = 0 ; k < l_width_dest ; ++k) {
7012 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
7015 l_dest_ptr+= l_line_offset_dest;
7016 l_src_ptr += l_line_offset_src;
7020 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
7021 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
7026 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
7027 l_src_ptr += l_start_offset_src;
7029 if (l_img_comp_src->sgnd) {
7030 for (j=0;j<l_height_dest;++j) {
7031 for (k=0;k<l_width_dest;++k) {
7032 *(l_dest_ptr++) = *(l_src_ptr++);
7035 l_dest_ptr+= l_line_offset_dest;
7036 l_src_ptr += l_line_offset_src ;
7040 for (j=0;j<l_height_dest;++j) {
7041 for (k=0;k<l_width_dest;++k) {
7042 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
7045 l_dest_ptr+= l_line_offset_dest;
7046 l_src_ptr += l_line_offset_src ;
7050 l_src_ptr += l_end_offset_src;
7051 p_data = (OPJ_BYTE*) l_src_ptr;
7056 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
7057 l_src_ptr += l_start_offset_src;
7059 for (j=0;j<l_height_dest;++j) {
7060 for (k=0;k<l_width_dest;++k) {
7061 *(l_dest_ptr++) = (*(l_src_ptr++));
7064 l_dest_ptr+= l_line_offset_dest;
7065 l_src_ptr += l_line_offset_src ;
7068 l_src_ptr += l_end_offset_src;
7069 p_data = (OPJ_BYTE*) l_src_ptr;
7083 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
7085 * @param p_j2k the jpeg2000 codec.
7086 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
7087 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
7088 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
7089 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
7090 * @param p_manager the user event manager
7092 * @return true if the area could be set.
7094 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
7095 opj_image_t* p_image,
7096 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
7097 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
7098 struct opj_event_mgr * p_manager )
7100 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
7101 opj_image_t * l_image = p_j2k->m_private_image;
7104 OPJ_INT32 l_comp_x1, l_comp_y1;
7105 opj_image_comp_t* l_img_comp = NULL;
7107 /* Check if we are read the main header */
7108 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { // FIXME J2K_DEC_STATE_TPHSOT)
7109 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
7113 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
7114 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the all image\n");
7116 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7117 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7118 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7119 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
7125 /* Check if the positions provided by the user are correct */
7128 if (p_start_x > l_image->x1 ) {
7129 opj_event_msg_v2(p_manager, EVT_ERROR,
7130 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
7131 p_start_x, l_image->x1);
7134 else if (p_start_x < l_image->x0){
7135 opj_event_msg_v2(p_manager, EVT_WARNING,
7136 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
7137 p_start_x, l_image->x0);
7138 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
7139 p_image->x0 = l_image->x0;
7142 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
7143 p_image->x0 = p_start_x;
7147 if (p_start_y > l_image->y1){
7148 opj_event_msg_v2(p_manager, EVT_ERROR,
7149 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
7150 p_start_y, l_image->y1);
7153 else if (p_start_y < l_image->y0){
7154 opj_event_msg_v2(p_manager, EVT_WARNING,
7155 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
7156 p_start_y, l_image->y0);
7157 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
7158 p_image->y0 = l_image->y0;
7161 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
7162 p_image->y0 = p_start_y;
7166 if (p_end_x < l_image->x0) {
7167 opj_event_msg_v2(p_manager, EVT_ERROR,
7168 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
7169 p_end_x, l_image->x0);
7172 else if (p_end_x > l_image->x1) {
7173 opj_event_msg_v2(p_manager, EVT_WARNING,
7174 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
7175 p_end_x, l_image->x1);
7176 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
7177 p_image->x1 = l_image->x1;
7180 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
7181 p_image->x1 = p_end_x;
7185 if (p_end_y < l_image->y0) {
7186 opj_event_msg_v2(p_manager, EVT_ERROR,
7187 "Right position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
7188 p_end_y, l_image->y0);
7191 if (p_end_y > l_image->y1){
7192 opj_event_msg_v2(p_manager, EVT_WARNING,
7193 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
7194 p_end_y, l_image->y1);
7195 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
7196 p_image->y1 = l_image->y1;
7199 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
7200 p_image->y1 = p_end_y;
7204 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
7206 l_img_comp = p_image->comps;
7207 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
7209 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
7210 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
7211 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
7212 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
7214 l_img_comp->w = int_ceildivpow2(l_comp_x1 - l_img_comp->x0, l_img_comp->factor);
7215 if (l_img_comp->w <= 0){
7216 opj_event_msg_v2(p_manager, EVT_ERROR,
7217 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
7218 it_comp, l_img_comp->w);
7222 l_img_comp->h = int_ceildivpow2(l_comp_y1 - l_img_comp->y0, l_img_comp->factor);
7223 if (l_img_comp->h <= 0){
7224 opj_event_msg_v2(p_manager, EVT_ERROR,
7225 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
7226 it_comp, l_img_comp->h);
7233 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
7234 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
7241 /* ----------------------------------------------------------------------- */
7242 /* J2K / JPT decoder interface */
7243 /* ----------------------------------------------------------------------- */
7245 * Creates a J2K decompression structure.
7247 * @return a handle to a J2K decompressor if successful, NULL otherwise.
7249 opj_j2k_v2_t* j2k_create_decompress_v2()
7251 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7255 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7257 l_j2k->m_is_decoder = 1;
7258 l_j2k->m_cp.m_is_decoder = 1;
7260 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
7261 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
7265 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
7267 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7268 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
7273 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
7275 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
7277 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
7279 /* codestream index creation */
7280 l_j2k->cstr_index = j2k_create_cstr_index();
7282 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
7283 if (!l_j2k->cstr_index){
7288 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
7291 /* validation list creation */
7292 l_j2k->m_validation_list = opj_procedure_list_create();
7293 if (! l_j2k->m_validation_list) {
7298 /* execution list creation */
7299 l_j2k->m_procedure_list = opj_procedure_list_create();
7300 if (! l_j2k->m_procedure_list) {
7309 opj_codestream_index_t* j2k_create_cstr_index(void)
7311 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
7312 opj_calloc(1,sizeof(opj_codestream_index_t));
7316 cstr_index->maxmarknum = 100;
7317 cstr_index->marknum = 0;
7318 cstr_index->marker = (opj_marker_info_t*)
7319 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
7320 if (!cstr_index-> marker)
7323 cstr_index->tile_index = NULL;
7329 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
7330 * @param p_header_data the data contained in the COM box.
7331 * @param p_j2k the jpeg2000 codec.
7332 * @param p_header_size the size of the data contained in the COM marker.
7333 * @param p_manager the user event manager.
7335 opj_bool j2k_read_SPCod_SPCoc(
7336 opj_j2k_v2_t *p_j2k,
7338 OPJ_BYTE * p_header_data,
7339 OPJ_UINT32 * p_header_size,
7340 struct opj_event_mgr * p_manager
7343 OPJ_UINT32 i, l_tmp;
7344 opj_cp_v2_t *l_cp = NULL;
7345 opj_tcp_v2_t *l_tcp = NULL;
7346 opj_tccp_t *l_tccp = NULL;
7347 OPJ_BYTE * l_current_ptr = NULL;
7350 assert(p_j2k != 00);
7351 assert(p_manager != 00);
7352 assert(p_header_data != 00);
7354 l_cp = &(p_j2k->m_cp);
7355 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
7356 &l_cp->tcps[p_j2k->m_current_tile_number] :
7357 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7359 /* precondition again */
7360 assert(compno < p_j2k->m_private_image->numcomps);
7362 l_tccp = &l_tcp->tccps[compno];
7363 l_current_ptr = p_header_data;
7365 /* make sure room is sufficient */
7366 if (*p_header_size < 5) {
7367 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
7371 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
7372 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
7375 /* If user wants to remove more resolutions than the codestream contains, return error */
7376 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
7377 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
7378 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
7379 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;// FIXME J2K_DEC_STATE_ERR;
7383 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
7387 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
7391 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
7394 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
7397 *p_header_size = *p_header_size - 5;
7399 /* use custom precinct size ? */
7400 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
7401 if (*p_header_size < l_tccp->numresolutions) {
7402 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
7406 for (i = 0; i < l_tccp->numresolutions; ++i) {
7407 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
7409 l_tccp->prcw[i] = l_tmp & 0xf;
7410 l_tccp->prch[i] = l_tmp >> 4;
7413 *p_header_size = *p_header_size - l_tccp->numresolutions;
7416 /* set default size for the precinct width and height */
7417 for (i = 0; i < l_tccp->numresolutions; ++i) {
7418 l_tccp->prcw[i] = 15;
7419 l_tccp->prch[i] = 15;
7423 #ifdef WIP_REMOVE_MSD
7425 if (p_j2k->cstr_info && compno == 0) {
7426 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
7428 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
7429 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
7430 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
7431 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
7432 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
7435 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
7436 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
7445 * Copies the tile component parameters of all the component from the first tile component.
7447 * @param p_j2k the J2k codec.
7449 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
7453 opj_cp_v2_t *l_cp = NULL;
7454 opj_tcp_v2_t *l_tcp = NULL;
7455 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
7456 OPJ_UINT32 l_prc_size;
7459 assert(p_j2k != 00);
7461 l_cp = &(p_j2k->m_cp);
7462 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
7463 &l_cp->tcps[p_j2k->m_current_tile_number] :
7464 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7466 l_ref_tccp = &l_tcp->tccps[0];
7467 l_copied_tccp = l_ref_tccp + 1;
7468 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
7470 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
7471 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
7472 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
7473 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
7474 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
7475 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
7476 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
7477 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
7483 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
7485 * @param p_comp_no the component being targeted.
7486 * @param p_header_data the data contained in the COM box.
7487 * @param p_j2k the jpeg2000 codec.
7488 * @param p_header_size the size of the data contained in the COM marker.
7489 * @param p_manager the user event manager.
7491 opj_bool j2k_read_SQcd_SQcc(
7492 opj_j2k_v2_t *p_j2k,
7493 OPJ_UINT32 p_comp_no,
7494 OPJ_BYTE* p_header_data,
7495 OPJ_UINT32 * p_header_size,
7496 struct opj_event_mgr * p_manager
7500 OPJ_UINT32 l_band_no;
7501 opj_cp_v2_t *l_cp = 00;
7502 opj_tcp_v2_t *l_tcp = 00;
7503 opj_tccp_t *l_tccp = 00;
7504 OPJ_BYTE * l_current_ptr = 00;
7505 OPJ_UINT32 l_tmp, l_num_band;
7508 assert(p_j2k != 00);
7509 assert(p_manager != 00);
7510 assert(p_header_data != 00);
7512 l_cp = &(p_j2k->m_cp);
7513 // come from tile part header or main header ?
7514 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
7515 &l_cp->tcps[p_j2k->m_current_tile_number] :
7516 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7518 // precondition again
7519 assert(p_comp_no < p_j2k->m_private_image->numcomps);
7521 l_tccp = &l_tcp->tccps[p_comp_no];
7522 l_current_ptr = p_header_data;
7524 if (*p_header_size < 1) {
7525 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
7528 *p_header_size -= 1;
7530 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
7533 l_tccp->qntsty = l_tmp & 0x1f;
7534 l_tccp->numgbits = l_tmp >> 5;
7535 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
7539 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
7541 (*p_header_size) / 2;
7543 if( l_num_band > J2K_MAXBANDS ) {
7544 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
7545 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limiting the number of elements stored to "
7546 "J2K_MAXBANDS (%d) and skip the other. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
7552 if (l_cp->correct) {
7554 /* if JPWL is on, we check whether there are too many subbands */
7555 if ((l_num_band < 0) || (l_num_band >= J2K_MAXBANDS)) {
7556 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
7557 "JPWL: bad number of subbands in Sqcx (%d)\n",
7560 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
7563 /* we try to correct */
7565 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
7566 "- setting number of bands to %d => HYPOTHESIS!!!\n",
7571 #endif /* USE_JPWL */
7573 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
7574 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
7575 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
7577 if (l_band_no < J2K_MAXBANDS){
7578 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
7579 l_tccp->stepsizes[l_band_no].mant = 0;
7582 *p_header_size = *p_header_size - l_num_band;
7585 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
7586 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
7588 if (l_band_no < J2K_MAXBANDS){
7589 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
7590 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
7593 *p_header_size = *p_header_size - 2*l_num_band;
7596 /* Add Antonin : if scalar_derived -> compute other stepsizes */
7597 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
7598 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
7599 l_tccp->stepsizes[l_band_no].expn =
7600 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
7601 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
7602 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
7610 * Copies the tile component parameters of all the component from the first tile component.
7612 * @param p_j2k the J2k codec.
7614 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
7617 opj_cp_v2_t *l_cp = NULL;
7618 opj_tcp_v2_t *l_tcp = NULL;
7619 opj_tccp_t *l_ref_tccp = NULL;
7620 opj_tccp_t *l_copied_tccp = NULL;
7624 assert(p_j2k != 00);
7626 l_cp = &(p_j2k->m_cp);
7627 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7628 &l_cp->tcps[p_j2k->m_current_tile_number] :
7629 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7631 l_ref_tccp = &l_tcp->tccps[0];
7632 l_copied_tccp = l_ref_tccp + 1;
7633 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
7635 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
7636 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
7637 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
7638 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
7644 * Dump some elements from the J2K decompression structure .
7646 *@param p_j2k the jpeg2000 codec.
7647 *@param flag flag to describe what elments are dump.
7648 *@param out_stream output stream where dump the elements.
7651 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
7653 /* Check if the flag is compatible with j2k file*/
7654 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
7655 fprintf(out_stream, "Wrong flag\n");
7659 /* Dump the image_header */
7660 if (flag & OPJ_IMG_INFO){
7661 if (p_j2k->m_private_image)
7662 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
7665 /* Dump the codestream info from main header */
7666 if (flag & OPJ_J2K_MH_INFO){
7667 j2k_dump_MH_info(p_j2k, out_stream);
7671 /* Dump the codestream info of the current tile */
7672 if (flag & OPJ_J2K_TH_INFO){
7676 /* Dump the codestream index from main header */
7677 if (flag & OPJ_J2K_MH_IND){
7678 j2k_dump_MH_index(p_j2k, out_stream);
7681 /* Dump the codestream index of the current tile */
7682 if (flag & OPJ_J2K_TH_IND){
7689 * Dump index elements of the codestream extract from the main header.
7691 *@param p_j2k the jpeg2000 codec.
7692 *@param out_stream output stream where dump the elements.
7695 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
7697 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
7698 OPJ_UINT32 it_marker, it_tile, it_tile_part;
7700 fprintf(out_stream, "Codestream index from main header: {\n");
7702 fprintf(out_stream, "\t Main header start position=%d\n\t Main header end position=%d\n",
7703 cstr_index->main_head_start, cstr_index->main_head_end);
7705 fprintf(out_stream, "\t Marker list: {\n");
7707 if (cstr_index->marker){
7708 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
7709 fprintf(out_stream, "\t\t type=%#x, pos=%d, len=%d\n",
7710 cstr_index->marker[it_marker].type,
7711 cstr_index->marker[it_marker].pos,
7712 cstr_index->marker[it_marker].len );
7716 fprintf(out_stream, "\t }\n");
7719 if (cstr_index->tile_index){
7720 fprintf(out_stream, "\t Tile index: {\n");
7722 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
7723 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
7725 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
7727 if (cstr_index->tile_index[it_tile].tp_index){
7728 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
7729 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%d, end_header=%d, end_pos=%d.\n",
7731 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
7732 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
7733 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
7737 if (cstr_index->tile_index[it_tile].marker){
7738 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
7739 fprintf(out_stream, "\t\t type=%#x, pos=%d, len=%d\n",
7740 cstr_index->tile_index[it_tile].marker[it_marker].type,
7741 cstr_index->tile_index[it_tile].marker[it_marker].pos,
7742 cstr_index->tile_index[it_tile].marker[it_marker].len );
7746 fprintf(out_stream,"\t }\n");
7749 fprintf(out_stream,"}\n");
7754 * Dump info elements of the codestream extract from the main header.
7756 *@param p_j2k the jpeg2000 codec.
7757 *@param out_stream output stream where dump the elements.
7760 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
7762 opj_tcp_v2_t * l_default_tile=NULL;
7764 fprintf(out_stream, "Codestream info from main header: {\n");
7766 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
7767 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
7768 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
7770 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7774 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
7776 fprintf(out_stream, "\t default tile {\n");
7777 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
7778 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
7779 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
7780 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
7782 for (compno = 0; compno < numcomps; compno++) {
7783 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
7784 OPJ_INT32 resno, bandno, numbands;
7787 fprintf(out_stream, "\t\t comp %d {\n", compno);
7788 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
7789 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
7790 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
7791 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
7792 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
7793 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
7795 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
7796 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
7797 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
7799 fprintf(out_stream, "\n");
7801 /* quantization style*/
7802 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
7803 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
7804 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
7805 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
7806 for (bandno = 0; bandno < numbands; bandno++) {
7807 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
7808 l_tccp->stepsizes[bandno].expn);
7810 fprintf(out_stream, "\n");
7813 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
7815 fprintf(out_stream, "\t\t }\n");
7816 } /*end of component of default tile*/
7817 fprintf(out_stream, "\t }\n"); /*end of default tile*/
7821 fprintf(out_stream, "}\n");
7826 * Dump an image header structure.
7828 *@param img_header the image header to dump.
7829 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
7830 *@param out_stream output stream where dump the elements.
7832 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
7837 fprintf(stdout, "[DEV] Dump a image_header struct {\n");
7841 fprintf(out_stream, "Image info {\n");
7842 tab[0] = '\t';tab[1] = '\0';
7845 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
7846 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
7847 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
7849 if (img_header->comps){
7851 for (compno = 0; compno < img_header->numcomps; compno++) {
7852 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
7853 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
7854 fprintf(out_stream,"%s}\n",tab);
7858 fprintf(out_stream, "}\n");
7862 * Dump a component image header structure.
7864 *@param comp_header the component image header to dump.
7865 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
7866 *@param out_stream output stream where dump the elements.
7868 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
7873 fprintf(stdout, "[DEV] Dump a image_comp_header struct {\n");
7876 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
7879 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
7880 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
7881 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
7884 fprintf(out_stream, "}\n");
7889 * Get the codestream info from a JPEG2000 codec.
7891 *@param p_j2k the component image header to dump.
7893 *@return the codestream information extract from the jpg2000 codec
7895 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
7898 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
7899 opj_tcp_v2_t *l_default_tile;
7900 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
7902 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
7904 cstr_info->tx0 = p_j2k->m_cp.tx0;
7905 cstr_info->ty0 = p_j2k->m_cp.ty0;
7906 cstr_info->tdx = p_j2k->m_cp.tdx;
7907 cstr_info->tdy = p_j2k->m_cp.tdy;
7908 cstr_info->tw = p_j2k->m_cp.tw;
7909 cstr_info->th = p_j2k->m_cp.th;
7911 cstr_info->tile_info = NULL; /* Not fill from the main header*/
7913 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7915 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
7916 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
7917 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
7918 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
7920 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
7922 for (compno = 0; compno < numcomps; compno++) {
7923 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
7924 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
7925 OPJ_INT32 bandno, numbands;
7928 l_tccp_info->csty = l_tccp->csty;
7929 l_tccp_info->numresolutions = l_tccp->numresolutions;
7930 l_tccp_info->cblkw = l_tccp->cblkw;
7931 l_tccp_info->cblkh = l_tccp->cblkh;
7932 l_tccp_info->cblksty = l_tccp->cblksty;
7933 l_tccp_info->qmfbid = l_tccp->qmfbid;
7934 if (l_tccp->numresolutions < J2K_MAXRLVLS)
7936 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
7937 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
7940 /* quantization style*/
7941 l_tccp_info->qntsty = l_tccp->qntsty;
7942 l_tccp_info->numgbits = l_tccp->numgbits;
7944 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
7945 if (numbands < J2K_MAXBANDS) {
7946 for (bandno = 0; bandno < numbands; bandno++) {
7947 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
7948 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
7953 l_tccp_info->roishift = l_tccp->roishift;
7961 * Get the codestream index from a JPEG2000 codec.
7963 *@param p_j2k the component image header to dump.
7965 *@return the codestream index extract from the jpg2000 codec
7967 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
7969 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
7970 opj_calloc(1,sizeof(opj_codestream_index_t));
7974 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
7975 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
7976 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
7978 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
7979 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
7980 if (!l_cstr_index->marker){
7981 opj_free( l_cstr_index);
7985 if (p_j2k->cstr_index->marker)
7986 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
7988 opj_free(l_cstr_index->marker);
7989 l_cstr_index->marker = NULL;
7992 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
7993 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
7994 if (!l_cstr_index->tile_index){
7995 opj_free( l_cstr_index->marker);
7996 opj_free( l_cstr_index);
8000 if (!p_j2k->cstr_index->tile_index){
8001 opj_free(l_cstr_index->tile_index);
8002 l_cstr_index->tile_index = NULL;
8005 OPJ_UINT32 it_tile = 0;
8006 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
8009 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
8011 l_cstr_index->tile_index[it_tile].marker =
8012 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
8014 if (!l_cstr_index->tile_index[it_tile].marker) {
8015 OPJ_UINT32 it_tile_free;
8017 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
8018 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
8021 opj_free( l_cstr_index->tile_index);
8022 opj_free( l_cstr_index->marker);
8023 opj_free( l_cstr_index);
8027 if (p_j2k->cstr_index->tile_index[it_tile].marker)
8028 memcpy( l_cstr_index->tile_index[it_tile].marker,
8029 p_j2k->cstr_index->tile_index[it_tile].marker,
8030 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
8032 opj_free(l_cstr_index->tile_index[it_tile].marker);
8033 l_cstr_index->tile_index[it_tile].marker = NULL;
8036 /* Tile part index*/
8037 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
8039 l_cstr_index->tile_index[it_tile].tp_index =
8040 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
8042 if(!l_cstr_index->tile_index[it_tile].tp_index){
8043 OPJ_UINT32 it_tile_free;
8045 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
8046 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
8047 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
8050 opj_free( l_cstr_index->tile_index);
8051 opj_free( l_cstr_index->marker);
8052 opj_free( l_cstr_index);
8056 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
8057 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
8058 p_j2k->cstr_index->tile_index[it_tile].tp_index,
8059 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
8062 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
8063 l_cstr_index->tile_index[it_tile].tp_index = NULL;
8066 /* Packet index (NOT USED)*/
8067 l_cstr_index->tile_index[it_tile].nb_packet = 0;
8068 l_cstr_index->tile_index[it_tile].packet_index = NULL;
8073 return l_cstr_index;
8076 opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
8078 OPJ_UINT32 it_tile=0;
8080 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.tw;
8081 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
8082 if (!p_j2k->cstr_index->tile_index)
8085 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
8086 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
8087 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
8088 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
8089 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
8090 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
8100 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
8101 opj_stream_private_t *p_stream,
8102 opj_event_mgr_t * p_manager)
8104 opj_bool l_go_on = OPJ_TRUE;
8105 OPJ_UINT32 l_current_tile_no;
8106 OPJ_UINT32 l_data_size,l_max_data_size;
8107 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
8108 OPJ_UINT32 l_nb_comps;
8109 OPJ_BYTE * l_current_data;
8111 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
8112 if (! l_current_data) {
8115 l_max_data_size = 1000;
8117 /*Allocate and initialize some elements of codestrem index*/
8118 if (!j2k_allocate_tile_element_cstr_index(p_j2k))
8122 if (! j2k_read_tile_header( p_j2k,
8125 &l_tile_x0, &l_tile_y0,
8126 &l_tile_x1, &l_tile_y1,
8131 opj_free(l_current_data);
8139 if (l_data_size > l_max_data_size) {
8140 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
8141 if (! l_current_data) {
8145 l_max_data_size = l_data_size;
8148 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
8149 opj_free(l_current_data);
8152 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);
8154 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
8155 opj_free(l_current_data);
8158 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
8162 opj_free(l_current_data);
8168 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
8170 void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
8173 assert(p_j2k != 00);
8175 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_tiles);
8176 /* DEVELOPER CORNER, add your custom procedures */
8181 * Read and decode one tile.
8183 opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
8184 opj_stream_private_t *p_stream,
8185 opj_event_mgr_t * p_manager)
8187 opj_bool l_go_on = OPJ_TRUE;
8188 OPJ_UINT32 l_current_tile_no;
8189 OPJ_UINT32 l_tile_no_to_dec;
8190 OPJ_UINT32 l_data_size,l_max_data_size;
8191 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
8192 OPJ_UINT32 l_nb_comps;
8193 OPJ_BYTE * l_current_data;
8195 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
8196 if (! l_current_data) {
8199 l_max_data_size = 1000;
8201 /*Allocate and initialize some elements of codestrem index if not already done*/
8202 if( !p_j2k->cstr_index->tile_index)
8204 if (!j2k_allocate_tile_element_cstr_index(p_j2k))
8208 /* Move into the codestream to the first SOT used to decode the desired tile */
8209 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
8210 if (p_j2k->cstr_index->tile_index)
8211 if(p_j2k->cstr_index->tile_index->tp_index)
8213 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
8214 /* not build the index for this tile, so we will move to the last SOT read*/
8215 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
8216 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
8221 if (opj_stream_read_seek(p_stream, p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos+2, p_manager)) {
8222 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
8226 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
8227 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
8228 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8232 if (! j2k_read_tile_header( p_j2k,
8235 &l_tile_x0, &l_tile_y0,
8236 &l_tile_x1, &l_tile_y1,
8241 opj_free(l_current_data);
8250 if (l_data_size > l_max_data_size) {
8251 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
8252 if (! l_current_data) {
8256 l_max_data_size = l_data_size;
8261 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
8262 opj_free(l_current_data);
8265 opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decode.\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8267 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
8268 opj_free(l_current_data);
8271 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
8273 if(l_current_tile_no == l_tile_no_to_dec)
8275 /* move into the codestream to the the first SOT (FIXME or not move?)*/
8276 if (opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) {
8277 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
8283 opj_event_msg_v2(p_manager, EVT_WARNING, "Tile read, decode and updated is not the desired (%d vs %d).\n", l_current_tile_no, l_tile_no_to_dec);
8288 opj_free(l_current_data);
8295 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
8297 void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
8300 assert(p_j2k != 00);
8302 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_one_tile);
8303 /* DEVELOPER CORNER, add your custom procedures */
8309 * Decodes the tiles of the stream.
8311 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
8312 opj_stream_private_t * p_stream,
8313 opj_image_t * p_image,
8314 opj_event_mgr_t * p_manager)
8321 p_j2k->m_output_image = opj_image_create0();
8322 if (! (p_j2k->m_output_image)) {
8325 opj_copy_image_header(p_image, p_j2k->m_output_image);
8327 /* customization of the decoding */
8328 j2k_setup_decoding(p_j2k);
8330 /* Decode the codestream */
8331 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8332 opj_image_destroy(p_j2k->m_private_image);
8333 p_j2k->m_private_image = NULL;
8337 /* Move data and copy one information from codec to output image*/
8338 for (compno = 0; compno < p_image->numcomps; compno++) {
8339 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
8340 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
8341 p_j2k->m_output_image->comps[compno].data = NULL;
8349 * Get the decoded tile.
8351 * @param p_j2k the jpeg2000 codestream codec.
8352 * @param p_stream input_stream
8353 * @param p_image output image. .
8354 * @param p_manager the user event manager
8355 * @param tile_index index of the tile we want decode
8357 * @return true if succeed.
8359 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
8360 opj_stream_private_t *p_stream,
8361 opj_image_t* p_image,
8362 struct opj_event_mgr * p_manager,
8363 OPJ_UINT32 tile_index )
8366 OPJ_UINT32 l_tile_x, l_tile_y;
8367 opj_image_comp_t* l_img_comp;
8370 opj_event_msg_v2(p_manager, EVT_ERROR, "We need a image previously created.\n");
8374 /* Compute the dimension of the desired tile*/
8375 l_tile_x = tile_index % p_j2k->m_cp.tw;
8376 l_tile_y = tile_index / p_j2k->m_cp.tw;
8378 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
8379 if (p_image->x0 < p_j2k->m_private_image->x0)
8380 p_image->x0 = p_j2k->m_private_image->x0;
8381 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
8382 if (p_image->x1 > p_j2k->m_private_image->x1)
8383 p_image->x1 = p_j2k->m_private_image->x1;
8385 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
8386 if (p_image->y0 < p_j2k->m_private_image->y0)
8387 p_image->y0 = p_j2k->m_private_image->y0;
8388 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
8389 if (p_image->y1 > p_j2k->m_private_image->y1)
8390 p_image->y1 = p_j2k->m_private_image->y1;
8392 l_img_comp = p_image->comps;
8393 for (compno=0; compno < p_image->numcomps; ++compno)
8395 OPJ_INT32 l_comp_x1, l_comp_y1;
8397 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
8398 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
8399 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
8400 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
8402 l_img_comp->w = int_ceildivpow2(l_comp_x1 - l_img_comp->x0, l_img_comp->factor);
8403 l_img_comp->h = int_ceildivpow2(l_comp_y1 - l_img_comp->y0, l_img_comp->factor);
8408 /* Destroy the previous output image*/
8409 if (p_j2k->m_output_image)
8410 opj_image_destroy(p_j2k->m_output_image);
8412 /* Create the ouput image from the information previously computed*/
8413 p_j2k->m_output_image = opj_image_create0();
8414 if (! (p_j2k->m_output_image)) {
8417 opj_copy_image_header(p_image, p_j2k->m_output_image);
8419 if ( (tile_index < 0) && (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
8420 opj_event_msg_v2(p_manager, EVT_ERROR, "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1);
8424 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
8426 /* customization of the decoding */
8427 j2k_setup_decoding_tile(p_j2k);
8429 /* Decode the codestream */
8430 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8431 opj_image_destroy(p_j2k->m_private_image);
8432 p_j2k->m_private_image = NULL;
8436 /* Move data and copy one information from codec to output image*/
8437 for (compno = 0; compno < p_image->numcomps; compno++) {
8438 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
8440 if (p_image->comps[compno].data)
8441 opj_free(p_image->comps[compno].data);
8443 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
8445 p_j2k->m_output_image->comps[compno].data = NULL;