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 (
3001 opj_j2k_v2_t *p_j2k,
3002 OPJ_BYTE * p_header_data,
3003 OPJ_UINT32 p_header_size,
3004 struct opj_event_mgr * p_manager
3008 opj_cp_v2_t *l_cp = 00;
3009 opj_tcp_v2_t *l_tcp = 00;
3013 assert(p_header_data != 00);
3014 assert(p_j2k != 00);
3015 assert(p_manager != 00);
3017 if (p_header_size < 1) {
3018 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3022 l_cp = &(p_j2k->m_cp);
3023 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3026 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3032 /* First PPT marker */
3033 l_tcp->ppt_len = p_header_size;
3034 l_tcp->ppt_data_size = 0;
3035 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_tcp->ppt_len);
3036 l_tcp->ppt_data = l_tcp->ppt_buffer;
3038 if (l_tcp->ppt_buffer == 00) {
3039 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3042 memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len);
3045 l_tcp->ppt_len += p_header_size;
3046 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3048 if (l_tcp->ppt_buffer == 00) {
3049 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3053 l_tcp->ppt_data = l_tcp->ppt_buffer;
3054 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3056 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3058 l_tcp->ppt_data_size += p_header_size;
3062 static void j2k_write_tlm(opj_j2k_t *j2k){
3064 opj_cio_t *cio = j2k->cio;
3065 j2k->tlm_start = cio_tell(cio);
3066 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
3067 lenp = 4 + (5*j2k->totnum_tp);
3068 cio_write(cio,lenp,2); /* Ltlm */
3069 cio_write(cio, 0,1); /* Ztlm=0*/
3070 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
3071 cio_skip(cio,5*j2k->totnum_tp);
3074 static void j2k_write_sot(opj_j2k_t *j2k) {
3077 opj_cio_t *cio = j2k->cio;
3079 j2k->sot_start = cio_tell(cio);
3080 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
3081 lenp = cio_tell(cio);
3082 cio_skip(cio, 2); /* Lsot (further) */
3083 cio_write(cio, j2k->curtileno, 2); /* Isot */
3084 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
3085 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
3086 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
3087 len = cio_tell(cio) - lenp;
3088 cio_seek(cio, lenp);
3089 cio_write(cio, len, 2); /* Lsot */
3090 cio_seek(cio, lenp + len);
3094 /* update markers struct */
3095 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
3096 #endif /* USE_JPWL */
3099 if( j2k->cstr_info && j2k->cur_tp_num==0){
3100 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
3104 static void j2k_read_sot(opj_j2k_t *j2k) {
3105 int len, tileno, totlen, partno, numparts, i;
3106 opj_tcp_t *tcp = NULL;
3109 opj_cp_t *cp = j2k->cp;
3110 opj_cio_t *cio = j2k->cio;
3112 len = cio_read(cio, 2);
3113 tileno = cio_read(cio, 2);
3116 if (j2k->cp->correct) {
3118 static int backup_tileno = 0;
3120 /* tileno is negative or larger than the number of tiles!!! */
3121 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3122 opj_event_msg(j2k->cinfo, EVT_ERROR,
3123 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3124 tileno, (cp->tw * cp->th));
3126 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3129 /* we try to correct */
3130 tileno = backup_tileno;
3131 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3132 "- setting tile number to %d\n",
3136 /* keep your private count of tiles */
3139 #endif /* USE_JPWL */
3141 if (cp->tileno_size == 0) {
3142 cp->tileno[cp->tileno_size] = tileno;
3146 while (i < cp->tileno_size && status == 0) {
3147 status = cp->tileno[i] == tileno ? 1 : 0;
3151 cp->tileno[cp->tileno_size] = tileno;
3156 totlen = cio_read(cio, 4);
3159 if (j2k->cp->correct) {
3161 /* totlen is negative or larger than the bytes left!!! */
3162 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
3163 opj_event_msg(j2k->cinfo, EVT_ERROR,
3164 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3165 totlen, cio_numbytesleft(cio) + 8);
3167 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3170 /* we try to correct */
3172 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3173 "- setting Psot to %d => assuming it is the last tile\n",
3178 #endif /* USE_JPWL */
3181 totlen = cio_numbytesleft(cio) + 8;
3183 partno = cio_read(cio, 1);
3184 numparts = cio_read(cio, 1);
3186 if (partno >= numparts) {
3187 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);
3188 numparts = partno+1;
3191 j2k->curtileno = tileno;
3192 j2k->cur_tp_num = partno;
3193 j2k->eot = cio_getbp(cio) - 12 + totlen;
3194 j2k->state = J2K_STATE_TPH;
3195 tcp = &cp->tcps[j2k->curtileno];
3198 if (j2k->cstr_info) {
3201 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
3202 j2k->cstr_info->tile[tileno].tileno = tileno;
3203 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
3204 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3206 j2k->cstr_info->tile[tileno].end_pos += totlen;
3208 j2k->cstr_info->tile[tileno].num_tps = numparts;
3210 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
3212 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)
3213 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
3214 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3215 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3218 if (tcp->first == 1) {
3219 /* Initialization PPT */
3220 opj_tccp_t *tmp = tcp->tccps;
3221 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
3223 tcp->ppt_data = NULL;
3224 tcp->ppt_data_first = NULL;
3227 for (i = 0; i < j2k->image->numcomps; i++) {
3228 tcp->tccps[i] = j2k->default_tcp->tccps[i];
3230 cp->tcps[j2k->curtileno].first = 0;
3235 * Reads a PPT marker (Packed packet headers, tile-part header)
3237 * @param p_header_data the data contained in the PPT box.
3238 * @param p_j2k the jpeg2000 codec.
3239 * @param p_header_size the size of the data contained in the PPT marker.
3240 * @param p_manager the user event manager.
3242 opj_bool j2k_read_sot_v2 (
3243 opj_j2k_v2_t *p_j2k,
3244 OPJ_BYTE * p_header_data,
3245 OPJ_UINT32 p_header_size,
3246 struct opj_event_mgr * p_manager
3250 opj_cp_v2_t *l_cp = 00;
3251 opj_tcp_v2_t *l_tcp = 00;
3252 OPJ_UINT32 l_tot_len, l_num_parts = 0;
3253 OPJ_UINT32 l_current_part;
3254 OPJ_UINT32 l_tile_x,l_tile_y;
3257 assert(p_header_data != 00);
3258 assert(p_j2k != 00);
3259 assert(p_manager != 00);
3261 if (p_header_size != 8) {
3262 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
3266 l_cp = &(p_j2k->m_cp);
3267 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
3270 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3271 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
3272 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
3275 if (p_j2k->m_cp->correct) {
3277 static int backup_tileno = 0;
3279 /* tileno is negative or larger than the number of tiles!!! */
3280 if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
3281 opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3282 "JPWL: bad tile number (%d out of a maximum of %d)\n",
3283 tileno, (cp->tw * cp->th));
3285 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3288 /* we try to correct */
3289 tileno = backup_tileno;
3290 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3291 "- setting tile number to %d\n",
3295 /* keep your private count of tiles */
3298 #endif /* USE_JPWL */
3300 /* look for the tile in the list of already processed tile (in parts). */
3301 /* Optimization possible here with a more complex data structure and with the removing of tiles */
3302 /* since the time taken by this function can only grow at the time */
3304 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
3308 if (p_j2k->m_cp->correct) {
3310 /* totlen is negative or larger than the bytes left!!! */
3311 if ((totlen < 0) || (totlen > (p_stream_numbytesleft(p_stream) + 8))) {
3312 opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3313 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
3314 totlen, p_stream_numbytesleft(p_stream) + 8);
3316 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3319 /* we try to correct */
3321 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3322 "- setting Psot to %d => assuming it is the last tile\n",
3327 #endif /* USE_JPWL */
3330 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot read data with no size known, giving up\n");
3334 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
3337 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
3340 if (l_num_parts != 0) {
3341 l_tcp->m_nb_tile_parts = l_num_parts;
3344 if (l_tcp->m_nb_tile_parts) {
3345 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
3346 p_j2k->m_specific_param.m_decoder.m_can_decode = 1;
3350 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; // SOT_marker_size = 12
3351 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;// FIXME J2K_DEC_STATE_TPH;
3352 p_j2k->m_specific_param.m_decoder.m_skip_data =
3353 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
3354 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
3355 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
3356 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
3360 /* TODO move this onto a separate method to call before reading any SOT */
3361 /* if (p_j2k->cstr_info) {
3364 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
3367 p_j2k->cstr_info->tile[tileno].tileno = tileno;
3368 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
3369 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
3370 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
3373 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
3376 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
3380 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
3383 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
3384 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
3385 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
3390 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
3393 opj_tcp_t *tcp = NULL;
3394 opj_codestream_info_t *cstr_info = NULL;
3396 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
3397 opj_cp_t *cp = j2k->cp;
3398 opj_cio_t *cio = j2k->cio;
3400 tcd->tp_num = j2k->tp_num ;
3401 tcd->cur_tp_num = j2k->cur_tp_num;
3403 cio_write(cio, J2K_MS_SOD, 2);
3405 if( j2k->cstr_info && j2k->cur_tp_num==0){
3406 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
3409 if (j2k->curtileno == 0) {
3410 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
3414 cstr_info = j2k->cstr_info;
3416 if (!j2k->cur_tp_num ) {
3417 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3418 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
3421 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
3422 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
3426 /* update markers struct */
3427 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
3428 #endif /* USE_JPWL */
3433 tcp = &cp->tcps[j2k->curtileno];
3434 for (layno = 0; layno < tcp->numlayers; layno++) {
3435 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
3436 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
3437 } else if (tcp->rates[layno]) {
3438 tcp->rates[layno]=1;
3441 if(j2k->cur_tp_num == 0){
3442 tcd->tcd_image->tiles->packno = 0;
3444 cstr_info->packno = 0;
3447 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
3449 /* Writing Psot in SOT marker */
3450 totlen = cio_tell(cio) + l - j2k->sot_start;
3451 cio_seek(cio, j2k->sot_start + 6);
3452 cio_write(cio, totlen, 4);
3453 cio_seek(cio, j2k->sot_start + totlen);
3454 /* Writing Ttlm and Ptlm in TLM marker */
3456 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
3457 cio_write(cio, j2k->curtileno, 1);
3458 cio_write(cio, totlen, 4);
3460 cio_seek(cio, j2k->sot_start + totlen);
3463 static void j2k_read_sod(opj_j2k_t *j2k) {
3464 int len, truncate = 0, i;
3465 unsigned char *data = NULL, *data_ptr = NULL;
3467 opj_cio_t *cio = j2k->cio;
3468 int curtileno = j2k->curtileno;
3471 if (j2k->cstr_info) {
3472 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
3473 cio_tell(cio) + j2k->pos_correction - 1;
3474 if (j2k->cur_tp_num == 0)
3475 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
3476 j2k->cstr_info->packno = 0;
3479 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
3481 if (len == cio_numbytesleft(cio) + 1) {
3482 truncate = 1; /* Case of a truncate codestream */
3485 data = j2k->tile_data[curtileno];
3486 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
3488 data_ptr = data + j2k->tile_len[curtileno];
3489 for (i = 0; i < len; i++) {
3490 data_ptr[i] = cio_read(cio, 1);
3493 j2k->tile_len[curtileno] += len;
3494 j2k->tile_data[curtileno] = data;
3497 j2k->state = J2K_STATE_TPHSOT;
3499 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
3505 * Reads a SOD marker (Start Of Data)
3507 * @param p_header_data the data contained in the SOD box.
3508 * @param p_j2k the jpeg2000 codec.
3509 * @param p_header_size the size of the data contained in the SOD marker.
3510 * @param p_manager the user event manager.
3512 opj_bool j2k_read_sod_v2 (
3513 opj_j2k_v2_t *p_j2k,
3514 struct opj_stream_private *p_stream,
3515 struct opj_event_mgr * p_manager
3518 OPJ_UINT32 l_current_read_size;
3519 opj_codestream_info_v2_t * l_cstr_info = 00;
3520 OPJ_BYTE ** l_current_data = 00;
3521 opj_tcp_v2_t * l_tcp = 00;
3522 OPJ_UINT32 * l_tile_len = 00;
3525 assert(p_j2k != 00);
3526 assert(p_manager != 00);
3527 assert(p_stream != 00);
3529 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
3530 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
3532 l_current_data = &(l_tcp->m_data);
3533 l_tile_len = &l_tcp->m_data_size;
3535 if (! *l_current_data) {
3536 *l_current_data = (OPJ_BYTE*) opj_malloc/*FIXME V2 -> my_opj_malloc*/(p_j2k->m_specific_param.m_decoder.m_sot_length);
3539 *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);
3542 if (*l_current_data == 00) {
3543 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
3547 l_cstr_info = p_j2k->cstr_info;
3550 OPJ_SIZE_T l_current_pos = opj_stream_tell(p_stream)-1;
3551 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;
3553 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
3554 l_cstr_info->tile[p_j2k->m_current_tile_number].end_header = l_current_pos;
3557 l_cstr_info->packno = 0;
3560 l_current_read_size = opj_stream_read_data( p_stream,
3561 *l_current_data + *l_tile_len,
3562 p_j2k->m_specific_param.m_decoder.m_sot_length,
3565 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
3566 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC; // FIXME J2K_DEC_STATE_NEOC;
3569 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; // FIXME J2K_DEC_STATE_TPHSOT;
3572 *l_tile_len += l_current_read_size;
3578 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
3579 opj_cp_t *cp = j2k->cp;
3580 opj_tcp_t *tcp = &cp->tcps[tileno];
3581 opj_cio_t *cio = j2k->cio;
3582 int numcomps = j2k->image->numcomps;
3584 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
3585 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
3586 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
3587 cio_write(cio, 0, 1); /* Srgn */
3588 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
3591 static void j2k_read_rgn(opj_j2k_t *j2k) {
3592 int len, compno, roisty;
3594 opj_cp_t *cp = j2k->cp;
3595 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3596 opj_cio_t *cio = j2k->cio;
3597 int numcomps = j2k->image->numcomps;
3599 len = cio_read(cio, 2); /* Lrgn */
3600 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
3601 roisty = cio_read(cio, 1); /* Srgn */
3604 if (j2k->cp->correct) {
3605 /* totlen is negative or larger than the bytes left!!! */
3606 if (compno >= numcomps) {
3607 opj_event_msg(j2k->cinfo, EVT_ERROR,
3608 "JPWL: bad component number in RGN (%d when there are only %d)\n",
3610 if (!JPWL_ASSUME || JPWL_ASSUME) {
3611 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3616 #endif /* USE_JPWL */
3618 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
3621 static void j2k_write_eoc(opj_j2k_t *j2k) {
3622 opj_cio_t *cio = j2k->cio;
3623 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
3624 cio_write(cio, J2K_MS_EOC, 2);
3628 /* update markers struct */
3629 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
3630 #endif /* USE_JPWL */
3635 * Reads a RGN marker (Region Of Interest)
3637 * @param p_header_data the data contained in the POC box.
3638 * @param p_j2k the jpeg2000 codec.
3639 * @param p_header_size the size of the data contained in the POC marker.
3640 * @param p_manager the user event manager.
3642 opj_bool j2k_read_rgn_v2 (
3643 opj_j2k_v2_t *p_j2k,
3644 OPJ_BYTE * p_header_data,
3645 OPJ_UINT32 p_header_size,
3646 struct opj_event_mgr * p_manager
3649 OPJ_UINT32 l_nb_comp;
3650 opj_image_header_t * l_image = 00;
3652 opj_cp_v2_t *l_cp = 00;
3653 opj_tcp_v2_t *l_tcp = 00;
3654 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
3657 assert(p_header_data != 00);
3658 assert(p_j2k != 00);
3659 assert(p_manager != 00);
3661 l_image = p_j2k->m_image_header;
3662 l_nb_comp = l_image->numcomps;
3664 if (l_nb_comp <= 256) {
3669 if (p_header_size != 2 + l_comp_room) {
3670 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
3674 l_cp = &(p_j2k->m_cp);
3675 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH)*/
3676 &l_cp->tcps[p_j2k->m_current_tile_number] :
3677 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3679 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
3680 p_header_data+=l_comp_room;
3681 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
3685 if (p_j2k->m_cp->correct) {
3686 /* totlen is negative or larger than the bytes left!!! */
3687 if (compno >= numcomps) {
3688 opj_event_msg(p_j2k->cinfo, EVT_ERROR,
3689 "JPWL: bad component number in RGN (%d when there are only %d)\n",
3691 if (!JPWL_ASSUME || JPWL_ASSUME) {
3692 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3697 #endif /* USE_JPWL */
3699 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
3706 static void j2k_read_eoc(opj_j2k_t *j2k) {
3710 /* if packets should be decoded */
3711 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
3712 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
3713 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
3714 for (i = 0; i < j2k->cp->tileno_size; i++) {
3715 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
3716 tileno = j2k->cp->tileno[i];
3717 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
3718 opj_free(j2k->tile_data[tileno]);
3719 j2k->tile_data[tileno] = NULL;
3720 tcd_free_decode_tile(tcd, i);
3721 if (success == OPJ_FALSE) {
3722 j2k->state |= J2K_STATE_ERR;
3726 tcd_free_decode(tcd);
3729 /* if packets should not be decoded */
3731 for (i = 0; i < j2k->cp->tileno_size; i++) {
3732 tileno = j2k->cp->tileno[i];
3733 opj_free(j2k->tile_data[tileno]);
3734 j2k->tile_data[tileno] = NULL;
3737 if (j2k->state & J2K_STATE_ERR)
3738 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
3740 j2k->state = J2K_STATE_MT;
3744 * Reads a EOC marker (End Of Codestream)
3746 * @param p_header_data the data contained in the SOD box.
3747 * @param p_j2k the jpeg2000 codec.
3748 * @param p_header_size the size of the data contained in the SOD marker.
3749 * @param p_manager the user event manager.
3751 opj_bool j2k_read_eoc_v2 (
3752 opj_j2k_v2_t *p_j2k,
3753 struct opj_stream_private *p_stream,
3754 struct opj_event_mgr * p_manager
3758 opj_tcd_v2_t * l_tcd = 00;
3759 OPJ_UINT32 l_nb_tiles;
3760 opj_tcp_v2_t * l_tcp = 00;
3764 assert(p_j2k != 00);
3765 assert(p_manager != 00);
3766 assert(p_stream != 00);
3768 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3769 l_tcp = p_j2k->m_cp.tcps;
3771 l_tcd = tcd_create_v2(OPJ_TRUE);
3773 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
3777 for (i = 0; i < l_nb_tiles; ++i) {
3778 if (l_tcp->m_data) {
3779 if (! tcd_init_decode_tile(l_tcd, i)) {
3780 tcd_destroy_v2(l_tcd);
3781 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
3785 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_info);
3789 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR; // TODO MSD J2K_DEC_STATE_ERR;
3794 j2k_tcp_destroy(l_tcp);
3798 tcd_destroy_v2(l_tcd);
3802 typedef struct opj_dec_mstabent {
3805 /** value of the state when the marker can appear */
3807 /** action linked to the marker */
3808 void (*handler) (opj_j2k_t *j2k);
3809 } opj_dec_mstabent_t;
3811 opj_dec_mstabent_t j2k_dec_mstab[] = {
3812 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
3813 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
3814 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
3815 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
3816 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
3817 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
3818 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
3819 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
3820 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
3821 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
3822 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
3823 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
3824 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
3825 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
3826 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
3827 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
3829 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
3830 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
3833 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
3834 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
3835 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
3836 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
3837 #endif /* USE_JPWL */
3839 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
3840 {J2K_MS_INSEC, 0, j2k_read_insec},
3841 #endif /* USE_JPSEC */
3843 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
3846 static void j2k_read_unk(opj_j2k_t *j2k) {
3847 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
3850 if (j2k->cp->correct) {
3852 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
3853 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3854 id = cio_read(j2k->cio, 2);
3855 opj_event_msg(j2k->cinfo, EVT_ERROR,
3856 "JPWL: really don't know this marker %x\n",
3859 opj_event_msg(j2k->cinfo, EVT_ERROR,
3860 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
3863 /* OK, activate this at your own risk!!! */
3864 /* we look for the marker at the minimum hamming distance from this */
3865 while (j2k_dec_mstab[m].id) {
3867 /* 1's where they differ */
3868 tmp_id = j2k_dec_mstab[m].id ^ id;
3870 /* compute the hamming distance between our id and the current */
3872 for (i = 0; i < 16; i++) {
3873 if ((tmp_id >> i) & 0x0001) {
3878 /* if current distance is smaller, set the minimum */
3879 if (cur_dist < min_dist) {
3880 min_dist = cur_dist;
3881 min_id = j2k_dec_mstab[m].id;
3884 /* jump to the next marker */
3888 /* do we substitute the marker? */
3889 if (min_dist < JPWL_MAXIMUM_HAMMING) {
3890 opj_event_msg(j2k->cinfo, EVT_ERROR,
3891 "- marker %x is at distance %d from the read %x\n",
3892 min_id, min_dist, id);
3893 opj_event_msg(j2k->cinfo, EVT_ERROR,
3894 "- trying to substitute in place and crossing fingers!\n");
3895 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3896 cio_write(j2k->cio, min_id, 2);
3899 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
3904 #endif /* USE_JPWL */
3909 * Reads an unknown marker
3911 * @param p_stream the stream object to read from.
3912 * @param p_j2k the jpeg2000 codec.
3913 * @param p_manager the user event manager.
3915 * @return true if the marker could be deduced.
3917 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
3918 struct opj_stream_private *p_stream,
3919 struct opj_event_mgr * p_manager
3922 OPJ_BYTE l_data [2];
3923 OPJ_UINT32 l_unknown_size, l_unknown_marker;
3924 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
3927 assert(p_j2k != 00);
3928 assert(p_manager != 00);
3929 assert(p_stream != 00);
3931 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
3935 /* if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
3936 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
3940 opj_read_bytes(l_data,&l_unknown_size,2);
3941 if (l_unknown_size < 2) {
3946 if (opj_stream_skip(p_stream,l_unknown_size,p_manager) != l_unknown_size) {
3950 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
3951 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
3952 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
3956 // read 2 bytes as the new marker ID
3957 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
3959 if (!(l_unknown_marker < 0xff00)) {
3961 // Get the marker handler from the marker ID
3962 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
3964 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
3965 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
3969 if (l_marker_handler->id != J2K_MS_UNK)
3970 break; // next marker is known and well located
3979 Read the lookup table containing all the marker, status and action
3980 @param id Marker value
3982 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
3983 opj_dec_mstabent_t *e;
3984 for (e = j2k_dec_mstab; e->id != 0; e++) {
3992 /* ----------------------------------------------------------------------- */
3993 /* J2K / JPT decoder interface */
3994 /* ----------------------------------------------------------------------- */
3996 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
3997 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4001 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
4002 if(!j2k->default_tcp) {
4008 j2k->tile_data = NULL;
4013 void j2k_destroy_decompress(opj_j2k_t *j2k) {
4016 if(j2k->tile_len != NULL) {
4017 opj_free(j2k->tile_len);
4019 if(j2k->tile_data != NULL) {
4020 opj_free(j2k->tile_data);
4022 if(j2k->default_tcp != NULL) {
4023 opj_tcp_t *default_tcp = j2k->default_tcp;
4024 if(default_tcp->ppt_data_first != NULL) {
4025 opj_free(default_tcp->ppt_data_first);
4027 if(j2k->default_tcp->tccps != NULL) {
4028 opj_free(j2k->default_tcp->tccps);
4030 opj_free(j2k->default_tcp);
4032 if(j2k->cp != NULL) {
4033 opj_cp_t *cp = j2k->cp;
4034 if(cp->tcps != NULL) {
4035 for(i = 0; i < cp->tw * cp->th; i++) {
4036 if(cp->tcps[i].ppt_data_first != NULL) {
4037 opj_free(cp->tcps[i].ppt_data_first);
4039 if(cp->tcps[i].tccps != NULL) {
4040 opj_free(cp->tcps[i].tccps);
4045 if(cp->ppm_data_first != NULL) {
4046 opj_free(cp->ppm_data_first);
4048 if(cp->tileno != NULL) {
4049 opj_free(cp->tileno);
4051 if(cp->comment != NULL) {
4052 opj_free(cp->comment);
4060 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
4061 if(j2k && parameters) {
4062 /* create and initialize the coding parameters structure */
4063 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4064 cp->reduce = parameters->cp_reduce;
4065 cp->layer = parameters->cp_layer;
4066 cp->limit_decoding = parameters->cp_limit_decoding;
4069 cp->correct = parameters->jpwl_correct;
4070 cp->exp_comps = parameters->jpwl_exp_comps;
4071 cp->max_tiles = parameters->jpwl_max_tiles;
4072 #endif /* USE_JPWL */
4075 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
4080 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters) {
4081 if(j2k && parameters) {
4082 /* create and initialize the coding parameters structure */
4083 //opj_cp_v2_t *cp = (opj_cp_v2_t*) opj_calloc(1, sizeof(opj_cp_v2_t));
4084 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
4085 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
4087 /*cp->reduce = parameters->cp_reduce;
4088 cp->layer = parameters->cp_layer;
4089 cp->limit_decoding = parameters->cp_limit_decoding;*/
4093 cp->correct = parameters->jpwl_correct;
4094 cp->exp_comps = parameters->jpwl_exp_comps;
4095 cp->max_tiles = parameters->jpwl_max_tiles;
4096 #endif /* USE_JPWL */
4101 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
4102 opj_image_t *image = NULL;
4104 opj_common_ptr cinfo = j2k->cinfo;
4107 j2k->cstr_info = cstr_info;
4109 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
4111 /* create an empty image */
4112 image = opj_image_create0();
4115 j2k->state = J2K_STATE_MHSOC;
4118 opj_dec_mstabent_t *e;
4119 int id = cio_read(cio, 2);
4122 /* we try to honor JPWL correction power */
4123 if (j2k->cp->correct) {
4125 int orig_pos = cio_tell(cio);
4128 /* call the corrector */
4129 status = jpwl_correct(j2k);
4131 /* go back to where you were */
4132 cio_seek(cio, orig_pos - 2);
4134 /* re-read the marker */
4135 id = cio_read(cio, 2);
4137 /* check whether it begins with ff */
4138 if (id >> 8 != 0xff) {
4139 opj_event_msg(cinfo, EVT_ERROR,
4140 "JPWL: possible bad marker %x at %d\n",
4141 id, cio_tell(cio) - 2);
4143 opj_image_destroy(image);
4144 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
4147 /* we try to correct */
4149 cio_seek(cio, cio_tell(cio) - 2);
4150 cio_write(cio, id, 2);
4151 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
4152 "- setting marker to %x\n",
4157 #endif /* USE_JPWL */
4159 if (id >> 8 != 0xff) {
4160 opj_image_destroy(image);
4161 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
4164 e = j2k_dec_mstab_lookup(id);
4165 // Check if the marker is known
4166 if (!(j2k->state & e->states)) {
4167 opj_image_destroy(image);
4168 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
4171 // Check if the decoding is limited to the main header
4172 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
4173 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
4180 if (j2k->state & J2K_STATE_ERR)
4183 if (j2k->state == J2K_STATE_MT) {
4186 if (j2k->state == J2K_STATE_NEOC) {
4190 if (j2k->state == J2K_STATE_NEOC) {
4194 if (j2k->state != J2K_STATE_MT) {
4195 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
4201 * Read a JPT-stream and decode file
4204 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
4205 opj_image_t *image = NULL;
4206 opj_jpt_msg_header_t header;
4208 opj_common_ptr cinfo = j2k->cinfo;
4210 OPJ_ARG_NOT_USED(cstr_info);
4214 /* create an empty image */
4215 image = opj_image_create0();
4218 j2k->state = J2K_STATE_MHSOC;
4220 /* Initialize the header */
4221 jpt_init_msg_header(&header);
4222 /* Read the first header of the message */
4223 jpt_read_msg_header(cinfo, cio, &header);
4225 position = cio_tell(cio);
4226 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
4227 opj_image_destroy(image);
4228 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
4233 opj_dec_mstabent_t *e = NULL;
4236 if (!cio_numbytesleft(cio)) {
4240 /* data-bin read -> need to read a new header */
4241 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
4242 jpt_read_msg_header(cinfo, cio, &header);
4243 position = cio_tell(cio);
4244 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
4245 opj_image_destroy(image);
4246 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
4251 id = cio_read(cio, 2);
4252 if (id >> 8 != 0xff) {
4253 opj_image_destroy(image);
4254 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
4257 e = j2k_dec_mstab_lookup(id);
4258 if (!(j2k->state & e->states)) {
4259 opj_image_destroy(image);
4260 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
4266 if (j2k->state == J2K_STATE_MT) {
4269 if (j2k->state == J2K_STATE_NEOC) {
4273 if (j2k->state == J2K_STATE_NEOC) {
4277 if (j2k->state != J2K_STATE_MT) {
4278 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
4284 /* ----------------------------------------------------------------------- */
4285 /* J2K encoder interface */
4286 /* ----------------------------------------------------------------------- */
4288 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
4289 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
4296 opj_j2k_v2_t* j2k_create_compress_v2()
4298 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
4303 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
4305 l_j2k->m_is_decoder = 0;
4306 l_j2k->m_cp.m_is_decoder = 0;
4308 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
4309 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4314 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
4316 // validation list creation
4317 l_j2k->m_validation_list = opj_procedure_list_create();
4318 if (! l_j2k->m_validation_list) {
4323 // execution list creation
4324 l_j2k->m_procedure_list = opj_procedure_list_create();
4325 if (! l_j2k->m_procedure_list) {
4333 void j2k_destroy_compress(opj_j2k_t *j2k) {
4337 if(j2k->cp != NULL) {
4338 opj_cp_t *cp = j2k->cp;
4341 opj_free(cp->comment);
4344 opj_free(cp->matrice);
4346 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4347 opj_free(cp->tcps[tileno].tccps);
4356 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
4357 int i, j, tileno, numpocs_tile;
4358 opj_cp_t *cp = NULL;
4360 if(!j2k || !parameters || ! image) {
4364 /* create and initialize the coding parameters structure */
4365 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
4367 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
4370 /* set default values for cp */
4375 copy user encoding parameters
4377 cp->cinema = parameters->cp_cinema;
4378 cp->max_comp_size = parameters->max_comp_size;
4379 cp->rsiz = parameters->cp_rsiz;
4380 cp->disto_alloc = parameters->cp_disto_alloc;
4381 cp->fixed_alloc = parameters->cp_fixed_alloc;
4382 cp->fixed_quality = parameters->cp_fixed_quality;
4384 /* mod fixed_quality */
4385 if(parameters->cp_matrice) {
4386 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
4387 cp->matrice = (int *) opj_malloc(array_size);
4388 memcpy(cp->matrice, parameters->cp_matrice, array_size);
4392 cp->tdx = parameters->cp_tdx;
4393 cp->tdy = parameters->cp_tdy;
4396 cp->tx0 = parameters->cp_tx0;
4397 cp->ty0 = parameters->cp_ty0;
4399 /* comment string */
4400 if(parameters->cp_comment) {
4401 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
4403 strcpy(cp->comment, parameters->cp_comment);
4408 calculate other encoding parameters
4411 if (parameters->tile_size_on) {
4412 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
4413 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
4415 cp->tdx = image->x1 - cp->tx0;
4416 cp->tdy = image->y1 - cp->ty0;
4419 if(parameters->tp_on){
4420 cp->tp_flag = parameters->tp_flag;
4425 for(i=0;i<image->numcomps ;i++){
4426 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
4432 calculate JPWL encoding parameters
4435 if (parameters->jpwl_epc_on) {
4439 cp->epc_on = OPJ_TRUE;
4440 cp->info_on = OPJ_FALSE; /* no informative technique */
4443 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
4444 cp->epb_on = OPJ_TRUE;
4446 cp->hprot_MH = parameters->jpwl_hprot_MH;
4447 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
4448 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
4449 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
4451 /* if tile specs are not specified, copy MH specs */
4452 if (cp->hprot_TPH[0] == -1) {
4453 cp->hprot_TPH_tileno[0] = 0;
4454 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
4456 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
4457 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
4458 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
4459 cp->pprot[i] = parameters->jpwl_pprot[i];
4463 /* set ESD writing */
4464 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
4465 cp->esd_on = OPJ_TRUE;
4467 cp->sens_size = parameters->jpwl_sens_size;
4468 cp->sens_addr = parameters->jpwl_sens_addr;
4469 cp->sens_range = parameters->jpwl_sens_range;
4471 cp->sens_MH = parameters->jpwl_sens_MH;
4472 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
4473 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
4474 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
4478 /* always set RED writing to false: we are at the encoder */
4479 cp->red_on = OPJ_FALSE;
4482 cp->epc_on = OPJ_FALSE;
4484 #endif /* USE_JPWL */
4487 /* initialize the mutiple tiles */
4488 /* ---------------------------- */
4489 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
4491 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4492 opj_tcp_t *tcp = &cp->tcps[tileno];
4493 tcp->numlayers = parameters->tcp_numlayers;
4494 for (j = 0; j < tcp->numlayers; j++) {
4496 if (cp->fixed_quality) {
4497 tcp->distoratio[j] = parameters->tcp_distoratio[j];
4499 tcp->rates[j] = parameters->tcp_rates[j];
4501 if (cp->fixed_quality) { /* add fixed_quality */
4502 tcp->distoratio[j] = parameters->tcp_distoratio[j];
4504 tcp->rates[j] = parameters->tcp_rates[j];
4508 tcp->csty = parameters->csty;
4509 tcp->prg = parameters->prog_order;
4510 tcp->mct = parameters->tcp_mct;
4514 if (parameters->numpocs) {
4515 /* initialisation of POC */
4517 for (i = 0; i < parameters->numpocs; i++) {
4518 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
4519 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
4520 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
4521 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
4522 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
4523 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
4524 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
4525 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
4526 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
4530 tcp->numpocs = numpocs_tile -1 ;
4535 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
4537 for (i = 0; i < image->numcomps; i++) {
4538 opj_tccp_t *tccp = &tcp->tccps[i];
4539 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
4540 tccp->numresolutions = parameters->numresolution;
4541 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
4542 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
4543 tccp->cblksty = parameters->mode;
4544 tccp->qmfbid = parameters->irreversible ? 0 : 1;
4545 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
4547 if (i == parameters->roi_compno) {
4548 tccp->roishift = parameters->roi_shift;
4553 if(parameters->cp_cinema)
4555 //Precinct size for lowest frequency subband=128
4558 //Precinct size at all other resolutions = 256
4559 for (j = 1; j < tccp->numresolutions; j++) {
4564 if (parameters->csty & J2K_CCP_CSTY_PRT) {
4566 for (j = tccp->numresolutions - 1; j >= 0; j--) {
4567 if (p < parameters->res_spec) {
4569 if (parameters->prcw_init[p] < 1) {
4572 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
4575 if (parameters->prch_init[p] < 1) {
4578 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
4582 int res_spec = parameters->res_spec;
4583 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
4584 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
4586 if (size_prcw < 1) {
4589 tccp->prcw[j] = int_floorlog2(size_prcw);
4592 if (size_prch < 1) {
4595 tccp->prch[j] = int_floorlog2(size_prch);
4599 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
4602 for (j = 0; j < tccp->numresolutions; j++) {
4609 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
4614 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
4616 opj_cp_t *cp = NULL;
4618 opj_tcd_t *tcd = NULL; /* TCD component */
4626 j2k->cstr_info = cstr_info;
4629 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
4630 cstr_info->image_w = image->x1 - image->x0;
4631 cstr_info->image_h = image->y1 - image->y0;
4632 cstr_info->prog = (&cp->tcps[0])->prg;
4633 cstr_info->tw = cp->tw;
4634 cstr_info->th = cp->th;
4635 cstr_info->tile_x = cp->tdx; /* new version parser */
4636 cstr_info->tile_y = cp->tdy; /* new version parser */
4637 cstr_info->tile_Ox = cp->tx0; /* new version parser */
4638 cstr_info->tile_Oy = cp->ty0; /* new version parser */
4639 cstr_info->numcomps = image->numcomps;
4640 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
4641 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
4642 for (compno=0; compno < image->numcomps; compno++) {
4643 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
4645 cstr_info->D_max = 0.0; /* ADD Marcela */
4646 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
4647 cstr_info->maxmarknum = 100;
4648 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
4649 cstr_info->marknum = 0;
4659 for (compno = 1; compno < image->numcomps; compno++) {
4660 j2k_write_coc(j2k, compno);
4661 j2k_write_qcc(j2k, compno);
4665 for (compno = 0; compno < image->numcomps; compno++) {
4666 opj_tcp_t *tcp = &cp->tcps[0];
4667 if (tcp->tccps[compno].roishift)
4668 j2k_write_rgn(j2k, compno, 0);
4670 if (cp->comment != NULL) {
4674 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
4678 if (cp->cinema == CINEMA4K_24) {
4683 /* uncomment only for testing JPSEC marker writing */
4684 /* j2k_write_sec(j2k); */
4688 cstr_info->main_head_end = cio_tell(cio) - 1;
4691 /**** Main Header ENDS here ***/
4693 /* create the tile encoder */
4694 tcd = tcd_create(j2k->cinfo);
4696 /* encode each tile */
4697 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
4701 int acc_pack_num = 0;
4705 opj_tcp_t *tcp = &cp->tcps[tileno];
4706 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
4708 j2k->curtileno = tileno;
4709 j2k->cur_tp_num = 0;
4710 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
4711 /* initialisation before tile encoding */
4713 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
4715 tcd_init_encode(tcd, image, cp, j2k->curtileno);
4720 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
4721 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
4722 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
4723 cstr_info->tile[j2k->curtileno].marknum = 0;
4727 for(pino = 0; pino <= tcp->numpocs; pino++) {
4731 /*Get number of tile parts*/
4732 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
4733 tcd->tp_pos = cp->tp_pos;
4735 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
4736 j2k->tp_num = tilepartno;
4739 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
4740 cio_tell(cio) + j2k->pos_correction;
4744 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
4745 for (compno = 1; compno < image->numcomps; compno++) {
4746 j2k_write_coc(j2k, compno);
4747 j2k_write_qcc(j2k, compno);
4749 if (cp->tcps[tileno].numpocs) {
4756 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
4757 cio_tell(cio) + j2k->pos_correction + 1;
4760 j2k_write_sod(j2k, tcd);
4764 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
4765 cio_tell(cio) + j2k->pos_correction - 1;
4766 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
4768 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
4769 cstr_info->packno - acc_pack_num;
4770 acc_pack_num = cstr_info->packno;
4778 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
4783 if (tile->PPT) { // BAD PPT !!!
4786 PPT_file=fopen("PPT","rb");
4787 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
4788 for (i=0;i<tile->len_ppt;i++) {
4790 fread(&elmt, 1, 1, PPT_file);
4791 fwrite(&elmt,1,1,f);
4800 /* destroy the tile encoder */
4801 tcd_free_encode(tcd);
4804 opj_free(j2k->cur_totnum_tp);
4809 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
4811 /* The following adjustment is done to adjust the codestream size */
4812 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
4813 /* the first bunch of bytes is not in the codestream */
4814 cstr_info->codestream_size -= cstr_info->main_head_start;
4820 preparation of JPWL marker segments
4824 /* encode according to JPWL */
4825 jpwl_encode(j2k, cio, image);
4828 #endif /* USE_JPWL */
4833 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
4838 /* expand the list? */
4839 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
4840 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
4841 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
4844 /* add the marker */
4845 cstr_info->marker[cstr_info->marknum].type = type;
4846 cstr_info->marker[cstr_info->marknum].pos = pos;
4847 cstr_info->marker[cstr_info->marknum].len = len;
4848 cstr_info->marknum++;
4852 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
4855 opj_marker_info_t *marker;
4860 /* expand the list? */
4861 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
4862 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
4863 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
4866 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
4868 /* add the marker */
4869 marker->type = type;
4872 cstr_info->tile[tileno].marknum++;
4879 * -----------------------------------------------------------------------
4880 * -----------------------------------------------------------------------
4881 * -----------------------------------------------------------------------
4885 * Ends the decompression procedures and possibiliy add data to be read after the
4888 opj_bool j2k_end_decompress(
4889 opj_j2k_v2_t *p_j2k,
4890 opj_stream_private_t *p_stream,
4891 opj_event_mgr_t * p_manager)
4897 * Reads a jpeg2000 codestream header structure.
4900 * @param p_stream the stream to read data from.
4901 * @param p_j2k the jpeg2000 codec.
4902 * @param p_manager the user event manager.
4904 * @return true if the box is valid.
4906 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
4907 opj_j2k_v2_t* p_j2k,
4908 opj_file_info_t* p_file_info,
4909 struct opj_event_mgr* p_manager )
4912 assert(p_j2k != 00);
4913 assert(p_stream != 00);
4914 assert(p_manager != 00);
4916 /* create an empty image header */
4917 p_j2k->m_image_header = opj_image_header_create0();
4918 if (! p_j2k->m_image_header) {
4922 /* customization of the validation */
4923 j2k_setup_decoding_validation(p_j2k);
4925 /* validation of the parameters codec */
4926 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
4927 opj_image_header_destroy(p_j2k->m_image_header);
4928 p_j2k->m_image_header = NULL;
4932 /* customization of the encoding */
4933 j2k_setup_header_reading(p_j2k);
4936 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
4937 opj_image_header_destroy(p_j2k->m_image_header);
4938 p_j2k->m_image_header = NULL;
4942 /* If necessary copy j2k header information into the output structure */
4943 if (p_file_info->file_info_flag & OPJ_J2K_INFO){
4944 fill_cstr_info( &(p_file_info->codestream_info), p_j2k, p_manager);
4947 /* If necessary copy image header information into the output structure */
4948 if (p_file_info->file_info_flag & OPJ_IMG_INFO){
4949 fill_img_info( &(p_file_info->img_info), p_j2k, p_manager);
4956 * Fill the image info struct from information read from main header.
4958 opj_bool fill_img_info(opj_image_header_t* img_info, opj_j2k_v2_t* p_j2k, struct opj_event_mgr* p_manager)
4960 opj_image_header_t* l_image_header = p_j2k->m_image_header;
4962 img_info->x0 = l_image_header->x0;
4963 img_info->y0 = l_image_header->y0;
4964 img_info->x1 = l_image_header->x1;
4965 img_info->y1 = l_image_header->y1;
4966 img_info->numcomps = l_image_header->numcomps;
4968 if (img_info->numcomps) {
4969 img_info->comps = (opj_image_comp_header_t*)opj_malloc(img_info->numcomps * sizeof(opj_image_comp_header_t));
4970 if (!img_info->comps){
4971 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
4975 memcpy(img_info->comps, l_image_header->comps, img_info->numcomps * sizeof(opj_image_comp_header_t) );
4979 img_info->icc_profile_len = l_image_header->icc_profile_len;
4980 if (img_info->icc_profile_len) {
4981 img_info->icc_profile_buf = (unsigned char*)opj_malloc(img_info->icc_profile_len * sizeof(unsigned char));
4982 if (!img_info->icc_profile_buf){
4983 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
4987 memcpy(img_info->icc_profile_buf, l_image_header->icc_profile_buf, img_info->icc_profile_len);
4994 * Fill the codestream info struct from information read from main header.
4996 opj_bool fill_cstr_info(opj_codestream_info_v2_t* cstr_info, opj_j2k_v2_t* p_j2k, struct opj_event_mgr* p_manager)
4999 //opj_cp_v2_t l_cp = p_j2k->m_cp;
5000 opj_image_header_t* l_image_header = p_j2k->m_image_header;
5001 int it_tile, it_comp;
5002 int nb_tiles, nb_comps;
5004 cstr_info->image_w = l_image_header->x1 - l_image_header->x0;
5005 cstr_info->image_h = l_image_header->y1 - l_image_header->y0;
5006 cstr_info->numcomps = l_image_header->numcomps;
5007 cstr_info->tw = p_j2k->m_cp.tw;
5008 cstr_info->th = p_j2k->m_cp.th;
5009 cstr_info->tdx = p_j2k->m_cp.tdx;
5010 cstr_info->tdy = p_j2k->m_cp.tdy;
5011 cstr_info->tx0 = p_j2k->m_cp.tx0;
5012 cstr_info->ty0 = p_j2k->m_cp.ty0;
5014 nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
5015 nb_comps = l_image_header->numcomps;
5017 cstr_info->tile = (opj_tile_info_v2_t*) opj_calloc(nb_tiles, sizeof(opj_tile_info_v2_t));
5018 if (! p_j2k->cstr_info->tile) {
5019 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
5023 for (it_tile=0; it_tile < nb_tiles; it_tile++){
5024 cstr_info->tile[it_tile].tileno = it_tile;
5025 cstr_info->tile[it_tile].csty = p_j2k->m_cp.tcps[it_tile].csty;
5026 cstr_info->tile[it_tile].prg = p_j2k->m_cp.tcps[it_tile].prg;
5027 cstr_info->tile[it_tile].mct = p_j2k->m_cp.tcps[it_tile].mct;
5028 cstr_info->tile[it_tile].numlayers = p_j2k->m_cp.tcps[it_tile].numlayers;
5029 // FIXME Number of tile part l_cstr_info->tile[it_tile].num_tps = ;
5031 cstr_info->tile[it_tile].tccp_info =
5032 (opj_tccp_info_t*) opj_calloc(nb_comps, sizeof(opj_tccp_info_t));
5033 if (! p_j2k->cstr_info->tile[it_tile].tccp_info) {
5034 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory\n");
5038 for (it_comp=0; it_comp < nb_comps; it_comp++){
5039 cstr_info->tile[it_tile].tccp_info[it_comp].compno = it_comp;
5040 cstr_info->tile[it_tile].tccp_info[it_comp].cblkh = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblkh;
5041 cstr_info->tile[it_tile].tccp_info[it_comp].cblkw = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblkw;
5042 cstr_info->tile[it_tile].tccp_info[it_comp].cblksty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].cblksty;
5043 cstr_info->tile[it_tile].tccp_info[it_comp].csty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].csty;
5044 cstr_info->tile[it_tile].tccp_info[it_comp].numgbits = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].numgbits;
5045 cstr_info->tile[it_tile].tccp_info[it_comp].numresolutions = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].numresolutions;
5046 cstr_info->tile[it_tile].tccp_info[it_comp].qmfbid = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].qmfbid;
5047 cstr_info->tile[it_tile].tccp_info[it_comp].qntsty = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].qntsty;
5048 cstr_info->tile[it_tile].tccp_info[it_comp].roishift = p_j2k->m_cp.tcps[it_tile].tccps[it_comp].roishift;
5058 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
5060 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
5063 assert(p_j2k != 00);
5065 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_read_header_procedure);
5067 /* DEVELOPER CORNER, add your custom procedures */
5068 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(void*)j2k_copy_default_tcp_and_create_tcd);
5073 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
5074 * are valid. Developpers wanting to extend the library can add their own validation procedures.
5076 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
5079 assert(p_j2k != 00);
5081 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_build_decoder);
5082 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (void*)j2k_decoding_validation);
5083 /* DEVELOPER CORNER, add your custom validation procedure */
5088 * Builds the cp decoder parameters to use to decode tile.
5090 opj_bool j2k_build_decoder (
5091 opj_j2k_v2_t * p_j2k,
5092 opj_stream_private_t *p_stream,
5093 opj_event_mgr_t * p_manager
5096 // add here initialization of cp
5097 // copy paste of setup_decoder
5102 * The default decoding validation procedure without any extension.
5104 * @param p_j2k the jpeg2000 codec to validate.
5105 * @param p_stream the input stream to validate.
5106 * @param p_manager the user event manager.
5108 * @return true if the parameters are correct.
5110 opj_bool j2k_decoding_validation (
5111 opj_j2k_v2_t *p_j2k,
5112 opj_stream_private_t *p_stream,
5113 opj_event_mgr_t * p_manager
5116 opj_bool l_is_valid = OPJ_TRUE;
5119 assert(p_j2k != 00);
5120 assert(p_stream != 00);
5121 assert(p_manager != 00);
5124 /* STATE checking */
5125 /* make sure the state is at 0 */
5127 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
5129 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
5131 /* POINTER validation */
5132 /* make sure a p_j2k codec is present */
5133 /* make sure a procedure list is present */
5134 l_is_valid &= (p_j2k->m_procedure_list != 00);
5135 /* make sure a validation list is present */
5136 l_is_valid &= (p_j2k->m_validation_list != 00);
5138 /* PARAMETER VALIDATION */
5142 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
5143 struct opj_stream_private *p_stream,
5144 struct opj_event_mgr * p_manager)
5146 OPJ_UINT32 l_current_marker;
5147 OPJ_UINT32 l_marker_size;
5148 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
5151 assert(p_stream != 00);
5152 assert(p_j2k != 00);
5153 assert(p_manager != 00);
5155 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC; // The codestream must begin with SOC marker FIXME J2K_DEC_STATE_MHSOC
5157 // Try to read the SOC marker
5158 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
5159 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
5163 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5164 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5165 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5169 // Read 2 bytes as the new marker ID
5170 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5172 // Try to read until the SOT is detected
5173 while (l_current_marker != J2K_MS_SOT) {
5175 // Check if the current marker ID is valid
5176 if (l_current_marker < 0xff00) {
5177 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
5181 // Get the marker handler from the marker ID
5182 l_marker_handler = j2k_get_marker_handler(l_current_marker);
5184 // Check if the marker is known and if it is the right place to find it
5185 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5186 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5190 // Manage case where marker is unknown
5191 if (l_marker_handler->id == J2K_MS_UNK) {
5193 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5195 // Try to detect the next valid marker
5197 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5198 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5199 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5203 // Read 2 bytes as the new marker ID
5204 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5206 // Check if the current marker ID is valid
5207 if ( !(l_current_marker < 0xff00) ) {
5208 // Get the marker handler from the marker ID
5209 l_marker_handler = j2k_get_marker_handler(l_current_marker);
5211 if ( !(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
5212 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5216 if (l_marker_handler->id != J2K_MS_UNK)
5217 break; // next marker is known and well located
5221 if (l_current_marker == J2K_MS_SOT)
5222 break; // SOT marker is detected main header is completely read.
5225 // Try to read 2 bytes (the marker size) from stream and copy them into the buffer
5226 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5227 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5231 // read 2 bytes as the marker size
5232 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
5233 l_marker_size -= 2; // Subtract the size of the marker ID already read
5235 // Check if the marker size is compatible with the header data size
5236 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
5237 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
5238 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
5239 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
5242 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
5245 // Try to read the rest of the marker segment from stream and copy them into the buffer
5246 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) {
5247 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5251 // Read the marker segment with the correct marker handler
5252 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
5253 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
5257 // Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer
5258 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5259 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5263 // read 2 bytes as the new marker ID
5264 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5267 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; // FIXME J2K_DEC_STATE_TPHSOT
5272 * Excutes the given procedures on the given codec.
5274 * @param p_procedure_list the list of procedures to execute
5275 * @param p_j2k the jpeg2000 codec to execute the procedures on.
5276 * @param p_stream the stream to execute the procedures on.
5277 * @param p_manager the user manager.
5279 * @return true if all the procedures were successfully executed.
5282 opj_j2k_v2_t * p_j2k,
5283 opj_procedure_list_t * p_procedure_list,
5284 opj_stream_private_t *p_stream,
5285 opj_event_mgr_t * p_manager
5288 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
5289 opj_bool l_result = OPJ_TRUE;
5290 OPJ_UINT32 l_nb_proc, i;
5293 assert(p_procedure_list != 00);
5294 assert(p_j2k != 00);
5295 assert(p_stream != 00);
5296 assert(p_manager != 00);
5299 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
5300 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);
5302 for (i=0;i<l_nb_proc;++i) {
5303 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
5307 // and clear the procedure list at the end.
5308 opj_procedure_list_clear(p_procedure_list);
5313 opj_bool j2k_copy_default_tcp_and_create_tcd
5315 opj_j2k_v2_t * p_j2k,
5316 opj_stream_private_t *p_stream,
5317 opj_event_mgr_t * p_manager
5320 opj_tcp_v2_t * l_tcp = 00;
5321 opj_tcp_v2_t * l_default_tcp = 00;
5322 OPJ_UINT32 l_nb_tiles;
5324 opj_tccp_t *l_current_tccp = 00;
5325 OPJ_UINT32 l_tccp_size;
5326 OPJ_UINT32 l_mct_size;
5327 opj_image_header_t * l_image;
5328 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
5329 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
5330 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
5331 OPJ_UINT32 l_offset;
5333 // preconditions in debug
5334 assert(p_j2k != 00);
5335 assert(p_stream != 00);
5336 assert(p_manager != 00);
5339 l_image = p_j2k->m_image_header;
5340 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5341 l_tcp = p_j2k->m_cp.tcps;
5342 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
5343 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
5344 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
5346 (i=0;i<l_nb_tiles;++i)
5348 l_current_tccp = l_tcp->tccps;
5349 memcpy(l_tcp,l_default_tcp, sizeof(opj_tcp_v2_t));
5351 l_tcp->ppt_data = 00;
5352 l_tcp->tccps = l_current_tccp;
5354 (l_default_tcp->m_mct_decoding_matrix)
5356 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5358 (! l_tcp->m_mct_decoding_matrix )
5362 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
5364 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
5365 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
5367 (! l_tcp->m_mct_records)
5371 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
5372 l_src_mct_rec = l_default_tcp->m_mct_records;
5373 l_dest_mct_rec = l_tcp->m_mct_records;
5375 (j=0;j<l_default_tcp->m_nb_mct_records;++j)
5378 (l_src_mct_rec->m_data)
5380 l_dest_mct_rec->m_data = (OPJ_BYTE*)
5381 opj_malloc(l_src_mct_rec->m_data_size);
5383 (! l_dest_mct_rec->m_data)
5387 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
5392 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
5393 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
5394 opj_malloc(l_mcc_records_size);
5396 (! l_tcp->m_mcc_records)
5400 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
5401 l_src_mcc_rec = l_default_tcp->m_mcc_records;
5402 l_dest_mcc_rec = l_tcp->m_mcc_records;
5404 (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j)
5407 (l_src_mcc_rec->m_decorrelation_array)
5409 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
5410 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
5413 (l_src_mcc_rec->m_offset_array)
5415 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
5416 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
5421 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
5424 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); // FIXME why a cast ?
5431 (! tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)))
5433 tcd_destroy_v2(p_j2k->m_tcd);
5435 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5442 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
5443 * with the marker value.
5444 * @param p_id Marker value to look up
5446 * @return the handler associated with the id.
5448 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (const OPJ_UINT32 p_id)
5450 const opj_dec_memory_marker_handler_t *e;
5451 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
5452 if (e->id == p_id) {
5453 break; // we find a handler corresponding to the marker ID
5461 * Destroys a jpeg2000 codec.
5463 * @param p_j2k the jpeg20000 structure to destroy.
5465 void j2k_destroy (opj_j2k_v2_t *p_j2k)
5474 (p_j2k->m_is_decoder)
5477 (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00)
5479 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
5480 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
5481 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
5484 (p_j2k->m_specific_param.m_decoder.m_header_data != 00)
5486 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
5487 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
5488 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
5495 (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data)
5497 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
5498 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
5501 (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer)
5503 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
5504 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
5505 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
5508 (p_j2k->m_specific_param.m_encoder.m_header_tile_data)
5510 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
5511 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
5512 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
5515 tcd_destroy_v2(p_j2k->m_tcd);
5517 j2k_cp_destroy(&(p_j2k->m_cp));
5518 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_t));
5520 opj_procedure_list_destroy(p_j2k->m_procedure_list);
5521 p_j2k->m_procedure_list = 00;
5523 opj_procedure_list_destroy(p_j2k->m_validation_list);
5524 p_j2k->m_procedure_list = 00;
5532 * Destroys a tile coding parameter structure.
5534 * @param p_tcp the tile coding parameter to destroy.
5536 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
5544 (p_tcp->ppt_buffer != 00)
5546 opj_free(p_tcp->ppt_buffer);
5547 p_tcp->ppt_buffer = 00;
5550 (p_tcp->tccps != 00)
5552 opj_free(p_tcp->tccps);
5556 (p_tcp->m_mct_coding_matrix != 00)
5558 opj_free(p_tcp->m_mct_coding_matrix);
5559 p_tcp->m_mct_coding_matrix = 00;
5562 (p_tcp->m_mct_decoding_matrix != 00)
5564 opj_free(p_tcp->m_mct_decoding_matrix);
5565 p_tcp->m_mct_decoding_matrix = 00;
5568 (p_tcp->m_mcc_records)
5570 opj_free(p_tcp->m_mcc_records);
5571 p_tcp->m_mcc_records = 00;
5572 p_tcp->m_nb_max_mcc_records = 0;
5573 p_tcp->m_nb_mcc_records = 0;
5576 (p_tcp->m_mct_records)
5578 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
5581 (i=0;i<p_tcp->m_nb_mct_records;++i)
5584 (l_mct_data->m_data)
5586 opj_free(l_mct_data->m_data);
5587 l_mct_data->m_data = 00;
5591 opj_free(p_tcp->m_mct_records);
5592 p_tcp->m_mct_records = 00;
5596 (p_tcp->mct_norms != 00)
5598 opj_free(p_tcp->mct_norms);
5599 p_tcp->mct_norms = 00;
5604 opj_free(p_tcp->m_data);
5611 * Destroys a coding parameter structure.
5613 * @param p_cp the coding parameter to destroy.
5615 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
5617 OPJ_UINT32 l_nb_tiles;
5618 opj_tcp_v2_t * l_current_tile = 00;
5629 l_current_tile = p_cp->tcps;
5630 l_nb_tiles = p_cp->th * p_cp->tw;
5633 (i = 0; i < l_nb_tiles; ++i)
5635 j2k_tcp_destroy(l_current_tile);
5638 opj_free(p_cp->tcps);
5642 (p_cp->ppm_buffer != 00)
5644 opj_free(p_cp->ppm_buffer);
5645 p_cp->ppm_buffer = 00;
5648 (p_cp->comment != 00)
5650 opj_free(p_cp->comment);
5654 (! p_cp->m_is_decoder)
5657 (p_cp->m_specific_param.m_enc.m_matrice)
5659 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
5660 p_cp->m_specific_param.m_enc.m_matrice = 00;
5668 * Reads a tile header.
5669 * @param p_j2k the jpeg2000 codec.
5670 * @param p_stream the stream to write data to.
5671 * @param p_manager the user event manager.
5673 opj_bool j2k_read_tile_header (
5674 opj_j2k_v2_t * p_j2k,
5675 OPJ_UINT32 * p_tile_index,
5676 OPJ_UINT32 * p_data_size,
5677 OPJ_INT32 * p_tile_x0,
5678 OPJ_INT32 * p_tile_y0,
5679 OPJ_INT32 * p_tile_x1,
5680 OPJ_INT32 * p_tile_y1,
5681 OPJ_UINT32 * p_nb_comps,
5683 opj_stream_private_t *p_stream,
5684 opj_event_mgr_t * p_manager
5687 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
5688 OPJ_UINT32 l_marker_size;
5689 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
5690 opj_tcp_v2_t * l_tcp = 00;
5691 OPJ_UINT32 l_nb_tiles;
5694 assert(p_stream != 00);
5695 assert(p_j2k != 00);
5696 assert(p_manager != 00);
5699 (p_j2k->m_specific_param.m_decoder.m_state == 0x0100)// FIXME J2K_DEC_STATE_EOC)
5701 l_current_marker = J2K_MS_EOC;
5704 (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) // FIXME J2K_DEC_STATE_TPHSOT)
5710 (! p_j2k->m_specific_param.m_decoder.m_can_decode && l_current_marker != J2K_MS_EOC)
5713 (l_current_marker != J2K_MS_SOD)
5716 (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2)
5718 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5721 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
5723 (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH) // FIXME J2K_DEC_STATE_TPH)
5725 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
5729 l_marker_handler = j2k_get_marker_handler(l_current_marker);
5730 // Check if the marker is known
5732 (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) )
5734 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5738 (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size)
5740 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
5741 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
5743 (p_j2k->m_specific_param.m_decoder.m_header_data == 00)
5747 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
5751 (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager) != l_marker_size)
5753 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5757 (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager))
5759 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5763 (p_j2k->m_specific_param.m_decoder.m_skip_data)
5766 (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)
5768 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5771 l_current_marker = J2K_MS_SOD;
5776 (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2)
5778 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5781 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5786 (! p_j2k->m_specific_param.m_decoder.m_skip_data)
5789 (! j2k_read_sod_v2(p_j2k,p_stream,p_manager))
5796 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
5797 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
5798 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT; // FIXME J2K_DEC_STATE_TPHSOT;
5800 (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2)
5802 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5805 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
5810 (l_current_marker == J2K_MS_EOC)
5813 (p_j2k->m_specific_param.m_decoder.m_state != 0x0100 )// FIXME J2K_DEC_STATE_EOC)
5815 p_j2k->m_current_tile_number = 0;
5816 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;// FIXMEJ2K_DEC_STATE_EOC;
5820 ( ! p_j2k->m_specific_param.m_decoder.m_can_decode)
5822 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
5823 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5826 (p_j2k->m_current_tile_number < l_nb_tiles)
5827 && (l_tcp->m_data == 00)
5830 ++p_j2k->m_current_tile_number;
5834 (p_j2k->m_current_tile_number == l_nb_tiles)
5836 *p_go_on = OPJ_FALSE;
5841 (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number))
5843 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5846 *p_tile_index = p_j2k->m_current_tile_number;
5847 *p_go_on = OPJ_TRUE;
5848 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
5849 * p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
5850 * p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
5851 * p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
5852 * p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
5853 * p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
5854 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;// FIXME J2K_DEC_STATE_DATA;
5858 opj_bool j2k_decode_tile (
5859 opj_j2k_v2_t * p_j2k,
5860 OPJ_UINT32 p_tile_index,
5862 OPJ_UINT32 p_data_size,
5863 opj_stream_private_t *p_stream,
5864 opj_event_mgr_t * p_manager
5867 OPJ_UINT32 l_current_marker;
5868 OPJ_BYTE l_data [2];
5869 opj_tcp_v2_t * l_tcp;
5872 assert(p_stream != 00);
5873 assert(p_j2k != 00);
5874 assert(p_manager != 00);
5877 (! (p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/) || p_tile_index != p_j2k->m_current_tile_number)
5881 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
5885 j2k_tcp_destroy(&(p_j2k->m_cp.tcps[p_tile_index]));
5889 (! tcd_decode_tile_v2(p_j2k->m_tcd, l_tcp->m_data, l_tcp->m_data_size, p_tile_index, p_j2k->cstr_info))
5891 j2k_tcp_destroy(l_tcp);
5892 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;//FIXME J2K_DEC_STATE_ERR;
5896 (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size))
5900 j2k_tcp_destroy(l_tcp);
5901 p_j2k->m_tcd->tcp = 0;
5903 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
5904 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));// FIXME J2K_DEC_STATE_DATA);
5906 (p_j2k->m_specific_param.m_decoder.m_state != 0x0100)//FIXME J2K_DEC_STATE_EOC)
5909 (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2)
5911 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5914 opj_read_bytes(l_data,&l_current_marker,2);
5916 (l_current_marker == J2K_MS_EOC)
5918 p_j2k->m_current_tile_number = 0;
5919 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;//FIXME J2K_DEC_STATE_EOC;
5922 (l_current_marker != J2K_MS_SOT)
5924 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
5933 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
5935 * @param p_j2k the jpeg2000 codec.
5936 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
5937 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
5938 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
5939 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
5940 * @param p_manager the user event manager
5942 * @return true if the area could be set.
5944 opj_bool j2k_set_decode_area(
5945 opj_j2k_v2_t *p_j2k,
5946 OPJ_INT32 p_start_x,
5947 OPJ_INT32 p_start_y,
5950 struct opj_event_mgr * p_manager
5953 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
5956 (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT)// FIXME J2K_DEC_STATE_TPHSOT)
5960 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
5961 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
5962 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
5963 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
5964 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
5969 /* ----------------------------------------------------------------------- */
5970 /* J2K / JPT decoder interface */
5971 /* ----------------------------------------------------------------------- */
5973 * Creates a J2K decompression structure.
5975 * @return a handle to a J2K decompressor if successful, NULL otherwise.
5977 opj_j2k_v2_t* j2k_create_decompress_v2()
5979 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
5983 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
5985 l_j2k->m_is_decoder = 1;
5986 l_j2k->m_cp.m_is_decoder = 1;
5987 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
5989 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
5993 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
5995 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
5996 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
6001 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
6003 // codestream info creation
6004 l_j2k->cstr_info = (opj_codestream_info_v2_t*) opj_malloc(sizeof(opj_codestream_info_v2_t));
6005 if (!l_j2k->cstr_info){
6010 // validation list creation
6011 l_j2k->m_validation_list = opj_procedure_list_create();
6012 if (! l_j2k->m_validation_list) {
6017 // execution list creation
6018 l_j2k->m_procedure_list = opj_procedure_list_create();
6019 if (! l_j2k->m_procedure_list) {
6029 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
6030 * @param p_header_data the data contained in the COM box.
6031 * @param p_j2k the jpeg2000 codec.
6032 * @param p_header_size the size of the data contained in the COM marker.
6033 * @param p_manager the user event manager.
6035 opj_bool j2k_read_SPCod_SPCoc(
6036 opj_j2k_v2_t *p_j2k,
6038 OPJ_BYTE * p_header_data,
6039 OPJ_UINT32 * p_header_size,
6040 struct opj_event_mgr * p_manager
6043 OPJ_UINT32 i, l_tmp;
6044 opj_cp_v2_t *l_cp = NULL;
6045 opj_tcp_v2_t *l_tcp = NULL;
6046 opj_tccp_t *l_tccp = NULL;
6047 OPJ_BYTE * l_current_ptr = NULL;
6050 assert(p_j2k != 00);
6051 assert(p_manager != 00);
6052 assert(p_header_data != 00);
6054 l_cp = &(p_j2k->m_cp);
6055 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
6056 &l_cp->tcps[p_j2k->m_current_tile_number] :
6057 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6059 /* precondition again */
6060 assert(compno < p_j2k->m_image_header->numcomps);
6062 l_tccp = &l_tcp->tccps[compno];
6063 l_current_ptr = p_header_data;
6065 /* make sure room is sufficient */
6066 if (*p_header_size < 5) {
6067 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
6071 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
6072 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
6075 /* If user wants to remove more resolutions than the codestream contains, return error */
6076 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
6077 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
6078 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
6079 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;// FIXME J2K_DEC_STATE_ERR;
6083 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
6087 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
6091 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
6094 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
6097 *p_header_size = *p_header_size - 5;
6099 /* use custom precinct size ? */
6100 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
6101 if (*p_header_size < l_tccp->numresolutions) {
6102 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
6106 for (i = 0; i < l_tccp->numresolutions; ++i) {
6107 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
6109 l_tccp->prcw[i] = l_tmp & 0xf;
6110 l_tccp->prch[i] = l_tmp >> 4;
6113 *p_header_size = *p_header_size - l_tccp->numresolutions;
6116 /* set default size for the precinct width and height */
6117 for (i = 0; i < l_tccp->numresolutions; ++i) {
6118 l_tccp->prcw[i] = 15;
6119 l_tccp->prch[i] = 15;
6124 if (p_j2k->cstr_info && compno == 0) {
6125 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
6127 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
6128 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
6129 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
6130 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
6131 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
6134 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
6135 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
6142 * Copies the tile component parameters of all the component from the first tile component.
6144 * @param p_j2k the J2k codec.
6146 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
6150 opj_cp_v2_t *l_cp = NULL;
6151 opj_tcp_v2_t *l_tcp = NULL;
6152 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
6153 OPJ_UINT32 l_prc_size;
6156 assert(p_j2k != 00);
6158 l_cp = &(p_j2k->m_cp);
6159 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
6160 &l_cp->tcps[p_j2k->m_current_tile_number] :
6161 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6163 l_ref_tccp = &l_tcp->tccps[0];
6164 l_copied_tccp = l_ref_tccp + 1;
6165 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
6167 for (i=1; i<p_j2k->m_image_header->numcomps; ++i) {
6168 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
6169 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
6170 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
6171 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
6172 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
6173 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
6174 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
6180 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
6182 * @param p_comp_no the component being targeted.
6183 * @param p_header_data the data contained in the COM box.
6184 * @param p_j2k the jpeg2000 codec.
6185 * @param p_header_size the size of the data contained in the COM marker.
6186 * @param p_manager the user event manager.
6188 opj_bool j2k_read_SQcd_SQcc(
6189 opj_j2k_v2_t *p_j2k,
6190 OPJ_UINT32 p_comp_no,
6191 OPJ_BYTE* p_header_data,
6192 OPJ_UINT32 * p_header_size,
6193 struct opj_event_mgr * p_manager
6197 OPJ_UINT32 l_band_no;
6198 opj_cp_v2_t *l_cp = 00;
6199 opj_tcp_v2_t *l_tcp = 00;
6200 opj_tccp_t *l_tccp = 00;
6201 OPJ_BYTE * l_current_ptr = 00;
6202 OPJ_UINT32 l_tmp, l_num_band;
6205 assert(p_j2k != 00);
6206 assert(p_manager != 00);
6207 assert(p_header_data != 00);
6209 l_cp = &(p_j2k->m_cp);
6210 // come from tile part header or main header ?
6211 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
6212 &l_cp->tcps[p_j2k->m_current_tile_number] :
6213 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6215 // precondition again
6216 assert(p_comp_no < p_j2k->m_image_header->numcomps);
6218 l_tccp = &l_tcp->tccps[p_comp_no];
6219 l_current_ptr = p_header_data;
6221 if (*p_header_size < 1) {
6222 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
6225 *p_header_size -= 1;
6227 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
6230 l_tccp->qntsty = l_tmp & 0x1f;
6231 l_tccp->numgbits = l_tmp >> 5;
6232 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
6236 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
6238 (*p_header_size) / 2;
6240 if( l_num_band > J2K_MAXBANDS ) {
6241 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
6242 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limiting the number of elements stored to "
6243 "J2K_MAXBANDS (%d) and skip the other. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
6249 if (p_j2k->m_cp->correct) {
6251 /* if JPWL is on, we check whether there are too many subbands */
6252 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
6253 opj_event_msg(p_j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
6254 "JPWL: bad number of subbands in Sqcx (%d)\n",
6257 opj_event_msg(p_j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
6260 /* we try to correct */
6262 opj_event_msg(p_j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
6263 "- setting number of bands to %d => HYPOTHESIS!!!\n",
6268 #endif /* USE_JPWL */
6270 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
6271 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
6272 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
6274 if (l_band_no < J2K_MAXBANDS){
6275 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
6276 l_tccp->stepsizes[l_band_no].mant = 0;
6279 *p_header_size = *p_header_size - l_num_band;
6282 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
6283 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
6285 if (l_band_no < J2K_MAXBANDS){
6286 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
6287 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
6290 *p_header_size = *p_header_size - 2*l_num_band;
6293 /* Add Antonin : if scalar_derived -> compute other stepsizes */
6294 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
6295 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
6296 l_tccp->stepsizes[l_band_no].expn =
6297 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
6298 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
6299 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
6309 * Copies the tile component parameters of all the component from the first tile component.
6311 * @param p_j2k the J2k codec.
6313 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
6316 opj_cp_v2_t *l_cp = NULL;
6317 opj_tcp_v2_t *l_tcp = NULL;
6318 opj_tccp_t *l_ref_tccp = NULL;
6319 opj_tccp_t *l_copied_tccp = NULL;
6323 assert(p_j2k != 00);
6325 l_cp = &(p_j2k->m_cp);
6326 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;
6327 // precondition again
6328 l_ref_tccp = &l_tcp->tccps[0];
6329 l_copied_tccp = l_ref_tccp + 1;
6330 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
6332 for (i=1;i<p_j2k->m_image_header->numcomps;++i) {
6333 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
6334 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
6335 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);