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 static void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k);
50 * The read header procedure.
52 static opj_bool opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
134 opj_stream_private_t *p_stream,
135 opj_event_mgr_t * 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 opj_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 opj_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 opj_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 opj_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 * opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_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 opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k );
321 static opj_bool opj_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 opj_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 opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
333 static void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data);
335 static opj_bool opj_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 opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k);
347 static opj_bool opj_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 opj_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 opj_j2k_get_end_header( opj_j2k_v2_t *p_j2k,
369 struct opj_stream_private *p_stream,
370 struct opj_event_mgr * p_manager );
372 static opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k);
375 * -----------------------------------------------------------------------
376 * -----------------------------------------------------------------------
377 * -----------------------------------------------------------------------
381 * Writes the SOC marker (Start Of Codestream)
383 * @param p_stream the stream to write data to.
384 * @param p_j2k J2K codec.
385 * @param p_manager the user event manager.
387 static opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
388 opj_stream_private_t *p_stream,
389 opj_event_mgr_t * p_manager );
392 * Reads a SOC marker (Start of Codestream)
393 * @param p_header_data the data contained in the SOC box.
394 * @param jp2 the jpeg2000 file codec.
395 * @param p_header_size the size of the data contained in the SOC marker.
396 * @param p_manager the user event manager.
398 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
399 opj_stream_private_t *p_stream,
400 opj_event_mgr_t * p_manager );
403 Write the SIZ marker (image and tile size)
404 @param j2k J2K handle
406 static void j2k_write_siz(opj_j2k_t *j2k);
409 * Writes the SIZ marker (image and tile size)
411 * @param p_stream the stream to write data to.
412 * @param p_j2k J2K codec.
413 * @param p_manager the user event manager.
415 static opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
416 struct opj_stream_private *p_stream,
417 struct opj_event_mgr * p_manager );
420 * Reads a SIZ marker (image and tile size)
421 * @param p_header_data the data contained in the SIZ box.
422 * @param jp2 the jpeg2000 file codec.
423 * @param p_header_size the size of the data contained in the SIZ marker.
424 * @param p_manager the user event manager.
426 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
427 OPJ_BYTE * p_header_data,
428 OPJ_UINT32 p_header_size,
429 opj_event_mgr_t * p_manager);
432 Write the COM marker (comment)
433 @param j2k J2K handle
435 static void j2k_write_com(opj_j2k_t *j2k);
438 * Writes the COM marker (comment)
440 * @param p_stream the stream to write data to.
441 * @param p_j2k J2K codec.
442 * @param p_manager the user event manager.
444 static opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
445 struct opj_stream_private *p_stream,
446 struct opj_event_mgr * p_manager );
449 * Reads a COM marker (comments)
450 * @param p_header_data the data contained in the COM box.
451 * @param jp2 the jpeg2000 file codec.
452 * @param p_header_size the size of the data contained in the COM marker.
453 * @param p_manager the user event manager.
455 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
456 OPJ_BYTE * p_header_data,
457 OPJ_UINT32 p_header_size,
458 opj_event_mgr_t * p_manager );
460 Write the value concerning the specified component in the marker COD and COC
461 @param j2k J2K handle
462 @param compno Number of the component concerned by the information written
464 static void j2k_write_cox(opj_j2k_t *j2k, int compno);
467 Write the COD marker (coding style default)
468 @param j2k J2K handle
470 static void j2k_write_cod(opj_j2k_t *j2k);
473 * Writes the COD marker (Coding style default)
475 * @param p_stream the stream to write data to.
476 * @param p_j2k J2K codec.
477 * @param p_manager the user event manager.
479 static opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
480 struct opj_stream_private *p_stream,
481 struct opj_event_mgr * p_manager );
484 * Reads a COD marker (Coding Styke defaults)
485 * @param p_header_data the data contained in the COD box.
486 * @param p_j2k the jpeg2000 codec.
487 * @param p_header_size the size of the data contained in the COD marker.
488 * @param p_manager the user event manager.
490 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
491 OPJ_BYTE * p_header_data,
492 OPJ_UINT32 p_header_size,
493 opj_event_mgr_t * p_manager);
496 Write the COC marker (coding style component)
497 @param j2k J2K handle
498 @param compno Number of the component concerned by the information written
500 static void j2k_write_coc(opj_j2k_t *j2k, int compno);
503 * Writes the COC marker (Coding style component)
505 * @param p_comp_number the index of the component to output.
506 * @param p_stream the stream to write data to.
507 * @param p_j2k J2K codec.
508 * @param p_manager the user event manager.
510 static opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
511 OPJ_UINT32 p_comp_number,
512 struct opj_stream_private *p_stream,
513 struct opj_event_mgr * p_manager
516 * Writes the COC marker (Coding style component)
518 * @param p_comp_no the index of the component to output.
519 * @param p_stream the stream to write data to.
520 * @param p_j2k J2K codec.
521 * @param p_manager the user event manager.
523 static void j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
524 OPJ_UINT32 p_comp_no,
526 OPJ_UINT32 * p_data_written,
527 struct opj_event_mgr * p_manager );
530 * Gets the maximum size taken by a coc.
532 * @param p_j2k the jpeg2000 codec to use.
534 static OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
538 * Reads a COC marker (Coding Style Component)
539 * @param p_header_data the data contained in the COC box.
540 * @param p_j2k the jpeg2000 codec.
541 * @param p_header_size the size of the data contained in the COC marker.
542 * @param p_manager the user event manager.
544 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
545 OPJ_BYTE * p_header_data,
546 OPJ_UINT32 p_header_size,
547 opj_event_mgr_t * p_manager );
550 Write the value concerning the specified component in the marker QCD and QCC
551 @param j2k J2K handle
552 @param compno Number of the component concerned by the information written
554 static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
557 Write the QCD marker (quantization default)
558 @param j2k J2K handle
560 static void j2k_write_qcd(opj_j2k_t *j2k);
563 * Writes the QCD marker (quantization default)
565 * @param p_comp_number the index of the component to output.
566 * @param p_stream the stream to write data to.
567 * @param p_j2k J2K codec.
568 * @param p_manager the user event manager.
570 static opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
571 struct opj_stream_private *p_stream,
572 struct opj_event_mgr * p_manager );
575 * Reads a QCD marker (Quantization defaults)
576 * @param p_header_data the data contained in the QCD box.
577 * @param p_j2k the jpeg2000 codec.
578 * @param p_header_size the size of the data contained in the QCD marker.
579 * @param p_manager the user event manager.
581 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
582 OPJ_BYTE * p_header_data,
583 OPJ_UINT32 p_header_size,
584 opj_event_mgr_t * p_manager );
587 Write the QCC marker (quantization component)
588 @param j2k J2K handle
589 @param compno Number of the component concerned by the information written
591 static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
594 * Writes the QCC marker (quantization component)
596 * @param p_comp_no the index of the component to output.
597 * @param p_stream the stream to write data to.
598 * @param p_j2k J2K codec.
599 * @param p_manager the user event manager.
601 static opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
602 OPJ_UINT32 p_comp_no,
603 struct opj_stream_private *p_stream,
604 struct opj_event_mgr * p_manager );
607 * Writes the QCC marker (quantization component)
609 * @param p_comp_no the index of the component to output.
610 * @param p_stream the stream to write data to.
611 * @param p_j2k J2K codec.
612 * @param p_manager the user event manager.
614 static void j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
615 OPJ_UINT32 p_comp_no,
617 OPJ_UINT32 * p_data_written,
618 struct opj_event_mgr * p_manager );
621 * Gets the maximum size taken by a qcc.
623 static OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
626 * Reads a QCC marker (Quantization component)
627 * @param p_header_data the data contained in the QCC box.
628 * @param p_j2k the jpeg2000 codec.
629 * @param p_header_size the size of the data contained in the QCC marker.
630 * @param p_manager the user event manager.
632 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
633 OPJ_BYTE * p_header_data,
634 OPJ_UINT32 p_header_size,
635 opj_event_mgr_t * p_manager);
638 Write the POC marker (progression order change)
639 @param j2k J2K handle
641 static void j2k_write_poc(opj_j2k_t *j2k);
644 * Writes the POC marker (Progression Order Change)
646 * @param p_stream the stream to write data to.
647 * @param p_j2k J2K codec.
648 * @param p_manager the user event manager.
650 static opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
651 struct opj_stream_private *p_stream,
652 struct opj_event_mgr * p_manager );
655 * Writes the POC marker (Progression Order Change)
657 * @param p_stream the stream to write data to.
658 * @param p_j2k J2K codec.
659 * @param p_manager the user event manager.
661 static void j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
663 OPJ_UINT32 * p_data_written,
664 struct opj_event_mgr * p_manager );
667 * Gets the maximum size taken by the writting of a POC.
669 static OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
672 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
674 static OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
677 * Gets the maximum size taken by the headers of the SOT.
679 * @param p_j2k the jpeg2000 codec to use.
681 static OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
684 * Reads a POC marker (Progression Order Change)
686 * @param p_header_data the data contained in the POC box.
687 * @param p_j2k the jpeg2000 codec.
688 * @param p_header_size the size of the data contained in the POC marker.
689 * @param p_manager the user event manager.
691 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
692 OPJ_BYTE * p_header_data,
693 OPJ_UINT32 p_header_size,
694 opj_event_mgr_t * p_manager );
697 * Reads a CRG marker (Component registration)
699 * @param p_header_data the data contained in the TLM box.
700 * @param p_j2k the jpeg2000 codec.
701 * @param p_header_size the size of the data contained in the TLM marker.
702 * @param p_manager the user event manager.
704 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
705 OPJ_BYTE * p_header_data,
706 OPJ_UINT32 p_header_size,
707 opj_event_mgr_t * p_manager );
709 * Reads a TLM marker (Tile Length Marker)
711 * @param p_header_data the data contained in the TLM box.
712 * @param p_j2k the jpeg2000 codec.
713 * @param p_header_size the size of the data contained in the TLM marker.
714 * @param p_manager the user event manager.
716 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
717 OPJ_BYTE * p_header_data,
718 OPJ_UINT32 p_header_size,
719 opj_event_mgr_t * p_manager);
722 * Writes the updated tlm.
724 * @param p_stream the stream to write data to.
725 * @param p_j2k J2K codec.
726 * @param p_manager the user event manager.
728 static opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
729 struct opj_stream_private *p_stream,
730 struct opj_event_mgr * p_manager );
733 * Reads a PLM marker (Packet length, main header marker)
735 * @param p_header_data the data contained in the TLM box.
736 * @param p_j2k the jpeg2000 codec.
737 * @param p_header_size the size of the data contained in the TLM marker.
738 * @param p_manager the user event manager.
740 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
741 OPJ_BYTE * p_header_data,
742 OPJ_UINT32 p_header_size,
743 opj_event_mgr_t * p_manager);
745 * Reads a PLT marker (Packet length, tile-part header)
747 * @param p_header_data the data contained in the PLT box.
748 * @param p_j2k the jpeg2000 codec.
749 * @param p_header_size the size of the data contained in the PLT marker.
750 * @param p_manager the user event manager.
752 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
753 OPJ_BYTE * p_header_data,
754 OPJ_UINT32 p_header_size,
755 opj_event_mgr_t * p_manager );
759 * Reads a PPM marker (Packed packet headers, main header)
761 * @param p_header_data the data contained in the POC box.
762 * @param p_j2k the jpeg2000 codec.
763 * @param p_header_size the size of the data contained in the POC marker.
764 * @param p_manager the user event manager.
767 static opj_bool j2k_read_ppm_v2 (
769 OPJ_BYTE * p_header_data,
770 OPJ_UINT32 p_header_size,
771 struct opj_event_mgr * p_manager
775 static opj_bool j2k_read_ppm_v3 (
777 OPJ_BYTE * p_header_data,
778 OPJ_UINT32 p_header_size,
779 struct opj_event_mgr * p_manager
784 * Reads a PPT marker (Packed packet headers, tile-part header)
786 * @param p_header_data the data contained in the PPT box.
787 * @param p_j2k the jpeg2000 codec.
788 * @param p_header_size the size of the data contained in the PPT marker.
789 * @param p_manager the user event manager.
791 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
792 OPJ_BYTE * p_header_data,
793 OPJ_UINT32 p_header_size,
794 opj_event_mgr_t * p_manager );
796 Write the TLM marker (Mainheader)
797 @param j2k J2K handle
799 static void j2k_write_tlm(opj_j2k_t *j2k);
802 * Writes the TLM marker (Tile Length Marker)
804 * @param p_stream the stream to write data to.
805 * @param p_j2k J2K codec.
806 * @param p_manager the user event manager.
808 static opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
809 struct opj_stream_private *p_stream,
810 struct opj_event_mgr * p_manager );
813 Write the SOT marker (start of tile-part)
814 @param j2k J2K handle
816 static void j2k_write_sot(opj_j2k_t *j2k);
819 * Writes the SOT marker (Start of tile-part)
821 * @param p_stream the stream to write data to.
822 * @param p_j2k J2K codec.
823 * @param p_manager the user event manager.
825 static opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
827 OPJ_UINT32 * p_data_written,
828 const struct opj_stream_private *p_stream,
829 struct opj_event_mgr * p_manager );
832 * Reads a PPT marker (Packed packet headers, tile-part header)
834 * @param p_header_data the data contained in the PPT box.
835 * @param p_j2k the jpeg2000 codec.
836 * @param p_header_size the size of the data contained in the PPT marker.
837 * @param p_manager the user event manager.
839 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
840 OPJ_BYTE * p_header_data,
841 OPJ_UINT32 p_header_size,
842 opj_event_mgr_t * p_manager );
845 Write the SOD marker (start of data)
846 @param j2k J2K handle
847 @param tile_coder Pointer to a TCD handle
849 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
852 * Writes the SOD marker (Start of data)
854 * @param p_stream the stream to write data to.
855 * @param p_j2k J2K codec.
856 * @param p_manager the user event manager.
858 static opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
859 struct opj_tcd_v2 * p_tile_coder,
861 OPJ_UINT32 * p_data_written,
862 OPJ_UINT32 p_total_data_size,
863 const struct opj_stream_private *p_stream,
864 struct opj_event_mgr * p_manager );
867 * Reads a SOD marker (Start Of Data)
869 * @param p_header_data the data contained in the SOD box.
870 * @param p_j2k the jpeg2000 codec.
871 * @param p_header_size the size of the data contained in the SOD marker.
872 * @param p_manager the user event manager.
874 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
875 opj_stream_private_t *p_stream,
876 opj_event_mgr_t * p_manager );
879 * Updates the Tile Length Marker.
881 void opj_j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
883 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
884 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
886 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
887 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
891 Write the RGN marker (region-of-interest)
892 @param j2k J2K handle
893 @param compno Number of the component concerned by the information written
894 @param tileno Number of the tile concerned by the information written
896 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
899 * Writes the RGN marker (Region Of Interest)
901 * @param p_tile_no the tile to output
902 * @param p_comp_no the component to output
903 * @param p_stream the stream to write data to.
904 * @param p_j2k J2K codec.
905 * @param p_manager the user event manager.
907 static opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
908 OPJ_UINT32 p_tile_no,
909 OPJ_UINT32 p_comp_no,
910 struct opj_stream_private *p_stream,
911 struct opj_event_mgr * p_manager );
914 * Reads a RGN marker (Region Of Interest)
916 * @param p_header_data the data contained in the POC box.
917 * @param p_j2k the jpeg2000 codec.
918 * @param p_header_size the size of the data contained in the POC marker.
919 * @param p_manager the user event manager.
921 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
922 OPJ_BYTE * p_header_data,
923 OPJ_UINT32 p_header_size,
924 opj_event_mgr_t * p_manager );
927 * Writes the EOC marker (End of Codestream)
929 * @param p_stream the stream to write data to.
930 * @param p_j2k J2K codec.
931 * @param p_manager the user event manager.
933 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
934 struct opj_stream_private *p_stream,
935 struct opj_event_mgr * p_manager );
938 Write the EOC marker (end of codestream)
939 @param j2k J2K handle
941 static void j2k_write_eoc(opj_j2k_t *j2k);
943 Read the EOC marker (end of codestream)
944 @param j2k J2K handle
946 static void j2k_read_eoc(opj_j2k_t *j2k);
949 * Reads a EOC marker (End Of Codestream)
951 * @param p_header_data the data contained in the SOD box.
952 * @param p_j2k the jpeg2000 codec.
953 * @param p_header_size the size of the data contained in the SOD marker.
954 * @param p_manager the user event manager.
957 static opj_bool j2k_read_eoc_v2 (
959 struct opj_stream_private *p_stream,
960 struct opj_event_mgr * p_manager
967 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
969 * @param p_stream the stream to write data to.
970 * @param p_j2k J2K codec.
971 * @param p_manager the user event manager.
973 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
974 struct opj_stream_private *p_stream,
975 struct opj_event_mgr * p_manager );
980 * @param p_stream the stream to write data to.
981 * @param p_j2k J2K codec.
982 * @param p_manager the user event manager.
984 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
985 struct opj_stream_private *p_stream,
986 struct opj_event_mgr * p_manager );
989 Add main header marker information
990 @param cstr_info Codestream information structure
991 @param type marker type
992 @param pos byte offset of marker segment
993 @param len length of marker segment
995 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
997 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
999 Add tile header marker information
1000 @param tileno tile index number
1001 @param cstr_info Codestream information structure
1002 @param type marker type
1003 @param pos byte offset of marker segment
1004 @param len length of marker segment
1006 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
1008 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);
1011 * Reads an unknown marker
1013 * @param p_stream the stream object to read from.
1014 * @param p_j2k the jpeg2000 codec.
1015 * @param p_manager the user event manager.
1017 * @return true if the marker could be deduced.
1019 static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
1020 opj_stream_private_t *p_stream,
1021 OPJ_UINT32 *output_marker,
1022 opj_event_mgr_t * p_manager );
1025 * Writes the MCT marker (Multiple Component Transform)
1027 * @param p_stream the stream to write data to.
1028 * @param p_j2k J2K codec.
1029 * @param p_manager the user event manager.
1031 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
1032 opj_mct_data_t * p_mct_record,
1033 struct opj_stream_private *p_stream,
1034 struct opj_event_mgr * p_manager );
1037 * Reads a MCT marker (Multiple Component Transform)
1039 * @param p_header_data the data contained in the MCT box.
1040 * @param p_j2k the jpeg2000 codec.
1041 * @param p_header_size the size of the data contained in the MCT marker.
1042 * @param p_manager the user event manager.
1044 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
1045 OPJ_BYTE * p_header_data,
1046 OPJ_UINT32 p_header_size,
1047 opj_event_mgr_t * p_manager );
1050 * Writes the MCC marker (Multiple Component Collection)
1052 * @param p_stream the stream to write data to.
1053 * @param p_j2k J2K codec.
1054 * @param p_manager the user event manager.
1056 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
1057 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
1058 struct opj_stream_private *p_stream,
1059 struct opj_event_mgr * p_manager );
1063 * Reads a MCC marker (Multiple Component Collection)
1065 * @param p_header_data the data contained in the MCC box.
1066 * @param p_j2k the jpeg2000 codec.
1067 * @param p_header_size the size of the data contained in the MCC marker.
1068 * @param p_manager the user event manager.
1070 static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
1071 OPJ_BYTE * p_header_data,
1072 OPJ_UINT32 p_header_size,
1073 opj_event_mgr_t * p_manager );
1076 * Writes the MCO marker (Multiple component transformation ordering)
1078 * @param p_stream the stream to write data to.
1079 * @param p_j2k J2K codec.
1080 * @param p_manager the user event manager.
1082 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1083 struct opj_stream_private *p_stream,
1084 struct opj_event_mgr * p_manager );
1087 * Reads a MCO marker (Multiple Component Transform Ordering)
1089 * @param p_header_data the data contained in the MCO box.
1090 * @param p_j2k the jpeg2000 codec.
1091 * @param p_header_size the size of the data contained in the MCO marker.
1092 * @param p_manager the user event manager.
1094 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1095 OPJ_BYTE * p_header_data,
1096 OPJ_UINT32 p_header_size,
1097 opj_event_mgr_t * p_manager );
1099 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1101 static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1102 static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1103 static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1104 static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1106 static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1107 static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1108 static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1109 static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1111 static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1112 static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1113 static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1114 static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1118 * Ends the encoding, i.e. frees memory.
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_end_encoding( opj_j2k_v2_t *p_j2k,
1125 struct opj_stream_private *p_stream,
1126 struct opj_event_mgr * p_manager );
1129 * Writes the CBD marker (Component bit depth definition)
1131 * @param p_stream the stream to write data to.
1132 * @param p_j2k J2K codec.
1133 * @param p_manager the user event manager.
1135 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1136 struct opj_stream_private *p_stream,
1137 struct opj_event_mgr * p_manager );
1140 * Reads a CBD marker (Component bit depth definition)
1141 * @param p_header_data the data contained in the CBD box.
1142 * @param p_j2k the jpeg2000 codec.
1143 * @param p_header_size the size of the data contained in the CBD marker.
1144 * @param p_manager the user event manager.
1146 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1147 OPJ_BYTE * p_header_data,
1148 OPJ_UINT32 p_header_size,
1149 opj_event_mgr_t * p_manager);
1152 * Writes the image components.
1154 * @param p_stream the stream to write data to.
1155 * @param p_j2k J2K codec.
1156 * @param p_manager the user event manager.
1158 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1159 struct opj_stream_private *p_stream,
1160 struct opj_event_mgr * p_manager );
1163 * Writes regions of interests.
1165 * @param p_stream the stream to write data to.
1166 * @param p_j2k J2K codec.
1167 * @param p_manager the user event manager.
1169 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1170 struct opj_stream_private *p_stream,
1171 struct opj_event_mgr * p_manager );
1176 * @param p_stream the stream to write data to.
1177 * @param p_j2k J2K codec.
1178 * @param p_manager the user event manager.
1180 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1181 struct opj_stream_private *p_stream,
1182 struct opj_event_mgr * p_manager );
1185 * Checks the progression order changes values. Tells of the poc given as input are valid.
1186 * A nice message is outputted at errors.
1188 * @param p_pocs the progression order changes.
1189 * @param p_nb_pocs the number of progression order changes.
1190 * @param p_nb_resolutions the number of resolutions.
1191 * @param numcomps the number of components
1192 * @param numlayers the number of layers.
1194 * @return true if the pocs are valid.
1196 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1197 OPJ_UINT32 p_nb_pocs,
1198 OPJ_UINT32 p_nb_resolutions,
1199 OPJ_UINT32 numcomps,
1200 OPJ_UINT32 numlayers,
1201 opj_event_mgr_t * p_manager);
1204 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1206 * @param cp the coding parameters.
1207 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1208 * @param tileno the given tile.
1210 * @return the number of tile parts.
1212 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1214 /** mem allocation for TLM marker*/
1215 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1218 * Calculates the total number of tile parts needed by the encoder to
1219 * encode such an image. If not enough memory is available, then the function return false.
1221 * @param p_nb_tiles pointer that will hold the number of tile parts.
1222 * @param cp the coding parameters for the image.
1223 * @param image the image to encode.
1224 * @param p_j2k the p_j2k encoder.
1225 * @param p_manager the user event manager.
1227 * @return true if the function was successful, false else.
1229 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1231 OPJ_UINT32 * p_nb_tiles,
1233 opj_event_mgr_t * p_manager);
1235 static void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1237 static void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1239 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1245 /* ----------------------------------------------------------------------- */
1246 typedef struct j2k_prog_order{
1247 OPJ_PROG_ORDER enum_prog;
1251 j2k_prog_order_t j2k_prog_order_list[] = {
1257 {(OPJ_PROG_ORDER)-1, ""}
1265 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1273 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1275 const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1277 opj_j2k_read_int16_to_float,
1278 opj_j2k_read_int32_to_float,
1279 opj_j2k_read_float32_to_float,
1280 opj_j2k_read_float64_to_float
1283 const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1285 opj_j2k_read_int16_to_int32,
1286 opj_j2k_read_int32_to_int32,
1287 opj_j2k_read_float32_to_int32,
1288 opj_j2k_read_float64_to_int32
1291 const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1293 opj_j2k_write_float_to_int16,
1294 opj_j2k_write_float_to_int32,
1295 opj_j2k_write_float_to_float,
1296 opj_j2k_write_float_to_float64
1299 typedef struct opj_dec_memory_marker_handler
1303 /** value of the state when the marker can appear */
1305 /** action linked to the marker */
1306 opj_bool (*handler) (
1307 opj_j2k_v2_t *p_j2k,
1308 OPJ_BYTE * p_header_data,
1309 OPJ_UINT32 p_header_size,
1310 struct opj_event_mgr * p_manager
1313 opj_dec_memory_marker_handler_t;
1315 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1318 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1319 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1320 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1321 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1322 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1323 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1324 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1325 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1326 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1327 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1328 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1329 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1330 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1332 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1333 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1334 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1335 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1336 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1337 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1339 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1340 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1341 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1342 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1343 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1344 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1345 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1346 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1347 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1348 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1349 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1350 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1351 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1353 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1354 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1355 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1356 {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1357 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1358 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1360 #ifdef TODO_MS /* FIXME */
1361 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1362 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1363 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1364 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1366 #endif /* USE_JPWL */
1368 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1369 {J2K_MS_INSEC, 0, j2k_read_insec}
1370 #endif /* USE_JPSEC */
1371 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1376 void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1378 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1379 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1383 for (i=0;i<p_nb_elem;++i) {
1384 opj_read_bytes(l_src_data,&l_temp,2);
1386 l_src_data+=sizeof(OPJ_INT16);
1388 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1392 void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1394 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1395 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1399 for (i=0;i<p_nb_elem;++i) {
1400 opj_read_bytes(l_src_data,&l_temp,4);
1402 l_src_data+=sizeof(OPJ_INT32);
1404 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1408 void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1410 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1411 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1415 for (i=0;i<p_nb_elem;++i) {
1416 opj_read_float(l_src_data,&l_temp);
1418 l_src_data+=sizeof(OPJ_FLOAT32);
1420 *(l_dest_data++) = l_temp;
1424 void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1426 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1427 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1431 for (i=0;i<p_nb_elem;++i) {
1432 opj_read_double(l_src_data,&l_temp);
1434 l_src_data+=sizeof(OPJ_FLOAT64);
1436 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1440 void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1442 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1443 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1447 for (i=0;i<p_nb_elem;++i) {
1448 opj_read_bytes(l_src_data,&l_temp,2);
1450 l_src_data+=sizeof(OPJ_INT16);
1452 *(l_dest_data++) = (OPJ_INT32) l_temp;
1456 void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1458 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1459 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1463 for (i=0;i<p_nb_elem;++i) {
1464 opj_read_bytes(l_src_data,&l_temp,4);
1466 l_src_data+=sizeof(OPJ_INT32);
1468 *(l_dest_data++) = (OPJ_INT32) l_temp;
1472 void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1474 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1475 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1479 for (i=0;i<p_nb_elem;++i) {
1480 opj_read_float(l_src_data,&l_temp);
1482 l_src_data+=sizeof(OPJ_FLOAT32);
1484 *(l_dest_data++) = (OPJ_INT32) l_temp;
1488 void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1490 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1491 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1495 for (i=0;i<p_nb_elem;++i) {
1496 opj_read_double(l_src_data,&l_temp);
1498 l_src_data+=sizeof(OPJ_FLOAT64);
1500 *(l_dest_data++) = (OPJ_INT32) l_temp;
1504 void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1506 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1507 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1511 for (i=0;i<p_nb_elem;++i) {
1512 l_temp = (OPJ_UINT32) *(l_src_data++);
1514 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1516 l_dest_data+=sizeof(OPJ_INT16);
1520 void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1522 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1523 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1527 for (i=0;i<p_nb_elem;++i) {
1528 l_temp = (OPJ_UINT32) *(l_src_data++);
1530 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1532 l_dest_data+=sizeof(OPJ_INT32);
1536 void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1538 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1539 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1543 for (i=0;i<p_nb_elem;++i) {
1544 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1546 opj_write_float(l_dest_data,l_temp);
1548 l_dest_data+=sizeof(OPJ_FLOAT32);
1552 void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1554 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1555 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1559 for (i=0;i<p_nb_elem;++i) {
1560 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1562 opj_write_double(l_dest_data,l_temp);
1564 l_dest_data+=sizeof(OPJ_FLOAT64);
1570 * Converts an enum type progression order to string type.
1572 * @param prg_order the progression order to get.
1574 * @return the string representation of the given progression order.
1576 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1577 j2k_prog_order_t *po;
1578 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1579 if(po->enum_prog == prg_order){
1580 return po->str_prog;
1583 return po->str_prog;
1587 * Checks the progression order changes values. Tells if the poc given as input are valid.
1589 * @param p_pocs the progression order changes.
1590 * @param p_nb_pocs the number of progression order changes.
1591 * @param p_nb_resolutions the number of resolutions.
1592 * @param numcomps the number of components
1593 * @param numlayers the number of layers.
1594 * @param p_manager the user event manager.
1596 * @return true if the pocs are valid.
1598 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1599 OPJ_UINT32 p_nb_pocs,
1600 OPJ_UINT32 p_nb_resolutions,
1601 OPJ_UINT32 p_num_comps,
1602 OPJ_UINT32 p_num_layers,
1603 opj_event_mgr_t * p_manager)
1605 OPJ_UINT32* packet_array;
1606 OPJ_UINT32 index , resno, compno, layno;
1608 OPJ_UINT32 step_c = 1;
1609 OPJ_UINT32 step_r = p_num_comps * step_c;
1610 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1611 opj_bool loss = OPJ_FALSE;
1612 OPJ_UINT32 layno0 = 0;
1614 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1615 if (packet_array == 00) {
1616 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1619 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1621 if (p_nb_pocs == 0) {
1625 index = step_r * p_pocs->resno0;
1626 // take each resolution for each poc
1627 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1629 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1631 // take each comp of each resolution for each poc
1632 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1633 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1635 // and finally take each layer of each res of ...
1636 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1637 //index = step_r * resno + step_c * compno + step_l * layno;
1638 packet_array[comp_index] = 1;
1639 comp_index += step_l;
1642 res_index += step_c;
1649 // iterate through all the pocs
1650 for (i = 1; i < p_nb_pocs ; ++i) {
1651 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1653 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1654 index = step_r * p_pocs->resno0;
1656 // take each resolution for each poc
1657 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1658 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1660 // take each comp of each resolution for each poc
1661 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1662 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1664 // and finally take each layer of each res of ...
1665 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1666 //index = step_r * resno + step_c * compno + step_l * layno;
1667 packet_array[comp_index] = 1;
1668 comp_index += step_l;
1671 res_index += step_c;
1681 for (layno = 0; layno < p_num_layers ; ++layno) {
1682 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1683 for (compno = 0; compno < p_num_comps; ++compno) {
1684 loss |= (packet_array[index]!=1);
1685 //index = step_r * resno + step_c * compno + step_l * layno;
1692 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1695 opj_free(packet_array);
1700 /* ----------------------------------------------------------------------- */
1701 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1704 int tpnum=1,tpend=0;
1705 opj_tcp_t *tcp = &cp->tcps[tileno];
1706 prog = j2k_convert_progression_order(tcp->prg);
1711 if( cp->tp_flag == prog[i] ){
1712 tpend=1;cp->tp_pos=i;
1716 tpnum= tpnum * tcp->pocs[pino].compE;
1719 tpnum= tpnum * tcp->pocs[pino].resE;
1722 tpnum= tpnum * tcp->pocs[pino].prcE;
1725 tpnum= tpnum * tcp->pocs[pino].layE;
1737 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1739 * @param cp the coding parameters.
1740 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1741 * @param tileno the given tile.
1743 * @return the number of tile parts.
1745 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1747 const OPJ_CHAR *prog = 00;
1749 OPJ_UINT32 tpnum = 1;
1750 opj_tcp_v2_t *tcp = 00;
1751 opj_poc_t * l_current_poc = 00;
1754 assert(tileno < (cp->tw * cp->th));
1755 assert(pino < (cp->tcps[tileno].numpocs + 1));
1757 /* get the given tile coding parameter */
1758 tcp = &cp->tcps[tileno];
1761 l_current_poc = &(tcp->pocs[pino]);
1762 assert(l_current_poc != 0);
1764 /* get the progression order as a character string */
1765 prog = j2k_convert_progression_order(tcp->prg);
1766 assert(strlen(prog) > 0);
1768 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1772 /* component wise */
1774 tpnum *= l_current_poc->compE;
1776 /* resolution wise */
1778 tpnum *= l_current_poc->resE;
1782 tpnum *= l_current_poc->prcE;
1786 tpnum *= l_current_poc->layE;
1789 /* whould we split here ? */
1790 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1791 cp->m_specific_param.m_enc.m_tp_pos=i;
1803 /** mem allocation for TLM marker*/
1804 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1805 int pino,tileno,totnum_tp=0;
1807 OPJ_ARG_NOT_USED(img_numcomp);
1809 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1810 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1811 int cur_totnum_tp = 0;
1812 opj_tcp_t *tcp = &cp->tcps[tileno];
1813 for(pino = 0; pino <= tcp->numpocs; pino++) {
1815 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1816 if(!pi) { return -1;}
1817 tp_num = j2k_get_num_tp(cp,pino,tileno);
1818 totnum_tp = totnum_tp + tp_num;
1819 cur_totnum_tp = cur_totnum_tp + tp_num;
1820 pi_destroy(pi, cp, tileno);
1822 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1824 if (j2k->cstr_info) {
1825 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1826 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1834 * Calculates the total number of tile parts needed by the encoder to
1835 * encode such an image. If not enough memory is available, then the function return false.
1837 * @param p_nb_tiles pointer that will hold the number of tile parts.
1838 * @param cp the coding parameters for the image.
1839 * @param image the image to encode.
1840 * @param p_j2k the p_j2k encoder.
1841 * @param p_manager the user event manager.
1843 * @return true if the function was successful, false else.
1845 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1847 OPJ_UINT32 * p_nb_tiles,
1849 opj_event_mgr_t * p_manager)
1851 OPJ_UINT32 pino,tileno;
1852 OPJ_UINT32 l_nb_tiles;
1856 assert(p_nb_tiles != 00);
1858 assert(image != 00);
1859 assert(p_j2k != 00);
1860 assert(p_manager != 00);
1862 l_nb_tiles = cp->tw * cp->th;
1867 /* TODO mergeV2: check this part which use cstr_info */
1868 /*if (p_j2k->cstr_info) {
1869 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1871 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1872 OPJ_UINT32 cur_totnum_tp = 0;
1874 pi_update_encoding_parameters(image,cp,tileno);
1876 for (pino = 0; pino <= tcp->numpocs; ++pino)
1878 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1880 *p_nb_tiles = *p_nb_tiles + tp_num;
1882 cur_totnum_tp += tp_num;
1885 tcp->m_nb_tile_parts = cur_totnum_tp;
1887 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1888 if (l_info_tile_ptr->tp == 00) {
1892 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1894 l_info_tile_ptr->num_tps = cur_totnum_tp;
1901 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1902 OPJ_UINT32 cur_totnum_tp = 0;
1904 pi_update_encoding_parameters(image,cp,tileno);
1906 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1907 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1909 *p_nb_tiles = *p_nb_tiles + tp_num;
1911 cur_totnum_tp += tp_num;
1913 tcp->m_nb_tile_parts = cur_totnum_tp;
1923 * Writes the SOC marker (Start Of Codestream)
1925 * @param p_stream the stream to write data to.
1926 * @param p_j2k J2K codec.
1927 * @param p_manager the user event manager.
1929 opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
1930 opj_stream_private_t *p_stream,
1931 opj_event_mgr_t * p_manager )
1933 /* 2 bytes will be written */
1934 OPJ_BYTE * l_start_stream = 00;
1937 assert(p_stream != 00);
1938 assert(p_j2k != 00);
1939 assert(p_manager != 00);
1941 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1943 /* write SOC identifier */
1944 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1946 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1952 /* update markers struct */
1954 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1956 assert( 0 && "TODO" );
1957 #endif /* USE_JPWL */
1966 * Reads a SOC marker (Start of Codestream)
1967 * @param p_header_data the data contained in the SOC box.
1968 * @param jp2 the jpeg2000 file codec.
1969 * @param p_header_size the size of the data contained in the SOC marker.
1970 * @param p_manager the user event manager.
1972 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
1973 opj_stream_private_t *p_stream,
1974 opj_event_mgr_t * p_manager
1977 OPJ_BYTE l_data [2];
1978 OPJ_UINT32 l_marker;
1981 assert(p_j2k != 00);
1982 assert(p_manager != 00);
1983 assert(p_stream != 00);
1985 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1989 opj_read_bytes(l_data,&l_marker,2);
1990 if (l_marker != J2K_MS_SOC) {
1994 /* Next marker should be a SIZ marker in the main header */
1995 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1997 /* FIXME move it in a index structure included in p_j2k*/
1998 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
2000 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
2002 /* Add the marker to the codestream index*/
2003 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
2008 static void j2k_write_siz(opj_j2k_t *j2k) {
2012 opj_cio_t *cio = j2k->cio;
2013 opj_image_t *image = j2k->image;
2014 opj_cp_t *cp = j2k->cp;
2016 cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
2017 lenp = cio_tell(cio);
2019 cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
2020 cio_write(cio, image->x1, 4); /* Xsiz */
2021 cio_write(cio, image->y1, 4); /* Ysiz */
2022 cio_write(cio, image->x0, 4); /* X0siz */
2023 cio_write(cio, image->y0, 4); /* Y0siz */
2024 cio_write(cio, cp->tdx, 4); /* XTsiz */
2025 cio_write(cio, cp->tdy, 4); /* YTsiz */
2026 cio_write(cio, cp->tx0, 4); /* XT0siz */
2027 cio_write(cio, cp->ty0, 4); /* YT0siz */
2028 cio_write(cio, image->numcomps, 2); /* Csiz */
2029 for (i = 0; i < image->numcomps; i++) {
2030 cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
2031 cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
2032 cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
2034 len = cio_tell(cio) - lenp;
2035 cio_seek(cio, lenp);
2036 cio_write(cio, len, 2); /* Lsiz */
2037 cio_seek(cio, lenp + len);
2040 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
2044 * Writes the SIZ marker (image and tile size)
2046 * @param p_stream the stream to write data to.
2047 * @param p_j2k J2K codec.
2048 * @param p_manager the user event manager.
2050 opj_bool j2k_write_siz_v2( opj_j2k_v2_t *p_j2k,
2051 struct opj_stream_private *p_stream,
2052 struct opj_event_mgr * p_manager )
2055 OPJ_UINT32 l_size_len;
2056 OPJ_BYTE * l_current_ptr;
2057 opj_image_t * l_image = 00;
2058 opj_cp_v2_t *cp = 00;
2059 opj_image_comp_t * l_img_comp = 00;
2062 assert(p_stream != 00);
2063 assert(p_j2k != 00);
2064 assert(p_manager != 00);
2066 l_image = p_j2k->m_private_image;
2067 cp = &(p_j2k->m_cp);
2068 l_size_len = 40 + 3 * l_image->numcomps;
2069 l_img_comp = l_image->comps;
2071 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2073 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2074 = (OPJ_BYTE*)opj_realloc(
2075 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2077 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2081 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
2084 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2086 /* write SOC identifier */
2087 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
2090 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
2093 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
2096 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
2099 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
2102 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
2105 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
2108 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
2111 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
2114 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2117 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2120 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2123 for (i = 0; i < l_image->numcomps; ++i) {
2124 /* TODO here with MCT ? */
2125 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2128 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2131 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2137 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) {
2147 * Reads a SIZ marker (image and tile size)
2148 * @param p_header_data the data contained in the SIZ box.
2149 * @param jp2 the jpeg2000 file codec.
2150 * @param p_header_size the size of the data contained in the SIZ marker.
2151 * @param p_manager the user event manager.
2153 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2154 OPJ_BYTE * p_header_data,
2155 OPJ_UINT32 p_header_size,
2156 opj_event_mgr_t * p_manager
2159 OPJ_UINT32 l_size, i;
2160 OPJ_UINT32 l_nb_comp;
2161 OPJ_UINT32 l_nb_comp_remain;
2162 OPJ_UINT32 l_remaining_size;
2163 OPJ_UINT32 l_nb_tiles;
2165 opj_image_t *l_image = 00;
2166 opj_cp_v2_t *l_cp = 00;
2167 opj_image_comp_t * l_img_comp = 00;
2168 opj_tcp_v2_t * l_current_tile_param = 00;
2171 assert(p_j2k != 00);
2172 assert(p_manager != 00);
2173 assert(p_header_data != 00);
2175 l_image = p_j2k->m_private_image;
2176 l_cp = &(p_j2k->m_cp);
2178 /* minimum size == 39 - 3 (= minimum component parameter) */
2179 if (p_header_size < 36) {
2180 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2184 l_remaining_size = p_header_size - 36;
2185 l_nb_comp = l_remaining_size / 3;
2186 l_nb_comp_remain = l_remaining_size % 3;
2187 if (l_nb_comp_remain != 0){
2188 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2192 l_size = p_header_size + 2; /* Lsiz */
2194 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2196 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2197 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2199 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2201 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2203 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2205 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2207 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2209 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2211 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2213 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2216 l_image->numcomps = (OPJ_UINT16) l_tmp;
2218 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2222 if (l_image->numcomps != l_nb_comp) {
2223 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);
2228 if (l_cp->correct) {
2229 /* if JPWL is on, we check whether TX errors have damaged
2230 too much the SIZ parameters */
2231 if (!(l_image->x1 * l_image->y1)) {
2232 opj_event_msg_v2(p_manager, EVT_ERROR,
2233 "JPWL: bad image size (%d x %d)\n",
2234 l_image->x1, l_image->y1);
2235 if (!JPWL_ASSUME || JPWL_ASSUME) {
2236 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2241 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2242 if (l_image->numcomps != ((len - 38) / 3)) {
2243 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2244 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2245 l_image->numcomps, ((len - 38) / 3));
2247 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2250 */ /* we try to correct */
2251 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2252 if (l_image->numcomps < ((len - 38) / 3)) {
2253 len = 38 + 3 * l_image->numcomps;
2254 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2257 l_image->numcomps = ((len - 38) / 3);
2258 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2264 /* update components number in the jpwl_exp_comps filed */
2265 l_cp->exp_comps = l_image->numcomps;
2267 #endif /* USE_JPWL */
2269 /* Allocate the resulting image components */
2270 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2271 if (l_image->comps == 00){
2272 l_image->numcomps = 0;
2273 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2277 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2278 l_img_comp = l_image->comps;
2280 /* Read the component information */
2281 for (i = 0; i < l_image->numcomps; ++i){
2283 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2285 l_img_comp->prec = (tmp & 0x7f) + 1;
2286 l_img_comp->sgnd = tmp >> 7;
2287 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2289 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2290 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2292 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2295 if (l_cp->correct) {
2296 /* if JPWL is on, we check whether TX errors have damaged
2297 too much the SIZ parameters, again */
2298 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2299 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2300 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2301 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2303 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2306 /* we try to correct */
2307 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2308 if (!l_image->comps[i].dx) {
2309 l_image->comps[i].dx = 1;
2310 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2311 i, l_image->comps[i].dx);
2313 if (!l_image->comps[i].dy) {
2314 l_image->comps[i].dy = 1;
2315 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2316 i, l_image->comps[i].dy);
2320 #endif /* USE_JPWL */
2321 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2322 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2326 /* Compute the number of tiles */
2327 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2328 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2329 l_nb_tiles = l_cp->tw * l_cp->th;
2331 /* Define the tiles which will be decoded */
2332 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2333 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;
2334 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;
2335 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);
2336 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);
2339 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2340 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2341 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2342 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2346 if (l_cp->correct) {
2347 /* if JPWL is on, we check whether TX errors have damaged
2348 too much the SIZ parameters */
2349 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2350 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2351 "JPWL: bad number of tiles (%d x %d)\n",
2352 l_cp->tw, l_cp->th);
2354 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2357 /* we try to correct */
2358 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2361 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2364 if (l_cp->tw > l_cp->max_tiles) {
2366 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2367 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2368 l_cp->max_tiles, l_cp->tw);
2372 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2375 if (l_cp->th > l_cp->max_tiles) {
2377 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2378 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2379 l_cp->max_tiles, l_cp->th);
2383 #endif /* USE_JPWL */
2385 /* memory allocations */
2386 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2387 if (l_cp->tcps == 00) {
2388 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2391 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2394 if (l_cp->correct) {
2396 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2397 "JPWL: could not alloc tcps field of cp\n");
2398 if (!JPWL_ASSUME || JPWL_ASSUME) {
2399 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2404 #endif /* USE_JPWL */
2406 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2407 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2408 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2409 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2412 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2414 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2415 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2417 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2418 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2421 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));
2422 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2424 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2425 (opj_simple_mcc_decorrelation_data_t*)
2426 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2428 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2429 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2432 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));
2433 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2435 /* set up default dc level shift */
2436 for (i=0;i<l_image->numcomps;++i) {
2437 if (! l_image->comps[i].sgnd) {
2438 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2442 l_current_tile_param = l_cp->tcps;
2443 for (i = 0; i < l_nb_tiles; ++i) {
2444 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2445 if (l_current_tile_param->tccps == 00) {
2446 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2449 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2451 ++l_current_tile_param;
2454 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2455 opj_image_comp_header_update(l_image,l_cp);
2462 static void j2k_write_com(opj_j2k_t *j2k) {
2466 if(j2k->cp->comment) {
2467 opj_cio_t *cio = j2k->cio;
2468 char *comment = j2k->cp->comment;
2470 cio_write(cio, J2K_MS_COM, 2);
2471 lenp = cio_tell(cio);
2473 cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
2474 for (i = 0; i < strlen(comment); i++) {
2475 cio_write(cio, comment[i], 1);
2477 len = cio_tell(cio) - lenp;
2478 cio_seek(cio, lenp);
2479 cio_write(cio, len, 2);
2480 cio_seek(cio, lenp + len);
2484 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
2490 * Writes the COM marker (comment)
2492 * @param p_stream the stream to write data to.
2493 * @param p_j2k J2K codec.
2494 * @param p_manager the user event manager.
2496 opj_bool j2k_write_com_v2( opj_j2k_v2_t *p_j2k,
2497 struct opj_stream_private *p_stream,
2498 struct opj_event_mgr * p_manager )
2500 OPJ_UINT32 l_comment_size;
2501 OPJ_UINT32 l_total_com_size;
2502 const OPJ_CHAR *l_comment;
2503 OPJ_BYTE * l_current_ptr = 00;
2506 assert(p_j2k != 00);
2507 assert(p_stream != 00);
2508 assert(p_manager != 00);
2510 l_comment = p_j2k->m_cp.comment;
2511 l_comment_size = strlen(l_comment);
2512 l_total_com_size = l_comment_size + 6;
2514 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2515 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2516 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2519 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2523 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2526 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2528 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2531 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2534 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2537 memcpy( l_current_ptr,l_comment,l_comment_size);
2539 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) {
2547 * Reads a COM marker (comments)
2548 * @param p_header_data the data contained in the COM box.
2549 * @param jp2 the jpeg2000 file codec.
2550 * @param p_header_size the size of the data contained in the COM marker.
2551 * @param p_manager the user event manager.
2553 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
2554 OPJ_BYTE * p_header_data,
2555 OPJ_UINT32 p_header_size,
2556 opj_event_mgr_t * p_manager
2560 assert(p_j2k != 00);
2561 assert(p_manager != 00);
2562 assert(p_header_data != 00);
2563 (void)p_header_size;
2568 static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
2571 opj_cp_t *cp = j2k->cp;
2572 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2573 opj_tccp_t *tccp = &tcp->tccps[compno];
2574 opj_cio_t *cio = j2k->cio;
2576 cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
2577 cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
2578 cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
2579 cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
2580 cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
2582 if (tccp->csty & J2K_CCP_CSTY_PRT) {
2583 for (i = 0; i < tccp->numresolutions; i++) {
2584 cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
2590 static void j2k_write_cod(opj_j2k_t *j2k) {
2591 opj_cp_t *cp = NULL;
2592 opj_tcp_t *tcp = NULL;
2595 opj_cio_t *cio = j2k->cio;
2597 cio_write(cio, J2K_MS_COD, 2); /* COD */
2599 lenp = cio_tell(cio);
2603 tcp = &cp->tcps[j2k->curtileno];
2605 cio_write(cio, tcp->csty, 1); /* Scod */
2606 cio_write(cio, tcp->prg, 1); /* SGcod (A) */
2607 cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
2608 cio_write(cio, tcp->mct, 1); /* SGcod (C) */
2610 j2k_write_cox(j2k, 0);
2611 len = cio_tell(cio) - lenp;
2612 cio_seek(cio, lenp);
2613 cio_write(cio, len, 2); /* Lcod */
2614 cio_seek(cio, lenp + len);
2617 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
2622 * Writes the COD marker (Coding style default)
2624 * @param p_stream the stream to write data to.
2625 * @param p_j2k J2K codec.
2626 * @param p_manager the user event manager.
2628 opj_bool j2k_write_cod_v2( opj_j2k_v2_t *p_j2k,
2629 struct opj_stream_private *p_stream,
2630 struct opj_event_mgr * p_manager )
2632 opj_cp_v2_t *l_cp = 00;
2633 opj_tcp_v2_t *l_tcp = 00;
2634 OPJ_UINT32 l_code_size,l_remaining_size;
2635 OPJ_BYTE * l_current_data = 00;
2638 assert(p_j2k != 00);
2639 assert(p_manager != 00);
2640 assert(p_stream != 00);
2642 l_cp = &(p_j2k->m_cp);
2643 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2644 l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2645 l_remaining_size = l_code_size;
2647 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2648 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2649 = (OPJ_BYTE*)opj_realloc(
2650 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2653 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2657 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2660 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2662 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2663 l_current_data += 2;
2665 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2666 l_current_data += 2;
2668 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2671 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2674 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2677 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2680 l_remaining_size -= 9;
2682 if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2683 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2687 if (l_remaining_size != 0) {
2688 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2692 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) {
2701 * Reads a COD marker (Coding Styke defaults)
2702 * @param p_header_data the data contained in the COD box.
2703 * @param p_j2k the jpeg2000 codec.
2704 * @param p_header_size the size of the data contained in the COD marker.
2705 * @param p_manager the user event manager.
2707 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2708 OPJ_BYTE * p_header_data,
2709 OPJ_UINT32 p_header_size,
2710 opj_event_mgr_t * p_manager
2716 opj_cp_v2_t *l_cp = 00;
2717 opj_tcp_v2_t *l_tcp = 00;
2718 opj_image_t *l_image = 00;
2721 assert(p_header_data != 00);
2722 assert(p_j2k != 00);
2723 assert(p_manager != 00);
2725 l_image = p_j2k->m_private_image;
2726 l_cp = &(p_j2k->m_cp);
2728 /* If we are in the first tile-part header of the current tile */
2729 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2730 &l_cp->tcps[p_j2k->m_current_tile_number] :
2731 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2733 /* Make sure room is sufficient */
2734 if (p_header_size < 5) {
2735 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2739 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2741 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2743 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2744 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2747 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2748 if (l_cp->m_specific_param.m_dec.m_layer) {
2749 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2752 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2755 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2759 for (i = 0; i < l_image->numcomps; ++i) {
2760 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2763 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2764 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2768 if (p_header_size != 0) {
2769 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2773 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2774 opj_j2k_copy_tile_component_parameters(p_j2k);
2777 #ifdef WIP_REMOVE_MSD
2778 if (p_j2k->cstr_info) {
2779 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2780 p_j2k->cstr_info->prog = l_tcp->prg;
2781 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2782 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2783 for (i = 0; i < l_image->numcomps; ++i) {
2784 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2792 static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
2795 opj_cp_t *cp = j2k->cp;
2796 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
2797 opj_image_t *image = j2k->image;
2798 opj_cio_t *cio = j2k->cio;
2800 cio_write(cio, J2K_MS_COC, 2); /* COC */
2801 lenp = cio_tell(cio);
2803 cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
2804 cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
2805 j2k_write_cox(j2k, compno);
2806 len = cio_tell(cio) - lenp;
2807 cio_seek(cio, lenp);
2808 cio_write(cio, len, 2); /* Lcoc */
2809 cio_seek(cio, lenp + len);
2813 * Writes the COC marker (Coding style component)
2815 * @param p_comp_no the index of the component to output.
2816 * @param p_stream the stream to write data to.
2817 * @param p_j2k J2K codec.
2818 * @param p_manager the user event manager.
2820 opj_bool j2k_write_coc_v2( opj_j2k_v2_t *p_j2k,
2821 OPJ_UINT32 p_comp_no,
2822 struct opj_stream_private *p_stream,
2823 struct opj_event_mgr * p_manager )
2825 OPJ_UINT32 l_coc_size,l_remaining_size;
2826 OPJ_UINT32 l_comp_room;
2829 assert(p_j2k != 00);
2830 assert(p_manager != 00);
2831 assert(p_stream != 00);
2833 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2835 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2837 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2838 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2839 = (OPJ_BYTE*)opj_realloc(
2840 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2842 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2846 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2849 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);
2851 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) {
2859 * Writes the COC marker (Coding style component)
2861 * @param p_comp_no the index of the component to output.
2862 * @param p_stream the stream to write data to.
2863 * @param p_j2k J2K codec.
2864 * @param p_manager the user event manager.
2866 void j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2867 OPJ_UINT32 p_comp_no,
2869 OPJ_UINT32 * p_data_written,
2870 struct opj_event_mgr * p_manager )
2872 opj_cp_v2_t *l_cp = 00;
2873 opj_tcp_v2_t *l_tcp = 00;
2874 OPJ_UINT32 l_coc_size,l_remaining_size;
2875 OPJ_BYTE * l_current_data = 00;
2876 opj_image_t *l_image = 00;
2877 OPJ_UINT32 l_comp_room;
2880 assert(p_j2k != 00);
2881 assert(p_manager != 00);
2883 l_cp = &(p_j2k->m_cp);
2884 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2885 l_image = p_j2k->m_private_image;
2886 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2888 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2889 l_remaining_size = l_coc_size;
2891 l_current_data = p_data;
2893 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2894 l_current_data += 2;
2896 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2897 l_current_data += 2;
2899 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2900 l_current_data+=l_comp_room;
2902 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2905 l_remaining_size -= (5 + l_comp_room);
2906 opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2907 * p_data_written = l_coc_size;
2911 * Gets the maximum size taken by a coc.
2913 * @param p_j2k the jpeg2000 codec to use.
2915 OPJ_UINT32 j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2918 OPJ_UINT32 l_nb_comp;
2919 OPJ_UINT32 l_nb_tiles;
2920 OPJ_UINT32 l_max = 0;
2924 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2925 l_nb_comp = p_j2k->m_private_image->numcomps;
2927 for (i=0;i<l_nb_tiles;++i) {
2928 for (j=0;j<l_nb_comp;++j) {
2929 l_max = uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2938 * Reads a COC marker (Coding Style Component)
2939 * @param p_header_data the data contained in the COC box.
2940 * @param p_j2k the jpeg2000 codec.
2941 * @param p_header_size the size of the data contained in the COC marker.
2942 * @param p_manager the user event manager.
2944 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2945 OPJ_BYTE * p_header_data,
2946 OPJ_UINT32 p_header_size,
2947 opj_event_mgr_t * p_manager
2950 opj_cp_v2_t *l_cp = NULL;
2951 opj_tcp_v2_t *l_tcp = NULL;
2952 opj_image_t *l_image = NULL;
2953 OPJ_UINT32 l_comp_room;
2954 OPJ_UINT32 l_comp_no;
2957 assert(p_header_data != 00);
2958 assert(p_j2k != 00);
2959 assert(p_manager != 00);
2961 l_cp = &(p_j2k->m_cp);
2962 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2963 &l_cp->tcps[p_j2k->m_current_tile_number] :
2964 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2965 l_image = p_j2k->m_private_image;
2967 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2969 /* make sure room is sufficient*/
2970 if (p_header_size < l_comp_room + 1) {
2971 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2974 p_header_size -= l_comp_room + 1;
2976 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2977 p_header_data += l_comp_room;
2978 if (l_comp_no >= l_image->numcomps) {
2979 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2983 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2986 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2987 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2991 if (p_header_size != 0) {
2992 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2998 static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
2999 int bandno, numbands;
3002 opj_cp_t *cp = j2k->cp;
3003 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3004 opj_tccp_t *tccp = &tcp->tccps[compno];
3005 opj_cio_t *cio = j2k->cio;
3007 cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
3008 numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
3010 for (bandno = 0; bandno < numbands; bandno++) {
3011 expn = tccp->stepsizes[bandno].expn;
3012 mant = tccp->stepsizes[bandno].mant;
3014 if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
3015 cio_write(cio, expn << 3, 1); /* SPqcx_i */
3017 cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
3023 static void j2k_write_qcd(opj_j2k_t *j2k) {
3026 opj_cio_t *cio = j2k->cio;
3028 cio_write(cio, J2K_MS_QCD, 2); /* QCD */
3029 lenp = cio_tell(cio);
3031 j2k_write_qcx(j2k, 0);
3032 len = cio_tell(cio) - lenp;
3033 cio_seek(cio, lenp);
3034 cio_write(cio, len, 2); /* Lqcd */
3035 cio_seek(cio, lenp + len);
3038 j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
3042 * Writes the QCD marker (quantization default)
3044 * @param p_comp_number the index of the component to output.
3045 * @param p_stream the stream to write data to.
3046 * @param p_j2k J2K codec.
3047 * @param p_manager the user event manager.
3049 opj_bool j2k_write_qcd_v2( opj_j2k_v2_t *p_j2k,
3050 struct opj_stream_private *p_stream,
3051 struct opj_event_mgr * p_manager )
3053 opj_cp_v2_t *l_cp = 00;
3054 opj_tcp_v2_t *l_tcp = 00;
3055 OPJ_UINT32 l_qcd_size,l_remaining_size;
3056 OPJ_BYTE * l_current_data = 00;
3059 assert(p_j2k != 00);
3060 assert(p_manager != 00);
3061 assert(p_stream != 00);
3063 l_cp = &(p_j2k->m_cp);
3064 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
3065 l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
3066 l_remaining_size = l_qcd_size;
3068 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3069 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3070 = (OPJ_BYTE*)opj_realloc(
3071 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3074 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3078 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
3081 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
3083 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
3084 l_current_data += 2;
3086 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
3087 l_current_data += 2;
3089 l_remaining_size -= 4;
3091 if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
3092 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3096 if (l_remaining_size != 0) {
3097 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
3101 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) {
3109 * Reads a QCD marker (Quantization defaults)
3110 * @param p_header_data the data contained in the QCD box.
3111 * @param p_j2k the jpeg2000 codec.
3112 * @param p_header_size the size of the data contained in the QCD marker.
3113 * @param p_manager the user event manager.
3115 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
3116 OPJ_BYTE * p_header_data,
3117 OPJ_UINT32 p_header_size,
3118 opj_event_mgr_t * p_manager
3122 assert(p_header_data != 00);
3123 assert(p_j2k != 00);
3124 assert(p_manager != 00);
3126 if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
3127 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3131 if (p_header_size != 0) {
3132 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
3136 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
3137 opj_j2k_copy_tile_quantization_parameters(p_j2k);
3142 static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
3145 opj_cio_t *cio = j2k->cio;
3147 cio_write(cio, J2K_MS_QCC, 2); /* QCC */
3148 lenp = cio_tell(cio);
3150 cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
3151 j2k_write_qcx(j2k, compno);
3152 len = cio_tell(cio) - lenp;
3153 cio_seek(cio, lenp);
3154 cio_write(cio, len, 2); /* Lqcc */
3155 cio_seek(cio, lenp + len);
3159 * Writes the QCC marker (quantization component)
3161 * @param p_comp_no the index of the component to output.
3162 * @param p_stream the stream to write data to.
3163 * @param p_j2k J2K codec.
3164 * @param p_manager the user event manager.
3166 opj_bool j2k_write_qcc_v2( opj_j2k_v2_t *p_j2k,
3167 OPJ_UINT32 p_comp_no,
3168 struct opj_stream_private *p_stream,
3169 struct opj_event_mgr * p_manager )
3171 OPJ_UINT32 l_qcc_size,l_remaining_size;
3174 assert(p_j2k != 00);
3175 assert(p_manager != 00);
3176 assert(p_stream != 00);
3178 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3179 l_remaining_size = l_qcc_size;
3181 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3182 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3183 = (OPJ_BYTE*)opj_realloc(
3184 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3186 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3190 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
3193 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);
3195 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) {
3203 * Writes the QCC marker (quantization component)
3205 * @param p_comp_no the index of the component to output.
3206 * @param p_stream the stream to write data to.
3207 * @param p_j2k J2K codec.
3208 * @param p_manager the user event manager.
3210 void j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
3211 OPJ_UINT32 p_comp_no,
3213 OPJ_UINT32 * p_data_written,
3214 struct opj_event_mgr * p_manager )
3216 OPJ_UINT32 l_qcc_size,l_remaining_size;
3217 OPJ_BYTE * l_current_data = 00;
3220 assert(p_j2k != 00);
3221 assert(p_manager != 00);
3223 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
3224 l_remaining_size = l_qcc_size;
3226 l_current_data = p_data;
3228 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
3229 l_current_data += 2;
3231 if (p_j2k->m_private_image->numcomps <= 256) {
3234 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3235 l_current_data += 2;
3237 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
3240 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
3241 l_remaining_size -= 6;
3244 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
3245 l_current_data += 2;
3247 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
3250 l_remaining_size -= 6;
3253 opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
3255 *p_data_written = l_qcc_size;
3259 * Gets the maximum size taken by a qcc.
3261 OPJ_UINT32 j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
3263 return j2k_get_max_coc_size(p_j2k);
3267 * Reads a QCC marker (Quantization component)
3268 * @param p_header_data the data contained in the QCC box.
3269 * @param p_j2k the jpeg2000 codec.
3270 * @param p_header_size the size of the data contained in the QCC marker.
3271 * @param p_manager the user event manager.
3273 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3274 OPJ_BYTE * p_header_data,
3275 OPJ_UINT32 p_header_size,
3276 opj_event_mgr_t * p_manager
3279 OPJ_UINT32 l_num_comp,l_comp_no;
3282 assert(p_header_data != 00);
3283 assert(p_j2k != 00);
3284 assert(p_manager != 00);
3286 l_num_comp = p_j2k->m_private_image->numcomps;
3288 if (l_num_comp <= 256) {
3289 if (p_header_size < 1) {
3290 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3293 opj_read_bytes(p_header_data,&l_comp_no,1);
3298 if (p_header_size < 2) {
3299 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3302 opj_read_bytes(p_header_data,&l_comp_no,2);
3308 if (p_j2k->m_cp.correct) {
3310 static OPJ_UINT32 backup_compno = 0;
3312 /* compno is negative or larger than the number of components!!! */
3313 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3314 opj_event_msg_v2(p_manager, EVT_ERROR,
3315 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3316 l_comp_no, l_num_comp);
3318 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3321 /* we try to correct */
3322 l_comp_no = backup_compno % l_num_comp;
3323 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3324 "- setting component number to %d\n",
3328 /* keep your private count of tiles */
3331 #endif /* USE_JPWL */
3333 if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3334 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3338 if (p_header_size != 0) {
3339 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3347 static void j2k_write_poc(opj_j2k_t *j2k) {
3348 int len, numpchgs, i;
3350 int numcomps = j2k->image->numcomps;
3352 opj_cp_t *cp = j2k->cp;
3353 opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
3354 opj_tccp_t *tccp = &tcp->tccps[0];
3355 opj_cio_t *cio = j2k->cio;
3357 numpchgs = 1 + tcp->numpocs;
3358 cio_write(cio, J2K_MS_POC, 2); /* POC */
3359 len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
3360 cio_write(cio, len, 2); /* Lpoc */
3361 for (i = 0; i < numpchgs; i++) {
3362 opj_poc_t *poc = &tcp->pocs[i];
3363 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
3364 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
3365 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
3366 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
3367 cio_write(cio, poc->resno1, 1); /* REpoc_i */
3368 poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
3369 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
3370 poc->compno1 = int_min(poc->compno1, numcomps);
3371 cio_write(cio, poc->prg, 1); /* Ppoc_i */
3376 * Writes the POC marker (Progression Order Change)
3378 * @param p_stream the stream to write data to.
3379 * @param p_j2k J2K codec.
3380 * @param p_manager the user event manager.
3382 opj_bool j2k_write_poc_v2( opj_j2k_v2_t *p_j2k,
3383 struct opj_stream_private *p_stream,
3384 struct opj_event_mgr * p_manager )
3386 OPJ_UINT32 l_nb_comp;
3387 OPJ_UINT32 l_nb_poc;
3388 OPJ_UINT32 l_poc_size;
3389 OPJ_UINT32 l_written_size = 0;
3390 opj_tcp_v2_t *l_tcp = 00;
3391 opj_tccp_t *l_tccp = 00;
3392 OPJ_UINT32 l_poc_room;
3395 assert(p_j2k != 00);
3396 assert(p_manager != 00);
3397 assert(p_stream != 00);
3399 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3400 l_tccp = &l_tcp->tccps[0];
3401 l_nb_comp = p_j2k->m_private_image->numcomps;
3402 l_nb_poc = 1 + l_tcp->numpocs;
3404 if (l_nb_comp <= 256) {
3410 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3412 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3413 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3414 = (OPJ_BYTE*)opj_realloc(
3415 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3418 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3422 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3425 j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3427 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) {
3436 * Writes the POC marker (Progression Order Change)
3438 * @param p_stream the stream to write data to.
3439 * @param p_j2k J2K codec.
3440 * @param p_manager the user event manager.
3442 void j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3444 OPJ_UINT32 * p_data_written,
3445 struct opj_event_mgr * p_manager )
3448 OPJ_BYTE * l_current_data = 00;
3449 OPJ_UINT32 l_nb_comp;
3450 OPJ_UINT32 l_nb_poc;
3451 OPJ_UINT32 l_poc_size;
3452 opj_image_t *l_image = 00;
3453 opj_tcp_v2_t *l_tcp = 00;
3454 opj_tccp_t *l_tccp = 00;
3455 opj_poc_t *l_current_poc = 00;
3456 OPJ_UINT32 l_poc_room;
3459 assert(p_j2k != 00);
3460 assert(p_manager != 00);
3462 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3463 l_tccp = &l_tcp->tccps[0];
3464 l_image = p_j2k->m_private_image;
3465 l_nb_comp = l_image->numcomps;
3466 l_nb_poc = 1 + l_tcp->numpocs;
3468 if (l_nb_comp <= 256) {
3475 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3477 l_current_data = p_data;
3479 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3480 l_current_data += 2;
3482 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3483 l_current_data += 2;
3485 l_current_poc = l_tcp->pocs;
3486 for (i = 0; i < l_nb_poc; ++i) {
3487 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3490 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3491 l_current_data+=l_poc_room;
3493 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3496 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3499 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3500 l_current_data+=l_poc_room;
3502 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3505 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3506 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3507 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3508 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3513 *p_data_written = l_poc_size;
3517 * Gets the maximum size taken by the writing of a POC.
3519 OPJ_UINT32 j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3521 opj_tcp_v2_t * l_tcp = 00;
3522 OPJ_UINT32 l_nb_tiles = 0;
3523 OPJ_UINT32 l_max_poc = 0;
3526 l_tcp = p_j2k->m_cp.tcps;
3527 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3529 for (i=0;i<l_nb_tiles;++i) {
3530 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3536 return 4 + 9 * l_max_poc;
3540 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3542 OPJ_UINT32 j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3545 OPJ_UINT32 l_nb_tiles;
3546 OPJ_UINT32 l_max = 0;
3547 opj_tcp_v2_t * l_tcp = 00;
3549 l_tcp = p_j2k->m_cp.tcps;
3550 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3552 for (i=0;i<l_nb_tiles;++i) {
3553 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3563 * Gets the maximum size taken by the headers of the SOT.
3565 * @param p_j2k the jpeg2000 codec to use.
3567 OPJ_UINT32 j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3569 OPJ_UINT32 l_nb_bytes = 0;
3570 OPJ_UINT32 l_nb_comps;
3571 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3573 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3574 l_nb_bytes += j2k_get_max_toc_size(p_j2k);
3576 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3577 l_coc_bytes = j2k_get_max_coc_size(p_j2k);
3578 l_nb_bytes += l_nb_comps * l_coc_bytes;
3580 l_qcc_bytes = j2k_get_max_qcc_size(p_j2k);
3581 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3584 l_nb_bytes += j2k_get_max_poc_size(p_j2k);
3586 /*** DEVELOPER CORNER, Add room for your headers ***/
3593 * Reads a POC marker (Progression Order Change)
3595 * @param p_header_data the data contained in the POC box.
3596 * @param p_j2k the jpeg2000 codec.
3597 * @param p_header_size the size of the data contained in the POC marker.
3598 * @param p_manager the user event manager.
3600 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3601 OPJ_BYTE * p_header_data,
3602 OPJ_UINT32 p_header_size,
3603 opj_event_mgr_t * p_manager
3606 OPJ_UINT32 i, l_nb_comp, l_tmp;
3607 opj_image_t * l_image = 00;
3608 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3609 OPJ_UINT32 l_chunk_size, l_comp_room;
3611 opj_cp_v2_t *l_cp = 00;
3612 opj_tcp_v2_t *l_tcp = 00;
3613 opj_poc_t *l_current_poc = 00;
3616 assert(p_header_data != 00);
3617 assert(p_j2k != 00);
3618 assert(p_manager != 00);
3620 l_image = p_j2k->m_private_image;
3621 l_nb_comp = l_image->numcomps;
3622 if (l_nb_comp <= 256) {
3628 l_chunk_size = 5 + 2 * l_comp_room;
3629 l_current_poc_nb = p_header_size / l_chunk_size;
3630 l_current_poc_remaining = p_header_size % l_chunk_size;
3632 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3633 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3637 l_cp = &(p_j2k->m_cp);
3638 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3639 &l_cp->tcps[p_j2k->m_current_tile_number] :
3640 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3641 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3642 l_current_poc_nb += l_old_poc_nb;
3644 assert(l_current_poc_nb < 32);
3646 /* now poc is in use.*/
3649 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3650 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3651 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3653 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3654 p_header_data+=l_comp_room;
3655 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3657 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3659 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3660 p_header_data+=l_comp_room;
3661 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3663 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3664 /* make sure comp is in acceptable bounds */
3665 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3669 l_tcp->numpocs = l_current_poc_nb - 1;
3674 * Reads a CRG marker (Component registration)
3676 * @param p_header_data the data contained in the TLM box.
3677 * @param p_j2k the jpeg2000 codec.
3678 * @param p_header_size the size of the data contained in the TLM marker.
3679 * @param p_manager the user event manager.
3681 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3682 OPJ_BYTE * p_header_data,
3683 OPJ_UINT32 p_header_size,
3684 opj_event_mgr_t * p_manager
3687 OPJ_UINT32 l_nb_comp;
3689 assert(p_header_data != 00);
3690 assert(p_j2k != 00);
3691 assert(p_manager != 00);
3693 l_nb_comp = p_j2k->m_private_image->numcomps;
3695 if (p_header_size != l_nb_comp *4) {
3696 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3699 /* Do not care of this at the moment since only local variables are set here */
3702 (i = 0; i < l_nb_comp; ++i)
3704 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3706 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3714 * Reads a TLM marker (Tile Length Marker)
3716 * @param p_header_data the data contained in the TLM box.
3717 * @param p_j2k the jpeg2000 codec.
3718 * @param p_header_size the size of the data contained in the TLM marker.
3719 * @param p_manager the user event manager.
3721 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3722 OPJ_BYTE * p_header_data,
3723 OPJ_UINT32 p_header_size,
3724 opj_event_mgr_t * p_manager
3727 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3729 assert(p_header_data != 00);
3730 assert(p_j2k != 00);
3731 assert(p_manager != 00);
3733 if (p_header_size < 2) {
3734 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3739 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3741 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3744 l_ST = ((l_Stlm >> 4) & 0x3);
3745 l_SP = (l_Stlm >> 6) & 0x1;
3747 l_Ptlm_size = (l_SP + 1) * 2;
3748 l_quotient = l_Ptlm_size + l_ST;
3750 l_tot_num_tp = p_header_size / l_quotient;
3751 l_tot_num_tp_remaining = p_header_size % l_quotient;
3753 if (l_tot_num_tp_remaining != 0) {
3754 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3757 /* FIXME Do not care of this at the moment since only local variables are set here */
3760 (i = 0; i < l_tot_num_tp; ++i)
3762 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3763 p_header_data += l_ST;
3764 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3765 p_header_data += l_Ptlm_size;
3772 * Reads a PLM marker (Packet length, main header marker)
3774 * @param p_header_data the data contained in the TLM box.
3775 * @param p_j2k the jpeg2000 codec.
3776 * @param p_header_size the size of the data contained in the TLM marker.
3777 * @param p_manager the user event manager.
3779 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3780 OPJ_BYTE * p_header_data,
3781 OPJ_UINT32 p_header_size,
3782 opj_event_mgr_t * p_manager
3786 assert(p_header_data != 00);
3787 assert(p_j2k != 00);
3788 assert(p_manager != 00);
3790 if (p_header_size < 1) {
3791 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3794 /* Do not care of this at the moment since only local variables are set here */
3796 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3803 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3805 p_header_size -= (1+l_Nplm);
3809 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3813 (i = 0; i < l_Nplm; ++i)
3815 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3817 // take only the last seven bytes
3818 l_packet_len |= (l_tmp & 0x7f);
3826 // store packet length and proceed to next packet
3833 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3842 * Reads a PLT marker (Packet length, tile-part header)
3844 * @param p_header_data the data contained in the PLT box.
3845 * @param p_j2k the jpeg2000 codec.
3846 * @param p_header_size the size of the data contained in the PLT marker.
3847 * @param p_manager the user event manager.
3849 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3850 OPJ_BYTE * p_header_data,
3851 OPJ_UINT32 p_header_size,
3852 opj_event_mgr_t * p_manager
3855 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3858 assert(p_header_data != 00);
3859 assert(p_j2k != 00);
3860 assert(p_manager != 00);
3862 if (p_header_size < 1) {
3863 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3867 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3871 for (i = 0; i < p_header_size; ++i) {
3872 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3874 /* take only the last seven bytes */
3875 l_packet_len |= (l_tmp & 0x7f);
3880 /* store packet length and proceed to next packet */
3885 if (l_packet_len != 0) {
3886 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3895 * Reads a PPM marker (Packed packet headers, main header)
3897 * @param p_header_data the data contained in the POC box.
3898 * @param p_j2k the jpeg2000 codec.
3899 * @param p_header_size the size of the data contained in the POC marker.
3900 * @param p_manager the user event manager.
3903 opj_bool j2k_read_ppm_v2 (
3904 opj_j2k_v2_t *p_j2k,
3905 OPJ_BYTE * p_header_data,
3906 OPJ_UINT32 p_header_size,
3907 struct opj_event_mgr * p_manager
3911 opj_cp_v2_t *l_cp = 00;
3912 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3915 assert(p_header_data != 00);
3916 assert(p_j2k != 00);
3917 assert(p_manager != 00);
3919 if (p_header_size < 1) {
3920 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3924 l_cp = &(p_j2k->m_cp);
3927 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3931 /* First PPM marker */
3933 if (p_header_size < 4) {
3934 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3938 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3942 /* First PPM marker: Initialization */
3943 l_cp->ppm_len = l_N_ppm;
3944 l_cp->ppm_data_size = 0;
3946 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3947 if (l_cp->ppm_buffer == 00) {
3948 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3951 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3953 l_cp->ppm_data = l_cp->ppm_buffer;
3957 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3958 if (p_header_size >= 4) {
3960 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3963 l_cp->ppm_len += l_N_ppm ;
3965 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3966 if (l_cp->ppm_buffer == 00) {
3967 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3970 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3972 l_cp->ppm_data = l_cp->ppm_buffer;
3979 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3981 if (l_remaining_data <= p_header_size) {
3982 /* we must store less information than available in the packet */
3983 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3984 l_cp->ppm_data_size = l_cp->ppm_len;
3985 p_header_size -= l_remaining_data;
3986 p_header_data += l_remaining_data;
3989 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3990 l_cp->ppm_data_size += p_header_size;
3991 p_header_data += p_header_size;
4004 * Reads a PPM marker (Packed packet headers, main header)
4006 * @param p_header_data the data contained in the POC box.
4007 * @param p_j2k the jpeg2000 codec.
4008 * @param p_header_size the size of the data contained in the POC marker.
4009 * @param p_manager the user event manager.
4011 opj_bool j2k_read_ppm_v3 (
4012 opj_j2k_v2_t *p_j2k,
4013 OPJ_BYTE * p_header_data,
4014 OPJ_UINT32 p_header_size,
4015 struct opj_event_mgr * p_manager
4018 opj_cp_v2_t *l_cp = 00;
4019 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
4022 assert(p_header_data != 00);
4023 assert(p_j2k != 00);
4024 assert(p_manager != 00);
4026 /* Minimum size of PPM marker is equal to the size of Zppm element */
4027 if (p_header_size < 1) {
4028 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4032 l_cp = &(p_j2k->m_cp);
4035 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
4039 /* First PPM marker */
4041 /* We need now at least the Nppm^0 element */
4042 if (p_header_size < 4) {
4043 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
4047 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4051 /* First PPM marker: Initialization */
4052 l_cp->ppm_len = l_N_ppm;
4053 l_cp->ppm_data_read = 0;
4055 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
4056 if (l_cp->ppm_data == 00) {
4057 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4060 memset(l_cp->ppm_data,0,l_cp->ppm_len);
4062 l_cp->ppm_data_current = l_cp->ppm_data;
4064 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
4067 if (p_header_size < 4) {
4068 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
4072 /* Uncompleted Ippm series in the previous PPM marker?*/
4073 if (l_cp->ppm_data_read < l_cp->ppm_len) {
4074 /* Get the place where add the remaining Ippm series*/
4075 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
4076 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
4079 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
4083 /* Increase the size of ppm_data to add the new Ippm series*/
4084 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4086 /* Keep the position of the place where concatenate the new series*/
4087 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4088 l_cp->ppm_len += l_N_ppm;
4093 l_remaining_data = p_header_size;
4095 while (l_remaining_data >= l_N_ppm) {
4096 /* read a complete Ippm series*/
4097 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
4098 p_header_size -= l_N_ppm;
4099 p_header_data += l_N_ppm;
4101 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
4105 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
4110 l_remaining_data = p_header_size;
4114 l_remaining_data = p_header_size;
4116 /* Next Ippm series is a complete series ?*/
4117 if (l_remaining_data > l_N_ppm) {
4118 /* Increase the size of ppm_data to add the new Ippm series*/
4119 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4121 /* Keep the position of the place where concatenate the new series */
4122 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4123 l_cp->ppm_len += l_N_ppm;
4128 /* Need to read an incomplete Ippm series*/
4129 if (l_remaining_data) {
4130 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
4132 /* Keep the position of the place where concatenate the new series*/
4133 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
4134 l_cp->ppm_len += l_N_ppm;
4136 /* Read incomplete Ippm series*/
4137 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
4138 p_header_size -= l_remaining_data;
4139 p_header_data += l_remaining_data;
4141 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
4146 if (l_cp->ppm_data_size == l_cp->ppm_len) {
4147 if (p_header_size >= 4) {
4149 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
4152 l_cp->ppm_len += l_N_ppm ;
4154 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
4155 if (l_cp->ppm_buffer == 00) {
4156 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
4159 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
4161 l_cp->ppm_data = l_cp->ppm_buffer;
4168 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
4170 if (l_remaining_data <= p_header_size) {
4171 /* we must store less information than available in the packet */
4172 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
4173 l_cp->ppm_data_size = l_cp->ppm_len;
4174 p_header_size -= l_remaining_data;
4175 p_header_data += l_remaining_data;
4178 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
4179 l_cp->ppm_data_size += p_header_size;
4180 p_header_data += p_header_size;
4190 * Reads a PPT marker (Packed packet headers, tile-part header)
4192 * @param p_header_data the data contained in the PPT box.
4193 * @param p_j2k the jpeg2000 codec.
4194 * @param p_header_size the size of the data contained in the PPT marker.
4195 * @param p_manager the user event manager.
4197 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
4198 OPJ_BYTE * p_header_data,
4199 OPJ_UINT32 p_header_size,
4200 opj_event_mgr_t * p_manager
4203 opj_cp_v2_t *l_cp = 00;
4204 opj_tcp_v2_t *l_tcp = 00;
4208 assert(p_header_data != 00);
4209 assert(p_j2k != 00);
4210 assert(p_manager != 00);
4212 /* We need to have the Z_ppt element at minimum */
4213 if (p_header_size < 1) {
4214 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
4218 l_cp = &(p_j2k->m_cp);
4220 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");
4224 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
4227 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
4231 /* Allocate buffer to read the packet header */
4233 /* First PPT marker */
4234 l_tcp->ppt_data_size = 0;
4235 l_tcp->ppt_len = p_header_size;
4237 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
4238 if (l_tcp->ppt_buffer == 00) {
4239 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4242 l_tcp->ppt_data = l_tcp->ppt_buffer;
4244 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
4247 l_tcp->ppt_len += p_header_size;
4249 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
4250 if (l_tcp->ppt_buffer == 00) {
4251 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
4254 l_tcp->ppt_data = l_tcp->ppt_buffer;
4256 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
4259 /* Read packet header from buffer */
4260 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
4262 l_tcp->ppt_data_size += p_header_size;
4268 * Writes the TLM marker (Tile Length Marker)
4270 * @param p_stream the stream to write data to.
4271 * @param p_j2k J2K codec.
4272 * @param p_manager the user event manager.
4274 opj_bool j2k_write_tlm_v2( opj_j2k_v2_t *p_j2k,
4275 struct opj_stream_private *p_stream,
4276 struct opj_event_mgr * p_manager )
4278 OPJ_BYTE * l_current_data = 00;
4279 OPJ_UINT32 l_tlm_size;
4282 assert(p_j2k != 00);
4283 assert(p_manager != 00);
4284 assert(p_stream != 00);
4286 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4288 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
4289 p_j2k->m_specific_param.m_encoder.m_header_tile_data
4290 = (OPJ_BYTE*)opj_realloc(
4291 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
4294 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4298 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4301 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4303 /* change the way data is written to avoid seeking if possible */
4305 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4307 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4308 l_current_data += 2;
4310 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4311 l_current_data += 2;
4313 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4316 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4319 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4320 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) {
4327 static void j2k_write_tlm(opj_j2k_t *j2k){
4329 opj_cio_t *cio = j2k->cio;
4330 j2k->tlm_start = cio_tell(cio);
4331 cio_write(cio, J2K_MS_TLM, 2);/* TLM */
4332 lenp = 4 + (5*j2k->totnum_tp);
4333 cio_write(cio,lenp,2); /* Ltlm */
4334 cio_write(cio, 0,1); /* Ztlm=0*/
4335 cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4336 cio_skip(cio,5*j2k->totnum_tp);
4339 static void j2k_write_sot(opj_j2k_t *j2k) {
4342 opj_cio_t *cio = j2k->cio;
4344 j2k->sot_start = cio_tell(cio);
4345 cio_write(cio, J2K_MS_SOT, 2); /* SOT */
4346 lenp = cio_tell(cio);
4347 cio_skip(cio, 2); /* Lsot (further) */
4348 cio_write(cio, j2k->curtileno, 2); /* Isot */
4349 cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
4350 cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
4351 cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
4352 len = cio_tell(cio) - lenp;
4353 cio_seek(cio, lenp);
4354 cio_write(cio, len, 2); /* Lsot */
4355 cio_seek(cio, lenp + len);
4359 /* update markers struct */
4360 j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
4361 #endif /* USE_JPWL */
4364 if( j2k->cstr_info && j2k->cur_tp_num==0){
4365 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
4370 * Writes the SOT marker (Start of tile-part)
4372 * @param p_stream the stream to write data to.
4373 * @param p_j2k J2K codec.
4374 * @param p_manager the user event manager.
4376 opj_bool j2k_write_sot_v2( opj_j2k_v2_t *p_j2k,
4378 OPJ_UINT32 * p_data_written,
4379 const struct opj_stream_private *p_stream,
4380 struct opj_event_mgr * p_manager )
4383 assert(p_j2k != 00);
4384 assert(p_manager != 00);
4385 assert(p_stream != 00);
4387 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4390 opj_write_bytes(p_data,10,2); /* Lsot */
4393 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4399 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4402 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4407 /* update markers struct */
4409 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4411 assert( 0 && "TODO" );
4412 #endif /* USE_JPWL */
4414 * p_data_written = 12;
4423 * Reads a PPT marker (Packed packet headers, tile-part header)
4425 * @param p_header_data the data contained in the PPT box.
4426 * @param p_j2k the jpeg2000 codec.
4427 * @param p_header_size the size of the data contained in the PPT marker.
4428 * @param p_manager the user event manager.
4430 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4431 OPJ_BYTE * p_header_data,
4432 OPJ_UINT32 p_header_size,
4433 opj_event_mgr_t * p_manager )
4435 opj_cp_v2_t *l_cp = 00;
4436 opj_tcp_v2_t *l_tcp = 00;
4437 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4438 OPJ_UINT32 l_current_part;
4439 OPJ_UINT32 l_tile_x,l_tile_y;
4442 assert(p_header_data != 00);
4443 assert(p_j2k != 00);
4444 assert(p_manager != 00);
4446 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4447 if (p_header_size != 8) {
4448 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4452 l_cp = &(p_j2k->m_cp);
4453 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4456 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4457 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4458 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4461 if (l_cp->correct) {
4463 int tileno = p_j2k->m_current_tile_number;
4464 static int backup_tileno = 0;
4466 /* tileno is negative or larger than the number of tiles!!! */
4467 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4468 opj_event_msg_v2(p_manager, EVT_ERROR,
4469 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4470 tileno, (l_cp->tw * l_cp->th));
4472 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4475 /* we try to correct */
4476 tileno = backup_tileno;
4477 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4478 "- setting tile number to %d\n",
4482 /* keep your private count of tiles */
4485 #endif /* USE_JPWL */
4487 /* look for the tile in the list of already processed tile (in parts). */
4488 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4489 /* since the time taken by this function can only grow at the time */
4491 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4494 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4495 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4497 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4503 if (l_cp->correct) {
4505 /* totlen is negative or larger than the bytes left!!! */
4506 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))) { */
4507 opj_event_msg_v2(p_manager, EVT_ERROR,
4508 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4509 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4511 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4514 /* we try to correct */
4516 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4517 "- setting Psot to %d => assuming it is the last tile\n",
4521 #endif /* USE_JPWL */
4523 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4525 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4526 "we assuming it is the last tile-part of the codestream.\n");
4527 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4530 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4533 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4536 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4537 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4538 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4539 if (l_tcp->m_nb_tile_parts) {
4540 if (l_current_part >= l_tcp->m_nb_tile_parts){
4541 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4542 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4543 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4547 l_tcp->m_nb_tile_parts = l_num_parts;
4550 /* If know the number of tile part header we will check if we didn't read the last*/
4551 if (l_tcp->m_nb_tile_parts) {
4552 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4553 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4557 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4558 /* Keep the size of data to skip after this marker */
4559 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4562 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4563 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4566 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4568 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4569 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4570 p_j2k->m_specific_param.m_decoder.m_skip_data =
4571 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4572 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4573 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4574 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4577 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4578 p_j2k->m_specific_param.m_decoder.m_skip_data =
4579 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4583 if (p_j2k->cstr_index)
4585 assert(p_j2k->cstr_index->tile_index != 00);
4586 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4587 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4589 if (l_num_parts != 0){
4590 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4591 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4594 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4595 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4596 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4598 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4599 (opj_tp_index_t*)opj_realloc(
4600 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4601 l_num_parts* sizeof(opj_tp_index_t));
4604 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4606 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4607 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4608 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4609 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4610 sizeof(opj_tp_index_t));
4613 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4614 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4615 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4616 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4617 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4618 * sizeof(opj_tp_index_t));
4627 /* 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 */
4628 /* if (p_j2k->cstr_info) {
4631 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4634 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4635 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4636 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4637 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4640 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4643 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4647 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4650 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4651 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4652 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4657 static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
4660 opj_tcp_t *tcp = NULL;
4661 opj_codestream_info_t *cstr_info = NULL;
4663 opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
4664 opj_cp_t *cp = j2k->cp;
4665 opj_cio_t *cio = j2k->cio;
4667 tcd->tp_num = j2k->tp_num ;
4668 tcd->cur_tp_num = j2k->cur_tp_num;
4670 cio_write(cio, J2K_MS_SOD, 2);
4672 if( j2k->cstr_info && j2k->cur_tp_num==0){
4673 j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
4676 if (j2k->curtileno == 0) {
4677 j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
4681 cstr_info = j2k->cstr_info;
4683 if (!j2k->cur_tp_num ) {
4684 cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
4685 j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
4688 if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
4689 cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
4693 /* update markers struct */
4694 j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
4695 #endif /* USE_JPWL */
4700 tcp = &cp->tcps[j2k->curtileno];
4701 for (layno = 0; layno < tcp->numlayers; layno++) {
4702 if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
4703 tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
4704 } else if (tcp->rates[layno]) {
4705 tcp->rates[layno]=1;
4708 if(j2k->cur_tp_num == 0){
4709 tcd->tcd_image->tiles->packno = 0;
4711 cstr_info->packno = 0;
4714 l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
4716 /* Writing Psot in SOT marker */
4717 totlen = cio_tell(cio) + l - j2k->sot_start;
4718 cio_seek(cio, j2k->sot_start + 6);
4719 cio_write(cio, totlen, 4);
4720 cio_seek(cio, j2k->sot_start + totlen);
4721 /* Writing Ttlm and Ptlm in TLM marker */
4723 cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
4724 cio_write(cio, j2k->curtileno, 1);
4725 cio_write(cio, totlen, 4);
4727 cio_seek(cio, j2k->sot_start + totlen);
4731 * Writes the SOD marker (Start of data)
4733 * @param p_stream the stream to write data to.
4734 * @param p_j2k J2K codec.
4735 * @param p_manager the user event manager.
4737 opj_bool j2k_write_sod_v2( opj_j2k_v2_t *p_j2k,
4738 struct opj_tcd_v2 * p_tile_coder,
4740 OPJ_UINT32 * p_data_written,
4741 OPJ_UINT32 p_total_data_size,
4742 const struct opj_stream_private *p_stream,
4743 struct opj_event_mgr * p_manager )
4745 opj_tcp_v2_t *l_tcp = 00;
4746 opj_codestream_info_t *l_cstr_info = 00;
4747 opj_cp_v2_t *l_cp = 00;
4749 OPJ_UINT32 l_size_tile;
4750 OPJ_UINT32 l_remaining_data;
4753 assert(p_j2k != 00);
4754 assert(p_manager != 00);
4755 assert(p_stream != 00);
4757 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4760 /* make room for the EOF marker */
4761 l_remaining_data = p_total_data_size - 4;
4763 l_cp = &(p_j2k->m_cp);
4764 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4767 /* update tile coder */
4768 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4769 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4771 l_size_tile = l_cp->th * l_cp->tw;
4774 /* TODO mergeV2: check this part which use cstr_info */
4775 /*l_cstr_info = p_j2k->cstr_info;
4777 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4778 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4779 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4785 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4787 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4792 /* update markers struct */
4793 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4795 assert( 0 && "TODO" );
4796 #endif /* USE_JPWL */
4801 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4802 p_tile_coder->tcd_image->tiles->packno = 0;
4804 l_cstr_info->packno = 0;
4808 *p_data_written = 0;
4810 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)) {
4811 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4815 *p_data_written += 2;
4824 * Reads a SOD marker (Start Of Data)
4826 * @param p_header_data the data contained in the SOD box.
4827 * @param p_j2k the jpeg2000 codec.
4828 * @param p_header_size the size of the data contained in the SOD marker.
4829 * @param p_manager the user event manager.
4831 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4832 opj_stream_private_t *p_stream,
4833 opj_event_mgr_t * p_manager
4836 OPJ_UINT32 l_current_read_size;
4837 opj_codestream_index_t * l_cstr_index = 00;
4838 OPJ_BYTE ** l_current_data = 00;
4839 opj_tcp_v2_t * l_tcp = 00;
4840 OPJ_UINT32 * l_tile_len = 00;
4843 assert(p_j2k != 00);
4844 assert(p_manager != 00);
4845 assert(p_stream != 00);
4847 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4849 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4850 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4851 // but we are in the last tile part,
4852 // so its result will fit on OPJ_UINT32 unless we find
4853 // a file with a single tile part of more than 4 GB...*/
4854 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4857 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4859 l_current_data = &(l_tcp->m_data);
4860 l_tile_len = &l_tcp->m_data_size;
4862 if (! *l_current_data) {
4863 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4866 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4869 if (*l_current_data == 00) {
4870 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4876 l_cstr_index = p_j2k->cstr_index;
4878 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4880 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4881 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4883 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4884 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4886 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4890 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4892 /*l_cstr_index->packno = 0;*/
4895 l_current_read_size = opj_stream_read_data( p_stream,
4896 *l_current_data + *l_tile_len,
4897 p_j2k->m_specific_param.m_decoder.m_sot_length,
4900 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4901 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4904 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4907 *l_tile_len += l_current_read_size;
4913 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
4914 opj_cp_t *cp = j2k->cp;
4915 opj_tcp_t *tcp = &cp->tcps[tileno];
4916 opj_cio_t *cio = j2k->cio;
4917 int numcomps = j2k->image->numcomps;
4919 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
4920 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
4921 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
4922 cio_write(cio, 0, 1); /* Srgn */
4923 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
4927 * Writes the RGN marker (Region Of Interest)
4929 * @param p_tile_no the tile to output
4930 * @param p_comp_no the component to output
4931 * @param p_stream the stream to write data to.
4932 * @param p_j2k J2K codec.
4933 * @param p_manager the user event manager.
4935 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
4936 OPJ_UINT32 p_tile_no,
4937 OPJ_UINT32 p_comp_no,
4938 struct opj_stream_private *p_stream,
4939 struct opj_event_mgr * p_manager )
4941 OPJ_BYTE * l_current_data = 00;
4942 OPJ_UINT32 l_nb_comp;
4943 OPJ_UINT32 l_rgn_size;
4944 opj_image_t *l_image = 00;
4945 opj_cp_v2_t *l_cp = 00;
4946 opj_tcp_v2_t *l_tcp = 00;
4947 opj_tccp_t *l_tccp = 00;
4948 OPJ_UINT32 l_comp_room;
4951 assert(p_j2k != 00);
4952 assert(p_manager != 00);
4953 assert(p_stream != 00);
4955 l_cp = &(p_j2k->m_cp);
4956 l_tcp = &l_cp->tcps[p_tile_no];
4957 l_tccp = &l_tcp->tccps[p_comp_no];
4959 l_nb_comp = l_image->numcomps;
4961 if (l_nb_comp <= 256) {
4968 l_rgn_size = 6 + l_comp_room;
4970 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4972 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
4973 l_current_data += 2;
4975 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
4976 l_current_data += 2;
4978 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
4979 l_current_data+=l_comp_room;
4981 opj_write_bytes(l_current_data, 0,1); /* Srgn */
4984 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
4987 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) {
4995 static void j2k_write_eoc(opj_j2k_t *j2k) {
4996 opj_cio_t *cio = j2k->cio;
4997 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
4998 cio_write(cio, J2K_MS_EOC, 2);
5002 /* update markers struct */
5003 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
5004 #endif /* USE_JPWL */
5009 * Writes the EOC marker (End of Codestream)
5011 * @param p_stream the stream to write data to.
5012 * @param p_j2k J2K codec.
5013 * @param p_manager the user event manager.
5015 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
5016 struct opj_stream_private *p_stream,
5017 struct opj_event_mgr * p_manager )
5020 assert(p_j2k != 00);
5021 assert(p_manager != 00);
5022 assert(p_stream != 00);
5024 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
5029 /* update markers struct */
5031 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
5033 #endif /* USE_JPWL */
5035 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
5039 if ( ! opj_stream_flush(p_stream,p_manager) ) {
5048 * Reads a RGN marker (Region Of Interest)
5050 * @param p_header_data the data contained in the POC box.
5051 * @param p_j2k the jpeg2000 codec.
5052 * @param p_header_size the size of the data contained in the POC marker.
5053 * @param p_manager the user event manager.
5055 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
5056 OPJ_BYTE * p_header_data,
5057 OPJ_UINT32 p_header_size,
5058 opj_event_mgr_t * p_manager
5061 OPJ_UINT32 l_nb_comp;
5062 opj_image_t * l_image = 00;
5064 opj_cp_v2_t *l_cp = 00;
5065 opj_tcp_v2_t *l_tcp = 00;
5066 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
5069 assert(p_header_data != 00);
5070 assert(p_j2k != 00);
5071 assert(p_manager != 00);
5073 l_image = p_j2k->m_private_image;
5074 l_nb_comp = l_image->numcomps;
5076 if (l_nb_comp <= 256) {
5081 if (p_header_size != 2 + l_comp_room) {
5082 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
5086 l_cp = &(p_j2k->m_cp);
5087 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
5088 &l_cp->tcps[p_j2k->m_current_tile_number] :
5089 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5091 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
5092 p_header_data+=l_comp_room;
5093 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
5097 if (l_cp->correct) {
5098 /* totlen is negative or larger than the bytes left!!! */
5099 if (l_comp_room >= l_nb_comp) {
5100 opj_event_msg_v2(p_manager, EVT_ERROR,
5101 "JPWL: bad component number in RGN (%d when there are only %d)\n",
5102 l_comp_room, l_nb_comp);
5103 if (!JPWL_ASSUME || JPWL_ASSUME) {
5104 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
5109 #endif /* USE_JPWL */
5111 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
5118 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
5120 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
5123 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
5130 * Updates the rates of the tcp.
5132 * @param p_stream the stream to write data to.
5133 * @param p_j2k J2K codec.
5134 * @param p_manager the user event manager.
5136 opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k,
5137 struct opj_stream_private *p_stream,
5138 struct opj_event_mgr * p_manager )
5140 opj_cp_v2_t * l_cp = 00;
5141 opj_image_t * l_image = 00;
5142 opj_tcp_v2_t * l_tcp = 00;
5143 opj_image_comp_t * l_img_comp = 00;
5146 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
5147 OPJ_FLOAT32 * l_rates = 0;
5148 OPJ_FLOAT32 l_sot_remove;
5149 OPJ_UINT32 l_bits_empty, l_size_pixel;
5150 OPJ_UINT32 l_tile_size = 0;
5151 OPJ_UINT32 l_last_res;
5152 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
5155 assert(p_j2k != 00);
5156 assert(p_manager != 00);
5157 assert(p_stream != 00);
5160 l_cp = &(p_j2k->m_cp);
5161 l_image = p_j2k->m_private_image;
5164 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
5165 l_size_pixel = l_image->numcomps * l_image->comps->prec;
5166 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
5168 if (l_cp->m_specific_param.m_enc.m_tp_on) {
5169 l_tp_stride_func = get_tp_stride;
5172 l_tp_stride_func = get_default_stride;
5175 for (i=0;i<l_cp->th;++i) {
5176 for (j=0;j<l_cp->tw;++j) {
5177 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
5179 /* 4 borders of the tile rescale on the image if necessary */
5180 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
5181 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
5182 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
5183 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
5185 l_rates = l_tcp->rates;
5187 /* Modification of the RATE >> */
5189 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5191 ((*l_rates) * l_bits_empty)
5199 for (k = 1; k < l_tcp->numlayers; ++k) {
5201 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
5203 ((*l_rates) * l_bits_empty)
5219 for (i=0;i<l_cp->th;++i) {
5220 for (j=0;j<l_cp->tw;++j) {
5221 l_rates = l_tcp->rates;
5224 *l_rates -= l_sot_remove;
5226 if (*l_rates < 30) {
5233 l_last_res = l_tcp->numlayers - 1;
5235 for (k = 1; k < l_last_res; ++k) {
5238 *l_rates -= l_sot_remove;
5240 if (*l_rates < *(l_rates - 1) + 10) {
5241 *l_rates = (*(l_rates - 1)) + 20;
5249 *l_rates -= (l_sot_remove + 2.f);
5251 if (*l_rates < *(l_rates - 1) + 10) {
5252 *l_rates = (*(l_rates - 1)) + 20;
5260 l_img_comp = l_image->comps;
5263 for (i=0;i<l_image->numcomps;++i) {
5264 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
5266 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
5274 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
5276 l_tile_size += j2k_get_specific_header_sizes(p_j2k);
5278 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
5279 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
5280 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
5281 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
5285 if (l_cp->m_specific_param.m_enc.m_cinema) {
5286 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
5287 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
5288 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
5292 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
5293 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
5299 static void j2k_read_eoc(opj_j2k_t *j2k) {
5301 opj_bool success = OPJ_FALSE;
5303 /* if packets should be decoded */
5304 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
5305 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
5306 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
5307 for (i = 0; i < j2k->cp->tileno_size; i++) {
5308 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
5309 if (j2k->cp->tileno[i] != -1)
5311 tileno = j2k->cp->tileno[i];
5312 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
5313 opj_free(j2k->tile_data[tileno]);
5314 j2k->tile_data[tileno] = NULL;
5315 tcd_free_decode_tile(tcd, i);
5318 success = OPJ_FALSE;
5319 if (success == OPJ_FALSE) {
5320 j2k->state |= J2K_STATE_ERR;
5324 tcd_free_decode(tcd);
5327 /* if packets should not be decoded */
5329 for (i = 0; i < j2k->cp->tileno_size; i++) {
5330 tileno = j2k->cp->tileno[i];
5331 opj_free(j2k->tile_data[tileno]);
5332 j2k->tile_data[tileno] = NULL;
5335 if (j2k->state & J2K_STATE_ERR)
5336 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
5338 j2k->state = J2K_STATE_MT;
5342 * Reads a EOC marker (End Of Codestream)
5344 * @param p_header_data the data contained in the SOD box.
5345 * @param p_j2k the jpeg2000 codec.
5346 * @param p_header_size the size of the data contained in the SOD marker.
5347 * @param p_manager the user event manager.
5350 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
5351 struct opj_stream_private *p_stream,
5352 struct opj_event_mgr * p_manager )
5355 opj_tcd_v2_t * l_tcd = 00;
5356 OPJ_UINT32 l_nb_tiles;
5357 opj_tcp_v2_t * l_tcp = 00;
5361 assert(p_j2k != 00);
5362 assert(p_manager != 00);
5363 assert(p_stream != 00);
5365 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
5366 l_tcp = p_j2k->m_cp.tcps;
5368 l_tcd = tcd_create_v2(OPJ_TRUE);
5370 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5374 for (i = 0; i < l_nb_tiles; ++i) {
5375 if (l_tcp->m_data) {
5376 if (! tcd_init_decode_tile(l_tcd, i)) {
5377 tcd_destroy_v2(l_tcd);
5378 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
5382 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
5386 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
5391 opj_j2k_tcp_destroy(l_tcp);
5395 tcd_destroy_v2(l_tcd);
5401 * Gets the offset of the header.
5403 * @param p_stream the stream to write data to.
5404 * @param p_j2k J2K codec.
5405 * @param p_manager the user event manager.
5407 opj_bool opj_j2k_get_end_header(opj_j2k_v2_t *p_j2k,
5408 struct opj_stream_private *p_stream,
5409 struct opj_event_mgr * p_manager )
5412 assert(p_j2k != 00);
5413 assert(p_manager != 00);
5414 assert(p_stream != 00);
5416 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5422 * Writes the MCT marker (Multiple Component Transform)
5424 * @param p_stream the stream to write data to.
5425 * @param p_j2k J2K codec.
5426 * @param p_manager the user event manager.
5428 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5429 struct opj_stream_private *p_stream,
5430 struct opj_event_mgr * p_manager )
5433 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5434 opj_mct_data_t * l_mct_record;
5435 opj_tcp_v2_t * l_tcp;
5438 assert(p_j2k != 00);
5439 assert(p_stream != 00);
5440 assert(p_manager != 00);
5442 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5446 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5447 l_mct_record = l_tcp->m_mct_records;
5449 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5451 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5458 l_mcc_record = l_tcp->m_mcc_records;
5460 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5462 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5469 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5477 * Writes the image components.
5479 * @param p_stream the stream to write data to.
5480 * @param p_j2k J2K codec.
5481 * @param p_manager the user event manager.
5483 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5484 struct opj_stream_private *p_stream,
5485 struct opj_event_mgr * p_manager )
5490 assert(p_j2k != 00);
5491 assert(p_manager != 00);
5492 assert(p_stream != 00);
5494 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5496 if (! j2k_write_coc_v2(p_j2k,compno,p_stream, p_manager)) {
5500 if (! j2k_write_qcc_v2(p_j2k,compno,p_stream, p_manager)) {
5509 * Writes regions of interests.
5511 * @param p_stream the stream to write data to.
5512 * @param p_j2k J2K codec.
5513 * @param p_manager the user event manager.
5515 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5516 struct opj_stream_private *p_stream,
5517 struct opj_event_mgr * p_manager )
5520 const opj_tccp_t *l_tccp = 00;
5523 assert(p_j2k != 00);
5524 assert(p_manager != 00);
5525 assert(p_stream != 00);
5527 l_tccp = p_j2k->m_cp.tcps->tccps;
5529 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5530 if (l_tccp->roishift) {
5532 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5546 * @param p_stream the stream to write data to.
5547 * @param p_j2k J2K codec.
5548 * @param p_manager the user event manager.
5550 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5551 struct opj_stream_private *p_stream,
5552 struct opj_event_mgr * p_manager )
5554 opj_codestream_index_t * l_cstr_index = 00;
5557 assert(p_j2k != 00);
5558 assert(p_manager != 00);
5559 assert(p_stream != 00);
5561 l_cstr_index = p_j2k->cstr_index;
5563 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5565 /* The following adjustment is done to adjust the codestream size */
5566 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5567 /* the first bunch of bytes is not in the codestream */
5568 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5573 /* preparation of JPWL marker segments */
5577 /* encode according to JPWL */
5578 jpwl_encode(p_j2k, p_stream, image);
5582 assert( 0 && "TODO" );
5583 #endif /* USE_JPWL */
5588 typedef struct opj_dec_mstabent {
5591 /** value of the state when the marker can appear */
5593 /** action linked to the marker */
5594 void (*handler) (opj_j2k_t *j2k);
5595 } opj_dec_mstabent_t;
5597 opj_dec_mstabent_t j2k_dec_mstab[] = {
5598 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5599 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5600 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5601 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5602 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5603 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5604 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5605 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5606 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5607 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5608 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5609 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5610 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5611 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5612 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5613 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5615 /*{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},*/
5616 /*{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},*/
5619 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5620 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5621 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5622 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5623 #endif /* USE_JPWL */
5625 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5626 {J2K_MS_INSEC, 0, j2k_read_insec},
5627 #endif /* USE_JPSEC */
5629 /*{0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}*/
5635 * Reads an unknown marker
5637 * @param p_stream the stream object to read from.
5638 * @param p_j2k the jpeg2000 codec.
5639 * @param p_manager the user event manager.
5641 * @return true if the marker could be deduced.
5643 opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
5644 opj_stream_private_t *p_stream,
5645 OPJ_UINT32 *output_marker,
5646 opj_event_mgr_t * p_manager
5649 OPJ_UINT32 l_unknown_marker;
5650 const opj_dec_memory_marker_handler_t * l_marker_handler;
5651 OPJ_UINT32 l_size_unk = 2;
5654 assert(p_j2k != 00);
5655 assert(p_manager != 00);
5656 assert(p_stream != 00);
5658 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5661 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5662 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5663 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5667 /* read 2 bytes as the new marker ID*/
5668 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5670 if (!(l_unknown_marker < 0xff00)) {
5672 /* Get the marker handler from the marker ID*/
5673 l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
5675 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5676 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5680 if (l_marker_handler->id != J2K_MS_UNK) {
5681 /* Add the marker to the codestream index*/
5682 if (l_marker_handler->id != J2K_MS_SOT)
5683 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5684 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5686 break; /* next marker is known and well located */
5694 *output_marker = l_marker_handler->id ;
5700 Read the lookup table containing all the marker, status and action
5701 @param id Marker value
5703 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5704 opj_dec_mstabent_t *e;
5705 for (e = j2k_dec_mstab; e->id != 0; e++) {
5714 * Writes the MCT marker (Multiple Component Transform)
5716 * @param p_stream the stream to write data to.
5717 * @param p_j2k J2K codec.
5718 * @param p_manager the user event manager.
5720 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5721 opj_mct_data_t * p_mct_record,
5722 struct opj_stream_private *p_stream,
5723 struct opj_event_mgr * p_manager )
5725 OPJ_UINT32 l_mct_size;
5726 OPJ_BYTE * l_current_data = 00;
5730 assert(p_j2k != 00);
5731 assert(p_manager != 00);
5732 assert(p_stream != 00);
5734 l_mct_size = 10 + p_mct_record->m_data_size;
5736 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5737 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5738 = (OPJ_BYTE*)opj_realloc(
5739 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5742 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5746 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5749 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5751 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5752 l_current_data += 2;
5754 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5755 l_current_data += 2;
5757 opj_write_bytes(l_current_data,0,2); /* Zmct */
5758 l_current_data += 2;
5760 /* only one marker atm */
5761 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5763 opj_write_bytes(l_current_data,l_tmp,2);
5764 l_current_data += 2;
5766 opj_write_bytes(l_current_data,0,2); /* Ymct */
5769 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5771 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) {
5779 * Reads a MCT marker (Multiple Component Transform)
5781 * @param p_header_data the data contained in the MCT box.
5782 * @param p_j2k the jpeg2000 codec.
5783 * @param p_header_size the size of the data contained in the MCT marker.
5784 * @param p_manager the user event manager.
5786 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5787 OPJ_BYTE * p_header_data,
5788 OPJ_UINT32 p_header_size,
5789 opj_event_mgr_t * p_manager
5793 opj_tcp_v2_t *l_tcp = 00;
5796 opj_mct_data_t * l_mct_data;
5799 assert(p_header_data != 00);
5800 assert(p_j2k != 00);
5802 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5803 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5804 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5806 if (p_header_size < 2) {
5807 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5812 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5815 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5819 if(p_header_size <= 6) {
5820 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5824 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5825 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5828 l_indix = l_tmp & 0xff;
5829 l_mct_data = l_tcp->m_mct_records;
5831 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5832 if (l_mct_data->m_index == l_indix) {
5839 if (i == l_tcp->m_nb_mct_records) {
5840 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5841 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5843 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));
5844 if(! l_tcp->m_mct_records) {
5845 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5849 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5850 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5853 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5856 if (l_mct_data->m_data) {
5857 opj_free(l_mct_data->m_data);
5858 l_mct_data->m_data = 00;
5861 l_mct_data->m_index = l_indix;
5862 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5863 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5865 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5868 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5874 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5875 if (! l_mct_data->m_data) {
5876 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5879 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5881 l_mct_data->m_data_size = p_header_size;
5882 ++l_tcp->m_nb_mct_records;
5888 * Writes the MCC marker (Multiple Component Collection)
5890 * @param p_stream the stream to write data to.
5891 * @param p_j2k J2K codec.
5892 * @param p_manager the user event manager.
5894 opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
5895 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5896 struct opj_stream_private *p_stream,
5897 struct opj_event_mgr * p_manager )
5900 OPJ_UINT32 l_mcc_size;
5901 OPJ_BYTE * l_current_data = 00;
5902 OPJ_UINT32 l_nb_bytes_for_comp;
5907 assert(p_j2k != 00);
5908 assert(p_manager != 00);
5909 assert(p_stream != 00);
5911 if (p_mcc_record->m_nb_comps > 255 ) {
5912 l_nb_bytes_for_comp = 2;
5916 l_nb_bytes_for_comp = 1;
5920 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5921 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5923 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5924 = (OPJ_BYTE*)opj_realloc(
5925 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5927 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5931 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5934 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5936 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
5937 l_current_data += 2;
5939 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
5940 l_current_data += 2;
5943 opj_write_bytes(l_current_data,0,2); /* Zmcc */
5944 l_current_data += 2;
5946 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
5949 /* only one marker atm */
5950 opj_write_bytes(l_current_data,0,2); /* Ymcc */
5953 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
5956 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
5959 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 */
5962 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5963 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
5964 l_current_data+=l_nb_bytes_for_comp;
5967 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 */
5970 for (i=0;i<p_mcc_record->m_nb_comps;++i)
5972 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
5973 l_current_data+=l_nb_bytes_for_comp;
5976 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
5978 if (p_mcc_record->m_decorrelation_array) {
5979 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
5982 if (p_mcc_record->m_offset_array) {
5983 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
5986 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
5989 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) {
5997 * Reads a MCC marker (Multiple Component Collection)
5999 * @param p_header_data the data contained in the MCC box.
6000 * @param p_j2k the jpeg2000 codec.
6001 * @param p_header_size the size of the data contained in the MCC marker.
6002 * @param p_manager the user event manager.
6004 opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
6005 OPJ_BYTE * p_header_data,
6006 OPJ_UINT32 p_header_size,
6007 opj_event_mgr_t * p_manager )
6012 opj_tcp_v2_t * l_tcp;
6013 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6014 opj_mct_data_t * l_mct_data;
6015 OPJ_UINT32 l_nb_collections;
6016 OPJ_UINT32 l_nb_comps;
6017 OPJ_UINT32 l_nb_bytes_by_comp;
6021 assert(p_header_data != 00);
6022 assert(p_j2k != 00);
6023 assert(p_manager != 00);
6025 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6026 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6027 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6029 if (p_header_size < 2) {
6030 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6035 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
6038 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6042 if (p_header_size < 7) {
6043 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6047 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
6050 l_mcc_record = l_tcp->m_mcc_records;
6052 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
6053 if (l_mcc_record->m_index == l_indix) {
6060 if (i == l_tcp->m_nb_mcc_records) {
6061 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
6062 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
6064 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
6065 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
6066 if (! l_tcp->m_mcc_records) {
6067 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6070 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6071 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));
6073 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
6075 l_mcc_record->m_index = l_indix;
6077 /* only one marker atm */
6078 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
6081 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
6085 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
6088 if (l_nb_collections > 1) {
6089 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
6095 for (i=0;i<l_nb_collections;++i) {
6096 if (p_header_size < 3) {
6097 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6101 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
6105 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
6109 opj_read_bytes(p_header_data,&l_nb_comps,2);
6114 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6115 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
6117 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
6118 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6122 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
6124 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6125 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
6126 p_header_data+=l_nb_bytes_by_comp;
6129 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6134 opj_read_bytes(p_header_data,&l_nb_comps,2);
6137 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
6138 l_nb_comps &= 0x7fff;
6140 if (l_nb_comps != l_mcc_record->m_nb_comps) {
6141 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
6145 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
6146 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6150 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
6152 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
6153 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
6154 p_header_data+=l_nb_bytes_by_comp;
6157 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
6162 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
6165 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
6166 l_mcc_record->m_decorrelation_array = 00;
6167 l_mcc_record->m_offset_array = 00;
6169 l_indix = l_tmp & 0xff;
6171 l_mct_data = l_tcp->m_mct_records;
6172 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6173 if (l_mct_data->m_index == l_indix) {
6174 l_mcc_record->m_decorrelation_array = l_mct_data;
6180 if (l_mcc_record->m_decorrelation_array == 00) {
6181 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6186 l_indix = (l_tmp >> 8) & 0xff;
6188 l_mct_data = l_tcp->m_mct_records;
6189 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
6190 if (l_mct_data->m_index == l_indix) {
6191 l_mcc_record->m_offset_array = l_mct_data;
6197 if (l_mcc_record->m_offset_array == 00) {
6198 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6204 if (p_header_size != 0) {
6205 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
6209 ++l_tcp->m_nb_mcc_records;
6216 * Writes the MCO marker (Multiple component transformation ordering)
6218 * @param p_stream the stream to write data to.
6219 * @param p_j2k J2K codec.
6220 * @param p_manager the user event manager.
6222 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
6223 struct opj_stream_private *p_stream,
6224 struct opj_event_mgr * p_manager
6227 OPJ_BYTE * l_current_data = 00;
6228 OPJ_UINT32 l_mco_size;
6229 opj_tcp_v2_t * l_tcp = 00;
6230 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6234 assert(p_j2k != 00);
6235 assert(p_manager != 00);
6236 assert(p_stream != 00);
6238 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
6239 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6241 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
6242 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6244 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6245 = (OPJ_BYTE*)opj_realloc(
6246 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6248 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
6253 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
6256 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
6257 l_current_data += 2;
6259 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
6260 l_current_data += 2;
6262 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
6265 l_mcc_record = l_tcp->m_mcc_records;
6266 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
6267 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
6273 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) {
6281 * Reads a MCO marker (Multiple Component Transform Ordering)
6283 * @param p_header_data the data contained in the MCO box.
6284 * @param p_j2k the jpeg2000 codec.
6285 * @param p_header_size the size of the data contained in the MCO marker.
6286 * @param p_manager the user event manager.
6288 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
6289 OPJ_BYTE * p_header_data,
6290 OPJ_UINT32 p_header_size,
6291 opj_event_mgr_t * p_manager
6294 OPJ_UINT32 l_tmp, i;
6295 OPJ_UINT32 l_nb_stages;
6296 opj_tcp_v2_t * l_tcp;
6297 opj_tccp_t * l_tccp;
6298 opj_image_t * l_image;
6299 opj_image_comp_t * l_img_comp;
6302 assert(p_header_data != 00);
6303 assert(p_j2k != 00);
6304 assert(p_manager != 00);
6306 l_image = p_j2k->m_private_image;
6307 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
6308 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
6309 p_j2k->m_specific_param.m_decoder.m_default_tcp;
6311 if (p_header_size < 1) {
6312 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
6316 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
6319 if (l_nb_stages > 1) {
6320 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
6324 if (p_header_size != l_nb_stages + 1) {
6325 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
6329 l_tccp = l_tcp->tccps;
6330 l_img_comp = l_image->comps;
6332 for (i=0;i<l_image->numcomps;++i) {
6333 l_tccp->m_dc_level_shift = 0;
6337 if (l_tcp->m_mct_decoding_matrix) {
6338 opj_free(l_tcp->m_mct_decoding_matrix);
6339 l_tcp->m_mct_decoding_matrix = 00;
6342 for (i=0;i<l_nb_stages;++i) {
6343 opj_read_bytes(p_header_data,&l_tmp,1);
6346 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
6354 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
6357 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
6358 opj_mct_data_t * l_deco_array, * l_offset_array;
6359 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
6360 OPJ_UINT32 l_nb_elem;
6361 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
6362 opj_tccp_t * l_tccp;
6365 assert(p_tcp != 00);
6367 l_mcc_record = p_tcp->m_mcc_records;
6369 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
6370 if (l_mcc_record->m_index == p_index) {
6375 if (i==p_tcp->m_nb_mcc_records) {
6376 /** element discarded **/
6380 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
6381 /** do not support number of comps != image */
6385 l_deco_array = l_mcc_record->m_decorrelation_array;
6388 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
6389 if (l_deco_array->m_data_size != l_data_size) {
6393 l_nb_elem = p_image->numcomps * p_image->numcomps;
6394 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
6395 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
6397 if (! p_tcp->m_mct_decoding_matrix ) {
6401 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);
6404 l_offset_array = l_mcc_record->m_offset_array;
6406 if (l_offset_array) {
6407 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
6408 if (l_offset_array->m_data_size != l_data_size) {
6412 l_nb_elem = p_image->numcomps;
6413 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6414 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6416 if (! l_offset_data ) {
6420 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6422 l_tccp = p_tcp->tccps;
6423 l_current_offset_data = l_offset_data;
6425 for (i=0;i<p_image->numcomps;++i) {
6426 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6430 opj_free(l_offset_data);
6437 * Writes the CBD marker (Component bit depth definition)
6439 * @param p_stream the stream to write data to.
6440 * @param p_j2k J2K codec.
6441 * @param p_manager the user event manager.
6443 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6444 struct opj_stream_private *p_stream,
6445 struct opj_event_mgr * p_manager )
6448 OPJ_UINT32 l_cbd_size;
6449 OPJ_BYTE * l_current_data = 00;
6450 opj_image_t *l_image = 00;
6451 opj_image_comp_t * l_comp = 00;
6454 assert(p_j2k != 00);
6455 assert(p_manager != 00);
6456 assert(p_stream != 00);
6458 l_image = p_j2k->m_private_image;
6459 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6461 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6462 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6463 = (OPJ_BYTE*)opj_realloc(
6464 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6467 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6471 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6474 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6476 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6477 l_current_data += 2;
6479 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6480 l_current_data += 2;
6482 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6485 l_comp = l_image->comps;
6487 for (i=0;i<l_image->numcomps;++i) {
6488 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6494 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) {
6502 * Reads a CBD marker (Component bit depth definition)
6503 * @param p_header_data the data contained in the CBD box.
6504 * @param p_j2k the jpeg2000 codec.
6505 * @param p_header_size the size of the data contained in the CBD marker.
6506 * @param p_manager the user event manager.
6508 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6509 OPJ_BYTE * p_header_data,
6510 OPJ_UINT32 p_header_size,
6511 opj_event_mgr_t * p_manager
6514 OPJ_UINT32 l_nb_comp,l_num_comp;
6515 OPJ_UINT32 l_comp_def;
6517 opj_image_comp_t * l_comp = 00;
6520 assert(p_header_data != 00);
6521 assert(p_j2k != 00);
6522 assert(p_manager != 00);
6524 l_num_comp = p_j2k->m_private_image->numcomps;
6526 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6527 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6531 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6534 if (l_nb_comp != l_num_comp) {
6535 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6539 l_comp = p_j2k->m_private_image->comps;
6540 for (i=0;i<l_num_comp;++i) {
6541 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6543 l_comp->sgnd = (l_comp_def>>7) & 1;
6544 l_comp->prec = (l_comp_def&0x7f) + 1;
6552 /* ----------------------------------------------------------------------- */
6553 /* J2K / JPT decoder interface */
6554 /* ----------------------------------------------------------------------- */
6556 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6558 if(j2k && parameters) {
6559 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6560 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6563 j2k->m_cp.correct = parameters->jpwl_correct;
6564 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6565 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6566 #endif /* USE_JPWL */
6571 /* ----------------------------------------------------------------------- */
6572 /* J2K encoder interface */
6573 /* ----------------------------------------------------------------------- */
6575 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6576 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6583 opj_j2k_v2_t* opj_j2k_create_compress_v2(void)
6585 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6590 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6592 l_j2k->m_is_decoder = 0;
6593 l_j2k->m_cp.m_is_decoder = 0;
6595 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6596 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6597 opj_j2k_destroy(l_j2k);
6601 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6603 /* validation list creation*/
6604 l_j2k->m_validation_list = opj_procedure_list_create();
6605 if (! l_j2k->m_validation_list) {
6606 opj_j2k_destroy(l_j2k);
6610 /* execution list creation*/
6611 l_j2k->m_procedure_list = opj_procedure_list_create();
6612 if (! l_j2k->m_procedure_list) {
6613 opj_j2k_destroy(l_j2k);
6620 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6622 int tileno, numpocs_tile;
6623 opj_cp_t *cp = NULL;
6625 if(!j2k || !parameters || ! image) {
6629 /* create and initialize the coding parameters structure */
6630 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6632 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6635 /* set default values for cp */
6640 copy user encoding parameters
6642 cp->cinema = parameters->cp_cinema;
6643 cp->max_comp_size = parameters->max_comp_size;
6644 cp->rsiz = parameters->cp_rsiz;
6645 cp->disto_alloc = parameters->cp_disto_alloc;
6646 cp->fixed_alloc = parameters->cp_fixed_alloc;
6647 cp->fixed_quality = parameters->cp_fixed_quality;
6649 /* mod fixed_quality */
6650 if(parameters->cp_matrice) {
6651 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6652 cp->matrice = (int *) opj_malloc(array_size);
6653 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6657 cp->tdx = parameters->cp_tdx;
6658 cp->tdy = parameters->cp_tdy;
6661 cp->tx0 = parameters->cp_tx0;
6662 cp->ty0 = parameters->cp_ty0;
6664 /* comment string */
6665 if(parameters->cp_comment) {
6666 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6668 strcpy(cp->comment, parameters->cp_comment);
6673 calculate other encoding parameters
6676 if (parameters->tile_size_on) {
6677 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6678 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6680 cp->tdx = image->x1 - cp->tx0;
6681 cp->tdy = image->y1 - cp->ty0;
6684 if(parameters->tp_on){
6685 cp->tp_flag = parameters->tp_flag;
6690 for(i=0;i<image->numcomps ;i++){
6691 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6697 calculate JPWL encoding parameters
6700 if (parameters->jpwl_epc_on) {
6704 cp->epc_on = OPJ_TRUE;
6705 cp->info_on = OPJ_FALSE; /* no informative technique */
6708 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6709 cp->epb_on = OPJ_TRUE;
6711 cp->hprot_MH = parameters->jpwl_hprot_MH;
6712 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6713 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6714 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6716 /* if tile specs are not specified, copy MH specs */
6717 if (cp->hprot_TPH[0] == -1) {
6718 cp->hprot_TPH_tileno[0] = 0;
6719 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6721 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6722 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6723 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6724 cp->pprot[i] = parameters->jpwl_pprot[i];
6728 /* set ESD writing */
6729 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6730 cp->esd_on = OPJ_TRUE;
6732 cp->sens_size = parameters->jpwl_sens_size;
6733 cp->sens_addr = parameters->jpwl_sens_addr;
6734 cp->sens_range = parameters->jpwl_sens_range;
6736 cp->sens_MH = parameters->jpwl_sens_MH;
6737 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6738 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6739 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6743 /* always set RED writing to false: we are at the encoder */
6744 cp->red_on = OPJ_FALSE;
6747 cp->epc_on = OPJ_FALSE;
6749 #endif /* USE_JPWL */
6752 /* initialize the mutiple tiles */
6753 /* ---------------------------- */
6754 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
6756 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6757 opj_tcp_t *tcp = &cp->tcps[tileno];
6758 tcp->numlayers = parameters->tcp_numlayers;
6759 assert ( tcp->numlayers >= 0 );
6760 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
6762 if (cp->fixed_quality) {
6763 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6765 tcp->rates[j] = parameters->tcp_rates[j];
6767 if (cp->fixed_quality) { /* add fixed_quality */
6768 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6770 tcp->rates[j] = parameters->tcp_rates[j];
6774 tcp->csty = parameters->csty;
6775 tcp->prg = parameters->prog_order;
6776 tcp->mct = parameters->tcp_mct;
6780 if (parameters->numpocs) {
6781 /* initialisation of POC */
6783 assert( parameters->numpocs >= 0 );
6784 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
6785 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6786 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6787 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6788 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6789 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6790 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6791 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6792 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6793 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6797 tcp->numpocs = numpocs_tile -1 ;
6802 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6804 for (i = 0; i < image->numcomps; i++) {
6805 opj_tccp_t *tccp = &tcp->tccps[i];
6806 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6807 tccp->numresolutions = parameters->numresolution;
6808 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6809 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6810 tccp->cblksty = parameters->mode;
6811 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6812 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6814 assert(parameters->roi_compno >= 0);
6815 if (i == (OPJ_UINT32)parameters->roi_compno) {
6816 tccp->roishift = parameters->roi_shift;
6821 if(parameters->cp_cinema)
6823 /*Precinct size for lowest frequency subband=128*/
6826 /*Precinct size at all other resolutions = 256*/
6827 for (j = 1; j < tccp->numresolutions; j++) {
6832 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6834 assert(tccp->numresolutions > 0);
6835 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
6836 if (p < parameters->res_spec) {
6838 if (parameters->prcw_init[p] < 1) {
6841 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
6844 if (parameters->prch_init[p] < 1) {
6847 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
6851 int res_spec = parameters->res_spec;
6852 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6853 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6855 if (size_prcw < 1) {
6858 tccp->prcw[j] = int_floorlog2(size_prcw);
6861 if (size_prch < 1) {
6864 tccp->prch[j] = int_floorlog2(size_prch);
6868 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
6871 for (j = 0; j < tccp->numresolutions; j++) {
6878 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6883 void opj_j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
6884 opj_cparameters_t *parameters,
6886 struct opj_event_mgr * p_manager)
6888 OPJ_UINT32 i, j, tileno, numpocs_tile;
6889 opj_cp_v2_t *cp = 00;
6892 if(!p_j2k || !parameters || ! image) {
6896 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6897 cp = &(p_j2k->m_cp);
6899 /* set default values for cp */
6904 copy user encoding parameters
6906 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
6907 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
6908 cp->rsiz = parameters->cp_rsiz;
6909 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
6910 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
6911 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
6913 /* mod fixed_quality */
6914 if (parameters->cp_matrice) {
6915 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
6916 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
6917 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
6921 cp->tdx = parameters->cp_tdx;
6922 cp->tdy = parameters->cp_tdy;
6925 cp->tx0 = parameters->cp_tx0;
6926 cp->ty0 = parameters->cp_ty0;
6928 /* comment string */
6929 if(parameters->cp_comment) {
6930 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6932 strcpy(cp->comment, parameters->cp_comment);
6937 calculate other encoding parameters
6940 if (parameters->tile_size_on) {
6941 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6942 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6944 cp->tdx = image->x1 - cp->tx0;
6945 cp->tdy = image->y1 - cp->ty0;
6948 if (parameters->tp_on) {
6949 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
6950 cp->m_specific_param.m_enc.m_tp_on = 1;
6955 calculate JPWL encoding parameters
6958 if (parameters->jpwl_epc_on) {
6962 cp->epc_on = OPJ_TRUE;
6963 cp->info_on = OPJ_FALSE; /* no informative technique */
6966 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6967 cp->epb_on = OPJ_TRUE;
6969 cp->hprot_MH = parameters->jpwl_hprot_MH;
6970 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6971 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6972 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6974 /* if tile specs are not specified, copy MH specs */
6975 if (cp->hprot_TPH[0] == -1) {
6976 cp->hprot_TPH_tileno[0] = 0;
6977 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6979 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6980 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6981 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6982 cp->pprot[i] = parameters->jpwl_pprot[i];
6986 /* set ESD writing */
6987 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6988 cp->esd_on = OPJ_TRUE;
6990 cp->sens_size = parameters->jpwl_sens_size;
6991 cp->sens_addr = parameters->jpwl_sens_addr;
6992 cp->sens_range = parameters->jpwl_sens_range;
6994 cp->sens_MH = parameters->jpwl_sens_MH;
6995 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6996 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6997 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
7001 /* always set RED writing to false: we are at the encoder */
7002 cp->red_on = OPJ_FALSE;
7005 cp->epc_on = OPJ_FALSE;
7007 #endif /* USE_JPWL */
7010 /* initialize the mutiple tiles */
7011 /* ---------------------------- */
7012 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
7013 if (parameters->numpocs) {
7014 /* initialisation of POC */
7015 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
7019 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
7020 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
7021 tcp->numlayers = parameters->tcp_numlayers;
7023 for (j = 0; j < tcp->numlayers; j++) {
7024 if(cp->m_specific_param.m_enc.m_cinema){
7025 if (cp->m_specific_param.m_enc.m_fixed_quality) {
7026 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7028 tcp->rates[j] = parameters->tcp_rates[j];
7030 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
7031 tcp->distoratio[j] = parameters->tcp_distoratio[j];
7033 tcp->rates[j] = parameters->tcp_rates[j];
7038 tcp->csty = parameters->csty;
7039 tcp->prg = parameters->prog_order;
7040 tcp->mct = parameters->tcp_mct;
7045 if (parameters->numpocs) {
7046 /* initialisation of POC */
7049 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
7050 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
7051 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
7053 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
7054 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
7055 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
7056 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
7057 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
7058 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
7059 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
7065 tcp->numpocs = numpocs_tile -1 ;
7070 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
7072 if (parameters->mct_data) {
7074 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
7077 /* TODO MSD : merge v2 add invert.c or used a external lib ?
7078 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
7079 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7080 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
7083 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7084 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
7085 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
7087 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
7088 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
7090 tcp->mct_norms = (OPJ_FLOAT64*)
7091 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
7093 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
7096 for (i = 0; i < image->numcomps; i++) {
7097 opj_tccp_t *tccp = &tcp->tccps[i];
7098 tccp->m_dc_level_shift = l_dc_shift[i];
7101 opj_j2k_setup_mct_encoding(tcp,image);
7105 for (i = 0; i < image->numcomps; i++) {
7106 opj_tccp_t *tccp = &tcp->tccps[i];
7107 opj_image_comp_t * l_comp = &(image->comps[i]);
7109 if (! l_comp->sgnd) {
7110 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
7115 for (i = 0; i < image->numcomps; i++) {
7116 opj_tccp_t *tccp = &tcp->tccps[i];
7118 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
7119 tccp->numresolutions = parameters->numresolution;
7120 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
7121 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
7122 tccp->cblksty = parameters->mode;
7123 tccp->qmfbid = parameters->irreversible ? 0 : 1;
7124 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
7127 if (i == parameters->roi_compno) {
7128 tccp->roishift = parameters->roi_shift;
7133 if(parameters->cp_cinema) {
7134 //Precinct size for lowest frequency subband=128
7137 //Precinct size at all other resolutions = 256
7138 for (j = 1; j < tccp->numresolutions; j++) {
7143 if (parameters->csty & J2K_CCP_CSTY_PRT) {
7144 OPJ_INT32 p = 0, it_res;
7145 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
7146 if (p < parameters->res_spec) {
7148 if (parameters->prcw_init[p] < 1) {
7149 tccp->prcw[it_res] = 1;
7151 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
7154 if (parameters->prch_init[p] < 1) {
7155 tccp->prch[it_res] = 1;
7157 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
7161 int res_spec = parameters->res_spec;
7162 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
7163 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
7165 if (size_prcw < 1) {
7166 tccp->prcw[it_res] = 1;
7168 tccp->prcw[it_res] = int_floorlog2(size_prcw);
7171 if (size_prch < 1) {
7172 tccp->prch[it_res] = 1;
7174 tccp->prch[it_res] = int_floorlog2(size_prch);
7178 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
7181 for (j = 0; j < tccp->numresolutions; j++) {
7188 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
7192 if (parameters->mct_data) {
7193 opj_free(parameters->mct_data);
7194 parameters->mct_data = 00;
7201 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7203 assert(cstr_info != 00);
7205 /* expand the list? */
7206 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
7207 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
7208 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
7211 /* add the marker */
7212 cstr_info->marker[cstr_info->marknum].type = type;
7213 cstr_info->marker[cstr_info->marknum].pos = pos;
7214 cstr_info->marker[cstr_info->marknum].len = len;
7215 cstr_info->marknum++;
7219 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
7221 assert(cstr_index != 00);
7223 /* expand the list? */
7224 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
7225 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
7226 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
7229 /* add the marker */
7230 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
7231 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
7232 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
7233 cstr_index->marknum++;
7237 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
7239 opj_marker_info_t *marker;
7241 assert(cstr_info != 00);
7243 /* expand the list? */
7244 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
7245 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
7246 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
7249 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
7251 /* add the marker */
7252 marker->type = type;
7255 cstr_info->tile[tileno].marknum++;
7258 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)
7260 assert(cstr_index != 00);
7261 assert(cstr_index->tile_index != 00);
7263 /* expand the list? */
7264 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
7265 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
7266 cstr_index->tile_index[tileno].marker =
7267 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
7268 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
7271 /* add the marker */
7272 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
7273 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
7274 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
7275 cstr_index->tile_index[tileno].marknum++;
7277 if (type == J2K_MS_SOT) {
7278 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
7280 if (cstr_index->tile_index[tileno].tp_index)
7281 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
7288 * -----------------------------------------------------------------------
7289 * -----------------------------------------------------------------------
7290 * -----------------------------------------------------------------------
7294 * Ends the decompression procedures and possibiliy add data to be read after the
7297 opj_bool opj_j2k_end_decompress(opj_j2k_v2_t *p_j2k,
7298 opj_stream_private_t *p_stream,
7299 opj_event_mgr_t * p_manager
7309 * Reads a jpeg2000 codestream header structure.
7312 * @param p_stream the stream to read data from.
7313 * @param p_j2k the jpeg2000 codec.
7314 * @param p_manager the user event manager.
7316 * @return true if the box is valid.
7318 opj_bool opj_j2k_read_header( opj_stream_private_t *p_stream,
7319 opj_j2k_v2_t* p_j2k,
7320 opj_image_t** p_image,
7321 opj_event_mgr_t* p_manager )
7324 assert(p_j2k != 00);
7325 assert(p_stream != 00);
7326 assert(p_manager != 00);
7328 /* create an empty image header */
7329 p_j2k->m_private_image = opj_image_create0();
7330 if (! p_j2k->m_private_image) {
7334 /* customization of the validation */
7335 opj_j2k_setup_decoding_validation(p_j2k);
7337 /* validation of the parameters codec */
7338 if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
7339 opj_image_destroy(p_j2k->m_private_image);
7340 p_j2k->m_private_image = NULL;
7344 /* customization of the encoding */
7345 opj_j2k_setup_header_reading(p_j2k);
7348 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
7349 opj_image_destroy(p_j2k->m_private_image);
7350 p_j2k->m_private_image = NULL;
7354 *p_image = opj_image_create0();
7359 /* Copy codestream image information to the output image */
7360 opj_copy_image_header(p_j2k->m_private_image, *p_image);
7362 /*Allocate and initialize some elements of codestrem index*/
7363 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
7371 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
7373 void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
7376 assert(p_j2k != 00);
7378 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);
7380 /* DEVELOPER CORNER, add your custom procedures */
7381 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);
7386 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
7387 * are valid. Developpers wanting to extend the library can add their own validation procedures.
7389 void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
7392 assert(p_j2k != 00);
7394 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);
7395 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);
7396 /* DEVELOPER CORNER, add your custom validation procedure */
7402 * The mct encoding validation procedure.
7404 * @param p_j2k the jpeg2000 codec to validate.
7405 * @param p_stream the input stream to validate.
7406 * @param p_manager the user event manager.
7408 * @return true if the parameters are correct.
7410 opj_bool opj_j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
7411 opj_stream_private_t *p_stream,
7412 opj_event_mgr_t * p_manager )
7414 opj_bool l_is_valid = OPJ_TRUE;
7418 assert(p_j2k != 00);
7419 assert(p_stream != 00);
7420 assert(p_manager != 00);
7422 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
7423 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7424 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
7426 for (i=0;i<l_nb_tiles;++i) {
7427 if (l_tcp->mct == 2) {
7428 opj_tccp_t * l_tccp = l_tcp->tccps;
7429 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
7431 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
7432 l_is_valid &= ! (l_tccp->qmfbid & 1);
7443 opj_bool opj_j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
7446 OPJ_UINT32 l_indix = 1;
7447 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
7448 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7449 OPJ_UINT32 l_mct_size,l_nb_elem;
7450 OPJ_FLOAT32 * l_data, * l_current_data;
7451 opj_tccp_t * l_tccp;
7454 assert(p_tcp != 00);
7456 if (p_tcp->mct != 2) {
7460 if (p_tcp->m_mct_decoding_matrix) {
7461 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7462 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7464 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));
7465 if (! p_tcp->m_mct_records) {
7468 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7470 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7472 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7474 if (l_mct_deco_data->m_data) {
7475 opj_free(l_mct_deco_data->m_data);
7476 l_mct_deco_data->m_data = 00;
7479 l_mct_deco_data->m_index = l_indix++;
7480 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7481 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7482 l_nb_elem = p_image->numcomps * p_image->numcomps;
7483 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7484 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7486 if (! l_mct_deco_data->m_data) {
7490 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);
7492 l_mct_deco_data->m_data_size = l_mct_size;
7493 ++p_tcp->m_nb_mct_records;
7496 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7497 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7498 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));
7500 if (! p_tcp->m_mct_records) {
7504 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7505 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7507 if (l_mct_deco_data) {
7508 l_mct_deco_data = l_mct_offset_data - 1;
7512 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7514 if (l_mct_offset_data->m_data) {
7515 opj_free(l_mct_offset_data->m_data);
7516 l_mct_offset_data->m_data = 00;
7519 l_mct_offset_data->m_index = l_indix++;
7520 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7521 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7522 l_nb_elem = p_image->numcomps;
7523 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
7524 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7526 if (! l_mct_offset_data->m_data) {
7530 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
7532 opj_free(l_mct_offset_data->m_data);
7533 l_mct_offset_data->m_data = 00;
7537 l_tccp = p_tcp->tccps;
7538 l_current_data = l_data;
7540 for (i=0;i<l_nb_elem;++i) {
7541 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
7545 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
7549 l_mct_offset_data->m_data_size = l_mct_size;
7551 ++p_tcp->m_nb_mct_records;
7553 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
7554 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
7555 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7556 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7558 if (! p_tcp->m_mcc_records) {
7561 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7562 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));
7566 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7567 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
7568 l_mcc_data->m_is_irreversible = 1;
7569 l_mcc_data->m_nb_comps = p_image->numcomps;
7570 l_mcc_data->m_index = l_indix++;
7571 l_mcc_data->m_offset_array = l_mct_offset_data;
7572 ++p_tcp->m_nb_mcc_records;
7578 * Builds the cp decoder parameters to use to decode tile.
7580 opj_bool opj_j2k_build_decoder (opj_j2k_v2_t * p_j2k,
7581 opj_stream_private_t *p_stream,
7582 opj_event_mgr_t * p_manager )
7584 /* add here initialization of cp
7585 copy paste of setup_decoder */
7593 * Builds the cp encoder parameters to use to encode tile.
7595 opj_bool opj_j2k_build_encoder (opj_j2k_v2_t * p_j2k,
7596 opj_stream_private_t *p_stream,
7597 opj_event_mgr_t * p_manager )
7599 /* add here initialization of cp
7600 copy paste of setup_encoder */
7608 * The default encoding validation procedure without any extension.
7610 * @param p_j2k the jpeg2000 codec to validate.
7611 * @param p_stream the input stream to validate.
7612 * @param p_manager the user event manager.
7614 * @return true if the parameters are correct.
7616 opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
7617 opj_stream_private_t *p_stream,
7618 opj_event_mgr_t * p_manager )
7620 opj_bool l_is_valid = OPJ_TRUE;
7623 assert(p_j2k != 00);
7624 assert(p_stream != 00);
7625 assert(p_manager != 00);
7627 /* STATE checking */
7628 /* make sure the state is at 0 */
7629 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
7631 /* POINTER validation */
7632 /* make sure a p_j2k codec is present */
7633 l_is_valid &= (p_j2k->m_procedure_list != 00);
7634 /* make sure a validation list is present */
7635 l_is_valid &= (p_j2k->m_validation_list != 00);
7637 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7638 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7642 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7643 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7647 /* PARAMETER VALIDATION */
7652 * The default decoding validation procedure without any extension.
7654 * @param p_j2k the jpeg2000 codec to validate.
7655 * @param p_stream the input stream to validate.
7656 * @param p_manager the user event manager.
7658 * @return true if the parameters are correct.
7660 opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t *p_j2k,
7661 opj_stream_private_t *p_stream,
7662 opj_event_mgr_t * p_manager
7665 opj_bool l_is_valid = OPJ_TRUE;
7668 assert(p_j2k != 00);
7669 assert(p_stream != 00);
7670 assert(p_manager != 00);
7673 /* STATE checking */
7674 /* make sure the state is at 0 */
7676 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
7678 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
7680 /* POINTER validation */
7681 /* make sure a p_j2k codec is present */
7682 /* make sure a procedure list is present */
7683 l_is_valid &= (p_j2k->m_procedure_list != 00);
7684 /* make sure a validation list is present */
7685 l_is_valid &= (p_j2k->m_validation_list != 00);
7687 /* PARAMETER VALIDATION */
7691 opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
7692 opj_stream_private_t *p_stream,
7693 opj_event_mgr_t * p_manager)
7695 OPJ_UINT32 l_current_marker;
7696 OPJ_UINT32 l_marker_size;
7697 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7700 assert(p_stream != 00);
7701 assert(p_j2k != 00);
7702 assert(p_manager != 00);
7704 /* We enter in the main header */
7705 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
7707 /* Try to read the SOC marker, the codestream must begin with SOC marker */
7708 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
7709 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
7713 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7714 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7715 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7719 /* Read 2 bytes as the new marker ID */
7720 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7722 /* Try to read until the SOT is detected */
7723 while (l_current_marker != J2K_MS_SOT) {
7725 /* Check if the current marker ID is valid */
7726 if (l_current_marker < 0xff00) {
7727 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
7731 /* Get the marker handler from the marker ID */
7732 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7734 /* Manage case where marker is unknown */
7735 if (l_marker_handler->id == J2K_MS_UNK) {
7736 if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
7737 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
7741 if (l_current_marker == J2K_MS_SOT)
7742 break; /* SOT marker is detected main header is completely read */
7743 else /* Get the marker handler from the marker ID */
7744 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7747 /* Check if the marker is known and if it is the right place to find it */
7748 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7749 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7753 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7754 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7755 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7759 /* read 2 bytes as the marker size */
7760 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7761 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7763 /* Check if the marker size is compatible with the header data size */
7764 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7765 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
7766 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
7767 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
7770 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7773 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7774 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) {
7775 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7779 /* Read the marker segment with the correct marker handler */
7780 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7781 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
7785 /* Add the marker to the codestream index*/
7786 j2k_add_mhmarker_v2(p_j2k->cstr_index,
7787 l_marker_handler->id,
7788 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7789 l_marker_size + 4 );
7791 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7792 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7793 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7797 /* read 2 bytes as the new marker ID */
7798 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7801 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
7803 /* Position of the last element if the main header */
7804 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
7806 /* Next step: read a tile-part header */
7807 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7813 * Excutes the given procedures on the given codec.
7815 * @param p_procedure_list the list of procedures to execute
7816 * @param p_j2k the jpeg2000 codec to execute the procedures on.
7817 * @param p_stream the stream to execute the procedures on.
7818 * @param p_manager the user manager.
7820 * @return true if all the procedures were successfully executed.
7822 opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
7823 opj_procedure_list_t * p_procedure_list,
7824 opj_stream_private_t *p_stream,
7825 opj_event_mgr_t * p_manager )
7827 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
7828 opj_bool l_result = OPJ_TRUE;
7829 OPJ_UINT32 l_nb_proc, i;
7832 assert(p_procedure_list != 00);
7833 assert(p_j2k != 00);
7834 assert(p_stream != 00);
7835 assert(p_manager != 00);
7838 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
7839 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);
7841 for (i=0;i<l_nb_proc;++i) {
7842 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
7846 /* and clear the procedure list at the end.*/
7847 opj_procedure_list_clear(p_procedure_list);
7852 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
7853 opj_stream_private_t *p_stream,
7854 opj_event_mgr_t * p_manager
7857 opj_tcp_v2_t * l_tcp = 00;
7858 opj_tcp_v2_t * l_default_tcp = 00;
7859 OPJ_UINT32 l_nb_tiles;
7861 opj_tccp_t *l_current_tccp = 00;
7862 OPJ_UINT32 l_tccp_size;
7863 OPJ_UINT32 l_mct_size;
7864 opj_image_t * l_image;
7865 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
7866 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
7867 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
7868 OPJ_UINT32 l_offset;
7871 assert(p_j2k != 00);
7872 assert(p_stream != 00);
7873 assert(p_manager != 00);
7875 l_image = p_j2k->m_private_image;
7876 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7877 l_tcp = p_j2k->m_cp.tcps;
7878 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
7879 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7880 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
7883 for (i=0; i<l_nb_tiles; ++i) {
7884 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
7885 l_current_tccp = l_tcp->tccps;
7886 /*Copy default coding parameters into the current tile coding parameters*/
7887 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
7888 /* Initialize some values of the current tile coding parameters*/
7890 l_tcp->ppt_data = 00;
7891 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
7892 l_tcp->tccps = l_current_tccp;
7894 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
7895 if (l_default_tcp->m_mct_decoding_matrix) {
7896 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7897 if (! l_tcp->m_mct_decoding_matrix ) {
7900 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
7903 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
7904 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
7905 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
7906 if (! l_tcp->m_mct_records) {
7909 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
7911 /* Copy the mct record data from dflt_tile_cp to the current tile*/
7912 l_src_mct_rec = l_default_tcp->m_mct_records;
7913 l_dest_mct_rec = l_tcp->m_mct_records;
7915 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
7917 if (l_src_mct_rec->m_data) {
7919 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
7920 if(! l_dest_mct_rec->m_data) {
7923 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
7930 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
7931 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
7932 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
7933 if (! l_tcp->m_mcc_records) {
7936 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
7938 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
7939 l_src_mcc_rec = l_default_tcp->m_mcc_records;
7940 l_dest_mcc_rec = l_tcp->m_mcc_records;
7942 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
7944 if (l_src_mcc_rec->m_decorrelation_array) {
7945 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
7946 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
7949 if (l_src_mcc_rec->m_offset_array) {
7950 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
7951 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
7958 /* Copy all the dflt_tile_compo_cp to the current tile cp */
7959 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
7961 /* Move to next tile cp*/
7965 /* Create the current tile decoder*/
7966 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
7967 if (! p_j2k->m_tcd ) {
7971 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
7972 tcd_destroy_v2(p_j2k->m_tcd);
7974 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7982 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
7983 * with the marker value.
7984 * @param p_id Marker value to look up
7986 * @return the handler associated with the id.
7988 const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
7990 const opj_dec_memory_marker_handler_t *e;
7991 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
7992 if (e->id == p_id) {
7993 break; /* we find a handler corresponding to the marker ID*/
8001 * Destroys a jpeg2000 codec.
8003 * @param p_j2k the jpeg20000 structure to destroy.
8005 void opj_j2k_destroy (opj_j2k_v2_t *p_j2k)
8011 if (p_j2k->m_is_decoder) {
8013 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
8014 opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8015 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
8016 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
8019 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
8020 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
8021 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
8022 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
8027 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
8028 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
8029 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
8032 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
8033 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
8034 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
8035 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
8038 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
8039 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
8040 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
8041 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
8045 tcd_destroy_v2(p_j2k->m_tcd);
8047 opj_j2k_cp_destroy(&(p_j2k->m_cp));
8048 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
8050 opj_procedure_list_destroy(p_j2k->m_procedure_list);
8051 p_j2k->m_procedure_list = 00;
8053 opj_procedure_list_destroy(p_j2k->m_validation_list);
8054 p_j2k->m_procedure_list = 00;
8056 j2k_destroy_cstr_index(p_j2k->cstr_index);
8057 p_j2k->cstr_index = NULL;
8059 opj_image_destroy(p_j2k->m_private_image);
8060 p_j2k->m_private_image = NULL;
8062 opj_image_destroy(p_j2k->m_output_image);
8063 p_j2k->m_output_image = NULL;
8068 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
8072 if (p_cstr_ind->marker) {
8073 opj_free(p_cstr_ind->marker);
8074 p_cstr_ind->marker = NULL;
8077 if (p_cstr_ind->tile_index) {
8078 OPJ_UINT32 it_tile = 0;
8080 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
8082 if(p_cstr_ind->tile_index[it_tile].packet_index) {
8083 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
8084 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
8087 if(p_cstr_ind->tile_index[it_tile].tp_index){
8088 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
8089 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
8092 if(p_cstr_ind->tile_index[it_tile].marker){
8093 opj_free(p_cstr_ind->tile_index[it_tile].marker);
8094 p_cstr_ind->tile_index[it_tile].marker = NULL;
8099 opj_free( p_cstr_ind->tile_index);
8100 p_cstr_ind->tile_index = NULL;
8103 opj_free(p_cstr_ind);
8110 * Destroys a tile coding parameter structure.
8112 * @param p_tcp the tile coding parameter to destroy.
8114 void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
8120 if (p_tcp->ppt_buffer != 00) {
8121 opj_free(p_tcp->ppt_buffer);
8122 p_tcp->ppt_buffer = 00;
8125 if (p_tcp->tccps != 00) {
8126 opj_free(p_tcp->tccps);
8130 if (p_tcp->m_mct_coding_matrix != 00) {
8131 opj_free(p_tcp->m_mct_coding_matrix);
8132 p_tcp->m_mct_coding_matrix = 00;
8135 if (p_tcp->m_mct_decoding_matrix != 00) {
8136 opj_free(p_tcp->m_mct_decoding_matrix);
8137 p_tcp->m_mct_decoding_matrix = 00;
8140 if (p_tcp->m_mcc_records) {
8141 opj_free(p_tcp->m_mcc_records);
8142 p_tcp->m_mcc_records = 00;
8143 p_tcp->m_nb_max_mcc_records = 0;
8144 p_tcp->m_nb_mcc_records = 0;
8147 if (p_tcp->m_mct_records) {
8148 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
8151 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
8152 if (l_mct_data->m_data) {
8153 opj_free(l_mct_data->m_data);
8154 l_mct_data->m_data = 00;
8160 opj_free(p_tcp->m_mct_records);
8161 p_tcp->m_mct_records = 00;
8164 if (p_tcp->mct_norms != 00) {
8165 opj_free(p_tcp->mct_norms);
8166 p_tcp->mct_norms = 00;
8169 opj_j2k_tcp_data_destroy(p_tcp);
8174 * Destroys the data inside a tile coding parameter structure.
8176 * @param p_tcp the tile coding parameter which contain data to destroy.
8178 void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
8180 if (p_tcp->m_data) {
8181 opj_free(p_tcp->m_data);
8182 p_tcp->m_data = NULL;
8183 p_tcp->m_data_size = 0;
8188 * Destroys a coding parameter structure.
8190 * @param p_cp the coding parameter to destroy.
8192 void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp)
8194 OPJ_UINT32 l_nb_tiles;
8195 opj_tcp_v2_t * l_current_tile = 00;
8206 l_current_tile = p_cp->tcps;
8207 l_nb_tiles = p_cp->th * p_cp->tw;
8210 (i = 0; i < l_nb_tiles; ++i)
8212 opj_j2k_tcp_destroy(l_current_tile);
8215 opj_free(p_cp->tcps);
8219 (p_cp->ppm_buffer != 00)
8221 opj_free(p_cp->ppm_buffer);
8222 p_cp->ppm_buffer = 00;
8225 (p_cp->comment != 00)
8227 opj_free(p_cp->comment);
8231 (! p_cp->m_is_decoder)
8234 (p_cp->m_specific_param.m_enc.m_matrice)
8236 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
8237 p_cp->m_specific_param.m_enc.m_matrice = 00;
8245 * Reads a tile header.
8246 * @param p_j2k the jpeg2000 codec.
8247 * @param p_stream the stream to write data to.
8248 * @param p_manager the user event manager.
8250 opj_bool opj_j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
8251 OPJ_UINT32 * p_tile_index,
8252 OPJ_UINT32 * p_data_size,
8253 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
8254 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
8255 OPJ_UINT32 * p_nb_comps,
8257 opj_stream_private_t *p_stream,
8258 opj_event_mgr_t * p_manager )
8260 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
8261 OPJ_UINT32 l_marker_size;
8262 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
8263 opj_tcp_v2_t * l_tcp = NULL;
8264 OPJ_UINT32 l_nb_tiles;
8267 assert(p_stream != 00);
8268 assert(p_j2k != 00);
8269 assert(p_manager != 00);
8271 /* Reach the End Of Codestream ?*/
8272 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
8273 l_current_marker = J2K_MS_EOC;
8275 /* We need to encounter a SOT marker (a new tile-part header) */
8276 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
8280 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
8281 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
8283 /* Try to read until the Start Of Data is detected */
8284 while (l_current_marker != J2K_MS_SOD) {
8286 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
8287 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8288 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8292 /* Read 2 bytes from the buffer as the marker size */
8293 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
8295 /* Why this condition? FIXME */
8296 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
8297 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
8299 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
8301 /* Get the marker handler from the marker ID */
8302 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
8304 /* Check if the marker is known and if it is the right place to find it */
8305 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
8306 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
8309 /* FIXME manage case of unknown marker as in the main header ? */
8311 /* Check if the marker size is compatible with the header data size */
8312 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
8313 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
8314 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
8315 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
8318 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
8321 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
8322 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) {
8323 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8327 /* Read the marker segment with the correct marker handler */
8328 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
8329 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
8333 /* Add the marker to the codestream index*/
8334 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
8336 l_marker_handler->id,
8337 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
8338 l_marker_size + 4 );
8340 /* Keep the position of the last SOT marker read */
8341 if ( l_marker_handler->id == J2K_MS_SOT ) {
8342 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
8343 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
8345 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
8350 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
8351 /* Skip the rest of the tile part header*/
8352 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) {
8353 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8356 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
8359 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
8360 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8361 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8364 /* Read 2 bytes from the buffer as the new marker ID */
8365 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8369 /* If we didn't skip data before, we need to read the SOD marker*/
8370 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
8371 /* Try to read the SOD marker and skip data ? FIXME */
8372 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
8378 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
8379 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8380 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8381 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8385 /* Read 2 bytes from buffer as the new marker ID */
8386 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8390 /* Indicate we will try to read a new tile-part header*/
8391 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
8392 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8393 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
8395 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
8396 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
8397 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8401 /* Read 2 bytes from buffer as the new marker ID */
8402 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
8406 /* Current marker is the EOC marker ?*/
8407 if (l_current_marker == J2K_MS_EOC) {
8408 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
8409 p_j2k->m_current_tile_number = 0;
8410 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8415 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8416 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8417 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8419 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
8420 ++p_j2k->m_current_tile_number;
8424 if (p_j2k->m_current_tile_number == l_nb_tiles) {
8425 *p_go_on = OPJ_FALSE;
8431 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
8432 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8436 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8437 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8439 *p_tile_index = p_j2k->m_current_tile_number;
8440 *p_go_on = OPJ_TRUE;
8441 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
8442 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8443 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8444 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8445 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8446 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8448 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8454 opj_bool opj_j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
8455 OPJ_UINT32 p_tile_index,
8457 OPJ_UINT32 p_data_size,
8458 opj_stream_private_t *p_stream,
8459 opj_event_mgr_t * p_manager )
8461 OPJ_UINT32 l_current_marker;
8462 OPJ_BYTE l_data [2];
8463 opj_tcp_v2_t * l_tcp;
8466 assert(p_stream != 00);
8467 assert(p_j2k != 00);
8468 assert(p_manager != 00);
8470 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8471 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8475 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8476 if (! l_tcp->m_data) {
8477 opj_j2k_tcp_destroy(l_tcp);
8481 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
8485 p_j2k->cstr_index) ) {
8486 opj_j2k_tcp_destroy(l_tcp);
8487 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8491 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8495 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8496 * we destroy just the data which will be re-read in read_tile_header*/
8497 /*opj_j2k_tcp_destroy(l_tcp);
8498 p_j2k->m_tcd->tcp = 0;*/
8499 opj_j2k_tcp_data_destroy(l_tcp);
8501 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8502 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
8504 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8505 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8506 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8510 opj_read_bytes(l_data,&l_current_marker,2);
8512 if (l_current_marker == J2K_MS_EOC) {
8513 p_j2k->m_current_tile_number = 0;
8514 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8516 else if (l_current_marker != J2K_MS_SOT)
8518 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
8527 opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
8529 OPJ_UINT32 i,j,k = 0;
8530 OPJ_UINT32 l_width_src,l_height_src;
8531 OPJ_UINT32 l_width_dest,l_height_dest;
8532 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
8533 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
8534 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
8535 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
8536 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
8538 opj_image_comp_t * l_img_comp_src = 00;
8539 opj_image_comp_t * l_img_comp_dest = 00;
8541 opj_tcd_tilecomp_v2_t * l_tilec = 00;
8542 opj_image_t * l_image_src = 00;
8543 OPJ_UINT32 l_size_comp, l_remaining;
8544 OPJ_INT32 * l_dest_ptr;
8545 opj_tcd_resolution_v2_t* l_res= 00;
8547 l_tilec = p_tcd->tcd_image->tiles->comps;
8548 l_image_src = p_tcd->image;
8549 l_img_comp_src = l_image_src->comps;
8551 l_img_comp_dest = p_output_image->comps;
8553 for (i=0; i<l_image_src->numcomps; i++) {
8555 /* Allocate output component buffer if necessary */
8556 if (!l_img_comp_dest->data) {
8558 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
8559 if (! l_img_comp_dest->data) {
8564 /* Copy info from decoded comp image to output image */
8565 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
8568 /* Compute the precision of the output buffer */
8569 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
8570 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
8571 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
8577 if (l_size_comp == 3) {
8582 /* Current tile component size*/
8584 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
8585 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
8588 l_width_src = (l_res->x1 - l_res->x0);
8589 l_height_src = (l_res->y1 - l_res->y0);
8591 /* Border of the current output component*/
8592 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
8593 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
8594 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
8595 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
8598 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
8599 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
8603 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
8604 * of the input buffer (decoded tile component) which will be move
8605 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
8606 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
8607 * by this input area.
8609 assert( l_res->x0 >= 0);
8610 assert( l_res->x1 >= 0);
8611 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
8612 l_start_x_dest = l_res->x0 - l_x0_dest;
8613 l_offset_x0_src = 0;
8615 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8616 l_width_dest = l_width_src;
8617 l_offset_x1_src = 0;
8620 l_width_dest = l_x1_dest - l_res->x0 ;
8621 l_offset_x1_src = l_width_src - l_width_dest;
8625 l_start_x_dest = 0 ;
8626 l_offset_x0_src = l_x0_dest - l_res->x0;
8628 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8629 l_width_dest = l_width_src - l_offset_x0_src;
8630 l_offset_x1_src = 0;
8633 l_width_dest = l_img_comp_dest->w ;
8634 l_offset_x1_src = l_res->x1 - l_x1_dest;
8638 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
8639 l_start_y_dest = l_res->y0 - l_y0_dest;
8640 l_offset_y0_src = 0;
8642 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8643 l_height_dest = l_height_src;
8644 l_offset_y1_src = 0;
8647 l_height_dest = l_y1_dest - l_res->y0 ;
8648 l_offset_y1_src = l_height_src - l_height_dest;
8652 l_start_y_dest = 0 ;
8653 l_offset_y0_src = l_y0_dest - l_res->y0;
8655 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8656 l_height_dest = l_height_src - l_offset_y0_src;
8657 l_offset_y1_src = 0;
8660 l_height_dest = l_img_comp_dest->h ;
8661 l_offset_y1_src = l_res->y1 - l_y1_dest;
8665 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
8670 /* Compute the input buffer offset */
8671 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
8672 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
8673 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
8675 /* Compute the output buffer offset */
8676 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
8677 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
8679 /* Move the output buffer to the first place where we will write*/
8680 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
8683 fprintf(stdout, "COMPO[%d]:\n",i);
8684 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
8685 "\t tile offset:%d, %d, %d, %d\n"
8686 "\t buffer offset: %d; %d, %d\n",
8687 l_res->x0, l_res->y0, l_width_src, l_height_src,
8688 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
8689 l_start_offset_src, l_line_offset_src, l_end_offset_src);
8691 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
8692 "\t start offset: %d, line offset= %d\n",
8693 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
8697 switch (l_size_comp) {
8700 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
8701 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
8703 if (l_img_comp_src->sgnd) {
8704 for (j = 0 ; j < l_height_dest ; ++j) {
8705 for ( k = 0 ; k < l_width_dest ; ++k) {
8706 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
8709 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
8710 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
8714 for ( j = 0 ; j < l_height_dest ; ++j ) {
8715 for ( k = 0 ; k < l_width_dest ; ++k) {
8716 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
8719 l_dest_ptr+= l_line_offset_dest;
8720 l_src_ptr += l_line_offset_src;
8724 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
8725 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
8730 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
8731 l_src_ptr += l_start_offset_src;
8733 if (l_img_comp_src->sgnd) {
8734 for (j=0;j<l_height_dest;++j) {
8735 for (k=0;k<l_width_dest;++k) {
8736 *(l_dest_ptr++) = *(l_src_ptr++);
8739 l_dest_ptr+= l_line_offset_dest;
8740 l_src_ptr += l_line_offset_src ;
8744 for (j=0;j<l_height_dest;++j) {
8745 for (k=0;k<l_width_dest;++k) {
8746 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
8749 l_dest_ptr+= l_line_offset_dest;
8750 l_src_ptr += l_line_offset_src ;
8754 l_src_ptr += l_end_offset_src;
8755 p_data = (OPJ_BYTE*) l_src_ptr;
8760 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
8761 l_src_ptr += l_start_offset_src;
8763 for (j=0;j<l_height_dest;++j) {
8764 for (k=0;k<l_width_dest;++k) {
8765 *(l_dest_ptr++) = (*(l_src_ptr++));
8768 l_dest_ptr+= l_line_offset_dest;
8769 l_src_ptr += l_line_offset_src ;
8772 l_src_ptr += l_end_offset_src;
8773 p_data = (OPJ_BYTE*) l_src_ptr;
8787 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
8789 * @param p_j2k the jpeg2000 codec.
8790 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
8791 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
8792 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
8793 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
8794 * @param p_manager the user event manager
8796 * @return true if the area could be set.
8798 opj_bool opj_j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
8799 opj_image_t* p_image,
8800 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
8801 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
8802 opj_event_mgr_t * p_manager )
8804 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
8805 opj_image_t * l_image = p_j2k->m_private_image;
8808 OPJ_INT32 l_comp_x1, l_comp_y1;
8809 opj_image_comp_t* l_img_comp = NULL;
8811 /* Check if we are read the main header */
8812 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
8813 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
8817 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
8818 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
8820 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8821 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8822 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8823 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8829 /* Check if the positions provided by the user are correct */
8832 assert(p_start_x >= 0 );
8833 assert(p_start_y >= 0 );
8835 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
8836 opj_event_msg_v2(p_manager, EVT_ERROR,
8837 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
8838 p_start_x, l_image->x1);
8841 else if ((OPJ_UINT32)p_start_x < l_image->x0){
8842 opj_event_msg_v2(p_manager, EVT_WARNING,
8843 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
8844 p_start_x, l_image->x0);
8845 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8846 p_image->x0 = l_image->x0;
8849 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
8850 p_image->x0 = p_start_x;
8854 if ((OPJ_UINT32)p_start_y > l_image->y1){
8855 opj_event_msg_v2(p_manager, EVT_ERROR,
8856 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
8857 p_start_y, l_image->y1);
8860 else if ((OPJ_UINT32)p_start_y < l_image->y0){
8861 opj_event_msg_v2(p_manager, EVT_WARNING,
8862 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
8863 p_start_y, l_image->y0);
8864 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8865 p_image->y0 = l_image->y0;
8868 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
8869 p_image->y0 = p_start_y;
8873 assert((OPJ_UINT32)p_end_x > 0);
8874 assert((OPJ_UINT32)p_end_y > 0);
8875 if ((OPJ_UINT32)p_end_x < l_image->x0) {
8876 opj_event_msg_v2(p_manager, EVT_ERROR,
8877 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
8878 p_end_x, l_image->x0);
8881 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
8882 opj_event_msg_v2(p_manager, EVT_WARNING,
8883 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
8884 p_end_x, l_image->x1);
8885 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8886 p_image->x1 = l_image->x1;
8889 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
8890 p_image->x1 = p_end_x;
8894 if ((OPJ_UINT32)p_end_y < l_image->y0) {
8895 opj_event_msg_v2(p_manager, EVT_ERROR,
8896 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
8897 p_end_y, l_image->y0);
8900 if ((OPJ_UINT32)p_end_y > l_image->y1){
8901 opj_event_msg_v2(p_manager, EVT_WARNING,
8902 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
8903 p_end_y, l_image->y1);
8904 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8905 p_image->y1 = l_image->y1;
8908 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
8909 p_image->y1 = p_end_y;
8913 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
8915 l_img_comp = p_image->comps;
8916 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
8920 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
8921 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
8922 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
8923 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
8925 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
8926 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
8928 opj_event_msg_v2(p_manager, EVT_ERROR,
8929 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
8933 l_img_comp->w = l_w;
8935 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
8936 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
8938 opj_event_msg_v2(p_manager, EVT_ERROR,
8939 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
8943 l_img_comp->h = l_h;
8948 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
8949 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
8956 /* ----------------------------------------------------------------------- */
8957 /* J2K / JPT decoder interface */
8958 /* ----------------------------------------------------------------------- */
8960 * Creates a J2K decompression structure.
8962 * @return a handle to a J2K decompressor if successful, NULL otherwise.
8964 opj_j2k_v2_t* opj_j2k_create_decompress(void)
8966 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
8970 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
8972 l_j2k->m_is_decoder = 1;
8973 l_j2k->m_cp.m_is_decoder = 1;
8975 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
8976 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
8977 opj_j2k_destroy(l_j2k);
8980 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
8982 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
8983 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
8984 opj_j2k_destroy(l_j2k);
8988 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
8990 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
8992 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
8994 /* codestream index creation */
8995 l_j2k->cstr_index = opj_j2k_create_cstr_index();
8997 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
8998 if (!l_j2k->cstr_index){
8999 opj_j2k_destroy(l_j2k);
9003 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
9006 /* validation list creation */
9007 l_j2k->m_validation_list = opj_procedure_list_create();
9008 if (! l_j2k->m_validation_list) {
9009 opj_j2k_destroy(l_j2k);
9013 /* execution list creation */
9014 l_j2k->m_procedure_list = opj_procedure_list_create();
9015 if (! l_j2k->m_procedure_list) {
9016 opj_j2k_destroy(l_j2k);
9024 opj_codestream_index_t* opj_j2k_create_cstr_index(void)
9026 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
9027 opj_calloc(1,sizeof(opj_codestream_index_t));
9031 cstr_index->maxmarknum = 100;
9032 cstr_index->marknum = 0;
9033 cstr_index->marker = (opj_marker_info_t*)
9034 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
9035 if (!cstr_index-> marker)
9038 cstr_index->tile_index = NULL;
9045 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
9047 * @param p_tile_no the tile index.
9048 * @param p_comp_no the component being outputted.
9049 * @param p_j2k the J2K codec.
9051 * @return the number of bytes taken by the SPCod element.
9053 OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
9054 OPJ_UINT32 p_tile_no,
9055 OPJ_UINT32 p_comp_no )
9057 opj_cp_v2_t *l_cp = 00;
9058 opj_tcp_v2_t *l_tcp = 00;
9059 opj_tccp_t *l_tccp = 00;
9062 assert(p_j2k != 00);
9064 l_cp = &(p_j2k->m_cp);
9065 l_tcp = &l_cp->tcps[p_tile_no];
9066 l_tccp = &l_tcp->tccps[p_comp_no];
9068 /* preconditions again */
9069 assert(p_tile_no < (l_cp->tw * l_cp->th));
9070 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9072 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9073 return 5 + l_tccp->numresolutions;
9081 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9083 * @param p_comp_no the component number to output.
9084 * @param p_stream the stream to write data to.
9085 * @param p_j2k J2K codec.
9086 * @param p_manager the user event manager.
9089 opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9090 OPJ_UINT32 p_tile_no,
9091 OPJ_UINT32 p_comp_no,
9093 OPJ_UINT32 * p_header_size,
9094 struct opj_event_mgr * p_manager )
9097 opj_cp_v2_t *l_cp = 00;
9098 opj_tcp_v2_t *l_tcp = 00;
9099 opj_tccp_t *l_tccp = 00;
9102 assert(p_j2k != 00);
9103 assert(p_header_size != 00);
9104 assert(p_manager != 00);
9105 assert(p_data != 00);
9107 l_cp = &(p_j2k->m_cp);
9108 l_tcp = &l_cp->tcps[p_tile_no];
9109 l_tccp = &l_tcp->tccps[p_comp_no];
9111 /* preconditions again */
9112 assert(p_tile_no < (l_cp->tw * l_cp->th));
9113 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
9115 if (*p_header_size < 5) {
9116 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
9120 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
9123 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
9126 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
9129 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
9132 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
9135 *p_header_size = *p_header_size - 5;
9137 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9139 if (*p_header_size < l_tccp->numresolutions) {
9140 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
9144 for (i = 0; i < l_tccp->numresolutions; ++i) {
9145 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
9149 *p_header_size = *p_header_size - l_tccp->numresolutions;
9156 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
9157 * @param p_header_data the data contained in the COM box.
9158 * @param p_j2k the jpeg2000 codec.
9159 * @param p_header_size the size of the data contained in the COM marker.
9160 * @param p_manager the user event manager.
9162 opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
9164 OPJ_BYTE * p_header_data,
9165 OPJ_UINT32 * p_header_size,
9166 opj_event_mgr_t * p_manager)
9168 OPJ_UINT32 i, l_tmp;
9169 opj_cp_v2_t *l_cp = NULL;
9170 opj_tcp_v2_t *l_tcp = NULL;
9171 opj_tccp_t *l_tccp = NULL;
9172 OPJ_BYTE * l_current_ptr = NULL;
9175 assert(p_j2k != 00);
9176 assert(p_manager != 00);
9177 assert(p_header_data != 00);
9179 l_cp = &(p_j2k->m_cp);
9180 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
9181 &l_cp->tcps[p_j2k->m_current_tile_number] :
9182 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9184 /* precondition again */
9185 assert(compno < p_j2k->m_private_image->numcomps);
9187 l_tccp = &l_tcp->tccps[compno];
9188 l_current_ptr = p_header_data;
9190 /* make sure room is sufficient */
9191 if (*p_header_size < 5) {
9192 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9196 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
9197 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
9200 /* If user wants to remove more resolutions than the codestream contains, return error */
9201 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
9202 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
9203 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
9204 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
9208 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
9212 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
9216 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
9219 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
9222 *p_header_size = *p_header_size - 5;
9224 /* use custom precinct size ? */
9225 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
9226 if (*p_header_size < l_tccp->numresolutions) {
9227 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
9231 for (i = 0; i < l_tccp->numresolutions; ++i) {
9232 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
9234 l_tccp->prcw[i] = l_tmp & 0xf;
9235 l_tccp->prch[i] = l_tmp >> 4;
9238 *p_header_size = *p_header_size - l_tccp->numresolutions;
9241 /* set default size for the precinct width and height */
9242 for (i = 0; i < l_tccp->numresolutions; ++i) {
9243 l_tccp->prcw[i] = 15;
9244 l_tccp->prch[i] = 15;
9248 #ifdef WIP_REMOVE_MSD
9250 if (p_j2k->cstr_info && compno == 0) {
9251 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
9253 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
9254 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
9255 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
9256 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
9257 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
9260 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
9261 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
9270 * Copies the tile component parameters of all the component from the first tile component.
9272 * @param p_j2k the J2k codec.
9274 void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
9278 opj_cp_v2_t *l_cp = NULL;
9279 opj_tcp_v2_t *l_tcp = NULL;
9280 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
9281 OPJ_UINT32 l_prc_size;
9284 assert(p_j2k != 00);
9286 l_cp = &(p_j2k->m_cp);
9287 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
9288 &l_cp->tcps[p_j2k->m_current_tile_number] :
9289 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9291 l_ref_tccp = &l_tcp->tccps[0];
9292 l_copied_tccp = l_ref_tccp + 1;
9293 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
9295 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
9296 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
9297 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
9298 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
9299 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
9300 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
9301 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
9302 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
9308 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
9310 * @param p_tile_no the tile index.
9311 * @param p_comp_no the component being outputted.
9312 * @param p_j2k the J2K codec.
9314 * @return the number of bytes taken by the SPCod element.
9316 OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
9317 OPJ_UINT32 p_tile_no,
9318 OPJ_UINT32 p_comp_no )
9320 OPJ_UINT32 l_num_bands;
9322 opj_cp_v2_t *l_cp = 00;
9323 opj_tcp_v2_t *l_tcp = 00;
9324 opj_tccp_t *l_tccp = 00;
9327 assert(p_j2k != 00);
9329 l_cp = &(p_j2k->m_cp);
9330 l_tcp = &l_cp->tcps[p_tile_no];
9331 l_tccp = &l_tcp->tccps[p_comp_no];
9333 /* preconditions again */
9334 assert(p_tile_no < l_cp->tw * l_cp->th);
9335 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9337 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9339 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9340 return 1 + l_num_bands;
9343 return 1 + 2*l_num_bands;
9348 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
9350 * @param p_tile_no the tile to output.
9351 * @param p_comp_no the component number to output.
9352 * @param p_data the data buffer.
9353 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
9354 * @param p_j2k J2K codec.
9355 * @param p_manager the user event manager.
9358 opj_bool opj_j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
9359 OPJ_UINT32 p_tile_no,
9360 OPJ_UINT32 p_comp_no,
9362 OPJ_UINT32 * p_header_size,
9363 struct opj_event_mgr * p_manager )
9365 OPJ_UINT32 l_header_size;
9366 OPJ_UINT32 l_band_no, l_num_bands;
9367 OPJ_UINT32 l_expn,l_mant;
9369 opj_cp_v2_t *l_cp = 00;
9370 opj_tcp_v2_t *l_tcp = 00;
9371 opj_tccp_t *l_tccp = 00;
9374 assert(p_j2k != 00);
9375 assert(p_header_size != 00);
9376 assert(p_manager != 00);
9377 assert(p_data != 00);
9379 l_cp = &(p_j2k->m_cp);
9380 l_tcp = &l_cp->tcps[p_tile_no];
9381 l_tccp = &l_tcp->tccps[p_comp_no];
9383 /* preconditions again */
9384 assert(p_tile_no < l_cp->tw * l_cp->th);
9385 assert(p_comp_no <p_j2k->m_private_image->numcomps);
9387 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
9389 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9390 l_header_size = 1 + l_num_bands;
9392 if (*p_header_size < l_header_size) {
9393 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9397 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9400 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9401 l_expn = l_tccp->stepsizes[l_band_no].expn;
9402 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
9407 l_header_size = 1 + 2*l_num_bands;
9409 if (*p_header_size < l_header_size) {
9410 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9414 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9417 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9418 l_expn = l_tccp->stepsizes[l_band_no].expn;
9419 l_mant = l_tccp->stepsizes[l_band_no].mant;
9421 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
9426 *p_header_size = *p_header_size - l_header_size;
9432 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
9434 * @param p_comp_no the component being targeted.
9435 * @param p_header_data the data contained in the COM box.
9436 * @param p_j2k the jpeg2000 codec.
9437 * @param p_header_size the size of the data contained in the COM marker.
9438 * @param p_manager the user event manager.
9440 opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
9441 OPJ_UINT32 p_comp_no,
9442 OPJ_BYTE* p_header_data,
9443 OPJ_UINT32 * p_header_size,
9444 opj_event_mgr_t * p_manager
9448 OPJ_UINT32 l_band_no;
9449 opj_cp_v2_t *l_cp = 00;
9450 opj_tcp_v2_t *l_tcp = 00;
9451 opj_tccp_t *l_tccp = 00;
9452 OPJ_BYTE * l_current_ptr = 00;
9453 OPJ_UINT32 l_tmp, l_num_band;
9456 assert(p_j2k != 00);
9457 assert(p_manager != 00);
9458 assert(p_header_data != 00);
9460 l_cp = &(p_j2k->m_cp);
9461 /* come from tile part header or main header ?*/
9462 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9463 &l_cp->tcps[p_j2k->m_current_tile_number] :
9464 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9466 /* precondition again*/
9467 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9469 l_tccp = &l_tcp->tccps[p_comp_no];
9470 l_current_ptr = p_header_data;
9472 if (*p_header_size < 1) {
9473 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9476 *p_header_size -= 1;
9478 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9481 l_tccp->qntsty = l_tmp & 0x1f;
9482 l_tccp->numgbits = l_tmp >> 5;
9483 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9487 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9489 (*p_header_size) / 2;
9491 if( l_num_band > J2K_MAXBANDS ) {
9492 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9493 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9494 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
9495 /*return OPJ_FALSE;*/
9500 if (l_cp->correct) {
9502 /* if JPWL is on, we check whether there are too many subbands */
9503 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
9504 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9505 "JPWL: bad number of subbands in Sqcx (%d)\n",
9508 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
9511 /* we try to correct */
9513 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
9514 "- setting number of bands to %d => HYPOTHESIS!!!\n",
9519 #endif /* USE_JPWL */
9521 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9522 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9523 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
9525 if (l_band_no < J2K_MAXBANDS){
9526 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
9527 l_tccp->stepsizes[l_band_no].mant = 0;
9530 *p_header_size = *p_header_size - l_num_band;
9533 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9534 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
9536 if (l_band_no < J2K_MAXBANDS){
9537 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
9538 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
9541 *p_header_size = *p_header_size - 2*l_num_band;
9544 /* Add Antonin : if scalar_derived -> compute other stepsizes */
9545 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9546 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
9547 l_tccp->stepsizes[l_band_no].expn =
9548 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
9549 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
9550 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
9558 * Copies the tile component parameters of all the component from the first tile component.
9560 * @param p_j2k the J2k codec.
9562 void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
9565 opj_cp_v2_t *l_cp = NULL;
9566 opj_tcp_v2_t *l_tcp = NULL;
9567 opj_tccp_t *l_ref_tccp = NULL;
9568 opj_tccp_t *l_copied_tccp = NULL;
9572 assert(p_j2k != 00);
9574 l_cp = &(p_j2k->m_cp);
9575 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
9576 &l_cp->tcps[p_j2k->m_current_tile_number] :
9577 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9579 l_ref_tccp = &l_tcp->tccps[0];
9580 l_copied_tccp = l_ref_tccp + 1;
9581 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
9583 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
9584 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
9585 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
9586 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
9592 * Dump some elements from the J2K decompression structure .
9594 *@param p_j2k the jpeg2000 codec.
9595 *@param flag flag to describe what elments are dump.
9596 *@param out_stream output stream where dump the elements.
9599 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
9601 /* Check if the flag is compatible with j2k file*/
9602 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
9603 fprintf(out_stream, "Wrong flag\n");
9607 /* Dump the image_header */
9608 if (flag & OPJ_IMG_INFO){
9609 if (p_j2k->m_private_image)
9610 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
9613 /* Dump the codestream info from main header */
9614 if (flag & OPJ_J2K_MH_INFO){
9615 opj_j2k_dump_MH_info(p_j2k, out_stream);
9619 /* Dump the codestream info of the current tile */
9620 if (flag & OPJ_J2K_TH_INFO){
9624 /* Dump the codestream index from main header */
9625 if (flag & OPJ_J2K_MH_IND){
9626 opj_j2k_dump_MH_index(p_j2k, out_stream);
9629 /* Dump the codestream index of the current tile */
9630 if (flag & OPJ_J2K_TH_IND){
9637 * Dump index elements of the codestream extract from the main header.
9639 *@param p_j2k the jpeg2000 codec.
9640 *@param out_stream output stream where dump the elements.
9643 void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9645 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
9646 OPJ_UINT32 it_marker, it_tile, it_tile_part;
9648 fprintf(out_stream, "Codestream index from main header: {\n");
9650 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
9651 "\t Main header end position=%" PRIi64 "\n",
9652 cstr_index->main_head_start, cstr_index->main_head_end);
9654 fprintf(out_stream, "\t Marker list: {\n");
9656 if (cstr_index->marker){
9657 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
9658 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9659 cstr_index->marker[it_marker].type,
9660 cstr_index->marker[it_marker].pos,
9661 cstr_index->marker[it_marker].len );
9665 fprintf(out_stream, "\t }\n");
9668 if (cstr_index->tile_index){
9670 /* Simple test to avoid to write empty information*/
9671 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
9672 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9673 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
9676 if (l_acc_nb_of_tile_part)
9678 fprintf(out_stream, "\t Tile index: {\n");
9680 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9681 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
9683 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
9685 if (cstr_index->tile_index[it_tile].tp_index){
9686 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
9687 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
9689 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
9690 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
9691 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
9695 if (cstr_index->tile_index[it_tile].marker){
9696 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
9697 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9698 cstr_index->tile_index[it_tile].marker[it_marker].type,
9699 cstr_index->tile_index[it_tile].marker[it_marker].pos,
9700 cstr_index->tile_index[it_tile].marker[it_marker].len );
9704 fprintf(out_stream,"\t }\n");
9708 fprintf(out_stream,"}\n");
9713 * Dump info elements of the codestream extract from the main header.
9715 *@param p_j2k the jpeg2000 codec.
9716 *@param out_stream output stream where dump the elements.
9719 void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9721 opj_tcp_v2_t * l_default_tile=NULL;
9723 fprintf(out_stream, "Codestream info from main header: {\n");
9725 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
9726 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
9727 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
9729 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9733 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
9735 fprintf(out_stream, "\t default tile {\n");
9736 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
9737 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
9738 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
9739 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
9741 for (compno = 0; compno < numcomps; compno++) {
9742 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9744 OPJ_INT32 bandno, numbands;
9747 fprintf(out_stream, "\t\t comp %d {\n", compno);
9748 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
9749 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
9750 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
9751 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
9752 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
9753 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
9755 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
9756 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
9757 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
9759 fprintf(out_stream, "\n");
9761 /* quantization style*/
9762 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
9763 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
9764 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
9765 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9766 for (bandno = 0; bandno < numbands; bandno++) {
9767 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
9768 l_tccp->stepsizes[bandno].expn);
9770 fprintf(out_stream, "\n");
9773 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
9775 fprintf(out_stream, "\t\t }\n");
9776 } /*end of component of default tile*/
9777 fprintf(out_stream, "\t }\n"); /*end of default tile*/
9781 fprintf(out_stream, "}\n");
9786 * Dump an image header structure.
9788 *@param img_header the image header to dump.
9789 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9790 *@param out_stream output stream where dump the elements.
9792 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
9797 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
9801 fprintf(out_stream, "Image info {\n");
9802 tab[0] = '\t';tab[1] = '\0';
9805 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
9806 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
9807 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
9809 if (img_header->comps){
9811 for (compno = 0; compno < img_header->numcomps; compno++) {
9812 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
9813 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
9814 fprintf(out_stream,"%s}\n",tab);
9818 fprintf(out_stream, "}\n");
9822 * Dump a component image header structure.
9824 *@param comp_header the component image header to dump.
9825 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9826 *@param out_stream output stream where dump the elements.
9828 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
9833 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
9836 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
9839 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
9840 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
9841 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
9844 fprintf(out_stream, "}\n");
9849 * Get the codestream info from a JPEG2000 codec.
9851 *@param p_j2k the component image header to dump.
9853 *@return the codestream information extract from the jpg2000 codec
9855 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
9858 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
9859 opj_tcp_v2_t *l_default_tile;
9860 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
9862 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
9864 cstr_info->tx0 = p_j2k->m_cp.tx0;
9865 cstr_info->ty0 = p_j2k->m_cp.ty0;
9866 cstr_info->tdx = p_j2k->m_cp.tdx;
9867 cstr_info->tdy = p_j2k->m_cp.tdy;
9868 cstr_info->tw = p_j2k->m_cp.tw;
9869 cstr_info->th = p_j2k->m_cp.th;
9871 cstr_info->tile_info = NULL; /* Not fill from the main header*/
9873 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9875 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
9876 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
9877 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
9878 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
9880 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
9882 for (compno = 0; compno < numcomps; compno++) {
9883 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9884 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
9885 OPJ_INT32 bandno, numbands;
9888 l_tccp_info->csty = l_tccp->csty;
9889 l_tccp_info->numresolutions = l_tccp->numresolutions;
9890 l_tccp_info->cblkw = l_tccp->cblkw;
9891 l_tccp_info->cblkh = l_tccp->cblkh;
9892 l_tccp_info->cblksty = l_tccp->cblksty;
9893 l_tccp_info->qmfbid = l_tccp->qmfbid;
9894 if (l_tccp->numresolutions < J2K_MAXRLVLS)
9896 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
9897 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
9900 /* quantization style*/
9901 l_tccp_info->qntsty = l_tccp->qntsty;
9902 l_tccp_info->numgbits = l_tccp->numgbits;
9904 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9905 if (numbands < J2K_MAXBANDS) {
9906 for (bandno = 0; bandno < numbands; bandno++) {
9907 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
9908 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
9913 l_tccp_info->roishift = l_tccp->roishift;
9921 * Get the codestream index from a JPEG2000 codec.
9923 *@param p_j2k the component image header to dump.
9925 *@return the codestream index extract from the jpg2000 codec
9927 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
9929 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
9930 opj_calloc(1,sizeof(opj_codestream_index_t));
9934 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
9935 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
9936 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
9938 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
9939 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
9940 if (!l_cstr_index->marker){
9941 opj_free( l_cstr_index);
9945 if (p_j2k->cstr_index->marker)
9946 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
9948 opj_free(l_cstr_index->marker);
9949 l_cstr_index->marker = NULL;
9952 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
9953 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
9954 if (!l_cstr_index->tile_index){
9955 opj_free( l_cstr_index->marker);
9956 opj_free( l_cstr_index);
9960 if (!p_j2k->cstr_index->tile_index){
9961 opj_free(l_cstr_index->tile_index);
9962 l_cstr_index->tile_index = NULL;
9965 OPJ_UINT32 it_tile = 0;
9966 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
9969 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
9971 l_cstr_index->tile_index[it_tile].marker =
9972 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
9974 if (!l_cstr_index->tile_index[it_tile].marker) {
9975 OPJ_UINT32 it_tile_free;
9977 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9978 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9981 opj_free( l_cstr_index->tile_index);
9982 opj_free( l_cstr_index->marker);
9983 opj_free( l_cstr_index);
9987 if (p_j2k->cstr_index->tile_index[it_tile].marker)
9988 memcpy( l_cstr_index->tile_index[it_tile].marker,
9989 p_j2k->cstr_index->tile_index[it_tile].marker,
9990 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
9992 opj_free(l_cstr_index->tile_index[it_tile].marker);
9993 l_cstr_index->tile_index[it_tile].marker = NULL;
9996 /* Tile part index*/
9997 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
9999 l_cstr_index->tile_index[it_tile].tp_index =
10000 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
10002 if(!l_cstr_index->tile_index[it_tile].tp_index){
10003 OPJ_UINT32 it_tile_free;
10005 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
10006 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
10007 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
10010 opj_free( l_cstr_index->tile_index);
10011 opj_free( l_cstr_index->marker);
10012 opj_free( l_cstr_index);
10016 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
10017 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
10018 p_j2k->cstr_index->tile_index[it_tile].tp_index,
10019 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
10022 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
10023 l_cstr_index->tile_index[it_tile].tp_index = NULL;
10026 /* Packet index (NOT USED)*/
10027 l_cstr_index->tile_index[it_tile].nb_packet = 0;
10028 l_cstr_index->tile_index[it_tile].packet_index = NULL;
10033 return l_cstr_index;
10036 opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
10038 OPJ_UINT32 it_tile=0;
10040 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
10041 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
10042 if (!p_j2k->cstr_index->tile_index)
10045 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
10046 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
10047 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
10048 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
10049 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
10050 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
10060 opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
10061 opj_stream_private_t *p_stream,
10062 opj_event_mgr_t * p_manager)
10064 opj_bool l_go_on = OPJ_TRUE;
10065 OPJ_UINT32 l_current_tile_no;
10066 OPJ_UINT32 l_data_size,l_max_data_size;
10067 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10068 OPJ_UINT32 l_nb_comps;
10069 OPJ_BYTE * l_current_data;
10071 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10072 if (! l_current_data) {
10075 l_max_data_size = 1000;
10080 if (! opj_j2k_read_tile_header( p_j2k,
10081 &l_current_tile_no,
10083 &l_tile_x0, &l_tile_y0,
10084 &l_tile_x1, &l_tile_y1,
10089 opj_free(l_current_data);
10097 if (l_data_size > l_max_data_size) {
10098 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10099 if (! l_current_data) {
10100 opj_free(l_current_data);
10104 l_max_data_size = l_data_size;
10107 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10108 opj_free(l_current_data);
10111 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);
10113 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10114 opj_free(l_current_data);
10117 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
10121 opj_free(l_current_data);
10127 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
10129 static void opj_j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
10132 assert(p_j2k != 00);
10134 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);
10135 /* DEVELOPER CORNER, add your custom procedures */
10140 * Read and decode one tile.
10142 static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
10143 opj_stream_private_t *p_stream,
10144 opj_event_mgr_t * p_manager)
10146 opj_bool l_go_on = OPJ_TRUE;
10147 OPJ_UINT32 l_current_tile_no;
10148 OPJ_UINT32 l_tile_no_to_dec;
10149 OPJ_UINT32 l_data_size,l_max_data_size;
10150 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
10151 OPJ_UINT32 l_nb_comps;
10152 OPJ_BYTE * l_current_data;
10154 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10155 if (! l_current_data) {
10158 l_max_data_size = 1000;
10160 /*Allocate and initialize some elements of codestrem index if not already done*/
10161 if( !p_j2k->cstr_index->tile_index)
10163 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
10164 opj_free(l_current_data);
10168 /* Move into the codestream to the first SOT used to decode the desired tile */
10169 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
10170 if (p_j2k->cstr_index->tile_index)
10171 if(p_j2k->cstr_index->tile_index->tp_index)
10173 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
10174 /* the index for this tile has not been built,
10175 * so move to the last SOT read */
10176 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
10177 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10182 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)) ) {
10183 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10187 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
10188 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
10189 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
10193 if (! opj_j2k_read_tile_header( p_j2k,
10194 &l_current_tile_no,
10196 &l_tile_x0, &l_tile_y0,
10197 &l_tile_x1, &l_tile_y1,
10202 opj_free(l_current_data);
10211 if (l_data_size > l_max_data_size) {
10212 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
10213 if (! l_current_data) {
10214 opj_free(l_current_data);
10218 l_max_data_size = l_data_size;
10223 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
10224 opj_free(l_current_data);
10227 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);
10229 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
10230 opj_free(l_current_data);
10233 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
10235 if(l_current_tile_no == l_tile_no_to_dec)
10237 /* move into the codestream to the the first SOT (FIXME or not move?)*/
10238 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
10239 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
10245 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);
10250 opj_free(l_current_data);
10257 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
10259 static void opj_j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
10262 assert(p_j2k != 00);
10264 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);
10265 /* DEVELOPER CORNER, add your custom procedures */
10271 * Decodes the tiles of the stream.
10273 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
10274 opj_stream_private_t * p_stream,
10275 opj_image_t * p_image,
10276 opj_event_mgr_t * p_manager)
10283 p_j2k->m_output_image = opj_image_create0();
10284 if (! (p_j2k->m_output_image)) {
10287 opj_copy_image_header(p_image, p_j2k->m_output_image);
10289 /* customization of the decoding */
10290 opj_j2k_setup_decoding(p_j2k);
10292 /* Decode the codestream */
10293 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10294 opj_image_destroy(p_j2k->m_private_image);
10295 p_j2k->m_private_image = NULL;
10299 /* Move data and copy one information from codec to output image*/
10300 for (compno = 0; compno < p_image->numcomps; compno++) {
10301 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10302 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10303 p_j2k->m_output_image->comps[compno].data = NULL;
10311 * Get the decoded tile.
10313 * @param p_j2k the jpeg2000 codestream codec.
10314 * @param p_stream input_stream
10315 * @param p_image output image. .
10316 * @param p_manager the user event manager
10317 * @param tile_index index of the tile we want decode
10319 * @return true if succeed.
10321 opj_bool opj_j2k_get_tile( opj_j2k_v2_t *p_j2k,
10322 opj_stream_private_t *p_stream,
10323 opj_image_t* p_image,
10324 opj_event_mgr_t * p_manager,
10325 OPJ_UINT32 tile_index )
10328 OPJ_UINT32 l_tile_x, l_tile_y;
10329 opj_image_comp_t* l_img_comp;
10332 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
10336 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
10337 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);
10341 /* Compute the dimension of the desired tile*/
10342 l_tile_x = tile_index % p_j2k->m_cp.tw;
10343 l_tile_y = tile_index / p_j2k->m_cp.tw;
10345 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10346 if (p_image->x0 < p_j2k->m_private_image->x0)
10347 p_image->x0 = p_j2k->m_private_image->x0;
10348 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
10349 if (p_image->x1 > p_j2k->m_private_image->x1)
10350 p_image->x1 = p_j2k->m_private_image->x1;
10352 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10353 if (p_image->y0 < p_j2k->m_private_image->y0)
10354 p_image->y0 = p_j2k->m_private_image->y0;
10355 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
10356 if (p_image->y1 > p_j2k->m_private_image->y1)
10357 p_image->y1 = p_j2k->m_private_image->y1;
10359 l_img_comp = p_image->comps;
10360 for (compno=0; compno < p_image->numcomps; ++compno)
10362 OPJ_INT32 l_comp_x1, l_comp_y1;
10364 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
10366 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
10367 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
10368 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
10369 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
10371 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
10372 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
10377 /* Destroy the previous output image*/
10378 if (p_j2k->m_output_image)
10379 opj_image_destroy(p_j2k->m_output_image);
10381 /* Create the ouput image from the information previously computed*/
10382 p_j2k->m_output_image = opj_image_create0();
10383 if (! (p_j2k->m_output_image)) {
10386 opj_copy_image_header(p_image, p_j2k->m_output_image);
10388 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
10390 /* customization of the decoding */
10391 opj_j2k_setup_decoding_tile(p_j2k);
10393 /* Decode the codestream */
10394 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10395 opj_image_destroy(p_j2k->m_private_image);
10396 p_j2k->m_private_image = NULL;
10400 /* Move data and copy one information from codec to output image*/
10401 for (compno = 0; compno < p_image->numcomps; compno++) {
10402 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
10404 if (p_image->comps[compno].data)
10405 opj_free(p_image->comps[compno].data);
10407 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
10409 p_j2k->m_output_image->comps[compno].data = NULL;
10415 opj_bool opj_j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k,
10416 OPJ_UINT32 res_factor,
10417 opj_event_mgr_t * p_manager)
10419 OPJ_UINT32 it_comp;
10421 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
10423 if (p_j2k->m_private_image) {
10424 if (p_j2k->m_private_image->comps) {
10425 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
10426 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
10427 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
10428 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
10429 if ( res_factor >= max_res){
10430 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
10433 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
10446 * Encodes all the tiles in a row.
10448 opj_bool opj_j2k_encode_v2( opj_j2k_v2_t * p_j2k,
10449 opj_stream_private_t *p_stream,
10450 opj_event_mgr_t * p_manager )
10453 OPJ_UINT32 l_nb_tiles;
10454 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
10455 OPJ_BYTE * l_current_data;
10457 /* preconditions */
10458 assert(p_j2k != 00);
10459 assert(p_stream != 00);
10460 assert(p_manager != 00);
10462 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10463 if (! l_current_data) {
10466 l_max_tile_size = 1000;
10468 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10469 for (i=0;i<l_nb_tiles;++i) {
10470 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
10471 opj_free(l_current_data);
10475 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
10476 if (l_current_tile_size > l_max_tile_size) {
10477 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
10478 if (! l_current_data) {
10481 l_max_tile_size = l_current_tile_size;
10484 opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10486 if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
10491 opj_free(l_current_data);
10496 * Ends the compression procedures and possibility add data to be read after the
10499 opj_bool opj_j2k_end_compress( opj_j2k_v2_t *p_j2k,
10500 opj_stream_private_t *p_stream,
10501 opj_event_mgr_t * p_manager)
10503 /* customization of the encoding */
10504 opj_j2k_setup_end_compress(p_j2k);
10506 if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10516 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
10518 * @param p_j2k the jpeg2000 codec.
10519 * @param p_stream the stream object.
10520 * @param p_manager the user event manager.
10522 * @return true if the codec is valid.
10524 opj_bool opj_j2k_start_compress(opj_j2k_v2_t *p_j2k,
10525 opj_stream_private_t *p_stream,
10526 opj_image_t * p_image,
10527 opj_event_mgr_t * p_manager)
10529 /* preconditions */
10530 assert(p_j2k != 00);
10531 assert(p_stream != 00);
10532 assert(p_manager != 00);
10534 p_j2k->m_private_image = opj_image_create0();
10535 opj_copy_image_header(p_image, p_j2k->m_private_image);
10537 // TODO_MSD: Find a better way
10538 if (p_image->comps) {
10539 OPJ_UINT32 it_comp;
10540 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
10541 if (p_image->comps[it_comp].data) {
10542 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
10543 p_image->comps[it_comp].data = NULL;
10549 /* customization of the validation */
10550 opj_j2k_setup_encoding_validation (p_j2k);
10552 /* validation of the parameters codec */
10553 if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
10557 /* customization of the encoding */
10558 opj_j2k_setup_header_writting(p_j2k);
10561 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10571 opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
10572 OPJ_UINT32 p_tile_index,
10573 opj_stream_private_t *p_stream,
10574 opj_event_mgr_t * p_manager )
10577 if (p_tile_index != p_j2k->m_current_tile_number) {
10578 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
10582 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);
10584 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
10585 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
10586 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10588 /* initialisation before tile encoding */
10589 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
10599 void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
10601 OPJ_UINT32 i,j,k = 0;
10602 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
10603 opj_image_comp_t * l_img_comp = 00;
10604 opj_tcd_tilecomp_v2_t * l_tilec = 00;
10605 opj_image_t * l_image = 00;
10606 OPJ_UINT32 l_size_comp, l_remaining;
10607 OPJ_INT32 * l_src_ptr;
10608 l_tilec = p_tcd->tcd_image->tiles->comps;
10609 l_image = p_tcd->image;
10610 l_img_comp = l_image->comps;
10612 for (i=0;i<p_tcd->image->numcomps;++i) {
10613 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
10614 l_remaining = l_img_comp->prec & 7; /* (%8) */
10619 if (l_size_comp == 3) {
10623 l_width = (l_tilec->x1 - l_tilec->x0);
10624 l_height = (l_tilec->y1 - l_tilec->y0);
10625 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
10626 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
10627 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
10628 l_stride = l_image_width - l_width;
10629 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
10631 switch (l_size_comp) {
10634 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
10635 if (l_img_comp->sgnd) {
10636 for (j=0;j<l_height;++j) {
10637 for (k=0;k<l_width;++k) {
10638 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
10642 l_src_ptr += l_stride;
10646 for (j=0;j<l_height;++j) {
10647 for (k=0;k<l_width;++k) {
10648 *(l_dest_ptr) = (*l_src_ptr)&0xff;
10652 l_src_ptr += l_stride;
10656 p_data = (OPJ_BYTE*) l_dest_ptr;
10661 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
10662 if (l_img_comp->sgnd) {
10663 for (j=0;j<l_height;++j) {
10664 for (k=0;k<l_width;++k) {
10665 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
10667 l_src_ptr += l_stride;
10671 for (j=0;j<l_height;++j) {
10672 for (k=0;k<l_width;++k) {
10673 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10675 l_src_ptr += l_stride;
10679 p_data = (OPJ_BYTE*) l_dest_ptr;
10684 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
10685 for (j=0;j<l_height;++j) {
10686 for (k=0;k<l_width;++k) {
10687 *(l_dest_ptr++) = *(l_src_ptr++);
10689 l_src_ptr += l_stride;
10692 p_data = (OPJ_BYTE*) l_dest_ptr;
10705 * @param p_j2k the jpeg2000 codec.
10706 * @param p_stream the stream to write data to.
10707 * @param p_manager the user event manager.
10709 opj_bool opj_j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
10711 OPJ_UINT32 p_data_size,
10712 opj_stream_private_t *p_stream,
10713 opj_event_mgr_t * p_manager )
10715 opj_tcd_v2_t * l_tcd = 00;
10716 opj_cp_v2_t * l_cp = 00;
10717 opj_tcp_v2_t * l_tcp = 00;
10718 OPJ_UINT32 l_nb_bytes_written;
10719 OPJ_BYTE * l_current_data = 00;
10720 OPJ_UINT32 l_tile_size = 0;
10721 OPJ_UINT32 l_available_data;
10723 /* preconditions */
10724 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10726 l_tcd = p_j2k->m_tcd;
10727 l_cp = &(p_j2k->m_cp);
10728 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10730 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
10731 l_available_data = l_tile_size;
10732 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
10734 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
10735 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
10739 l_nb_bytes_written = 0;
10740 if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10743 l_current_data += l_nb_bytes_written;
10744 l_available_data -= l_nb_bytes_written;
10746 l_nb_bytes_written = 0;
10747 if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10751 l_available_data -= l_nb_bytes_written;
10752 l_nb_bytes_written = l_tile_size - l_available_data;
10754 if ( opj_stream_write_data( p_stream,
10755 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
10756 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
10760 ++p_j2k->m_current_tile_number;
10767 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
10768 * are valid. Developers wanting to extend the library can add their own validation procedures.
10770 void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
10772 /* preconditions */
10773 assert(p_j2k != 00);
10775 /* DEVELOPER CORNER, insert your custom procedures */
10776 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
10778 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10779 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_updated_tlm);
10782 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
10783 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
10784 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory);
10788 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
10789 * are valid. Developers wanting to extend the library can add their own validation procedures.
10791 void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
10793 /* preconditions */
10794 assert(p_j2k != 00);
10796 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder);
10797 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation);
10799 /* DEVELOPER CORNER, add your custom validation procedure */
10800 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation);
10805 * Sets up the procedures to do on writing header.
10806 * Developers wanting to extend the library can add their own writing procedures.
10808 void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
10810 /* preconditions */
10811 assert(p_j2k != 00);
10813 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
10814 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc );
10815 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_siz_v2 );
10816 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_cod_v2 );
10817 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_qcd_v2 );
10820 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10821 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
10822 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_tlm_v2 );
10824 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
10825 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_poc_v2 );
10829 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
10831 if (p_j2k->m_cp.comment != 00) {
10832 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_com_v2);
10835 /* DEVELOPER CORNER, insert your custom procedures */
10836 if (p_j2k->m_cp.rsiz & MCT) {
10837 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
10839 /* End of Developer Corner */
10841 if (p_j2k->cstr_index) {
10842 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header );
10845 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd);
10846 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);
10850 opj_bool opj_j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
10852 OPJ_UINT32 * p_data_written,
10853 OPJ_UINT32 p_total_data_size,
10854 opj_stream_private_t *p_stream,
10855 struct opj_event_mgr * p_manager )
10858 OPJ_UINT32 l_nb_bytes_written = 0;
10859 OPJ_UINT32 l_current_nb_bytes_written;
10860 OPJ_BYTE * l_begin_data = 00;
10862 opj_tcp_v2_t *l_tcp = 00;
10863 opj_tcd_v2_t * l_tcd = 00;
10864 opj_cp_v2_t * l_cp = 00;
10866 l_tcd = p_j2k->m_tcd;
10867 l_cp = &(p_j2k->m_cp);
10868 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10870 l_tcd->cur_pino = 0;
10872 /*Get number of tile parts*/
10873 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10878 l_current_nb_bytes_written = 0;
10879 l_begin_data = p_data;
10880 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
10885 l_nb_bytes_written += l_current_nb_bytes_written;
10886 p_data += l_current_nb_bytes_written;
10887 p_total_data_size -= l_current_nb_bytes_written;
10889 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
10890 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
10891 l_current_nb_bytes_written = 0;
10892 j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10893 l_nb_bytes_written += l_current_nb_bytes_written;
10894 p_data += l_current_nb_bytes_written;
10895 p_total_data_size -= l_current_nb_bytes_written;
10897 l_current_nb_bytes_written = 0;
10898 j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10899 l_nb_bytes_written += l_current_nb_bytes_written;
10900 p_data += l_current_nb_bytes_written;
10901 p_total_data_size -= l_current_nb_bytes_written;
10904 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
10905 l_current_nb_bytes_written = 0;
10906 j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
10907 l_nb_bytes_written += l_current_nb_bytes_written;
10908 p_data += l_current_nb_bytes_written;
10909 p_total_data_size -= l_current_nb_bytes_written;
10913 l_current_nb_bytes_written = 0;
10914 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10918 l_nb_bytes_written += l_current_nb_bytes_written;
10919 * p_data_written = l_nb_bytes_written;
10921 /* Writing Psot in SOT marker */
10922 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
10924 if (l_cp->m_specific_param.m_enc.m_cinema){
10925 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
10931 opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
10933 OPJ_UINT32 * p_data_written,
10934 OPJ_UINT32 p_total_data_size,
10935 opj_stream_private_t *p_stream,
10936 struct opj_event_mgr * p_manager
10939 OPJ_UINT32 tilepartno=0;
10940 OPJ_UINT32 l_nb_bytes_written = 0;
10941 OPJ_UINT32 l_current_nb_bytes_written;
10942 OPJ_UINT32 l_part_tile_size;
10943 OPJ_UINT32 tot_num_tp;
10946 OPJ_BYTE * l_begin_data;
10947 opj_tcp_v2_t *l_tcp = 00;
10948 opj_tcd_v2_t * l_tcd = 00;
10949 opj_cp_v2_t * l_cp = 00;
10952 l_tcd = p_j2k->m_tcd;
10953 l_cp = &(p_j2k->m_cp);
10954 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10956 /*Get number of tile parts*/
10957 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
10959 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
10960 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10961 l_current_nb_bytes_written = 0;
10962 l_part_tile_size = 0;
10963 l_begin_data = p_data;
10965 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10969 l_nb_bytes_written += l_current_nb_bytes_written;
10970 p_data += l_current_nb_bytes_written;
10971 p_total_data_size -= l_current_nb_bytes_written;
10972 l_part_tile_size += l_nb_bytes_written;
10974 l_current_nb_bytes_written = 0;
10975 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10979 p_data += l_current_nb_bytes_written;
10980 l_nb_bytes_written += l_current_nb_bytes_written;
10981 p_total_data_size -= l_current_nb_bytes_written;
10982 l_part_tile_size += l_nb_bytes_written;
10984 /* Writing Psot in SOT marker */
10985 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10987 if (l_cp->m_specific_param.m_enc.m_cinema) {
10988 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10991 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10994 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
10995 l_tcd->cur_pino = pino;
10997 /*Get number of tile parts*/
10998 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
10999 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
11000 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
11001 l_current_nb_bytes_written = 0;
11002 l_part_tile_size = 0;
11003 l_begin_data = p_data;
11005 if (! j2k_write_sot_v2(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
11009 l_nb_bytes_written += l_current_nb_bytes_written;
11010 p_data += l_current_nb_bytes_written;
11011 p_total_data_size -= l_current_nb_bytes_written;
11012 l_part_tile_size += l_current_nb_bytes_written;
11014 l_current_nb_bytes_written = 0;
11016 if (! j2k_write_sod_v2(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
11020 l_nb_bytes_written += l_current_nb_bytes_written;
11021 p_data += l_current_nb_bytes_written;
11022 p_total_data_size -= l_current_nb_bytes_written;
11023 l_part_tile_size += l_current_nb_bytes_written;
11025 /* Writing Psot in SOT marker */
11026 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
11028 if (l_cp->m_specific_param.m_enc.m_cinema) {
11029 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
11032 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
11036 *p_data_written = l_nb_bytes_written;
11042 * Writes the updated tlm.
11044 * @param p_stream the stream to write data to.
11045 * @param p_j2k J2K codec.
11046 * @param p_manager the user event manager.
11048 opj_bool j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
11049 struct opj_stream_private *p_stream,
11050 struct opj_event_mgr * p_manager )
11052 OPJ_UINT32 l_tlm_size;
11053 OPJ_SIZE_T l_tlm_position, l_current_position;
11055 /* preconditions */
11056 assert(p_j2k != 00);
11057 assert(p_manager != 00);
11058 assert(p_stream != 00);
11060 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
11061 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
11062 l_current_position = opj_stream_tell(p_stream);
11064 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
11068 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) {
11072 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
11081 * Ends the encoding, i.e. frees memory.
11083 * @param p_stream the stream to write data to.
11084 * @param p_j2k J2K codec.
11085 * @param p_manager the user event manager.
11087 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
11088 struct opj_stream_private *p_stream,
11089 struct opj_event_mgr * p_manager )
11091 /* preconditions */
11092 assert(p_j2k != 00);
11093 assert(p_manager != 00);
11094 assert(p_stream != 00);
11096 tcd_destroy_v2(p_j2k->m_tcd);
11099 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
11100 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
11101 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
11102 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
11105 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
11106 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
11107 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
11110 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
11116 * Destroys the memory associated with the decoding of headers.
11118 static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
11119 opj_stream_private_t *p_stream,
11120 opj_event_mgr_t * p_manager
11123 /* preconditions */
11124 assert(p_j2k != 00);
11125 assert(p_stream != 00);
11126 assert(p_manager != 00);
11128 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
11129 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
11130 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
11133 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
11142 * @param p_stream the stream to write data to.
11143 * @param p_j2k J2K codec.
11144 * @param p_manager the user event manager.
11146 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
11147 struct opj_stream_private *p_stream,
11148 struct opj_event_mgr * p_manager )
11150 opj_codestream_info_t * l_cstr_info = 00;
11152 /* preconditions */
11153 assert(p_j2k != 00);
11154 assert(p_manager != 00);
11155 assert(p_stream != 00);
11158 /* TODO mergeV2: check this part which use cstr_info */
11159 /*l_cstr_info = p_j2k->cstr_info;
11163 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));
11165 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
11166 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
11168 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
11170 l_cstr_info->tw = p_j2k->m_cp.tw;
11171 l_cstr_info->th = p_j2k->m_cp.th;
11173 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
11174 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
11175 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
11176 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
11178 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
11180 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
11182 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
11184 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
11185 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
11188 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
11190 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
11192 /*l_cstr_info->maxmarknum = 100;
11193 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
11194 l_cstr_info->marknum = 0;
11197 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);
11201 * Creates a tile-coder decoder.
11203 * @param p_stream the stream to write data to.
11204 * @param p_j2k J2K codec.
11205 * @param p_manager the user event manager.
11207 static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
11208 opj_stream_private_t *p_stream,
11209 opj_event_mgr_t * p_manager
11212 /* preconditions */
11213 assert(p_j2k != 00);
11214 assert(p_manager != 00);
11215 assert(p_stream != 00);
11217 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
11219 if (! p_j2k->m_tcd) {
11220 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
11224 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
11225 tcd_destroy_v2(p_j2k->m_tcd);
11236 * @param p_j2k the jpeg2000 codec.
11237 * @param p_stream the stream to write data to.
11238 * @param p_manager the user event manager.
11240 opj_bool opj_j2k_write_tile (opj_j2k_v2_t * p_j2k,
11241 OPJ_UINT32 p_tile_index,
11243 OPJ_UINT32 p_data_size,
11244 opj_stream_private_t *p_stream,
11245 opj_event_mgr_t * p_manager )
11247 if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
11248 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
11252 if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
11253 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);