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);
575 Write the QCD marker (quantization default)
576 @param j2k J2K handle
578 static void j2k_write_qcd(opj_j2k_t *j2k);
581 * Writes the QCD marker (quantization default)
583 * @param p_comp_number the index of the component to output.
584 * @param p_stream the stream to write data to.
585 * @param p_j2k J2K codec.
586 * @param p_manager the user event manager.
588 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
589 struct opj_stream_private *p_stream,
590 struct opj_event_mgr * p_manager );
593 * Reads a QCD marker (Quantization defaults)
594 * @param p_header_data the data contained in the QCD box.
595 * @param p_j2k the jpeg2000 codec.
596 * @param p_header_size the size of the data contained in the QCD marker.
597 * @param p_manager the user event manager.
599 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
600 OPJ_BYTE * p_header_data,
601 OPJ_UINT32 p_header_size,
602 opj_event_mgr_t * p_manager );
605 Write the QCC marker (quantization component)
606 @param j2k J2K handle
607 @param compno Number of the component concerned by the information written
609 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
612 * Writes the QCC marker (quantization component)
614 * @param p_comp_no the index of the component to output.
615 * @param p_stream the stream to write data to.
616 * @param p_j2k J2K codec.
617 * @param p_manager the user event manager.
619 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
620 OPJ_UINT32 p_comp_no,
621 struct opj_stream_private *p_stream,
622 struct opj_event_mgr * p_manager );
625 * Writes the QCC marker (quantization component)
627 * @param p_comp_no the index of the component to output.
628 * @param p_stream the stream to write data to.
629 * @param p_j2k J2K codec.
630 * @param p_manager the user event manager.
632 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
633 OPJ_UINT32 p_comp_no,
635 OPJ_UINT32 * p_data_written,
636 struct opj_event_mgr * p_manager );
639 * Gets the maximum size taken by a qcc.
641 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
644 * Reads a QCC marker (Quantization component)
645 * @param p_header_data the data contained in the QCC box.
646 * @param p_j2k the jpeg2000 codec.
647 * @param p_header_size the size of the data contained in the QCC marker.
648 * @param p_manager the user event manager.
650 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
651 OPJ_BYTE * p_header_data,
652 OPJ_UINT32 p_header_size,
653 opj_event_mgr_t * p_manager);
656 Write the POC marker (progression order change)
657 @param j2k J2K handle
659 static void j2k_write_poc(opj_j2k_t *j2k);
662 * Writes the POC marker (Progression Order Change)
664 * @param p_stream the stream to write data to.
665 * @param p_j2k J2K codec.
666 * @param p_manager the user event manager.
668 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
669 struct opj_stream_private *p_stream,
670 struct opj_event_mgr * p_manager );
673 * Writes the POC marker (Progression Order Change)
675 * @param p_stream the stream to write data to.
676 * @param p_j2k J2K codec.
677 * @param p_manager the user event manager.
679 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
681 OPJ_UINT32 * p_data_written,
682 struct opj_event_mgr * p_manager );
685 * Gets the maximum size taken by the writting of a POC.
687 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
690 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
692 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
695 * Gets the maximum size taken by the headers of the SOT.
697 * @param p_j2k the jpeg2000 codec to use.
699 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
702 Read the POC marker (progression order change)
703 @param j2k J2K handle
705 static void j2k_read_poc(opj_j2k_t *j2k);
707 * Reads a POC marker (Progression Order Change)
709 * @param p_header_data the data contained in the POC box.
710 * @param p_j2k the jpeg2000 codec.
711 * @param p_header_size the size of the data contained in the POC marker.
712 * @param p_manager the user event manager.
714 static opj_bool j2k_read_poc_v2 (
716 OPJ_BYTE * p_header_data,
717 OPJ_UINT32 p_header_size,
718 struct opj_event_mgr * p_manager
722 Read the CRG marker (component registration)
723 @param j2k J2K handle
725 static void j2k_read_crg(opj_j2k_t *j2k);
727 * Reads a CRG marker (Component registration)
729 * @param p_header_data the data contained in the TLM box.
730 * @param p_j2k the jpeg2000 codec.
731 * @param p_header_size the size of the data contained in the TLM marker.
732 * @param p_manager the user event manager.
734 static opj_bool j2k_read_crg_v2 (
736 OPJ_BYTE * p_header_data,
737 OPJ_UINT32 p_header_size,
738 struct opj_event_mgr * p_manager
741 Read the TLM marker (tile-part lengths)
742 @param j2k J2K handle
744 static void j2k_read_tlm(opj_j2k_t *j2k);
746 * Reads a TLM marker (Tile Length Marker)
748 * @param p_header_data the data contained in the TLM box.
749 * @param p_j2k the jpeg2000 codec.
750 * @param p_header_size the size of the data contained in the TLM marker.
751 * @param p_manager the user event manager.
753 static opj_bool j2k_read_tlm_v2 (
755 OPJ_BYTE * p_header_data,
756 OPJ_UINT32 p_header_size,
757 struct opj_event_mgr * p_manager
761 * Writes the updated tlm.
763 * @param p_stream the stream to write data to.
764 * @param p_j2k J2K codec.
765 * @param p_manager the user event manager.
767 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
768 struct opj_stream_private *p_stream,
769 struct opj_event_mgr * p_manager );
772 Read the PLM marker (packet length, main header)
773 @param j2k J2K handle
775 static void j2k_read_plm(opj_j2k_t *j2k);
778 * Reads a PLM marker (Packet length, main header marker)
780 * @param p_header_data the data contained in the TLM box.
781 * @param p_j2k the jpeg2000 codec.
782 * @param p_header_size the size of the data contained in the TLM marker.
783 * @param p_manager the user event manager.
785 static opj_bool j2k_read_plm_v2 (
787 OPJ_BYTE * p_header_data,
788 OPJ_UINT32 p_header_size,
789 struct opj_event_mgr * p_manager
792 Read the PLT marker (packet length, tile-part header)
793 @param j2k J2K handle
795 static void j2k_read_plt(opj_j2k_t *j2k);
797 * Reads a PLT marker (Packet length, tile-part header)
799 * @param p_header_data the data contained in the PLT box.
800 * @param p_j2k the jpeg2000 codec.
801 * @param p_header_size the size of the data contained in the PLT marker.
802 * @param p_manager the user event manager.
804 static opj_bool j2k_read_plt_v2 (
806 OPJ_BYTE * p_header_data,
807 OPJ_UINT32 p_header_size,
808 struct opj_event_mgr * p_manager
811 Read the PPM marker (packet packet headers, main header)
812 @param j2k J2K handle
814 static void j2k_read_ppm(opj_j2k_t *j2k);
816 * Reads a PPM marker (Packed packet headers, main header)
818 * @param p_header_data the data contained in the POC box.
819 * @param p_j2k the jpeg2000 codec.
820 * @param p_header_size the size of the data contained in the POC marker.
821 * @param p_manager the user event manager.
824 static opj_bool j2k_read_ppm_v2 (
826 OPJ_BYTE * p_header_data,
827 OPJ_UINT32 p_header_size,
828 struct opj_event_mgr * p_manager
832 static opj_bool j2k_read_ppm_v3 (
834 OPJ_BYTE * p_header_data,
835 OPJ_UINT32 p_header_size,
836 struct opj_event_mgr * p_manager
840 Read the PPT marker (packet packet headers, tile-part header)
841 @param j2k J2K handle
843 static void j2k_read_ppt(opj_j2k_t *j2k);
845 * Reads a PPT marker (Packed packet headers, tile-part header)
847 * @param p_header_data the data contained in the PPT box.
848 * @param p_j2k the jpeg2000 codec.
849 * @param p_header_size the size of the data contained in the PPT marker.
850 * @param p_manager the user event manager.
852 static opj_bool j2k_read_ppt_v2 (
854 OPJ_BYTE * p_header_data,
855 OPJ_UINT32 p_header_size,
856 struct opj_event_mgr * p_manager
859 Write the TLM marker (Mainheader)
860 @param j2k J2K handle
862 static void j2k_write_tlm(opj_j2k_t *j2k);
865 * Writes the TLM marker (Tile Length Marker)
867 * @param p_stream the stream to write data to.
868 * @param p_j2k J2K codec.
869 * @param p_manager the user event manager.
871 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
872 struct opj_stream_private *p_stream,
873 struct opj_event_mgr * p_manager );
876 Write the SOT marker (start of tile-part)
877 @param j2k J2K handle
879 static void j2k_write_sot(opj_j2k_t *j2k);
882 * Writes the SOT marker (Start of tile-part)
884 * @param p_stream the stream to write data to.
885 * @param p_j2k J2K codec.
886 * @param p_manager the user event manager.
888 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
890 OPJ_UINT32 * p_data_written,
891 const struct opj_stream_private *p_stream,
892 struct opj_event_mgr * p_manager );
895 * Reads a PPT marker (Packed packet headers, tile-part header)
897 * @param p_header_data the data contained in the PPT box.
898 * @param p_j2k the jpeg2000 codec.
899 * @param p_header_size the size of the data contained in the PPT marker.
900 * @param p_manager the user event manager.
902 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
903 OPJ_BYTE * p_header_data,
904 OPJ_UINT32 p_header_size,
905 opj_event_mgr_t * p_manager );
908 Write the SOD marker (start of data)
909 @param j2k J2K handle
910 @param tile_coder Pointer to a TCD handle
912 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
915 * Writes the SOD marker (Start of data)
917 * @param p_stream the stream to write data to.
918 * @param p_j2k J2K codec.
919 * @param p_manager the user event manager.
921 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
922 struct opj_tcd_v2 * p_tile_coder,
924 OPJ_UINT32 * p_data_written,
925 OPJ_UINT32 p_total_data_size,
926 const struct opj_stream_private *p_stream,
927 struct opj_event_mgr * p_manager );
930 * Reads a SOD marker (Start Of Data)
932 * @param p_header_data the data contained in the SOD box.
933 * @param p_j2k the jpeg2000 codec.
934 * @param p_header_size the size of the data contained in the SOD marker.
935 * @param p_manager the user event manager.
937 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
938 opj_stream_private_t *p_stream,
939 opj_event_mgr_t * p_manager );
942 * Updates the Tile Length Marker.
944 void j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
946 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
947 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
949 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
950 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
954 Write the RGN marker (region-of-interest)
955 @param j2k J2K handle
956 @param compno Number of the component concerned by the information written
957 @param tileno Number of the tile concerned by the information written
959 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
962 * Writes the RGN marker (Region Of Interest)
964 * @param p_tile_no the tile to output
965 * @param p_comp_no the component to output
966 * @param p_stream the stream to write data to.
967 * @param p_j2k J2K codec.
968 * @param p_manager the user event manager.
970 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
971 OPJ_UINT32 p_tile_no,
972 OPJ_UINT32 p_comp_no,
973 struct opj_stream_private *p_stream,
974 struct opj_event_mgr * p_manager );
977 * Reads a RGN marker (Region Of Interest)
979 * @param p_header_data the data contained in the POC box.
980 * @param p_j2k the jpeg2000 codec.
981 * @param p_header_size the size of the data contained in the POC marker.
982 * @param p_manager the user event manager.
984 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
985 OPJ_BYTE * p_header_data,
986 OPJ_UINT32 p_header_size,
987 opj_event_mgr_t * p_manager );
990 * Writes the EOC marker (End of Codestream)
992 * @param p_stream the stream to write data to.
993 * @param p_j2k J2K codec.
994 * @param p_manager the user event manager.
996 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
997 struct opj_stream_private *p_stream,
998 struct opj_event_mgr * p_manager );
1001 Write the EOC marker (end of codestream)
1002 @param j2k J2K handle
1004 static void j2k_write_eoc(opj_j2k_t *j2k);
1006 Read the EOC marker (end of codestream)
1007 @param j2k J2K handle
1009 static void j2k_read_eoc(opj_j2k_t *j2k);
1012 * Reads a EOC marker (End Of Codestream)
1014 * @param p_header_data the data contained in the SOD box.
1015 * @param p_j2k the jpeg2000 codec.
1016 * @param p_header_size the size of the data contained in the SOD marker.
1017 * @param p_manager the user event manager.
1020 static opj_bool j2k_read_eoc_v2 (
1021 opj_j2k_v2_t *p_j2k,
1022 struct opj_stream_private *p_stream,
1023 struct opj_event_mgr * p_manager
1030 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
1032 * @param p_stream the stream to write data to.
1033 * @param p_j2k J2K codec.
1034 * @param p_manager the user event manager.
1036 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
1037 struct opj_stream_private *p_stream,
1038 struct opj_event_mgr * p_manager );
1043 * @param p_stream the stream to write data to.
1044 * @param p_j2k J2K codec.
1045 * @param p_manager the user event manager.
1047 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
1048 struct opj_stream_private *p_stream,
1049 struct opj_event_mgr * p_manager );
1052 Read an unknown marker
1053 @param j2k J2K handle
1055 static void j2k_read_unk(opj_j2k_t *j2k);
1057 Add main header marker information
1058 @param cstr_info Codestream information structure
1059 @param type marker type
1060 @param pos byte offset of marker segment
1061 @param len length of marker segment
1063 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1065 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
1067 Add tile header marker information
1068 @param tileno tile index number
1069 @param cstr_info Codestream information structure
1070 @param type marker type
1071 @param pos byte offset of marker segment
1072 @param len length of marker segment
1074 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1076 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);
1079 * Reads an unknown marker
1081 * @param p_stream the stream object to read from.
1082 * @param p_j2k the jpeg2000 codec.
1083 * @param p_manager the user event manager.
1085 * @return true if the marker could be deduced.
1087 static opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
1088 struct opj_stream_private *p_stream,
1089 OPJ_UINT32 *output_marker,
1090 struct opj_event_mgr * p_manager );
1093 * Writes the MCT marker (Multiple Component Transform)
1095 * @param p_stream the stream to write data to.
1096 * @param p_j2k J2K codec.
1097 * @param p_manager the user event manager.
1099 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1100 opj_mct_data_t * p_mct_record,
1101 struct opj_stream_private *p_stream,
1102 struct opj_event_mgr * p_manager );
1105 * Reads a MCT marker (Multiple Component Transform)
1107 * @param p_header_data the data contained in the MCT box.
1108 * @param p_j2k the jpeg2000 codec.
1109 * @param p_header_size the size of the data contained in the MCT marker.
1110 * @param p_manager the user event manager.
1112 static opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1113 OPJ_BYTE * p_header_data,
1114 OPJ_UINT32 p_header_size,
1115 struct opj_event_mgr * p_manager );
1118 * Writes the MCC marker (Multiple Component Collection)
1120 * @param p_stream the stream to write data to.
1121 * @param p_j2k J2K codec.
1122 * @param p_manager the user event manager.
1124 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1125 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1126 struct opj_stream_private *p_stream,
1127 struct opj_event_mgr * p_manager );
1131 * Reads a MCC marker (Multiple Component Collection)
1133 * @param p_header_data the data contained in the MCC box.
1134 * @param p_j2k the jpeg2000 codec.
1135 * @param p_header_size the size of the data contained in the MCC marker.
1136 * @param p_manager the user event manager.
1138 static opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1139 OPJ_BYTE * p_header_data,
1140 OPJ_UINT32 p_header_size,
1141 struct opj_event_mgr * p_manager );
1144 * Writes the MCO marker (Multiple component transformation ordering)
1146 * @param p_stream the stream to write data to.
1147 * @param p_j2k J2K codec.
1148 * @param p_manager the user event manager.
1150 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1151 struct opj_stream_private *p_stream,
1152 struct opj_event_mgr * p_manager );
1155 * Reads a MCO marker (Multiple Component Transform Ordering)
1157 * @param p_header_data the data contained in the MCO box.
1158 * @param p_j2k the jpeg2000 codec.
1159 * @param p_header_size the size of the data contained in the MCO marker.
1160 * @param p_manager the user event manager.
1162 static opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1163 OPJ_BYTE * p_header_data,
1164 OPJ_UINT32 p_header_size,
1165 struct opj_event_mgr * p_manager );
1167 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1169 static void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1170 static void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1171 static void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1172 static void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1174 static void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1175 static void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1176 static void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1177 static void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1179 static void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1180 static void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1181 static void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1182 static void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1186 * Ends the encoding, i.e. frees memory.
1188 * @param p_stream the stream to write data to.
1189 * @param p_j2k J2K codec.
1190 * @param p_manager the user event manager.
1192 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1193 struct opj_stream_private *p_stream,
1194 struct opj_event_mgr * p_manager );
1197 * Writes the CBD marker (Component bit depth definition)
1199 * @param p_stream the stream to write data to.
1200 * @param p_j2k J2K codec.
1201 * @param p_manager the user event manager.
1203 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1204 struct opj_stream_private *p_stream,
1205 struct opj_event_mgr * p_manager );
1208 * Reads a CBD marker (Component bit depth definition)
1209 * @param p_header_data the data contained in the CBD box.
1210 * @param p_j2k the jpeg2000 codec.
1211 * @param p_header_size the size of the data contained in the CBD marker.
1212 * @param p_manager the user event manager.
1214 static opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1215 OPJ_BYTE * p_header_data,
1216 OPJ_UINT32 p_header_size,
1217 struct opj_event_mgr * p_manager);
1220 * Writes the image components.
1222 * @param p_stream the stream to write data to.
1223 * @param p_j2k J2K codec.
1224 * @param p_manager the user event manager.
1226 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1227 struct opj_stream_private *p_stream,
1228 struct opj_event_mgr * p_manager );
1231 * Writes regions of interests.
1233 * @param p_stream the stream to write data to.
1234 * @param p_j2k J2K codec.
1235 * @param p_manager the user event manager.
1237 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1238 struct opj_stream_private *p_stream,
1239 struct opj_event_mgr * p_manager );
1244 * @param p_stream the stream to write data to.
1245 * @param p_j2k J2K codec.
1246 * @param p_manager the user event manager.
1248 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1249 struct opj_stream_private *p_stream,
1250 struct opj_event_mgr * p_manager );
1253 * Checks the progression order changes values. Tells of the poc given as input are valid.
1254 * A nice message is outputted at errors.
1256 * @param p_pocs the progression order changes.
1257 * @param p_nb_pocs the number of progression order changes.
1258 * @param p_nb_resolutions the number of resolutions.
1259 * @param numcomps the number of components
1260 * @param numlayers the number of layers.
1262 * @return true if the pocs are valid.
1264 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1265 OPJ_UINT32 p_nb_pocs,
1266 OPJ_UINT32 p_nb_resolutions,
1267 OPJ_UINT32 numcomps,
1268 OPJ_UINT32 numlayers,
1269 opj_event_mgr_t * p_manager);
1272 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1274 * @param cp the coding parameters.
1275 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1276 * @param tileno the given tile.
1278 * @return the number of tile parts.
1280 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1282 /** mem allocation for TLM marker*/
1283 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1286 * Calculates the total number of tile parts needed by the encoder to
1287 * encode such an image. If not enough memory is available, then the function return false.
1289 * @param p_nb_tiles pointer that will hold the number of tile parts.
1290 * @param cp the coding parameters for the image.
1291 * @param image the image to encode.
1292 * @param p_j2k the p_j2k encoder.
1293 * @param p_manager the user event manager.
1295 * @return true if the function was successful, false else.
1297 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1299 OPJ_UINT32 * p_nb_tiles,
1301 opj_event_mgr_t * p_manager);
1303 static void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1305 static void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1307 static opj_codestream_index_t* j2k_create_cstr_index(void);
1313 /* ----------------------------------------------------------------------- */
1314 typedef struct j2k_prog_order{
1315 OPJ_PROG_ORDER enum_prog;
1319 j2k_prog_order_t j2k_prog_order_list[] = {
1325 {(OPJ_PROG_ORDER)-1, ""}
1333 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1341 typedef void (* j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1343 const j2k_mct_function j2k_mct_read_functions_to_float [] =
1345 j2k_read_int16_to_float,
1346 j2k_read_int32_to_float,
1347 j2k_read_float32_to_float,
1348 j2k_read_float64_to_float
1351 const j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1353 j2k_read_int16_to_int32,
1354 j2k_read_int32_to_int32,
1355 j2k_read_float32_to_int32,
1356 j2k_read_float64_to_int32
1359 const j2k_mct_function j2k_mct_write_functions_from_float [] =
1361 j2k_write_float_to_int16,
1362 j2k_write_float_to_int32,
1363 j2k_write_float_to_float,
1364 j2k_write_float_to_float64
1367 typedef struct opj_dec_memory_marker_handler
1371 /** value of the state when the marker can appear */
1373 /** action linked to the marker */
1374 opj_bool (*handler) (
1375 opj_j2k_v2_t *p_j2k,
1376 OPJ_BYTE * p_header_data,
1377 OPJ_UINT32 p_header_size,
1378 struct opj_event_mgr * p_manager
1381 opj_dec_memory_marker_handler_t;
1383 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1386 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1387 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1388 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1389 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1390 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1391 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1392 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1393 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1394 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1395 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1396 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1397 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1398 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1400 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1401 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1402 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1403 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1404 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1405 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1407 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1408 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1409 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1410 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1411 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1412 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1413 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc_v2},
1414 {J2K_MS_SIZ, J2K_STATE_MHSIZ , opj_j2k_read_siz},
1415 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm_v2},
1416 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm_v2},
1417 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt_v2},
1418 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1419 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt_v2},
1421 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg_v2},
1422 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com_v2},
1423 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mct},
1424 {J2K_MS_CBD, J2K_STATE_MH , j2k_read_cbd},
1425 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mcc},
1426 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_mco},
1428 #ifdef TODO_MS /* FIXME */
1429 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1430 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1431 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1432 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1434 #endif /* USE_JPWL */
1436 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1437 {J2K_MS_INSEC, 0, j2k_read_insec}
1438 #endif /* USE_JPSEC */
1439 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1444 void j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1446 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1447 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1451 for (i=0;i<p_nb_elem;++i) {
1452 opj_read_bytes(l_src_data,&l_temp,2);
1454 l_src_data+=sizeof(OPJ_INT16);
1456 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1460 void j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1462 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1463 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1467 for (i=0;i<p_nb_elem;++i) {
1468 opj_read_bytes(l_src_data,&l_temp,4);
1470 l_src_data+=sizeof(OPJ_INT32);
1472 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1476 void j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1478 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1479 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1483 for (i=0;i<p_nb_elem;++i) {
1484 opj_read_float(l_src_data,&l_temp);
1486 l_src_data+=sizeof(OPJ_FLOAT32);
1488 *(l_dest_data++) = l_temp;
1492 void j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1494 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1495 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1499 for (i=0;i<p_nb_elem;++i) {
1500 opj_read_double(l_src_data,&l_temp);
1502 l_src_data+=sizeof(OPJ_FLOAT64);
1504 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1508 void j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1510 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1511 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1515 for (i=0;i<p_nb_elem;++i) {
1516 opj_read_bytes(l_src_data,&l_temp,2);
1518 l_src_data+=sizeof(OPJ_INT16);
1520 *(l_dest_data++) = (OPJ_INT32) l_temp;
1524 void j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1526 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1527 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1531 for (i=0;i<p_nb_elem;++i) {
1532 opj_read_bytes(l_src_data,&l_temp,4);
1534 l_src_data+=sizeof(OPJ_INT32);
1536 *(l_dest_data++) = (OPJ_INT32) l_temp;
1540 void j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1542 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1543 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1547 for (i=0;i<p_nb_elem;++i) {
1548 opj_read_float(l_src_data,&l_temp);
1550 l_src_data+=sizeof(OPJ_FLOAT32);
1552 *(l_dest_data++) = (OPJ_INT32) l_temp;
1556 void j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1558 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1559 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1563 for (i=0;i<p_nb_elem;++i) {
1564 opj_read_double(l_src_data,&l_temp);
1566 l_src_data+=sizeof(OPJ_FLOAT64);
1568 *(l_dest_data++) = (OPJ_INT32) l_temp;
1572 void j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1574 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1575 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1579 for (i=0;i<p_nb_elem;++i) {
1580 l_temp = (OPJ_UINT32) *(l_src_data++);
1582 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1584 l_dest_data+=sizeof(OPJ_INT16);
1588 void j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1590 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1591 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1595 for (i=0;i<p_nb_elem;++i) {
1596 l_temp = (OPJ_UINT32) *(l_src_data++);
1598 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1600 l_dest_data+=sizeof(OPJ_INT32);
1604 void j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1606 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1607 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1611 for (i=0;i<p_nb_elem;++i) {
1612 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1614 opj_write_float(l_dest_data,l_temp);
1616 l_dest_data+=sizeof(OPJ_FLOAT32);
1620 void j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1622 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1623 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1627 for (i=0;i<p_nb_elem;++i) {
1628 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1630 opj_write_double(l_dest_data,l_temp);
1632 l_dest_data+=sizeof(OPJ_FLOAT64);
1638 * Converts an enum type progression order to string type.
1640 * @param prg_order the progression order to get.
1642 * @return the string representation of the given progression order.
1644 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1645 j2k_prog_order_t *po;
1646 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1647 if(po->enum_prog == prg_order){
1648 return po->str_prog;
1651 return po->str_prog;
1655 * Checks the progression order changes values. Tells if the poc given as input are valid.
1657 * @param p_pocs the progression order changes.
1658 * @param p_nb_pocs the number of progression order changes.
1659 * @param p_nb_resolutions the number of resolutions.
1660 * @param numcomps the number of components
1661 * @param numlayers the number of layers.
1662 * @param p_manager the user event manager.
1664 * @return true if the pocs are valid.
1666 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1667 OPJ_UINT32 p_nb_pocs,
1668 OPJ_UINT32 p_nb_resolutions,
1669 OPJ_UINT32 p_num_comps,
1670 OPJ_UINT32 p_num_layers,
1671 opj_event_mgr_t * p_manager)
1673 OPJ_UINT32* packet_array;
1674 OPJ_UINT32 index , resno, compno, layno;
1676 OPJ_UINT32 step_c = 1;
1677 OPJ_UINT32 step_r = p_num_comps * step_c;
1678 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1679 opj_bool loss = OPJ_FALSE;
1680 OPJ_UINT32 layno0 = 0;
1682 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1683 if (packet_array == 00) {
1684 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1687 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1689 if (p_nb_pocs == 0) {
1693 index = step_r * p_pocs->resno0;
1694 // take each resolution for each poc
1695 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1697 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1699 // take each comp of each resolution for each poc
1700 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1701 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1703 // and finally take each layer of each res of ...
1704 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1705 //index = step_r * resno + step_c * compno + step_l * layno;
1706 packet_array[comp_index] = 1;
1707 comp_index += step_l;
1710 res_index += step_c;
1717 // iterate through all the pocs
1718 for (i = 1; i < p_nb_pocs ; ++i) {
1719 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1721 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1722 index = step_r * p_pocs->resno0;
1724 // take each resolution for each poc
1725 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1726 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1728 // take each comp of each resolution for each poc
1729 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1730 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1732 // and finally take each layer of each res of ...
1733 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1734 //index = step_r * resno + step_c * compno + step_l * layno;
1735 packet_array[comp_index] = 1;
1736 comp_index += step_l;
1739 res_index += step_c;
1749 for (layno = 0; layno < p_num_layers ; ++layno) {
1750 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1751 for (compno = 0; compno < p_num_comps; ++compno) {
1752 loss |= (packet_array[index]!=1);
1753 //index = step_r * resno + step_c * compno + step_l * layno;
1760 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1763 opj_free(packet_array);
1768 /* ----------------------------------------------------------------------- */
1769 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1772 int tpnum=1,tpend=0;
1773 opj_tcp_t *tcp = &cp->tcps[tileno];
1774 prog = j2k_convert_progression_order(tcp->prg);
1779 if( cp->tp_flag == prog[i] ){
1780 tpend=1;cp->tp_pos=i;
1784 tpnum= tpnum * tcp->pocs[pino].compE;
1787 tpnum= tpnum * tcp->pocs[pino].resE;
1790 tpnum= tpnum * tcp->pocs[pino].prcE;
1793 tpnum= tpnum * tcp->pocs[pino].layE;
1805 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1807 * @param cp the coding parameters.
1808 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1809 * @param tileno the given tile.
1811 * @return the number of tile parts.
1813 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1815 const OPJ_CHAR *prog = 00;
1817 OPJ_UINT32 tpnum = 1;
1818 opj_tcp_v2_t *tcp = 00;
1819 opj_poc_t * l_current_poc = 00;
1822 assert(tileno < (cp->tw * cp->th));
1823 assert(pino < (cp->tcps[tileno].numpocs + 1));
1825 /* get the given tile coding parameter */
1826 tcp = &cp->tcps[tileno];
1829 l_current_poc = &(tcp->pocs[pino]);
1830 assert(l_current_poc != 0);
1832 /* get the progression order as a character string */
1833 prog = j2k_convert_progression_order(tcp->prg);
1834 assert(strlen(prog) > 0);
1836 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1840 /* component wise */
1842 tpnum *= l_current_poc->compE;
1844 /* resolution wise */
1846 tpnum *= l_current_poc->resE;
1850 tpnum *= l_current_poc->prcE;
1854 tpnum *= l_current_poc->layE;
1857 /* whould we split here ? */
1858 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1859 cp->m_specific_param.m_enc.m_tp_pos=i;
1871 /** mem allocation for TLM marker*/
1872 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1873 int pino,tileno,totnum_tp=0;
1875 OPJ_ARG_NOT_USED(img_numcomp);
1877 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1878 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1879 int cur_totnum_tp = 0;
1880 opj_tcp_t *tcp = &cp->tcps[tileno];
1881 for(pino = 0; pino <= tcp->numpocs; pino++) {
1883 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1884 if(!pi) { return -1;}
1885 tp_num = j2k_get_num_tp(cp,pino,tileno);
1886 totnum_tp = totnum_tp + tp_num;
1887 cur_totnum_tp = cur_totnum_tp + tp_num;
1888 pi_destroy(pi, cp, tileno);
1890 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1892 if (j2k->cstr_info) {
1893 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1894 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1902 * Calculates the total number of tile parts needed by the encoder to
1903 * encode such an image. If not enough memory is available, then the function return false.
1905 * @param p_nb_tiles pointer that will hold the number of tile parts.
1906 * @param cp the coding parameters for the image.
1907 * @param image the image to encode.
1908 * @param p_j2k the p_j2k encoder.
1909 * @param p_manager the user event manager.
1911 * @return true if the function was successful, false else.
1913 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1915 OPJ_UINT32 * p_nb_tiles,
1917 opj_event_mgr_t * p_manager)
1919 OPJ_UINT32 pino,tileno;
1920 OPJ_UINT32 l_nb_tiles;
1924 assert(p_nb_tiles != 00);
1926 assert(image != 00);
1927 assert(p_j2k != 00);
1928 assert(p_manager != 00);
1930 l_nb_tiles = cp->tw * cp->th;
1935 /* TODO mergeV2: check this part which use cstr_info */
1936 /*if (p_j2k->cstr_info) {
1937 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1939 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1940 OPJ_UINT32 cur_totnum_tp = 0;
1942 pi_update_encoding_parameters(image,cp,tileno);
1944 for (pino = 0; pino <= tcp->numpocs; ++pino)
1946 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1948 *p_nb_tiles = *p_nb_tiles + tp_num;
1950 cur_totnum_tp += tp_num;
1953 tcp->m_nb_tile_parts = cur_totnum_tp;
1955 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1956 if (l_info_tile_ptr->tp == 00) {
1960 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1962 l_info_tile_ptr->num_tps = cur_totnum_tp;
1969 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1970 OPJ_UINT32 cur_totnum_tp = 0;
1972 pi_update_encoding_parameters(image,cp,tileno);
1974 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1975 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1977 *p_nb_tiles = *p_nb_tiles + tp_num;
1979 cur_totnum_tp += tp_num;
1981 tcp->m_nb_tile_parts = cur_totnum_tp;
1990 static void j2k_write_soc(opj_j2k_t *j2k) {
1991 opj_cio_t *cio = j2k->cio;
1992 cio_write(cio, J2K_MS_SOC, 2);
1995 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
2000 /* update markers struct */
2001 j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
2002 #endif /* USE_JPWL */
2007 * Writes the SOC marker (Start Of Codestream)
2009 * @param p_stream the stream to write data to.
2010 * @param p_j2k J2K codec.
2011 * @param p_manager the user event manager.
2013 opj_bool j2k_write_soc_v2( opj_j2k_v2_t *p_j2k,
2014 struct opj_stream_private *p_stream,
2015 struct opj_event_mgr * p_manager )
2017 /* 2 bytes will be written */
2018 OPJ_BYTE * l_start_stream = 00;
2021 assert(p_stream != 00);
2022 assert(p_j2k != 00);
2023 assert(p_manager != 00);
2025 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2027 /* write SOC identifier */
2028 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
2030 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
2036 /* update markers struct */
2038 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
2040 assert( 0 && "TODO" );
2041 #endif /* USE_JPWL */
2050 * Reads a SOC marker (Start of Codestream)
2051 * @param p_header_data the data contained in the SOC box.
2052 * @param jp2 the jpeg2000 file codec.
2053 * @param p_header_size the size of the data contained in the SOC marker.
2054 * @param p_manager the user event manager.
2056 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
2057 struct opj_stream_private *p_stream,
2058 struct opj_event_mgr * p_manager )
2060 OPJ_BYTE l_data [2];
2061 OPJ_UINT32 l_marker;
2064 assert(p_j2k != 00);
2065 assert(p_manager != 00);
2066 assert(p_stream != 00);
2068 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
2072 opj_read_bytes(l_data,&l_marker,2);
2073 if (l_marker != J2K_MS_SOC) {
2077 /* Next marker should be a SIZ marker in the main header */
2078 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
2080 /* FIXME move it in a index structure included in p_j2k*/
2081 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2083 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2085 /* Add the marker to the codestream index*/
2086 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2091 static void j2k_write_siz(opj_j2k_t *j2k) {
2095 opj_cio_t *cio = j2k->cio;
2096 opj_image_t *image = j2k->image;
2097 opj_cp_t *cp = j2k->cp;
2099 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2100 lenp = cio_tell(cio);
2102 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2103 cio_write(cio, image->x1, 4); /* Xsiz */
2104 cio_write(cio, image->y1, 4); /* Ysiz */
2105 cio_write(cio, image->x0, 4); /* X0siz */
2106 cio_write(cio, image->y0, 4); /* Y0siz */
2107 cio_write(cio, cp->tdx, 4); /* XTsiz */
2108 cio_write(cio, cp->tdy, 4); /* YTsiz */
2109 cio_write(cio, cp->tx0, 4); /* XT0siz */
2110 cio_write(cio, cp->ty0, 4); /* YT0siz */
2111 cio_write(cio, image->numcomps, 2); /* Csiz */
2112 for (i = 0; i < image->numcomps; i++) {
2113 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2114 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2115 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2117 len = cio_tell(cio) - lenp;
2118 cio_seek(cio, lenp);
2119 cio_write(cio, len, 2); /* Lsiz */
2120 cio_seek(cio, lenp + len);
2123 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2127 * Writes the SIZ marker (image and tile size)
2129 * @param p_stream the stream to write data to.
2130 * @param p_j2k J2K codec.
2131 * @param p_manager the user event manager.
2133 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2134 struct opj_stream_private *p_stream,
2135 struct opj_event_mgr * p_manager )
2138 OPJ_UINT32 l_size_len;
2139 OPJ_BYTE * l_current_ptr;
2140 opj_image_t * l_image = 00;
2141 opj_cp_v2_t *cp = 00;
2142 opj_image_comp_t * l_img_comp = 00;
2145 assert(p_stream != 00);
2146 assert(p_j2k != 00);
2147 assert(p_manager != 00);
2149 l_image = p_j2k->m_private_image;
2150 cp = &(p_j2k->m_cp);
2151 l_size_len = 40 + 3 * l_image->numcomps;
2152 l_img_comp = l_image->comps;
2154 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2156 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2157 = (OPJ_BYTE*)opj_realloc(
2158 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2160 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2164 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2167 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2169 /* write SOC identifier */
2170 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2173 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2176 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2179 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2182 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2185 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2188 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2191 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2194 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2197 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2200 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2203 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2206 for (i = 0; i < l_image->numcomps; ++i) {
2207 /* TODO here with MCT ? */
2208 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2211 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2214 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2220 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) {
2230 * Reads a SIZ marker (image and tile size)
2231 * @param p_header_data the data contained in the SIZ box.
2232 * @param jp2 the jpeg2000 file codec.
2233 * @param p_header_size the size of the data contained in the SIZ marker.
2234 * @param p_manager the user event manager.
2236 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2237 OPJ_BYTE * p_header_data,
2238 OPJ_UINT32 p_header_size,
2239 opj_event_mgr_t * p_manager
2242 OPJ_UINT32 l_size, i;
2243 OPJ_UINT32 l_nb_comp;
2244 OPJ_UINT32 l_nb_comp_remain;
2245 OPJ_UINT32 l_remaining_size;
2246 OPJ_UINT32 l_nb_tiles;
2248 opj_image_t *l_image = 00;
2249 opj_cp_v2_t *l_cp = 00;
2250 opj_image_comp_t * l_img_comp = 00;
2251 opj_tcp_v2_t * l_current_tile_param = 00;
2254 assert(p_j2k != 00);
2255 assert(p_manager != 00);
2256 assert(p_header_data != 00);
2258 l_image = p_j2k->m_private_image;
2259 l_cp = &(p_j2k->m_cp);
2261 /* minimum size == 39 - 3 (= minimum component parameter) */
2262 if (p_header_size < 36) {
2263 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2267 l_remaining_size = p_header_size - 36;
2268 l_nb_comp = l_remaining_size / 3;
2269 l_nb_comp_remain = l_remaining_size % 3;
2270 if (l_nb_comp_remain != 0){
2271 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2275 l_size = p_header_size + 2; /* Lsiz */
2277 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2279 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2280 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2282 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2284 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2286 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2288 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2290 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2292 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2294 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2296 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2299 l_image->numcomps = (OPJ_UINT16) l_tmp;
2301 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2305 if (l_image->numcomps != l_nb_comp) {
2306 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);
2311 if (l_cp->correct) {
2312 /* if JPWL is on, we check whether TX errors have damaged
2313 too much the SIZ parameters */
2314 if (!(l_image->x1 * l_image->y1)) {
2315 opj_event_msg_v2(p_manager, EVT_ERROR,
2316 "JPWL: bad image size (%d x %d)\n",
2317 l_image->x1, l_image->y1);
2318 if (!JPWL_ASSUME || JPWL_ASSUME) {
2319 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2324 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2325 if (l_image->numcomps != ((len - 38) / 3)) {
2326 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2327 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2328 l_image->numcomps, ((len - 38) / 3));
2330 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2333 */ /* we try to correct */
2334 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2335 if (l_image->numcomps < ((len - 38) / 3)) {
2336 len = 38 + 3 * l_image->numcomps;
2337 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2340 l_image->numcomps = ((len - 38) / 3);
2341 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2347 /* update components number in the jpwl_exp_comps filed */
2348 l_cp->exp_comps = l_image->numcomps;
2350 #endif /* USE_JPWL */
2352 /* Allocate the resulting image components */
2353 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2354 if (l_image->comps == 00){
2355 l_image->numcomps = 0;
2356 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2360 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2361 l_img_comp = l_image->comps;
2363 /* Read the component information */
2364 for (i = 0; i < l_image->numcomps; ++i){
2366 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2368 l_img_comp->prec = (tmp & 0x7f) + 1;
2369 l_img_comp->sgnd = tmp >> 7;
2370 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2372 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2373 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2375 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2378 if (l_cp->correct) {
2379 /* if JPWL is on, we check whether TX errors have damaged
2380 too much the SIZ parameters, again */
2381 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2382 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2383 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2384 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2386 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2389 /* we try to correct */
2390 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2391 if (!l_image->comps[i].dx) {
2392 l_image->comps[i].dx = 1;
2393 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2394 i, l_image->comps[i].dx);
2396 if (!l_image->comps[i].dy) {
2397 l_image->comps[i].dy = 1;
2398 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2399 i, l_image->comps[i].dy);
2403 #endif /* USE_JPWL */
2404 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2405 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2409 /* Compute the number of tiles */
2410 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2411 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2412 l_nb_tiles = l_cp->tw * l_cp->th;
2414 /* Define the tiles which will be decoded */
2415 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2416 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;
2417 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;
2418 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);
2419 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);
2422 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2423 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2424 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2425 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2429 if (l_cp->correct) {
2430 /* if JPWL is on, we check whether TX errors have damaged
2431 too much the SIZ parameters */
2432 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2433 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2434 "JPWL: bad number of tiles (%d x %d)\n",
2435 l_cp->tw, l_cp->th);
2437 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2440 /* we try to correct */
2441 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2444 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2447 if (l_cp->tw > l_cp->max_tiles) {
2449 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2450 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2451 l_cp->max_tiles, l_cp->tw);
2455 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2458 if (l_cp->th > l_cp->max_tiles) {
2460 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2461 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2462 l_cp->max_tiles, l_cp->th);
2466 #endif /* USE_JPWL */
2468 /* memory allocations */
2469 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2470 if (l_cp->tcps == 00) {
2471 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2474 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2477 if (l_cp->correct) {
2479 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2480 "JPWL: could not alloc tcps field of cp\n");
2481 if (!JPWL_ASSUME || JPWL_ASSUME) {
2482 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2487 #endif /* USE_JPWL */
2489 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2490 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2491 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2492 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2495 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2497 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2498 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2500 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2501 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2504 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));
2505 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2507 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2508 (opj_simple_mcc_decorrelation_data_t*)
2509 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2511 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2512 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2515 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));
2516 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2518 /* set up default dc level shift */
2519 for (i=0;i<l_image->numcomps;++i) {
2520 if (! l_image->comps[i].sgnd) {
2521 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2525 l_current_tile_param = l_cp->tcps;
2526 for (i = 0; i < l_nb_tiles; ++i) {
2527 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2528 if (l_current_tile_param->tccps == 00) {
2529 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2532 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2534 ++l_current_tile_param;
2537 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2538 opj_image_comp_header_update(l_image,l_cp);
2545 static void j2k_write_com(opj_j2k_t *j2k) {
2549 if(j2k->cp->comment) {
2550 opj_cio_t *cio = j2k->cio;
2551 char *comment = j2k->cp->comment;
2553 cio_write(cio, J2K_MS_COM, 2);
2554 lenp = cio_tell(cio);
2556 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2557 for (i = 0; i < strlen(comment); i++) {
2558 cio_write(cio, comment[i], 1);
2560 len = cio_tell(cio) - lenp;
2561 cio_seek(cio, lenp);
2562 cio_write(cio, len, 2);
2563 cio_seek(cio, lenp + len);
2567 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2573 * Writes the COM marker (comment)
2575 * @param p_stream the stream to write data to.
2576 * @param p_j2k J2K codec.
2577 * @param p_manager the user event manager.
2579 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2580 struct opj_stream_private *p_stream,
2581 struct opj_event_mgr * p_manager )
2583 OPJ_UINT32 l_comment_size;
2584 OPJ_UINT32 l_total_com_size;
2585 const OPJ_CHAR *l_comment;
2586 OPJ_BYTE * l_current_ptr = 00;
2589 assert(p_j2k != 00);
2590 assert(p_stream != 00);
2591 assert(p_manager != 00);
2593 l_comment = p_j2k->m_cp.comment;
2594 l_comment_size = strlen(l_comment);
2595 l_total_com_size = l_comment_size + 6;
2597 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2598 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2599 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2602 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2606 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2609 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2611 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2614 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2617 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2620 memcpy( l_current_ptr,l_comment,l_comment_size);
2622 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) {
2629 static void j2k_read_com(opj_j2k_t *j2k) {
2632 opj_cio_t *cio = j2k->cio;
2634 len = cio_read(cio, 2);
2635 cio_skip(cio, len - 2);
2638 * Reads a COM marker (comments)
2639 * @param p_header_data the data contained in the COM box.
2640 * @param jp2 the jpeg2000 file codec.
2641 * @param p_header_size the size of the data contained in the COM marker.
2642 * @param p_manager the user event manager.
2644 opj_bool j2k_read_com_v2 (
2645 opj_j2k_v2_t *p_j2k,
2646 OPJ_BYTE * p_header_data,
2647 OPJ_UINT32 p_header_size,
2648 struct opj_event_mgr * p_manager
2652 assert(p_j2k != 00);
2653 assert(p_manager != 00);
2654 assert(p_header_data != 00);
2655 (void)p_header_size;
2660 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2663 opj_cp_t *cp = j2k->cp;
2664 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2665 opj_tccp_t *tccp = &tcp->tccps[compno];
2666 opj_cio_t *cio = j2k->cio;
2668 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2669 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2670 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2671 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2672 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2674 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2675 for (i = 0; i < tccp->numresolutions; i++) {
2676 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2681 static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
2684 opj_cp_t *cp = j2k->cp;
2685 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
2686 opj_tccp_t *tccp = &tcp->tccps[compno];
2687 opj_cio_t *cio = j2k->cio;
2689 tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
2691 /* If user wants to remove more resolutions than the codestream contains, return error*/
2692 assert(cp->reduce >= 0);
2693 if ((OPJ_UINT32)cp->reduce >= tccp->numresolutions) {
2694 opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
2695 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
2696 j2k->state |= J2K_STATE_ERR;
2699 tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
2700 tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
2701 tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
2702 tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
2703 if (tccp->csty & J2K_CP_CSTY_PRT) {
2704 for (i = 0; i < tccp->numresolutions; i++) {
2705 int tmp = cio_read(cio, 1); /* SPcox (I_i) */
2706 tccp->prcw[i] = tmp & 0xf;
2707 tccp->prch[i] = tmp >> 4;
2712 if(j2k->cstr_info && compno == 0) {
2713 for (i = 0; i < tccp->numresolutions; i++) {
2714 if (tccp->csty & J2K_CP_CSTY_PRT) {
2715 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
2716 j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
2719 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2720 j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
2727 static void j2k_write_cod(opj_j2k_t *j2k) {
2728 opj_cp_t *cp = NULL;
2729 opj_tcp_t *tcp = NULL;
2732 opj_cio_t *cio = j2k->cio;
2734 cio_write(cio, J2K_MS_COD, 2); /* COD */
2736 lenp = cio_tell(cio);
2740 tcp = &cp->tcps[j2k->curtileno];
2742 cio_write(cio, tcp->csty, 1); /* Scod */
2743 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2744 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2745 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2747 j2k_write_cox(j2k, 0);
2748 len = cio_tell(cio) - lenp;
2749 cio_seek(cio, lenp);
2750 cio_write(cio, len, 2); /* Lcod */
2751 cio_seek(cio, lenp + len);
2754 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2759 * Writes the COD marker (Coding style default)
2761 * @param p_stream the stream to write data to.
2762 * @param p_j2k J2K codec.
2763 * @param p_manager the user event manager.
2765 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2766 struct opj_stream_private *p_stream,
2767 struct opj_event_mgr * p_manager )
2769 opj_cp_v2_t *l_cp = 00;
2770 opj_tcp_v2_t *l_tcp = 00;
2771 OPJ_UINT32 l_code_size,l_remaining_size;
2772 OPJ_BYTE * l_current_data = 00;
2775 assert(p_j2k != 00);
2776 assert(p_manager != 00);
2777 assert(p_stream != 00);
2779 l_cp = &(p_j2k->m_cp);
2780 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2781 l_code_size = 9 + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2782 l_remaining_size = l_code_size;
2784 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2785 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2786 = (OPJ_BYTE*)opj_realloc(
2787 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2790 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2794 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2797 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2799 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2800 l_current_data += 2;
2802 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2803 l_current_data += 2;
2805 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2808 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2811 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2814 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2817 l_remaining_size -= 9;
2819 if (! j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2820 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2824 if (l_remaining_size != 0) {
2825 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2829 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) {
2838 * Reads a COD marker (Coding Styke defaults)
2839 * @param p_header_data the data contained in the COD box.
2840 * @param p_j2k the jpeg2000 codec.
2841 * @param p_header_size the size of the data contained in the COD marker.
2842 * @param p_manager the user event manager.
2844 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2845 OPJ_BYTE * p_header_data,
2846 OPJ_UINT32 p_header_size,
2847 opj_event_mgr_t * p_manager
2853 opj_cp_v2_t *l_cp = 00;
2854 opj_tcp_v2_t *l_tcp = 00;
2855 opj_image_t *l_image = 00;
2858 assert(p_header_data != 00);
2859 assert(p_j2k != 00);
2860 assert(p_manager != 00);
2862 l_image = p_j2k->m_private_image;
2863 l_cp = &(p_j2k->m_cp);
2865 /* If we are in the first tile-part header of the current tile */
2866 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2867 &l_cp->tcps[p_j2k->m_current_tile_number] :
2868 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2870 /* Make sure room is sufficient */
2871 if (p_header_size < 5) {
2872 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2876 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2878 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2880 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2881 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2884 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2885 if (l_cp->m_specific_param.m_dec.m_layer) {
2886 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2889 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2892 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2896 for (i = 0; i < l_image->numcomps; ++i) {
2897 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2900 if (! j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2901 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2905 if (p_header_size != 0) {
2906 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2910 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2911 j2k_copy_tile_component_parameters(p_j2k);
2914 #ifdef WIP_REMOVE_MSD
2915 if (p_j2k->cstr_info) {
2916 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2917 p_j2k->cstr_info->prog = l_tcp->prg;
2918 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2919 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2920 for (i = 0; i < l_image->numcomps; ++i) {
2921 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2929 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2932 opj_cp_t *cp = j2k->cp;
2933 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2934 opj_image_t *image = j2k->image;
2935 opj_cio_t *cio = j2k->cio;
2937 cio_write(cio, J2K_MS_COC, 2); /* COC */
2938 lenp = cio_tell(cio);
2940 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2941 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2942 j2k_write_cox(j2k, compno);
2943 len = cio_tell(cio) - lenp;
2944 cio_seek(cio, lenp);
2945 cio_write(cio, len, 2); /* Lcoc */
2946 cio_seek(cio, lenp + len);
2950 * Writes the COC marker (Coding style component)
2952 * @param p_comp_no the index of the component to output.
2953 * @param p_stream the stream to write data to.
2954 * @param p_j2k J2K codec.
2955 * @param p_manager the user event manager.
2957 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2958 OPJ_UINT32 p_comp_no,
2959 struct opj_stream_private *p_stream,
2960 struct opj_event_mgr * p_manager )
2962 OPJ_UINT32 l_coc_size,l_remaining_size;
2963 OPJ_UINT32 l_comp_room;
2966 assert(p_j2k != 00);
2967 assert(p_manager != 00);
2968 assert(p_stream != 00);
2970 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2972 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2974 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2975 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2976 = (OPJ_BYTE*)opj_realloc(
2977 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2979 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2983 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2986 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);
2988 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) {
2996 * Writes the COC marker (Coding style component)
2998 * @param p_comp_no the index of the component to output.
2999 * @param p_stream the stream to write data to.
3000 * @param p_j2k J2K codec.
3001 * @param p_manager the user event manager.
3003 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
3004 OPJ_UINT32 p_comp_no,
3006 OPJ_UINT32 * p_data_written,
3007 struct opj_event_mgr * p_manager )
3009 opj_cp_v2_t *l_cp = 00;
3010 opj_tcp_v2_t *l_tcp = 00;
3011 OPJ_UINT32 l_coc_size,l_remaining_size;
3012 OPJ_BYTE * l_current_data = 00;
3013 opj_image_t *l_image = 00;
3014 OPJ_UINT32 l_comp_room;
3017 assert(p_j2k != 00);
3018 assert(p_manager != 00);
3020 l_cp = &(p_j2k->m_cp);
3021 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3022 l_image = p_j2k->m_private_image;
3023 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
3025 l_coc_size = 5 + l_comp_room + j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3026 l_remaining_size = l_coc_size;
3028 l_current_data = p_data;
3030 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
3031 l_current_data += 2;
3033 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
3034 l_current_data += 2;
3036 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
3037 l_current_data+=l_comp_room;
3039 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
3042 l_remaining_size -= (5 + l_comp_room);
3043 j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
3044 * p_data_written = l_coc_size;
3048 * Gets the maximum size taken by a coc.
3050 * @param p_j2k the jpeg2000 codec to use.
3052 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
3055 OPJ_UINT32 l_nb_comp;
3056 OPJ_UINT32 l_nb_tiles;
3057 OPJ_UINT32 l_max = 0;
3061 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3062 l_nb_comp = p_j2k->m_private_image->numcomps;
3064 for (i=0;i<l_nb_tiles;++i) {
3065 for (j=0;j<l_nb_comp;++j) {
3066 l_max = uint_max(l_max,j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
3075 * Reads a COC marker (Coding Style Component)
3076 * @param p_header_data the data contained in the COC box.
3077 * @param p_j2k the jpeg2000 codec.
3078 * @param p_header_size the size of the data contained in the COC marker.
3079 * @param p_manager the user event manager.
3081 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
3082 OPJ_BYTE * p_header_data,
3083 OPJ_UINT32 p_header_size,
3084 opj_event_mgr_t * p_manager
3087 opj_cp_v2_t *l_cp = NULL;
3088 opj_tcp_v2_t *l_tcp = NULL;
3089 opj_image_t *l_image = NULL;
3090 OPJ_UINT32 l_comp_room;
3091 OPJ_UINT32 l_comp_no;
3094 assert(p_header_data != 00);
3095 assert(p_j2k != 00);
3096 assert(p_manager != 00);
3098 l_cp = &(p_j2k->m_cp);
3099 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
3100 &l_cp->tcps[p_j2k->m_current_tile_number] :
3101 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3102 l_image = p_j2k->m_private_image;
3104 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
3106 /* make sure room is sufficient*/
3107 if (p_header_size < l_comp_room + 1) {
3108 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3111 p_header_size -= l_comp_room + 1;
3113 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
3114 p_header_data += l_comp_room;
3115 if (l_comp_no >= l_image->numcomps) {
3116 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
3120 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
3123 if (! j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3124 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3128 if (p_header_size != 0) {
3129 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
3135 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
3136 int bandno, numbands;
3139 opj_cp_t *cp = j2k->cp;
3140 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3141 opj_tccp_t *tccp = &tcp->tccps[compno];
3142 opj_cio_t *cio = j2k->cio;
3144 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3145 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3147 for (bandno = 0; bandno < numbands; bandno++) {
3148 expn = tccp->stepsizes[bandno].expn;
3149 mant = tccp->stepsizes[bandno].mant;
3151 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3152 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3154 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3160 static void j2k_write_qcd(opj_j2k_t *j2k) {
3163 opj_cio_t *cio = j2k->cio;
3165 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3166 lenp = cio_tell(cio);
3168 j2k_write_qcx(j2k, 0);
3169 len = cio_tell(cio) - lenp;
3170 cio_seek(cio, lenp);
3171 cio_write(cio, len, 2); /* Lqcd */
3172 cio_seek(cio, lenp + len);
3175 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3179 * Writes the QCD marker (quantization default)
3181 * @param p_comp_number the index of the component to output.
3182 * @param p_stream the stream to write data to.
3183 * @param p_j2k J2K codec.
3184 * @param p_manager the user event manager.
3186 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3187 struct opj_stream_private *p_stream,
3188 struct opj_event_mgr * p_manager )
3190 opj_cp_v2_t *l_cp = 00;
3191 opj_tcp_v2_t *l_tcp = 00;
3192 OPJ_UINT32 l_qcd_size,l_remaining_size;
3193 OPJ_BYTE * l_current_data = 00;
3196 assert(p_j2k != 00);
3197 assert(p_manager != 00);
3198 assert(p_stream != 00);
3200 l_cp = &(p_j2k->m_cp);
3201 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3202 l_qcd_size = 4 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3203 l_remaining_size = l_qcd_size;
3205 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3206 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3207 = (OPJ_BYTE*)opj_realloc(
3208 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3211 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3215 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3218 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3220 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3221 l_current_data += 2;
3223 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3224 l_current_data += 2;
3226 l_remaining_size -= 4;
3228 if (! j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3229 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3233 if (l_remaining_size != 0) {
3234 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3238 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) {
3246 * Reads a QCD marker (Quantization defaults)
3247 * @param p_header_data the data contained in the QCD box.
3248 * @param p_j2k the jpeg2000 codec.
3249 * @param p_header_size the size of the data contained in the QCD marker.
3250 * @param p_manager the user event manager.
3252 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
3253 OPJ_BYTE * p_header_data,
3254 OPJ_UINT32 p_header_size,
3255 opj_event_mgr_t * p_manager
3259 assert(p_header_data != 00);
3260 assert(p_j2k != 00);
3261 assert(p_manager != 00);
3263 if (! j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3264 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3268 if (p_header_size != 0) {
3269 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3273 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3274 j2k_copy_tile_quantization_parameters(p_j2k);
3279 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3282 opj_cio_t *cio = j2k->cio;
3284 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3285 lenp = cio_tell(cio);
3287 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3288 j2k_write_qcx(j2k, compno);
3289 len = cio_tell(cio) - lenp;
3290 cio_seek(cio, lenp);
3291 cio_write(cio, len, 2); /* Lqcc */
3292 cio_seek(cio, lenp + len);
3296 * Writes the QCC marker (quantization component)
3298 * @param p_comp_no the index of the component to output.
3299 * @param p_stream the stream to write data to.
3300 * @param p_j2k J2K codec.
3301 * @param p_manager the user event manager.
3303 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3304 OPJ_UINT32 p_comp_no,
3305 struct opj_stream_private *p_stream,
3306 struct opj_event_mgr * p_manager )
3308 OPJ_UINT32 l_qcc_size,l_remaining_size;
3311 assert(p_j2k != 00);
3312 assert(p_manager != 00);
3313 assert(p_stream != 00);
3315 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3316 l_remaining_size = l_qcc_size;
3318 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3319 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3320 = (OPJ_BYTE*)opj_realloc(
3321 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3323 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3327 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3330 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);
3332 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) {
3340 * Writes the QCC marker (quantization component)
3342 * @param p_comp_no the index of the component to output.
3343 * @param p_stream the stream to write data to.
3344 * @param p_j2k J2K codec.
3345 * @param p_manager the user event manager.
3347 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3348 OPJ_UINT32 p_comp_no,
3350 OPJ_UINT32 * p_data_written,
3351 struct opj_event_mgr * p_manager )
3353 OPJ_UINT32 l_qcc_size,l_remaining_size;
3354 OPJ_BYTE * l_current_data = 00;
3357 assert(p_j2k != 00);
3358 assert(p_manager != 00);
3360 l_qcc_size = 6 + j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3361 l_remaining_size = l_qcc_size;
3363 l_current_data = p_data;
3365 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3366 l_current_data += 2;
3368 if (p_j2k->m_private_image->numcomps <= 256) {
3371 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3372 l_current_data += 2;
3374 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3377 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3378 l_remaining_size -= 6;
3381 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3382 l_current_data += 2;
3384 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3387 l_remaining_size -= 6;
3390 j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3392 *p_data_written = l_qcc_size;
3396 * Gets the maximum size taken by a qcc.
3398 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3400 return j2k_get_max_coc_size(p_j2k);
3404 * Reads a QCC marker (Quantization component)
3405 * @param p_header_data the data contained in the QCC box.
3406 * @param p_j2k the jpeg2000 codec.
3407 * @param p_header_size the size of the data contained in the QCC marker.
3408 * @param p_manager the user event manager.
3410 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3411 OPJ_BYTE * p_header_data,
3412 OPJ_UINT32 p_header_size,
3413 opj_event_mgr_t * p_manager
3416 OPJ_UINT32 l_num_comp,l_comp_no;
3419 assert(p_header_data != 00);
3420 assert(p_j2k != 00);
3421 assert(p_manager != 00);
3423 l_num_comp = p_j2k->m_private_image->numcomps;
3425 if (l_num_comp <= 256) {
3426 if (p_header_size < 1) {
3427 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3430 opj_read_bytes(p_header_data,&l_comp_no,1);
3435 if (p_header_size < 2) {
3436 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3439 opj_read_bytes(p_header_data,&l_comp_no,2);
3445 if (p_j2k->m_cp.correct) {
3447 static OPJ_UINT32 backup_compno = 0;
3449 /* compno is negative or larger than the number of components!!! */
3450 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3451 opj_event_msg_v2(p_manager, EVT_ERROR,
3452 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3453 l_comp_no, l_num_comp);
3455 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3458 /* we try to correct */
3459 l_comp_no = backup_compno % l_num_comp;
3460 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3461 "- setting component number to %d\n",
3465 /* keep your private count of tiles */
3468 #endif /* USE_JPWL */
3470 if (! j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3471 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3475 if (p_header_size != 0) {
3476 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3484 static void j2k_write_poc(opj_j2k_t *j2k) {
3485 int len, numpchgs, i;
3487 int numcomps = j2k->image->numcomps;
3489 opj_cp_t *cp = j2k->cp;
3490 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3491 opj_tccp_t *tccp = &tcp->tccps[0];
3492 opj_cio_t *cio = j2k->cio;
3494 numpchgs = 1 + tcp->numpocs;
3495 cio_write(cio, J2K_MS_POC, 2); /* POC */
3496 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3497 cio_write(cio, len, 2); /* Lpoc */
3498 for (i = 0; i < numpchgs; i++) {
3499 opj_poc_t *poc = &tcp->pocs[i];
3500 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3501 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3502 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3503 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3504 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3505 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3506 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3507 poc->compno1 = int_min(poc->compno1, numcomps);
3508 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3513 * Writes the POC marker (Progression Order Change)
3515 * @param p_stream the stream to write data to.
3516 * @param p_j2k J2K codec.
3517 * @param p_manager the user event manager.
3519 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3520 struct opj_stream_private *p_stream,
3521 struct opj_event_mgr * p_manager )
3523 OPJ_UINT32 l_nb_comp;
3524 OPJ_UINT32 l_nb_poc;
3525 OPJ_UINT32 l_poc_size;
3526 OPJ_UINT32 l_written_size = 0;
3527 opj_tcp_v2_t *l_tcp = 00;
3528 opj_tccp_t *l_tccp = 00;
3529 OPJ_UINT32 l_poc_room;
3532 assert(p_j2k != 00);
3533 assert(p_manager != 00);
3534 assert(p_stream != 00);
3536 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3537 l_tccp = &l_tcp->tccps[0];
3538 l_nb_comp = p_j2k->m_private_image->numcomps;
3539 l_nb_poc = 1 + l_tcp->numpocs;
3541 if (l_nb_comp <= 256) {
3547 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3549 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3550 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3551 = (OPJ_BYTE*)opj_realloc(
3552 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3555 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3559 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3562 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3564 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) {
3573 * Writes the POC marker (Progression Order Change)
3575 * @param p_stream the stream to write data to.
3576 * @param p_j2k J2K codec.
3577 * @param p_manager the user event manager.
3579 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3581 OPJ_UINT32 * p_data_written,
3582 struct opj_event_mgr * p_manager )
3585 OPJ_BYTE * l_current_data = 00;
3586 OPJ_UINT32 l_nb_comp;
3587 OPJ_UINT32 l_nb_poc;
3588 OPJ_UINT32 l_poc_size;
3589 opj_image_t *l_image = 00;
3590 opj_tcp_v2_t *l_tcp = 00;
3591 opj_tccp_t *l_tccp = 00;
3592 opj_poc_t *l_current_poc = 00;
3593 OPJ_UINT32 l_poc_room;
3596 assert(p_j2k != 00);
3597 assert(p_manager != 00);
3599 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3600 l_tccp = &l_tcp->tccps[0];
3601 l_image = p_j2k->m_private_image;
3602 l_nb_comp = l_image->numcomps;
3603 l_nb_poc = 1 + l_tcp->numpocs;
3605 if (l_nb_comp <= 256) {
3612 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3614 l_current_data = p_data;
3616 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3617 l_current_data += 2;
3619 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3620 l_current_data += 2;
3622 l_current_poc = l_tcp->pocs;
3623 for (i = 0; i < l_nb_poc; ++i) {
3624 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3627 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3628 l_current_data+=l_poc_room;
3630 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3633 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3636 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3637 l_current_data+=l_poc_room;
3639 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3642 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3643 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3644 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3645 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3650 *p_data_written = l_poc_size;
3654 * Gets the maximum size taken by the writing of a POC.
3656 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3658 opj_tcp_v2_t * l_tcp = 00;
3659 OPJ_UINT32 l_nb_tiles = 0;
3660 OPJ_UINT32 l_max_poc = 0;
3663 l_tcp = p_j2k->m_cp.tcps;
3664 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3666 for (i=0;i<l_nb_tiles;++i) {
3667 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3673 return 4 + 9 * l_max_poc;
3677 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3679 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3682 OPJ_UINT32 l_nb_tiles;
3683 OPJ_UINT32 l_max = 0;
3684 opj_tcp_v2_t * l_tcp = 00;
3686 l_tcp = p_j2k->m_cp.tcps;
3687 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3689 for (i=0;i<l_nb_tiles;++i) {
3690 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3700 * Gets the maximum size taken by the headers of the SOT.
3702 * @param p_j2k the jpeg2000 codec to use.
3704 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3706 OPJ_UINT32 l_nb_bytes = 0;
3707 OPJ_UINT32 l_nb_comps;
3708 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3710 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3711 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3713 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3714 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3715 l_nb_bytes += l_nb_comps * l_coc_bytes;
3717 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3718 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3721 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3723 /*** DEVELOPER CORNER, Add room for your headers ***/
3728 static void j2k_read_poc(opj_j2k_t *j2k) {
3729 int len, numpchgs, i, old_poc;
3731 int numcomps = j2k->image->numcomps;
3733 opj_cp_t *cp = j2k->cp;
3734 opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
3735 opj_cio_t *cio = j2k->cio;
3737 old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
3739 len = cio_read(cio, 2); /* Lpoc */
3740 numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
3742 for (i = old_poc; i < numpchgs + old_poc; i++) {
3744 poc = &tcp->pocs[i];
3745 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
3746 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
3747 poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
3748 poc->resno1 = cio_read(cio, 1); /* REpoc_i */
3749 poc->compno1 = int_min(
3750 cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
3751 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
3754 tcp->numpocs = numpchgs + old_poc - 1;
3758 * Reads a POC marker (Progression Order Change)
3760 * @param p_header_data the data contained in the POC box.
3761 * @param p_j2k the jpeg2000 codec.
3762 * @param p_header_size the size of the data contained in the POC marker.
3763 * @param p_manager the user event manager.
3765 opj_bool j2k_read_poc_v2 (
3766 opj_j2k_v2_t *p_j2k,
3767 OPJ_BYTE * p_header_data,
3768 OPJ_UINT32 p_header_size,
3769 struct opj_event_mgr * p_manager)
3771 OPJ_UINT32 i, l_nb_comp, l_tmp;
3772 opj_image_t * l_image = 00;
3773 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3774 OPJ_UINT32 l_chunk_size, l_comp_room;
3776 opj_cp_v2_t *l_cp = 00;
3777 opj_tcp_v2_t *l_tcp = 00;
3778 opj_poc_t *l_current_poc = 00;
3781 assert(p_header_data != 00);
3782 assert(p_j2k != 00);
3783 assert(p_manager != 00);
3785 l_image = p_j2k->m_private_image;
3786 l_nb_comp = l_image->numcomps;
3787 if (l_nb_comp <= 256) {
3793 l_chunk_size = 5 + 2 * l_comp_room;
3794 l_current_poc_nb = p_header_size / l_chunk_size;
3795 l_current_poc_remaining = p_header_size % l_chunk_size;
3797 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3798 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3802 l_cp = &(p_j2k->m_cp);
3803 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3804 &l_cp->tcps[p_j2k->m_current_tile_number] :
3805 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3806 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3807 l_current_poc_nb += l_old_poc_nb;
3809 assert(l_current_poc_nb < 32);
3811 /* now poc is in use.*/
3814 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3815 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3816 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3818 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3819 p_header_data+=l_comp_room;
3820 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3822 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3824 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3825 p_header_data+=l_comp_room;
3826 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3828 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3829 /* make sure comp is in acceptable bounds */
3830 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3834 l_tcp->numpocs = l_current_poc_nb - 1;
3838 static void j2k_read_crg(opj_j2k_t *j2k) {
3839 int len, i, Xcrg_i, Ycrg_i;
3841 opj_cio_t *cio = j2k->cio;
3842 int numcomps = j2k->image->numcomps;
3844 len = cio_read(cio, 2); /* Lcrg */
3845 for (i = 0; i < numcomps; i++) {
3846 Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
3847 Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
3852 * Reads a CRG marker (Component registration)
3854 * @param p_header_data the data contained in the TLM box.
3855 * @param p_j2k the jpeg2000 codec.
3856 * @param p_header_size the size of the data contained in the TLM marker.
3857 * @param p_manager the user event manager.
3859 opj_bool j2k_read_crg_v2 (
3860 opj_j2k_v2_t *p_j2k,
3861 OPJ_BYTE * p_header_data,
3862 OPJ_UINT32 p_header_size,
3863 struct opj_event_mgr * p_manager
3866 OPJ_UINT32 l_nb_comp;
3868 assert(p_header_data != 00);
3869 assert(p_j2k != 00);
3870 assert(p_manager != 00);
3872 l_nb_comp = p_j2k->m_private_image->numcomps;
3874 if (p_header_size != l_nb_comp *4) {
3875 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3878 /* Do not care of this at the moment since only local variables are set here */
3881 (i = 0; i < l_nb_comp; ++i)
3883 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3885 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3892 static void j2k_read_tlm(opj_j2k_t *j2k) {
3893 int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
3894 long int Ttlm_i, Ptlm_i;
3896 opj_cio_t *cio = j2k->cio;
3898 len = cio_read(cio, 2); /* Ltlm */
3899 Ztlm = cio_read(cio, 1); /* Ztlm */
3900 Stlm = cio_read(cio, 1); /* Stlm */
3901 ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
3902 SP = (Stlm >> 6) & 0x01;
3903 tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
3904 for (i = 0; i < tile_tlm; i++) {
3905 Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
3906 Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
3911 * Reads a TLM marker (Tile Length Marker)
3913 * @param p_header_data the data contained in the TLM box.
3914 * @param p_j2k the jpeg2000 codec.
3915 * @param p_header_size the size of the data contained in the TLM marker.
3916 * @param p_manager the user event manager.
3918 opj_bool j2k_read_tlm_v2 (
3919 opj_j2k_v2_t *p_j2k,
3920 OPJ_BYTE * p_header_data,
3921 OPJ_UINT32 p_header_size,
3922 struct opj_event_mgr * p_manager
3925 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3927 assert(p_header_data != 00);
3928 assert(p_j2k != 00);
3929 assert(p_manager != 00);
3931 if (p_header_size < 2) {
3932 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3937 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3939 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3942 l_ST = ((l_Stlm >> 4) & 0x3);
3943 l_SP = (l_Stlm >> 6) & 0x1;
3945 l_Ptlm_size = (l_SP + 1) * 2;
3946 l_quotient = l_Ptlm_size + l_ST;
3948 l_tot_num_tp = p_header_size / l_quotient;
3949 l_tot_num_tp_remaining = p_header_size % l_quotient;
3951 if (l_tot_num_tp_remaining != 0) {
3952 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3955 /* FIXME Do not care of this at the moment since only local variables are set here */
3958 (i = 0; i < l_tot_num_tp; ++i)
3960 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3961 p_header_data += l_ST;
3962 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3963 p_header_data += l_Ptlm_size;
3968 static void j2k_read_plm(opj_j2k_t *j2k) {
3969 int len, i, Zplm, Nplm, add, packet_len = 0;
3971 opj_cio_t *cio = j2k->cio;
3973 len = cio_read(cio, 2); /* Lplm */
3974 Zplm = cio_read(cio, 1); /* Zplm */
3977 Nplm = cio_read(cio, 4); /* Nplm */
3979 for (i = Nplm; i > 0; i--) {
3980 add = cio_read(cio, 1);
3982 packet_len = (packet_len << 7) + add; /* Iplm_ij */
3983 if ((add & 0x80) == 0) {
3994 * Reads a PLM marker (Packet length, main header marker)
3996 * @param p_header_data the data contained in the TLM box.
3997 * @param p_j2k the jpeg2000 codec.
3998 * @param p_header_size the size of the data contained in the TLM marker.
3999 * @param p_manager the user event manager.
4001 opj_bool j2k_read_plm_v2 (
4002 opj_j2k_v2_t *p_j2k,
4003 OPJ_BYTE * p_header_data,
4004 OPJ_UINT32 p_header_size,
4005 struct opj_event_mgr * p_manager
4009 assert(p_header_data != 00);
4010 assert(p_j2k != 00);
4011 assert(p_manager != 00);
4013 if (p_header_size < 1) {
4014 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4017 /* Do not care of this at the moment since only local variables are set here */
4019 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
4026 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
4028 p_header_size -= (1+l_Nplm);
4032 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4036 (i = 0; i < l_Nplm; ++i)
4038 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
4040 // take only the last seven bytes
4041 l_packet_len |= (l_tmp & 0x7f);
4049 // store packet length and proceed to next packet
4056 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
4064 static void j2k_read_plt(opj_j2k_t *j2k) {
4065 int len, i, Zplt, packet_len = 0, add;
4067 opj_cio_t *cio = j2k->cio;
4069 len = cio_read(cio, 2); /* Lplt */
4070 Zplt = cio_read(cio, 1); /* Zplt */
4071 for (i = len - 3; i > 0; i--) {
4072 add = cio_read(cio, 1);
4073 packet_len = (packet_len << 7) + add; /* Iplt_i */
4074 if ((add & 0x80) == 0) {
4082 * Reads a PLT marker (Packet length, tile-part header)
4084 * @param p_header_data the data contained in the PLT box.
4085 * @param p_j2k the jpeg2000 codec.
4086 * @param p_header_size the size of the data contained in the PLT marker.
4087 * @param p_manager the user event manager.
4089 opj_bool j2k_read_plt_v2 (
4090 opj_j2k_v2_t *p_j2k,
4091 OPJ_BYTE * p_header_data,
4092 OPJ_UINT32 p_header_size,
4093 struct opj_event_mgr * p_manager
4096 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
4099 assert(p_header_data != 00);
4100 assert(p_j2k != 00);
4101 assert(p_manager != 00);
4103 if (p_header_size < 1) {
4104 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4108 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
4112 for (i = 0; i < p_header_size; ++i) {
4113 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
4115 /* take only the last seven bytes */
4116 l_packet_len |= (l_tmp & 0x7f);
4121 /* store packet length and proceed to next packet */
4126 if (l_packet_len != 0) {
4127 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
4134 static void j2k_read_ppm(opj_j2k_t *j2k) {
4135 int len, Z_ppm, i, j;
4138 opj_cp_t *cp = j2k->cp;
4139 opj_cio_t *cio = j2k->cio;
4141 len = cio_read(cio, 2);
4144 Z_ppm = cio_read(cio, 1); /* Z_ppm */
4147 if (cp->ppm_previous == 0) {
4148 N_ppm = cio_read(cio, 4); /* N_ppm */
4151 N_ppm = cp->ppm_previous;
4154 if (Z_ppm == 0) { /* First PPM marker */
4155 cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
4156 cp->ppm_data_first = cp->ppm_data;
4157 cp->ppm_len = N_ppm;
4158 } else { /* NON-first PPM marker */
4159 cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
4162 /* this memory allocation check could be done even in non-JPWL cases */
4164 if (!cp->ppm_data) {
4165 opj_event_msg(j2k->cinfo, EVT_ERROR,
4166 "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
4168 if (!JPWL_ASSUME || JPWL_ASSUME) {
4169 opj_free(cp->ppm_data);
4170 opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
4177 cp->ppm_data_first = cp->ppm_data;
4178 cp->ppm_len = N_ppm + cp->ppm_store;
4180 for (i = N_ppm; i > 0; i--) { /* Read packet header */
4181 cp->ppm_data[j] = cio_read(cio, 1);
4185 break; /* Case of non-finished packet header in present marker but finished in next one */
4187 cp->ppm_previous = i - 1;
4192 * Reads a PPM marker (Packed packet headers, main header)
4194 * @param p_header_data the data contained in the POC box.
4195 * @param p_j2k the jpeg2000 codec.
4196 * @param p_header_size the size of the data contained in the POC marker.
4197 * @param p_manager the user event manager.
4200 opj_bool j2k_read_ppm_v2 (
4201 opj_j2k_v2_t *p_j2k,
4202 OPJ_BYTE * p_header_data,
4203 OPJ_UINT32 p_header_size,
4204 struct opj_event_mgr * p_manager
4208 opj_cp_v2_t *l_cp = 00;
4209 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4212 assert(p_header_data != 00);
4213 assert(p_j2k != 00);
4214 assert(p_manager != 00);
4216 if (p_header_size < 1) {
4217 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4221 l_cp = &(p_j2k->m_cp);
4224 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4228 /* First PPM marker */
4230 if (p_header_size < 4) {
4231 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4235 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4239 /* First PPM marker: Initialization */
4240 l_cp->ppm_len = l_N_ppm;
4241 l_cp->ppm_data_size = 0;
4243 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4244 if (l_cp->ppm_buffer == 00) {
4245 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4248 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
4250 l_cp->ppm_data = l_cp->ppm_buffer;
4254 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4255 if (p_header_size >= 4) {
4257 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4260 l_cp->ppm_len += l_N_ppm ;
4262 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4263 if (l_cp->ppm_buffer == 00) {
4264 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4267 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4269 l_cp->ppm_data = l_cp->ppm_buffer;
4276 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4278 if (l_remaining_data <= p_header_size) {
4279 /* we must store less information than available in the packet */
4280 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4281 l_cp->ppm_data_size = l_cp->ppm_len;
4282 p_header_size -= l_remaining_data;
4283 p_header_data += l_remaining_data;
4286 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4287 l_cp->ppm_data_size += p_header_size;
4288 p_header_data += p_header_size;
4301 * Reads a PPM marker (Packed packet headers, main header)
4303 * @param p_header_data the data contained in the POC box.
4304 * @param p_j2k the jpeg2000 codec.
4305 * @param p_header_size the size of the data contained in the POC marker.
4306 * @param p_manager the user event manager.
4308 opj_bool j2k_read_ppm_v3 (
4309 opj_j2k_v2_t *p_j2k,
4310 OPJ_BYTE * p_header_data,
4311 OPJ_UINT32 p_header_size,
4312 struct opj_event_mgr * p_manager
4315 opj_cp_v2_t *l_cp = 00;
4316 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4319 assert(p_header_data != 00);
4320 assert(p_j2k != 00);
4321 assert(p_manager != 00);
4323 /* Minimum size of PPM marker is equal to the size of Zppm element */
4324 if (p_header_size < 1) {
4325 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4329 l_cp = &(p_j2k->m_cp);
4332 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4336 /* First PPM marker */
4338 /* We need now at least the Nppm^0 element */
4339 if (p_header_size < 4) {
4340 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4344 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4348 /* First PPM marker: Initialization */
4349 l_cp->ppm_len = l_N_ppm;
4350 l_cp->ppm_data_read = 0;
4352 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4353 if (l_cp->ppm_data == 00) {
4354 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4357 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4359 l_cp->ppm_data_current = l_cp->ppm_data;
4361 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4364 if (p_header_size < 4) {
4365 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4369 /* Uncompleted Ippm series in the previous PPM marker?*/
4370 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4371 /* Get the place where add the remaining Ippm series*/
4372 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4373 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4376 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4380 /* Increase the size of ppm_data to add the new Ippm series*/
4381 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4383 /* Keep the position of the place where concatenate the new series*/
4384 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4385 l_cp->ppm_len += l_N_ppm;
4390 l_remaining_data = p_header_size;
4392 while (l_remaining_data >= l_N_ppm) {
4393 /* read a complete Ippm series*/
4394 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4395 p_header_size -= l_N_ppm;
4396 p_header_data += l_N_ppm;
4398 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4402 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4407 l_remaining_data = p_header_size;
4411 l_remaining_data = p_header_size;
4413 /* Next Ippm series is a complete series ?*/
4414 if (l_remaining_data > l_N_ppm) {
4415 /* Increase the size of ppm_data to add the new Ippm series*/
4416 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4418 /* Keep the position of the place where concatenate the new series */
4419 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4420 l_cp->ppm_len += l_N_ppm;
4425 /* Need to read an incomplete Ippm series*/
4426 if (l_remaining_data) {
4427 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4429 /* Keep the position of the place where concatenate the new series*/
4430 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4431 l_cp->ppm_len += l_N_ppm;
4433 /* Read incomplete Ippm series*/
4434 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4435 p_header_size -= l_remaining_data;
4436 p_header_data += l_remaining_data;
4438 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4443 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4444 if (p_header_size >= 4) {
4446 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4449 l_cp->ppm_len += l_N_ppm ;
4451 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4452 if (l_cp->ppm_buffer == 00) {
4453 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4456 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4458 l_cp->ppm_data = l_cp->ppm_buffer;
4465 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4467 if (l_remaining_data <= p_header_size) {
4468 /* we must store less information than available in the packet */
4469 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4470 l_cp->ppm_data_size = l_cp->ppm_len;
4471 p_header_size -= l_remaining_data;
4472 p_header_data += l_remaining_data;
4475 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4476 l_cp->ppm_data_size += p_header_size;
4477 p_header_data += p_header_size;
4486 static void j2k_read_ppt(opj_j2k_t *j2k) {
4487 int len, Z_ppt, i, j = 0;
4489 opj_cp_t *cp = j2k->cp;
4490 opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
4491 opj_cio_t *cio = j2k->cio;
4493 len = cio_read(cio, 2);
4494 Z_ppt = cio_read(cio, 1);
4496 if (Z_ppt == 0) { /* First PPT marker */
4497 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
4498 tcp->ppt_data_first = tcp->ppt_data;
4500 tcp->ppt_len = len - 3;
4501 } else { /* NON-first PPT marker */
4502 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
4503 tcp->ppt_data_first = tcp->ppt_data;
4504 tcp->ppt_len = len - 3 + tcp->ppt_store;
4507 for (i = len - 3; i > 0; i--) {
4508 tcp->ppt_data[j] = cio_read(cio, 1);
4515 * Reads a PPT marker (Packed packet headers, tile-part header)
4517 * @param p_header_data the data contained in the PPT box.
4518 * @param p_j2k the jpeg2000 codec.
4519 * @param p_header_size the size of the data contained in the PPT marker.
4520 * @param p_manager the user event manager.
4522 opj_bool j2k_read_ppt_v2 ( opj_j2k_v2_t *p_j2k,
4523 OPJ_BYTE * p_header_data,
4524 OPJ_UINT32 p_header_size,
4525 struct opj_event_mgr * p_manager )
4527 opj_cp_v2_t *l_cp = 00;
4528 opj_tcp_v2_t *l_tcp = 00;
4532 assert(p_header_data != 00);
4533 assert(p_j2k != 00);
4534 assert(p_manager != 00);
4536 /* We need to have the Z_ppt element at minimum */
4537 if (p_header_size < 1) {
4538 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4542 l_cp = &(p_j2k->m_cp);
4544 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");
4548 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4551 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4555 /* Allocate buffer to read the packet header */
4557 /* First PPT marker */
4558 l_tcp->ppt_data_size = 0;
4559 l_tcp->ppt_len = p_header_size;
4561 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4562 if (l_tcp->ppt_buffer == 00) {
4563 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4566 l_tcp->ppt_data = l_tcp->ppt_buffer;
4568 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4571 l_tcp->ppt_len += p_header_size;
4573 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4574 if (l_tcp->ppt_buffer == 00) {
4575 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4578 l_tcp->ppt_data = l_tcp->ppt_buffer;
4580 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4583 /* Read packet header from buffer */
4584 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4586 l_tcp->ppt_data_size += p_header_size;
4592 * Writes the TLM marker (Tile Length Marker)
4594 * @param p_stream the stream to write data to.
4595 * @param p_j2k J2K codec.
4596 * @param p_manager the user event manager.
4598 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4599 struct opj_stream_private *p_stream,
4600 struct opj_event_mgr * p_manager )
4602 OPJ_BYTE * l_current_data = 00;
4603 OPJ_UINT32 l_tlm_size;
4606 assert(p_j2k != 00);
4607 assert(p_manager != 00);
4608 assert(p_stream != 00);
4610 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4612 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4613 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4614 = (OPJ_BYTE*)opj_realloc(
4615 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4618 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4622 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4625 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4627 /* change the way data is written to avoid seeking if possible */
4629 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4631 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4632 l_current_data += 2;
4634 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4635 l_current_data += 2;
4637 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4640 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4643 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4644 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) {
4651 static void j2k_write_tlm(opj_j2k_t *j2k){
4653 opj_cio_t *cio = j2k->cio;
4654 j2k->tlm_start = cio_tell(cio);
4655 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4656 lenp = 4 + (5*j2k->totnum_tp);
4657 cio_write(cio,lenp,2); /* Ltlm */
4658 cio_write(cio, 0,1); /* Ztlm=0*/
4659 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4660 cio_skip(cio,5*j2k->totnum_tp);
4663 static void j2k_write_sot(opj_j2k_t *j2k) {
4666 opj_cio_t *cio = j2k->cio;
4668 j2k->sot_start = cio_tell(cio);
4669 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4670 lenp = cio_tell(cio);
4671 cio_skip(cio, 2); /* Lsot (further) */
4672 cio_write(cio, j2k->curtileno, 2); /* Isot */
4673 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4674 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4675 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4676 len = cio_tell(cio) - lenp;
4677 cio_seek(cio, lenp);
4678 cio_write(cio, len, 2); /* Lsot */
4679 cio_seek(cio, lenp + len);
4683 /* update markers struct */
4684 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4685 #endif /* USE_JPWL */
4688 if( j2k->cstr_info && j2k->cur_tp_num==0){
4689 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4694 * Writes the SOT marker (Start of tile-part)
4696 * @param p_stream the stream to write data to.
4697 * @param p_j2k J2K codec.
4698 * @param p_manager the user event manager.
4700 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4702 OPJ_UINT32 * p_data_written,
4703 const struct opj_stream_private *p_stream,
4704 struct opj_event_mgr * p_manager )
4707 assert(p_j2k != 00);
4708 assert(p_manager != 00);
4709 assert(p_stream != 00);
4711 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4714 opj_write_bytes(p_data,10,2); /* Lsot */
4717 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4723 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4726 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4731 /* update markers struct */
4733 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4735 assert( 0 && "TODO" );
4736 #endif /* USE_JPWL */
4738 * p_data_written = 12;
4747 * Reads a PPT marker (Packed packet headers, tile-part header)
4749 * @param p_header_data the data contained in the PPT box.
4750 * @param p_j2k the jpeg2000 codec.
4751 * @param p_header_size the size of the data contained in the PPT marker.
4752 * @param p_manager the user event manager.
4754 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4755 OPJ_BYTE * p_header_data,
4756 OPJ_UINT32 p_header_size,
4757 opj_event_mgr_t * p_manager )
4759 opj_cp_v2_t *l_cp = 00;
4760 opj_tcp_v2_t *l_tcp = 00;
4761 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4762 OPJ_UINT32 l_current_part;
4763 OPJ_UINT32 l_tile_x,l_tile_y;
4766 assert(p_header_data != 00);
4767 assert(p_j2k != 00);
4768 assert(p_manager != 00);
4770 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4771 if (p_header_size != 8) {
4772 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4776 l_cp = &(p_j2k->m_cp);
4777 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4780 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4781 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4782 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4785 if (l_cp->correct) {
4787 int tileno = p_j2k->m_current_tile_number;
4788 static int backup_tileno = 0;
4790 /* tileno is negative or larger than the number of tiles!!! */
4791 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4792 opj_event_msg_v2(p_manager, EVT_ERROR,
4793 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4794 tileno, (l_cp->tw * l_cp->th));
4796 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4799 /* we try to correct */
4800 tileno = backup_tileno;
4801 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4802 "- setting tile number to %d\n",
4806 /* keep your private count of tiles */
4809 #endif /* USE_JPWL */
4811 /* look for the tile in the list of already processed tile (in parts). */
4812 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4813 /* since the time taken by this function can only grow at the time */
4815 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4818 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4819 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4821 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4827 if (l_cp->correct) {
4829 /* totlen is negative or larger than the bytes left!!! */
4830 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))) { */
4831 opj_event_msg_v2(p_manager, EVT_ERROR,
4832 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4833 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4835 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4838 /* we try to correct */
4840 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4841 "- setting Psot to %d => assuming it is the last tile\n",
4845 #endif /* USE_JPWL */
4847 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4849 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4850 "we assuming it is the last tile-part of the codestream.\n");
4851 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4854 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4857 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4860 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4861 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4862 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4863 if (l_tcp->m_nb_tile_parts) {
4864 if (l_current_part >= l_tcp->m_nb_tile_parts){
4865 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4866 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4867 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4871 l_tcp->m_nb_tile_parts = l_num_parts;
4874 /* If know the number of tile part header we will check if we didn't read the last*/
4875 if (l_tcp->m_nb_tile_parts) {
4876 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4877 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4881 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4882 /* Keep the size of data to skip after this marker */
4883 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4886 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4887 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4890 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4892 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4893 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4894 p_j2k->m_specific_param.m_decoder.m_skip_data =
4895 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4896 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4897 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4898 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4901 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4902 p_j2k->m_specific_param.m_decoder.m_skip_data =
4903 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4907 if (p_j2k->cstr_index)
4909 assert(p_j2k->cstr_index->tile_index != 00);
4910 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4911 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4913 if (l_num_parts != 0){
4914 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4915 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4918 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4919 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4920 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4922 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4923 (opj_tp_index_t*)opj_realloc(
4924 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4925 l_num_parts* sizeof(opj_tp_index_t));
4928 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4930 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4931 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4932 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4933 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4934 sizeof(opj_tp_index_t));
4937 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4938 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4939 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4940 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4941 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4942 * sizeof(opj_tp_index_t));
4951 /* 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 */
4952 /* if (p_j2k->cstr_info) {
4955 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4958 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4959 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4960 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4961 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4964 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4967 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4971 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4974 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4975 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4976 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4981 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
4984 opj_tcp_t *tcp = NULL;
4985 opj_codestream_info_t *cstr_info = NULL;
4987 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
4988 opj_cp_t *cp = j2k->cp;
4989 opj_cio_t *cio = j2k->cio;
4991 tcd->tp_num = j2k->tp_num ;
4992 tcd->cur_tp_num = j2k->cur_tp_num;
4994 cio_write(cio, J2K_MS_SOD, 2);
4996 if( j2k->cstr_info && j2k->cur_tp_num==0){
4997 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
5000 if (j2k->curtileno == 0) {
5001 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
5005 cstr_info = j2k->cstr_info;
5007 if (!j2k->cur_tp_num ) {
5008 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
5009 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
5012 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
5013 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
5017 /* update markers struct */
5018 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
5019 #endif /* USE_JPWL */
5024 tcp = &cp->tcps[j2k->curtileno];
5025 for (layno = 0; layno < tcp->numlayers; layno++) {
5026 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
5027 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
5028 } else if (tcp->rates[layno]) {
5029 tcp->rates[layno]=1;
5032 if(j2k->cur_tp_num == 0){
5033 tcd->tcd_image->tiles->packno = 0;
5035 cstr_info->packno = 0;
5038 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
5040 /* Writing Psot in SOT marker */
5041 totlen = cio_tell(cio) + l - j2k->sot_start;
5042 cio_seek(cio, j2k->sot_start + 6);
5043 cio_write(cio, totlen, 4);
5044 cio_seek(cio, j2k->sot_start + totlen);
5045 /* Writing Ttlm and Ptlm in TLM marker */
5047 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
5048 cio_write(cio, j2k->curtileno, 1);
5049 cio_write(cio, totlen, 4);
5051 cio_seek(cio, j2k->sot_start + totlen);
5055 * Writes the SOD marker (Start of data)
5057 * @param p_stream the stream to write data to.
5058 * @param p_j2k J2K codec.
5059 * @param p_manager the user event manager.
5061 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
5062 struct opj_tcd_v2 * p_tile_coder,
5064 OPJ_UINT32 * p_data_written,
5065 OPJ_UINT32 p_total_data_size,
5066 const struct opj_stream_private *p_stream,
5067 struct opj_event_mgr * p_manager )
5069 opj_tcp_v2_t *l_tcp = 00;
5070 opj_codestream_info_t *l_cstr_info = 00;
5071 opj_cp_v2_t *l_cp = 00;
5073 OPJ_UINT32 l_size_tile;
5074 OPJ_UINT32 l_remaining_data;
5077 assert(p_j2k != 00);
5078 assert(p_manager != 00);
5079 assert(p_stream != 00);
5081 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
5084 /* make room for the EOF marker */
5085 l_remaining_data = p_total_data_size - 4;
5087 l_cp = &(p_j2k->m_cp);
5088 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
5091 /* update tile coder */
5092 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
5093 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
5095 l_size_tile = l_cp->th * l_cp->tw;
5098 /* TODO mergeV2: check this part which use cstr_info */
5099 /*l_cstr_info = p_j2k->cstr_info;
5101 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
5102 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
5103 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
5109 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
5111 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
5116 /* update markers struct */
5117 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
5119 assert( 0 && "TODO" );
5120 #endif /* USE_JPWL */
5125 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
5126 p_tile_coder->tcd_image->tiles->packno = 0;
5128 l_cstr_info->packno = 0;
5132 *p_data_written = 0;
5134 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)) {
5135 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
5139 *p_data_written += 2;
5148 * Reads a SOD marker (Start Of Data)
5150 * @param p_header_data the data contained in the SOD box.
5151 * @param p_j2k the jpeg2000 codec.
5152 * @param p_header_size the size of the data contained in the SOD marker.
5153 * @param p_manager the user event manager.
5155 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
5156 opj_stream_private_t *p_stream,
5157 opj_event_mgr_t * p_manager
5160 OPJ_UINT32 l_current_read_size;
5161 opj_codestream_index_t * l_cstr_index = 00;
5162 OPJ_BYTE ** l_current_data = 00;
5163 opj_tcp_v2_t * l_tcp = 00;
5164 OPJ_UINT32 * l_tile_len = 00;
5167 assert(p_j2k != 00);
5168 assert(p_manager != 00);
5169 assert(p_stream != 00);
5171 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5173 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
5174 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
5175 // but we are in the last tile part,
5176 // so its result will fit on OPJ_UINT32 unless we find
5177 // a file with a single tile part of more than 4 GB...*/
5178 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
5181 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
5183 l_current_data = &(l_tcp->m_data);
5184 l_tile_len = &l_tcp->m_data_size;
5186 if (! *l_current_data) {
5187 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
5190 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
5193 if (*l_current_data == 00) {
5194 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
5200 l_cstr_index = p_j2k->cstr_index;
5202 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
5204 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
5205 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
5207 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
5208 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
5210 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
5214 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
5216 /*l_cstr_index->packno = 0;*/
5219 l_current_read_size = opj_stream_read_data( p_stream,
5220 *l_current_data + *l_tile_len,
5221 p_j2k->m_specific_param.m_decoder.m_sot_length,
5224 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
5225 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
5228 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
5231 *l_tile_len += l_current_read_size;
5237 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
5238 opj_cp_t *cp = j2k->cp;
5239 opj_tcp_t *tcp = &cp->tcps[tileno];
5240 opj_cio_t *cio = j2k->cio;
5241 int numcomps = j2k->image->numcomps;
5243 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
5244 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
5245 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
5246 cio_write(cio, 0, 1); /* Srgn */
5247 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
5251 * Writes the RGN marker (Region Of Interest)
5253 * @param p_tile_no the tile to output
5254 * @param p_comp_no the component to output
5255 * @param p_stream the stream to write data to.
5256 * @param p_j2k J2K codec.
5257 * @param p_manager the user event manager.
5259 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
5260 OPJ_UINT32 p_tile_no,
5261 OPJ_UINT32 p_comp_no,
5262 struct opj_stream_private *p_stream,
5263 struct opj_event_mgr * p_manager )
5265 OPJ_BYTE * l_current_data = 00;
5266 OPJ_UINT32 l_nb_comp;
5267 OPJ_UINT32 l_rgn_size;
5268 opj_image_t *l_image = 00;
5269 opj_cp_v2_t *l_cp = 00;
5270 opj_tcp_v2_t *l_tcp = 00;
5271 opj_tccp_t *l_tccp = 00;
5272 OPJ_UINT32 l_comp_room;
5275 assert(p_j2k != 00);
5276 assert(p_manager != 00);
5277 assert(p_stream != 00);
5279 l_cp = &(p_j2k->m_cp);
5280 l_tcp = &l_cp->tcps[p_tile_no];
5281 l_tccp = &l_tcp->tccps[p_comp_no];
5283 l_nb_comp = l_image->numcomps;
5285 if (l_nb_comp <= 256) {
5292 l_rgn_size = 6 + l_comp_room;
5294 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5296 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
5297 l_current_data += 2;
5299 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
5300 l_current_data += 2;
5302 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
5303 l_current_data+=l_comp_room;
5305 opj_write_bytes(l_current_data, 0,1); /* Srgn */
5308 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
5311 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) {
5319 static void j2k_write_eoc(opj_j2k_t *j2k) {
5320 opj_cio_t *cio = j2k->cio;
5321 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
5322 cio_write(cio, J2K_MS_EOC, 2);
5326 /* update markers struct */
5327 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5328 #endif /* USE_JPWL */
5333 * Writes the EOC marker (End of Codestream)
5335 * @param p_stream the stream to write data to.
5336 * @param p_j2k J2K codec.
5337 * @param p_manager the user event manager.
5339 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5340 struct opj_stream_private *p_stream,
5341 struct opj_event_mgr * p_manager )
5344 assert(p_j2k != 00);
5345 assert(p_manager != 00);
5346 assert(p_stream != 00);
5348 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5353 /* update markers struct */
5355 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5357 #endif /* USE_JPWL */
5359 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5363 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5372 * Reads a RGN marker (Region Of Interest)
5374 * @param p_header_data the data contained in the POC box.
5375 * @param p_j2k the jpeg2000 codec.
5376 * @param p_header_size the size of the data contained in the POC marker.
5377 * @param p_manager the user event manager.
5379 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
5380 OPJ_BYTE * p_header_data,
5381 OPJ_UINT32 p_header_size,
5382 opj_event_mgr_t * p_manager
5385 OPJ_UINT32 l_nb_comp;
5386 opj_image_t * l_image = 00;
5388 opj_cp_v2_t *l_cp = 00;
5389 opj_tcp_v2_t *l_tcp = 00;
5390 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5393 assert(p_header_data != 00);
5394 assert(p_j2k != 00);
5395 assert(p_manager != 00);
5397 l_image = p_j2k->m_private_image;
5398 l_nb_comp = l_image->numcomps;
5400 if (l_nb_comp <= 256) {
5405 if (p_header_size != 2 + l_comp_room) {
5406 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5410 l_cp = &(p_j2k->m_cp);
5411 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5412 &l_cp->tcps[p_j2k->m_current_tile_number] :
5413 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5415 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5416 p_header_data+=l_comp_room;
5417 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5421 if (l_cp->correct) {
5422 /* totlen is negative or larger than the bytes left!!! */
5423 if (l_comp_room >= l_nb_comp) {
5424 opj_event_msg_v2(p_manager, EVT_ERROR,
5425 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5426 l_comp_room, l_nb_comp);
5427 if (!JPWL_ASSUME || JPWL_ASSUME) {
5428 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5433 #endif /* USE_JPWL */
5435 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5442 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5444 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5447 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5454 * Updates the rates of the tcp.
5456 * @param p_stream the stream to write data to.
5457 * @param p_j2k J2K codec.
5458 * @param p_manager the user event manager.
5460 opj_bool j2k_update_rates( opj_j2k_v2_t *p_j2k,
5461 struct opj_stream_private *p_stream,
5462 struct opj_event_mgr * p_manager )
5464 opj_cp_v2_t * l_cp = 00;
5465 opj_image_t * l_image = 00;
5466 opj_tcp_v2_t * l_tcp = 00;
5467 opj_image_comp_t * l_img_comp = 00;
5470 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5471 OPJ_FLOAT32 * l_rates = 0;
5472 OPJ_FLOAT32 l_sot_remove;
5473 OPJ_UINT32 l_bits_empty, l_size_pixel;
5474 OPJ_UINT32 l_tile_size = 0;
5475 OPJ_UINT32 l_last_res;
5476 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5479 assert(p_j2k != 00);
5480 assert(p_manager != 00);
5481 assert(p_stream != 00);
5484 l_cp = &(p_j2k->m_cp);
5485 l_image = p_j2k->m_private_image;
5488 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5489 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5490 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5492 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5493 l_tp_stride_func = get_tp_stride;
5496 l_tp_stride_func = get_default_stride;
5499 for (i=0;i<l_cp->th;++i) {
5500 for (j=0;j<l_cp->tw;++j) {
5501 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5503 /* 4 borders of the tile rescale on the image if necessary */
5504 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5505 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5506 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5507 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5509 l_rates = l_tcp->rates;
5511 /* Modification of the RATE >> */
5513 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5515 ((*l_rates) * l_bits_empty)
5523 for (k = 1; k < l_tcp->numlayers; ++k) {
5525 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5527 ((*l_rates) * l_bits_empty)
5543 for (i=0;i<l_cp->th;++i) {
5544 for (j=0;j<l_cp->tw;++j) {
5545 l_rates = l_tcp->rates;
5548 *l_rates -= l_sot_remove;
5550 if (*l_rates < 30) {
5557 l_last_res = l_tcp->numlayers - 1;
5559 for (k = 1; k < l_last_res; ++k) {
5562 *l_rates -= l_sot_remove;
5564 if (*l_rates < *(l_rates - 1) + 10) {
5565 *l_rates = (*(l_rates - 1)) + 20;
5573 *l_rates -= (l_sot_remove + 2.f);
5575 if (*l_rates < *(l_rates - 1) + 10) {
5576 *l_rates = (*(l_rates - 1)) + 20;
5584 l_img_comp = l_image->comps;
5587 for (i=0;i<l_image->numcomps;++i) {
5588 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5590 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5598 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5600 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5602 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5603 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5604 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5605 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5609 if (l_cp->m_specific_param.m_enc.m_cinema) {
5610 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5611 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5612 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5616 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5617 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5623 static void j2k_read_eoc(opj_j2k_t *j2k) {
5625 opj_bool success = OPJ_FALSE;
5627 /* if packets should be decoded */
5628 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5629 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5630 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5631 for (i = 0; i < j2k->cp->tileno_size; i++) {
5632 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5633 if (j2k->cp->tileno[i] != -1)
5635 tileno = j2k->cp->tileno[i];
5636 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5637 opj_free(j2k->tile_data[tileno]);
5638 j2k->tile_data[tileno] = NULL;
5639 tcd_free_decode_tile(tcd, i);
5642 success = OPJ_FALSE;
5643 if (success == OPJ_FALSE) {
5644 j2k->state |= J2K_STATE_ERR;
5648 tcd_free_decode(tcd);
5651 /* if packets should not be decoded */
5653 for (i = 0; i < j2k->cp->tileno_size; i++) {
5654 tileno = j2k->cp->tileno[i];
5655 opj_free(j2k->tile_data[tileno]);
5656 j2k->tile_data[tileno] = NULL;
5659 if (j2k->state & J2K_STATE_ERR)
5660 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5662 j2k->state = J2K_STATE_MT;
5666 * Reads a EOC marker (End Of Codestream)
5668 * @param p_header_data the data contained in the SOD box.
5669 * @param p_j2k the jpeg2000 codec.
5670 * @param p_header_size the size of the data contained in the SOD marker.
5671 * @param p_manager the user event manager.
5674 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5675 struct opj_stream_private *p_stream,
5676 struct opj_event_mgr * p_manager )
5679 opj_tcd_v2_t * l_tcd = 00;
5680 OPJ_UINT32 l_nb_tiles;
5681 opj_tcp_v2_t * l_tcp = 00;
5685 assert(p_j2k != 00);
5686 assert(p_manager != 00);
5687 assert(p_stream != 00);
5689 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5690 l_tcp = p_j2k->m_cp.tcps;
5692 l_tcd = tcd_create_v2(OPJ_TRUE);
5694 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5698 for (i = 0; i < l_nb_tiles; ++i) {
5699 if (l_tcp->m_data) {
5700 if (! tcd_init_decode_tile(l_tcd, i)) {
5701 tcd_destroy_v2(l_tcd);
5702 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5706 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5710 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5715 j2k_tcp_destroy(l_tcp);
5719 tcd_destroy_v2(l_tcd);
5725 * Gets the offset of the header.
5727 * @param p_stream the stream to write data to.
5728 * @param p_j2k J2K codec.
5729 * @param p_manager the user event manager.
5731 opj_bool j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5732 struct opj_stream_private *p_stream,
5733 struct opj_event_mgr * p_manager )
5736 assert(p_j2k != 00);
5737 assert(p_manager != 00);
5738 assert(p_stream != 00);
5740 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5746 * Writes the MCT marker (Multiple Component Transform)
5748 * @param p_stream the stream to write data to.
5749 * @param p_j2k J2K codec.
5750 * @param p_manager the user event manager.
5752 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5753 struct opj_stream_private *p_stream,
5754 struct opj_event_mgr * p_manager )
5757 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5758 opj_mct_data_t * l_mct_record;
5759 opj_tcp_v2_t * l_tcp;
5762 assert(p_j2k != 00);
5763 assert(p_stream != 00);
5764 assert(p_manager != 00);
5766 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5770 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5771 l_mct_record = l_tcp->m_mct_records;
5773 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5775 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5782 l_mcc_record = l_tcp->m_mcc_records;
5784 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5786 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5793 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5801 * Writes the image components.
5803 * @param p_stream the stream to write data to.
5804 * @param p_j2k J2K codec.
5805 * @param p_manager the user event manager.
5807 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5808 struct opj_stream_private *p_stream,
5809 struct opj_event_mgr * p_manager )
5814 assert(p_j2k != 00);
5815 assert(p_manager != 00);
5816 assert(p_stream != 00);
5818 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5820 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5824 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5833 * Writes regions of interests.
5835 * @param p_stream the stream to write data to.
5836 * @param p_j2k J2K codec.
5837 * @param p_manager the user event manager.
5839 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5840 struct opj_stream_private *p_stream,
5841 struct opj_event_mgr * p_manager )
5844 const opj_tccp_t *l_tccp = 00;
5847 assert(p_j2k != 00);
5848 assert(p_manager != 00);
5849 assert(p_stream != 00);
5851 l_tccp = p_j2k->m_cp.tcps->tccps;
5853 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5854 if (l_tccp->roishift) {
5856 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5870 * @param p_stream the stream to write data to.
5871 * @param p_j2k J2K codec.
5872 * @param p_manager the user event manager.
5874 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5875 struct opj_stream_private *p_stream,
5876 struct opj_event_mgr * p_manager )
5878 opj_codestream_index_t * l_cstr_index = 00;
5881 assert(p_j2k != 00);
5882 assert(p_manager != 00);
5883 assert(p_stream != 00);
5885 l_cstr_index = p_j2k->cstr_index;
5887 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5889 /* The following adjustment is done to adjust the codestream size */
5890 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5891 /* the first bunch of bytes is not in the codestream */
5892 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5897 /* preparation of JPWL marker segments */
5901 /* encode according to JPWL */
5902 jpwl_encode(p_j2k, p_stream, image);
5906 assert( 0 && "TODO" );
5907 #endif /* USE_JPWL */
5912 typedef struct opj_dec_mstabent {
5915 /** value of the state when the marker can appear */
5917 /** action linked to the marker */
5918 void (*handler) (opj_j2k_t *j2k);
5919 } opj_dec_mstabent_t;
5921 opj_dec_mstabent_t j2k_dec_mstab[] = {
5922 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5923 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5924 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5925 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5926 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5927 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5928 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5929 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5930 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5931 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5932 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
5933 {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
5934 {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
5935 {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
5936 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
5937 {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
5939 {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
5940 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
5943 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5944 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5945 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5946 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5947 #endif /* USE_JPWL */
5949 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5950 {J2K_MS_INSEC, 0, j2k_read_insec},
5951 #endif /* USE_JPSEC */
5953 {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
5956 static void j2k_read_unk(opj_j2k_t *j2k) {
5957 opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
5960 if (j2k->cp->correct) {
5962 int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
5963 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
5964 id = cio_read(j2k->cio, 2);
5965 opj_event_msg(j2k->cinfo, EVT_ERROR,
5966 "JPWL: really don't know this marker %x\n",
5969 opj_event_msg(j2k->cinfo, EVT_ERROR,
5970 "- possible synch loss due to uncorrectable codestream errors => giving up\n");
5973 /* OK, activate this at your own risk!!! */
5974 /* we look for the marker at the minimum hamming distance from this */
5975 while (j2k_dec_mstab[m].id) {
5977 /* 1's where they differ */
5978 tmp_id = j2k_dec_mstab[m].id ^ id;
5980 /* compute the hamming distance between our id and the current */
5982 for (i = 0; i < 16; i++) {
5983 if ((tmp_id >> i) & 0x0001) {
5988 /* if current distance is smaller, set the minimum */
5989 if (cur_dist < min_dist) {
5990 min_dist = cur_dist;
5991 min_id = j2k_dec_mstab[m].id;
5994 /* jump to the next marker */
5998 /* do we substitute the marker? */
5999 if (min_dist < JPWL_MAXIMUM_HAMMING) {
6000 opj_event_msg(j2k->cinfo, EVT_ERROR,
6001 "- marker %x is at distance %d from the read %x\n",
6002 min_id, min_dist, id);
6003 opj_event_msg(j2k->cinfo, EVT_ERROR,
6004 "- trying to substitute in place and crossing fingers!\n");
6005 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6006 cio_write(j2k->cio, min_id, 2);
6009 cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
6014 #endif /* USE_JPWL */
6019 * Reads an unknown marker
6021 * @param p_stream the stream object to read from.
6022 * @param p_j2k the jpeg2000 codec.
6023 * @param p_manager the user event manager.
6025 * @return true if the marker could be deduced.
6027 opj_bool j2k_read_unk_v2 ( opj_j2k_v2_t *p_j2k,
6028 struct opj_stream_private *p_stream,
6029 OPJ_UINT32 *output_marker,
6030 struct opj_event_mgr * p_manager
6033 OPJ_UINT32 l_unknown_marker;
6034 const opj_dec_memory_marker_handler_t * l_marker_handler;
6035 OPJ_UINT32 l_size_unk = 2;
6038 assert(p_j2k != 00);
6039 assert(p_manager != 00);
6040 assert(p_stream != 00);
6042 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
6045 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
6046 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
6047 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
6051 /* read 2 bytes as the new marker ID*/
6052 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
6054 if (!(l_unknown_marker < 0xff00)) {
6056 /* Get the marker handler from the marker ID*/
6057 l_marker_handler = j2k_get_marker_handler(l_unknown_marker);
6059 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
6060 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
6064 if (l_marker_handler->id != J2K_MS_UNK) {
6065 /* Add the marker to the codestream index*/
6066 if (l_marker_handler->id != J2K_MS_SOT)
6067 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
6068 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
6070 break; /* next marker is known and well located */
6078 *output_marker = l_marker_handler->id ;
6084 Read the lookup table containing all the marker, status and action
6085 @param id Marker value
6087 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
6088 opj_dec_mstabent_t *e;
6089 for (e = j2k_dec_mstab; e->id != 0; e++) {
6098 * Writes the MCT marker (Multiple Component Transform)
6100 * @param p_stream the stream to write data to.
6101 * @param p_j2k J2K codec.
6102 * @param p_manager the user event manager.
6104 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
6105 opj_mct_data_t * p_mct_record,
6106 struct opj_stream_private *p_stream,
6107 struct opj_event_mgr * p_manager )
6109 OPJ_UINT32 l_mct_size;
6110 OPJ_BYTE * l_current_data = 00;
6114 assert(p_j2k != 00);
6115 assert(p_manager != 00);
6116 assert(p_stream != 00);
6118 l_mct_size = 10 + p_mct_record->m_data_size;
6120 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6121 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6122 = (OPJ_BYTE*)opj_realloc(
6123 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6126 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6130 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
6133 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6135 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
6136 l_current_data += 2;
6138 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
6139 l_current_data += 2;
6141 opj_write_bytes(l_current_data,0,2); /* Zmct */
6142 l_current_data += 2;
6144 /* only one marker atm */
6145 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
6147 opj_write_bytes(l_current_data,l_tmp,2);
6148 l_current_data += 2;
6150 opj_write_bytes(l_current_data,0,2); /* Ymct */
6153 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
6155 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) {
6163 * Reads a MCT marker (Multiple Component Transform)
6165 * @param p_header_data the data contained in the MCT box.
6166 * @param p_j2k the jpeg2000 codec.
6167 * @param p_header_size the size of the data contained in the MCT marker.
6168 * @param p_manager the user event manager.
6170 opj_bool j2k_read_mct ( opj_j2k_v2_t *p_j2k,
6171 OPJ_BYTE * p_header_data,
6172 OPJ_UINT32 p_header_size,
6173 struct opj_event_mgr * p_manager )
6176 opj_tcp_v2_t *l_tcp = 00;
6179 opj_mct_data_t * l_mct_data;
6182 assert(p_header_data != 00);
6183 assert(p_j2k != 00);
6185 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6186 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6187 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6189 if (p_header_size < 2) {
6190 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6195 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
6198 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
6202 if(p_header_size <= 6) {
6203 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6207 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
6208 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
6211 l_indix = l_tmp & 0xff;
6212 l_mct_data = l_tcp->m_mct_records;
6214 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
6215 if (l_mct_data->m_index == l_indix) {
6222 if (i == l_tcp->m_nb_mct_records) {
6223 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
6224 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
6226 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));
6227 if(! l_tcp->m_mct_records) {
6228 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6232 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6233 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
6236 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
6239 if (l_mct_data->m_data) {
6240 opj_free(l_mct_data->m_data);
6241 l_mct_data->m_data = 00;
6244 l_mct_data->m_index = l_indix;
6245 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
6246 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
6248 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
6251 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
6257 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
6258 if (! l_mct_data->m_data) {
6259 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
6262 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
6264 l_mct_data->m_data_size = p_header_size;
6265 ++l_tcp->m_nb_mct_records;
6271 * Writes the MCC marker (Multiple Component Collection)
6273 * @param p_stream the stream to write data to.
6274 * @param p_j2k J2K codec.
6275 * @param p_manager the user event manager.
6277 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
6278 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
6279 struct opj_stream_private *p_stream,
6280 struct opj_event_mgr * p_manager )
6283 OPJ_UINT32 l_mcc_size;
6284 OPJ_BYTE * l_current_data = 00;
6285 OPJ_UINT32 l_nb_bytes_for_comp;
6290 assert(p_j2k != 00);
6291 assert(p_manager != 00);
6292 assert(p_stream != 00);
6294 if (p_mcc_record->m_nb_comps > 255 ) {
6295 l_nb_bytes_for_comp = 2;
6299 l_nb_bytes_for_comp = 1;
6303 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
6304 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
6306 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6307 = (OPJ_BYTE*)opj_realloc(
6308 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6310 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6314 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
6317 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6319 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
6320 l_current_data += 2;
6322 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
6323 l_current_data += 2;
6326 opj_write_bytes(l_current_data,0,2); /* Zmcc */
6327 l_current_data += 2;
6329 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
6332 /* only one marker atm */
6333 opj_write_bytes(l_current_data,0,2); /* Ymcc */
6336 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
6339 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
6342 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 */
6345 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
6346 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
6347 l_current_data+=l_nb_bytes_for_comp;
6350 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 */
6353 for (i=0;i<p_mcc_record->m_nb_comps;++i)
6355 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
6356 l_current_data+=l_nb_bytes_for_comp;
6359 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
6361 if (p_mcc_record->m_decorrelation_array) {
6362 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
6365 if (p_mcc_record->m_offset_array) {
6366 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
6369 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
6372 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) {
6380 * Reads a MCC marker (Multiple Component Collection)
6382 * @param p_header_data the data contained in the MCC box.
6383 * @param p_j2k the jpeg2000 codec.
6384 * @param p_header_size the size of the data contained in the MCC marker.
6385 * @param p_manager the user event manager.
6387 opj_bool j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6388 OPJ_BYTE * p_header_data,
6389 OPJ_UINT32 p_header_size,
6390 struct opj_event_mgr * p_manager )
6395 opj_tcp_v2_t * l_tcp;
6396 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6397 opj_mct_data_t * l_mct_data;
6398 OPJ_UINT32 l_nb_collections;
6399 OPJ_UINT32 l_nb_comps;
6400 OPJ_UINT32 l_nb_bytes_by_comp;
6404 assert(p_header_data != 00);
6405 assert(p_j2k != 00);
6406 assert(p_manager != 00);
6408 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6409 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6410 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6412 if (p_header_size < 2) {
6413 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6418 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6421 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6425 if (p_header_size < 7) {
6426 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6430 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6433 l_mcc_record = l_tcp->m_mcc_records;
6435 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6436 if (l_mcc_record->m_index == l_indix) {
6443 if (i == l_tcp->m_nb_mcc_records) {
6444 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6445 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6447 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6448 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6449 if (! l_tcp->m_mcc_records) {
6450 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6453 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6454 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));
6456 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6458 l_mcc_record->m_index = l_indix;
6460 /* only one marker atm */
6461 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6464 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6468 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6471 if (l_nb_collections > 1) {
6472 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6478 for (i=0;i<l_nb_collections;++i) {
6479 if (p_header_size < 3) {
6480 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6484 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6488 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6492 opj_read_bytes(p_header_data,&l_nb_comps,2);
6497 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6498 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6500 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6501 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6505 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6507 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6508 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6509 p_header_data+=l_nb_bytes_by_comp;
6512 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6517 opj_read_bytes(p_header_data,&l_nb_comps,2);
6520 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6521 l_nb_comps &= 0x7fff;
6523 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6524 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6528 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6529 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6533 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6535 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6536 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6537 p_header_data+=l_nb_bytes_by_comp;
6540 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6545 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6548 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6549 l_mcc_record->m_decorrelation_array = 00;
6550 l_mcc_record->m_offset_array = 00;
6552 l_indix = l_tmp & 0xff;
6554 l_mct_data = l_tcp->m_mct_records;
6555 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6556 if (l_mct_data->m_index == l_indix) {
6557 l_mcc_record->m_decorrelation_array = l_mct_data;
6563 if (l_mcc_record->m_decorrelation_array == 00) {
6564 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6569 l_indix = (l_tmp >> 8) & 0xff;
6571 l_mct_data = l_tcp->m_mct_records;
6572 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6573 if (l_mct_data->m_index == l_indix) {
6574 l_mcc_record->m_offset_array = l_mct_data;
6580 if (l_mcc_record->m_offset_array == 00) {
6581 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6587 if (p_header_size != 0) {
6588 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6592 ++l_tcp->m_nb_mcc_records;
6599 * Writes the MCO marker (Multiple component transformation ordering)
6601 * @param p_stream the stream to write data to.
6602 * @param p_j2k J2K codec.
6603 * @param p_manager the user event manager.
6605 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6606 struct opj_stream_private *p_stream,
6607 struct opj_event_mgr * p_manager
6610 OPJ_BYTE * l_current_data = 00;
6611 OPJ_UINT32 l_mco_size;
6612 opj_tcp_v2_t * l_tcp = 00;
6613 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6617 assert(p_j2k != 00);
6618 assert(p_manager != 00);
6619 assert(p_stream != 00);
6621 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6622 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6624 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6625 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6627 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6628 = (OPJ_BYTE*)opj_realloc(
6629 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6631 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6636 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6639 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6640 l_current_data += 2;
6642 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6643 l_current_data += 2;
6645 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6648 l_mcc_record = l_tcp->m_mcc_records;
6649 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6650 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6656 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) {
6664 * Reads a MCO marker (Multiple Component Transform Ordering)
6666 * @param p_header_data the data contained in the MCO box.
6667 * @param p_j2k the jpeg2000 codec.
6668 * @param p_header_size the size of the data contained in the MCO marker.
6669 * @param p_manager the user event manager.
6671 opj_bool j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6672 OPJ_BYTE * p_header_data,
6673 OPJ_UINT32 p_header_size,
6674 struct opj_event_mgr * p_manager )
6676 OPJ_UINT32 l_tmp, i;
6677 OPJ_UINT32 l_nb_stages;
6678 opj_tcp_v2_t * l_tcp;
6679 opj_tccp_t * l_tccp;
6680 opj_image_t * l_image;
6681 opj_image_comp_t * l_img_comp;
6684 assert(p_header_data != 00);
6685 assert(p_j2k != 00);
6686 assert(p_manager != 00);
6688 l_image = p_j2k->m_private_image;
6689 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6690 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6691 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6693 if (p_header_size < 1) {
6694 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6698 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6701 if (l_nb_stages > 1) {
6702 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6706 if (p_header_size != l_nb_stages + 1) {
6707 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6711 l_tccp = l_tcp->tccps;
6712 l_img_comp = l_image->comps;
6714 for (i=0;i<l_image->numcomps;++i) {
6715 l_tccp->m_dc_level_shift = 0;
6719 if (l_tcp->m_mct_decoding_matrix) {
6720 opj_free(l_tcp->m_mct_decoding_matrix);
6721 l_tcp->m_mct_decoding_matrix = 00;
6724 for (i=0;i<l_nb_stages;++i) {
6725 opj_read_bytes(p_header_data,&l_tmp,1);
6728 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6736 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6739 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6740 opj_mct_data_t * l_deco_array, * l_offset_array;
6741 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6742 OPJ_UINT32 l_nb_elem;
6743 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6744 opj_tccp_t * l_tccp;
6747 assert(p_tcp != 00);
6749 l_mcc_record = p_tcp->m_mcc_records;
6751 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6752 if (l_mcc_record->m_index == p_index) {
6757 if (i==p_tcp->m_nb_mcc_records) {
6758 /** element discarded **/
6762 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6763 /** do not support number of comps != image */
6767 l_deco_array = l_mcc_record->m_decorrelation_array;
6770 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6771 if (l_deco_array->m_data_size != l_data_size) {
6775 l_nb_elem = p_image->numcomps * p_image->numcomps;
6776 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6777 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6779 if (! p_tcp->m_mct_decoding_matrix ) {
6783 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);
6786 l_offset_array = l_mcc_record->m_offset_array;
6788 if (l_offset_array) {
6789 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6790 if (l_offset_array->m_data_size != l_data_size) {
6794 l_nb_elem = p_image->numcomps;
6795 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6796 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6798 if (! l_offset_data ) {
6802 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6804 l_tccp = p_tcp->tccps;
6805 l_current_offset_data = l_offset_data;
6807 for (i=0;i<p_image->numcomps;++i) {
6808 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6812 opj_free(l_offset_data);
6819 * Writes the CBD marker (Component bit depth definition)
6821 * @param p_stream the stream to write data to.
6822 * @param p_j2k J2K codec.
6823 * @param p_manager the user event manager.
6825 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6826 struct opj_stream_private *p_stream,
6827 struct opj_event_mgr * p_manager )
6830 OPJ_UINT32 l_cbd_size;
6831 OPJ_BYTE * l_current_data = 00;
6832 opj_image_t *l_image = 00;
6833 opj_image_comp_t * l_comp = 00;
6836 assert(p_j2k != 00);
6837 assert(p_manager != 00);
6838 assert(p_stream != 00);
6840 l_image = p_j2k->m_private_image;
6841 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6843 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6844 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6845 = (OPJ_BYTE*)opj_realloc(
6846 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6849 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6853 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6856 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6858 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6859 l_current_data += 2;
6861 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6862 l_current_data += 2;
6864 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6867 l_comp = l_image->comps;
6869 for (i=0;i<l_image->numcomps;++i) {
6870 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6876 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) {
6884 * Reads a CBD marker (Component bit depth definition)
6885 * @param p_header_data the data contained in the CBD box.
6886 * @param p_j2k the jpeg2000 codec.
6887 * @param p_header_size the size of the data contained in the CBD marker.
6888 * @param p_manager the user event manager.
6890 opj_bool j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6891 OPJ_BYTE * p_header_data,
6892 OPJ_UINT32 p_header_size,
6893 struct opj_event_mgr * p_manager)
6895 OPJ_UINT32 l_nb_comp,l_num_comp;
6896 OPJ_UINT32 l_comp_def;
6898 opj_image_comp_t * l_comp = 00;
6901 assert(p_header_data != 00);
6902 assert(p_j2k != 00);
6903 assert(p_manager != 00);
6905 l_num_comp = p_j2k->m_private_image->numcomps;
6907 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6908 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6912 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6915 if (l_nb_comp != l_num_comp) {
6916 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6920 l_comp = p_j2k->m_private_image->comps;
6921 for (i=0;i<l_num_comp;++i) {
6922 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6924 l_comp->sgnd = (l_comp_def>>7) & 1;
6925 l_comp->prec = (l_comp_def&0x7f) + 1;
6933 /* ----------------------------------------------------------------------- */
6934 /* J2K / JPT decoder interface */
6935 /* ----------------------------------------------------------------------- */
6939 void j2k_destroy_decompress(opj_j2k_t *j2k) {
6942 if(j2k->tile_len != NULL) {
6943 opj_free(j2k->tile_len);
6945 if(j2k->tile_data != NULL) {
6946 opj_free(j2k->tile_data);
6948 if(j2k->default_tcp != NULL) {
6949 opj_tcp_t *default_tcp = j2k->default_tcp;
6950 if(default_tcp->ppt_data_first != NULL) {
6951 opj_free(default_tcp->ppt_data_first);
6953 if(j2k->default_tcp->tccps != NULL) {
6954 opj_free(j2k->default_tcp->tccps);
6956 opj_free(j2k->default_tcp);
6958 if(j2k->cp != NULL) {
6959 opj_cp_t *cp = j2k->cp;
6960 if(cp->tcps != NULL) {
6961 for(i = 0; i < cp->tw * cp->th; i++) {
6962 if(cp->tcps[i].ppt_data_first != NULL) {
6963 opj_free(cp->tcps[i].ppt_data_first);
6965 if(cp->tcps[i].tccps != NULL) {
6966 opj_free(cp->tcps[i].tccps);
6971 if(cp->ppm_data_first != NULL) {
6972 opj_free(cp->ppm_data_first);
6974 if(cp->tileno != NULL) {
6975 opj_free(cp->tileno);
6977 if(cp->comment != NULL) {
6978 opj_free(cp->comment);
6987 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6989 if(j2k && parameters) {
6990 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6991 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6994 j2k->m_cp.correct = parameters->jpwl_correct;
6995 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6996 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6997 #endif /* USE_JPWL */
7004 * Read a JPT-stream and decode file
7007 opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
7008 opj_image_t *image = NULL;
7009 opj_jpt_msg_header_t header;
7011 opj_common_ptr cinfo = j2k->cinfo;
7013 OPJ_ARG_NOT_USED(cstr_info);
7017 /* create an empty image */
7018 image = opj_image_create0();
7021 j2k->state = J2K_STATE_MHSOC;
7023 /* Initialize the header */
7024 jpt_init_msg_header(&header);
7025 /* Read the first header of the message */
7026 jpt_read_msg_header(cinfo, cio, &header);
7028 position = cio_tell(cio);
7029 if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
7030 opj_image_destroy(image);
7031 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
7036 opj_dec_mstabent_t *e = NULL;
7039 if (!cio_numbytesleft(cio)) {
7043 /* data-bin read -> need to read a new header */
7044 if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
7045 jpt_read_msg_header(cinfo, cio, &header);
7046 position = cio_tell(cio);
7047 if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
7048 opj_image_destroy(image);
7049 opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
7054 id = cio_read(cio, 2);
7055 if (id >> 8 != 0xff) {
7056 opj_image_destroy(image);
7057 opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
7060 e = j2k_dec_mstab_lookup(id);
7061 if (!(j2k->state & e->states)) {
7062 opj_image_destroy(image);
7063 opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
7069 if (j2k->state == J2K_STATE_MT) {
7072 if (j2k->state == J2K_STATE_NEOC) {
7076 if (j2k->state == J2K_STATE_NEOC) {
7080 if (j2k->state != J2K_STATE_MT) {
7081 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
7087 /* ----------------------------------------------------------------------- */
7088 /* J2K encoder interface */
7089 /* ----------------------------------------------------------------------- */
7091 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
7092 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
7099 opj_j2k_v2_t* j2k_create_compress_v2(void)
7101 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
7106 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
7108 l_j2k->m_is_decoder = 0;
7109 l_j2k->m_cp.m_is_decoder = 0;
7111 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
7112 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7117 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
7119 /* validation list creation*/
7120 l_j2k->m_validation_list = opj_procedure_list_create();
7121 if (! l_j2k->m_validation_list) {
7126 /* execution list creation*/
7127 l_j2k->m_procedure_list = opj_procedure_list_create();
7128 if (! l_j2k->m_procedure_list) {
7136 void j2k_destroy_compress(opj_j2k_t *j2k) {
7140 if(j2k->cp != NULL) {
7141 opj_cp_t *cp = j2k->cp;
7144 opj_free(cp->comment);
7147 opj_free(cp->matrice);
7149 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7150 opj_free(cp->tcps[tileno].tccps);
7159 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
7161 int tileno, numpocs_tile;
7162 opj_cp_t *cp = NULL;
7164 if(!j2k || !parameters || ! image) {
7168 /* create and initialize the coding parameters structure */
7169 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
7171 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7174 /* set default values for cp */
7179 copy user encoding parameters
7181 cp->cinema = parameters->cp_cinema;
7182 cp->max_comp_size = parameters->max_comp_size;
7183 cp->rsiz = parameters->cp_rsiz;
7184 cp->disto_alloc = parameters->cp_disto_alloc;
7185 cp->fixed_alloc = parameters->cp_fixed_alloc;
7186 cp->fixed_quality = parameters->cp_fixed_quality;
7188 /* mod fixed_quality */
7189 if(parameters->cp_matrice) {
7190 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
7191 cp->matrice = (int *) opj_malloc(array_size);
7192 memcpy(cp->matrice, parameters->cp_matrice, array_size);
7196 cp->tdx = parameters->cp_tdx;
7197 cp->tdy = parameters->cp_tdy;
7200 cp->tx0 = parameters->cp_tx0;
7201 cp->ty0 = parameters->cp_ty0;
7203 /* comment string */
7204 if(parameters->cp_comment) {
7205 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7207 strcpy(cp->comment, parameters->cp_comment);
7212 calculate other encoding parameters
7215 if (parameters->tile_size_on) {
7216 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7217 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7219 cp->tdx = image->x1 - cp->tx0;
7220 cp->tdy = image->y1 - cp->ty0;
7223 if(parameters->tp_on){
7224 cp->tp_flag = parameters->tp_flag;
7229 for(i=0;i<image->numcomps ;i++){
7230 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
7236 calculate JPWL encoding parameters
7239 if (parameters->jpwl_epc_on) {
7243 cp->epc_on = OPJ_TRUE;
7244 cp->info_on = OPJ_FALSE; /* no informative technique */
7247 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7248 cp->epb_on = OPJ_TRUE;
7250 cp->hprot_MH = parameters->jpwl_hprot_MH;
7251 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7252 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7253 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7255 /* if tile specs are not specified, copy MH specs */
7256 if (cp->hprot_TPH[0] == -1) {
7257 cp->hprot_TPH_tileno[0] = 0;
7258 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7260 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7261 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7262 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7263 cp->pprot[i] = parameters->jpwl_pprot[i];
7267 /* set ESD writing */
7268 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7269 cp->esd_on = OPJ_TRUE;
7271 cp->sens_size = parameters->jpwl_sens_size;
7272 cp->sens_addr = parameters->jpwl_sens_addr;
7273 cp->sens_range = parameters->jpwl_sens_range;
7275 cp->sens_MH = parameters->jpwl_sens_MH;
7276 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7277 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7278 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7282 /* always set RED writing to false: we are at the encoder */
7283 cp->red_on = OPJ_FALSE;
7286 cp->epc_on = OPJ_FALSE;
7288 #endif /* USE_JPWL */
7291 /* initialize the mutiple tiles */
7292 /* ---------------------------- */
7293 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
7295 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7296 opj_tcp_t *tcp = &cp->tcps[tileno];
7297 tcp->numlayers = parameters->tcp_numlayers;
7298 assert ( tcp->numlayers >= 0 );
7299 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
7301 if (cp->fixed_quality) {
7302 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7304 tcp->rates[j] = parameters->tcp_rates[j];
7306 if (cp->fixed_quality) { /* add fixed_quality */
7307 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7309 tcp->rates[j] = parameters->tcp_rates[j];
7313 tcp->csty = parameters->csty;
7314 tcp->prg = parameters->prog_order;
7315 tcp->mct = parameters->tcp_mct;
7319 if (parameters->numpocs) {
7320 /* initialisation of POC */
7322 assert( parameters->numpocs >= 0 );
7323 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
7324 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7325 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7326 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7327 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7328 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7329 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7330 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7331 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7332 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7336 tcp->numpocs = numpocs_tile -1 ;
7341 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7343 for (i = 0; i < image->numcomps; i++) {
7344 opj_tccp_t *tccp = &tcp->tccps[i];
7345 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7346 tccp->numresolutions = parameters->numresolution;
7347 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7348 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7349 tccp->cblksty = parameters->mode;
7350 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7351 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7353 assert(parameters->roi_compno >= 0);
7354 if (i == (OPJ_UINT32)parameters->roi_compno) {
7355 tccp->roishift = parameters->roi_shift;
7360 if(parameters->cp_cinema)
7362 /*Precinct size for lowest frequency subband=128*/
7365 /*Precinct size at all other resolutions = 256*/
7366 for (j = 1; j < tccp->numresolutions; j++) {
7371 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7373 assert(tccp->numresolutions > 0);
7374 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
7375 if (p < parameters->res_spec) {
7377 if (parameters->prcw_init[p] < 1) {
7380 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
7383 if (parameters->prch_init[p] < 1) {
7386 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
7390 int res_spec = parameters->res_spec;
7391 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7392 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7394 if (size_prcw < 1) {
7397 tccp->prcw[j] = int_floorlog2(size_prcw);
7400 if (size_prch < 1) {
7403 tccp->prch[j] = int_floorlog2(size_prch);
7407 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
7410 for (j = 0; j < tccp->numresolutions; j++) {
7417 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7422 void j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
7423 opj_cparameters_t *parameters,
7425 struct opj_event_mgr * p_manager)
7427 OPJ_UINT32 i, j, tileno, numpocs_tile;
7428 opj_cp_v2_t *cp = 00;
7431 if(!p_j2k || !parameters || ! image) {
7435 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
7436 cp = &(p_j2k->m_cp);
7438 /* set default values for cp */
7443 copy user encoding parameters
7445 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
7446 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
7447 cp->rsiz = parameters->cp_rsiz;
7448 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
7449 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
7450 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
7452 /* mod fixed_quality */
7453 if (parameters->cp_matrice) {
7454 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
7455 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
7456 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
7460 cp->tdx = parameters->cp_tdx;
7461 cp->tdy = parameters->cp_tdy;
7464 cp->tx0 = parameters->cp_tx0;
7465 cp->ty0 = parameters->cp_ty0;
7467 /* comment string */
7468 if(parameters->cp_comment) {
7469 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
7471 strcpy(cp->comment, parameters->cp_comment);
7476 calculate other encoding parameters
7479 if (parameters->tile_size_on) {
7480 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
7481 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
7483 cp->tdx = image->x1 - cp->tx0;
7484 cp->tdy = image->y1 - cp->ty0;
7487 if (parameters->tp_on) {
7488 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
7489 cp->m_specific_param.m_enc.m_tp_on = 1;
7494 calculate JPWL encoding parameters
7497 if (parameters->jpwl_epc_on) {
7501 cp->epc_on = OPJ_TRUE;
7502 cp->info_on = OPJ_FALSE; /* no informative technique */
7505 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
7506 cp->epb_on = OPJ_TRUE;
7508 cp->hprot_MH = parameters->jpwl_hprot_MH;
7509 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7510 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
7511 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
7513 /* if tile specs are not specified, copy MH specs */
7514 if (cp->hprot_TPH[0] == -1) {
7515 cp->hprot_TPH_tileno[0] = 0;
7516 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
7518 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
7519 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
7520 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
7521 cp->pprot[i] = parameters->jpwl_pprot[i];
7525 /* set ESD writing */
7526 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
7527 cp->esd_on = OPJ_TRUE;
7529 cp->sens_size = parameters->jpwl_sens_size;
7530 cp->sens_addr = parameters->jpwl_sens_addr;
7531 cp->sens_range = parameters->jpwl_sens_range;
7533 cp->sens_MH = parameters->jpwl_sens_MH;
7534 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
7535 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
7536 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7540 /* always set RED writing to false: we are at the encoder */
7541 cp->red_on = OPJ_FALSE;
7544 cp->epc_on = OPJ_FALSE;
7546 #endif /* USE_JPWL */
7549 /* initialize the mutiple tiles */
7550 /* ---------------------------- */
7551 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7552 if (parameters->numpocs) {
7553 /* initialisation of POC */
7554 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7558 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7559 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7560 tcp->numlayers = parameters->tcp_numlayers;
7562 for (j = 0; j < tcp->numlayers; j++) {
7563 if(cp->m_specific_param.m_enc.m_cinema){
7564 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7565 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7567 tcp->rates[j] = parameters->tcp_rates[j];
7569 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7570 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7572 tcp->rates[j] = parameters->tcp_rates[j];
7577 tcp->csty = parameters->csty;
7578 tcp->prg = parameters->prog_order;
7579 tcp->mct = parameters->tcp_mct;
7584 if (parameters->numpocs) {
7585 /* initialisation of POC */
7588 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7589 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7590 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7592 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7593 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7594 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7595 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7596 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7597 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7598 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7604 tcp->numpocs = numpocs_tile -1 ;
7609 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7611 if (parameters->mct_data) {
7613 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7616 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7617 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7618 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7619 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7622 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7623 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7624 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7626 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7627 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7629 tcp->mct_norms = (OPJ_FLOAT64*)
7630 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7632 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7635 for (i = 0; i < image->numcomps; i++) {
7636 opj_tccp_t *tccp = &tcp->tccps[i];
7637 tccp->m_dc_level_shift = l_dc_shift[i];
7640 j2k_setup_mct_encoding(tcp,image);
7644 for (i = 0; i < image->numcomps; i++) {
7645 opj_tccp_t *tccp = &tcp->tccps[i];
7646 opj_image_comp_t * l_comp = &(image->comps[i]);
7648 if (! l_comp->sgnd) {
7649 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7654 for (i = 0; i < image->numcomps; i++) {
7655 opj_tccp_t *tccp = &tcp->tccps[i];
7657 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7658 tccp->numresolutions = parameters->numresolution;
7659 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7660 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7661 tccp->cblksty = parameters->mode;
7662 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7663 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7666 if (i == parameters->roi_compno) {
7667 tccp->roishift = parameters->roi_shift;
7672 if(parameters->cp_cinema) {
7673 //Precinct size for lowest frequency subband=128
7676 //Precinct size at all other resolutions = 256
7677 for (j = 1; j < tccp->numresolutions; j++) {
7682 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7683 OPJ_INT32 p = 0, it_res;
7684 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7685 if (p < parameters->res_spec) {
7687 if (parameters->prcw_init[p] < 1) {
7688 tccp->prcw[it_res] = 1;
7690 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7693 if (parameters->prch_init[p] < 1) {
7694 tccp->prch[it_res] = 1;
7696 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7700 int res_spec = parameters->res_spec;
7701 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7702 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7704 if (size_prcw < 1) {
7705 tccp->prcw[it_res] = 1;
7707 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7710 if (size_prch < 1) {
7711 tccp->prch[it_res] = 1;
7713 tccp->prch[it_res] = int_floorlog2(size_prch);
7717 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7720 for (j = 0; j < tccp->numresolutions; j++) {
7727 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7731 if (parameters->mct_data) {
7732 opj_free(parameters->mct_data);
7733 parameters->mct_data = 00;
7738 opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
7741 opj_cp_t *cp = NULL;
7743 opj_tcd_t *tcd = NULL; /* TCD component */
7751 j2k->cstr_info = cstr_info;
7754 cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
7755 cstr_info->image_w = image->x1 - image->x0;
7756 cstr_info->image_h = image->y1 - image->y0;
7757 cstr_info->prog = (&cp->tcps[0])->prg;
7758 cstr_info->tw = cp->tw;
7759 cstr_info->th = cp->th;
7760 cstr_info->tile_x = cp->tdx; /* new version parser */
7761 cstr_info->tile_y = cp->tdy; /* new version parser */
7762 cstr_info->tile_Ox = cp->tx0; /* new version parser */
7763 cstr_info->tile_Oy = cp->ty0; /* new version parser */
7764 cstr_info->numcomps = image->numcomps;
7765 cstr_info->numlayers = (&cp->tcps[0])->numlayers;
7766 cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
7767 for (compno=0; compno < image->numcomps; compno++) {
7768 cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
7770 cstr_info->D_max = 0.0; /* ADD Marcela */
7771 cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
7772 cstr_info->maxmarknum = 100;
7773 cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
7774 cstr_info->marknum = 0;
7784 for (compno = 1; compno < image->numcomps; compno++) {
7785 j2k_write_coc(j2k, compno);
7786 j2k_write_qcc(j2k, compno);
7790 for (compno = 0; compno < image->numcomps; compno++) {
7791 opj_tcp_t *tcp = &cp->tcps[0];
7792 if (tcp->tccps[compno].roishift)
7793 j2k_write_rgn(j2k, compno, 0);
7795 if (cp->comment != NULL) {
7799 j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
7803 if (cp->cinema == CINEMA4K_24) {
7808 /* uncomment only for testing JPSEC marker writing */
7809 /* j2k_write_sec(j2k); */
7813 cstr_info->main_head_end = cio_tell(cio) - 1;
7816 /**** Main Header ENDS here ***/
7818 /* create the tile encoder */
7819 tcd = tcd_create(j2k->cinfo);
7821 /* encode each tile */
7822 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7826 int acc_pack_num = 0;
7830 opj_tcp_t *tcp = &cp->tcps[tileno];
7831 opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
7833 j2k->curtileno = tileno;
7834 j2k->cur_tp_num = 0;
7835 tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
7836 /* initialisation before tile encoding */
7838 tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
7840 tcd_init_encode(tcd, image, cp, j2k->curtileno);
7845 cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
7846 cstr_info->tile[j2k->curtileno].maxmarknum = 10;
7847 cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
7848 cstr_info->tile[j2k->curtileno].marknum = 0;
7852 for(pino = 0; pino <= tcp->numpocs; pino++) {
7856 /*Get number of tile parts*/
7857 tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
7858 tcd->tp_pos = cp->tp_pos;
7860 for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
7861 j2k->tp_num = tilepartno;
7864 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
7865 cio_tell(cio) + j2k->pos_correction;
7869 if(j2k->cur_tp_num == 0 && cp->cinema == 0){
7870 for (compno = 1; compno < image->numcomps; compno++) {
7871 j2k_write_coc(j2k, compno);
7872 j2k_write_qcc(j2k, compno);
7874 if (cp->tcps[tileno].numpocs) {
7881 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
7882 cio_tell(cio) + j2k->pos_correction + 1;
7885 j2k_write_sod(j2k, tcd);
7889 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
7890 cio_tell(cio) + j2k->pos_correction - 1;
7891 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
7893 cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
7894 cstr_info->packno - acc_pack_num;
7895 acc_pack_num = cstr_info->packno;
7903 cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
7908 if (tile->PPT) { // BAD PPT !!!
7911 PPT_file=fopen("PPT","rb");
7912 fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
7913 for (i=0;i<tile->len_ppt;i++) {
7915 fread(&elmt, 1, 1, PPT_file);
7916 fwrite(&elmt,1,1,f);
7925 /* destroy the tile encoder */
7926 tcd_free_encode(tcd);
7929 opj_free(j2k->cur_totnum_tp);
7934 cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
7936 /* The following adjustment is done to adjust the codestream size */
7937 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
7938 /* the first bunch of bytes is not in the codestream */
7939 cstr_info->codestream_size -= cstr_info->main_head_start;
7945 preparation of JPWL marker segments
7949 /* encode according to JPWL */
7950 jpwl_encode(j2k, cio, image);
7953 #endif /* USE_JPWL */
7958 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7960 assert(cstr_info != 00);
7962 /* expand the list? */
7963 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
7964 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
7965 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
7968 /* add the marker */
7969 cstr_info->marker[cstr_info->marknum].type = type;
7970 cstr_info->marker[cstr_info->marknum].pos = pos;
7971 cstr_info->marker[cstr_info->marknum].len = len;
7972 cstr_info->marknum++;
7976 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7978 assert(cstr_index != 00);
7980 /* expand the list? */
7981 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
7982 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
7983 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
7986 /* add the marker */
7987 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
7988 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
7989 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
7990 cstr_index->marknum++;
7994 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7996 opj_marker_info_t *marker;
7998 assert(cstr_info != 00);
8000 /* expand the list? */
8001 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
8002 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
8003 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
8006 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
8008 /* add the marker */
8009 marker->type = type;
8012 cstr_info->tile[tileno].marknum++;
8015 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)
8017 assert(cstr_index != 00);
8018 assert(cstr_index->tile_index != 00);
8020 /* expand the list? */
8021 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
8022 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
8023 cstr_index->tile_index[tileno].marker =
8024 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
8025 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
8028 /* add the marker */
8029 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
8030 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
8031 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
8032 cstr_index->tile_index[tileno].marknum++;
8034 if (type == J2K_MS_SOT) {
8035 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
8037 if (cstr_index->tile_index[tileno].tp_index)
8038 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
8045 * -----------------------------------------------------------------------
8046 * -----------------------------------------------------------------------
8047 * -----------------------------------------------------------------------
8051 * Ends the decompression procedures and possibiliy add data to be read after the
8054 opj_bool j2k_end_decompress(
8055 opj_j2k_v2_t *p_j2k,
8056 opj_stream_private_t *p_stream,
8057 opj_event_mgr_t * p_manager)
8066 * Reads a jpeg2000 codestream header structure.
8069 * @param p_stream the stream to read data from.
8070 * @param p_j2k the jpeg2000 codec.
8071 * @param p_manager the user event manager.
8073 * @return true if the box is valid.
8075 opj_bool j2k_read_header( struct opj_stream_private *p_stream,
8076 opj_j2k_v2_t* p_j2k,
8077 opj_image_t** p_image,
8078 struct opj_event_mgr* p_manager )
8081 assert(p_j2k != 00);
8082 assert(p_stream != 00);
8083 assert(p_manager != 00);
8085 /* create an empty image header */
8086 p_j2k->m_private_image = opj_image_create0();
8087 if (! p_j2k->m_private_image) {
8091 /* customization of the validation */
8092 j2k_setup_decoding_validation(p_j2k);
8094 /* validation of the parameters codec */
8095 if (! j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
8096 opj_image_destroy(p_j2k->m_private_image);
8097 p_j2k->m_private_image = NULL;
8101 /* customization of the encoding */
8102 j2k_setup_header_reading(p_j2k);
8105 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
8106 opj_image_destroy(p_j2k->m_private_image);
8107 p_j2k->m_private_image = NULL;
8111 *p_image = opj_image_create0();
8116 /* Copy codestream image information to the output image */
8117 opj_copy_image_header(p_j2k->m_private_image, *p_image);
8119 /*Allocate and initialize some elements of codestrem index*/
8120 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
8128 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
8130 void j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
8133 assert(p_j2k != 00);
8135 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_read_header_procedure);
8137 /* DEVELOPER CORNER, add your custom procedures */
8138 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_copy_default_tcp_and_create_tcd);
8143 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
8144 * are valid. Developpers wanting to extend the library can add their own validation procedures.
8146 void j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
8149 assert(p_j2k != 00);
8151 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_decoder);
8152 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_decoding_validation);
8153 /* DEVELOPER CORNER, add your custom validation procedure */
8159 * The mct encoding validation procedure.
8161 * @param p_j2k the jpeg2000 codec to validate.
8162 * @param p_stream the input stream to validate.
8163 * @param p_manager the user event manager.
8165 * @return true if the parameters are correct.
8167 opj_bool j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
8168 opj_stream_private_t *p_stream,
8169 opj_event_mgr_t * p_manager )
8171 opj_bool l_is_valid = OPJ_TRUE;
8175 assert(p_j2k != 00);
8176 assert(p_stream != 00);
8177 assert(p_manager != 00);
8179 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
8180 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8181 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
8183 for (i=0;i<l_nb_tiles;++i) {
8184 if (l_tcp->mct == 2) {
8185 opj_tccp_t * l_tccp = l_tcp->tccps;
8186 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
8188 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
8189 l_is_valid &= ! (l_tccp->qmfbid & 1);
8200 opj_bool j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
8203 OPJ_UINT32 l_indix = 1;
8204 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
8205 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
8206 OPJ_UINT32 l_mct_size,l_nb_elem;
8207 OPJ_FLOAT32 * l_data, * l_current_data;
8208 opj_tccp_t * l_tccp;
8211 assert(p_tcp != 00);
8213 if (p_tcp->mct != 2) {
8217 if (p_tcp->m_mct_decoding_matrix) {
8218 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8219 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8221 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));
8222 if (! p_tcp->m_mct_records) {
8225 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8227 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8229 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8231 if (l_mct_deco_data->m_data) {
8232 opj_free(l_mct_deco_data->m_data);
8233 l_mct_deco_data->m_data = 00;
8236 l_mct_deco_data->m_index = l_indix++;
8237 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
8238 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
8239 l_nb_elem = p_image->numcomps * p_image->numcomps;
8240 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
8241 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8243 if (! l_mct_deco_data->m_data) {
8247 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);
8249 l_mct_deco_data->m_data_size = l_mct_size;
8250 ++p_tcp->m_nb_mct_records;
8253 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
8254 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
8255 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));
8257 if (! p_tcp->m_mct_records) {
8261 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8262 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
8264 if (l_mct_deco_data) {
8265 l_mct_deco_data = l_mct_offset_data - 1;
8269 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
8271 if (l_mct_offset_data->m_data) {
8272 opj_free(l_mct_offset_data->m_data);
8273 l_mct_offset_data->m_data = 00;
8276 l_mct_offset_data->m_index = l_indix++;
8277 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
8278 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
8279 l_nb_elem = p_image->numcomps;
8280 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
8281 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
8283 if (! l_mct_offset_data->m_data) {
8287 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
8289 opj_free(l_mct_offset_data->m_data);
8290 l_mct_offset_data->m_data = 00;
8294 l_tccp = p_tcp->tccps;
8295 l_current_data = l_data;
8297 for (i=0;i<l_nb_elem;++i) {
8298 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
8302 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
8306 l_mct_offset_data->m_data_size = l_mct_size;
8308 ++p_tcp->m_nb_mct_records;
8310 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
8311 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
8312 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
8313 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
8315 if (! p_tcp->m_mcc_records) {
8318 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8319 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));
8323 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
8324 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
8325 l_mcc_data->m_is_irreversible = 1;
8326 l_mcc_data->m_nb_comps = p_image->numcomps;
8327 l_mcc_data->m_index = l_indix++;
8328 l_mcc_data->m_offset_array = l_mct_offset_data;
8329 ++p_tcp->m_nb_mcc_records;
8335 * Builds the cp decoder parameters to use to decode tile.
8337 opj_bool j2k_build_decoder (opj_j2k_v2_t * p_j2k,
8338 opj_stream_private_t *p_stream,
8339 opj_event_mgr_t * p_manager )
8341 /* add here initialization of cp
8342 copy paste of setup_decoder */
8350 * Builds the cp encoder parameters to use to encode tile.
8352 opj_bool j2k_build_encoder (opj_j2k_v2_t * p_j2k,
8353 opj_stream_private_t *p_stream,
8354 opj_event_mgr_t * p_manager )
8356 /* add here initialization of cp
8357 copy paste of setup_encoder */
8365 * The default encoding validation procedure without any extension.
8367 * @param p_j2k the jpeg2000 codec to validate.
8368 * @param p_stream the input stream to validate.
8369 * @param p_manager the user event manager.
8371 * @return true if the parameters are correct.
8373 opj_bool j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
8374 opj_stream_private_t *p_stream,
8375 opj_event_mgr_t * p_manager )
8377 opj_bool l_is_valid = OPJ_TRUE;
8380 assert(p_j2k != 00);
8381 assert(p_stream != 00);
8382 assert(p_manager != 00);
8384 /* STATE checking */
8385 /* make sure the state is at 0 */
8386 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
8388 /* POINTER validation */
8389 /* make sure a p_j2k codec is present */
8390 l_is_valid &= (p_j2k->m_procedure_list != 00);
8391 /* make sure a validation list is present */
8392 l_is_valid &= (p_j2k->m_validation_list != 00);
8394 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8395 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8399 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
8400 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
8404 /* PARAMETER VALIDATION */
8409 * The default decoding validation procedure without any extension.
8411 * @param p_j2k the jpeg2000 codec to validate.
8412 * @param p_stream the input stream to validate.
8413 * @param p_manager the user event manager.
8415 * @return true if the parameters are correct.
8417 opj_bool j2k_decoding_validation (
8418 opj_j2k_v2_t *p_j2k,
8419 opj_stream_private_t *p_stream,
8420 opj_event_mgr_t * p_manager
8423 opj_bool l_is_valid = OPJ_TRUE;
8426 assert(p_j2k != 00);
8427 assert(p_stream != 00);
8428 assert(p_manager != 00);
8431 /* STATE checking */
8432 /* make sure the state is at 0 */
8434 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
8436 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
8438 /* POINTER validation */
8439 /* make sure a p_j2k codec is present */
8440 /* make sure a procedure list is present */
8441 l_is_valid &= (p_j2k->m_procedure_list != 00);
8442 /* make sure a validation list is present */
8443 l_is_valid &= (p_j2k->m_validation_list != 00);
8445 /* PARAMETER VALIDATION */
8449 opj_bool j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
8450 struct opj_stream_private *p_stream,
8451 struct opj_event_mgr * p_manager)
8453 OPJ_UINT32 l_current_marker;
8454 OPJ_UINT32 l_marker_size;
8455 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8458 assert(p_stream != 00);
8459 assert(p_j2k != 00);
8460 assert(p_manager != 00);
8462 /* We enter in the main header */
8463 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
8465 /* Try to read the SOC marker, the codestream must begin with SOC marker */
8466 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
8467 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
8471 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8472 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8473 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8477 /* Read 2 bytes as the new marker ID */
8478 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8480 /* Try to read until the SOT is detected */
8481 while (l_current_marker != J2K_MS_SOT) {
8483 /* Check if the current marker ID is valid */
8484 if (l_current_marker < 0xff00) {
8485 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
8489 /* Get the marker handler from the marker ID */
8490 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8492 /* Manage case where marker is unknown */
8493 if (l_marker_handler->id == J2K_MS_UNK) {
8494 if (! j2k_read_unk_v2(p_j2k, p_stream, &l_current_marker, p_manager)){
8495 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
8499 if (l_current_marker == J2K_MS_SOT)
8500 break; /* SOT marker is detected main header is completely read */
8501 else /* Get the marker handler from the marker ID */
8502 l_marker_handler = j2k_get_marker_handler(l_current_marker);
8505 /* Check if the marker is known and if it is the right place to find it */
8506 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8507 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8511 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8512 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8513 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8517 /* read 2 bytes as the marker size */
8518 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8519 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8521 /* Check if the marker size is compatible with the header data size */
8522 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8523 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8524 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8525 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8528 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8531 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8532 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) {
8533 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8537 /* Read the marker segment with the correct marker handler */
8538 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8539 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
8543 /* Add the marker to the codestream index*/
8544 j2k_add_mhmarker_v2(p_j2k->cstr_index,
8545 l_marker_handler->id,
8546 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8547 l_marker_size + 4 );
8549 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8550 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8551 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8555 /* read 2 bytes as the new marker ID */
8556 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8559 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
8561 /* Position of the last element if the main header */
8562 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
8564 /* Next step: read a tile-part header */
8565 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8571 * Excutes the given procedures on the given codec.
8573 * @param p_procedure_list the list of procedures to execute
8574 * @param p_j2k the jpeg2000 codec to execute the procedures on.
8575 * @param p_stream the stream to execute the procedures on.
8576 * @param p_manager the user manager.
8578 * @return true if all the procedures were successfully executed.
8580 opj_bool j2k_exec ( opj_j2k_v2_t * p_j2k,
8581 opj_procedure_list_t * p_procedure_list,
8582 opj_stream_private_t *p_stream,
8583 opj_event_mgr_t * p_manager )
8585 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
8586 opj_bool l_result = OPJ_TRUE;
8587 OPJ_UINT32 l_nb_proc, i;
8590 assert(p_procedure_list != 00);
8591 assert(p_j2k != 00);
8592 assert(p_stream != 00);
8593 assert(p_manager != 00);
8596 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
8597 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);
8599 for (i=0;i<l_nb_proc;++i) {
8600 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
8604 /* and clear the procedure list at the end.*/
8605 opj_procedure_list_clear(p_procedure_list);
8610 opj_bool j2k_copy_default_tcp_and_create_tcd
8612 opj_j2k_v2_t * p_j2k,
8613 opj_stream_private_t *p_stream,
8614 opj_event_mgr_t * p_manager
8617 opj_tcp_v2_t * l_tcp = 00;
8618 opj_tcp_v2_t * l_default_tcp = 00;
8619 OPJ_UINT32 l_nb_tiles;
8621 opj_tccp_t *l_current_tccp = 00;
8622 OPJ_UINT32 l_tccp_size;
8623 OPJ_UINT32 l_mct_size;
8624 opj_image_t * l_image;
8625 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
8626 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
8627 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
8628 OPJ_UINT32 l_offset;
8631 assert(p_j2k != 00);
8632 assert(p_stream != 00);
8633 assert(p_manager != 00);
8635 l_image = p_j2k->m_private_image;
8636 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8637 l_tcp = p_j2k->m_cp.tcps;
8638 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
8639 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
8640 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
8643 for (i=0; i<l_nb_tiles; ++i) {
8644 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
8645 l_current_tccp = l_tcp->tccps;
8646 /*Copy default coding parameters into the current tile coding parameters*/
8647 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
8648 /* Initialize some values of the current tile coding parameters*/
8650 l_tcp->ppt_data = 00;
8651 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
8652 l_tcp->tccps = l_current_tccp;
8654 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
8655 if (l_default_tcp->m_mct_decoding_matrix) {
8656 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
8657 if (! l_tcp->m_mct_decoding_matrix ) {
8660 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
8663 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
8664 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
8665 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
8666 if (! l_tcp->m_mct_records) {
8669 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
8671 /* Copy the mct record data from dflt_tile_cp to the current tile*/
8672 l_src_mct_rec = l_default_tcp->m_mct_records;
8673 l_dest_mct_rec = l_tcp->m_mct_records;
8675 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
8677 if (l_src_mct_rec->m_data) {
8679 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
8680 if(! l_dest_mct_rec->m_data) {
8683 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
8690 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
8691 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
8692 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
8693 if (! l_tcp->m_mcc_records) {
8696 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
8698 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
8699 l_src_mcc_rec = l_default_tcp->m_mcc_records;
8700 l_dest_mcc_rec = l_tcp->m_mcc_records;
8702 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
8704 if (l_src_mcc_rec->m_decorrelation_array) {
8705 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
8706 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
8709 if (l_src_mcc_rec->m_offset_array) {
8710 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
8711 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
8718 /* Copy all the dflt_tile_compo_cp to the current tile cp */
8719 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
8721 /* Move to next tile cp*/
8725 /* Create the current tile decoder*/
8726 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
8727 if (! p_j2k->m_tcd ) {
8731 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
8732 tcd_destroy_v2(p_j2k->m_tcd);
8734 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8742 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
8743 * with the marker value.
8744 * @param p_id Marker value to look up
8746 * @return the handler associated with the id.
8748 const opj_dec_memory_marker_handler_t * j2k_get_marker_handler (OPJ_UINT32 p_id)
8750 const opj_dec_memory_marker_handler_t *e;
8751 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
8752 if (e->id == p_id) {
8753 break; /* we find a handler corresponding to the marker ID*/
8761 * Destroys a jpeg2000 codec.
8763 * @param p_j2k the jpeg20000 structure to destroy.
8765 void j2k_destroy (opj_j2k_v2_t *p_j2k)
8771 if (p_j2k->m_is_decoder) {
8773 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8774 j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8775 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8776 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8779 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8780 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8781 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8782 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8787 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8788 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8789 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8792 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8793 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8794 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8795 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8798 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8799 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8800 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8801 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8805 tcd_destroy_v2(p_j2k->m_tcd);
8807 j2k_cp_destroy(&(p_j2k->m_cp));
8808 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8810 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8811 p_j2k->m_procedure_list = 00;
8813 opj_procedure_list_destroy(p_j2k->m_validation_list);
8814 p_j2k->m_procedure_list = 00;
8816 j2k_destroy_cstr_index(p_j2k->cstr_index);
8817 p_j2k->cstr_index = NULL;
8819 opj_image_destroy(p_j2k->m_private_image);
8820 p_j2k->m_private_image = NULL;
8822 opj_image_destroy(p_j2k->m_output_image);
8823 p_j2k->m_output_image = NULL;
8828 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8832 if (p_cstr_ind->marker) {
8833 opj_free(p_cstr_ind->marker);
8834 p_cstr_ind->marker = NULL;
8837 if (p_cstr_ind->tile_index) {
8838 OPJ_UINT32 it_tile = 0;
8840 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8842 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8843 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8844 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8847 if(p_cstr_ind->tile_index[it_tile].tp_index){
8848 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8849 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8852 if(p_cstr_ind->tile_index[it_tile].marker){
8853 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8854 p_cstr_ind->tile_index[it_tile].marker = NULL;
8859 opj_free( p_cstr_ind->tile_index);
8860 p_cstr_ind->tile_index = NULL;
8863 opj_free(p_cstr_ind);
8870 * Destroys a tile coding parameter structure.
8872 * @param p_tcp the tile coding parameter to destroy.
8874 void j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8880 if (p_tcp->ppt_buffer != 00) {
8881 opj_free(p_tcp->ppt_buffer);
8882 p_tcp->ppt_buffer = 00;
8885 if (p_tcp->tccps != 00) {
8886 opj_free(p_tcp->tccps);
8890 if (p_tcp->m_mct_coding_matrix != 00) {
8891 opj_free(p_tcp->m_mct_coding_matrix);
8892 p_tcp->m_mct_coding_matrix = 00;
8895 if (p_tcp->m_mct_decoding_matrix != 00) {
8896 opj_free(p_tcp->m_mct_decoding_matrix);
8897 p_tcp->m_mct_decoding_matrix = 00;
8900 if (p_tcp->m_mcc_records) {
8901 opj_free(p_tcp->m_mcc_records);
8902 p_tcp->m_mcc_records = 00;
8903 p_tcp->m_nb_max_mcc_records = 0;
8904 p_tcp->m_nb_mcc_records = 0;
8907 if (p_tcp->m_mct_records) {
8908 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8911 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8912 if (l_mct_data->m_data) {
8913 opj_free(l_mct_data->m_data);
8914 l_mct_data->m_data = 00;
8920 opj_free(p_tcp->m_mct_records);
8921 p_tcp->m_mct_records = 00;
8924 if (p_tcp->mct_norms != 00) {
8925 opj_free(p_tcp->mct_norms);
8926 p_tcp->mct_norms = 00;
8929 j2k_tcp_data_destroy(p_tcp);
8934 * Destroys the data inside a tile coding parameter structure.
8936 * @param p_tcp the tile coding parameter which contain data to destroy.
8938 void j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
8940 if (p_tcp->m_data) {
8941 opj_free(p_tcp->m_data);
8942 p_tcp->m_data = NULL;
8943 p_tcp->m_data_size = 0;
8948 * Destroys a coding parameter structure.
8950 * @param p_cp the coding parameter to destroy.
8952 void j2k_cp_destroy (opj_cp_v2_t *p_cp)
8954 OPJ_UINT32 l_nb_tiles;
8955 opj_tcp_v2_t * l_current_tile = 00;
8966 l_current_tile = p_cp->tcps;
8967 l_nb_tiles = p_cp->th * p_cp->tw;
8970 (i = 0; i < l_nb_tiles; ++i)
8972 j2k_tcp_destroy(l_current_tile);
8975 opj_free(p_cp->tcps);
8979 (p_cp->ppm_buffer != 00)
8981 opj_free(p_cp->ppm_buffer);
8982 p_cp->ppm_buffer = 00;
8985 (p_cp->comment != 00)
8987 opj_free(p_cp->comment);
8991 (! p_cp->m_is_decoder)
8994 (p_cp->m_specific_param.m_enc.m_matrice)
8996 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8997 p_cp->m_specific_param.m_enc.m_matrice = 00;
9005 * Reads a tile header.
9006 * @param p_j2k the jpeg2000 codec.
9007 * @param p_stream the stream to write data to.
9008 * @param p_manager the user event manager.
9010 opj_bool j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
9011 OPJ_UINT32 * p_tile_index,
9012 OPJ_UINT32 * p_data_size,
9013 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
9014 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
9015 OPJ_UINT32 * p_nb_comps,
9017 opj_stream_private_t *p_stream,
9018 opj_event_mgr_t * p_manager )
9020 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
9021 OPJ_UINT32 l_marker_size;
9022 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
9023 opj_tcp_v2_t * l_tcp = NULL;
9024 OPJ_UINT32 l_nb_tiles;
9027 assert(p_stream != 00);
9028 assert(p_j2k != 00);
9029 assert(p_manager != 00);
9031 /* Reach the End Of Codestream ?*/
9032 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
9033 l_current_marker = J2K_MS_EOC;
9035 /* We need to encounter a SOT marker (a new tile-part header) */
9036 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
9040 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
9041 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
9043 /* Try to read until the Start Of Data is detected */
9044 while (l_current_marker != J2K_MS_SOD) {
9046 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
9047 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9048 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9052 /* Read 2 bytes from the buffer as the marker size */
9053 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
9055 /* Why this condition? FIXME */
9056 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
9057 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
9059 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
9061 /* Get the marker handler from the marker ID */
9062 l_marker_handler = j2k_get_marker_handler(l_current_marker);
9064 /* Check if the marker is known and if it is the right place to find it */
9065 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
9066 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
9069 /* FIXME manage case of unknown marker as in the main header ? */
9071 /* Check if the marker size is compatible with the header data size */
9072 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
9073 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
9074 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
9075 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
9078 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
9081 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
9082 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) {
9083 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9087 /* Read the marker segment with the correct marker handler */
9088 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
9089 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
9093 /* Add the marker to the codestream index*/
9094 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
9096 l_marker_handler->id,
9097 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
9098 l_marker_size + 4 );
9100 /* Keep the position of the last SOT marker read */
9101 if ( l_marker_handler->id == J2K_MS_SOT ) {
9102 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
9103 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
9105 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
9110 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
9111 /* Skip the rest of the tile part header*/
9112 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) {
9113 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9116 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
9119 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
9120 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9121 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9124 /* Read 2 bytes from the buffer as the new marker ID */
9125 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9129 /* If we didn't skip data before, we need to read the SOD marker*/
9130 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
9131 /* Try to read the SOD marker and skip data ? FIXME */
9132 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
9138 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
9139 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9140 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9141 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9145 /* Read 2 bytes from buffer as the new marker ID */
9146 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9150 /* Indicate we will try to read a new tile-part header*/
9151 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
9152 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9153 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9155 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
9156 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
9157 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9161 /* Read 2 bytes from buffer as the new marker ID */
9162 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
9166 /* Current marker is the EOC marker ?*/
9167 if (l_current_marker == J2K_MS_EOC) {
9168 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
9169 p_j2k->m_current_tile_number = 0;
9170 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
9175 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
9176 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
9177 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
9179 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
9180 ++p_j2k->m_current_tile_number;
9184 if (p_j2k->m_current_tile_number == l_nb_tiles) {
9185 *p_go_on = OPJ_FALSE;
9191 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
9192 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
9196 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
9197 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
9199 *p_tile_index = p_j2k->m_current_tile_number;
9200 *p_go_on = OPJ_TRUE;
9201 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
9202 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
9203 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
9204 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
9205 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
9206 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
9208 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
9214 opj_bool j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
9215 OPJ_UINT32 p_tile_index,
9217 OPJ_UINT32 p_data_size,
9218 opj_stream_private_t *p_stream,
9219 opj_event_mgr_t * p_manager )
9221 OPJ_UINT32 l_current_marker;
9222 OPJ_BYTE l_data [2];
9223 opj_tcp_v2_t * l_tcp;
9226 assert(p_stream != 00);
9227 assert(p_j2k != 00);
9228 assert(p_manager != 00);
9230 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
9231 || (p_tile_index != p_j2k->m_current_tile_number) ) {
9235 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
9236 if (! l_tcp->m_data) {
9237 j2k_tcp_destroy(l_tcp);
9241 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
9245 p_j2k->cstr_index) ) {
9246 j2k_tcp_destroy(l_tcp);
9247 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
9251 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
9255 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
9256 * we destroy just the data which will be re-read in read_tile_header*/
9257 /*j2k_tcp_destroy(l_tcp);
9258 p_j2k->m_tcd->tcp = 0;*/
9259 j2k_tcp_data_destroy(l_tcp);
9261 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
9262 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
9264 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
9265 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
9266 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
9270 opj_read_bytes(l_data,&l_current_marker,2);
9272 if (l_current_marker == J2K_MS_EOC) {
9273 p_j2k->m_current_tile_number = 0;
9274 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
9276 else if (l_current_marker != J2K_MS_SOT)
9278 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
9287 opj_bool j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
9289 OPJ_UINT32 i,j,k = 0;
9290 OPJ_UINT32 l_width_src,l_height_src;
9291 OPJ_UINT32 l_width_dest,l_height_dest;
9292 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
9293 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
9294 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
9295 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
9296 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
9298 opj_image_comp_t * l_img_comp_src = 00;
9299 opj_image_comp_t * l_img_comp_dest = 00;
9301 opj_tcd_tilecomp_v2_t * l_tilec = 00;
9302 opj_image_t * l_image_src = 00;
9303 OPJ_UINT32 l_size_comp, l_remaining;
9304 OPJ_INT32 * l_dest_ptr;
9305 opj_tcd_resolution_v2_t* l_res= 00;
9307 l_tilec = p_tcd->tcd_image->tiles->comps;
9308 l_image_src = p_tcd->image;
9309 l_img_comp_src = l_image_src->comps;
9311 l_img_comp_dest = p_output_image->comps;
9313 for (i=0; i<l_image_src->numcomps; i++) {
9315 /* Allocate output component buffer if necessary */
9316 if (!l_img_comp_dest->data) {
9318 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
9319 if (! l_img_comp_dest->data) {
9324 /* Copy info from decoded comp image to output image */
9325 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
9328 /* Compute the precision of the output buffer */
9329 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
9330 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
9331 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
9337 if (l_size_comp == 3) {
9342 /* Current tile component size*/
9344 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
9345 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
9348 l_width_src = (l_res->x1 - l_res->x0);
9349 l_height_src = (l_res->y1 - l_res->y0);
9351 /* Border of the current output component*/
9352 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
9353 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
9354 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
9355 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
9358 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
9359 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
9363 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
9364 * of the input buffer (decoded tile component) which will be move
9365 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
9366 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
9367 * by this input area.
9369 assert( l_res->x0 >= 0);
9370 assert( l_res->x1 >= 0);
9371 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
9372 l_start_x_dest = l_res->x0 - l_x0_dest;
9373 l_offset_x0_src = 0;
9375 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9376 l_width_dest = l_width_src;
9377 l_offset_x1_src = 0;
9380 l_width_dest = l_x1_dest - l_res->x0 ;
9381 l_offset_x1_src = l_width_src - l_width_dest;
9385 l_start_x_dest = 0 ;
9386 l_offset_x0_src = l_x0_dest - l_res->x0;
9388 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
9389 l_width_dest = l_width_src - l_offset_x0_src;
9390 l_offset_x1_src = 0;
9393 l_width_dest = l_img_comp_dest->w ;
9394 l_offset_x1_src = l_res->x1 - l_x1_dest;
9398 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
9399 l_start_y_dest = l_res->y0 - l_y0_dest;
9400 l_offset_y0_src = 0;
9402 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9403 l_height_dest = l_height_src;
9404 l_offset_y1_src = 0;
9407 l_height_dest = l_y1_dest - l_res->y0 ;
9408 l_offset_y1_src = l_height_src - l_height_dest;
9412 l_start_y_dest = 0 ;
9413 l_offset_y0_src = l_y0_dest - l_res->y0;
9415 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
9416 l_height_dest = l_height_src - l_offset_y0_src;
9417 l_offset_y1_src = 0;
9420 l_height_dest = l_img_comp_dest->h ;
9421 l_offset_y1_src = l_res->y1 - l_y1_dest;
9425 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
9430 /* Compute the input buffer offset */
9431 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
9432 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
9433 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
9435 /* Compute the output buffer offset */
9436 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
9437 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
9439 /* Move the output buffer to the first place where we will write*/
9440 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
9443 fprintf(stdout, "COMPO[%d]:\n",i);
9444 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
9445 "\t tile offset:%d, %d, %d, %d\n"
9446 "\t buffer offset: %d; %d, %d\n",
9447 l_res->x0, l_res->y0, l_width_src, l_height_src,
9448 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
9449 l_start_offset_src, l_line_offset_src, l_end_offset_src);
9451 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
9452 "\t start offset: %d, line offset= %d\n",
9453 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
9457 switch (l_size_comp) {
9460 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
9461 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
9463 if (l_img_comp_src->sgnd) {
9464 for (j = 0 ; j < l_height_dest ; ++j) {
9465 for ( k = 0 ; k < l_width_dest ; ++k) {
9466 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
9469 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
9470 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
9474 for ( j = 0 ; j < l_height_dest ; ++j ) {
9475 for ( k = 0 ; k < l_width_dest ; ++k) {
9476 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
9479 l_dest_ptr+= l_line_offset_dest;
9480 l_src_ptr += l_line_offset_src;
9484 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
9485 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
9490 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
9491 l_src_ptr += l_start_offset_src;
9493 if (l_img_comp_src->sgnd) {
9494 for (j=0;j<l_height_dest;++j) {
9495 for (k=0;k<l_width_dest;++k) {
9496 *(l_dest_ptr++) = *(l_src_ptr++);
9499 l_dest_ptr+= l_line_offset_dest;
9500 l_src_ptr += l_line_offset_src ;
9504 for (j=0;j<l_height_dest;++j) {
9505 for (k=0;k<l_width_dest;++k) {
9506 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
9509 l_dest_ptr+= l_line_offset_dest;
9510 l_src_ptr += l_line_offset_src ;
9514 l_src_ptr += l_end_offset_src;
9515 p_data = (OPJ_BYTE*) l_src_ptr;
9520 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
9521 l_src_ptr += l_start_offset_src;
9523 for (j=0;j<l_height_dest;++j) {
9524 for (k=0;k<l_width_dest;++k) {
9525 *(l_dest_ptr++) = (*(l_src_ptr++));
9528 l_dest_ptr+= l_line_offset_dest;
9529 l_src_ptr += l_line_offset_src ;
9532 l_src_ptr += l_end_offset_src;
9533 p_data = (OPJ_BYTE*) l_src_ptr;
9547 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
9549 * @param p_j2k the jpeg2000 codec.
9550 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
9551 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
9552 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
9553 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
9554 * @param p_manager the user event manager
9556 * @return true if the area could be set.
9558 opj_bool j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
9559 opj_image_t* p_image,
9560 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
9561 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
9562 struct opj_event_mgr * p_manager )
9564 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
9565 opj_image_t * l_image = p_j2k->m_private_image;
9568 OPJ_INT32 l_comp_x1, l_comp_y1;
9569 opj_image_comp_t* l_img_comp = NULL;
9571 /* Check if we are read the main header */
9572 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
9573 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
9577 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
9578 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
9580 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9581 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9582 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9583 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9589 /* Check if the positions provided by the user are correct */
9592 assert(p_start_x >= 0 );
9593 assert(p_start_y >= 0 );
9595 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
9596 opj_event_msg_v2(p_manager, EVT_ERROR,
9597 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
9598 p_start_x, l_image->x1);
9601 else if ((OPJ_UINT32)p_start_x < l_image->x0){
9602 opj_event_msg_v2(p_manager, EVT_WARNING,
9603 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
9604 p_start_x, l_image->x0);
9605 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
9606 p_image->x0 = l_image->x0;
9609 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
9610 p_image->x0 = p_start_x;
9614 if ((OPJ_UINT32)p_start_y > l_image->y1){
9615 opj_event_msg_v2(p_manager, EVT_ERROR,
9616 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
9617 p_start_y, l_image->y1);
9620 else if ((OPJ_UINT32)p_start_y < l_image->y0){
9621 opj_event_msg_v2(p_manager, EVT_WARNING,
9622 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
9623 p_start_y, l_image->y0);
9624 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
9625 p_image->y0 = l_image->y0;
9628 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
9629 p_image->y0 = p_start_y;
9633 assert((OPJ_UINT32)p_end_x > 0);
9634 assert((OPJ_UINT32)p_end_y > 0);
9635 if ((OPJ_UINT32)p_end_x < l_image->x0) {
9636 opj_event_msg_v2(p_manager, EVT_ERROR,
9637 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
9638 p_end_x, l_image->x0);
9641 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
9642 opj_event_msg_v2(p_manager, EVT_WARNING,
9643 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
9644 p_end_x, l_image->x1);
9645 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
9646 p_image->x1 = l_image->x1;
9649 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
9650 p_image->x1 = p_end_x;
9654 if ((OPJ_UINT32)p_end_y < l_image->y0) {
9655 opj_event_msg_v2(p_manager, EVT_ERROR,
9656 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
9657 p_end_y, l_image->y0);
9660 if ((OPJ_UINT32)p_end_y > l_image->y1){
9661 opj_event_msg_v2(p_manager, EVT_WARNING,
9662 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
9663 p_end_y, l_image->y1);
9664 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
9665 p_image->y1 = l_image->y1;
9668 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
9669 p_image->y1 = p_end_y;
9673 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
9675 l_img_comp = p_image->comps;
9676 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
9680 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9681 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9682 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9683 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9685 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
9686 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9688 opj_event_msg_v2(p_manager, EVT_ERROR,
9689 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
9693 l_img_comp->w = l_w;
9695 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
9696 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9698 opj_event_msg_v2(p_manager, EVT_ERROR,
9699 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
9703 l_img_comp->h = l_h;
9708 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
9709 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
9716 /* ----------------------------------------------------------------------- */
9717 /* J2K / JPT decoder interface */
9718 /* ----------------------------------------------------------------------- */
9720 * Creates a J2K decompression structure.
9722 * @return a handle to a J2K decompressor if successful, NULL otherwise.
9724 opj_j2k_v2_t* opj_j2k_create_decompress(void)
9726 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
9730 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
9732 l_j2k->m_is_decoder = 1;
9733 l_j2k->m_cp.m_is_decoder = 1;
9735 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
9736 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
9740 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
9742 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
9743 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
9748 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
9750 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
9752 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
9754 /* codestream index creation */
9755 l_j2k->cstr_index = j2k_create_cstr_index();
9757 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
9758 if (!l_j2k->cstr_index){
9763 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9766 /* validation list creation */
9767 l_j2k->m_validation_list = opj_procedure_list_create();
9768 if (! l_j2k->m_validation_list) {
9773 /* execution list creation */
9774 l_j2k->m_procedure_list = opj_procedure_list_create();
9775 if (! l_j2k->m_procedure_list) {
9784 opj_codestream_index_t* j2k_create_cstr_index(void)
9786 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9787 opj_calloc(1,sizeof(opj_codestream_index_t));
9791 cstr_index->maxmarknum = 100;
9792 cstr_index->marknum = 0;
9793 cstr_index->marker = (opj_marker_info_t*)
9794 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9795 if (!cstr_index-> marker)
9798 cstr_index->tile_index = NULL;
9805 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9807 * @param p_tile_no the tile index.
9808 * @param p_comp_no the component being outputted.
9809 * @param p_j2k the J2K codec.
9811 * @return the number of bytes taken by the SPCod element.
9813 OPJ_UINT32 j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9814 OPJ_UINT32 p_tile_no,
9815 OPJ_UINT32 p_comp_no )
9817 opj_cp_v2_t *l_cp = 00;
9818 opj_tcp_v2_t *l_tcp = 00;
9819 opj_tccp_t *l_tccp = 00;
9822 assert(p_j2k != 00);
9824 l_cp = &(p_j2k->m_cp);
9825 l_tcp = &l_cp->tcps[p_tile_no];
9826 l_tccp = &l_tcp->tccps[p_comp_no];
9828 /* preconditions again */
9829 assert(p_tile_no < (l_cp->tw * l_cp->th));
9830 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9832 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9833 return 5 + l_tccp->numresolutions;
9841 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9843 * @param p_comp_no the component number to output.
9844 * @param p_stream the stream to write data to.
9845 * @param p_j2k J2K codec.
9846 * @param p_manager the user event manager.
9849 opj_bool j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9850 OPJ_UINT32 p_tile_no,
9851 OPJ_UINT32 p_comp_no,
9853 OPJ_UINT32 * p_header_size,
9854 struct opj_event_mgr * p_manager )
9857 opj_cp_v2_t *l_cp = 00;
9858 opj_tcp_v2_t *l_tcp = 00;
9859 opj_tccp_t *l_tccp = 00;
9862 assert(p_j2k != 00);
9863 assert(p_header_size != 00);
9864 assert(p_manager != 00);
9865 assert(p_data != 00);
9867 l_cp = &(p_j2k->m_cp);
9868 l_tcp = &l_cp->tcps[p_tile_no];
9869 l_tccp = &l_tcp->tccps[p_comp_no];
9871 /* preconditions again */
9872 assert(p_tile_no < (l_cp->tw * l_cp->th));
9873 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9875 if (*p_header_size < 5) {
9876 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9880 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9883 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9886 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9889 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9892 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9895 *p_header_size = *p_header_size - 5;
9897 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9899 if (*p_header_size < l_tccp->numresolutions) {
9900 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9904 for (i = 0; i < l_tccp->numresolutions; ++i) {
9905 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9909 *p_header_size = *p_header_size - l_tccp->numresolutions;
9916 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9917 * @param p_header_data the data contained in the COM box.
9918 * @param p_j2k the jpeg2000 codec.
9919 * @param p_header_size the size of the data contained in the COM marker.
9920 * @param p_manager the user event manager.
9922 opj_bool j2k_read_SPCod_SPCoc(
9923 opj_j2k_v2_t *p_j2k,
9925 OPJ_BYTE * p_header_data,
9926 OPJ_UINT32 * p_header_size,
9927 struct opj_event_mgr * p_manager
9930 OPJ_UINT32 i, l_tmp;
9931 opj_cp_v2_t *l_cp = NULL;
9932 opj_tcp_v2_t *l_tcp = NULL;
9933 opj_tccp_t *l_tccp = NULL;
9934 OPJ_BYTE * l_current_ptr = NULL;
9937 assert(p_j2k != 00);
9938 assert(p_manager != 00);
9939 assert(p_header_data != 00);
9941 l_cp = &(p_j2k->m_cp);
9942 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9943 &l_cp->tcps[p_j2k->m_current_tile_number] :
9944 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9946 /* precondition again */
9947 assert(compno < p_j2k->m_private_image->numcomps);
9949 l_tccp = &l_tcp->tccps[compno];
9950 l_current_ptr = p_header_data;
9952 /* make sure room is sufficient */
9953 if (*p_header_size < 5) {
9954 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9958 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
9959 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
9962 /* If user wants to remove more resolutions than the codestream contains, return error */
9963 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9964 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
9965 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
9966 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9970 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
9974 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
9978 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
9981 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
9984 *p_header_size = *p_header_size - 5;
9986 /* use custom precinct size ? */
9987 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9988 if (*p_header_size < l_tccp->numresolutions) {
9989 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9993 for (i = 0; i < l_tccp->numresolutions; ++i) {
9994 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
9996 l_tccp->prcw[i] = l_tmp & 0xf;
9997 l_tccp->prch[i] = l_tmp >> 4;
10000 *p_header_size = *p_header_size - l_tccp->numresolutions;
10003 /* set default size for the precinct width and height */
10004 for (i = 0; i < l_tccp->numresolutions; ++i) {
10005 l_tccp->prcw[i] = 15;
10006 l_tccp->prch[i] = 15;
10010 #ifdef WIP_REMOVE_MSD
10012 if (p_j2k->cstr_info && compno == 0) {
10013 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
10015 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
10016 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
10017 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
10018 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
10019 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
10022 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
10023 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
10032 * Copies the tile component parameters of all the component from the first tile component.
10034 * @param p_j2k the J2k codec.
10036 void j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
10040 opj_cp_v2_t *l_cp = NULL;
10041 opj_tcp_v2_t *l_tcp = NULL;
10042 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
10043 OPJ_UINT32 l_prc_size;
10045 /* preconditions */
10046 assert(p_j2k != 00);
10048 l_cp = &(p_j2k->m_cp);
10049 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
10050 &l_cp->tcps[p_j2k->m_current_tile_number] :
10051 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10053 l_ref_tccp = &l_tcp->tccps[0];
10054 l_copied_tccp = l_ref_tccp + 1;
10055 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
10057 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
10058 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
10059 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
10060 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
10061 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
10062 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
10063 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
10064 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
10070 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
10072 * @param p_tile_no the tile index.
10073 * @param p_comp_no the component being outputted.
10074 * @param p_j2k the J2K codec.
10076 * @return the number of bytes taken by the SPCod element.
10078 OPJ_UINT32 j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
10079 OPJ_UINT32 p_tile_no,
10080 OPJ_UINT32 p_comp_no )
10082 OPJ_UINT32 l_num_bands;
10084 opj_cp_v2_t *l_cp = 00;
10085 opj_tcp_v2_t *l_tcp = 00;
10086 opj_tccp_t *l_tccp = 00;
10088 /* preconditions */
10089 assert(p_j2k != 00);
10091 l_cp = &(p_j2k->m_cp);
10092 l_tcp = &l_cp->tcps[p_tile_no];
10093 l_tccp = &l_tcp->tccps[p_comp_no];
10095 /* preconditions again */
10096 assert(p_tile_no < l_cp->tw * l_cp->th);
10097 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10099 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10101 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10102 return 1 + l_num_bands;
10105 return 1 + 2*l_num_bands;
10110 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
10112 * @param p_tile_no the tile to output.
10113 * @param p_comp_no the component number to output.
10114 * @param p_data the data buffer.
10115 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
10116 * @param p_j2k J2K codec.
10117 * @param p_manager the user event manager.
10120 opj_bool j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
10121 OPJ_UINT32 p_tile_no,
10122 OPJ_UINT32 p_comp_no,
10124 OPJ_UINT32 * p_header_size,
10125 struct opj_event_mgr * p_manager )
10127 OPJ_UINT32 l_header_size;
10128 OPJ_UINT32 l_band_no, l_num_bands;
10129 OPJ_UINT32 l_expn,l_mant;
10131 opj_cp_v2_t *l_cp = 00;
10132 opj_tcp_v2_t *l_tcp = 00;
10133 opj_tccp_t *l_tccp = 00;
10135 /* preconditions */
10136 assert(p_j2k != 00);
10137 assert(p_header_size != 00);
10138 assert(p_manager != 00);
10139 assert(p_data != 00);
10141 l_cp = &(p_j2k->m_cp);
10142 l_tcp = &l_cp->tcps[p_tile_no];
10143 l_tccp = &l_tcp->tccps[p_comp_no];
10145 /* preconditions again */
10146 assert(p_tile_no < l_cp->tw * l_cp->th);
10147 assert(p_comp_no <p_j2k->m_private_image->numcomps);
10149 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
10151 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10152 l_header_size = 1 + l_num_bands;
10154 if (*p_header_size < l_header_size) {
10155 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10159 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10162 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10163 l_expn = l_tccp->stepsizes[l_band_no].expn;
10164 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
10169 l_header_size = 1 + 2*l_num_bands;
10171 if (*p_header_size < l_header_size) {
10172 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
10176 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
10179 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
10180 l_expn = l_tccp->stepsizes[l_band_no].expn;
10181 l_mant = l_tccp->stepsizes[l_band_no].mant;
10183 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
10188 *p_header_size = *p_header_size - l_header_size;
10194 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
10196 * @param p_comp_no the component being targeted.
10197 * @param p_header_data the data contained in the COM box.
10198 * @param p_j2k the jpeg2000 codec.
10199 * @param p_header_size the size of the data contained in the COM marker.
10200 * @param p_manager the user event manager.
10202 opj_bool j2k_read_SQcd_SQcc(
10203 opj_j2k_v2_t *p_j2k,
10204 OPJ_UINT32 p_comp_no,
10205 OPJ_BYTE* p_header_data,
10206 OPJ_UINT32 * p_header_size,
10207 struct opj_event_mgr * p_manager
10211 OPJ_UINT32 l_band_no;
10212 opj_cp_v2_t *l_cp = 00;
10213 opj_tcp_v2_t *l_tcp = 00;
10214 opj_tccp_t *l_tccp = 00;
10215 OPJ_BYTE * l_current_ptr = 00;
10216 OPJ_UINT32 l_tmp, l_num_band;
10219 assert(p_j2k != 00);
10220 assert(p_manager != 00);
10221 assert(p_header_data != 00);
10223 l_cp = &(p_j2k->m_cp);
10224 /* come from tile part header or main header ?*/
10225 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
10226 &l_cp->tcps[p_j2k->m_current_tile_number] :
10227 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10229 /* precondition again*/
10230 assert(p_comp_no < p_j2k->m_private_image->numcomps);
10232 l_tccp = &l_tcp->tccps[p_comp_no];
10233 l_current_ptr = p_header_data;
10235 if (*p_header_size < 1) {
10236 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
10239 *p_header_size -= 1;
10241 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
10244 l_tccp->qntsty = l_tmp & 0x1f;
10245 l_tccp->numgbits = l_tmp >> 5;
10246 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10250 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
10252 (*p_header_size) / 2;
10254 if( l_num_band > J2K_MAXBANDS ) {
10255 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
10256 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
10257 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
10258 /*return OPJ_FALSE;*/
10263 if (l_cp->correct) {
10265 /* if JPWL is on, we check whether there are too many subbands */
10266 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
10267 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
10268 "JPWL: bad number of subbands in Sqcx (%d)\n",
10270 if (!JPWL_ASSUME) {
10271 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
10274 /* we try to correct */
10276 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
10277 "- setting number of bands to %d => HYPOTHESIS!!!\n",
10282 #endif /* USE_JPWL */
10284 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
10285 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10286 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
10288 if (l_band_no < J2K_MAXBANDS){
10289 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
10290 l_tccp->stepsizes[l_band_no].mant = 0;
10293 *p_header_size = *p_header_size - l_num_band;
10296 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
10297 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
10299 if (l_band_no < J2K_MAXBANDS){
10300 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
10301 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
10304 *p_header_size = *p_header_size - 2*l_num_band;
10307 /* Add Antonin : if scalar_derived -> compute other stepsizes */
10308 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
10309 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
10310 l_tccp->stepsizes[l_band_no].expn =
10311 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
10312 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
10313 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
10321 * Copies the tile component parameters of all the component from the first tile component.
10323 * @param p_j2k the J2k codec.
10325 void j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
10328 opj_cp_v2_t *l_cp = NULL;
10329 opj_tcp_v2_t *l_tcp = NULL;
10330 opj_tccp_t *l_ref_tccp = NULL;
10331 opj_tccp_t *l_copied_tccp = NULL;
10334 /* preconditions */
10335 assert(p_j2k != 00);
10337 l_cp = &(p_j2k->m_cp);
10338 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
10339 &l_cp->tcps[p_j2k->m_current_tile_number] :
10340 p_j2k->m_specific_param.m_decoder.m_default_tcp;
10342 l_ref_tccp = &l_tcp->tccps[0];
10343 l_copied_tccp = l_ref_tccp + 1;
10344 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
10346 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
10347 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
10348 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
10349 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
10355 * Dump some elements from the J2K decompression structure .
10357 *@param p_j2k the jpeg2000 codec.
10358 *@param flag flag to describe what elments are dump.
10359 *@param out_stream output stream where dump the elements.
10362 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
10364 /* Check if the flag is compatible with j2k file*/
10365 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
10366 fprintf(out_stream, "Wrong flag\n");
10370 /* Dump the image_header */
10371 if (flag & OPJ_IMG_INFO){
10372 if (p_j2k->m_private_image)
10373 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
10376 /* Dump the codestream info from main header */
10377 if (flag & OPJ_J2K_MH_INFO){
10378 j2k_dump_MH_info(p_j2k, out_stream);
10382 /* Dump the codestream info of the current tile */
10383 if (flag & OPJ_J2K_TH_INFO){
10387 /* Dump the codestream index from main header */
10388 if (flag & OPJ_J2K_MH_IND){
10389 j2k_dump_MH_index(p_j2k, out_stream);
10392 /* Dump the codestream index of the current tile */
10393 if (flag & OPJ_J2K_TH_IND){
10400 * Dump index elements of the codestream extract from the main header.
10402 *@param p_j2k the jpeg2000 codec.
10403 *@param out_stream output stream where dump the elements.
10406 void j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10408 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
10409 OPJ_UINT32 it_marker, it_tile, it_tile_part;
10411 fprintf(out_stream, "Codestream index from main header: {\n");
10413 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
10414 "\t Main header end position=%" PRIi64 "\n",
10415 cstr_index->main_head_start, cstr_index->main_head_end);
10417 fprintf(out_stream, "\t Marker list: {\n");
10419 if (cstr_index->marker){
10420 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
10421 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10422 cstr_index->marker[it_marker].type,
10423 cstr_index->marker[it_marker].pos,
10424 cstr_index->marker[it_marker].len );
10428 fprintf(out_stream, "\t }\n");
10431 if (cstr_index->tile_index){
10433 /* Simple test to avoid to write empty information*/
10434 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
10435 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10436 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
10439 if (l_acc_nb_of_tile_part)
10441 fprintf(out_stream, "\t Tile index: {\n");
10443 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
10444 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
10446 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
10448 if (cstr_index->tile_index[it_tile].tp_index){
10449 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
10450 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
10452 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
10453 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
10454 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
10458 if (cstr_index->tile_index[it_tile].marker){
10459 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
10460 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
10461 cstr_index->tile_index[it_tile].marker[it_marker].type,
10462 cstr_index->tile_index[it_tile].marker[it_marker].pos,
10463 cstr_index->tile_index[it_tile].marker[it_marker].len );
10467 fprintf(out_stream,"\t }\n");
10471 fprintf(out_stream,"}\n");
10476 * Dump info elements of the codestream extract from the main header.
10478 *@param p_j2k the jpeg2000 codec.
10479 *@param out_stream output stream where dump the elements.
10482 void j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
10484 opj_tcp_v2_t * l_default_tile=NULL;
10486 fprintf(out_stream, "Codestream info from main header: {\n");
10488 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
10489 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
10490 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
10492 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10493 if (l_default_tile)
10496 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
10498 fprintf(out_stream, "\t default tile {\n");
10499 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
10500 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
10501 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
10502 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
10504 for (compno = 0; compno < numcomps; compno++) {
10505 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10507 OPJ_INT32 bandno, numbands;
10510 fprintf(out_stream, "\t\t comp %d {\n", compno);
10511 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
10512 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
10513 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
10514 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
10515 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
10516 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
10518 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
10519 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
10520 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
10522 fprintf(out_stream, "\n");
10524 /* quantization style*/
10525 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
10526 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
10527 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
10528 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10529 for (bandno = 0; bandno < numbands; bandno++) {
10530 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
10531 l_tccp->stepsizes[bandno].expn);
10533 fprintf(out_stream, "\n");
10536 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
10538 fprintf(out_stream, "\t\t }\n");
10539 } /*end of component of default tile*/
10540 fprintf(out_stream, "\t }\n"); /*end of default tile*/
10544 fprintf(out_stream, "}\n");
10549 * Dump an image header structure.
10551 *@param img_header the image header to dump.
10552 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10553 *@param out_stream output stream where dump the elements.
10555 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
10559 if (dev_dump_flag){
10560 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
10564 fprintf(out_stream, "Image info {\n");
10565 tab[0] = '\t';tab[1] = '\0';
10568 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
10569 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
10570 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
10572 if (img_header->comps){
10574 for (compno = 0; compno < img_header->numcomps; compno++) {
10575 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
10576 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
10577 fprintf(out_stream,"%s}\n",tab);
10581 fprintf(out_stream, "}\n");
10585 * Dump a component image header structure.
10587 *@param comp_header the component image header to dump.
10588 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
10589 *@param out_stream output stream where dump the elements.
10591 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
10595 if (dev_dump_flag){
10596 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
10599 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
10602 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
10603 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
10604 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
10607 fprintf(out_stream, "}\n");
10612 * Get the codestream info from a JPEG2000 codec.
10614 *@param p_j2k the component image header to dump.
10616 *@return the codestream information extract from the jpg2000 codec
10618 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
10621 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
10622 opj_tcp_v2_t *l_default_tile;
10623 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
10625 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
10627 cstr_info->tx0 = p_j2k->m_cp.tx0;
10628 cstr_info->ty0 = p_j2k->m_cp.ty0;
10629 cstr_info->tdx = p_j2k->m_cp.tdx;
10630 cstr_info->tdy = p_j2k->m_cp.tdy;
10631 cstr_info->tw = p_j2k->m_cp.tw;
10632 cstr_info->th = p_j2k->m_cp.th;
10634 cstr_info->tile_info = NULL; /* Not fill from the main header*/
10636 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
10638 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
10639 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
10640 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
10641 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
10643 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
10645 for (compno = 0; compno < numcomps; compno++) {
10646 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
10647 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
10648 OPJ_INT32 bandno, numbands;
10651 l_tccp_info->csty = l_tccp->csty;
10652 l_tccp_info->numresolutions = l_tccp->numresolutions;
10653 l_tccp_info->cblkw = l_tccp->cblkw;
10654 l_tccp_info->cblkh = l_tccp->cblkh;
10655 l_tccp_info->cblksty = l_tccp->cblksty;
10656 l_tccp_info->qmfbid = l_tccp->qmfbid;
10657 if (l_tccp->numresolutions < J2K_MAXRLVLS)
10659 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
10660 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
10663 /* quantization style*/
10664 l_tccp_info->qntsty = l_tccp->qntsty;
10665 l_tccp_info->numgbits = l_tccp->numgbits;
10667 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
10668 if (numbands < J2K_MAXBANDS) {
10669 for (bandno = 0; bandno < numbands; bandno++) {
10670 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
10671 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
10676 l_tccp_info->roishift = l_tccp->roishift;
10684 * Get the codestream index from a JPEG2000 codec.
10686 *@param p_j2k the component image header to dump.
10688 *@return the codestream index extract from the jpg2000 codec
10690 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
10692 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
10693 opj_calloc(1,sizeof(opj_codestream_index_t));
10697 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
10698 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
10699 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
10701 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
10702 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
10703 if (!l_cstr_index->marker){
10704 opj_free( l_cstr_index);
10708 if (p_j2k->cstr_index->marker)
10709 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
10711 opj_free(l_cstr_index->marker);
10712 l_cstr_index->marker = NULL;
10715 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
10716 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
10717 if (!l_cstr_index->tile_index){
10718 opj_free( l_cstr_index->marker);
10719 opj_free( l_cstr_index);
10723 if (!p_j2k->cstr_index->tile_index){
10724 opj_free(l_cstr_index->tile_index);
10725 l_cstr_index->tile_index = NULL;
10728 OPJ_UINT32 it_tile = 0;
10729 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
10732 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
10734 l_cstr_index->tile_index[it_tile].marker =
10735 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
10737 if (!l_cstr_index->tile_index[it_tile].marker) {
10738 OPJ_UINT32 it_tile_free;
10740 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10741 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10744 opj_free( l_cstr_index->tile_index);
10745 opj_free( l_cstr_index->marker);
10746 opj_free( l_cstr_index);
10750 if (p_j2k->cstr_index->tile_index[it_tile].marker)
10751 memcpy( l_cstr_index->tile_index[it_tile].marker,
10752 p_j2k->cstr_index->tile_index[it_tile].marker,
10753 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
10755 opj_free(l_cstr_index->tile_index[it_tile].marker);
10756 l_cstr_index->tile_index[it_tile].marker = NULL;
10759 /* Tile part index*/
10760 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
10762 l_cstr_index->tile_index[it_tile].tp_index =
10763 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10765 if(!l_cstr_index->tile_index[it_tile].tp_index){
10766 OPJ_UINT32 it_tile_free;
10768 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10769 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10770 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10773 opj_free( l_cstr_index->tile_index);
10774 opj_free( l_cstr_index->marker);
10775 opj_free( l_cstr_index);
10779 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10780 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10781 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10782 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10785 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10786 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10789 /* Packet index (NOT USED)*/
10790 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10791 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10796 return l_cstr_index;
10799 static opj_bool j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10801 OPJ_UINT32 it_tile=0;
10803 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10804 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10805 if (!p_j2k->cstr_index->tile_index)
10808 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10809 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10810 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10811 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10812 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10813 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10823 opj_bool j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10824 opj_stream_private_t *p_stream,
10825 opj_event_mgr_t * p_manager)
10827 opj_bool l_go_on = OPJ_TRUE;
10828 OPJ_UINT32 l_current_tile_no;
10829 OPJ_UINT32 l_data_size,l_max_data_size;
10830 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10831 OPJ_UINT32 l_nb_comps;
10832 OPJ_BYTE * l_current_data;
10834 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10835 if (! l_current_data) {
10838 l_max_data_size = 1000;
10843 if (! j2k_read_tile_header( p_j2k,
10844 &l_current_tile_no,
10846 &l_tile_x0, &l_tile_y0,
10847 &l_tile_x1, &l_tile_y1,
10852 opj_free(l_current_data);
10860 if (l_data_size > l_max_data_size) {
10861 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10862 if (! l_current_data) {
10863 opj_free(l_current_data);
10867 l_max_data_size = l_data_size;
10870 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10871 opj_free(l_current_data);
10874 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);
10876 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10877 opj_free(l_current_data);
10880 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10884 opj_free(l_current_data);
10890 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10892 static void j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10895 assert(p_j2k != 00);
10897 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_tiles);
10898 /* DEVELOPER CORNER, add your custom procedures */
10903 * Read and decode one tile.
10905 static opj_bool j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10906 opj_stream_private_t *p_stream,
10907 opj_event_mgr_t * p_manager)
10909 opj_bool l_go_on = OPJ_TRUE;
10910 OPJ_UINT32 l_current_tile_no;
10911 OPJ_UINT32 l_tile_no_to_dec;
10912 OPJ_UINT32 l_data_size,l_max_data_size;
10913 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10914 OPJ_UINT32 l_nb_comps;
10915 OPJ_BYTE * l_current_data;
10917 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10918 if (! l_current_data) {
10921 l_max_data_size = 1000;
10923 /*Allocate and initialize some elements of codestrem index if not already done*/
10924 if( !p_j2k->cstr_index->tile_index)
10926 if (!j2k_allocate_tile_element_cstr_index(p_j2k)){
10927 opj_free(l_current_data);
10931 /* Move into the codestream to the first SOT used to decode the desired tile */
10932 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10933 if (p_j2k->cstr_index->tile_index)
10934 if(p_j2k->cstr_index->tile_index->tp_index)
10936 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10937 /* the index for this tile has not been built,
10938 * so move to the last SOT read */
10939 if ( opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager) ){
10940 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10945 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)) {
10946 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10950 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10951 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
10952 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10956 if (! j2k_read_tile_header( p_j2k,
10957 &l_current_tile_no,
10959 &l_tile_x0, &l_tile_y0,
10960 &l_tile_x1, &l_tile_y1,
10965 opj_free(l_current_data);
10974 if (l_data_size > l_max_data_size) {
10975 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10976 if (! l_current_data) {
10977 opj_free(l_current_data);
10981 l_max_data_size = l_data_size;
10986 if (! j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10987 opj_free(l_current_data);
10990 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);
10992 if (! j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10993 opj_free(l_current_data);
10996 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
10998 if(l_current_tile_no == l_tile_no_to_dec)
11000 /* move into the codestream to the the first SOT (FIXME or not move?)*/
11001 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
11002 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
11008 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);
11013 opj_free(l_current_data);
11020 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
11022 static void j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
11025 assert(p_j2k != 00);
11027 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_decode_one_tile);
11028 /* DEVELOPER CORNER, add your custom procedures */
11034 * Decodes the tiles of the stream.
11036 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
11037 opj_stream_private_t * p_stream,
11038 opj_image_t * p_image,
11039 opj_event_mgr_t * p_manager)
11046 p_j2k->m_output_image = opj_image_create0();
11047 if (! (p_j2k->m_output_image)) {
11050 opj_copy_image_header(p_image, p_j2k->m_output_image);
11052 /* customization of the decoding */
11053 j2k_setup_decoding(p_j2k);
11055 /* Decode the codestream */
11056 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11057 opj_image_destroy(p_j2k->m_private_image);
11058 p_j2k->m_private_image = NULL;
11062 /* Move data and copy one information from codec to output image*/
11063 for (compno = 0; compno < p_image->numcomps; compno++) {
11064 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11065 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11066 p_j2k->m_output_image->comps[compno].data = NULL;
11074 * Get the decoded tile.
11076 * @param p_j2k the jpeg2000 codestream codec.
11077 * @param p_stream input_stream
11078 * @param p_image output image. .
11079 * @param p_manager the user event manager
11080 * @param tile_index index of the tile we want decode
11082 * @return true if succeed.
11084 opj_bool j2k_get_tile( opj_j2k_v2_t *p_j2k,
11085 opj_stream_private_t *p_stream,
11086 opj_image_t* p_image,
11087 struct opj_event_mgr * p_manager,
11088 OPJ_UINT32 tile_index )
11091 OPJ_UINT32 l_tile_x, l_tile_y;
11092 opj_image_comp_t* l_img_comp;
11095 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
11099 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
11100 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);
11104 /* Compute the dimension of the desired tile*/
11105 l_tile_x = tile_index % p_j2k->m_cp.tw;
11106 l_tile_y = tile_index / p_j2k->m_cp.tw;
11108 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11109 if (p_image->x0 < p_j2k->m_private_image->x0)
11110 p_image->x0 = p_j2k->m_private_image->x0;
11111 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
11112 if (p_image->x1 > p_j2k->m_private_image->x1)
11113 p_image->x1 = p_j2k->m_private_image->x1;
11115 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11116 if (p_image->y0 < p_j2k->m_private_image->y0)
11117 p_image->y0 = p_j2k->m_private_image->y0;
11118 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
11119 if (p_image->y1 > p_j2k->m_private_image->y1)
11120 p_image->y1 = p_j2k->m_private_image->y1;
11122 l_img_comp = p_image->comps;
11123 for (compno=0; compno < p_image->numcomps; ++compno)
11125 OPJ_INT32 l_comp_x1, l_comp_y1;
11127 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
11129 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
11130 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
11131 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
11132 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
11134 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
11135 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
11140 /* Destroy the previous output image*/
11141 if (p_j2k->m_output_image)
11142 opj_image_destroy(p_j2k->m_output_image);
11144 /* Create the ouput image from the information previously computed*/
11145 p_j2k->m_output_image = opj_image_create0();
11146 if (! (p_j2k->m_output_image)) {
11149 opj_copy_image_header(p_image, p_j2k->m_output_image);
11151 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
11153 /* customization of the decoding */
11154 j2k_setup_decoding_tile(p_j2k);
11156 /* Decode the codestream */
11157 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11158 opj_image_destroy(p_j2k->m_private_image);
11159 p_j2k->m_private_image = NULL;
11163 /* Move data and copy one information from codec to output image*/
11164 for (compno = 0; compno < p_image->numcomps; compno++) {
11165 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
11167 if (p_image->comps[compno].data)
11168 opj_free(p_image->comps[compno].data);
11170 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
11172 p_j2k->m_output_image->comps[compno].data = NULL;
11178 opj_bool j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k, OPJ_UINT32 res_factor, opj_event_mgr_t * p_manager)
11180 OPJ_UINT32 it_comp;
11182 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
11184 if (p_j2k->m_private_image) {
11185 if (p_j2k->m_private_image->comps) {
11186 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
11187 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
11188 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
11189 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
11190 if ( res_factor >= max_res){
11191 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
11194 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
11207 * Encodes all the tiles in a row.
11209 opj_bool j2k_encode_v2( opj_j2k_v2_t * p_j2k,
11210 opj_stream_private_t *p_stream,
11211 opj_event_mgr_t * p_manager )
11214 OPJ_UINT32 l_nb_tiles;
11215 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
11216 OPJ_BYTE * l_current_data;
11218 /* preconditions */
11219 assert(p_j2k != 00);
11220 assert(p_stream != 00);
11221 assert(p_manager != 00);
11223 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
11224 if (! l_current_data) {
11227 l_max_tile_size = 1000;
11229 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
11230 for (i=0;i<l_nb_tiles;++i) {
11231 if (! j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
11232 opj_free(l_current_data);
11236 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
11237 if (l_current_tile_size > l_max_tile_size) {
11238 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
11239 if (! l_current_data) {
11242 l_max_tile_size = l_current_tile_size;
11245 j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
11247 if (! j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
11252 opj_free(l_current_data);
11257 * Ends the compression procedures and possibility add data to be read after the
11260 opj_bool j2k_end_compress( opj_j2k_v2_t *p_j2k,
11261 opj_stream_private_t *p_stream,
11262 struct opj_event_mgr * p_manager)
11264 /* customization of the encoding */
11265 j2k_setup_end_compress(p_j2k);
11267 if (! j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
11277 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
11279 * @param p_j2k the jpeg2000 codec.
11280 * @param p_stream the stream object.
11281 * @param p_manager the user event manager.
11283 * @return true if the codec is valid.
11285 opj_bool j2k_start_compress(opj_j2k_v2_t *p_j2k,
11286 opj_stream_private_t *p_stream,
11287 opj_image_t * p_image,
11288 opj_event_mgr_t * p_manager)
11290 /* preconditions */
11291 assert(p_j2k != 00);
11292 assert(p_stream != 00);
11293 assert(p_manager != 00);
11295 p_j2k->m_private_image = opj_image_create0();
11296 opj_copy_image_header(p_image, p_j2k->m_private_image);
11298 // TODO_MSD: Find a better way
11299 if (p_image->comps) {
11300 OPJ_UINT32 it_comp;
11301 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
11302 if (p_image->comps[it_comp].data) {
11303 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
11304 p_image->comps[it_comp].data = NULL;
11310 /* customization of the validation */
11311 j2k_setup_encoding_validation (p_j2k);
11313 /* validation of the parameters codec */
11314 if (! j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
11318 /* customization of the encoding */
11319 j2k_setup_header_writting(p_j2k);
11322 if (! j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
11332 opj_bool j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
11333 OPJ_UINT32 p_tile_index,
11334 opj_stream_private_t *p_stream,
11335 opj_event_mgr_t * p_manager )
11338 if (p_tile_index != p_j2k->m_current_tile_number) {
11339 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
11343 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);
11345 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
11346 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
11347 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11349 /* initialisation before tile encoding */
11350 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
11360 void j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
11362 OPJ_UINT32 i,j,k = 0;
11363 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
11364 opj_image_comp_t * l_img_comp = 00;
11365 opj_tcd_tilecomp_v2_t * l_tilec = 00;
11366 opj_image_t * l_image = 00;
11367 OPJ_UINT32 l_size_comp, l_remaining;
11368 OPJ_INT32 * l_src_ptr;
11369 l_tilec = p_tcd->tcd_image->tiles->comps;
11370 l_image = p_tcd->image;
11371 l_img_comp = l_image->comps;
11373 for (i=0;i<p_tcd->image->numcomps;++i) {
11374 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
11375 l_remaining = l_img_comp->prec & 7; /* (%8) */
11380 if (l_size_comp == 3) {
11384 l_width = (l_tilec->x1 - l_tilec->x0);
11385 l_height = (l_tilec->y1 - l_tilec->y0);
11386 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
11387 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
11388 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
11389 l_stride = l_image_width - l_width;
11390 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
11392 switch (l_size_comp) {
11395 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
11396 if (l_img_comp->sgnd) {
11397 for (j=0;j<l_height;++j) {
11398 for (k=0;k<l_width;++k) {
11399 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
11403 l_src_ptr += l_stride;
11407 for (j=0;j<l_height;++j) {
11408 for (k=0;k<l_width;++k) {
11409 *(l_dest_ptr) = (*l_src_ptr)&0xff;
11413 l_src_ptr += l_stride;
11417 p_data = (OPJ_BYTE*) l_dest_ptr;
11422 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
11423 if (l_img_comp->sgnd) {
11424 for (j=0;j<l_height;++j) {
11425 for (k=0;k<l_width;++k) {
11426 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
11428 l_src_ptr += l_stride;
11432 for (j=0;j<l_height;++j) {
11433 for (k=0;k<l_width;++k) {
11434 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
11436 l_src_ptr += l_stride;
11440 p_data = (OPJ_BYTE*) l_dest_ptr;
11445 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
11446 for (j=0;j<l_height;++j) {
11447 for (k=0;k<l_width;++k) {
11448 *(l_dest_ptr++) = *(l_src_ptr++);
11450 l_src_ptr += l_stride;
11453 p_data = (OPJ_BYTE*) l_dest_ptr;
11466 * @param p_j2k the jpeg2000 codec.
11467 * @param p_stream the stream to write data to.
11468 * @param p_manager the user event manager.
11470 opj_bool j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
11472 OPJ_UINT32 p_data_size,
11473 opj_stream_private_t *p_stream,
11474 opj_event_mgr_t * p_manager )
11476 opj_tcd_v2_t * l_tcd = 00;
11477 opj_cp_v2_t * l_cp = 00;
11478 opj_tcp_v2_t * l_tcp = 00;
11479 OPJ_UINT32 l_nb_bytes_written;
11480 OPJ_BYTE * l_current_data = 00;
11481 OPJ_UINT32 l_tile_size = 0;
11482 OPJ_UINT32 l_available_data;
11484 /* preconditions */
11485 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11487 l_tcd = p_j2k->m_tcd;
11488 l_cp = &(p_j2k->m_cp);
11489 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11491 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
11492 l_available_data = l_tile_size;
11493 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
11495 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
11496 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
11500 l_nb_bytes_written = 0;
11501 if (! j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11504 l_current_data += l_nb_bytes_written;
11505 l_available_data -= l_nb_bytes_written;
11507 l_nb_bytes_written = 0;
11508 if (! j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
11512 l_available_data -= l_nb_bytes_written;
11513 l_nb_bytes_written = l_tile_size - l_available_data;
11515 if ( opj_stream_write_data( p_stream,
11516 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
11517 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
11521 ++p_j2k->m_current_tile_number;
11528 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11529 * are valid. Developers wanting to extend the library can add their own validation procedures.
11531 void j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
11533 /* preconditions */
11534 assert(p_j2k != 00);
11536 /* DEVELOPER CORNER, insert your custom procedures */
11537 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
11539 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11540 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
11543 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
11544 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
11545 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_destroy_header_memory);
11549 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
11550 * are valid. Developers wanting to extend the library can add their own validation procedures.
11552 void j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
11554 /* preconditions */
11555 assert(p_j2k != 00);
11557 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_build_encoder);
11558 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_encoding_validation);
11560 /* DEVELOPER CORNER, add your custom validation procedure */
11561 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)j2k_mct_validation);
11566 * Sets up the procedures to do on writing header.
11567 * Developers wanting to extend the library can add their own writing procedures.
11569 void j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
11571 /* preconditions */
11572 assert(p_j2k != 00);
11574 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
11575 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_soc_v2 );
11576 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
11577 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
11578 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
11581 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
11582 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
11583 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
11585 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
11586 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
11590 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
11592 if (p_j2k->m_cp.comment != 00) {
11593 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
11596 /* DEVELOPER CORNER, insert your custom procedures */
11597 if (p_j2k->m_cp.rsiz & MCT) {
11598 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
11600 /* End of Developer Corner */
11602 if (p_j2k->cstr_index) {
11603 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_get_end_header );
11606 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_create_tcd);
11607 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_update_rates);
11611 opj_bool j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
11613 OPJ_UINT32 * p_data_written,
11614 OPJ_UINT32 p_total_data_size,
11615 opj_stream_private_t *p_stream,
11616 struct opj_event_mgr * p_manager )
11619 OPJ_UINT32 l_nb_bytes_written = 0;
11620 OPJ_UINT32 l_current_nb_bytes_written;
11621 OPJ_BYTE * l_begin_data = 00;
11623 opj_tcp_v2_t *l_tcp = 00;
11624 opj_tcd_v2_t * l_tcd = 00;
11625 opj_cp_v2_t * l_cp = 00;
11627 l_tcd = p_j2k->m_tcd;
11628 l_cp = &(p_j2k->m_cp);
11629 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11631 l_tcd->cur_pino = 0;
11633 /*Get number of tile parts*/
11634 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
11639 l_current_nb_bytes_written = 0;
11640 l_begin_data = p_data;
11641 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
11646 l_nb_bytes_written += l_current_nb_bytes_written;
11647 p_data += l_current_nb_bytes_written;
11648 p_total_data_size -= l_current_nb_bytes_written;
11650 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
11651 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
11652 l_current_nb_bytes_written = 0;
11653 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11654 l_nb_bytes_written += l_current_nb_bytes_written;
11655 p_data += l_current_nb_bytes_written;
11656 p_total_data_size -= l_current_nb_bytes_written;
11658 l_current_nb_bytes_written = 0;
11659 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
11660 l_nb_bytes_written += l_current_nb_bytes_written;
11661 p_data += l_current_nb_bytes_written;
11662 p_total_data_size -= l_current_nb_bytes_written;
11665 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
11666 l_current_nb_bytes_written = 0;
11667 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
11668 l_nb_bytes_written += l_current_nb_bytes_written;
11669 p_data += l_current_nb_bytes_written;
11670 p_total_data_size -= l_current_nb_bytes_written;
11674 l_current_nb_bytes_written = 0;
11675 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11679 l_nb_bytes_written += l_current_nb_bytes_written;
11680 * p_data_written = l_nb_bytes_written;
11682 /* Writing Psot in SOT marker */
11683 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
11685 if (l_cp->m_specific_param.m_enc.m_cinema){
11686 j2k_update_tlm(p_j2k,l_nb_bytes_written);
11692 opj_bool j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
11694 OPJ_UINT32 * p_data_written,
11695 OPJ_UINT32 p_total_data_size,
11696 opj_stream_private_t *p_stream,
11697 struct opj_event_mgr * p_manager
11700 OPJ_UINT32 tilepartno=0;
11701 OPJ_UINT32 l_nb_bytes_written = 0;
11702 OPJ_UINT32 l_current_nb_bytes_written;
11703 OPJ_UINT32 l_part_tile_size;
11704 OPJ_UINT32 tot_num_tp;
11707 OPJ_BYTE * l_begin_data;
11708 opj_tcp_v2_t *l_tcp = 00;
11709 opj_tcd_v2_t * l_tcd = 00;
11710 opj_cp_v2_t * l_cp = 00;
11713 l_tcd = p_j2k->m_tcd;
11714 l_cp = &(p_j2k->m_cp);
11715 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
11717 /*Get number of tile parts*/
11718 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
11720 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
11721 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11722 l_current_nb_bytes_written = 0;
11723 l_part_tile_size = 0;
11724 l_begin_data = p_data;
11726 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11730 l_nb_bytes_written += l_current_nb_bytes_written;
11731 p_data += l_current_nb_bytes_written;
11732 p_total_data_size -= l_current_nb_bytes_written;
11733 l_part_tile_size += l_nb_bytes_written;
11735 l_current_nb_bytes_written = 0;
11736 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11740 p_data += l_current_nb_bytes_written;
11741 l_nb_bytes_written += l_current_nb_bytes_written;
11742 p_total_data_size -= l_current_nb_bytes_written;
11743 l_part_tile_size += l_nb_bytes_written;
11745 /* Writing Psot in SOT marker */
11746 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11748 if (l_cp->m_specific_param.m_enc.m_cinema) {
11749 j2k_update_tlm(p_j2k,l_part_tile_size);
11752 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11755 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
11756 l_tcd->cur_pino = pino;
11758 /*Get number of tile parts*/
11759 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
11760 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11761 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11762 l_current_nb_bytes_written = 0;
11763 l_part_tile_size = 0;
11764 l_begin_data = p_data;
11766 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11770 l_nb_bytes_written += l_current_nb_bytes_written;
11771 p_data += l_current_nb_bytes_written;
11772 p_total_data_size -= l_current_nb_bytes_written;
11773 l_part_tile_size += l_current_nb_bytes_written;
11775 l_current_nb_bytes_written = 0;
11777 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11781 l_nb_bytes_written += l_current_nb_bytes_written;
11782 p_data += l_current_nb_bytes_written;
11783 p_total_data_size -= l_current_nb_bytes_written;
11784 l_part_tile_size += l_current_nb_bytes_written;
11786 /* Writing Psot in SOT marker */
11787 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11789 if (l_cp->m_specific_param.m_enc.m_cinema) {
11790 j2k_update_tlm(p_j2k,l_part_tile_size);
11793 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11797 *p_data_written = l_nb_bytes_written;
11803 * Writes the updated tlm.
11805 * @param p_stream the stream to write data to.
11806 * @param p_j2k J2K codec.
11807 * @param p_manager the user event manager.
11809 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11810 struct opj_stream_private *p_stream,
11811 struct opj_event_mgr * p_manager )
11813 OPJ_UINT32 l_tlm_size;
11814 OPJ_SIZE_T l_tlm_position, l_current_position;
11816 /* preconditions */
11817 assert(p_j2k != 00);
11818 assert(p_manager != 00);
11819 assert(p_stream != 00);
11821 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11822 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11823 l_current_position = opj_stream_tell(p_stream);
11825 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11829 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) {
11833 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11842 * Ends the encoding, i.e. frees memory.
11844 * @param p_stream the stream to write data to.
11845 * @param p_j2k J2K codec.
11846 * @param p_manager the user event manager.
11848 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11849 struct opj_stream_private *p_stream,
11850 struct opj_event_mgr * p_manager )
11852 /* preconditions */
11853 assert(p_j2k != 00);
11854 assert(p_manager != 00);
11855 assert(p_stream != 00);
11857 tcd_destroy_v2(p_j2k->m_tcd);
11860 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11861 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11862 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11863 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11866 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11867 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11868 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11871 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11877 * Destroys the memory associated with the decoding of headers.
11879 opj_bool j2k_destroy_header_memory (opj_j2k_v2_t * p_j2k,
11880 opj_stream_private_t *p_stream,
11881 opj_event_mgr_t * p_manager )
11883 /* preconditions */
11884 assert(p_j2k != 00);
11885 assert(p_stream != 00);
11886 assert(p_manager != 00);
11888 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11889 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11890 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11893 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11902 * @param p_stream the stream to write data to.
11903 * @param p_j2k J2K codec.
11904 * @param p_manager the user event manager.
11906 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11907 struct opj_stream_private *p_stream,
11908 struct opj_event_mgr * p_manager )
11910 opj_codestream_info_t * l_cstr_info = 00;
11912 /* preconditions */
11913 assert(p_j2k != 00);
11914 assert(p_manager != 00);
11915 assert(p_stream != 00);
11918 /* TODO mergeV2: check this part which use cstr_info */
11919 /*l_cstr_info = p_j2k->cstr_info;
11923 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));
11925 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
11926 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
11928 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
11930 l_cstr_info->tw = p_j2k->m_cp.tw;
11931 l_cstr_info->th = p_j2k->m_cp.th;
11933 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
11934 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
11935 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
11936 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
11938 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
11940 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
11942 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
11944 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
11945 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
11948 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
11950 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
11952 /*l_cstr_info->maxmarknum = 100;
11953 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
11954 l_cstr_info->marknum = 0;
11957 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);
11961 * Creates a tile-coder decoder.
11963 * @param p_stream the stream to write data to.
11964 * @param p_j2k J2K codec.
11965 * @param p_manager the user event manager.
11967 opj_bool j2k_create_tcd(opj_j2k_v2_t *p_j2k,
11968 struct opj_stream_private *p_stream,
11969 struct opj_event_mgr * p_manager )
11971 /* preconditions */
11972 assert(p_j2k != 00);
11973 assert(p_manager != 00);
11974 assert(p_stream != 00);
11976 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
11978 if (! p_j2k->m_tcd) {
11979 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
11983 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
11984 tcd_destroy_v2(p_j2k->m_tcd);
11995 * @param p_j2k the jpeg2000 codec.
11996 * @param p_stream the stream to write data to.
11997 * @param p_manager the user event manager.
11999 opj_bool j2k_write_tile (opj_j2k_v2_t * p_j2k,
12000 OPJ_UINT32 p_tile_index,
12002 OPJ_UINT32 p_data_size,
12003 opj_stream_private_t *p_stream,
12004 opj_event_mgr_t * p_manager )
12006 if (! j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
12007 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_pre_write_tile with tile index = %d\n", p_tile_index);
12011 if (! j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
12012 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while j2k_post_write_tile with tile index = %d\n", p_tile_index);