2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9 * Copyright (c) 2006-2007, Parvatha Elangovan
10 * Copyright (c) 2010-2011, Kaori Hagihara
11 * Copyright (c) 2011-2012, Mickael Savinaud, Communications & Systemes <mickael.savinaud@c-s.fr>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include "opj_includes.h"
38 /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
41 /** @name Local static functions */
45 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
47 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 static opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
53 opj_stream_private_t *p_stream,
54 opj_event_mgr_t * p_manager);
57 * The default encoding validation procedure without any extension.
59 * @param p_j2k the jpeg2000 codec to validate.
60 * @param p_stream the input stream to validate.
61 * @param p_manager the user event manager.
63 * @return true if the parameters are correct.
65 static opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
66 opj_stream_private_t *p_stream,
67 opj_event_mgr_t * p_manager );
70 * The default decoding validation procedure without any extension.
72 * @param p_j2k the jpeg2000 codec to validate.
73 * @param p_stream the input stream to validate.
74 * @param p_manager the user event manager.
76 * @return true if the parameters are correct.
78 static opj_bool j2k_decoding_validation ( opj_j2k_v2_t * p_j2k,
79 opj_stream_private_t *p_stream,
80 opj_event_mgr_t * p_manager );
83 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
84 * are valid. Developpers wanting to extend the library can add their own validation procedures.
86 static void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k);
89 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
90 * are valid. Developpers wanting to extend the library can add their own validation procedures.
92 static void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k);
95 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
96 * are valid. Developpers wanting to extend the library can add their own validation procedures.
98 static void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k);
101 * The mct encoding validation procedure.
103 * @param p_j2k the jpeg2000 codec to validate.
104 * @param p_stream the input stream to validate.
105 * @param p_manager the user event manager.
107 * @return true if the parameters are correct.
109 static opj_bool j2k_mct_validation (opj_j2k_v2_t * p_j2k,
110 opj_stream_private_t *p_stream,
111 opj_event_mgr_t * p_manager );
114 * Builds the tcd decoder to use to decode tile.
116 static opj_bool j2k_build_decoder ( opj_j2k_v2_t * p_j2k,
117 opj_stream_private_t *p_stream,
118 opj_event_mgr_t * p_manager );
120 * Builds the tcd encoder to use to encode tile.
122 static opj_bool j2k_build_encoder ( opj_j2k_v2_t * p_j2k,
123 opj_stream_private_t *p_stream,
124 opj_event_mgr_t * p_manager );
127 * Creates a tile-coder decoder.
129 * @param p_stream the stream to write data to.
130 * @param p_j2k J2K codec.
131 * @param p_manager the user event manager.
133 static opj_bool j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134 struct opj_stream_private *p_stream,
135 struct opj_event_mgr * p_manager );
138 * Excutes the given procedures on the given codec.
140 * @param p_procedure_list the list of procedures to execute
141 * @param p_j2k the jpeg2000 codec to execute the procedures on.
142 * @param p_stream the stream to execute the procedures on.
143 * @param p_manager the user manager.
145 * @return true if all the procedures were successfully executed.
147 static opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
148 opj_procedure_list_t * p_procedure_list,
149 opj_stream_private_t *p_stream,
150 opj_event_mgr_t * p_manager);
153 * Updates the rates of the tcp.
155 * @param p_stream the stream to write data to.
156 * @param p_j2k J2K codec.
157 * @param p_manager the user event manager.
159 static opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
160 opj_stream_private_t *p_stream,
161 opj_event_mgr_t * p_manager );
164 * Copies the decoding tile parameters onto all the tile parameters.
165 * Creates also the tile decoder.
167 static opj_bool j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
168 opj_stream_private_t *p_stream,
169 opj_event_mgr_t * p_manager );
172 * Destroys the memory associated with the decoding of headers.
174 static opj_bool j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
175 opj_stream_private_t *p_stream,
176 opj_event_mgr_t * p_manager );
179 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
180 * with the marker value.
181 * @param p_id Marker value to look up
183 * @return the handler associated with the id.
185 static const struct opj_dec_memory_marker_handler * j2k_get_marker_handler (OPJ_UINT32 p_id);
188 * Destroys a tile coding parameter structure.
190 * @param p_tcp the tile coding parameter to destroy.
192 static void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp);
195 * Destroys the data inside a tile coding parameter structure.
197 * @param p_tcp the tile coding parameter which contain data to destroy.
199 static void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp);
202 * Destroys a coding parameter structure.
204 * @param p_cp the coding parameter to destroy.
206 static void j2k_cp_destroy (opj_cp_v2_t *p_cp);
210 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
212 * @param p_comp_no the component number to output.
213 * @param p_stream the stream to write data to.
214 * @param p_j2k J2K codec.
215 * @param p_manager the user event manager.
218 static opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
219 OPJ_UINT32 p_tile_no,
220 OPJ_UINT32 p_comp_no,
222 OPJ_UINT32 * p_header_size,
223 struct opj_event_mgr * p_manager );
226 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
228 * @param p_tile_no the tile index.
229 * @param p_comp_no the component being outputted.
230 * @param p_j2k the J2K codec.
232 * @return the number of bytes taken by the SPCod element.
234 static OPJ_UINT32 j2k_get_SPCod_SPCoc_size (opj_j2k_v2_t *p_j2k,
235 OPJ_UINT32 p_tile_no,
236 OPJ_UINT32 p_comp_no );
239 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
240 * @param p_header_data the data contained in the COM box.
241 * @param p_j2k the jpeg2000 codec.
242 * @param p_header_size the size of the data contained in the COM marker.
243 * @param p_manager the user event manager.
245 static opj_bool j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
247 OPJ_BYTE * p_header_data,
248 OPJ_UINT32 * p_header_size,
249 opj_event_mgr_t * p_manager );
252 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
254 * @param p_tile_no the tile index.
255 * @param p_comp_no the component being outputted.
256 * @param p_j2k the J2K codec.
258 * @return the number of bytes taken by the SPCod element.
260 static OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
261 OPJ_UINT32 p_tile_no,
262 OPJ_UINT32 p_comp_no );
265 * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
267 * @param p_tile_no the tile to output.
268 * @param p_comp_no the component number to output.
269 * @param p_data the data buffer.
270 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
271 * @param p_j2k J2K codec.
272 * @param p_manager the user event manager.
275 static opj_bool j2k_write_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
276 OPJ_UINT32 p_tile_no,
277 OPJ_UINT32 p_comp_no,
279 OPJ_UINT32 * p_header_size,
280 opj_event_mgr_t * p_manager);
283 * Updates the Tile Length Marker.
285 static void j2k_update_tlm ( opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size);
288 * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
290 * @param p_tile_no the tile to output.
291 * @param p_comp_no the component number to output.
292 * @param p_data the data buffer.
293 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
294 * @param p_j2k J2K codec.
295 * @param p_manager the user event manager.
298 static opj_bool j2k_read_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
300 OPJ_BYTE * p_header_data,
301 OPJ_UINT32 * p_header_size,
302 opj_event_mgr_t * p_manager );
305 * Copies the tile component parameters of all the component from the first tile component.
307 * @param p_j2k the J2k codec.
309 static void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k );
312 * Copies the tile quantization parameters of all the component from the first tile component.
314 * @param p_j2k the J2k codec.
316 static void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
321 static opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
322 opj_stream_private_t *p_stream,
323 opj_event_mgr_t * p_manager);
326 static opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
327 OPJ_UINT32 p_tile_index,
328 opj_stream_private_t *p_stream,
329 opj_event_mgr_t * p_manager );
331 static opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
333 static void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
335 static opj_bool j2k_post_write_tile (opj_j2k_v2_t * p_j2k,
337 OPJ_UINT32 p_data_size,
338 opj_stream_private_t *p_stream,
339 opj_event_mgr_t * p_manager );
342 * Sets up the procedures to do on writing header.
343 * Developers wanting to extend the library can add their own writing procedures.
345 static void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
347 static opj_bool j2k_write_first_tile_part( opj_j2k_v2_t *p_j2k,
349 OPJ_UINT32 * p_data_written,
350 OPJ_UINT32 p_total_data_size,
351 opj_stream_private_t *p_stream,
352 struct opj_event_mgr * p_manager );
354 static opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
356 OPJ_UINT32 * p_data_written,
357 OPJ_UINT32 p_total_data_size,
358 opj_stream_private_t *p_stream,
359 struct opj_event_mgr * p_manager );
362 * Gets the offset of the header.
364 * @param p_stream the stream to write data to.
365 * @param p_j2k J2K codec.
366 * @param p_manager the user event manager.
368 static opj_bool j2k_get_end_header( opj_j2k_v2_t *p_j2k,
369 struct opj_stream_private *p_stream,
370 struct opj_event_mgr * p_manager );
373 * -----------------------------------------------------------------------
374 * -----------------------------------------------------------------------
375 * -----------------------------------------------------------------------
379 Write the SOC marker (Start Of Codestream)
380 @param j2k J2K handle
382 static void j2k_write_soc(opj_j2k_t *j2k);
385 * Writes the SOC marker (Start Of Codestream)
387 * @param p_stream the stream to write data to.
388 * @param p_j2k J2K codec.
389 * @param p_manager the user event manager.
391 static opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
392 struct opj_stream_private *p_stream,
393 struct opj_event_mgr * p_manager );
396 Read the SOC marker (Start of Codestream)
397 @param j2k J2K handle
399 static void j2k_read_soc(opj_j2k_t *j2k);
402 * Reads a SOC marker (Start of Codestream)
403 * @param p_header_data the data contained in the SOC box.
404 * @param jp2 the jpeg2000 file codec.
405 * @param p_header_size the size of the data contained in the SOC marker.
406 * @param p_manager the user event manager.
408 static opj_bool j2k_read_soc_v2(
410 struct opj_stream_private *p_stream,
411 struct opj_event_mgr * p_manager
415 Write the SIZ marker (image and tile size)
416 @param j2k J2K handle
418 static void j2k_write_siz(opj_j2k_t *j2k);
421 * Writes the SIZ marker (image and tile size)
423 * @param p_stream the stream to write data to.
424 * @param p_j2k J2K codec.
425 * @param p_manager the user event manager.
427 static opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
428 struct opj_stream_private *p_stream,
429 struct opj_event_mgr * p_manager );
432 Read the SIZ marker (image and tile size)
433 @param j2k J2K handle
435 static void j2k_read_siz(opj_j2k_t *j2k);
438 * Reads a SIZ marker (image and tile size)
439 * @param p_header_data the data contained in the SIZ box.
440 * @param jp2 the jpeg2000 file codec.
441 * @param p_header_size the size of the data contained in the SIZ marker.
442 * @param p_manager the user event manager.
444 static opj_bool j2k_read_siz_v2 (
446 OPJ_BYTE * p_header_data,
447 OPJ_UINT32 p_header_size,
448 struct opj_event_mgr * p_manager
452 Write the COM marker (comment)
453 @param j2k J2K handle
455 static void j2k_write_com(opj_j2k_t *j2k);
458 * Writes the COM marker (comment)
460 * @param p_stream the stream to write data to.
461 * @param p_j2k J2K codec.
462 * @param p_manager the user event manager.
464 static opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
465 struct opj_stream_private *p_stream,
466 struct opj_event_mgr * p_manager );
469 Read the COM marker (comment)
470 @param j2k J2K handle
472 static void j2k_read_com(opj_j2k_t *j2k);
474 * Reads a COM marker (comments)
475 * @param p_header_data the data contained in the COM box.
476 * @param jp2 the jpeg2000 file codec.
477 * @param p_header_size the size of the data contained in the COM marker.
478 * @param p_manager the user event manager.
480 static opj_bool j2k_read_com_v2 (
482 OPJ_BYTE * p_header_data,
483 OPJ_UINT32 p_header_size,
484 struct opj_event_mgr * p_manager
487 Write the value concerning the specified component in the marker COD and COC
488 @param j2k J2K handle
489 @param compno Number of the component concerned by the information written
491 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
493 Read the value concerning the specified component in the marker COD and COC
494 @param j2k J2K handle
495 @param compno Number of the component concerned by the information read
497 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
499 Write the COD marker (coding style default)
500 @param j2k J2K handle
502 static void j2k_write_cod(opj_j2k_t *j2k);
505 * Writes the COD marker (Coding style default)
507 * @param p_stream the stream to write data to.
508 * @param p_j2k J2K codec.
509 * @param p_manager the user event manager.
511 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
512 struct opj_stream_private *p_stream,
513 struct opj_event_mgr * p_manager );
516 Read the COD marker (coding style default)
517 @param j2k J2K handle
519 static void j2k_read_cod(opj_j2k_t *j2k);
522 * Reads a COD marker (Coding Styke defaults)
523 * @param p_header_data the data contained in the COD box.
524 * @param p_j2k the jpeg2000 codec.
525 * @param p_header_size the size of the data contained in the COD marker.
526 * @param p_manager the user event manager.
528 static opj_bool j2k_read_cod_v2 (
530 OPJ_BYTE * p_header_data,
531 OPJ_UINT32 p_header_size,
532 struct opj_event_mgr * p_manager
536 Write the COC marker (coding style component)
537 @param j2k J2K handle
538 @param compno Number of the component concerned by the information written
540 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
543 * Writes the COC marker (Coding style component)
545 * @param p_comp_number the index of the component to output.
546 * @param p_stream the stream to write data to.
547 * @param p_j2k J2K codec.
548 * @param p_manager the user event manager.
550 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
551 OPJ_UINT32 p_comp_number,
552 struct opj_stream_private *p_stream,
553 struct opj_event_mgr * p_manager
556 * Writes the COC marker (Coding style component)
558 * @param p_comp_no the index of the component to output.
559 * @param p_stream the stream to write data to.
560 * @param p_j2k J2K codec.
561 * @param p_manager the user event manager.
563 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
564 OPJ_UINT32 p_comp_no,
566 OPJ_UINT32 * p_data_written,
567 struct opj_event_mgr * p_manager );
570 * Gets the maximum size taken by a coc.
572 * @param p_j2k the jpeg2000 codec to use.
574 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
577 Read the COC marker (coding style component)
578 @param j2k J2K handle
580 static void j2k_read_coc(opj_j2k_t *j2k);
583 * Reads a COC marker (Coding Style Component)
584 * @param p_header_data the data contained in the COC box.
585 * @param p_j2k the jpeg2000 codec.
586 * @param p_header_size the size of the data contained in the COC marker.
587 * @param p_manager the user event manager.
589 static opj_bool j2k_read_coc_v2 (
591 OPJ_BYTE * p_header_data,
592 OPJ_UINT32 p_header_size,
593 struct opj_event_mgr * p_manager
597 Write the value concerning the specified component in the marker QCD and QCC
598 @param j2k J2K handle
599 @param compno Number of the component concerned by the information written
601 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
603 Read the value concerning the specified component in the marker QCD and QCC
604 @param j2k J2K handle
605 @param compno Number of the component concern by the information read
606 @param len Length of the information in the QCX part of the marker QCD/QCC
608 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
610 Write the QCD marker (quantization default)
611 @param j2k J2K handle
613 static void j2k_write_qcd(opj_j2k_t *j2k);
616 * Writes the QCD marker (quantization default)
618 * @param p_comp_number the index of the component to output.
619 * @param p_stream the stream to write data to.
620 * @param p_j2k J2K codec.
621 * @param p_manager the user event manager.
623 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
624 struct opj_stream_private *p_stream,
625 struct opj_event_mgr * p_manager );
628 Read the QCD marker (quantization default)
629 @param j2k J2K handle
631 static void j2k_read_qcd(opj_j2k_t *j2k);
634 * Reads a QCD marker (Quantization defaults)
635 * @param p_header_data the data contained in the QCD box.
636 * @param p_j2k the jpeg2000 codec.
637 * @param p_header_size the size of the data contained in the QCD marker.
638 * @param p_manager the user event manager.
640 static opj_bool j2k_read_qcd_v2 (
642 OPJ_BYTE * p_header_data,
643 OPJ_UINT32 p_header_size,
644 struct opj_event_mgr * p_manager
648 Write the QCC marker (quantization component)
649 @param j2k J2K handle
650 @param compno Number of the component concerned by the information written
652 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
655 * Writes the QCC marker (quantization component)
657 * @param p_comp_no the index of the component to output.
658 * @param p_stream the stream to write data to.
659 * @param p_j2k J2K codec.
660 * @param p_manager the user event manager.
662 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
663 OPJ_UINT32 p_comp_no,
664 struct opj_stream_private *p_stream,
665 struct opj_event_mgr * p_manager );
668 * Writes the QCC marker (quantization component)
670 * @param p_comp_no the index of the component to output.
671 * @param p_stream the stream to write data to.
672 * @param p_j2k J2K codec.
673 * @param p_manager the user event manager.
675 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
676 OPJ_UINT32 p_comp_no,
678 OPJ_UINT32 * p_data_written,
679 struct opj_event_mgr * p_manager );
682 * Gets the maximum size taken by a qcc.
684 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
687 Read the QCC marker (quantization component)
688 @param j2k J2K handle
690 static void j2k_read_qcc(opj_j2k_t *j2k);
692 * Reads a QCC marker (Quantization component)
693 * @param p_header_data the data contained in the QCC box.
694 * @param p_j2k the jpeg2000 codec.
695 * @param p_header_size the size of the data contained in the QCC marker.
696 * @param p_manager the user event manager.
698 static opj_bool j2k_read_qcc_v2(
700 OPJ_BYTE * p_header_data,
701 OPJ_UINT32 p_header_size,
702 struct opj_event_mgr * p_manager);
705 Write the POC marker (progression order change)
706 @param j2k J2K handle
708 static void j2k_write_poc(opj_j2k_t *j2k);
711 * Writes the POC marker (Progression Order Change)
713 * @param p_stream the stream to write data to.
714 * @param p_j2k J2K codec.
715 * @param p_manager the user event manager.
717 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
718 struct opj_stream_private *p_stream,
719 struct opj_event_mgr * p_manager );
722 * Writes the POC marker (Progression Order Change)
724 * @param p_stream the stream to write data to.
725 * @param p_j2k J2K codec.
726 * @param p_manager the user event manager.
728 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
730 OPJ_UINT32 * p_data_written,
731 struct opj_event_mgr * p_manager );
734 * Gets the maximum size taken by the writting of a POC.
736 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
739 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
741 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
744 * Gets the maximum size taken by the headers of the SOT.
746 * @param p_j2k the jpeg2000 codec to use.
748 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
751 Read the POC marker (progression order change)
752 @param j2k J2K handle
754 static void j2k_read_poc(opj_j2k_t *j2k);
756 * Reads a POC marker (Progression Order Change)
758 * @param p_header_data the data contained in the POC box.
759 * @param p_j2k the jpeg2000 codec.
760 * @param p_header_size the size of the data contained in the POC marker.
761 * @param p_manager the user event manager.
763 static opj_bool j2k_read_poc_v2 (
765 OPJ_BYTE * p_header_data,
766 OPJ_UINT32 p_header_size,
767 struct opj_event_mgr * p_manager
771 Read the CRG marker (component registration)
772 @param j2k J2K handle
774 static void j2k_read_crg(opj_j2k_t *j2k);
776 * Reads a CRG marker (Component registration)
778 * @param p_header_data the data contained in the TLM box.
779 * @param p_j2k the jpeg2000 codec.
780 * @param p_header_size the size of the data contained in the TLM marker.
781 * @param p_manager the user event manager.
783 static opj_bool j2k_read_crg_v2 (
785 OPJ_BYTE * p_header_data,
786 OPJ_UINT32 p_header_size,
787 struct opj_event_mgr * p_manager
790 Read the TLM marker (tile-part lengths)
791 @param j2k J2K handle
793 static void j2k_read_tlm(opj_j2k_t *j2k);
795 * Reads a TLM marker (Tile Length Marker)
797 * @param p_header_data the data contained in the TLM box.
798 * @param p_j2k the jpeg2000 codec.
799 * @param p_header_size the size of the data contained in the TLM marker.
800 * @param p_manager the user event manager.
802 static opj_bool j2k_read_tlm_v2 (
804 OPJ_BYTE * p_header_data,
805 OPJ_UINT32 p_header_size,
806 struct opj_event_mgr * p_manager
810 * Writes the updated tlm.
812 * @param p_stream the stream to write data to.
813 * @param p_j2k J2K codec.
814 * @param p_manager the user event manager.
816 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
817 struct opj_stream_private *p_stream,
818 struct opj_event_mgr * p_manager );
821 Read the PLM marker (packet length, main header)
822 @param j2k J2K handle
824 static void j2k_read_plm(opj_j2k_t *j2k);
827 * Reads a PLM marker (Packet length, main header marker)
829 * @param p_header_data the data contained in the TLM box.
830 * @param p_j2k the jpeg2000 codec.
831 * @param p_header_size the size of the data contained in the TLM marker.
832 * @param p_manager the user event manager.
834 static opj_bool j2k_read_plm_v2 (
836 OPJ_BYTE * p_header_data,
837 OPJ_UINT32 p_header_size,
838 struct opj_event_mgr * p_manager
841 Read the PLT marker (packet length, tile-part header)
842 @param j2k J2K handle
844 static void j2k_read_plt(opj_j2k_t *j2k);
846 * Reads a PLT marker (Packet length, tile-part header)
848 * @param p_header_data the data contained in the PLT box.
849 * @param p_j2k the jpeg2000 codec.
850 * @param p_header_size the size of the data contained in the PLT marker.
851 * @param p_manager the user event manager.
853 static opj_bool j2k_read_plt_v2 (
855 OPJ_BYTE * p_header_data,
856 OPJ_UINT32 p_header_size,
857 struct opj_event_mgr * p_manager
860 Read the PPM marker (packet packet headers, main header)
861 @param j2k J2K handle
863 static void j2k_read_ppm(opj_j2k_t *j2k);
865 * Reads a PPM marker (Packed packet headers, main header)
867 * @param p_header_data the data contained in the POC box.
868 * @param p_j2k the jpeg2000 codec.
869 * @param p_header_size the size of the data contained in the POC marker.
870 * @param p_manager the user event manager.
873 static opj_bool j2k_read_ppm_v2 (
875 OPJ_BYTE * p_header_data,
876 OPJ_UINT32 p_header_size,
877 struct opj_event_mgr * p_manager
881 static opj_bool j2k_read_ppm_v3 (
883 OPJ_BYTE * p_header_data,
884 OPJ_UINT32 p_header_size,
885 struct opj_event_mgr * p_manager
889 Read the PPT marker (packet packet headers, tile-part header)
890 @param j2k J2K handle
892 static void j2k_read_ppt(opj_j2k_t *j2k);
894 * Reads a PPT marker (Packed packet headers, tile-part header)
896 * @param p_header_data the data contained in the PPT box.
897 * @param p_j2k the jpeg2000 codec.
898 * @param p_header_size the size of the data contained in the PPT marker.
899 * @param p_manager the user event manager.
901 static opj_bool j2k_read_ppt_v2 (
903 OPJ_BYTE * p_header_data,
904 OPJ_UINT32 p_header_size,
905 struct opj_event_mgr * p_manager
908 Write the TLM marker (Mainheader)
909 @param j2k J2K handle
911 static void j2k_write_tlm(opj_j2k_t *j2k);
914 * Writes the TLM marker (Tile Length Marker)
916 * @param p_stream the stream to write data to.
917 * @param p_j2k J2K codec.
918 * @param p_manager the user event manager.
920 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
921 struct opj_stream_private *p_stream,
922 struct opj_event_mgr * p_manager );
925 Write the SOT marker (start of tile-part)
926 @param j2k J2K handle
928 static void j2k_write_sot(opj_j2k_t *j2k);
930 Read the SOT marker (start of tile-part)
931 @param j2k J2K handle
933 static void j2k_read_sot(opj_j2k_t *j2k);
936 * Writes the SOT marker (Start of tile-part)
938 * @param p_stream the stream to write data to.
939 * @param p_j2k J2K codec.
940 * @param p_manager the user event manager.
942 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
944 OPJ_UINT32 * p_data_written,
945 const struct opj_stream_private *p_stream,
946 struct opj_event_mgr * p_manager );
949 * Reads a PPT marker (Packed packet headers, tile-part header)
951 * @param p_header_data the data contained in the PPT box.
952 * @param p_j2k the jpeg2000 codec.
953 * @param p_header_size the size of the data contained in the PPT marker.
954 * @param p_manager the user event manager.
956 static opj_bool j2k_read_sot_v2 (
958 OPJ_BYTE * p_header_data,
959 OPJ_UINT32 p_header_size,
960 struct opj_event_mgr * p_manager
963 Write the SOD marker (start of data)
964 @param j2k J2K handle
965 @param tile_coder Pointer to a TCD handle
967 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
970 * Writes the SOD marker (Start of data)
972 * @param p_stream the stream to write data to.
973 * @param p_j2k J2K codec.
974 * @param p_manager the user event manager.
976 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
977 struct opj_tcd_v2 * p_tile_coder,
979 OPJ_UINT32 * p_data_written,
980 OPJ_UINT32 p_total_data_size,
981 const struct opj_stream_private *p_stream,
982 struct opj_event_mgr * p_manager );
985 Read the SOD marker (start of data)
986 @param j2k J2K handle
988 static void j2k_read_sod(opj_j2k_t *j2k);
991 * Reads a SOD marker (Start Of Data)
993 * @param p_header_data the data contained in the SOD box.
994 * @param p_j2k the jpeg2000 codec.
995 * @param p_header_size the size of the data contained in the SOD marker.
996 * @param p_manager the user event manager.
998 static opj_bool j2k_read_sod_v2 (
1000 struct opj_stream_private *p_stream,
1001 struct opj_event_mgr * p_manager
1005 * Updates the Tile Length Marker.
1007 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
1009 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
1010 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
1012 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
1013 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
1017 Write the RGN marker (region-of-interest)
1018 @param j2k J2K handle
1019 @param compno Number of the component concerned by the information written
1020 @param tileno Number of the tile concerned by the information written
1022 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
1025 * Writes the RGN marker (Region Of Interest)
1027 * @param p_tile_no the tile to output
1028 * @param p_comp_no the component to output
1029 * @param p_stream the stream to write data to.
1030 * @param p_j2k J2K codec.
1031 * @param p_manager the user event manager.
1033 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
1034 OPJ_UINT32 p_tile_no,
1035 OPJ_UINT32 p_comp_no,
1036 struct opj_stream_private *p_stream,
1037 struct opj_event_mgr * p_manager );
1040 Read the RGN marker (region-of-interest)
1041 @param j2k J2K handle
1043 static void j2k_read_rgn(opj_j2k_t *j2k);
1046 * Reads a RGN marker (Region Of Interest)
1048 * @param p_header_data the data contained in the POC box.
1049 * @param p_j2k the jpeg2000 codec.
1050 * @param p_header_size the size of the data contained in the POC marker.
1051 * @param p_manager the user event manager.
1053 static opj_bool j2k_read_rgn_v2 (
1054 opj_j2k_v2_t *p_j2k,
1055 OPJ_BYTE * p_header_data,
1056 OPJ_UINT32 p_header_size,
1057 struct opj_event_mgr * p_manager
1061 * Writes the EOC marker (End of Codestream)
1063 * @param p_stream the stream to write data to.
1064 * @param p_j2k J2K codec.
1065 * @param p_manager the user event manager.
1067 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
1068 struct opj_stream_private *p_stream,
1069 struct opj_event_mgr * p_manager );
1072 Write the EOC marker (end of codestream)
1073 @param j2k J2K handle
1075 static void j2k_write_eoc(opj_j2k_t *j2k);
1077 Read the EOC marker (end of codestream)
1078 @param j2k J2K handle
1080 static void j2k_read_eoc(opj_j2k_t *j2k);
1083 * Reads a EOC marker (End Of Codestream)
1085 * @param p_header_data the data contained in the SOD box.
1086 * @param p_j2k the jpeg2000 codec.
1087 * @param p_header_size the size of the data contained in the SOD marker.
1088 * @param p_manager the user event manager.
1091 static opj_bool j2k_read_eoc_v2 (
1092 opj_j2k_v2_t *p_j2k,
1093 struct opj_stream_private *p_stream,
1094 struct opj_event_mgr * p_manager
1101 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1103 * @param p_stream the stream to write data to.
1104 * @param p_j2k J2K codec.
1105 * @param p_manager the user event manager.
1107 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1108 struct opj_stream_private *p_stream,
1109 struct opj_event_mgr * p_manager );
1114 * @param p_stream the stream to write data to.
1115 * @param p_j2k J2K codec.
1116 * @param p_manager the user event manager.
1118 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1119 struct opj_stream_private *p_stream,
1120 struct opj_event_mgr * p_manager );
1123 Read an unknown marker
1124 @param j2k J2K handle
1126 static void j2k_read_unk(opj_j2k_t *j2k);
1128 Add main header marker information
1129 @param cstr_info Codestream information structure
1130 @param type marker type
1131 @param pos byte offset of marker segment
1132 @param len length of marker segment
1134 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1136 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1138 Add tile header marker information
1139 @param tileno tile index number
1140 @param cstr_info Codestream information structure
1141 @param type marker type
1142 @param pos byte offset of marker segment
1143 @param len length of marker segment
1145 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1147 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len);
1150 * Reads an unknown marker
1152 * @param p_stream the stream object to read from.
1153 * @param p_j2k the jpeg2000 codec.
1154 * @param p_manager the user event manager.
1156 * @return true if the marker could be deduced.
1158 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1159 struct opj_stream_private *p_stream,
1160 OPJ_UINT32 *output_marker,
1161 struct opj_event_mgr * p_manager );
1164 * Writes the MCT marker (Multiple Component Transform)
1166 * @param p_stream the stream to write data to.
1167 * @param p_j2k J2K codec.
1168 * @param p_manager the user event manager.
1170 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1171 opj_mct_data_t * p_mct_record,
1172 struct opj_stream_private *p_stream,
1173 struct opj_event_mgr * p_manager );
1176 * Reads a MCT marker (Multiple Component Transform)
1178 * @param p_header_data the data contained in the MCT box.
1179 * @param p_j2k the jpeg2000 codec.
1180 * @param p_header_size the size of the data contained in the MCT marker.
1181 * @param p_manager the user event manager.
1183 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1184 OPJ_BYTE * p_header_data,
1185 OPJ_UINT32 p_header_size,
1186 struct opj_event_mgr * p_manager );
1189 * Writes the MCC marker (Multiple Component Collection)
1191 * @param p_stream the stream to write data to.
1192 * @param p_j2k J2K codec.
1193 * @param p_manager the user event manager.
1195 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1196 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1197 struct opj_stream_private *p_stream,
1198 struct opj_event_mgr * p_manager );
1202 * Reads a MCC marker (Multiple Component Collection)
1204 * @param p_header_data the data contained in the MCC box.
1205 * @param p_j2k the jpeg2000 codec.
1206 * @param p_header_size the size of the data contained in the MCC marker.
1207 * @param p_manager the user event manager.
1209 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1210 OPJ_BYTE * p_header_data,
1211 OPJ_UINT32 p_header_size,
1212 struct opj_event_mgr * p_manager );
1215 * Writes the MCO marker (Multiple component transformation ordering)
1217 * @param p_stream the stream to write data to.
1218 * @param p_j2k J2K codec.
1219 * @param p_manager the user event manager.
1221 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1222 struct opj_stream_private *p_stream,
1223 struct opj_event_mgr * p_manager );
1226 * Reads a MCO marker (Multiple Component Transform Ordering)
1228 * @param p_header_data the data contained in the MCO box.
1229 * @param p_j2k the jpeg2000 codec.
1230 * @param p_header_size the size of the data contained in the MCO marker.
1231 * @param p_manager the user event manager.
1233 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1234 OPJ_BYTE * p_header_data,
1235 OPJ_UINT32 p_header_size,
1236 struct opj_event_mgr * p_manager );
1238 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1240 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1241 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1242 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1243 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1245 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1246 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1247 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1248 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1250 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1251 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1252 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1253 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1257 * Ends the encoding, i.e. frees memory.
1259 * @param p_stream the stream to write data to.
1260 * @param p_j2k J2K codec.
1261 * @param p_manager the user event manager.
1263 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1264 struct opj_stream_private *p_stream,
1265 struct opj_event_mgr * p_manager );
1268 * Writes the CBD marker (Component bit depth definition)
1270 * @param p_stream the stream to write data to.
1271 * @param p_j2k J2K codec.
1272 * @param p_manager the user event manager.
1274 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1275 struct opj_stream_private *p_stream,
1276 struct opj_event_mgr * p_manager );
1279 * Reads a CBD marker (Component bit depth definition)
1280 * @param p_header_data the data contained in the CBD box.
1281 * @param p_j2k the jpeg2000 codec.
1282 * @param p_header_size the size of the data contained in the CBD marker.
1283 * @param p_manager the user event manager.
1285 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1286 OPJ_BYTE * p_header_data,
1287 OPJ_UINT32 p_header_size,
1288 struct opj_event_mgr * p_manager);
1291 * Writes the image components.
1293 * @param p_stream the stream to write data to.
1294 * @param p_j2k J2K codec.
1295 * @param p_manager the user event manager.
1297 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1298 struct opj_stream_private *p_stream,
1299 struct opj_event_mgr * p_manager );
1302 * Writes regions of interests.
1304 * @param p_stream the stream to write data to.
1305 * @param p_j2k J2K codec.
1306 * @param p_manager the user event manager.
1308 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1309 struct opj_stream_private *p_stream,
1310 struct opj_event_mgr * p_manager );
1315 * @param p_stream the stream to write data to.
1316 * @param p_j2k J2K codec.
1317 * @param p_manager the user event manager.
1319 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1320 struct opj_stream_private *p_stream,
1321 struct opj_event_mgr * p_manager );
1324 * Checks the progression order changes values. Tells of the poc given as input are valid.
1325 * A nice message is outputted at errors.
1327 * @param p_pocs the progression order changes.
1328 * @param p_nb_pocs the number of progression order changes.
1329 * @param p_nb_resolutions the number of resolutions.
1330 * @param numcomps the number of components
1331 * @param numlayers the number of layers.
1333 * @return true if the pocs are valid.
1335 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1336 OPJ_UINT32 p_nb_pocs,
1337 OPJ_UINT32 p_nb_resolutions,
1338 OPJ_UINT32 numcomps,
1339 OPJ_UINT32 numlayers,
1340 opj_event_mgr_t * p_manager);
1343 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1345 * @param cp the coding parameters.
1346 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1347 * @param tileno the given tile.
1349 * @return the number of tile parts.
1351 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1353 /** mem allocation for TLM marker*/
1354 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1357 * Calculates the total number of tile parts needed by the encoder to
1358 * encode such an image. If not enough memory is available, then the function return false.
1360 * @param p_nb_tiles pointer that will hold the number of tile parts.
1361 * @param cp the coding parameters for the image.
1362 * @param image the image to encode.
1363 * @param p_j2k the p_j2k encoder.
1364 * @param p_manager the user event manager.
1366 * @return true if the function was successful, false else.
1368 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1370 OPJ_UINT32 * p_nb_tiles,
1372 opj_event_mgr_t * p_manager);
1374 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1376 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1378 static opj_codestream_index_t* j2k_create_cstr_index(void);
1384 /* ----------------------------------------------------------------------- */
1385 typedef struct j2k_prog_order{
1386 OPJ_PROG_ORDER enum_prog;
1390 j2k_prog_order_t j2k_prog_order_list[] = {
1396 {(OPJ_PROG_ORDER)-1, ""}
1404 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1412 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1414 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1416 j2k_read_int16_to_float,
1417 j2k_read_int32_to_float,
1418 j2k_read_float32_to_float,
1419 j2k_read_float64_to_float
1422 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1424 j2k_read_int16_to_int32,
1425 j2k_read_int32_to_int32,
1426 j2k_read_float32_to_int32,
1427 j2k_read_float64_to_int32
1430 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1432 j2k_write_float_to_int16,
1433 j2k_write_float_to_int32,
1434 j2k_write_float_to_float,
1435 j2k_write_float_to_float64
1438 typedef struct opj_dec_memory_marker_handler
1442 /** value of the state when the marker can appear */
1444 /** action linked to the marker */
1445 opj_bool (*handler) (
1446 opj_j2k_v2_t *p_j2k,
1447 OPJ_BYTE * p_header_data,
1448 OPJ_UINT32 p_header_size,
1449 struct opj_event_mgr * p_manager
1452 opj_dec_memory_marker_handler_t;
1454 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1457 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1458 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1459 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1460 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1461 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1462 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1463 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1464 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1465 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1466 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1467 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1468 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1469 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1471 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1472 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1473 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1474 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1475 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1476 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1478 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot_v2},
1479 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod_v2},
1480 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc_v2},
1481 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn_v2},
1482 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd_v2},
1483 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc_v2},
1484 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1485 {J2K_MS_SIZ, J2K_STATE_MHSIZ , j2k_read_siz_v2},
1486 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1487 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1488 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1489 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1490 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1492 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1493 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1494 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1495 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1496 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1497 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1499 #ifdef TODO_MS /* FIXME */
1500 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1501 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1502 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1503 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1505 #endif /* USE_JPWL */
1507 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1508 {J2K_MS_INSEC, 0, j2k_read_insec}
1509 #endif /* USE_JPSEC */
1510 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1515 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1517 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1518 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1522 for (i=0;i<p_nb_elem;++i) {
1523 opj_read_bytes(l_src_data,&l_temp,2);
1525 l_src_data+=sizeof(OPJ_INT16);
1527 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1531 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1533 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1534 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1538 for (i=0;i<p_nb_elem;++i) {
1539 opj_read_bytes(l_src_data,&l_temp,4);
1541 l_src_data+=sizeof(OPJ_INT32);
1543 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1547 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1549 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1550 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1554 for (i=0;i<p_nb_elem;++i) {
1555 opj_read_float(l_src_data,&l_temp);
1557 l_src_data+=sizeof(OPJ_FLOAT32);
1559 *(l_dest_data++) = l_temp;
1563 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1565 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1566 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1570 for (i=0;i<p_nb_elem;++i) {
1571 opj_read_double(l_src_data,&l_temp);
1573 l_src_data+=sizeof(OPJ_FLOAT64);
1575 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1579 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1581 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1582 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1586 for (i=0;i<p_nb_elem;++i) {
1587 opj_read_bytes(l_src_data,&l_temp,2);
1589 l_src_data+=sizeof(OPJ_INT16);
1591 *(l_dest_data++) = (OPJ_INT32) l_temp;
1595 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1597 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1598 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1602 for (i=0;i<p_nb_elem;++i) {
1603 opj_read_bytes(l_src_data,&l_temp,4);
1605 l_src_data+=sizeof(OPJ_INT32);
1607 *(l_dest_data++) = (OPJ_INT32) l_temp;
1611 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1613 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1614 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1618 for (i=0;i<p_nb_elem;++i) {
1619 opj_read_float(l_src_data,&l_temp);
1621 l_src_data+=sizeof(OPJ_FLOAT32);
1623 *(l_dest_data++) = (OPJ_INT32) l_temp;
1627 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1629 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1630 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1634 for (i=0;i<p_nb_elem;++i) {
1635 opj_read_double(l_src_data,&l_temp);
1637 l_src_data+=sizeof(OPJ_FLOAT64);
1639 *(l_dest_data++) = (OPJ_INT32) l_temp;
1643 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1645 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1646 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1650 for (i=0;i<p_nb_elem;++i) {
1651 l_temp = (OPJ_UINT32) *(l_src_data++);
1653 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1655 l_dest_data+=sizeof(OPJ_INT16);
1659 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1661 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1662 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1666 for (i=0;i<p_nb_elem;++i) {
1667 l_temp = (OPJ_UINT32) *(l_src_data++);
1669 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1671 l_dest_data+=sizeof(OPJ_INT32);
1675 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1677 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1678 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1682 for (i=0;i<p_nb_elem;++i) {
1683 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1685 opj_write_float(l_dest_data,l_temp);
1687 l_dest_data+=sizeof(OPJ_FLOAT32);
1691 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1693 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1694 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1698 for (i=0;i<p_nb_elem;++i) {
1699 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1701 opj_write_double(l_dest_data,l_temp);
1703 l_dest_data+=sizeof(OPJ_FLOAT64);
1709 * Converts an enum type progression order to string type.
1711 * @param prg_order the progression order to get.
1713 * @return the string representation of the given progression order.
1715 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1716 j2k_prog_order_t *po;
1717 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1718 if(po->enum_prog == prg_order){
1719 return po->str_prog;
1722 return po->str_prog;
1726 * Checks the progression order changes values. Tells if the poc given as input are valid.
1728 * @param p_pocs the progression order changes.
1729 * @param p_nb_pocs the number of progression order changes.
1730 * @param p_nb_resolutions the number of resolutions.
1731 * @param numcomps the number of components
1732 * @param numlayers the number of layers.
1733 * @param p_manager the user event manager.
1735 * @return true if the pocs are valid.
1737 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1738 OPJ_UINT32 p_nb_pocs,
1739 OPJ_UINT32 p_nb_resolutions,
1740 OPJ_UINT32 p_num_comps,
1741 OPJ_UINT32 p_num_layers,
1742 opj_event_mgr_t * p_manager)
1744 OPJ_UINT32* packet_array;
1745 OPJ_UINT32 index , resno, compno, layno;
1747 OPJ_UINT32 step_c = 1;
1748 OPJ_UINT32 step_r = p_num_comps * step_c;
1749 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1750 opj_bool loss = OPJ_FALSE;
1751 OPJ_UINT32 layno0 = 0;
1753 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1754 if (packet_array == 00) {
1755 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1758 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1760 if (p_nb_pocs == 0) {
1764 index = step_r * p_pocs->resno0;
1765 // take each resolution for each poc
1766 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1768 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1770 // take each comp of each resolution for each poc
1771 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1772 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1774 // and finally take each layer of each res of ...
1775 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1776 //index = step_r * resno + step_c * compno + step_l * layno;
1777 packet_array[comp_index] = 1;
1778 comp_index += step_l;
1781 res_index += step_c;
1788 // iterate through all the pocs
1789 for (i = 1; i < p_nb_pocs ; ++i) {
1790 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1792 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1793 index = step_r * p_pocs->resno0;
1795 // take each resolution for each poc
1796 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1797 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1799 // take each comp of each resolution for each poc
1800 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1801 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1803 // and finally take each layer of each res of ...
1804 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1805 //index = step_r * resno + step_c * compno + step_l * layno;
1806 packet_array[comp_index] = 1;
1807 comp_index += step_l;
1810 res_index += step_c;
1820 for (layno = 0; layno < p_num_layers ; ++layno) {
1821 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1822 for (compno = 0; compno < p_num_comps; ++compno) {
1823 loss |= (packet_array[index]!=1);
1824 //index = step_r * resno + step_c * compno + step_l * layno;
1831 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1834 opj_free(packet_array);
1839 /* ----------------------------------------------------------------------- */
1840 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1843 int tpnum=1,tpend=0;
1844 opj_tcp_t *tcp = &cp->tcps[tileno];
1845 prog = j2k_convert_progression_order(tcp->prg);
1850 if( cp->tp_flag == prog[i] ){
1851 tpend=1;cp->tp_pos=i;
1855 tpnum= tpnum * tcp->pocs[pino].compE;
1858 tpnum= tpnum * tcp->pocs[pino].resE;
1861 tpnum= tpnum * tcp->pocs[pino].prcE;
1864 tpnum= tpnum * tcp->pocs[pino].layE;
1876 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1878 * @param cp the coding parameters.
1879 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1880 * @param tileno the given tile.
1882 * @return the number of tile parts.
1884 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1886 const OPJ_CHAR *prog = 00;
1888 OPJ_UINT32 tpnum = 1;
1889 opj_tcp_v2_t *tcp = 00;
1890 opj_poc_t * l_current_poc = 00;
1893 assert(tileno < (cp->tw * cp->th));
1894 assert(pino < (cp->tcps[tileno].numpocs + 1));
1896 /* get the given tile coding parameter */
1897 tcp = &cp->tcps[tileno];
1900 l_current_poc = &(tcp->pocs[pino]);
1901 assert(l_current_poc != 0);
1903 /* get the progression order as a character string */
1904 prog = j2k_convert_progression_order(tcp->prg);
1905 assert(strlen(prog) > 0);
1907 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1911 /* component wise */
1913 tpnum *= l_current_poc->compE;
1915 /* resolution wise */
1917 tpnum *= l_current_poc->resE;
1921 tpnum *= l_current_poc->prcE;
1925 tpnum *= l_current_poc->layE;
1928 /* whould we split here ? */
1929 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1930 cp->m_specific_param.m_enc.m_tp_pos=i;
1942 /** mem allocation for TLM marker*/
1943 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1944 int pino,tileno,totnum_tp=0;
1946 OPJ_ARG_NOT_USED(img_numcomp);
1948 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1949 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1950 int cur_totnum_tp = 0;
1951 opj_tcp_t *tcp = &cp->tcps[tileno];
1952 for(pino = 0; pino <= tcp->numpocs; pino++) {
1954 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1955 if(!pi) { return -1;}
1956 tp_num = j2k_get_num_tp(cp,pino,tileno);
1957 totnum_tp = totnum_tp + tp_num;
1958 cur_totnum_tp = cur_totnum_tp + tp_num;
1959 pi_destroy(pi, cp, tileno);
1961 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1963 if (j2k->cstr_info) {
1964 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1965 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1973 * Calculates the total number of tile parts needed by the encoder to
1974 * encode such an image. If not enough memory is available, then the function return false.
1976 * @param p_nb_tiles pointer that will hold the number of tile parts.
1977 * @param cp the coding parameters for the image.
1978 * @param image the image to encode.
1979 * @param p_j2k the p_j2k encoder.
1980 * @param p_manager the user event manager.
1982 * @return true if the function was successful, false else.
1984 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1986 OPJ_UINT32 * p_nb_tiles,
1988 opj_event_mgr_t * p_manager)
1990 OPJ_UINT32 pino,tileno;
1991 OPJ_UINT32 l_nb_tiles;
1995 assert(p_nb_tiles != 00);
1997 assert(image != 00);
1998 assert(p_j2k != 00);
1999 assert(p_manager != 00);
2001 l_nb_tiles = cp->tw * cp->th;
2006 /* TODO mergeV2: check this part which use cstr_info */
2007 /*if (p_j2k->cstr_info) {
2008 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
2010 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2011 OPJ_UINT32 cur_totnum_tp = 0;
2013 pi_update_encoding_parameters(image,cp,tileno);
2015 for (pino = 0; pino <= tcp->numpocs; ++pino)
2017 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2019 *p_nb_tiles = *p_nb_tiles + tp_num;
2021 cur_totnum_tp += tp_num;
2024 tcp->m_nb_tile_parts = cur_totnum_tp;
2026 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
2027 if (l_info_tile_ptr->tp == 00) {
2031 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
2033 l_info_tile_ptr->num_tps = cur_totnum_tp;
2040 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
2041 OPJ_UINT32 cur_totnum_tp = 0;
2043 pi_update_encoding_parameters(image,cp,tileno);
2045 for (pino = 0; pino <= tcp->numpocs; ++pino) {
2046 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
2048 *p_nb_tiles = *p_nb_tiles + tp_num;
2050 cur_totnum_tp += tp_num;
2052 tcp->m_nb_tile_parts = cur_totnum_tp;
2061 static void j2k_write_soc(opj_j2k_t *j2k) {
2062 opj_cio_t *cio = j2k->cio;
2063 cio_write(cio, J2K_MS_SOC, 2);
2066 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2071 /* update markers struct */
2072 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2073 #endif /* USE_JPWL */
2078 * Writes the SOC marker (Start Of Codestream)
2080 * @param p_stream the stream to write data to.
2081 * @param p_j2k J2K codec.
2082 * @param p_manager the user event manager.
2084 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2085 struct opj_stream_private *p_stream,
2086 struct opj_event_mgr * p_manager )
2088 /* 2 bytes will be written */
2089 OPJ_BYTE * l_start_stream = 00;
2092 assert(p_stream != 00);
2093 assert(p_j2k != 00);
2094 assert(p_manager != 00);
2096 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2098 /* write SOC identifier */
2099 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2101 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2107 /* update markers struct */
2109 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2111 assert( 0 && "TODO" );
2112 #endif /* USE_JPWL */
2118 static void j2k_read_soc(opj_j2k_t *j2k) {
2119 j2k->state = J2K_STATE_MHSIZ;
2121 if (j2k->cstr_info) {
2122 j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
2123 j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
2128 * Reads a SOC marker (Start of Codestream)
2129 * @param p_header_data the data contained in the SOC box.
2130 * @param jp2 the jpeg2000 file codec.
2131 * @param p_header_size the size of the data contained in the SOC marker.
2132 * @param p_manager the user event manager.
2134 static opj_bool j2k_read_soc_v2( opj_j2k_v2_t *p_j2k,
2135 struct opj_stream_private *p_stream,
2136 struct opj_event_mgr * p_manager )
2138 OPJ_BYTE l_data [2];
2139 OPJ_UINT32 l_marker;
2142 assert(p_j2k != 00);
2143 assert(p_manager != 00);
2144 assert(p_stream != 00);
2146 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2150 opj_read_bytes(l_data,&l_marker,2);
2151 if (l_marker != J2K_MS_SOC) {
2155 /* Next marker should be a SIZ marker in the main header */
2156 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2158 /* FIXME move it in a index structure included in p_j2k*/
2159 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2161 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2163 /* Add the marker to the codestream index*/
2164 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2169 static void j2k_write_siz(opj_j2k_t *j2k) {
2173 opj_cio_t *cio = j2k->cio;
2174 opj_image_t *image = j2k->image;
2175 opj_cp_t *cp = j2k->cp;
2177 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2178 lenp = cio_tell(cio);
2180 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2181 cio_write(cio, image->x1, 4); /* Xsiz */
2182 cio_write(cio, image->y1, 4); /* Ysiz */
2183 cio_write(cio, image->x0, 4); /* X0siz */
2184 cio_write(cio, image->y0, 4); /* Y0siz */
2185 cio_write(cio, cp->tdx, 4); /* XTsiz */
2186 cio_write(cio, cp->tdy, 4); /* YTsiz */
2187 cio_write(cio, cp->tx0, 4); /* XT0siz */
2188 cio_write(cio, cp->ty0, 4); /* YT0siz */
2189 cio_write(cio, image->numcomps, 2); /* Csiz */
2190 for (i = 0; i < image->numcomps; i++) {
2191 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2192 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2193 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2195 len = cio_tell(cio) - lenp;
2196 cio_seek(cio, lenp);
2197 cio_write(cio, len, 2); /* Lsiz */
2198 cio_seek(cio, lenp + len);
2201 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2205 * Writes the SIZ marker (image and tile size)
2207 * @param p_stream the stream to write data to.
2208 * @param p_j2k J2K codec.
2209 * @param p_manager the user event manager.
2211 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2212 struct opj_stream_private *p_stream,
2213 struct opj_event_mgr * p_manager )
2216 OPJ_UINT32 l_size_len;
2217 OPJ_BYTE * l_current_ptr;
2218 opj_image_t * l_image = 00;
2219 opj_cp_v2_t *cp = 00;
2220 opj_image_comp_t * l_img_comp = 00;
2223 assert(p_stream != 00);
2224 assert(p_j2k != 00);
2225 assert(p_manager != 00);
2227 l_image = p_j2k->m_private_image;
2228 cp = &(p_j2k->m_cp);
2229 l_size_len = 40 + 3 * l_image->numcomps;
2230 l_img_comp = l_image->comps;
2232 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2234 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2235 = (OPJ_BYTE*)opj_realloc(
2236 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2238 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2242 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2245 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2247 /* write SOC identifier */
2248 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2251 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2254 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2257 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2260 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2263 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2266 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2269 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2272 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2275 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2278 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2281 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2284 for (i = 0; i < l_image->numcomps; ++i) {
2285 /* TODO here with MCT ? */
2286 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2289 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2292 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2298 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_size_len,p_manager) != l_size_len) {
2305 static void j2k_read_siz(opj_j2k_t *j2k) {
2309 opj_cio_t *cio = j2k->cio;
2310 opj_image_t *image = j2k->image;
2311 opj_cp_t *cp = j2k->cp;
2313 len = cio_read(cio, 2); /* Lsiz */
2314 cio_read(cio, 2); /* Rsiz (capabilities) */
2315 image->x1 = cio_read(cio, 4); /* Xsiz */
2316 image->y1 = cio_read(cio, 4); /* Ysiz */
2317 image->x0 = cio_read(cio, 4); /* X0siz */
2318 image->y0 = cio_read(cio, 4); /* Y0siz */
2319 cp->tdx = cio_read(cio, 4); /* XTsiz */
2320 cp->tdy = cio_read(cio, 4); /* YTsiz */
2321 cp->tx0 = cio_read(cio, 4); /* XT0siz */
2322 cp->ty0 = cio_read(cio, 4); /* YT0siz */
2324 /* the following code triggers: */
2325 /* warning: comparison of unsigned expression < 0 is always false */
2327 if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
2328 opj_event_msg(j2k->cinfo, EVT_ERROR,
2329 "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
2330 image->x0,image->x1,image->y0,image->y1);
2335 image->numcomps = cio_read(cio, 2); /* Csiz */
2338 if (j2k->cp->correct) {
2339 /* if JPWL is on, we check whether TX errors have damaged
2340 too much the SIZ parameters */
2341 if (!(image->x1 * image->y1)) {
2342 opj_event_msg(j2k->cinfo, EVT_ERROR,
2343 "JPWL: bad image size (%d x %d)\n",
2344 image->x1, image->y1);
2345 if (!JPWL_ASSUME || JPWL_ASSUME) {
2346 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2350 assert( len >= 38 );
2351 if (image->numcomps != (OPJ_UINT32)((len - 38) / 3)) {
2352 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2353 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2354 image->numcomps, ((len - 38) / 3));
2356 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2359 /* we try to correct */
2360 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
2361 if (image->numcomps < (OPJ_UINT32)((len - 38) / 3)) {
2362 len = 38 + 3 * image->numcomps;
2363 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2366 image->numcomps = ((len - 38) / 3);
2367 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2372 /* update components number in the jpwl_exp_comps filed */
2373 cp->exp_comps = image->numcomps;
2375 #endif /* USE_JPWL */
2377 image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
2378 for (i = 0; i < image->numcomps; i++) {
2380 tmp = cio_read(cio, 1); /* Ssiz_i */
2381 image->comps[i].prec = (tmp & 0x7f) + 1;
2382 image->comps[i].sgnd = tmp >> 7;
2383 image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
2384 image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
2387 if (j2k->cp->correct) {
2388 /* if JPWL is on, we check whether TX errors have damaged
2389 too much the SIZ parameters, again */
2390 if (!(image->comps[i].dx * image->comps[i].dy)) {
2391 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2392 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2393 i, i, image->comps[i].dx, image->comps[i].dy);
2395 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2398 /* we try to correct */
2399 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2400 if (!image->comps[i].dx) {
2401 image->comps[i].dx = 1;
2402 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2403 i, image->comps[i].dx);
2405 if (!image->comps[i].dy) {
2406 image->comps[i].dy = 1;
2407 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2408 i, image->comps[i].dy);
2413 #endif /* USE_JPWL */
2415 /* TODO: unused ? */
2416 w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
2417 h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
2419 image->comps[i].resno_decoded = 0; /* number of resolution decoded */
2420 image->comps[i].factor = cp->reduce; /* reducing factor per component */
2423 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
2424 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
2427 if (j2k->cp->correct) {
2428 /* if JPWL is on, we check whether TX errors have damaged
2429 too much the SIZ parameters */
2430 if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
2431 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2432 "JPWL: bad number of tiles (%d x %d)\n",
2435 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2438 /* we try to correct */
2439 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
2442 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2445 if (cp->tw > cp->max_tiles) {
2447 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
2448 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2449 cp->max_tiles, cp->tw);
2453 opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2456 if (cp->th > cp->max_tiles) {
2458 opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2459 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2460 cp->max_tiles, cp->th);
2464 #endif /* USE_JPWL */
2466 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
2467 cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
2468 cp->tileno_size = 0;
2471 if (j2k->cp->correct) {
2473 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2474 "JPWL: could not alloc tcps field of cp\n");
2475 if (!JPWL_ASSUME || JPWL_ASSUME) {
2476 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
2481 #endif /* USE_JPWL */
2483 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2484 cp->tcps[i].POC = 0;
2485 cp->tcps[i].numpocs = 0;
2486 cp->tcps[i].first = 1;
2489 /* Initialization for PPM marker */
2491 cp->ppm_data = NULL;
2492 cp->ppm_data_first = NULL;
2493 cp->ppm_previous = 0;
2496 j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
2497 for (i = 0; i < (OPJ_UINT32)cp->tw * cp->th; i++) {
2498 cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
2500 j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
2501 j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
2502 j2k->state = J2K_STATE_MH;
2505 if (j2k->cstr_info) {
2506 opj_codestream_info_t *cstr_info = j2k->cstr_info;
2507 cstr_info->image_w = image->x1 - image->x0;
2508 cstr_info->image_h = image->y1 - image->y0;
2509 cstr_info->numcomps = image->numcomps;
2510 cstr_info->tw = cp->tw;
2511 cstr_info->th = cp->th;
2512 cstr_info->tile_x = cp->tdx;
2513 cstr_info->tile_y = cp->tdy;
2514 cstr_info->tile_Ox = cp->tx0;
2515 cstr_info->tile_Oy = cp->ty0;
2516 cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
2522 * Reads a SIZ marker (image and tile size)
2523 * @param p_header_data the data contained in the SIZ box.
2524 * @param jp2 the jpeg2000 file codec.
2525 * @param p_header_size the size of the data contained in the SIZ marker.
2526 * @param p_manager the user event manager.
2528 opj_bool j2k_read_siz_v2 (
2529 opj_j2k_v2_t *p_j2k,
2530 OPJ_BYTE * p_header_data,
2531 OPJ_UINT32 p_header_size,
2532 struct opj_event_mgr * p_manager
2535 OPJ_UINT32 l_size, i;
2536 OPJ_UINT32 l_nb_comp;
2537 OPJ_UINT32 l_nb_comp_remain;
2538 OPJ_UINT32 l_remaining_size;
2539 OPJ_UINT32 l_nb_tiles;
2541 opj_image_t *l_image = 00;
2542 opj_cp_v2_t *l_cp = 00;
2543 opj_image_comp_t * l_img_comp = 00;
2544 opj_tcp_v2_t * l_current_tile_param = 00;
2547 assert(p_j2k != 00);
2548 assert(p_manager != 00);
2549 assert(p_header_data != 00);
2551 l_image = p_j2k->m_private_image;
2552 l_cp = &(p_j2k->m_cp);
2554 /* minimum size == 39 - 3 (= minimum component parameter) */
2555 if (p_header_size < 36) {
2556 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2560 l_remaining_size = p_header_size - 36;
2561 l_nb_comp = l_remaining_size / 3;
2562 l_nb_comp_remain = l_remaining_size % 3;
2563 if (l_nb_comp_remain != 0){
2564 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2568 l_size = p_header_size + 2; /* Lsiz */
2570 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2572 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2573 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2575 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2577 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2579 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2581 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2583 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2585 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2587 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2589 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2592 l_image->numcomps = (OPJ_UINT16) l_tmp;
2594 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2598 if (l_image->numcomps != l_nb_comp) {
2599 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);
2604 if (l_cp->correct) {
2605 /* if JPWL is on, we check whether TX errors have damaged
2606 too much the SIZ parameters */
2607 if (!(l_image->x1 * l_image->y1)) {
2608 opj_event_msg_v2(p_manager, EVT_ERROR,
2609 "JPWL: bad image size (%d x %d)\n",
2610 l_image->x1, l_image->y1);
2611 if (!JPWL_ASSUME || JPWL_ASSUME) {
2612 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2617 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2618 if (l_image->numcomps != ((len - 38) / 3)) {
2619 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2620 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2621 l_image->numcomps, ((len - 38) / 3));
2623 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2626 */ /* we try to correct */
2627 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2628 if (l_image->numcomps < ((len - 38) / 3)) {
2629 len = 38 + 3 * l_image->numcomps;
2630 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2633 l_image->numcomps = ((len - 38) / 3);
2634 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2640 /* update components number in the jpwl_exp_comps filed */
2641 l_cp->exp_comps = l_image->numcomps;
2643 #endif /* USE_JPWL */
2645 /* Allocate the resulting image components */
2646 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2647 if (l_image->comps == 00){
2648 l_image->numcomps = 0;
2649 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2653 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2654 l_img_comp = l_image->comps;
2656 /* Read the component information */
2657 for (i = 0; i < l_image->numcomps; ++i){
2659 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2661 l_img_comp->prec = (tmp & 0x7f) + 1;
2662 l_img_comp->sgnd = tmp >> 7;
2663 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2665 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2666 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2668 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2671 if (l_cp->correct) {
2672 /* if JPWL is on, we check whether TX errors have damaged
2673 too much the SIZ parameters, again */
2674 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2675 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2676 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2677 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2679 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2682 /* we try to correct */
2683 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2684 if (!l_image->comps[i].dx) {
2685 l_image->comps[i].dx = 1;
2686 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2687 i, l_image->comps[i].dx);
2689 if (!l_image->comps[i].dy) {
2690 l_image->comps[i].dy = 1;
2691 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2692 i, l_image->comps[i].dy);
2696 #endif /* USE_JPWL */
2697 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2698 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2702 /* Compute the number of tiles */
2703 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2704 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2705 l_nb_tiles = l_cp->tw * l_cp->th;
2707 /* Define the tiles which will be decoded */
2708 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2709 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;
2710 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;
2711 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);
2712 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);
2715 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2716 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2717 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2718 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2722 if (l_cp->correct) {
2723 /* if JPWL is on, we check whether TX errors have damaged
2724 too much the SIZ parameters */
2725 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2726 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2727 "JPWL: bad number of tiles (%d x %d)\n",
2728 l_cp->tw, l_cp->th);
2730 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2733 /* we try to correct */
2734 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2737 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2740 if (l_cp->tw > l_cp->max_tiles) {
2742 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2743 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2744 l_cp->max_tiles, l_cp->tw);
2748 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2751 if (l_cp->th > l_cp->max_tiles) {
2753 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2754 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2755 l_cp->max_tiles, l_cp->th);
2759 #endif /* USE_JPWL */
2761 /* memory allocations */
2762 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2763 if (l_cp->tcps == 00) {
2764 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2767 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2770 if (l_cp->correct) {
2772 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2773 "JPWL: could not alloc tcps field of cp\n");
2774 if (!JPWL_ASSUME || JPWL_ASSUME) {
2775 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2780 #endif /* USE_JPWL */
2782 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2783 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2784 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2785 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2788 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2790 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2791 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2793 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2794 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2797 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));
2798 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2800 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2801 (opj_simple_mcc_decorrelation_data_t*)
2802 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2804 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2805 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2808 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));
2809 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2811 /* set up default dc level shift */
2812 for (i=0;i<l_image->numcomps;++i) {
2813 if (! l_image->comps[i].sgnd) {
2814 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2818 l_current_tile_param = l_cp->tcps;
2819 for (i = 0; i < l_nb_tiles; ++i) {
2820 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2821 if (l_current_tile_param->tccps == 00) {
2822 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2825 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2827 ++l_current_tile_param;
2830 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2831 opj_image_comp_header_update(l_image,l_cp);
2838 static void j2k_write_com(opj_j2k_t *j2k) {
2842 if(j2k->cp->comment) {
2843 opj_cio_t *cio = j2k->cio;
2844 char *comment = j2k->cp->comment;
2846 cio_write(cio, J2K_MS_COM, 2);
2847 lenp = cio_tell(cio);
2849 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2850 for (i = 0; i < strlen(comment); i++) {
2851 cio_write(cio, comment[i], 1);
2853 len = cio_tell(cio) - lenp;
2854 cio_seek(cio, lenp);
2855 cio_write(cio, len, 2);
2856 cio_seek(cio, lenp + len);
2860 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2866 * Writes the COM marker (comment)
2868 * @param p_stream the stream to write data to.
2869 * @param p_j2k J2K codec.
2870 * @param p_manager the user event manager.
2872 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2873 struct opj_stream_private *p_stream,
2874 struct opj_event_mgr * p_manager )
2876 OPJ_UINT32 l_comment_size;
2877 OPJ_UINT32 l_total_com_size;
2878 const OPJ_CHAR *l_comment;
2879 OPJ_BYTE * l_current_ptr = 00;
2882 assert(p_j2k != 00);
2883 assert(p_stream != 00);
2884 assert(p_manager != 00);
2886 l_comment = p_j2k->m_cp.comment;
2887 l_comment_size = strlen(l_comment);
2888 l_total_com_size = l_comment_size + 6;
2890 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2891 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2892 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2895 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2899 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2902 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2904 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2907 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2910 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2913 memcpy( l_current_ptr,l_comment,l_comment_size);
2915 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_total_com_size,p_manager) != l_total_com_size) {
2922 static void j2k_read_com(opj_j2k_t *j2k) {
2925 opj_cio_t *cio = j2k->cio;
2927 len = cio_read(cio, 2);
2928 cio_skip(cio, len - 2);
2931 * Reads a COM marker (comments)
2932 * @param p_header_data the data contained in the COM box.
2933 * @param jp2 the jpeg2000 file codec.
2934 * @param p_header_size the size of the data contained in the COM marker.
2935 * @param p_manager the user event manager.
2937 opj_bool j2k_read_com_v2 (
2938 opj_j2k_v2_t *p_j2k,
2939 OPJ_BYTE * p_header_data,
2940 OPJ_UINT32 p_header_size,
2941 struct opj_event_mgr * p_manager
2945 assert(p_j2k != 00);
2946 assert(p_manager != 00);
2947 assert(p_header_data != 00);
2948 (void)p_header_size;
2953 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2956 opj_cp_t *cp = j2k->cp;
2957 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2958 opj_tccp_t *tccp = &tcp->tccps[compno];
2959 opj_cio_t *cio = j2k->cio;
2961 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2962 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2963 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2964 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2965 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2967 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2968 for (i = 0; i < tccp->numresolutions; i++) {
2969 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2974 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2977 opj_cp_t *cp = j2k->cp;
2978 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2979 opj_tccp_t *tccp = &tcp->tccps[compno];
2980 opj_cio_t *cio = j2k->cio;
2982 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2984 /* If user wants to remove more resolutions than the codestream contains, return error*/
2985 assert(cp->reduce >= 0);
2986 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2987 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2988 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2989 j2k->state |= J2K_STATE_ERR;
2992 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2993 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2994 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2995 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2996 if (tccp->csty & J2K_CP_CSTY_PRT) {
2997 for (i = 0; i < tccp->numresolutions; i++) {
2998 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2999 tccp->prcw[i] = tmp & 0xf;
3000 tccp->prch[i] = tmp >> 4;
3005 if(j2k->cstr_info && compno == 0) {
3006 for (i = 0; i < tccp->numresolutions; i++) {
3007 if (tccp->csty & J2K_CP_CSTY_PRT) {
3008 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
3009 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
3012 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
3013 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
3020 static void j2k_write_cod(opj_j2k_t *j2k) {
3021 opj_cp_t *cp = NULL;
3022 opj_tcp_t *tcp = NULL;
3025 opj_cio_t *cio = j2k->cio;
3027 cio_write(cio, J2K_MS_COD, 2); /* COD */
3029 lenp = cio_tell(cio);
3033 tcp = &cp->tcps[j2k->curtileno];
3035 cio_write(cio, tcp->csty, 1); /* Scod */
3036 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
3037 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
3038 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
3040 j2k_write_cox(j2k, 0);
3041 len = cio_tell(cio) - lenp;
3042 cio_seek(cio, lenp);
3043 cio_write(cio, len, 2); /* Lcod */
3044 cio_seek(cio, lenp + len);
3047 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
3052 * Writes the COD marker (Coding style default)
3054 * @param p_stream the stream to write data to.
3055 * @param p_j2k J2K codec.
3056 * @param p_manager the user event manager.
3058 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
3059 struct opj_stream_private *p_stream,
3060 struct opj_event_mgr * p_manager )
3062 opj_cp_v2_t *l_cp = 00;
3063 opj_tcp_v2_t *l_tcp = 00;
3064 OPJ_UINT32 l_code_size,l_remaining_size;
3065 OPJ_BYTE * l_current_data = 00;
3068 assert(p_j2k != 00);
3069 assert(p_manager != 00);
3070 assert(p_stream != 00);
3072 l_cp = &(p_j2k->m_cp);
3073 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3074 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
3075 l_remaining_size = l_code_size;
3077 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3078 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3079 = (OPJ_BYTE*)opj_realloc(
3080 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3083 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3087 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
3090 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3092 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
3093 l_current_data += 2;
3095 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
3096 l_current_data += 2;
3098 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
3101 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
3104 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
3107 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
3110 l_remaining_size -= 9;
3112 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3113 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3117 if (l_remaining_size != 0) {
3118 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
3122 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_code_size,p_manager) != l_code_size) {
3129 static void j2k_read_cod(opj_j2k_t *j2k) {
3133 opj_cio_t *cio = j2k->cio;
3134 opj_cp_t *cp = j2k->cp;
3135 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3136 opj_image_t *image = j2k->image;
3138 len = cio_read(cio, 2); /* Lcod */
3139 tcp->csty = cio_read(cio, 1); /* Scod */
3140 tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
3141 tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
3142 tcp->mct = cio_read(cio, 1); /* SGcod (C) */
3144 pos = cio_tell(cio);
3145 for (i = 0; i < image->numcomps; i++) {
3146 tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
3148 j2k_read_cox(j2k, i);
3152 if (j2k->cstr_info) {
3153 opj_codestream_info_t *cstr_info = j2k->cstr_info;
3154 cstr_info->prog = tcp->prg;
3155 cstr_info->numlayers = tcp->numlayers;
3156 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
3157 for (i = 0; i < image->numcomps; i++) {
3158 cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
3164 * Reads a COD marker (Coding Styke defaults)
3165 * @param p_header_data the data contained in the COD box.
3166 * @param p_j2k the jpeg2000 codec.
3167 * @param p_header_size the size of the data contained in the COD marker.
3168 * @param p_manager the user event manager.
3170 opj_bool j2k_read_cod_v2 (
3171 opj_j2k_v2_t *p_j2k,
3172 OPJ_BYTE * p_header_data,
3173 OPJ_UINT32 p_header_size,
3174 struct opj_event_mgr * p_manager
3180 opj_cp_v2_t *l_cp = 00;
3181 opj_tcp_v2_t *l_tcp = 00;
3182 opj_image_t *l_image = 00;
3185 assert(p_header_data != 00);
3186 assert(p_j2k != 00);
3187 assert(p_manager != 00);
3189 l_image = p_j2k->m_private_image;
3190 l_cp = &(p_j2k->m_cp);
3192 /* If we are in the first tile-part header of the current tile */
3193 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3194 &l_cp->tcps[p_j2k->m_current_tile_number] :
3195 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3197 /* Make sure room is sufficient */
3198 if (p_header_size < 5) {
3199 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3203 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
3205 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
3207 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
3208 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
3211 /* If user didn't set a number layer to decode take the max specify in the codestream. */
3212 if (l_cp->m_specific_param.m_dec.m_layer) {
3213 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
3216 l_tcp->num_layers_to_decode = l_tcp->numlayers;
3219 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
3223 for (i = 0; i < l_image->numcomps; ++i) {
3224 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
3227 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3228 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3232 if (p_header_size != 0) {
3233 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
3237 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
3238 j2k_copy_tile_component_parameters(p_j2k);
3241 #ifdef WIP_REMOVE_MSD
3242 if (p_j2k->cstr_info) {
3243 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
3244 p_j2k->cstr_info->prog = l_tcp->prg;
3245 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
3246 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
3247 for (i = 0; i < l_image->numcomps; ++i) {
3248 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
3256 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
3259 opj_cp_t *cp = j2k->cp;
3260 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3261 opj_image_t *image = j2k->image;
3262 opj_cio_t *cio = j2k->cio;
3264 cio_write(cio, J2K_MS_COC, 2); /* COC */
3265 lenp = cio_tell(cio);
3267 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3268 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
3269 j2k_write_cox(j2k, compno);
3270 len = cio_tell(cio) - lenp;
3271 cio_seek(cio, lenp);
3272 cio_write(cio, len, 2); /* Lcoc */
3273 cio_seek(cio, lenp + len);
3277 * Writes the COC marker (Coding style component)
3279 * @param p_comp_no the index of the component to output.
3280 * @param p_stream the stream to write data to.
3281 * @param p_j2k J2K codec.
3282 * @param p_manager the user event manager.
3284 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
3285 OPJ_UINT32 p_comp_no,
3286 struct opj_stream_private *p_stream,
3287 struct opj_event_mgr * p_manager )
3289 OPJ_UINT32 l_coc_size,l_remaining_size;
3290 OPJ_UINT32 l_comp_room;
3293 assert(p_j2k != 00);
3294 assert(p_manager != 00);
3295 assert(p_stream != 00);
3297 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
3299 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3301 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3302 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3303 = (OPJ_BYTE*)opj_realloc(
3304 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3306 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3310 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
3313 j2k_write_coc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);
3315 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_coc_size,p_manager) != l_coc_size) {
3323 * Writes the COC marker (Coding style component)
3325 * @param p_comp_no the index of the component to output.
3326 * @param p_stream the stream to write data to.
3327 * @param p_j2k J2K codec.
3328 * @param p_manager the user event manager.
3330 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3331 OPJ_UINT32 p_comp_no,
3333 OPJ_UINT32 * p_data_written,
3334 struct opj_event_mgr * p_manager )
3336 opj_cp_v2_t *l_cp = 00;
3337 opj_tcp_v2_t *l_tcp = 00;
3338 OPJ_UINT32 l_coc_size,l_remaining_size;
3339 OPJ_BYTE * l_current_data = 00;
3340 opj_image_t *l_image = 00;
3341 OPJ_UINT32 l_comp_room;
3344 assert(p_j2k != 00);
3345 assert(p_manager != 00);
3347 l_cp = &(p_j2k->m_cp);
3348 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3349 l_image = p_j2k->m_private_image;
3350 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3352 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3353 l_remaining_size = l_coc_size;
3355 l_current_data = p_data;
3357 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3358 l_current_data += 2;
3360 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3361 l_current_data += 2;
3363 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3364 l_current_data+=l_comp_room;
3366 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3369 l_remaining_size -= (5 + l_comp_room);
3370 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3371 * p_data_written = l_coc_size;
3375 * Gets the maximum size taken by a coc.
3377 * @param p_j2k the jpeg2000 codec to use.
3379 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3382 OPJ_UINT32 l_nb_comp;
3383 OPJ_UINT32 l_nb_tiles;
3384 OPJ_UINT32 l_max = 0;
3388 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3389 l_nb_comp = p_j2k->m_private_image->numcomps;
3391 for (i=0;i<l_nb_tiles;++i) {
3392 for (j=0;j<l_nb_comp;++j) {
3393 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3400 static void j2k_read_coc(opj_j2k_t *j2k) {
3403 opj_cp_t *cp = j2k->cp;
3404 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3405 opj_image_t *image = j2k->image;
3406 opj_cio_t *cio = j2k->cio;
3408 len = cio_read(cio, 2); /* Lcoc */
3409 compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
3410 tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
3411 j2k_read_cox(j2k, compno);
3415 * Reads a COC marker (Coding Style Component)
3416 * @param p_header_data the data contained in the COC box.
3417 * @param p_j2k the jpeg2000 codec.
3418 * @param p_header_size the size of the data contained in the COC marker.
3419 * @param p_manager the user event manager.
3421 opj_bool j2k_read_coc_v2 (
3422 opj_j2k_v2_t *p_j2k,
3423 OPJ_BYTE * p_header_data,
3424 OPJ_UINT32 p_header_size,
3425 struct opj_event_mgr * p_manager
3428 opj_cp_v2_t *l_cp = NULL;
3429 opj_tcp_v2_t *l_tcp = NULL;
3430 opj_image_t *l_image = NULL;
3431 OPJ_UINT32 l_comp_room;
3432 OPJ_UINT32 l_comp_no;
3435 assert(p_header_data != 00);
3436 assert(p_j2k != 00);
3437 assert(p_manager != 00);
3439 l_cp = &(p_j2k->m_cp);
3440 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3441 &l_cp->tcps[p_j2k->m_current_tile_number] :
3442 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3443 l_image = p_j2k->m_private_image;
3445 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3447 /* make sure room is sufficient*/
3448 if (p_header_size < l_comp_room + 1) {
3449 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3452 p_header_size -= l_comp_room + 1;
3454 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3455 p_header_data += l_comp_room;
3456 if (l_comp_no >= l_image->numcomps) {
3457 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3461 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3464 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3465 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3469 if (p_header_size != 0) {
3470 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3476 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3477 int bandno, numbands;
3480 opj_cp_t *cp = j2k->cp;
3481 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3482 opj_tccp_t *tccp = &tcp->tccps[compno];
3483 opj_cio_t *cio = j2k->cio;
3485 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3486 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3488 for (bandno = 0; bandno < numbands; bandno++) {
3489 expn = tccp->stepsizes[bandno].expn;
3490 mant = tccp->stepsizes[bandno].mant;
3492 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3493 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3495 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3500 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3502 int bandno, numbands;
3504 opj_cp_t *cp = j2k->cp;
3505 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3506 opj_tccp_t *tccp = &tcp->tccps[compno];
3507 opj_cio_t *cio = j2k->cio;
3509 tmp = cio_read(cio, 1); /* Sqcx */
3510 tccp->qntsty = tmp & 0x1f;
3511 tccp->numgbits = tmp >> 5;
3512 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3513 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3516 if (j2k->cp->correct) {
3518 /* if JPWL is on, we check whether there are too many subbands */
3519 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3520 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3521 "JPWL: bad number of subbands in Sqcx (%d)\n",
3524 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3527 /* we try to correct */
3529 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3530 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3537 /* We check whether there are too many subbands */
3538 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3539 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3540 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3541 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3544 #endif /* USE_JPWL */
3546 for (bandno = 0; bandno < numbands; bandno++) {
3548 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3549 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3552 tmp = cio_read(cio, 2); /* SPqcx_i */
3556 if (bandno < J2K_MAXBANDS){
3557 tccp->stepsizes[bandno].expn = expn;
3558 tccp->stepsizes[bandno].mant = mant;
3562 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3563 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3564 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3565 tccp->stepsizes[bandno].expn =
3566 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3567 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3568 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3574 static void j2k_write_qcd(opj_j2k_t *j2k) {
3577 opj_cio_t *cio = j2k->cio;
3579 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3580 lenp = cio_tell(cio);
3582 j2k_write_qcx(j2k, 0);
3583 len = cio_tell(cio) - lenp;
3584 cio_seek(cio, lenp);
3585 cio_write(cio, len, 2); /* Lqcd */
3586 cio_seek(cio, lenp + len);
3589 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3593 * Writes the QCD marker (quantization default)
3595 * @param p_comp_number the index of the component to output.
3596 * @param p_stream the stream to write data to.
3597 * @param p_j2k J2K codec.
3598 * @param p_manager the user event manager.
3600 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3601 struct opj_stream_private *p_stream,
3602 struct opj_event_mgr * p_manager )
3604 opj_cp_v2_t *l_cp = 00;
3605 opj_tcp_v2_t *l_tcp = 00;
3606 OPJ_UINT32 l_qcd_size,l_remaining_size;
3607 OPJ_BYTE * l_current_data = 00;
3610 assert(p_j2k != 00);
3611 assert(p_manager != 00);
3612 assert(p_stream != 00);
3614 l_cp = &(p_j2k->m_cp);
3615 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3616 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3617 l_remaining_size = l_qcd_size;
3619 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3620 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3621 = (OPJ_BYTE*)opj_realloc(
3622 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3625 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3629 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3632 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3634 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3635 l_current_data += 2;
3637 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3638 l_current_data += 2;
3640 l_remaining_size -= 4;
3642 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3643 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3647 if (l_remaining_size != 0) {
3648 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3652 if (opj_stream_write_data(p_stream, p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcd_size,p_manager) != l_qcd_size) {
3660 static void j2k_read_qcd(opj_j2k_t *j2k) {
3664 opj_cio_t *cio = j2k->cio;
3665 opj_image_t *image = j2k->image;
3667 len = cio_read(cio, 2); /* Lqcd */
3668 pos = cio_tell(cio);
3669 for (i = 0; i < image->numcomps; i++) {
3671 j2k_read_qcx(j2k, i, len - 2);
3676 * Reads a QCD marker (Quantization defaults)
3677 * @param p_header_data the data contained in the QCD box.
3678 * @param p_j2k the jpeg2000 codec.
3679 * @param p_header_size the size of the data contained in the QCD marker.
3680 * @param p_manager the user event manager.
3682 opj_bool j2k_read_qcd_v2 (
3683 opj_j2k_v2_t *p_j2k,
3684 OPJ_BYTE * p_header_data,
3685 OPJ_UINT32 p_header_size,
3686 struct opj_event_mgr * p_manager
3690 assert(p_header_data != 00);
3691 assert(p_j2k != 00);
3692 assert(p_manager != 00);
3694 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3695 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3699 if (p_header_size != 0) {
3700 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3704 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3705 j2k_copy_tile_quantization_parameters(p_j2k);
3710 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3713 opj_cio_t *cio = j2k->cio;
3715 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3716 lenp = cio_tell(cio);
3718 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3719 j2k_write_qcx(j2k, compno);
3720 len = cio_tell(cio) - lenp;
3721 cio_seek(cio, lenp);
3722 cio_write(cio, len, 2); /* Lqcc */
3723 cio_seek(cio, lenp + len);
3727 * Writes the QCC marker (quantization component)
3729 * @param p_comp_no the index of the component to output.
3730 * @param p_stream the stream to write data to.
3731 * @param p_j2k J2K codec.
3732 * @param p_manager the user event manager.
3734 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3735 OPJ_UINT32 p_comp_no,
3736 struct opj_stream_private *p_stream,
3737 struct opj_event_mgr * p_manager )
3739 OPJ_UINT32 l_qcc_size,l_remaining_size;
3742 assert(p_j2k != 00);
3743 assert(p_manager != 00);
3744 assert(p_stream != 00);
3746 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3747 l_remaining_size = l_qcc_size;
3749 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3750 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3751 = (OPJ_BYTE*)opj_realloc(
3752 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3754 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3758 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3761 j2k_write_qcc_in_memory(p_j2k,p_comp_no,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_remaining_size,p_manager);
3763 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_qcc_size,p_manager) != l_qcc_size) {
3771 * Writes the QCC marker (quantization component)
3773 * @param p_comp_no the index of the component to output.
3774 * @param p_stream the stream to write data to.
3775 * @param p_j2k J2K codec.
3776 * @param p_manager the user event manager.
3778 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3779 OPJ_UINT32 p_comp_no,
3781 OPJ_UINT32 * p_data_written,
3782 struct opj_event_mgr * p_manager )
3784 OPJ_UINT32 l_qcc_size,l_remaining_size;
3785 OPJ_BYTE * l_current_data = 00;
3788 assert(p_j2k != 00);
3789 assert(p_manager != 00);
3791 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3792 l_remaining_size = l_qcc_size;
3794 l_current_data = p_data;
3796 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3797 l_current_data += 2;
3799 if (p_j2k->m_private_image->numcomps <= 256) {
3802 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3803 l_current_data += 2;
3805 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3808 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3809 l_remaining_size -= 6;
3812 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3813 l_current_data += 2;
3815 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3818 l_remaining_size -= 6;
3821 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3823 *p_data_written = l_qcc_size;
3827 * Gets the maximum size taken by a qcc.
3829 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3831 return j2k_get_max_coc_size(p_j2k);
3834 static void j2k_read_qcc(opj_j2k_t *j2k) {
3836 int numcomp = j2k->image->numcomps;
3837 opj_cio_t *cio = j2k->cio;
3839 len = cio_read(cio, 2); /* Lqcc */
3840 compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
3843 if (j2k->cp->correct) {
3845 static int backup_compno = 0;
3847 /* compno is negative or larger than the number of components!!! */
3848 if ((compno < 0) || (compno >= numcomp)) {
3849 opj_event_msg(j2k->cinfo, EVT_ERROR,
3850 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3853 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3856 /* we try to correct */
3857 compno = backup_compno % numcomp;
3858 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
3859 "- setting component number to %d\n",
3863 /* keep your private count of tiles */
3866 #endif /* USE_JPWL */
3868 j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
3872 * Reads a QCC marker (Quantization component)
3873 * @param p_header_data the data contained in the QCC box.
3874 * @param p_j2k the jpeg2000 codec.
3875 * @param p_header_size the size of the data contained in the QCC marker.
3876 * @param p_manager the user event manager.
3878 opj_bool j2k_read_qcc_v2( opj_j2k_v2_t *p_j2k,
3879 OPJ_BYTE * p_header_data,
3880 OPJ_UINT32 p_header_size,
3881 struct opj_event_mgr * p_manager)
3883 OPJ_UINT32 l_num_comp,l_comp_no;
3886 assert(p_header_data != 00);
3887 assert(p_j2k != 00);
3888 assert(p_manager != 00);
3890 l_num_comp = p_j2k->m_private_image->numcomps;
3892 if (l_num_comp <= 256) {
3893 if (p_header_size < 1) {
3894 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3897 opj_read_bytes(p_header_data,&l_comp_no,1);
3902 if (p_header_size < 2) {
3903 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3906 opj_read_bytes(p_header_data,&l_comp_no,2);
3912 if (p_j2k->m_cp.correct) {
3914 static OPJ_UINT32 backup_compno = 0;
3916 /* compno is negative or larger than the number of components!!! */
3917 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3918 opj_event_msg_v2(p_manager, EVT_ERROR,
3919 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3920 l_comp_no, l_num_comp);
3922 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3925 /* we try to correct */
3926 l_comp_no = backup_compno % l_num_comp;
3927 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3928 "- setting component number to %d\n",
3932 /* keep your private count of tiles */
3935 #endif /* USE_JPWL */
3937 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3938 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3942 if (p_header_size != 0) {
3943 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3951 static void j2k_write_poc(opj_j2k_t *j2k) {
3952 int len, numpchgs, i;
3954 int numcomps = j2k->image->numcomps;
3956 opj_cp_t *cp = j2k->cp;
3957 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3958 opj_tccp_t *tccp = &tcp->tccps[0];
3959 opj_cio_t *cio = j2k->cio;
3961 numpchgs = 1 + tcp->numpocs;
3962 cio_write(cio, J2K_MS_POC, 2); /* POC */
3963 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3964 cio_write(cio, len, 2); /* Lpoc */
3965 for (i = 0; i < numpchgs; i++) {
3966 opj_poc_t *poc = &tcp->pocs[i];
3967 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3968 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3969 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3970 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3971 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3972 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3973 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3974 poc->compno1 = int_min(poc->compno1, numcomps);
3975 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3980 * Writes the POC marker (Progression Order Change)
3982 * @param p_stream the stream to write data to.
3983 * @param p_j2k J2K codec.
3984 * @param p_manager the user event manager.
3986 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3987 struct opj_stream_private *p_stream,
3988 struct opj_event_mgr * p_manager )
3990 OPJ_UINT32 l_nb_comp;
3991 OPJ_UINT32 l_nb_poc;
3992 OPJ_UINT32 l_poc_size;
3993 OPJ_UINT32 l_written_size = 0;
3994 opj_tcp_v2_t *l_tcp = 00;
3995 opj_tccp_t *l_tccp = 00;
3996 OPJ_UINT32 l_poc_room;
3999 assert(p_j2k != 00);
4000 assert(p_manager != 00);
4001 assert(p_stream != 00);
4003 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4004 l_tccp = &l_tcp->tccps[0];
4005 l_nb_comp = p_j2k->m_private_image->numcomps;
4006 l_nb_poc = 1 + l_tcp->numpocs;
4008 if (l_nb_comp <= 256) {
4014 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4016 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4017 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4018 = (OPJ_BYTE*)opj_realloc(
4019 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4022 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4026 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
4029 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
4031 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_poc_size,p_manager) != l_poc_size) {
4040 * Writes the POC marker (Progression Order Change)
4042 * @param p_stream the stream to write data to.
4043 * @param p_j2k J2K codec.
4044 * @param p_manager the user event manager.
4046 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
4048 OPJ_UINT32 * p_data_written,
4049 struct opj_event_mgr * p_manager )
4052 OPJ_BYTE * l_current_data = 00;
4053 OPJ_UINT32 l_nb_comp;
4054 OPJ_UINT32 l_nb_poc;
4055 OPJ_UINT32 l_poc_size;
4056 opj_image_t *l_image = 00;
4057 opj_tcp_v2_t *l_tcp = 00;
4058 opj_tccp_t *l_tccp = 00;
4059 opj_poc_t *l_current_poc = 00;
4060 OPJ_UINT32 l_poc_room;
4063 assert(p_j2k != 00);
4064 assert(p_manager != 00);
4066 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
4067 l_tccp = &l_tcp->tccps[0];
4068 l_image = p_j2k->m_private_image;
4069 l_nb_comp = l_image->numcomps;
4070 l_nb_poc = 1 + l_tcp->numpocs;
4072 if (l_nb_comp <= 256) {
4079 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
4081 l_current_data = p_data;
4083 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
4084 l_current_data += 2;
4086 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
4087 l_current_data += 2;
4089 l_current_poc = l_tcp->pocs;
4090 for (i = 0; i < l_nb_poc; ++i) {
4091 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
4094 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
4095 l_current_data+=l_poc_room;
4097 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
4100 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
4103 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
4104 l_current_data+=l_poc_room;
4106 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
4109 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
4110 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
4111 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
4112 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
4117 *p_data_written = l_poc_size;
4121 * Gets the maximum size taken by the writing of a POC.
4123 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
4125 opj_tcp_v2_t * l_tcp = 00;
4126 OPJ_UINT32 l_nb_tiles = 0;
4127 OPJ_UINT32 l_max_poc = 0;
4130 l_tcp = p_j2k->m_cp.tcps;
4131 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4133 for (i=0;i<l_nb_tiles;++i) {
4134 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
4140 return 4 + 9 * l_max_poc;
4144 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
4146 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
4149 OPJ_UINT32 l_nb_tiles;
4150 OPJ_UINT32 l_max = 0;
4151 opj_tcp_v2_t * l_tcp = 00;
4153 l_tcp = p_j2k->m_cp.tcps;
4154 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
4156 for (i=0;i<l_nb_tiles;++i) {
4157 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
4167 * Gets the maximum size taken by the headers of the SOT.
4169 * @param p_j2k the jpeg2000 codec to use.
4171 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
4173 OPJ_UINT32 l_nb_bytes = 0;
4174 OPJ_UINT32 l_nb_comps;
4175 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
4177 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
4178 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
4180 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
4181 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
4182 l_nb_bytes += l_nb_comps * l_coc_bytes;
4184 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
4185 l_nb_bytes += l_nb_comps * l_qcc_bytes;
4188 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
4190 /*** DEVELOPER CORNER, Add room for your headers ***/
4195 static void j2k_read_poc(opj_j2k_t *j2k) {
4196 int len, numpchgs, i, old_poc;
4198 int numcomps = j2k->image->numcomps;
4200 opj_cp_t *cp = j2k->cp;
4201 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
4202 opj_cio_t *cio = j2k->cio;
4204 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
4206 len = cio_read(cio, 2); /* Lpoc */
4207 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
4209 for (i = old_poc; i < numpchgs + old_poc; i++) {
4211 poc = &tcp->pocs[i];
4212 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
4213 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
4214 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
4215 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
4216 poc->compno1 = int_min(
4217 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
4218 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
4221 tcp->numpocs = numpchgs + old_poc - 1;
4225 * Reads a POC marker (Progression Order Change)
4227 * @param p_header_data the data contained in the POC box.
4228 * @param p_j2k the jpeg2000 codec.
4229 * @param p_header_size the size of the data contained in the POC marker.
4230 * @param p_manager the user event manager.
4232 opj_bool j2k_read_poc_v2 (
4233 opj_j2k_v2_t *p_j2k,
4234 OPJ_BYTE * p_header_data,
4235 OPJ_UINT32 p_header_size,
4236 struct opj_event_mgr * p_manager)
4238 OPJ_UINT32 i, l_nb_comp, l_tmp;
4239 opj_image_t * l_image = 00;
4240 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
4241 OPJ_UINT32 l_chunk_size, l_comp_room;
4243 opj_cp_v2_t *l_cp = 00;
4244 opj_tcp_v2_t *l_tcp = 00;
4245 opj_poc_t *l_current_poc = 00;
4248 assert(p_header_data != 00);
4249 assert(p_j2k != 00);
4250 assert(p_manager != 00);
4252 l_image = p_j2k->m_private_image;
4253 l_nb_comp = l_image->numcomps;
4254 if (l_nb_comp <= 256) {
4260 l_chunk_size = 5 + 2 * l_comp_room;
4261 l_current_poc_nb = p_header_size / l_chunk_size;
4262 l_current_poc_remaining = p_header_size % l_chunk_size;
4264 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
4265 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
4269 l_cp = &(p_j2k->m_cp);
4270 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4271 &l_cp->tcps[p_j2k->m_current_tile_number] :
4272 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4273 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
4274 l_current_poc_nb += l_old_poc_nb;
4276 assert(l_current_poc_nb < 32);
4278 /* now poc is in use.*/
4281 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
4282 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
4283 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
4285 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
4286 p_header_data+=l_comp_room;
4287 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
4289 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
4291 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
4292 p_header_data+=l_comp_room;
4293 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
4295 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
4296 /* make sure comp is in acceptable bounds */
4297 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
4301 l_tcp->numpocs = l_current_poc_nb - 1;
4305 static void j2k_read_crg(opj_j2k_t *j2k) {
4306 int len, i, Xcrg_i, Ycrg_i;
4308 opj_cio_t *cio = j2k->cio;
4309 int numcomps = j2k->image->numcomps;
4311 len = cio_read(cio, 2); /* Lcrg */
4312 for (i = 0; i < numcomps; i++) {
4313 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
4314 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
4319 * Reads a CRG marker (Component registration)
4321 * @param p_header_data the data contained in the TLM box.
4322 * @param p_j2k the jpeg2000 codec.
4323 * @param p_header_size the size of the data contained in the TLM marker.
4324 * @param p_manager the user event manager.
4326 opj_bool j2k_read_crg_v2 (
4327 opj_j2k_v2_t *p_j2k,
4328 OPJ_BYTE * p_header_data,
4329 OPJ_UINT32 p_header_size,
4330 struct opj_event_mgr * p_manager
4333 OPJ_UINT32 l_nb_comp;
4335 assert(p_header_data != 00);
4336 assert(p_j2k != 00);
4337 assert(p_manager != 00);
4339 l_nb_comp = p_j2k->m_private_image->numcomps;
4341 if (p_header_size != l_nb_comp *4) {
4342 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
4345 /* Do not care of this at the moment since only local variables are set here */
4348 (i = 0; i < l_nb_comp; ++i)
4350 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
4352 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
4359 static void j2k_read_tlm(opj_j2k_t *j2k) {
4360 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
4361 long int Ttlm_i, Ptlm_i;
4363 opj_cio_t *cio = j2k->cio;
4365 len = cio_read(cio, 2); /* Ltlm */
4366 Ztlm = cio_read(cio, 1); /* Ztlm */
4367 Stlm = cio_read(cio, 1); /* Stlm */
4368 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
4369 SP = (Stlm >> 6) & 0x01;
4370 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
4371 for (i = 0; i < tile_tlm; i++) {
4372 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
4373 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
4378 * Reads a TLM marker (Tile Length Marker)
4380 * @param p_header_data the data contained in the TLM box.
4381 * @param p_j2k the jpeg2000 codec.
4382 * @param p_header_size the size of the data contained in the TLM marker.
4383 * @param p_manager the user event manager.
4385 opj_bool j2k_read_tlm_v2 (
4386 opj_j2k_v2_t *p_j2k,
4387 OPJ_BYTE * p_header_data,
4388 OPJ_UINT32 p_header_size,
4389 struct opj_event_mgr * p_manager
4392 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4394 assert(p_header_data != 00);
4395 assert(p_j2k != 00);
4396 assert(p_manager != 00);
4398 if (p_header_size < 2) {
4399 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4404 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4406 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4409 l_ST = ((l_Stlm >> 4) & 0x3);
4410 l_SP = (l_Stlm >> 6) & 0x1;
4412 l_Ptlm_size = (l_SP + 1) * 2;
4413 l_quotient = l_Ptlm_size + l_ST;
4415 l_tot_num_tp = p_header_size / l_quotient;
4416 l_tot_num_tp_remaining = p_header_size % l_quotient;
4418 if (l_tot_num_tp_remaining != 0) {
4419 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4422 /* FIXME Do not care of this at the moment since only local variables are set here */
4425 (i = 0; i < l_tot_num_tp; ++i)
4427 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4428 p_header_data += l_ST;
4429 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4430 p_header_data += l_Ptlm_size;
4435 static void j2k_read_plm(opj_j2k_t *j2k) {
4436 int len, i, Zplm, Nplm, add, packet_len = 0;
4438 opj_cio_t *cio = j2k->cio;
4440 len = cio_read(cio, 2); /* Lplm */
4441 Zplm = cio_read(cio, 1); /* Zplm */
4444 Nplm = cio_read(cio, 4); /* Nplm */
4446 for (i = Nplm; i > 0; i--) {
4447 add = cio_read(cio, 1);
4449 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4450 if ((add & 0x80) == 0) {
4461 * Reads a PLM marker (Packet length, main header marker)
4463 * @param p_header_data the data contained in the TLM box.
4464 * @param p_j2k the jpeg2000 codec.
4465 * @param p_header_size the size of the data contained in the TLM marker.
4466 * @param p_manager the user event manager.
4468 opj_bool j2k_read_plm_v2 (
4469 opj_j2k_v2_t *p_j2k,
4470 OPJ_BYTE * p_header_data,
4471 OPJ_UINT32 p_header_size,
4472 struct opj_event_mgr * p_manager
4476 assert(p_header_data != 00);
4477 assert(p_j2k != 00);
4478 assert(p_manager != 00);
4480 if (p_header_size < 1) {
4481 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4484 /* Do not care of this at the moment since only local variables are set here */
4486 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4493 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4495 p_header_size -= (1+l_Nplm);
4499 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4503 (i = 0; i < l_Nplm; ++i)
4505 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4507 // take only the last seven bytes
4508 l_packet_len |= (l_tmp & 0x7f);
4516 // store packet length and proceed to next packet
4523 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4531 static void j2k_read_plt(opj_j2k_t *j2k) {
4532 int len, i, Zplt, packet_len = 0, add;
4534 opj_cio_t *cio = j2k->cio;
4536 len = cio_read(cio, 2); /* Lplt */
4537 Zplt = cio_read(cio, 1); /* Zplt */
4538 for (i = len - 3; i > 0; i--) {
4539 add = cio_read(cio, 1);
4540 packet_len = (packet_len << 7) + add; /* Iplt_i */
4541 if ((add & 0x80) == 0) {
4549 * Reads a PLT marker (Packet length, tile-part header)
4551 * @param p_header_data the data contained in the PLT box.
4552 * @param p_j2k the jpeg2000 codec.
4553 * @param p_header_size the size of the data contained in the PLT marker.
4554 * @param p_manager the user event manager.
4556 opj_bool j2k_read_plt_v2 (
4557 opj_j2k_v2_t *p_j2k,
4558 OPJ_BYTE * p_header_data,
4559 OPJ_UINT32 p_header_size,
4560 struct opj_event_mgr * p_manager
4563 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4566 assert(p_header_data != 00);
4567 assert(p_j2k != 00);
4568 assert(p_manager != 00);
4570 if (p_header_size < 1) {
4571 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4575 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4579 for (i = 0; i < p_header_size; ++i) {
4580 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4582 /* take only the last seven bytes */
4583 l_packet_len |= (l_tmp & 0x7f);
4588 /* store packet length and proceed to next packet */
4593 if (l_packet_len != 0) {
4594 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4601 static void j2k_read_ppm(opj_j2k_t *j2k) {
4602 int len, Z_ppm, i, j;
4605 opj_cp_t *cp = j2k->cp;
4606 opj_cio_t *cio = j2k->cio;
4608 len = cio_read(cio, 2);
4611 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4614 if (cp->ppm_previous == 0) {
4615 N_ppm = cio_read(cio, 4); /* N_ppm */
4618 N_ppm = cp->ppm_previous;
4621 if (Z_ppm == 0) { /* First PPM marker */
4622 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4623 cp->ppm_data_first = cp->ppm_data;
4624 cp->ppm_len = N_ppm;
4625 } else { /* NON-first PPM marker */
4626 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4629 /* this memory allocation check could be done even in non-JPWL cases */
4631 if (!cp->ppm_data) {
4632 opj_event_msg(j2k->cinfo, EVT_ERROR,
4633 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4635 if (!JPWL_ASSUME || JPWL_ASSUME) {
4636 opj_free(cp->ppm_data);
4637 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4644 cp->ppm_data_first = cp->ppm_data;
4645 cp->ppm_len = N_ppm + cp->ppm_store;
4647 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4648 cp->ppm_data[j] = cio_read(cio, 1);
4652 break; /* Case of non-finished packet header in present marker but finished in next one */
4654 cp->ppm_previous = i - 1;
4659 * Reads a PPM marker (Packed packet headers, main header)
4661 * @param p_header_data the data contained in the POC box.
4662 * @param p_j2k the jpeg2000 codec.
4663 * @param p_header_size the size of the data contained in the POC marker.
4664 * @param p_manager the user event manager.
4667 opj_bool j2k_read_ppm_v2 (
4668 opj_j2k_v2_t *p_j2k,
4669 OPJ_BYTE * p_header_data,
4670 OPJ_UINT32 p_header_size,
4671 struct opj_event_mgr * p_manager
4675 opj_cp_v2_t *l_cp = 00;
4676 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4679 assert(p_header_data != 00);
4680 assert(p_j2k != 00);
4681 assert(p_manager != 00);
4683 if (p_header_size < 1) {
4684 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4688 l_cp = &(p_j2k->m_cp);
4691 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4695 /* First PPM marker */
4697 if (p_header_size < 4) {
4698 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4702 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4706 /* First PPM marker: Initialization */
4707 l_cp->ppm_len = l_N_ppm;
4708 l_cp->ppm_data_size = 0;
4710 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4711 if (l_cp->ppm_buffer == 00) {
4712 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4715 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4717 l_cp->ppm_data = l_cp->ppm_buffer;
4721 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4722 if (p_header_size >= 4) {
4724 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4727 l_cp->ppm_len += l_N_ppm ;
4729 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4730 if (l_cp->ppm_buffer == 00) {
4731 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4734 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4736 l_cp->ppm_data = l_cp->ppm_buffer;
4743 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4745 if (l_remaining_data <= p_header_size) {
4746 /* we must store less information than available in the packet */
4747 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4748 l_cp->ppm_data_size = l_cp->ppm_len;
4749 p_header_size -= l_remaining_data;
4750 p_header_data += l_remaining_data;
4753 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4754 l_cp->ppm_data_size += p_header_size;
4755 p_header_data += p_header_size;
4768 * Reads a PPM marker (Packed packet headers, main header)
4770 * @param p_header_data the data contained in the POC box.
4771 * @param p_j2k the jpeg2000 codec.
4772 * @param p_header_size the size of the data contained in the POC marker.
4773 * @param p_manager the user event manager.
4775 opj_bool j2k_read_ppm_v3 (
4776 opj_j2k_v2_t *p_j2k,
4777 OPJ_BYTE * p_header_data,
4778 OPJ_UINT32 p_header_size,
4779 struct opj_event_mgr * p_manager
4782 opj_cp_v2_t *l_cp = 00;
4783 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4786 assert(p_header_data != 00);
4787 assert(p_j2k != 00);
4788 assert(p_manager != 00);
4790 /* Minimum size of PPM marker is equal to the size of Zppm element */
4791 if (p_header_size < 1) {
4792 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4796 l_cp = &(p_j2k->m_cp);
4799 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4803 /* First PPM marker */
4805 /* We need now at least the Nppm^0 element */
4806 if (p_header_size < 4) {
4807 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4811 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4815 /* First PPM marker: Initialization */
4816 l_cp->ppm_len = l_N_ppm;
4817 l_cp->ppm_data_read = 0;
4819 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4820 if (l_cp->ppm_data == 00) {
4821 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4824 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4826 l_cp->ppm_data_current = l_cp->ppm_data;
4828 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4831 if (p_header_size < 4) {
4832 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4836 /* Uncompleted Ippm series in the previous PPM marker?*/
4837 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4838 /* Get the place where add the remaining Ippm series*/
4839 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4840 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4843 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4847 /* Increase the size of ppm_data to add the new Ippm series*/
4848 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4850 /* Keep the position of the place where concatenate the new series*/
4851 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4852 l_cp->ppm_len += l_N_ppm;
4857 l_remaining_data = p_header_size;
4859 while (l_remaining_data >= l_N_ppm) {
4860 /* read a complete Ippm series*/
4861 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4862 p_header_size -= l_N_ppm;
4863 p_header_data += l_N_ppm;
4865 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4869 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4874 l_remaining_data = p_header_size;
4878 l_remaining_data = p_header_size;
4880 /* Next Ippm series is a complete series ?*/
4881 if (l_remaining_data > l_N_ppm) {
4882 /* Increase the size of ppm_data to add the new Ippm series*/
4883 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4885 /* Keep the position of the place where concatenate the new series */
4886 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4887 l_cp->ppm_len += l_N_ppm;
4892 /* Need to read an incomplete Ippm series*/
4893 if (l_remaining_data) {
4894 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4896 /* Keep the position of the place where concatenate the new series*/
4897 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4898 l_cp->ppm_len += l_N_ppm;
4900 /* Read incomplete Ippm series*/
4901 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4902 p_header_size -= l_remaining_data;
4903 p_header_data += l_remaining_data;
4905 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4910 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4911 if (p_header_size >= 4) {
4913 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4916 l_cp->ppm_len += l_N_ppm ;
4918 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4919 if (l_cp->ppm_buffer == 00) {
4920 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4923 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4925 l_cp->ppm_data = l_cp->ppm_buffer;
4932 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4934 if (l_remaining_data <= p_header_size) {
4935 /* we must store less information than available in the packet */
4936 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4937 l_cp->ppm_data_size = l_cp->ppm_len;
4938 p_header_size -= l_remaining_data;
4939 p_header_data += l_remaining_data;
4942 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4943 l_cp->ppm_data_size += p_header_size;
4944 p_header_data += p_header_size;
4953 static void j2k_read_ppt(opj_j2k_t *j2k) {
4954 int len, Z_ppt, i, j = 0;
4956 opj_cp_t *cp = j2k->cp;
4957 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4958 opj_cio_t *cio = j2k->cio;
4960 len = cio_read(cio, 2);
4961 Z_ppt = cio_read(cio, 1);
4963 if (Z_ppt == 0) { /* First PPT marker */
4964 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4965 tcp->ppt_data_first = tcp->ppt_data;
4967 tcp->ppt_len = len - 3;
4968 } else { /* NON-first PPT marker */
4969 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4970 tcp->ppt_data_first = tcp->ppt_data;
4971 tcp->ppt_len = len - 3 + tcp->ppt_store;
4974 for (i = len - 3; i > 0; i--) {
4975 tcp->ppt_data[j] = cio_read(cio, 1);
4982 * Reads a PPT marker (Packed packet headers, tile-part header)
4984 * @param p_header_data the data contained in the PPT box.
4985 * @param p_j2k the jpeg2000 codec.
4986 * @param p_header_size the size of the data contained in the PPT marker.
4987 * @param p_manager the user event manager.
4989 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4990 OPJ_BYTE * p_header_data,
4991 OPJ_UINT32 p_header_size,
4992 struct opj_event_mgr * p_manager )
4994 opj_cp_v2_t *l_cp = 00;
4995 opj_tcp_v2_t *l_tcp = 00;
4999 assert(p_header_data != 00);
5000 assert(p_j2k != 00);
5001 assert(p_manager != 00);
5003 /* We need to have the Z_ppt element at minimum */
5004 if (p_header_size < 1) {
5005 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
5009 l_cp = &(p_j2k->m_cp);
5011 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
5015 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
5018 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
5022 /* Allocate buffer to read the packet header */
5024 /* First PPT marker */
5025 l_tcp->ppt_data_size = 0;
5026 l_tcp->ppt_len = p_header_size;
5028 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
5029 if (l_tcp->ppt_buffer == 00) {
5030 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5033 l_tcp->ppt_data = l_tcp->ppt_buffer;
5035 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
5038 l_tcp->ppt_len += p_header_size;
5040 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
5041 if (l_tcp->ppt_buffer == 00) {
5042 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
5045 l_tcp->ppt_data = l_tcp->ppt_buffer;
5047 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
5050 /* Read packet header from buffer */
5051 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
5053 l_tcp->ppt_data_size += p_header_size;
5059 * Writes the TLM marker (Tile Length Marker)
5061 * @param p_stream the stream to write data to.
5062 * @param p_j2k J2K codec.
5063 * @param p_manager the user event manager.
5065 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
5066 struct opj_stream_private *p_stream,
5067 struct opj_event_mgr * p_manager )
5069 OPJ_BYTE * l_current_data = 00;
5070 OPJ_UINT32 l_tlm_size;
5073 assert(p_j2k != 00);
5074 assert(p_manager != 00);
5075 assert(p_stream != 00);
5077 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5079 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5080 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5081 = (OPJ_BYTE*)opj_realloc(
5082 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5085 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5089 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
5092 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5094 /* change the way data is written to avoid seeking if possible */
5096 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
5098 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
5099 l_current_data += 2;
5101 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
5102 l_current_data += 2;
5104 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
5107 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5110 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
5111 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_tlm_size,p_manager) != l_tlm_size) {
5118 static void j2k_write_tlm(opj_j2k_t *j2k){
5120 opj_cio_t *cio = j2k->cio;
5121 j2k->tlm_start = cio_tell(cio);
5122 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
5123 lenp = 4 + (5*j2k->totnum_tp);
5124 cio_write(cio,lenp,2); /* Ltlm */
5125 cio_write(cio, 0,1); /* Ztlm=0*/
5126 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
5127 cio_skip(cio,5*j2k->totnum_tp);
5130 static void j2k_write_sot(opj_j2k_t *j2k) {
5133 opj_cio_t *cio = j2k->cio;
5135 j2k->sot_start = cio_tell(cio);
5136 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
5137 lenp = cio_tell(cio);
5138 cio_skip(cio, 2); /* Lsot (further) */
5139 cio_write(cio, j2k->curtileno, 2); /* Isot */
5140 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
5141 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
5142 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
5143 len = cio_tell(cio) - lenp;
5144 cio_seek(cio, lenp);
5145 cio_write(cio, len, 2); /* Lsot */
5146 cio_seek(cio, lenp + len);
5150 /* update markers struct */
5151 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
5152 #endif /* USE_JPWL */
5155 if( j2k->cstr_info && j2k->cur_tp_num==0){
5156 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
5161 * Writes the SOT marker (Start of tile-part)
5163 * @param p_stream the stream to write data to.
5164 * @param p_j2k J2K codec.
5165 * @param p_manager the user event manager.
5167 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
5169 OPJ_UINT32 * p_data_written,
5170 const struct opj_stream_private *p_stream,
5171 struct opj_event_mgr * p_manager )
5174 assert(p_j2k != 00);
5175 assert(p_manager != 00);
5176 assert(p_stream != 00);
5178 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
5181 opj_write_bytes(p_data,10,2); /* Lsot */
5184 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
5190 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
5193 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
5198 /* update markers struct */
5200 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
5202 assert( 0 && "TODO" );
5203 #endif /* USE_JPWL */
5205 * p_data_written = 12;
5211 static void j2k_read_sot(opj_j2k_t *j2k) {
5212 int len, tileno, totlen, partno, numparts;
5214 opj_tcp_t *tcp = NULL;
5217 opj_cp_t *cp = j2k->cp;
5218 opj_cio_t *cio = j2k->cio;
5220 len = cio_read(cio, 2);
5221 tileno = cio_read(cio, 2);
5224 if (j2k->cp->correct) {
5226 static int backup_tileno = 0;
5228 /* tileno is negative or larger than the number of tiles!!! */
5229 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
5230 opj_event_msg(j2k->cinfo, EVT_ERROR,
5231 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5232 tileno, (cp->tw * cp->th));
5234 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5237 /* we try to correct */
5238 tileno = backup_tileno;
5239 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
5240 "- setting tile number to %d\n",
5244 /* keep your private count of tiles */
5248 #endif /* USE_JPWL */
5250 /* tileno is negative or larger than the number of tiles!!! */
5251 if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
5252 opj_event_msg(j2k->cinfo, EVT_ERROR,
5253 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5254 tileno, (cp->tw * cp->th));
5259 if (cp->tileno_size == 0) {
5260 cp->tileno[cp->tileno_size] = tileno;
5264 assert(cp->tileno_size >= 0);
5265 while (i < (OPJ_UINT32)cp->tileno_size && status == 0) {
5266 status = cp->tileno[i] == tileno ? 1 : 0;
5270 cp->tileno[cp->tileno_size] = tileno;
5275 totlen = cio_read(cio, 4);
5278 if (j2k->cp->correct) {
5280 /* totlen is negative or larger than the bytes left!!! */
5281 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
5282 opj_event_msg(j2k->cinfo, EVT_ERROR,
5283 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5284 totlen, cio_numbytesleft(cio) + 8);
5286 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
5289 /* we try to correct */
5291 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
5292 "- setting Psot to %d => assuming it is the last tile\n",
5298 #endif /* USE_JPWL */
5300 /* totlen is negative or larger than the bytes left!!! */
5301 if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
5302 opj_event_msg(j2k->cinfo, EVT_ERROR,
5303 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5304 totlen, cio_numbytesleft(cio) + 8);
5310 totlen = cio_numbytesleft(cio) + 8;
5312 partno = cio_read(cio, 1);
5313 numparts = cio_read(cio, 1);
5315 if (partno >= numparts) {
5316 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);
5317 numparts = partno+1;
5320 j2k->curtileno = tileno;
5321 j2k->cur_tp_num = partno;
5322 j2k->eot = cio_getbp(cio) - 12 + totlen;
5323 j2k->state = J2K_STATE_TPH;
5324 tcp = &cp->tcps[j2k->curtileno];
5327 if (j2k->cstr_info) {
5330 j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
5331 j2k->cstr_info->tile[tileno].tileno = tileno;
5332 j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
5333 j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5335 j2k->cstr_info->tile[tileno].end_pos += totlen;
5337 j2k->cstr_info->tile[tileno].num_tps = numparts;
5339 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
5341 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)*/
5342 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
5343 j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5344 j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5347 if (tcp->first == 1) {
5348 /* Initialization PPT */
5349 opj_tccp_t *tmp = tcp->tccps;
5350 memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
5352 tcp->ppt_data = NULL;
5353 tcp->ppt_data_first = NULL;
5356 for (i = 0; i < j2k->image->numcomps; i++) {
5357 tcp->tccps[i] = j2k->default_tcp->tccps[i];
5359 cp->tcps[j2k->curtileno].first = 0;
5364 * Reads a PPT marker (Packed packet headers, tile-part header)
5366 * @param p_header_data the data contained in the PPT box.
5367 * @param p_j2k the jpeg2000 codec.
5368 * @param p_header_size the size of the data contained in the PPT marker.
5369 * @param p_manager the user event manager.
5371 opj_bool j2k_read_sot_v2 (
5372 opj_j2k_v2_t *p_j2k,
5373 OPJ_BYTE * p_header_data,
5374 OPJ_UINT32 p_header_size,
5375 struct opj_event_mgr * p_manager
5379 opj_cp_v2_t *l_cp = 00;
5380 opj_tcp_v2_t *l_tcp = 00;
5381 OPJ_UINT32 l_tot_len, l_num_parts = 0;
5382 OPJ_UINT32 l_current_part;
5383 OPJ_UINT32 l_tile_x,l_tile_y;
5386 assert(p_header_data != 00);
5387 assert(p_j2k != 00);
5388 assert(p_manager != 00);
5390 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
5391 if (p_header_size != 8) {
5392 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
5396 l_cp = &(p_j2k->m_cp);
5397 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
5400 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5401 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
5402 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
5405 if (l_cp->correct) {
5407 int tileno = p_j2k->m_current_tile_number;
5408 static int backup_tileno = 0;
5410 /* tileno is negative or larger than the number of tiles!!! */
5411 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
5412 opj_event_msg_v2(p_manager, EVT_ERROR,
5413 "JPWL: bad tile number (%d out of a maximum of %d)\n",
5414 tileno, (l_cp->tw * l_cp->th));
5416 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5419 /* we try to correct */
5420 tileno = backup_tileno;
5421 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5422 "- setting tile number to %d\n",
5426 /* keep your private count of tiles */
5429 #endif /* USE_JPWL */
5431 /* look for the tile in the list of already processed tile (in parts). */
5432 /* Optimization possible here with a more complex data structure and with the removing of tiles */
5433 /* since the time taken by this function can only grow at the time */
5435 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
5438 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
5439 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
5441 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
5447 if (l_cp->correct) {
5449 /* totlen is negative or larger than the bytes left!!! */
5450 if (/*(l_tot_len < 0) ||*/ (l_tot_len > p_header_size ) ) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */
5451 opj_event_msg_v2(p_manager, EVT_ERROR,
5452 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
5453 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
5455 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5458 /* we try to correct */
5460 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
5461 "- setting Psot to %d => assuming it is the last tile\n",
5465 #endif /* USE_JPWL */
5467 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
5469 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
5470 "we assuming it is the last tile-part of the codestream.\n");
5471 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5474 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
5477 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
5480 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
5481 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
5482 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
5483 if (l_tcp->m_nb_tile_parts) {
5484 if (l_current_part >= l_tcp->m_nb_tile_parts){
5485 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
5486 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
5487 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
5491 l_tcp->m_nb_tile_parts = l_num_parts;
5494 /* If know the number of tile part header we will check if we didn't read the last*/
5495 if (l_tcp->m_nb_tile_parts) {
5496 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
5497 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
5501 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
5502 /* Keep the size of data to skip after this marker */
5503 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
5506 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
5507 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
5510 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
5512 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
5513 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
5514 p_j2k->m_specific_param.m_decoder.m_skip_data =
5515 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
5516 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
5517 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
5518 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
5521 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
5522 p_j2k->m_specific_param.m_decoder.m_skip_data =
5523 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
5527 if (p_j2k->cstr_index)
5529 assert(p_j2k->cstr_index->tile_index != 00);
5530 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5531 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
5533 if (l_num_parts != 0){
5534 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
5535 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
5538 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
5539 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5540 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5542 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5543 (opj_tp_index_t*)opj_realloc(
5544 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5545 l_num_parts* sizeof(opj_tp_index_t));
5548 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5550 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5551 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5552 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5553 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5554 sizeof(opj_tp_index_t));
5557 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5558 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5559 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5560 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5561 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5562 * sizeof(opj_tp_index_t));
5571 /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */
5572 /* if (p_j2k->cstr_info) {
5575 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5578 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5579 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5580 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5581 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5584 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5587 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5591 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5594 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5595 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5596 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5601 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5604 opj_tcp_t *tcp = NULL;
5605 opj_codestream_info_t *cstr_info = NULL;
5607 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5608 opj_cp_t *cp = j2k->cp;
5609 opj_cio_t *cio = j2k->cio;
5611 tcd->tp_num = j2k->tp_num ;
5612 tcd->cur_tp_num = j2k->cur_tp_num;
5614 cio_write(cio, J2K_MS_SOD, 2);
5616 if( j2k->cstr_info && j2k->cur_tp_num==0){
5617 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5620 if (j2k->curtileno == 0) {
5621 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5625 cstr_info = j2k->cstr_info;
5627 if (!j2k->cur_tp_num ) {
5628 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5629 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5632 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5633 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5637 /* update markers struct */
5638 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5639 #endif /* USE_JPWL */
5644 tcp = &cp->tcps[j2k->curtileno];
5645 for (layno = 0; layno < tcp->numlayers; layno++) {
5646 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5647 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5648 } else if (tcp->rates[layno]) {
5649 tcp->rates[layno]=1;
5652 if(j2k->cur_tp_num == 0){
5653 tcd->tcd_image->tiles->packno = 0;
5655 cstr_info->packno = 0;
5658 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5660 /* Writing Psot in SOT marker */
5661 totlen = cio_tell(cio) + l - j2k->sot_start;
5662 cio_seek(cio, j2k->sot_start + 6);
5663 cio_write(cio, totlen, 4);
5664 cio_seek(cio, j2k->sot_start + totlen);
5665 /* Writing Ttlm and Ptlm in TLM marker */
5667 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5668 cio_write(cio, j2k->curtileno, 1);
5669 cio_write(cio, totlen, 4);
5671 cio_seek(cio, j2k->sot_start + totlen);
5675 * Writes the SOD marker (Start of data)
5677 * @param p_stream the stream to write data to.
5678 * @param p_j2k J2K codec.
5679 * @param p_manager the user event manager.
5681 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5682 struct opj_tcd_v2 * p_tile_coder,
5684 OPJ_UINT32 * p_data_written,
5685 OPJ_UINT32 p_total_data_size,
5686 const struct opj_stream_private *p_stream,
5687 struct opj_event_mgr * p_manager )
5689 opj_tcp_v2_t *l_tcp = 00;
5690 opj_codestream_info_t *l_cstr_info = 00;
5691 opj_cp_v2_t *l_cp = 00;
5693 OPJ_UINT32 l_size_tile;
5694 OPJ_UINT32 l_remaining_data;
5697 assert(p_j2k != 00);
5698 assert(p_manager != 00);
5699 assert(p_stream != 00);
5701 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5704 /* make room for the EOF marker */
5705 l_remaining_data = p_total_data_size - 4;
5707 l_cp = &(p_j2k->m_cp);
5708 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5711 /* update tile coder */
5712 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5713 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5715 l_size_tile = l_cp->th * l_cp->tw;
5718 /* TODO mergeV2: check this part which use cstr_info */
5719 /*l_cstr_info = p_j2k->cstr_info;
5721 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5722 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5723 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5729 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5731 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5736 /* update markers struct */
5737 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5739 assert( 0 && "TODO" );
5740 #endif /* USE_JPWL */
5745 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5746 p_tile_coder->tcd_image->tiles->packno = 0;
5748 l_cstr_info->packno = 0;
5752 *p_data_written = 0;
5754 if (! tcd_encode_tile_v2(p_tile_coder, p_j2k->m_current_tile_number, p_data, p_data_written, l_remaining_data , l_cstr_info)) {
5755 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5759 *p_data_written += 2;
5765 static void j2k_read_sod(opj_j2k_t *j2k) {
5766 int len, truncate = 0, i;
5767 unsigned char *data = NULL, *data_ptr = NULL;
5769 opj_cio_t *cio = j2k->cio;
5770 int curtileno = j2k->curtileno;
5773 if (j2k->cstr_info) {
5774 j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
5775 cio_tell(cio) + j2k->pos_correction - 1;
5776 if (j2k->cur_tp_num == 0)
5777 j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5778 j2k->cstr_info->packno = 0;
5781 len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
5783 if (len == cio_numbytesleft(cio) + 1) {
5784 truncate = 1; /* Case of a truncate codestream */
5787 data = j2k->tile_data[curtileno];
5788 data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
5790 data_ptr = data + j2k->tile_len[curtileno];
5791 for (i = 0; i < len; i++) {
5792 data_ptr[i] = cio_read(cio, 1);
5795 j2k->tile_len[curtileno] += len;
5796 j2k->tile_data[curtileno] = data;
5799 j2k->state = J2K_STATE_TPHSOT;
5801 j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
5807 * Reads a SOD marker (Start Of Data)
5809 * @param p_header_data the data contained in the SOD box.
5810 * @param p_j2k the jpeg2000 codec.
5811 * @param p_header_size the size of the data contained in the SOD marker.
5812 * @param p_manager the user event manager.
5814 opj_bool j2k_read_sod_v2 (
5815 opj_j2k_v2_t *p_j2k,
5816 struct opj_stream_private *p_stream,
5817 struct opj_event_mgr * p_manager
5820 OPJ_UINT32 l_current_read_size;
5821 opj_codestream_index_t * l_cstr_index = 00;
5822 OPJ_BYTE ** l_current_data = 00;
5823 opj_tcp_v2_t * l_tcp = 00;
5824 OPJ_UINT32 * l_tile_len = 00;
5827 assert(p_j2k != 00);
5828 assert(p_manager != 00);
5829 assert(p_stream != 00);
5831 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5833 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5834 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5835 // but we are in the last tile part,
5836 // so its result will fit on OPJ_UINT32 unless we find
5837 // a file with a single tile part of more than 4 GB...*/
5838 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5841 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5843 l_current_data = &(l_tcp->m_data);
5844 l_tile_len = &l_tcp->m_data_size;
5846 if (! *l_current_data) {
5847 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5850 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5853 if (*l_current_data == 00) {
5854 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5860 l_cstr_index = p_j2k->cstr_index;
5862 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5864 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5865 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5867 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5868 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5870 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5874 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5876 /*l_cstr_index->packno = 0;*/
5879 l_current_read_size = opj_stream_read_data( p_stream,
5880 *l_current_data + *l_tile_len,
5881 p_j2k->m_specific_param.m_decoder.m_sot_length,
5884 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5885 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5888 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5891 *l_tile_len += l_current_read_size;
5897 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5898 opj_cp_t *cp = j2k->cp;
5899 opj_tcp_t *tcp = &cp->tcps[tileno];
5900 opj_cio_t *cio = j2k->cio;
5901 int numcomps = j2k->image->numcomps;
5903 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5904 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5905 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5906 cio_write(cio, 0, 1); /* Srgn */
5907 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5911 * Writes the RGN marker (Region Of Interest)
5913 * @param p_tile_no the tile to output
5914 * @param p_comp_no the component to output
5915 * @param p_stream the stream to write data to.
5916 * @param p_j2k J2K codec.
5917 * @param p_manager the user event manager.
5919 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5920 OPJ_UINT32 p_tile_no,
5921 OPJ_UINT32 p_comp_no,
5922 struct opj_stream_private *p_stream,
5923 struct opj_event_mgr * p_manager )
5925 OPJ_BYTE * l_current_data = 00;
5926 OPJ_UINT32 l_nb_comp;
5927 OPJ_UINT32 l_rgn_size;
5928 opj_image_t *l_image = 00;
5929 opj_cp_v2_t *l_cp = 00;
5930 opj_tcp_v2_t *l_tcp = 00;
5931 opj_tccp_t *l_tccp = 00;
5932 OPJ_UINT32 l_comp_room;
5935 assert(p_j2k != 00);
5936 assert(p_manager != 00);
5937 assert(p_stream != 00);
5939 l_cp = &(p_j2k->m_cp);
5940 l_tcp = &l_cp->tcps[p_tile_no];
5941 l_tccp = &l_tcp->tccps[p_comp_no];
5943 l_nb_comp = l_image->numcomps;
5945 if (l_nb_comp <= 256) {
5952 l_rgn_size = 6 + l_comp_room;
5954 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5956 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5957 l_current_data += 2;
5959 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5960 l_current_data += 2;
5962 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5963 l_current_data+=l_comp_room;
5965 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5968 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5971 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) {
5978 static void j2k_read_rgn(opj_j2k_t *j2k) {
5979 int len, compno, roisty;
5981 opj_cp_t *cp = j2k->cp;
5982 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
5983 opj_cio_t *cio = j2k->cio;
5984 int numcomps = j2k->image->numcomps;
5986 len = cio_read(cio, 2); /* Lrgn */
5987 compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
5988 roisty = cio_read(cio, 1); /* Srgn */
5991 if (j2k->cp->correct) {
5992 /* totlen is negative or larger than the bytes left!!! */
5993 if (compno >= numcomps) {
5994 opj_event_msg(j2k->cinfo, EVT_ERROR,
5995 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5997 if (!JPWL_ASSUME || JPWL_ASSUME) {
5998 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
6003 #endif /* USE_JPWL */
6005 tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
6008 static void j2k_write_eoc(opj_j2k_t *j2k) {
6009 opj_cio_t *cio = j2k->cio;
6010 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
6011 cio_write(cio, J2K_MS_EOC, 2);
6015 /* update markers struct */
6016 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
6017 #endif /* USE_JPWL */
6022 * Writes the EOC marker (End of Codestream)
6024 * @param p_stream the stream to write data to.
6025 * @param p_j2k J2K codec.
6026 * @param p_manager the user event manager.
6028 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
6029 struct opj_stream_private *p_stream,
6030 struct opj_event_mgr * p_manager )
6033 assert(p_j2k != 00);
6034 assert(p_manager != 00);
6035 assert(p_stream != 00);
6037 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
6042 /* update markers struct */
6044 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
6046 #endif /* USE_JPWL */
6048 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
6052 if ( ! opj_stream_flush(p_stream,p_manager) ) {
6061 * Reads a RGN marker (Region Of Interest)
6063 * @param p_header_data the data contained in the POC box.
6064 * @param p_j2k the jpeg2000 codec.
6065 * @param p_header_size the size of the data contained in the POC marker.
6066 * @param p_manager the user event manager.
6068 opj_bool j2k_read_rgn_v2 (
6069 opj_j2k_v2_t *p_j2k,
6070 OPJ_BYTE * p_header_data,
6071 OPJ_UINT32 p_header_size,
6072 struct opj_event_mgr * p_manager
6075 OPJ_UINT32 l_nb_comp;
6076 opj_image_t * l_image = 00;
6078 opj_cp_v2_t *l_cp = 00;
6079 opj_tcp_v2_t *l_tcp = 00;
6080 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
6083 assert(p_header_data != 00);
6084 assert(p_j2k != 00);
6085 assert(p_manager != 00);
6087 l_image = p_j2k->m_private_image;
6088 l_nb_comp = l_image->numcomps;
6090 if (l_nb_comp <= 256) {
6095 if (p_header_size != 2 + l_comp_room) {
6096 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
6100 l_cp = &(p_j2k->m_cp);
6101 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
6102 &l_cp->tcps[p_j2k->m_current_tile_number] :
6103 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6105 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
6106 p_header_data+=l_comp_room;
6107 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
6111 if (l_cp->correct) {
6112 /* totlen is negative or larger than the bytes left!!! */
6113 if (l_comp_room >= l_nb_comp) {
6114 opj_event_msg_v2(p_manager, EVT_ERROR,
6115 "JPWL: bad component number in RGN (%d when there are only %d)\n",
6116 l_comp_room, l_nb_comp);
6117 if (!JPWL_ASSUME || JPWL_ASSUME) {
6118 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
6123 #endif /* USE_JPWL */
6125 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
6132 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
6134 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
6137 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
6144 * Updates the rates of the tcp.
6146 * @param p_stream the stream to write data to.
6147 * @param p_j2k J2K codec.
6148 * @param p_manager the user event manager.
6150 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
6151 struct opj_stream_private *p_stream,
6152 struct opj_event_mgr * p_manager )
6154 opj_cp_v2_t * l_cp = 00;
6155 opj_image_t * l_image = 00;
6156 opj_tcp_v2_t * l_tcp = 00;
6157 opj_image_comp_t * l_img_comp = 00;
6160 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
6161 OPJ_FLOAT32 * l_rates = 0;
6162 OPJ_FLOAT32 l_sot_remove;
6163 OPJ_UINT32 l_bits_empty, l_size_pixel;
6164 OPJ_UINT32 l_tile_size = 0;
6165 OPJ_UINT32 l_last_res;
6166 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
6169 assert(p_j2k != 00);
6170 assert(p_manager != 00);
6171 assert(p_stream != 00);
6174 l_cp = &(p_j2k->m_cp);
6175 l_image = p_j2k->m_private_image;
6178 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
6179 l_size_pixel = l_image->numcomps * l_image->comps->prec;
6180 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
6182 if (l_cp->m_specific_param.m_enc.m_tp_on) {
6183 l_tp_stride_func = get_tp_stride;
6186 l_tp_stride_func = get_default_stride;
6189 for (i=0;i<l_cp->th;++i) {
6190 for (j=0;j<l_cp->tw;++j) {
6191 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
6193 /* 4 borders of the tile rescale on the image if necessary */
6194 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
6195 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
6196 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
6197 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
6199 l_rates = l_tcp->rates;
6201 /* Modification of the RATE >> */
6203 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6205 ((*l_rates) * l_bits_empty)
6213 for (k = 1; k < l_tcp->numlayers; ++k) {
6215 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
6217 ((*l_rates) * l_bits_empty)
6233 for (i=0;i<l_cp->th;++i) {
6234 for (j=0;j<l_cp->tw;++j) {
6235 l_rates = l_tcp->rates;
6238 *l_rates -= l_sot_remove;
6240 if (*l_rates < 30) {
6247 l_last_res = l_tcp->numlayers - 1;
6249 for (k = 1; k < l_last_res; ++k) {
6252 *l_rates -= l_sot_remove;
6254 if (*l_rates < *(l_rates - 1) + 10) {
6255 *l_rates = (*(l_rates - 1)) + 20;
6263 *l_rates -= (l_sot_remove + 2.f);
6265 if (*l_rates < *(l_rates - 1) + 10) {
6266 *l_rates = (*(l_rates - 1)) + 20;
6274 l_img_comp = l_image->comps;
6277 for (i=0;i<l_image->numcomps;++i) {
6278 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
6280 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
6288 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
6290 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
6292 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
6293 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
6294 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
6295 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
6299 if (l_cp->m_specific_param.m_enc.m_cinema) {
6300 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
6301 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
6302 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
6306 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
6307 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
6313 static void j2k_read_eoc(opj_j2k_t *j2k) {
6315 opj_bool success = OPJ_FALSE;
6317 /* if packets should be decoded */
6318 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
6319 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
6320 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
6321 for (i = 0; i < j2k->cp->tileno_size; i++) {
6322 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
6323 if (j2k->cp->tileno[i] != -1)
6325 tileno = j2k->cp->tileno[i];
6326 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
6327 opj_free(j2k->tile_data[tileno]);
6328 j2k->tile_data[tileno] = NULL;
6329 tcd_free_decode_tile(tcd, i);
6332 success = OPJ_FALSE;
6333 if (success == OPJ_FALSE) {
6334 j2k->state |= J2K_STATE_ERR;
6338 tcd_free_decode(tcd);
6341 /* if packets should not be decoded */
6343 for (i = 0; i < j2k->cp->tileno_size; i++) {
6344 tileno = j2k->cp->tileno[i];
6345 opj_free(j2k->tile_data[tileno]);
6346 j2k->tile_data[tileno] = NULL;
6349 if (j2k->state & J2K_STATE_ERR)
6350 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
6352 j2k->state = J2K_STATE_MT;
6356 * Reads a EOC marker (End Of Codestream)
6358 * @param p_header_data the data contained in the SOD box.
6359 * @param p_j2k the jpeg2000 codec.
6360 * @param p_header_size the size of the data contained in the SOD marker.
6361 * @param p_manager the user event manager.
6364 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
6365 struct opj_stream_private *p_stream,
6366 struct opj_event_mgr * p_manager )
6369 opj_tcd_v2_t * l_tcd = 00;
6370 OPJ_UINT32 l_nb_tiles;
6371 opj_tcp_v2_t * l_tcp = 00;
6375 assert(p_j2k != 00);
6376 assert(p_manager != 00);
6377 assert(p_stream != 00);
6379 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
6380 l_tcp = p_j2k->m_cp.tcps;
6382 l_tcd = tcd_create_v2(OPJ_TRUE);
6384 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6388 for (i = 0; i < l_nb_tiles; ++i) {
6389 if (l_tcp->m_data) {
6390 if (! tcd_init_decode_tile(l_tcd, i)) {
6391 tcd_destroy_v2(l_tcd);
6392 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
6396 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
6400 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
6405 j2k_tcp_destroy(l_tcp);
6409 tcd_destroy_v2(l_tcd);
6415 * Gets the offset of the header.
6417 * @param p_stream the stream to write data to.
6418 * @param p_j2k J2K codec.
6419 * @param p_manager the user event manager.
6421 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
6422 struct opj_stream_private *p_stream,
6423 struct opj_event_mgr * p_manager )
6426 assert(p_j2k != 00);
6427 assert(p_manager != 00);
6428 assert(p_stream != 00);
6430 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
6436 * Writes the MCT marker (Multiple Component Transform)
6438 * @param p_stream the stream to write data to.
6439 * @param p_j2k J2K codec.
6440 * @param p_manager the user event manager.
6442 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
6443 struct opj_stream_private *p_stream,
6444 struct opj_event_mgr * p_manager )
6447 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6448 opj_mct_data_t * l_mct_record;
6449 opj_tcp_v2_t * l_tcp;
6452 assert(p_j2k != 00);
6453 assert(p_stream != 00);
6454 assert(p_manager != 00);
6456 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
6460 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6461 l_mct_record = l_tcp->m_mct_records;
6463 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6465 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
6472 l_mcc_record = l_tcp->m_mcc_records;
6474 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6476 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
6483 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
6491 * Writes the image components.
6493 * @param p_stream the stream to write data to.
6494 * @param p_j2k J2K codec.
6495 * @param p_manager the user event manager.
6497 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
6498 struct opj_stream_private *p_stream,
6499 struct opj_event_mgr * p_manager )
6504 assert(p_j2k != 00);
6505 assert(p_manager != 00);
6506 assert(p_stream != 00);
6508 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
6510 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
6514 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
6523 * Writes regions of interests.
6525 * @param p_stream the stream to write data to.
6526 * @param p_j2k J2K codec.
6527 * @param p_manager the user event manager.
6529 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
6530 struct opj_stream_private *p_stream,
6531 struct opj_event_mgr * p_manager )
6534 const opj_tccp_t *l_tccp = 00;
6537 assert(p_j2k != 00);
6538 assert(p_manager != 00);
6539 assert(p_stream != 00);
6541 l_tccp = p_j2k->m_cp.tcps->tccps;
6543 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
6544 if (l_tccp->roishift) {
6546 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
6560 * @param p_stream the stream to write data to.
6561 * @param p_j2k J2K codec.
6562 * @param p_manager the user event manager.
6564 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
6565 struct opj_stream_private *p_stream,
6566 struct opj_event_mgr * p_manager )
6568 opj_codestream_index_t * l_cstr_index = 00;
6571 assert(p_j2k != 00);
6572 assert(p_manager != 00);
6573 assert(p_stream != 00);
6575 l_cstr_index = p_j2k->cstr_index;
6577 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
6579 /* The following adjustment is done to adjust the codestream size */
6580 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
6581 /* the first bunch of bytes is not in the codestream */
6582 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
6587 /* preparation of JPWL marker segments */
6591 /* encode according to JPWL */
6592 jpwl_encode(p_j2k, p_stream, image);
6596 assert( 0 && "TODO" );
6597 #endif /* USE_JPWL */
6602 typedef struct opj_dec_mstabent {
6605 /** value of the state when the marker can appear */
6607 /** action linked to the marker */
6608 void (*handler) (opj_j2k_t *j2k);
6609 } opj_dec_mstabent_t;
6611 opj_dec_mstabent_t j2k_dec_mstab[] = {
6612 {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
6613 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
6614 {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
6615 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6616 {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
6617 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
6618 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
6619 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
6620 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
6621 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
6622 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6623 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6624 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6625 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6626 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6627 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6629 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6630 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6633 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6634 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6635 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6636 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6637 #endif /* USE_JPWL */
6639 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6640 {J2K_MS_INSEC, 0, j2k_read_insec},
6641 #endif /* USE_JPSEC */
6643 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6646 static void j2k_read_unk(opj_j2k_t *j2k) {
6647 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6650 if (j2k->cp->correct) {
6652 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6653 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6654 id = cio_read(j2k->cio, 2);
6655 opj_event_msg(j2k->cinfo, EVT_ERROR,
6656 "JPWL: really don't know this marker %x\n",
6659 opj_event_msg(j2k->cinfo, EVT_ERROR,
6660 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6663 /* OK, activate this at your own risk!!! */
6664 /* we look for the marker at the minimum hamming distance from this */
6665 while (j2k_dec_mstab[m].id) {
6667 /* 1's where they differ */
6668 tmp_id = j2k_dec_mstab[m].id ^ id;
6670 /* compute the hamming distance between our id and the current */
6672 for (i = 0; i < 16; i++) {
6673 if ((tmp_id >> i) & 0x0001) {
6678 /* if current distance is smaller, set the minimum */
6679 if (cur_dist < min_dist) {
6680 min_dist = cur_dist;
6681 min_id = j2k_dec_mstab[m].id;
6684 /* jump to the next marker */
6688 /* do we substitute the marker? */
6689 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6690 opj_event_msg(j2k->cinfo, EVT_ERROR,
6691 "- marker %x is at distance %d from the read %x\n",
6692 min_id, min_dist, id);
6693 opj_event_msg(j2k->cinfo, EVT_ERROR,
6694 "- trying to substitute in place and crossing fingers!\n");
6695 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6696 cio_write(j2k->cio, min_id, 2);
6699 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6704 #endif /* USE_JPWL */
6709 * Reads an unknown marker
6711 * @param p_stream the stream object to read from.
6712 * @param p_j2k the jpeg2000 codec.
6713 * @param p_manager the user event manager.
6715 * @return true if the marker could be deduced.
6717 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6718 struct opj_stream_private *p_stream,
6719 OPJ_UINT32 *output_marker,
6720 struct opj_event_mgr * p_manager
6723 OPJ_UINT32 l_unknown_marker;
6724 const opj_dec_memory_marker_handler_t * l_marker_handler;
6725 OPJ_UINT32 l_size_unk = 2;
6728 assert(p_j2k != 00);
6729 assert(p_manager != 00);
6730 assert(p_stream != 00);
6732 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6735 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6736 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6737 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6741 /* read 2 bytes as the new marker ID*/
6742 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6744 if (!(l_unknown_marker < 0xff00)) {
6746 /* Get the marker handler from the marker ID*/
6747 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6749 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6750 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6754 if (l_marker_handler->id != J2K_MS_UNK) {
6755 /* Add the marker to the codestream index*/
6756 if (l_marker_handler->id != J2K_MS_SOT)
6757 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6758 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6760 break; /* next marker is known and well located */
6768 *output_marker = l_marker_handler->id ;
6774 Read the lookup table containing all the marker, status and action
6775 @param id Marker value
6777 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6778 opj_dec_mstabent_t *e;
6779 for (e = j2k_dec_mstab; e->id != 0; e++) {
6788 * Writes the MCT marker (Multiple Component Transform)
6790 * @param p_stream the stream to write data to.
6791 * @param p_j2k J2K codec.
6792 * @param p_manager the user event manager.
6794 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6795 opj_mct_data_t * p_mct_record,
6796 struct opj_stream_private *p_stream,
6797 struct opj_event_mgr * p_manager )
6799 OPJ_UINT32 l_mct_size;
6800 OPJ_BYTE * l_current_data = 00;
6804 assert(p_j2k != 00);
6805 assert(p_manager != 00);
6806 assert(p_stream != 00);
6808 l_mct_size = 10 + p_mct_record->m_data_size;
6810 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6811 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6812 = (OPJ_BYTE*)opj_realloc(
6813 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6816 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6820 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6823 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6825 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6826 l_current_data += 2;
6828 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6829 l_current_data += 2;
6831 opj_write_bytes(l_current_data,0,2); /* Zmct */
6832 l_current_data += 2;
6834 /* only one marker atm */
6835 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6837 opj_write_bytes(l_current_data,l_tmp,2);
6838 l_current_data += 2;
6840 opj_write_bytes(l_current_data,0,2); /* Ymct */
6843 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6845 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mct_size,p_manager) != l_mct_size) {
6853 * Reads a MCT marker (Multiple Component Transform)
6855 * @param p_header_data the data contained in the MCT box.
6856 * @param p_j2k the jpeg2000 codec.
6857 * @param p_header_size the size of the data contained in the MCT marker.
6858 * @param p_manager the user event manager.
6860 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6861 OPJ_BYTE * p_header_data,
6862 OPJ_UINT32 p_header_size,
6863 struct opj_event_mgr * p_manager )
6866 opj_tcp_v2_t *l_tcp = 00;
6869 opj_mct_data_t * l_mct_data;
6872 assert(p_header_data != 00);
6873 assert(p_j2k != 00);
6875 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6876 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6877 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6879 if (p_header_size < 2) {
6880 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6885 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6888 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6892 if(p_header_size <= 6) {
6893 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6897 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6898 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6901 l_indix = l_tmp & 0xff;
6902 l_mct_data = l_tcp->m_mct_records;
6904 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6905 if (l_mct_data->m_index == l_indix) {
6912 if (i == l_tcp->m_nb_mct_records) {
6913 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6914 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6916 l_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(l_tcp->m_mct_records,l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
6917 if(! l_tcp->m_mct_records) {
6918 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6922 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6923 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6926 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6929 if (l_mct_data->m_data) {
6930 opj_free(l_mct_data->m_data);
6931 l_mct_data->m_data = 00;
6934 l_mct_data->m_index = l_indix;
6935 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6936 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6938 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6941 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6947 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6948 if (! l_mct_data->m_data) {
6949 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6952 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6954 l_mct_data->m_data_size = p_header_size;
6955 ++l_tcp->m_nb_mct_records;
6961 * Writes the MCC marker (Multiple Component Collection)
6963 * @param p_stream the stream to write data to.
6964 * @param p_j2k J2K codec.
6965 * @param p_manager the user event manager.
6967 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6968 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6969 struct opj_stream_private *p_stream,
6970 struct opj_event_mgr * p_manager )
6973 OPJ_UINT32 l_mcc_size;
6974 OPJ_BYTE * l_current_data = 00;
6975 OPJ_UINT32 l_nb_bytes_for_comp;
6980 assert(p_j2k != 00);
6981 assert(p_manager != 00);
6982 assert(p_stream != 00);
6984 if (p_mcc_record->m_nb_comps > 255 ) {
6985 l_nb_bytes_for_comp = 2;
6989 l_nb_bytes_for_comp = 1;
6993 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6994 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6996 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6997 = (OPJ_BYTE*)opj_realloc(
6998 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7000 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7004 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
7007 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7009 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
7010 l_current_data += 2;
7012 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
7013 l_current_data += 2;
7016 opj_write_bytes(l_current_data,0,2); /* Zmcc */
7017 l_current_data += 2;
7019 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
7022 /* only one marker atm */
7023 opj_write_bytes(l_current_data,0,2); /* Ymcc */
7026 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
7029 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
7032 opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps | l_mask,2); /* Nmcci number of input components involved and size for each component offset = 8 bits */
7035 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
7036 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
7037 l_current_data+=l_nb_bytes_for_comp;
7040 opj_write_bytes(l_current_data,p_mcc_record->m_nb_comps|l_mask,2); /* Mmcci number of output components involved and size for each component offset = 8 bits */
7043 for (i=0;i<p_mcc_record->m_nb_comps;++i)
7045 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
7046 l_current_data+=l_nb_bytes_for_comp;
7049 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
7051 if (p_mcc_record->m_decorrelation_array) {
7052 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
7055 if (p_mcc_record->m_offset_array) {
7056 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
7059 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
7062 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mcc_size,p_manager) != l_mcc_size) {
7070 * Reads a MCC marker (Multiple Component Collection)
7072 * @param p_header_data the data contained in the MCC box.
7073 * @param p_j2k the jpeg2000 codec.
7074 * @param p_header_size the size of the data contained in the MCC marker.
7075 * @param p_manager the user event manager.
7077 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
7078 OPJ_BYTE * p_header_data,
7079 OPJ_UINT32 p_header_size,
7080 struct opj_event_mgr * p_manager )
7085 opj_tcp_v2_t * l_tcp;
7086 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7087 opj_mct_data_t * l_mct_data;
7088 OPJ_UINT32 l_nb_collections;
7089 OPJ_UINT32 l_nb_comps;
7090 OPJ_UINT32 l_nb_bytes_by_comp;
7094 assert(p_header_data != 00);
7095 assert(p_j2k != 00);
7096 assert(p_manager != 00);
7098 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7099 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7100 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7102 if (p_header_size < 2) {
7103 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7108 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
7111 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
7115 if (p_header_size < 7) {
7116 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7120 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
7123 l_mcc_record = l_tcp->m_mcc_records;
7125 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
7126 if (l_mcc_record->m_index == l_indix) {
7133 if (i == l_tcp->m_nb_mcc_records) {
7134 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
7135 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
7137 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7138 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7139 if (! l_tcp->m_mcc_records) {
7140 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7143 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
7144 memset(l_mcc_record,0,(l_tcp->m_nb_max_mcc_records-l_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
7146 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
7148 l_mcc_record->m_index = l_indix;
7150 /* only one marker atm */
7151 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
7154 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
7158 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
7161 if (l_nb_collections > 1) {
7162 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
7168 for (i=0;i<l_nb_collections;++i) {
7169 if (p_header_size < 3) {
7170 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7174 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
7178 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
7182 opj_read_bytes(p_header_data,&l_nb_comps,2);
7187 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7188 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
7190 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
7191 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7195 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
7197 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7198 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
7199 p_header_data+=l_nb_bytes_by_comp;
7202 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7207 opj_read_bytes(p_header_data,&l_nb_comps,2);
7210 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
7211 l_nb_comps &= 0x7fff;
7213 if (l_nb_comps != l_mcc_record->m_nb_comps) {
7214 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
7218 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
7219 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7223 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
7225 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
7226 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
7227 p_header_data+=l_nb_bytes_by_comp;
7230 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
7235 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
7238 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
7239 l_mcc_record->m_decorrelation_array = 00;
7240 l_mcc_record->m_offset_array = 00;
7242 l_indix = l_tmp & 0xff;
7244 l_mct_data = l_tcp->m_mct_records;
7245 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7246 if (l_mct_data->m_index == l_indix) {
7247 l_mcc_record->m_decorrelation_array = l_mct_data;
7253 if (l_mcc_record->m_decorrelation_array == 00) {
7254 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7259 l_indix = (l_tmp >> 8) & 0xff;
7261 l_mct_data = l_tcp->m_mct_records;
7262 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
7263 if (l_mct_data->m_index == l_indix) {
7264 l_mcc_record->m_offset_array = l_mct_data;
7270 if (l_mcc_record->m_offset_array == 00) {
7271 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7277 if (p_header_size != 0) {
7278 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
7282 ++l_tcp->m_nb_mcc_records;
7289 * Writes the MCO marker (Multiple component transformation ordering)
7291 * @param p_stream the stream to write data to.
7292 * @param p_j2k J2K codec.
7293 * @param p_manager the user event manager.
7295 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
7296 struct opj_stream_private *p_stream,
7297 struct opj_event_mgr * p_manager
7300 OPJ_BYTE * l_current_data = 00;
7301 OPJ_UINT32 l_mco_size;
7302 opj_tcp_v2_t * l_tcp = 00;
7303 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7307 assert(p_j2k != 00);
7308 assert(p_manager != 00);
7309 assert(p_stream != 00);
7311 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
7312 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7314 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
7315 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7317 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7318 = (OPJ_BYTE*)opj_realloc(
7319 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7321 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
7326 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
7329 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
7330 l_current_data += 2;
7332 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
7333 l_current_data += 2;
7335 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
7338 l_mcc_record = l_tcp->m_mcc_records;
7339 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
7340 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
7346 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_mco_size,p_manager) != l_mco_size) {
7354 * Reads a MCO marker (Multiple Component Transform Ordering)
7356 * @param p_header_data the data contained in the MCO box.
7357 * @param p_j2k the jpeg2000 codec.
7358 * @param p_header_size the size of the data contained in the MCO marker.
7359 * @param p_manager the user event manager.
7361 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
7362 OPJ_BYTE * p_header_data,
7363 OPJ_UINT32 p_header_size,
7364 struct opj_event_mgr * p_manager )
7366 OPJ_UINT32 l_tmp, i;
7367 OPJ_UINT32 l_nb_stages;
7368 opj_tcp_v2_t * l_tcp;
7369 opj_tccp_t * l_tccp;
7370 opj_image_t * l_image;
7371 opj_image_comp_t * l_img_comp;
7374 assert(p_header_data != 00);
7375 assert(p_j2k != 00);
7376 assert(p_manager != 00);
7378 l_image = p_j2k->m_private_image;
7379 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
7380 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
7381 p_j2k->m_specific_param.m_decoder.m_default_tcp;
7383 if (p_header_size < 1) {
7384 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
7388 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
7391 if (l_nb_stages > 1) {
7392 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
7396 if (p_header_size != l_nb_stages + 1) {
7397 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
7401 l_tccp = l_tcp->tccps;
7402 l_img_comp = l_image->comps;
7404 for (i=0;i<l_image->numcomps;++i) {
7405 l_tccp->m_dc_level_shift = 0;
7409 if (l_tcp->m_mct_decoding_matrix) {
7410 opj_free(l_tcp->m_mct_decoding_matrix);
7411 l_tcp->m_mct_decoding_matrix = 00;
7414 for (i=0;i<l_nb_stages;++i) {
7415 opj_read_bytes(p_header_data,&l_tmp,1);
7418 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
7426 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
7429 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
7430 opj_mct_data_t * l_deco_array, * l_offset_array;
7431 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
7432 OPJ_UINT32 l_nb_elem;
7433 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
7434 opj_tccp_t * l_tccp;
7437 assert(p_tcp != 00);
7439 l_mcc_record = p_tcp->m_mcc_records;
7441 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
7442 if (l_mcc_record->m_index == p_index) {
7447 if (i==p_tcp->m_nb_mcc_records) {
7448 /** element discarded **/
7452 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
7453 /** do not support number of comps != image */
7457 l_deco_array = l_mcc_record->m_decorrelation_array;
7460 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
7461 if (l_deco_array->m_data_size != l_data_size) {
7465 l_nb_elem = p_image->numcomps * p_image->numcomps;
7466 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
7467 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7469 if (! p_tcp->m_mct_decoding_matrix ) {
7473 j2k_mct_read_functions_to_float[l_deco_array->m_element_type](l_deco_array->m_data,p_tcp->m_mct_decoding_matrix,l_nb_elem);
7476 l_offset_array = l_mcc_record->m_offset_array;
7478 if (l_offset_array) {
7479 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
7480 if (l_offset_array->m_data_size != l_data_size) {
7484 l_nb_elem = p_image->numcomps;
7485 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
7486 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
7488 if (! l_offset_data ) {
7492 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
7494 l_tccp = p_tcp->tccps;
7495 l_current_offset_data = l_offset_data;
7497 for (i=0;i<p_image->numcomps;++i) {
7498 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
7502 opj_free(l_offset_data);
7509 * Writes the CBD marker (Component bit depth definition)
7511 * @param p_stream the stream to write data to.
7512 * @param p_j2k J2K codec.
7513 * @param p_manager the user event manager.
7515 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
7516 struct opj_stream_private *p_stream,
7517 struct opj_event_mgr * p_manager )
7520 OPJ_UINT32 l_cbd_size;
7521 OPJ_BYTE * l_current_data = 00;
7522 opj_image_t *l_image = 00;
7523 opj_image_comp_t * l_comp = 00;
7526 assert(p_j2k != 00);
7527 assert(p_manager != 00);
7528 assert(p_stream != 00);
7530 l_image = p_j2k->m_private_image;
7531 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
7533 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
7534 p_j2k->m_specific_param.m_encoder.m_header_tile_data
7535 = (OPJ_BYTE*)opj_realloc(
7536 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
7539 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7543 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
7546 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
7548 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
7549 l_current_data += 2;
7551 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
7552 l_current_data += 2;
7554 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
7557 l_comp = l_image->comps;
7559 for (i=0;i<l_image->numcomps;++i) {
7560 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
7566 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_cbd_size,p_manager) != l_cbd_size) {
7574 * Reads a CBD marker (Component bit depth definition)
7575 * @param p_header_data the data contained in the CBD box.
7576 * @param p_j2k the jpeg2000 codec.
7577 * @param p_header_size the size of the data contained in the CBD marker.
7578 * @param p_manager the user event manager.
7580 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
7581 OPJ_BYTE * p_header_data,
7582 OPJ_UINT32 p_header_size,
7583 struct opj_event_mgr * p_manager)
7585 OPJ_UINT32 l_nb_comp,l_num_comp;
7586 OPJ_UINT32 l_comp_def;
7588 opj_image_comp_t * l_comp = 00;
7591 assert(p_header_data != 00);
7592 assert(p_j2k != 00);
7593 assert(p_manager != 00);
7595 l_num_comp = p_j2k->m_private_image->numcomps;
7597 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
7598 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7602 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
7605 if (l_nb_comp != l_num_comp) {
7606 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
7610 l_comp = p_j2k->m_private_image->comps;
7611 for (i=0;i<l_num_comp;++i) {
7612 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7614 l_comp->sgnd = (l_comp_def>>7) & 1;
7615 l_comp->prec = (l_comp_def&0x7f) + 1;
7623 /* ----------------------------------------------------------------------- */
7624 /* J2K / JPT decoder interface */
7625 /* ----------------------------------------------------------------------- */
7627 opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
7628 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7632 j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
7633 if(!j2k->default_tcp) {
7639 j2k->tile_data = NULL;
7644 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7647 if(j2k->tile_len != NULL) {
7648 opj_free(j2k->tile_len);
7650 if(j2k->tile_data != NULL) {
7651 opj_free(j2k->tile_data);
7653 if(j2k->default_tcp != NULL) {
7654 opj_tcp_t *default_tcp = j2k->default_tcp;
7655 if(default_tcp->ppt_data_first != NULL) {
7656 opj_free(default_tcp->ppt_data_first);
7658 if(j2k->default_tcp->tccps != NULL) {
7659 opj_free(j2k->default_tcp->tccps);
7661 opj_free(j2k->default_tcp);
7663 if(j2k->cp != NULL) {
7664 opj_cp_t *cp = j2k->cp;
7665 if(cp->tcps != NULL) {
7666 for(i = 0; i < cp->tw * cp->th; i++) {
7667 if(cp->tcps[i].ppt_data_first != NULL) {
7668 opj_free(cp->tcps[i].ppt_data_first);
7670 if(cp->tcps[i].tccps != NULL) {
7671 opj_free(cp->tcps[i].tccps);
7676 if(cp->ppm_data_first != NULL) {
7677 opj_free(cp->ppm_data_first);
7679 if(cp->tileno != NULL) {
7680 opj_free(cp->tileno);
7682 if(cp->comment != NULL) {
7683 opj_free(cp->comment);
7691 void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
7692 if(j2k && parameters) {
7693 /* create and initialize the coding parameters structure */
7694 opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7695 cp->reduce = parameters->cp_reduce;
7696 cp->layer = parameters->cp_layer;
7697 cp->limit_decoding = parameters->cp_limit_decoding;
7700 cp->correct = parameters->jpwl_correct;
7701 cp->exp_comps = parameters->jpwl_exp_comps;
7702 cp->max_tiles = parameters->jpwl_max_tiles;
7703 #endif /* USE_JPWL */
7706 /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
7711 void j2k_setup_decoder_v2(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7713 if(j2k && parameters) {
7714 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7715 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7718 j2k->m_cp.correct = parameters->jpwl_correct;
7719 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7720 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7721 #endif /* USE_JPWL */
7725 opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7726 opj_image_t *image = NULL;
7728 opj_common_ptr cinfo = j2k->cinfo;
7731 j2k->cstr_info = cstr_info;
7733 memset(cstr_info, 0, sizeof(opj_codestream_info_t));
7735 /* create an empty image */
7736 image = opj_image_create0();
7739 j2k->state = J2K_STATE_MHSOC;
7742 opj_dec_mstabent_t *e;
7743 int id = cio_read(cio, 2);
7746 /* we try to honor JPWL correction power */
7747 if (j2k->cp->correct) {
7749 int orig_pos = cio_tell(cio);
7752 /* call the corrector */
7753 status = jpwl_correct(j2k);
7755 /* go back to where you were */
7756 cio_seek(cio, orig_pos - 2);
7758 /* re-read the marker */
7759 id = cio_read(cio, 2);
7761 /* check whether it begins with ff */
7762 if (id >> 8 != 0xff) {
7763 opj_event_msg(cinfo, EVT_ERROR,
7764 "JPWL: possible bad marker %x at %d\n",
7765 id, cio_tell(cio) - 2);
7767 opj_image_destroy(image);
7768 opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
7771 /* we try to correct */
7773 cio_seek(cio, cio_tell(cio) - 2);
7774 cio_write(cio, id, 2);
7775 opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
7776 "- setting marker to %x\n",
7781 #endif /* USE_JPWL */
7783 if (id >> 8 != 0xff) {
7784 opj_image_destroy(image);
7785 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7788 e = j2k_dec_mstab_lookup(id);
7789 /* Check if the marker is known*/
7790 if (!(j2k->state & e->states)) {
7791 opj_image_destroy(image);
7792 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7795 /* Check if the decoding is limited to the main header*/
7796 if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
7797 opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
7804 if (j2k->state & J2K_STATE_ERR)
7807 if (j2k->state == J2K_STATE_MT) {
7810 if (j2k->state == J2K_STATE_NEOC) {
7814 if (j2k->state == J2K_STATE_NEOC) {
7818 if (j2k->state != J2K_STATE_MT) {
7819 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7825 * Read a JPT-stream and decode file
7828 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7829 opj_image_t *image = NULL;
7830 opj_jpt_msg_header_t header;
7832 opj_common_ptr cinfo = j2k->cinfo;
7834 OPJ_ARG_NOT_USED(cstr_info);
7838 /* create an empty image */
7839 image = opj_image_create0();
7842 j2k->state = J2K_STATE_MHSOC;
7844 /* Initialize the header */
7845 jpt_init_msg_header(&header);
7846 /* Read the first header of the message */
7847 jpt_read_msg_header(cinfo, cio, &header);
7849 position = cio_tell(cio);
7850 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7851 opj_image_destroy(image);
7852 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7857 opj_dec_mstabent_t *e = NULL;
7860 if (!cio_numbytesleft(cio)) {
7864 /* data-bin read -> need to read a new header */
7865 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7866 jpt_read_msg_header(cinfo, cio, &header);
7867 position = cio_tell(cio);
7868 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7869 opj_image_destroy(image);
7870 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7875 id = cio_read(cio, 2);
7876 if (id >> 8 != 0xff) {
7877 opj_image_destroy(image);
7878 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7881 e = j2k_dec_mstab_lookup(id);
7882 if (!(j2k->state & e->states)) {
7883 opj_image_destroy(image);
7884 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7890 if (j2k->state == J2K_STATE_MT) {
7893 if (j2k->state == J2K_STATE_NEOC) {
7897 if (j2k->state == J2K_STATE_NEOC) {
7901 if (j2k->state != J2K_STATE_MT) {
7902 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7908 /* ----------------------------------------------------------------------- */
7909 /* J2K encoder interface */
7910 /* ----------------------------------------------------------------------- */
7912 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7913 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7920 opj_j2k_v2_t* j2k_create_compress_v2(void)
7922 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7927 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7929 l_j2k->m_is_decoder = 0;
7930 l_j2k->m_cp.m_is_decoder = 0;
7932 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7933 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7938 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7940 /* validation list creation*/
7941 l_j2k->m_validation_list = opj_procedure_list_create();
7942 if (! l_j2k->m_validation_list) {
7947 /* execution list creation*/
7948 l_j2k->m_procedure_list = opj_procedure_list_create();
7949 if (! l_j2k->m_procedure_list) {
7957 void j2k_destroy_compress(opj_j2k_t *j2k) {
7961 if(j2k->cp != NULL) {
7962 opj_cp_t *cp = j2k->cp;
7965 opj_free(cp->comment);
7968 opj_free(cp->matrice);
7970 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7971 opj_free(cp->tcps[tileno].tccps);
7980 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7982 int tileno, numpocs_tile;
7983 opj_cp_t *cp = NULL;
7985 if(!j2k || !parameters || ! image) {
7989 /* create and initialize the coding parameters structure */
7990 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7992 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7995 /* set default values for cp */
8000 copy user encoding parameters
8002 cp->cinema = parameters->cp_cinema;
8003 cp->max_comp_size = parameters->max_comp_size;
8004 cp->rsiz = parameters->cp_rsiz;
8005 cp->disto_alloc = parameters->cp_disto_alloc;
8006 cp->fixed_alloc = parameters->cp_fixed_alloc;
8007 cp->fixed_quality = parameters->cp_fixed_quality;
8009 /* mod fixed_quality */
8010 if(parameters->cp_matrice) {
8011 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
8012 cp->matrice = (int *) opj_malloc(array_size);
8013 memcpy(cp->matrice, parameters->cp_matrice, array_size);
8017 cp->tdx = parameters->cp_tdx;
8018 cp->tdy = parameters->cp_tdy;
8021 cp->tx0 = parameters->cp_tx0;
8022 cp->ty0 = parameters->cp_ty0;
8024 /* comment string */
8025 if(parameters->cp_comment) {
8026 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
8028 strcpy(cp->comment, parameters->cp_comment);
8033 calculate other encoding parameters
8036 if (parameters->tile_size_on) {
8037 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
8038 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8040 cp->tdx = image->x1 - cp->tx0;
8041 cp->tdy = image->y1 - cp->ty0;
8044 if(parameters->tp_on){
8045 cp->tp_flag = parameters->tp_flag;
8050 for(i=0;i<image->numcomps ;i++){
8051 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
8057 calculate JPWL encoding parameters
8060 if (parameters->jpwl_epc_on) {
8064 cp->epc_on = OPJ_TRUE;
8065 cp->info_on = OPJ_FALSE; /* no informative technique */
8068 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8069 cp->epb_on = OPJ_TRUE;
8071 cp->hprot_MH = parameters->jpwl_hprot_MH;
8072 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8073 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8074 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8076 /* if tile specs are not specified, copy MH specs */
8077 if (cp->hprot_TPH[0] == -1) {
8078 cp->hprot_TPH_tileno[0] = 0;
8079 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8081 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8082 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8083 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8084 cp->pprot[i] = parameters->jpwl_pprot[i];
8088 /* set ESD writing */
8089 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8090 cp->esd_on = OPJ_TRUE;
8092 cp->sens_size = parameters->jpwl_sens_size;
8093 cp->sens_addr = parameters->jpwl_sens_addr;
8094 cp->sens_range = parameters->jpwl_sens_range;
8096 cp->sens_MH = parameters->jpwl_sens_MH;
8097 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8098 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8099 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8103 /* always set RED writing to false: we are at the encoder */
8104 cp->red_on = OPJ_FALSE;
8107 cp->epc_on = OPJ_FALSE;
8109 #endif /* USE_JPWL */
8112 /* initialize the mutiple tiles */
8113 /* ---------------------------- */
8114 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
8116 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8117 opj_tcp_t *tcp = &cp->tcps[tileno];
8118 tcp->numlayers = parameters->tcp_numlayers;
8119 assert ( tcp->numlayers >= 0 );
8120 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
8122 if (cp->fixed_quality) {
8123 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8125 tcp->rates[j] = parameters->tcp_rates[j];
8127 if (cp->fixed_quality) { /* add fixed_quality */
8128 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8130 tcp->rates[j] = parameters->tcp_rates[j];
8134 tcp->csty = parameters->csty;
8135 tcp->prg = parameters->prog_order;
8136 tcp->mct = parameters->tcp_mct;
8140 if (parameters->numpocs) {
8141 /* initialisation of POC */
8143 assert( parameters->numpocs >= 0 );
8144 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
8145 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8146 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8147 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
8148 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
8149 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
8150 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
8151 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
8152 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
8153 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
8157 tcp->numpocs = numpocs_tile -1 ;
8162 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8164 for (i = 0; i < image->numcomps; i++) {
8165 opj_tccp_t *tccp = &tcp->tccps[i];
8166 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
8167 tccp->numresolutions = parameters->numresolution;
8168 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8169 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8170 tccp->cblksty = parameters->mode;
8171 tccp->qmfbid = parameters->irreversible ? 0 : 1;
8172 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8174 assert(parameters->roi_compno >= 0);
8175 if (i == (OPJ_UINT32)parameters->roi_compno) {
8176 tccp->roishift = parameters->roi_shift;
8181 if(parameters->cp_cinema)
8183 /*Precinct size for lowest frequency subband=128*/
8186 /*Precinct size at all other resolutions = 256*/
8187 for (j = 1; j < tccp->numresolutions; j++) {
8192 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8194 assert(tccp->numresolutions > 0);
8195 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
8196 if (p < parameters->res_spec) {
8198 if (parameters->prcw_init[p] < 1) {
8201 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
8204 if (parameters->prch_init[p] < 1) {
8207 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
8211 int res_spec = parameters->res_spec;
8212 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8213 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8215 if (size_prcw < 1) {
8218 tccp->prcw[j] = int_floorlog2(size_prcw);
8221 if (size_prch < 1) {
8224 tccp->prch[j] = int_floorlog2(size_prch);
8228 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
8231 for (j = 0; j < tccp->numresolutions; j++) {
8238 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8243 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
8244 opj_cparameters_t *parameters,
8246 struct opj_event_mgr * p_manager)
8248 OPJ_UINT32 i, j, tileno, numpocs_tile;
8249 opj_cp_v2_t *cp = 00;
8252 if(!p_j2k || !parameters || ! image) {
8256 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
8257 cp = &(p_j2k->m_cp);
8259 /* set default values for cp */
8264 copy user encoding parameters
8266 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
8267 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
8268 cp->rsiz = parameters->cp_rsiz;
8269 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
8270 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
8271 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
8273 /* mod fixed_quality */
8274 if (parameters->cp_matrice) {
8275 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
8276 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
8277 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
8281 cp->tdx = parameters->cp_tdx;
8282 cp->tdy = parameters->cp_tdy;
8285 cp->tx0 = parameters->cp_tx0;
8286 cp->ty0 = parameters->cp_ty0;
8288 /* comment string */
8289 if(parameters->cp_comment) {
8290 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
8292 strcpy(cp->comment, parameters->cp_comment);
8297 calculate other encoding parameters
8300 if (parameters->tile_size_on) {
8301 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
8302 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
8304 cp->tdx = image->x1 - cp->tx0;
8305 cp->tdy = image->y1 - cp->ty0;
8308 if (parameters->tp_on) {
8309 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
8310 cp->m_specific_param.m_enc.m_tp_on = 1;
8315 calculate JPWL encoding parameters
8318 if (parameters->jpwl_epc_on) {
8322 cp->epc_on = OPJ_TRUE;
8323 cp->info_on = OPJ_FALSE; /* no informative technique */
8326 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
8327 cp->epb_on = OPJ_TRUE;
8329 cp->hprot_MH = parameters->jpwl_hprot_MH;
8330 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8331 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
8332 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
8334 /* if tile specs are not specified, copy MH specs */
8335 if (cp->hprot_TPH[0] == -1) {
8336 cp->hprot_TPH_tileno[0] = 0;
8337 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
8339 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
8340 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
8341 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
8342 cp->pprot[i] = parameters->jpwl_pprot[i];
8346 /* set ESD writing */
8347 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
8348 cp->esd_on = OPJ_TRUE;
8350 cp->sens_size = parameters->jpwl_sens_size;
8351 cp->sens_addr = parameters->jpwl_sens_addr;
8352 cp->sens_range = parameters->jpwl_sens_range;
8354 cp->sens_MH = parameters->jpwl_sens_MH;
8355 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
8356 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
8357 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
8361 /* always set RED writing to false: we are at the encoder */
8362 cp->red_on = OPJ_FALSE;
8365 cp->epc_on = OPJ_FALSE;
8367 #endif /* USE_JPWL */
8370 /* initialize the mutiple tiles */
8371 /* ---------------------------- */
8372 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
8373 if (parameters->numpocs) {
8374 /* initialisation of POC */
8375 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
8379 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8380 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
8381 tcp->numlayers = parameters->tcp_numlayers;
8383 for (j = 0; j < tcp->numlayers; j++) {
8384 if(cp->m_specific_param.m_enc.m_cinema){
8385 if (cp->m_specific_param.m_enc.m_fixed_quality) {
8386 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8388 tcp->rates[j] = parameters->tcp_rates[j];
8390 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
8391 tcp->distoratio[j] = parameters->tcp_distoratio[j];
8393 tcp->rates[j] = parameters->tcp_rates[j];
8398 tcp->csty = parameters->csty;
8399 tcp->prg = parameters->prog_order;
8400 tcp->mct = parameters->tcp_mct;
8405 if (parameters->numpocs) {
8406 /* initialisation of POC */
8409 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
8410 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
8411 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
8413 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
8414 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
8415 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
8416 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
8417 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
8418 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
8419 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
8425 tcp->numpocs = numpocs_tile -1 ;
8430 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
8432 if (parameters->mct_data) {
8434 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
8437 /* TODO MSD : merge v2 add invert.c or used a external lib ?
8438 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
8439 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8440 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
8443 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8444 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
8445 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
8447 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
8448 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
8450 tcp->mct_norms = (OPJ_FLOAT64*)
8451 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
8453 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
8456 for (i = 0; i < image->numcomps; i++) {
8457 opj_tccp_t *tccp = &tcp->tccps[i];
8458 tccp->m_dc_level_shift = l_dc_shift[i];
8461 j2k_setup_mct_encoding(tcp,image);
8465 for (i = 0; i < image->numcomps; i++) {
8466 opj_tccp_t *tccp = &tcp->tccps[i];
8467 opj_image_comp_t * l_comp = &(image->comps[i]);
8469 if (! l_comp->sgnd) {
8470 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
8475 for (i = 0; i < image->numcomps; i++) {
8476 opj_tccp_t *tccp = &tcp->tccps[i];
8478 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
8479 tccp->numresolutions = parameters->numresolution;
8480 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
8481 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
8482 tccp->cblksty = parameters->mode;
8483 tccp->qmfbid = parameters->irreversible ? 0 : 1;
8484 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
8487 if (i == parameters->roi_compno) {
8488 tccp->roishift = parameters->roi_shift;
8493 if(parameters->cp_cinema) {
8494 //Precinct size for lowest frequency subband=128
8497 //Precinct size at all other resolutions = 256
8498 for (j = 1; j < tccp->numresolutions; j++) {
8503 if (parameters->csty & J2K_CCP_CSTY_PRT) {
8504 OPJ_INT32 p = 0, it_res;
8505 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
8506 if (p < parameters->res_spec) {
8508 if (parameters->prcw_init[p] < 1) {
8509 tccp->prcw[it_res] = 1;
8511 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
8514 if (parameters->prch_init[p] < 1) {
8515 tccp->prch[it_res] = 1;
8517 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
8521 int res_spec = parameters->res_spec;
8522 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
8523 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
8525 if (size_prcw < 1) {
8526 tccp->prcw[it_res] = 1;
8528 tccp->prcw[it_res] = int_floorlog2(size_prcw);
8531 if (size_prch < 1) {
8532 tccp->prch[it_res] = 1;
8534 tccp->prch[it_res] = int_floorlog2(size_prch);
8538 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
8541 for (j = 0; j < tccp->numresolutions; j++) {
8548 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
8552 if (parameters->mct_data) {
8553 opj_free(parameters->mct_data);
8554 parameters->mct_data = 00;
8559 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
8562 opj_cp_t *cp = NULL;
8564 opj_tcd_t *tcd = NULL; /* TCD component */
8572 j2k->cstr_info = cstr_info;
8575 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
8576 cstr_info->image_w = image->x1 - image->x0;
8577 cstr_info->image_h = image->y1 - image->y0;
8578 cstr_info->prog = (&cp->tcps[0])->prg;
8579 cstr_info->tw = cp->tw;
8580 cstr_info->th = cp->th;
8581 cstr_info->tile_x = cp->tdx; /* new version parser */
8582 cstr_info->tile_y = cp->tdy; /* new version parser */
8583 cstr_info->tile_Ox = cp->tx0; /* new version parser */
8584 cstr_info->tile_Oy = cp->ty0; /* new version parser */
8585 cstr_info->numcomps = image->numcomps;
8586 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
8587 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
8588 for (compno=0; compno < image->numcomps; compno++) {
8589 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
8591 cstr_info->D_max = 0.0; /* ADD Marcela */
8592 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
8593 cstr_info->maxmarknum = 100;
8594 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
8595 cstr_info->marknum = 0;
8605 for (compno = 1; compno < image->numcomps; compno++) {
8606 j2k_write_coc(j2k, compno);
8607 j2k_write_qcc(j2k, compno);
8611 for (compno = 0; compno < image->numcomps; compno++) {
8612 opj_tcp_t *tcp = &cp->tcps[0];
8613 if (tcp->tccps[compno].roishift)
8614 j2k_write_rgn(j2k, compno, 0);
8616 if (cp->comment != NULL) {
8620 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
8624 if (cp->cinema == CINEMA4K_24) {
8629 /* uncomment only for testing JPSEC marker writing */
8630 /* j2k_write_sec(j2k); */
8634 cstr_info->main_head_end = cio_tell(cio) - 1;
8637 /**** Main Header ENDS here ***/
8639 /* create the tile encoder */
8640 tcd = tcd_create(j2k->cinfo);
8642 /* encode each tile */
8643 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
8647 int acc_pack_num = 0;
8651 opj_tcp_t *tcp = &cp->tcps[tileno];
8652 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
8654 j2k->curtileno = tileno;
8655 j2k->cur_tp_num = 0;
8656 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
8657 /* initialisation before tile encoding */
8659 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
8661 tcd_init_encode(tcd, image, cp, j2k->curtileno);
8666 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
8667 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
8668 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
8669 cstr_info->tile[j2k->curtileno].marknum = 0;
8673 for(pino = 0; pino <= tcp->numpocs; pino++) {
8677 /*Get number of tile parts*/
8678 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
8679 tcd->tp_pos = cp->tp_pos;
8681 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
8682 j2k->tp_num = tilepartno;
8685 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
8686 cio_tell(cio) + j2k->pos_correction;
8690 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
8691 for (compno = 1; compno < image->numcomps; compno++) {
8692 j2k_write_coc(j2k, compno);
8693 j2k_write_qcc(j2k, compno);
8695 if (cp->tcps[tileno].numpocs) {
8702 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
8703 cio_tell(cio) + j2k->pos_correction + 1;
8706 j2k_write_sod(j2k, tcd);
8710 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
8711 cio_tell(cio) + j2k->pos_correction - 1;
8712 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
8714 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
8715 cstr_info->packno - acc_pack_num;
8716 acc_pack_num = cstr_info->packno;
8724 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
8729 if (tile->PPT) { // BAD PPT !!!
8732 PPT_file=fopen("PPT","rb");
8733 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
8734 for (i=0;i<tile->len_ppt;i++) {
8736 fread(&elmt, 1, 1, PPT_file);
8737 fwrite(&elmt,1,1,f);
8746 /* destroy the tile encoder */
8747 tcd_free_encode(tcd);
8750 opj_free(j2k->cur_totnum_tp);
8755 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8757 /* The following adjustment is done to adjust the codestream size */
8758 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8759 /* the first bunch of bytes is not in the codestream */
8760 cstr_info->codestream_size -= cstr_info->main_head_start;
8766 preparation of JPWL marker segments
8770 /* encode according to JPWL */
8771 jpwl_encode(j2k, cio, image);
8774 #endif /* USE_JPWL */
8779 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8781 assert(cstr_info != 00);
8783 /* expand the list? */
8784 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8785 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8786 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8789 /* add the marker */
8790 cstr_info->marker[cstr_info->marknum].type = type;
8791 cstr_info->marker[cstr_info->marknum].pos = pos;
8792 cstr_info->marker[cstr_info->marknum].len = len;
8793 cstr_info->marknum++;
8797 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8799 assert(cstr_index != 00);
8801 /* expand the list? */
8802 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8803 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8804 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8807 /* add the marker */
8808 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8809 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8810 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8811 cstr_index->marknum++;
8815 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8817 opj_marker_info_t *marker;
8819 assert(cstr_info != 00);
8821 /* expand the list? */
8822 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8823 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8824 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8827 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8829 /* add the marker */
8830 marker->type = type;
8833 cstr_info->tile[tileno].marknum++;
8836 static void j2k_add_tlmarker_v2(OPJ_UINT32 tileno, opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8838 assert(cstr_index != 00);
8839 assert(cstr_index->tile_index != 00);
8841 /* expand the list? */
8842 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8843 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8844 cstr_index->tile_index[tileno].marker =
8845 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8846 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8849 /* add the marker */
8850 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8851 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8852 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8853 cstr_index->tile_index[tileno].marknum++;
8855 if (type == J2K_MS_SOT) {
8856 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8858 if (cstr_index->tile_index[tileno].tp_index)
8859 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8866 * -----------------------------------------------------------------------
8867 * -----------------------------------------------------------------------
8868 * -----------------------------------------------------------------------
8872 * Ends the decompression procedures and possibiliy add data to be read after the
8875 opj_bool j2k_end_decompress(
8876 opj_j2k_v2_t *p_j2k,
8877 opj_stream_private_t *p_stream,
8878 opj_event_mgr_t * p_manager)
8887 * Reads a jpeg2000 codestream header structure.
8890 * @param p_stream the stream to read data from.
8891 * @param p_j2k the jpeg2000 codec.
8892 * @param p_manager the user event manager.
8894 * @return true if the box is valid.
8896 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8897 opj_j2k_v2_t* p_j2k,
8898 opj_image_t** p_image,
8899 struct opj_event_mgr* p_manager )
8902 assert(p_j2k != 00);
8903 assert(p_stream != 00);
8904 assert(p_manager != 00);
8906 /* create an empty image header */
8907 p_j2k->m_private_image = opj_image_create0();
8908 if (! p_j2k->m_private_image) {
8912 /* customization of the validation */
8913 j2k_setup_decoding_validation(p_j2k);
8915 /* validation of the parameters codec */
8916 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8917 opj_image_destroy(p_j2k->m_private_image);
8918 p_j2k->m_private_image = NULL;
8922 /* customization of the encoding */
8923 j2k_setup_header_reading(p_j2k);
8926 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8927 opj_image_destroy(p_j2k->m_private_image);
8928 p_j2k->m_private_image = NULL;
8932 *p_image = opj_image_create0();
8937 /* Copy codestream image information to the output image */
8938 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8940 /*Allocate and initialize some elements of codestrem index*/
8941 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8949 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8951 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8954 assert(p_j2k != 00);
8956 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8958 /* DEVELOPER CORNER, add your custom procedures */
8959 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8964 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8965 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8967 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8970 assert(p_j2k != 00);
8972 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8973 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8974 /* DEVELOPER CORNER, add your custom validation procedure */
8980 * The mct encoding validation procedure.
8982 * @param p_j2k the jpeg2000 codec to validate.
8983 * @param p_stream the input stream to validate.
8984 * @param p_manager the user event manager.
8986 * @return true if the parameters are correct.
8988 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8989 opj_stream_private_t *p_stream,
8990 opj_event_mgr_t * p_manager )
8992 opj_bool l_is_valid = OPJ_TRUE;
8996 assert(p_j2k != 00);
8997 assert(p_stream != 00);
8998 assert(p_manager != 00);
9000 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
9001 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9002 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
9004 for (i=0;i<l_nb_tiles;++i) {
9005 if (l_tcp->mct == 2) {
9006 opj_tccp_t * l_tccp = l_tcp->tccps;
9007 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
9009 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
9010 l_is_valid &= ! (l_tccp->qmfbid & 1);
9021 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
9024 OPJ_UINT32 l_indix = 1;
9025 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
9026 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
9027 OPJ_UINT32 l_mct_size,l_nb_elem;
9028 OPJ_FLOAT32 * l_data, * l_current_data;
9029 opj_tccp_t * l_tccp;
9032 assert(p_tcp != 00);
9034 if (p_tcp->mct != 2) {
9038 if (p_tcp->m_mct_decoding_matrix) {
9039 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
9040 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
9042 p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
9043 if (! p_tcp->m_mct_records) {
9046 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9048 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
9050 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9052 if (l_mct_deco_data->m_data) {
9053 opj_free(l_mct_deco_data->m_data);
9054 l_mct_deco_data->m_data = 00;
9057 l_mct_deco_data->m_index = l_indix++;
9058 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
9059 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
9060 l_nb_elem = p_image->numcomps * p_image->numcomps;
9061 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
9062 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
9064 if (! l_mct_deco_data->m_data) {
9068 j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](p_tcp->m_mct_decoding_matrix,l_mct_deco_data->m_data,l_nb_elem);
9070 l_mct_deco_data->m_data_size = l_mct_size;
9071 ++p_tcp->m_nb_mct_records;
9074 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
9075 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
9076 p_tcp->m_mct_records = (opj_mct_data_t*)opj_realloc(p_tcp->m_mct_records,p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
9078 if (! p_tcp->m_mct_records) {
9082 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9083 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
9085 if (l_mct_deco_data) {
9086 l_mct_deco_data = l_mct_offset_data - 1;
9090 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
9092 if (l_mct_offset_data->m_data) {
9093 opj_free(l_mct_offset_data->m_data);
9094 l_mct_offset_data->m_data = 00;
9097 l_mct_offset_data->m_index = l_indix++;
9098 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
9099 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
9100 l_nb_elem = p_image->numcomps;
9101 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
9102 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
9104 if (! l_mct_offset_data->m_data) {
9108 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
9110 opj_free(l_mct_offset_data->m_data);
9111 l_mct_offset_data->m_data = 00;
9115 l_tccp = p_tcp->tccps;
9116 l_current_data = l_data;
9118 for (i=0;i<l_nb_elem;++i) {
9119 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
9123 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
9127 l_mct_offset_data->m_data_size = l_mct_size;
9129 ++p_tcp->m_nb_mct_records;
9131 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
9132 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
9133 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
9134 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
9136 if (! p_tcp->m_mcc_records) {
9139 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
9140 memset(l_mcc_data ,0,(p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) * sizeof(opj_simple_mcc_decorrelation_data_t));
9144 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
9145 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
9146 l_mcc_data->m_is_irreversible = 1;
9147 l_mcc_data->m_nb_comps = p_image->numcomps;
9148 l_mcc_data->m_index = l_indix++;
9149 l_mcc_data->m_offset_array = l_mct_offset_data;
9150 ++p_tcp->m_nb_mcc_records;
9156 * Builds the cp decoder parameters to use to decode tile.
9158 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
9159 opj_stream_private_t *p_stream,
9160 opj_event_mgr_t * p_manager )
9162 /* add here initialization of cp
9163 copy paste of setup_decoder */
9171 * Builds the cp encoder parameters to use to encode tile.
9173 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
9174 opj_stream_private_t *p_stream,
9175 opj_event_mgr_t * p_manager )
9177 /* add here initialization of cp
9178 copy paste of setup_encoder */
9186 * The default encoding validation procedure without any extension.
9188 * @param p_j2k the jpeg2000 codec to validate.
9189 * @param p_stream the input stream to validate.
9190 * @param p_manager the user event manager.
9192 * @return true if the parameters are correct.
9194 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
9195 opj_stream_private_t *p_stream,
9196 opj_event_mgr_t * p_manager )
9198 opj_bool l_is_valid = OPJ_TRUE;
9201 assert(p_j2k != 00);
9202 assert(p_stream != 00);
9203 assert(p_manager != 00);
9205 /* STATE checking */
9206 /* make sure the state is at 0 */
9207 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
9209 /* POINTER validation */
9210 /* make sure a p_j2k codec is present */
9211 l_is_valid &= (p_j2k->m_procedure_list != 00);
9212 /* make sure a validation list is present */
9213 l_is_valid &= (p_j2k->m_validation_list != 00);
9215 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
9216 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
9220 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
9221 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
9225 /* PARAMETER VALIDATION */
9230 * The default decoding validation procedure without any extension.
9232 * @param p_j2k the jpeg2000 codec to validate.
9233 * @param p_stream the input stream to validate.
9234 * @param p_manager the user event manager.
9236 * @return true if the parameters are correct.
9238 opj_bool j2k_decoding_validation (
9239 opj_j2k_v2_t *p_j2k,
9240 opj_stream_private_t *p_stream,
9241 opj_event_mgr_t * p_manager
9244 opj_bool l_is_valid = OPJ_TRUE;
9247 assert(p_j2k != 00);
9248 assert(p_stream != 00);
9249 assert(p_manager != 00);
9252 /* STATE checking */
9253 /* make sure the state is at 0 */
9255 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
9257 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
9259 /* POINTER validation */
9260 /* make sure a p_j2k codec is present */
9261 /* make sure a procedure list is present */
9262 l_is_valid &= (p_j2k->m_procedure_list != 00);
9263 /* make sure a validation list is present */
9264 l_is_valid &= (p_j2k->m_validation_list != 00);
9266 /* PARAMETER VALIDATION */
9270 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
9271 struct opj_stream_private *p_stream,
9272 struct opj_event_mgr * p_manager)
9274 OPJ_UINT32 l_current_marker;
9275 OPJ_UINT32 l_marker_size;
9276 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9279 assert(p_stream != 00);
9280 assert(p_j2k != 00);
9281 assert(p_manager != 00);
9283 /* We enter in the main header */
9284 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
9286 /* Try to read the SOC marker, the codestream must begin with SOC marker */
9287 if (! j2k_read_soc_v2(p_j2k,p_stream,p_manager)) {
9288 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
9292 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9293 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9294 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9298 /* Read 2 bytes as the new marker ID */
9299 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9301 /* Try to read until the SOT is detected */
9302 while (l_current_marker != J2K_MS_SOT) {
9304 /* Check if the current marker ID is valid */
9305 if (l_current_marker < 0xff00) {
9306 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
9310 /* Get the marker handler from the marker ID */
9311 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9313 /* Manage case where marker is unknown */
9314 if (l_marker_handler->id == J2K_MS_UNK) {
9315 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
9316 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
9320 if (l_current_marker == J2K_MS_SOT)
9321 break; /* SOT marker is detected main header is completely read */
9322 else /* Get the marker handler from the marker ID */
9323 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9326 /* Check if the marker is known and if it is the right place to find it */
9327 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9328 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9332 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9333 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9334 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9338 /* read 2 bytes as the marker size */
9339 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9340 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9342 /* Check if the marker size is compatible with the header data size */
9343 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9344 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9345 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9346 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9349 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9352 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9353 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) {
9354 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9358 /* Read the marker segment with the correct marker handler */
9359 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9360 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
9364 /* Add the marker to the codestream index*/
9365 j2k_add_mhmarker_v2(p_j2k->cstr_index,
9366 l_marker_handler->id,
9367 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9368 l_marker_size + 4 );
9370 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9371 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9372 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9376 /* read 2 bytes as the new marker ID */
9377 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9380 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
9382 /* Position of the last element if the main header */
9383 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
9385 /* Next step: read a tile-part header */
9386 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9392 * Excutes the given procedures on the given codec.
9394 * @param p_procedure_list the list of procedures to execute
9395 * @param p_j2k the jpeg2000 codec to execute the procedures on.
9396 * @param p_stream the stream to execute the procedures on.
9397 * @param p_manager the user manager.
9399 * @return true if all the procedures were successfully executed.
9401 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
9402 opj_procedure_list_t * p_procedure_list,
9403 opj_stream_private_t *p_stream,
9404 opj_event_mgr_t * p_manager )
9406 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
9407 opj_bool l_result = OPJ_TRUE;
9408 OPJ_UINT32 l_nb_proc, i;
9411 assert(p_procedure_list != 00);
9412 assert(p_j2k != 00);
9413 assert(p_stream != 00);
9414 assert(p_manager != 00);
9417 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
9418 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);
9420 for (i=0;i<l_nb_proc;++i) {
9421 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
9425 /* and clear the procedure list at the end.*/
9426 opj_procedure_list_clear(p_procedure_list);
9431 opj_bool j2k_copy_default_tcp_and_create_tcd
9433 opj_j2k_v2_t * p_j2k,
9434 opj_stream_private_t *p_stream,
9435 opj_event_mgr_t * p_manager
9438 opj_tcp_v2_t * l_tcp = 00;
9439 opj_tcp_v2_t * l_default_tcp = 00;
9440 OPJ_UINT32 l_nb_tiles;
9442 opj_tccp_t *l_current_tccp = 00;
9443 OPJ_UINT32 l_tccp_size;
9444 OPJ_UINT32 l_mct_size;
9445 opj_image_t * l_image;
9446 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
9447 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
9448 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
9449 OPJ_UINT32 l_offset;
9452 assert(p_j2k != 00);
9453 assert(p_stream != 00);
9454 assert(p_manager != 00);
9456 l_image = p_j2k->m_private_image;
9457 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9458 l_tcp = p_j2k->m_cp.tcps;
9459 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
9460 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9461 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
9464 for (i=0; i<l_nb_tiles; ++i) {
9465 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
9466 l_current_tccp = l_tcp->tccps;
9467 /*Copy default coding parameters into the current tile coding parameters*/
9468 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
9469 /* Initialize some values of the current tile coding parameters*/
9471 l_tcp->ppt_data = 00;
9472 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
9473 l_tcp->tccps = l_current_tccp;
9475 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
9476 if (l_default_tcp->m_mct_decoding_matrix) {
9477 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
9478 if (! l_tcp->m_mct_decoding_matrix ) {
9481 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
9484 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
9485 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
9486 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
9487 if (! l_tcp->m_mct_records) {
9490 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
9492 /* Copy the mct record data from dflt_tile_cp to the current tile*/
9493 l_src_mct_rec = l_default_tcp->m_mct_records;
9494 l_dest_mct_rec = l_tcp->m_mct_records;
9496 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
9498 if (l_src_mct_rec->m_data) {
9500 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
9501 if(! l_dest_mct_rec->m_data) {
9504 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
9511 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
9512 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
9513 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
9514 if (! l_tcp->m_mcc_records) {
9517 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
9519 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
9520 l_src_mcc_rec = l_default_tcp->m_mcc_records;
9521 l_dest_mcc_rec = l_tcp->m_mcc_records;
9523 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
9525 if (l_src_mcc_rec->m_decorrelation_array) {
9526 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
9527 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
9530 if (l_src_mcc_rec->m_offset_array) {
9531 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
9532 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
9539 /* Copy all the dflt_tile_compo_cp to the current tile cp */
9540 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
9542 /* Move to next tile cp*/
9546 /* Create the current tile decoder*/
9547 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
9548 if (! p_j2k->m_tcd ) {
9552 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
9553 tcd_destroy_v2(p_j2k->m_tcd);
9555 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9563 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
9564 * with the marker value.
9565 * @param p_id Marker value to look up
9567 * @return the handler associated with the id.
9569 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
9571 const opj_dec_memory_marker_handler_t *e;
9572 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
9573 if (e->id == p_id) {
9574 break; /* we find a handler corresponding to the marker ID*/
9582 * Destroys a jpeg2000 codec.
9584 * @param p_j2k the jpeg20000 structure to destroy.
9586 void j2k_destroy (opj_j2k_v2_t *p_j2k)
9592 if (p_j2k->m_is_decoder) {
9594 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
9595 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9596 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
9597 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
9600 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
9601 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
9602 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
9603 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
9608 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
9609 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
9610 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
9613 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
9614 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
9615 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
9616 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
9619 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
9620 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
9621 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
9622 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
9626 tcd_destroy_v2(p_j2k->m_tcd);
9628 j2k_cp_destroy(&(p_j2k->m_cp));
9629 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
9631 opj_procedure_list_destroy(p_j2k->m_procedure_list);
9632 p_j2k->m_procedure_list = 00;
9634 opj_procedure_list_destroy(p_j2k->m_validation_list);
9635 p_j2k->m_procedure_list = 00;
9637 j2k_destroy_cstr_index(p_j2k->cstr_index);
9638 p_j2k->cstr_index = NULL;
9640 opj_image_destroy(p_j2k->m_private_image);
9641 p_j2k->m_private_image = NULL;
9643 opj_image_destroy(p_j2k->m_output_image);
9644 p_j2k->m_output_image = NULL;
9649 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
9653 if (p_cstr_ind->marker) {
9654 opj_free(p_cstr_ind->marker);
9655 p_cstr_ind->marker = NULL;
9658 if (p_cstr_ind->tile_index) {
9659 OPJ_UINT32 it_tile = 0;
9661 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
9663 if(p_cstr_ind->tile_index[it_tile].packet_index) {
9664 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
9665 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
9668 if(p_cstr_ind->tile_index[it_tile].tp_index){
9669 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
9670 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
9673 if(p_cstr_ind->tile_index[it_tile].marker){
9674 opj_free(p_cstr_ind->tile_index[it_tile].marker);
9675 p_cstr_ind->tile_index[it_tile].marker = NULL;
9680 opj_free( p_cstr_ind->tile_index);
9681 p_cstr_ind->tile_index = NULL;
9684 opj_free(p_cstr_ind);
9691 * Destroys a tile coding parameter structure.
9693 * @param p_tcp the tile coding parameter to destroy.
9695 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
9701 if (p_tcp->ppt_buffer != 00) {
9702 opj_free(p_tcp->ppt_buffer);
9703 p_tcp->ppt_buffer = 00;
9706 if (p_tcp->tccps != 00) {
9707 opj_free(p_tcp->tccps);
9711 if (p_tcp->m_mct_coding_matrix != 00) {
9712 opj_free(p_tcp->m_mct_coding_matrix);
9713 p_tcp->m_mct_coding_matrix = 00;
9716 if (p_tcp->m_mct_decoding_matrix != 00) {
9717 opj_free(p_tcp->m_mct_decoding_matrix);
9718 p_tcp->m_mct_decoding_matrix = 00;
9721 if (p_tcp->m_mcc_records) {
9722 opj_free(p_tcp->m_mcc_records);
9723 p_tcp->m_mcc_records = 00;
9724 p_tcp->m_nb_max_mcc_records = 0;
9725 p_tcp->m_nb_mcc_records = 0;
9728 if (p_tcp->m_mct_records) {
9729 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
9732 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
9733 if (l_mct_data->m_data) {
9734 opj_free(l_mct_data->m_data);
9735 l_mct_data->m_data = 00;
9741 opj_free(p_tcp->m_mct_records);
9742 p_tcp->m_mct_records = 00;
9745 if (p_tcp->mct_norms != 00) {
9746 opj_free(p_tcp->mct_norms);
9747 p_tcp->mct_norms = 00;
9750 j2k_tcp_data_destroy(p_tcp);
9755 * Destroys the data inside a tile coding parameter structure.
9757 * @param p_tcp the tile coding parameter which contain data to destroy.
9759 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9761 if (p_tcp->m_data) {
9762 opj_free(p_tcp->m_data);
9763 p_tcp->m_data = NULL;
9764 p_tcp->m_data_size = 0;
9769 * Destroys a coding parameter structure.
9771 * @param p_cp the coding parameter to destroy.
9773 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9775 OPJ_UINT32 l_nb_tiles;
9776 opj_tcp_v2_t * l_current_tile = 00;
9787 l_current_tile = p_cp->tcps;
9788 l_nb_tiles = p_cp->th * p_cp->tw;
9791 (i = 0; i < l_nb_tiles; ++i)
9793 j2k_tcp_destroy(l_current_tile);
9796 opj_free(p_cp->tcps);
9800 (p_cp->ppm_buffer != 00)
9802 opj_free(p_cp->ppm_buffer);
9803 p_cp->ppm_buffer = 00;
9806 (p_cp->comment != 00)
9808 opj_free(p_cp->comment);
9812 (! p_cp->m_is_decoder)
9815 (p_cp->m_specific_param.m_enc.m_matrice)
9817 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9818 p_cp->m_specific_param.m_enc.m_matrice = 00;
9826 * Reads a tile header.
9827 * @param p_j2k the jpeg2000 codec.
9828 * @param p_stream the stream to write data to.
9829 * @param p_manager the user event manager.
9831 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9832 OPJ_UINT32 * p_tile_index,
9833 OPJ_UINT32 * p_data_size,
9834 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9835 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9836 OPJ_UINT32 * p_nb_comps,
9838 opj_stream_private_t *p_stream,
9839 opj_event_mgr_t * p_manager )
9841 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9842 OPJ_UINT32 l_marker_size;
9843 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9844 opj_tcp_v2_t * l_tcp = NULL;
9845 OPJ_UINT32 l_nb_tiles;
9848 assert(p_stream != 00);
9849 assert(p_j2k != 00);
9850 assert(p_manager != 00);
9852 /* Reach the End Of Codestream ?*/
9853 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9854 l_current_marker = J2K_MS_EOC;
9856 /* We need to encounter a SOT marker (a new tile-part header) */
9857 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9861 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9862 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9864 /* Try to read until the Start Of Data is detected */
9865 while (l_current_marker != J2K_MS_SOD) {
9867 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9868 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9869 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9873 /* Read 2 bytes from the buffer as the marker size */
9874 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9876 /* Why this condition? FIXME */
9877 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9878 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9880 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9882 /* Get the marker handler from the marker ID */
9883 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9885 /* Check if the marker is known and if it is the right place to find it */
9886 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9887 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9890 /* FIXME manage case of unknown marker as in the main header ? */
9892 /* Check if the marker size is compatible with the header data size */
9893 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9894 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9895 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9896 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9899 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9902 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9903 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) {
9904 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9908 /* Read the marker segment with the correct marker handler */
9909 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9910 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9914 /* Add the marker to the codestream index*/
9915 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9917 l_marker_handler->id,
9918 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9919 l_marker_size + 4 );
9921 /* Keep the position of the last SOT marker read */
9922 if ( l_marker_handler->id == J2K_MS_SOT ) {
9923 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9924 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9926 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9931 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9932 /* Skip the rest of the tile part header*/
9933 if (opj_stream_skip(p_stream,p_j2k->m_specific_param.m_decoder.m_sot_length,p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) {
9934 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9937 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9940 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9941 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9942 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9945 /* Read 2 bytes from the buffer as the new marker ID */
9946 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9950 /* If we didn't skip data before, we need to read the SOD marker*/
9951 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9952 /* Try to read the SOD marker and skip data ? FIXME */
9953 if (! j2k_read_sod_v2(p_j2k, p_stream, p_manager)) {
9959 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9960 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9961 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9962 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9966 /* Read 2 bytes from buffer as the new marker ID */
9967 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9971 /* Indicate we will try to read a new tile-part header*/
9972 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9973 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9974 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9976 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9977 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9978 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9982 /* Read 2 bytes from buffer as the new marker ID */
9983 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9987 /* Current marker is the EOC marker ?*/
9988 if (l_current_marker == J2K_MS_EOC) {
9989 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9990 p_j2k->m_current_tile_number = 0;
9991 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9996 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9997 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9998 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10000 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
10001 ++p_j2k->m_current_tile_number;
10005 if (p_j2k->m_current_tile_number == l_nb_tiles) {
10006 *p_go_on = OPJ_FALSE;
10012 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
10013 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
10017 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
10018 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
10020 *p_tile_index = p_j2k->m_current_tile_number;
10021 *p_go_on = OPJ_TRUE;
10022 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
10023 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
10024 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
10025 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
10026 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
10027 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
10029 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
10035 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
10036 OPJ_UINT32 p_tile_index,
10038 OPJ_UINT32 p_data_size,
10039 opj_stream_private_t *p_stream,
10040 opj_event_mgr_t * p_manager )
10042 OPJ_UINT32 l_current_marker;
10043 OPJ_BYTE l_data [2];
10044 opj_tcp_v2_t * l_tcp;
10046 /* preconditions */
10047 assert(p_stream != 00);
10048 assert(p_j2k != 00);
10049 assert(p_manager != 00);
10051 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
10052 || (p_tile_index != p_j2k->m_current_tile_number) ) {
10056 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
10057 if (! l_tcp->m_data) {
10058 j2k_tcp_destroy(l_tcp);
10062 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
10064 l_tcp->m_data_size,
10066 p_j2k->cstr_index) ) {
10067 j2k_tcp_destroy(l_tcp);
10068 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
10072 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
10076 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
10077 * we destroy just the data which will be re-read in read_tile_header*/
10078 /*j2k_tcp_destroy(l_tcp);
10079 p_j2k->m_tcd->tcp = 0;*/
10080 j2k_tcp_data_destroy(l_tcp);
10082 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
10083 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
10085 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
10086 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
10087 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
10091 opj_read_bytes(l_data,&l_current_marker,2);
10093 if (l_current_marker == J2K_MS_EOC) {
10094 p_j2k->m_current_tile_number = 0;
10095 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
10097 else if (l_current_marker != J2K_MS_SOT)
10099 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
10108 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
10110 OPJ_UINT32 i,j,k = 0;
10111 OPJ_UINT32 l_width_src,l_height_src;
10112 OPJ_UINT32 l_width_dest,l_height_dest;
10113 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
10114 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
10115 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
10116 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
10117 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
10119 opj_image_comp_t * l_img_comp_src = 00;
10120 opj_image_comp_t * l_img_comp_dest = 00;
10122 opj_tcd_tilecomp_v2_t * l_tilec = 00;
10123 opj_image_t * l_image_src = 00;
10124 OPJ_UINT32 l_size_comp, l_remaining;
10125 OPJ_INT32 * l_dest_ptr;
10126 opj_tcd_resolution_v2_t* l_res= 00;
10128 l_tilec = p_tcd->tcd_image->tiles->comps;
10129 l_image_src = p_tcd->image;
10130 l_img_comp_src = l_image_src->comps;
10132 l_img_comp_dest = p_output_image->comps;
10134 for (i=0; i<l_image_src->numcomps; i++) {
10136 /* Allocate output component buffer if necessary */
10137 if (!l_img_comp_dest->data) {
10139 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
10140 if (! l_img_comp_dest->data) {
10145 /* Copy info from decoded comp image to output image */
10146 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
10149 /* Compute the precision of the output buffer */
10150 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
10151 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
10152 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
10158 if (l_size_comp == 3) {
10163 /* Current tile component size*/
10165 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
10166 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
10169 l_width_src = (l_res->x1 - l_res->x0);
10170 l_height_src = (l_res->y1 - l_res->y0);
10172 /* Border of the current output component*/
10173 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
10174 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
10175 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
10176 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
10179 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
10180 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
10184 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
10185 * of the input buffer (decoded tile component) which will be move
10186 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
10187 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
10188 * by this input area.
10190 assert( l_res->x0 >= 0);
10191 assert( l_res->x1 >= 0);
10192 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
10193 l_start_x_dest = l_res->x0 - l_x0_dest;
10194 l_offset_x0_src = 0;
10196 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
10197 l_width_dest = l_width_src;
10198 l_offset_x1_src = 0;
10201 l_width_dest = l_x1_dest - l_res->x0 ;
10202 l_offset_x1_src = l_width_src - l_width_dest;
10206 l_start_x_dest = 0 ;
10207 l_offset_x0_src = l_x0_dest - l_res->x0;
10209 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
10210 l_width_dest = l_width_src - l_offset_x0_src;
10211 l_offset_x1_src = 0;
10214 l_width_dest = l_img_comp_dest->w ;
10215 l_offset_x1_src = l_res->x1 - l_x1_dest;
10219 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
10220 l_start_y_dest = l_res->y0 - l_y0_dest;
10221 l_offset_y0_src = 0;
10223 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
10224 l_height_dest = l_height_src;
10225 l_offset_y1_src = 0;
10228 l_height_dest = l_y1_dest - l_res->y0 ;
10229 l_offset_y1_src = l_height_src - l_height_dest;
10233 l_start_y_dest = 0 ;
10234 l_offset_y0_src = l_y0_dest - l_res->y0;
10236 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
10237 l_height_dest = l_height_src - l_offset_y0_src;
10238 l_offset_y1_src = 0;
10241 l_height_dest = l_img_comp_dest->h ;
10242 l_offset_y1_src = l_res->y1 - l_y1_dest;
10246 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
10251 /* Compute the input buffer offset */
10252 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
10253 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
10254 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
10256 /* Compute the output buffer offset */
10257 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
10258 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
10260 /* Move the output buffer to the first place where we will write*/
10261 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
10264 fprintf(stdout, "COMPO[%d]:\n",i);
10265 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
10266 "\t tile offset:%d, %d, %d, %d\n"
10267 "\t buffer offset: %d; %d, %d\n",
10268 l_res->x0, l_res->y0, l_width_src, l_height_src,
10269 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
10270 l_start_offset_src, l_line_offset_src, l_end_offset_src);
10272 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
10273 "\t start offset: %d, line offset= %d\n",
10274 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
10278 switch (l_size_comp) {
10281 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
10282 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
10284 if (l_img_comp_src->sgnd) {
10285 for (j = 0 ; j < l_height_dest ; ++j) {
10286 for ( k = 0 ; k < l_width_dest ; ++k) {
10287 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
10290 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
10291 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
10295 for ( j = 0 ; j < l_height_dest ; ++j ) {
10296 for ( k = 0 ; k < l_width_dest ; ++k) {
10297 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
10300 l_dest_ptr+= l_line_offset_dest;
10301 l_src_ptr += l_line_offset_src;
10305 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
10306 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
10311 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
10312 l_src_ptr += l_start_offset_src;
10314 if (l_img_comp_src->sgnd) {
10315 for (j=0;j<l_height_dest;++j) {
10316 for (k=0;k<l_width_dest;++k) {
10317 *(l_dest_ptr++) = *(l_src_ptr++);
10320 l_dest_ptr+= l_line_offset_dest;
10321 l_src_ptr += l_line_offset_src ;
10325 for (j=0;j<l_height_dest;++j) {
10326 for (k=0;k<l_width_dest;++k) {
10327 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10330 l_dest_ptr+= l_line_offset_dest;
10331 l_src_ptr += l_line_offset_src ;
10335 l_src_ptr += l_end_offset_src;
10336 p_data = (OPJ_BYTE*) l_src_ptr;
10341 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
10342 l_src_ptr += l_start_offset_src;
10344 for (j=0;j<l_height_dest;++j) {
10345 for (k=0;k<l_width_dest;++k) {
10346 *(l_dest_ptr++) = (*(l_src_ptr++));
10349 l_dest_ptr+= l_line_offset_dest;
10350 l_src_ptr += l_line_offset_src ;
10353 l_src_ptr += l_end_offset_src;
10354 p_data = (OPJ_BYTE*) l_src_ptr;
10368 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
10370 * @param p_j2k the jpeg2000 codec.
10371 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
10372 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
10373 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
10374 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
10375 * @param p_manager the user event manager
10377 * @return true if the area could be set.
10379 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
10380 opj_image_t* p_image,
10381 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
10382 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
10383 struct opj_event_mgr * p_manager )
10385 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
10386 opj_image_t * l_image = p_j2k->m_private_image;
10388 OPJ_UINT32 it_comp;
10389 OPJ_INT32 l_comp_x1, l_comp_y1;
10390 opj_image_comp_t* l_img_comp = NULL;
10392 /* Check if we are read the main header */
10393 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
10394 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
10398 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
10399 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
10401 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10402 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10403 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10404 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10410 /* Check if the positions provided by the user are correct */
10413 assert(p_start_x >= 0 );
10414 assert(p_start_y >= 0 );
10416 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
10417 opj_event_msg_v2(p_manager, EVT_ERROR,
10418 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
10419 p_start_x, l_image->x1);
10422 else if ((OPJ_UINT32)p_start_x < l_image->x0){
10423 opj_event_msg_v2(p_manager, EVT_WARNING,
10424 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
10425 p_start_x, l_image->x0);
10426 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
10427 p_image->x0 = l_image->x0;
10430 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
10431 p_image->x0 = p_start_x;
10435 if ((OPJ_UINT32)p_start_y > l_image->y1){
10436 opj_event_msg_v2(p_manager, EVT_ERROR,
10437 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
10438 p_start_y, l_image->y1);
10441 else if ((OPJ_UINT32)p_start_y < l_image->y0){
10442 opj_event_msg_v2(p_manager, EVT_WARNING,
10443 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
10444 p_start_y, l_image->y0);
10445 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
10446 p_image->y0 = l_image->y0;
10449 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
10450 p_image->y0 = p_start_y;
10454 assert((OPJ_UINT32)p_end_x > 0);
10455 assert((OPJ_UINT32)p_end_y > 0);
10456 if ((OPJ_UINT32)p_end_x < l_image->x0) {
10457 opj_event_msg_v2(p_manager, EVT_ERROR,
10458 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
10459 p_end_x, l_image->x0);
10462 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
10463 opj_event_msg_v2(p_manager, EVT_WARNING,
10464 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
10465 p_end_x, l_image->x1);
10466 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
10467 p_image->x1 = l_image->x1;
10470 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
10471 p_image->x1 = p_end_x;
10475 if ((OPJ_UINT32)p_end_y < l_image->y0) {
10476 opj_event_msg_v2(p_manager, EVT_ERROR,
10477 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
10478 p_end_y, l_image->y0);
10481 if ((OPJ_UINT32)p_end_y > l_image->y1){
10482 opj_event_msg_v2(p_manager, EVT_WARNING,
10483 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
10484 p_end_y, l_image->y1);
10485 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
10486 p_image->y1 = l_image->y1;
10489 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
10490 p_image->y1 = p_end_y;
10494 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
10496 l_img_comp = p_image->comps;
10497 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
10501 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10502 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10503 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10504 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10506 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
10507 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10509 opj_event_msg_v2(p_manager, EVT_ERROR,
10510 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
10514 l_img_comp->w = l_w;
10516 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
10517 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10519 opj_event_msg_v2(p_manager, EVT_ERROR,
10520 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
10524 l_img_comp->h = l_h;
10529 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
10530 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
10537 /* ----------------------------------------------------------------------- */
10538 /* J2K / JPT decoder interface */
10539 /* ----------------------------------------------------------------------- */
10541 * Creates a J2K decompression structure.
10543 * @return a handle to a J2K decompressor if successful, NULL otherwise.
10545 opj_j2k_v2_t* j2k_create_decompress_v2(void)
10547 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
10551 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
10553 l_j2k->m_is_decoder = 1;
10554 l_j2k->m_cp.m_is_decoder = 1;
10556 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
10557 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
10558 j2k_destroy(l_j2k);
10561 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
10563 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
10564 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
10565 j2k_destroy(l_j2k);
10569 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
10571 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
10573 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
10575 /* codestream index creation */
10576 l_j2k->cstr_index = j2k_create_cstr_index();
10578 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
10579 if (!l_j2k->cstr_index){
10580 j2k_destroy(l_j2k);
10584 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
10587 /* validation list creation */
10588 l_j2k->m_validation_list = opj_procedure_list_create();
10589 if (! l_j2k->m_validation_list) {
10590 j2k_destroy(l_j2k);
10594 /* execution list creation */
10595 l_j2k->m_procedure_list = opj_procedure_list_create();
10596 if (! l_j2k->m_procedure_list) {
10597 j2k_destroy(l_j2k);
10605 opj_codestream_index_t* j2k_create_cstr_index(void)
10607 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
10608 opj_calloc(1,sizeof(opj_codestream_index_t));
10612 cstr_index->maxmarknum = 100;
10613 cstr_index->marknum = 0;
10614 cstr_index->marker = (opj_marker_info_t*)
10615 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
10616 if (!cstr_index-> marker)
10619 cstr_index->tile_index = NULL;
10626 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
10628 * @param p_tile_no the tile index.
10629 * @param p_comp_no the component being outputted.
10630 * @param p_j2k the J2K codec.
10632 * @return the number of bytes taken by the SPCod element.
10634 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
10635 OPJ_UINT32 p_tile_no,
10636 OPJ_UINT32 p_comp_no )
10638 opj_cp_v2_t *l_cp = 00;
10639 opj_tcp_v2_t *l_tcp = 00;
10640 opj_tccp_t *l_tccp = 00;
10642 /* preconditions */
10643 assert(p_j2k != 00);
10645 l_cp = &(p_j2k->m_cp);
10646 l_tcp = &l_cp->tcps[p_tile_no];
10647 l_tccp = &l_tcp->tccps[p_comp_no];
10649 /* preconditions again */
10650 assert(p_tile_no < (l_cp->tw * l_cp->th));
10651 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10653 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10654 return 5 + l_tccp->numresolutions;
10662 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10664 * @param p_comp_no the component number to output.
10665 * @param p_stream the stream to write data to.
10666 * @param p_j2k J2K codec.
10667 * @param p_manager the user event manager.
10670 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
10671 OPJ_UINT32 p_tile_no,
10672 OPJ_UINT32 p_comp_no,
10674 OPJ_UINT32 * p_header_size,
10675 struct opj_event_mgr * p_manager )
10678 opj_cp_v2_t *l_cp = 00;
10679 opj_tcp_v2_t *l_tcp = 00;
10680 opj_tccp_t *l_tccp = 00;
10682 /* preconditions */
10683 assert(p_j2k != 00);
10684 assert(p_header_size != 00);
10685 assert(p_manager != 00);
10686 assert(p_data != 00);
10688 l_cp = &(p_j2k->m_cp);
10689 l_tcp = &l_cp->tcps[p_tile_no];
10690 l_tccp = &l_tcp->tccps[p_comp_no];
10692 /* preconditions again */
10693 assert(p_tile_no < (l_cp->tw * l_cp->th));
10694 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
10696 if (*p_header_size < 5) {
10697 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
10701 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
10704 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
10707 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
10710 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
10713 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
10716 *p_header_size = *p_header_size - 5;
10718 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10720 if (*p_header_size < l_tccp->numresolutions) {
10721 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
10725 for (i = 0; i < l_tccp->numresolutions; ++i) {
10726 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
10730 *p_header_size = *p_header_size - l_tccp->numresolutions;
10737 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
10738 * @param p_header_data the data contained in the COM box.
10739 * @param p_j2k the jpeg2000 codec.
10740 * @param p_header_size the size of the data contained in the COM marker.
10741 * @param p_manager the user event manager.
10743 opj_bool j2k_read_SPCod_SPCoc(
10744 opj_j2k_v2_t *p_j2k,
10746 OPJ_BYTE * p_header_data,
10747 OPJ_UINT32 * p_header_size,
10748 struct opj_event_mgr * p_manager
10751 OPJ_UINT32 i, l_tmp;
10752 opj_cp_v2_t *l_cp = NULL;
10753 opj_tcp_v2_t *l_tcp = NULL;
10754 opj_tccp_t *l_tccp = NULL;
10755 OPJ_BYTE * l_current_ptr = NULL;
10757 /* preconditions */
10758 assert(p_j2k != 00);
10759 assert(p_manager != 00);
10760 assert(p_header_data != 00);
10762 l_cp = &(p_j2k->m_cp);
10763 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10764 &l_cp->tcps[p_j2k->m_current_tile_number] :
10765 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10767 /* precondition again */
10768 assert(compno < p_j2k->m_private_image->numcomps);
10770 l_tccp = &l_tcp->tccps[compno];
10771 l_current_ptr = p_header_data;
10773 /* make sure room is sufficient */
10774 if (*p_header_size < 5) {
10775 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10779 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10780 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10783 /* If user wants to remove more resolutions than the codestream contains, return error */
10784 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10785 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10786 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10787 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10791 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10793 l_tccp->cblkw += 2;
10795 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10797 l_tccp->cblkh += 2;
10799 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10802 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10805 *p_header_size = *p_header_size - 5;
10807 /* use custom precinct size ? */
10808 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10809 if (*p_header_size < l_tccp->numresolutions) {
10810 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10814 for (i = 0; i < l_tccp->numresolutions; ++i) {
10815 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10817 l_tccp->prcw[i] = l_tmp & 0xf;
10818 l_tccp->prch[i] = l_tmp >> 4;
10821 *p_header_size = *p_header_size - l_tccp->numresolutions;
10824 /* set default size for the precinct width and height */
10825 for (i = 0; i < l_tccp->numresolutions; ++i) {
10826 l_tccp->prcw[i] = 15;
10827 l_tccp->prch[i] = 15;
10831 #ifdef WIP_REMOVE_MSD
10833 if (p_j2k->cstr_info && compno == 0) {
10834 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10836 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10837 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10838 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10839 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10840 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10843 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10844 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10853 * Copies the tile component parameters of all the component from the first tile component.
10855 * @param p_j2k the J2k codec.
10857 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10861 opj_cp_v2_t *l_cp = NULL;
10862 opj_tcp_v2_t *l_tcp = NULL;
10863 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10864 OPJ_UINT32 l_prc_size;
10866 /* preconditions */
10867 assert(p_j2k != 00);
10869 l_cp = &(p_j2k->m_cp);
10870 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10871 &l_cp->tcps[p_j2k->m_current_tile_number] :
10872 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10874 l_ref_tccp = &l_tcp->tccps[0];
10875 l_copied_tccp = l_ref_tccp + 1;
10876 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10878 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10879 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10880 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10881 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10882 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10883 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10884 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10885 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10891 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10893 * @param p_tile_no the tile index.
10894 * @param p_comp_no the component being outputted.
10895 * @param p_j2k the J2K codec.
10897 * @return the number of bytes taken by the SPCod element.
10899 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10900 OPJ_UINT32 p_tile_no,
10901 OPJ_UINT32 p_comp_no )
10903 OPJ_UINT32 l_num_bands;
10905 opj_cp_v2_t *l_cp = 00;
10906 opj_tcp_v2_t *l_tcp = 00;
10907 opj_tccp_t *l_tccp = 00;
10909 /* preconditions */
10910 assert(p_j2k != 00);
10912 l_cp = &(p_j2k->m_cp);
10913 l_tcp = &l_cp->tcps[p_tile_no];
10914 l_tccp = &l_tcp->tccps[p_comp_no];
10916 /* preconditions again */
10917 assert(p_tile_no < l_cp->tw * l_cp->th);
10918 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10920 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10922 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10923 return 1 + l_num_bands;
10926 return 1 + 2*l_num_bands;
10931 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10933 * @param p_tile_no the tile to output.
10934 * @param p_comp_no the component number to output.
10935 * @param p_data the data buffer.
10936 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10937 * @param p_j2k J2K codec.
10938 * @param p_manager the user event manager.
10941 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10942 OPJ_UINT32 p_tile_no,
10943 OPJ_UINT32 p_comp_no,
10945 OPJ_UINT32 * p_header_size,
10946 struct opj_event_mgr * p_manager )
10948 OPJ_UINT32 l_header_size;
10949 OPJ_UINT32 l_band_no, l_num_bands;
10950 OPJ_UINT32 l_expn,l_mant;
10952 opj_cp_v2_t *l_cp = 00;
10953 opj_tcp_v2_t *l_tcp = 00;
10954 opj_tccp_t *l_tccp = 00;
10956 /* preconditions */
10957 assert(p_j2k != 00);
10958 assert(p_header_size != 00);
10959 assert(p_manager != 00);
10960 assert(p_data != 00);
10962 l_cp = &(p_j2k->m_cp);
10963 l_tcp = &l_cp->tcps[p_tile_no];
10964 l_tccp = &l_tcp->tccps[p_comp_no];
10966 /* preconditions again */
10967 assert(p_tile_no < l_cp->tw * l_cp->th);
10968 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10970 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10972 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10973 l_header_size = 1 + l_num_bands;
10975 if (*p_header_size < l_header_size) {
10976 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10980 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10983 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10984 l_expn = l_tccp->stepsizes[l_band_no].expn;
10985 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10990 l_header_size = 1 + 2*l_num_bands;
10992 if (*p_header_size < l_header_size) {
10993 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10997 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
11000 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
11001 l_expn = l_tccp->stepsizes[l_band_no].expn;
11002 l_mant = l_tccp->stepsizes[l_band_no].mant;
11004 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
11009 *p_header_size = *p_header_size - l_header_size;
11015 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
11017 * @param p_comp_no the component being targeted.
11018 * @param p_header_data the data contained in the COM box.
11019 * @param p_j2k the jpeg2000 codec.
11020 * @param p_header_size the size of the data contained in the COM marker.
11021 * @param p_manager the user event manager.
11023 opj_bool j2k_read_SQcd_SQcc(
11024 opj_j2k_v2_t *p_j2k,
11025 OPJ_UINT32 p_comp_no,
11026 OPJ_BYTE* p_header_data,
11027 OPJ_UINT32 * p_header_size,
11028 struct opj_event_mgr * p_manager
11032 OPJ_UINT32 l_band_no;
11033 opj_cp_v2_t *l_cp = 00;
11034 opj_tcp_v2_t *l_tcp = 00;
11035 opj_tccp_t *l_tccp = 00;
11036 OPJ_BYTE * l_current_ptr = 00;
11037 OPJ_UINT32 l_tmp, l_num_band;
11040 assert(p_j2k != 00);
11041 assert(p_manager != 00);
11042 assert(p_header_data != 00);
11044 l_cp = &(p_j2k->m_cp);
11045 /* come from tile part header or main header ?*/
11046 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
11047 &l_cp->tcps[p_j2k->m_current_tile_number] :
11048 p_j2k->m_specific_param.m_decoder.m_default_tcp;
11050 /* precondition again*/
11051 assert(p_comp_no < p_j2k->m_private_image->numcomps);
11053 l_tccp = &l_tcp->tccps[p_comp_no];
11054 l_current_ptr = p_header_data;
11056 if (*p_header_size < 1) {
11057 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
11060 *p_header_size -= 1;
11062 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
11065 l_tccp->qntsty = l_tmp & 0x1f;
11066 l_tccp->numgbits = l_tmp >> 5;
11067 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
11071 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
11073 (*p_header_size) / 2;
11075 if( l_num_band > J2K_MAXBANDS ) {
11076 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
11077 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
11078 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
11079 /*return OPJ_FALSE;*/
11084 if (l_cp->correct) {
11086 /* if JPWL is on, we check whether there are too many subbands */
11087 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
11088 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
11089 "JPWL: bad number of subbands in Sqcx (%d)\n",
11091 if (!JPWL_ASSUME) {
11092 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
11095 /* we try to correct */
11097 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
11098 "- setting number of bands to %d => HYPOTHESIS!!!\n",
11103 #endif /* USE_JPWL */
11105 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
11106 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
11107 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
11109 if (l_band_no < J2K_MAXBANDS){
11110 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
11111 l_tccp->stepsizes[l_band_no].mant = 0;
11114 *p_header_size = *p_header_size - l_num_band;
11117 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
11118 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
11120 if (l_band_no < J2K_MAXBANDS){
11121 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
11122 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
11125 *p_header_size = *p_header_size - 2*l_num_band;
11128 /* Add Antonin : if scalar_derived -> compute other stepsizes */
11129 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
11130 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
11131 l_tccp->stepsizes[l_band_no].expn =
11132 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
11133 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
11134 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
11142 * Copies the tile component parameters of all the component from the first tile component.
11144 * @param p_j2k the J2k codec.
11146 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
11149 opj_cp_v2_t *l_cp = NULL;
11150 opj_tcp_v2_t *l_tcp = NULL;
11151 opj_tccp_t *l_ref_tccp = NULL;
11152 opj_tccp_t *l_copied_tccp = NULL;
11155 /* preconditions */
11156 assert(p_j2k != 00);
11158 l_cp = &(p_j2k->m_cp);
11159 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
11160 &l_cp->tcps[p_j2k->m_current_tile_number] :
11161 p_j2k->m_specific_param.m_decoder.m_default_tcp;
11163 l_ref_tccp = &l_tcp->tccps[0];
11164 l_copied_tccp = l_ref_tccp + 1;
11165 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
11167 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
11168 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
11169 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
11170 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
11176 * Dump some elements from the J2K decompression structure .
11178 *@param p_j2k the jpeg2000 codec.
11179 *@param flag flag to describe what elments are dump.
11180 *@param out_stream output stream where dump the elements.
11183 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
11185 /* Check if the flag is compatible with j2k file*/
11186 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
11187 fprintf(out_stream, "Wrong flag\n");
11191 /* Dump the image_header */
11192 if (flag & OPJ_IMG_INFO){
11193 if (p_j2k->m_private_image)
11194 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
11197 /* Dump the codestream info from main header */
11198 if (flag & OPJ_J2K_MH_INFO){
11199 j2k_dump_MH_info(p_j2k, out_stream);
11203 /* Dump the codestream info of the current tile */
11204 if (flag & OPJ_J2K_TH_INFO){
11208 /* Dump the codestream index from main header */
11209 if (flag & OPJ_J2K_MH_IND){
11210 j2k_dump_MH_index(p_j2k, out_stream);
11213 /* Dump the codestream index of the current tile */
11214 if (flag & OPJ_J2K_TH_IND){
11221 * Dump index elements of the codestream extract from the main header.
11223 *@param p_j2k the jpeg2000 codec.
11224 *@param out_stream output stream where dump the elements.
11227 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11229 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
11230 OPJ_UINT32 it_marker, it_tile, it_tile_part;
11232 fprintf(out_stream, "Codestream index from main header: {\n");
11234 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
11235 "\t Main header end position=%" PRIi64 "\n",
11236 cstr_index->main_head_start, cstr_index->main_head_end);
11238 fprintf(out_stream, "\t Marker list: {\n");
11240 if (cstr_index->marker){
11241 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
11242 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
11243 cstr_index->marker[it_marker].type,
11244 cstr_index->marker[it_marker].pos,
11245 cstr_index->marker[it_marker].len );
11249 fprintf(out_stream, "\t }\n");
11252 if (cstr_index->tile_index){
11254 /* Simple test to avoid to write empty information*/
11255 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
11256 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
11257 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
11260 if (l_acc_nb_of_tile_part)
11262 fprintf(out_stream, "\t Tile index: {\n");
11264 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
11265 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
11267 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
11269 if (cstr_index->tile_index[it_tile].tp_index){
11270 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
11271 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
11273 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
11274 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
11275 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
11279 if (cstr_index->tile_index[it_tile].marker){
11280 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
11281 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
11282 cstr_index->tile_index[it_tile].marker[it_marker].type,
11283 cstr_index->tile_index[it_tile].marker[it_marker].pos,
11284 cstr_index->tile_index[it_tile].marker[it_marker].len );
11288 fprintf(out_stream,"\t }\n");
11292 fprintf(out_stream,"}\n");
11297 * Dump info elements of the codestream extract from the main header.
11299 *@param p_j2k the jpeg2000 codec.
11300 *@param out_stream output stream where dump the elements.
11303 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
11305 opj_tcp_v2_t * l_default_tile=NULL;
11307 fprintf(out_stream, "Codestream info from main header: {\n");
11309 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
11310 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
11311 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
11313 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11314 if (l_default_tile)
11317 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
11319 fprintf(out_stream, "\t default tile {\n");
11320 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
11321 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
11322 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
11323 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
11325 for (compno = 0; compno < numcomps; compno++) {
11326 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11328 OPJ_INT32 bandno, numbands;
11331 fprintf(out_stream, "\t\t comp %d {\n", compno);
11332 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
11333 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
11334 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
11335 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
11336 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
11337 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
11339 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
11340 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
11341 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
11343 fprintf(out_stream, "\n");
11345 /* quantization style*/
11346 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
11347 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
11348 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
11349 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11350 for (bandno = 0; bandno < numbands; bandno++) {
11351 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
11352 l_tccp->stepsizes[bandno].expn);
11354 fprintf(out_stream, "\n");
11357 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
11359 fprintf(out_stream, "\t\t }\n");
11360 } /*end of component of default tile*/
11361 fprintf(out_stream, "\t }\n"); /*end of default tile*/
11365 fprintf(out_stream, "}\n");
11370 * Dump an image header structure.
11372 *@param img_header the image header to dump.
11373 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11374 *@param out_stream output stream where dump the elements.
11376 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
11380 if (dev_dump_flag){
11381 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
11385 fprintf(out_stream, "Image info {\n");
11386 tab[0] = '\t';tab[1] = '\0';
11389 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
11390 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
11391 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
11393 if (img_header->comps){
11395 for (compno = 0; compno < img_header->numcomps; compno++) {
11396 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
11397 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
11398 fprintf(out_stream,"%s}\n",tab);
11402 fprintf(out_stream, "}\n");
11406 * Dump a component image header structure.
11408 *@param comp_header the component image header to dump.
11409 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
11410 *@param out_stream output stream where dump the elements.
11412 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
11416 if (dev_dump_flag){
11417 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
11420 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
11423 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
11424 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
11425 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
11428 fprintf(out_stream, "}\n");
11433 * Get the codestream info from a JPEG2000 codec.
11435 *@param p_j2k the component image header to dump.
11437 *@return the codestream information extract from the jpg2000 codec
11439 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
11442 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
11443 opj_tcp_v2_t *l_default_tile;
11444 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
11446 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
11448 cstr_info->tx0 = p_j2k->m_cp.tx0;
11449 cstr_info->ty0 = p_j2k->m_cp.ty0;
11450 cstr_info->tdx = p_j2k->m_cp.tdx;
11451 cstr_info->tdy = p_j2k->m_cp.tdy;
11452 cstr_info->tw = p_j2k->m_cp.tw;
11453 cstr_info->th = p_j2k->m_cp.th;
11455 cstr_info->tile_info = NULL; /* Not fill from the main header*/
11457 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
11459 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
11460 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
11461 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
11462 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
11464 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
11466 for (compno = 0; compno < numcomps; compno++) {
11467 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
11468 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
11469 OPJ_INT32 bandno, numbands;
11472 l_tccp_info->csty = l_tccp->csty;
11473 l_tccp_info->numresolutions = l_tccp->numresolutions;
11474 l_tccp_info->cblkw = l_tccp->cblkw;
11475 l_tccp_info->cblkh = l_tccp->cblkh;
11476 l_tccp_info->cblksty = l_tccp->cblksty;
11477 l_tccp_info->qmfbid = l_tccp->qmfbid;
11478 if (l_tccp->numresolutions < J2K_MAXRLVLS)
11480 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
11481 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
11484 /* quantization style*/
11485 l_tccp_info->qntsty = l_tccp->qntsty;
11486 l_tccp_info->numgbits = l_tccp->numgbits;
11488 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
11489 if (numbands < J2K_MAXBANDS) {
11490 for (bandno = 0; bandno < numbands; bandno++) {
11491 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
11492 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
11497 l_tccp_info->roishift = l_tccp->roishift;
11505 * Get the codestream index from a JPEG2000 codec.
11507 *@param p_j2k the component image header to dump.
11509 *@return the codestream index extract from the jpg2000 codec
11511 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
11513 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
11514 opj_calloc(1,sizeof(opj_codestream_index_t));
11518 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
11519 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
11520 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
11522 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
11523 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
11524 if (!l_cstr_index->marker){
11525 opj_free( l_cstr_index);
11529 if (p_j2k->cstr_index->marker)
11530 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
11532 opj_free(l_cstr_index->marker);
11533 l_cstr_index->marker = NULL;
11536 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
11537 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
11538 if (!l_cstr_index->tile_index){
11539 opj_free( l_cstr_index->marker);
11540 opj_free( l_cstr_index);
11544 if (!p_j2k->cstr_index->tile_index){
11545 opj_free(l_cstr_index->tile_index);
11546 l_cstr_index->tile_index = NULL;
11549 OPJ_UINT32 it_tile = 0;
11550 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
11553 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
11555 l_cstr_index->tile_index[it_tile].marker =
11556 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
11558 if (!l_cstr_index->tile_index[it_tile].marker) {
11559 OPJ_UINT32 it_tile_free;
11561 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11562 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11565 opj_free( l_cstr_index->tile_index);
11566 opj_free( l_cstr_index->marker);
11567 opj_free( l_cstr_index);
11571 if (p_j2k->cstr_index->tile_index[it_tile].marker)
11572 memcpy( l_cstr_index->tile_index[it_tile].marker,
11573 p_j2k->cstr_index->tile_index[it_tile].marker,
11574 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
11576 opj_free(l_cstr_index->tile_index[it_tile].marker);
11577 l_cstr_index->tile_index[it_tile].marker = NULL;
11580 /* Tile part index*/
11581 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
11583 l_cstr_index->tile_index[it_tile].tp_index =
11584 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
11586 if(!l_cstr_index->tile_index[it_tile].tp_index){
11587 OPJ_UINT32 it_tile_free;
11589 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
11590 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
11591 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
11594 opj_free( l_cstr_index->tile_index);
11595 opj_free( l_cstr_index->marker);
11596 opj_free( l_cstr_index);
11600 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
11601 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
11602 p_j2k->cstr_index->tile_index[it_tile].tp_index,
11603 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
11606 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
11607 l_cstr_index->tile_index[it_tile].tp_index = NULL;
11610 /* Packet index (NOT USED)*/
11611 l_cstr_index->tile_index[it_tile].nb_packet = 0;
11612 l_cstr_index->tile_index[it_tile].packet_index = NULL;
11617 return l_cstr_index;
11620 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
11622 OPJ_UINT32 it_tile=0;
11624 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
11625 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
11626 if (!p_j2k->cstr_index->tile_index)
11629 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
11630 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
11631 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
11632 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
11633 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
11634 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
11644 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
11645 opj_stream_private_t *p_stream,
11646 opj_event_mgr_t * p_manager)
11648 opj_bool l_go_on = OPJ_TRUE;
11649 OPJ_UINT32 l_current_tile_no;
11650 OPJ_UINT32 l_data_size,l_max_data_size;
11651 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11652 OPJ_UINT32 l_nb_comps;
11653 OPJ_BYTE * l_current_data;
11655 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11656 if (! l_current_data) {
11659 l_max_data_size = 1000;
11664 if (! j2k_read_tile_header( p_j2k,
11665 &l_current_tile_no,
11667 &l_tile_x0, &l_tile_y0,
11668 &l_tile_x1, &l_tile_y1,
11673 opj_free(l_current_data);
11681 if (l_data_size > l_max_data_size) {
11682 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11683 if (! l_current_data) {
11684 opj_free(l_current_data);
11688 l_max_data_size = l_data_size;
11691 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11692 opj_free(l_current_data);
11695 opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
11697 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11698 opj_free(l_current_data);
11701 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
11705 opj_free(l_current_data);
11711 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
11713 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
11716 assert(p_j2k != 00);
11718 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
11719 /* DEVELOPER CORNER, add your custom procedures */
11724 * Read and decode one tile.
11726 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
11727 opj_stream_private_t *p_stream,
11728 opj_event_mgr_t * p_manager)
11730 opj_bool l_go_on = OPJ_TRUE;
11731 OPJ_UINT32 l_current_tile_no;
11732 OPJ_UINT32 l_tile_no_to_dec;
11733 OPJ_UINT32 l_data_size,l_max_data_size;
11734 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
11735 OPJ_UINT32 l_nb_comps;
11736 OPJ_BYTE * l_current_data;
11738 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11739 if (! l_current_data) {
11742 l_max_data_size = 1000;
11744 /*Allocate and initialize some elements of codestrem index if not already done*/
11745 if( !p_j2k->cstr_index->tile_index)
11747 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11748 opj_free(l_current_data);
11752 /* Move into the codestream to the first SOT used to decode the desired tile */
11753 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11754 if (p_j2k->cstr_index->tile_index)
11755 if(p_j2k->cstr_index->tile_index->tp_index)
11757 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11758 /* the index for this tile has not been built,
11759 * so move to the last SOT read */
11760 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11761 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11766 if (opj_stream_read_seek(p_stream, p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos+2, p_manager)) {
11767 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11771 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11772 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11773 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11777 if (! j2k_read_tile_header( p_j2k,
11778 &l_current_tile_no,
11780 &l_tile_x0, &l_tile_y0,
11781 &l_tile_x1, &l_tile_y1,
11786 opj_free(l_current_data);
11795 if (l_data_size > l_max_data_size) {
11796 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11797 if (! l_current_data) {
11798 opj_free(l_current_data);
11802 l_max_data_size = l_data_size;
11807 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11808 opj_free(l_current_data);
11811 opj_event_msg_v2(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
11813 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11814 opj_free(l_current_data);
11817 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11819 if(l_current_tile_no == l_tile_no_to_dec)
11821 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11822 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11823 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11829 opj_event_msg_v2(p_manager, EVT_WARNING, "Tile read, decode and updated is not the desired (%d vs %d).\n", l_current_tile_no, l_tile_no_to_dec);
11834 opj_free(l_current_data);
11841 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11843 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11846 assert(p_j2k != 00);
11848 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11849 /* DEVELOPER CORNER, add your custom procedures */
11855 * Decodes the tiles of the stream.
11857 opj_bool j2k_decode_v2( opj_j2k_v2_t * p_j2k,
11858 opj_stream_private_t * p_stream,
11859 opj_image_t * p_image,
11860 opj_event_mgr_t * p_manager)
11867 p_j2k->m_output_image = opj_image_create0();
11868 if (! (p_j2k->m_output_image)) {
11871 opj_copy_image_header(p_image, p_j2k->m_output_image);
11873 /* customization of the decoding */
11874 j2k_setup_decoding(p_j2k);
11876 /* Decode the codestream */
11877 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11878 opj_image_destroy(p_j2k->m_private_image);
11879 p_j2k->m_private_image = NULL;
11883 /* Move data and copy one information from codec to output image*/
11884 for (compno = 0; compno < p_image->numcomps; compno++) {
11885 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11886 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11887 p_j2k->m_output_image->comps[compno].data = NULL;
11895 * Get the decoded tile.
11897 * @param p_j2k the jpeg2000 codestream codec.
11898 * @param p_stream input_stream
11899 * @param p_image output image. .
11900 * @param p_manager the user event manager
11901 * @param tile_index index of the tile we want decode
11903 * @return true if succeed.
11905 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11906 opj_stream_private_t *p_stream,
11907 opj_image_t* p_image,
11908 struct opj_event_mgr * p_manager,
11909 OPJ_UINT32 tile_index )
11912 OPJ_UINT32 l_tile_x, l_tile_y;
11913 opj_image_comp_t* l_img_comp;
11916 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11920 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11921 opj_event_msg_v2(p_manager, EVT_ERROR, "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index, (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1);
11925 /* Compute the dimension of the desired tile*/
11926 l_tile_x = tile_index % p_j2k->m_cp.tw;
11927 l_tile_y = tile_index / p_j2k->m_cp.tw;
11929 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11930 if (p_image->x0 < p_j2k->m_private_image->x0)
11931 p_image->x0 = p_j2k->m_private_image->x0;
11932 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11933 if (p_image->x1 > p_j2k->m_private_image->x1)
11934 p_image->x1 = p_j2k->m_private_image->x1;
11936 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11937 if (p_image->y0 < p_j2k->m_private_image->y0)
11938 p_image->y0 = p_j2k->m_private_image->y0;
11939 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11940 if (p_image->y1 > p_j2k->m_private_image->y1)
11941 p_image->y1 = p_j2k->m_private_image->y1;
11943 l_img_comp = p_image->comps;
11944 for (compno=0; compno < p_image->numcomps; ++compno)
11946 OPJ_INT32 l_comp_x1, l_comp_y1;
11948 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11950 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11951 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11952 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11953 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11955 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11956 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11961 /* Destroy the previous output image*/
11962 if (p_j2k->m_output_image)
11963 opj_image_destroy(p_j2k->m_output_image);
11965 /* Create the ouput image from the information previously computed*/
11966 p_j2k->m_output_image = opj_image_create0();
11967 if (! (p_j2k->m_output_image)) {
11970 opj_copy_image_header(p_image, p_j2k->m_output_image);
11972 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11974 /* customization of the decoding */
11975 j2k_setup_decoding_tile(p_j2k);
11977 /* Decode the codestream */
11978 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11979 opj_image_destroy(p_j2k->m_private_image);
11980 p_j2k->m_private_image = NULL;
11984 /* Move data and copy one information from codec to output image*/
11985 for (compno = 0; compno < p_image->numcomps; compno++) {
11986 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11988 if (p_image->comps[compno].data)
11989 opj_free(p_image->comps[compno].data);
11991 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11993 p_j2k->m_output_image->comps[compno].data = NULL;
11999 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
12001 OPJ_UINT32 it_comp;
12003 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
12005 if (p_j2k->m_private_image) {
12006 if (p_j2k->m_private_image->comps) {
12007 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
12008 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
12009 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
12010 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
12011 if ( res_factor >= max_res){
12012 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
12015 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
12028 * Encodes all the tiles in a row.
12030 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
12031 opj_stream_private_t *p_stream,
12032 opj_event_mgr_t * p_manager )
12035 OPJ_UINT32 l_nb_tiles;
12036 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
12037 OPJ_BYTE * l_current_data;
12039 /* preconditions */
12040 assert(p_j2k != 00);
12041 assert(p_stream != 00);
12042 assert(p_manager != 00);
12044 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
12045 if (! l_current_data) {
12048 l_max_tile_size = 1000;
12050 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
12051 for (i=0;i<l_nb_tiles;++i) {
12052 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
12053 opj_free(l_current_data);
12057 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
12058 if (l_current_tile_size > l_max_tile_size) {
12059 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
12060 if (! l_current_data) {
12063 l_max_tile_size = l_current_tile_size;
12066 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
12068 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
12073 opj_free(l_current_data);
12078 * Ends the compression procedures and possibility add data to be read after the
12081 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
12082 opj_stream_private_t *p_stream,
12083 struct opj_event_mgr * p_manager)
12085 /* customization of the encoding */
12086 j2k_setup_end_compress(p_j2k);
12088 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
12098 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
12100 * @param p_j2k the jpeg2000 codec.
12101 * @param p_stream the stream object.
12102 * @param p_manager the user event manager.
12104 * @return true if the codec is valid.
12106 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
12107 opj_stream_private_t *p_stream,
12108 opj_image_t * p_image,
12109 opj_event_mgr_t * p_manager)
12111 /* preconditions */
12112 assert(p_j2k != 00);
12113 assert(p_stream != 00);
12114 assert(p_manager != 00);
12116 p_j2k->m_private_image = opj_image_create0();
12117 opj_copy_image_header(p_image, p_j2k->m_private_image);
12119 // TODO_MSD: Find a better way
12120 if (p_image->comps) {
12121 OPJ_UINT32 it_comp;
12122 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
12123 if (p_image->comps[it_comp].data) {
12124 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
12125 p_image->comps[it_comp].data = NULL;
12131 /* customization of the validation */
12132 j2k_setup_encoding_validation (p_j2k);
12134 /* validation of the parameters codec */
12135 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
12139 /* customization of the encoding */
12140 j2k_setup_header_writting(p_j2k);
12143 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
12153 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
12154 OPJ_UINT32 p_tile_index,
12155 opj_stream_private_t *p_stream,
12156 opj_event_mgr_t * p_manager )
12159 if (p_tile_index != p_j2k->m_current_tile_number) {
12160 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
12164 opj_event_msg_v2(p_manager, EVT_INFO, "tile number %d / %d\n", p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th);
12166 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
12167 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
12168 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12170 /* initialisation before tile encoding */
12171 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
12181 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
12183 OPJ_UINT32 i,j,k = 0;
12184 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
12185 opj_image_comp_t * l_img_comp = 00;
12186 opj_tcd_tilecomp_v2_t * l_tilec = 00;
12187 opj_image_t * l_image = 00;
12188 OPJ_UINT32 l_size_comp, l_remaining;
12189 OPJ_INT32 * l_src_ptr;
12190 l_tilec = p_tcd->tcd_image->tiles->comps;
12191 l_image = p_tcd->image;
12192 l_img_comp = l_image->comps;
12194 for (i=0;i<p_tcd->image->numcomps;++i) {
12195 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
12196 l_remaining = l_img_comp->prec & 7; /* (%8) */
12201 if (l_size_comp == 3) {
12205 l_width = (l_tilec->x1 - l_tilec->x0);
12206 l_height = (l_tilec->y1 - l_tilec->y0);
12207 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
12208 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
12209 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
12210 l_stride = l_image_width - l_width;
12211 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
12213 switch (l_size_comp) {
12216 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
12217 if (l_img_comp->sgnd) {
12218 for (j=0;j<l_height;++j) {
12219 for (k=0;k<l_width;++k) {
12220 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
12224 l_src_ptr += l_stride;
12228 for (j=0;j<l_height;++j) {
12229 for (k=0;k<l_width;++k) {
12230 *(l_dest_ptr) = (*l_src_ptr)&0xff;
12234 l_src_ptr += l_stride;
12238 p_data = (OPJ_BYTE*) l_dest_ptr;
12243 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
12244 if (l_img_comp->sgnd) {
12245 for (j=0;j<l_height;++j) {
12246 for (k=0;k<l_width;++k) {
12247 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
12249 l_src_ptr += l_stride;
12253 for (j=0;j<l_height;++j) {
12254 for (k=0;k<l_width;++k) {
12255 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
12257 l_src_ptr += l_stride;
12261 p_data = (OPJ_BYTE*) l_dest_ptr;
12266 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
12267 for (j=0;j<l_height;++j) {
12268 for (k=0;k<l_width;++k) {
12269 *(l_dest_ptr++) = *(l_src_ptr++);
12271 l_src_ptr += l_stride;
12274 p_data = (OPJ_BYTE*) l_dest_ptr;
12287 * @param p_j2k the jpeg2000 codec.
12288 * @param p_stream the stream to write data to.
12289 * @param p_manager the user event manager.
12291 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
12293 OPJ_UINT32 p_data_size,
12294 opj_stream_private_t *p_stream,
12295 opj_event_mgr_t * p_manager )
12297 opj_tcd_v2_t * l_tcd = 00;
12298 opj_cp_v2_t * l_cp = 00;
12299 opj_tcp_v2_t * l_tcp = 00;
12300 OPJ_UINT32 l_nb_bytes_written;
12301 OPJ_BYTE * l_current_data = 00;
12302 OPJ_UINT32 l_tile_size = 0;
12303 OPJ_UINT32 l_available_data;
12305 /* preconditions */
12306 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12308 l_tcd = p_j2k->m_tcd;
12309 l_cp = &(p_j2k->m_cp);
12310 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12312 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
12313 l_available_data = l_tile_size;
12314 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
12316 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
12317 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
12321 l_nb_bytes_written = 0;
12322 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12325 l_current_data += l_nb_bytes_written;
12326 l_available_data -= l_nb_bytes_written;
12328 l_nb_bytes_written = 0;
12329 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
12333 l_available_data -= l_nb_bytes_written;
12334 l_nb_bytes_written = l_tile_size - l_available_data;
12336 if ( opj_stream_write_data( p_stream,
12337 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
12338 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
12342 ++p_j2k->m_current_tile_number;
12349 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12350 * are valid. Developers wanting to extend the library can add their own validation procedures.
12352 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
12354 /* preconditions */
12355 assert(p_j2k != 00);
12357 /* DEVELOPER CORNER, insert your custom procedures */
12358 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
12360 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12361 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
12364 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
12365 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
12366 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
12370 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
12371 * are valid. Developers wanting to extend the library can add their own validation procedures.
12373 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
12375 /* preconditions */
12376 assert(p_j2k != 00);
12378 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
12379 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
12381 /* DEVELOPER CORNER, add your custom validation procedure */
12382 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
12387 * Sets up the procedures to do on writing header.
12388 * Developers wanting to extend the library can add their own writing procedures.
12390 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
12392 /* preconditions */
12393 assert(p_j2k != 00);
12395 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
12396 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
12397 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
12398 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
12399 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
12402 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
12403 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
12404 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
12406 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
12407 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
12411 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
12413 if (p_j2k->m_cp.comment != 00) {
12414 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
12417 /* DEVELOPER CORNER, insert your custom procedures */
12418 if (p_j2k->m_cp.rsiz & MCT) {
12419 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
12421 /* End of Developer Corner */
12423 if (p_j2k->cstr_index) {
12424 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
12427 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
12428 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
12432 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
12434 OPJ_UINT32 * p_data_written,
12435 OPJ_UINT32 p_total_data_size,
12436 opj_stream_private_t *p_stream,
12437 struct opj_event_mgr * p_manager )
12440 OPJ_UINT32 l_nb_bytes_written = 0;
12441 OPJ_UINT32 l_current_nb_bytes_written;
12442 OPJ_BYTE * l_begin_data = 00;
12444 opj_tcp_v2_t *l_tcp = 00;
12445 opj_tcd_v2_t * l_tcd = 00;
12446 opj_cp_v2_t * l_cp = 00;
12448 l_tcd = p_j2k->m_tcd;
12449 l_cp = &(p_j2k->m_cp);
12450 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12452 l_tcd->cur_pino = 0;
12454 /*Get number of tile parts*/
12455 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
12460 l_current_nb_bytes_written = 0;
12461 l_begin_data = p_data;
12462 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
12467 l_nb_bytes_written += l_current_nb_bytes_written;
12468 p_data += l_current_nb_bytes_written;
12469 p_total_data_size -= l_current_nb_bytes_written;
12471 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
12472 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
12473 l_current_nb_bytes_written = 0;
12474 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12475 l_nb_bytes_written += l_current_nb_bytes_written;
12476 p_data += l_current_nb_bytes_written;
12477 p_total_data_size -= l_current_nb_bytes_written;
12479 l_current_nb_bytes_written = 0;
12480 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
12481 l_nb_bytes_written += l_current_nb_bytes_written;
12482 p_data += l_current_nb_bytes_written;
12483 p_total_data_size -= l_current_nb_bytes_written;
12486 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
12487 l_current_nb_bytes_written = 0;
12488 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
12489 l_nb_bytes_written += l_current_nb_bytes_written;
12490 p_data += l_current_nb_bytes_written;
12491 p_total_data_size -= l_current_nb_bytes_written;
12495 l_current_nb_bytes_written = 0;
12496 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12500 l_nb_bytes_written += l_current_nb_bytes_written;
12501 * p_data_written = l_nb_bytes_written;
12503 /* Writing Psot in SOT marker */
12504 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
12506 if (l_cp->m_specific_param.m_enc.m_cinema){
12507 j2k_update_tlm(p_j2k,l_nb_bytes_written);
12513 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
12515 OPJ_UINT32 * p_data_written,
12516 OPJ_UINT32 p_total_data_size,
12517 opj_stream_private_t *p_stream,
12518 struct opj_event_mgr * p_manager
12521 OPJ_UINT32 tilepartno=0;
12522 OPJ_UINT32 l_nb_bytes_written = 0;
12523 OPJ_UINT32 l_current_nb_bytes_written;
12524 OPJ_UINT32 l_part_tile_size;
12525 OPJ_UINT32 tot_num_tp;
12528 OPJ_BYTE * l_begin_data;
12529 opj_tcp_v2_t *l_tcp = 00;
12530 opj_tcd_v2_t * l_tcd = 00;
12531 opj_cp_v2_t * l_cp = 00;
12534 l_tcd = p_j2k->m_tcd;
12535 l_cp = &(p_j2k->m_cp);
12536 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
12538 /*Get number of tile parts*/
12539 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
12541 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
12542 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12543 l_current_nb_bytes_written = 0;
12544 l_part_tile_size = 0;
12545 l_begin_data = p_data;
12547 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12551 l_nb_bytes_written += l_current_nb_bytes_written;
12552 p_data += l_current_nb_bytes_written;
12553 p_total_data_size -= l_current_nb_bytes_written;
12554 l_part_tile_size += l_nb_bytes_written;
12556 l_current_nb_bytes_written = 0;
12557 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12561 p_data += l_current_nb_bytes_written;
12562 l_nb_bytes_written += l_current_nb_bytes_written;
12563 p_total_data_size -= l_current_nb_bytes_written;
12564 l_part_tile_size += l_nb_bytes_written;
12566 /* Writing Psot in SOT marker */
12567 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12569 if (l_cp->m_specific_param.m_enc.m_cinema) {
12570 j2k_update_tlm(p_j2k,l_part_tile_size);
12573 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12576 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
12577 l_tcd->cur_pino = pino;
12579 /*Get number of tile parts*/
12580 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
12581 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
12582 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
12583 l_current_nb_bytes_written = 0;
12584 l_part_tile_size = 0;
12585 l_begin_data = p_data;
12587 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
12591 l_nb_bytes_written += l_current_nb_bytes_written;
12592 p_data += l_current_nb_bytes_written;
12593 p_total_data_size -= l_current_nb_bytes_written;
12594 l_part_tile_size += l_current_nb_bytes_written;
12596 l_current_nb_bytes_written = 0;
12598 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
12602 l_nb_bytes_written += l_current_nb_bytes_written;
12603 p_data += l_current_nb_bytes_written;
12604 p_total_data_size -= l_current_nb_bytes_written;
12605 l_part_tile_size += l_current_nb_bytes_written;
12607 /* Writing Psot in SOT marker */
12608 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
12610 if (l_cp->m_specific_param.m_enc.m_cinema) {
12611 j2k_update_tlm(p_j2k,l_part_tile_size);
12614 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
12618 *p_data_written = l_nb_bytes_written;
12624 * Writes the updated tlm.
12626 * @param p_stream the stream to write data to.
12627 * @param p_j2k J2K codec.
12628 * @param p_manager the user event manager.
12630 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
12631 struct opj_stream_private *p_stream,
12632 struct opj_event_mgr * p_manager )
12634 OPJ_UINT32 l_tlm_size;
12635 OPJ_SIZE_T l_tlm_position, l_current_position;
12637 /* preconditions */
12638 assert(p_j2k != 00);
12639 assert(p_manager != 00);
12640 assert(p_stream != 00);
12642 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
12643 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
12644 l_current_position = opj_stream_tell(p_stream);
12646 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
12650 if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer,l_tlm_size,p_manager) != l_tlm_size) {
12654 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
12663 * Ends the encoding, i.e. frees memory.
12665 * @param p_stream the stream to write data to.
12666 * @param p_j2k J2K codec.
12667 * @param p_manager the user event manager.
12669 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
12670 struct opj_stream_private *p_stream,
12671 struct opj_event_mgr * p_manager )
12673 /* preconditions */
12674 assert(p_j2k != 00);
12675 assert(p_manager != 00);
12676 assert(p_stream != 00);
12678 tcd_destroy_v2(p_j2k->m_tcd);
12681 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
12682 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
12683 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
12684 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
12687 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
12688 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
12689 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
12692 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
12698 * Destroys the memory associated with the decoding of headers.
12700 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
12701 opj_stream_private_t *p_stream,
12702 opj_event_mgr_t * p_manager )
12704 /* preconditions */
12705 assert(p_j2k != 00);
12706 assert(p_stream != 00);
12707 assert(p_manager != 00);
12709 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
12710 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
12711 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
12714 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
12723 * @param p_stream the stream to write data to.
12724 * @param p_j2k J2K codec.
12725 * @param p_manager the user event manager.
12727 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
12728 struct opj_stream_private *p_stream,
12729 struct opj_event_mgr * p_manager )
12731 opj_codestream_info_t * l_cstr_info = 00;
12733 /* preconditions */
12734 assert(p_j2k != 00);
12735 assert(p_manager != 00);
12736 assert(p_stream != 00);
12739 /* TODO mergeV2: check this part which use cstr_info */
12740 /*l_cstr_info = p_j2k->cstr_info;
12744 l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t));
12746 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12747 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12749 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12751 l_cstr_info->tw = p_j2k->m_cp.tw;
12752 l_cstr_info->th = p_j2k->m_cp.th;
12754 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12755 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12756 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12757 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12759 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12761 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12763 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12765 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12766 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12769 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12771 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12773 /*l_cstr_info->maxmarknum = 100;
12774 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12775 l_cstr_info->marknum = 0;
12778 return j2k_calculate_tp_v2(p_j2k,&(p_j2k->m_cp),&p_j2k->m_specific_param.m_encoder.m_total_tile_parts,p_j2k->m_private_image,p_manager);
12782 * Creates a tile-coder decoder.
12784 * @param p_stream the stream to write data to.
12785 * @param p_j2k J2K codec.
12786 * @param p_manager the user event manager.
12788 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12789 struct opj_stream_private *p_stream,
12790 struct opj_event_mgr * p_manager )
12792 /* preconditions */
12793 assert(p_j2k != 00);
12794 assert(p_manager != 00);
12795 assert(p_stream != 00);
12797 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12799 if (! p_j2k->m_tcd) {
12800 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12804 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12805 tcd_destroy_v2(p_j2k->m_tcd);
12816 * @param p_j2k the jpeg2000 codec.
12817 * @param p_stream the stream to write data to.
12818 * @param p_manager the user event manager.
12820 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12821 OPJ_UINT32 p_tile_index,
12823 OPJ_UINT32 p_data_size,
12824 opj_stream_private_t *p_stream,
12825 opj_event_mgr_t * p_manager )
12827 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12828 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12832 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12833 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);