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 * Reads a SOC marker (Start of Codestream)
397 * @param p_header_data the data contained in the SOC box.
398 * @param jp2 the jpeg2000 file codec.
399 * @param p_header_size the size of the data contained in the SOC marker.
400 * @param p_manager the user event manager.
402 static opj_bool opj_j2k_read_soc(
404 struct opj_stream_private *p_stream,
405 struct opj_event_mgr * p_manager
409 Write the SIZ marker (image and tile size)
410 @param j2k J2K handle
412 static void j2k_write_siz(opj_j2k_t *j2k);
415 * Writes the SIZ marker (image and tile size)
417 * @param p_stream the stream to write data to.
418 * @param p_j2k J2K codec.
419 * @param p_manager the user event manager.
421 static opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
422 struct opj_stream_private *p_stream,
423 struct opj_event_mgr * p_manager );
426 * Reads a SIZ marker (image and tile size)
427 * @param p_header_data the data contained in the SIZ box.
428 * @param jp2 the jpeg2000 file codec.
429 * @param p_header_size the size of the data contained in the SIZ marker.
430 * @param p_manager the user event manager.
432 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
433 OPJ_BYTE * p_header_data,
434 OPJ_UINT32 p_header_size,
435 opj_event_mgr_t * p_manager);
438 Write the COM marker (comment)
439 @param j2k J2K handle
441 static void j2k_write_com(opj_j2k_t *j2k);
444 * Writes the COM marker (comment)
446 * @param p_stream the stream to write data to.
447 * @param p_j2k J2K codec.
448 * @param p_manager the user event manager.
450 static opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
451 struct opj_stream_private *p_stream,
452 struct opj_event_mgr * p_manager );
455 Read the COM marker (comment)
456 @param j2k J2K handle
458 static void j2k_read_com(opj_j2k_t *j2k);
460 * Reads a COM marker (comments)
461 * @param p_header_data the data contained in the COM box.
462 * @param jp2 the jpeg2000 file codec.
463 * @param p_header_size the size of the data contained in the COM marker.
464 * @param p_manager the user event manager.
466 static opj_bool j2k_read_com_v2 (
468 OPJ_BYTE * p_header_data,
469 OPJ_UINT32 p_header_size,
470 struct opj_event_mgr * p_manager
473 Write the value concerning the specified component in the marker COD and COC
474 @param j2k J2K handle
475 @param compno Number of the component concerned by the information written
477 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
479 Read the value concerning the specified component in the marker COD and COC
480 @param j2k J2K handle
481 @param compno Number of the component concerned by the information read
483 static void j2k_read_cox(opj_j2k_t *j2k, int compno);
485 Write the COD marker (coding style default)
486 @param j2k J2K handle
488 static void j2k_write_cod(opj_j2k_t *j2k);
491 * Writes the COD marker (Coding style default)
493 * @param p_stream the stream to write data to.
494 * @param p_j2k J2K codec.
495 * @param p_manager the user event manager.
497 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
498 struct opj_stream_private *p_stream,
499 struct opj_event_mgr * p_manager );
502 * Reads a COD marker (Coding Styke defaults)
503 * @param p_header_data the data contained in the COD box.
504 * @param p_j2k the jpeg2000 codec.
505 * @param p_header_size the size of the data contained in the COD marker.
506 * @param p_manager the user event manager.
508 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
509 OPJ_BYTE * p_header_data,
510 OPJ_UINT32 p_header_size,
511 opj_event_mgr_t * p_manager);
514 Write the COC marker (coding style component)
515 @param j2k J2K handle
516 @param compno Number of the component concerned by the information written
518 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
521 * Writes the COC marker (Coding style component)
523 * @param p_comp_number the index of the component to output.
524 * @param p_stream the stream to write data to.
525 * @param p_j2k J2K codec.
526 * @param p_manager the user event manager.
528 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
529 OPJ_UINT32 p_comp_number,
530 struct opj_stream_private *p_stream,
531 struct opj_event_mgr * p_manager
534 * Writes the COC marker (Coding style component)
536 * @param p_comp_no the index of the component to output.
537 * @param p_stream the stream to write data to.
538 * @param p_j2k J2K codec.
539 * @param p_manager the user event manager.
541 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
542 OPJ_UINT32 p_comp_no,
544 OPJ_UINT32 * p_data_written,
545 struct opj_event_mgr * p_manager );
548 * Gets the maximum size taken by a coc.
550 * @param p_j2k the jpeg2000 codec to use.
552 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
556 * Reads a COC marker (Coding Style Component)
557 * @param p_header_data the data contained in the COC box.
558 * @param p_j2k the jpeg2000 codec.
559 * @param p_header_size the size of the data contained in the COC marker.
560 * @param p_manager the user event manager.
562 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
563 OPJ_BYTE * p_header_data,
564 OPJ_UINT32 p_header_size,
565 opj_event_mgr_t * p_manager );
568 Write the value concerning the specified component in the marker QCD and QCC
569 @param j2k J2K handle
570 @param compno Number of the component concerned by the information written
572 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
574 Read the value concerning the specified component in the marker QCD and QCC
575 @param j2k J2K handle
576 @param compno Number of the component concern by the information read
577 @param len Length of the information in the QCX part of the marker QCD/QCC
579 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
581 Write the QCD marker (quantization default)
582 @param j2k J2K handle
584 static void j2k_write_qcd(opj_j2k_t *j2k);
587 * Writes the QCD marker (quantization default)
589 * @param p_comp_number the index of the component to output.
590 * @param p_stream the stream to write data to.
591 * @param p_j2k J2K codec.
592 * @param p_manager the user event manager.
594 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
595 struct opj_stream_private *p_stream,
596 struct opj_event_mgr * p_manager );
599 * Reads a QCD marker (Quantization defaults)
600 * @param p_header_data the data contained in the QCD box.
601 * @param p_j2k the jpeg2000 codec.
602 * @param p_header_size the size of the data contained in the QCD marker.
603 * @param p_manager the user event manager.
605 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
606 OPJ_BYTE * p_header_data,
607 OPJ_UINT32 p_header_size,
608 opj_event_mgr_t * p_manager );
611 Write the QCC marker (quantization component)
612 @param j2k J2K handle
613 @param compno Number of the component concerned by the information written
615 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
618 * Writes the QCC marker (quantization component)
620 * @param p_comp_no the index of the component to output.
621 * @param p_stream the stream to write data to.
622 * @param p_j2k J2K codec.
623 * @param p_manager the user event manager.
625 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
626 OPJ_UINT32 p_comp_no,
627 struct opj_stream_private *p_stream,
628 struct opj_event_mgr * p_manager );
631 * Writes the QCC marker (quantization component)
633 * @param p_comp_no the index of the component to output.
634 * @param p_stream the stream to write data to.
635 * @param p_j2k J2K codec.
636 * @param p_manager the user event manager.
638 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
639 OPJ_UINT32 p_comp_no,
641 OPJ_UINT32 * p_data_written,
642 struct opj_event_mgr * p_manager );
645 * Gets the maximum size taken by a qcc.
647 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
650 * Reads a QCC marker (Quantization component)
651 * @param p_header_data the data contained in the QCC box.
652 * @param p_j2k the jpeg2000 codec.
653 * @param p_header_size the size of the data contained in the QCC marker.
654 * @param p_manager the user event manager.
656 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
657 OPJ_BYTE * p_header_data,
658 OPJ_UINT32 p_header_size,
659 opj_event_mgr_t * p_manager);
662 Write the POC marker (progression order change)
663 @param j2k J2K handle
665 static void j2k_write_poc(opj_j2k_t *j2k);
668 * Writes the POC marker (Progression Order Change)
670 * @param p_stream the stream to write data to.
671 * @param p_j2k J2K codec.
672 * @param p_manager the user event manager.
674 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
675 struct opj_stream_private *p_stream,
676 struct opj_event_mgr * p_manager );
679 * Writes the POC marker (Progression Order Change)
681 * @param p_stream the stream to write data to.
682 * @param p_j2k J2K codec.
683 * @param p_manager the user event manager.
685 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
687 OPJ_UINT32 * p_data_written,
688 struct opj_event_mgr * p_manager );
691 * Gets the maximum size taken by the writting of a POC.
693 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
696 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
698 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
701 * Gets the maximum size taken by the headers of the SOT.
703 * @param p_j2k the jpeg2000 codec to use.
705 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
708 Read the POC marker (progression order change)
709 @param j2k J2K handle
711 static void j2k_read_poc(opj_j2k_t *j2k);
713 * Reads a POC marker (Progression Order Change)
715 * @param p_header_data the data contained in the POC box.
716 * @param p_j2k the jpeg2000 codec.
717 * @param p_header_size the size of the data contained in the POC marker.
718 * @param p_manager the user event manager.
720 static opj_bool j2k_read_poc_v2 (
722 OPJ_BYTE * p_header_data,
723 OPJ_UINT32 p_header_size,
724 struct opj_event_mgr * p_manager
728 Read the CRG marker (component registration)
729 @param j2k J2K handle
731 static void j2k_read_crg(opj_j2k_t *j2k);
733 * Reads a CRG marker (Component registration)
735 * @param p_header_data the data contained in the TLM box.
736 * @param p_j2k the jpeg2000 codec.
737 * @param p_header_size the size of the data contained in the TLM marker.
738 * @param p_manager the user event manager.
740 static opj_bool j2k_read_crg_v2 (
742 OPJ_BYTE * p_header_data,
743 OPJ_UINT32 p_header_size,
744 struct opj_event_mgr * p_manager
747 Read the TLM marker (tile-part lengths)
748 @param j2k J2K handle
750 static void j2k_read_tlm(opj_j2k_t *j2k);
752 * Reads a TLM marker (Tile Length Marker)
754 * @param p_header_data the data contained in the TLM box.
755 * @param p_j2k the jpeg2000 codec.
756 * @param p_header_size the size of the data contained in the TLM marker.
757 * @param p_manager the user event manager.
759 static opj_bool j2k_read_tlm_v2 (
761 OPJ_BYTE * p_header_data,
762 OPJ_UINT32 p_header_size,
763 struct opj_event_mgr * p_manager
767 * Writes the updated tlm.
769 * @param p_stream the stream to write data to.
770 * @param p_j2k J2K codec.
771 * @param p_manager the user event manager.
773 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
774 struct opj_stream_private *p_stream,
775 struct opj_event_mgr * p_manager );
778 Read the PLM marker (packet length, main header)
779 @param j2k J2K handle
781 static void j2k_read_plm(opj_j2k_t *j2k);
784 * Reads a PLM marker (Packet length, main header marker)
786 * @param p_header_data the data contained in the TLM box.
787 * @param p_j2k the jpeg2000 codec.
788 * @param p_header_size the size of the data contained in the TLM marker.
789 * @param p_manager the user event manager.
791 static opj_bool j2k_read_plm_v2 (
793 OPJ_BYTE * p_header_data,
794 OPJ_UINT32 p_header_size,
795 struct opj_event_mgr * p_manager
798 Read the PLT marker (packet length, tile-part header)
799 @param j2k J2K handle
801 static void j2k_read_plt(opj_j2k_t *j2k);
803 * Reads a PLT marker (Packet length, tile-part header)
805 * @param p_header_data the data contained in the PLT box.
806 * @param p_j2k the jpeg2000 codec.
807 * @param p_header_size the size of the data contained in the PLT marker.
808 * @param p_manager the user event manager.
810 static opj_bool j2k_read_plt_v2 (
812 OPJ_BYTE * p_header_data,
813 OPJ_UINT32 p_header_size,
814 struct opj_event_mgr * p_manager
817 Read the PPM marker (packet packet headers, main header)
818 @param j2k J2K handle
820 static void j2k_read_ppm(opj_j2k_t *j2k);
822 * Reads a PPM marker (Packed packet headers, main header)
824 * @param p_header_data the data contained in the POC box.
825 * @param p_j2k the jpeg2000 codec.
826 * @param p_header_size the size of the data contained in the POC marker.
827 * @param p_manager the user event manager.
830 static opj_bool j2k_read_ppm_v2 (
832 OPJ_BYTE * p_header_data,
833 OPJ_UINT32 p_header_size,
834 struct opj_event_mgr * p_manager
838 static opj_bool j2k_read_ppm_v3 (
840 OPJ_BYTE * p_header_data,
841 OPJ_UINT32 p_header_size,
842 struct opj_event_mgr * p_manager
846 Read the PPT marker (packet packet headers, tile-part header)
847 @param j2k J2K handle
849 static void j2k_read_ppt(opj_j2k_t *j2k);
851 * Reads a PPT marker (Packed packet headers, tile-part header)
853 * @param p_header_data the data contained in the PPT box.
854 * @param p_j2k the jpeg2000 codec.
855 * @param p_header_size the size of the data contained in the PPT marker.
856 * @param p_manager the user event manager.
858 static opj_bool j2k_read_ppt_v2 (
860 OPJ_BYTE * p_header_data,
861 OPJ_UINT32 p_header_size,
862 struct opj_event_mgr * p_manager
865 Write the TLM marker (Mainheader)
866 @param j2k J2K handle
868 static void j2k_write_tlm(opj_j2k_t *j2k);
871 * Writes the TLM marker (Tile Length Marker)
873 * @param p_stream the stream to write data to.
874 * @param p_j2k J2K codec.
875 * @param p_manager the user event manager.
877 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
878 struct opj_stream_private *p_stream,
879 struct opj_event_mgr * p_manager );
882 Write the SOT marker (start of tile-part)
883 @param j2k J2K handle
885 static void j2k_write_sot(opj_j2k_t *j2k);
888 * Writes the SOT marker (Start of tile-part)
890 * @param p_stream the stream to write data to.
891 * @param p_j2k J2K codec.
892 * @param p_manager the user event manager.
894 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
896 OPJ_UINT32 * p_data_written,
897 const struct opj_stream_private *p_stream,
898 struct opj_event_mgr * p_manager );
901 * Reads a PPT marker (Packed packet headers, tile-part header)
903 * @param p_header_data the data contained in the PPT box.
904 * @param p_j2k the jpeg2000 codec.
905 * @param p_header_size the size of the data contained in the PPT marker.
906 * @param p_manager the user event manager.
908 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
909 OPJ_BYTE * p_header_data,
910 OPJ_UINT32 p_header_size,
911 opj_event_mgr_t * p_manager );
914 Write the SOD marker (start of data)
915 @param j2k J2K handle
916 @param tile_coder Pointer to a TCD handle
918 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
921 * Writes the SOD marker (Start of data)
923 * @param p_stream the stream to write data to.
924 * @param p_j2k J2K codec.
925 * @param p_manager the user event manager.
927 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
928 struct opj_tcd_v2 * p_tile_coder,
930 OPJ_UINT32 * p_data_written,
931 OPJ_UINT32 p_total_data_size,
932 const struct opj_stream_private *p_stream,
933 struct opj_event_mgr * p_manager );
936 * Reads a SOD marker (Start Of Data)
938 * @param p_header_data the data contained in the SOD box.
939 * @param p_j2k the jpeg2000 codec.
940 * @param p_header_size the size of the data contained in the SOD marker.
941 * @param p_manager the user event manager.
943 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
944 opj_stream_private_t *p_stream,
945 opj_event_mgr_t * p_manager );
948 * Updates the Tile Length Marker.
950 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
952 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
953 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
955 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
956 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
960 Write the RGN marker (region-of-interest)
961 @param j2k J2K handle
962 @param compno Number of the component concerned by the information written
963 @param tileno Number of the tile concerned by the information written
965 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
968 * Writes the RGN marker (Region Of Interest)
970 * @param p_tile_no the tile to output
971 * @param p_comp_no the component to output
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_rgn_v2( opj_j2k_v2_t *p_j2k,
977 OPJ_UINT32 p_tile_no,
978 OPJ_UINT32 p_comp_no,
979 struct opj_stream_private *p_stream,
980 struct opj_event_mgr * p_manager );
983 * Reads a RGN marker (Region Of Interest)
985 * @param p_header_data the data contained in the POC box.
986 * @param p_j2k the jpeg2000 codec.
987 * @param p_header_size the size of the data contained in the POC marker.
988 * @param p_manager the user event manager.
990 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
991 OPJ_BYTE * p_header_data,
992 OPJ_UINT32 p_header_size,
993 opj_event_mgr_t * p_manager );
996 * Writes the EOC marker (End of Codestream)
998 * @param p_stream the stream to write data to.
999 * @param p_j2k J2K codec.
1000 * @param p_manager the user event manager.
1002 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
1003 struct opj_stream_private *p_stream,
1004 struct opj_event_mgr * p_manager );
1007 Write the EOC marker (end of codestream)
1008 @param j2k J2K handle
1010 static void j2k_write_eoc(opj_j2k_t *j2k);
1012 Read the EOC marker (end of codestream)
1013 @param j2k J2K handle
1015 static void j2k_read_eoc(opj_j2k_t *j2k);
1018 * Reads a EOC marker (End Of Codestream)
1020 * @param p_header_data the data contained in the SOD box.
1021 * @param p_j2k the jpeg2000 codec.
1022 * @param p_header_size the size of the data contained in the SOD marker.
1023 * @param p_manager the user event manager.
1026 static opj_bool j2k_read_eoc_v2 (
1027 opj_j2k_v2_t *p_j2k,
1028 struct opj_stream_private *p_stream,
1029 struct opj_event_mgr * p_manager
1036 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1038 * @param p_stream the stream to write data to.
1039 * @param p_j2k J2K codec.
1040 * @param p_manager the user event manager.
1042 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1043 struct opj_stream_private *p_stream,
1044 struct opj_event_mgr * p_manager );
1049 * @param p_stream the stream to write data to.
1050 * @param p_j2k J2K codec.
1051 * @param p_manager the user event manager.
1053 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1054 struct opj_stream_private *p_stream,
1055 struct opj_event_mgr * p_manager );
1058 Read an unknown marker
1059 @param j2k J2K handle
1061 static void j2k_read_unk(opj_j2k_t *j2k);
1063 Add main header marker information
1064 @param cstr_info Codestream information structure
1065 @param type marker type
1066 @param pos byte offset of marker segment
1067 @param len length of marker segment
1069 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1071 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1073 Add tile header marker information
1074 @param tileno tile index number
1075 @param cstr_info Codestream information structure
1076 @param type marker type
1077 @param pos byte offset of marker segment
1078 @param len length of marker segment
1080 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1082 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);
1085 * Reads an unknown marker
1087 * @param p_stream the stream object to read from.
1088 * @param p_j2k the jpeg2000 codec.
1089 * @param p_manager the user event manager.
1091 * @return true if the marker could be deduced.
1093 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1094 struct opj_stream_private *p_stream,
1095 OPJ_UINT32 *output_marker,
1096 struct opj_event_mgr * p_manager );
1099 * Writes the MCT marker (Multiple Component Transform)
1101 * @param p_stream the stream to write data to.
1102 * @param p_j2k J2K codec.
1103 * @param p_manager the user event manager.
1105 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1106 opj_mct_data_t * p_mct_record,
1107 struct opj_stream_private *p_stream,
1108 struct opj_event_mgr * p_manager );
1111 * Reads a MCT marker (Multiple Component Transform)
1113 * @param p_header_data the data contained in the MCT box.
1114 * @param p_j2k the jpeg2000 codec.
1115 * @param p_header_size the size of the data contained in the MCT marker.
1116 * @param p_manager the user event manager.
1118 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1119 OPJ_BYTE * p_header_data,
1120 OPJ_UINT32 p_header_size,
1121 struct opj_event_mgr * p_manager );
1124 * Writes the MCC marker (Multiple Component Collection)
1126 * @param p_stream the stream to write data to.
1127 * @param p_j2k J2K codec.
1128 * @param p_manager the user event manager.
1130 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1131 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1132 struct opj_stream_private *p_stream,
1133 struct opj_event_mgr * p_manager );
1137 * Reads a MCC marker (Multiple Component Collection)
1139 * @param p_header_data the data contained in the MCC box.
1140 * @param p_j2k the jpeg2000 codec.
1141 * @param p_header_size the size of the data contained in the MCC marker.
1142 * @param p_manager the user event manager.
1144 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1145 OPJ_BYTE * p_header_data,
1146 OPJ_UINT32 p_header_size,
1147 struct opj_event_mgr * p_manager );
1150 * Writes the MCO marker (Multiple component transformation ordering)
1152 * @param p_stream the stream to write data to.
1153 * @param p_j2k J2K codec.
1154 * @param p_manager the user event manager.
1156 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1157 struct opj_stream_private *p_stream,
1158 struct opj_event_mgr * p_manager );
1161 * Reads a MCO marker (Multiple Component Transform Ordering)
1163 * @param p_header_data the data contained in the MCO box.
1164 * @param p_j2k the jpeg2000 codec.
1165 * @param p_header_size the size of the data contained in the MCO marker.
1166 * @param p_manager the user event manager.
1168 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1169 OPJ_BYTE * p_header_data,
1170 OPJ_UINT32 p_header_size,
1171 struct opj_event_mgr * p_manager );
1173 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1175 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1176 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1177 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1178 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1180 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1181 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1182 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1183 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1185 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1186 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1187 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1188 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1192 * Ends the encoding, i.e. frees memory.
1194 * @param p_stream the stream to write data to.
1195 * @param p_j2k J2K codec.
1196 * @param p_manager the user event manager.
1198 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1199 struct opj_stream_private *p_stream,
1200 struct opj_event_mgr * p_manager );
1203 * Writes the CBD marker (Component bit depth definition)
1205 * @param p_stream the stream to write data to.
1206 * @param p_j2k J2K codec.
1207 * @param p_manager the user event manager.
1209 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1210 struct opj_stream_private *p_stream,
1211 struct opj_event_mgr * p_manager );
1214 * Reads a CBD marker (Component bit depth definition)
1215 * @param p_header_data the data contained in the CBD box.
1216 * @param p_j2k the jpeg2000 codec.
1217 * @param p_header_size the size of the data contained in the CBD marker.
1218 * @param p_manager the user event manager.
1220 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1221 OPJ_BYTE * p_header_data,
1222 OPJ_UINT32 p_header_size,
1223 struct opj_event_mgr * p_manager);
1226 * Writes the image components.
1228 * @param p_stream the stream to write data to.
1229 * @param p_j2k J2K codec.
1230 * @param p_manager the user event manager.
1232 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1233 struct opj_stream_private *p_stream,
1234 struct opj_event_mgr * p_manager );
1237 * Writes regions of interests.
1239 * @param p_stream the stream to write data to.
1240 * @param p_j2k J2K codec.
1241 * @param p_manager the user event manager.
1243 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1244 struct opj_stream_private *p_stream,
1245 struct opj_event_mgr * p_manager );
1250 * @param p_stream the stream to write data to.
1251 * @param p_j2k J2K codec.
1252 * @param p_manager the user event manager.
1254 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1255 struct opj_stream_private *p_stream,
1256 struct opj_event_mgr * p_manager );
1259 * Checks the progression order changes values. Tells of the poc given as input are valid.
1260 * A nice message is outputted at errors.
1262 * @param p_pocs the progression order changes.
1263 * @param p_nb_pocs the number of progression order changes.
1264 * @param p_nb_resolutions the number of resolutions.
1265 * @param numcomps the number of components
1266 * @param numlayers the number of layers.
1268 * @return true if the pocs are valid.
1270 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1271 OPJ_UINT32 p_nb_pocs,
1272 OPJ_UINT32 p_nb_resolutions,
1273 OPJ_UINT32 numcomps,
1274 OPJ_UINT32 numlayers,
1275 opj_event_mgr_t * p_manager);
1278 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1280 * @param cp the coding parameters.
1281 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1282 * @param tileno the given tile.
1284 * @return the number of tile parts.
1286 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1288 /** mem allocation for TLM marker*/
1289 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1292 * Calculates the total number of tile parts needed by the encoder to
1293 * encode such an image. If not enough memory is available, then the function return false.
1295 * @param p_nb_tiles pointer that will hold the number of tile parts.
1296 * @param cp the coding parameters for the image.
1297 * @param image the image to encode.
1298 * @param p_j2k the p_j2k encoder.
1299 * @param p_manager the user event manager.
1301 * @return true if the function was successful, false else.
1303 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1305 OPJ_UINT32 * p_nb_tiles,
1307 opj_event_mgr_t * p_manager);
1309 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1311 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1313 static opj_codestream_index_t* j2k_create_cstr_index(void);
1319 /* ----------------------------------------------------------------------- */
1320 typedef struct j2k_prog_order{
1321 OPJ_PROG_ORDER enum_prog;
1325 j2k_prog_order_t j2k_prog_order_list[] = {
1331 {(OPJ_PROG_ORDER)-1, ""}
1339 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1347 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1349 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1351 j2k_read_int16_to_float,
1352 j2k_read_int32_to_float,
1353 j2k_read_float32_to_float,
1354 j2k_read_float64_to_float
1357 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1359 j2k_read_int16_to_int32,
1360 j2k_read_int32_to_int32,
1361 j2k_read_float32_to_int32,
1362 j2k_read_float64_to_int32
1365 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1367 j2k_write_float_to_int16,
1368 j2k_write_float_to_int32,
1369 j2k_write_float_to_float,
1370 j2k_write_float_to_float64
1373 typedef struct opj_dec_memory_marker_handler
1377 /** value of the state when the marker can appear */
1379 /** action linked to the marker */
1380 opj_bool (*handler) (
1381 opj_j2k_v2_t *p_j2k,
1382 OPJ_BYTE * p_header_data,
1383 OPJ_UINT32 p_header_size,
1384 struct opj_event_mgr * p_manager
1387 opj_dec_memory_marker_handler_t;
1389 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1392 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1393 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1394 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1395 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1396 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1397 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1398 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1399 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1400 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1401 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1402 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1403 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1404 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1406 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1407 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1408 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1409 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1410 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1411 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1413 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1414 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1415 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1416 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1417 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1418 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1419 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1420 {J2K_MS_SIZ, J2K_STATE_MHSIZ , opj_j2k_read_siz},
1421 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1422 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1423 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1424 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1425 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1427 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1428 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1429 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1430 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1431 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1432 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1434 #ifdef TODO_MS /* FIXME */
1435 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1436 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1437 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1438 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1440 #endif /* USE_JPWL */
1442 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1443 {J2K_MS_INSEC, 0, j2k_read_insec}
1444 #endif /* USE_JPSEC */
1445 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1450 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1452 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1453 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1457 for (i=0;i<p_nb_elem;++i) {
1458 opj_read_bytes(l_src_data,&l_temp,2);
1460 l_src_data+=sizeof(OPJ_INT16);
1462 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1466 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1468 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1469 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1473 for (i=0;i<p_nb_elem;++i) {
1474 opj_read_bytes(l_src_data,&l_temp,4);
1476 l_src_data+=sizeof(OPJ_INT32);
1478 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1482 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1484 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1485 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1489 for (i=0;i<p_nb_elem;++i) {
1490 opj_read_float(l_src_data,&l_temp);
1492 l_src_data+=sizeof(OPJ_FLOAT32);
1494 *(l_dest_data++) = l_temp;
1498 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1500 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1501 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1505 for (i=0;i<p_nb_elem;++i) {
1506 opj_read_double(l_src_data,&l_temp);
1508 l_src_data+=sizeof(OPJ_FLOAT64);
1510 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1514 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1516 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1517 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1521 for (i=0;i<p_nb_elem;++i) {
1522 opj_read_bytes(l_src_data,&l_temp,2);
1524 l_src_data+=sizeof(OPJ_INT16);
1526 *(l_dest_data++) = (OPJ_INT32) l_temp;
1530 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1532 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1533 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1537 for (i=0;i<p_nb_elem;++i) {
1538 opj_read_bytes(l_src_data,&l_temp,4);
1540 l_src_data+=sizeof(OPJ_INT32);
1542 *(l_dest_data++) = (OPJ_INT32) l_temp;
1546 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1548 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1549 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1553 for (i=0;i<p_nb_elem;++i) {
1554 opj_read_float(l_src_data,&l_temp);
1556 l_src_data+=sizeof(OPJ_FLOAT32);
1558 *(l_dest_data++) = (OPJ_INT32) l_temp;
1562 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1564 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1565 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1569 for (i=0;i<p_nb_elem;++i) {
1570 opj_read_double(l_src_data,&l_temp);
1572 l_src_data+=sizeof(OPJ_FLOAT64);
1574 *(l_dest_data++) = (OPJ_INT32) l_temp;
1578 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1580 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1581 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1585 for (i=0;i<p_nb_elem;++i) {
1586 l_temp = (OPJ_UINT32) *(l_src_data++);
1588 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1590 l_dest_data+=sizeof(OPJ_INT16);
1594 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1596 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1597 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1601 for (i=0;i<p_nb_elem;++i) {
1602 l_temp = (OPJ_UINT32) *(l_src_data++);
1604 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1606 l_dest_data+=sizeof(OPJ_INT32);
1610 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1612 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1613 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1617 for (i=0;i<p_nb_elem;++i) {
1618 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1620 opj_write_float(l_dest_data,l_temp);
1622 l_dest_data+=sizeof(OPJ_FLOAT32);
1626 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1628 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1629 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1633 for (i=0;i<p_nb_elem;++i) {
1634 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1636 opj_write_double(l_dest_data,l_temp);
1638 l_dest_data+=sizeof(OPJ_FLOAT64);
1644 * Converts an enum type progression order to string type.
1646 * @param prg_order the progression order to get.
1648 * @return the string representation of the given progression order.
1650 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1651 j2k_prog_order_t *po;
1652 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1653 if(po->enum_prog == prg_order){
1654 return po->str_prog;
1657 return po->str_prog;
1661 * Checks the progression order changes values. Tells if the poc given as input are valid.
1663 * @param p_pocs the progression order changes.
1664 * @param p_nb_pocs the number of progression order changes.
1665 * @param p_nb_resolutions the number of resolutions.
1666 * @param numcomps the number of components
1667 * @param numlayers the number of layers.
1668 * @param p_manager the user event manager.
1670 * @return true if the pocs are valid.
1672 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1673 OPJ_UINT32 p_nb_pocs,
1674 OPJ_UINT32 p_nb_resolutions,
1675 OPJ_UINT32 p_num_comps,
1676 OPJ_UINT32 p_num_layers,
1677 opj_event_mgr_t * p_manager)
1679 OPJ_UINT32* packet_array;
1680 OPJ_UINT32 index , resno, compno, layno;
1682 OPJ_UINT32 step_c = 1;
1683 OPJ_UINT32 step_r = p_num_comps * step_c;
1684 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1685 opj_bool loss = OPJ_FALSE;
1686 OPJ_UINT32 layno0 = 0;
1688 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1689 if (packet_array == 00) {
1690 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1693 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1695 if (p_nb_pocs == 0) {
1699 index = step_r * p_pocs->resno0;
1700 // take each resolution for each poc
1701 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1703 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1705 // take each comp of each resolution for each poc
1706 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1707 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1709 // and finally take each layer of each res of ...
1710 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1711 //index = step_r * resno + step_c * compno + step_l * layno;
1712 packet_array[comp_index] = 1;
1713 comp_index += step_l;
1716 res_index += step_c;
1723 // iterate through all the pocs
1724 for (i = 1; i < p_nb_pocs ; ++i) {
1725 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1727 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1728 index = step_r * p_pocs->resno0;
1730 // take each resolution for each poc
1731 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1732 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1734 // take each comp of each resolution for each poc
1735 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1736 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1738 // and finally take each layer of each res of ...
1739 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1740 //index = step_r * resno + step_c * compno + step_l * layno;
1741 packet_array[comp_index] = 1;
1742 comp_index += step_l;
1745 res_index += step_c;
1755 for (layno = 0; layno < p_num_layers ; ++layno) {
1756 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1757 for (compno = 0; compno < p_num_comps; ++compno) {
1758 loss |= (packet_array[index]!=1);
1759 //index = step_r * resno + step_c * compno + step_l * layno;
1766 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1769 opj_free(packet_array);
1774 /* ----------------------------------------------------------------------- */
1775 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1778 int tpnum=1,tpend=0;
1779 opj_tcp_t *tcp = &cp->tcps[tileno];
1780 prog = j2k_convert_progression_order(tcp->prg);
1785 if( cp->tp_flag == prog[i] ){
1786 tpend=1;cp->tp_pos=i;
1790 tpnum= tpnum * tcp->pocs[pino].compE;
1793 tpnum= tpnum * tcp->pocs[pino].resE;
1796 tpnum= tpnum * tcp->pocs[pino].prcE;
1799 tpnum= tpnum * tcp->pocs[pino].layE;
1811 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1813 * @param cp the coding parameters.
1814 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1815 * @param tileno the given tile.
1817 * @return the number of tile parts.
1819 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1821 const OPJ_CHAR *prog = 00;
1823 OPJ_UINT32 tpnum = 1;
1824 opj_tcp_v2_t *tcp = 00;
1825 opj_poc_t * l_current_poc = 00;
1828 assert(tileno < (cp->tw * cp->th));
1829 assert(pino < (cp->tcps[tileno].numpocs + 1));
1831 /* get the given tile coding parameter */
1832 tcp = &cp->tcps[tileno];
1835 l_current_poc = &(tcp->pocs[pino]);
1836 assert(l_current_poc != 0);
1838 /* get the progression order as a character string */
1839 prog = j2k_convert_progression_order(tcp->prg);
1840 assert(strlen(prog) > 0);
1842 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1846 /* component wise */
1848 tpnum *= l_current_poc->compE;
1850 /* resolution wise */
1852 tpnum *= l_current_poc->resE;
1856 tpnum *= l_current_poc->prcE;
1860 tpnum *= l_current_poc->layE;
1863 /* whould we split here ? */
1864 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1865 cp->m_specific_param.m_enc.m_tp_pos=i;
1877 /** mem allocation for TLM marker*/
1878 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1879 int pino,tileno,totnum_tp=0;
1881 OPJ_ARG_NOT_USED(img_numcomp);
1883 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1884 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1885 int cur_totnum_tp = 0;
1886 opj_tcp_t *tcp = &cp->tcps[tileno];
1887 for(pino = 0; pino <= tcp->numpocs; pino++) {
1889 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1890 if(!pi) { return -1;}
1891 tp_num = j2k_get_num_tp(cp,pino,tileno);
1892 totnum_tp = totnum_tp + tp_num;
1893 cur_totnum_tp = cur_totnum_tp + tp_num;
1894 pi_destroy(pi, cp, tileno);
1896 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1898 if (j2k->cstr_info) {
1899 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1900 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1908 * Calculates the total number of tile parts needed by the encoder to
1909 * encode such an image. If not enough memory is available, then the function return false.
1911 * @param p_nb_tiles pointer that will hold the number of tile parts.
1912 * @param cp the coding parameters for the image.
1913 * @param image the image to encode.
1914 * @param p_j2k the p_j2k encoder.
1915 * @param p_manager the user event manager.
1917 * @return true if the function was successful, false else.
1919 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1921 OPJ_UINT32 * p_nb_tiles,
1923 opj_event_mgr_t * p_manager)
1925 OPJ_UINT32 pino,tileno;
1926 OPJ_UINT32 l_nb_tiles;
1930 assert(p_nb_tiles != 00);
1932 assert(image != 00);
1933 assert(p_j2k != 00);
1934 assert(p_manager != 00);
1936 l_nb_tiles = cp->tw * cp->th;
1941 /* TODO mergeV2: check this part which use cstr_info */
1942 /*if (p_j2k->cstr_info) {
1943 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1945 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1946 OPJ_UINT32 cur_totnum_tp = 0;
1948 pi_update_encoding_parameters(image,cp,tileno);
1950 for (pino = 0; pino <= tcp->numpocs; ++pino)
1952 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1954 *p_nb_tiles = *p_nb_tiles + tp_num;
1956 cur_totnum_tp += tp_num;
1959 tcp->m_nb_tile_parts = cur_totnum_tp;
1961 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1962 if (l_info_tile_ptr->tp == 00) {
1966 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1968 l_info_tile_ptr->num_tps = cur_totnum_tp;
1975 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1976 OPJ_UINT32 cur_totnum_tp = 0;
1978 pi_update_encoding_parameters(image,cp,tileno);
1980 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1981 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1983 *p_nb_tiles = *p_nb_tiles + tp_num;
1985 cur_totnum_tp += tp_num;
1987 tcp->m_nb_tile_parts = cur_totnum_tp;
1996 static void j2k_write_soc(opj_j2k_t *j2k) {
1997 opj_cio_t *cio = j2k->cio;
1998 cio_write(cio, J2K_MS_SOC, 2);
2001 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2006 /* update markers struct */
2007 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2008 #endif /* USE_JPWL */
2013 * Writes the SOC marker (Start Of Codestream)
2015 * @param p_stream the stream to write data to.
2016 * @param p_j2k J2K codec.
2017 * @param p_manager the user event manager.
2019 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2020 struct opj_stream_private *p_stream,
2021 struct opj_event_mgr * p_manager )
2023 /* 2 bytes will be written */
2024 OPJ_BYTE * l_start_stream = 00;
2027 assert(p_stream != 00);
2028 assert(p_j2k != 00);
2029 assert(p_manager != 00);
2031 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2033 /* write SOC identifier */
2034 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2036 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2042 /* update markers struct */
2044 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2046 assert( 0 && "TODO" );
2047 #endif /* USE_JPWL */
2056 * Reads a SOC marker (Start of Codestream)
2057 * @param p_header_data the data contained in the SOC box.
2058 * @param jp2 the jpeg2000 file codec.
2059 * @param p_header_size the size of the data contained in the SOC marker.
2060 * @param p_manager the user event manager.
2062 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2063 struct opj_stream_private *p_stream,
2064 struct opj_event_mgr * p_manager )
2066 OPJ_BYTE l_data [2];
2067 OPJ_UINT32 l_marker;
2070 assert(p_j2k != 00);
2071 assert(p_manager != 00);
2072 assert(p_stream != 00);
2074 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2078 opj_read_bytes(l_data,&l_marker,2);
2079 if (l_marker != J2K_MS_SOC) {
2083 /* Next marker should be a SIZ marker in the main header */
2084 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2086 /* FIXME move it in a index structure included in p_j2k*/
2087 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2089 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2091 /* Add the marker to the codestream index*/
2092 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2097 static void j2k_write_siz(opj_j2k_t *j2k) {
2101 opj_cio_t *cio = j2k->cio;
2102 opj_image_t *image = j2k->image;
2103 opj_cp_t *cp = j2k->cp;
2105 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2106 lenp = cio_tell(cio);
2108 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2109 cio_write(cio, image->x1, 4); /* Xsiz */
2110 cio_write(cio, image->y1, 4); /* Ysiz */
2111 cio_write(cio, image->x0, 4); /* X0siz */
2112 cio_write(cio, image->y0, 4); /* Y0siz */
2113 cio_write(cio, cp->tdx, 4); /* XTsiz */
2114 cio_write(cio, cp->tdy, 4); /* YTsiz */
2115 cio_write(cio, cp->tx0, 4); /* XT0siz */
2116 cio_write(cio, cp->ty0, 4); /* YT0siz */
2117 cio_write(cio, image->numcomps, 2); /* Csiz */
2118 for (i = 0; i < image->numcomps; i++) {
2119 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2120 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2121 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2123 len = cio_tell(cio) - lenp;
2124 cio_seek(cio, lenp);
2125 cio_write(cio, len, 2); /* Lsiz */
2126 cio_seek(cio, lenp + len);
2129 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2133 * Writes the SIZ marker (image and tile size)
2135 * @param p_stream the stream to write data to.
2136 * @param p_j2k J2K codec.
2137 * @param p_manager the user event manager.
2139 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2140 struct opj_stream_private *p_stream,
2141 struct opj_event_mgr * p_manager )
2144 OPJ_UINT32 l_size_len;
2145 OPJ_BYTE * l_current_ptr;
2146 opj_image_t * l_image = 00;
2147 opj_cp_v2_t *cp = 00;
2148 opj_image_comp_t * l_img_comp = 00;
2151 assert(p_stream != 00);
2152 assert(p_j2k != 00);
2153 assert(p_manager != 00);
2155 l_image = p_j2k->m_private_image;
2156 cp = &(p_j2k->m_cp);
2157 l_size_len = 40 + 3 * l_image->numcomps;
2158 l_img_comp = l_image->comps;
2160 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2162 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2163 = (OPJ_BYTE*)opj_realloc(
2164 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2166 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2170 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2173 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2175 /* write SOC identifier */
2176 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2179 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2182 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2185 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2188 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2191 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2194 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2197 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2200 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2203 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2206 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2209 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2212 for (i = 0; i < l_image->numcomps; ++i) {
2213 /* TODO here with MCT ? */
2214 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2217 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2220 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2226 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) {
2236 * Reads a SIZ marker (image and tile size)
2237 * @param p_header_data the data contained in the SIZ box.
2238 * @param jp2 the jpeg2000 file codec.
2239 * @param p_header_size the size of the data contained in the SIZ marker.
2240 * @param p_manager the user event manager.
2242 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2243 OPJ_BYTE * p_header_data,
2244 OPJ_UINT32 p_header_size,
2245 opj_event_mgr_t * p_manager
2248 OPJ_UINT32 l_size, i;
2249 OPJ_UINT32 l_nb_comp;
2250 OPJ_UINT32 l_nb_comp_remain;
2251 OPJ_UINT32 l_remaining_size;
2252 OPJ_UINT32 l_nb_tiles;
2254 opj_image_t *l_image = 00;
2255 opj_cp_v2_t *l_cp = 00;
2256 opj_image_comp_t * l_img_comp = 00;
2257 opj_tcp_v2_t * l_current_tile_param = 00;
2260 assert(p_j2k != 00);
2261 assert(p_manager != 00);
2262 assert(p_header_data != 00);
2264 l_image = p_j2k->m_private_image;
2265 l_cp = &(p_j2k->m_cp);
2267 /* minimum size == 39 - 3 (= minimum component parameter) */
2268 if (p_header_size < 36) {
2269 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2273 l_remaining_size = p_header_size - 36;
2274 l_nb_comp = l_remaining_size / 3;
2275 l_nb_comp_remain = l_remaining_size % 3;
2276 if (l_nb_comp_remain != 0){
2277 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2281 l_size = p_header_size + 2; /* Lsiz */
2283 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2285 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2286 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2288 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2290 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2292 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2294 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2296 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2298 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2300 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2302 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2305 l_image->numcomps = (OPJ_UINT16) l_tmp;
2307 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2311 if (l_image->numcomps != l_nb_comp) {
2312 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);
2317 if (l_cp->correct) {
2318 /* if JPWL is on, we check whether TX errors have damaged
2319 too much the SIZ parameters */
2320 if (!(l_image->x1 * l_image->y1)) {
2321 opj_event_msg_v2(p_manager, EVT_ERROR,
2322 "JPWL: bad image size (%d x %d)\n",
2323 l_image->x1, l_image->y1);
2324 if (!JPWL_ASSUME || JPWL_ASSUME) {
2325 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2330 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2331 if (l_image->numcomps != ((len - 38) / 3)) {
2332 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2333 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2334 l_image->numcomps, ((len - 38) / 3));
2336 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2339 */ /* we try to correct */
2340 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2341 if (l_image->numcomps < ((len - 38) / 3)) {
2342 len = 38 + 3 * l_image->numcomps;
2343 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2346 l_image->numcomps = ((len - 38) / 3);
2347 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2353 /* update components number in the jpwl_exp_comps filed */
2354 l_cp->exp_comps = l_image->numcomps;
2356 #endif /* USE_JPWL */
2358 /* Allocate the resulting image components */
2359 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2360 if (l_image->comps == 00){
2361 l_image->numcomps = 0;
2362 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2366 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2367 l_img_comp = l_image->comps;
2369 /* Read the component information */
2370 for (i = 0; i < l_image->numcomps; ++i){
2372 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2374 l_img_comp->prec = (tmp & 0x7f) + 1;
2375 l_img_comp->sgnd = tmp >> 7;
2376 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2378 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2379 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2381 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2384 if (l_cp->correct) {
2385 /* if JPWL is on, we check whether TX errors have damaged
2386 too much the SIZ parameters, again */
2387 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2388 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2389 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2390 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2392 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2395 /* we try to correct */
2396 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2397 if (!l_image->comps[i].dx) {
2398 l_image->comps[i].dx = 1;
2399 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2400 i, l_image->comps[i].dx);
2402 if (!l_image->comps[i].dy) {
2403 l_image->comps[i].dy = 1;
2404 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2405 i, l_image->comps[i].dy);
2409 #endif /* USE_JPWL */
2410 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2411 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2415 /* Compute the number of tiles */
2416 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2417 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2418 l_nb_tiles = l_cp->tw * l_cp->th;
2420 /* Define the tiles which will be decoded */
2421 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2422 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;
2423 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;
2424 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);
2425 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);
2428 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2429 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2430 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2431 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2435 if (l_cp->correct) {
2436 /* if JPWL is on, we check whether TX errors have damaged
2437 too much the SIZ parameters */
2438 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2439 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2440 "JPWL: bad number of tiles (%d x %d)\n",
2441 l_cp->tw, l_cp->th);
2443 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2446 /* we try to correct */
2447 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2450 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2453 if (l_cp->tw > l_cp->max_tiles) {
2455 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2456 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2457 l_cp->max_tiles, l_cp->tw);
2461 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2464 if (l_cp->th > l_cp->max_tiles) {
2466 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2467 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2468 l_cp->max_tiles, l_cp->th);
2472 #endif /* USE_JPWL */
2474 /* memory allocations */
2475 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2476 if (l_cp->tcps == 00) {
2477 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2480 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2483 if (l_cp->correct) {
2485 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2486 "JPWL: could not alloc tcps field of cp\n");
2487 if (!JPWL_ASSUME || JPWL_ASSUME) {
2488 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2493 #endif /* USE_JPWL */
2495 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2496 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2497 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2498 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2501 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2503 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2504 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2506 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2507 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2510 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));
2511 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2513 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2514 (opj_simple_mcc_decorrelation_data_t*)
2515 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2517 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2518 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2521 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));
2522 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2524 /* set up default dc level shift */
2525 for (i=0;i<l_image->numcomps;++i) {
2526 if (! l_image->comps[i].sgnd) {
2527 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2531 l_current_tile_param = l_cp->tcps;
2532 for (i = 0; i < l_nb_tiles; ++i) {
2533 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2534 if (l_current_tile_param->tccps == 00) {
2535 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2538 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2540 ++l_current_tile_param;
2543 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2544 opj_image_comp_header_update(l_image,l_cp);
2551 static void j2k_write_com(opj_j2k_t *j2k) {
2555 if(j2k->cp->comment) {
2556 opj_cio_t *cio = j2k->cio;
2557 char *comment = j2k->cp->comment;
2559 cio_write(cio, J2K_MS_COM, 2);
2560 lenp = cio_tell(cio);
2562 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2563 for (i = 0; i < strlen(comment); i++) {
2564 cio_write(cio, comment[i], 1);
2566 len = cio_tell(cio) - lenp;
2567 cio_seek(cio, lenp);
2568 cio_write(cio, len, 2);
2569 cio_seek(cio, lenp + len);
2573 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2579 * Writes the COM marker (comment)
2581 * @param p_stream the stream to write data to.
2582 * @param p_j2k J2K codec.
2583 * @param p_manager the user event manager.
2585 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2586 struct opj_stream_private *p_stream,
2587 struct opj_event_mgr * p_manager )
2589 OPJ_UINT32 l_comment_size;
2590 OPJ_UINT32 l_total_com_size;
2591 const OPJ_CHAR *l_comment;
2592 OPJ_BYTE * l_current_ptr = 00;
2595 assert(p_j2k != 00);
2596 assert(p_stream != 00);
2597 assert(p_manager != 00);
2599 l_comment = p_j2k->m_cp.comment;
2600 l_comment_size = strlen(l_comment);
2601 l_total_com_size = l_comment_size + 6;
2603 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2604 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2605 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2608 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2612 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2615 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2617 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2620 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2623 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2626 memcpy( l_current_ptr,l_comment,l_comment_size);
2628 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) {
2635 static void j2k_read_com(opj_j2k_t *j2k) {
2638 opj_cio_t *cio = j2k->cio;
2640 len = cio_read(cio, 2);
2641 cio_skip(cio, len - 2);
2644 * Reads a COM marker (comments)
2645 * @param p_header_data the data contained in the COM box.
2646 * @param jp2 the jpeg2000 file codec.
2647 * @param p_header_size the size of the data contained in the COM marker.
2648 * @param p_manager the user event manager.
2650 opj_bool j2k_read_com_v2 (
2651 opj_j2k_v2_t *p_j2k,
2652 OPJ_BYTE * p_header_data,
2653 OPJ_UINT32 p_header_size,
2654 struct opj_event_mgr * p_manager
2658 assert(p_j2k != 00);
2659 assert(p_manager != 00);
2660 assert(p_header_data != 00);
2661 (void)p_header_size;
2666 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2669 opj_cp_t *cp = j2k->cp;
2670 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2671 opj_tccp_t *tccp = &tcp->tccps[compno];
2672 opj_cio_t *cio = j2k->cio;
2674 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2675 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2676 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2677 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2678 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2680 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2681 for (i = 0; i < tccp->numresolutions; i++) {
2682 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2687 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2690 opj_cp_t *cp = j2k->cp;
2691 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2692 opj_tccp_t *tccp = &tcp->tccps[compno];
2693 opj_cio_t *cio = j2k->cio;
2695 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2697 /* If user wants to remove more resolutions than the codestream contains, return error*/
2698 assert(cp->reduce >= 0);
2699 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2700 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2701 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2702 j2k->state |= J2K_STATE_ERR;
2705 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2706 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2707 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2708 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2709 if (tccp->csty & J2K_CP_CSTY_PRT) {
2710 for (i = 0; i < tccp->numresolutions; i++) {
2711 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2712 tccp->prcw[i] = tmp & 0xf;
2713 tccp->prch[i] = tmp >> 4;
2718 if(j2k->cstr_info && compno == 0) {
2719 for (i = 0; i < tccp->numresolutions; i++) {
2720 if (tccp->csty & J2K_CP_CSTY_PRT) {
2721 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2722 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2725 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2726 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2733 static void j2k_write_cod(opj_j2k_t *j2k) {
2734 opj_cp_t *cp = NULL;
2735 opj_tcp_t *tcp = NULL;
2738 opj_cio_t *cio = j2k->cio;
2740 cio_write(cio, J2K_MS_COD, 2); /* COD */
2742 lenp = cio_tell(cio);
2746 tcp = &cp->tcps[j2k->curtileno];
2748 cio_write(cio, tcp->csty, 1); /* Scod */
2749 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2750 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2751 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2753 j2k_write_cox(j2k, 0);
2754 len = cio_tell(cio) - lenp;
2755 cio_seek(cio, lenp);
2756 cio_write(cio, len, 2); /* Lcod */
2757 cio_seek(cio, lenp + len);
2760 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2765 * Writes the COD marker (Coding style default)
2767 * @param p_stream the stream to write data to.
2768 * @param p_j2k J2K codec.
2769 * @param p_manager the user event manager.
2771 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2772 struct opj_stream_private *p_stream,
2773 struct opj_event_mgr * p_manager )
2775 opj_cp_v2_t *l_cp = 00;
2776 opj_tcp_v2_t *l_tcp = 00;
2777 OPJ_UINT32 l_code_size,l_remaining_size;
2778 OPJ_BYTE * l_current_data = 00;
2781 assert(p_j2k != 00);
2782 assert(p_manager != 00);
2783 assert(p_stream != 00);
2785 l_cp = &(p_j2k->m_cp);
2786 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2787 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2788 l_remaining_size = l_code_size;
2790 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2791 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2792 = (OPJ_BYTE*)opj_realloc(
2793 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2796 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2800 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2803 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2805 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2806 l_current_data += 2;
2808 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2809 l_current_data += 2;
2811 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2814 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2817 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2820 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2823 l_remaining_size -= 9;
2825 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2826 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2830 if (l_remaining_size != 0) {
2831 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2835 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) {
2844 * Reads a COD marker (Coding Styke defaults)
2845 * @param p_header_data the data contained in the COD box.
2846 * @param p_j2k the jpeg2000 codec.
2847 * @param p_header_size the size of the data contained in the COD marker.
2848 * @param p_manager the user event manager.
2850 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2851 OPJ_BYTE * p_header_data,
2852 OPJ_UINT32 p_header_size,
2853 opj_event_mgr_t * p_manager
2859 opj_cp_v2_t *l_cp = 00;
2860 opj_tcp_v2_t *l_tcp = 00;
2861 opj_image_t *l_image = 00;
2864 assert(p_header_data != 00);
2865 assert(p_j2k != 00);
2866 assert(p_manager != 00);
2868 l_image = p_j2k->m_private_image;
2869 l_cp = &(p_j2k->m_cp);
2871 /* If we are in the first tile-part header of the current tile */
2872 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2873 &l_cp->tcps[p_j2k->m_current_tile_number] :
2874 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2876 /* Make sure room is sufficient */
2877 if (p_header_size < 5) {
2878 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2882 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2884 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2886 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2887 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2890 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2891 if (l_cp->m_specific_param.m_dec.m_layer) {
2892 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2895 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2898 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2902 for (i = 0; i < l_image->numcomps; ++i) {
2903 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2906 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2907 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2911 if (p_header_size != 0) {
2912 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2916 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2917 j2k_copy_tile_component_parameters(p_j2k);
2920 #ifdef WIP_REMOVE_MSD
2921 if (p_j2k->cstr_info) {
2922 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2923 p_j2k->cstr_info->prog = l_tcp->prg;
2924 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2925 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2926 for (i = 0; i < l_image->numcomps; ++i) {
2927 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2935 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2938 opj_cp_t *cp = j2k->cp;
2939 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2940 opj_image_t *image = j2k->image;
2941 opj_cio_t *cio = j2k->cio;
2943 cio_write(cio, J2K_MS_COC, 2); /* COC */
2944 lenp = cio_tell(cio);
2946 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2947 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2948 j2k_write_cox(j2k, compno);
2949 len = cio_tell(cio) - lenp;
2950 cio_seek(cio, lenp);
2951 cio_write(cio, len, 2); /* Lcoc */
2952 cio_seek(cio, lenp + len);
2956 * Writes the COC marker (Coding style component)
2958 * @param p_comp_no the index of the component to output.
2959 * @param p_stream the stream to write data to.
2960 * @param p_j2k J2K codec.
2961 * @param p_manager the user event manager.
2963 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2964 OPJ_UINT32 p_comp_no,
2965 struct opj_stream_private *p_stream,
2966 struct opj_event_mgr * p_manager )
2968 OPJ_UINT32 l_coc_size,l_remaining_size;
2969 OPJ_UINT32 l_comp_room;
2972 assert(p_j2k != 00);
2973 assert(p_manager != 00);
2974 assert(p_stream != 00);
2976 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2978 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2980 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2981 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2982 = (OPJ_BYTE*)opj_realloc(
2983 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2985 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2989 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2992 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);
2994 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) {
3002 * Writes the COC marker (Coding style component)
3004 * @param p_comp_no the index of the component to output.
3005 * @param p_stream the stream to write data to.
3006 * @param p_j2k J2K codec.
3007 * @param p_manager the user event manager.
3009 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3010 OPJ_UINT32 p_comp_no,
3012 OPJ_UINT32 * p_data_written,
3013 struct opj_event_mgr * p_manager )
3015 opj_cp_v2_t *l_cp = 00;
3016 opj_tcp_v2_t *l_tcp = 00;
3017 OPJ_UINT32 l_coc_size,l_remaining_size;
3018 OPJ_BYTE * l_current_data = 00;
3019 opj_image_t *l_image = 00;
3020 OPJ_UINT32 l_comp_room;
3023 assert(p_j2k != 00);
3024 assert(p_manager != 00);
3026 l_cp = &(p_j2k->m_cp);
3027 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3028 l_image = p_j2k->m_private_image;
3029 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3031 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3032 l_remaining_size = l_coc_size;
3034 l_current_data = p_data;
3036 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3037 l_current_data += 2;
3039 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3040 l_current_data += 2;
3042 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3043 l_current_data+=l_comp_room;
3045 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3048 l_remaining_size -= (5 + l_comp_room);
3049 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3050 * p_data_written = l_coc_size;
3054 * Gets the maximum size taken by a coc.
3056 * @param p_j2k the jpeg2000 codec to use.
3058 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3061 OPJ_UINT32 l_nb_comp;
3062 OPJ_UINT32 l_nb_tiles;
3063 OPJ_UINT32 l_max = 0;
3067 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3068 l_nb_comp = p_j2k->m_private_image->numcomps;
3070 for (i=0;i<l_nb_tiles;++i) {
3071 for (j=0;j<l_nb_comp;++j) {
3072 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3081 * Reads a COC marker (Coding Style Component)
3082 * @param p_header_data the data contained in the COC box.
3083 * @param p_j2k the jpeg2000 codec.
3084 * @param p_header_size the size of the data contained in the COC marker.
3085 * @param p_manager the user event manager.
3087 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
3088 OPJ_BYTE * p_header_data,
3089 OPJ_UINT32 p_header_size,
3090 opj_event_mgr_t * p_manager
3093 opj_cp_v2_t *l_cp = NULL;
3094 opj_tcp_v2_t *l_tcp = NULL;
3095 opj_image_t *l_image = NULL;
3096 OPJ_UINT32 l_comp_room;
3097 OPJ_UINT32 l_comp_no;
3100 assert(p_header_data != 00);
3101 assert(p_j2k != 00);
3102 assert(p_manager != 00);
3104 l_cp = &(p_j2k->m_cp);
3105 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3106 &l_cp->tcps[p_j2k->m_current_tile_number] :
3107 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3108 l_image = p_j2k->m_private_image;
3110 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3112 /* make sure room is sufficient*/
3113 if (p_header_size < l_comp_room + 1) {
3114 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3117 p_header_size -= l_comp_room + 1;
3119 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3120 p_header_data += l_comp_room;
3121 if (l_comp_no >= l_image->numcomps) {
3122 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3126 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3129 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3130 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3134 if (p_header_size != 0) {
3135 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3141 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3142 int bandno, numbands;
3145 opj_cp_t *cp = j2k->cp;
3146 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3147 opj_tccp_t *tccp = &tcp->tccps[compno];
3148 opj_cio_t *cio = j2k->cio;
3150 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3151 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3153 for (bandno = 0; bandno < numbands; bandno++) {
3154 expn = tccp->stepsizes[bandno].expn;
3155 mant = tccp->stepsizes[bandno].mant;
3157 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3158 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3160 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3165 static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
3167 int bandno, numbands;
3169 opj_cp_t *cp = j2k->cp;
3170 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3171 opj_tccp_t *tccp = &tcp->tccps[compno];
3172 opj_cio_t *cio = j2k->cio;
3174 tmp = cio_read(cio, 1); /* Sqcx */
3175 tccp->qntsty = tmp & 0x1f;
3176 tccp->numgbits = tmp >> 5;
3177 numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
3178 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
3181 if (j2k->cp->correct) {
3183 /* if JPWL is on, we check whether there are too many subbands */
3184 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3185 opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
3186 "JPWL: bad number of subbands in Sqcx (%d)\n",
3189 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
3192 /* we try to correct */
3194 opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
3195 "- setting number of bands to %d => HYPOTHESIS!!!\n",
3202 /* We check whether there are too many subbands */
3203 if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
3204 opj_event_msg(j2k->cinfo, EVT_WARNING ,
3205 "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
3206 "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
3209 #endif /* USE_JPWL */
3211 for (bandno = 0; bandno < numbands; bandno++) {
3213 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3214 expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
3217 tmp = cio_read(cio, 2); /* SPqcx_i */
3221 if (bandno < J2K_MAXBANDS){
3222 tccp->stepsizes[bandno].expn = expn;
3223 tccp->stepsizes[bandno].mant = mant;
3227 /* Add Antonin : if scalar_derived -> compute other stepsizes */
3228 if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
3229 for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
3230 tccp->stepsizes[bandno].expn =
3231 ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
3232 (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
3233 tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
3239 static void j2k_write_qcd(opj_j2k_t *j2k) {
3242 opj_cio_t *cio = j2k->cio;
3244 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3245 lenp = cio_tell(cio);
3247 j2k_write_qcx(j2k, 0);
3248 len = cio_tell(cio) - lenp;
3249 cio_seek(cio, lenp);
3250 cio_write(cio, len, 2); /* Lqcd */
3251 cio_seek(cio, lenp + len);
3254 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3258 * Writes the QCD marker (quantization default)
3260 * @param p_comp_number the index of the component to output.
3261 * @param p_stream the stream to write data to.
3262 * @param p_j2k J2K codec.
3263 * @param p_manager the user event manager.
3265 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3266 struct opj_stream_private *p_stream,
3267 struct opj_event_mgr * p_manager )
3269 opj_cp_v2_t *l_cp = 00;
3270 opj_tcp_v2_t *l_tcp = 00;
3271 OPJ_UINT32 l_qcd_size,l_remaining_size;
3272 OPJ_BYTE * l_current_data = 00;
3275 assert(p_j2k != 00);
3276 assert(p_manager != 00);
3277 assert(p_stream != 00);
3279 l_cp = &(p_j2k->m_cp);
3280 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3281 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3282 l_remaining_size = l_qcd_size;
3284 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3285 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3286 = (OPJ_BYTE*)opj_realloc(
3287 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3290 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3294 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3297 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3299 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3300 l_current_data += 2;
3302 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3303 l_current_data += 2;
3305 l_remaining_size -= 4;
3307 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3308 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3312 if (l_remaining_size != 0) {
3313 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3317 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) {
3325 * Reads a QCD marker (Quantization defaults)
3326 * @param p_header_data the data contained in the QCD box.
3327 * @param p_j2k the jpeg2000 codec.
3328 * @param p_header_size the size of the data contained in the QCD marker.
3329 * @param p_manager the user event manager.
3331 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
3332 OPJ_BYTE * p_header_data,
3333 OPJ_UINT32 p_header_size,
3334 opj_event_mgr_t * p_manager
3338 assert(p_header_data != 00);
3339 assert(p_j2k != 00);
3340 assert(p_manager != 00);
3342 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3343 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3347 if (p_header_size != 0) {
3348 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3352 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3353 j2k_copy_tile_quantization_parameters(p_j2k);
3358 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3361 opj_cio_t *cio = j2k->cio;
3363 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3364 lenp = cio_tell(cio);
3366 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3367 j2k_write_qcx(j2k, compno);
3368 len = cio_tell(cio) - lenp;
3369 cio_seek(cio, lenp);
3370 cio_write(cio, len, 2); /* Lqcc */
3371 cio_seek(cio, lenp + len);
3375 * Writes the QCC marker (quantization component)
3377 * @param p_comp_no the index of the component to output.
3378 * @param p_stream the stream to write data to.
3379 * @param p_j2k J2K codec.
3380 * @param p_manager the user event manager.
3382 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3383 OPJ_UINT32 p_comp_no,
3384 struct opj_stream_private *p_stream,
3385 struct opj_event_mgr * p_manager )
3387 OPJ_UINT32 l_qcc_size,l_remaining_size;
3390 assert(p_j2k != 00);
3391 assert(p_manager != 00);
3392 assert(p_stream != 00);
3394 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3395 l_remaining_size = l_qcc_size;
3397 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3398 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3399 = (OPJ_BYTE*)opj_realloc(
3400 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3402 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3406 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3409 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);
3411 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) {
3419 * Writes the QCC marker (quantization component)
3421 * @param p_comp_no the index of the component to output.
3422 * @param p_stream the stream to write data to.
3423 * @param p_j2k J2K codec.
3424 * @param p_manager the user event manager.
3426 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3427 OPJ_UINT32 p_comp_no,
3429 OPJ_UINT32 * p_data_written,
3430 struct opj_event_mgr * p_manager )
3432 OPJ_UINT32 l_qcc_size,l_remaining_size;
3433 OPJ_BYTE * l_current_data = 00;
3436 assert(p_j2k != 00);
3437 assert(p_manager != 00);
3439 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3440 l_remaining_size = l_qcc_size;
3442 l_current_data = p_data;
3444 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3445 l_current_data += 2;
3447 if (p_j2k->m_private_image->numcomps <= 256) {
3450 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3451 l_current_data += 2;
3453 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3456 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3457 l_remaining_size -= 6;
3460 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3461 l_current_data += 2;
3463 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3466 l_remaining_size -= 6;
3469 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3471 *p_data_written = l_qcc_size;
3475 * Gets the maximum size taken by a qcc.
3477 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3479 return j2k_get_max_coc_size(p_j2k);
3483 * Reads a QCC marker (Quantization component)
3484 * @param p_header_data the data contained in the QCC box.
3485 * @param p_j2k the jpeg2000 codec.
3486 * @param p_header_size the size of the data contained in the QCC marker.
3487 * @param p_manager the user event manager.
3489 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3490 OPJ_BYTE * p_header_data,
3491 OPJ_UINT32 p_header_size,
3492 opj_event_mgr_t * p_manager
3495 OPJ_UINT32 l_num_comp,l_comp_no;
3498 assert(p_header_data != 00);
3499 assert(p_j2k != 00);
3500 assert(p_manager != 00);
3502 l_num_comp = p_j2k->m_private_image->numcomps;
3504 if (l_num_comp <= 256) {
3505 if (p_header_size < 1) {
3506 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3509 opj_read_bytes(p_header_data,&l_comp_no,1);
3514 if (p_header_size < 2) {
3515 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3518 opj_read_bytes(p_header_data,&l_comp_no,2);
3524 if (p_j2k->m_cp.correct) {
3526 static OPJ_UINT32 backup_compno = 0;
3528 /* compno is negative or larger than the number of components!!! */
3529 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3530 opj_event_msg_v2(p_manager, EVT_ERROR,
3531 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3532 l_comp_no, l_num_comp);
3534 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3537 /* we try to correct */
3538 l_comp_no = backup_compno % l_num_comp;
3539 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3540 "- setting component number to %d\n",
3544 /* keep your private count of tiles */
3547 #endif /* USE_JPWL */
3549 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3550 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3554 if (p_header_size != 0) {
3555 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3563 static void j2k_write_poc(opj_j2k_t *j2k) {
3564 int len, numpchgs, i;
3566 int numcomps = j2k->image->numcomps;
3568 opj_cp_t *cp = j2k->cp;
3569 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3570 opj_tccp_t *tccp = &tcp->tccps[0];
3571 opj_cio_t *cio = j2k->cio;
3573 numpchgs = 1 + tcp->numpocs;
3574 cio_write(cio, J2K_MS_POC, 2); /* POC */
3575 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3576 cio_write(cio, len, 2); /* Lpoc */
3577 for (i = 0; i < numpchgs; i++) {
3578 opj_poc_t *poc = &tcp->pocs[i];
3579 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3580 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3581 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3582 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3583 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3584 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3585 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3586 poc->compno1 = int_min(poc->compno1, numcomps);
3587 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3592 * Writes the POC marker (Progression Order Change)
3594 * @param p_stream the stream to write data to.
3595 * @param p_j2k J2K codec.
3596 * @param p_manager the user event manager.
3598 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3599 struct opj_stream_private *p_stream,
3600 struct opj_event_mgr * p_manager )
3602 OPJ_UINT32 l_nb_comp;
3603 OPJ_UINT32 l_nb_poc;
3604 OPJ_UINT32 l_poc_size;
3605 OPJ_UINT32 l_written_size = 0;
3606 opj_tcp_v2_t *l_tcp = 00;
3607 opj_tccp_t *l_tccp = 00;
3608 OPJ_UINT32 l_poc_room;
3611 assert(p_j2k != 00);
3612 assert(p_manager != 00);
3613 assert(p_stream != 00);
3615 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3616 l_tccp = &l_tcp->tccps[0];
3617 l_nb_comp = p_j2k->m_private_image->numcomps;
3618 l_nb_poc = 1 + l_tcp->numpocs;
3620 if (l_nb_comp <= 256) {
3626 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3628 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3629 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3630 = (OPJ_BYTE*)opj_realloc(
3631 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3634 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3638 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3641 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3643 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) {
3652 * Writes the POC marker (Progression Order Change)
3654 * @param p_stream the stream to write data to.
3655 * @param p_j2k J2K codec.
3656 * @param p_manager the user event manager.
3658 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3660 OPJ_UINT32 * p_data_written,
3661 struct opj_event_mgr * p_manager )
3664 OPJ_BYTE * l_current_data = 00;
3665 OPJ_UINT32 l_nb_comp;
3666 OPJ_UINT32 l_nb_poc;
3667 OPJ_UINT32 l_poc_size;
3668 opj_image_t *l_image = 00;
3669 opj_tcp_v2_t *l_tcp = 00;
3670 opj_tccp_t *l_tccp = 00;
3671 opj_poc_t *l_current_poc = 00;
3672 OPJ_UINT32 l_poc_room;
3675 assert(p_j2k != 00);
3676 assert(p_manager != 00);
3678 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3679 l_tccp = &l_tcp->tccps[0];
3680 l_image = p_j2k->m_private_image;
3681 l_nb_comp = l_image->numcomps;
3682 l_nb_poc = 1 + l_tcp->numpocs;
3684 if (l_nb_comp <= 256) {
3691 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3693 l_current_data = p_data;
3695 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3696 l_current_data += 2;
3698 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3699 l_current_data += 2;
3701 l_current_poc = l_tcp->pocs;
3702 for (i = 0; i < l_nb_poc; ++i) {
3703 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3706 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3707 l_current_data+=l_poc_room;
3709 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3712 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3715 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3716 l_current_data+=l_poc_room;
3718 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3721 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3722 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3723 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3724 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3729 *p_data_written = l_poc_size;
3733 * Gets the maximum size taken by the writing of a POC.
3735 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3737 opj_tcp_v2_t * l_tcp = 00;
3738 OPJ_UINT32 l_nb_tiles = 0;
3739 OPJ_UINT32 l_max_poc = 0;
3742 l_tcp = p_j2k->m_cp.tcps;
3743 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3745 for (i=0;i<l_nb_tiles;++i) {
3746 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3752 return 4 + 9 * l_max_poc;
3756 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3758 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3761 OPJ_UINT32 l_nb_tiles;
3762 OPJ_UINT32 l_max = 0;
3763 opj_tcp_v2_t * l_tcp = 00;
3765 l_tcp = p_j2k->m_cp.tcps;
3766 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3768 for (i=0;i<l_nb_tiles;++i) {
3769 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3779 * Gets the maximum size taken by the headers of the SOT.
3781 * @param p_j2k the jpeg2000 codec to use.
3783 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3785 OPJ_UINT32 l_nb_bytes = 0;
3786 OPJ_UINT32 l_nb_comps;
3787 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3789 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3790 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3792 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3793 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3794 l_nb_bytes += l_nb_comps * l_coc_bytes;
3796 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3797 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3800 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3802 /*** DEVELOPER CORNER, Add room for your headers ***/
3807 static void j2k_read_poc(opj_j2k_t *j2k) {
3808 int len, numpchgs, i, old_poc;
3810 int numcomps = j2k->image->numcomps;
3812 opj_cp_t *cp = j2k->cp;
3813 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3814 opj_cio_t *cio = j2k->cio;
3816 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
3818 len = cio_read(cio, 2); /* Lpoc */
3819 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
3821 for (i = old_poc; i < numpchgs + old_poc; i++) {
3823 poc = &tcp->pocs[i];
3824 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
3825 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
3826 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
3827 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
3828 poc->compno1 = int_min(
3829 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
3830 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
3833 tcp->numpocs = numpchgs + old_poc - 1;
3837 * Reads a POC marker (Progression Order Change)
3839 * @param p_header_data the data contained in the POC box.
3840 * @param p_j2k the jpeg2000 codec.
3841 * @param p_header_size the size of the data contained in the POC marker.
3842 * @param p_manager the user event manager.
3844 opj_bool j2k_read_poc_v2 (
3845 opj_j2k_v2_t *p_j2k,
3846 OPJ_BYTE * p_header_data,
3847 OPJ_UINT32 p_header_size,
3848 struct opj_event_mgr * p_manager)
3850 OPJ_UINT32 i, l_nb_comp, l_tmp;
3851 opj_image_t * l_image = 00;
3852 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3853 OPJ_UINT32 l_chunk_size, l_comp_room;
3855 opj_cp_v2_t *l_cp = 00;
3856 opj_tcp_v2_t *l_tcp = 00;
3857 opj_poc_t *l_current_poc = 00;
3860 assert(p_header_data != 00);
3861 assert(p_j2k != 00);
3862 assert(p_manager != 00);
3864 l_image = p_j2k->m_private_image;
3865 l_nb_comp = l_image->numcomps;
3866 if (l_nb_comp <= 256) {
3872 l_chunk_size = 5 + 2 * l_comp_room;
3873 l_current_poc_nb = p_header_size / l_chunk_size;
3874 l_current_poc_remaining = p_header_size % l_chunk_size;
3876 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3877 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3881 l_cp = &(p_j2k->m_cp);
3882 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3883 &l_cp->tcps[p_j2k->m_current_tile_number] :
3884 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3885 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3886 l_current_poc_nb += l_old_poc_nb;
3888 assert(l_current_poc_nb < 32);
3890 /* now poc is in use.*/
3893 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3894 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3895 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3897 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3898 p_header_data+=l_comp_room;
3899 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3901 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3903 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3904 p_header_data+=l_comp_room;
3905 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3907 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3908 /* make sure comp is in acceptable bounds */
3909 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3913 l_tcp->numpocs = l_current_poc_nb - 1;
3917 static void j2k_read_crg(opj_j2k_t *j2k) {
3918 int len, i, Xcrg_i, Ycrg_i;
3920 opj_cio_t *cio = j2k->cio;
3921 int numcomps = j2k->image->numcomps;
3923 len = cio_read(cio, 2); /* Lcrg */
3924 for (i = 0; i < numcomps; i++) {
3925 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
3926 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
3931 * Reads a CRG marker (Component registration)
3933 * @param p_header_data the data contained in the TLM box.
3934 * @param p_j2k the jpeg2000 codec.
3935 * @param p_header_size the size of the data contained in the TLM marker.
3936 * @param p_manager the user event manager.
3938 opj_bool j2k_read_crg_v2 (
3939 opj_j2k_v2_t *p_j2k,
3940 OPJ_BYTE * p_header_data,
3941 OPJ_UINT32 p_header_size,
3942 struct opj_event_mgr * p_manager
3945 OPJ_UINT32 l_nb_comp;
3947 assert(p_header_data != 00);
3948 assert(p_j2k != 00);
3949 assert(p_manager != 00);
3951 l_nb_comp = p_j2k->m_private_image->numcomps;
3953 if (p_header_size != l_nb_comp *4) {
3954 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3957 /* Do not care of this at the moment since only local variables are set here */
3960 (i = 0; i < l_nb_comp; ++i)
3962 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3964 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3971 static void j2k_read_tlm(opj_j2k_t *j2k) {
3972 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
3973 long int Ttlm_i, Ptlm_i;
3975 opj_cio_t *cio = j2k->cio;
3977 len = cio_read(cio, 2); /* Ltlm */
3978 Ztlm = cio_read(cio, 1); /* Ztlm */
3979 Stlm = cio_read(cio, 1); /* Stlm */
3980 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
3981 SP = (Stlm >> 6) & 0x01;
3982 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
3983 for (i = 0; i < tile_tlm; i++) {
3984 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
3985 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
3990 * Reads a TLM marker (Tile Length Marker)
3992 * @param p_header_data the data contained in the TLM box.
3993 * @param p_j2k the jpeg2000 codec.
3994 * @param p_header_size the size of the data contained in the TLM marker.
3995 * @param p_manager the user event manager.
3997 opj_bool j2k_read_tlm_v2 (
3998 opj_j2k_v2_t *p_j2k,
3999 OPJ_BYTE * p_header_data,
4000 OPJ_UINT32 p_header_size,
4001 struct opj_event_mgr * p_manager
4004 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
4006 assert(p_header_data != 00);
4007 assert(p_j2k != 00);
4008 assert(p_manager != 00);
4010 if (p_header_size < 2) {
4011 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4016 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
4018 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
4021 l_ST = ((l_Stlm >> 4) & 0x3);
4022 l_SP = (l_Stlm >> 6) & 0x1;
4024 l_Ptlm_size = (l_SP + 1) * 2;
4025 l_quotient = l_Ptlm_size + l_ST;
4027 l_tot_num_tp = p_header_size / l_quotient;
4028 l_tot_num_tp_remaining = p_header_size % l_quotient;
4030 if (l_tot_num_tp_remaining != 0) {
4031 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
4034 /* FIXME Do not care of this at the moment since only local variables are set here */
4037 (i = 0; i < l_tot_num_tp; ++i)
4039 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
4040 p_header_data += l_ST;
4041 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
4042 p_header_data += l_Ptlm_size;
4047 static void j2k_read_plm(opj_j2k_t *j2k) {
4048 int len, i, Zplm, Nplm, add, packet_len = 0;
4050 opj_cio_t *cio = j2k->cio;
4052 len = cio_read(cio, 2); /* Lplm */
4053 Zplm = cio_read(cio, 1); /* Zplm */
4056 Nplm = cio_read(cio, 4); /* Nplm */
4058 for (i = Nplm; i > 0; i--) {
4059 add = cio_read(cio, 1);
4061 packet_len = (packet_len << 7) + add; /* Iplm_ij */
4062 if ((add & 0x80) == 0) {
4073 * Reads a PLM marker (Packet length, main header marker)
4075 * @param p_header_data the data contained in the TLM box.
4076 * @param p_j2k the jpeg2000 codec.
4077 * @param p_header_size the size of the data contained in the TLM marker.
4078 * @param p_manager the user event manager.
4080 opj_bool j2k_read_plm_v2 (
4081 opj_j2k_v2_t *p_j2k,
4082 OPJ_BYTE * p_header_data,
4083 OPJ_UINT32 p_header_size,
4084 struct opj_event_mgr * p_manager
4088 assert(p_header_data != 00);
4089 assert(p_j2k != 00);
4090 assert(p_manager != 00);
4092 if (p_header_size < 1) {
4093 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4096 /* Do not care of this at the moment since only local variables are set here */
4098 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4105 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4107 p_header_size -= (1+l_Nplm);
4111 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4115 (i = 0; i < l_Nplm; ++i)
4117 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4119 // take only the last seven bytes
4120 l_packet_len |= (l_tmp & 0x7f);
4128 // store packet length and proceed to next packet
4135 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4143 static void j2k_read_plt(opj_j2k_t *j2k) {
4144 int len, i, Zplt, packet_len = 0, add;
4146 opj_cio_t *cio = j2k->cio;
4148 len = cio_read(cio, 2); /* Lplt */
4149 Zplt = cio_read(cio, 1); /* Zplt */
4150 for (i = len - 3; i > 0; i--) {
4151 add = cio_read(cio, 1);
4152 packet_len = (packet_len << 7) + add; /* Iplt_i */
4153 if ((add & 0x80) == 0) {
4161 * Reads a PLT marker (Packet length, tile-part header)
4163 * @param p_header_data the data contained in the PLT box.
4164 * @param p_j2k the jpeg2000 codec.
4165 * @param p_header_size the size of the data contained in the PLT marker.
4166 * @param p_manager the user event manager.
4168 opj_bool j2k_read_plt_v2 (
4169 opj_j2k_v2_t *p_j2k,
4170 OPJ_BYTE * p_header_data,
4171 OPJ_UINT32 p_header_size,
4172 struct opj_event_mgr * p_manager
4175 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4178 assert(p_header_data != 00);
4179 assert(p_j2k != 00);
4180 assert(p_manager != 00);
4182 if (p_header_size < 1) {
4183 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4187 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4191 for (i = 0; i < p_header_size; ++i) {
4192 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4194 /* take only the last seven bytes */
4195 l_packet_len |= (l_tmp & 0x7f);
4200 /* store packet length and proceed to next packet */
4205 if (l_packet_len != 0) {
4206 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4213 static void j2k_read_ppm(opj_j2k_t *j2k) {
4214 int len, Z_ppm, i, j;
4217 opj_cp_t *cp = j2k->cp;
4218 opj_cio_t *cio = j2k->cio;
4220 len = cio_read(cio, 2);
4223 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4226 if (cp->ppm_previous == 0) {
4227 N_ppm = cio_read(cio, 4); /* N_ppm */
4230 N_ppm = cp->ppm_previous;
4233 if (Z_ppm == 0) { /* First PPM marker */
4234 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4235 cp->ppm_data_first = cp->ppm_data;
4236 cp->ppm_len = N_ppm;
4237 } else { /* NON-first PPM marker */
4238 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4241 /* this memory allocation check could be done even in non-JPWL cases */
4243 if (!cp->ppm_data) {
4244 opj_event_msg(j2k->cinfo, EVT_ERROR,
4245 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4247 if (!JPWL_ASSUME || JPWL_ASSUME) {
4248 opj_free(cp->ppm_data);
4249 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4256 cp->ppm_data_first = cp->ppm_data;
4257 cp->ppm_len = N_ppm + cp->ppm_store;
4259 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4260 cp->ppm_data[j] = cio_read(cio, 1);
4264 break; /* Case of non-finished packet header in present marker but finished in next one */
4266 cp->ppm_previous = i - 1;
4271 * Reads a PPM marker (Packed packet headers, main header)
4273 * @param p_header_data the data contained in the POC box.
4274 * @param p_j2k the jpeg2000 codec.
4275 * @param p_header_size the size of the data contained in the POC marker.
4276 * @param p_manager the user event manager.
4279 opj_bool j2k_read_ppm_v2 (
4280 opj_j2k_v2_t *p_j2k,
4281 OPJ_BYTE * p_header_data,
4282 OPJ_UINT32 p_header_size,
4283 struct opj_event_mgr * p_manager
4287 opj_cp_v2_t *l_cp = 00;
4288 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4291 assert(p_header_data != 00);
4292 assert(p_j2k != 00);
4293 assert(p_manager != 00);
4295 if (p_header_size < 1) {
4296 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4300 l_cp = &(p_j2k->m_cp);
4303 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4307 /* First PPM marker */
4309 if (p_header_size < 4) {
4310 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4314 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4318 /* First PPM marker: Initialization */
4319 l_cp->ppm_len = l_N_ppm;
4320 l_cp->ppm_data_size = 0;
4322 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4323 if (l_cp->ppm_buffer == 00) {
4324 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4327 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4329 l_cp->ppm_data = l_cp->ppm_buffer;
4333 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4334 if (p_header_size >= 4) {
4336 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4339 l_cp->ppm_len += l_N_ppm ;
4341 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4342 if (l_cp->ppm_buffer == 00) {
4343 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4346 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4348 l_cp->ppm_data = l_cp->ppm_buffer;
4355 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4357 if (l_remaining_data <= p_header_size) {
4358 /* we must store less information than available in the packet */
4359 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4360 l_cp->ppm_data_size = l_cp->ppm_len;
4361 p_header_size -= l_remaining_data;
4362 p_header_data += l_remaining_data;
4365 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4366 l_cp->ppm_data_size += p_header_size;
4367 p_header_data += p_header_size;
4380 * Reads a PPM marker (Packed packet headers, main header)
4382 * @param p_header_data the data contained in the POC box.
4383 * @param p_j2k the jpeg2000 codec.
4384 * @param p_header_size the size of the data contained in the POC marker.
4385 * @param p_manager the user event manager.
4387 opj_bool j2k_read_ppm_v3 (
4388 opj_j2k_v2_t *p_j2k,
4389 OPJ_BYTE * p_header_data,
4390 OPJ_UINT32 p_header_size,
4391 struct opj_event_mgr * p_manager
4394 opj_cp_v2_t *l_cp = 00;
4395 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4398 assert(p_header_data != 00);
4399 assert(p_j2k != 00);
4400 assert(p_manager != 00);
4402 /* Minimum size of PPM marker is equal to the size of Zppm element */
4403 if (p_header_size < 1) {
4404 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4408 l_cp = &(p_j2k->m_cp);
4411 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4415 /* First PPM marker */
4417 /* We need now at least the Nppm^0 element */
4418 if (p_header_size < 4) {
4419 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4423 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4427 /* First PPM marker: Initialization */
4428 l_cp->ppm_len = l_N_ppm;
4429 l_cp->ppm_data_read = 0;
4431 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4432 if (l_cp->ppm_data == 00) {
4433 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4436 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4438 l_cp->ppm_data_current = l_cp->ppm_data;
4440 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4443 if (p_header_size < 4) {
4444 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4448 /* Uncompleted Ippm series in the previous PPM marker?*/
4449 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4450 /* Get the place where add the remaining Ippm series*/
4451 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4452 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4455 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4459 /* Increase the size of ppm_data to add the new Ippm series*/
4460 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4462 /* Keep the position of the place where concatenate the new series*/
4463 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4464 l_cp->ppm_len += l_N_ppm;
4469 l_remaining_data = p_header_size;
4471 while (l_remaining_data >= l_N_ppm) {
4472 /* read a complete Ippm series*/
4473 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4474 p_header_size -= l_N_ppm;
4475 p_header_data += l_N_ppm;
4477 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4481 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4486 l_remaining_data = p_header_size;
4490 l_remaining_data = p_header_size;
4492 /* Next Ippm series is a complete series ?*/
4493 if (l_remaining_data > l_N_ppm) {
4494 /* Increase the size of ppm_data to add the new Ippm series*/
4495 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4497 /* Keep the position of the place where concatenate the new series */
4498 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4499 l_cp->ppm_len += l_N_ppm;
4504 /* Need to read an incomplete Ippm series*/
4505 if (l_remaining_data) {
4506 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4508 /* Keep the position of the place where concatenate the new series*/
4509 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4510 l_cp->ppm_len += l_N_ppm;
4512 /* Read incomplete Ippm series*/
4513 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4514 p_header_size -= l_remaining_data;
4515 p_header_data += l_remaining_data;
4517 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4522 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4523 if (p_header_size >= 4) {
4525 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4528 l_cp->ppm_len += l_N_ppm ;
4530 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4531 if (l_cp->ppm_buffer == 00) {
4532 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4535 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4537 l_cp->ppm_data = l_cp->ppm_buffer;
4544 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4546 if (l_remaining_data <= p_header_size) {
4547 /* we must store less information than available in the packet */
4548 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4549 l_cp->ppm_data_size = l_cp->ppm_len;
4550 p_header_size -= l_remaining_data;
4551 p_header_data += l_remaining_data;
4554 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4555 l_cp->ppm_data_size += p_header_size;
4556 p_header_data += p_header_size;
4565 static void j2k_read_ppt(opj_j2k_t *j2k) {
4566 int len, Z_ppt, i, j = 0;
4568 opj_cp_t *cp = j2k->cp;
4569 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4570 opj_cio_t *cio = j2k->cio;
4572 len = cio_read(cio, 2);
4573 Z_ppt = cio_read(cio, 1);
4575 if (Z_ppt == 0) { /* First PPT marker */
4576 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4577 tcp->ppt_data_first = tcp->ppt_data;
4579 tcp->ppt_len = len - 3;
4580 } else { /* NON-first PPT marker */
4581 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4582 tcp->ppt_data_first = tcp->ppt_data;
4583 tcp->ppt_len = len - 3 + tcp->ppt_store;
4586 for (i = len - 3; i > 0; i--) {
4587 tcp->ppt_data[j] = cio_read(cio, 1);
4594 * Reads a PPT marker (Packed packet headers, tile-part header)
4596 * @param p_header_data the data contained in the PPT box.
4597 * @param p_j2k the jpeg2000 codec.
4598 * @param p_header_size the size of the data contained in the PPT marker.
4599 * @param p_manager the user event manager.
4601 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4602 OPJ_BYTE * p_header_data,
4603 OPJ_UINT32 p_header_size,
4604 struct opj_event_mgr * p_manager )
4606 opj_cp_v2_t *l_cp = 00;
4607 opj_tcp_v2_t *l_tcp = 00;
4611 assert(p_header_data != 00);
4612 assert(p_j2k != 00);
4613 assert(p_manager != 00);
4615 /* We need to have the Z_ppt element at minimum */
4616 if (p_header_size < 1) {
4617 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4621 l_cp = &(p_j2k->m_cp);
4623 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");
4627 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4630 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4634 /* Allocate buffer to read the packet header */
4636 /* First PPT marker */
4637 l_tcp->ppt_data_size = 0;
4638 l_tcp->ppt_len = p_header_size;
4640 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4641 if (l_tcp->ppt_buffer == 00) {
4642 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4645 l_tcp->ppt_data = l_tcp->ppt_buffer;
4647 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4650 l_tcp->ppt_len += p_header_size;
4652 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4653 if (l_tcp->ppt_buffer == 00) {
4654 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4657 l_tcp->ppt_data = l_tcp->ppt_buffer;
4659 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4662 /* Read packet header from buffer */
4663 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4665 l_tcp->ppt_data_size += p_header_size;
4671 * Writes the TLM marker (Tile Length Marker)
4673 * @param p_stream the stream to write data to.
4674 * @param p_j2k J2K codec.
4675 * @param p_manager the user event manager.
4677 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4678 struct opj_stream_private *p_stream,
4679 struct opj_event_mgr * p_manager )
4681 OPJ_BYTE * l_current_data = 00;
4682 OPJ_UINT32 l_tlm_size;
4685 assert(p_j2k != 00);
4686 assert(p_manager != 00);
4687 assert(p_stream != 00);
4689 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4691 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4692 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4693 = (OPJ_BYTE*)opj_realloc(
4694 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4697 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4701 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4704 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4706 /* change the way data is written to avoid seeking if possible */
4708 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4710 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4711 l_current_data += 2;
4713 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4714 l_current_data += 2;
4716 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4719 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4722 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4723 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) {
4730 static void j2k_write_tlm(opj_j2k_t *j2k){
4732 opj_cio_t *cio = j2k->cio;
4733 j2k->tlm_start = cio_tell(cio);
4734 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4735 lenp = 4 + (5*j2k->totnum_tp);
4736 cio_write(cio,lenp,2); /* Ltlm */
4737 cio_write(cio, 0,1); /* Ztlm=0*/
4738 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4739 cio_skip(cio,5*j2k->totnum_tp);
4742 static void j2k_write_sot(opj_j2k_t *j2k) {
4745 opj_cio_t *cio = j2k->cio;
4747 j2k->sot_start = cio_tell(cio);
4748 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4749 lenp = cio_tell(cio);
4750 cio_skip(cio, 2); /* Lsot (further) */
4751 cio_write(cio, j2k->curtileno, 2); /* Isot */
4752 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4753 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4754 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4755 len = cio_tell(cio) - lenp;
4756 cio_seek(cio, lenp);
4757 cio_write(cio, len, 2); /* Lsot */
4758 cio_seek(cio, lenp + len);
4762 /* update markers struct */
4763 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4764 #endif /* USE_JPWL */
4767 if( j2k->cstr_info && j2k->cur_tp_num==0){
4768 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4773 * Writes the SOT marker (Start of tile-part)
4775 * @param p_stream the stream to write data to.
4776 * @param p_j2k J2K codec.
4777 * @param p_manager the user event manager.
4779 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4781 OPJ_UINT32 * p_data_written,
4782 const struct opj_stream_private *p_stream,
4783 struct opj_event_mgr * p_manager )
4786 assert(p_j2k != 00);
4787 assert(p_manager != 00);
4788 assert(p_stream != 00);
4790 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4793 opj_write_bytes(p_data,10,2); /* Lsot */
4796 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4802 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4805 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4810 /* update markers struct */
4812 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4814 assert( 0 && "TODO" );
4815 #endif /* USE_JPWL */
4817 * p_data_written = 12;
4826 * Reads a PPT marker (Packed packet headers, tile-part header)
4828 * @param p_header_data the data contained in the PPT box.
4829 * @param p_j2k the jpeg2000 codec.
4830 * @param p_header_size the size of the data contained in the PPT marker.
4831 * @param p_manager the user event manager.
4833 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4834 OPJ_BYTE * p_header_data,
4835 OPJ_UINT32 p_header_size,
4836 opj_event_mgr_t * p_manager )
4838 opj_cp_v2_t *l_cp = 00;
4839 opj_tcp_v2_t *l_tcp = 00;
4840 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4841 OPJ_UINT32 l_current_part;
4842 OPJ_UINT32 l_tile_x,l_tile_y;
4845 assert(p_header_data != 00);
4846 assert(p_j2k != 00);
4847 assert(p_manager != 00);
4849 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4850 if (p_header_size != 8) {
4851 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4855 l_cp = &(p_j2k->m_cp);
4856 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4859 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4860 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4861 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4864 if (l_cp->correct) {
4866 int tileno = p_j2k->m_current_tile_number;
4867 static int backup_tileno = 0;
4869 /* tileno is negative or larger than the number of tiles!!! */
4870 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4871 opj_event_msg_v2(p_manager, EVT_ERROR,
4872 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4873 tileno, (l_cp->tw * l_cp->th));
4875 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4878 /* we try to correct */
4879 tileno = backup_tileno;
4880 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4881 "- setting tile number to %d\n",
4885 /* keep your private count of tiles */
4888 #endif /* USE_JPWL */
4890 /* look for the tile in the list of already processed tile (in parts). */
4891 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4892 /* since the time taken by this function can only grow at the time */
4894 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4897 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4898 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4900 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4906 if (l_cp->correct) {
4908 /* totlen is negative or larger than the bytes left!!! */
4909 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))) { */
4910 opj_event_msg_v2(p_manager, EVT_ERROR,
4911 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4912 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4914 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4917 /* we try to correct */
4919 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4920 "- setting Psot to %d => assuming it is the last tile\n",
4924 #endif /* USE_JPWL */
4926 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4928 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4929 "we assuming it is the last tile-part of the codestream.\n");
4930 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4933 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4936 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4939 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4940 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4941 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4942 if (l_tcp->m_nb_tile_parts) {
4943 if (l_current_part >= l_tcp->m_nb_tile_parts){
4944 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4945 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4946 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4950 l_tcp->m_nb_tile_parts = l_num_parts;
4953 /* If know the number of tile part header we will check if we didn't read the last*/
4954 if (l_tcp->m_nb_tile_parts) {
4955 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4956 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4960 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4961 /* Keep the size of data to skip after this marker */
4962 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4965 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4966 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4969 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4971 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4972 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4973 p_j2k->m_specific_param.m_decoder.m_skip_data =
4974 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4975 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4976 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4977 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4980 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4981 p_j2k->m_specific_param.m_decoder.m_skip_data =
4982 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4986 if (p_j2k->cstr_index)
4988 assert(p_j2k->cstr_index->tile_index != 00);
4989 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4990 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4992 if (l_num_parts != 0){
4993 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4994 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4997 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4998 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4999 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
5001 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5002 (opj_tp_index_t*)opj_realloc(
5003 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5004 l_num_parts* sizeof(opj_tp_index_t));
5007 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
5009 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
5010 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
5011 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5012 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
5013 sizeof(opj_tp_index_t));
5016 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
5017 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
5018 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
5019 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
5020 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
5021 * sizeof(opj_tp_index_t));
5030 /* 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 */
5031 /* if (p_j2k->cstr_info) {
5034 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
5037 p_j2k->cstr_info->tile[tileno].tileno = tileno;
5038 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
5039 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
5040 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
5043 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
5046 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
5050 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
5053 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
5054 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
5055 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
5060 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
5063 opj_tcp_t *tcp = NULL;
5064 opj_codestream_info_t *cstr_info = NULL;
5066 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
5067 opj_cp_t *cp = j2k->cp;
5068 opj_cio_t *cio = j2k->cio;
5070 tcd->tp_num = j2k->tp_num ;
5071 tcd->cur_tp_num = j2k->cur_tp_num;
5073 cio_write(cio, J2K_MS_SOD, 2);
5075 if( j2k->cstr_info && j2k->cur_tp_num==0){
5076 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5079 if (j2k->curtileno == 0) {
5080 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5084 cstr_info = j2k->cstr_info;
5086 if (!j2k->cur_tp_num ) {
5087 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5088 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5091 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5092 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5096 /* update markers struct */
5097 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5098 #endif /* USE_JPWL */
5103 tcp = &cp->tcps[j2k->curtileno];
5104 for (layno = 0; layno < tcp->numlayers; layno++) {
5105 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5106 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5107 } else if (tcp->rates[layno]) {
5108 tcp->rates[layno]=1;
5111 if(j2k->cur_tp_num == 0){
5112 tcd->tcd_image->tiles->packno = 0;
5114 cstr_info->packno = 0;
5117 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5119 /* Writing Psot in SOT marker */
5120 totlen = cio_tell(cio) + l - j2k->sot_start;
5121 cio_seek(cio, j2k->sot_start + 6);
5122 cio_write(cio, totlen, 4);
5123 cio_seek(cio, j2k->sot_start + totlen);
5124 /* Writing Ttlm and Ptlm in TLM marker */
5126 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5127 cio_write(cio, j2k->curtileno, 1);
5128 cio_write(cio, totlen, 4);
5130 cio_seek(cio, j2k->sot_start + totlen);
5134 * Writes the SOD marker (Start of data)
5136 * @param p_stream the stream to write data to.
5137 * @param p_j2k J2K codec.
5138 * @param p_manager the user event manager.
5140 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5141 struct opj_tcd_v2 * p_tile_coder,
5143 OPJ_UINT32 * p_data_written,
5144 OPJ_UINT32 p_total_data_size,
5145 const struct opj_stream_private *p_stream,
5146 struct opj_event_mgr * p_manager )
5148 opj_tcp_v2_t *l_tcp = 00;
5149 opj_codestream_info_t *l_cstr_info = 00;
5150 opj_cp_v2_t *l_cp = 00;
5152 OPJ_UINT32 l_size_tile;
5153 OPJ_UINT32 l_remaining_data;
5156 assert(p_j2k != 00);
5157 assert(p_manager != 00);
5158 assert(p_stream != 00);
5160 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5163 /* make room for the EOF marker */
5164 l_remaining_data = p_total_data_size - 4;
5166 l_cp = &(p_j2k->m_cp);
5167 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5170 /* update tile coder */
5171 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5172 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5174 l_size_tile = l_cp->th * l_cp->tw;
5177 /* TODO mergeV2: check this part which use cstr_info */
5178 /*l_cstr_info = p_j2k->cstr_info;
5180 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5181 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5182 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5188 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5190 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5195 /* update markers struct */
5196 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5198 assert( 0 && "TODO" );
5199 #endif /* USE_JPWL */
5204 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5205 p_tile_coder->tcd_image->tiles->packno = 0;
5207 l_cstr_info->packno = 0;
5211 *p_data_written = 0;
5213 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)) {
5214 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5218 *p_data_written += 2;
5227 * Reads a SOD marker (Start Of Data)
5229 * @param p_header_data the data contained in the SOD box.
5230 * @param p_j2k the jpeg2000 codec.
5231 * @param p_header_size the size of the data contained in the SOD marker.
5232 * @param p_manager the user event manager.
5234 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
5235 opj_stream_private_t *p_stream,
5236 opj_event_mgr_t * p_manager
5239 OPJ_UINT32 l_current_read_size;
5240 opj_codestream_index_t * l_cstr_index = 00;
5241 OPJ_BYTE ** l_current_data = 00;
5242 opj_tcp_v2_t * l_tcp = 00;
5243 OPJ_UINT32 * l_tile_len = 00;
5246 assert(p_j2k != 00);
5247 assert(p_manager != 00);
5248 assert(p_stream != 00);
5250 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5252 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5253 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5254 // but we are in the last tile part,
5255 // so its result will fit on OPJ_UINT32 unless we find
5256 // a file with a single tile part of more than 4 GB...*/
5257 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5260 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5262 l_current_data = &(l_tcp->m_data);
5263 l_tile_len = &l_tcp->m_data_size;
5265 if (! *l_current_data) {
5266 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5269 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5272 if (*l_current_data == 00) {
5273 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5279 l_cstr_index = p_j2k->cstr_index;
5281 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5283 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5284 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5286 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5287 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5289 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5293 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5295 /*l_cstr_index->packno = 0;*/
5298 l_current_read_size = opj_stream_read_data( p_stream,
5299 *l_current_data + *l_tile_len,
5300 p_j2k->m_specific_param.m_decoder.m_sot_length,
5303 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5304 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5307 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5310 *l_tile_len += l_current_read_size;
5316 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5317 opj_cp_t *cp = j2k->cp;
5318 opj_tcp_t *tcp = &cp->tcps[tileno];
5319 opj_cio_t *cio = j2k->cio;
5320 int numcomps = j2k->image->numcomps;
5322 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5323 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5324 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5325 cio_write(cio, 0, 1); /* Srgn */
5326 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5330 * Writes the RGN marker (Region Of Interest)
5332 * @param p_tile_no the tile to output
5333 * @param p_comp_no the component to output
5334 * @param p_stream the stream to write data to.
5335 * @param p_j2k J2K codec.
5336 * @param p_manager the user event manager.
5338 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5339 OPJ_UINT32 p_tile_no,
5340 OPJ_UINT32 p_comp_no,
5341 struct opj_stream_private *p_stream,
5342 struct opj_event_mgr * p_manager )
5344 OPJ_BYTE * l_current_data = 00;
5345 OPJ_UINT32 l_nb_comp;
5346 OPJ_UINT32 l_rgn_size;
5347 opj_image_t *l_image = 00;
5348 opj_cp_v2_t *l_cp = 00;
5349 opj_tcp_v2_t *l_tcp = 00;
5350 opj_tccp_t *l_tccp = 00;
5351 OPJ_UINT32 l_comp_room;
5354 assert(p_j2k != 00);
5355 assert(p_manager != 00);
5356 assert(p_stream != 00);
5358 l_cp = &(p_j2k->m_cp);
5359 l_tcp = &l_cp->tcps[p_tile_no];
5360 l_tccp = &l_tcp->tccps[p_comp_no];
5362 l_nb_comp = l_image->numcomps;
5364 if (l_nb_comp <= 256) {
5371 l_rgn_size = 6 + l_comp_room;
5373 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5375 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5376 l_current_data += 2;
5378 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5379 l_current_data += 2;
5381 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5382 l_current_data+=l_comp_room;
5384 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5387 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5390 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) {
5398 static void j2k_write_eoc(opj_j2k_t *j2k) {
5399 opj_cio_t *cio = j2k->cio;
5400 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5401 cio_write(cio, J2K_MS_EOC, 2);
5405 /* update markers struct */
5406 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5407 #endif /* USE_JPWL */
5412 * Writes the EOC marker (End of Codestream)
5414 * @param p_stream the stream to write data to.
5415 * @param p_j2k J2K codec.
5416 * @param p_manager the user event manager.
5418 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5419 struct opj_stream_private *p_stream,
5420 struct opj_event_mgr * p_manager )
5423 assert(p_j2k != 00);
5424 assert(p_manager != 00);
5425 assert(p_stream != 00);
5427 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5432 /* update markers struct */
5434 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5436 #endif /* USE_JPWL */
5438 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5442 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5451 * Reads a RGN marker (Region Of Interest)
5453 * @param p_header_data the data contained in the POC box.
5454 * @param p_j2k the jpeg2000 codec.
5455 * @param p_header_size the size of the data contained in the POC marker.
5456 * @param p_manager the user event manager.
5458 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
5459 OPJ_BYTE * p_header_data,
5460 OPJ_UINT32 p_header_size,
5461 opj_event_mgr_t * p_manager
5464 OPJ_UINT32 l_nb_comp;
5465 opj_image_t * l_image = 00;
5467 opj_cp_v2_t *l_cp = 00;
5468 opj_tcp_v2_t *l_tcp = 00;
5469 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5472 assert(p_header_data != 00);
5473 assert(p_j2k != 00);
5474 assert(p_manager != 00);
5476 l_image = p_j2k->m_private_image;
5477 l_nb_comp = l_image->numcomps;
5479 if (l_nb_comp <= 256) {
5484 if (p_header_size != 2 + l_comp_room) {
5485 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5489 l_cp = &(p_j2k->m_cp);
5490 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5491 &l_cp->tcps[p_j2k->m_current_tile_number] :
5492 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5494 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5495 p_header_data+=l_comp_room;
5496 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5500 if (l_cp->correct) {
5501 /* totlen is negative or larger than the bytes left!!! */
5502 if (l_comp_room >= l_nb_comp) {
5503 opj_event_msg_v2(p_manager, EVT_ERROR,
5504 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5505 l_comp_room, l_nb_comp);
5506 if (!JPWL_ASSUME || JPWL_ASSUME) {
5507 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5512 #endif /* USE_JPWL */
5514 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5521 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5523 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5526 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5533 * Updates the rates of the tcp.
5535 * @param p_stream the stream to write data to.
5536 * @param p_j2k J2K codec.
5537 * @param p_manager the user event manager.
5539 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5540 struct opj_stream_private *p_stream,
5541 struct opj_event_mgr * p_manager )
5543 opj_cp_v2_t * l_cp = 00;
5544 opj_image_t * l_image = 00;
5545 opj_tcp_v2_t * l_tcp = 00;
5546 opj_image_comp_t * l_img_comp = 00;
5549 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5550 OPJ_FLOAT32 * l_rates = 0;
5551 OPJ_FLOAT32 l_sot_remove;
5552 OPJ_UINT32 l_bits_empty, l_size_pixel;
5553 OPJ_UINT32 l_tile_size = 0;
5554 OPJ_UINT32 l_last_res;
5555 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5558 assert(p_j2k != 00);
5559 assert(p_manager != 00);
5560 assert(p_stream != 00);
5563 l_cp = &(p_j2k->m_cp);
5564 l_image = p_j2k->m_private_image;
5567 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5568 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5569 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5571 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5572 l_tp_stride_func = get_tp_stride;
5575 l_tp_stride_func = get_default_stride;
5578 for (i=0;i<l_cp->th;++i) {
5579 for (j=0;j<l_cp->tw;++j) {
5580 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5582 /* 4 borders of the tile rescale on the image if necessary */
5583 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5584 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5585 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5586 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5588 l_rates = l_tcp->rates;
5590 /* Modification of the RATE >> */
5592 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5594 ((*l_rates) * l_bits_empty)
5602 for (k = 1; k < l_tcp->numlayers; ++k) {
5604 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5606 ((*l_rates) * l_bits_empty)
5622 for (i=0;i<l_cp->th;++i) {
5623 for (j=0;j<l_cp->tw;++j) {
5624 l_rates = l_tcp->rates;
5627 *l_rates -= l_sot_remove;
5629 if (*l_rates < 30) {
5636 l_last_res = l_tcp->numlayers - 1;
5638 for (k = 1; k < l_last_res; ++k) {
5641 *l_rates -= l_sot_remove;
5643 if (*l_rates < *(l_rates - 1) + 10) {
5644 *l_rates = (*(l_rates - 1)) + 20;
5652 *l_rates -= (l_sot_remove + 2.f);
5654 if (*l_rates < *(l_rates - 1) + 10) {
5655 *l_rates = (*(l_rates - 1)) + 20;
5663 l_img_comp = l_image->comps;
5666 for (i=0;i<l_image->numcomps;++i) {
5667 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5669 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5677 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5679 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5681 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5682 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5683 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5684 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5688 if (l_cp->m_specific_param.m_enc.m_cinema) {
5689 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5690 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5691 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5695 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5696 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5702 static void j2k_read_eoc(opj_j2k_t *j2k) {
5704 opj_bool success = OPJ_FALSE;
5706 /* if packets should be decoded */
5707 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5708 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5709 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5710 for (i = 0; i < j2k->cp->tileno_size; i++) {
5711 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5712 if (j2k->cp->tileno[i] != -1)
5714 tileno = j2k->cp->tileno[i];
5715 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5716 opj_free(j2k->tile_data[tileno]);
5717 j2k->tile_data[tileno] = NULL;
5718 tcd_free_decode_tile(tcd, i);
5721 success = OPJ_FALSE;
5722 if (success == OPJ_FALSE) {
5723 j2k->state |= J2K_STATE_ERR;
5727 tcd_free_decode(tcd);
5730 /* if packets should not be decoded */
5732 for (i = 0; i < j2k->cp->tileno_size; i++) {
5733 tileno = j2k->cp->tileno[i];
5734 opj_free(j2k->tile_data[tileno]);
5735 j2k->tile_data[tileno] = NULL;
5738 if (j2k->state & J2K_STATE_ERR)
5739 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5741 j2k->state = J2K_STATE_MT;
5745 * Reads a EOC marker (End Of Codestream)
5747 * @param p_header_data the data contained in the SOD box.
5748 * @param p_j2k the jpeg2000 codec.
5749 * @param p_header_size the size of the data contained in the SOD marker.
5750 * @param p_manager the user event manager.
5753 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5754 struct opj_stream_private *p_stream,
5755 struct opj_event_mgr * p_manager )
5758 opj_tcd_v2_t * l_tcd = 00;
5759 OPJ_UINT32 l_nb_tiles;
5760 opj_tcp_v2_t * l_tcp = 00;
5764 assert(p_j2k != 00);
5765 assert(p_manager != 00);
5766 assert(p_stream != 00);
5768 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5769 l_tcp = p_j2k->m_cp.tcps;
5771 l_tcd = tcd_create_v2(OPJ_TRUE);
5773 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5777 for (i = 0; i < l_nb_tiles; ++i) {
5778 if (l_tcp->m_data) {
5779 if (! tcd_init_decode_tile(l_tcd, i)) {
5780 tcd_destroy_v2(l_tcd);
5781 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5785 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5789 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5794 j2k_tcp_destroy(l_tcp);
5798 tcd_destroy_v2(l_tcd);
5804 * Gets the offset of the header.
5806 * @param p_stream the stream to write data to.
5807 * @param p_j2k J2K codec.
5808 * @param p_manager the user event manager.
5810 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5811 struct opj_stream_private *p_stream,
5812 struct opj_event_mgr * p_manager )
5815 assert(p_j2k != 00);
5816 assert(p_manager != 00);
5817 assert(p_stream != 00);
5819 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5825 * Writes the MCT marker (Multiple Component Transform)
5827 * @param p_stream the stream to write data to.
5828 * @param p_j2k J2K codec.
5829 * @param p_manager the user event manager.
5831 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5832 struct opj_stream_private *p_stream,
5833 struct opj_event_mgr * p_manager )
5836 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5837 opj_mct_data_t * l_mct_record;
5838 opj_tcp_v2_t * l_tcp;
5841 assert(p_j2k != 00);
5842 assert(p_stream != 00);
5843 assert(p_manager != 00);
5845 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5849 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5850 l_mct_record = l_tcp->m_mct_records;
5852 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5854 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5861 l_mcc_record = l_tcp->m_mcc_records;
5863 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5865 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5872 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5880 * Writes the image components.
5882 * @param p_stream the stream to write data to.
5883 * @param p_j2k J2K codec.
5884 * @param p_manager the user event manager.
5886 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5887 struct opj_stream_private *p_stream,
5888 struct opj_event_mgr * p_manager )
5893 assert(p_j2k != 00);
5894 assert(p_manager != 00);
5895 assert(p_stream != 00);
5897 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5899 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5903 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5912 * Writes regions of interests.
5914 * @param p_stream the stream to write data to.
5915 * @param p_j2k J2K codec.
5916 * @param p_manager the user event manager.
5918 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5919 struct opj_stream_private *p_stream,
5920 struct opj_event_mgr * p_manager )
5923 const opj_tccp_t *l_tccp = 00;
5926 assert(p_j2k != 00);
5927 assert(p_manager != 00);
5928 assert(p_stream != 00);
5930 l_tccp = p_j2k->m_cp.tcps->tccps;
5932 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5933 if (l_tccp->roishift) {
5935 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5949 * @param p_stream the stream to write data to.
5950 * @param p_j2k J2K codec.
5951 * @param p_manager the user event manager.
5953 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5954 struct opj_stream_private *p_stream,
5955 struct opj_event_mgr * p_manager )
5957 opj_codestream_index_t * l_cstr_index = 00;
5960 assert(p_j2k != 00);
5961 assert(p_manager != 00);
5962 assert(p_stream != 00);
5964 l_cstr_index = p_j2k->cstr_index;
5966 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5968 /* The following adjustment is done to adjust the codestream size */
5969 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5970 /* the first bunch of bytes is not in the codestream */
5971 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5976 /* preparation of JPWL marker segments */
5980 /* encode according to JPWL */
5981 jpwl_encode(p_j2k, p_stream, image);
5985 assert( 0 && "TODO" );
5986 #endif /* USE_JPWL */
5991 typedef struct opj_dec_mstabent {
5994 /** value of the state when the marker can appear */
5996 /** action linked to the marker */
5997 void (*handler) (opj_j2k_t *j2k);
5998 } opj_dec_mstabent_t;
6000 opj_dec_mstabent_t j2k_dec_mstab[] = {
6001 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
6002 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
6003 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
6004 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
6005 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
6006 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
6007 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
6008 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
6009 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
6010 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
6011 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
6012 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
6013 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
6014 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
6015 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
6016 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
6018 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
6019 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
6022 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
6023 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
6024 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
6025 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
6026 #endif /* USE_JPWL */
6028 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
6029 {J2K_MS_INSEC, 0, j2k_read_insec},
6030 #endif /* USE_JPSEC */
6032 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
6035 static void j2k_read_unk(opj_j2k_t *j2k) {
6036 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
6039 if (j2k->cp->correct) {
6041 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
6042 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6043 id = cio_read(j2k->cio, 2);
6044 opj_event_msg(j2k->cinfo, EVT_ERROR,
6045 "JPWL: really don't know this marker %x\n",
6048 opj_event_msg(j2k->cinfo, EVT_ERROR,
6049 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
6052 /* OK, activate this at your own risk!!! */
6053 /* we look for the marker at the minimum hamming distance from this */
6054 while (j2k_dec_mstab[m].id) {
6056 /* 1's where they differ */
6057 tmp_id = j2k_dec_mstab[m].id ^ id;
6059 /* compute the hamming distance between our id and the current */
6061 for (i = 0; i < 16; i++) {
6062 if ((tmp_id >> i) & 0x0001) {
6067 /* if current distance is smaller, set the minimum */
6068 if (cur_dist < min_dist) {
6069 min_dist = cur_dist;
6070 min_id = j2k_dec_mstab[m].id;
6073 /* jump to the next marker */
6077 /* do we substitute the marker? */
6078 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6079 opj_event_msg(j2k->cinfo, EVT_ERROR,
6080 "- marker %x is at distance %d from the read %x\n",
6081 min_id, min_dist, id);
6082 opj_event_msg(j2k->cinfo, EVT_ERROR,
6083 "- trying to substitute in place and crossing fingers!\n");
6084 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6085 cio_write(j2k->cio, min_id, 2);
6088 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6093 #endif /* USE_JPWL */
6098 * Reads an unknown marker
6100 * @param p_stream the stream object to read from.
6101 * @param p_j2k the jpeg2000 codec.
6102 * @param p_manager the user event manager.
6104 * @return true if the marker could be deduced.
6106 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6107 struct opj_stream_private *p_stream,
6108 OPJ_UINT32 *output_marker,
6109 struct opj_event_mgr * p_manager
6112 OPJ_UINT32 l_unknown_marker;
6113 const opj_dec_memory_marker_handler_t * l_marker_handler;
6114 OPJ_UINT32 l_size_unk = 2;
6117 assert(p_j2k != 00);
6118 assert(p_manager != 00);
6119 assert(p_stream != 00);
6121 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6124 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6125 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6126 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6130 /* read 2 bytes as the new marker ID*/
6131 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6133 if (!(l_unknown_marker < 0xff00)) {
6135 /* Get the marker handler from the marker ID*/
6136 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6138 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6139 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6143 if (l_marker_handler->id != J2K_MS_UNK) {
6144 /* Add the marker to the codestream index*/
6145 if (l_marker_handler->id != J2K_MS_SOT)
6146 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6147 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6149 break; /* next marker is known and well located */
6157 *output_marker = l_marker_handler->id ;
6163 Read the lookup table containing all the marker, status and action
6164 @param id Marker value
6166 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6167 opj_dec_mstabent_t *e;
6168 for (e = j2k_dec_mstab; e->id != 0; e++) {
6177 * Writes the MCT marker (Multiple Component Transform)
6179 * @param p_stream the stream to write data to.
6180 * @param p_j2k J2K codec.
6181 * @param p_manager the user event manager.
6183 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6184 opj_mct_data_t * p_mct_record,
6185 struct opj_stream_private *p_stream,
6186 struct opj_event_mgr * p_manager )
6188 OPJ_UINT32 l_mct_size;
6189 OPJ_BYTE * l_current_data = 00;
6193 assert(p_j2k != 00);
6194 assert(p_manager != 00);
6195 assert(p_stream != 00);
6197 l_mct_size = 10 + p_mct_record->m_data_size;
6199 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6200 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6201 = (OPJ_BYTE*)opj_realloc(
6202 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6205 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6209 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6212 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6214 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6215 l_current_data += 2;
6217 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6218 l_current_data += 2;
6220 opj_write_bytes(l_current_data,0,2); /* Zmct */
6221 l_current_data += 2;
6223 /* only one marker atm */
6224 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6226 opj_write_bytes(l_current_data,l_tmp,2);
6227 l_current_data += 2;
6229 opj_write_bytes(l_current_data,0,2); /* Ymct */
6232 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6234 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) {
6242 * Reads a MCT marker (Multiple Component Transform)
6244 * @param p_header_data the data contained in the MCT box.
6245 * @param p_j2k the jpeg2000 codec.
6246 * @param p_header_size the size of the data contained in the MCT marker.
6247 * @param p_manager the user event manager.
6249 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6250 OPJ_BYTE * p_header_data,
6251 OPJ_UINT32 p_header_size,
6252 struct opj_event_mgr * p_manager )
6255 opj_tcp_v2_t *l_tcp = 00;
6258 opj_mct_data_t * l_mct_data;
6261 assert(p_header_data != 00);
6262 assert(p_j2k != 00);
6264 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6265 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6266 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6268 if (p_header_size < 2) {
6269 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6274 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6277 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6281 if(p_header_size <= 6) {
6282 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6286 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6287 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6290 l_indix = l_tmp & 0xff;
6291 l_mct_data = l_tcp->m_mct_records;
6293 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6294 if (l_mct_data->m_index == l_indix) {
6301 if (i == l_tcp->m_nb_mct_records) {
6302 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6303 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6305 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));
6306 if(! l_tcp->m_mct_records) {
6307 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6311 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6312 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6315 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6318 if (l_mct_data->m_data) {
6319 opj_free(l_mct_data->m_data);
6320 l_mct_data->m_data = 00;
6323 l_mct_data->m_index = l_indix;
6324 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6325 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6327 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6330 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6336 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6337 if (! l_mct_data->m_data) {
6338 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6341 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6343 l_mct_data->m_data_size = p_header_size;
6344 ++l_tcp->m_nb_mct_records;
6350 * Writes the MCC marker (Multiple Component Collection)
6352 * @param p_stream the stream to write data to.
6353 * @param p_j2k J2K codec.
6354 * @param p_manager the user event manager.
6356 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6357 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6358 struct opj_stream_private *p_stream,
6359 struct opj_event_mgr * p_manager )
6362 OPJ_UINT32 l_mcc_size;
6363 OPJ_BYTE * l_current_data = 00;
6364 OPJ_UINT32 l_nb_bytes_for_comp;
6369 assert(p_j2k != 00);
6370 assert(p_manager != 00);
6371 assert(p_stream != 00);
6373 if (p_mcc_record->m_nb_comps > 255 ) {
6374 l_nb_bytes_for_comp = 2;
6378 l_nb_bytes_for_comp = 1;
6382 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6383 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6385 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6386 = (OPJ_BYTE*)opj_realloc(
6387 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6389 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6393 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6396 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6398 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6399 l_current_data += 2;
6401 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6402 l_current_data += 2;
6405 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6406 l_current_data += 2;
6408 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6411 /* only one marker atm */
6412 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6415 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6418 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6421 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 */
6424 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6425 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6426 l_current_data+=l_nb_bytes_for_comp;
6429 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 */
6432 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6434 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6435 l_current_data+=l_nb_bytes_for_comp;
6438 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6440 if (p_mcc_record->m_decorrelation_array) {
6441 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6444 if (p_mcc_record->m_offset_array) {
6445 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6448 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6451 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) {
6459 * Reads a MCC marker (Multiple Component Collection)
6461 * @param p_header_data the data contained in the MCC box.
6462 * @param p_j2k the jpeg2000 codec.
6463 * @param p_header_size the size of the data contained in the MCC marker.
6464 * @param p_manager the user event manager.
6466 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6467 OPJ_BYTE * p_header_data,
6468 OPJ_UINT32 p_header_size,
6469 struct opj_event_mgr * p_manager )
6474 opj_tcp_v2_t * l_tcp;
6475 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6476 opj_mct_data_t * l_mct_data;
6477 OPJ_UINT32 l_nb_collections;
6478 OPJ_UINT32 l_nb_comps;
6479 OPJ_UINT32 l_nb_bytes_by_comp;
6483 assert(p_header_data != 00);
6484 assert(p_j2k != 00);
6485 assert(p_manager != 00);
6487 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6488 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6489 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6491 if (p_header_size < 2) {
6492 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6497 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6500 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6504 if (p_header_size < 7) {
6505 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6509 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6512 l_mcc_record = l_tcp->m_mcc_records;
6514 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6515 if (l_mcc_record->m_index == l_indix) {
6522 if (i == l_tcp->m_nb_mcc_records) {
6523 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6524 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6526 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6527 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6528 if (! l_tcp->m_mcc_records) {
6529 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6532 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6533 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));
6535 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6537 l_mcc_record->m_index = l_indix;
6539 /* only one marker atm */
6540 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6543 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6547 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6550 if (l_nb_collections > 1) {
6551 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6557 for (i=0;i<l_nb_collections;++i) {
6558 if (p_header_size < 3) {
6559 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6563 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6567 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6571 opj_read_bytes(p_header_data,&l_nb_comps,2);
6576 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6577 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6579 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6580 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6584 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6586 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6587 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6588 p_header_data+=l_nb_bytes_by_comp;
6591 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6596 opj_read_bytes(p_header_data,&l_nb_comps,2);
6599 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6600 l_nb_comps &= 0x7fff;
6602 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6603 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6607 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6608 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6612 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6614 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6615 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6616 p_header_data+=l_nb_bytes_by_comp;
6619 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6624 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6627 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6628 l_mcc_record->m_decorrelation_array = 00;
6629 l_mcc_record->m_offset_array = 00;
6631 l_indix = l_tmp & 0xff;
6633 l_mct_data = l_tcp->m_mct_records;
6634 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6635 if (l_mct_data->m_index == l_indix) {
6636 l_mcc_record->m_decorrelation_array = l_mct_data;
6642 if (l_mcc_record->m_decorrelation_array == 00) {
6643 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6648 l_indix = (l_tmp >> 8) & 0xff;
6650 l_mct_data = l_tcp->m_mct_records;
6651 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6652 if (l_mct_data->m_index == l_indix) {
6653 l_mcc_record->m_offset_array = l_mct_data;
6659 if (l_mcc_record->m_offset_array == 00) {
6660 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6666 if (p_header_size != 0) {
6667 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6671 ++l_tcp->m_nb_mcc_records;
6678 * Writes the MCO marker (Multiple component transformation ordering)
6680 * @param p_stream the stream to write data to.
6681 * @param p_j2k J2K codec.
6682 * @param p_manager the user event manager.
6684 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6685 struct opj_stream_private *p_stream,
6686 struct opj_event_mgr * p_manager
6689 OPJ_BYTE * l_current_data = 00;
6690 OPJ_UINT32 l_mco_size;
6691 opj_tcp_v2_t * l_tcp = 00;
6692 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6696 assert(p_j2k != 00);
6697 assert(p_manager != 00);
6698 assert(p_stream != 00);
6700 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6701 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6703 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6704 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6706 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6707 = (OPJ_BYTE*)opj_realloc(
6708 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6710 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6715 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6718 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6719 l_current_data += 2;
6721 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6722 l_current_data += 2;
6724 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6727 l_mcc_record = l_tcp->m_mcc_records;
6728 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6729 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6735 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) {
6743 * Reads a MCO marker (Multiple Component Transform Ordering)
6745 * @param p_header_data the data contained in the MCO box.
6746 * @param p_j2k the jpeg2000 codec.
6747 * @param p_header_size the size of the data contained in the MCO marker.
6748 * @param p_manager the user event manager.
6750 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6751 OPJ_BYTE * p_header_data,
6752 OPJ_UINT32 p_header_size,
6753 struct opj_event_mgr * p_manager )
6755 OPJ_UINT32 l_tmp, i;
6756 OPJ_UINT32 l_nb_stages;
6757 opj_tcp_v2_t * l_tcp;
6758 opj_tccp_t * l_tccp;
6759 opj_image_t * l_image;
6760 opj_image_comp_t * l_img_comp;
6763 assert(p_header_data != 00);
6764 assert(p_j2k != 00);
6765 assert(p_manager != 00);
6767 l_image = p_j2k->m_private_image;
6768 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6769 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6770 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6772 if (p_header_size < 1) {
6773 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6777 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6780 if (l_nb_stages > 1) {
6781 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6785 if (p_header_size != l_nb_stages + 1) {
6786 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6790 l_tccp = l_tcp->tccps;
6791 l_img_comp = l_image->comps;
6793 for (i=0;i<l_image->numcomps;++i) {
6794 l_tccp->m_dc_level_shift = 0;
6798 if (l_tcp->m_mct_decoding_matrix) {
6799 opj_free(l_tcp->m_mct_decoding_matrix);
6800 l_tcp->m_mct_decoding_matrix = 00;
6803 for (i=0;i<l_nb_stages;++i) {
6804 opj_read_bytes(p_header_data,&l_tmp,1);
6807 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6815 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6818 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6819 opj_mct_data_t * l_deco_array, * l_offset_array;
6820 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6821 OPJ_UINT32 l_nb_elem;
6822 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6823 opj_tccp_t * l_tccp;
6826 assert(p_tcp != 00);
6828 l_mcc_record = p_tcp->m_mcc_records;
6830 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6831 if (l_mcc_record->m_index == p_index) {
6836 if (i==p_tcp->m_nb_mcc_records) {
6837 /** element discarded **/
6841 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6842 /** do not support number of comps != image */
6846 l_deco_array = l_mcc_record->m_decorrelation_array;
6849 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6850 if (l_deco_array->m_data_size != l_data_size) {
6854 l_nb_elem = p_image->numcomps * p_image->numcomps;
6855 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6856 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6858 if (! p_tcp->m_mct_decoding_matrix ) {
6862 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);
6865 l_offset_array = l_mcc_record->m_offset_array;
6867 if (l_offset_array) {
6868 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6869 if (l_offset_array->m_data_size != l_data_size) {
6873 l_nb_elem = p_image->numcomps;
6874 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6875 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6877 if (! l_offset_data ) {
6881 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6883 l_tccp = p_tcp->tccps;
6884 l_current_offset_data = l_offset_data;
6886 for (i=0;i<p_image->numcomps;++i) {
6887 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6891 opj_free(l_offset_data);
6898 * Writes the CBD marker (Component bit depth definition)
6900 * @param p_stream the stream to write data to.
6901 * @param p_j2k J2K codec.
6902 * @param p_manager the user event manager.
6904 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6905 struct opj_stream_private *p_stream,
6906 struct opj_event_mgr * p_manager )
6909 OPJ_UINT32 l_cbd_size;
6910 OPJ_BYTE * l_current_data = 00;
6911 opj_image_t *l_image = 00;
6912 opj_image_comp_t * l_comp = 00;
6915 assert(p_j2k != 00);
6916 assert(p_manager != 00);
6917 assert(p_stream != 00);
6919 l_image = p_j2k->m_private_image;
6920 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6922 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6923 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6924 = (OPJ_BYTE*)opj_realloc(
6925 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6928 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6932 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6935 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6937 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6938 l_current_data += 2;
6940 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6941 l_current_data += 2;
6943 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6946 l_comp = l_image->comps;
6948 for (i=0;i<l_image->numcomps;++i) {
6949 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6955 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) {
6963 * Reads a CBD marker (Component bit depth definition)
6964 * @param p_header_data the data contained in the CBD box.
6965 * @param p_j2k the jpeg2000 codec.
6966 * @param p_header_size the size of the data contained in the CBD marker.
6967 * @param p_manager the user event manager.
6969 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6970 OPJ_BYTE * p_header_data,
6971 OPJ_UINT32 p_header_size,
6972 struct opj_event_mgr * p_manager)
6974 OPJ_UINT32 l_nb_comp,l_num_comp;
6975 OPJ_UINT32 l_comp_def;
6977 opj_image_comp_t * l_comp = 00;
6980 assert(p_header_data != 00);
6981 assert(p_j2k != 00);
6982 assert(p_manager != 00);
6984 l_num_comp = p_j2k->m_private_image->numcomps;
6986 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6987 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6991 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6994 if (l_nb_comp != l_num_comp) {
6995 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6999 l_comp = p_j2k->m_private_image->comps;
7000 for (i=0;i<l_num_comp;++i) {
7001 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
7003 l_comp->sgnd = (l_comp_def>>7) & 1;
7004 l_comp->prec = (l_comp_def&0x7f) + 1;
7012 /* ----------------------------------------------------------------------- */
7013 /* J2K / JPT decoder interface */
7014 /* ----------------------------------------------------------------------- */
7018 void j2k_destroy_decompress(opj_j2k_t *j2k) {
7021 if(j2k->tile_len != NULL) {
7022 opj_free(j2k->tile_len);
7024 if(j2k->tile_data != NULL) {
7025 opj_free(j2k->tile_data);
7027 if(j2k->default_tcp != NULL) {
7028 opj_tcp_t *default_tcp = j2k->default_tcp;
7029 if(default_tcp->ppt_data_first != NULL) {
7030 opj_free(default_tcp->ppt_data_first);
7032 if(j2k->default_tcp->tccps != NULL) {
7033 opj_free(j2k->default_tcp->tccps);
7035 opj_free(j2k->default_tcp);
7037 if(j2k->cp != NULL) {
7038 opj_cp_t *cp = j2k->cp;
7039 if(cp->tcps != NULL) {
7040 for(i = 0; i < cp->tw * cp->th; i++) {
7041 if(cp->tcps[i].ppt_data_first != NULL) {
7042 opj_free(cp->tcps[i].ppt_data_first);
7044 if(cp->tcps[i].tccps != NULL) {
7045 opj_free(cp->tcps[i].tccps);
7050 if(cp->ppm_data_first != NULL) {
7051 opj_free(cp->ppm_data_first);
7053 if(cp->tileno != NULL) {
7054 opj_free(cp->tileno);
7056 if(cp->comment != NULL) {
7057 opj_free(cp->comment);
7066 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
7068 if(j2k && parameters) {
7069 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
7070 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
7073 j2k->m_cp.correct = parameters->jpwl_correct;
7074 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
7075 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
7076 #endif /* USE_JPWL */
7083 * Read a JPT-stream and decode file
7086 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7087 opj_image_t *image = NULL;
7088 opj_jpt_msg_header_t header;
7090 opj_common_ptr cinfo = j2k->cinfo;
7092 OPJ_ARG_NOT_USED(cstr_info);
7096 /* create an empty image */
7097 image = opj_image_create0();
7100 j2k->state = J2K_STATE_MHSOC;
7102 /* Initialize the header */
7103 jpt_init_msg_header(&header);
7104 /* Read the first header of the message */
7105 jpt_read_msg_header(cinfo, cio, &header);
7107 position = cio_tell(cio);
7108 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7109 opj_image_destroy(image);
7110 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7115 opj_dec_mstabent_t *e = NULL;
7118 if (!cio_numbytesleft(cio)) {
7122 /* data-bin read -> need to read a new header */
7123 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7124 jpt_read_msg_header(cinfo, cio, &header);
7125 position = cio_tell(cio);
7126 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7127 opj_image_destroy(image);
7128 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7133 id = cio_read(cio, 2);
7134 if (id >> 8 != 0xff) {
7135 opj_image_destroy(image);
7136 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7139 e = j2k_dec_mstab_lookup(id);
7140 if (!(j2k->state & e->states)) {
7141 opj_image_destroy(image);
7142 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7148 if (j2k->state == J2K_STATE_MT) {
7151 if (j2k->state == J2K_STATE_NEOC) {
7155 if (j2k->state == J2K_STATE_NEOC) {
7159 if (j2k->state != J2K_STATE_MT) {
7160 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7166 /* ----------------------------------------------------------------------- */
7167 /* J2K encoder interface */
7168 /* ----------------------------------------------------------------------- */
7170 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7171 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7178 opj_j2k_v2_t* j2k_create_compress_v2(void)
7180 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7185 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7187 l_j2k->m_is_decoder = 0;
7188 l_j2k->m_cp.m_is_decoder = 0;
7190 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7191 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7196 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7198 /* validation list creation*/
7199 l_j2k->m_validation_list = opj_procedure_list_create();
7200 if (! l_j2k->m_validation_list) {
7205 /* execution list creation*/
7206 l_j2k->m_procedure_list = opj_procedure_list_create();
7207 if (! l_j2k->m_procedure_list) {
7215 void j2k_destroy_compress(opj_j2k_t *j2k) {
7219 if(j2k->cp != NULL) {
7220 opj_cp_t *cp = j2k->cp;
7223 opj_free(cp->comment);
7226 opj_free(cp->matrice);
7228 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7229 opj_free(cp->tcps[tileno].tccps);
7238 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7240 int tileno, numpocs_tile;
7241 opj_cp_t *cp = NULL;
7243 if(!j2k || !parameters || ! image) {
7247 /* create and initialize the coding parameters structure */
7248 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7250 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7253 /* set default values for cp */
7258 copy user encoding parameters
7260 cp->cinema = parameters->cp_cinema;
7261 cp->max_comp_size = parameters->max_comp_size;
7262 cp->rsiz = parameters->cp_rsiz;
7263 cp->disto_alloc = parameters->cp_disto_alloc;
7264 cp->fixed_alloc = parameters->cp_fixed_alloc;
7265 cp->fixed_quality = parameters->cp_fixed_quality;
7267 /* mod fixed_quality */
7268 if(parameters->cp_matrice) {
7269 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7270 cp->matrice = (int *) opj_malloc(array_size);
7271 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7275 cp->tdx = parameters->cp_tdx;
7276 cp->tdy = parameters->cp_tdy;
7279 cp->tx0 = parameters->cp_tx0;
7280 cp->ty0 = parameters->cp_ty0;
7282 /* comment string */
7283 if(parameters->cp_comment) {
7284 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7286 strcpy(cp->comment, parameters->cp_comment);
7291 calculate other encoding parameters
7294 if (parameters->tile_size_on) {
7295 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7296 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7298 cp->tdx = image->x1 - cp->tx0;
7299 cp->tdy = image->y1 - cp->ty0;
7302 if(parameters->tp_on){
7303 cp->tp_flag = parameters->tp_flag;
7308 for(i=0;i<image->numcomps ;i++){
7309 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7315 calculate JPWL encoding parameters
7318 if (parameters->jpwl_epc_on) {
7322 cp->epc_on = OPJ_TRUE;
7323 cp->info_on = OPJ_FALSE; /* no informative technique */
7326 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7327 cp->epb_on = OPJ_TRUE;
7329 cp->hprot_MH = parameters->jpwl_hprot_MH;
7330 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7331 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7332 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7334 /* if tile specs are not specified, copy MH specs */
7335 if (cp->hprot_TPH[0] == -1) {
7336 cp->hprot_TPH_tileno[0] = 0;
7337 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7339 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7340 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7341 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7342 cp->pprot[i] = parameters->jpwl_pprot[i];
7346 /* set ESD writing */
7347 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7348 cp->esd_on = OPJ_TRUE;
7350 cp->sens_size = parameters->jpwl_sens_size;
7351 cp->sens_addr = parameters->jpwl_sens_addr;
7352 cp->sens_range = parameters->jpwl_sens_range;
7354 cp->sens_MH = parameters->jpwl_sens_MH;
7355 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7356 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7357 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7361 /* always set RED writing to false: we are at the encoder */
7362 cp->red_on = OPJ_FALSE;
7365 cp->epc_on = OPJ_FALSE;
7367 #endif /* USE_JPWL */
7370 /* initialize the mutiple tiles */
7371 /* ---------------------------- */
7372 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7374 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7375 opj_tcp_t *tcp = &cp->tcps[tileno];
7376 tcp->numlayers = parameters->tcp_numlayers;
7377 assert ( tcp->numlayers >= 0 );
7378 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7380 if (cp->fixed_quality) {
7381 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7383 tcp->rates[j] = parameters->tcp_rates[j];
7385 if (cp->fixed_quality) { /* add fixed_quality */
7386 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7388 tcp->rates[j] = parameters->tcp_rates[j];
7392 tcp->csty = parameters->csty;
7393 tcp->prg = parameters->prog_order;
7394 tcp->mct = parameters->tcp_mct;
7398 if (parameters->numpocs) {
7399 /* initialisation of POC */
7401 assert( parameters->numpocs >= 0 );
7402 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7403 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7404 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7405 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7406 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7407 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7408 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7409 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7410 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7411 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7415 tcp->numpocs = numpocs_tile -1 ;
7420 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7422 for (i = 0; i < image->numcomps; i++) {
7423 opj_tccp_t *tccp = &tcp->tccps[i];
7424 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7425 tccp->numresolutions = parameters->numresolution;
7426 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7427 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7428 tccp->cblksty = parameters->mode;
7429 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7430 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7432 assert(parameters->roi_compno >= 0);
7433 if (i == (OPJ_UINT32)parameters->roi_compno) {
7434 tccp->roishift = parameters->roi_shift;
7439 if(parameters->cp_cinema)
7441 /*Precinct size for lowest frequency subband=128*/
7444 /*Precinct size at all other resolutions = 256*/
7445 for (j = 1; j < tccp->numresolutions; j++) {
7450 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7452 assert(tccp->numresolutions > 0);
7453 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7454 if (p < parameters->res_spec) {
7456 if (parameters->prcw_init[p] < 1) {
7459 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7462 if (parameters->prch_init[p] < 1) {
7465 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7469 int res_spec = parameters->res_spec;
7470 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7471 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7473 if (size_prcw < 1) {
7476 tccp->prcw[j] = int_floorlog2(size_prcw);
7479 if (size_prch < 1) {
7482 tccp->prch[j] = int_floorlog2(size_prch);
7486 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7489 for (j = 0; j < tccp->numresolutions; j++) {
7496 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7501 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7502 opj_cparameters_t *parameters,
7504 struct opj_event_mgr * p_manager)
7506 OPJ_UINT32 i, j, tileno, numpocs_tile;
7507 opj_cp_v2_t *cp = 00;
7510 if(!p_j2k || !parameters || ! image) {
7514 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7515 cp = &(p_j2k->m_cp);
7517 /* set default values for cp */
7522 copy user encoding parameters
7524 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7525 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7526 cp->rsiz = parameters->cp_rsiz;
7527 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7528 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7529 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7531 /* mod fixed_quality */
7532 if (parameters->cp_matrice) {
7533 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7534 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7535 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7539 cp->tdx = parameters->cp_tdx;
7540 cp->tdy = parameters->cp_tdy;
7543 cp->tx0 = parameters->cp_tx0;
7544 cp->ty0 = parameters->cp_ty0;
7546 /* comment string */
7547 if(parameters->cp_comment) {
7548 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7550 strcpy(cp->comment, parameters->cp_comment);
7555 calculate other encoding parameters
7558 if (parameters->tile_size_on) {
7559 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7560 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7562 cp->tdx = image->x1 - cp->tx0;
7563 cp->tdy = image->y1 - cp->ty0;
7566 if (parameters->tp_on) {
7567 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7568 cp->m_specific_param.m_enc.m_tp_on = 1;
7573 calculate JPWL encoding parameters
7576 if (parameters->jpwl_epc_on) {
7580 cp->epc_on = OPJ_TRUE;
7581 cp->info_on = OPJ_FALSE; /* no informative technique */
7584 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7585 cp->epb_on = OPJ_TRUE;
7587 cp->hprot_MH = parameters->jpwl_hprot_MH;
7588 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7589 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7590 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7592 /* if tile specs are not specified, copy MH specs */
7593 if (cp->hprot_TPH[0] == -1) {
7594 cp->hprot_TPH_tileno[0] = 0;
7595 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7597 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7598 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7599 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7600 cp->pprot[i] = parameters->jpwl_pprot[i];
7604 /* set ESD writing */
7605 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7606 cp->esd_on = OPJ_TRUE;
7608 cp->sens_size = parameters->jpwl_sens_size;
7609 cp->sens_addr = parameters->jpwl_sens_addr;
7610 cp->sens_range = parameters->jpwl_sens_range;
7612 cp->sens_MH = parameters->jpwl_sens_MH;
7613 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7614 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7615 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7619 /* always set RED writing to false: we are at the encoder */
7620 cp->red_on = OPJ_FALSE;
7623 cp->epc_on = OPJ_FALSE;
7625 #endif /* USE_JPWL */
7628 /* initialize the mutiple tiles */
7629 /* ---------------------------- */
7630 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7631 if (parameters->numpocs) {
7632 /* initialisation of POC */
7633 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7637 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7638 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7639 tcp->numlayers = parameters->tcp_numlayers;
7641 for (j = 0; j < tcp->numlayers; j++) {
7642 if(cp->m_specific_param.m_enc.m_cinema){
7643 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7644 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7646 tcp->rates[j] = parameters->tcp_rates[j];
7648 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7649 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7651 tcp->rates[j] = parameters->tcp_rates[j];
7656 tcp->csty = parameters->csty;
7657 tcp->prg = parameters->prog_order;
7658 tcp->mct = parameters->tcp_mct;
7663 if (parameters->numpocs) {
7664 /* initialisation of POC */
7667 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7668 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7669 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7671 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7672 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7673 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7674 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7675 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7676 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7677 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7683 tcp->numpocs = numpocs_tile -1 ;
7688 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7690 if (parameters->mct_data) {
7692 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7695 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7696 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7697 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7698 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7701 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7702 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7703 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7705 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7706 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7708 tcp->mct_norms = (OPJ_FLOAT64*)
7709 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7711 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7714 for (i = 0; i < image->numcomps; i++) {
7715 opj_tccp_t *tccp = &tcp->tccps[i];
7716 tccp->m_dc_level_shift = l_dc_shift[i];
7719 j2k_setup_mct_encoding(tcp,image);
7723 for (i = 0; i < image->numcomps; i++) {
7724 opj_tccp_t *tccp = &tcp->tccps[i];
7725 opj_image_comp_t * l_comp = &(image->comps[i]);
7727 if (! l_comp->sgnd) {
7728 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7733 for (i = 0; i < image->numcomps; i++) {
7734 opj_tccp_t *tccp = &tcp->tccps[i];
7736 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7737 tccp->numresolutions = parameters->numresolution;
7738 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7739 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7740 tccp->cblksty = parameters->mode;
7741 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7742 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7745 if (i == parameters->roi_compno) {
7746 tccp->roishift = parameters->roi_shift;
7751 if(parameters->cp_cinema) {
7752 //Precinct size for lowest frequency subband=128
7755 //Precinct size at all other resolutions = 256
7756 for (j = 1; j < tccp->numresolutions; j++) {
7761 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7762 OPJ_INT32 p = 0, it_res;
7763 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7764 if (p < parameters->res_spec) {
7766 if (parameters->prcw_init[p] < 1) {
7767 tccp->prcw[it_res] = 1;
7769 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7772 if (parameters->prch_init[p] < 1) {
7773 tccp->prch[it_res] = 1;
7775 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7779 int res_spec = parameters->res_spec;
7780 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7781 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7783 if (size_prcw < 1) {
7784 tccp->prcw[it_res] = 1;
7786 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7789 if (size_prch < 1) {
7790 tccp->prch[it_res] = 1;
7792 tccp->prch[it_res] = int_floorlog2(size_prch);
7796 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7799 for (j = 0; j < tccp->numresolutions; j++) {
7806 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7810 if (parameters->mct_data) {
7811 opj_free(parameters->mct_data);
7812 parameters->mct_data = 00;
7817 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7820 opj_cp_t *cp = NULL;
7822 opj_tcd_t *tcd = NULL; /* TCD component */
7830 j2k->cstr_info = cstr_info;
7833 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7834 cstr_info->image_w = image->x1 - image->x0;
7835 cstr_info->image_h = image->y1 - image->y0;
7836 cstr_info->prog = (&cp->tcps[0])->prg;
7837 cstr_info->tw = cp->tw;
7838 cstr_info->th = cp->th;
7839 cstr_info->tile_x = cp->tdx; /* new version parser */
7840 cstr_info->tile_y = cp->tdy; /* new version parser */
7841 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7842 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7843 cstr_info->numcomps = image->numcomps;
7844 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7845 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7846 for (compno=0; compno < image->numcomps; compno++) {
7847 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7849 cstr_info->D_max = 0.0; /* ADD Marcela */
7850 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7851 cstr_info->maxmarknum = 100;
7852 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7853 cstr_info->marknum = 0;
7863 for (compno = 1; compno < image->numcomps; compno++) {
7864 j2k_write_coc(j2k, compno);
7865 j2k_write_qcc(j2k, compno);
7869 for (compno = 0; compno < image->numcomps; compno++) {
7870 opj_tcp_t *tcp = &cp->tcps[0];
7871 if (tcp->tccps[compno].roishift)
7872 j2k_write_rgn(j2k, compno, 0);
7874 if (cp->comment != NULL) {
7878 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7882 if (cp->cinema == CINEMA4K_24) {
7887 /* uncomment only for testing JPSEC marker writing */
7888 /* j2k_write_sec(j2k); */
7892 cstr_info->main_head_end = cio_tell(cio) - 1;
7895 /**** Main Header ENDS here ***/
7897 /* create the tile encoder */
7898 tcd = tcd_create(j2k->cinfo);
7900 /* encode each tile */
7901 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7905 int acc_pack_num = 0;
7909 opj_tcp_t *tcp = &cp->tcps[tileno];
7910 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7912 j2k->curtileno = tileno;
7913 j2k->cur_tp_num = 0;
7914 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7915 /* initialisation before tile encoding */
7917 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7919 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7924 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7925 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7926 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7927 cstr_info->tile[j2k->curtileno].marknum = 0;
7931 for(pino = 0; pino <= tcp->numpocs; pino++) {
7935 /*Get number of tile parts*/
7936 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7937 tcd->tp_pos = cp->tp_pos;
7939 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7940 j2k->tp_num = tilepartno;
7943 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7944 cio_tell(cio) + j2k->pos_correction;
7948 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7949 for (compno = 1; compno < image->numcomps; compno++) {
7950 j2k_write_coc(j2k, compno);
7951 j2k_write_qcc(j2k, compno);
7953 if (cp->tcps[tileno].numpocs) {
7960 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
7961 cio_tell(cio) + j2k->pos_correction + 1;
7964 j2k_write_sod(j2k, tcd);
7968 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
7969 cio_tell(cio) + j2k->pos_correction - 1;
7970 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
7972 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
7973 cstr_info->packno - acc_pack_num;
7974 acc_pack_num = cstr_info->packno;
7982 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
7987 if (tile->PPT) { // BAD PPT !!!
7990 PPT_file=fopen("PPT","rb");
7991 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
7992 for (i=0;i<tile->len_ppt;i++) {
7994 fread(&elmt, 1, 1, PPT_file);
7995 fwrite(&elmt,1,1,f);
8004 /* destroy the tile encoder */
8005 tcd_free_encode(tcd);
8008 opj_free(j2k->cur_totnum_tp);
8013 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
8015 /* The following adjustment is done to adjust the codestream size */
8016 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
8017 /* the first bunch of bytes is not in the codestream */
8018 cstr_info->codestream_size -= cstr_info->main_head_start;
8024 preparation of JPWL marker segments
8028 /* encode according to JPWL */
8029 jpwl_encode(j2k, cio, image);
8032 #endif /* USE_JPWL */
8037 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8039 assert(cstr_info != 00);
8041 /* expand the list? */
8042 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
8043 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
8044 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
8047 /* add the marker */
8048 cstr_info->marker[cstr_info->marknum].type = type;
8049 cstr_info->marker[cstr_info->marknum].pos = pos;
8050 cstr_info->marker[cstr_info->marknum].len = len;
8051 cstr_info->marknum++;
8055 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
8057 assert(cstr_index != 00);
8059 /* expand the list? */
8060 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
8061 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
8062 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
8065 /* add the marker */
8066 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
8067 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
8068 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
8069 cstr_index->marknum++;
8073 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
8075 opj_marker_info_t *marker;
8077 assert(cstr_info != 00);
8079 /* expand the list? */
8080 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8081 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8082 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8085 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8087 /* add the marker */
8088 marker->type = type;
8091 cstr_info->tile[tileno].marknum++;
8094 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)
8096 assert(cstr_index != 00);
8097 assert(cstr_index->tile_index != 00);
8099 /* expand the list? */
8100 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8101 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8102 cstr_index->tile_index[tileno].marker =
8103 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8104 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8107 /* add the marker */
8108 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8109 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8110 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8111 cstr_index->tile_index[tileno].marknum++;
8113 if (type == J2K_MS_SOT) {
8114 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8116 if (cstr_index->tile_index[tileno].tp_index)
8117 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8124 * -----------------------------------------------------------------------
8125 * -----------------------------------------------------------------------
8126 * -----------------------------------------------------------------------
8130 * Ends the decompression procedures and possibiliy add data to be read after the
8133 opj_bool j2k_end_decompress(
8134 opj_j2k_v2_t *p_j2k,
8135 opj_stream_private_t *p_stream,
8136 opj_event_mgr_t * p_manager)
8145 * Reads a jpeg2000 codestream header structure.
8148 * @param p_stream the stream to read data from.
8149 * @param p_j2k the jpeg2000 codec.
8150 * @param p_manager the user event manager.
8152 * @return true if the box is valid.
8154 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8155 opj_j2k_v2_t* p_j2k,
8156 opj_image_t** p_image,
8157 struct opj_event_mgr* p_manager )
8160 assert(p_j2k != 00);
8161 assert(p_stream != 00);
8162 assert(p_manager != 00);
8164 /* create an empty image header */
8165 p_j2k->m_private_image = opj_image_create0();
8166 if (! p_j2k->m_private_image) {
8170 /* customization of the validation */
8171 j2k_setup_decoding_validation(p_j2k);
8173 /* validation of the parameters codec */
8174 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8175 opj_image_destroy(p_j2k->m_private_image);
8176 p_j2k->m_private_image = NULL;
8180 /* customization of the encoding */
8181 j2k_setup_header_reading(p_j2k);
8184 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8185 opj_image_destroy(p_j2k->m_private_image);
8186 p_j2k->m_private_image = NULL;
8190 *p_image = opj_image_create0();
8195 /* Copy codestream image information to the output image */
8196 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8198 /*Allocate and initialize some elements of codestrem index*/
8199 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8207 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8209 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8212 assert(p_j2k != 00);
8214 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8216 /* DEVELOPER CORNER, add your custom procedures */
8217 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8222 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8223 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8225 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8228 assert(p_j2k != 00);
8230 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8231 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8232 /* DEVELOPER CORNER, add your custom validation procedure */
8238 * The mct encoding validation procedure.
8240 * @param p_j2k the jpeg2000 codec to validate.
8241 * @param p_stream the input stream to validate.
8242 * @param p_manager the user event manager.
8244 * @return true if the parameters are correct.
8246 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8247 opj_stream_private_t *p_stream,
8248 opj_event_mgr_t * p_manager )
8250 opj_bool l_is_valid = OPJ_TRUE;
8254 assert(p_j2k != 00);
8255 assert(p_stream != 00);
8256 assert(p_manager != 00);
8258 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8259 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8260 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8262 for (i=0;i<l_nb_tiles;++i) {
8263 if (l_tcp->mct == 2) {
8264 opj_tccp_t * l_tccp = l_tcp->tccps;
8265 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8267 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8268 l_is_valid &= ! (l_tccp->qmfbid & 1);
8279 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8282 OPJ_UINT32 l_indix = 1;
8283 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8284 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8285 OPJ_UINT32 l_mct_size,l_nb_elem;
8286 OPJ_FLOAT32 * l_data, * l_current_data;
8287 opj_tccp_t * l_tccp;
8290 assert(p_tcp != 00);
8292 if (p_tcp->mct != 2) {
8296 if (p_tcp->m_mct_decoding_matrix) {
8297 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8298 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8300 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));
8301 if (! p_tcp->m_mct_records) {
8304 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8306 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8308 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8310 if (l_mct_deco_data->m_data) {
8311 opj_free(l_mct_deco_data->m_data);
8312 l_mct_deco_data->m_data = 00;
8315 l_mct_deco_data->m_index = l_indix++;
8316 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8317 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8318 l_nb_elem = p_image->numcomps * p_image->numcomps;
8319 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8320 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8322 if (! l_mct_deco_data->m_data) {
8326 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);
8328 l_mct_deco_data->m_data_size = l_mct_size;
8329 ++p_tcp->m_nb_mct_records;
8332 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8333 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8334 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));
8336 if (! p_tcp->m_mct_records) {
8340 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8341 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8343 if (l_mct_deco_data) {
8344 l_mct_deco_data = l_mct_offset_data - 1;
8348 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8350 if (l_mct_offset_data->m_data) {
8351 opj_free(l_mct_offset_data->m_data);
8352 l_mct_offset_data->m_data = 00;
8355 l_mct_offset_data->m_index = l_indix++;
8356 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8357 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8358 l_nb_elem = p_image->numcomps;
8359 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8360 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8362 if (! l_mct_offset_data->m_data) {
8366 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8368 opj_free(l_mct_offset_data->m_data);
8369 l_mct_offset_data->m_data = 00;
8373 l_tccp = p_tcp->tccps;
8374 l_current_data = l_data;
8376 for (i=0;i<l_nb_elem;++i) {
8377 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8381 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8385 l_mct_offset_data->m_data_size = l_mct_size;
8387 ++p_tcp->m_nb_mct_records;
8389 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8390 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8391 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8392 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8394 if (! p_tcp->m_mcc_records) {
8397 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8398 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));
8402 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8403 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8404 l_mcc_data->m_is_irreversible = 1;
8405 l_mcc_data->m_nb_comps = p_image->numcomps;
8406 l_mcc_data->m_index = l_indix++;
8407 l_mcc_data->m_offset_array = l_mct_offset_data;
8408 ++p_tcp->m_nb_mcc_records;
8414 * Builds the cp decoder parameters to use to decode tile.
8416 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8417 opj_stream_private_t *p_stream,
8418 opj_event_mgr_t * p_manager )
8420 /* add here initialization of cp
8421 copy paste of setup_decoder */
8429 * Builds the cp encoder parameters to use to encode tile.
8431 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8432 opj_stream_private_t *p_stream,
8433 opj_event_mgr_t * p_manager )
8435 /* add here initialization of cp
8436 copy paste of setup_encoder */
8444 * The default encoding validation procedure without any extension.
8446 * @param p_j2k the jpeg2000 codec to validate.
8447 * @param p_stream the input stream to validate.
8448 * @param p_manager the user event manager.
8450 * @return true if the parameters are correct.
8452 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8453 opj_stream_private_t *p_stream,
8454 opj_event_mgr_t * p_manager )
8456 opj_bool l_is_valid = OPJ_TRUE;
8459 assert(p_j2k != 00);
8460 assert(p_stream != 00);
8461 assert(p_manager != 00);
8463 /* STATE checking */
8464 /* make sure the state is at 0 */
8465 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8467 /* POINTER validation */
8468 /* make sure a p_j2k codec is present */
8469 l_is_valid &= (p_j2k->m_procedure_list != 00);
8470 /* make sure a validation list is present */
8471 l_is_valid &= (p_j2k->m_validation_list != 00);
8473 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8474 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8478 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8479 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8483 /* PARAMETER VALIDATION */
8488 * The default decoding validation procedure without any extension.
8490 * @param p_j2k the jpeg2000 codec to validate.
8491 * @param p_stream the input stream to validate.
8492 * @param p_manager the user event manager.
8494 * @return true if the parameters are correct.
8496 opj_bool j2k_decoding_validation (
8497 opj_j2k_v2_t *p_j2k,
8498 opj_stream_private_t *p_stream,
8499 opj_event_mgr_t * p_manager
8502 opj_bool l_is_valid = OPJ_TRUE;
8505 assert(p_j2k != 00);
8506 assert(p_stream != 00);
8507 assert(p_manager != 00);
8510 /* STATE checking */
8511 /* make sure the state is at 0 */
8513 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8515 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8517 /* POINTER validation */
8518 /* make sure a p_j2k codec is present */
8519 /* make sure a procedure list is present */
8520 l_is_valid &= (p_j2k->m_procedure_list != 00);
8521 /* make sure a validation list is present */
8522 l_is_valid &= (p_j2k->m_validation_list != 00);
8524 /* PARAMETER VALIDATION */
8528 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8529 struct opj_stream_private *p_stream,
8530 struct opj_event_mgr * p_manager)
8532 OPJ_UINT32 l_current_marker;
8533 OPJ_UINT32 l_marker_size;
8534 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8537 assert(p_stream != 00);
8538 assert(p_j2k != 00);
8539 assert(p_manager != 00);
8541 /* We enter in the main header */
8542 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8544 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8545 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8546 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8550 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8551 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8552 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8556 /* Read 2 bytes as the new marker ID */
8557 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8559 /* Try to read until the SOT is detected */
8560 while (l_current_marker != J2K_MS_SOT) {
8562 /* Check if the current marker ID is valid */
8563 if (l_current_marker < 0xff00) {
8564 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8568 /* Get the marker handler from the marker ID */
8569 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8571 /* Manage case where marker is unknown */
8572 if (l_marker_handler->id == J2K_MS_UNK) {
8573 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8574 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8578 if (l_current_marker == J2K_MS_SOT)
8579 break; /* SOT marker is detected main header is completely read */
8580 else /* Get the marker handler from the marker ID */
8581 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8584 /* Check if the marker is known and if it is the right place to find it */
8585 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8586 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8590 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8591 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8592 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8596 /* read 2 bytes as the marker size */
8597 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8598 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8600 /* Check if the marker size is compatible with the header data size */
8601 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8602 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8603 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8604 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8607 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8610 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8611 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) {
8612 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8616 /* Read the marker segment with the correct marker handler */
8617 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8618 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8622 /* Add the marker to the codestream index*/
8623 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8624 l_marker_handler->id,
8625 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8626 l_marker_size + 4 );
8628 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8629 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8630 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8634 /* read 2 bytes as the new marker ID */
8635 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8638 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8640 /* Position of the last element if the main header */
8641 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8643 /* Next step: read a tile-part header */
8644 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8650 * Excutes the given procedures on the given codec.
8652 * @param p_procedure_list the list of procedures to execute
8653 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8654 * @param p_stream the stream to execute the procedures on.
8655 * @param p_manager the user manager.
8657 * @return true if all the procedures were successfully executed.
8659 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8660 opj_procedure_list_t * p_procedure_list,
8661 opj_stream_private_t *p_stream,
8662 opj_event_mgr_t * p_manager )
8664 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8665 opj_bool l_result = OPJ_TRUE;
8666 OPJ_UINT32 l_nb_proc, i;
8669 assert(p_procedure_list != 00);
8670 assert(p_j2k != 00);
8671 assert(p_stream != 00);
8672 assert(p_manager != 00);
8675 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8676 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);
8678 for (i=0;i<l_nb_proc;++i) {
8679 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8683 /* and clear the procedure list at the end.*/
8684 opj_procedure_list_clear(p_procedure_list);
8689 opj_bool j2k_copy_default_tcp_and_create_tcd
8691 opj_j2k_v2_t * p_j2k,
8692 opj_stream_private_t *p_stream,
8693 opj_event_mgr_t * p_manager
8696 opj_tcp_v2_t * l_tcp = 00;
8697 opj_tcp_v2_t * l_default_tcp = 00;
8698 OPJ_UINT32 l_nb_tiles;
8700 opj_tccp_t *l_current_tccp = 00;
8701 OPJ_UINT32 l_tccp_size;
8702 OPJ_UINT32 l_mct_size;
8703 opj_image_t * l_image;
8704 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8705 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8706 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8707 OPJ_UINT32 l_offset;
8710 assert(p_j2k != 00);
8711 assert(p_stream != 00);
8712 assert(p_manager != 00);
8714 l_image = p_j2k->m_private_image;
8715 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8716 l_tcp = p_j2k->m_cp.tcps;
8717 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8718 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8719 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8722 for (i=0; i<l_nb_tiles; ++i) {
8723 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8724 l_current_tccp = l_tcp->tccps;
8725 /*Copy default coding parameters into the current tile coding parameters*/
8726 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8727 /* Initialize some values of the current tile coding parameters*/
8729 l_tcp->ppt_data = 00;
8730 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8731 l_tcp->tccps = l_current_tccp;
8733 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8734 if (l_default_tcp->m_mct_decoding_matrix) {
8735 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8736 if (! l_tcp->m_mct_decoding_matrix ) {
8739 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8742 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8743 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8744 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8745 if (! l_tcp->m_mct_records) {
8748 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8750 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8751 l_src_mct_rec = l_default_tcp->m_mct_records;
8752 l_dest_mct_rec = l_tcp->m_mct_records;
8754 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8756 if (l_src_mct_rec->m_data) {
8758 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8759 if(! l_dest_mct_rec->m_data) {
8762 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8769 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8770 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8771 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8772 if (! l_tcp->m_mcc_records) {
8775 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8777 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8778 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8779 l_dest_mcc_rec = l_tcp->m_mcc_records;
8781 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8783 if (l_src_mcc_rec->m_decorrelation_array) {
8784 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8785 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8788 if (l_src_mcc_rec->m_offset_array) {
8789 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8790 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8797 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8798 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8800 /* Move to next tile cp*/
8804 /* Create the current tile decoder*/
8805 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8806 if (! p_j2k->m_tcd ) {
8810 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8811 tcd_destroy_v2(p_j2k->m_tcd);
8813 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8821 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8822 * with the marker value.
8823 * @param p_id Marker value to look up
8825 * @return the handler associated with the id.
8827 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8829 const opj_dec_memory_marker_handler_t *e;
8830 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8831 if (e->id == p_id) {
8832 break; /* we find a handler corresponding to the marker ID*/
8840 * Destroys a jpeg2000 codec.
8842 * @param p_j2k the jpeg20000 structure to destroy.
8844 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8850 if (p_j2k->m_is_decoder) {
8852 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8853 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8854 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8855 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8858 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8859 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8860 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8861 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8866 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8867 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8868 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8871 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8872 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8873 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8874 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8877 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8878 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8879 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8880 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8884 tcd_destroy_v2(p_j2k->m_tcd);
8886 j2k_cp_destroy(&(p_j2k->m_cp));
8887 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8889 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8890 p_j2k->m_procedure_list = 00;
8892 opj_procedure_list_destroy(p_j2k->m_validation_list);
8893 p_j2k->m_procedure_list = 00;
8895 j2k_destroy_cstr_index(p_j2k->cstr_index);
8896 p_j2k->cstr_index = NULL;
8898 opj_image_destroy(p_j2k->m_private_image);
8899 p_j2k->m_private_image = NULL;
8901 opj_image_destroy(p_j2k->m_output_image);
8902 p_j2k->m_output_image = NULL;
8907 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8911 if (p_cstr_ind->marker) {
8912 opj_free(p_cstr_ind->marker);
8913 p_cstr_ind->marker = NULL;
8916 if (p_cstr_ind->tile_index) {
8917 OPJ_UINT32 it_tile = 0;
8919 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8921 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8922 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8923 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8926 if(p_cstr_ind->tile_index[it_tile].tp_index){
8927 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8928 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8931 if(p_cstr_ind->tile_index[it_tile].marker){
8932 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8933 p_cstr_ind->tile_index[it_tile].marker = NULL;
8938 opj_free( p_cstr_ind->tile_index);
8939 p_cstr_ind->tile_index = NULL;
8942 opj_free(p_cstr_ind);
8949 * Destroys a tile coding parameter structure.
8951 * @param p_tcp the tile coding parameter to destroy.
8953 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8959 if (p_tcp->ppt_buffer != 00) {
8960 opj_free(p_tcp->ppt_buffer);
8961 p_tcp->ppt_buffer = 00;
8964 if (p_tcp->tccps != 00) {
8965 opj_free(p_tcp->tccps);
8969 if (p_tcp->m_mct_coding_matrix != 00) {
8970 opj_free(p_tcp->m_mct_coding_matrix);
8971 p_tcp->m_mct_coding_matrix = 00;
8974 if (p_tcp->m_mct_decoding_matrix != 00) {
8975 opj_free(p_tcp->m_mct_decoding_matrix);
8976 p_tcp->m_mct_decoding_matrix = 00;
8979 if (p_tcp->m_mcc_records) {
8980 opj_free(p_tcp->m_mcc_records);
8981 p_tcp->m_mcc_records = 00;
8982 p_tcp->m_nb_max_mcc_records = 0;
8983 p_tcp->m_nb_mcc_records = 0;
8986 if (p_tcp->m_mct_records) {
8987 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8990 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8991 if (l_mct_data->m_data) {
8992 opj_free(l_mct_data->m_data);
8993 l_mct_data->m_data = 00;
8999 opj_free(p_tcp->m_mct_records);
9000 p_tcp->m_mct_records = 00;
9003 if (p_tcp->mct_norms != 00) {
9004 opj_free(p_tcp->mct_norms);
9005 p_tcp->mct_norms = 00;
9008 j2k_tcp_data_destroy(p_tcp);
9013 * Destroys the data inside a tile coding parameter structure.
9015 * @param p_tcp the tile coding parameter which contain data to destroy.
9017 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
9019 if (p_tcp->m_data) {
9020 opj_free(p_tcp->m_data);
9021 p_tcp->m_data = NULL;
9022 p_tcp->m_data_size = 0;
9027 * Destroys a coding parameter structure.
9029 * @param p_cp the coding parameter to destroy.
9031 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
9033 OPJ_UINT32 l_nb_tiles;
9034 opj_tcp_v2_t * l_current_tile = 00;
9045 l_current_tile = p_cp->tcps;
9046 l_nb_tiles = p_cp->th * p_cp->tw;
9049 (i = 0; i < l_nb_tiles; ++i)
9051 j2k_tcp_destroy(l_current_tile);
9054 opj_free(p_cp->tcps);
9058 (p_cp->ppm_buffer != 00)
9060 opj_free(p_cp->ppm_buffer);
9061 p_cp->ppm_buffer = 00;
9064 (p_cp->comment != 00)
9066 opj_free(p_cp->comment);
9070 (! p_cp->m_is_decoder)
9073 (p_cp->m_specific_param.m_enc.m_matrice)
9075 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
9076 p_cp->m_specific_param.m_enc.m_matrice = 00;
9084 * Reads a tile header.
9085 * @param p_j2k the jpeg2000 codec.
9086 * @param p_stream the stream to write data to.
9087 * @param p_manager the user event manager.
9089 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9090 OPJ_UINT32 * p_tile_index,
9091 OPJ_UINT32 * p_data_size,
9092 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9093 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9094 OPJ_UINT32 * p_nb_comps,
9096 opj_stream_private_t *p_stream,
9097 opj_event_mgr_t * p_manager )
9099 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9100 OPJ_UINT32 l_marker_size;
9101 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9102 opj_tcp_v2_t * l_tcp = NULL;
9103 OPJ_UINT32 l_nb_tiles;
9106 assert(p_stream != 00);
9107 assert(p_j2k != 00);
9108 assert(p_manager != 00);
9110 /* Reach the End Of Codestream ?*/
9111 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9112 l_current_marker = J2K_MS_EOC;
9114 /* We need to encounter a SOT marker (a new tile-part header) */
9115 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9119 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9120 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9122 /* Try to read until the Start Of Data is detected */
9123 while (l_current_marker != J2K_MS_SOD) {
9125 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9126 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9127 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9131 /* Read 2 bytes from the buffer as the marker size */
9132 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9134 /* Why this condition? FIXME */
9135 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9136 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9138 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9140 /* Get the marker handler from the marker ID */
9141 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9143 /* Check if the marker is known and if it is the right place to find it */
9144 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9145 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9148 /* FIXME manage case of unknown marker as in the main header ? */
9150 /* Check if the marker size is compatible with the header data size */
9151 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9152 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9153 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9154 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9157 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9160 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9161 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) {
9162 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9166 /* Read the marker segment with the correct marker handler */
9167 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9168 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9172 /* Add the marker to the codestream index*/
9173 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9175 l_marker_handler->id,
9176 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9177 l_marker_size + 4 );
9179 /* Keep the position of the last SOT marker read */
9180 if ( l_marker_handler->id == J2K_MS_SOT ) {
9181 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9182 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9184 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9189 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9190 /* Skip the rest of the tile part header*/
9191 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) {
9192 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9195 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9198 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9199 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9200 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9203 /* Read 2 bytes from the buffer as the new marker ID */
9204 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9208 /* If we didn't skip data before, we need to read the SOD marker*/
9209 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9210 /* Try to read the SOD marker and skip data ? FIXME */
9211 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
9217 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9218 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9219 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9220 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9224 /* Read 2 bytes from buffer as the new marker ID */
9225 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9229 /* Indicate we will try to read a new tile-part header*/
9230 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9231 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9232 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9234 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9235 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9236 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9240 /* Read 2 bytes from buffer as the new marker ID */
9241 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9245 /* Current marker is the EOC marker ?*/
9246 if (l_current_marker == J2K_MS_EOC) {
9247 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9248 p_j2k->m_current_tile_number = 0;
9249 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9254 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9255 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9256 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9258 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9259 ++p_j2k->m_current_tile_number;
9263 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9264 *p_go_on = OPJ_FALSE;
9270 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9271 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9275 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9276 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9278 *p_tile_index = p_j2k->m_current_tile_number;
9279 *p_go_on = OPJ_TRUE;
9280 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9281 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9282 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9283 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9284 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9285 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9287 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9293 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9294 OPJ_UINT32 p_tile_index,
9296 OPJ_UINT32 p_data_size,
9297 opj_stream_private_t *p_stream,
9298 opj_event_mgr_t * p_manager )
9300 OPJ_UINT32 l_current_marker;
9301 OPJ_BYTE l_data [2];
9302 opj_tcp_v2_t * l_tcp;
9305 assert(p_stream != 00);
9306 assert(p_j2k != 00);
9307 assert(p_manager != 00);
9309 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9310 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9314 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9315 if (! l_tcp->m_data) {
9316 j2k_tcp_destroy(l_tcp);
9320 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9324 p_j2k->cstr_index) ) {
9325 j2k_tcp_destroy(l_tcp);
9326 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9330 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9334 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9335 * we destroy just the data which will be re-read in read_tile_header*/
9336 /*j2k_tcp_destroy(l_tcp);
9337 p_j2k->m_tcd->tcp = 0;*/
9338 j2k_tcp_data_destroy(l_tcp);
9340 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9341 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9343 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9344 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9345 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9349 opj_read_bytes(l_data,&l_current_marker,2);
9351 if (l_current_marker == J2K_MS_EOC) {
9352 p_j2k->m_current_tile_number = 0;
9353 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9355 else if (l_current_marker != J2K_MS_SOT)
9357 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9366 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9368 OPJ_UINT32 i,j,k = 0;
9369 OPJ_UINT32 l_width_src,l_height_src;
9370 OPJ_UINT32 l_width_dest,l_height_dest;
9371 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9372 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9373 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9374 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9375 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9377 opj_image_comp_t * l_img_comp_src = 00;
9378 opj_image_comp_t * l_img_comp_dest = 00;
9380 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9381 opj_image_t * l_image_src = 00;
9382 OPJ_UINT32 l_size_comp, l_remaining;
9383 OPJ_INT32 * l_dest_ptr;
9384 opj_tcd_resolution_v2_t* l_res= 00;
9386 l_tilec = p_tcd->tcd_image->tiles->comps;
9387 l_image_src = p_tcd->image;
9388 l_img_comp_src = l_image_src->comps;
9390 l_img_comp_dest = p_output_image->comps;
9392 for (i=0; i<l_image_src->numcomps; i++) {
9394 /* Allocate output component buffer if necessary */
9395 if (!l_img_comp_dest->data) {
9397 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9398 if (! l_img_comp_dest->data) {
9403 /* Copy info from decoded comp image to output image */
9404 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9407 /* Compute the precision of the output buffer */
9408 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9409 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9410 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9416 if (l_size_comp == 3) {
9421 /* Current tile component size*/
9423 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9424 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9427 l_width_src = (l_res->x1 - l_res->x0);
9428 l_height_src = (l_res->y1 - l_res->y0);
9430 /* Border of the current output component*/
9431 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9432 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9433 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9434 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9437 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9438 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9442 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9443 * of the input buffer (decoded tile component) which will be move
9444 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9445 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9446 * by this input area.
9448 assert( l_res->x0 >= 0);
9449 assert( l_res->x1 >= 0);
9450 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9451 l_start_x_dest = l_res->x0 - l_x0_dest;
9452 l_offset_x0_src = 0;
9454 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9455 l_width_dest = l_width_src;
9456 l_offset_x1_src = 0;
9459 l_width_dest = l_x1_dest - l_res->x0 ;
9460 l_offset_x1_src = l_width_src - l_width_dest;
9464 l_start_x_dest = 0 ;
9465 l_offset_x0_src = l_x0_dest - l_res->x0;
9467 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9468 l_width_dest = l_width_src - l_offset_x0_src;
9469 l_offset_x1_src = 0;
9472 l_width_dest = l_img_comp_dest->w ;
9473 l_offset_x1_src = l_res->x1 - l_x1_dest;
9477 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9478 l_start_y_dest = l_res->y0 - l_y0_dest;
9479 l_offset_y0_src = 0;
9481 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9482 l_height_dest = l_height_src;
9483 l_offset_y1_src = 0;
9486 l_height_dest = l_y1_dest - l_res->y0 ;
9487 l_offset_y1_src = l_height_src - l_height_dest;
9491 l_start_y_dest = 0 ;
9492 l_offset_y0_src = l_y0_dest - l_res->y0;
9494 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9495 l_height_dest = l_height_src - l_offset_y0_src;
9496 l_offset_y1_src = 0;
9499 l_height_dest = l_img_comp_dest->h ;
9500 l_offset_y1_src = l_res->y1 - l_y1_dest;
9504 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9509 /* Compute the input buffer offset */
9510 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9511 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9512 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9514 /* Compute the output buffer offset */
9515 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9516 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9518 /* Move the output buffer to the first place where we will write*/
9519 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9522 fprintf(stdout, "COMPO[%d]:\n",i);
9523 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9524 "\t tile offset:%d, %d, %d, %d\n"
9525 "\t buffer offset: %d; %d, %d\n",
9526 l_res->x0, l_res->y0, l_width_src, l_height_src,
9527 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9528 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9530 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9531 "\t start offset: %d, line offset= %d\n",
9532 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9536 switch (l_size_comp) {
9539 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9540 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9542 if (l_img_comp_src->sgnd) {
9543 for (j = 0 ; j < l_height_dest ; ++j) {
9544 for ( k = 0 ; k < l_width_dest ; ++k) {
9545 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9548 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9549 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9553 for ( j = 0 ; j < l_height_dest ; ++j ) {
9554 for ( k = 0 ; k < l_width_dest ; ++k) {
9555 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9558 l_dest_ptr+= l_line_offset_dest;
9559 l_src_ptr += l_line_offset_src;
9563 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9564 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9569 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9570 l_src_ptr += l_start_offset_src;
9572 if (l_img_comp_src->sgnd) {
9573 for (j=0;j<l_height_dest;++j) {
9574 for (k=0;k<l_width_dest;++k) {
9575 *(l_dest_ptr++) = *(l_src_ptr++);
9578 l_dest_ptr+= l_line_offset_dest;
9579 l_src_ptr += l_line_offset_src ;
9583 for (j=0;j<l_height_dest;++j) {
9584 for (k=0;k<l_width_dest;++k) {
9585 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9588 l_dest_ptr+= l_line_offset_dest;
9589 l_src_ptr += l_line_offset_src ;
9593 l_src_ptr += l_end_offset_src;
9594 p_data = (OPJ_BYTE*) l_src_ptr;
9599 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9600 l_src_ptr += l_start_offset_src;
9602 for (j=0;j<l_height_dest;++j) {
9603 for (k=0;k<l_width_dest;++k) {
9604 *(l_dest_ptr++) = (*(l_src_ptr++));
9607 l_dest_ptr+= l_line_offset_dest;
9608 l_src_ptr += l_line_offset_src ;
9611 l_src_ptr += l_end_offset_src;
9612 p_data = (OPJ_BYTE*) l_src_ptr;
9626 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9628 * @param p_j2k the jpeg2000 codec.
9629 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9630 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9631 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9632 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9633 * @param p_manager the user event manager
9635 * @return true if the area could be set.
9637 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9638 opj_image_t* p_image,
9639 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9640 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9641 struct opj_event_mgr * p_manager )
9643 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9644 opj_image_t * l_image = p_j2k->m_private_image;
9647 OPJ_INT32 l_comp_x1, l_comp_y1;
9648 opj_image_comp_t* l_img_comp = NULL;
9650 /* Check if we are read the main header */
9651 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9652 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9656 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9657 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9659 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9660 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9661 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9662 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9668 /* Check if the positions provided by the user are correct */
9671 assert(p_start_x >= 0 );
9672 assert(p_start_y >= 0 );
9674 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9675 opj_event_msg_v2(p_manager, EVT_ERROR,
9676 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9677 p_start_x, l_image->x1);
9680 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9681 opj_event_msg_v2(p_manager, EVT_WARNING,
9682 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9683 p_start_x, l_image->x0);
9684 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9685 p_image->x0 = l_image->x0;
9688 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9689 p_image->x0 = p_start_x;
9693 if ((OPJ_UINT32)p_start_y > l_image->y1){
9694 opj_event_msg_v2(p_manager, EVT_ERROR,
9695 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9696 p_start_y, l_image->y1);
9699 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9700 opj_event_msg_v2(p_manager, EVT_WARNING,
9701 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9702 p_start_y, l_image->y0);
9703 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9704 p_image->y0 = l_image->y0;
9707 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9708 p_image->y0 = p_start_y;
9712 assert((OPJ_UINT32)p_end_x > 0);
9713 assert((OPJ_UINT32)p_end_y > 0);
9714 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9715 opj_event_msg_v2(p_manager, EVT_ERROR,
9716 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9717 p_end_x, l_image->x0);
9720 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9721 opj_event_msg_v2(p_manager, EVT_WARNING,
9722 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9723 p_end_x, l_image->x1);
9724 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9725 p_image->x1 = l_image->x1;
9728 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9729 p_image->x1 = p_end_x;
9733 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9734 opj_event_msg_v2(p_manager, EVT_ERROR,
9735 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9736 p_end_y, l_image->y0);
9739 if ((OPJ_UINT32)p_end_y > l_image->y1){
9740 opj_event_msg_v2(p_manager, EVT_WARNING,
9741 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9742 p_end_y, l_image->y1);
9743 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9744 p_image->y1 = l_image->y1;
9747 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9748 p_image->y1 = p_end_y;
9752 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9754 l_img_comp = p_image->comps;
9755 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9759 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9760 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9761 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9762 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9764 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9765 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9767 opj_event_msg_v2(p_manager, EVT_ERROR,
9768 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9772 l_img_comp->w = l_w;
9774 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9775 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9777 opj_event_msg_v2(p_manager, EVT_ERROR,
9778 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9782 l_img_comp->h = l_h;
9787 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9788 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9795 /* ----------------------------------------------------------------------- */
9796 /* J2K / JPT decoder interface */
9797 /* ----------------------------------------------------------------------- */
9799 * Creates a J2K decompression structure.
9801 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9803 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9805 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9809 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9811 l_j2k->m_is_decoder = 1;
9812 l_j2k->m_cp.m_is_decoder = 1;
9814 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9815 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9819 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9821 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9822 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9827 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9829 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9831 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9833 /* codestream index creation */
9834 l_j2k->cstr_index = j2k_create_cstr_index();
9836 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9837 if (!l_j2k->cstr_index){
9842 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9845 /* validation list creation */
9846 l_j2k->m_validation_list = opj_procedure_list_create();
9847 if (! l_j2k->m_validation_list) {
9852 /* execution list creation */
9853 l_j2k->m_procedure_list = opj_procedure_list_create();
9854 if (! l_j2k->m_procedure_list) {
9863 opj_codestream_index_t* j2k_create_cstr_index(void)
9865 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9866 opj_calloc(1,sizeof(opj_codestream_index_t));
9870 cstr_index->maxmarknum = 100;
9871 cstr_index->marknum = 0;
9872 cstr_index->marker = (opj_marker_info_t*)
9873 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9874 if (!cstr_index-> marker)
9877 cstr_index->tile_index = NULL;
9884 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9886 * @param p_tile_no the tile index.
9887 * @param p_comp_no the component being outputted.
9888 * @param p_j2k the J2K codec.
9890 * @return the number of bytes taken by the SPCod element.
9892 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9893 OPJ_UINT32 p_tile_no,
9894 OPJ_UINT32 p_comp_no )
9896 opj_cp_v2_t *l_cp = 00;
9897 opj_tcp_v2_t *l_tcp = 00;
9898 opj_tccp_t *l_tccp = 00;
9901 assert(p_j2k != 00);
9903 l_cp = &(p_j2k->m_cp);
9904 l_tcp = &l_cp->tcps[p_tile_no];
9905 l_tccp = &l_tcp->tccps[p_comp_no];
9907 /* preconditions again */
9908 assert(p_tile_no < (l_cp->tw * l_cp->th));
9909 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9911 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9912 return 5 + l_tccp->numresolutions;
9920 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9922 * @param p_comp_no the component number to output.
9923 * @param p_stream the stream to write data to.
9924 * @param p_j2k J2K codec.
9925 * @param p_manager the user event manager.
9928 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9929 OPJ_UINT32 p_tile_no,
9930 OPJ_UINT32 p_comp_no,
9932 OPJ_UINT32 * p_header_size,
9933 struct opj_event_mgr * p_manager )
9936 opj_cp_v2_t *l_cp = 00;
9937 opj_tcp_v2_t *l_tcp = 00;
9938 opj_tccp_t *l_tccp = 00;
9941 assert(p_j2k != 00);
9942 assert(p_header_size != 00);
9943 assert(p_manager != 00);
9944 assert(p_data != 00);
9946 l_cp = &(p_j2k->m_cp);
9947 l_tcp = &l_cp->tcps[p_tile_no];
9948 l_tccp = &l_tcp->tccps[p_comp_no];
9950 /* preconditions again */
9951 assert(p_tile_no < (l_cp->tw * l_cp->th));
9952 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9954 if (*p_header_size < 5) {
9955 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9959 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9962 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9965 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9968 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9971 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9974 *p_header_size = *p_header_size - 5;
9976 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9978 if (*p_header_size < l_tccp->numresolutions) {
9979 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9983 for (i = 0; i < l_tccp->numresolutions; ++i) {
9984 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9988 *p_header_size = *p_header_size - l_tccp->numresolutions;
9995 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9996 * @param p_header_data the data contained in the COM box.
9997 * @param p_j2k the jpeg2000 codec.
9998 * @param p_header_size the size of the data contained in the COM marker.
9999 * @param p_manager the user event manager.
10001 opj_bool j2k_read_SPCod_SPCoc(
10002 opj_j2k_v2_t *p_j2k,
10004 OPJ_BYTE * p_header_data,
10005 OPJ_UINT32 * p_header_size,
10006 struct opj_event_mgr * p_manager
10009 OPJ_UINT32 i, l_tmp;
10010 opj_cp_v2_t *l_cp = NULL;
10011 opj_tcp_v2_t *l_tcp = NULL;
10012 opj_tccp_t *l_tccp = NULL;
10013 OPJ_BYTE * l_current_ptr = NULL;
10015 /* preconditions */
10016 assert(p_j2k != 00);
10017 assert(p_manager != 00);
10018 assert(p_header_data != 00);
10020 l_cp = &(p_j2k->m_cp);
10021 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
10022 &l_cp->tcps[p_j2k->m_current_tile_number] :
10023 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10025 /* precondition again */
10026 assert(compno < p_j2k->m_private_image->numcomps);
10028 l_tccp = &l_tcp->tccps[compno];
10029 l_current_ptr = p_header_data;
10031 /* make sure room is sufficient */
10032 if (*p_header_size < 5) {
10033 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10037 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
10038 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
10041 /* If user wants to remove more resolutions than the codestream contains, return error */
10042 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
10043 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
10044 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
10045 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
10049 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
10051 l_tccp->cblkw += 2;
10053 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
10055 l_tccp->cblkh += 2;
10057 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
10060 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
10063 *p_header_size = *p_header_size - 5;
10065 /* use custom precinct size ? */
10066 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
10067 if (*p_header_size < l_tccp->numresolutions) {
10068 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
10072 for (i = 0; i < l_tccp->numresolutions; ++i) {
10073 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
10075 l_tccp->prcw[i] = l_tmp & 0xf;
10076 l_tccp->prch[i] = l_tmp >> 4;
10079 *p_header_size = *p_header_size - l_tccp->numresolutions;
10082 /* set default size for the precinct width and height */
10083 for (i = 0; i < l_tccp->numresolutions; ++i) {
10084 l_tccp->prcw[i] = 15;
10085 l_tccp->prch[i] = 15;
10089 #ifdef WIP_REMOVE_MSD
10091 if (p_j2k->cstr_info && compno == 0) {
10092 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10094 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10095 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10096 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10097 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10098 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10101 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10102 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10111 * Copies the tile component parameters of all the component from the first tile component.
10113 * @param p_j2k the J2k codec.
10115 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10119 opj_cp_v2_t *l_cp = NULL;
10120 opj_tcp_v2_t *l_tcp = NULL;
10121 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10122 OPJ_UINT32 l_prc_size;
10124 /* preconditions */
10125 assert(p_j2k != 00);
10127 l_cp = &(p_j2k->m_cp);
10128 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10129 &l_cp->tcps[p_j2k->m_current_tile_number] :
10130 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10132 l_ref_tccp = &l_tcp->tccps[0];
10133 l_copied_tccp = l_ref_tccp + 1;
10134 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10136 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10137 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10138 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10139 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10140 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10141 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10142 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10143 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10149 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10151 * @param p_tile_no the tile index.
10152 * @param p_comp_no the component being outputted.
10153 * @param p_j2k the J2K codec.
10155 * @return the number of bytes taken by the SPCod element.
10157 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10158 OPJ_UINT32 p_tile_no,
10159 OPJ_UINT32 p_comp_no )
10161 OPJ_UINT32 l_num_bands;
10163 opj_cp_v2_t *l_cp = 00;
10164 opj_tcp_v2_t *l_tcp = 00;
10165 opj_tccp_t *l_tccp = 00;
10167 /* preconditions */
10168 assert(p_j2k != 00);
10170 l_cp = &(p_j2k->m_cp);
10171 l_tcp = &l_cp->tcps[p_tile_no];
10172 l_tccp = &l_tcp->tccps[p_comp_no];
10174 /* preconditions again */
10175 assert(p_tile_no < l_cp->tw * l_cp->th);
10176 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10178 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10180 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10181 return 1 + l_num_bands;
10184 return 1 + 2*l_num_bands;
10189 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10191 * @param p_tile_no the tile to output.
10192 * @param p_comp_no the component number to output.
10193 * @param p_data the data buffer.
10194 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10195 * @param p_j2k J2K codec.
10196 * @param p_manager the user event manager.
10199 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10200 OPJ_UINT32 p_tile_no,
10201 OPJ_UINT32 p_comp_no,
10203 OPJ_UINT32 * p_header_size,
10204 struct opj_event_mgr * p_manager )
10206 OPJ_UINT32 l_header_size;
10207 OPJ_UINT32 l_band_no, l_num_bands;
10208 OPJ_UINT32 l_expn,l_mant;
10210 opj_cp_v2_t *l_cp = 00;
10211 opj_tcp_v2_t *l_tcp = 00;
10212 opj_tccp_t *l_tccp = 00;
10214 /* preconditions */
10215 assert(p_j2k != 00);
10216 assert(p_header_size != 00);
10217 assert(p_manager != 00);
10218 assert(p_data != 00);
10220 l_cp = &(p_j2k->m_cp);
10221 l_tcp = &l_cp->tcps[p_tile_no];
10222 l_tccp = &l_tcp->tccps[p_comp_no];
10224 /* preconditions again */
10225 assert(p_tile_no < l_cp->tw * l_cp->th);
10226 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10228 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10230 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10231 l_header_size = 1 + l_num_bands;
10233 if (*p_header_size < l_header_size) {
10234 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10238 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10241 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10242 l_expn = l_tccp->stepsizes[l_band_no].expn;
10243 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10248 l_header_size = 1 + 2*l_num_bands;
10250 if (*p_header_size < l_header_size) {
10251 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10255 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10258 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10259 l_expn = l_tccp->stepsizes[l_band_no].expn;
10260 l_mant = l_tccp->stepsizes[l_band_no].mant;
10262 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10267 *p_header_size = *p_header_size - l_header_size;
10273 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10275 * @param p_comp_no the component being targeted.
10276 * @param p_header_data the data contained in the COM box.
10277 * @param p_j2k the jpeg2000 codec.
10278 * @param p_header_size the size of the data contained in the COM marker.
10279 * @param p_manager the user event manager.
10281 opj_bool j2k_read_SQcd_SQcc(
10282 opj_j2k_v2_t *p_j2k,
10283 OPJ_UINT32 p_comp_no,
10284 OPJ_BYTE* p_header_data,
10285 OPJ_UINT32 * p_header_size,
10286 struct opj_event_mgr * p_manager
10290 OPJ_UINT32 l_band_no;
10291 opj_cp_v2_t *l_cp = 00;
10292 opj_tcp_v2_t *l_tcp = 00;
10293 opj_tccp_t *l_tccp = 00;
10294 OPJ_BYTE * l_current_ptr = 00;
10295 OPJ_UINT32 l_tmp, l_num_band;
10298 assert(p_j2k != 00);
10299 assert(p_manager != 00);
10300 assert(p_header_data != 00);
10302 l_cp = &(p_j2k->m_cp);
10303 /* come from tile part header or main header ?*/
10304 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10305 &l_cp->tcps[p_j2k->m_current_tile_number] :
10306 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10308 /* precondition again*/
10309 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10311 l_tccp = &l_tcp->tccps[p_comp_no];
10312 l_current_ptr = p_header_data;
10314 if (*p_header_size < 1) {
10315 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10318 *p_header_size -= 1;
10320 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10323 l_tccp->qntsty = l_tmp & 0x1f;
10324 l_tccp->numgbits = l_tmp >> 5;
10325 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10329 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10331 (*p_header_size) / 2;
10333 if( l_num_band > J2K_MAXBANDS ) {
10334 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10335 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10336 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10337 /*return OPJ_FALSE;*/
10342 if (l_cp->correct) {
10344 /* if JPWL is on, we check whether there are too many subbands */
10345 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10346 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10347 "JPWL: bad number of subbands in Sqcx (%d)\n",
10349 if (!JPWL_ASSUME) {
10350 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10353 /* we try to correct */
10355 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10356 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10361 #endif /* USE_JPWL */
10363 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10364 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10365 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10367 if (l_band_no < J2K_MAXBANDS){
10368 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10369 l_tccp->stepsizes[l_band_no].mant = 0;
10372 *p_header_size = *p_header_size - l_num_band;
10375 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10376 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10378 if (l_band_no < J2K_MAXBANDS){
10379 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10380 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10383 *p_header_size = *p_header_size - 2*l_num_band;
10386 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10387 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10388 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10389 l_tccp->stepsizes[l_band_no].expn =
10390 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10391 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10392 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10400 * Copies the tile component parameters of all the component from the first tile component.
10402 * @param p_j2k the J2k codec.
10404 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10407 opj_cp_v2_t *l_cp = NULL;
10408 opj_tcp_v2_t *l_tcp = NULL;
10409 opj_tccp_t *l_ref_tccp = NULL;
10410 opj_tccp_t *l_copied_tccp = NULL;
10413 /* preconditions */
10414 assert(p_j2k != 00);
10416 l_cp = &(p_j2k->m_cp);
10417 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10418 &l_cp->tcps[p_j2k->m_current_tile_number] :
10419 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10421 l_ref_tccp = &l_tcp->tccps[0];
10422 l_copied_tccp = l_ref_tccp + 1;
10423 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10425 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10426 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10427 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10428 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10434 * Dump some elements from the J2K decompression structure .
10436 *@param p_j2k the jpeg2000 codec.
10437 *@param flag flag to describe what elments are dump.
10438 *@param out_stream output stream where dump the elements.
10441 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10443 /* Check if the flag is compatible with j2k file*/
10444 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10445 fprintf(out_stream, "Wrong flag\n");
10449 /* Dump the image_header */
10450 if (flag & OPJ_IMG_INFO){
10451 if (p_j2k->m_private_image)
10452 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10455 /* Dump the codestream info from main header */
10456 if (flag & OPJ_J2K_MH_INFO){
10457 j2k_dump_MH_info(p_j2k, out_stream);
10461 /* Dump the codestream info of the current tile */
10462 if (flag & OPJ_J2K_TH_INFO){
10466 /* Dump the codestream index from main header */
10467 if (flag & OPJ_J2K_MH_IND){
10468 j2k_dump_MH_index(p_j2k, out_stream);
10471 /* Dump the codestream index of the current tile */
10472 if (flag & OPJ_J2K_TH_IND){
10479 * Dump index elements of the codestream extract from the main header.
10481 *@param p_j2k the jpeg2000 codec.
10482 *@param out_stream output stream where dump the elements.
10485 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10487 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10488 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10490 fprintf(out_stream, "Codestream index from main header: {\n");
10492 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10493 "\t Main header end position=%" PRIi64 "\n",
10494 cstr_index->main_head_start, cstr_index->main_head_end);
10496 fprintf(out_stream, "\t Marker list: {\n");
10498 if (cstr_index->marker){
10499 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10500 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10501 cstr_index->marker[it_marker].type,
10502 cstr_index->marker[it_marker].pos,
10503 cstr_index->marker[it_marker].len );
10507 fprintf(out_stream, "\t }\n");
10510 if (cstr_index->tile_index){
10512 /* Simple test to avoid to write empty information*/
10513 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10514 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10515 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10518 if (l_acc_nb_of_tile_part)
10520 fprintf(out_stream, "\t Tile index: {\n");
10522 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10523 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10525 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10527 if (cstr_index->tile_index[it_tile].tp_index){
10528 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10529 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10531 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10532 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10533 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10537 if (cstr_index->tile_index[it_tile].marker){
10538 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10539 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10540 cstr_index->tile_index[it_tile].marker[it_marker].type,
10541 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10542 cstr_index->tile_index[it_tile].marker[it_marker].len );
10546 fprintf(out_stream,"\t }\n");
10550 fprintf(out_stream,"}\n");
10555 * Dump info elements of the codestream extract from the main header.
10557 *@param p_j2k the jpeg2000 codec.
10558 *@param out_stream output stream where dump the elements.
10561 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10563 opj_tcp_v2_t * l_default_tile=NULL;
10565 fprintf(out_stream, "Codestream info from main header: {\n");
10567 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10568 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10569 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10571 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10572 if (l_default_tile)
10575 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10577 fprintf(out_stream, "\t default tile {\n");
10578 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10579 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10580 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10581 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10583 for (compno = 0; compno < numcomps; compno++) {
10584 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10586 OPJ_INT32 bandno, numbands;
10589 fprintf(out_stream, "\t\t comp %d {\n", compno);
10590 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10591 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10592 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10593 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10594 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10595 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10597 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10598 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10599 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10601 fprintf(out_stream, "\n");
10603 /* quantization style*/
10604 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10605 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10606 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10607 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10608 for (bandno = 0; bandno < numbands; bandno++) {
10609 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10610 l_tccp->stepsizes[bandno].expn);
10612 fprintf(out_stream, "\n");
10615 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10617 fprintf(out_stream, "\t\t }\n");
10618 } /*end of component of default tile*/
10619 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10623 fprintf(out_stream, "}\n");
10628 * Dump an image header structure.
10630 *@param img_header the image header to dump.
10631 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10632 *@param out_stream output stream where dump the elements.
10634 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10638 if (dev_dump_flag){
10639 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10643 fprintf(out_stream, "Image info {\n");
10644 tab[0] = '\t';tab[1] = '\0';
10647 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10648 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10649 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10651 if (img_header->comps){
10653 for (compno = 0; compno < img_header->numcomps; compno++) {
10654 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10655 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10656 fprintf(out_stream,"%s}\n",tab);
10660 fprintf(out_stream, "}\n");
10664 * Dump a component image header structure.
10666 *@param comp_header the component image header to dump.
10667 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10668 *@param out_stream output stream where dump the elements.
10670 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10674 if (dev_dump_flag){
10675 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10678 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10681 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10682 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10683 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10686 fprintf(out_stream, "}\n");
10691 * Get the codestream info from a JPEG2000 codec.
10693 *@param p_j2k the component image header to dump.
10695 *@return the codestream information extract from the jpg2000 codec
10697 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10700 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10701 opj_tcp_v2_t *l_default_tile;
10702 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10704 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10706 cstr_info->tx0 = p_j2k->m_cp.tx0;
10707 cstr_info->ty0 = p_j2k->m_cp.ty0;
10708 cstr_info->tdx = p_j2k->m_cp.tdx;
10709 cstr_info->tdy = p_j2k->m_cp.tdy;
10710 cstr_info->tw = p_j2k->m_cp.tw;
10711 cstr_info->th = p_j2k->m_cp.th;
10713 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10715 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10717 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10718 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10719 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10720 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10722 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10724 for (compno = 0; compno < numcomps; compno++) {
10725 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10726 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10727 OPJ_INT32 bandno, numbands;
10730 l_tccp_info->csty = l_tccp->csty;
10731 l_tccp_info->numresolutions = l_tccp->numresolutions;
10732 l_tccp_info->cblkw = l_tccp->cblkw;
10733 l_tccp_info->cblkh = l_tccp->cblkh;
10734 l_tccp_info->cblksty = l_tccp->cblksty;
10735 l_tccp_info->qmfbid = l_tccp->qmfbid;
10736 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10738 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10739 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10742 /* quantization style*/
10743 l_tccp_info->qntsty = l_tccp->qntsty;
10744 l_tccp_info->numgbits = l_tccp->numgbits;
10746 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10747 if (numbands < J2K_MAXBANDS) {
10748 for (bandno = 0; bandno < numbands; bandno++) {
10749 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10750 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10755 l_tccp_info->roishift = l_tccp->roishift;
10763 * Get the codestream index from a JPEG2000 codec.
10765 *@param p_j2k the component image header to dump.
10767 *@return the codestream index extract from the jpg2000 codec
10769 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10771 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10772 opj_calloc(1,sizeof(opj_codestream_index_t));
10776 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10777 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10778 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10780 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10781 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10782 if (!l_cstr_index->marker){
10783 opj_free( l_cstr_index);
10787 if (p_j2k->cstr_index->marker)
10788 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10790 opj_free(l_cstr_index->marker);
10791 l_cstr_index->marker = NULL;
10794 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10795 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10796 if (!l_cstr_index->tile_index){
10797 opj_free( l_cstr_index->marker);
10798 opj_free( l_cstr_index);
10802 if (!p_j2k->cstr_index->tile_index){
10803 opj_free(l_cstr_index->tile_index);
10804 l_cstr_index->tile_index = NULL;
10807 OPJ_UINT32 it_tile = 0;
10808 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10811 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10813 l_cstr_index->tile_index[it_tile].marker =
10814 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10816 if (!l_cstr_index->tile_index[it_tile].marker) {
10817 OPJ_UINT32 it_tile_free;
10819 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10820 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10823 opj_free( l_cstr_index->tile_index);
10824 opj_free( l_cstr_index->marker);
10825 opj_free( l_cstr_index);
10829 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10830 memcpy( l_cstr_index->tile_index[it_tile].marker,
10831 p_j2k->cstr_index->tile_index[it_tile].marker,
10832 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10834 opj_free(l_cstr_index->tile_index[it_tile].marker);
10835 l_cstr_index->tile_index[it_tile].marker = NULL;
10838 /* Tile part index*/
10839 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10841 l_cstr_index->tile_index[it_tile].tp_index =
10842 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10844 if(!l_cstr_index->tile_index[it_tile].tp_index){
10845 OPJ_UINT32 it_tile_free;
10847 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10848 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10849 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10852 opj_free( l_cstr_index->tile_index);
10853 opj_free( l_cstr_index->marker);
10854 opj_free( l_cstr_index);
10858 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10859 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10860 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10861 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10864 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10865 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10868 /* Packet index (NOT USED)*/
10869 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10870 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10875 return l_cstr_index;
10878 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10880 OPJ_UINT32 it_tile=0;
10882 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10883 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10884 if (!p_j2k->cstr_index->tile_index)
10887 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10888 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10889 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10890 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10891 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10892 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10902 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10903 opj_stream_private_t *p_stream,
10904 opj_event_mgr_t * p_manager)
10906 opj_bool l_go_on = OPJ_TRUE;
10907 OPJ_UINT32 l_current_tile_no;
10908 OPJ_UINT32 l_data_size,l_max_data_size;
10909 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10910 OPJ_UINT32 l_nb_comps;
10911 OPJ_BYTE * l_current_data;
10913 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10914 if (! l_current_data) {
10917 l_max_data_size = 1000;
10922 if (! j2k_read_tile_header( p_j2k,
10923 &l_current_tile_no,
10925 &l_tile_x0, &l_tile_y0,
10926 &l_tile_x1, &l_tile_y1,
10931 opj_free(l_current_data);
10939 if (l_data_size > l_max_data_size) {
10940 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10941 if (! l_current_data) {
10942 opj_free(l_current_data);
10946 l_max_data_size = l_data_size;
10949 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10950 opj_free(l_current_data);
10953 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);
10955 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10956 opj_free(l_current_data);
10959 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10963 opj_free(l_current_data);
10969 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10971 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10974 assert(p_j2k != 00);
10976 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
10977 /* DEVELOPER CORNER, add your custom procedures */
10982 * Read and decode one tile.
10984 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10985 opj_stream_private_t *p_stream,
10986 opj_event_mgr_t * p_manager)
10988 opj_bool l_go_on = OPJ_TRUE;
10989 OPJ_UINT32 l_current_tile_no;
10990 OPJ_UINT32 l_tile_no_to_dec;
10991 OPJ_UINT32 l_data_size,l_max_data_size;
10992 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10993 OPJ_UINT32 l_nb_comps;
10994 OPJ_BYTE * l_current_data;
10996 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10997 if (! l_current_data) {
11000 l_max_data_size = 1000;
11002 /*Allocate and initialize some elements of codestrem index if not already done*/
11003 if( !p_j2k->cstr_index->tile_index)
11005 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
11006 opj_free(l_current_data);
11010 /* Move into the codestream to the first SOT used to decode the desired tile */
11011 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
11012 if (p_j2k->cstr_index->tile_index)
11013 if(p_j2k->cstr_index->tile_index->tp_index)
11015 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
11016 /* the index for this tile has not been built,
11017 * so move to the last SOT read */
11018 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
11019 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11024 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)) {
11025 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11029 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
11030 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
11031 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
11035 if (! j2k_read_tile_header( p_j2k,
11036 &l_current_tile_no,
11038 &l_tile_x0, &l_tile_y0,
11039 &l_tile_x1, &l_tile_y1,
11044 opj_free(l_current_data);
11053 if (l_data_size > l_max_data_size) {
11054 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
11055 if (! l_current_data) {
11056 opj_free(l_current_data);
11060 l_max_data_size = l_data_size;
11065 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
11066 opj_free(l_current_data);
11069 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);
11071 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
11072 opj_free(l_current_data);
11075 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
11077 if(l_current_tile_no == l_tile_no_to_dec)
11079 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11080 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11081 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11087 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);
11092 opj_free(l_current_data);
11099 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11101 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11104 assert(p_j2k != 00);
11106 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11107 /* DEVELOPER CORNER, add your custom procedures */
11113 * Decodes the tiles of the stream.
11115 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
11116 opj_stream_private_t * p_stream,
11117 opj_image_t * p_image,
11118 opj_event_mgr_t * p_manager)
11125 p_j2k->m_output_image = opj_image_create0();
11126 if (! (p_j2k->m_output_image)) {
11129 opj_copy_image_header(p_image, p_j2k->m_output_image);
11131 /* customization of the decoding */
11132 j2k_setup_decoding(p_j2k);
11134 /* Decode the codestream */
11135 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11136 opj_image_destroy(p_j2k->m_private_image);
11137 p_j2k->m_private_image = NULL;
11141 /* Move data and copy one information from codec to output image*/
11142 for (compno = 0; compno < p_image->numcomps; compno++) {
11143 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11144 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11145 p_j2k->m_output_image->comps[compno].data = NULL;
11153 * Get the decoded tile.
11155 * @param p_j2k the jpeg2000 codestream codec.
11156 * @param p_stream input_stream
11157 * @param p_image output image. .
11158 * @param p_manager the user event manager
11159 * @param tile_index index of the tile we want decode
11161 * @return true if succeed.
11163 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11164 opj_stream_private_t *p_stream,
11165 opj_image_t* p_image,
11166 struct opj_event_mgr * p_manager,
11167 OPJ_UINT32 tile_index )
11170 OPJ_UINT32 l_tile_x, l_tile_y;
11171 opj_image_comp_t* l_img_comp;
11174 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11178 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11179 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);
11183 /* Compute the dimension of the desired tile*/
11184 l_tile_x = tile_index % p_j2k->m_cp.tw;
11185 l_tile_y = tile_index / p_j2k->m_cp.tw;
11187 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11188 if (p_image->x0 < p_j2k->m_private_image->x0)
11189 p_image->x0 = p_j2k->m_private_image->x0;
11190 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11191 if (p_image->x1 > p_j2k->m_private_image->x1)
11192 p_image->x1 = p_j2k->m_private_image->x1;
11194 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11195 if (p_image->y0 < p_j2k->m_private_image->y0)
11196 p_image->y0 = p_j2k->m_private_image->y0;
11197 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11198 if (p_image->y1 > p_j2k->m_private_image->y1)
11199 p_image->y1 = p_j2k->m_private_image->y1;
11201 l_img_comp = p_image->comps;
11202 for (compno=0; compno < p_image->numcomps; ++compno)
11204 OPJ_INT32 l_comp_x1, l_comp_y1;
11206 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11208 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11209 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11210 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11211 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11213 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11214 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11219 /* Destroy the previous output image*/
11220 if (p_j2k->m_output_image)
11221 opj_image_destroy(p_j2k->m_output_image);
11223 /* Create the ouput image from the information previously computed*/
11224 p_j2k->m_output_image = opj_image_create0();
11225 if (! (p_j2k->m_output_image)) {
11228 opj_copy_image_header(p_image, p_j2k->m_output_image);
11230 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11232 /* customization of the decoding */
11233 j2k_setup_decoding_tile(p_j2k);
11235 /* Decode the codestream */
11236 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11237 opj_image_destroy(p_j2k->m_private_image);
11238 p_j2k->m_private_image = NULL;
11242 /* Move data and copy one information from codec to output image*/
11243 for (compno = 0; compno < p_image->numcomps; compno++) {
11244 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11246 if (p_image->comps[compno].data)
11247 opj_free(p_image->comps[compno].data);
11249 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11251 p_j2k->m_output_image->comps[compno].data = NULL;
11257 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11259 OPJ_UINT32 it_comp;
11261 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11263 if (p_j2k->m_private_image) {
11264 if (p_j2k->m_private_image->comps) {
11265 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11266 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11267 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11268 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11269 if ( res_factor >= max_res){
11270 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11273 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11286 * Encodes all the tiles in a row.
11288 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11289 opj_stream_private_t *p_stream,
11290 opj_event_mgr_t * p_manager )
11293 OPJ_UINT32 l_nb_tiles;
11294 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11295 OPJ_BYTE * l_current_data;
11297 /* preconditions */
11298 assert(p_j2k != 00);
11299 assert(p_stream != 00);
11300 assert(p_manager != 00);
11302 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11303 if (! l_current_data) {
11306 l_max_tile_size = 1000;
11308 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11309 for (i=0;i<l_nb_tiles;++i) {
11310 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11311 opj_free(l_current_data);
11315 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11316 if (l_current_tile_size > l_max_tile_size) {
11317 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11318 if (! l_current_data) {
11321 l_max_tile_size = l_current_tile_size;
11324 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11326 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11331 opj_free(l_current_data);
11336 * Ends the compression procedures and possibility add data to be read after the
11339 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11340 opj_stream_private_t *p_stream,
11341 struct opj_event_mgr * p_manager)
11343 /* customization of the encoding */
11344 j2k_setup_end_compress(p_j2k);
11346 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11356 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11358 * @param p_j2k the jpeg2000 codec.
11359 * @param p_stream the stream object.
11360 * @param p_manager the user event manager.
11362 * @return true if the codec is valid.
11364 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11365 opj_stream_private_t *p_stream,
11366 opj_image_t * p_image,
11367 opj_event_mgr_t * p_manager)
11369 /* preconditions */
11370 assert(p_j2k != 00);
11371 assert(p_stream != 00);
11372 assert(p_manager != 00);
11374 p_j2k->m_private_image = opj_image_create0();
11375 opj_copy_image_header(p_image, p_j2k->m_private_image);
11377 // TODO_MSD: Find a better way
11378 if (p_image->comps) {
11379 OPJ_UINT32 it_comp;
11380 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11381 if (p_image->comps[it_comp].data) {
11382 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11383 p_image->comps[it_comp].data = NULL;
11389 /* customization of the validation */
11390 j2k_setup_encoding_validation (p_j2k);
11392 /* validation of the parameters codec */
11393 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11397 /* customization of the encoding */
11398 j2k_setup_header_writting(p_j2k);
11401 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11411 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11412 OPJ_UINT32 p_tile_index,
11413 opj_stream_private_t *p_stream,
11414 opj_event_mgr_t * p_manager )
11417 if (p_tile_index != p_j2k->m_current_tile_number) {
11418 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11422 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);
11424 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11425 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11426 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11428 /* initialisation before tile encoding */
11429 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11439 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11441 OPJ_UINT32 i,j,k = 0;
11442 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11443 opj_image_comp_t * l_img_comp = 00;
11444 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11445 opj_image_t * l_image = 00;
11446 OPJ_UINT32 l_size_comp, l_remaining;
11447 OPJ_INT32 * l_src_ptr;
11448 l_tilec = p_tcd->tcd_image->tiles->comps;
11449 l_image = p_tcd->image;
11450 l_img_comp = l_image->comps;
11452 for (i=0;i<p_tcd->image->numcomps;++i) {
11453 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11454 l_remaining = l_img_comp->prec & 7; /* (%8) */
11459 if (l_size_comp == 3) {
11463 l_width = (l_tilec->x1 - l_tilec->x0);
11464 l_height = (l_tilec->y1 - l_tilec->y0);
11465 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11466 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11467 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11468 l_stride = l_image_width - l_width;
11469 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11471 switch (l_size_comp) {
11474 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11475 if (l_img_comp->sgnd) {
11476 for (j=0;j<l_height;++j) {
11477 for (k=0;k<l_width;++k) {
11478 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11482 l_src_ptr += l_stride;
11486 for (j=0;j<l_height;++j) {
11487 for (k=0;k<l_width;++k) {
11488 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11492 l_src_ptr += l_stride;
11496 p_data = (OPJ_BYTE*) l_dest_ptr;
11501 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11502 if (l_img_comp->sgnd) {
11503 for (j=0;j<l_height;++j) {
11504 for (k=0;k<l_width;++k) {
11505 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11507 l_src_ptr += l_stride;
11511 for (j=0;j<l_height;++j) {
11512 for (k=0;k<l_width;++k) {
11513 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11515 l_src_ptr += l_stride;
11519 p_data = (OPJ_BYTE*) l_dest_ptr;
11524 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11525 for (j=0;j<l_height;++j) {
11526 for (k=0;k<l_width;++k) {
11527 *(l_dest_ptr++) = *(l_src_ptr++);
11529 l_src_ptr += l_stride;
11532 p_data = (OPJ_BYTE*) l_dest_ptr;
11545 * @param p_j2k the jpeg2000 codec.
11546 * @param p_stream the stream to write data to.
11547 * @param p_manager the user event manager.
11549 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11551 OPJ_UINT32 p_data_size,
11552 opj_stream_private_t *p_stream,
11553 opj_event_mgr_t * p_manager )
11555 opj_tcd_v2_t * l_tcd = 00;
11556 opj_cp_v2_t * l_cp = 00;
11557 opj_tcp_v2_t * l_tcp = 00;
11558 OPJ_UINT32 l_nb_bytes_written;
11559 OPJ_BYTE * l_current_data = 00;
11560 OPJ_UINT32 l_tile_size = 0;
11561 OPJ_UINT32 l_available_data;
11563 /* preconditions */
11564 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11566 l_tcd = p_j2k->m_tcd;
11567 l_cp = &(p_j2k->m_cp);
11568 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11570 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11571 l_available_data = l_tile_size;
11572 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11574 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11575 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11579 l_nb_bytes_written = 0;
11580 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11583 l_current_data += l_nb_bytes_written;
11584 l_available_data -= l_nb_bytes_written;
11586 l_nb_bytes_written = 0;
11587 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11591 l_available_data -= l_nb_bytes_written;
11592 l_nb_bytes_written = l_tile_size - l_available_data;
11594 if ( opj_stream_write_data( p_stream,
11595 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11596 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11600 ++p_j2k->m_current_tile_number;
11607 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11608 * are valid. Developers wanting to extend the library can add their own validation procedures.
11610 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11612 /* preconditions */
11613 assert(p_j2k != 00);
11615 /* DEVELOPER CORNER, insert your custom procedures */
11616 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11618 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11619 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11622 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11623 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11624 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11628 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11629 * are valid. Developers wanting to extend the library can add their own validation procedures.
11631 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11633 /* preconditions */
11634 assert(p_j2k != 00);
11636 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11637 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11639 /* DEVELOPER CORNER, add your custom validation procedure */
11640 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11645 * Sets up the procedures to do on writing header.
11646 * Developers wanting to extend the library can add their own writing procedures.
11648 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11650 /* preconditions */
11651 assert(p_j2k != 00);
11653 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11654 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11655 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11656 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11657 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11660 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11661 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11662 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11664 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11665 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11669 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11671 if (p_j2k->m_cp.comment != 00) {
11672 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11675 /* DEVELOPER CORNER, insert your custom procedures */
11676 if (p_j2k->m_cp.rsiz & MCT) {
11677 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11679 /* End of Developer Corner */
11681 if (p_j2k->cstr_index) {
11682 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11685 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11686 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11690 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11692 OPJ_UINT32 * p_data_written,
11693 OPJ_UINT32 p_total_data_size,
11694 opj_stream_private_t *p_stream,
11695 struct opj_event_mgr * p_manager )
11698 OPJ_UINT32 l_nb_bytes_written = 0;
11699 OPJ_UINT32 l_current_nb_bytes_written;
11700 OPJ_BYTE * l_begin_data = 00;
11702 opj_tcp_v2_t *l_tcp = 00;
11703 opj_tcd_v2_t * l_tcd = 00;
11704 opj_cp_v2_t * l_cp = 00;
11706 l_tcd = p_j2k->m_tcd;
11707 l_cp = &(p_j2k->m_cp);
11708 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11710 l_tcd->cur_pino = 0;
11712 /*Get number of tile parts*/
11713 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11718 l_current_nb_bytes_written = 0;
11719 l_begin_data = p_data;
11720 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11725 l_nb_bytes_written += l_current_nb_bytes_written;
11726 p_data += l_current_nb_bytes_written;
11727 p_total_data_size -= l_current_nb_bytes_written;
11729 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11730 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11731 l_current_nb_bytes_written = 0;
11732 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11733 l_nb_bytes_written += l_current_nb_bytes_written;
11734 p_data += l_current_nb_bytes_written;
11735 p_total_data_size -= l_current_nb_bytes_written;
11737 l_current_nb_bytes_written = 0;
11738 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11739 l_nb_bytes_written += l_current_nb_bytes_written;
11740 p_data += l_current_nb_bytes_written;
11741 p_total_data_size -= l_current_nb_bytes_written;
11744 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11745 l_current_nb_bytes_written = 0;
11746 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11747 l_nb_bytes_written += l_current_nb_bytes_written;
11748 p_data += l_current_nb_bytes_written;
11749 p_total_data_size -= l_current_nb_bytes_written;
11753 l_current_nb_bytes_written = 0;
11754 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11758 l_nb_bytes_written += l_current_nb_bytes_written;
11759 * p_data_written = l_nb_bytes_written;
11761 /* Writing Psot in SOT marker */
11762 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11764 if (l_cp->m_specific_param.m_enc.m_cinema){
11765 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11771 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11773 OPJ_UINT32 * p_data_written,
11774 OPJ_UINT32 p_total_data_size,
11775 opj_stream_private_t *p_stream,
11776 struct opj_event_mgr * p_manager
11779 OPJ_UINT32 tilepartno=0;
11780 OPJ_UINT32 l_nb_bytes_written = 0;
11781 OPJ_UINT32 l_current_nb_bytes_written;
11782 OPJ_UINT32 l_part_tile_size;
11783 OPJ_UINT32 tot_num_tp;
11786 OPJ_BYTE * l_begin_data;
11787 opj_tcp_v2_t *l_tcp = 00;
11788 opj_tcd_v2_t * l_tcd = 00;
11789 opj_cp_v2_t * l_cp = 00;
11792 l_tcd = p_j2k->m_tcd;
11793 l_cp = &(p_j2k->m_cp);
11794 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11796 /*Get number of tile parts*/
11797 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11799 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11800 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11801 l_current_nb_bytes_written = 0;
11802 l_part_tile_size = 0;
11803 l_begin_data = p_data;
11805 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11809 l_nb_bytes_written += l_current_nb_bytes_written;
11810 p_data += l_current_nb_bytes_written;
11811 p_total_data_size -= l_current_nb_bytes_written;
11812 l_part_tile_size += l_nb_bytes_written;
11814 l_current_nb_bytes_written = 0;
11815 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11819 p_data += l_current_nb_bytes_written;
11820 l_nb_bytes_written += l_current_nb_bytes_written;
11821 p_total_data_size -= l_current_nb_bytes_written;
11822 l_part_tile_size += l_nb_bytes_written;
11824 /* Writing Psot in SOT marker */
11825 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11827 if (l_cp->m_specific_param.m_enc.m_cinema) {
11828 j2k_update_tlm(p_j2k,l_part_tile_size);
11831 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11834 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11835 l_tcd->cur_pino = pino;
11837 /*Get number of tile parts*/
11838 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11839 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11840 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11841 l_current_nb_bytes_written = 0;
11842 l_part_tile_size = 0;
11843 l_begin_data = p_data;
11845 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11849 l_nb_bytes_written += l_current_nb_bytes_written;
11850 p_data += l_current_nb_bytes_written;
11851 p_total_data_size -= l_current_nb_bytes_written;
11852 l_part_tile_size += l_current_nb_bytes_written;
11854 l_current_nb_bytes_written = 0;
11856 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11860 l_nb_bytes_written += l_current_nb_bytes_written;
11861 p_data += l_current_nb_bytes_written;
11862 p_total_data_size -= l_current_nb_bytes_written;
11863 l_part_tile_size += l_current_nb_bytes_written;
11865 /* Writing Psot in SOT marker */
11866 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11868 if (l_cp->m_specific_param.m_enc.m_cinema) {
11869 j2k_update_tlm(p_j2k,l_part_tile_size);
11872 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11876 *p_data_written = l_nb_bytes_written;
11882 * Writes the updated tlm.
11884 * @param p_stream the stream to write data to.
11885 * @param p_j2k J2K codec.
11886 * @param p_manager the user event manager.
11888 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11889 struct opj_stream_private *p_stream,
11890 struct opj_event_mgr * p_manager )
11892 OPJ_UINT32 l_tlm_size;
11893 OPJ_SIZE_T l_tlm_position, l_current_position;
11895 /* preconditions */
11896 assert(p_j2k != 00);
11897 assert(p_manager != 00);
11898 assert(p_stream != 00);
11900 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11901 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11902 l_current_position = opj_stream_tell(p_stream);
11904 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11908 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) {
11912 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11921 * Ends the encoding, i.e. frees memory.
11923 * @param p_stream the stream to write data to.
11924 * @param p_j2k J2K codec.
11925 * @param p_manager the user event manager.
11927 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11928 struct opj_stream_private *p_stream,
11929 struct opj_event_mgr * p_manager )
11931 /* preconditions */
11932 assert(p_j2k != 00);
11933 assert(p_manager != 00);
11934 assert(p_stream != 00);
11936 tcd_destroy_v2(p_j2k->m_tcd);
11939 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11940 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11941 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11942 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11945 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11946 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11947 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11950 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11956 * Destroys the memory associated with the decoding of headers.
11958 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
11959 opj_stream_private_t *p_stream,
11960 opj_event_mgr_t * p_manager )
11962 /* preconditions */
11963 assert(p_j2k != 00);
11964 assert(p_stream != 00);
11965 assert(p_manager != 00);
11967 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11968 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11969 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11972 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11981 * @param p_stream the stream to write data to.
11982 * @param p_j2k J2K codec.
11983 * @param p_manager the user event manager.
11985 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11986 struct opj_stream_private *p_stream,
11987 struct opj_event_mgr * p_manager )
11989 opj_codestream_info_t * l_cstr_info = 00;
11991 /* preconditions */
11992 assert(p_j2k != 00);
11993 assert(p_manager != 00);
11994 assert(p_stream != 00);
11997 /* TODO mergeV2: check this part which use cstr_info */
11998 /*l_cstr_info = p_j2k->cstr_info;
12002 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));
12004 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
12005 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
12007 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
12009 l_cstr_info->tw = p_j2k->m_cp.tw;
12010 l_cstr_info->th = p_j2k->m_cp.th;
12012 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
12013 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
12014 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
12015 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
12017 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
12019 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
12021 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
12023 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
12024 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
12027 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
12029 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
12031 /*l_cstr_info->maxmarknum = 100;
12032 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
12033 l_cstr_info->marknum = 0;
12036 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);
12040 * Creates a tile-coder decoder.
12042 * @param p_stream the stream to write data to.
12043 * @param p_j2k J2K codec.
12044 * @param p_manager the user event manager.
12046 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
12047 struct opj_stream_private *p_stream,
12048 struct opj_event_mgr * p_manager )
12050 /* preconditions */
12051 assert(p_j2k != 00);
12052 assert(p_manager != 00);
12053 assert(p_stream != 00);
12055 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
12057 if (! p_j2k->m_tcd) {
12058 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
12062 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
12063 tcd_destroy_v2(p_j2k->m_tcd);
12074 * @param p_j2k the jpeg2000 codec.
12075 * @param p_stream the stream to write data to.
12076 * @param p_manager the user event manager.
12078 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12079 OPJ_UINT32 p_tile_index,
12081 OPJ_UINT32 p_data_size,
12082 opj_stream_private_t *p_stream,
12083 opj_event_mgr_t * p_manager )
12085 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12086 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12090 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12091 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);