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(
107 opj_j2k_v2_t * p_j2k,
108 opj_stream_private_t *p_stream,
109 opj_event_mgr_t * p_manager
113 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
114 * with the marker value.
115 * @param p_id Marker value to look up
117 * @return the handler associated with the id.
119 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
122 * Destroys a tile coding parameter structure.
124 * @param p_tcp the tile coding parameter to destroy.
126 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
129 * Destroys a coding parameter structure.
131 * @param p_cp the coding parameter to destroy.
133 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
137 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
138 * @param p_header_data the data contained in the COM box.
139 * @param p_j2k the jpeg2000 codec.
140 * @param p_header_size the size of the data contained in the COM marker.
141 * @param p_manager the user event manager.
143 static opj_bool j2k_read_SPCod_SPCoc(
146 OPJ_BYTE * p_header_data,
147 OPJ_UINT32 * p_header_size,
148 struct opj_event_mgr * p_manager
152 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
154 * @param p_tile_no the tile to output.
155 * @param p_comp_no the component number to output.
156 * @param p_data the data buffer.
157 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
158 * @param p_j2k J2K codec.
159 * @param p_manager the user event manager.
162 static opj_bool j2k_read_SQcd_SQcc(
165 OPJ_BYTE * p_header_data,
166 OPJ_UINT32 * p_header_size,
167 struct opj_event_mgr * p_manager
171 * Copies the tile component parameters of all the component from the first tile component.
173 * @param p_j2k the J2k codec.
175 static void j2k_copy_tile_component_parameters(
180 * Copies the tile quantization parameters of all the component from the first tile component.
182 * @param p_j2k the J2k codec.
184 static void j2k_copy_tile_quantization_parameters(
189 * -----------------------------------------------------------------------
190 * -----------------------------------------------------------------------
191 * -----------------------------------------------------------------------
195 Write the SOC marker (Start Of Codestream)
196 @param j2k J2K handle
198 static void j2k_write_soc(opj_j2k_t *j2k);
200 Read the SOC marker (Start of Codestream)
201 @param j2k J2K handle
203 static void j2k_read_soc(opj_j2k_t *j2k);
206 * Reads a SOC marker (Start of Codestream)
207 * @param p_header_data the data contained in the SOC box.
208 * @param jp2 the jpeg2000 file codec.
209 * @param p_header_size the size of the data contained in the SOC marker.
210 * @param p_manager the user event manager.
212 static opj_bool j2k_read_soc_v2(
214 struct opj_stream_private *p_stream,
215 struct opj_event_mgr * p_manager
219 Write the SIZ marker (image and tile size)
220 @param j2k J2K handle
222 static void j2k_write_siz(opj_j2k_t *j2k);
224 Read the SIZ marker (image and tile size)
225 @param j2k J2K handle
227 static void j2k_read_siz(opj_j2k_t *j2k);
230 * Reads a SIZ marker (image and tile size)
231 * @param p_header_data the data contained in the SIZ box.
232 * @param jp2 the jpeg2000 file codec.
233 * @param p_header_size the size of the data contained in the SIZ marker.
234 * @param p_manager the user event manager.
236 static opj_bool j2k_read_siz_v2 (
238 OPJ_BYTE * p_header_data,
239 OPJ_UINT32 p_header_size,
240 struct opj_event_mgr * p_manager
244 Write the COM marker (comment)
245 @param j2k J2K handle
247 static void j2k_write_com(opj_j2k_t *j2k);
249 Read the COM marker (comment)
250 @param j2k J2K handle
252 static void j2k_read_com(opj_j2k_t *j2k);
254 * Reads a COM marker (comments)
255 * @param p_header_data the data contained in the COM box.
256 * @param jp2 the jpeg2000 file codec.
257 * @param p_header_size the size of the data contained in the COM marker.
258 * @param p_manager the user event manager.
260 static opj_bool j2k_read_com_v2 (
262 OPJ_BYTE * p_header_data,
263 OPJ_UINT32 p_header_size,
264 struct opj_event_mgr * p_manager
267 Write the value concerning the specified component in the marker COD and COC
268 @param j2k J2K handle
269 @param compno Number of the component concerned by the information written
271 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
273 Read the value concerning the specified component in the marker COD and COC
274 @param j2k J2K handle
275 @param compno Number of the component concerned by the information read
277 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
279 Write the COD marker (coding style default)
280 @param j2k J2K handle
282 static void j2k_write_cod(opj_j2k_t *j2k);
284 Read the COD marker (coding style default)
285 @param j2k J2K handle
287 static void j2k_read_cod(opj_j2k_t *j2k);
290 * Reads a COD marker (Coding Styke defaults)
291 * @param p_header_data the data contained in the COD box.
292 * @param p_j2k the jpeg2000 codec.
293 * @param p_header_size the size of the data contained in the COD marker.
294 * @param p_manager the user event manager.
296 static opj_bool j2k_read_cod_v2 (
298 OPJ_BYTE * p_header_data,
299 OPJ_UINT32 p_header_size,
300 struct opj_event_mgr * p_manager
304 Write the COC marker (coding style component)
305 @param j2k J2K handle
306 @param compno Number of the component concerned by the information written
308 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
310 Read the COC marker (coding style component)
311 @param j2k J2K handle
313 static void j2k_read_coc(opj_j2k_t *j2k);
316 * Reads a COC marker (Coding Style Component)
317 * @param p_header_data the data contained in the COC box.
318 * @param p_j2k the jpeg2000 codec.
319 * @param p_header_size the size of the data contained in the COC marker.
320 * @param p_manager the user event manager.
322 static opj_bool j2k_read_coc_v2 (
324 OPJ_BYTE * p_header_data,
325 OPJ_UINT32 p_header_size,
326 struct opj_event_mgr * p_manager
330 Write the value concerning the specified component in the marker QCD and QCC
331 @param j2k J2K handle
332 @param compno Number of the component concerned by the information written
334 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
336 Read the value concerning the specified component in the marker QCD and QCC
337 @param j2k J2K handle
338 @param compno Number of the component concern by the information read
339 @param len Length of the information in the QCX part of the marker QCD/QCC
341 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
343 Write the QCD marker (quantization default)
344 @param j2k J2K handle
346 static void j2k_write_qcd(opj_j2k_t *j2k);
348 Read the QCD marker (quantization default)
349 @param j2k J2K handle
351 static void j2k_read_qcd(opj_j2k_t *j2k);
354 * Reads a QCD marker (Quantization defaults)
355 * @param p_header_data the data contained in the QCD box.
356 * @param p_j2k the jpeg2000 codec.
357 * @param p_header_size the size of the data contained in the QCD marker.
358 * @param p_manager the user event manager.
360 static opj_bool j2k_read_qcd_v2 (
362 OPJ_BYTE * p_header_data,
363 OPJ_UINT32 p_header_size,
364 struct opj_event_mgr * p_manager
368 Write the QCC marker (quantization component)
369 @param j2k J2K handle
370 @param compno Number of the component concerned by the information written
372 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
374 Read the QCC marker (quantization component)
375 @param j2k J2K handle
377 static void j2k_read_qcc(opj_j2k_t *j2k);
379 * Reads a QCC marker (Quantization component)
380 * @param p_header_data the data contained in the QCC box.
381 * @param p_j2k the jpeg2000 codec.
382 * @param p_header_size the size of the data contained in the QCC marker.
383 * @param p_manager the user event manager.
385 static opj_bool j2k_read_qcc_v2(
387 OPJ_BYTE * p_header_data,
388 OPJ_UINT32 p_header_size,
389 struct opj_event_mgr * p_manager);
392 Write the POC marker (progression order change)
393 @param j2k J2K handle
395 static void j2k_write_poc(opj_j2k_t *j2k);
397 Read the POC marker (progression order change)
398 @param j2k J2K handle
400 static void j2k_read_poc(opj_j2k_t *j2k);
402 * Reads a POC marker (Progression Order Change)
404 * @param p_header_data the data contained in the POC box.
405 * @param p_j2k the jpeg2000 codec.
406 * @param p_header_size the size of the data contained in the POC marker.
407 * @param p_manager the user event manager.
409 static opj_bool j2k_read_poc_v2 (
411 OPJ_BYTE * p_header_data,
412 OPJ_UINT32 p_header_size,
413 struct opj_event_mgr * p_manager
416 Read the CRG marker (component registration)
417 @param j2k J2K handle
419 static void j2k_read_crg(opj_j2k_t *j2k);
421 * Reads a CRG marker (Component registration)
423 * @param p_header_data the data contained in the TLM box.
424 * @param p_j2k the jpeg2000 codec.
425 * @param p_header_size the size of the data contained in the TLM marker.
426 * @param p_manager the user event manager.
428 static opj_bool j2k_read_crg_v2 (
430 OPJ_BYTE * p_header_data,
431 OPJ_UINT32 p_header_size,
432 struct opj_event_mgr * p_manager
435 Read the TLM marker (tile-part lengths)
436 @param j2k J2K handle
438 static void j2k_read_tlm(opj_j2k_t *j2k);
440 * Reads a TLM marker (Tile Length Marker)
442 * @param p_header_data the data contained in the TLM box.
443 * @param p_j2k the jpeg2000 codec.
444 * @param p_header_size the size of the data contained in the TLM marker.
445 * @param p_manager the user event manager.
447 static opj_bool j2k_read_tlm_v2 (
449 OPJ_BYTE * p_header_data,
450 OPJ_UINT32 p_header_size,
451 struct opj_event_mgr * p_manager
454 Read the PLM marker (packet length, main header)
455 @param j2k J2K handle
457 static void j2k_read_plm(opj_j2k_t *j2k);
460 * Reads a PLM marker (Packet length, main header marker)
462 * @param p_header_data the data contained in the TLM box.
463 * @param p_j2k the jpeg2000 codec.
464 * @param p_header_size the size of the data contained in the TLM marker.
465 * @param p_manager the user event manager.
467 static opj_bool j2k_read_plm_v2 (
469 OPJ_BYTE * p_header_data,
470 OPJ_UINT32 p_header_size,
471 struct opj_event_mgr * p_manager
474 Read the PLT marker (packet length, tile-part header)
475 @param j2k J2K handle
477 static void j2k_read_plt(opj_j2k_t *j2k);
479 * Reads a PLT marker (Packet length, tile-part header)
481 * @param p_header_data the data contained in the PLT box.
482 * @param p_j2k the jpeg2000 codec.
483 * @param p_header_size the size of the data contained in the PLT marker.
484 * @param p_manager the user event manager.
486 static opj_bool j2k_read_plt_v2 (
488 OPJ_BYTE * p_header_data,
489 OPJ_UINT32 p_header_size,
490 struct opj_event_mgr * p_manager
493 Read the PPM marker (packet packet headers, main header)
494 @param j2k J2K handle
496 static void j2k_read_ppm(opj_j2k_t *j2k);
498 * Reads a PPM marker (Packed packet headers, main header)
500 * @param p_header_data the data contained in the POC box.
501 * @param p_j2k the jpeg2000 codec.
502 * @param p_header_size the size of the data contained in the POC marker.
503 * @param p_manager the user event manager.
505 static opj_bool j2k_read_ppm_v2 (
507 OPJ_BYTE * p_header_data,
508 OPJ_UINT32 p_header_size,
509 struct opj_event_mgr * p_manager
512 static opj_bool j2k_read_ppm_v3 (
514 OPJ_BYTE * p_header_data,
515 OPJ_UINT32 p_header_size,
516 struct opj_event_mgr * p_manager
520 Read the PPT marker (packet packet headers, tile-part header)
521 @param j2k J2K handle
523 static void j2k_read_ppt(opj_j2k_t *j2k);
525 * Reads a PPT marker (Packed packet headers, tile-part header)
527 * @param p_header_data the data contained in the PPT box.
528 * @param p_j2k the jpeg2000 codec.
529 * @param p_header_size the size of the data contained in the PPT marker.
530 * @param p_manager the user event manager.
532 static opj_bool j2k_read_ppt_v2 (
534 OPJ_BYTE * p_header_data,
535 OPJ_UINT32 p_header_size,
536 struct opj_event_mgr * p_manager
539 Write the TLM marker (Mainheader)
540 @param j2k J2K handle
542 static void j2k_write_tlm(opj_j2k_t *j2k);
544 Write the SOT marker (start of tile-part)
545 @param j2k J2K handle
547 static void j2k_write_sot(opj_j2k_t *j2k);
549 Read the SOT marker (start of tile-part)
550 @param j2k J2K handle
552 static void j2k_read_sot(opj_j2k_t *j2k);
555 * Reads a PPT marker (Packed packet headers, tile-part header)
557 * @param p_header_data the data contained in the PPT box.
558 * @param p_j2k the jpeg2000 codec.
559 * @param p_header_size the size of the data contained in the PPT marker.
560 * @param p_manager the user event manager.
562 static opj_bool j2k_read_sot_v2 (
564 OPJ_BYTE * p_header_data,
565 OPJ_UINT32 p_header_size,
566 struct opj_event_mgr * p_manager
569 Write the SOD marker (start of data)
570 @param j2k J2K handle
571 @param tile_coder Pointer to a TCD handle
573 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
575 Read the SOD marker (start of data)
576 @param j2k J2K handle
578 static void j2k_read_sod(opj_j2k_t *j2k);
581 * Reads a SOD marker (Start Of Data)
583 * @param p_header_data the data contained in the SOD box.
584 * @param p_j2k the jpeg2000 codec.
585 * @param p_header_size the size of the data contained in the SOD marker.
586 * @param p_manager the user event manager.
588 static opj_bool j2k_read_sod_v2 (
590 struct opj_stream_private *p_stream,
591 struct opj_event_mgr * p_manager
595 Write the RGN marker (region-of-interest)
596 @param j2k J2K handle
597 @param compno Number of the component concerned by the information written
598 @param tileno Number of the tile concerned by the information written
600 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
602 Read the RGN marker (region-of-interest)
603 @param j2k J2K handle
605 static void j2k_read_rgn(opj_j2k_t *j2k);
608 * Reads a RGN marker (Region Of Interest)
610 * @param p_header_data the data contained in the POC box.
611 * @param p_j2k the jpeg2000 codec.
612 * @param p_header_size the size of the data contained in the POC marker.
613 * @param p_manager the user event manager.
615 static opj_bool j2k_read_rgn_v2 (
617 OPJ_BYTE * p_header_data,
618 OPJ_UINT32 p_header_size,
619 struct opj_event_mgr * p_manager
623 Write the EOC marker (end of codestream)
624 @param j2k J2K handle
626 static void j2k_write_eoc(opj_j2k_t *j2k);
628 Read the EOC marker (end of codestream)
629 @param j2k J2K handle
631 static void j2k_read_eoc(opj_j2k_t *j2k);
634 * Reads a EOC marker (End Of Codestream)
636 * @param p_header_data the data contained in the SOD box.
637 * @param p_j2k the jpeg2000 codec.
638 * @param p_header_size the size of the data contained in the SOD marker.
639 * @param p_manager the user event manager.
641 static opj_bool j2k_read_eoc_v2 (
643 struct opj_stream_private *p_stream,
644 struct opj_event_mgr * p_manager
648 Read an unknown marker
649 @param j2k J2K handle
651 static void j2k_read_unk(opj_j2k_t *j2k);
653 Add main header marker information
654 @param cstr_info Codestream information structure
655 @param type marker type
656 @param pos byte offset of marker segment
657 @param len length of marker segment
659 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
661 Add tile header marker information
662 @param tileno tile index number
663 @param cstr_info Codestream information structure
664 @param type marker type
665 @param pos byte offset of marker segment
666 @param len length of marker segment
668 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
672 * Reads an unknown marker
674 * @param p_stream the stream object to read from.
675 * @param p_j2k the jpeg2000 codec.
676 * @param p_manager the user event manager.
678 * @return true if the marker could be deduced.
680 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
681 struct opj_stream_private *p_stream,
682 struct opj_event_mgr * p_manager );
688 /* ----------------------------------------------------------------------- */
689 typedef struct j2k_prog_order{
690 OPJ_PROG_ORDER enum_prog;
694 j2k_prog_order_t j2k_prog_order_list[] = {
700 {(OPJ_PROG_ORDER)-1, ""}
703 typedef struct opj_dec_memory_marker_handler
707 /** value of the state when the marker can appear */
709 /** action linked to the marker */
710 opj_bool (*handler) (
712 OPJ_BYTE * p_header_data,
713 OPJ_UINT32 p_header_size,
714 struct opj_event_mgr * p_manager
717 opj_dec_memory_marker_handler_t;
719 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
722 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
723 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
724 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
725 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
726 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
727 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
728 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
729 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
730 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
731 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
732 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
733 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
734 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
736 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
737 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
738 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
739 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
740 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
741 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
743 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
744 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
745 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
746 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
747 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
748 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
749 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
750 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
751 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
752 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
753 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
754 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
755 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
757 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
758 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
759 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}//j2k_read_unk_v2}
761 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
762 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
763 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
764 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
767 {J2K_MS_EPC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_epc},
768 {J2K_MS_EPB, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_epb},
769 {J2K_MS_ESD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_esd},
770 {J2K_MS_RED, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_red},
771 #endif /* USE_JPWL */
773 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
774 {J2K_MS_INSEC, 0, j2k_read_insec}
775 #endif /* USE_JPSEC */
780 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
781 j2k_prog_order_t *po;
782 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
783 if(po->enum_prog == prg_order){
790 /* ----------------------------------------------------------------------- */
791 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
795 opj_tcp_t *tcp = &cp->tcps[tileno];
796 prog = j2k_convert_progression_order(tcp->prg);
801 if( cp->tp_flag == prog[i] ){
802 tpend=1;cp->tp_pos=i;
806 tpnum= tpnum * tcp->pocs[pino].compE;
809 tpnum= tpnum * tcp->pocs[pino].resE;
812 tpnum= tpnum * tcp->pocs[pino].prcE;
815 tpnum= tpnum * tcp->pocs[pino].layE;
826 /** mem allocation for TLM marker*/
827 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
828 int pino,tileno,totnum_tp=0;
830 OPJ_ARG_NOT_USED(img_numcomp);
832 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
833 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
834 int cur_totnum_tp = 0;
835 opj_tcp_t *tcp = &cp->tcps[tileno];
836 for(pino = 0; pino <= tcp->numpocs; pino++) {
838 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
839 if(!pi) { return -1;}
840 tp_num = j2k_get_num_tp(cp,pino,tileno);
841 totnum_tp = totnum_tp + tp_num;
842 cur_totnum_tp = cur_totnum_tp + tp_num;
843 pi_destroy(pi, cp, tileno);
845 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
847 if (j2k->cstr_info) {
848 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
849 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
856 static void j2k_write_soc(opj_j2k_t *j2k) {
857 opj_cio_t *cio = j2k->cio;
858 cio_write(cio, J2K_MS_SOC, 2);
861 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
866 /* update markers struct */
867 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
868 #endif /* USE_JPWL */
872 static void j2k_read_soc(opj_j2k_t *j2k) {
873 j2k->state = J2K_STATE_MHSIZ;
875 if (j2k->cstr_info) {
876 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
877 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
882 * Reads a SOC marker (Start of Codestream)
883 * @param p_header_data the data contained in the SOC box.
884 * @param jp2 the jpeg2000 file codec.
885 * @param p_header_size the size of the data contained in the SOC marker.
886 * @param p_manager the user event manager.
888 static opj_bool j2k_read_soc_v2(
890 struct opj_stream_private *p_stream,
891 struct opj_event_mgr * p_manager
899 assert(p_manager != 00);
900 assert(p_stream != 00);
902 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
906 opj_read_bytes(l_data,&l_marker,2);
907 if (l_marker != J2K_MS_SOC) {
911 /* assure length of data is correct (0) */
912 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ; // FIXME J2K_DEC_STATE_MHSIZ;
915 if (p_j2k->cstr_info) {
916 //TODO p_j2k->cstr_info->main_head_start = opj_stream_tell(p_stream) - 2; // why - 2 ?
917 p_j2k->cstr_info->codestream_size = 0;/*p_stream_numbytesleft(p_j2k->p_stream) + 2 - p_j2k->cstr_info->main_head_start*/;
923 static void j2k_write_siz(opj_j2k_t *j2k) {
927 opj_cio_t *cio = j2k->cio;
928 opj_image_t *image = j2k->image;
929 opj_cp_t *cp = j2k->cp;
931 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
932 lenp = cio_tell(cio);
934 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
935 cio_write(cio, image->x1, 4); /* Xsiz */
936 cio_write(cio, image->y1, 4); /* Ysiz */
937 cio_write(cio, image->x0, 4); /* X0siz */
938 cio_write(cio, image->y0, 4); /* Y0siz */
939 cio_write(cio, cp->tdx, 4); /* XTsiz */
940 cio_write(cio, cp->tdy, 4); /* YTsiz */
941 cio_write(cio, cp->tx0, 4); /* XT0siz */
942 cio_write(cio, cp->ty0, 4); /* YT0siz */
943 cio_write(cio, image->numcomps, 2); /* Csiz */
944 for (i = 0; i < image->numcomps; i++) {
945 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
946 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
947 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
949 len = cio_tell(cio) - lenp;
951 cio_write(cio, len, 2); /* Lsiz */
952 cio_seek(cio, lenp + len);
955 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
958 static void j2k_read_siz(opj_j2k_t *j2k) {
961 opj_cio_t *cio = j2k->cio;
962 opj_image_t *image = j2k->image;
963 opj_cp_t *cp = j2k->cp;
965 len = cio_read(cio, 2); /* Lsiz */
966 cio_read(cio, 2); /* Rsiz (capabilities) */
967 image->x1 = cio_read(cio, 4); /* Xsiz */
968 image->y1 = cio_read(cio, 4); /* Ysiz */
969 image->x0 = cio_read(cio, 4); /* X0siz */
970 image->y0 = cio_read(cio, 4); /* Y0siz */
971 cp->tdx = cio_read(cio, 4); /* XTsiz */
972 cp->tdy = cio_read(cio, 4); /* YTsiz */
973 cp->tx0 = cio_read(cio, 4); /* XT0siz */
974 cp->ty0 = cio_read(cio, 4); /* YT0siz */
976 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
977 opj_event_msg(j2k->cinfo, EVT_ERROR,
978 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
979 image->x0,image->x1,image->y0,image->y1);
983 image->numcomps = cio_read(cio, 2); /* Csiz */
986 if (j2k->cp->correct) {
987 /* if JPWL is on, we check whether TX errors have damaged
988 too much the SIZ parameters */
989 if (!(image->x1 * image->y1)) {
990 opj_event_msg(j2k->cinfo, EVT_ERROR,
991 "JPWL: bad image size (%d x %d)\n",
992 image->x1, image->y1);
993 if (!JPWL_ASSUME || JPWL_ASSUME) {
994 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
998 if (image->numcomps != ((len - 38) / 3)) {
999 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1000 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1001 image->numcomps, ((len - 38) / 3));
1003 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1006 /* we try to correct */
1007 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
1008 if (image->numcomps < ((len - 38) / 3)) {
1009 len = 38 + 3 * image->numcomps;
1010 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1013 image->numcomps = ((len - 38) / 3);
1014 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1019 /* update components number in the jpwl_exp_comps filed */
1020 cp->exp_comps = image->numcomps;
1022 #endif /* USE_JPWL */
1024 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
1025 for (i = 0; i < image->numcomps; i++) {
1027 tmp = cio_read(cio, 1); /* Ssiz_i */
1028 image->comps[i].prec = (tmp & 0x7f) + 1;
1029 image->comps[i].sgnd = tmp >> 7;
1030 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
1031 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
1034 if (j2k->cp->correct) {
1035 /* if JPWL is on, we check whether TX errors have damaged
1036 too much the SIZ parameters, again */
1037 if (!(image->comps[i].dx * image->comps[i].dy)) {
1038 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1039 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1040 i, i, image->comps[i].dx, image->comps[i].dy);
1042 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1045 /* we try to correct */
1046 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1047 if (!image->comps[i].dx) {
1048 image->comps[i].dx = 1;
1049 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1050 i, image->comps[i].dx);
1052 if (!image->comps[i].dy) {
1053 image->comps[i].dy = 1;
1054 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1055 i, image->comps[i].dy);
1060 #endif /* USE_JPWL */
1062 /* TODO: unused ? */
1063 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
1064 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
1066 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
1067 image->comps[i].factor = cp->reduce; /* reducing factor per component */
1070 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
1071 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
1074 if (j2k->cp->correct) {
1075 /* if JPWL is on, we check whether TX errors have damaged
1076 too much the SIZ parameters */
1077 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
1078 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1079 "JPWL: bad number of tiles (%d x %d)\n",
1082 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1085 /* we try to correct */
1086 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1089 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1092 if (cp->tw > cp->max_tiles) {
1094 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
1095 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1096 cp->max_tiles, cp->tw);
1100 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1103 if (cp->th > cp->max_tiles) {
1105 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1106 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1107 cp->max_tiles, cp->th);
1111 #endif /* USE_JPWL */
1113 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
1114 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
1115 cp->tileno_size = 0;
1118 if (j2k->cp->correct) {
1120 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1121 "JPWL: could not alloc tcps field of cp\n");
1122 if (!JPWL_ASSUME || JPWL_ASSUME) {
1123 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1128 #endif /* USE_JPWL */
1130 for (i = 0; i < cp->tw * cp->th; i++) {
1131 cp->tcps[i].POC = 0;
1132 cp->tcps[i].numpocs = 0;
1133 cp->tcps[i].first = 1;
1136 /* Initialization for PPM marker */
1138 cp->ppm_data = NULL;
1139 cp->ppm_data_first = NULL;
1140 cp->ppm_previous = 0;
1143 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
1144 for (i = 0; i < cp->tw * cp->th; i++) {
1145 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
1147 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
1148 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
1149 j2k->state = J2K_STATE_MH;
1152 if (j2k->cstr_info) {
1153 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1154 cstr_info->image_w = image->x1 - image->x0;
1155 cstr_info->image_h = image->y1 - image->y0;
1156 cstr_info->numcomps = image->numcomps;
1157 cstr_info->tw = cp->tw;
1158 cstr_info->th = cp->th;
1159 cstr_info->tile_x = cp->tdx;
1160 cstr_info->tile_y = cp->tdy;
1161 cstr_info->tile_Ox = cp->tx0;
1162 cstr_info->tile_Oy = cp->ty0;
1163 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
1169 * Reads a SIZ marker (image and tile size)
1170 * @param p_header_data the data contained in the SIZ box.
1171 * @param jp2 the jpeg2000 file codec.
1172 * @param p_header_size the size of the data contained in the SIZ marker.
1173 * @param p_manager the user event manager.
1175 opj_bool j2k_read_siz_v2 (
1176 opj_j2k_v2_t *p_j2k,
1177 OPJ_BYTE * p_header_data,
1178 OPJ_UINT32 p_header_size,
1179 struct opj_event_mgr * p_manager
1182 OPJ_UINT32 l_size, i;
1183 OPJ_UINT32 l_nb_comp;
1184 OPJ_UINT32 l_nb_comp_remain;
1185 OPJ_UINT32 l_remaining_size;
1186 OPJ_UINT32 l_nb_tiles;
1188 opj_image_header_t *l_image = 00;
1189 opj_cp_v2_t *l_cp = 00;
1190 opj_image_comp_header_t * l_img_comp = 00;
1191 opj_tcp_v2_t * l_current_tile_param = 00;
1194 assert(p_j2k != 00);
1195 assert(p_manager != 00);
1196 assert(p_header_data != 00);
1198 l_image = p_j2k->m_image_header;
1199 l_cp = &(p_j2k->m_cp);
1201 // minimum size == 39 - 3 (= minimum component parameter)
1202 if (p_header_size < 36) {
1203 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1207 l_remaining_size = p_header_size - 36;
1208 l_nb_comp = l_remaining_size / 3;
1209 l_nb_comp_remain = l_remaining_size % 3;
1210 if (l_nb_comp_remain != 0){
1211 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
1215 l_size = p_header_size + 2; /* Lsiz */
1217 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
1219 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
1220 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
1222 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
1224 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
1226 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
1228 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
1230 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
1232 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
1234 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
1236 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
1239 l_image->numcomps = (OPJ_UINT16) l_tmp;
1241 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
1245 if (l_image->numcomps != l_nb_comp) {
1246 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);
1251 if (p_j2k->m_cp->correct) {
1252 /* if JPWL is on, we check whether TX errors have damaged
1253 too much the SIZ parameters */
1254 if (!(image->x1 * image->y1)) {
1255 opj_event_msg(p_j2k->cinfo, EVT_ERROR,
1256 "JPWL: bad image size (%d x %d)\n",
1257 image->x1, image->y1);
1258 if (!JPWL_ASSUME || JPWL_ASSUME) {
1259 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1263 if (image->numcomps != ((len - 38) / 3)) {
1264 opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1265 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
1266 image->numcomps, ((len - 38) / 3));
1268 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1271 /* we try to correct */
1272 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
1273 if (image->numcomps < ((len - 38) / 3)) {
1274 len = 38 + 3 * image->numcomps;
1275 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
1278 image->numcomps = ((len - 38) / 3);
1279 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
1284 /* update components number in the jpwl_exp_comps filed */
1285 cp->exp_comps = image->numcomps;
1287 #endif /* USE_JPWL */
1289 // Allocate the resulting image components
1290 l_image->comps = (opj_image_comp_header_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_header_t));
1291 if (l_image->comps == 00){
1292 l_image->numcomps = 0;
1293 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1297 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_header_t));
1298 l_img_comp = l_image->comps;
1300 // Read the component information
1301 for (i = 0; i < l_image->numcomps; ++i){
1303 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
1305 l_img_comp->prec = (tmp & 0x7f) + 1;
1306 l_img_comp->sgnd = tmp >> 7;
1307 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
1309 l_img_comp->dx = (OPJ_INT32)tmp; // should be between 1 and 255
1310 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
1312 l_img_comp->dy = (OPJ_INT32)tmp; // should be between 1 and 255
1315 if (p_j2k->m_cp->correct) {
1316 /* if JPWL is on, we check whether TX errors have damaged
1317 too much the SIZ parameters, again */
1318 if (!(image->comps[i].dx * image->comps[i].dy)) {
1319 opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1320 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
1321 i, i, image->comps[i].dx, image->comps[i].dy);
1323 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1326 /* we try to correct */
1327 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1328 if (!image->comps[i].dx) {
1329 image->comps[i].dx = 1;
1330 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
1331 i, image->comps[i].dx);
1333 if (!image->comps[i].dy) {
1334 image->comps[i].dy = 1;
1335 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
1336 i, image->comps[i].dy);
1340 #endif /* USE_JPWL */
1341 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
1342 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
1346 // Compute the number of tiles
1347 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
1348 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
1349 l_nb_tiles = l_cp->tw * l_cp->th;
1351 // Define the tiles which will be decoded
1352 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
1353 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;
1354 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;
1355 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);
1356 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);
1359 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
1360 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
1361 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
1362 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
1366 if (p_j2k->m_cp->correct) {
1367 /* if JPWL is on, we check whether TX errors have damaged
1368 too much the SIZ parameters */
1369 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
1370 opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1371 "JPWL: bad number of tiles (%d x %d)\n",
1374 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1377 /* we try to correct */
1378 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
1381 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
1384 if (cp->tw > cp->max_tiles) {
1386 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
1387 "- setting %d tiles in x => HYPOTHESIS!!!\n",
1388 cp->max_tiles, cp->tw);
1392 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
1395 if (cp->th > cp->max_tiles) {
1397 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
1398 "- setting %d tiles in y => HYPOTHESIS!!!\n",
1399 cp->max_tiles, cp->th);
1403 #endif /* USE_JPWL */
1405 /* memory allocations */
1406 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
1407 if (l_cp->tcps == 00) {
1408 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1411 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
1414 if (p_j2k->m_cp->correct) {
1416 opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1417 "JPWL: could not alloc tcps field of cp\n");
1418 if (!JPWL_ASSUME || JPWL_ASSUME) {
1419 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1424 #endif /* USE_JPWL */
1426 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
1427 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
1428 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
1429 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1432 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
1434 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
1435 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
1437 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
1438 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1441 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));
1442 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
1444 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
1445 (opj_simple_mcc_decorrelation_data_t*)
1446 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
1448 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
1449 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1452 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));
1453 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
1455 /* set up default dc level shift */
1456 for (i=0;i<l_image->numcomps;++i) {
1457 if (! l_image->comps[i].sgnd) {
1458 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
1462 l_current_tile_param = l_cp->tcps;
1463 for (i = 0; i < l_nb_tiles; ++i) {
1464 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
1465 if (l_current_tile_param->tccps == 00) {
1466 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
1469 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
1471 ++l_current_tile_param;
1474 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; // FIXME J2K_DEC_STATE_MH;
1475 opj_image_comp_header_update(l_image,l_cp);
1478 if (p_j2k->cstr_info) {
1481 p_j2k->cstr_info->image_w = l_image->x1 - l_image->x0;
1482 p_j2k->cstr_info->image_h = l_image->y1 - l_image->y0;
1483 p_j2k->cstr_info->numcomps = l_image->numcomps;
1484 p_j2k->cstr_info->tw = l_cp->tw;
1485 p_j2k->cstr_info->th = l_cp->th;
1486 p_j2k->cstr_info->tdx = l_cp->tdx;
1487 p_j2k->cstr_info->tdy = l_cp->tdy;
1488 p_j2k->cstr_info->tx0 = l_cp->tx0;
1489 p_j2k->cstr_info->ty0 = l_cp->ty0;
1491 p_j2k->cstr_info->tile = (opj_tile_info_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tile_info_v2_t));
1492 if (! p_j2k->cstr_info->tile) {
1493 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory [SIZ marker]\n");
1497 for (it_tile = 0; it_tile < l_nb_tiles; it_tile++ ) {
1498 p_j2k->cstr_info->tile[it_tile].tccp_info =
1499 (opj_tccp_info_t*) opj_calloc( l_image->numcomps, sizeof(opj_tccp_info_t));
1500 if (! p_j2k->cstr_info->tile[it_tile].tccp_info) {
1501 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory [SIZ marker]\n");
1512 static void j2k_write_com(opj_j2k_t *j2k) {
1516 if(j2k->cp->comment) {
1517 opj_cio_t *cio = j2k->cio;
1518 char *comment = j2k->cp->comment;
1520 cio_write(cio, J2K_MS_COM, 2);
1521 lenp = cio_tell(cio);
1523 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
1524 for (i = 0; i < strlen(comment); i++) {
1525 cio_write(cio, comment[i], 1);
1527 len = cio_tell(cio) - lenp;
1528 cio_seek(cio, lenp);
1529 cio_write(cio, len, 2);
1530 cio_seek(cio, lenp + len);
1534 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
1539 static void j2k_read_com(opj_j2k_t *j2k) {
1542 opj_cio_t *cio = j2k->cio;
1544 len = cio_read(cio, 2);
1545 cio_skip(cio, len - 2);
1548 * Reads a COM marker (comments)
1549 * @param p_header_data the data contained in the COM box.
1550 * @param jp2 the jpeg2000 file codec.
1551 * @param p_header_size the size of the data contained in the COM marker.
1552 * @param p_manager the user event manager.
1554 opj_bool j2k_read_com_v2 (
1555 opj_j2k_v2_t *p_j2k,
1556 OPJ_BYTE * p_header_data,
1557 OPJ_UINT32 p_header_size,
1558 struct opj_event_mgr * p_manager
1562 assert(p_j2k != 00);
1563 assert(p_manager != 00);
1564 assert(p_header_data != 00);
1569 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
1572 opj_cp_t *cp = j2k->cp;
1573 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1574 opj_tccp_t *tccp = &tcp->tccps[compno];
1575 opj_cio_t *cio = j2k->cio;
1577 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
1578 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
1579 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
1580 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
1581 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
1583 if (tccp->csty & J2K_CCP_CSTY_PRT) {
1584 for (i = 0; i < tccp->numresolutions; i++) {
1585 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
1590 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
1593 opj_cp_t *cp = j2k->cp;
1594 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1595 opj_tccp_t *tccp = &tcp->tccps[compno];
1596 opj_cio_t *cio = j2k->cio;
1598 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
1600 // If user wants to remove more resolutions than the codestream contains, return error
1601 if (cp->reduce >= tccp->numresolutions) {
1602 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
1603 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
1604 j2k->state |= J2K_STATE_ERR;
1607 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
1608 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
1609 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
1610 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
1611 if (tccp->csty & J2K_CP_CSTY_PRT) {
1612 for (i = 0; i < tccp->numresolutions; i++) {
1613 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
1614 tccp->prcw[i] = tmp & 0xf;
1615 tccp->prch[i] = tmp >> 4;
1620 if(j2k->cstr_info && compno == 0) {
1621 for (i = 0; i < tccp->numresolutions; i++) {
1622 if (tccp->csty & J2K_CP_CSTY_PRT) {
1623 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
1624 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
1627 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1628 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
1635 static void j2k_write_cod(opj_j2k_t *j2k) {
1636 opj_cp_t *cp = NULL;
1637 opj_tcp_t *tcp = NULL;
1640 opj_cio_t *cio = j2k->cio;
1642 cio_write(cio, J2K_MS_COD, 2); /* COD */
1644 lenp = cio_tell(cio);
1648 tcp = &cp->tcps[j2k->curtileno];
1650 cio_write(cio, tcp->csty, 1); /* Scod */
1651 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
1652 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
1653 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
1655 j2k_write_cox(j2k, 0);
1656 len = cio_tell(cio) - lenp;
1657 cio_seek(cio, lenp);
1658 cio_write(cio, len, 2); /* Lcod */
1659 cio_seek(cio, lenp + len);
1662 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
1666 static void j2k_read_cod(opj_j2k_t *j2k) {
1669 opj_cio_t *cio = j2k->cio;
1670 opj_cp_t *cp = j2k->cp;
1671 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1672 opj_image_t *image = j2k->image;
1674 len = cio_read(cio, 2); /* Lcod */
1675 tcp->csty = cio_read(cio, 1); /* Scod */
1676 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
1677 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
1678 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
1680 pos = cio_tell(cio);
1681 for (i = 0; i < image->numcomps; i++) {
1682 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
1684 j2k_read_cox(j2k, i);
1688 if (j2k->cstr_info) {
1689 opj_codestream_info_t *cstr_info = j2k->cstr_info;
1690 cstr_info->prog = tcp->prg;
1691 cstr_info->numlayers = tcp->numlayers;
1692 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
1693 for (i = 0; i < image->numcomps; i++) {
1694 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
1700 * Reads a COD marker (Coding Styke defaults)
1701 * @param p_header_data the data contained in the COD box.
1702 * @param p_j2k the jpeg2000 codec.
1703 * @param p_header_size the size of the data contained in the COD marker.
1704 * @param p_manager the user event manager.
1706 opj_bool j2k_read_cod_v2 (
1707 opj_j2k_v2_t *p_j2k,
1708 OPJ_BYTE * p_header_data,
1709 OPJ_UINT32 p_header_size,
1710 struct opj_event_mgr * p_manager
1716 opj_cp_v2_t *l_cp = 00;
1717 opj_tcp_v2_t *l_tcp = 00;
1718 opj_image_header_t *l_image = 00;
1721 assert(p_header_data != 00);
1722 assert(p_j2k != 00);
1723 assert(p_manager != 00);
1725 l_image = p_j2k->m_image_header;
1726 l_cp = &(p_j2k->m_cp);
1728 /* If we are in the first tile-part header of the current tile */
1729 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH)*/
1730 &l_cp->tcps[p_j2k->m_current_tile_number] :
1731 p_j2k->m_specific_param.m_decoder.m_default_tcp;
1733 /* Make sure room is sufficient */
1734 if (p_header_size < 5) {
1735 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
1739 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
1741 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
1743 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
1744 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
1747 // If user didn't set a number layer to decode take the max specify in the codestream.
1748 if (l_cp->m_specific_param.m_dec.m_layer) {
1749 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
1752 l_tcp->num_layers_to_decode = l_tcp->numlayers;
1755 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
1759 for (i = 0; i < l_image->numcomps; ++i) {
1760 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
1763 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
1764 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
1768 if (p_header_size != 0) {
1769 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
1773 j2k_copy_tile_component_parameters(p_j2k);
1776 if (p_j2k->cstr_info) {
1777 //opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;
1778 p_j2k->cstr_info->prog = l_tcp->prg;
1779 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
1780 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
1781 for (i = 0; i < l_image->numcomps; ++i) {
1782 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
1788 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
1791 opj_cp_t *cp = j2k->cp;
1792 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1793 opj_image_t *image = j2k->image;
1794 opj_cio_t *cio = j2k->cio;
1796 cio_write(cio, J2K_MS_COC, 2); /* COC */
1797 lenp = cio_tell(cio);
1799 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
1800 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
1801 j2k_write_cox(j2k, compno);
1802 len = cio_tell(cio) - lenp;
1803 cio_seek(cio, lenp);
1804 cio_write(cio, len, 2); /* Lcoc */
1805 cio_seek(cio, lenp + len);
1808 static void j2k_read_coc(opj_j2k_t *j2k) {
1811 opj_cp_t *cp = j2k->cp;
1812 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1813 opj_image_t *image = j2k->image;
1814 opj_cio_t *cio = j2k->cio;
1816 len = cio_read(cio, 2); /* Lcoc */
1817 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
1818 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
1819 j2k_read_cox(j2k, compno);
1823 * Reads a COC marker (Coding Style Component)
1824 * @param p_header_data the data contained in the COC box.
1825 * @param p_j2k the jpeg2000 codec.
1826 * @param p_header_size the size of the data contained in the COC marker.
1827 * @param p_manager the user event manager.
1829 opj_bool j2k_read_coc_v2 (
1830 opj_j2k_v2_t *p_j2k,
1831 OPJ_BYTE * p_header_data,
1832 OPJ_UINT32 p_header_size,
1833 struct opj_event_mgr * p_manager
1836 opj_cp_v2_t *l_cp = NULL;
1837 opj_tcp_v2_t *l_tcp = NULL;
1838 opj_image_header_t *l_image = NULL;
1839 OPJ_UINT32 l_comp_room;
1840 OPJ_UINT32 l_comp_no;
1843 assert(p_header_data != 00);
1844 assert(p_j2k != 00);
1845 assert(p_manager != 00);
1847 l_cp = &(p_j2k->m_cp);
1848 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
1849 &l_cp->tcps[p_j2k->m_current_tile_number] :
1850 p_j2k->m_specific_param.m_decoder.m_default_tcp;
1851 l_image = p_j2k->m_image_header;
1853 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
1855 // make sure room is sufficient
1856 if (p_header_size < l_comp_room + 1) {
1857 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
1860 p_header_size -= l_comp_room + 1;
1862 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
1863 p_header_data += l_comp_room;
1864 if (l_comp_no >= l_image->numcomps) {
1865 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
1869 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
1872 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
1873 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
1877 if (p_header_size != 0) {
1878 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
1884 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
1885 int bandno, numbands;
1888 opj_cp_t *cp = j2k->cp;
1889 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
1890 opj_tccp_t *tccp = &tcp->tccps[compno];
1891 opj_cio_t *cio = j2k->cio;
1893 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
1894 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
1896 for (bandno = 0; bandno < numbands; bandno++) {
1897 expn = tccp->stepsizes[bandno].expn;
1898 mant = tccp->stepsizes[bandno].mant;
1900 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1901 cio_write(cio, expn << 3, 1); /* SPqcx_i */
1903 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
1908 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
1910 int bandno, numbands;
1912 opj_cp_t *cp = j2k->cp;
1913 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
1914 opj_tccp_t *tccp = &tcp->tccps[compno];
1915 opj_cio_t *cio = j2k->cio;
1917 tmp = cio_read(cio, 1); /* Sqcx */
1918 tccp->qntsty = tmp & 0x1f;
1919 tccp->numgbits = tmp >> 5;
1920 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
1921 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
1924 if (j2k->cp->correct) {
1926 /* if JPWL is on, we check whether there are too many subbands */
1927 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
1928 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
1929 "JPWL: bad number of subbands in Sqcx (%d)\n",
1932 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
1935 /* we try to correct */
1937 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
1938 "- setting number of bands to %d => HYPOTHESIS!!!\n",
1945 /* We check whether there are too many subbands */
1946 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
1947 opj_event_msg(j2k->cinfo, EVT_WARNING ,
1948 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
1949 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
1952 #endif /* USE_JPWL */
1954 for (bandno = 0; bandno < numbands; bandno++) {
1956 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
1957 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
1960 tmp = cio_read(cio, 2); /* SPqcx_i */
1964 if (bandno < J2K_MAXBANDS){
1965 tccp->stepsizes[bandno].expn = expn;
1966 tccp->stepsizes[bandno].mant = mant;
1970 /* Add Antonin : if scalar_derived -> compute other stepsizes */
1971 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
1972 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
1973 tccp->stepsizes[bandno].expn =
1974 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
1975 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
1976 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
1982 static void j2k_write_qcd(opj_j2k_t *j2k) {
1985 opj_cio_t *cio = j2k->cio;
1987 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
1988 lenp = cio_tell(cio);
1990 j2k_write_qcx(j2k, 0);
1991 len = cio_tell(cio) - lenp;
1992 cio_seek(cio, lenp);
1993 cio_write(cio, len, 2); /* Lqcd */
1994 cio_seek(cio, lenp + len);
1997 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
2000 static void j2k_read_qcd(opj_j2k_t *j2k) {
2003 opj_cio_t *cio = j2k->cio;
2004 opj_image_t *image = j2k->image;
2006 len = cio_read(cio, 2); /* Lqcd */
2007 pos = cio_tell(cio);
2008 for (i = 0; i < image->numcomps; i++) {
2010 j2k_read_qcx(j2k, i, len - 2);
2015 * Reads a QCD marker (Quantization defaults)
2016 * @param p_header_data the data contained in the QCD box.
2017 * @param p_j2k the jpeg2000 codec.
2018 * @param p_header_size the size of the data contained in the QCD marker.
2019 * @param p_manager the user event manager.
2021 opj_bool j2k_read_qcd_v2 (
2022 opj_j2k_v2_t *p_j2k,
2023 OPJ_BYTE * p_header_data,
2024 OPJ_UINT32 p_header_size,
2025 struct opj_event_mgr * p_manager
2029 assert(p_header_data != 00);
2030 assert(p_j2k != 00);
2031 assert(p_manager != 00);
2033 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2034 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2037 if (p_header_size != 0) {
2038 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2041 j2k_copy_tile_quantization_parameters(p_j2k);
2046 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
2049 opj_cio_t *cio = j2k->cio;
2051 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
2052 lenp = cio_tell(cio);
2054 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
2055 j2k_write_qcx(j2k, compno);
2056 len = cio_tell(cio) - lenp;
2057 cio_seek(cio, lenp);
2058 cio_write(cio, len, 2); /* Lqcc */
2059 cio_seek(cio, lenp + len);
2062 static void j2k_read_qcc(opj_j2k_t *j2k) {
2064 int numcomp = j2k->image->numcomps;
2065 opj_cio_t *cio = j2k->cio;
2067 len = cio_read(cio, 2); /* Lqcc */
2068 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
2071 if (j2k->cp->correct) {
2073 static int backup_compno = 0;
2075 /* compno is negative or larger than the number of components!!! */
2076 if ((compno < 0) || (compno >= numcomp)) {
2077 opj_event_msg(j2k->cinfo, EVT_ERROR,
2078 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2081 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2084 /* we try to correct */
2085 compno = backup_compno % numcomp;
2086 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
2087 "- setting component number to %d\n",
2091 /* keep your private count of tiles */
2094 #endif /* USE_JPWL */
2096 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
2100 * Reads a QCC marker (Quantization component)
2101 * @param p_header_data the data contained in the QCC box.
2102 * @param p_j2k the jpeg2000 codec.
2103 * @param p_header_size the size of the data contained in the QCC marker.
2104 * @param p_manager the user event manager.
2106 opj_bool j2k_read_qcc_v2(
2107 opj_j2k_v2_t *p_j2k,
2108 OPJ_BYTE * p_header_data,
2109 OPJ_UINT32 p_header_size,
2110 struct opj_event_mgr * p_manager)
2112 OPJ_UINT32 l_num_comp,l_comp_no;
2115 assert(p_header_data != 00);
2116 assert(p_j2k != 00);
2117 assert(p_manager != 00);
2119 l_num_comp = p_j2k->m_image_header->numcomps;
2122 if (p_j2k->m_cp->correct) {
2124 static OPJ_UINT32 backup_compno = 0;
2126 /* compno is negative or larger than the number of components!!! */
2127 if ((compno < 0) || (compno >= numcomp)) {
2128 opj_event_msg(p_j2k->cinfo, EVT_ERROR,
2129 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
2132 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2135 /* we try to correct */
2136 compno = backup_compno % numcomp;
2137 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
2138 "- setting component number to %d\n",
2142 /* keep your private count of tiles */
2145 #endif /* USE_JPWL */
2147 if (l_num_comp <= 256) {
2148 if (p_header_size < 1) {
2149 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2152 opj_read_bytes(p_header_data,&l_comp_no,1);
2157 if (p_header_size < 2) {
2158 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2161 opj_read_bytes(p_header_data,&l_comp_no,2);
2166 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2167 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2171 if (p_header_size != 0) {
2172 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
2180 static void j2k_write_poc(opj_j2k_t *j2k) {
2181 int len, numpchgs, i;
2183 int numcomps = j2k->image->numcomps;
2185 opj_cp_t *cp = j2k->cp;
2186 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2187 opj_tccp_t *tccp = &tcp->tccps[0];
2188 opj_cio_t *cio = j2k->cio;
2190 numpchgs = 1 + tcp->numpocs;
2191 cio_write(cio, J2K_MS_POC, 2); /* POC */
2192 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
2193 cio_write(cio, len, 2); /* Lpoc */
2194 for (i = 0; i < numpchgs; i++) {
2195 opj_poc_t *poc = &tcp->pocs[i];
2196 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
2197 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
2198 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
2199 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
2200 cio_write(cio, poc->resno1, 1); /* REpoc_i */
2201 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
2202 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
2203 poc->compno1 = int_min(poc->compno1, numcomps);
2204 cio_write(cio, poc->prg, 1); /* Ppoc_i */
2208 static void j2k_read_poc(opj_j2k_t *j2k) {
2209 int len, numpchgs, i, old_poc;
2211 int numcomps = j2k->image->numcomps;
2213 opj_cp_t *cp = j2k->cp;
2214 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2215 opj_cio_t *cio = j2k->cio;
2217 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
2219 len = cio_read(cio, 2); /* Lpoc */
2220 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
2222 for (i = old_poc; i < numpchgs + old_poc; i++) {
2224 poc = &tcp->pocs[i];
2225 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
2226 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
2227 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
2228 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
2229 poc->compno1 = int_min(
2230 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
2231 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
2234 tcp->numpocs = numpchgs + old_poc - 1;
2238 * Reads a POC marker (Progression Order Change)
2240 * @param p_header_data the data contained in the POC box.
2241 * @param p_j2k the jpeg2000 codec.
2242 * @param p_header_size the size of the data contained in the POC marker.
2243 * @param p_manager the user event manager.
2245 opj_bool j2k_read_poc_v2 (
2246 opj_j2k_v2_t *p_j2k,
2247 OPJ_BYTE * p_header_data,
2248 OPJ_UINT32 p_header_size,
2249 struct opj_event_mgr * p_manager)
2251 OPJ_UINT32 i, l_nb_comp, l_tmp;
2252 opj_image_header_t * l_image = 00;
2253 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
2254 OPJ_UINT32 l_chunk_size, l_comp_room;
2256 opj_cp_v2_t *l_cp = 00;
2257 opj_tcp_v2_t *l_tcp = 00;
2258 opj_poc_t *l_current_poc = 00;
2261 assert(p_header_data != 00);
2262 assert(p_j2k != 00);
2263 assert(p_manager != 00);
2265 l_image = p_j2k->m_image_header;
2266 l_nb_comp = l_image->numcomps;
2267 if (l_nb_comp <= 256) {
2273 l_chunk_size = 5 + 2 * l_comp_room;
2274 l_current_poc_nb = p_header_size / l_chunk_size;
2275 l_current_poc_remaining = p_header_size % l_chunk_size;
2277 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
2278 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
2282 l_cp = &(p_j2k->m_cp);
2283 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
2284 &l_cp->tcps[p_j2k->m_current_tile_number] :
2285 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2286 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
2287 l_current_poc_nb += l_old_poc_nb;
2289 assert(l_current_poc_nb < 32);
2291 /* now poc is in use.*/
2294 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
2295 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
2296 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
2298 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
2299 p_header_data+=l_comp_room;
2300 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
2302 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
2304 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
2305 p_header_data+=l_comp_room;
2306 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
2308 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
2309 /* make sure comp is in acceptable bounds */
2310 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
2314 l_tcp->numpocs = l_current_poc_nb - 1;
2318 static void j2k_read_crg(opj_j2k_t *j2k) {
2319 int len, i, Xcrg_i, Ycrg_i;
2321 opj_cio_t *cio = j2k->cio;
2322 int numcomps = j2k->image->numcomps;
2324 len = cio_read(cio, 2); /* Lcrg */
2325 for (i = 0; i < numcomps; i++) {
2326 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
2327 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
2332 * Reads a CRG marker (Component registration)
2334 * @param p_header_data the data contained in the TLM box.
2335 * @param p_j2k the jpeg2000 codec.
2336 * @param p_header_size the size of the data contained in the TLM marker.
2337 * @param p_manager the user event manager.
2339 opj_bool j2k_read_crg_v2 (
2340 opj_j2k_v2_t *p_j2k,
2341 OPJ_BYTE * p_header_data,
2342 OPJ_UINT32 p_header_size,
2343 struct opj_event_mgr * p_manager
2346 OPJ_UINT32 l_nb_comp;
2348 assert(p_header_data != 00);
2349 assert(p_j2k != 00);
2350 assert(p_manager != 00);
2352 l_nb_comp = p_j2k->m_image_header->numcomps;
2354 if (p_header_size != l_nb_comp *4) {
2355 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
2358 /* Do not care of this at the moment since only local variables are set here */
2361 (i = 0; i < l_nb_comp; ++i)
2363 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
2365 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
2372 static void j2k_read_tlm(opj_j2k_t *j2k) {
2373 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
2374 long int Ttlm_i, Ptlm_i;
2376 opj_cio_t *cio = j2k->cio;
2378 len = cio_read(cio, 2); /* Ltlm */
2379 Ztlm = cio_read(cio, 1); /* Ztlm */
2380 Stlm = cio_read(cio, 1); /* Stlm */
2381 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
2382 SP = (Stlm >> 6) & 0x01;
2383 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
2384 for (i = 0; i < tile_tlm; i++) {
2385 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
2386 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
2391 * Reads a TLM marker (Tile Length Marker)
2393 * @param p_header_data the data contained in the TLM box.
2394 * @param p_j2k the jpeg2000 codec.
2395 * @param p_header_size the size of the data contained in the TLM marker.
2396 * @param p_manager the user event manager.
2398 opj_bool j2k_read_tlm_v2 (
2399 opj_j2k_v2_t *p_j2k,
2400 OPJ_BYTE * p_header_data,
2401 OPJ_UINT32 p_header_size,
2402 struct opj_event_mgr * p_manager
2405 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
2407 assert(p_header_data != 00);
2408 assert(p_j2k != 00);
2409 assert(p_manager != 00);
2411 if (p_header_size < 2) {
2412 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2417 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
2419 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
2422 l_ST = ((l_Stlm >> 4) & 0x3);
2423 l_SP = (l_Stlm >> 6) & 0x1;
2425 l_Ptlm_size = (l_SP + 1) * 2;
2426 l_quotient = l_Ptlm_size + l_ST;
2428 l_tot_num_tp = p_header_size / l_quotient;
2429 l_tot_num_tp_remaining = p_header_size % l_quotient;
2431 if (l_tot_num_tp_remaining != 0) {
2432 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
2435 /* FIXME Do not care of this at the moment since only local variables are set here */
2438 (i = 0; i < l_tot_num_tp; ++i)
2440 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
2441 p_header_data += l_ST;
2442 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
2443 p_header_data += l_Ptlm_size;
2448 static void j2k_read_plm(opj_j2k_t *j2k) {
2449 int len, i, Zplm, Nplm, add, packet_len = 0;
2451 opj_cio_t *cio = j2k->cio;
2453 len = cio_read(cio, 2); /* Lplm */
2454 Zplm = cio_read(cio, 1); /* Zplm */
2457 Nplm = cio_read(cio, 4); /* Nplm */
2459 for (i = Nplm; i > 0; i--) {
2460 add = cio_read(cio, 1);
2462 packet_len = (packet_len << 7) + add; /* Iplm_ij */
2463 if ((add & 0x80) == 0) {
2474 * Reads a PLM marker (Packet length, main header marker)
2476 * @param p_header_data the data contained in the TLM box.
2477 * @param p_j2k the jpeg2000 codec.
2478 * @param p_header_size the size of the data contained in the TLM marker.
2479 * @param p_manager the user event manager.
2481 opj_bool j2k_read_plm_v2 (
2482 opj_j2k_v2_t *p_j2k,
2483 OPJ_BYTE * p_header_data,
2484 OPJ_UINT32 p_header_size,
2485 struct opj_event_mgr * p_manager
2489 assert(p_header_data != 00);
2490 assert(p_j2k != 00);
2491 assert(p_manager != 00);
2493 if (p_header_size < 1) {
2494 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2497 /* Do not care of this at the moment since only local variables are set here */
2499 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
2506 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
2508 p_header_size -= (1+l_Nplm);
2512 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2516 (i = 0; i < l_Nplm; ++i)
2518 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
2520 // take only the last seven bytes
2521 l_packet_len |= (l_tmp & 0x7f);
2529 // store packet length and proceed to next packet
2536 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2544 static void j2k_read_plt(opj_j2k_t *j2k) {
2545 int len, i, Zplt, packet_len = 0, add;
2547 opj_cio_t *cio = j2k->cio;
2549 len = cio_read(cio, 2); /* Lplt */
2550 Zplt = cio_read(cio, 1); /* Zplt */
2551 for (i = len - 3; i > 0; i--) {
2552 add = cio_read(cio, 1);
2553 packet_len = (packet_len << 7) + add; /* Iplt_i */
2554 if ((add & 0x80) == 0) {
2562 * Reads a PLT marker (Packet length, tile-part header)
2564 * @param p_header_data the data contained in the PLT box.
2565 * @param p_j2k the jpeg2000 codec.
2566 * @param p_header_size the size of the data contained in the PLT marker.
2567 * @param p_manager the user event manager.
2569 opj_bool j2k_read_plt_v2 (
2570 opj_j2k_v2_t *p_j2k,
2571 OPJ_BYTE * p_header_data,
2572 OPJ_UINT32 p_header_size,
2573 struct opj_event_mgr * p_manager
2576 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
2579 assert(p_header_data != 00);
2580 assert(p_j2k != 00);
2581 assert(p_manager != 00);
2583 if (p_header_size < 1) {
2584 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2588 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
2592 for (i = 0; i < p_header_size; ++i) {
2593 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
2595 // take only the last seven bytes
2596 l_packet_len |= (l_tmp & 0x7f);
2601 // store packet length and proceed to next packet
2606 if (l_packet_len != 0) {
2607 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
2614 static void j2k_read_ppm(opj_j2k_t *j2k) {
2615 int len, Z_ppm, i, j;
2618 opj_cp_t *cp = j2k->cp;
2619 opj_cio_t *cio = j2k->cio;
2621 len = cio_read(cio, 2);
2624 Z_ppm = cio_read(cio, 1); /* Z_ppm */
2627 if (cp->ppm_previous == 0) {
2628 N_ppm = cio_read(cio, 4); /* N_ppm */
2631 N_ppm = cp->ppm_previous;
2634 if (Z_ppm == 0) { /* First PPM marker */
2635 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
2636 cp->ppm_data_first = cp->ppm_data;
2637 cp->ppm_len = N_ppm;
2638 } else { /* NON-first PPM marker */
2639 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
2642 /* this memory allocation check could be done even in non-JPWL cases */
2644 if (!cp->ppm_data) {
2645 opj_event_msg(j2k->cinfo, EVT_ERROR,
2646 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
2648 if (!JPWL_ASSUME || JPWL_ASSUME) {
2649 opj_free(cp->ppm_data);
2650 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2657 cp->ppm_data_first = cp->ppm_data;
2658 cp->ppm_len = N_ppm + cp->ppm_store;
2660 for (i = N_ppm; i > 0; i--) { /* Read packet header */
2661 cp->ppm_data[j] = cio_read(cio, 1);
2665 break; /* Case of non-finished packet header in present marker but finished in next one */
2667 cp->ppm_previous = i - 1;
2672 * Reads a PPM marker (Packed packet headers, main header)
2674 * @param p_header_data the data contained in the POC box.
2675 * @param p_j2k the jpeg2000 codec.
2676 * @param p_header_size the size of the data contained in the POC marker.
2677 * @param p_manager the user event manager.
2679 opj_bool j2k_read_ppm_v2 (
2680 opj_j2k_v2_t *p_j2k,
2681 OPJ_BYTE * p_header_data,
2682 OPJ_UINT32 p_header_size,
2683 struct opj_event_mgr * p_manager
2687 opj_cp_v2_t *l_cp = 00;
2688 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
2691 assert(p_header_data != 00);
2692 assert(p_j2k != 00);
2693 assert(p_manager != 00);
2695 if (p_header_size < 1) {
2696 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2700 l_cp = &(p_j2k->m_cp);
2703 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
2709 if (p_header_size < 4) {
2710 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2714 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
2718 /* First PPM marker: Initialization */
2719 l_cp->ppm_len = l_N_ppm;
2720 l_cp->ppm_data_size = 0;
2722 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
2723 if (l_cp->ppm_buffer == 00) {
2724 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2727 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
2729 l_cp->ppm_data = l_cp->ppm_buffer;
2733 if (l_cp->ppm_data_size == l_cp->ppm_len) {
2734 if (p_header_size >= 4) {
2736 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
2739 l_cp->ppm_len += l_N_ppm ;
2741 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
2742 if (l_cp->ppm_buffer == 00) {
2743 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2746 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
2748 l_cp->ppm_data = l_cp->ppm_buffer;
2755 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
2757 if (l_remaining_data <= p_header_size) {
2758 /* we must store less information than available in the packet */
2759 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
2760 l_cp->ppm_data_size = l_cp->ppm_len;
2761 p_header_size -= l_remaining_data;
2762 p_header_data += l_remaining_data;
2765 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
2766 l_cp->ppm_data_size += p_header_size;
2767 p_header_data += p_header_size;
2779 * Reads a PPM marker (Packed packet headers, main header)
2781 * @param p_header_data the data contained in the POC box.
2782 * @param p_j2k the jpeg2000 codec.
2783 * @param p_header_size the size of the data contained in the POC marker.
2784 * @param p_manager the user event manager.
2786 opj_bool j2k_read_ppm_v3 (
2787 opj_j2k_v2_t *p_j2k,
2788 OPJ_BYTE * p_header_data,
2789 OPJ_UINT32 p_header_size,
2790 struct opj_event_mgr * p_manager
2793 opj_cp_v2_t *l_cp = 00;
2794 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
2797 assert(p_header_data != 00);
2798 assert(p_j2k != 00);
2799 assert(p_manager != 00);
2801 // Minimum size of PPM marker is equal to the size of Zppm element
2802 if (p_header_size < 1) {
2803 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2807 l_cp = &(p_j2k->m_cp);
2810 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
2816 // We need now at least the Nppm^0 element
2817 if (p_header_size < 4) {
2818 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
2822 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
2826 /* First PPM marker: Initialization */
2827 l_cp->ppm_len = l_N_ppm;
2828 l_cp->ppm_data_read = 0;
2830 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
2831 if (l_cp->ppm_data == 00) {
2832 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2835 memset(l_cp->ppm_data,0,l_cp->ppm_len);
2837 l_cp->ppm_data_current = l_cp->ppm_data;
2839 //l_cp->ppm_data = l_cp->ppm_buffer;
2842 if (p_header_size < 4) {
2843 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
2847 // Uncompleted Ippm series in the previous PPM marker?
2848 if (l_cp->ppm_data_read < l_cp->ppm_len) {
2849 // Get the place where add the remaining Ippm series
2850 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
2851 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
2854 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
2858 // Increase the size of ppm_data to add the new Ippm series
2859 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
2861 // Keep the position of the place where concatenate the new series
2862 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
2863 l_cp->ppm_len += l_N_ppm;
2868 l_remaining_data = p_header_size;
2870 while (l_remaining_data >= l_N_ppm) {
2871 // read a complete Ippm series
2872 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
2873 p_header_size -= l_N_ppm;
2874 p_header_data += l_N_ppm;
2876 l_cp->ppm_data_read += l_N_ppm; // Increase the number of data read
2880 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
2885 l_remaining_data = p_header_size;
2889 l_remaining_data = p_header_size;
2891 // Next Ippm series is a complete series ?
2892 if (l_remaining_data > l_N_ppm) {
2893 // Increase the size of ppm_data to add the new Ippm series
2894 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
2896 // Keep the position of the place where concatenate the new series
2897 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
2898 l_cp->ppm_len += l_N_ppm;
2903 // Need to read an incomplete Ippm series
2904 if (l_remaining_data) {
2905 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
2907 // Keep the position of the place where concatenate the new series
2908 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
2909 l_cp->ppm_len += l_N_ppm;
2911 // Read incomplete Ippm series
2912 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
2913 p_header_size -= l_remaining_data;
2914 p_header_data += l_remaining_data;
2916 l_cp->ppm_data_read += l_remaining_data; // Increase the number of data read
2921 if (l_cp->ppm_data_size == l_cp->ppm_len) {
2922 if (p_header_size >= 4) {
2924 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
2927 l_cp->ppm_len += l_N_ppm ;
2929 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
2930 if (l_cp->ppm_buffer == 00) {
2931 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
2934 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
2936 l_cp->ppm_data = l_cp->ppm_buffer;
2943 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
2945 if (l_remaining_data <= p_header_size) {
2946 /* we must store less information than available in the packet */
2947 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
2948 l_cp->ppm_data_size = l_cp->ppm_len;
2949 p_header_size -= l_remaining_data;
2950 p_header_data += l_remaining_data;
2953 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
2954 l_cp->ppm_data_size += p_header_size;
2955 p_header_data += p_header_size;
2964 static void j2k_read_ppt(opj_j2k_t *j2k) {
2965 int len, Z_ppt, i, j = 0;
2967 opj_cp_t *cp = j2k->cp;
2968 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
2969 opj_cio_t *cio = j2k->cio;
2971 len = cio_read(cio, 2);
2972 Z_ppt = cio_read(cio, 1);
2974 if (Z_ppt == 0) { /* First PPT marker */
2975 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
2976 tcp->ppt_data_first = tcp->ppt_data;
2978 tcp->ppt_len = len - 3;
2979 } else { /* NON-first PPT marker */
2980 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
2981 tcp->ppt_data_first = tcp->ppt_data;
2982 tcp->ppt_len = len - 3 + tcp->ppt_store;
2985 for (i = len - 3; i > 0; i--) {
2986 tcp->ppt_data[j] = cio_read(cio, 1);
2993 * Reads a PPT marker (Packed packet headers, tile-part header)
2995 * @param p_header_data the data contained in the PPT box.
2996 * @param p_j2k the jpeg2000 codec.
2997 * @param p_header_size the size of the data contained in the PPT marker.
2998 * @param p_manager the user event manager.
3000 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
3001 OPJ_BYTE * p_header_data,
3002 OPJ_UINT32 p_header_size,
3003 struct opj_event_mgr * p_manager )
3005 opj_cp_v2_t *l_cp = 00;
3006 opj_tcp_v2_t *l_tcp = 00;
3010 assert(p_header_data != 00);
3011 assert(p_j2k != 00);
3012 assert(p_manager != 00);
3014 /* We need to have the Z_ppt element at minimum */
3015 if (p_header_size < 1) {
3016 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3020 l_cp = &(p_j2k->m_cp);
3022 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");
3026 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3029 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3033 /* Allocate buffer to read the packet header */
3035 /* First PPT marker */
3036 l_tcp->ppt_data_size = 0;
3037 l_tcp->ppt_len = p_header_size;
3039 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3040 if (l_tcp->ppt_buffer == 00) {
3041 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3044 l_tcp->ppt_data = l_tcp->ppt_buffer;
3046 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3049 l_tcp->ppt_len += p_header_size;
3051 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3052 if (l_tcp->ppt_buffer == 00) {
3053 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3056 l_tcp->ppt_data = l_tcp->ppt_buffer;
3058 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3061 /* Read packet header from buffer */
3062 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3064 l_tcp->ppt_data_size += p_header_size;
3069 static void j2k_write_tlm(opj_j2k_t *j2k){
3071 opj_cio_t *cio = j2k->cio;
3072 j2k->tlm_start = cio_tell(cio);
3073 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
3074 lenp = 4 + (5*j2k->totnum_tp);
3075 cio_write(cio,lenp,2); /* Ltlm */
3076 cio_write(cio, 0,1); /* Ztlm=0*/
3077 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3078 cio_skip(cio,5*j2k->totnum_tp);
3081 static void j2k_write_sot(opj_j2k_t *j2k) {
3084 opj_cio_t *cio = j2k->cio;
3086 j2k->sot_start = cio_tell(cio);
3087 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
3088 lenp = cio_tell(cio);
3089 cio_skip(cio, 2); /* Lsot (further) */
3090 cio_write(cio, j2k->curtileno, 2); /* Isot */
3091 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
3092 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
3093 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
3094 len = cio_tell(cio) - lenp;
3095 cio_seek(cio, lenp);
3096 cio_write(cio, len, 2); /* Lsot */
3097 cio_seek(cio, lenp + len);
3101 /* update markers struct */
3102 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
3103 #endif /* USE_JPWL */
3106 if( j2k->cstr_info && j2k->cur_tp_num==0){
3107 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
3111 static void j2k_read_sot(opj_j2k_t *j2k) {
3112 int len, tileno, totlen, partno, numparts, i;
3113 opj_tcp_t *tcp = NULL;
3116 opj_cp_t *cp = j2k->cp;
3117 opj_cio_t *cio = j2k->cio;
3119 len = cio_read(cio, 2);
3120 tileno = cio_read(cio, 2);
3123 if (j2k->cp->correct) {
3125 static int backup_tileno = 0;
3127 /* tileno is negative or larger than the number of tiles!!! */
3128 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3129 opj_event_msg(j2k->cinfo, EVT_ERROR,
3130 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3131 tileno, (cp->tw * cp->th));
3133 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3136 /* we try to correct */
3137 tileno = backup_tileno;
3138 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3139 "- setting tile number to %d\n",
3143 /* keep your private count of tiles */
3146 #endif /* USE_JPWL */
3148 if (cp->tileno_size == 0) {
3149 cp->tileno[cp->tileno_size] = tileno;
3153 while (i < cp->tileno_size && status == 0) {
3154 status = cp->tileno[i] == tileno ? 1 : 0;
3158 cp->tileno[cp->tileno_size] = tileno;
3163 totlen = cio_read(cio, 4);
3166 if (j2k->cp->correct) {
3168 /* totlen is negative or larger than the bytes left!!! */
3169 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
3170 opj_event_msg(j2k->cinfo, EVT_ERROR,
3171 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3172 totlen, cio_numbytesleft(cio) + 8);
3174 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3177 /* we try to correct */
3179 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3180 "- setting Psot to %d => assuming it is the last tile\n",
3185 #endif /* USE_JPWL */
3188 totlen = cio_numbytesleft(cio) + 8;
3190 partno = cio_read(cio, 1);
3191 numparts = cio_read(cio, 1);
3193 if (partno >= numparts) {
3194 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);
3195 numparts = partno+1;
3198 j2k->curtileno = tileno;
3199 j2k->cur_tp_num = partno;
3200 j2k->eot = cio_getbp(cio) - 12 + totlen;
3201 j2k->state = J2K_STATE_TPH;
3202 tcp = &cp->tcps[j2k->curtileno];
3205 if (j2k->cstr_info) {
3208 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
3209 j2k->cstr_info->tile[tileno].tileno = tileno;
3210 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
3211 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3213 j2k->cstr_info->tile[tileno].end_pos += totlen;
3215 j2k->cstr_info->tile[tileno].num_tps = numparts;
3217 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
3219 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)
3220 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
3221 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3222 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3225 if (tcp->first == 1) {
3226 /* Initialization PPT */
3227 opj_tccp_t *tmp = tcp->tccps;
3228 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
3230 tcp->ppt_data = NULL;
3231 tcp->ppt_data_first = NULL;
3234 for (i = 0; i < j2k->image->numcomps; i++) {
3235 tcp->tccps[i] = j2k->default_tcp->tccps[i];
3237 cp->tcps[j2k->curtileno].first = 0;
3242 * Reads a PPT marker (Packed packet headers, tile-part header)
3244 * @param p_header_data the data contained in the PPT box.
3245 * @param p_j2k the jpeg2000 codec.
3246 * @param p_header_size the size of the data contained in the PPT marker.
3247 * @param p_manager the user event manager.
3249 opj_bool j2k_read_sot_v2 (
3250 opj_j2k_v2_t *p_j2k,
3251 OPJ_BYTE * p_header_data,
3252 OPJ_UINT32 p_header_size,
3253 struct opj_event_mgr * p_manager
3257 opj_cp_v2_t *l_cp = 00;
3258 opj_tcp_v2_t *l_tcp = 00;
3259 OPJ_UINT32 l_tot_len, l_num_parts = 0;
3260 OPJ_UINT32 l_current_part;
3261 OPJ_UINT32 l_tile_x,l_tile_y;
3264 assert(p_header_data != 00);
3265 assert(p_j2k != 00);
3266 assert(p_manager != 00);
3268 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
3269 if (p_header_size != 8) {
3270 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3274 l_cp = &(p_j2k->m_cp);
3275 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
3278 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3279 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
3280 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
3283 if (p_j2k->m_cp->correct) {
3285 static int backup_tileno = 0;
3287 /* tileno is negative or larger than the number of tiles!!! */
3288 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3289 opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3290 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3291 tileno, (cp->tw * cp->th));
3293 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3296 /* we try to correct */
3297 tileno = backup_tileno;
3298 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3299 "- setting tile number to %d\n",
3303 /* keep your private count of tiles */
3306 #endif /* USE_JPWL */
3308 /* look for the tile in the list of already processed tile (in parts). */
3309 /* Optimization possible here with a more complex data structure and with the removing of tiles */
3310 /* since the time taken by this function can only grow at the time */
3312 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
3316 if (p_j2k->m_cp->correct) {
3318 /* totlen is negative or larger than the bytes left!!! */
3319 if ((totlen < 0) || (totlen > (p_stream_numbytesleft(p_stream) + 8))) {
3320 opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3321 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3322 totlen, p_stream_numbytesleft(p_stream) + 8);
3324 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3327 /* we try to correct */
3329 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3330 "- setting Psot to %d => assuming it is the last tile\n",
3334 #endif /* USE_JPWL */
3337 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot read data with no size known, giving up\n");
3341 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
3344 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
3347 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
3348 l_tcp->m_nb_tile_parts = l_num_parts;
3351 /* If know the number of tile part header we will check if we didn't read the last*/
3352 if (l_tcp->m_nb_tile_parts) {
3353 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
3354 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
3358 /* Keep the size of data to skip after this marker */
3359 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
3360 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
3362 /* Check if the current tile is outside the area we want decode (in tile index)*/
3363 p_j2k->m_specific_param.m_decoder.m_skip_data =
3364 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
3365 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
3366 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
3367 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
3371 /* TODO move this onto a separate method to call before reading any SOT */
3372 /* if (p_j2k->cstr_info) {
3375 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
3378 p_j2k->cstr_info->tile[tileno].tileno = tileno;
3379 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
3380 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3381 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
3384 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
3387 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
3391 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
3394 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
3395 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3396 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3401 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
3404 opj_tcp_t *tcp = NULL;
3405 opj_codestream_info_t *cstr_info = NULL;
3407 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
3408 opj_cp_t *cp = j2k->cp;
3409 opj_cio_t *cio = j2k->cio;
3411 tcd->tp_num = j2k->tp_num ;
3412 tcd->cur_tp_num = j2k->cur_tp_num;
3414 cio_write(cio, J2K_MS_SOD, 2);
3416 if( j2k->cstr_info && j2k->cur_tp_num==0){
3417 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
3420 if (j2k->curtileno == 0) {
3421 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
3425 cstr_info = j2k->cstr_info;
3427 if (!j2k->cur_tp_num ) {
3428 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3429 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
3432 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
3433 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
3437 /* update markers struct */
3438 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
3439 #endif /* USE_JPWL */
3444 tcp = &cp->tcps[j2k->curtileno];
3445 for (layno = 0; layno < tcp->numlayers; layno++) {
3446 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
3447 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
3448 } else if (tcp->rates[layno]) {
3449 tcp->rates[layno]=1;
3452 if(j2k->cur_tp_num == 0){
3453 tcd->tcd_image->tiles->packno = 0;
3455 cstr_info->packno = 0;
3458 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
3460 /* Writing Psot in SOT marker */
3461 totlen = cio_tell(cio) + l - j2k->sot_start;
3462 cio_seek(cio, j2k->sot_start + 6);
3463 cio_write(cio, totlen, 4);
3464 cio_seek(cio, j2k->sot_start + totlen);
3465 /* Writing Ttlm and Ptlm in TLM marker */
3467 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
3468 cio_write(cio, j2k->curtileno, 1);
3469 cio_write(cio, totlen, 4);
3471 cio_seek(cio, j2k->sot_start + totlen);
3474 static void j2k_read_sod(opj_j2k_t *j2k) {
3475 int len, truncate = 0, i;
3476 unsigned char *data = NULL, *data_ptr = NULL;
3478 opj_cio_t *cio = j2k->cio;
3479 int curtileno = j2k->curtileno;
3482 if (j2k->cstr_info) {
3483 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
3484 cio_tell(cio) + j2k->pos_correction - 1;
3485 if (j2k->cur_tp_num == 0)
3486 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3487 j2k->cstr_info->packno = 0;
3490 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
3492 if (len == cio_numbytesleft(cio) + 1) {
3493 truncate = 1; /* Case of a truncate codestream */
3496 data = j2k->tile_data[curtileno];
3497 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
3499 data_ptr = data + j2k->tile_len[curtileno];
3500 for (i = 0; i < len; i++) {
3501 data_ptr[i] = cio_read(cio, 1);
3504 j2k->tile_len[curtileno] += len;
3505 j2k->tile_data[curtileno] = data;
3508 j2k->state = J2K_STATE_TPHSOT;
3510 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
3516 * Reads a SOD marker (Start Of Data)
3518 * @param p_header_data the data contained in the SOD box.
3519 * @param p_j2k the jpeg2000 codec.
3520 * @param p_header_size the size of the data contained in the SOD marker.
3521 * @param p_manager the user event manager.
3523 opj_bool j2k_read_sod_v2 (
3524 opj_j2k_v2_t *p_j2k,
3525 struct opj_stream_private *p_stream,
3526 struct opj_event_mgr * p_manager
3529 OPJ_UINT32 l_current_read_size;
3530 opj_codestream_info_v2_t * l_cstr_info = 00;
3531 OPJ_BYTE ** l_current_data = 00;
3532 opj_tcp_v2_t * l_tcp = 00;
3533 OPJ_UINT32 * l_tile_len = 00;
3536 assert(p_j2k != 00);
3537 assert(p_manager != 00);
3538 assert(p_stream != 00);
3540 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
3541 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
3543 l_current_data = &(l_tcp->m_data);
3544 l_tile_len = &l_tcp->m_data_size;
3546 if (! *l_current_data) {
3547 *l_current_data = (OPJ_BYTE*) opj_malloc/*FIXME V2 -> my_opj_malloc*/(p_j2k->m_specific_param.m_decoder.m_sot_length);
3550 *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);
3553 if (*l_current_data == 00) {
3554 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
3558 l_cstr_info = p_j2k->cstr_info;
3563 OPJ_SIZE_T l_current_pos = opj_stream_tell(p_stream)-1;
3564 l_cstr_info->tile[p_j2k->m_current_tile_number].tp[p_j2k->m_specific_param.m_encoder.m_current_tile_part_number].tp_end_header = l_current_pos;
3566 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
3567 l_cstr_info->tile[p_j2k->m_current_tile_number].end_header = l_current_pos;
3570 l_cstr_info->packno = 0;
3574 l_current_read_size = opj_stream_read_data( p_stream,
3575 *l_current_data + *l_tile_len,
3576 p_j2k->m_specific_param.m_decoder.m_sot_length,
3579 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
3580 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
3583 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
3586 *l_tile_len += l_current_read_size;
3592 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
3593 opj_cp_t *cp = j2k->cp;
3594 opj_tcp_t *tcp = &cp->tcps[tileno];
3595 opj_cio_t *cio = j2k->cio;
3596 int numcomps = j2k->image->numcomps;
3598 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
3599 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
3600 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
3601 cio_write(cio, 0, 1); /* Srgn */
3602 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
3605 static void j2k_read_rgn(opj_j2k_t *j2k) {
3606 int len, compno, roisty;
3608 opj_cp_t *cp = j2k->cp;
3609 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3610 opj_cio_t *cio = j2k->cio;
3611 int numcomps = j2k->image->numcomps;
3613 len = cio_read(cio, 2); /* Lrgn */
3614 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
3615 roisty = cio_read(cio, 1); /* Srgn */
3618 if (j2k->cp->correct) {
3619 /* totlen is negative or larger than the bytes left!!! */
3620 if (compno >= numcomps) {
3621 opj_event_msg(j2k->cinfo, EVT_ERROR,
3622 "JPWL: bad component number in RGN (%d when there are only %d)\n",
3624 if (!JPWL_ASSUME || JPWL_ASSUME) {
3625 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3630 #endif /* USE_JPWL */
3632 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
3635 static void j2k_write_eoc(opj_j2k_t *j2k) {
3636 opj_cio_t *cio = j2k->cio;
3637 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
3638 cio_write(cio, J2K_MS_EOC, 2);
3642 /* update markers struct */
3643 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
3644 #endif /* USE_JPWL */
3649 * Reads a RGN marker (Region Of Interest)
3651 * @param p_header_data the data contained in the POC box.
3652 * @param p_j2k the jpeg2000 codec.
3653 * @param p_header_size the size of the data contained in the POC marker.
3654 * @param p_manager the user event manager.
3656 opj_bool j2k_read_rgn_v2 (
3657 opj_j2k_v2_t *p_j2k,
3658 OPJ_BYTE * p_header_data,
3659 OPJ_UINT32 p_header_size,
3660 struct opj_event_mgr * p_manager
3663 OPJ_UINT32 l_nb_comp;
3664 opj_image_header_t * l_image = 00;
3666 opj_cp_v2_t *l_cp = 00;
3667 opj_tcp_v2_t *l_tcp = 00;
3668 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
3671 assert(p_header_data != 00);
3672 assert(p_j2k != 00);
3673 assert(p_manager != 00);
3675 l_image = p_j2k->m_image_header;
3676 l_nb_comp = l_image->numcomps;
3678 if (l_nb_comp <= 256) {
3683 if (p_header_size != 2 + l_comp_room) {
3684 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
3688 l_cp = &(p_j2k->m_cp);
3689 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH)*/
3690 &l_cp->tcps[p_j2k->m_current_tile_number] :
3691 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3693 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
3694 p_header_data+=l_comp_room;
3695 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
3699 if (p_j2k->m_cp->correct) {
3700 /* totlen is negative or larger than the bytes left!!! */
3701 if (compno >= numcomps) {
3702 opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3703 "JPWL: bad component number in RGN (%d when there are only %d)\n",
3705 if (!JPWL_ASSUME || JPWL_ASSUME) {
3706 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3711 #endif /* USE_JPWL */
3713 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
3720 static void j2k_read_eoc(opj_j2k_t *j2k) {
3724 /* if packets should be decoded */
3725 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
3726 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
3727 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
3728 for (i = 0; i < j2k->cp->tileno_size; i++) {
3729 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
3730 tileno = j2k->cp->tileno[i];
3731 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
3732 opj_free(j2k->tile_data[tileno]);
3733 j2k->tile_data[tileno] = NULL;
3734 tcd_free_decode_tile(tcd, i);
3735 if (success == OPJ_FALSE) {
3736 j2k->state |= J2K_STATE_ERR;
3740 tcd_free_decode(tcd);
3743 /* if packets should not be decoded */
3745 for (i = 0; i < j2k->cp->tileno_size; i++) {
3746 tileno = j2k->cp->tileno[i];
3747 opj_free(j2k->tile_data[tileno]);
3748 j2k->tile_data[tileno] = NULL;
3751 if (j2k->state & J2K_STATE_ERR)
3752 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
3754 j2k->state = J2K_STATE_MT;
3758 * Reads a EOC marker (End Of Codestream)
3760 * @param p_header_data the data contained in the SOD box.
3761 * @param p_j2k the jpeg2000 codec.
3762 * @param p_header_size the size of the data contained in the SOD marker.
3763 * @param p_manager the user event manager.
3765 opj_bool j2k_read_eoc_v2 (
3766 opj_j2k_v2_t *p_j2k,
3767 struct opj_stream_private *p_stream,
3768 struct opj_event_mgr * p_manager
3772 opj_tcd_v2_t * l_tcd = 00;
3773 OPJ_UINT32 l_nb_tiles;
3774 opj_tcp_v2_t * l_tcp = 00;
3778 assert(p_j2k != 00);
3779 assert(p_manager != 00);
3780 assert(p_stream != 00);
3782 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3783 l_tcp = p_j2k->m_cp.tcps;
3785 l_tcd = tcd_create_v2(OPJ_TRUE);
3787 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
3791 for (i = 0; i < l_nb_tiles; ++i) {
3792 if (l_tcp->m_data) {
3793 if (! tcd_init_decode_tile(l_tcd, i)) {
3794 tcd_destroy_v2(l_tcd);
3795 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
3799 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_info);
3803 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; // TODO MSD J2K_DEC_STATE_ERR;
3808 j2k_tcp_destroy(l_tcp);
3812 tcd_destroy_v2(l_tcd);
3816 typedef struct opj_dec_mstabent {
3819 /** value of the state when the marker can appear */
3821 /** action linked to the marker */
3822 void (*handler) (opj_j2k_t *j2k);
3823 } opj_dec_mstabent_t;
3825 opj_dec_mstabent_t j2k_dec_mstab[] = {
3826 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
3827 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
3828 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
3829 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
3830 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
3831 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
3832 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
3833 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
3834 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
3835 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
3836 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
3837 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
3838 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
3839 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
3840 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
3841 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
3843 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
3844 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
3847 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
3848 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
3849 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
3850 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
3851 #endif /* USE_JPWL */
3853 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
3854 {J2K_MS_INSEC, 0, j2k_read_insec},
3855 #endif /* USE_JPSEC */
3857 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
3860 static void j2k_read_unk(opj_j2k_t *j2k) {
3861 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
3864 if (j2k->cp->correct) {
3866 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
3867 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3868 id = cio_read(j2k->cio, 2);
3869 opj_event_msg(j2k->cinfo, EVT_ERROR,
3870 "JPWL: really don't know this marker %x\n",
3873 opj_event_msg(j2k->cinfo, EVT_ERROR,
3874 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
3877 /* OK, activate this at your own risk!!! */
3878 /* we look for the marker at the minimum hamming distance from this */
3879 while (j2k_dec_mstab[m].id) {
3881 /* 1's where they differ */
3882 tmp_id = j2k_dec_mstab[m].id ^ id;
3884 /* compute the hamming distance between our id and the current */
3886 for (i = 0; i < 16; i++) {
3887 if ((tmp_id >> i) & 0x0001) {
3892 /* if current distance is smaller, set the minimum */
3893 if (cur_dist < min_dist) {
3894 min_dist = cur_dist;
3895 min_id = j2k_dec_mstab[m].id;
3898 /* jump to the next marker */
3902 /* do we substitute the marker? */
3903 if (min_dist < JPWL_MAXIMUM_HAMMING) {
3904 opj_event_msg(j2k->cinfo, EVT_ERROR,
3905 "- marker %x is at distance %d from the read %x\n",
3906 min_id, min_dist, id);
3907 opj_event_msg(j2k->cinfo, EVT_ERROR,
3908 "- trying to substitute in place and crossing fingers!\n");
3909 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3910 cio_write(j2k->cio, min_id, 2);
3913 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3918 #endif /* USE_JPWL */
3923 * Reads an unknown marker
3925 * @param p_stream the stream object to read from.
3926 * @param p_j2k the jpeg2000 codec.
3927 * @param p_manager the user event manager.
3929 * @return true if the marker could be deduced.
3931 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
3932 struct opj_stream_private *p_stream,
3933 struct opj_event_mgr * p_manager
3936 OPJ_BYTE l_data [2];
3937 OPJ_UINT32 l_unknown_size, l_unknown_marker;
3938 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
3941 assert(p_j2k != 00);
3942 assert(p_manager != 00);
3943 assert(p_stream != 00);
3945 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
3949 /* if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
3950 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
3954 opj_read_bytes(l_data,&l_unknown_size,2);
3955 if (l_unknown_size < 2) {
3960 if (opj_stream_skip(p_stream,l_unknown_size,p_manager) != l_unknown_size) {
3964 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
3965 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
3966 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
3970 // read 2 bytes as the new marker ID
3971 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
3973 if (!(l_unknown_marker < 0xff00)) {
3975 // Get the marker handler from the marker ID
3976 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
3978 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
3979 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
3983 if (l_marker_handler->id != J2K_MS_UNK)
3984 break; // next marker is known and well located
3993 Read the lookup table containing all the marker, status and action
3994 @param id Marker value
3996 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
3997 opj_dec_mstabent_t *e;
3998 for (e = j2k_dec_mstab; e->id != 0; e++) {
4006 /* ----------------------------------------------------------------------- */
4007 /* J2K / JPT decoder interface */
4008 /* ----------------------------------------------------------------------- */
4010 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
4011 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4015 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
4016 if(!j2k->default_tcp) {
4022 j2k->tile_data = NULL;
4027 void j2k_destroy_decompress(opj_j2k_t *j2k) {
4030 if(j2k->tile_len != NULL) {
4031 opj_free(j2k->tile_len);
4033 if(j2k->tile_data != NULL) {
4034 opj_free(j2k->tile_data);
4036 if(j2k->default_tcp != NULL) {
4037 opj_tcp_t *default_tcp = j2k->default_tcp;
4038 if(default_tcp->ppt_data_first != NULL) {
4039 opj_free(default_tcp->ppt_data_first);
4041 if(j2k->default_tcp->tccps != NULL) {
4042 opj_free(j2k->default_tcp->tccps);
4044 opj_free(j2k->default_tcp);
4046 if(j2k->cp != NULL) {
4047 opj_cp_t *cp = j2k->cp;
4048 if(cp->tcps != NULL) {
4049 for(i = 0; i < cp->tw * cp->th; i++) {
4050 if(cp->tcps[i].ppt_data_first != NULL) {
4051 opj_free(cp->tcps[i].ppt_data_first);
4053 if(cp->tcps[i].tccps != NULL) {
4054 opj_free(cp->tcps[i].tccps);
4059 if(cp->ppm_data_first != NULL) {
4060 opj_free(cp->ppm_data_first);
4062 if(cp->tileno != NULL) {
4063 opj_free(cp->tileno);
4065 if(cp->comment != NULL) {
4066 opj_free(cp->comment);
4074 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
4075 if(j2k && parameters) {
4076 /* create and initialize the coding parameters structure */
4077 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4078 cp->reduce = parameters->cp_reduce;
4079 cp->layer = parameters->cp_layer;
4080 cp->limit_decoding = parameters->cp_limit_decoding;
4083 cp->correct = parameters->jpwl_correct;
4084 cp->exp_comps = parameters->jpwl_exp_comps;
4085 cp->max_tiles = parameters->jpwl_max_tiles;
4086 #endif /* USE_JPWL */
4089 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
4094 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters) {
4095 if(j2k && parameters) {
4096 /* create and initialize the coding parameters structure */
4097 //opj_cp_v2_t *cp = (opj_cp_v2_t*) opj_calloc(1, sizeof(opj_cp_v2_t));
4098 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
4099 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
4101 /*cp->reduce = parameters->cp_reduce;
4102 cp->layer = parameters->cp_layer;
4103 cp->limit_decoding = parameters->cp_limit_decoding;*/
4107 cp->correct = parameters->jpwl_correct;
4108 cp->exp_comps = parameters->jpwl_exp_comps;
4109 cp->max_tiles = parameters->jpwl_max_tiles;
4110 #endif /* USE_JPWL */
4115 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
4116 opj_image_t *image = NULL;
4118 opj_common_ptr cinfo = j2k->cinfo;
4121 j2k->cstr_info = cstr_info;
4123 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
4125 /* create an empty image */
4126 image = opj_image_create0();
4129 j2k->state = J2K_STATE_MHSOC;
4132 opj_dec_mstabent_t *e;
4133 int id = cio_read(cio, 2);
4136 /* we try to honor JPWL correction power */
4137 if (j2k->cp->correct) {
4139 int orig_pos = cio_tell(cio);
4142 /* call the corrector */
4143 status = jpwl_correct(j2k);
4145 /* go back to where you were */
4146 cio_seek(cio, orig_pos - 2);
4148 /* re-read the marker */
4149 id = cio_read(cio, 2);
4151 /* check whether it begins with ff */
4152 if (id >> 8 != 0xff) {
4153 opj_event_msg(cinfo, EVT_ERROR,
4154 "JPWL: possible bad marker %x at %d\n",
4155 id, cio_tell(cio) - 2);
4157 opj_image_destroy(image);
4158 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
4161 /* we try to correct */
4163 cio_seek(cio, cio_tell(cio) - 2);
4164 cio_write(cio, id, 2);
4165 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
4166 "- setting marker to %x\n",
4171 #endif /* USE_JPWL */
4173 if (id >> 8 != 0xff) {
4174 opj_image_destroy(image);
4175 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
4178 e = j2k_dec_mstab_lookup(id);
4179 // Check if the marker is known
4180 if (!(j2k->state & e->states)) {
4181 opj_image_destroy(image);
4182 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
4185 // Check if the decoding is limited to the main header
4186 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
4187 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
4194 if (j2k->state & J2K_STATE_ERR)
4197 if (j2k->state == J2K_STATE_MT) {
4200 if (j2k->state == J2K_STATE_NEOC) {
4204 if (j2k->state == J2K_STATE_NEOC) {
4208 if (j2k->state != J2K_STATE_MT) {
4209 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
4215 * Read a JPT-stream and decode file
4218 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
4219 opj_image_t *image = NULL;
4220 opj_jpt_msg_header_t header;
4222 opj_common_ptr cinfo = j2k->cinfo;
4224 OPJ_ARG_NOT_USED(cstr_info);
4228 /* create an empty image */
4229 image = opj_image_create0();
4232 j2k->state = J2K_STATE_MHSOC;
4234 /* Initialize the header */
4235 jpt_init_msg_header(&header);
4236 /* Read the first header of the message */
4237 jpt_read_msg_header(cinfo, cio, &header);
4239 position = cio_tell(cio);
4240 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
4241 opj_image_destroy(image);
4242 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
4247 opj_dec_mstabent_t *e = NULL;
4250 if (!cio_numbytesleft(cio)) {
4254 /* data-bin read -> need to read a new header */
4255 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
4256 jpt_read_msg_header(cinfo, cio, &header);
4257 position = cio_tell(cio);
4258 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
4259 opj_image_destroy(image);
4260 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
4265 id = cio_read(cio, 2);
4266 if (id >> 8 != 0xff) {
4267 opj_image_destroy(image);
4268 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
4271 e = j2k_dec_mstab_lookup(id);
4272 if (!(j2k->state & e->states)) {
4273 opj_image_destroy(image);
4274 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
4280 if (j2k->state == J2K_STATE_MT) {
4283 if (j2k->state == J2K_STATE_NEOC) {
4287 if (j2k->state == J2K_STATE_NEOC) {
4291 if (j2k->state != J2K_STATE_MT) {
4292 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
4298 /* ----------------------------------------------------------------------- */
4299 /* J2K encoder interface */
4300 /* ----------------------------------------------------------------------- */
4302 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
4303 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4310 opj_j2k_v2_t* j2k_create_compress_v2()
4312 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
4317 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
4319 l_j2k->m_is_decoder = 0;
4320 l_j2k->m_cp.m_is_decoder = 0;
4322 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
4323 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4328 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
4330 // validation list creation
4331 l_j2k->m_validation_list = opj_procedure_list_create();
4332 if (! l_j2k->m_validation_list) {
4337 // execution list creation
4338 l_j2k->m_procedure_list = opj_procedure_list_create();
4339 if (! l_j2k->m_procedure_list) {
4347 void j2k_destroy_compress(opj_j2k_t *j2k) {
4351 if(j2k->cp != NULL) {
4352 opj_cp_t *cp = j2k->cp;
4355 opj_free(cp->comment);
4358 opj_free(cp->matrice);
4360 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4361 opj_free(cp->tcps[tileno].tccps);
4370 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
4371 int i, j, tileno, numpocs_tile;
4372 opj_cp_t *cp = NULL;
4374 if(!j2k || !parameters || ! image) {
4378 /* create and initialize the coding parameters structure */
4379 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4381 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
4384 /* set default values for cp */
4389 copy user encoding parameters
4391 cp->cinema = parameters->cp_cinema;
4392 cp->max_comp_size = parameters->max_comp_size;
4393 cp->rsiz = parameters->cp_rsiz;
4394 cp->disto_alloc = parameters->cp_disto_alloc;
4395 cp->fixed_alloc = parameters->cp_fixed_alloc;
4396 cp->fixed_quality = parameters->cp_fixed_quality;
4398 /* mod fixed_quality */
4399 if(parameters->cp_matrice) {
4400 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
4401 cp->matrice = (int *) opj_malloc(array_size);
4402 memcpy(cp->matrice, parameters->cp_matrice, array_size);
4406 cp->tdx = parameters->cp_tdx;
4407 cp->tdy = parameters->cp_tdy;
4410 cp->tx0 = parameters->cp_tx0;
4411 cp->ty0 = parameters->cp_ty0;
4413 /* comment string */
4414 if(parameters->cp_comment) {
4415 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
4417 strcpy(cp->comment, parameters->cp_comment);
4422 calculate other encoding parameters
4425 if (parameters->tile_size_on) {
4426 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
4427 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
4429 cp->tdx = image->x1 - cp->tx0;
4430 cp->tdy = image->y1 - cp->ty0;
4433 if(parameters->tp_on){
4434 cp->tp_flag = parameters->tp_flag;
4439 for(i=0;i<image->numcomps ;i++){
4440 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
4446 calculate JPWL encoding parameters
4449 if (parameters->jpwl_epc_on) {
4453 cp->epc_on = OPJ_TRUE;
4454 cp->info_on = OPJ_FALSE; /* no informative technique */
4457 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
4458 cp->epb_on = OPJ_TRUE;
4460 cp->hprot_MH = parameters->jpwl_hprot_MH;
4461 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
4462 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
4463 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
4465 /* if tile specs are not specified, copy MH specs */
4466 if (cp->hprot_TPH[0] == -1) {
4467 cp->hprot_TPH_tileno[0] = 0;
4468 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
4470 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
4471 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
4472 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
4473 cp->pprot[i] = parameters->jpwl_pprot[i];
4477 /* set ESD writing */
4478 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
4479 cp->esd_on = OPJ_TRUE;
4481 cp->sens_size = parameters->jpwl_sens_size;
4482 cp->sens_addr = parameters->jpwl_sens_addr;
4483 cp->sens_range = parameters->jpwl_sens_range;
4485 cp->sens_MH = parameters->jpwl_sens_MH;
4486 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
4487 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
4488 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
4492 /* always set RED writing to false: we are at the encoder */
4493 cp->red_on = OPJ_FALSE;
4496 cp->epc_on = OPJ_FALSE;
4498 #endif /* USE_JPWL */
4501 /* initialize the mutiple tiles */
4502 /* ---------------------------- */
4503 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
4505 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4506 opj_tcp_t *tcp = &cp->tcps[tileno];
4507 tcp->numlayers = parameters->tcp_numlayers;
4508 for (j = 0; j < tcp->numlayers; j++) {
4510 if (cp->fixed_quality) {
4511 tcp->distoratio[j] = parameters->tcp_distoratio[j];
4513 tcp->rates[j] = parameters->tcp_rates[j];
4515 if (cp->fixed_quality) { /* add fixed_quality */
4516 tcp->distoratio[j] = parameters->tcp_distoratio[j];
4518 tcp->rates[j] = parameters->tcp_rates[j];
4522 tcp->csty = parameters->csty;
4523 tcp->prg = parameters->prog_order;
4524 tcp->mct = parameters->tcp_mct;
4528 if (parameters->numpocs) {
4529 /* initialisation of POC */
4531 for (i = 0; i < parameters->numpocs; i++) {
4532 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
4533 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
4534 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
4535 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
4536 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
4537 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
4538 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
4539 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
4540 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
4544 tcp->numpocs = numpocs_tile -1 ;
4549 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
4551 for (i = 0; i < image->numcomps; i++) {
4552 opj_tccp_t *tccp = &tcp->tccps[i];
4553 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
4554 tccp->numresolutions = parameters->numresolution;
4555 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
4556 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
4557 tccp->cblksty = parameters->mode;
4558 tccp->qmfbid = parameters->irreversible ? 0 : 1;
4559 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
4561 if (i == parameters->roi_compno) {
4562 tccp->roishift = parameters->roi_shift;
4567 if(parameters->cp_cinema)
4569 //Precinct size for lowest frequency subband=128
4572 //Precinct size at all other resolutions = 256
4573 for (j = 1; j < tccp->numresolutions; j++) {
4578 if (parameters->csty & J2K_CCP_CSTY_PRT) {
4580 for (j = tccp->numresolutions - 1; j >= 0; j--) {
4581 if (p < parameters->res_spec) {
4583 if (parameters->prcw_init[p] < 1) {
4586 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
4589 if (parameters->prch_init[p] < 1) {
4592 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
4596 int res_spec = parameters->res_spec;
4597 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
4598 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
4600 if (size_prcw < 1) {
4603 tccp->prcw[j] = int_floorlog2(size_prcw);
4606 if (size_prch < 1) {
4609 tccp->prch[j] = int_floorlog2(size_prch);
4613 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
4616 for (j = 0; j < tccp->numresolutions; j++) {
4623 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
4628 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
4630 opj_cp_t *cp = NULL;
4632 opj_tcd_t *tcd = NULL; /* TCD component */
4640 j2k->cstr_info = cstr_info;
4643 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
4644 cstr_info->image_w = image->x1 - image->x0;
4645 cstr_info->image_h = image->y1 - image->y0;
4646 cstr_info->prog = (&cp->tcps[0])->prg;
4647 cstr_info->tw = cp->tw;
4648 cstr_info->th = cp->th;
4649 cstr_info->tile_x = cp->tdx; /* new version parser */
4650 cstr_info->tile_y = cp->tdy; /* new version parser */
4651 cstr_info->tile_Ox = cp->tx0; /* new version parser */
4652 cstr_info->tile_Oy = cp->ty0; /* new version parser */
4653 cstr_info->numcomps = image->numcomps;
4654 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
4655 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
4656 for (compno=0; compno < image->numcomps; compno++) {
4657 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
4659 cstr_info->D_max = 0.0; /* ADD Marcela */
4660 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
4661 cstr_info->maxmarknum = 100;
4662 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
4663 cstr_info->marknum = 0;
4673 for (compno = 1; compno < image->numcomps; compno++) {
4674 j2k_write_coc(j2k, compno);
4675 j2k_write_qcc(j2k, compno);
4679 for (compno = 0; compno < image->numcomps; compno++) {
4680 opj_tcp_t *tcp = &cp->tcps[0];
4681 if (tcp->tccps[compno].roishift)
4682 j2k_write_rgn(j2k, compno, 0);
4684 if (cp->comment != NULL) {
4688 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
4692 if (cp->cinema == CINEMA4K_24) {
4697 /* uncomment only for testing JPSEC marker writing */
4698 /* j2k_write_sec(j2k); */
4702 cstr_info->main_head_end = cio_tell(cio) - 1;
4705 /**** Main Header ENDS here ***/
4707 /* create the tile encoder */
4708 tcd = tcd_create(j2k->cinfo);
4710 /* encode each tile */
4711 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4715 int acc_pack_num = 0;
4719 opj_tcp_t *tcp = &cp->tcps[tileno];
4720 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
4722 j2k->curtileno = tileno;
4723 j2k->cur_tp_num = 0;
4724 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
4725 /* initialisation before tile encoding */
4727 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
4729 tcd_init_encode(tcd, image, cp, j2k->curtileno);
4734 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
4735 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
4736 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
4737 cstr_info->tile[j2k->curtileno].marknum = 0;
4741 for(pino = 0; pino <= tcp->numpocs; pino++) {
4745 /*Get number of tile parts*/
4746 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
4747 tcd->tp_pos = cp->tp_pos;
4749 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
4750 j2k->tp_num = tilepartno;
4753 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
4754 cio_tell(cio) + j2k->pos_correction;
4758 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
4759 for (compno = 1; compno < image->numcomps; compno++) {
4760 j2k_write_coc(j2k, compno);
4761 j2k_write_qcc(j2k, compno);
4763 if (cp->tcps[tileno].numpocs) {
4770 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
4771 cio_tell(cio) + j2k->pos_correction + 1;
4774 j2k_write_sod(j2k, tcd);
4778 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
4779 cio_tell(cio) + j2k->pos_correction - 1;
4780 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
4782 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
4783 cstr_info->packno - acc_pack_num;
4784 acc_pack_num = cstr_info->packno;
4792 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
4797 if (tile->PPT) { // BAD PPT !!!
4800 PPT_file=fopen("PPT","rb");
4801 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
4802 for (i=0;i<tile->len_ppt;i++) {
4804 fread(&elmt, 1, 1, PPT_file);
4805 fwrite(&elmt,1,1,f);
4814 /* destroy the tile encoder */
4815 tcd_free_encode(tcd);
4818 opj_free(j2k->cur_totnum_tp);
4823 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
4825 /* The following adjustment is done to adjust the codestream size */
4826 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
4827 /* the first bunch of bytes is not in the codestream */
4828 cstr_info->codestream_size -= cstr_info->main_head_start;
4834 preparation of JPWL marker segments
4838 /* encode according to JPWL */
4839 jpwl_encode(j2k, cio, image);
4842 #endif /* USE_JPWL */
4847 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
4852 /* expand the list? */
4853 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
4854 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
4855 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
4858 /* add the marker */
4859 cstr_info->marker[cstr_info->marknum].type = type;
4860 cstr_info->marker[cstr_info->marknum].pos = pos;
4861 cstr_info->marker[cstr_info->marknum].len = len;
4862 cstr_info->marknum++;
4866 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
4869 opj_marker_info_t *marker;
4874 /* expand the list? */
4875 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
4876 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
4877 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
4880 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
4882 /* add the marker */
4883 marker->type = type;
4886 cstr_info->tile[tileno].marknum++;
4893 * -----------------------------------------------------------------------
4894 * -----------------------------------------------------------------------
4895 * -----------------------------------------------------------------------
4899 * Ends the decompression procedures and possibiliy add data to be read after the
4902 opj_bool j2k_end_decompress(
4903 opj_j2k_v2_t *p_j2k,
4904 opj_stream_private_t *p_stream,
4905 opj_event_mgr_t * p_manager)
4911 * Reads a jpeg2000 codestream header structure.
4914 * @param p_stream the stream to read data from.
4915 * @param p_j2k the jpeg2000 codec.
4916 * @param p_manager the user event manager.
4918 * @return true if the box is valid.
4920 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
4921 opj_j2k_v2_t* p_j2k,
4922 opj_file_info_t* p_file_info,
4923 struct opj_event_mgr* p_manager )
4926 assert(p_j2k != 00);
4927 assert(p_stream != 00);
4928 assert(p_manager != 00);
4930 /* create an empty image header */
4931 p_j2k->m_image_header = opj_image_header_create0();
4932 if (! p_j2k->m_image_header) {
4936 /* customization of the validation */
4937 j2k_setup_decoding_validation(p_j2k);
4939 /* validation of the parameters codec */
4940 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
4941 opj_image_header_destroy(p_j2k->m_image_header);
4942 p_j2k->m_image_header = NULL;
4946 /* customization of the encoding */
4947 j2k_setup_header_reading(p_j2k);
4950 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
4951 opj_image_header_destroy(p_j2k->m_image_header);
4952 p_j2k->m_image_header = NULL;
4956 /* If necessary copy j2k header information into the output structure */
4957 if (p_file_info->file_info_flag & OPJ_J2K_INFO){
4958 fill_cstr_info( &(p_file_info->codestream_info), p_j2k, p_manager);
4961 /* If necessary copy image header information into the output structure */
4962 if (p_file_info->file_info_flag & OPJ_IMG_INFO){
4963 fill_img_info( &(p_file_info->img_info), p_j2k, p_manager);
4970 * Fill the image info struct from information read from main header.
4972 opj_bool fill_img_info(opj_image_header_t* img_info, opj_j2k_v2_t* p_j2k, struct opj_event_mgr* p_manager)
4974 opj_image_header_t* l_image_header = p_j2k->m_image_header;
4976 img_info->x0 = l_image_header->x0;
4977 img_info->y0 = l_image_header->y0;
4978 img_info->x1 = l_image_header->x1;
4979 img_info->y1 = l_image_header->y1;
4980 img_info->numcomps = l_image_header->numcomps;
4982 if (img_info->numcomps) {
4983 img_info->comps = (opj_image_comp_header_t*)opj_malloc(img_info->numcomps * sizeof(opj_image_comp_header_t));
4984 if (!img_info->comps){
4985 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
4989 memcpy(img_info->comps, l_image_header->comps, img_info->numcomps * sizeof(opj_image_comp_header_t) );
4993 img_info->icc_profile_len = l_image_header->icc_profile_len;
4994 if (img_info->icc_profile_len) {
4995 img_info->icc_profile_buf = (unsigned char*)opj_malloc(img_info->icc_profile_len * sizeof(unsigned char));
4996 if (!img_info->icc_profile_buf){
4997 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
5001 memcpy(img_info->icc_profile_buf, l_image_header->icc_profile_buf, img_info->icc_profile_len);
5008 * Fill the codestream info struct from information read from main header.
5010 opj_bool fill_cstr_info(opj_codestream_info_v2_t* cstr_info, opj_j2k_v2_t* p_j2k, struct opj_event_mgr* p_manager)
5013 //opj_cp_v2_t l_cp = p_j2k->m_cp;
5014 opj_image_header_t* l_image_header = p_j2k->m_image_header;
5015 int it_tile, it_comp;
5016 int nb_tiles, nb_comps;
5018 cstr_info->image_w = l_image_header->x1 - l_image_header->x0;
5019 cstr_info->image_h = l_image_header->y1 - l_image_header->y0;
5020 cstr_info->numcomps = l_image_header->numcomps;
5021 cstr_info->tw = p_j2k->m_cp.tw;
5022 cstr_info->th = p_j2k->m_cp.th;
5023 cstr_info->tdx = p_j2k->m_cp.tdx;
5024 cstr_info->tdy = p_j2k->m_cp.tdy;
5025 cstr_info->tx0 = p_j2k->m_cp.tx0;
5026 cstr_info->ty0 = p_j2k->m_cp.ty0;
5028 nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
5029 nb_comps = l_image_header->numcomps;
5031 cstr_info->tile = (opj_tile_info_v2_t*) opj_calloc(nb_tiles, sizeof(opj_tile_info_v2_t));
5032 if (! p_j2k->cstr_info->tile) {
5033 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
5037 for (it_tile=0; it_tile < nb_tiles; it_tile++){
5038 cstr_info->tile[it_tile].tileno = it_tile;
5039 cstr_info->tile[it_tile].csty = p_j2k->m_cp.tcps[it_tile].csty;
5040 cstr_info->tile[it_tile].prg = p_j2k->m_cp.tcps[it_tile].prg;
5041 cstr_info->tile[it_tile].mct = p_j2k->m_cp.tcps[it_tile].mct;
5042 cstr_info->tile[it_tile].numlayers = p_j2k->m_cp.tcps[it_tile].numlayers;
5043 // FIXME Number of tile part l_cstr_info->tile[it_tile].num_tps = ;
5045 cstr_info->tile[it_tile].tccp_info =
5046 (opj_tccp_info_t*) opj_calloc(nb_comps, sizeof(opj_tccp_info_t));
5047 if (! p_j2k->cstr_info->tile[it_tile].tccp_info) {
5048 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
5052 for (it_comp=0; it_comp < nb_comps; it_comp++){
5053 cstr_info->tile[it_tile].tccp_info[it_comp].compno = it_comp;
5054 cstr_info->tile[it_tile].tccp_info[it_comp].cblkh = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblkh;
5055 cstr_info->tile[it_tile].tccp_info[it_comp].cblkw = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblkw;
5056 cstr_info->tile[it_tile].tccp_info[it_comp].cblksty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblksty;
5057 cstr_info->tile[it_tile].tccp_info[it_comp].csty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].csty;
5058 cstr_info->tile[it_tile].tccp_info[it_comp].numgbits = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].numgbits;
5059 cstr_info->tile[it_tile].tccp_info[it_comp].numresolutions = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].numresolutions;
5060 cstr_info->tile[it_tile].tccp_info[it_comp].qmfbid = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].qmfbid;
5061 cstr_info->tile[it_tile].tccp_info[it_comp].qntsty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].qntsty;
5062 cstr_info->tile[it_tile].tccp_info[it_comp].roishift = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].roishift;
5072 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
5074 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
5077 assert(p_j2k != 00);
5079 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_read_header_procedure);
5081 /* DEVELOPER CORNER, add your custom procedures */
5082 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_copy_default_tcp_and_create_tcd);
5087 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
5088 * are valid. Developpers wanting to extend the library can add their own validation procedures.
5090 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
5093 assert(p_j2k != 00);
5095 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_decoder);
5096 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_decoding_validation);
5097 /* DEVELOPER CORNER, add your custom validation procedure */
5102 * Builds the cp decoder parameters to use to decode tile.
5104 opj_bool j2k_build_decoder (
5105 opj_j2k_v2_t * p_j2k,
5106 opj_stream_private_t *p_stream,
5107 opj_event_mgr_t * p_manager
5110 // add here initialization of cp
5111 // copy paste of setup_decoder
5116 * The default decoding validation procedure without any extension.
5118 * @param p_j2k the jpeg2000 codec to validate.
5119 * @param p_stream the input stream to validate.
5120 * @param p_manager the user event manager.
5122 * @return true if the parameters are correct.
5124 opj_bool j2k_decoding_validation (
5125 opj_j2k_v2_t *p_j2k,
5126 opj_stream_private_t *p_stream,
5127 opj_event_mgr_t * p_manager
5130 opj_bool l_is_valid = OPJ_TRUE;
5133 assert(p_j2k != 00);
5134 assert(p_stream != 00);
5135 assert(p_manager != 00);
5138 /* STATE checking */
5139 /* make sure the state is at 0 */
5141 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
5143 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
5145 /* POINTER validation */
5146 /* make sure a p_j2k codec is present */
5147 /* make sure a procedure list is present */
5148 l_is_valid &= (p_j2k->m_procedure_list != 00);
5149 /* make sure a validation list is present */
5150 l_is_valid &= (p_j2k->m_validation_list != 00);
5152 /* PARAMETER VALIDATION */
5156 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
5157 struct opj_stream_private *p_stream,
5158 struct opj_event_mgr * p_manager)
5160 OPJ_UINT32 l_current_marker;
5161 OPJ_UINT32 l_marker_size;
5162 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
5165 assert(p_stream != 00);
5166 assert(p_j2k != 00);
5167 assert(p_manager != 00);
5169 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; // The codestream must begin with SOC marker FIXME J2K_DEC_STATE_MHSOC
5171 // Try to read the SOC marker
5172 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
5173 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
5177 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5178 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5179 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5183 // Read 2 bytes as the new marker ID
5184 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5186 /* Try to read until the SOT is detected */
5187 while (l_current_marker != J2K_MS_SOT) {
5189 /* Check if the current marker ID is valid */
5190 if (l_current_marker < 0xff00) {
5191 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
5195 // Get the marker handler from the marker ID
5196 l_marker_handler = j2k_get_marker_handler(l_current_marker);
5198 // Check if the marker is known and if it is the right place to find it
5199 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5200 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5204 // Manage case where marker is unknown
5205 if (l_marker_handler->id == J2K_MS_UNK) {
5207 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5209 // Try to detect the next valid marker
5211 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5212 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5213 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5217 // Read 2 bytes as the new marker ID
5218 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5220 // Check if the current marker ID is valid
5221 if ( !(l_current_marker < 0xff00) ) {
5222 // Get the marker handler from the marker ID
5223 l_marker_handler = j2k_get_marker_handler(l_current_marker);
5225 if ( !(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5226 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5230 if (l_marker_handler->id != J2K_MS_UNK)
5231 break; // next marker is known and well located
5235 if (l_current_marker == J2K_MS_SOT)
5236 break; // SOT marker is detected main header is completely read.
5239 // Try to read 2 bytes (the marker size) from stream and copy them into the buffer
5240 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5241 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5245 // read 2 bytes as the marker size
5246 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
5247 l_marker_size -= 2; // Subtract the size of the marker ID already read
5249 // Check if the marker size is compatible with the header data size
5250 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
5251 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
5252 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
5253 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
5256 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
5259 // Try to read the rest of the marker segment from stream and copy them into the buffer
5260 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) {
5261 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5265 // Read the marker segment with the correct marker handler
5266 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
5267 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
5271 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5272 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5273 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5277 // read 2 bytes as the new marker ID
5278 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5281 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; // FIXME J2K_DEC_STATE_TPHSOT
5286 * Excutes the given procedures on the given codec.
5288 * @param p_procedure_list the list of procedures to execute
5289 * @param p_j2k the jpeg2000 codec to execute the procedures on.
5290 * @param p_stream the stream to execute the procedures on.
5291 * @param p_manager the user manager.
5293 * @return true if all the procedures were successfully executed.
5296 opj_j2k_v2_t * p_j2k,
5297 opj_procedure_list_t * p_procedure_list,
5298 opj_stream_private_t *p_stream,
5299 opj_event_mgr_t * p_manager
5302 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
5303 opj_bool l_result = OPJ_TRUE;
5304 OPJ_UINT32 l_nb_proc, i;
5307 assert(p_procedure_list != 00);
5308 assert(p_j2k != 00);
5309 assert(p_stream != 00);
5310 assert(p_manager != 00);
5313 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
5314 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);
5316 for (i=0;i<l_nb_proc;++i) {
5317 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
5321 // and clear the procedure list at the end.
5322 opj_procedure_list_clear(p_procedure_list);
5327 opj_bool j2k_copy_default_tcp_and_create_tcd
5329 opj_j2k_v2_t * p_j2k,
5330 opj_stream_private_t *p_stream,
5331 opj_event_mgr_t * p_manager
5334 opj_tcp_v2_t * l_tcp = 00;
5335 opj_tcp_v2_t * l_default_tcp = 00;
5336 OPJ_UINT32 l_nb_tiles;
5338 opj_tccp_t *l_current_tccp = 00;
5339 OPJ_UINT32 l_tccp_size;
5340 OPJ_UINT32 l_mct_size;
5341 opj_image_header_t * l_image;
5342 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
5343 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
5344 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
5345 OPJ_UINT32 l_offset;
5347 // preconditions in debug
5348 assert(p_j2k != 00);
5349 assert(p_stream != 00);
5350 assert(p_manager != 00);
5353 l_image = p_j2k->m_image_header;
5354 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5355 l_tcp = p_j2k->m_cp.tcps;
5356 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
5357 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
5358 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
5360 (i=0;i<l_nb_tiles;++i)
5362 l_current_tccp = l_tcp->tccps;
5363 memcpy(l_tcp,l_default_tcp, sizeof(opj_tcp_v2_t));
5365 l_tcp->ppt_data = 00;
5366 l_tcp->tccps = l_current_tccp;
5368 (l_default_tcp->m_mct_decoding_matrix)
5370 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5372 (! l_tcp->m_mct_decoding_matrix )
5376 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
5378 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
5379 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
5381 (! l_tcp->m_mct_records)
5385 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
5386 l_src_mct_rec = l_default_tcp->m_mct_records;
5387 l_dest_mct_rec = l_tcp->m_mct_records;
5389 (j=0;j<l_default_tcp->m_nb_mct_records;++j)
5392 (l_src_mct_rec->m_data)
5394 l_dest_mct_rec->m_data = (OPJ_BYTE*)
5395 opj_malloc(l_src_mct_rec->m_data_size);
5397 (! l_dest_mct_rec->m_data)
5401 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
5406 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
5407 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
5408 opj_malloc(l_mcc_records_size);
5410 (! l_tcp->m_mcc_records)
5414 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
5415 l_src_mcc_rec = l_default_tcp->m_mcc_records;
5416 l_dest_mcc_rec = l_tcp->m_mcc_records;
5418 (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j)
5421 (l_src_mcc_rec->m_decorrelation_array)
5423 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
5424 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
5427 (l_src_mcc_rec->m_offset_array)
5429 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
5430 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
5435 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
5438 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); // FIXME why a cast ?
5445 (! tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)))
5447 tcd_destroy_v2(p_j2k->m_tcd);
5449 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5456 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
5457 * with the marker value.
5458 * @param p_id Marker value to look up
5460 * @return the handler associated with the id.
5462 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (const OPJ_UINT32 p_id)
5464 const opj_dec_memory_marker_handler_t *e;
5465 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
5466 if (e->id == p_id) {
5467 break; // we find a handler corresponding to the marker ID
5475 * Destroys a jpeg2000 codec.
5477 * @param p_j2k the jpeg20000 structure to destroy.
5479 void j2k_destroy (opj_j2k_v2_t *p_j2k)
5488 (p_j2k->m_is_decoder)
5491 (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00)
5493 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
5494 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
5495 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
5498 (p_j2k->m_specific_param.m_decoder.m_header_data != 00)
5500 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
5501 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
5502 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
5509 (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data)
5511 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
5512 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
5515 (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer)
5517 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
5518 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
5519 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
5522 (p_j2k->m_specific_param.m_encoder.m_header_tile_data)
5524 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5525 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
5526 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5529 tcd_destroy_v2(p_j2k->m_tcd);
5531 j2k_cp_destroy(&(p_j2k->m_cp));
5532 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t));
5534 opj_procedure_list_destroy(p_j2k->m_procedure_list);
5535 p_j2k->m_procedure_list = 00;
5537 opj_procedure_list_destroy(p_j2k->m_validation_list);
5538 p_j2k->m_procedure_list = 00;
5546 * Destroys a tile coding parameter structure.
5548 * @param p_tcp the tile coding parameter to destroy.
5550 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
5558 (p_tcp->ppt_buffer != 00)
5560 opj_free(p_tcp->ppt_buffer);
5561 p_tcp->ppt_buffer = 00;
5564 (p_tcp->tccps != 00)
5566 opj_free(p_tcp->tccps);
5570 (p_tcp->m_mct_coding_matrix != 00)
5572 opj_free(p_tcp->m_mct_coding_matrix);
5573 p_tcp->m_mct_coding_matrix = 00;
5576 (p_tcp->m_mct_decoding_matrix != 00)
5578 opj_free(p_tcp->m_mct_decoding_matrix);
5579 p_tcp->m_mct_decoding_matrix = 00;
5582 (p_tcp->m_mcc_records)
5584 opj_free(p_tcp->m_mcc_records);
5585 p_tcp->m_mcc_records = 00;
5586 p_tcp->m_nb_max_mcc_records = 0;
5587 p_tcp->m_nb_mcc_records = 0;
5590 (p_tcp->m_mct_records)
5592 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
5595 (i=0;i<p_tcp->m_nb_mct_records;++i)
5598 (l_mct_data->m_data)
5600 opj_free(l_mct_data->m_data);
5601 l_mct_data->m_data = 00;
5605 opj_free(p_tcp->m_mct_records);
5606 p_tcp->m_mct_records = 00;
5610 (p_tcp->mct_norms != 00)
5612 opj_free(p_tcp->mct_norms);
5613 p_tcp->mct_norms = 00;
5618 opj_free(p_tcp->m_data);
5625 * Destroys a coding parameter structure.
5627 * @param p_cp the coding parameter to destroy.
5629 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
5631 OPJ_UINT32 l_nb_tiles;
5632 opj_tcp_v2_t * l_current_tile = 00;
5643 l_current_tile = p_cp->tcps;
5644 l_nb_tiles = p_cp->th * p_cp->tw;
5647 (i = 0; i < l_nb_tiles; ++i)
5649 j2k_tcp_destroy(l_current_tile);
5652 opj_free(p_cp->tcps);
5656 (p_cp->ppm_buffer != 00)
5658 opj_free(p_cp->ppm_buffer);
5659 p_cp->ppm_buffer = 00;
5662 (p_cp->comment != 00)
5664 opj_free(p_cp->comment);
5668 (! p_cp->m_is_decoder)
5671 (p_cp->m_specific_param.m_enc.m_matrice)
5673 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
5674 p_cp->m_specific_param.m_enc.m_matrice = 00;
5682 * Reads a tile header.
5683 * @param p_j2k the jpeg2000 codec.
5684 * @param p_stream the stream to write data to.
5685 * @param p_manager the user event manager.
5687 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
5688 OPJ_UINT32 * p_tile_index,
5689 OPJ_UINT32 * p_data_size,
5690 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
5691 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
5692 OPJ_UINT32 * p_nb_comps,
5694 opj_stream_private_t *p_stream,
5695 opj_event_mgr_t * p_manager )
5697 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
5698 OPJ_UINT32 l_marker_size;
5699 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
5700 opj_tcp_v2_t * l_tcp = NULL;
5701 OPJ_UINT32 l_nb_tiles;
5704 assert(p_stream != 00);
5705 assert(p_j2k != 00);
5706 assert(p_manager != 00);
5708 /* Reach the End Of Codestream ?*/
5709 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
5710 l_current_marker = J2K_MS_EOC;
5712 /* We need to encounter a SOT marker (a new tile-part header) */
5713 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
5717 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
5718 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
5720 /* Try to read until the Start Of Data is detected */
5721 while (l_current_marker != J2K_MS_SOD) {
5723 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
5724 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5725 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5729 /* Read 2 bytes from the buffer as the marker size */
5730 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
5732 /* Why this condition? FIXME */
5733 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
5734 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
5736 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
5738 /* Get the marker handler from the marker ID */
5739 l_marker_handler = j2k_get_marker_handler(l_current_marker);
5741 /* Check if the marker is known and if it is the right place to find it */
5742 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5743 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5746 /* FIXME manage case of unknown marker as in the main header ? */
5748 /* Check if the marker size is compatible with the header data size */
5749 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
5750 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
5751 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
5752 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
5755 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
5758 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
5759 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) {
5760 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5764 /* Read the marker segment with the correct marker handler */
5765 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
5766 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5770 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
5771 /* Skip the rest of the tile part header*/
5772 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) {
5773 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5776 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
5779 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5780 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5781 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5784 /* Read 2 bytes from the buffer as the new marker ID */
5785 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5789 /* If we didn't skip data before, we need to read the SOD marker*/
5790 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
5791 /* Try to read the SOD marker and skip data ? FIXME */
5792 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
5797 /* Indicate we will try to read a new tile-part header*/
5798 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
5799 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
5800 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5802 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
5803 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5804 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5808 /* Read 2 bytes from buffer as the new marker ID */
5809 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5813 /* Current marker is the EOC marker ?*/
5814 if (l_current_marker == J2K_MS_EOC) {
5815 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
5816 p_j2k->m_current_tile_number = 0;
5817 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
5822 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
5823 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
5824 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5826 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
5827 ++p_j2k->m_current_tile_number;
5831 if (p_j2k->m_current_tile_number == l_nb_tiles) {
5832 *p_go_on = OPJ_FALSE;
5838 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
5839 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5843 *p_tile_index = p_j2k->m_current_tile_number;
5844 *p_go_on = OPJ_TRUE;
5845 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
5846 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
5847 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
5848 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
5849 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
5850 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
5852 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;// FIXME J2K_DEC_STATE_DATA;
5858 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
5859 OPJ_UINT32 p_tile_index,
5861 OPJ_UINT32 p_data_size,
5862 opj_stream_private_t *p_stream,
5863 opj_event_mgr_t * p_manager )
5865 OPJ_UINT32 l_current_marker;
5866 OPJ_BYTE l_data [2];
5867 opj_tcp_v2_t * l_tcp;
5870 assert(p_stream != 00);
5871 assert(p_j2k != 00);
5872 assert(p_manager != 00);
5874 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
5875 || (p_tile_index != p_j2k->m_current_tile_number) ) {
5879 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
5880 if (! l_tcp->m_data) {
5881 j2k_tcp_destroy(&(p_j2k->m_cp.tcps[p_tile_index]));
5885 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
5889 p_j2k->cstr_info) ) {
5890 j2k_tcp_destroy(l_tcp);
5891 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;//FIXME J2K_DEC_STATE_ERR;
5895 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
5899 j2k_tcp_destroy(l_tcp);
5900 p_j2k->m_tcd->tcp = 0;
5902 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
5903 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));// FIXME J2K_DEC_STATE_DATA);
5905 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ //FIXME J2K_DEC_STATE_EOC)
5906 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
5907 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5911 opj_read_bytes(l_data,&l_current_marker,2);
5913 if (l_current_marker == J2K_MS_EOC) {
5914 p_j2k->m_current_tile_number = 0;
5915 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;//FIXME J2K_DEC_STATE_EOC;
5917 else if (l_current_marker != J2K_MS_SOT)
5919 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
5929 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
5931 * @param p_j2k the jpeg2000 codec.
5932 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
5933 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
5934 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
5935 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
5936 * @param p_manager the user event manager
5938 * @return true if the area could be set.
5940 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
5941 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
5942 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
5943 struct opj_event_mgr * p_manager )
5945 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
5947 /* Check if we are read the main header */
5948 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { // FIXME J2K_DEC_STATE_TPHSOT)
5949 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
5954 /* Check if the positions provided by the user are correct */
5957 if (p_start_x > l_cp->tx0 + l_cp->tw * l_cp->tdx) {
5958 opj_event_msg_v2(p_manager, EVT_ERROR,
5959 "Left position of the decoded area (ROI_x0=%d) is outside the tile area (XTOsiz + nb_tw x XTsiz=%d).\n",
5960 p_start_x, l_cp->tx0 + l_cp->tw * l_cp->tdx);
5963 else if (p_start_x < l_cp->tx0){
5964 opj_event_msg_v2(p_manager, EVT_WARNING,
5965 "Left position of the decoded area (ROI_x0=%d) is outside the tile area (XTOsiz=%d).\n",
5966 p_start_x, l_cp->tx0);
5967 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
5970 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
5973 if (p_start_y > l_cp->ty0 + l_cp->th * l_cp->tdy){
5974 opj_event_msg_v2(p_manager, EVT_ERROR,
5975 "Up position of the decoded area (ROI_y0=%d) is outside the tile area (YTOsiz + nb_th x YTsiz=%d).\n",
5976 p_start_y, l_cp->ty0 + l_cp->th * l_cp->tdy);
5979 else if (p_start_y < l_cp->ty0){
5980 opj_event_msg_v2(p_manager, EVT_WARNING,
5981 "Up position of the decoded area (ROI_y0=%d) is outside the tile area (YTOsiz=%d).\n",
5982 p_start_y, l_cp->ty0);
5983 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
5986 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
5989 if (p_end_x < l_cp->tx0) {
5990 opj_event_msg_v2(p_manager, EVT_ERROR,
5991 "Right position of the decoded area (ROI_x1=%d) is outside the tile area (XTOsiz=%d).\n",
5992 p_end_x, l_cp->tx0);
5995 else if (p_end_x > l_cp->tx0 + l_cp->tw * l_cp->tdx) {
5996 opj_event_msg_v2(p_manager, EVT_WARNING,
5997 "Right position of the decoded area (ROI_x1=%d) is outside the tile area (XTOsiz + nb_tw x XTsiz=%d).\n",
5998 p_end_x, l_cp->tx0 + l_cp->tw * l_cp->tdx);
5999 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw; // FIXME (-1) ???
6002 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
6005 if (p_end_x < l_cp->ty0) {
6006 opj_event_msg_v2(p_manager, EVT_ERROR,
6007 "Right position of the decoded area (ROI_y1=%d) is outside the tile area (YTOsiz=%d).\n",
6008 p_end_x, l_cp->ty0);
6011 if (p_end_y > l_cp->ty0 + l_cp->th * l_cp->tdy){
6012 opj_event_msg_v2(p_manager, EVT_WARNING,
6013 "Bottom position of the decoded area (ROI_y1=%d) is outside the tile area (YTOsiz + nb_th x YTsiz=%d).\n",
6014 p_end_y, l_cp->ty0 + l_cp->th * l_cp->tdy);
6015 p_j2k->m_specific_param.m_decoder.m_start_tile_y = l_cp->th; // FIXME (-1) ???
6018 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
6021 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
6027 /* ----------------------------------------------------------------------- */
6028 /* J2K / JPT decoder interface */
6029 /* ----------------------------------------------------------------------- */
6031 * Creates a J2K decompression structure.
6033 * @return a handle to a J2K decompressor if successful, NULL otherwise.
6035 opj_j2k_v2_t* j2k_create_decompress_v2()
6037 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6041 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6043 l_j2k->m_is_decoder = 1;
6044 l_j2k->m_cp.m_is_decoder = 1;
6045 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
6047 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
6051 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
6053 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6054 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
6059 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
6061 // codestream info creation
6062 l_j2k->cstr_info = (opj_codestream_info_v2_t*) opj_malloc(sizeof(opj_codestream_info_v2_t));
6063 if (!l_j2k->cstr_info){
6068 // validation list creation
6069 l_j2k->m_validation_list = opj_procedure_list_create();
6070 if (! l_j2k->m_validation_list) {
6075 // execution list creation
6076 l_j2k->m_procedure_list = opj_procedure_list_create();
6077 if (! l_j2k->m_procedure_list) {
6087 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
6088 * @param p_header_data the data contained in the COM box.
6089 * @param p_j2k the jpeg2000 codec.
6090 * @param p_header_size the size of the data contained in the COM marker.
6091 * @param p_manager the user event manager.
6093 opj_bool j2k_read_SPCod_SPCoc(
6094 opj_j2k_v2_t *p_j2k,
6096 OPJ_BYTE * p_header_data,
6097 OPJ_UINT32 * p_header_size,
6098 struct opj_event_mgr * p_manager
6101 OPJ_UINT32 i, l_tmp;
6102 opj_cp_v2_t *l_cp = NULL;
6103 opj_tcp_v2_t *l_tcp = NULL;
6104 opj_tccp_t *l_tccp = NULL;
6105 OPJ_BYTE * l_current_ptr = NULL;
6108 assert(p_j2k != 00);
6109 assert(p_manager != 00);
6110 assert(p_header_data != 00);
6112 l_cp = &(p_j2k->m_cp);
6113 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
6114 &l_cp->tcps[p_j2k->m_current_tile_number] :
6115 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6117 /* precondition again */
6118 assert(compno < p_j2k->m_image_header->numcomps);
6120 l_tccp = &l_tcp->tccps[compno];
6121 l_current_ptr = p_header_data;
6123 /* make sure room is sufficient */
6124 if (*p_header_size < 5) {
6125 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
6129 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
6130 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
6133 /* If user wants to remove more resolutions than the codestream contains, return error */
6134 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
6135 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
6136 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
6137 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;// FIXME J2K_DEC_STATE_ERR;
6141 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
6145 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
6149 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
6152 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
6155 *p_header_size = *p_header_size - 5;
6157 /* use custom precinct size ? */
6158 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
6159 if (*p_header_size < l_tccp->numresolutions) {
6160 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
6164 for (i = 0; i < l_tccp->numresolutions; ++i) {
6165 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
6167 l_tccp->prcw[i] = l_tmp & 0xf;
6168 l_tccp->prch[i] = l_tmp >> 4;
6171 *p_header_size = *p_header_size - l_tccp->numresolutions;
6174 /* set default size for the precinct width and height */
6175 for (i = 0; i < l_tccp->numresolutions; ++i) {
6176 l_tccp->prcw[i] = 15;
6177 l_tccp->prch[i] = 15;
6182 if (p_j2k->cstr_info && compno == 0) {
6183 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
6185 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
6186 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
6187 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
6188 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
6189 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
6192 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
6193 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
6200 * Copies the tile component parameters of all the component from the first tile component.
6202 * @param p_j2k the J2k codec.
6204 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
6208 opj_cp_v2_t *l_cp = NULL;
6209 opj_tcp_v2_t *l_tcp = NULL;
6210 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
6211 OPJ_UINT32 l_prc_size;
6214 assert(p_j2k != 00);
6216 l_cp = &(p_j2k->m_cp);
6217 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
6218 &l_cp->tcps[p_j2k->m_current_tile_number] :
6219 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6221 l_ref_tccp = &l_tcp->tccps[0];
6222 l_copied_tccp = l_ref_tccp + 1;
6223 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
6225 for (i=1; i<p_j2k->m_image_header->numcomps; ++i) {
6226 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
6227 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
6228 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
6229 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
6230 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
6231 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
6232 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
6238 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
6240 * @param p_comp_no the component being targeted.
6241 * @param p_header_data the data contained in the COM box.
6242 * @param p_j2k the jpeg2000 codec.
6243 * @param p_header_size the size of the data contained in the COM marker.
6244 * @param p_manager the user event manager.
6246 opj_bool j2k_read_SQcd_SQcc(
6247 opj_j2k_v2_t *p_j2k,
6248 OPJ_UINT32 p_comp_no,
6249 OPJ_BYTE* p_header_data,
6250 OPJ_UINT32 * p_header_size,
6251 struct opj_event_mgr * p_manager
6255 OPJ_UINT32 l_band_no;
6256 opj_cp_v2_t *l_cp = 00;
6257 opj_tcp_v2_t *l_tcp = 00;
6258 opj_tccp_t *l_tccp = 00;
6259 OPJ_BYTE * l_current_ptr = 00;
6260 OPJ_UINT32 l_tmp, l_num_band;
6263 assert(p_j2k != 00);
6264 assert(p_manager != 00);
6265 assert(p_header_data != 00);
6267 l_cp = &(p_j2k->m_cp);
6268 // come from tile part header or main header ?
6269 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
6270 &l_cp->tcps[p_j2k->m_current_tile_number] :
6271 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6273 // precondition again
6274 assert(p_comp_no < p_j2k->m_image_header->numcomps);
6276 l_tccp = &l_tcp->tccps[p_comp_no];
6277 l_current_ptr = p_header_data;
6279 if (*p_header_size < 1) {
6280 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
6283 *p_header_size -= 1;
6285 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
6288 l_tccp->qntsty = l_tmp & 0x1f;
6289 l_tccp->numgbits = l_tmp >> 5;
6290 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
6294 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
6296 (*p_header_size) / 2;
6298 if( l_num_band > J2K_MAXBANDS ) {
6299 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
6300 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limiting the number of elements stored to "
6301 "J2K_MAXBANDS (%d) and skip the other. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
6307 if (p_j2k->m_cp->correct) {
6309 /* if JPWL is on, we check whether there are too many subbands */
6310 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
6311 opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
6312 "JPWL: bad number of subbands in Sqcx (%d)\n",
6315 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
6318 /* we try to correct */
6320 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
6321 "- setting number of bands to %d => HYPOTHESIS!!!\n",
6326 #endif /* USE_JPWL */
6328 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
6329 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
6330 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
6332 if (l_band_no < J2K_MAXBANDS){
6333 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
6334 l_tccp->stepsizes[l_band_no].mant = 0;
6337 *p_header_size = *p_header_size - l_num_band;
6340 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
6341 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
6343 if (l_band_no < J2K_MAXBANDS){
6344 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
6345 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
6348 *p_header_size = *p_header_size - 2*l_num_band;
6351 /* Add Antonin : if scalar_derived -> compute other stepsizes */
6352 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
6353 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
6354 l_tccp->stepsizes[l_band_no].expn =
6355 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
6356 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
6357 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
6367 * Copies the tile component parameters of all the component from the first tile component.
6369 * @param p_j2k the J2k codec.
6371 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
6374 opj_cp_v2_t *l_cp = NULL;
6375 opj_tcp_v2_t *l_tcp = NULL;
6376 opj_tccp_t *l_ref_tccp = NULL;
6377 opj_tccp_t *l_copied_tccp = NULL;
6381 assert(p_j2k != 00);
6383 l_cp = &(p_j2k->m_cp);
6384 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH /* FIXME J2K_DEC_STATE_TPH*/ ? &l_cp->tcps[p_j2k->m_current_tile_number] : p_j2k->m_specific_param.m_decoder.m_default_tcp;
6385 // precondition again
6386 l_ref_tccp = &l_tcp->tccps[0];
6387 l_copied_tccp = l_ref_tccp + 1;
6388 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
6390 for (i=1;i<p_j2k->m_image_header->numcomps;++i) {
6391 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
6392 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
6393 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);