2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2006-2007, Parvatha Elangovan
9 * Copyright (c) 2010-2011, Kaori Hagihara
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include "opj_includes.h"
36 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
39 /** @name Local static functions */
43 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
45 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
48 * The read header procedure.
50 opj_bool j2k_read_header_procedure(
52 struct opj_stream_private *p_stream,
53 struct opj_event_mgr * p_manager);
56 * The default decoding validation procedure without any extension.
58 * @param p_j2k the jpeg2000 codec to validate.
59 * @param p_stream the input stream to validate.
60 * @param p_manager the user event manager.
62 * @return true if the parameters are correct.
64 opj_bool j2k_decoding_validation (
66 opj_stream_private_t *p_stream,
67 opj_event_mgr_t * p_manager
71 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
72 * are valid. Developpers wanting to extend the library can add their own validation procedures.
74 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
77 * Builds the tcd decoder to use to decode tile.
79 opj_bool j2k_build_decoder (
81 opj_stream_private_t *p_stream,
82 opj_event_mgr_t * p_manager
86 * Excutes the given procedures on the given codec.
88 * @param p_procedure_list the list of procedures to execute
89 * @param p_j2k the jpeg2000 codec to execute the procedures on.
90 * @param p_stream the stream to execute the procedures on.
91 * @param p_manager the user manager.
93 * @return true if all the procedures were successfully executed.
95 static opj_bool j2k_exec (
97 opj_procedure_list_t * p_procedure_list,
98 opj_stream_private_t *p_stream,
99 opj_event_mgr_t * p_manager
103 * Copies the decoding tile parameters onto all the tile parameters.
104 * Creates also the tile decoder.
106 opj_bool j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
107 opj_stream_private_t *p_stream,
108 opj_event_mgr_t * p_manager );
111 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
112 * with the marker value.
113 * @param p_id Marker value to look up
115 * @return the handler associated with the id.
117 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
120 * Destroys a tile coding parameter structure.
122 * @param p_tcp the tile coding parameter to destroy.
124 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
127 * Destroys a codestream index structure.
129 * @param p_cstr_ind the codestream index parameter to destroy.
131 static void j2k_destroy_cstr_index (opj_codestream_index_t* p_cstr_ind);
134 * Destroys a coding parameter structure.
136 * @param p_cp the coding parameter to destroy.
138 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
142 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
143 * @param p_header_data the data contained in the COM box.
144 * @param p_j2k the jpeg2000 codec.
145 * @param p_header_size the size of the data contained in the COM marker.
146 * @param p_manager the user event manager.
148 static opj_bool j2k_read_SPCod_SPCoc(
151 OPJ_BYTE * p_header_data,
152 OPJ_UINT32 * p_header_size,
153 struct opj_event_mgr * p_manager
157 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
159 * @param p_tile_no the tile to output.
160 * @param p_comp_no the component number to output.
161 * @param p_data the data buffer.
162 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
163 * @param p_j2k J2K codec.
164 * @param p_manager the user event manager.
167 static opj_bool j2k_read_SQcd_SQcc(
170 OPJ_BYTE * p_header_data,
171 OPJ_UINT32 * p_header_size,
172 struct opj_event_mgr * p_manager
176 * Copies the tile component parameters of all the component from the first tile component.
178 * @param p_j2k the J2k codec.
180 static void j2k_copy_tile_component_parameters(
185 * Copies the tile quantization parameters of all the component from the first tile component.
187 * @param p_j2k the J2k codec.
189 static void j2k_copy_tile_quantization_parameters(
196 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
197 opj_stream_private_t *p_stream,
198 opj_event_mgr_t * p_manager);
200 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
204 * -----------------------------------------------------------------------
205 * -----------------------------------------------------------------------
206 * -----------------------------------------------------------------------
210 Write the SOC marker (Start Of Codestream)
211 @param j2k J2K handle
213 static void j2k_write_soc(opj_j2k_t *j2k);
215 Read the SOC marker (Start of Codestream)
216 @param j2k J2K handle
218 static void j2k_read_soc(opj_j2k_t *j2k);
221 * Reads a SOC marker (Start of Codestream)
222 * @param p_header_data the data contained in the SOC box.
223 * @param jp2 the jpeg2000 file codec.
224 * @param p_header_size the size of the data contained in the SOC marker.
225 * @param p_manager the user event manager.
227 static opj_bool j2k_read_soc_v2(
229 struct opj_stream_private *p_stream,
230 struct opj_event_mgr * p_manager
234 Write the SIZ marker (image and tile size)
235 @param j2k J2K handle
237 static void j2k_write_siz(opj_j2k_t *j2k);
239 Read the SIZ marker (image and tile size)
240 @param j2k J2K handle
242 static void j2k_read_siz(opj_j2k_t *j2k);
245 * Reads a SIZ marker (image and tile size)
246 * @param p_header_data the data contained in the SIZ box.
247 * @param jp2 the jpeg2000 file codec.
248 * @param p_header_size the size of the data contained in the SIZ marker.
249 * @param p_manager the user event manager.
251 static opj_bool j2k_read_siz_v2 (
253 OPJ_BYTE * p_header_data,
254 OPJ_UINT32 p_header_size,
255 struct opj_event_mgr * p_manager
259 Write the COM marker (comment)
260 @param j2k J2K handle
262 static void j2k_write_com(opj_j2k_t *j2k);
264 Read the COM marker (comment)
265 @param j2k J2K handle
267 static void j2k_read_com(opj_j2k_t *j2k);
269 * Reads a COM marker (comments)
270 * @param p_header_data the data contained in the COM box.
271 * @param jp2 the jpeg2000 file codec.
272 * @param p_header_size the size of the data contained in the COM marker.
273 * @param p_manager the user event manager.
275 static opj_bool j2k_read_com_v2 (
277 OPJ_BYTE * p_header_data,
278 OPJ_UINT32 p_header_size,
279 struct opj_event_mgr * p_manager
282 Write the value concerning the specified component in the marker COD and COC
283 @param j2k J2K handle
284 @param compno Number of the component concerned by the information written
286 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
288 Read the value concerning the specified component in the marker COD and COC
289 @param j2k J2K handle
290 @param compno Number of the component concerned by the information read
292 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
294 Write the COD marker (coding style default)
295 @param j2k J2K handle
297 static void j2k_write_cod(opj_j2k_t *j2k);
299 Read the COD marker (coding style default)
300 @param j2k J2K handle
302 static void j2k_read_cod(opj_j2k_t *j2k);
305 * Reads a COD marker (Coding Styke defaults)
306 * @param p_header_data the data contained in the COD box.
307 * @param p_j2k the jpeg2000 codec.
308 * @param p_header_size the size of the data contained in the COD marker.
309 * @param p_manager the user event manager.
311 static opj_bool j2k_read_cod_v2 (
313 OPJ_BYTE * p_header_data,
314 OPJ_UINT32 p_header_size,
315 struct opj_event_mgr * p_manager
319 Write the COC marker (coding style component)
320 @param j2k J2K handle
321 @param compno Number of the component concerned by the information written
323 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
325 Read the COC marker (coding style component)
326 @param j2k J2K handle
328 static void j2k_read_coc(opj_j2k_t *j2k);
331 * Reads a COC marker (Coding Style Component)
332 * @param p_header_data the data contained in the COC box.
333 * @param p_j2k the jpeg2000 codec.
334 * @param p_header_size the size of the data contained in the COC marker.
335 * @param p_manager the user event manager.
337 static opj_bool j2k_read_coc_v2 (
339 OPJ_BYTE * p_header_data,
340 OPJ_UINT32 p_header_size,
341 struct opj_event_mgr * p_manager
345 Write the value concerning the specified component in the marker QCD and QCC
346 @param j2k J2K handle
347 @param compno Number of the component concerned by the information written
349 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
351 Read the value concerning the specified component in the marker QCD and QCC
352 @param j2k J2K handle
353 @param compno Number of the component concern by the information read
354 @param len Length of the information in the QCX part of the marker QCD/QCC
356 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
358 Write the QCD marker (quantization default)
359 @param j2k J2K handle
361 static void j2k_write_qcd(opj_j2k_t *j2k);
363 Read the QCD marker (quantization default)
364 @param j2k J2K handle
366 static void j2k_read_qcd(opj_j2k_t *j2k);
369 * Reads a QCD marker (Quantization defaults)
370 * @param p_header_data the data contained in the QCD box.
371 * @param p_j2k the jpeg2000 codec.
372 * @param p_header_size the size of the data contained in the QCD marker.
373 * @param p_manager the user event manager.
375 static opj_bool j2k_read_qcd_v2 (
377 OPJ_BYTE * p_header_data,
378 OPJ_UINT32 p_header_size,
379 struct opj_event_mgr * p_manager
383 Write the QCC marker (quantization component)
384 @param j2k J2K handle
385 @param compno Number of the component concerned by the information written
387 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
389 Read the QCC marker (quantization component)
390 @param j2k J2K handle
392 static void j2k_read_qcc(opj_j2k_t *j2k);
394 * Reads a QCC marker (Quantization component)
395 * @param p_header_data the data contained in the QCC box.
396 * @param p_j2k the jpeg2000 codec.
397 * @param p_header_size the size of the data contained in the QCC marker.
398 * @param p_manager the user event manager.
400 static opj_bool j2k_read_qcc_v2(
402 OPJ_BYTE * p_header_data,
403 OPJ_UINT32 p_header_size,
404 struct opj_event_mgr * p_manager);
407 Write the POC marker (progression order change)
408 @param j2k J2K handle
410 static void j2k_write_poc(opj_j2k_t *j2k);
412 Read the POC marker (progression order change)
413 @param j2k J2K handle
415 static void j2k_read_poc(opj_j2k_t *j2k);
417 * Reads a POC marker (Progression Order Change)
419 * @param p_header_data the data contained in the POC box.
420 * @param p_j2k the jpeg2000 codec.
421 * @param p_header_size the size of the data contained in the POC marker.
422 * @param p_manager the user event manager.
424 static opj_bool j2k_read_poc_v2 (
426 OPJ_BYTE * p_header_data,
427 OPJ_UINT32 p_header_size,
428 struct opj_event_mgr * p_manager
431 Read the CRG marker (component registration)
432 @param j2k J2K handle
434 static void j2k_read_crg(opj_j2k_t *j2k);
436 * Reads a CRG marker (Component registration)
438 * @param p_header_data the data contained in the TLM box.
439 * @param p_j2k the jpeg2000 codec.
440 * @param p_header_size the size of the data contained in the TLM marker.
441 * @param p_manager the user event manager.
443 static opj_bool j2k_read_crg_v2 (
445 OPJ_BYTE * p_header_data,
446 OPJ_UINT32 p_header_size,
447 struct opj_event_mgr * p_manager
450 Read the TLM marker (tile-part lengths)
451 @param j2k J2K handle
453 static void j2k_read_tlm(opj_j2k_t *j2k);
455 * Reads a TLM marker (Tile Length Marker)
457 * @param p_header_data the data contained in the TLM box.
458 * @param p_j2k the jpeg2000 codec.
459 * @param p_header_size the size of the data contained in the TLM marker.
460 * @param p_manager the user event manager.
462 static opj_bool j2k_read_tlm_v2 (
464 OPJ_BYTE * p_header_data,
465 OPJ_UINT32 p_header_size,
466 struct opj_event_mgr * p_manager
469 Read the PLM marker (packet length, main header)
470 @param j2k J2K handle
472 static void j2k_read_plm(opj_j2k_t *j2k);
475 * Reads a PLM marker (Packet length, main header marker)
477 * @param p_header_data the data contained in the TLM box.
478 * @param p_j2k the jpeg2000 codec.
479 * @param p_header_size the size of the data contained in the TLM marker.
480 * @param p_manager the user event manager.
482 static opj_bool j2k_read_plm_v2 (
484 OPJ_BYTE * p_header_data,
485 OPJ_UINT32 p_header_size,
486 struct opj_event_mgr * p_manager
489 Read the PLT marker (packet length, tile-part header)
490 @param j2k J2K handle
492 static void j2k_read_plt(opj_j2k_t *j2k);
494 * Reads a PLT marker (Packet length, tile-part header)
496 * @param p_header_data the data contained in the PLT box.
497 * @param p_j2k the jpeg2000 codec.
498 * @param p_header_size the size of the data contained in the PLT marker.
499 * @param p_manager the user event manager.
501 static opj_bool j2k_read_plt_v2 (
503 OPJ_BYTE * p_header_data,
504 OPJ_UINT32 p_header_size,
505 struct opj_event_mgr * p_manager
508 Read the PPM marker (packet packet headers, main header)
509 @param j2k J2K handle
511 static void j2k_read_ppm(opj_j2k_t *j2k);
513 * Reads a PPM marker (Packed packet headers, main header)
515 * @param p_header_data the data contained in the POC box.
516 * @param p_j2k the jpeg2000 codec.
517 * @param p_header_size the size of the data contained in the POC marker.
518 * @param p_manager the user event manager.
520 static opj_bool j2k_read_ppm_v2 (
522 OPJ_BYTE * p_header_data,
523 OPJ_UINT32 p_header_size,
524 struct opj_event_mgr * p_manager
527 static opj_bool j2k_read_ppm_v3 (
529 OPJ_BYTE * p_header_data,
530 OPJ_UINT32 p_header_size,
531 struct opj_event_mgr * p_manager
535 Read the PPT marker (packet packet headers, tile-part header)
536 @param j2k J2K handle
538 static void j2k_read_ppt(opj_j2k_t *j2k);
540 * Reads a PPT marker (Packed packet headers, tile-part header)
542 * @param p_header_data the data contained in the PPT box.
543 * @param p_j2k the jpeg2000 codec.
544 * @param p_header_size the size of the data contained in the PPT marker.
545 * @param p_manager the user event manager.
547 static opj_bool j2k_read_ppt_v2 (
549 OPJ_BYTE * p_header_data,
550 OPJ_UINT32 p_header_size,
551 struct opj_event_mgr * p_manager
554 Write the TLM marker (Mainheader)
555 @param j2k J2K handle
557 static void j2k_write_tlm(opj_j2k_t *j2k);
559 Write the SOT marker (start of tile-part)
560 @param j2k J2K handle
562 static void j2k_write_sot(opj_j2k_t *j2k);
564 Read the SOT marker (start of tile-part)
565 @param j2k J2K handle
567 static void j2k_read_sot(opj_j2k_t *j2k);
570 * Reads a PPT marker (Packed packet headers, tile-part header)
572 * @param p_header_data the data contained in the PPT box.
573 * @param p_j2k the jpeg2000 codec.
574 * @param p_header_size the size of the data contained in the PPT marker.
575 * @param p_manager the user event manager.
577 static opj_bool j2k_read_sot_v2 (
579 OPJ_BYTE * p_header_data,
580 OPJ_UINT32 p_header_size,
581 struct opj_event_mgr * p_manager
584 Write the SOD marker (start of data)
585 @param j2k J2K handle
586 @param tile_coder Pointer to a TCD handle
588 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
590 Read the SOD marker (start of data)
591 @param j2k J2K handle
593 static void j2k_read_sod(opj_j2k_t *j2k);
596 * Reads a SOD marker (Start Of Data)
598 * @param p_header_data the data contained in the SOD box.
599 * @param p_j2k the jpeg2000 codec.
600 * @param p_header_size the size of the data contained in the SOD marker.
601 * @param p_manager the user event manager.
603 static opj_bool j2k_read_sod_v2 (
605 struct opj_stream_private *p_stream,
606 struct opj_event_mgr * p_manager
610 Write the RGN marker (region-of-interest)
611 @param j2k J2K handle
612 @param compno Number of the component concerned by the information written
613 @param tileno Number of the tile concerned by the information written
615 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
617 Read the RGN marker (region-of-interest)
618 @param j2k J2K handle
620 static void j2k_read_rgn(opj_j2k_t *j2k);
623 * Reads a RGN marker (Region Of Interest)
625 * @param p_header_data the data contained in the POC box.
626 * @param p_j2k the jpeg2000 codec.
627 * @param p_header_size the size of the data contained in the POC marker.
628 * @param p_manager the user event manager.
630 static opj_bool j2k_read_rgn_v2 (
632 OPJ_BYTE * p_header_data,
633 OPJ_UINT32 p_header_size,
634 struct opj_event_mgr * p_manager
638 Write the EOC marker (end of codestream)
639 @param j2k J2K handle
641 static void j2k_write_eoc(opj_j2k_t *j2k);
643 Read the EOC marker (end of codestream)
644 @param j2k J2K handle
646 static void j2k_read_eoc(opj_j2k_t *j2k);
649 * Reads a EOC marker (End Of Codestream)
651 * @param p_header_data the data contained in the SOD box.
652 * @param p_j2k the jpeg2000 codec.
653 * @param p_header_size the size of the data contained in the SOD marker.
654 * @param p_manager the user event manager.
656 static opj_bool j2k_read_eoc_v2 (
658 struct opj_stream_private *p_stream,
659 struct opj_event_mgr * p_manager
663 Read an unknown marker
664 @param j2k J2K handle
666 static void j2k_read_unk(opj_j2k_t *j2k);
668 Add main header marker information
669 @param cstr_info Codestream information structure
670 @param type marker type
671 @param pos byte offset of marker segment
672 @param len length of marker segment
674 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
676 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) ;
678 Add tile header marker information
679 @param tileno tile index number
680 @param cstr_info Codestream information structure
681 @param type marker type
682 @param pos byte offset of marker segment
683 @param len length of marker segment
685 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
689 * Reads an unknown marker
691 * @param p_stream the stream object to read from.
692 * @param p_j2k the jpeg2000 codec.
693 * @param p_manager the user event manager.
695 * @return true if the marker could be deduced.
697 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
698 struct opj_stream_private *p_stream,
699 OPJ_UINT32 *output_marker,
700 struct opj_event_mgr * p_manager );
703 * Copy the image header from the jpeg2000 codec into an external image_header
705 * @param p_j2k the jpeg2000 codec.
706 * @param p_image_header the external empty image header
708 * @return true if the image header is correctly copy.
710 static opj_bool j2k_copy_img_header(opj_j2k_v2_t* p_j2k, opj_image_t* p_image);
713 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
715 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
717 static opj_codestream_index_t* j2k_create_cstr_index(void);
723 /* ----------------------------------------------------------------------- */
724 typedef struct j2k_prog_order{
725 OPJ_PROG_ORDER enum_prog;
729 j2k_prog_order_t j2k_prog_order_list[] = {
735 {(OPJ_PROG_ORDER)-1, ""}
738 typedef struct opj_dec_memory_marker_handler
742 /** value of the state when the marker can appear */
744 /** action linked to the marker */
745 opj_bool (*handler) (
747 OPJ_BYTE * p_header_data,
748 OPJ_UINT32 p_header_size,
749 struct opj_event_mgr * p_manager
752 opj_dec_memory_marker_handler_t;
754 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
757 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
758 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
759 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
760 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
761 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
762 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
763 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
764 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
765 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
766 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
767 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
768 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
769 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
771 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
772 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
773 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
774 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
775 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
776 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
778 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
779 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
780 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
781 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
782 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
783 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
784 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
785 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
786 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
787 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
788 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
789 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
790 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
792 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
793 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
794 #ifdef TODO_MS /* FIXME */
795 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
796 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
797 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
798 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
801 #ifdef TODO_MS /* FIXME */
802 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
803 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
804 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
805 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
807 #endif /* USE_JPWL */
809 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
810 {J2K_MS_INSEC, 0, j2k_read_insec}
811 #endif /* USE_JPSEC */
812 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}//j2k_read_unk_v2}
817 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
818 j2k_prog_order_t *po;
819 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
820 if(po->enum_prog == prg_order){
827 /* ----------------------------------------------------------------------- */
828 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
832 opj_tcp_t *tcp = &cp->tcps[tileno];
833 prog = j2k_convert_progression_order(tcp->prg);
838 if( cp->tp_flag == prog[i] ){
839 tpend=1;cp->tp_pos=i;
843 tpnum= tpnum * tcp->pocs[pino].compE;
846 tpnum= tpnum * tcp->pocs[pino].resE;
849 tpnum= tpnum * tcp->pocs[pino].prcE;
852 tpnum= tpnum * tcp->pocs[pino].layE;
863 /** mem allocation for TLM marker*/
864 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
865 int pino,tileno,totnum_tp=0;
867 OPJ_ARG_NOT_USED(img_numcomp);
869 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
870 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
871 int cur_totnum_tp = 0;
872 opj_tcp_t *tcp = &cp->tcps[tileno];
873 for(pino = 0; pino <= tcp->numpocs; pino++) {
875 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
876 if(!pi) { return -1;}
877 tp_num = j2k_get_num_tp(cp,pino,tileno);
878 totnum_tp = totnum_tp + tp_num;
879 cur_totnum_tp = cur_totnum_tp + tp_num;
880 pi_destroy(pi, cp, tileno);
882 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
884 if (j2k->cstr_info) {
885 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
886 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
893 static void j2k_write_soc(opj_j2k_t *j2k) {
894 opj_cio_t *cio = j2k->cio;
895 cio_write(cio, J2K_MS_SOC, 2);
898 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
903 /* update markers struct */
904 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
905 #endif /* USE_JPWL */
909 static void j2k_read_soc(opj_j2k_t *j2k) {
910 j2k->state = J2K_STATE_MHSIZ;
912 if (j2k->cstr_info) {
913 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
914 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
919 * Reads a SOC marker (Start of Codestream)
920 * @param p_header_data the data contained in the SOC box.
921 * @param jp2 the jpeg2000 file codec.
922 * @param p_header_size the size of the data contained in the SOC marker.
923 * @param p_manager the user event manager.
925 static opj_bool j2k_read_soc_v2( opj_j2k_v2_t *p_j2k,
926 struct opj_stream_private *p_stream,
927 struct opj_event_mgr * p_manager )
934 assert(p_manager != 00);
935 assert(p_stream != 00);
937 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
941 opj_read_bytes(l_data,&l_marker,2);
942 if (l_marker != J2K_MS_SOC) {
946 /* Next marker should be a SIZ marker in the main header */
947 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
949 /* FIXME move it in a index structure included in p_j2k*/
950 p_j2k->cstr_index->main_head_start = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
952 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
954 /* Add the marker to the codestream index*/
955 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
960 static void j2k_write_siz(opj_j2k_t *j2k) {
964 opj_cio_t *cio = j2k->cio;
965 opj_image_t *image = j2k->image;
966 opj_cp_t *cp = j2k->cp;
968 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
969 lenp = cio_tell(cio);
971 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
972 cio_write(cio, image->x1, 4); /* Xsiz */
973 cio_write(cio, image->y1, 4); /* Ysiz */
974 cio_write(cio, image->x0, 4); /* X0siz */
975 cio_write(cio, image->y0, 4); /* Y0siz */
976 cio_write(cio, cp->tdx, 4); /* XTsiz */
977 cio_write(cio, cp->tdy, 4); /* YTsiz */
978 cio_write(cio, cp->tx0, 4); /* XT0siz */
979 cio_write(cio, cp->ty0, 4); /* YT0siz */
980 cio_write(cio, image->numcomps, 2); /* Csiz */
981 for (i = 0; i < image->numcomps; i++) {
982 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
983 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
984 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
986 len = cio_tell(cio) - lenp;
988 cio_write(cio, len, 2); /* Lsiz */
989 cio_seek(cio, lenp + len);
992 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
995 static void j2k_read_siz(opj_j2k_t *j2k) {
998 opj_cio_t *cio = j2k->cio;
999 opj_image_t *image = j2k->image;
1000 opj_cp_t *cp = j2k->cp;
1002 len = cio_read(cio, 2); /* Lsiz */
1003 cio_read(cio, 2); /* Rsiz (capabilities) */
1004 image->x1 = cio_read(cio, 4); /* Xsiz */
1005 image->y1 = cio_read(cio, 4); /* Ysiz */
1006 image->x0 = cio_read(cio, 4); /* X0siz */
1007 image->y0 = cio_read(cio, 4); /* Y0siz */
1008 cp->tdx = cio_read(cio, 4); /* XTsiz */
1009 cp->tdy = cio_read(cio, 4); /* YTsiz */
1010 cp->tx0 = cio_read(cio, 4); /* XT0siz */
1011 cp->ty0 = cio_read(cio, 4); /* YT0siz */
1013 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
1014 opj_event_msg(j2k->cinfo, EVT_ERROR,
1015 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
1016 image->x0,image->x1,image->y0,image->y1);
1020 image->numcomps = cio_read(cio, 2); /* Csiz */
1023 if (j2k->cp->correct) {
1024 /* if JPWL is on, we check whether TX errors have damaged
1025 too much the SIZ parameters */
1026 if (!(image->x1 * image->y1)) {
1027 opj_event_msg(j2k->cinfo, EVT_ERROR,
1028 "JPWL: bad image size (%d x %d)\n",
1029 image->x1, image->y1);
1030 if (!JPWL_ASSUME || JPWL_ASSUME) {
1031 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1035 if (image->numcomps != ((len - 38) / 3)) {
1036 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1037 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1038 image->numcomps, ((len - 38) / 3));
1040 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1043 /* we try to correct */
1044 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
1045 if (image->numcomps < ((len - 38) / 3)) {
1046 len = 38 + 3 * image->numcomps;
1047 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1050 image->numcomps = ((len - 38) / 3);
1051 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1056 /* update components number in the jpwl_exp_comps filed */
1057 cp->exp_comps = image->numcomps;
1059 #endif /* USE_JPWL */
1061 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
1062 for (i = 0; i < image->numcomps; i++) {
1064 tmp = cio_read(cio, 1); /* Ssiz_i */
1065 image->comps[i].prec = (tmp & 0x7f) + 1;
1066 image->comps[i].sgnd = tmp >> 7;
1067 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
1068 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
1071 if (j2k->cp->correct) {
1072 /* if JPWL is on, we check whether TX errors have damaged
1073 too much the SIZ parameters, again */
1074 if (!(image->comps[i].dx * image->comps[i].dy)) {
1075 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1076 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1077 i, i, image->comps[i].dx, image->comps[i].dy);
1079 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1082 /* we try to correct */
1083 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1084 if (!image->comps[i].dx) {
1085 image->comps[i].dx = 1;
1086 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1087 i, image->comps[i].dx);
1089 if (!image->comps[i].dy) {
1090 image->comps[i].dy = 1;
1091 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1092 i, image->comps[i].dy);
1097 #endif /* USE_JPWL */
1099 /* TODO: unused ? */
1100 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
1101 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
1103 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
1104 image->comps[i].factor = cp->reduce; /* reducing factor per component */
1107 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
1108 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
1111 if (j2k->cp->correct) {
1112 /* if JPWL is on, we check whether TX errors have damaged
1113 too much the SIZ parameters */
1114 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
1115 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1116 "JPWL: bad number of tiles (%d x %d)\n",
1119 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1122 /* we try to correct */
1123 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1126 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1129 if (cp->tw > cp->max_tiles) {
1131 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
1132 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1133 cp->max_tiles, cp->tw);
1137 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1140 if (cp->th > cp->max_tiles) {
1142 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1143 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1144 cp->max_tiles, cp->th);
1148 #endif /* USE_JPWL */
1150 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
1151 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
1152 cp->tileno_size = 0;
1155 if (j2k->cp->correct) {
1157 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1158 "JPWL: could not alloc tcps field of cp\n");
1159 if (!JPWL_ASSUME || JPWL_ASSUME) {
1160 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1165 #endif /* USE_JPWL */
1167 for (i = 0; i < cp->tw * cp->th; i++) {
1168 cp->tcps[i].POC = 0;
1169 cp->tcps[i].numpocs = 0;
1170 cp->tcps[i].first = 1;
1173 /* Initialization for PPM marker */
1175 cp->ppm_data = NULL;
1176 cp->ppm_data_first = NULL;
1177 cp->ppm_previous = 0;
1180 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
1181 for (i = 0; i < cp->tw * cp->th; i++) {
1182 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
1184 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
1185 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
1186 j2k->state = J2K_STATE_MH;
1189 if (j2k->cstr_info) {
1190 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1191 cstr_info->image_w = image->x1 - image->x0;
1192 cstr_info->image_h = image->y1 - image->y0;
1193 cstr_info->numcomps = image->numcomps;
1194 cstr_info->tw = cp->tw;
1195 cstr_info->th = cp->th;
1196 cstr_info->tile_x = cp->tdx;
1197 cstr_info->tile_y = cp->tdy;
1198 cstr_info->tile_Ox = cp->tx0;
1199 cstr_info->tile_Oy = cp->ty0;
1200 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
1206 * Reads a SIZ marker (image and tile size)
1207 * @param p_header_data the data contained in the SIZ box.
1208 * @param jp2 the jpeg2000 file codec.
1209 * @param p_header_size the size of the data contained in the SIZ marker.
1210 * @param p_manager the user event manager.
1212 opj_bool j2k_read_siz_v2 (
1213 opj_j2k_v2_t *p_j2k,
1214 OPJ_BYTE * p_header_data,
1215 OPJ_UINT32 p_header_size,
1216 struct opj_event_mgr * p_manager
1219 OPJ_UINT32 l_size, i;
1220 OPJ_UINT32 l_nb_comp;
1221 OPJ_UINT32 l_nb_comp_remain;
1222 OPJ_UINT32 l_remaining_size;
1223 OPJ_UINT32 l_nb_tiles;
1225 opj_image_t *l_image = 00;
1226 opj_cp_v2_t *l_cp = 00;
1227 opj_image_comp_t * l_img_comp = 00;
1228 opj_tcp_v2_t * l_current_tile_param = 00;
1231 assert(p_j2k != 00);
1232 assert(p_manager != 00);
1233 assert(p_header_data != 00);
1235 l_image = p_j2k->m_image;
1236 l_cp = &(p_j2k->m_cp);
1238 // minimum size == 39 - 3 (= minimum component parameter)
1239 if (p_header_size < 36) {
1240 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1244 l_remaining_size = p_header_size - 36;
1245 l_nb_comp = l_remaining_size / 3;
1246 l_nb_comp_remain = l_remaining_size % 3;
1247 if (l_nb_comp_remain != 0){
1248 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1252 l_size = p_header_size + 2; /* Lsiz */
1254 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
1256 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1257 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
1259 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
1261 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
1263 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
1265 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
1267 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
1269 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
1271 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
1273 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
1276 l_image->numcomps = (OPJ_UINT16) l_tmp;
1278 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
1282 if (l_image->numcomps != l_nb_comp) {
1283 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);
1288 if (l_cp->correct) {
1289 /* if JPWL is on, we check whether TX errors have damaged
1290 too much the SIZ parameters */
1291 if (!(l_image->x1 * l_image->y1)) {
1292 opj_event_msg_v2(p_manager, EVT_ERROR,
1293 "JPWL: bad image size (%d x %d)\n",
1294 l_image->x1, l_image->y1);
1295 if (!JPWL_ASSUME || JPWL_ASSUME) {
1296 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1301 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
1302 if (l_image->numcomps != ((len - 38) / 3)) {
1303 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1304 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1305 l_image->numcomps, ((len - 38) / 3));
1307 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1310 */ /* we try to correct */
1311 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
1312 if (l_image->numcomps < ((len - 38) / 3)) {
1313 len = 38 + 3 * l_image->numcomps;
1314 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1317 l_image->numcomps = ((len - 38) / 3);
1318 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1324 /* update components number in the jpwl_exp_comps filed */
1325 l_cp->exp_comps = l_image->numcomps;
1327 #endif /* USE_JPWL */
1329 // Allocate the resulting image components
1330 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
1331 if (l_image->comps == 00){
1332 l_image->numcomps = 0;
1333 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1337 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
1338 l_img_comp = l_image->comps;
1340 // Read the component information
1341 for (i = 0; i < l_image->numcomps; ++i){
1343 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
1345 l_img_comp->prec = (tmp & 0x7f) + 1;
1346 l_img_comp->sgnd = tmp >> 7;
1347 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
1349 l_img_comp->dx = (OPJ_INT32)tmp; // should be between 1 and 255
1350 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
1352 l_img_comp->dy = (OPJ_INT32)tmp; // should be between 1 and 255
1355 if (l_cp->correct) {
1356 /* if JPWL is on, we check whether TX errors have damaged
1357 too much the SIZ parameters, again */
1358 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
1359 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1360 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1361 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
1363 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1366 /* we try to correct */
1367 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1368 if (!l_image->comps[i].dx) {
1369 l_image->comps[i].dx = 1;
1370 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1371 i, l_image->comps[i].dx);
1373 if (!l_image->comps[i].dy) {
1374 l_image->comps[i].dy = 1;
1375 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1376 i, l_image->comps[i].dy);
1380 #endif /* USE_JPWL */
1381 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
1382 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
1386 // Compute the number of tiles
1387 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
1388 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
1389 l_nb_tiles = l_cp->tw * l_cp->th;
1391 // Define the tiles which will be decoded
1392 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
1393 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;
1394 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;
1395 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);
1396 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);
1399 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
1400 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
1401 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
1402 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
1406 if (l_cp->correct) {
1407 /* if JPWL is on, we check whether TX errors have damaged
1408 too much the SIZ parameters */
1409 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
1410 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1411 "JPWL: bad number of tiles (%d x %d)\n",
1412 l_cp->tw, l_cp->th);
1414 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1417 /* we try to correct */
1418 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
1421 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1424 if (l_cp->tw > l_cp->max_tiles) {
1426 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
1427 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1428 l_cp->max_tiles, l_cp->tw);
1432 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1435 if (l_cp->th > l_cp->max_tiles) {
1437 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1438 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1439 l_cp->max_tiles, l_cp->th);
1443 #endif /* USE_JPWL */
1445 /* memory allocations */
1446 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
1447 if (l_cp->tcps == 00) {
1448 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1451 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
1454 if (l_cp->correct) {
1456 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1457 "JPWL: could not alloc tcps field of cp\n");
1458 if (!JPWL_ASSUME || JPWL_ASSUME) {
1459 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
1464 #endif /* USE_JPWL */
1466 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
1467 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
1468 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
1469 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1472 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
1474 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
1475 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
1477 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
1478 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1481 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));
1482 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
1484 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
1485 (opj_simple_mcc_decorrelation_data_t*)
1486 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
1488 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
1489 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1492 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));
1493 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
1495 /* set up default dc level shift */
1496 for (i=0;i<l_image->numcomps;++i) {
1497 if (! l_image->comps[i].sgnd) {
1498 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
1502 l_current_tile_param = l_cp->tcps;
1503 for (i = 0; i < l_nb_tiles; ++i) {
1504 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
1505 if (l_current_tile_param->tccps == 00) {
1506 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1509 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
1511 ++l_current_tile_param;
1514 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; // FIXME J2K_DEC_STATE_MH;
1515 opj_image_comp_header_update(l_image,l_cp);
1522 static void j2k_write_com(opj_j2k_t *j2k) {
1526 if(j2k->cp->comment) {
1527 opj_cio_t *cio = j2k->cio;
1528 char *comment = j2k->cp->comment;
1530 cio_write(cio, J2K_MS_COM, 2);
1531 lenp = cio_tell(cio);
1533 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
1534 for (i = 0; i < strlen(comment); i++) {
1535 cio_write(cio, comment[i], 1);
1537 len = cio_tell(cio) - lenp;
1538 cio_seek(cio, lenp);
1539 cio_write(cio, len, 2);
1540 cio_seek(cio, lenp + len);
1544 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
1549 static void j2k_read_com(opj_j2k_t *j2k) {
1552 opj_cio_t *cio = j2k->cio;
1554 len = cio_read(cio, 2);
1555 cio_skip(cio, len - 2);
1558 * Reads a COM marker (comments)
1559 * @param p_header_data the data contained in the COM box.
1560 * @param jp2 the jpeg2000 file codec.
1561 * @param p_header_size the size of the data contained in the COM marker.
1562 * @param p_manager the user event manager.
1564 opj_bool j2k_read_com_v2 (
1565 opj_j2k_v2_t *p_j2k,
1566 OPJ_BYTE * p_header_data,
1567 OPJ_UINT32 p_header_size,
1568 struct opj_event_mgr * p_manager
1572 assert(p_j2k != 00);
1573 assert(p_manager != 00);
1574 assert(p_header_data != 00);
1579 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
1582 opj_cp_t *cp = j2k->cp;
1583 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1584 opj_tccp_t *tccp = &tcp->tccps[compno];
1585 opj_cio_t *cio = j2k->cio;
1587 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
1588 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
1589 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
1590 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
1591 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
1593 if (tccp->csty & J2K_CCP_CSTY_PRT) {
1594 for (i = 0; i < tccp->numresolutions; i++) {
1595 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
1600 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
1603 opj_cp_t *cp = j2k->cp;
1604 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1605 opj_tccp_t *tccp = &tcp->tccps[compno];
1606 opj_cio_t *cio = j2k->cio;
1608 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
1610 // If user wants to remove more resolutions than the codestream contains, return error
1611 if (cp->reduce >= tccp->numresolutions) {
1612 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
1613 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
1614 j2k->state |= J2K_STATE_ERR;
1617 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
1618 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
1619 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
1620 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
1621 if (tccp->csty & J2K_CP_CSTY_PRT) {
1622 for (i = 0; i < tccp->numresolutions; i++) {
1623 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
1624 tccp->prcw[i] = tmp & 0xf;
1625 tccp->prch[i] = tmp >> 4;
1630 if(j2k->cstr_info && compno == 0) {
1631 for (i = 0; i < tccp->numresolutions; i++) {
1632 if (tccp->csty & J2K_CP_CSTY_PRT) {
1633 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
1634 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
1637 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1638 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1645 static void j2k_write_cod(opj_j2k_t *j2k) {
1646 opj_cp_t *cp = NULL;
1647 opj_tcp_t *tcp = NULL;
1650 opj_cio_t *cio = j2k->cio;
1652 cio_write(cio, J2K_MS_COD, 2); /* COD */
1654 lenp = cio_tell(cio);
1658 tcp = &cp->tcps[j2k->curtileno];
1660 cio_write(cio, tcp->csty, 1); /* Scod */
1661 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
1662 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
1663 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
1665 j2k_write_cox(j2k, 0);
1666 len = cio_tell(cio) - lenp;
1667 cio_seek(cio, lenp);
1668 cio_write(cio, len, 2); /* Lcod */
1669 cio_seek(cio, lenp + len);
1672 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
1676 static void j2k_read_cod(opj_j2k_t *j2k) {
1679 opj_cio_t *cio = j2k->cio;
1680 opj_cp_t *cp = j2k->cp;
1681 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1682 opj_image_t *image = j2k->image;
1684 len = cio_read(cio, 2); /* Lcod */
1685 tcp->csty = cio_read(cio, 1); /* Scod */
1686 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
1687 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
1688 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
1690 pos = cio_tell(cio);
1691 for (i = 0; i < image->numcomps; i++) {
1692 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
1694 j2k_read_cox(j2k, i);
1698 if (j2k->cstr_info) {
1699 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1700 cstr_info->prog = tcp->prg;
1701 cstr_info->numlayers = tcp->numlayers;
1702 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
1703 for (i = 0; i < image->numcomps; i++) {
1704 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
1710 * Reads a COD marker (Coding Styke defaults)
1711 * @param p_header_data the data contained in the COD box.
1712 * @param p_j2k the jpeg2000 codec.
1713 * @param p_header_size the size of the data contained in the COD marker.
1714 * @param p_manager the user event manager.
1716 opj_bool j2k_read_cod_v2 (
1717 opj_j2k_v2_t *p_j2k,
1718 OPJ_BYTE * p_header_data,
1719 OPJ_UINT32 p_header_size,
1720 struct opj_event_mgr * p_manager
1726 opj_cp_v2_t *l_cp = 00;
1727 opj_tcp_v2_t *l_tcp = 00;
1728 opj_image_t *l_image = 00;
1731 assert(p_header_data != 00);
1732 assert(p_j2k != 00);
1733 assert(p_manager != 00);
1735 l_image = p_j2k->m_image;
1736 l_cp = &(p_j2k->m_cp);
1738 /* If we are in the first tile-part header of the current tile */
1739 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
1740 &l_cp->tcps[p_j2k->m_current_tile_number] :
1741 p_j2k->m_specific_param.m_decoder.m_default_tcp;
1743 /* Make sure room is sufficient */
1744 if (p_header_size < 5) {
1745 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
1749 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
1751 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
1753 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
1754 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
1757 // If user didn't set a number layer to decode take the max specify in the codestream.
1758 if (l_cp->m_specific_param.m_dec.m_layer) {
1759 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
1762 l_tcp->num_layers_to_decode = l_tcp->numlayers;
1765 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
1769 for (i = 0; i < l_image->numcomps; ++i) {
1770 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
1773 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
1774 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
1778 if (p_header_size != 0) {
1779 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
1783 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
1784 j2k_copy_tile_component_parameters(p_j2k);
1787 #ifdef WIP_REMOVE_MSD
1788 if (p_j2k->cstr_info) {
1789 //opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;
1790 p_j2k->cstr_info->prog = l_tcp->prg;
1791 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
1792 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
1793 for (i = 0; i < l_image->numcomps; ++i) {
1794 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
1802 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
1805 opj_cp_t *cp = j2k->cp;
1806 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1807 opj_image_t *image = j2k->image;
1808 opj_cio_t *cio = j2k->cio;
1810 cio_write(cio, J2K_MS_COC, 2); /* COC */
1811 lenp = cio_tell(cio);
1813 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
1814 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
1815 j2k_write_cox(j2k, compno);
1816 len = cio_tell(cio) - lenp;
1817 cio_seek(cio, lenp);
1818 cio_write(cio, len, 2); /* Lcoc */
1819 cio_seek(cio, lenp + len);
1822 static void j2k_read_coc(opj_j2k_t *j2k) {
1825 opj_cp_t *cp = j2k->cp;
1826 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1827 opj_image_t *image = j2k->image;
1828 opj_cio_t *cio = j2k->cio;
1830 len = cio_read(cio, 2); /* Lcoc */
1831 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
1832 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
1833 j2k_read_cox(j2k, compno);
1837 * Reads a COC marker (Coding Style Component)
1838 * @param p_header_data the data contained in the COC box.
1839 * @param p_j2k the jpeg2000 codec.
1840 * @param p_header_size the size of the data contained in the COC marker.
1841 * @param p_manager the user event manager.
1843 opj_bool j2k_read_coc_v2 (
1844 opj_j2k_v2_t *p_j2k,
1845 OPJ_BYTE * p_header_data,
1846 OPJ_UINT32 p_header_size,
1847 struct opj_event_mgr * p_manager
1850 opj_cp_v2_t *l_cp = NULL;
1851 opj_tcp_v2_t *l_tcp = NULL;
1852 opj_image_t *l_image = NULL;
1853 OPJ_UINT32 l_comp_room;
1854 OPJ_UINT32 l_comp_no;
1857 assert(p_header_data != 00);
1858 assert(p_j2k != 00);
1859 assert(p_manager != 00);
1861 l_cp = &(p_j2k->m_cp);
1862 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
1863 &l_cp->tcps[p_j2k->m_current_tile_number] :
1864 p_j2k->m_specific_param.m_decoder.m_default_tcp;
1865 l_image = p_j2k->m_image;
1867 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
1869 // make sure room is sufficient
1870 if (p_header_size < l_comp_room + 1) {
1871 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
1874 p_header_size -= l_comp_room + 1;
1876 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
1877 p_header_data += l_comp_room;
1878 if (l_comp_no >= l_image->numcomps) {
1879 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
1883 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
1886 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
1887 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
1891 if (p_header_size != 0) {
1892 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
1898 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
1899 int bandno, numbands;
1902 opj_cp_t *cp = j2k->cp;
1903 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1904 opj_tccp_t *tccp = &tcp->tccps[compno];
1905 opj_cio_t *cio = j2k->cio;
1907 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
1908 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
1910 for (bandno = 0; bandno < numbands; bandno++) {
1911 expn = tccp->stepsizes[bandno].expn;
1912 mant = tccp->stepsizes[bandno].mant;
1914 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1915 cio_write(cio, expn << 3, 1); /* SPqcx_i */
1917 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
1922 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
1924 int bandno, numbands;
1926 opj_cp_t *cp = j2k->cp;
1927 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1928 opj_tccp_t *tccp = &tcp->tccps[compno];
1929 opj_cio_t *cio = j2k->cio;
1931 tmp = cio_read(cio, 1); /* Sqcx */
1932 tccp->qntsty = tmp & 0x1f;
1933 tccp->numgbits = tmp >> 5;
1934 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
1935 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
1938 if (j2k->cp->correct) {
1940 /* if JPWL is on, we check whether there are too many subbands */
1941 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
1942 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1943 "JPWL: bad number of subbands in Sqcx (%d)\n",
1946 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1949 /* we try to correct */
1951 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
1952 "- setting number of bands to %d => HYPOTHESIS!!!\n",
1959 /* We check whether there are too many subbands */
1960 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
1961 opj_event_msg(j2k->cinfo, EVT_WARNING ,
1962 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
1963 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
1966 #endif /* USE_JPWL */
1968 for (bandno = 0; bandno < numbands; bandno++) {
1970 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1971 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
1974 tmp = cio_read(cio, 2); /* SPqcx_i */
1978 if (bandno < J2K_MAXBANDS){
1979 tccp->stepsizes[bandno].expn = expn;
1980 tccp->stepsizes[bandno].mant = mant;
1984 /* Add Antonin : if scalar_derived -> compute other stepsizes */
1985 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
1986 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
1987 tccp->stepsizes[bandno].expn =
1988 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
1989 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
1990 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
1996 static void j2k_write_qcd(opj_j2k_t *j2k) {
1999 opj_cio_t *cio = j2k->cio;
2001 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
2002 lenp = cio_tell(cio);
2004 j2k_write_qcx(j2k, 0);
2005 len = cio_tell(cio) - lenp;
2006 cio_seek(cio, lenp);
2007 cio_write(cio, len, 2); /* Lqcd */
2008 cio_seek(cio, lenp + len);
2011 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
2014 static void j2k_read_qcd(opj_j2k_t *j2k) {
2017 opj_cio_t *cio = j2k->cio;
2018 opj_image_t *image = j2k->image;
2020 len = cio_read(cio, 2); /* Lqcd */
2021 pos = cio_tell(cio);
2022 for (i = 0; i < image->numcomps; i++) {
2024 j2k_read_qcx(j2k, i, len - 2);
2029 * Reads a QCD marker (Quantization defaults)
2030 * @param p_header_data the data contained in the QCD box.
2031 * @param p_j2k the jpeg2000 codec.
2032 * @param p_header_size the size of the data contained in the QCD marker.
2033 * @param p_manager the user event manager.
2035 opj_bool j2k_read_qcd_v2 (
2036 opj_j2k_v2_t *p_j2k,
2037 OPJ_BYTE * p_header_data,
2038 OPJ_UINT32 p_header_size,
2039 struct opj_event_mgr * p_manager
2043 assert(p_header_data != 00);
2044 assert(p_j2k != 00);
2045 assert(p_manager != 00);
2047 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2048 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2052 if (p_header_size != 0) {
2053 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2057 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2058 j2k_copy_tile_quantization_parameters(p_j2k);
2063 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
2066 opj_cio_t *cio = j2k->cio;
2068 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
2069 lenp = cio_tell(cio);
2071 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
2072 j2k_write_qcx(j2k, compno);
2073 len = cio_tell(cio) - lenp;
2074 cio_seek(cio, lenp);
2075 cio_write(cio, len, 2); /* Lqcc */
2076 cio_seek(cio, lenp + len);
2079 static void j2k_read_qcc(opj_j2k_t *j2k) {
2081 int numcomp = j2k->image->numcomps;
2082 opj_cio_t *cio = j2k->cio;
2084 len = cio_read(cio, 2); /* Lqcc */
2085 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
2088 if (j2k->cp->correct) {
2090 static int backup_compno = 0;
2092 /* compno is negative or larger than the number of components!!! */
2093 if ((compno < 0) || (compno >= numcomp)) {
2094 opj_event_msg(j2k->cinfo, EVT_ERROR,
2095 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2098 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2101 /* we try to correct */
2102 compno = backup_compno % numcomp;
2103 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
2104 "- setting component number to %d\n",
2108 /* keep your private count of tiles */
2111 #endif /* USE_JPWL */
2113 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
2117 * Reads a QCC marker (Quantization component)
2118 * @param p_header_data the data contained in the QCC box.
2119 * @param p_j2k the jpeg2000 codec.
2120 * @param p_header_size the size of the data contained in the QCC marker.
2121 * @param p_manager the user event manager.
2123 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
2124 OPJ_BYTE * p_header_data,
2125 OPJ_UINT32 p_header_size,
2126 struct opj_event_mgr * p_manager)
2128 OPJ_UINT32 l_num_comp,l_comp_no;
2131 assert(p_header_data != 00);
2132 assert(p_j2k != 00);
2133 assert(p_manager != 00);
2135 l_num_comp = p_j2k->m_image->numcomps;
2137 if (l_num_comp <= 256) {
2138 if (p_header_size < 1) {
2139 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2142 opj_read_bytes(p_header_data,&l_comp_no,1);
2147 if (p_header_size < 2) {
2148 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2151 opj_read_bytes(p_header_data,&l_comp_no,2);
2157 if (p_j2k->m_cp.correct) {
2159 static OPJ_UINT32 backup_compno = 0;
2161 /* compno is negative or larger than the number of components!!! */
2162 if ((l_comp_no < 0) || (l_comp_no >= l_num_comp)) {
2163 opj_event_msg_v2(p_manager, EVT_ERROR,
2164 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2165 l_comp_no, l_num_comp);
2167 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2170 /* we try to correct */
2171 l_comp_no = backup_compno % l_num_comp;
2172 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
2173 "- setting component number to %d\n",
2177 /* keep your private count of tiles */
2180 #endif /* USE_JPWL */
2182 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2183 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2187 if (p_header_size != 0) {
2188 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2196 static void j2k_write_poc(opj_j2k_t *j2k) {
2197 int len, numpchgs, i;
2199 int numcomps = j2k->image->numcomps;
2201 opj_cp_t *cp = j2k->cp;
2202 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2203 opj_tccp_t *tccp = &tcp->tccps[0];
2204 opj_cio_t *cio = j2k->cio;
2206 numpchgs = 1 + tcp->numpocs;
2207 cio_write(cio, J2K_MS_POC, 2); /* POC */
2208 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
2209 cio_write(cio, len, 2); /* Lpoc */
2210 for (i = 0; i < numpchgs; i++) {
2211 opj_poc_t *poc = &tcp->pocs[i];
2212 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
2213 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
2214 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
2215 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
2216 cio_write(cio, poc->resno1, 1); /* REpoc_i */
2217 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
2218 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
2219 poc->compno1 = int_min(poc->compno1, numcomps);
2220 cio_write(cio, poc->prg, 1); /* Ppoc_i */
2224 static void j2k_read_poc(opj_j2k_t *j2k) {
2225 int len, numpchgs, i, old_poc;
2227 int numcomps = j2k->image->numcomps;
2229 opj_cp_t *cp = j2k->cp;
2230 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2231 opj_cio_t *cio = j2k->cio;
2233 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
2235 len = cio_read(cio, 2); /* Lpoc */
2236 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
2238 for (i = old_poc; i < numpchgs + old_poc; i++) {
2240 poc = &tcp->pocs[i];
2241 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
2242 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
2243 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
2244 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
2245 poc->compno1 = int_min(
2246 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
2247 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
2250 tcp->numpocs = numpchgs + old_poc - 1;
2254 * Reads a POC marker (Progression Order Change)
2256 * @param p_header_data the data contained in the POC box.
2257 * @param p_j2k the jpeg2000 codec.
2258 * @param p_header_size the size of the data contained in the POC marker.
2259 * @param p_manager the user event manager.
2261 opj_bool j2k_read_poc_v2 (
2262 opj_j2k_v2_t *p_j2k,
2263 OPJ_BYTE * p_header_data,
2264 OPJ_UINT32 p_header_size,
2265 struct opj_event_mgr * p_manager)
2267 OPJ_UINT32 i, l_nb_comp, l_tmp;
2268 opj_image_t * l_image = 00;
2269 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
2270 OPJ_UINT32 l_chunk_size, l_comp_room;
2272 opj_cp_v2_t *l_cp = 00;
2273 opj_tcp_v2_t *l_tcp = 00;
2274 opj_poc_t *l_current_poc = 00;
2277 assert(p_header_data != 00);
2278 assert(p_j2k != 00);
2279 assert(p_manager != 00);
2281 l_image = p_j2k->m_image;
2282 l_nb_comp = l_image->numcomps;
2283 if (l_nb_comp <= 256) {
2289 l_chunk_size = 5 + 2 * l_comp_room;
2290 l_current_poc_nb = p_header_size / l_chunk_size;
2291 l_current_poc_remaining = p_header_size % l_chunk_size;
2293 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
2294 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
2298 l_cp = &(p_j2k->m_cp);
2299 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2300 &l_cp->tcps[p_j2k->m_current_tile_number] :
2301 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2302 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
2303 l_current_poc_nb += l_old_poc_nb;
2305 assert(l_current_poc_nb < 32);
2307 /* now poc is in use.*/
2310 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
2311 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
2312 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
2314 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
2315 p_header_data+=l_comp_room;
2316 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
2318 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
2320 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
2321 p_header_data+=l_comp_room;
2322 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
2324 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
2325 /* make sure comp is in acceptable bounds */
2326 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
2330 l_tcp->numpocs = l_current_poc_nb - 1;
2334 static void j2k_read_crg(opj_j2k_t *j2k) {
2335 int len, i, Xcrg_i, Ycrg_i;
2337 opj_cio_t *cio = j2k->cio;
2338 int numcomps = j2k->image->numcomps;
2340 len = cio_read(cio, 2); /* Lcrg */
2341 for (i = 0; i < numcomps; i++) {
2342 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
2343 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
2348 * Reads a CRG marker (Component registration)
2350 * @param p_header_data the data contained in the TLM box.
2351 * @param p_j2k the jpeg2000 codec.
2352 * @param p_header_size the size of the data contained in the TLM marker.
2353 * @param p_manager the user event manager.
2355 opj_bool j2k_read_crg_v2 (
2356 opj_j2k_v2_t *p_j2k,
2357 OPJ_BYTE * p_header_data,
2358 OPJ_UINT32 p_header_size,
2359 struct opj_event_mgr * p_manager
2362 OPJ_UINT32 l_nb_comp;
2364 assert(p_header_data != 00);
2365 assert(p_j2k != 00);
2366 assert(p_manager != 00);
2368 l_nb_comp = p_j2k->m_image->numcomps;
2370 if (p_header_size != l_nb_comp *4) {
2371 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
2374 /* Do not care of this at the moment since only local variables are set here */
2377 (i = 0; i < l_nb_comp; ++i)
2379 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
2381 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
2388 static void j2k_read_tlm(opj_j2k_t *j2k) {
2389 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
2390 long int Ttlm_i, Ptlm_i;
2392 opj_cio_t *cio = j2k->cio;
2394 len = cio_read(cio, 2); /* Ltlm */
2395 Ztlm = cio_read(cio, 1); /* Ztlm */
2396 Stlm = cio_read(cio, 1); /* Stlm */
2397 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
2398 SP = (Stlm >> 6) & 0x01;
2399 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
2400 for (i = 0; i < tile_tlm; i++) {
2401 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
2402 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
2407 * Reads a TLM marker (Tile Length Marker)
2409 * @param p_header_data the data contained in the TLM box.
2410 * @param p_j2k the jpeg2000 codec.
2411 * @param p_header_size the size of the data contained in the TLM marker.
2412 * @param p_manager the user event manager.
2414 opj_bool j2k_read_tlm_v2 (
2415 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
2421 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
2423 assert(p_header_data != 00);
2424 assert(p_j2k != 00);
2425 assert(p_manager != 00);
2427 if (p_header_size < 2) {
2428 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2433 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
2435 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
2438 l_ST = ((l_Stlm >> 4) & 0x3);
2439 l_SP = (l_Stlm >> 6) & 0x1;
2441 l_Ptlm_size = (l_SP + 1) * 2;
2442 l_quotient = l_Ptlm_size + l_ST;
2444 l_tot_num_tp = p_header_size / l_quotient;
2445 l_tot_num_tp_remaining = p_header_size % l_quotient;
2447 if (l_tot_num_tp_remaining != 0) {
2448 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2451 /* FIXME Do not care of this at the moment since only local variables are set here */
2454 (i = 0; i < l_tot_num_tp; ++i)
2456 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
2457 p_header_data += l_ST;
2458 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
2459 p_header_data += l_Ptlm_size;
2464 static void j2k_read_plm(opj_j2k_t *j2k) {
2465 int len, i, Zplm, Nplm, add, packet_len = 0;
2467 opj_cio_t *cio = j2k->cio;
2469 len = cio_read(cio, 2); /* Lplm */
2470 Zplm = cio_read(cio, 1); /* Zplm */
2473 Nplm = cio_read(cio, 4); /* Nplm */
2475 for (i = Nplm; i > 0; i--) {
2476 add = cio_read(cio, 1);
2478 packet_len = (packet_len << 7) + add; /* Iplm_ij */
2479 if ((add & 0x80) == 0) {
2490 * Reads a PLM marker (Packet length, main header marker)
2492 * @param p_header_data the data contained in the TLM box.
2493 * @param p_j2k the jpeg2000 codec.
2494 * @param p_header_size the size of the data contained in the TLM marker.
2495 * @param p_manager the user event manager.
2497 opj_bool j2k_read_plm_v2 (
2498 opj_j2k_v2_t *p_j2k,
2499 OPJ_BYTE * p_header_data,
2500 OPJ_UINT32 p_header_size,
2501 struct opj_event_mgr * p_manager
2505 assert(p_header_data != 00);
2506 assert(p_j2k != 00);
2507 assert(p_manager != 00);
2509 if (p_header_size < 1) {
2510 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2513 /* Do not care of this at the moment since only local variables are set here */
2515 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
2522 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
2524 p_header_size -= (1+l_Nplm);
2528 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2532 (i = 0; i < l_Nplm; ++i)
2534 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
2536 // take only the last seven bytes
2537 l_packet_len |= (l_tmp & 0x7f);
2545 // store packet length and proceed to next packet
2552 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2560 static void j2k_read_plt(opj_j2k_t *j2k) {
2561 int len, i, Zplt, packet_len = 0, add;
2563 opj_cio_t *cio = j2k->cio;
2565 len = cio_read(cio, 2); /* Lplt */
2566 Zplt = cio_read(cio, 1); /* Zplt */
2567 for (i = len - 3; i > 0; i--) {
2568 add = cio_read(cio, 1);
2569 packet_len = (packet_len << 7) + add; /* Iplt_i */
2570 if ((add & 0x80) == 0) {
2578 * Reads a PLT marker (Packet length, tile-part header)
2580 * @param p_header_data the data contained in the PLT box.
2581 * @param p_j2k the jpeg2000 codec.
2582 * @param p_header_size the size of the data contained in the PLT marker.
2583 * @param p_manager the user event manager.
2585 opj_bool j2k_read_plt_v2 (
2586 opj_j2k_v2_t *p_j2k,
2587 OPJ_BYTE * p_header_data,
2588 OPJ_UINT32 p_header_size,
2589 struct opj_event_mgr * p_manager
2592 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
2595 assert(p_header_data != 00);
2596 assert(p_j2k != 00);
2597 assert(p_manager != 00);
2599 if (p_header_size < 1) {
2600 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2604 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
2608 for (i = 0; i < p_header_size; ++i) {
2609 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
2611 // take only the last seven bytes
2612 l_packet_len |= (l_tmp & 0x7f);
2617 // store packet length and proceed to next packet
2622 if (l_packet_len != 0) {
2623 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2630 static void j2k_read_ppm(opj_j2k_t *j2k) {
2631 int len, Z_ppm, i, j;
2634 opj_cp_t *cp = j2k->cp;
2635 opj_cio_t *cio = j2k->cio;
2637 len = cio_read(cio, 2);
2640 Z_ppm = cio_read(cio, 1); /* Z_ppm */
2643 if (cp->ppm_previous == 0) {
2644 N_ppm = cio_read(cio, 4); /* N_ppm */
2647 N_ppm = cp->ppm_previous;
2650 if (Z_ppm == 0) { /* First PPM marker */
2651 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
2652 cp->ppm_data_first = cp->ppm_data;
2653 cp->ppm_len = N_ppm;
2654 } else { /* NON-first PPM marker */
2655 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
2658 /* this memory allocation check could be done even in non-JPWL cases */
2660 if (!cp->ppm_data) {
2661 opj_event_msg(j2k->cinfo, EVT_ERROR,
2662 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
2664 if (!JPWL_ASSUME || JPWL_ASSUME) {
2665 opj_free(cp->ppm_data);
2666 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2673 cp->ppm_data_first = cp->ppm_data;
2674 cp->ppm_len = N_ppm + cp->ppm_store;
2676 for (i = N_ppm; i > 0; i--) { /* Read packet header */
2677 cp->ppm_data[j] = cio_read(cio, 1);
2681 break; /* Case of non-finished packet header in present marker but finished in next one */
2683 cp->ppm_previous = i - 1;
2688 * Reads a PPM marker (Packed packet headers, main header)
2690 * @param p_header_data the data contained in the POC box.
2691 * @param p_j2k the jpeg2000 codec.
2692 * @param p_header_size the size of the data contained in the POC marker.
2693 * @param p_manager the user event manager.
2695 opj_bool j2k_read_ppm_v2 (
2696 opj_j2k_v2_t *p_j2k,
2697 OPJ_BYTE * p_header_data,
2698 OPJ_UINT32 p_header_size,
2699 struct opj_event_mgr * p_manager
2703 opj_cp_v2_t *l_cp = 00;
2704 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
2707 assert(p_header_data != 00);
2708 assert(p_j2k != 00);
2709 assert(p_manager != 00);
2711 if (p_header_size < 1) {
2712 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2716 l_cp = &(p_j2k->m_cp);
2719 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
2725 if (p_header_size < 4) {
2726 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2730 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
2734 /* First PPM marker: Initialization */
2735 l_cp->ppm_len = l_N_ppm;
2736 l_cp->ppm_data_size = 0;
2738 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
2739 if (l_cp->ppm_buffer == 00) {
2740 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2743 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
2745 l_cp->ppm_data = l_cp->ppm_buffer;
2749 if (l_cp->ppm_data_size == l_cp->ppm_len) {
2750 if (p_header_size >= 4) {
2752 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
2755 l_cp->ppm_len += l_N_ppm ;
2757 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
2758 if (l_cp->ppm_buffer == 00) {
2759 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2762 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
2764 l_cp->ppm_data = l_cp->ppm_buffer;
2771 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
2773 if (l_remaining_data <= p_header_size) {
2774 /* we must store less information than available in the packet */
2775 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
2776 l_cp->ppm_data_size = l_cp->ppm_len;
2777 p_header_size -= l_remaining_data;
2778 p_header_data += l_remaining_data;
2781 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
2782 l_cp->ppm_data_size += p_header_size;
2783 p_header_data += p_header_size;
2795 * Reads a PPM marker (Packed packet headers, main header)
2797 * @param p_header_data the data contained in the POC box.
2798 * @param p_j2k the jpeg2000 codec.
2799 * @param p_header_size the size of the data contained in the POC marker.
2800 * @param p_manager the user event manager.
2802 opj_bool j2k_read_ppm_v3 (
2803 opj_j2k_v2_t *p_j2k,
2804 OPJ_BYTE * p_header_data,
2805 OPJ_UINT32 p_header_size,
2806 struct opj_event_mgr * p_manager
2809 opj_cp_v2_t *l_cp = 00;
2810 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
2813 assert(p_header_data != 00);
2814 assert(p_j2k != 00);
2815 assert(p_manager != 00);
2817 // Minimum size of PPM marker is equal to the size of Zppm element
2818 if (p_header_size < 1) {
2819 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2823 l_cp = &(p_j2k->m_cp);
2826 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
2832 // We need now at least the Nppm^0 element
2833 if (p_header_size < 4) {
2834 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2838 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
2842 /* First PPM marker: Initialization */
2843 l_cp->ppm_len = l_N_ppm;
2844 l_cp->ppm_data_read = 0;
2846 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
2847 if (l_cp->ppm_data == 00) {
2848 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2851 memset(l_cp->ppm_data,0,l_cp->ppm_len);
2853 l_cp->ppm_data_current = l_cp->ppm_data;
2855 //l_cp->ppm_data = l_cp->ppm_buffer;
2858 if (p_header_size < 4) {
2859 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
2863 // Uncompleted Ippm series in the previous PPM marker?
2864 if (l_cp->ppm_data_read < l_cp->ppm_len) {
2865 // Get the place where add the remaining Ippm series
2866 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
2867 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
2870 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
2874 // Increase the size of ppm_data to add the new Ippm series
2875 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
2877 // Keep the position of the place where concatenate the new series
2878 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
2879 l_cp->ppm_len += l_N_ppm;
2884 l_remaining_data = p_header_size;
2886 while (l_remaining_data >= l_N_ppm) {
2887 // read a complete Ippm series
2888 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
2889 p_header_size -= l_N_ppm;
2890 p_header_data += l_N_ppm;
2892 l_cp->ppm_data_read += l_N_ppm; // Increase the number of data read
2896 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
2901 l_remaining_data = p_header_size;
2905 l_remaining_data = p_header_size;
2907 // Next Ippm series is a complete series ?
2908 if (l_remaining_data > l_N_ppm) {
2909 // Increase the size of ppm_data to add the new Ippm series
2910 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
2912 // Keep the position of the place where concatenate the new series
2913 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
2914 l_cp->ppm_len += l_N_ppm;
2919 // Need to read an incomplete Ippm series
2920 if (l_remaining_data) {
2921 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
2923 // Keep the position of the place where concatenate the new series
2924 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
2925 l_cp->ppm_len += l_N_ppm;
2927 // Read incomplete Ippm series
2928 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
2929 p_header_size -= l_remaining_data;
2930 p_header_data += l_remaining_data;
2932 l_cp->ppm_data_read += l_remaining_data; // Increase the number of data read
2937 if (l_cp->ppm_data_size == l_cp->ppm_len) {
2938 if (p_header_size >= 4) {
2940 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
2943 l_cp->ppm_len += l_N_ppm ;
2945 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
2946 if (l_cp->ppm_buffer == 00) {
2947 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2950 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
2952 l_cp->ppm_data = l_cp->ppm_buffer;
2959 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
2961 if (l_remaining_data <= p_header_size) {
2962 /* we must store less information than available in the packet */
2963 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
2964 l_cp->ppm_data_size = l_cp->ppm_len;
2965 p_header_size -= l_remaining_data;
2966 p_header_data += l_remaining_data;
2969 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
2970 l_cp->ppm_data_size += p_header_size;
2971 p_header_data += p_header_size;
2980 static void j2k_read_ppt(opj_j2k_t *j2k) {
2981 int len, Z_ppt, i, j = 0;
2983 opj_cp_t *cp = j2k->cp;
2984 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
2985 opj_cio_t *cio = j2k->cio;
2987 len = cio_read(cio, 2);
2988 Z_ppt = cio_read(cio, 1);
2990 if (Z_ppt == 0) { /* First PPT marker */
2991 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
2992 tcp->ppt_data_first = tcp->ppt_data;
2994 tcp->ppt_len = len - 3;
2995 } else { /* NON-first PPT marker */
2996 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
2997 tcp->ppt_data_first = tcp->ppt_data;
2998 tcp->ppt_len = len - 3 + tcp->ppt_store;
3001 for (i = len - 3; i > 0; i--) {
3002 tcp->ppt_data[j] = cio_read(cio, 1);
3009 * Reads a PPT marker (Packed packet headers, tile-part header)
3011 * @param p_header_data the data contained in the PPT box.
3012 * @param p_j2k the jpeg2000 codec.
3013 * @param p_header_size the size of the data contained in the PPT marker.
3014 * @param p_manager the user event manager.
3016 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
3017 OPJ_BYTE * p_header_data,
3018 OPJ_UINT32 p_header_size,
3019 struct opj_event_mgr * p_manager )
3021 opj_cp_v2_t *l_cp = 00;
3022 opj_tcp_v2_t *l_tcp = 00;
3026 assert(p_header_data != 00);
3027 assert(p_j2k != 00);
3028 assert(p_manager != 00);
3030 /* We need to have the Z_ppt element at minimum */
3031 if (p_header_size < 1) {
3032 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3036 l_cp = &(p_j2k->m_cp);
3038 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");
3042 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3045 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3049 /* Allocate buffer to read the packet header */
3051 /* First PPT marker */
3052 l_tcp->ppt_data_size = 0;
3053 l_tcp->ppt_len = p_header_size;
3055 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3056 if (l_tcp->ppt_buffer == 00) {
3057 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3060 l_tcp->ppt_data = l_tcp->ppt_buffer;
3062 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3065 l_tcp->ppt_len += p_header_size;
3067 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3068 if (l_tcp->ppt_buffer == 00) {
3069 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3072 l_tcp->ppt_data = l_tcp->ppt_buffer;
3074 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3077 /* Read packet header from buffer */
3078 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3080 l_tcp->ppt_data_size += p_header_size;
3085 static void j2k_write_tlm(opj_j2k_t *j2k){
3087 opj_cio_t *cio = j2k->cio;
3088 j2k->tlm_start = cio_tell(cio);
3089 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
3090 lenp = 4 + (5*j2k->totnum_tp);
3091 cio_write(cio,lenp,2); /* Ltlm */
3092 cio_write(cio, 0,1); /* Ztlm=0*/
3093 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3094 cio_skip(cio,5*j2k->totnum_tp);
3097 static void j2k_write_sot(opj_j2k_t *j2k) {
3100 opj_cio_t *cio = j2k->cio;
3102 j2k->sot_start = cio_tell(cio);
3103 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
3104 lenp = cio_tell(cio);
3105 cio_skip(cio, 2); /* Lsot (further) */
3106 cio_write(cio, j2k->curtileno, 2); /* Isot */
3107 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
3108 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
3109 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
3110 len = cio_tell(cio) - lenp;
3111 cio_seek(cio, lenp);
3112 cio_write(cio, len, 2); /* Lsot */
3113 cio_seek(cio, lenp + len);
3117 /* update markers struct */
3118 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
3119 #endif /* USE_JPWL */
3122 if( j2k->cstr_info && j2k->cur_tp_num==0){
3123 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
3127 static void j2k_read_sot(opj_j2k_t *j2k) {
3128 int len, tileno, totlen, partno, numparts, i;
3129 opj_tcp_t *tcp = NULL;
3132 opj_cp_t *cp = j2k->cp;
3133 opj_cio_t *cio = j2k->cio;
3135 len = cio_read(cio, 2);
3136 tileno = cio_read(cio, 2);
3139 if (j2k->cp->correct) {
3141 static int backup_tileno = 0;
3143 /* tileno is negative or larger than the number of tiles!!! */
3144 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3145 opj_event_msg(j2k->cinfo, EVT_ERROR,
3146 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3147 tileno, (cp->tw * cp->th));
3149 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3152 /* we try to correct */
3153 tileno = backup_tileno;
3154 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3155 "- setting tile number to %d\n",
3159 /* keep your private count of tiles */
3162 #endif /* USE_JPWL */
3164 if (cp->tileno_size == 0) {
3165 cp->tileno[cp->tileno_size] = tileno;
3169 while (i < cp->tileno_size && status == 0) {
3170 status = cp->tileno[i] == tileno ? 1 : 0;
3174 cp->tileno[cp->tileno_size] = tileno;
3179 totlen = cio_read(cio, 4);
3182 if (j2k->cp->correct) {
3184 /* totlen is negative or larger than the bytes left!!! */
3185 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
3186 opj_event_msg(j2k->cinfo, EVT_ERROR,
3187 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3188 totlen, cio_numbytesleft(cio) + 8);
3190 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3193 /* we try to correct */
3195 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3196 "- setting Psot to %d => assuming it is the last tile\n",
3201 #endif /* USE_JPWL */
3204 totlen = cio_numbytesleft(cio) + 8;
3206 partno = cio_read(cio, 1);
3207 numparts = cio_read(cio, 1);
3209 if (partno >= numparts) {
3210 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);
3211 numparts = partno+1;
3214 j2k->curtileno = tileno;
3215 j2k->cur_tp_num = partno;
3216 j2k->eot = cio_getbp(cio) - 12 + totlen;
3217 j2k->state = J2K_STATE_TPH;
3218 tcp = &cp->tcps[j2k->curtileno];
3221 if (j2k->cstr_info) {
3224 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
3225 j2k->cstr_info->tile[tileno].tileno = tileno;
3226 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
3227 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3229 j2k->cstr_info->tile[tileno].end_pos += totlen;
3231 j2k->cstr_info->tile[tileno].num_tps = numparts;
3233 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
3235 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)
3236 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
3237 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3238 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3241 if (tcp->first == 1) {
3242 /* Initialization PPT */
3243 opj_tccp_t *tmp = tcp->tccps;
3244 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
3246 tcp->ppt_data = NULL;
3247 tcp->ppt_data_first = NULL;
3250 for (i = 0; i < j2k->image->numcomps; i++) {
3251 tcp->tccps[i] = j2k->default_tcp->tccps[i];
3253 cp->tcps[j2k->curtileno].first = 0;
3258 * Reads a PPT marker (Packed packet headers, tile-part header)
3260 * @param p_header_data the data contained in the PPT box.
3261 * @param p_j2k the jpeg2000 codec.
3262 * @param p_header_size the size of the data contained in the PPT marker.
3263 * @param p_manager the user event manager.
3265 opj_bool j2k_read_sot_v2 (
3266 opj_j2k_v2_t *p_j2k,
3267 OPJ_BYTE * p_header_data,
3268 OPJ_UINT32 p_header_size,
3269 struct opj_event_mgr * p_manager
3273 opj_cp_v2_t *l_cp = 00;
3274 opj_tcp_v2_t *l_tcp = 00;
3275 OPJ_UINT32 l_tot_len, l_num_parts = 0;
3276 OPJ_UINT32 l_current_part;
3277 OPJ_UINT32 l_tile_x,l_tile_y;
3280 assert(p_header_data != 00);
3281 assert(p_j2k != 00);
3282 assert(p_manager != 00);
3284 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
3285 if (p_header_size != 8) {
3286 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3290 l_cp = &(p_j2k->m_cp);
3291 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
3294 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3295 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
3296 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
3299 if (l_cp->correct) {
3301 int tileno = p_j2k->m_current_tile_number;
3302 static int backup_tileno = 0;
3304 /* tileno is negative or larger than the number of tiles!!! */
3305 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
3306 opj_event_msg_v2(p_manager, EVT_ERROR,
3307 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3308 tileno, (l_cp->tw * l_cp->th));
3310 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3313 /* we try to correct */
3314 tileno = backup_tileno;
3315 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3316 "- setting tile number to %d\n",
3320 /* keep your private count of tiles */
3323 #endif /* USE_JPWL */
3325 /* look for the tile in the list of already processed tile (in parts). */
3326 /* Optimization possible here with a more complex data structure and with the removing of tiles */
3327 /* since the time taken by this function can only grow at the time */
3329 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
3333 if (l_cp->correct) {
3335 /* totlen is negative or larger than the bytes left!!! */
3336 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))) { */
3337 opj_event_msg_v2(p_manager, EVT_ERROR,
3338 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3339 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
3341 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3344 /* we try to correct */
3346 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3347 "- setting Psot to %d => assuming it is the last tile\n",
3351 #endif /* USE_JPWL */
3353 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
3355 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
3356 "we assuming it is the last tile-part of the codestream.\n");
3357 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3360 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
3363 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
3366 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
3367 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
3368 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
3369 if (l_tcp->m_nb_tile_parts) {
3370 if (l_current_part >= l_tcp->m_nb_tile_parts){
3371 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
3372 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
3373 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
3377 l_tcp->m_nb_tile_parts = l_num_parts;
3380 /* If know the number of tile part header we will check if we didn't read the last*/
3381 if (l_tcp->m_nb_tile_parts) {
3382 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
3383 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
3387 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
3388 /* Keep the size of data to skip after this marker */
3389 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
3392 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
3393 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
3396 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
3398 /* Check if the current tile is outside the area we want decode (in tile index)*/
3399 p_j2k->m_specific_param.m_decoder.m_skip_data =
3400 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
3401 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
3402 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
3403 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
3407 /* 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 */
3408 /* if (p_j2k->cstr_info) {
3411 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
3414 p_j2k->cstr_info->tile[tileno].tileno = tileno;
3415 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
3416 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3417 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
3420 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
3423 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
3427 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
3430 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
3431 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3432 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3437 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
3440 opj_tcp_t *tcp = NULL;
3441 opj_codestream_info_t *cstr_info = NULL;
3443 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
3444 opj_cp_t *cp = j2k->cp;
3445 opj_cio_t *cio = j2k->cio;
3447 tcd->tp_num = j2k->tp_num ;
3448 tcd->cur_tp_num = j2k->cur_tp_num;
3450 cio_write(cio, J2K_MS_SOD, 2);
3452 if( j2k->cstr_info && j2k->cur_tp_num==0){
3453 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
3456 if (j2k->curtileno == 0) {
3457 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
3461 cstr_info = j2k->cstr_info;
3463 if (!j2k->cur_tp_num ) {
3464 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3465 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
3468 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
3469 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
3473 /* update markers struct */
3474 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
3475 #endif /* USE_JPWL */
3480 tcp = &cp->tcps[j2k->curtileno];
3481 for (layno = 0; layno < tcp->numlayers; layno++) {
3482 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
3483 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
3484 } else if (tcp->rates[layno]) {
3485 tcp->rates[layno]=1;
3488 if(j2k->cur_tp_num == 0){
3489 tcd->tcd_image->tiles->packno = 0;
3491 cstr_info->packno = 0;
3494 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
3496 /* Writing Psot in SOT marker */
3497 totlen = cio_tell(cio) + l - j2k->sot_start;
3498 cio_seek(cio, j2k->sot_start + 6);
3499 cio_write(cio, totlen, 4);
3500 cio_seek(cio, j2k->sot_start + totlen);
3501 /* Writing Ttlm and Ptlm in TLM marker */
3503 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
3504 cio_write(cio, j2k->curtileno, 1);
3505 cio_write(cio, totlen, 4);
3507 cio_seek(cio, j2k->sot_start + totlen);
3510 static void j2k_read_sod(opj_j2k_t *j2k) {
3511 int len, truncate = 0, i;
3512 unsigned char *data = NULL, *data_ptr = NULL;
3514 opj_cio_t *cio = j2k->cio;
3515 int curtileno = j2k->curtileno;
3518 if (j2k->cstr_info) {
3519 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
3520 cio_tell(cio) + j2k->pos_correction - 1;
3521 if (j2k->cur_tp_num == 0)
3522 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3523 j2k->cstr_info->packno = 0;
3526 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
3528 if (len == cio_numbytesleft(cio) + 1) {
3529 truncate = 1; /* Case of a truncate codestream */
3532 data = j2k->tile_data[curtileno];
3533 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
3535 data_ptr = data + j2k->tile_len[curtileno];
3536 for (i = 0; i < len; i++) {
3537 data_ptr[i] = cio_read(cio, 1);
3540 j2k->tile_len[curtileno] += len;
3541 j2k->tile_data[curtileno] = data;
3544 j2k->state = J2K_STATE_TPHSOT;
3546 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
3552 * Reads a SOD marker (Start Of Data)
3554 * @param p_header_data the data contained in the SOD box.
3555 * @param p_j2k the jpeg2000 codec.
3556 * @param p_header_size the size of the data contained in the SOD marker.
3557 * @param p_manager the user event manager.
3559 opj_bool j2k_read_sod_v2 (
3560 opj_j2k_v2_t *p_j2k,
3561 struct opj_stream_private *p_stream,
3562 struct opj_event_mgr * p_manager
3565 OPJ_UINT32 l_current_read_size;
3566 opj_codestream_index_t * l_cstr_index = 00;
3567 OPJ_BYTE ** l_current_data = 00;
3568 opj_tcp_v2_t * l_tcp = 00;
3569 OPJ_UINT32 * l_tile_len = 00;
3572 assert(p_j2k != 00);
3573 assert(p_manager != 00);
3574 assert(p_stream != 00);
3576 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
3578 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part)
3579 p_j2k->m_specific_param.m_decoder.m_sot_length = opj_stream_get_number_byte_left(p_stream) - 2;
3581 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
3583 l_current_data = &(l_tcp->m_data);
3584 l_tile_len = &l_tcp->m_data_size;
3586 if (! *l_current_data) {
3587 *l_current_data = (OPJ_BYTE*) opj_malloc/*FIXME V2 -> my_opj_malloc*/(p_j2k->m_specific_param.m_decoder.m_sot_length);
3590 *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);
3593 if (*l_current_data == 00) {
3594 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
3598 #ifdef TODO_MSD /* FIXME */
3599 l_cstr_index = p_j2k->cstr_index;
3603 OPJ_SIZE_T l_current_pos = opj_stream_tell(p_stream)-1;
3604 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[p_j2k->m_specific_param.m_encoder.m_current_tile_part_number].tp_end_header = l_current_pos;
3606 l_cstr_index->packno = 0;
3610 l_current_read_size = opj_stream_read_data( p_stream,
3611 *l_current_data + *l_tile_len,
3612 p_j2k->m_specific_param.m_decoder.m_sot_length,
3615 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
3616 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
3619 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
3622 *l_tile_len += l_current_read_size;
3628 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
3629 opj_cp_t *cp = j2k->cp;
3630 opj_tcp_t *tcp = &cp->tcps[tileno];
3631 opj_cio_t *cio = j2k->cio;
3632 int numcomps = j2k->image->numcomps;
3634 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
3635 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
3636 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
3637 cio_write(cio, 0, 1); /* Srgn */
3638 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
3641 static void j2k_read_rgn(opj_j2k_t *j2k) {
3642 int len, compno, roisty;
3644 opj_cp_t *cp = j2k->cp;
3645 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3646 opj_cio_t *cio = j2k->cio;
3647 int numcomps = j2k->image->numcomps;
3649 len = cio_read(cio, 2); /* Lrgn */
3650 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
3651 roisty = cio_read(cio, 1); /* Srgn */
3654 if (j2k->cp->correct) {
3655 /* totlen is negative or larger than the bytes left!!! */
3656 if (compno >= numcomps) {
3657 opj_event_msg(j2k->cinfo, EVT_ERROR,
3658 "JPWL: bad component number in RGN (%d when there are only %d)\n",
3660 if (!JPWL_ASSUME || JPWL_ASSUME) {
3661 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3666 #endif /* USE_JPWL */
3668 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
3671 static void j2k_write_eoc(opj_j2k_t *j2k) {
3672 opj_cio_t *cio = j2k->cio;
3673 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
3674 cio_write(cio, J2K_MS_EOC, 2);
3678 /* update markers struct */
3679 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
3680 #endif /* USE_JPWL */
3685 * Reads a RGN marker (Region Of Interest)
3687 * @param p_header_data the data contained in the POC box.
3688 * @param p_j2k the jpeg2000 codec.
3689 * @param p_header_size the size of the data contained in the POC marker.
3690 * @param p_manager the user event manager.
3692 opj_bool j2k_read_rgn_v2 (
3693 opj_j2k_v2_t *p_j2k,
3694 OPJ_BYTE * p_header_data,
3695 OPJ_UINT32 p_header_size,
3696 struct opj_event_mgr * p_manager
3699 OPJ_UINT32 l_nb_comp;
3700 opj_image_t * l_image = 00;
3702 opj_cp_v2_t *l_cp = 00;
3703 opj_tcp_v2_t *l_tcp = 00;
3704 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
3707 assert(p_header_data != 00);
3708 assert(p_j2k != 00);
3709 assert(p_manager != 00);
3711 l_image = p_j2k->m_image;
3712 l_nb_comp = l_image->numcomps;
3714 if (l_nb_comp <= 256) {
3719 if (p_header_size != 2 + l_comp_room) {
3720 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
3724 l_cp = &(p_j2k->m_cp);
3725 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3726 &l_cp->tcps[p_j2k->m_current_tile_number] :
3727 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3729 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
3730 p_header_data+=l_comp_room;
3731 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
3735 if (l_cp->correct) {
3736 /* totlen is negative or larger than the bytes left!!! */
3737 if (l_comp_room >= l_nb_comp) {
3738 opj_event_msg_v2(p_manager, EVT_ERROR,
3739 "JPWL: bad component number in RGN (%d when there are only %d)\n",
3740 l_comp_room, l_nb_comp);
3741 if (!JPWL_ASSUME || JPWL_ASSUME) {
3742 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3747 #endif /* USE_JPWL */
3749 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
3756 static void j2k_read_eoc(opj_j2k_t *j2k) {
3760 /* if packets should be decoded */
3761 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
3762 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
3763 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
3764 for (i = 0; i < j2k->cp->tileno_size; i++) {
3765 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
3766 tileno = j2k->cp->tileno[i];
3767 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
3768 opj_free(j2k->tile_data[tileno]);
3769 j2k->tile_data[tileno] = NULL;
3770 tcd_free_decode_tile(tcd, i);
3771 if (success == OPJ_FALSE) {
3772 j2k->state |= J2K_STATE_ERR;
3776 tcd_free_decode(tcd);
3779 /* if packets should not be decoded */
3781 for (i = 0; i < j2k->cp->tileno_size; i++) {
3782 tileno = j2k->cp->tileno[i];
3783 opj_free(j2k->tile_data[tileno]);
3784 j2k->tile_data[tileno] = NULL;
3787 if (j2k->state & J2K_STATE_ERR)
3788 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
3790 j2k->state = J2K_STATE_MT;
3794 * Reads a EOC marker (End Of Codestream)
3796 * @param p_header_data the data contained in the SOD box.
3797 * @param p_j2k the jpeg2000 codec.
3798 * @param p_header_size the size of the data contained in the SOD marker.
3799 * @param p_manager the user event manager.
3801 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
3802 struct opj_stream_private *p_stream,
3803 struct opj_event_mgr * p_manager )
3806 opj_tcd_v2_t * l_tcd = 00;
3807 OPJ_UINT32 l_nb_tiles;
3808 opj_tcp_v2_t * l_tcp = 00;
3812 assert(p_j2k != 00);
3813 assert(p_manager != 00);
3814 assert(p_stream != 00);
3816 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3817 l_tcp = p_j2k->m_cp.tcps;
3819 l_tcd = tcd_create_v2(OPJ_TRUE);
3821 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
3825 for (i = 0; i < l_nb_tiles; ++i) {
3826 if (l_tcp->m_data) {
3827 if (! tcd_init_decode_tile(l_tcd, i)) {
3828 tcd_destroy_v2(l_tcd);
3829 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
3833 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
3837 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
3842 j2k_tcp_destroy(l_tcp);
3846 tcd_destroy_v2(l_tcd);
3850 typedef struct opj_dec_mstabent {
3853 /** value of the state when the marker can appear */
3855 /** action linked to the marker */
3856 void (*handler) (opj_j2k_t *j2k);
3857 } opj_dec_mstabent_t;
3859 opj_dec_mstabent_t j2k_dec_mstab[] = {
3860 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
3861 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
3862 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
3863 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
3864 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
3865 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
3866 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
3867 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
3868 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
3869 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
3870 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
3871 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
3872 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
3873 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
3874 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
3875 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
3877 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
3878 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
3881 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
3882 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
3883 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
3884 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
3885 #endif /* USE_JPWL */
3887 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
3888 {J2K_MS_INSEC, 0, j2k_read_insec},
3889 #endif /* USE_JPSEC */
3891 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
3894 static void j2k_read_unk(opj_j2k_t *j2k) {
3895 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
3898 if (j2k->cp->correct) {
3900 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
3901 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3902 id = cio_read(j2k->cio, 2);
3903 opj_event_msg(j2k->cinfo, EVT_ERROR,
3904 "JPWL: really don't know this marker %x\n",
3907 opj_event_msg(j2k->cinfo, EVT_ERROR,
3908 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
3911 /* OK, activate this at your own risk!!! */
3912 /* we look for the marker at the minimum hamming distance from this */
3913 while (j2k_dec_mstab[m].id) {
3915 /* 1's where they differ */
3916 tmp_id = j2k_dec_mstab[m].id ^ id;
3918 /* compute the hamming distance between our id and the current */
3920 for (i = 0; i < 16; i++) {
3921 if ((tmp_id >> i) & 0x0001) {
3926 /* if current distance is smaller, set the minimum */
3927 if (cur_dist < min_dist) {
3928 min_dist = cur_dist;
3929 min_id = j2k_dec_mstab[m].id;
3932 /* jump to the next marker */
3936 /* do we substitute the marker? */
3937 if (min_dist < JPWL_MAXIMUM_HAMMING) {
3938 opj_event_msg(j2k->cinfo, EVT_ERROR,
3939 "- marker %x is at distance %d from the read %x\n",
3940 min_id, min_dist, id);
3941 opj_event_msg(j2k->cinfo, EVT_ERROR,
3942 "- trying to substitute in place and crossing fingers!\n");
3943 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3944 cio_write(j2k->cio, min_id, 2);
3947 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3952 #endif /* USE_JPWL */
3957 * Reads an unknown marker
3959 * @param p_stream the stream object to read from.
3960 * @param p_j2k the jpeg2000 codec.
3961 * @param p_manager the user event manager.
3963 * @return true if the marker could be deduced.
3965 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
3966 struct opj_stream_private *p_stream,
3967 OPJ_UINT32 *output_marker,
3968 struct opj_event_mgr * p_manager
3971 OPJ_UINT32 l_unknown_marker;
3972 const opj_dec_memory_marker_handler_t * l_marker_handler;
3973 OPJ_UINT32 l_size_unk = 2;
3976 assert(p_j2k != 00);
3977 assert(p_manager != 00);
3978 assert(p_stream != 00);
3980 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
3983 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
3984 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
3985 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
3989 // read 2 bytes as the new marker ID
3990 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
3992 if (!(l_unknown_marker < 0xff00)) {
3994 // Get the marker handler from the marker ID
3995 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
3997 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
3998 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
4002 if (l_marker_handler->id != J2K_MS_UNK) {
4003 /* Add the marker to the codestream index*/
4004 if (l_marker_handler->id != J2K_MS_SOT)
4005 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
4006 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
4008 break; /* next marker is known and well located */
4016 *output_marker = l_marker_handler->id ;
4022 Read the lookup table containing all the marker, status and action
4023 @param id Marker value
4025 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
4026 opj_dec_mstabent_t *e;
4027 for (e = j2k_dec_mstab; e->id != 0; e++) {
4035 /* ----------------------------------------------------------------------- */
4036 /* J2K / JPT decoder interface */
4037 /* ----------------------------------------------------------------------- */
4039 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
4040 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4044 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
4045 if(!j2k->default_tcp) {
4051 j2k->tile_data = NULL;
4056 void j2k_destroy_decompress(opj_j2k_t *j2k) {
4059 if(j2k->tile_len != NULL) {
4060 opj_free(j2k->tile_len);
4062 if(j2k->tile_data != NULL) {
4063 opj_free(j2k->tile_data);
4065 if(j2k->default_tcp != NULL) {
4066 opj_tcp_t *default_tcp = j2k->default_tcp;
4067 if(default_tcp->ppt_data_first != NULL) {
4068 opj_free(default_tcp->ppt_data_first);
4070 if(j2k->default_tcp->tccps != NULL) {
4071 opj_free(j2k->default_tcp->tccps);
4073 opj_free(j2k->default_tcp);
4075 if(j2k->cp != NULL) {
4076 opj_cp_t *cp = j2k->cp;
4077 if(cp->tcps != NULL) {
4078 for(i = 0; i < cp->tw * cp->th; i++) {
4079 if(cp->tcps[i].ppt_data_first != NULL) {
4080 opj_free(cp->tcps[i].ppt_data_first);
4082 if(cp->tcps[i].tccps != NULL) {
4083 opj_free(cp->tcps[i].tccps);
4088 if(cp->ppm_data_first != NULL) {
4089 opj_free(cp->ppm_data_first);
4091 if(cp->tileno != NULL) {
4092 opj_free(cp->tileno);
4094 if(cp->comment != NULL) {
4095 opj_free(cp->comment);
4103 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
4104 if(j2k && parameters) {
4105 /* create and initialize the coding parameters structure */
4106 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4107 cp->reduce = parameters->cp_reduce;
4108 cp->layer = parameters->cp_layer;
4109 cp->limit_decoding = parameters->cp_limit_decoding;
4112 cp->correct = parameters->jpwl_correct;
4113 cp->exp_comps = parameters->jpwl_exp_comps;
4114 cp->max_tiles = parameters->jpwl_max_tiles;
4115 #endif /* USE_JPWL */
4118 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
4123 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
4125 if(j2k && parameters) {
4126 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
4127 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
4130 j2k->m_cp.correct = parameters->jpwl_correct;
4131 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
4132 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
4133 #endif /* USE_JPWL */
4137 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
4138 opj_image_t *image = NULL;
4140 opj_common_ptr cinfo = j2k->cinfo;
4143 j2k->cstr_info = cstr_info;
4145 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
4147 /* create an empty image */
4148 image = opj_image_create0();
4151 j2k->state = J2K_STATE_MHSOC;
4154 opj_dec_mstabent_t *e;
4155 int id = cio_read(cio, 2);
4158 /* we try to honor JPWL correction power */
4159 if (j2k->cp->correct) {
4161 int orig_pos = cio_tell(cio);
4164 /* call the corrector */
4165 status = jpwl_correct(j2k);
4167 /* go back to where you were */
4168 cio_seek(cio, orig_pos - 2);
4170 /* re-read the marker */
4171 id = cio_read(cio, 2);
4173 /* check whether it begins with ff */
4174 if (id >> 8 != 0xff) {
4175 opj_event_msg(cinfo, EVT_ERROR,
4176 "JPWL: possible bad marker %x at %d\n",
4177 id, cio_tell(cio) - 2);
4179 opj_image_destroy(image);
4180 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
4183 /* we try to correct */
4185 cio_seek(cio, cio_tell(cio) - 2);
4186 cio_write(cio, id, 2);
4187 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
4188 "- setting marker to %x\n",
4193 #endif /* USE_JPWL */
4195 if (id >> 8 != 0xff) {
4196 opj_image_destroy(image);
4197 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
4200 e = j2k_dec_mstab_lookup(id);
4201 // Check if the marker is known
4202 if (!(j2k->state & e->states)) {
4203 opj_image_destroy(image);
4204 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
4207 // Check if the decoding is limited to the main header
4208 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
4209 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
4216 if (j2k->state & J2K_STATE_ERR)
4219 if (j2k->state == J2K_STATE_MT) {
4222 if (j2k->state == J2K_STATE_NEOC) {
4226 if (j2k->state == J2K_STATE_NEOC) {
4230 if (j2k->state != J2K_STATE_MT) {
4231 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
4237 * Read a JPT-stream and decode file
4240 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
4241 opj_image_t *image = NULL;
4242 opj_jpt_msg_header_t header;
4244 opj_common_ptr cinfo = j2k->cinfo;
4246 OPJ_ARG_NOT_USED(cstr_info);
4250 /* create an empty image */
4251 image = opj_image_create0();
4254 j2k->state = J2K_STATE_MHSOC;
4256 /* Initialize the header */
4257 jpt_init_msg_header(&header);
4258 /* Read the first header of the message */
4259 jpt_read_msg_header(cinfo, cio, &header);
4261 position = cio_tell(cio);
4262 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
4263 opj_image_destroy(image);
4264 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
4269 opj_dec_mstabent_t *e = NULL;
4272 if (!cio_numbytesleft(cio)) {
4276 /* data-bin read -> need to read a new header */
4277 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
4278 jpt_read_msg_header(cinfo, cio, &header);
4279 position = cio_tell(cio);
4280 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
4281 opj_image_destroy(image);
4282 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
4287 id = cio_read(cio, 2);
4288 if (id >> 8 != 0xff) {
4289 opj_image_destroy(image);
4290 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
4293 e = j2k_dec_mstab_lookup(id);
4294 if (!(j2k->state & e->states)) {
4295 opj_image_destroy(image);
4296 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
4302 if (j2k->state == J2K_STATE_MT) {
4305 if (j2k->state == J2K_STATE_NEOC) {
4309 if (j2k->state == J2K_STATE_NEOC) {
4313 if (j2k->state != J2K_STATE_MT) {
4314 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
4320 /* ----------------------------------------------------------------------- */
4321 /* J2K encoder interface */
4322 /* ----------------------------------------------------------------------- */
4324 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
4325 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4332 opj_j2k_v2_t* j2k_create_compress_v2()
4334 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
4339 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
4341 l_j2k->m_is_decoder = 0;
4342 l_j2k->m_cp.m_is_decoder = 0;
4344 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
4345 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4350 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
4352 // validation list creation
4353 l_j2k->m_validation_list = opj_procedure_list_create();
4354 if (! l_j2k->m_validation_list) {
4359 // execution list creation
4360 l_j2k->m_procedure_list = opj_procedure_list_create();
4361 if (! l_j2k->m_procedure_list) {
4369 void j2k_destroy_compress(opj_j2k_t *j2k) {
4373 if(j2k->cp != NULL) {
4374 opj_cp_t *cp = j2k->cp;
4377 opj_free(cp->comment);
4380 opj_free(cp->matrice);
4382 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4383 opj_free(cp->tcps[tileno].tccps);
4392 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
4393 int i, j, tileno, numpocs_tile;
4394 opj_cp_t *cp = NULL;
4396 if(!j2k || !parameters || ! image) {
4400 /* create and initialize the coding parameters structure */
4401 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4403 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
4406 /* set default values for cp */
4411 copy user encoding parameters
4413 cp->cinema = parameters->cp_cinema;
4414 cp->max_comp_size = parameters->max_comp_size;
4415 cp->rsiz = parameters->cp_rsiz;
4416 cp->disto_alloc = parameters->cp_disto_alloc;
4417 cp->fixed_alloc = parameters->cp_fixed_alloc;
4418 cp->fixed_quality = parameters->cp_fixed_quality;
4420 /* mod fixed_quality */
4421 if(parameters->cp_matrice) {
4422 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
4423 cp->matrice = (int *) opj_malloc(array_size);
4424 memcpy(cp->matrice, parameters->cp_matrice, array_size);
4428 cp->tdx = parameters->cp_tdx;
4429 cp->tdy = parameters->cp_tdy;
4432 cp->tx0 = parameters->cp_tx0;
4433 cp->ty0 = parameters->cp_ty0;
4435 /* comment string */
4436 if(parameters->cp_comment) {
4437 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
4439 strcpy(cp->comment, parameters->cp_comment);
4444 calculate other encoding parameters
4447 if (parameters->tile_size_on) {
4448 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
4449 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
4451 cp->tdx = image->x1 - cp->tx0;
4452 cp->tdy = image->y1 - cp->ty0;
4455 if(parameters->tp_on){
4456 cp->tp_flag = parameters->tp_flag;
4461 for(i=0;i<image->numcomps ;i++){
4462 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
4468 calculate JPWL encoding parameters
4471 if (parameters->jpwl_epc_on) {
4475 cp->epc_on = OPJ_TRUE;
4476 cp->info_on = OPJ_FALSE; /* no informative technique */
4479 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
4480 cp->epb_on = OPJ_TRUE;
4482 cp->hprot_MH = parameters->jpwl_hprot_MH;
4483 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
4484 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
4485 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
4487 /* if tile specs are not specified, copy MH specs */
4488 if (cp->hprot_TPH[0] == -1) {
4489 cp->hprot_TPH_tileno[0] = 0;
4490 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
4492 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
4493 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
4494 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
4495 cp->pprot[i] = parameters->jpwl_pprot[i];
4499 /* set ESD writing */
4500 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
4501 cp->esd_on = OPJ_TRUE;
4503 cp->sens_size = parameters->jpwl_sens_size;
4504 cp->sens_addr = parameters->jpwl_sens_addr;
4505 cp->sens_range = parameters->jpwl_sens_range;
4507 cp->sens_MH = parameters->jpwl_sens_MH;
4508 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
4509 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
4510 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
4514 /* always set RED writing to false: we are at the encoder */
4515 cp->red_on = OPJ_FALSE;
4518 cp->epc_on = OPJ_FALSE;
4520 #endif /* USE_JPWL */
4523 /* initialize the mutiple tiles */
4524 /* ---------------------------- */
4525 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
4527 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4528 opj_tcp_t *tcp = &cp->tcps[tileno];
4529 tcp->numlayers = parameters->tcp_numlayers;
4530 for (j = 0; j < tcp->numlayers; j++) {
4532 if (cp->fixed_quality) {
4533 tcp->distoratio[j] = parameters->tcp_distoratio[j];
4535 tcp->rates[j] = parameters->tcp_rates[j];
4537 if (cp->fixed_quality) { /* add fixed_quality */
4538 tcp->distoratio[j] = parameters->tcp_distoratio[j];
4540 tcp->rates[j] = parameters->tcp_rates[j];
4544 tcp->csty = parameters->csty;
4545 tcp->prg = parameters->prog_order;
4546 tcp->mct = parameters->tcp_mct;
4550 if (parameters->numpocs) {
4551 /* initialisation of POC */
4553 for (i = 0; i < parameters->numpocs; i++) {
4554 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
4555 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
4556 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
4557 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
4558 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
4559 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
4560 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
4561 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
4562 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
4566 tcp->numpocs = numpocs_tile -1 ;
4571 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
4573 for (i = 0; i < image->numcomps; i++) {
4574 opj_tccp_t *tccp = &tcp->tccps[i];
4575 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
4576 tccp->numresolutions = parameters->numresolution;
4577 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
4578 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
4579 tccp->cblksty = parameters->mode;
4580 tccp->qmfbid = parameters->irreversible ? 0 : 1;
4581 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
4583 if (i == parameters->roi_compno) {
4584 tccp->roishift = parameters->roi_shift;
4589 if(parameters->cp_cinema)
4591 //Precinct size for lowest frequency subband=128
4594 //Precinct size at all other resolutions = 256
4595 for (j = 1; j < tccp->numresolutions; j++) {
4600 if (parameters->csty & J2K_CCP_CSTY_PRT) {
4602 for (j = tccp->numresolutions - 1; j >= 0; j--) {
4603 if (p < parameters->res_spec) {
4605 if (parameters->prcw_init[p] < 1) {
4608 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
4611 if (parameters->prch_init[p] < 1) {
4614 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
4618 int res_spec = parameters->res_spec;
4619 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
4620 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
4622 if (size_prcw < 1) {
4625 tccp->prcw[j] = int_floorlog2(size_prcw);
4628 if (size_prch < 1) {
4631 tccp->prch[j] = int_floorlog2(size_prch);
4635 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
4638 for (j = 0; j < tccp->numresolutions; j++) {
4645 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
4650 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
4652 opj_cp_t *cp = NULL;
4654 opj_tcd_t *tcd = NULL; /* TCD component */
4662 j2k->cstr_info = cstr_info;
4665 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
4666 cstr_info->image_w = image->x1 - image->x0;
4667 cstr_info->image_h = image->y1 - image->y0;
4668 cstr_info->prog = (&cp->tcps[0])->prg;
4669 cstr_info->tw = cp->tw;
4670 cstr_info->th = cp->th;
4671 cstr_info->tile_x = cp->tdx; /* new version parser */
4672 cstr_info->tile_y = cp->tdy; /* new version parser */
4673 cstr_info->tile_Ox = cp->tx0; /* new version parser */
4674 cstr_info->tile_Oy = cp->ty0; /* new version parser */
4675 cstr_info->numcomps = image->numcomps;
4676 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
4677 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
4678 for (compno=0; compno < image->numcomps; compno++) {
4679 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
4681 cstr_info->D_max = 0.0; /* ADD Marcela */
4682 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
4683 cstr_info->maxmarknum = 100;
4684 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
4685 cstr_info->marknum = 0;
4695 for (compno = 1; compno < image->numcomps; compno++) {
4696 j2k_write_coc(j2k, compno);
4697 j2k_write_qcc(j2k, compno);
4701 for (compno = 0; compno < image->numcomps; compno++) {
4702 opj_tcp_t *tcp = &cp->tcps[0];
4703 if (tcp->tccps[compno].roishift)
4704 j2k_write_rgn(j2k, compno, 0);
4706 if (cp->comment != NULL) {
4710 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
4714 if (cp->cinema == CINEMA4K_24) {
4719 /* uncomment only for testing JPSEC marker writing */
4720 /* j2k_write_sec(j2k); */
4724 cstr_info->main_head_end = cio_tell(cio) - 1;
4727 /**** Main Header ENDS here ***/
4729 /* create the tile encoder */
4730 tcd = tcd_create(j2k->cinfo);
4732 /* encode each tile */
4733 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4737 int acc_pack_num = 0;
4741 opj_tcp_t *tcp = &cp->tcps[tileno];
4742 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
4744 j2k->curtileno = tileno;
4745 j2k->cur_tp_num = 0;
4746 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
4747 /* initialisation before tile encoding */
4749 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
4751 tcd_init_encode(tcd, image, cp, j2k->curtileno);
4756 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
4757 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
4758 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
4759 cstr_info->tile[j2k->curtileno].marknum = 0;
4763 for(pino = 0; pino <= tcp->numpocs; pino++) {
4767 /*Get number of tile parts*/
4768 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
4769 tcd->tp_pos = cp->tp_pos;
4771 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
4772 j2k->tp_num = tilepartno;
4775 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
4776 cio_tell(cio) + j2k->pos_correction;
4780 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
4781 for (compno = 1; compno < image->numcomps; compno++) {
4782 j2k_write_coc(j2k, compno);
4783 j2k_write_qcc(j2k, compno);
4785 if (cp->tcps[tileno].numpocs) {
4792 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
4793 cio_tell(cio) + j2k->pos_correction + 1;
4796 j2k_write_sod(j2k, tcd);
4800 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
4801 cio_tell(cio) + j2k->pos_correction - 1;
4802 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
4804 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
4805 cstr_info->packno - acc_pack_num;
4806 acc_pack_num = cstr_info->packno;
4814 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
4819 if (tile->PPT) { // BAD PPT !!!
4822 PPT_file=fopen("PPT","rb");
4823 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
4824 for (i=0;i<tile->len_ppt;i++) {
4826 fread(&elmt, 1, 1, PPT_file);
4827 fwrite(&elmt,1,1,f);
4836 /* destroy the tile encoder */
4837 tcd_free_encode(tcd);
4840 opj_free(j2k->cur_totnum_tp);
4845 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
4847 /* The following adjustment is done to adjust the codestream size */
4848 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
4849 /* the first bunch of bytes is not in the codestream */
4850 cstr_info->codestream_size -= cstr_info->main_head_start;
4856 preparation of JPWL marker segments
4860 /* encode according to JPWL */
4861 jpwl_encode(j2k, cio, image);
4864 #endif /* USE_JPWL */
4869 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
4874 /* expand the list? */
4875 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
4876 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
4877 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
4880 /* add the marker */
4881 cstr_info->marker[cstr_info->marknum].type = type;
4882 cstr_info->marker[cstr_info->marknum].pos = pos;
4883 cstr_info->marker[cstr_info->marknum].len = len;
4884 cstr_info->marknum++;
4888 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_UINT32 pos, OPJ_UINT32 len) {
4893 /* expand the list? */
4894 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
4895 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
4896 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
4899 /* add the marker */
4900 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
4901 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
4902 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
4903 cstr_index->marknum++;
4907 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
4910 opj_marker_info_t *marker;
4915 /* expand the list? */
4916 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
4917 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
4918 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
4921 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
4923 /* add the marker */
4924 marker->type = type;
4927 cstr_info->tile[tileno].marknum++;
4934 * -----------------------------------------------------------------------
4935 * -----------------------------------------------------------------------
4936 * -----------------------------------------------------------------------
4940 * Ends the decompression procedures and possibiliy add data to be read after the
4943 opj_bool j2k_end_decompress(
4944 opj_j2k_v2_t *p_j2k,
4945 opj_stream_private_t *p_stream,
4946 opj_event_mgr_t * p_manager)
4952 * Reads a jpeg2000 codestream header structure.
4955 * @param p_stream the stream to read data from.
4956 * @param p_j2k the jpeg2000 codec.
4957 * @param p_manager the user event manager.
4959 * @return true if the box is valid.
4961 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
4962 opj_j2k_v2_t* p_j2k,
4963 opj_image_t* p_image,
4964 struct opj_event_mgr* p_manager )
4967 assert(p_j2k != 00);
4968 assert(p_stream != 00);
4969 assert(p_image != 00);
4970 assert(p_manager != 00);
4972 /* create an empty image header */
4973 p_j2k->m_image = opj_image_create0();
4974 if (! p_j2k->m_image) {
4978 /* customization of the validation */
4979 j2k_setup_decoding_validation(p_j2k);
4981 /* validation of the parameters codec */
4982 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
4983 opj_image_destroy(p_j2k->m_image);
4984 p_j2k->m_image = NULL;
4988 /* customization of the encoding */
4989 j2k_setup_header_reading(p_j2k);
4992 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
4993 opj_image_destroy(p_j2k->m_image);
4994 p_j2k->m_image = NULL;
4998 if (! j2k_copy_img_header(p_j2k, p_image)){
4999 opj_image_destroy(p_j2k->m_image);
5000 p_j2k->m_image = NULL;
5008 opj_bool j2k_copy_img_header(opj_j2k_v2_t* p_j2k, opj_image_t* p_image)
5013 assert(p_j2k != 00);
5014 assert(p_image != 00);
5016 p_image->x0 = p_j2k->m_image->x0;
5017 p_image->y0 = p_j2k->m_image->y0;
5018 p_image->x1 = p_j2k->m_image->x1;
5019 p_image->y1 = p_j2k->m_image->y1;
5021 p_image->numcomps = p_j2k->m_image->numcomps;
5022 p_image->comps = (opj_image_comp_t*)opj_malloc(p_image->numcomps * sizeof(opj_image_comp_t));
5023 if (!p_image->comps)
5025 for (compno=0; compno < p_image->numcomps; compno++){
5026 memcpy( &(p_image->comps[compno]),
5027 &(p_j2k->m_image->comps[compno]),
5028 sizeof(opj_image_comp_t));
5031 p_image->color_space = p_j2k->m_image->color_space;
5032 p_image->icc_profile_len = p_j2k->m_image->icc_profile_len;
5033 if (!p_image->icc_profile_len) {
5035 p_image->icc_profile_buf = (unsigned char*)opj_malloc(p_image->icc_profile_len);
5036 if (!p_image->icc_profile_buf)
5038 memcpy( p_image->icc_profile_buf,
5039 p_j2k->m_image->icc_profile_buf,
5040 p_j2k->m_image->icc_profile_len);
5043 p_image->icc_profile_buf = NULL;
5051 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
5053 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
5056 assert(p_j2k != 00);
5058 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_read_header_procedure);
5060 /* DEVELOPER CORNER, add your custom procedures */
5061 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_copy_default_tcp_and_create_tcd);
5066 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
5067 * are valid. Developpers wanting to extend the library can add their own validation procedures.
5069 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
5072 assert(p_j2k != 00);
5074 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_decoder);
5075 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_decoding_validation);
5076 /* DEVELOPER CORNER, add your custom validation procedure */
5081 * Builds the cp decoder parameters to use to decode tile.
5083 opj_bool j2k_build_decoder (
5084 opj_j2k_v2_t * p_j2k,
5085 opj_stream_private_t *p_stream,
5086 opj_event_mgr_t * p_manager
5089 // add here initialization of cp
5090 // copy paste of setup_decoder
5095 * The default decoding validation procedure without any extension.
5097 * @param p_j2k the jpeg2000 codec to validate.
5098 * @param p_stream the input stream to validate.
5099 * @param p_manager the user event manager.
5101 * @return true if the parameters are correct.
5103 opj_bool j2k_decoding_validation (
5104 opj_j2k_v2_t *p_j2k,
5105 opj_stream_private_t *p_stream,
5106 opj_event_mgr_t * p_manager
5109 opj_bool l_is_valid = OPJ_TRUE;
5112 assert(p_j2k != 00);
5113 assert(p_stream != 00);
5114 assert(p_manager != 00);
5117 /* STATE checking */
5118 /* make sure the state is at 0 */
5120 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
5122 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
5124 /* POINTER validation */
5125 /* make sure a p_j2k codec is present */
5126 /* make sure a procedure list is present */
5127 l_is_valid &= (p_j2k->m_procedure_list != 00);
5128 /* make sure a validation list is present */
5129 l_is_valid &= (p_j2k->m_validation_list != 00);
5131 /* PARAMETER VALIDATION */
5135 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
5136 struct opj_stream_private *p_stream,
5137 struct opj_event_mgr * p_manager)
5139 OPJ_UINT32 l_current_marker;
5140 OPJ_UINT32 l_marker_size;
5141 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
5144 assert(p_stream != 00);
5145 assert(p_j2k != 00);
5146 assert(p_manager != 00);
5148 /* We enter in the main header */
5149 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
5151 /* Try to read the SOC marker, the codestream must begin with SOC marker */
5152 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
5153 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
5157 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
5158 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5159 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5163 /* Read 2 bytes as the new marker ID */
5164 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5166 /* Try to read until the SOT is detected */
5167 while (l_current_marker != J2K_MS_SOT) {
5169 /* Check if the current marker ID is valid */
5170 if (l_current_marker < 0xff00) {
5171 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
5175 /* Get the marker handler from the marker ID */
5176 l_marker_handler = j2k_get_marker_handler(l_current_marker);
5178 /* Manage case where marker is unknown */
5179 if (l_marker_handler->id == J2K_MS_UNK) {
5180 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
5181 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
5185 if (l_current_marker == J2K_MS_SOT)
5186 break; /* SOT marker is detected main header is completely read */
5187 else /* Get the marker handler from the marker ID */
5188 l_marker_handler = j2k_get_marker_handler(l_current_marker);
5191 /* Check if the marker is known and if it is the right place to find it */
5192 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5193 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5197 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
5198 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5199 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5203 /* read 2 bytes as the marker size */
5204 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
5205 l_marker_size -= 2; // Subtract the size of the marker ID already read
5207 /* Check if the marker size is compatible with the header data size */
5208 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
5209 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
5210 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
5211 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
5214 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
5217 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
5218 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) {
5219 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5223 /* Read the marker segment with the correct marker handler */
5224 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
5225 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
5229 /* Add the marker to the codestream index*/
5230 j2k_add_mhmarker_v2(p_j2k->cstr_index,
5231 l_marker_handler->id,
5232 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
5233 l_marker_size + 4 );
5235 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
5236 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5237 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5241 /* read 2 bytes as the new marker ID */
5242 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5245 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decode.\n");
5247 /* Position of the last element if the main header */
5248 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
5250 /* Next step: read a tile-part header */
5251 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5257 * Excutes the given procedures on the given codec.
5259 * @param p_procedure_list the list of procedures to execute
5260 * @param p_j2k the jpeg2000 codec to execute the procedures on.
5261 * @param p_stream the stream to execute the procedures on.
5262 * @param p_manager the user manager.
5264 * @return true if all the procedures were successfully executed.
5266 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
5267 opj_procedure_list_t * p_procedure_list,
5268 opj_stream_private_t *p_stream,
5269 opj_event_mgr_t * p_manager )
5271 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
5272 opj_bool l_result = OPJ_TRUE;
5273 OPJ_UINT32 l_nb_proc, i;
5276 assert(p_procedure_list != 00);
5277 assert(p_j2k != 00);
5278 assert(p_stream != 00);
5279 assert(p_manager != 00);
5282 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
5283 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);
5285 for (i=0;i<l_nb_proc;++i) {
5286 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
5290 // and clear the procedure list at the end.
5291 opj_procedure_list_clear(p_procedure_list);
5296 opj_bool j2k_copy_default_tcp_and_create_tcd
5298 opj_j2k_v2_t * p_j2k,
5299 opj_stream_private_t *p_stream,
5300 opj_event_mgr_t * p_manager
5303 opj_tcp_v2_t * l_tcp = 00;
5304 opj_tcp_v2_t * l_default_tcp = 00;
5305 OPJ_UINT32 l_nb_tiles;
5307 opj_tccp_t *l_current_tccp = 00;
5308 OPJ_UINT32 l_tccp_size;
5309 OPJ_UINT32 l_mct_size;
5310 opj_image_t * l_image;
5311 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
5312 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
5313 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
5314 OPJ_UINT32 l_offset;
5317 assert(p_j2k != 00);
5318 assert(p_stream != 00);
5319 assert(p_manager != 00);
5321 l_image = p_j2k->m_image;
5322 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5323 l_tcp = p_j2k->m_cp.tcps;
5324 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
5325 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
5326 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
5329 for (i=0; i<l_nb_tiles; ++i) {
5330 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
5331 l_current_tccp = l_tcp->tccps;
5332 /*Copy default coding parameters into the current tile coding parameters*/
5333 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
5334 /* Initialize some values of the current tile coding parameters*/
5336 l_tcp->ppt_data = 00;
5337 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
5338 l_tcp->tccps = l_current_tccp;
5340 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
5341 if (l_default_tcp->m_mct_decoding_matrix) {
5342 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5343 if (! l_tcp->m_mct_decoding_matrix ) {
5346 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
5349 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
5350 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
5351 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
5352 if (! l_tcp->m_mct_records) {
5355 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
5357 /* Copy the mct record data from dflt_tile_cp to the current tile*/
5358 l_src_mct_rec = l_default_tcp->m_mct_records;
5359 l_dest_mct_rec = l_tcp->m_mct_records;
5361 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
5363 if (l_src_mct_rec->m_data) {
5365 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
5366 if(! l_dest_mct_rec->m_data) {
5369 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
5376 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
5377 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
5378 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
5379 if (! l_tcp->m_mcc_records) {
5382 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
5384 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
5385 l_src_mcc_rec = l_default_tcp->m_mcc_records;
5386 l_dest_mcc_rec = l_tcp->m_mcc_records;
5388 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
5390 if (l_src_mcc_rec->m_decorrelation_array) {
5391 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
5392 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
5395 if (l_src_mcc_rec->m_offset_array) {
5396 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
5397 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
5404 /* Copy all the dflt_tile_compo_cp to the current tile cp */
5405 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
5407 /* Move to next tile cp*/
5411 /* Create the current tile decoder*/
5412 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); // FIXME why a cast ?
5413 if (! p_j2k->m_tcd ) {
5417 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
5418 tcd_destroy_v2(p_j2k->m_tcd);
5420 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5428 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
5429 * with the marker value.
5430 * @param p_id Marker value to look up
5432 * @return the handler associated with the id.
5434 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (const OPJ_UINT32 p_id)
5436 const opj_dec_memory_marker_handler_t *e;
5437 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
5438 if (e->id == p_id) {
5439 break; // we find a handler corresponding to the marker ID
5447 * Destroys a jpeg2000 codec.
5449 * @param p_j2k the jpeg20000 structure to destroy.
5451 void j2k_destroy (opj_j2k_v2_t *p_j2k)
5457 if (p_j2k->m_is_decoder) {
5459 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
5460 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
5461 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
5462 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
5465 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
5466 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
5467 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
5468 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
5473 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
5474 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
5475 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
5478 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5479 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
5480 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
5481 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
5484 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5485 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5486 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
5487 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5491 tcd_destroy_v2(p_j2k->m_tcd);
5493 j2k_cp_destroy(&(p_j2k->m_cp));
5494 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t));
5496 opj_procedure_list_destroy(p_j2k->m_procedure_list);
5497 p_j2k->m_procedure_list = 00;
5499 opj_procedure_list_destroy(p_j2k->m_validation_list);
5500 p_j2k->m_procedure_list = 00;
5502 j2k_destroy_cstr_index(p_j2k->cstr_index);
5503 p_j2k->cstr_index = NULL;
5508 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
5514 if (p_cstr_ind->marker) {
5515 opj_free(p_cstr_ind->marker);
5516 p_cstr_ind->marker = NULL;
5519 if (p_cstr_ind->tile_index) {
5527 * Destroys a tile coding parameter structure.
5529 * @param p_tcp the tile coding parameter to destroy.
5531 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
5537 if (p_tcp->ppt_buffer != 00) {
5538 opj_free(p_tcp->ppt_buffer);
5539 p_tcp->ppt_buffer = 00;
5542 if (p_tcp->tccps != 00) {
5543 opj_free(p_tcp->tccps);
5547 if (p_tcp->m_mct_coding_matrix != 00) {
5548 opj_free(p_tcp->m_mct_coding_matrix);
5549 p_tcp->m_mct_coding_matrix = 00;
5552 if (p_tcp->m_mct_decoding_matrix != 00) {
5553 opj_free(p_tcp->m_mct_decoding_matrix);
5554 p_tcp->m_mct_decoding_matrix = 00;
5557 if (p_tcp->m_mcc_records) {
5558 opj_free(p_tcp->m_mcc_records);
5559 p_tcp->m_mcc_records = 00;
5560 p_tcp->m_nb_max_mcc_records = 0;
5561 p_tcp->m_nb_mcc_records = 0;
5564 if (p_tcp->m_mct_records) {
5565 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
5568 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
5569 if (l_mct_data->m_data) {
5570 opj_free(l_mct_data->m_data);
5571 l_mct_data->m_data = 00;
5577 opj_free(p_tcp->m_mct_records);
5578 p_tcp->m_mct_records = 00;
5581 if (p_tcp->mct_norms != 00) {
5582 opj_free(p_tcp->mct_norms);
5583 p_tcp->mct_norms = 00;
5586 if (p_tcp->m_data) {
5587 opj_free(p_tcp->m_data);
5594 * Destroys a coding parameter structure.
5596 * @param p_cp the coding parameter to destroy.
5598 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
5600 OPJ_UINT32 l_nb_tiles;
5601 opj_tcp_v2_t * l_current_tile = 00;
5612 l_current_tile = p_cp->tcps;
5613 l_nb_tiles = p_cp->th * p_cp->tw;
5616 (i = 0; i < l_nb_tiles; ++i)
5618 j2k_tcp_destroy(l_current_tile);
5621 opj_free(p_cp->tcps);
5625 (p_cp->ppm_buffer != 00)
5627 opj_free(p_cp->ppm_buffer);
5628 p_cp->ppm_buffer = 00;
5631 (p_cp->comment != 00)
5633 opj_free(p_cp->comment);
5637 (! p_cp->m_is_decoder)
5640 (p_cp->m_specific_param.m_enc.m_matrice)
5642 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
5643 p_cp->m_specific_param.m_enc.m_matrice = 00;
5651 * Reads a tile header.
5652 * @param p_j2k the jpeg2000 codec.
5653 * @param p_stream the stream to write data to.
5654 * @param p_manager the user event manager.
5656 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
5657 OPJ_UINT32 * p_tile_index,
5658 OPJ_UINT32 * p_data_size,
5659 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
5660 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
5661 OPJ_UINT32 * p_nb_comps,
5663 opj_stream_private_t *p_stream,
5664 opj_event_mgr_t * p_manager )
5666 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
5667 OPJ_UINT32 l_marker_size;
5668 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
5669 opj_tcp_v2_t * l_tcp = NULL;
5670 OPJ_UINT32 l_nb_tiles;
5673 assert(p_stream != 00);
5674 assert(p_j2k != 00);
5675 assert(p_manager != 00);
5677 /* Reach the End Of Codestream ?*/
5678 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
5679 l_current_marker = J2K_MS_EOC;
5681 /* We need to encounter a SOT marker (a new tile-part header) */
5682 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
5686 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
5687 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
5689 /* Try to read until the Start Of Data is detected */
5690 while (l_current_marker != J2K_MS_SOD) {
5692 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
5693 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5694 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5698 /* Read 2 bytes from the buffer as the marker size */
5699 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
5701 /* Why this condition? FIXME */
5702 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
5703 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
5705 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
5707 /* Get the marker handler from the marker ID */
5708 l_marker_handler = j2k_get_marker_handler(l_current_marker);
5710 /* Check if the marker is known and if it is the right place to find it */
5711 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5712 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5715 /* FIXME manage case of unknown marker as in the main header ? */
5717 /* Check if the marker size is compatible with the header data size */
5718 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
5719 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
5720 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
5721 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
5724 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
5727 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
5728 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) {
5729 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5733 /* Read the marker segment with the correct marker handler */
5734 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
5735 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
5739 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
5740 /* Skip the rest of the tile part header*/
5741 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) {
5742 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5745 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
5748 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5749 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5750 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5753 /* Read 2 bytes from the buffer as the new marker ID */
5754 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5758 /* If we didn't skip data before, we need to read the SOD marker*/
5759 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
5760 /* Try to read the SOD marker and skip data ? FIXME */
5761 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
5765 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
5766 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
5767 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5768 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5772 /* Read 2 bytes from buffer as the new marker ID */
5773 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5777 /* Indicate we will try to read a new tile-part header*/
5778 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
5779 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
5780 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5782 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
5783 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5784 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5788 /* Read 2 bytes from buffer as the new marker ID */
5789 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5793 /* Current marker is the EOC marker ?*/
5794 if (l_current_marker == J2K_MS_EOC) {
5795 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
5796 p_j2k->m_current_tile_number = 0;
5797 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
5802 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
5803 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
5804 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5806 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
5807 ++p_j2k->m_current_tile_number;
5811 if (p_j2k->m_current_tile_number == l_nb_tiles) {
5812 *p_go_on = OPJ_FALSE;
5818 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
5819 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5823 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
5824 p_j2k->m_current_tile_number +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
5826 *p_tile_index = p_j2k->m_current_tile_number;
5827 *p_go_on = OPJ_TRUE;
5828 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
5829 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
5830 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
5831 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
5832 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
5833 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
5835 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;// FIXME J2K_DEC_STATE_DATA;
5841 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
5842 OPJ_UINT32 p_tile_index,
5844 OPJ_UINT32 p_data_size,
5845 opj_stream_private_t *p_stream,
5846 opj_event_mgr_t * p_manager )
5848 OPJ_UINT32 l_current_marker;
5849 OPJ_BYTE l_data [2];
5850 opj_tcp_v2_t * l_tcp;
5853 assert(p_stream != 00);
5854 assert(p_j2k != 00);
5855 assert(p_manager != 00);
5857 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
5858 || (p_tile_index != p_j2k->m_current_tile_number) ) {
5862 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
5863 if (! l_tcp->m_data) {
5864 j2k_tcp_destroy(&(p_j2k->m_cp.tcps[p_tile_index]));
5868 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
5872 p_j2k->cstr_index) ) {
5873 j2k_tcp_destroy(l_tcp);
5874 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;//FIXME J2K_DEC_STATE_ERR;
5878 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
5882 j2k_tcp_destroy(l_tcp);
5883 p_j2k->m_tcd->tcp = 0;
5885 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
5886 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));// FIXME J2K_DEC_STATE_DATA);
5888 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ //FIXME J2K_DEC_STATE_EOC)
5889 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
5890 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5894 opj_read_bytes(l_data,&l_current_marker,2);
5896 if (l_current_marker == J2K_MS_EOC) {
5897 p_j2k->m_current_tile_number = 0;
5898 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;//FIXME J2K_DEC_STATE_EOC;
5900 else if (l_current_marker != J2K_MS_SOT)
5902 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
5911 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
5913 OPJ_UINT32 i,j,k = 0;
5914 OPJ_UINT32 l_width,l_height,l_offset_x,l_offset_y;
5915 opj_image_comp_t * l_img_comp = 00;
5916 opj_tcd_tilecomp_v2_t * l_tilec = 00;
5917 opj_image_t * l_image = 00;
5918 OPJ_UINT32 l_size_comp, l_remaining;
5919 OPJ_UINT32 l_dest_stride;
5920 OPJ_INT32 * l_dest_ptr;
5921 opj_tcd_resolution_v2_t* l_res= 00;
5924 l_tilec = p_tcd->tcd_image->tiles->comps;
5925 l_image = p_tcd->image;
5926 l_img_comp = l_image->comps;
5928 for (i=0;i<p_tcd->image->numcomps;++i) {
5930 if (!l_img_comp->data) {
5932 l_img_comp->data = (OPJ_INT32*) opj_malloc(l_img_comp->w * l_img_comp->h * sizeof(OPJ_INT32));
5933 if (! l_img_comp->data) {
5936 memset(l_img_comp->data,0,l_img_comp->w * l_img_comp->h * sizeof(OPJ_INT32));
5939 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
5940 l_remaining = l_img_comp->prec & 7; /* (%8) */
5941 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
5947 if (l_size_comp == 3) {
5951 l_width = (l_res->x1 - l_res->x0);
5952 l_height = (l_res->y1 - l_res->y0);
5954 l_dest_stride = (l_img_comp->w) - l_width;
5956 l_offset_x = int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
5957 l_offset_y = int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
5958 l_dest_ptr = l_img_comp->data + (l_res->x0 - l_offset_x) + (l_res->y0 - l_offset_y) * l_img_comp->w;
5960 switch (l_size_comp) {
5963 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
5964 if (l_img_comp->sgnd) {
5965 for (j=0;j<l_height;++j) {
5966 for (k=0;k<l_width;++k) {
5967 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
5970 l_dest_ptr += l_dest_stride;
5974 for (j=0;j<l_height;++j) {
5975 for (k=0;k<l_width;++k) {
5976 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
5979 l_dest_ptr += l_dest_stride;
5983 p_data = (OPJ_BYTE*) l_src_ptr;
5988 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
5990 if (l_img_comp->sgnd) {
5991 for (j=0;j<l_height;++j) {
5992 for (k=0;k<l_width;++k) {
5993 *(l_dest_ptr++) = *(l_src_ptr++);
5996 l_dest_ptr += l_dest_stride;
6000 for (j=0;j<l_height;++j) {
6001 for (k=0;k<l_width;++k) {
6002 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
6005 l_dest_ptr += l_dest_stride;
6009 p_data = (OPJ_BYTE*) l_src_ptr;
6014 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
6015 for (j=0;j<l_height;++j) {
6016 for (k=0;k<l_width;++k) {
6017 *(l_dest_ptr++) = (*(l_src_ptr++));
6020 l_dest_ptr += l_dest_stride;
6023 p_data = (OPJ_BYTE*) l_src_ptr;
6035 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
6037 * @param p_j2k the jpeg2000 codec.
6038 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
6039 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
6040 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
6041 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
6042 * @param p_manager the user event manager
6044 * @return true if the area could be set.
6046 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
6047 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
6048 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
6049 struct opj_event_mgr * p_manager )
6051 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
6053 /* Check if we are read the main header */
6054 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { // FIXME J2K_DEC_STATE_TPHSOT)
6055 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
6060 /* Check if the positions provided by the user are correct */
6063 if (p_start_x > l_cp->tx0 + l_cp->tw * l_cp->tdx) {
6064 opj_event_msg_v2(p_manager, EVT_ERROR,
6065 "Left position of the decoded area (ROI_x0=%d) is outside the tile area (XTOsiz + nb_tw x XTsiz=%d).\n",
6066 p_start_x, l_cp->tx0 + l_cp->tw * l_cp->tdx);
6069 else if (p_start_x < l_cp->tx0){
6070 opj_event_msg_v2(p_manager, EVT_WARNING,
6071 "Left position of the decoded area (ROI_x0=%d) is outside the tile area (XTOsiz=%d).\n",
6072 p_start_x, l_cp->tx0);
6073 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
6076 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
6079 if (p_start_y > l_cp->ty0 + l_cp->th * l_cp->tdy){
6080 opj_event_msg_v2(p_manager, EVT_ERROR,
6081 "Up position of the decoded area (ROI_y0=%d) is outside the tile area (YTOsiz + nb_th x YTsiz=%d).\n",
6082 p_start_y, l_cp->ty0 + l_cp->th * l_cp->tdy);
6085 else if (p_start_y < l_cp->ty0){
6086 opj_event_msg_v2(p_manager, EVT_WARNING,
6087 "Up position of the decoded area (ROI_y0=%d) is outside the tile area (YTOsiz=%d).\n",
6088 p_start_y, l_cp->ty0);
6089 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
6092 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
6095 if (p_end_x < l_cp->tx0) {
6096 opj_event_msg_v2(p_manager, EVT_ERROR,
6097 "Right position of the decoded area (ROI_x1=%d) is outside the tile area (XTOsiz=%d).\n",
6098 p_end_x, l_cp->tx0);
6101 else if (p_end_x > l_cp->tx0 + l_cp->tw * l_cp->tdx) {
6102 opj_event_msg_v2(p_manager, EVT_WARNING,
6103 "Right position of the decoded area (ROI_x1=%d) is outside the tile area (XTOsiz + nb_tw x XTsiz=%d).\n",
6104 p_end_x, l_cp->tx0 + l_cp->tw * l_cp->tdx);
6105 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; // FIXME (-1) ???
6108 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
6111 if (p_end_y < l_cp->ty0) {
6112 opj_event_msg_v2(p_manager, EVT_ERROR,
6113 "Right position of the decoded area (ROI_y1=%d) is outside the tile area (YTOsiz=%d).\n",
6114 p_end_y, l_cp->ty0);
6117 if (p_end_y > l_cp->ty0 + l_cp->th * l_cp->tdy){
6118 opj_event_msg_v2(p_manager, EVT_WARNING,
6119 "Bottom position of the decoded area (ROI_y1=%d) is outside the tile area (YTOsiz + nb_th x YTsiz=%d).\n",
6120 p_end_y, l_cp->ty0 + l_cp->th * l_cp->tdy);
6121 p_j2k->m_specific_param.m_decoder.m_start_tile_y = l_cp->th; // FIXME (-1) ???
6124 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
6127 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
6133 /* ----------------------------------------------------------------------- */
6134 /* J2K / JPT decoder interface */
6135 /* ----------------------------------------------------------------------- */
6137 * Creates a J2K decompression structure.
6139 * @return a handle to a J2K decompressor if successful, NULL otherwise.
6141 opj_j2k_v2_t* j2k_create_decompress_v2()
6143 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6147 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6149 l_j2k->m_is_decoder = 1;
6150 l_j2k->m_cp.m_is_decoder = 1;
6152 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
6153 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
6157 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
6159 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6160 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
6165 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
6167 /* codestream index creation */
6168 l_j2k->cstr_index = j2k_create_cstr_index();
6170 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
6171 if (!l_j2k->cstr_index){
6176 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
6179 /* validation list creation */
6180 l_j2k->m_validation_list = opj_procedure_list_create();
6181 if (! l_j2k->m_validation_list) {
6186 /* execution list creation */
6187 l_j2k->m_procedure_list = opj_procedure_list_create();
6188 if (! l_j2k->m_procedure_list) {
6197 opj_codestream_index_t* j2k_create_cstr_index(void)
6199 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
6200 opj_calloc(1,sizeof(opj_codestream_index_t));
6204 cstr_index->maxmarknum = 100;
6205 cstr_index->marknum = 0;
6206 cstr_index->marker = (opj_marker_info_t*)
6207 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
6208 if (!cstr_index-> marker)
6211 cstr_index->tile_index = NULL;
6217 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
6218 * @param p_header_data the data contained in the COM box.
6219 * @param p_j2k the jpeg2000 codec.
6220 * @param p_header_size the size of the data contained in the COM marker.
6221 * @param p_manager the user event manager.
6223 opj_bool j2k_read_SPCod_SPCoc(
6224 opj_j2k_v2_t *p_j2k,
6226 OPJ_BYTE * p_header_data,
6227 OPJ_UINT32 * p_header_size,
6228 struct opj_event_mgr * p_manager
6231 OPJ_UINT32 i, l_tmp;
6232 opj_cp_v2_t *l_cp = NULL;
6233 opj_tcp_v2_t *l_tcp = NULL;
6234 opj_tccp_t *l_tccp = NULL;
6235 OPJ_BYTE * l_current_ptr = NULL;
6238 assert(p_j2k != 00);
6239 assert(p_manager != 00);
6240 assert(p_header_data != 00);
6242 l_cp = &(p_j2k->m_cp);
6243 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
6244 &l_cp->tcps[p_j2k->m_current_tile_number] :
6245 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6247 /* precondition again */
6248 assert(compno < p_j2k->m_image->numcomps);
6250 l_tccp = &l_tcp->tccps[compno];
6251 l_current_ptr = p_header_data;
6253 /* make sure room is sufficient */
6254 if (*p_header_size < 5) {
6255 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
6259 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
6260 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
6263 /* If user wants to remove more resolutions than the codestream contains, return error */
6264 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
6265 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
6266 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
6267 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;// FIXME J2K_DEC_STATE_ERR;
6271 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
6275 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
6279 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
6282 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
6285 *p_header_size = *p_header_size - 5;
6287 /* use custom precinct size ? */
6288 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
6289 if (*p_header_size < l_tccp->numresolutions) {
6290 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
6294 for (i = 0; i < l_tccp->numresolutions; ++i) {
6295 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
6297 l_tccp->prcw[i] = l_tmp & 0xf;
6298 l_tccp->prch[i] = l_tmp >> 4;
6301 *p_header_size = *p_header_size - l_tccp->numresolutions;
6304 /* set default size for the precinct width and height */
6305 for (i = 0; i < l_tccp->numresolutions; ++i) {
6306 l_tccp->prcw[i] = 15;
6307 l_tccp->prch[i] = 15;
6311 #ifdef WIP_REMOVE_MSD
6313 if (p_j2k->cstr_info && compno == 0) {
6314 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
6316 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
6317 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
6318 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
6319 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
6320 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
6323 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
6324 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
6333 * Copies the tile component parameters of all the component from the first tile component.
6335 * @param p_j2k the J2k codec.
6337 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
6341 opj_cp_v2_t *l_cp = NULL;
6342 opj_tcp_v2_t *l_tcp = NULL;
6343 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
6344 OPJ_UINT32 l_prc_size;
6347 assert(p_j2k != 00);
6349 l_cp = &(p_j2k->m_cp);
6350 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
6351 &l_cp->tcps[p_j2k->m_current_tile_number] :
6352 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6354 l_ref_tccp = &l_tcp->tccps[0];
6355 l_copied_tccp = l_ref_tccp + 1;
6356 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
6358 for (i=1; i<p_j2k->m_image->numcomps; ++i) {
6359 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
6360 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
6361 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
6362 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
6363 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
6364 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
6365 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
6371 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
6373 * @param p_comp_no the component being targeted.
6374 * @param p_header_data the data contained in the COM box.
6375 * @param p_j2k the jpeg2000 codec.
6376 * @param p_header_size the size of the data contained in the COM marker.
6377 * @param p_manager the user event manager.
6379 opj_bool j2k_read_SQcd_SQcc(
6380 opj_j2k_v2_t *p_j2k,
6381 OPJ_UINT32 p_comp_no,
6382 OPJ_BYTE* p_header_data,
6383 OPJ_UINT32 * p_header_size,
6384 struct opj_event_mgr * p_manager
6388 OPJ_UINT32 l_band_no;
6389 opj_cp_v2_t *l_cp = 00;
6390 opj_tcp_v2_t *l_tcp = 00;
6391 opj_tccp_t *l_tccp = 00;
6392 OPJ_BYTE * l_current_ptr = 00;
6393 OPJ_UINT32 l_tmp, l_num_band;
6396 assert(p_j2k != 00);
6397 assert(p_manager != 00);
6398 assert(p_header_data != 00);
6400 l_cp = &(p_j2k->m_cp);
6401 // come from tile part header or main header ?
6402 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
6403 &l_cp->tcps[p_j2k->m_current_tile_number] :
6404 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6406 // precondition again
6407 assert(p_comp_no < p_j2k->m_image->numcomps);
6409 l_tccp = &l_tcp->tccps[p_comp_no];
6410 l_current_ptr = p_header_data;
6412 if (*p_header_size < 1) {
6413 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
6416 *p_header_size -= 1;
6418 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
6421 l_tccp->qntsty = l_tmp & 0x1f;
6422 l_tccp->numgbits = l_tmp >> 5;
6423 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
6427 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
6429 (*p_header_size) / 2;
6431 if( l_num_band > J2K_MAXBANDS ) {
6432 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
6433 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limiting the number of elements stored to "
6434 "J2K_MAXBANDS (%d) and skip the other. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
6440 if (l_cp->correct) {
6442 /* if JPWL is on, we check whether there are too many subbands */
6443 if ((l_num_band < 0) || (l_num_band >= J2K_MAXBANDS)) {
6444 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
6445 "JPWL: bad number of subbands in Sqcx (%d)\n",
6448 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
6451 /* we try to correct */
6453 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
6454 "- setting number of bands to %d => HYPOTHESIS!!!\n",
6459 #endif /* USE_JPWL */
6461 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
6462 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
6463 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
6465 if (l_band_no < J2K_MAXBANDS){
6466 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
6467 l_tccp->stepsizes[l_band_no].mant = 0;
6470 *p_header_size = *p_header_size - l_num_band;
6473 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
6474 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
6476 if (l_band_no < J2K_MAXBANDS){
6477 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
6478 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
6481 *p_header_size = *p_header_size - 2*l_num_band;
6484 /* Add Antonin : if scalar_derived -> compute other stepsizes */
6485 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
6486 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
6487 l_tccp->stepsizes[l_band_no].expn =
6488 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
6489 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
6490 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
6498 * Copies the tile component parameters of all the component from the first tile component.
6500 * @param p_j2k the J2k codec.
6502 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
6505 opj_cp_v2_t *l_cp = NULL;
6506 opj_tcp_v2_t *l_tcp = NULL;
6507 opj_tccp_t *l_ref_tccp = NULL;
6508 opj_tccp_t *l_copied_tccp = NULL;
6512 assert(p_j2k != 00);
6514 l_cp = &(p_j2k->m_cp);
6515 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6516 &l_cp->tcps[p_j2k->m_current_tile_number] :
6517 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6519 l_ref_tccp = &l_tcp->tccps[0];
6520 l_copied_tccp = l_ref_tccp + 1;
6521 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
6523 for (i=1;i<p_j2k->m_image->numcomps;++i) {
6524 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
6525 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
6526 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
6532 * Dump some elements from the J2K decompression structure .
6534 *@param p_j2k the jpeg2000 codec.
6535 *@param flag flag to describe what elments are dump.
6536 *@param out_stream output stream where dump the elements.
6539 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
6541 /* Check if the flag is compatible with j2k file*/
6542 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
6543 fprintf(out_stream, "Wrong flag\n");
6547 /* Dump the image_header */
6548 if (flag & OPJ_IMG_INFO){
6550 j2k_dump_image_header(p_j2k->m_image, 0, out_stream);
6553 /* Dump the codestream info from main header */
6554 if (flag & OPJ_J2K_MH_INFO){
6555 j2k_dump_MH_info(p_j2k, out_stream);
6559 /* Dump the codestream info of the current tile */
6560 if (flag & OPJ_J2K_TH_INFO){
6564 /* Dump the codestream index from main header */
6565 if (flag & OPJ_J2K_MH_IND){
6566 j2k_dump_MH_index(p_j2k, out_stream);
6569 /* Dump the codestream index of the current tile */
6570 if (flag & OPJ_J2K_TH_IND){
6577 * Dump index elements of the codestream extract from the main header.
6579 *@param p_j2k the jpeg2000 codec.
6580 *@param out_stream output stream where dump the elements.
6583 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
6585 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
6586 OPJ_UINT32 it_marker;
6588 fprintf(out_stream, "Codestream index from main header: {\n");
6590 fprintf(out_stream, "\t Main header start position=%d\n\t Main header end position=%d\n",
6591 cstr_index->main_head_start, cstr_index->main_head_end);
6593 fprintf(out_stream, "\t Marker list: {\n");
6595 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
6596 fprintf(out_stream, "\t\t type=%#x, pos=%d, len=%d\n",
6597 cstr_index->marker[it_marker].type,
6598 cstr_index->marker[it_marker].pos,
6599 cstr_index->marker[it_marker].len );
6602 fprintf(out_stream, "\t }\n}\n");
6607 * Dump info elements of the codestream extract from the main header.
6609 *@param p_j2k the jpeg2000 codec.
6610 *@param out_stream output stream where dump the elements.
6613 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
6615 opj_tcp_v2_t * l_default_tile=NULL;
6617 fprintf(out_stream, "Codestream info from main header: {\n");
6619 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
6620 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
6621 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
6623 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
6627 OPJ_INT32 numcomps = p_j2k->m_image->numcomps;
6629 fprintf(out_stream, "\t default tile {\n");
6630 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
6631 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
6632 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
6633 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
6635 for (compno = 0; compno < numcomps; compno++) {
6636 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
6637 OPJ_INT32 resno, bandno, numbands;
6640 fprintf(out_stream, "\t\t comp %d {\n", compno);
6641 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
6642 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
6643 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
6644 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
6645 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
6646 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
6648 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
6649 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
6650 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
6652 fprintf(out_stream, "\n");
6654 /* quantization style*/
6655 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
6656 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
6657 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
6658 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
6659 for (bandno = 0; bandno < numbands; bandno++) {
6660 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
6661 l_tccp->stepsizes[bandno].expn);
6663 fprintf(out_stream, "\n");
6666 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
6668 fprintf(out_stream, "\t\t }\n");
6669 } /*end of component of default tile*/
6670 fprintf(out_stream, "\t }\n"); /*end of default tile*/
6674 fprintf(out_stream, "}\n");
6679 * Dump an image header structure.
6681 *@param img_header the image header to dump.
6682 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
6683 *@param out_stream output stream where dump the elements.
6685 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
6690 fprintf(stdout, "[DEV] Dump a image_header struct {\n");
6694 fprintf(out_stream, "Image info {\n");
6695 tab[0] = '\t';tab[1] = '\0';
6698 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
6699 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
6700 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
6702 if (img_header->comps){
6704 for (compno = 0; compno < img_header->numcomps; compno++) {
6705 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
6706 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
6707 fprintf(out_stream,"%s}\n",tab);
6711 fprintf(out_stream, "}\n");
6715 * Dump a component image header structure.
6717 *@param comp_header the component image header to dump.
6718 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
6719 *@param out_stream output stream where dump the elements.
6721 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
6726 fprintf(stdout, "[DEV] Dump a image_comp_header struct {\n");
6729 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
6732 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
6733 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
6734 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
6737 fprintf(out_stream, "}\n");
6742 * Get the codestream info from a JPEG2000 codec.
6744 *@param p_j2k the component image header to dump.
6746 *@return the codestream information extract from the jpg2000 codec
6748 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
6751 OPJ_UINT16 numcomps = p_j2k->m_image->numcomps;
6752 opj_tcp_v2_t *l_default_tile;
6753 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
6755 cstr_info->nbcomps = p_j2k->m_image->numcomps;
6757 cstr_info->tx0 = p_j2k->m_cp.tx0;
6758 cstr_info->ty0 = p_j2k->m_cp.ty0;
6759 cstr_info->tdx = p_j2k->m_cp.tdx;
6760 cstr_info->tdy = p_j2k->m_cp.tdy;
6761 cstr_info->tw = p_j2k->m_cp.tw;
6762 cstr_info->th = p_j2k->m_cp.th;
6764 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
6766 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
6767 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
6768 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
6769 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
6771 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
6773 for (compno = 0; compno < numcomps; compno++) {
6774 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
6775 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
6776 OPJ_INT32 bandno, numbands;
6779 l_tccp_info->csty = l_tccp->csty;
6780 l_tccp_info->numresolutions = l_tccp->numresolutions;
6781 l_tccp_info->cblkw = l_tccp->cblkw;
6782 l_tccp_info->cblkh = l_tccp->cblkh;
6783 l_tccp_info->cblksty = l_tccp->cblksty;
6784 l_tccp_info->qmfbid = l_tccp->qmfbid;
6785 if (l_tccp->numresolutions < J2K_MAXRLVLS)
6787 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions - 1);
6788 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions - 1);
6791 /* quantization style*/
6792 l_tccp_info->qntsty = l_tccp->qntsty;
6793 l_tccp_info->numgbits = l_tccp->numgbits;
6795 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
6796 if (numbands < J2K_MAXBANDS) {
6797 for (bandno = 0; bandno < numbands; bandno++) {
6798 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
6799 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
6804 l_tccp_info->roishift = l_tccp->roishift;
6812 * Get the codestream index from a JPEG2000 codec.
6814 *@param p_j2k the component image header to dump.
6816 *@return the codestream index extract from the jpg2000 codec
6818 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
6820 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
6821 opj_calloc(1,sizeof(opj_codestream_index_t));
6825 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
6826 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
6827 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
6829 l_cstr_index->maxmarknum = p_j2k->cstr_index->maxmarknum;
6830 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
6831 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
6832 if (!l_cstr_index->marker)
6835 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
6838 return l_cstr_index;
6846 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
6847 opj_stream_private_t *p_stream,
6848 opj_event_mgr_t * p_manager)
6850 opj_bool l_go_on = OPJ_TRUE;
6851 OPJ_UINT32 l_current_tile_no;
6852 OPJ_UINT32 l_data_size,l_max_data_size;
6853 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
6854 OPJ_UINT32 l_nb_comps;
6855 OPJ_BYTE * l_current_data;
6857 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
6858 if (! l_current_data) {
6861 l_max_data_size = 1000;
6864 if (! j2k_read_tile_header( p_j2k,
6867 &l_tile_x0, &l_tile_y0,
6868 &l_tile_x1, &l_tile_y1,
6880 if (l_data_size > l_max_data_size) {
6881 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
6882 if (! l_current_data) {
6886 l_max_data_size = l_data_size;
6889 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
6890 opj_free(l_current_data);
6893 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);
6895 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data)) {
6896 opj_free(l_current_data);
6899 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
6903 opj_free(l_current_data);
6909 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
6911 void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
6914 assert(p_j2k != 00);
6916 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_decode_tiles);
6917 /* DEVELOPER CORNER, add your custom procedures */
6923 * Decodes the tiles of the stream.
6925 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
6926 opj_stream_private_t * p_stream,
6927 opj_image_t * p_image,
6928 opj_event_mgr_t * p_manager)
6932 /* customization of the decoding */
6933 j2k_setup_decoding(p_j2k);
6936 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
6937 opj_image_destroy(p_j2k->m_image);
6938 p_j2k->m_image = NULL;
6942 for (compno = 0; compno < p_image->numcomps; compno++) {
6943 p_image->comps[compno].data = p_j2k->m_image->comps[compno].data;
6944 p_j2k->m_image->comps[compno].data = NULL;
6947 return OPJ_TRUE /*p_j2k->m_image*/;