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 * Writes the SIZ marker (image and tile size)
405 * @param p_stream the stream to write data to.
406 * @param p_j2k J2K codec.
407 * @param p_manager the user event manager.
409 static opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k,
410 opj_stream_private_t *p_stream,
411 opj_event_mgr_t * p_manager );
414 * Reads a SIZ marker (image and tile size)
415 * @param p_header_data the data contained in the SIZ box.
416 * @param jp2 the jpeg2000 file codec.
417 * @param p_header_size the size of the data contained in the SIZ marker.
418 * @param p_manager the user event manager.
420 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
421 OPJ_BYTE * p_header_data,
422 OPJ_UINT32 p_header_size,
423 opj_event_mgr_t * p_manager);
426 * Writes the COM marker (comment)
428 * @param p_stream the stream to write data to.
429 * @param p_j2k J2K codec.
430 * @param p_manager the user event manager.
432 static opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k,
433 opj_stream_private_t *p_stream,
434 opj_event_mgr_t * p_manager );
437 * Reads a COM marker (comments)
438 * @param p_header_data the data contained in the COM box.
439 * @param jp2 the jpeg2000 file codec.
440 * @param p_header_size the size of the data contained in the COM marker.
441 * @param p_manager the user event manager.
443 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
444 OPJ_BYTE * p_header_data,
445 OPJ_UINT32 p_header_size,
446 opj_event_mgr_t * p_manager );
448 * Writes the COD marker (Coding style default)
450 * @param p_stream the stream to write data to.
451 * @param p_j2k J2K codec.
452 * @param p_manager the user event manager.
454 static opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k,
455 opj_stream_private_t *p_stream,
456 opj_event_mgr_t * p_manager );
459 * Reads a COD marker (Coding Styke defaults)
460 * @param p_header_data the data contained in the COD box.
461 * @param p_j2k the jpeg2000 codec.
462 * @param p_header_size the size of the data contained in the COD marker.
463 * @param p_manager the user event manager.
465 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
466 OPJ_BYTE * p_header_data,
467 OPJ_UINT32 p_header_size,
468 opj_event_mgr_t * p_manager);
471 * Writes the COC marker (Coding style component)
473 * @param p_comp_number the index of the component to output.
474 * @param p_stream the stream to write data to.
475 * @param p_j2k J2K codec.
476 * @param p_manager the user event manager.
478 static opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k,
479 OPJ_UINT32 p_comp_no,
480 opj_stream_private_t *p_stream,
481 opj_event_mgr_t * p_manager );
483 * Writes the COC marker (Coding style component)
485 * @param p_comp_no the index of the component to output.
486 * @param p_stream the stream to write data to.
487 * @param p_j2k J2K codec.
488 * @param p_manager the user event manager.
490 static void opj_j2k_write_coc_in_memory(opj_j2k_v2_t *p_j2k,
491 OPJ_UINT32 p_comp_no,
493 OPJ_UINT32 * p_data_written,
494 opj_event_mgr_t * p_manager );
497 * Gets the maximum size taken by a coc.
499 * @param p_j2k the jpeg2000 codec to use.
501 static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k);
505 * Reads a COC marker (Coding Style Component)
506 * @param p_header_data the data contained in the COC box.
507 * @param p_j2k the jpeg2000 codec.
508 * @param p_header_size the size of the data contained in the COC marker.
509 * @param p_manager the user event manager.
511 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
512 OPJ_BYTE * p_header_data,
513 OPJ_UINT32 p_header_size,
514 opj_event_mgr_t * p_manager );
517 * Writes the QCD marker (quantization default)
519 * @param p_comp_number the index of the component to output.
520 * @param p_stream the stream to write data to.
521 * @param p_j2k J2K codec.
522 * @param p_manager the user event manager.
524 static opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k,
525 opj_stream_private_t *p_stream,
526 opj_event_mgr_t * p_manager );
529 * Reads a QCD marker (Quantization defaults)
530 * @param p_header_data the data contained in the QCD box.
531 * @param p_j2k the jpeg2000 codec.
532 * @param p_header_size the size of the data contained in the QCD marker.
533 * @param p_manager the user event manager.
535 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
536 OPJ_BYTE * p_header_data,
537 OPJ_UINT32 p_header_size,
538 opj_event_mgr_t * p_manager );
540 * Writes the QCC marker (quantization component)
542 * @param p_comp_no the index of the component to output.
543 * @param p_stream the stream to write data to.
544 * @param p_j2k J2K codec.
545 * @param p_manager the user event manager.
547 static opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k,
548 OPJ_UINT32 p_comp_no,
549 opj_stream_private_t *p_stream,
550 opj_event_mgr_t * p_manager );
553 * Writes the QCC marker (quantization component)
555 * @param p_comp_no the index of the component to output.
556 * @param p_stream the stream to write data to.
557 * @param p_j2k J2K codec.
558 * @param p_manager the user event manager.
560 static void opj_j2k_write_qcc_in_memory(opj_j2k_v2_t *p_j2k,
561 OPJ_UINT32 p_comp_no,
563 OPJ_UINT32 * p_data_written,
564 opj_event_mgr_t * p_manager );
567 * Gets the maximum size taken by a qcc.
569 static OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k);
572 * Reads a QCC marker (Quantization component)
573 * @param p_header_data the data contained in the QCC box.
574 * @param p_j2k the jpeg2000 codec.
575 * @param p_header_size the size of the data contained in the QCC marker.
576 * @param p_manager the user event manager.
578 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
579 OPJ_BYTE * p_header_data,
580 OPJ_UINT32 p_header_size,
581 opj_event_mgr_t * p_manager);
583 * Writes the POC marker (Progression Order Change)
585 * @param p_stream the stream to write data to.
586 * @param p_j2k J2K codec.
587 * @param p_manager the user event manager.
589 static opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k,
590 opj_stream_private_t *p_stream,
591 opj_event_mgr_t * p_manager );
593 * Writes the POC marker (Progression Order Change)
595 * @param p_stream the stream to write data to.
596 * @param p_j2k J2K codec.
597 * @param p_manager the user event manager.
599 static void opj_j2k_write_poc_in_memory(opj_j2k_v2_t *p_j2k,
601 OPJ_UINT32 * p_data_written,
602 opj_event_mgr_t * p_manager );
604 * Gets the maximum size taken by the writting of a POC.
606 static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k);
609 * Reads a POC marker (Progression Order Change)
611 * @param p_header_data the data contained in the POC box.
612 * @param p_j2k the jpeg2000 codec.
613 * @param p_header_size the size of the data contained in the POC marker.
614 * @param p_manager the user event manager.
616 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
617 OPJ_BYTE * p_header_data,
618 OPJ_UINT32 p_header_size,
619 opj_event_mgr_t * p_manager );
622 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
624 static OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k);
627 * Gets the maximum size taken by the headers of the SOT.
629 * @param p_j2k the jpeg2000 codec to use.
631 static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k);
635 * Reads a CRG marker (Component registration)
637 * @param p_header_data the data contained in the TLM box.
638 * @param p_j2k the jpeg2000 codec.
639 * @param p_header_size the size of the data contained in the TLM marker.
640 * @param p_manager the user event manager.
642 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
643 OPJ_BYTE * p_header_data,
644 OPJ_UINT32 p_header_size,
645 opj_event_mgr_t * p_manager );
647 * Reads a TLM marker (Tile Length Marker)
649 * @param p_header_data the data contained in the TLM box.
650 * @param p_j2k the jpeg2000 codec.
651 * @param p_header_size the size of the data contained in the TLM marker.
652 * @param p_manager the user event manager.
654 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
655 OPJ_BYTE * p_header_data,
656 OPJ_UINT32 p_header_size,
657 opj_event_mgr_t * p_manager);
660 * Writes the updated tlm.
662 * @param p_stream the stream to write data to.
663 * @param p_j2k J2K codec.
664 * @param p_manager the user event manager.
666 static opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
667 struct opj_stream_private *p_stream,
668 struct opj_event_mgr * p_manager );
671 * Reads a PLM marker (Packet length, main header marker)
673 * @param p_header_data the data contained in the TLM box.
674 * @param p_j2k the jpeg2000 codec.
675 * @param p_header_size the size of the data contained in the TLM marker.
676 * @param p_manager the user event manager.
678 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
679 OPJ_BYTE * p_header_data,
680 OPJ_UINT32 p_header_size,
681 opj_event_mgr_t * p_manager);
683 * Reads a PLT marker (Packet length, tile-part header)
685 * @param p_header_data the data contained in the PLT box.
686 * @param p_j2k the jpeg2000 codec.
687 * @param p_header_size the size of the data contained in the PLT marker.
688 * @param p_manager the user event manager.
690 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
691 OPJ_BYTE * p_header_data,
692 OPJ_UINT32 p_header_size,
693 opj_event_mgr_t * p_manager );
697 * Reads a PPM marker (Packed packet headers, main header)
699 * @param p_header_data the data contained in the POC box.
700 * @param p_j2k the jpeg2000 codec.
701 * @param p_header_size the size of the data contained in the POC marker.
702 * @param p_manager the user event manager.
705 static opj_bool j2k_read_ppm_v2 (
707 OPJ_BYTE * p_header_data,
708 OPJ_UINT32 p_header_size,
709 struct opj_event_mgr * p_manager
713 static opj_bool j2k_read_ppm_v3 (
715 OPJ_BYTE * p_header_data,
716 OPJ_UINT32 p_header_size,
717 struct opj_event_mgr * p_manager
722 * Reads a PPT marker (Packed packet headers, tile-part header)
724 * @param p_header_data the data contained in the PPT box.
725 * @param p_j2k the jpeg2000 codec.
726 * @param p_header_size the size of the data contained in the PPT marker.
727 * @param p_manager the user event manager.
729 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
730 OPJ_BYTE * p_header_data,
731 OPJ_UINT32 p_header_size,
732 opj_event_mgr_t * p_manager );
734 * Writes the TLM marker (Tile Length Marker)
736 * @param p_stream the stream to write data to.
737 * @param p_j2k J2K codec.
738 * @param p_manager the user event manager.
740 static opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k,
741 opj_stream_private_t *p_stream,
742 opj_event_mgr_t * p_manager );
745 * Writes the SOT marker (Start of tile-part)
747 * @param p_stream the stream to write data to.
748 * @param p_j2k J2K codec.
749 * @param p_manager the user event manager.
751 static opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k,
753 OPJ_UINT32 * p_data_written,
754 const opj_stream_private_t *p_stream,
755 opj_event_mgr_t * p_manager );
758 * Reads a PPT marker (Packed packet headers, tile-part header)
760 * @param p_header_data the data contained in the PPT box.
761 * @param p_j2k the jpeg2000 codec.
762 * @param p_header_size the size of the data contained in the PPT marker.
763 * @param p_manager the user event manager.
765 static opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
766 OPJ_BYTE * p_header_data,
767 OPJ_UINT32 p_header_size,
768 opj_event_mgr_t * p_manager );
770 * Writes the SOD marker (Start of data)
772 * @param p_stream the stream to write data to.
773 * @param p_j2k J2K codec.
774 * @param p_manager the user event manager.
776 static opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k,
777 opj_tcd_v2_t * p_tile_coder,
779 OPJ_UINT32 * p_data_written,
780 OPJ_UINT32 p_total_data_size,
781 const opj_stream_private_t *p_stream,
782 opj_event_mgr_t * p_manager );
785 * Reads a SOD marker (Start Of Data)
787 * @param p_header_data the data contained in the SOD box.
788 * @param p_j2k the jpeg2000 codec.
789 * @param p_header_size the size of the data contained in the SOD marker.
790 * @param p_manager the user event manager.
792 static opj_bool opj_j2k_read_sod( opj_j2k_v2_t *p_j2k,
793 opj_stream_private_t *p_stream,
794 opj_event_mgr_t * p_manager );
797 * Updates the Tile Length Marker.
799 void opj_j2k_update_tlm (opj_j2k_v2_t * p_j2k, OPJ_UINT32 p_tile_part_size )
801 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_j2k->m_current_tile_number,1); /* PSOT */
802 ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
804 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,p_tile_part_size,4); /* PSOT */
805 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
809 Write the RGN marker (region-of-interest)
810 @param j2k J2K handle
811 @param compno Number of the component concerned by the information written
812 @param tileno Number of the tile concerned by the information written
814 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
817 * Writes the RGN marker (Region Of Interest)
819 * @param p_tile_no the tile to output
820 * @param p_comp_no the component to output
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_rgn_v2( opj_j2k_v2_t *p_j2k,
826 OPJ_UINT32 p_tile_no,
827 OPJ_UINT32 p_comp_no,
828 struct opj_stream_private *p_stream,
829 struct opj_event_mgr * p_manager );
832 * Reads a RGN marker (Region Of Interest)
834 * @param p_header_data the data contained in the POC box.
835 * @param p_j2k the jpeg2000 codec.
836 * @param p_header_size the size of the data contained in the POC marker.
837 * @param p_manager the user event manager.
839 static opj_bool opj_j2k_read_rgn (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 * Writes the EOC marker (End of Codestream)
847 * @param p_stream the stream to write data to.
848 * @param p_j2k J2K codec.
849 * @param p_manager the user event manager.
851 static opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
852 struct opj_stream_private *p_stream,
853 struct opj_event_mgr * p_manager );
856 Write the EOC marker (end of codestream)
857 @param j2k J2K handle
859 static void j2k_write_eoc(opj_j2k_t *j2k);
861 Read the EOC marker (end of codestream)
862 @param j2k J2K handle
864 static void j2k_read_eoc(opj_j2k_t *j2k);
867 * Reads a EOC marker (End Of Codestream)
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.
875 static opj_bool j2k_read_eoc_v2 (
877 struct opj_stream_private *p_stream,
878 struct opj_event_mgr * p_manager
885 * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
887 * @param p_stream the stream to write data to.
888 * @param p_j2k J2K codec.
889 * @param p_manager the user event manager.
891 static opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
892 struct opj_stream_private *p_stream,
893 struct opj_event_mgr * p_manager );
898 * @param p_stream the stream to write data to.
899 * @param p_j2k J2K codec.
900 * @param p_manager the user event manager.
902 static opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
903 struct opj_stream_private *p_stream,
904 struct opj_event_mgr * p_manager );
907 Add main header marker information
908 @param cstr_info Codestream information structure
909 @param type marker type
910 @param pos byte offset of marker segment
911 @param len length of marker segment
913 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
915 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
917 Add tile header marker information
918 @param tileno tile index number
919 @param cstr_info Codestream information structure
920 @param type marker type
921 @param pos byte offset of marker segment
922 @param len length of marker segment
924 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
926 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);
929 * Reads an unknown marker
931 * @param p_stream the stream object to read from.
932 * @param p_j2k the jpeg2000 codec.
933 * @param p_manager the user event manager.
935 * @return true if the marker could be deduced.
937 static opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
938 opj_stream_private_t *p_stream,
939 OPJ_UINT32 *output_marker,
940 opj_event_mgr_t * p_manager );
943 * Writes the MCT marker (Multiple Component Transform)
945 * @param p_stream the stream to write data to.
946 * @param p_j2k J2K codec.
947 * @param p_manager the user event manager.
949 static opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
950 opj_mct_data_t * p_mct_record,
951 struct opj_stream_private *p_stream,
952 struct opj_event_mgr * p_manager );
955 * Reads a MCT marker (Multiple Component Transform)
957 * @param p_header_data the data contained in the MCT box.
958 * @param p_j2k the jpeg2000 codec.
959 * @param p_header_size the size of the data contained in the MCT marker.
960 * @param p_manager the user event manager.
962 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
963 OPJ_BYTE * p_header_data,
964 OPJ_UINT32 p_header_size,
965 opj_event_mgr_t * p_manager );
968 * Writes the MCC marker (Multiple Component Collection)
970 * @param p_stream the stream to write data to.
971 * @param p_j2k J2K codec.
972 * @param p_manager the user event manager.
974 static opj_bool j2k_write_mcc_record( opj_j2k_v2_t *p_j2k,
975 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
976 struct opj_stream_private *p_stream,
977 struct opj_event_mgr * p_manager );
981 * Reads a MCC marker (Multiple Component Collection)
983 * @param p_header_data the data contained in the MCC box.
984 * @param p_j2k the jpeg2000 codec.
985 * @param p_header_size the size of the data contained in the MCC marker.
986 * @param p_manager the user event manager.
988 static opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
989 OPJ_BYTE * p_header_data,
990 OPJ_UINT32 p_header_size,
991 opj_event_mgr_t * p_manager );
994 * Writes the MCO marker (Multiple component transformation ordering)
996 * @param p_stream the stream to write data to.
997 * @param p_j2k J2K codec.
998 * @param p_manager the user event manager.
1000 static opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
1001 struct opj_stream_private *p_stream,
1002 struct opj_event_mgr * p_manager );
1005 * Reads a MCO marker (Multiple Component Transform Ordering)
1007 * @param p_header_data the data contained in the MCO box.
1008 * @param p_j2k the jpeg2000 codec.
1009 * @param p_header_size the size of the data contained in the MCO marker.
1010 * @param p_manager the user event manager.
1012 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
1013 OPJ_BYTE * p_header_data,
1014 OPJ_UINT32 p_header_size,
1015 opj_event_mgr_t * p_manager );
1017 static opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index);
1019 static void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1020 static void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1021 static void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1022 static void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1024 static void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1025 static void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1026 static void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1027 static void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1029 static void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1030 static void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1031 static void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1032 static void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1036 * Ends the encoding, i.e. frees memory.
1038 * @param p_stream the stream to write data to.
1039 * @param p_j2k J2K codec.
1040 * @param p_manager the user event manager.
1042 static opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
1043 struct opj_stream_private *p_stream,
1044 struct opj_event_mgr * p_manager );
1047 * Writes the CBD marker (Component bit depth definition)
1049 * @param p_stream the stream to write data to.
1050 * @param p_j2k J2K codec.
1051 * @param p_manager the user event manager.
1053 static opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
1054 struct opj_stream_private *p_stream,
1055 struct opj_event_mgr * p_manager );
1058 * Reads a CBD marker (Component bit depth definition)
1059 * @param p_header_data the data contained in the CBD box.
1060 * @param p_j2k the jpeg2000 codec.
1061 * @param p_header_size the size of the data contained in the CBD marker.
1062 * @param p_manager the user event manager.
1064 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
1065 OPJ_BYTE * p_header_data,
1066 OPJ_UINT32 p_header_size,
1067 opj_event_mgr_t * p_manager);
1070 * Writes the image components.
1072 * @param p_stream the stream to write data to.
1073 * @param p_j2k J2K codec.
1074 * @param p_manager the user event manager.
1076 static opj_bool j2k_write_image_components( opj_j2k_v2_t *p_j2k,
1077 struct opj_stream_private *p_stream,
1078 struct opj_event_mgr * p_manager );
1081 * Writes regions of interests.
1083 * @param p_stream the stream to write data to.
1084 * @param p_j2k J2K codec.
1085 * @param p_manager the user event manager.
1087 static opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
1088 struct opj_stream_private *p_stream,
1089 struct opj_event_mgr * p_manager );
1094 * @param p_stream the stream to write data to.
1095 * @param p_j2k J2K codec.
1096 * @param p_manager the user event manager.
1098 static opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
1099 struct opj_stream_private *p_stream,
1100 struct opj_event_mgr * p_manager );
1103 * Checks the progression order changes values. Tells of the poc given as input are valid.
1104 * A nice message is outputted at errors.
1106 * @param p_pocs the progression order changes.
1107 * @param p_nb_pocs the number of progression order changes.
1108 * @param p_nb_resolutions the number of resolutions.
1109 * @param numcomps the number of components
1110 * @param numlayers the number of layers.
1112 * @return true if the pocs are valid.
1114 static opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1115 OPJ_UINT32 p_nb_pocs,
1116 OPJ_UINT32 p_nb_resolutions,
1117 OPJ_UINT32 numcomps,
1118 OPJ_UINT32 numlayers,
1119 opj_event_mgr_t * p_manager);
1122 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1124 * @param cp the coding parameters.
1125 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1126 * @param tileno the given tile.
1128 * @return the number of tile parts.
1130 static OPJ_UINT32 j2k_get_num_tp_v2( opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno);
1132 /** mem allocation for TLM marker*/
1133 static int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k );
1136 * Calculates the total number of tile parts needed by the encoder to
1137 * encode such an image. If not enough memory is available, then the function return false.
1139 * @param p_nb_tiles pointer that will hold the number of tile parts.
1140 * @param cp the coding parameters for the image.
1141 * @param image the image to encode.
1142 * @param p_j2k the p_j2k encoder.
1143 * @param p_manager the user event manager.
1145 * @return true if the function was successful, false else.
1147 static opj_bool j2k_calculate_tp_v2(opj_j2k_v2_t *p_j2k,
1149 OPJ_UINT32 * p_nb_tiles,
1151 opj_event_mgr_t * p_manager);
1153 static void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1155 static void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream);
1157 static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
1163 /* ----------------------------------------------------------------------- */
1164 typedef struct j2k_prog_order{
1165 OPJ_PROG_ORDER enum_prog;
1169 j2k_prog_order_t j2k_prog_order_list[] = {
1175 {(OPJ_PROG_ORDER)-1, ""}
1183 const OPJ_UINT32 MCT_ELEMENT_SIZE [] =
1191 typedef void (* opj_j2k_mct_function) (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem);
1193 const opj_j2k_mct_function j2k_mct_read_functions_to_float [] =
1195 opj_j2k_read_int16_to_float,
1196 opj_j2k_read_int32_to_float,
1197 opj_j2k_read_float32_to_float,
1198 opj_j2k_read_float64_to_float
1201 const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] =
1203 opj_j2k_read_int16_to_int32,
1204 opj_j2k_read_int32_to_int32,
1205 opj_j2k_read_float32_to_int32,
1206 opj_j2k_read_float64_to_int32
1209 const opj_j2k_mct_function j2k_mct_write_functions_from_float [] =
1211 opj_j2k_write_float_to_int16,
1212 opj_j2k_write_float_to_int32,
1213 opj_j2k_write_float_to_float,
1214 opj_j2k_write_float_to_float64
1217 typedef struct opj_dec_memory_marker_handler
1221 /** value of the state when the marker can appear */
1223 /** action linked to the marker */
1224 opj_bool (*handler) (
1225 opj_j2k_v2_t *p_j2k,
1226 OPJ_BYTE * p_header_data,
1227 OPJ_UINT32 p_header_size,
1228 struct opj_event_mgr * p_manager
1231 opj_dec_memory_marker_handler_t;
1233 const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
1236 {J2K_MS_SOT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPHSOT, j2k_read_sot},
1237 {J2K_MS_COD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_cod},
1238 {J2K_MS_COC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_coc},
1239 {J2K_MS_RGN, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_rgn},
1240 {J2K_MS_QCD, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcd},
1241 {J2K_MS_QCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_qcc},
1242 {J2K_MS_POC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_poc},
1243 {J2K_MS_SIZ, J2K_DEC_STATE_MHSIZ , j2k_read_siz},
1244 {J2K_MS_TLM, J2K_DEC_STATE_MH, j2k_read_tlm},
1245 {J2K_MS_PLM, J2K_DEC_STATE_MH, j2k_read_plm},
1246 {J2K_MS_PLT, J2K_DEC_STATE_TPH, j2k_read_plt},
1247 {J2K_MS_PPM, J2K_DEC_STATE_MH, j2k_read_ppm},
1248 {J2K_MS_PPT, J2K_DEC_STATE_TPH, j2k_read_ppt},
1250 {J2K_MS_CRG, J2K_DEC_STATE_MH, j2k_read_crg},
1251 {J2K_MS_COM, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_com},
1252 {J2K_MS_MCT, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mct},
1253 {J2K_MS_CBD, J2K_DEC_STATE_MH , j2k_read_cbd},
1254 {J2K_MS_MCC, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mcc},
1255 {J2K_MS_MCO, J2K_DEC_STATE_MH | J2K_DEC_STATE_TPH, j2k_read_mco},
1257 {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
1258 {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
1259 {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
1260 {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
1261 {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
1262 {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
1263 {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
1264 {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
1265 {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
1266 {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
1267 {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
1268 {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm_v3},
1269 {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
1271 {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
1272 {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
1273 {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
1274 {J2K_MS_CBD, J2K_STATE_MH , opj_j2k_read_cbd},
1275 {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
1276 {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
1278 #ifdef TODO_MS /* FIXME */
1279 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
1280 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
1281 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
1282 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
1284 #endif /* USE_JPWL */
1286 {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
1287 {J2K_MS_INSEC, 0, j2k_read_insec}
1288 #endif /* USE_JPSEC */
1289 {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*j2k_read_unk_v2}*/
1294 void opj_j2k_read_int16_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1296 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1297 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1301 for (i=0;i<p_nb_elem;++i) {
1302 opj_read_bytes(l_src_data,&l_temp,2);
1304 l_src_data+=sizeof(OPJ_INT16);
1306 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1310 void opj_j2k_read_int32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1312 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1313 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1317 for (i=0;i<p_nb_elem;++i) {
1318 opj_read_bytes(l_src_data,&l_temp,4);
1320 l_src_data+=sizeof(OPJ_INT32);
1322 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1326 void opj_j2k_read_float32_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1328 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1329 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1333 for (i=0;i<p_nb_elem;++i) {
1334 opj_read_float(l_src_data,&l_temp);
1336 l_src_data+=sizeof(OPJ_FLOAT32);
1338 *(l_dest_data++) = l_temp;
1342 void opj_j2k_read_float64_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1344 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1345 OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
1349 for (i=0;i<p_nb_elem;++i) {
1350 opj_read_double(l_src_data,&l_temp);
1352 l_src_data+=sizeof(OPJ_FLOAT64);
1354 *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
1358 void opj_j2k_read_int16_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1360 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1361 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1365 for (i=0;i<p_nb_elem;++i) {
1366 opj_read_bytes(l_src_data,&l_temp,2);
1368 l_src_data+=sizeof(OPJ_INT16);
1370 *(l_dest_data++) = (OPJ_INT32) l_temp;
1374 void opj_j2k_read_int32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1376 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1377 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1381 for (i=0;i<p_nb_elem;++i) {
1382 opj_read_bytes(l_src_data,&l_temp,4);
1384 l_src_data+=sizeof(OPJ_INT32);
1386 *(l_dest_data++) = (OPJ_INT32) l_temp;
1390 void opj_j2k_read_float32_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1392 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1393 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1397 for (i=0;i<p_nb_elem;++i) {
1398 opj_read_float(l_src_data,&l_temp);
1400 l_src_data+=sizeof(OPJ_FLOAT32);
1402 *(l_dest_data++) = (OPJ_INT32) l_temp;
1406 void opj_j2k_read_float64_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1408 OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
1409 OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
1413 for (i=0;i<p_nb_elem;++i) {
1414 opj_read_double(l_src_data,&l_temp);
1416 l_src_data+=sizeof(OPJ_FLOAT64);
1418 *(l_dest_data++) = (OPJ_INT32) l_temp;
1422 void opj_j2k_write_float_to_int16 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1424 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1425 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1429 for (i=0;i<p_nb_elem;++i) {
1430 l_temp = (OPJ_UINT32) *(l_src_data++);
1432 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT16));
1434 l_dest_data+=sizeof(OPJ_INT16);
1438 void opj_j2k_write_float_to_int32 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1440 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1441 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1445 for (i=0;i<p_nb_elem;++i) {
1446 l_temp = (OPJ_UINT32) *(l_src_data++);
1448 opj_write_bytes(l_dest_data,l_temp,sizeof(OPJ_INT32));
1450 l_dest_data+=sizeof(OPJ_INT32);
1454 void opj_j2k_write_float_to_float (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1456 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1457 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1461 for (i=0;i<p_nb_elem;++i) {
1462 l_temp = (OPJ_FLOAT32) *(l_src_data++);
1464 opj_write_float(l_dest_data,l_temp);
1466 l_dest_data+=sizeof(OPJ_FLOAT32);
1470 void opj_j2k_write_float_to_float64 (const void * p_src_data, void * p_dest_data, OPJ_UINT32 p_nb_elem)
1472 OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
1473 OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
1477 for (i=0;i<p_nb_elem;++i) {
1478 l_temp = (OPJ_FLOAT64) *(l_src_data++);
1480 opj_write_double(l_dest_data,l_temp);
1482 l_dest_data+=sizeof(OPJ_FLOAT64);
1488 * Converts an enum type progression order to string type.
1490 * @param prg_order the progression order to get.
1492 * @return the string representation of the given progression order.
1494 char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
1495 j2k_prog_order_t *po;
1496 for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
1497 if(po->enum_prog == prg_order){
1498 return po->str_prog;
1501 return po->str_prog;
1505 * Checks the progression order changes values. Tells if the poc given as input are valid.
1507 * @param p_pocs the progression order changes.
1508 * @param p_nb_pocs the number of progression order changes.
1509 * @param p_nb_resolutions the number of resolutions.
1510 * @param numcomps the number of components
1511 * @param numlayers the number of layers.
1512 * @param p_manager the user event manager.
1514 * @return true if the pocs are valid.
1516 opj_bool j2k_check_poc_val( const opj_poc_t *p_pocs,
1517 OPJ_UINT32 p_nb_pocs,
1518 OPJ_UINT32 p_nb_resolutions,
1519 OPJ_UINT32 p_num_comps,
1520 OPJ_UINT32 p_num_layers,
1521 opj_event_mgr_t * p_manager)
1523 OPJ_UINT32* packet_array;
1524 OPJ_UINT32 index , resno, compno, layno;
1526 OPJ_UINT32 step_c = 1;
1527 OPJ_UINT32 step_r = p_num_comps * step_c;
1528 OPJ_UINT32 step_l = p_nb_resolutions * step_r;
1529 opj_bool loss = OPJ_FALSE;
1530 OPJ_UINT32 layno0 = 0;
1532 packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers, sizeof(OPJ_UINT32));
1533 if (packet_array == 00) {
1534 opj_event_msg_v2(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
1537 memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
1539 if (p_nb_pocs == 0) {
1543 index = step_r * p_pocs->resno0;
1544 // take each resolution for each poc
1545 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno)
1547 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1549 // take each comp of each resolution for each poc
1550 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1551 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1553 // and finally take each layer of each res of ...
1554 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1555 //index = step_r * resno + step_c * compno + step_l * layno;
1556 packet_array[comp_index] = 1;
1557 comp_index += step_l;
1560 res_index += step_c;
1567 // iterate through all the pocs
1568 for (i = 1; i < p_nb_pocs ; ++i) {
1569 OPJ_UINT32 l_last_layno1 = (p_pocs-1)->layno1 ;
1571 layno0 = (p_pocs->layno1 > l_last_layno1)? l_last_layno1 : 0;
1572 index = step_r * p_pocs->resno0;
1574 // take each resolution for each poc
1575 for (resno = p_pocs->resno0 ; resno < p_pocs->resno1 ; ++resno) {
1576 OPJ_UINT32 res_index = index + p_pocs->compno0 * step_c;
1578 // take each comp of each resolution for each poc
1579 for (compno = p_pocs->compno0 ; compno < p_pocs->compno1 ; ++compno) {
1580 OPJ_UINT32 comp_index = res_index + layno0 * step_l;
1582 // and finally take each layer of each res of ...
1583 for (layno = layno0; layno < p_pocs->layno1 ; ++layno) {
1584 //index = step_r * resno + step_c * compno + step_l * layno;
1585 packet_array[comp_index] = 1;
1586 comp_index += step_l;
1589 res_index += step_c;
1599 for (layno = 0; layno < p_num_layers ; ++layno) {
1600 for (resno = 0; resno < p_nb_resolutions; ++resno) {
1601 for (compno = 0; compno < p_num_comps; ++compno) {
1602 loss |= (packet_array[index]!=1);
1603 //index = step_r * resno + step_c * compno + step_l * layno;
1610 opj_event_msg_v2(p_manager , EVT_ERROR, "Missing packets possible loss of data\n");
1613 opj_free(packet_array);
1618 /* ----------------------------------------------------------------------- */
1619 static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
1622 int tpnum=1,tpend=0;
1623 opj_tcp_t *tcp = &cp->tcps[tileno];
1624 prog = j2k_convert_progression_order(tcp->prg);
1629 if( cp->tp_flag == prog[i] ){
1630 tpend=1;cp->tp_pos=i;
1634 tpnum= tpnum * tcp->pocs[pino].compE;
1637 tpnum= tpnum * tcp->pocs[pino].resE;
1640 tpnum= tpnum * tcp->pocs[pino].prcE;
1643 tpnum= tpnum * tcp->pocs[pino].layE;
1655 * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
1657 * @param cp the coding parameters.
1658 * @param pino the offset of the given poc (i.e. its position in the coding parameter).
1659 * @param tileno the given tile.
1661 * @return the number of tile parts.
1663 OPJ_UINT32 j2k_get_num_tp_v2(opj_cp_v2_t *cp, OPJ_UINT32 pino, OPJ_UINT32 tileno)
1665 const OPJ_CHAR *prog = 00;
1667 OPJ_UINT32 tpnum = 1;
1668 opj_tcp_v2_t *tcp = 00;
1669 opj_poc_t * l_current_poc = 00;
1672 assert(tileno < (cp->tw * cp->th));
1673 assert(pino < (cp->tcps[tileno].numpocs + 1));
1675 /* get the given tile coding parameter */
1676 tcp = &cp->tcps[tileno];
1679 l_current_poc = &(tcp->pocs[pino]);
1680 assert(l_current_poc != 0);
1682 /* get the progression order as a character string */
1683 prog = j2k_convert_progression_order(tcp->prg);
1684 assert(strlen(prog) > 0);
1686 if (cp->m_specific_param.m_enc.m_tp_on == 1) {
1690 /* component wise */
1692 tpnum *= l_current_poc->compE;
1694 /* resolution wise */
1696 tpnum *= l_current_poc->resE;
1700 tpnum *= l_current_poc->prcE;
1704 tpnum *= l_current_poc->layE;
1707 /* whould we split here ? */
1708 if ( cp->m_specific_param.m_enc.m_tp_flag == prog[i] ) {
1709 cp->m_specific_param.m_enc.m_tp_pos=i;
1721 /** mem allocation for TLM marker*/
1722 int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
1723 int pino,tileno,totnum_tp=0;
1725 OPJ_ARG_NOT_USED(img_numcomp);
1727 j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
1728 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
1729 int cur_totnum_tp = 0;
1730 opj_tcp_t *tcp = &cp->tcps[tileno];
1731 for(pino = 0; pino <= tcp->numpocs; pino++) {
1733 opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
1734 if(!pi) { return -1;}
1735 tp_num = j2k_get_num_tp(cp,pino,tileno);
1736 totnum_tp = totnum_tp + tp_num;
1737 cur_totnum_tp = cur_totnum_tp + tp_num;
1738 pi_destroy(pi, cp, tileno);
1740 j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
1742 if (j2k->cstr_info) {
1743 j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
1744 j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1752 * Calculates the total number of tile parts needed by the encoder to
1753 * encode such an image. If not enough memory is available, then the function return false.
1755 * @param p_nb_tiles pointer that will hold the number of tile parts.
1756 * @param cp the coding parameters for the image.
1757 * @param image the image to encode.
1758 * @param p_j2k the p_j2k encoder.
1759 * @param p_manager the user event manager.
1761 * @return true if the function was successful, false else.
1763 opj_bool j2k_calculate_tp_v2( opj_j2k_v2_t *p_j2k,
1765 OPJ_UINT32 * p_nb_tiles,
1767 opj_event_mgr_t * p_manager)
1769 OPJ_UINT32 pino,tileno;
1770 OPJ_UINT32 l_nb_tiles;
1774 assert(p_nb_tiles != 00);
1776 assert(image != 00);
1777 assert(p_j2k != 00);
1778 assert(p_manager != 00);
1780 l_nb_tiles = cp->tw * cp->th;
1785 /* TODO mergeV2: check this part which use cstr_info */
1786 /*if (p_j2k->cstr_info) {
1787 opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
1789 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1790 OPJ_UINT32 cur_totnum_tp = 0;
1792 pi_update_encoding_parameters(image,cp,tileno);
1794 for (pino = 0; pino <= tcp->numpocs; ++pino)
1796 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1798 *p_nb_tiles = *p_nb_tiles + tp_num;
1800 cur_totnum_tp += tp_num;
1803 tcp->m_nb_tile_parts = cur_totnum_tp;
1805 l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
1806 if (l_info_tile_ptr->tp == 00) {
1810 memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
1812 l_info_tile_ptr->num_tps = cur_totnum_tp;
1819 for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
1820 OPJ_UINT32 cur_totnum_tp = 0;
1822 pi_update_encoding_parameters(image,cp,tileno);
1824 for (pino = 0; pino <= tcp->numpocs; ++pino) {
1825 OPJ_UINT32 tp_num = j2k_get_num_tp_v2(cp,pino,tileno);
1827 *p_nb_tiles = *p_nb_tiles + tp_num;
1829 cur_totnum_tp += tp_num;
1831 tcp->m_nb_tile_parts = cur_totnum_tp;
1841 * Writes the SOC marker (Start Of Codestream)
1843 * @param p_stream the stream to write data to.
1844 * @param p_j2k J2K codec.
1845 * @param p_manager the user event manager.
1847 opj_bool opj_j2k_write_soc( opj_j2k_v2_t *p_j2k,
1848 opj_stream_private_t *p_stream,
1849 opj_event_mgr_t * p_manager )
1851 /* 2 bytes will be written */
1852 OPJ_BYTE * l_start_stream = 00;
1855 assert(p_stream != 00);
1856 assert(p_j2k != 00);
1857 assert(p_manager != 00);
1859 l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1861 /* write SOC identifier */
1862 opj_write_bytes(l_start_stream,J2K_MS_SOC,2);
1864 if (opj_stream_write_data(p_stream,l_start_stream,2,p_manager) != 2) {
1870 /* update markers struct */
1872 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
1874 assert( 0 && "TODO" );
1875 #endif /* USE_JPWL */
1884 * Reads a SOC marker (Start of Codestream)
1885 * @param p_header_data the data contained in the SOC box.
1886 * @param jp2 the jpeg2000 file codec.
1887 * @param p_header_size the size of the data contained in the SOC marker.
1888 * @param p_manager the user event manager.
1890 static opj_bool opj_j2k_read_soc( opj_j2k_v2_t *p_j2k,
1891 opj_stream_private_t *p_stream,
1892 opj_event_mgr_t * p_manager
1895 OPJ_BYTE l_data [2];
1896 OPJ_UINT32 l_marker;
1899 assert(p_j2k != 00);
1900 assert(p_manager != 00);
1901 assert(p_stream != 00);
1903 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
1907 opj_read_bytes(l_data,&l_marker,2);
1908 if (l_marker != J2K_MS_SOC) {
1912 /* Next marker should be a SIZ marker in the main header */
1913 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
1915 /* FIXME move it in a index structure included in p_j2k*/
1916 p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
1918 opj_event_msg_v2(p_manager, EVT_INFO, "Start to read j2k main header (%d).\n", p_j2k->cstr_index->main_head_start);
1920 /* Add the marker to the codestream index*/
1921 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_SOC, p_j2k->cstr_index->main_head_start, 2);
1928 * Writes the SIZ marker (image and tile size)
1930 * @param p_stream the stream to write data to.
1931 * @param p_j2k J2K codec.
1932 * @param p_manager the user event manager.
1934 opj_bool opj_j2k_write_siz( opj_j2k_v2_t *p_j2k,
1935 opj_stream_private_t *p_stream,
1936 opj_event_mgr_t * p_manager )
1939 OPJ_UINT32 l_size_len;
1940 OPJ_BYTE * l_current_ptr;
1941 opj_image_t * l_image = 00;
1942 opj_cp_v2_t *cp = 00;
1943 opj_image_comp_t * l_img_comp = 00;
1946 assert(p_stream != 00);
1947 assert(p_j2k != 00);
1948 assert(p_manager != 00);
1950 l_image = p_j2k->m_private_image;
1951 cp = &(p_j2k->m_cp);
1952 l_size_len = 40 + 3 * l_image->numcomps;
1953 l_img_comp = l_image->comps;
1955 if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
1957 p_j2k->m_specific_param.m_encoder.m_header_tile_data
1958 = (OPJ_BYTE*)opj_realloc(
1959 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
1961 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
1965 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
1968 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1970 /* write SOC identifier */
1971 opj_write_bytes(l_current_ptr,J2K_MS_SIZ,2); /* SIZ */
1974 opj_write_bytes(l_current_ptr,l_size_len-2,2); /* L_SIZ */
1977 opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
1980 opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
1983 opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
1986 opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
1989 opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
1992 opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
1995 opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
1998 opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
2001 opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
2004 opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
2007 for (i = 0; i < l_image->numcomps; ++i) {
2008 /* TODO here with MCT ? */
2009 opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7), 1); /* Ssiz_i */
2012 opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
2015 opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
2021 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) {
2031 * Reads a SIZ marker (image and tile size)
2032 * @param p_header_data the data contained in the SIZ box.
2033 * @param jp2 the jpeg2000 file codec.
2034 * @param p_header_size the size of the data contained in the SIZ marker.
2035 * @param p_manager the user event manager.
2037 static opj_bool opj_j2k_read_siz(opj_j2k_v2_t *p_j2k,
2038 OPJ_BYTE * p_header_data,
2039 OPJ_UINT32 p_header_size,
2040 opj_event_mgr_t * p_manager
2043 OPJ_UINT32 l_size, i;
2044 OPJ_UINT32 l_nb_comp;
2045 OPJ_UINT32 l_nb_comp_remain;
2046 OPJ_UINT32 l_remaining_size;
2047 OPJ_UINT32 l_nb_tiles;
2049 opj_image_t *l_image = 00;
2050 opj_cp_v2_t *l_cp = 00;
2051 opj_image_comp_t * l_img_comp = 00;
2052 opj_tcp_v2_t * l_current_tile_param = 00;
2055 assert(p_j2k != 00);
2056 assert(p_manager != 00);
2057 assert(p_header_data != 00);
2059 l_image = p_j2k->m_private_image;
2060 l_cp = &(p_j2k->m_cp);
2062 /* minimum size == 39 - 3 (= minimum component parameter) */
2063 if (p_header_size < 36) {
2064 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2068 l_remaining_size = p_header_size - 36;
2069 l_nb_comp = l_remaining_size / 3;
2070 l_nb_comp_remain = l_remaining_size % 3;
2071 if (l_nb_comp_remain != 0){
2072 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
2076 l_size = p_header_size + 2; /* Lsiz */
2078 opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
2080 l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
2081 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
2083 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
2085 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
2087 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
2089 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx, 4); /* XTsiz */
2091 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy, 4); /* YTsiz */
2093 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0, 4); /* XT0siz */
2095 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0, 4); /* YT0siz */
2097 opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp, 2); /* Csiz */
2100 l_image->numcomps = (OPJ_UINT16) l_tmp;
2102 opj_event_msg_v2(p_manager, EVT_ERROR, "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
2106 if (l_image->numcomps != l_nb_comp) {
2107 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);
2112 if (l_cp->correct) {
2113 /* if JPWL is on, we check whether TX errors have damaged
2114 too much the SIZ parameters */
2115 if (!(l_image->x1 * l_image->y1)) {
2116 opj_event_msg_v2(p_manager, EVT_ERROR,
2117 "JPWL: bad image size (%d x %d)\n",
2118 l_image->x1, l_image->y1);
2119 if (!JPWL_ASSUME || JPWL_ASSUME) {
2120 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2125 /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
2126 if (l_image->numcomps != ((len - 38) / 3)) {
2127 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2128 "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
2129 l_image->numcomps, ((len - 38) / 3));
2131 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2134 */ /* we try to correct */
2135 /* opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n");
2136 if (l_image->numcomps < ((len - 38) / 3)) {
2137 len = 38 + 3 * l_image->numcomps;
2138 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
2141 l_image->numcomps = ((len - 38) / 3);
2142 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
2148 /* update components number in the jpwl_exp_comps filed */
2149 l_cp->exp_comps = l_image->numcomps;
2151 #endif /* USE_JPWL */
2153 /* Allocate the resulting image components */
2154 l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps, sizeof(opj_image_comp_t));
2155 if (l_image->comps == 00){
2156 l_image->numcomps = 0;
2157 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2161 memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
2162 l_img_comp = l_image->comps;
2164 /* Read the component information */
2165 for (i = 0; i < l_image->numcomps; ++i){
2167 opj_read_bytes(p_header_data,&tmp,1); /* Ssiz_i */
2169 l_img_comp->prec = (tmp & 0x7f) + 1;
2170 l_img_comp->sgnd = tmp >> 7;
2171 opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
2173 l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2174 opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
2176 l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
2179 if (l_cp->correct) {
2180 /* if JPWL is on, we check whether TX errors have damaged
2181 too much the SIZ parameters, again */
2182 if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
2183 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2184 "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
2185 i, i, l_image->comps[i].dx, l_image->comps[i].dy);
2187 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2190 /* we try to correct */
2191 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2192 if (!l_image->comps[i].dx) {
2193 l_image->comps[i].dx = 1;
2194 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
2195 i, l_image->comps[i].dx);
2197 if (!l_image->comps[i].dy) {
2198 l_image->comps[i].dy = 1;
2199 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
2200 i, l_image->comps[i].dy);
2204 #endif /* USE_JPWL */
2205 l_img_comp->resno_decoded = 0; /* number of resolution decoded */
2206 l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
2210 /* Compute the number of tiles */
2211 l_cp->tw = int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
2212 l_cp->th = int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
2213 l_nb_tiles = l_cp->tw * l_cp->th;
2215 /* Define the tiles which will be decoded */
2216 if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
2217 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;
2218 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;
2219 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);
2220 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);
2223 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
2224 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
2225 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
2226 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
2230 if (l_cp->correct) {
2231 /* if JPWL is on, we check whether TX errors have damaged
2232 too much the SIZ parameters */
2233 if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) || (l_cp->th > l_cp->max_tiles)) {
2234 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2235 "JPWL: bad number of tiles (%d x %d)\n",
2236 l_cp->tw, l_cp->th);
2238 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2241 /* we try to correct */
2242 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n");
2245 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
2248 if (l_cp->tw > l_cp->max_tiles) {
2250 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large x, increase expectance of %d\n"
2251 "- setting %d tiles in x => HYPOTHESIS!!!\n",
2252 l_cp->max_tiles, l_cp->tw);
2256 opj_event_msg_v2(p_manager, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
2259 if (l_cp->th > l_cp->max_tiles) {
2261 opj_event_msg_v2(p_manager, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
2262 "- setting %d tiles in y => HYPOTHESIS!!!\n",
2263 l_cp->max_tiles, l_cp->th);
2267 #endif /* USE_JPWL */
2269 /* memory allocations */
2270 l_cp->tcps = (opj_tcp_v2_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_v2_t));
2271 if (l_cp->tcps == 00) {
2272 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2275 memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
2278 if (l_cp->correct) {
2280 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
2281 "JPWL: could not alloc tcps field of cp\n");
2282 if (!JPWL_ASSUME || JPWL_ASSUME) {
2283 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
2288 #endif /* USE_JPWL */
2290 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
2291 (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
2292 if(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
2293 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2296 memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
2298 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
2299 (opj_mct_data_t*)opj_malloc(J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
2301 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
2302 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2305 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));
2306 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = J2K_MCT_DEFAULT_NB_RECORDS;
2308 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
2309 (opj_simple_mcc_decorrelation_data_t*)
2310 opj_malloc(J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
2312 if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
2313 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2316 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));
2317 p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = J2K_MCC_DEFAULT_NB_RECORDS;
2319 /* set up default dc level shift */
2320 for (i=0;i<l_image->numcomps;++i) {
2321 if (! l_image->comps[i].sgnd) {
2322 p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1 << (l_image->comps[i].prec - 1);
2326 l_current_tile_param = l_cp->tcps;
2327 for (i = 0; i < l_nb_tiles; ++i) {
2328 l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
2329 if (l_current_tile_param->tccps == 00) {
2330 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
2333 memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
2335 ++l_current_tile_param;
2338 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH; /* FIXME J2K_DEC_STATE_MH; */
2339 opj_image_comp_header_update(l_image,l_cp);
2345 * Writes the COM marker (comment)
2347 * @param p_stream the stream to write data to.
2348 * @param p_j2k J2K codec.
2349 * @param p_manager the user event manager.
2351 opj_bool opj_j2k_write_com( opj_j2k_v2_t *p_j2k,
2352 opj_stream_private_t *p_stream,
2353 opj_event_mgr_t * p_manager
2356 OPJ_UINT32 l_comment_size;
2357 OPJ_UINT32 l_total_com_size;
2358 const OPJ_CHAR *l_comment;
2359 OPJ_BYTE * l_current_ptr = 00;
2362 assert(p_j2k != 00);
2363 assert(p_stream != 00);
2364 assert(p_manager != 00);
2366 l_comment = p_j2k->m_cp.comment;
2367 l_comment_size = strlen(l_comment);
2368 l_total_com_size = l_comment_size + 6;
2370 if (l_total_com_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2371 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2372 = (OPJ_BYTE*)opj_realloc( p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2375 if(! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2379 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
2382 l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2384 opj_write_bytes(l_current_ptr,J2K_MS_COM , 2); /* COM */
2387 opj_write_bytes(l_current_ptr,l_total_com_size - 2 , 2); /* L_COM */
2390 opj_write_bytes(l_current_ptr,1 , 2); /* General use (IS 8859-15:1999 (Latin) values) */
2393 memcpy( l_current_ptr,l_comment,l_comment_size);
2395 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) {
2403 * Reads a COM marker (comments)
2404 * @param p_header_data the data contained in the COM box.
2405 * @param jp2 the jpeg2000 file codec.
2406 * @param p_header_size the size of the data contained in the COM marker.
2407 * @param p_manager the user event manager.
2409 static opj_bool opj_j2k_read_com ( opj_j2k_v2_t *p_j2k,
2410 OPJ_BYTE * p_header_data,
2411 OPJ_UINT32 p_header_size,
2412 opj_event_mgr_t * p_manager
2416 assert(p_j2k != 00);
2417 assert(p_manager != 00);
2418 assert(p_header_data != 00);
2419 (void)p_header_size;
2425 * Writes the COD marker (Coding style default)
2427 * @param p_stream the stream to write data to.
2428 * @param p_j2k J2K codec.
2429 * @param p_manager the user event manager.
2431 opj_bool opj_j2k_write_cod( opj_j2k_v2_t *p_j2k,
2432 opj_stream_private_t *p_stream,
2433 opj_event_mgr_t * p_manager )
2435 opj_cp_v2_t *l_cp = 00;
2436 opj_tcp_v2_t *l_tcp = 00;
2437 OPJ_UINT32 l_code_size,l_remaining_size;
2438 OPJ_BYTE * l_current_data = 00;
2441 assert(p_j2k != 00);
2442 assert(p_manager != 00);
2443 assert(p_stream != 00);
2445 l_cp = &(p_j2k->m_cp);
2446 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2447 l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,0);
2448 l_remaining_size = l_code_size;
2450 if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2451 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2452 = (OPJ_BYTE*)opj_realloc(
2453 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2456 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2460 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
2463 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2465 opj_write_bytes(l_current_data,J2K_MS_COD,2); /* COD */
2466 l_current_data += 2;
2468 opj_write_bytes(l_current_data,l_code_size-2,2); /* L_COD */
2469 l_current_data += 2;
2471 opj_write_bytes(l_current_data,l_tcp->csty,1); /* Scod */
2474 opj_write_bytes(l_current_data,l_tcp->prg,1); /* SGcod (A) */
2477 opj_write_bytes(l_current_data,l_tcp->numlayers,2); /* SGcod (B) */
2480 opj_write_bytes(l_current_data,l_tcp->mct,1); /* SGcod (C) */
2483 l_remaining_size -= 9;
2485 if (! opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2486 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2490 if (l_remaining_size != 0) {
2491 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting COD marker\n");
2495 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) {
2504 * Reads a COD marker (Coding Styke defaults)
2505 * @param p_header_data the data contained in the COD box.
2506 * @param p_j2k the jpeg2000 codec.
2507 * @param p_header_size the size of the data contained in the COD marker.
2508 * @param p_manager the user event manager.
2510 static opj_bool opj_j2k_read_cod ( opj_j2k_v2_t *p_j2k,
2511 OPJ_BYTE * p_header_data,
2512 OPJ_UINT32 p_header_size,
2513 opj_event_mgr_t * p_manager
2519 opj_cp_v2_t *l_cp = 00;
2520 opj_tcp_v2_t *l_tcp = 00;
2521 opj_image_t *l_image = 00;
2524 assert(p_header_data != 00);
2525 assert(p_j2k != 00);
2526 assert(p_manager != 00);
2528 l_image = p_j2k->m_private_image;
2529 l_cp = &(p_j2k->m_cp);
2531 /* If we are in the first tile-part header of the current tile */
2532 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
2533 &l_cp->tcps[p_j2k->m_current_tile_number] :
2534 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2536 /* Make sure room is sufficient */
2537 if (p_header_size < 5) {
2538 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2542 opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
2544 opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
2546 l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
2547 opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
2550 /* If user didn't set a number layer to decode take the max specify in the codestream. */
2551 if (l_cp->m_specific_param.m_dec.m_layer) {
2552 l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
2555 l_tcp->num_layers_to_decode = l_tcp->numlayers;
2558 opj_read_bytes(p_header_data,&l_tcp->mct,1); /* SGcod (C) */
2562 for (i = 0; i < l_image->numcomps; ++i) {
2563 l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
2566 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2567 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2571 if (p_header_size != 0) {
2572 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COD marker\n");
2576 /* Apply the coding style to other components of the current tile or the m_default_tcp*/
2577 opj_j2k_copy_tile_component_parameters(p_j2k);
2580 #ifdef WIP_REMOVE_MSD
2581 if (p_j2k->cstr_info) {
2582 /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
2583 p_j2k->cstr_info->prog = l_tcp->prg;
2584 p_j2k->cstr_info->numlayers = l_tcp->numlayers;
2585 p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(l_image->numcomps * sizeof(OPJ_UINT32));
2586 for (i = 0; i < l_image->numcomps; ++i) {
2587 p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
2596 * Writes the COC marker (Coding style component)
2598 * @param p_comp_no the index of the component to output.
2599 * @param p_stream the stream to write data to.
2600 * @param p_j2k J2K codec.
2601 * @param p_manager the user event manager.
2603 opj_bool opj_j2k_write_coc( opj_j2k_v2_t *p_j2k,
2604 OPJ_UINT32 p_comp_no,
2605 opj_stream_private_t *p_stream,
2606 opj_event_mgr_t * p_manager )
2608 OPJ_UINT32 l_coc_size,l_remaining_size;
2609 OPJ_UINT32 l_comp_room;
2612 assert(p_j2k != 00);
2613 assert(p_manager != 00);
2614 assert(p_stream != 00);
2616 l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
2618 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2620 if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2621 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2622 = (OPJ_BYTE*)opj_realloc(
2623 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2625 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2629 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
2632 opj_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);
2634 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) {
2642 * Writes the COC marker (Coding style component)
2644 * @param p_comp_no the index of the component to output.
2645 * @param p_stream the stream to write data to.
2646 * @param p_j2k J2K codec.
2647 * @param p_manager the user event manager.
2649 void opj_j2k_write_coc_in_memory( opj_j2k_v2_t *p_j2k,
2650 OPJ_UINT32 p_comp_no,
2652 OPJ_UINT32 * p_data_written,
2653 opj_event_mgr_t * p_manager
2656 opj_cp_v2_t *l_cp = 00;
2657 opj_tcp_v2_t *l_tcp = 00;
2658 OPJ_UINT32 l_coc_size,l_remaining_size;
2659 OPJ_BYTE * l_current_data = 00;
2660 opj_image_t *l_image = 00;
2661 OPJ_UINT32 l_comp_room;
2664 assert(p_j2k != 00);
2665 assert(p_manager != 00);
2667 l_cp = &(p_j2k->m_cp);
2668 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2669 l_image = p_j2k->m_private_image;
2670 l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
2672 l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2673 l_remaining_size = l_coc_size;
2675 l_current_data = p_data;
2677 opj_write_bytes(l_current_data,J2K_MS_COC,2); /* COC */
2678 l_current_data += 2;
2680 opj_write_bytes(l_current_data,l_coc_size-2,2); /* L_COC */
2681 l_current_data += 2;
2683 opj_write_bytes(l_current_data,p_comp_no, l_comp_room); /* Ccoc */
2684 l_current_data+=l_comp_room;
2686 opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty, 1); /* Scoc */
2689 l_remaining_size -= (5 + l_comp_room);
2690 opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
2691 * p_data_written = l_coc_size;
2695 * Gets the maximum size taken by a coc.
2697 * @param p_j2k the jpeg2000 codec to use.
2699 OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_v2_t *p_j2k)
2702 OPJ_UINT32 l_nb_comp;
2703 OPJ_UINT32 l_nb_tiles;
2704 OPJ_UINT32 l_max = 0;
2708 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
2709 l_nb_comp = p_j2k->m_private_image->numcomps;
2711 for (i=0;i<l_nb_tiles;++i) {
2712 for (j=0;j<l_nb_comp;++j) {
2713 l_max = uint_max(l_max,opj_j2k_get_SPCod_SPCoc_size(p_j2k,i,j));
2722 * Reads a COC marker (Coding Style Component)
2723 * @param p_header_data the data contained in the COC box.
2724 * @param p_j2k the jpeg2000 codec.
2725 * @param p_header_size the size of the data contained in the COC marker.
2726 * @param p_manager the user event manager.
2728 static opj_bool opj_j2k_read_coc ( opj_j2k_v2_t *p_j2k,
2729 OPJ_BYTE * p_header_data,
2730 OPJ_UINT32 p_header_size,
2731 opj_event_mgr_t * p_manager
2734 opj_cp_v2_t *l_cp = NULL;
2735 opj_tcp_v2_t *l_tcp = NULL;
2736 opj_image_t *l_image = NULL;
2737 OPJ_UINT32 l_comp_room;
2738 OPJ_UINT32 l_comp_no;
2741 assert(p_header_data != 00);
2742 assert(p_j2k != 00);
2743 assert(p_manager != 00);
2745 l_cp = &(p_j2k->m_cp);
2746 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ) ? /*FIXME J2K_DEC_STATE_TPH*/
2747 &l_cp->tcps[p_j2k->m_current_tile_number] :
2748 p_j2k->m_specific_param.m_decoder.m_default_tcp;
2749 l_image = p_j2k->m_private_image;
2751 l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
2753 /* make sure room is sufficient*/
2754 if (p_header_size < l_comp_room + 1) {
2755 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2758 p_header_size -= l_comp_room + 1;
2760 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Ccoc */
2761 p_header_data += l_comp_room;
2762 if (l_comp_no >= l_image->numcomps) {
2763 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker (bad number of components)\n");
2767 opj_read_bytes(p_header_data,&l_tcp->tccps[l_comp_no].csty,1); /* Scoc */
2770 if (! opj_j2k_read_SPCod_SPCoc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
2771 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2775 if (p_header_size != 0) {
2776 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading COC marker\n");
2783 * Writes the QCD marker (quantization default)
2785 * @param p_comp_number the index of the component to output.
2786 * @param p_stream the stream to write data to.
2787 * @param p_j2k J2K codec.
2788 * @param p_manager the user event manager.
2790 opj_bool opj_j2k_write_qcd( opj_j2k_v2_t *p_j2k,
2791 opj_stream_private_t *p_stream,
2792 opj_event_mgr_t * p_manager
2795 opj_cp_v2_t *l_cp = 00;
2796 opj_tcp_v2_t *l_tcp = 00;
2797 OPJ_UINT32 l_qcd_size,l_remaining_size;
2798 OPJ_BYTE * l_current_data = 00;
2801 assert(p_j2k != 00);
2802 assert(p_manager != 00);
2803 assert(p_stream != 00);
2805 l_cp = &(p_j2k->m_cp);
2806 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
2807 l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,0);
2808 l_remaining_size = l_qcd_size;
2810 if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2811 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2812 = (OPJ_BYTE*)opj_realloc(
2813 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2816 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2820 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
2823 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
2825 opj_write_bytes(l_current_data,J2K_MS_QCD,2); /* QCD */
2826 l_current_data += 2;
2828 opj_write_bytes(l_current_data,l_qcd_size-2,2); /* L_QCD */
2829 l_current_data += 2;
2831 l_remaining_size -= 4;
2833 if (! opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager)) {
2834 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2838 if (l_remaining_size != 0) {
2839 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting QCD marker\n");
2843 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) {
2851 * Reads a QCD marker (Quantization defaults)
2852 * @param p_header_data the data contained in the QCD box.
2853 * @param p_j2k the jpeg2000 codec.
2854 * @param p_header_size the size of the data contained in the QCD marker.
2855 * @param p_manager the user event manager.
2857 static opj_bool opj_j2k_read_qcd ( opj_j2k_v2_t *p_j2k,
2858 OPJ_BYTE * p_header_data,
2859 OPJ_UINT32 p_header_size,
2860 opj_event_mgr_t * p_manager
2864 assert(p_header_data != 00);
2865 assert(p_j2k != 00);
2866 assert(p_manager != 00);
2868 if (! opj_j2k_read_SQcd_SQcc(p_j2k,0,p_header_data,&p_header_size,p_manager)) {
2869 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2873 if (p_header_size != 0) {
2874 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCD marker\n");
2878 /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
2879 opj_j2k_copy_tile_quantization_parameters(p_j2k);
2885 * Writes the QCC marker (quantization component)
2887 * @param p_comp_no the index of the component to output.
2888 * @param p_stream the stream to write data to.
2889 * @param p_j2k J2K codec.
2890 * @param p_manager the user event manager.
2892 opj_bool opj_j2k_write_qcc( opj_j2k_v2_t *p_j2k,
2893 OPJ_UINT32 p_comp_no,
2894 opj_stream_private_t *p_stream,
2895 opj_event_mgr_t * p_manager
2898 OPJ_UINT32 l_qcc_size,l_remaining_size;
2901 assert(p_j2k != 00);
2902 assert(p_manager != 00);
2903 assert(p_stream != 00);
2905 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2906 l_remaining_size = l_qcc_size;
2908 if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
2909 p_j2k->m_specific_param.m_encoder.m_header_tile_data
2910 = (OPJ_BYTE*)opj_realloc(
2911 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
2913 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
2917 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
2920 opj_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);
2922 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) {
2930 * Writes the QCC marker (quantization component)
2932 * @param p_comp_no the index of the component to output.
2933 * @param p_stream the stream to write data to.
2934 * @param p_j2k J2K codec.
2935 * @param p_manager the user event manager.
2937 void opj_j2k_write_qcc_in_memory( opj_j2k_v2_t *p_j2k,
2938 OPJ_UINT32 p_comp_no,
2940 OPJ_UINT32 * p_data_written,
2941 opj_event_mgr_t * p_manager
2944 OPJ_UINT32 l_qcc_size,l_remaining_size;
2945 OPJ_BYTE * l_current_data = 00;
2948 assert(p_j2k != 00);
2949 assert(p_manager != 00);
2951 l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k,p_j2k->m_current_tile_number,p_comp_no);
2952 l_remaining_size = l_qcc_size;
2954 l_current_data = p_data;
2956 opj_write_bytes(l_current_data,J2K_MS_QCC,2); /* QCC */
2957 l_current_data += 2;
2959 if (p_j2k->m_private_image->numcomps <= 256) {
2962 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2963 l_current_data += 2;
2965 opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
2968 /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
2969 l_remaining_size -= 6;
2972 opj_write_bytes(l_current_data,l_qcc_size-2,2); /* L_QCC */
2973 l_current_data += 2;
2975 opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
2978 l_remaining_size -= 6;
2981 opj_j2k_write_SQcd_SQcc(p_j2k,p_j2k->m_current_tile_number,p_comp_no,l_current_data,&l_remaining_size,p_manager);
2983 *p_data_written = l_qcc_size;
2987 * Gets the maximum size taken by a qcc.
2989 OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_v2_t *p_j2k)
2991 return opj_j2k_get_max_coc_size(p_j2k);
2995 * Reads a QCC marker (Quantization component)
2996 * @param p_header_data the data contained in the QCC box.
2997 * @param p_j2k the jpeg2000 codec.
2998 * @param p_header_size the size of the data contained in the QCC marker.
2999 * @param p_manager the user event manager.
3001 static opj_bool opj_j2k_read_qcc( opj_j2k_v2_t *p_j2k,
3002 OPJ_BYTE * p_header_data,
3003 OPJ_UINT32 p_header_size,
3004 opj_event_mgr_t * p_manager
3007 OPJ_UINT32 l_num_comp,l_comp_no;
3010 assert(p_header_data != 00);
3011 assert(p_j2k != 00);
3012 assert(p_manager != 00);
3014 l_num_comp = p_j2k->m_private_image->numcomps;
3016 if (l_num_comp <= 256) {
3017 if (p_header_size < 1) {
3018 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3021 opj_read_bytes(p_header_data,&l_comp_no,1);
3026 if (p_header_size < 2) {
3027 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3030 opj_read_bytes(p_header_data,&l_comp_no,2);
3036 if (p_j2k->m_cp.correct) {
3038 static OPJ_UINT32 backup_compno = 0;
3040 /* compno is negative or larger than the number of components!!! */
3041 if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
3042 opj_event_msg_v2(p_manager, EVT_ERROR,
3043 "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
3044 l_comp_no, l_num_comp);
3046 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
3049 /* we try to correct */
3050 l_comp_no = backup_compno % l_num_comp;
3051 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
3052 "- setting component number to %d\n",
3056 /* keep your private count of tiles */
3059 #endif /* USE_JPWL */
3061 if (! opj_j2k_read_SQcd_SQcc(p_j2k,l_comp_no,p_header_data,&p_header_size,p_manager)) {
3062 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3066 if (p_header_size != 0) {
3067 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading QCC marker\n");
3075 * Writes the POC marker (Progression Order Change)
3077 * @param p_stream the stream to write data to.
3078 * @param p_j2k J2K codec.
3079 * @param p_manager the user event manager.
3081 opj_bool opj_j2k_write_poc( opj_j2k_v2_t *p_j2k,
3082 opj_stream_private_t *p_stream,
3083 opj_event_mgr_t * p_manager
3086 OPJ_UINT32 l_nb_comp;
3087 OPJ_UINT32 l_nb_poc;
3088 OPJ_UINT32 l_poc_size;
3089 OPJ_UINT32 l_written_size = 0;
3090 opj_tcp_v2_t *l_tcp = 00;
3091 opj_tccp_t *l_tccp = 00;
3092 OPJ_UINT32 l_poc_room;
3095 assert(p_j2k != 00);
3096 assert(p_manager != 00);
3097 assert(p_stream != 00);
3099 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3100 l_tccp = &l_tcp->tccps[0];
3101 l_nb_comp = p_j2k->m_private_image->numcomps;
3102 l_nb_poc = 1 + l_tcp->numpocs;
3104 if (l_nb_comp <= 256) {
3110 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3112 if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3113 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3114 = (OPJ_BYTE*)opj_realloc(
3115 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3118 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
3122 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
3125 opj_j2k_write_poc_in_memory(p_j2k,p_j2k->m_specific_param.m_encoder.m_header_tile_data,&l_written_size,p_manager);
3127 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) {
3136 * Writes the POC marker (Progression Order Change)
3138 * @param p_stream the stream to write data to.
3139 * @param p_j2k J2K codec.
3140 * @param p_manager the user event manager.
3142 void opj_j2k_write_poc_in_memory( opj_j2k_v2_t *p_j2k,
3144 OPJ_UINT32 * p_data_written,
3145 opj_event_mgr_t * p_manager
3149 OPJ_BYTE * l_current_data = 00;
3150 OPJ_UINT32 l_nb_comp;
3151 OPJ_UINT32 l_nb_poc;
3152 OPJ_UINT32 l_poc_size;
3153 opj_image_t *l_image = 00;
3154 opj_tcp_v2_t *l_tcp = 00;
3155 opj_tccp_t *l_tccp = 00;
3156 opj_poc_t *l_current_poc = 00;
3157 OPJ_UINT32 l_poc_room;
3160 assert(p_j2k != 00);
3161 assert(p_manager != 00);
3163 l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
3164 l_tccp = &l_tcp->tccps[0];
3165 l_image = p_j2k->m_private_image;
3166 l_nb_comp = l_image->numcomps;
3167 l_nb_poc = 1 + l_tcp->numpocs;
3169 if (l_nb_comp <= 256) {
3176 l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
3178 l_current_data = p_data;
3180 opj_write_bytes(l_current_data,J2K_MS_POC,2); /* POC */
3181 l_current_data += 2;
3183 opj_write_bytes(l_current_data,l_poc_size-2,2); /* Lpoc */
3184 l_current_data += 2;
3186 l_current_poc = l_tcp->pocs;
3187 for (i = 0; i < l_nb_poc; ++i) {
3188 opj_write_bytes(l_current_data,l_current_poc->resno0,1); /* RSpoc_i */
3191 opj_write_bytes(l_current_data,l_current_poc->compno0,l_poc_room); /* CSpoc_i */
3192 l_current_data+=l_poc_room;
3194 opj_write_bytes(l_current_data,l_current_poc->layno1,2); /* LYEpoc_i */
3197 opj_write_bytes(l_current_data,l_current_poc->resno1,1); /* REpoc_i */
3200 opj_write_bytes(l_current_data,l_current_poc->compno1,l_poc_room); /* CEpoc_i */
3201 l_current_data+=l_poc_room;
3203 opj_write_bytes(l_current_data,l_current_poc->prg,1); /* Ppoc_i */
3206 /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
3207 l_current_poc->layno1 = int_min(l_current_poc->layno1, l_tcp->numlayers);
3208 l_current_poc->resno1 = int_min(l_current_poc->resno1, l_tccp->numresolutions);
3209 l_current_poc->compno1 = int_min(l_current_poc->compno1, l_nb_comp);
3214 *p_data_written = l_poc_size;
3218 * Gets the maximum size taken by the writing of a POC.
3220 OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_v2_t *p_j2k)
3222 opj_tcp_v2_t * l_tcp = 00;
3223 OPJ_UINT32 l_nb_tiles = 0;
3224 OPJ_UINT32 l_max_poc = 0;
3227 l_tcp = p_j2k->m_cp.tcps;
3228 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
3230 for (i=0;i<l_nb_tiles;++i) {
3231 l_max_poc = uint_max(l_max_poc,l_tcp->numpocs);
3237 return 4 + 9 * l_max_poc;
3241 * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
3243 OPJ_UINT32 opj_j2k_get_max_toc_size (opj_j2k_v2_t *p_j2k)
3246 OPJ_UINT32 l_nb_tiles;
3247 OPJ_UINT32 l_max = 0;
3248 opj_tcp_v2_t * l_tcp = 00;
3250 l_tcp = p_j2k->m_cp.tcps;
3251 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
3253 for (i=0;i<l_nb_tiles;++i) {
3254 l_max = uint_max(l_max,l_tcp->m_nb_tile_parts);
3264 * Gets the maximum size taken by the headers of the SOT.
3266 * @param p_j2k the jpeg2000 codec to use.
3268 OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_v2_t *p_j2k)
3270 OPJ_UINT32 l_nb_bytes = 0;
3271 OPJ_UINT32 l_nb_comps;
3272 OPJ_UINT32 l_coc_bytes,l_qcc_bytes;
3274 l_nb_comps = p_j2k->m_private_image->numcomps - 1;
3275 l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
3277 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
3278 l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
3279 l_nb_bytes += l_nb_comps * l_coc_bytes;
3281 l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
3282 l_nb_bytes += l_nb_comps * l_qcc_bytes;
3285 l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
3287 /*** DEVELOPER CORNER, Add room for your headers ***/
3294 * Reads a POC marker (Progression Order Change)
3296 * @param p_header_data the data contained in the POC box.
3297 * @param p_j2k the jpeg2000 codec.
3298 * @param p_header_size the size of the data contained in the POC marker.
3299 * @param p_manager the user event manager.
3301 static opj_bool opj_j2k_read_poc ( opj_j2k_v2_t *p_j2k,
3302 OPJ_BYTE * p_header_data,
3303 OPJ_UINT32 p_header_size,
3304 opj_event_mgr_t * p_manager
3307 OPJ_UINT32 i, l_nb_comp, l_tmp;
3308 opj_image_t * l_image = 00;
3309 OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
3310 OPJ_UINT32 l_chunk_size, l_comp_room;
3312 opj_cp_v2_t *l_cp = 00;
3313 opj_tcp_v2_t *l_tcp = 00;
3314 opj_poc_t *l_current_poc = 00;
3317 assert(p_header_data != 00);
3318 assert(p_j2k != 00);
3319 assert(p_manager != 00);
3321 l_image = p_j2k->m_private_image;
3322 l_nb_comp = l_image->numcomps;
3323 if (l_nb_comp <= 256) {
3329 l_chunk_size = 5 + 2 * l_comp_room;
3330 l_current_poc_nb = p_header_size / l_chunk_size;
3331 l_current_poc_remaining = p_header_size % l_chunk_size;
3333 if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
3334 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading POC marker\n");
3338 l_cp = &(p_j2k->m_cp);
3339 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
3340 &l_cp->tcps[p_j2k->m_current_tile_number] :
3341 p_j2k->m_specific_param.m_decoder.m_default_tcp;
3342 l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
3343 l_current_poc_nb += l_old_poc_nb;
3345 assert(l_current_poc_nb < 32);
3347 /* now poc is in use.*/
3350 l_current_poc = &l_tcp->pocs[l_old_poc_nb];
3351 for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
3352 opj_read_bytes(p_header_data,&(l_current_poc->resno0),1); /* RSpoc_i */
3354 opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
3355 p_header_data+=l_comp_room;
3356 opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
3358 opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
3360 opj_read_bytes(p_header_data,&(l_current_poc->compno1),l_comp_room); /* CEpoc_i */
3361 p_header_data+=l_comp_room;
3362 opj_read_bytes(p_header_data,&l_tmp,1); /* Ppoc_i */
3364 l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
3365 /* make sure comp is in acceptable bounds */
3366 l_current_poc->compno1 = uint_min(l_current_poc->compno1, l_nb_comp);
3370 l_tcp->numpocs = l_current_poc_nb - 1;
3375 * Reads a CRG marker (Component registration)
3377 * @param p_header_data the data contained in the TLM box.
3378 * @param p_j2k the jpeg2000 codec.
3379 * @param p_header_size the size of the data contained in the TLM marker.
3380 * @param p_manager the user event manager.
3382 static opj_bool opj_j2k_read_crg ( opj_j2k_v2_t *p_j2k,
3383 OPJ_BYTE * p_header_data,
3384 OPJ_UINT32 p_header_size,
3385 opj_event_mgr_t * p_manager
3388 OPJ_UINT32 l_nb_comp;
3390 assert(p_header_data != 00);
3391 assert(p_j2k != 00);
3392 assert(p_manager != 00);
3394 l_nb_comp = p_j2k->m_private_image->numcomps;
3396 if (p_header_size != l_nb_comp *4) {
3397 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading CRG marker\n");
3400 /* Do not care of this at the moment since only local variables are set here */
3403 (i = 0; i < l_nb_comp; ++i)
3405 opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
3407 opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
3415 * Reads a TLM marker (Tile Length Marker)
3417 * @param p_header_data the data contained in the TLM box.
3418 * @param p_j2k the jpeg2000 codec.
3419 * @param p_header_size the size of the data contained in the TLM marker.
3420 * @param p_manager the user event manager.
3422 static opj_bool opj_j2k_read_tlm ( opj_j2k_v2_t *p_j2k,
3423 OPJ_BYTE * p_header_data,
3424 OPJ_UINT32 p_header_size,
3425 opj_event_mgr_t * p_manager
3428 OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp, l_tot_num_tp_remaining, l_quotient, l_Ptlm_size;
3430 assert(p_header_data != 00);
3431 assert(p_j2k != 00);
3432 assert(p_manager != 00);
3434 if (p_header_size < 2) {
3435 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3440 opj_read_bytes(p_header_data,&l_Ztlm,1); /* Ztlm */
3442 opj_read_bytes(p_header_data,&l_Stlm,1); /* Stlm */
3445 l_ST = ((l_Stlm >> 4) & 0x3);
3446 l_SP = (l_Stlm >> 6) & 0x1;
3448 l_Ptlm_size = (l_SP + 1) * 2;
3449 l_quotient = l_Ptlm_size + l_ST;
3451 l_tot_num_tp = p_header_size / l_quotient;
3452 l_tot_num_tp_remaining = p_header_size % l_quotient;
3454 if (l_tot_num_tp_remaining != 0) {
3455 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading TLM marker\n");
3458 /* FIXME Do not care of this at the moment since only local variables are set here */
3461 (i = 0; i < l_tot_num_tp; ++i)
3463 opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
3464 p_header_data += l_ST;
3465 opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
3466 p_header_data += l_Ptlm_size;
3473 * Reads a PLM marker (Packet length, main header marker)
3475 * @param p_header_data the data contained in the TLM box.
3476 * @param p_j2k the jpeg2000 codec.
3477 * @param p_header_size the size of the data contained in the TLM marker.
3478 * @param p_manager the user event manager.
3480 static opj_bool opj_j2k_read_plm ( opj_j2k_v2_t *p_j2k,
3481 OPJ_BYTE * p_header_data,
3482 OPJ_UINT32 p_header_size,
3483 opj_event_mgr_t * p_manager
3487 assert(p_header_data != 00);
3488 assert(p_j2k != 00);
3489 assert(p_manager != 00);
3491 if (p_header_size < 1) {
3492 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3495 /* Do not care of this at the moment since only local variables are set here */
3497 opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
3504 opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
3506 p_header_size -= (1+l_Nplm);
3510 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3514 (i = 0; i < l_Nplm; ++i)
3516 opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
3518 // take only the last seven bytes
3519 l_packet_len |= (l_tmp & 0x7f);
3527 // store packet length and proceed to next packet
3534 opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
3543 * Reads a PLT marker (Packet length, tile-part header)
3545 * @param p_header_data the data contained in the PLT box.
3546 * @param p_j2k the jpeg2000 codec.
3547 * @param p_header_size the size of the data contained in the PLT marker.
3548 * @param p_manager the user event manager.
3550 static opj_bool opj_j2k_read_plt ( opj_j2k_v2_t *p_j2k,
3551 OPJ_BYTE * p_header_data,
3552 OPJ_UINT32 p_header_size,
3553 opj_event_mgr_t * p_manager
3556 OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
3559 assert(p_header_data != 00);
3560 assert(p_j2k != 00);
3561 assert(p_manager != 00);
3563 if (p_header_size < 1) {
3564 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3568 opj_read_bytes(p_header_data,&l_Zplt,1); /* Zplt */
3572 for (i = 0; i < p_header_size; ++i) {
3573 opj_read_bytes(p_header_data,&l_tmp,1); /* Iplt_ij */
3575 /* take only the last seven bytes */
3576 l_packet_len |= (l_tmp & 0x7f);
3581 /* store packet length and proceed to next packet */
3586 if (l_packet_len != 0) {
3587 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PLT marker\n");
3596 * Reads a PPM marker (Packed packet headers, main header)
3598 * @param p_header_data the data contained in the POC box.
3599 * @param p_j2k the jpeg2000 codec.
3600 * @param p_header_size the size of the data contained in the POC marker.
3601 * @param p_manager the user event manager.
3604 opj_bool j2k_read_ppm_v2 (
3605 opj_j2k_v2_t *p_j2k,
3606 OPJ_BYTE * p_header_data,
3607 OPJ_UINT32 p_header_size,
3608 struct opj_event_mgr * p_manager
3612 opj_cp_v2_t *l_cp = 00;
3613 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3616 assert(p_header_data != 00);
3617 assert(p_j2k != 00);
3618 assert(p_manager != 00);
3620 if (p_header_size < 1) {
3621 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3625 l_cp = &(p_j2k->m_cp);
3628 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3632 /* First PPM marker */
3634 if (p_header_size < 4) {
3635 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3639 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3643 /* First PPM marker: Initialization */
3644 l_cp->ppm_len = l_N_ppm;
3645 l_cp->ppm_data_size = 0;
3647 l_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3648 if (l_cp->ppm_buffer == 00) {
3649 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3652 memset(l_cp->ppm_buffer,0,l_cp->ppm_len);
3654 l_cp->ppm_data = l_cp->ppm_buffer;
3658 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3659 if (p_header_size >= 4) {
3661 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3664 l_cp->ppm_len += l_N_ppm ;
3666 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3667 if (l_cp->ppm_buffer == 00) {
3668 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3671 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3673 l_cp->ppm_data = l_cp->ppm_buffer;
3680 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3682 if (l_remaining_data <= p_header_size) {
3683 /* we must store less information than available in the packet */
3684 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3685 l_cp->ppm_data_size = l_cp->ppm_len;
3686 p_header_size -= l_remaining_data;
3687 p_header_data += l_remaining_data;
3690 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3691 l_cp->ppm_data_size += p_header_size;
3692 p_header_data += p_header_size;
3705 * Reads a PPM marker (Packed packet headers, main header)
3707 * @param p_header_data the data contained in the POC box.
3708 * @param p_j2k the jpeg2000 codec.
3709 * @param p_header_size the size of the data contained in the POC marker.
3710 * @param p_manager the user event manager.
3712 opj_bool j2k_read_ppm_v3 (
3713 opj_j2k_v2_t *p_j2k,
3714 OPJ_BYTE * p_header_data,
3715 OPJ_UINT32 p_header_size,
3716 struct opj_event_mgr * p_manager
3719 opj_cp_v2_t *l_cp = 00;
3720 OPJ_UINT32 l_remaining_data, l_Z_ppm, l_N_ppm;
3723 assert(p_header_data != 00);
3724 assert(p_j2k != 00);
3725 assert(p_manager != 00);
3727 /* Minimum size of PPM marker is equal to the size of Zppm element */
3728 if (p_header_size < 1) {
3729 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3733 l_cp = &(p_j2k->m_cp);
3736 opj_read_bytes(p_header_data,&l_Z_ppm,1); /* Z_ppm */
3740 /* First PPM marker */
3742 /* We need now at least the Nppm^0 element */
3743 if (p_header_size < 4) {
3744 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPM marker\n");
3748 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3752 /* First PPM marker: Initialization */
3753 l_cp->ppm_len = l_N_ppm;
3754 l_cp->ppm_data_read = 0;
3756 l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
3757 if (l_cp->ppm_data == 00) {
3758 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3761 memset(l_cp->ppm_data,0,l_cp->ppm_len);
3763 l_cp->ppm_data_current = l_cp->ppm_data;
3765 /*l_cp->ppm_data = l_cp->ppm_buffer;*/
3768 if (p_header_size < 4) {
3769 opj_event_msg_v2(p_manager, EVT_WARNING, "Empty PPM marker\n");
3773 /* Uncompleted Ippm series in the previous PPM marker?*/
3774 if (l_cp->ppm_data_read < l_cp->ppm_len) {
3775 /* Get the place where add the remaining Ippm series*/
3776 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_data_read]);
3777 l_N_ppm = l_cp->ppm_len - l_cp->ppm_data_read;
3780 opj_read_bytes(p_header_data,&l_N_ppm,4); /* First N_ppm */
3784 /* Increase the size of ppm_data to add the new Ippm series*/
3785 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3787 /* Keep the position of the place where concatenate the new series*/
3788 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3789 l_cp->ppm_len += l_N_ppm;
3794 l_remaining_data = p_header_size;
3796 while (l_remaining_data >= l_N_ppm) {
3797 /* read a complete Ippm series*/
3798 memcpy(l_cp->ppm_data_current, p_header_data, l_N_ppm);
3799 p_header_size -= l_N_ppm;
3800 p_header_data += l_N_ppm;
3802 l_cp->ppm_data_read += l_N_ppm; /* Increase the number of data read*/
3806 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
3811 l_remaining_data = p_header_size;
3815 l_remaining_data = p_header_size;
3817 /* Next Ippm series is a complete series ?*/
3818 if (l_remaining_data > l_N_ppm) {
3819 /* Increase the size of ppm_data to add the new Ippm series*/
3820 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3822 /* Keep the position of the place where concatenate the new series */
3823 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3824 l_cp->ppm_len += l_N_ppm;
3829 /* Need to read an incomplete Ippm series*/
3830 if (l_remaining_data) {
3831 l_cp->ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
3833 /* Keep the position of the place where concatenate the new series*/
3834 l_cp->ppm_data_current = &(l_cp->ppm_data[l_cp->ppm_len]);
3835 l_cp->ppm_len += l_N_ppm;
3837 /* Read incomplete Ippm series*/
3838 memcpy(l_cp->ppm_data_current, p_header_data, l_remaining_data);
3839 p_header_size -= l_remaining_data;
3840 p_header_data += l_remaining_data;
3842 l_cp->ppm_data_read += l_remaining_data; /* Increase the number of data read*/
3847 if (l_cp->ppm_data_size == l_cp->ppm_len) {
3848 if (p_header_size >= 4) {
3850 opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm */
3853 l_cp->ppm_len += l_N_ppm ;
3855 l_cp->ppm_buffer = (OPJ_BYTE *) opj_realloc(l_cp->ppm_buffer, l_cp->ppm_len);
3856 if (l_cp->ppm_buffer == 00) {
3857 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading ppm marker\n");
3860 memset(l_cp->ppm_buffer+l_cp->ppm_data_size,0,l_N_ppm);
3862 l_cp->ppm_data = l_cp->ppm_buffer;
3869 l_remaining_data = l_cp->ppm_len - l_cp->ppm_data_size;
3871 if (l_remaining_data <= p_header_size) {
3872 /* we must store less information than available in the packet */
3873 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , l_remaining_data);
3874 l_cp->ppm_data_size = l_cp->ppm_len;
3875 p_header_size -= l_remaining_data;
3876 p_header_data += l_remaining_data;
3879 memcpy(l_cp->ppm_buffer + l_cp->ppm_data_size , p_header_data , p_header_size);
3880 l_cp->ppm_data_size += p_header_size;
3881 p_header_data += p_header_size;
3891 * Reads a PPT marker (Packed packet headers, tile-part header)
3893 * @param p_header_data the data contained in the PPT box.
3894 * @param p_j2k the jpeg2000 codec.
3895 * @param p_header_size the size of the data contained in the PPT marker.
3896 * @param p_manager the user event manager.
3898 static opj_bool opj_j2k_read_ppt ( opj_j2k_v2_t *p_j2k,
3899 OPJ_BYTE * p_header_data,
3900 OPJ_UINT32 p_header_size,
3901 opj_event_mgr_t * p_manager
3904 opj_cp_v2_t *l_cp = 00;
3905 opj_tcp_v2_t *l_tcp = 00;
3909 assert(p_header_data != 00);
3910 assert(p_j2k != 00);
3911 assert(p_manager != 00);
3913 /* We need to have the Z_ppt element at minimum */
3914 if (p_header_size < 1) {
3915 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading PPT marker\n");
3919 l_cp = &(p_j2k->m_cp);
3921 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");
3925 l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
3928 opj_read_bytes(p_header_data,&l_Z_ppt,1); /* Z_ppt */
3932 /* Allocate buffer to read the packet header */
3934 /* First PPT marker */
3935 l_tcp->ppt_data_size = 0;
3936 l_tcp->ppt_len = p_header_size;
3938 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
3939 if (l_tcp->ppt_buffer == 00) {
3940 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3943 l_tcp->ppt_data = l_tcp->ppt_buffer;
3945 /* memset(l_tcp->ppt_buffer,0,l_tcp->ppt_len); */
3948 l_tcp->ppt_len += p_header_size;
3950 l_tcp->ppt_buffer = (OPJ_BYTE *) opj_realloc(l_tcp->ppt_buffer,l_tcp->ppt_len);
3951 if (l_tcp->ppt_buffer == 00) {
3952 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory reading PPT marker\n");
3955 l_tcp->ppt_data = l_tcp->ppt_buffer;
3957 memset(l_tcp->ppt_buffer+l_tcp->ppt_data_size,0,p_header_size);
3960 /* Read packet header from buffer */
3961 memcpy(l_tcp->ppt_buffer+l_tcp->ppt_data_size,p_header_data,p_header_size);
3963 l_tcp->ppt_data_size += p_header_size;
3969 * Writes the TLM marker (Tile Length Marker)
3971 * @param p_stream the stream to write data to.
3972 * @param p_j2k J2K codec.
3973 * @param p_manager the user event manager.
3975 opj_bool opj_j2k_write_tlm( opj_j2k_v2_t *p_j2k,
3976 opj_stream_private_t *p_stream,
3977 opj_event_mgr_t * p_manager
3980 OPJ_BYTE * l_current_data = 00;
3981 OPJ_UINT32 l_tlm_size;
3984 assert(p_j2k != 00);
3985 assert(p_manager != 00);
3986 assert(p_stream != 00);
3988 l_tlm_size = 6 + (5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
3990 if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
3991 p_j2k->m_specific_param.m_encoder.m_header_tile_data
3992 = (OPJ_BYTE*)opj_realloc(
3993 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
3996 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
4000 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
4003 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4005 /* change the way data is written to avoid seeking if possible */
4007 p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
4009 opj_write_bytes(l_current_data,J2K_MS_TLM,2); /* TLM */
4010 l_current_data += 2;
4012 opj_write_bytes(l_current_data,l_tlm_size-2,2); /* Lpoc */
4013 l_current_data += 2;
4015 opj_write_bytes(l_current_data,0,1); /* Ztlm=0*/
4018 opj_write_bytes(l_current_data,0x50,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
4021 /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
4022 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) {
4030 * Writes the SOT marker (Start of tile-part)
4032 * @param p_stream the stream to write data to.
4033 * @param p_j2k J2K codec.
4034 * @param p_manager the user event manager.
4036 opj_bool opj_j2k_write_sot( opj_j2k_v2_t *p_j2k,
4038 OPJ_UINT32 * p_data_written,
4039 const opj_stream_private_t *p_stream,
4040 opj_event_mgr_t * p_manager
4044 assert(p_j2k != 00);
4045 assert(p_manager != 00);
4046 assert(p_stream != 00);
4048 opj_write_bytes(p_data,J2K_MS_SOT,2); /* SOT */
4051 opj_write_bytes(p_data,10,2); /* Lsot */
4054 opj_write_bytes(p_data, p_j2k->m_current_tile_number,2); /* Isot */
4060 opj_write_bytes(p_data, p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,1); /* TPsot */
4063 opj_write_bytes(p_data, p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,1); /* TNsot */
4068 /* update markers struct */
4070 j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
4072 assert( 0 && "TODO" );
4073 #endif /* USE_JPWL */
4075 * p_data_written = 12;
4084 * Reads a PPT marker (Packed packet headers, tile-part header)
4086 * @param p_header_data the data contained in the PPT box.
4087 * @param p_j2k the jpeg2000 codec.
4088 * @param p_header_size the size of the data contained in the PPT marker.
4089 * @param p_manager the user event manager.
4091 opj_bool opj_j2k_read_sot ( opj_j2k_v2_t *p_j2k,
4092 OPJ_BYTE * p_header_data,
4093 OPJ_UINT32 p_header_size,
4094 opj_event_mgr_t * p_manager )
4096 opj_cp_v2_t *l_cp = 00;
4097 opj_tcp_v2_t *l_tcp = 00;
4098 OPJ_UINT32 l_tot_len, l_num_parts = 0;
4099 OPJ_UINT32 l_current_part;
4100 OPJ_UINT32 l_tile_x,l_tile_y;
4103 assert(p_header_data != 00);
4104 assert(p_j2k != 00);
4105 assert(p_manager != 00);
4107 /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
4108 if (p_header_size != 8) {
4109 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SOT marker\n");
4113 l_cp = &(p_j2k->m_cp);
4114 opj_read_bytes(p_header_data,&(p_j2k->m_current_tile_number),2); /* Isot */
4117 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4118 l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
4119 l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
4122 if (l_cp->correct) {
4124 int tileno = p_j2k->m_current_tile_number;
4125 static int backup_tileno = 0;
4127 /* tileno is negative or larger than the number of tiles!!! */
4128 if ((tileno < 0) || (tileno > (l_cp->tw * l_cp->th))) {
4129 opj_event_msg_v2(p_manager, EVT_ERROR,
4130 "JPWL: bad tile number (%d out of a maximum of %d)\n",
4131 tileno, (l_cp->tw * l_cp->th));
4133 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4136 /* we try to correct */
4137 tileno = backup_tileno;
4138 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4139 "- setting tile number to %d\n",
4143 /* keep your private count of tiles */
4146 #endif /* USE_JPWL */
4148 /* look for the tile in the list of already processed tile (in parts). */
4149 /* Optimization possible here with a more complex data structure and with the removing of tiles */
4150 /* since the time taken by this function can only grow at the time */
4152 opj_read_bytes(p_header_data,&l_tot_len,4); /* Psot */
4155 /* PSot should be equal to zero or >=14 or <= 2^32-1 */
4156 if ((l_tot_len !=0 ) && (l_tot_len < 14) )
4158 opj_event_msg_v2(p_manager, EVT_ERROR, "Psot value (%d) is not correct regards to the JPEG2000 norm!\n", l_tot_len);
4164 if (l_cp->correct) {
4166 /* totlen is negative or larger than the bytes left!!! */
4167 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))) { */
4168 opj_event_msg_v2(p_manager, EVT_ERROR,
4169 "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
4170 l_tot_len, p_header_size ); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
4172 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4175 /* we try to correct */
4177 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust this\n"
4178 "- setting Psot to %d => assuming it is the last tile\n",
4182 #endif /* USE_JPWL */
4184 /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
4186 opj_event_msg_v2(p_manager, EVT_INFO, "Psot value of the current tile-part is equal to zero, "
4187 "we assuming it is the last tile-part of the codestream.\n");
4188 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4191 opj_read_bytes(p_header_data,&l_current_part ,1); /* TPsot */
4194 opj_read_bytes(p_header_data,&l_num_parts ,1); /* TNsot */
4197 if (l_num_parts != 0) { /* Number of tile-part header is provided by this tile-part header */
4198 /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
4199 * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
4200 if (l_tcp->m_nb_tile_parts) {
4201 if (l_current_part >= l_tcp->m_nb_tile_parts){
4202 opj_event_msg_v2(p_manager, EVT_ERROR, "In SOT marker, TPSot (%d) is not valid regards to the current "
4203 "number of tile-part (%d), giving up\n", l_current_part, l_tcp->m_nb_tile_parts );
4204 p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
4208 l_tcp->m_nb_tile_parts = l_num_parts;
4211 /* If know the number of tile part header we will check if we didn't read the last*/
4212 if (l_tcp->m_nb_tile_parts) {
4213 if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
4214 p_j2k->m_specific_param.m_decoder.m_can_decode = 1; /* Process the last tile-part header*/
4218 if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part){
4219 /* Keep the size of data to skip after this marker */
4220 p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len - 12; /* SOT_marker_size = 12 */
4223 /* FIXME: need to be computed from the number of bytes remaining in the codestream */
4224 p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
4227 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
4229 /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
4230 if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
4231 p_j2k->m_specific_param.m_decoder.m_skip_data =
4232 (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
4233 || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
4234 || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
4235 || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
4238 assert( p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0 );
4239 p_j2k->m_specific_param.m_decoder.m_skip_data =
4240 (p_j2k->m_current_tile_number != (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
4244 if (p_j2k->cstr_index)
4246 assert(p_j2k->cstr_index->tile_index != 00);
4247 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4248 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno = l_current_part;
4250 if (l_num_parts != 0){
4251 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps = l_num_parts;
4252 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = l_num_parts;
4255 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)
4256 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4257 (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
4259 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4260 (opj_tp_index_t*)opj_realloc(
4261 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4262 l_num_parts* sizeof(opj_tp_index_t));
4265 /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
4267 if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
4268 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
4269 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4270 (opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
4271 sizeof(opj_tp_index_t));
4274 if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
4275 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps += 10;
4276 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
4277 (opj_tp_index_t*)opj_realloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
4278 p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps
4279 * sizeof(opj_tp_index_t));
4288 /* 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 */
4289 /* if (p_j2k->cstr_info) {
4292 p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
4295 p_j2k->cstr_info->tile[tileno].tileno = tileno;
4296 p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
4297 p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
4298 p_j2k->cstr_info->tile[tileno].num_tps = numparts;
4301 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
4304 p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
4308 p_j2k->cstr_info->tile[tileno].end_pos += totlen;
4311 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
4312 p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
4313 p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
4319 * Writes the SOD marker (Start of data)
4321 * @param p_stream the stream to write data to.
4322 * @param p_j2k J2K codec.
4323 * @param p_manager the user event manager.
4325 opj_bool opj_j2k_write_sod( opj_j2k_v2_t *p_j2k,
4326 opj_tcd_v2_t * p_tile_coder,
4328 OPJ_UINT32 * p_data_written,
4329 OPJ_UINT32 p_total_data_size,
4330 const opj_stream_private_t *p_stream,
4331 opj_event_mgr_t * p_manager
4334 opj_tcp_v2_t *l_tcp = 00;
4335 opj_codestream_info_t *l_cstr_info = 00;
4336 opj_cp_v2_t *l_cp = 00;
4338 OPJ_UINT32 l_size_tile;
4339 OPJ_UINT32 l_remaining_data;
4342 assert(p_j2k != 00);
4343 assert(p_manager != 00);
4344 assert(p_stream != 00);
4346 opj_write_bytes(p_data,J2K_MS_SOD,2); /* SOD */
4349 /* make room for the EOF marker */
4350 l_remaining_data = p_total_data_size - 4;
4352 l_cp = &(p_j2k->m_cp);
4353 l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
4356 /* update tile coder */
4357 p_tile_coder->tp_num = p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
4358 p_tile_coder->cur_tp_num = p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
4360 l_size_tile = l_cp->th * l_cp->tw;
4363 /* TODO mergeV2: check this part which use cstr_info */
4364 /*l_cstr_info = p_j2k->cstr_info;
4366 if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
4367 //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
4368 l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
4374 (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
4376 cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
4381 /* update markers struct */
4382 /*j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
4384 assert( 0 && "TODO" );
4385 #endif /* USE_JPWL */
4390 if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
4391 p_tile_coder->tcd_image->tiles->packno = 0;
4393 l_cstr_info->packno = 0;
4397 *p_data_written = 0;
4399 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)) {
4400 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot encode tile\n");
4404 *p_data_written += 2;
4413 * Reads a SOD marker (Start Of Data)
4415 * @param p_header_data the data contained in the SOD box.
4416 * @param p_j2k the jpeg2000 codec.
4417 * @param p_header_size the size of the data contained in the SOD marker.
4418 * @param p_manager the user event manager.
4420 opj_bool opj_j2k_read_sod (opj_j2k_v2_t *p_j2k,
4421 opj_stream_private_t *p_stream,
4422 opj_event_mgr_t * p_manager
4425 OPJ_UINT32 l_current_read_size;
4426 opj_codestream_index_t * l_cstr_index = 00;
4427 OPJ_BYTE ** l_current_data = 00;
4428 opj_tcp_v2_t * l_tcp = 00;
4429 OPJ_UINT32 * l_tile_len = 00;
4432 assert(p_j2k != 00);
4433 assert(p_manager != 00);
4434 assert(p_stream != 00);
4436 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
4438 if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
4439 /* opj_stream_get_number_byte_left returns OPJ_OFF_T
4440 // but we are in the last tile part,
4441 // so its result will fit on OPJ_UINT32 unless we find
4442 // a file with a single tile part of more than 4 GB...*/
4443 p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(opj_stream_get_number_byte_left(p_stream) - 2);
4446 p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
4448 l_current_data = &(l_tcp->m_data);
4449 l_tile_len = &l_tcp->m_data_size;
4451 if (! *l_current_data) {
4452 *l_current_data = (OPJ_BYTE*) opj_malloc(p_j2k->m_specific_param.m_decoder.m_sot_length);
4455 *l_current_data = (OPJ_BYTE*) opj_realloc(*l_current_data, *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length);
4458 if (*l_current_data == 00) {
4459 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile\n");
4465 l_cstr_index = p_j2k->cstr_index;
4467 OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
4469 OPJ_UINT32 l_current_tile_part = l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
4470 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header =
4472 l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos =
4473 l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
4475 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
4479 p_j2k->m_specific_param.m_decoder.m_sot_length + 2);
4481 /*l_cstr_index->packno = 0;*/
4484 l_current_read_size = opj_stream_read_data( p_stream,
4485 *l_current_data + *l_tile_len,
4486 p_j2k->m_specific_param.m_decoder.m_sot_length,
4489 if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
4490 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
4493 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
4496 *l_tile_len += l_current_read_size;
4502 static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
4503 opj_cp_t *cp = j2k->cp;
4504 opj_tcp_t *tcp = &cp->tcps[tileno];
4505 opj_cio_t *cio = j2k->cio;
4506 int numcomps = j2k->image->numcomps;
4508 cio_write(cio, J2K_MS_RGN, 2); /* RGN */
4509 cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
4510 cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
4511 cio_write(cio, 0, 1); /* Srgn */
4512 cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
4516 * Writes the RGN marker (Region Of Interest)
4518 * @param p_tile_no the tile to output
4519 * @param p_comp_no the component to output
4520 * @param p_stream the stream to write data to.
4521 * @param p_j2k J2K codec.
4522 * @param p_manager the user event manager.
4524 opj_bool j2k_write_rgn_v2( opj_j2k_v2_t *p_j2k,
4525 OPJ_UINT32 p_tile_no,
4526 OPJ_UINT32 p_comp_no,
4527 struct opj_stream_private *p_stream,
4528 struct opj_event_mgr * p_manager )
4530 OPJ_BYTE * l_current_data = 00;
4531 OPJ_UINT32 l_nb_comp;
4532 OPJ_UINT32 l_rgn_size;
4533 opj_image_t *l_image = 00;
4534 opj_cp_v2_t *l_cp = 00;
4535 opj_tcp_v2_t *l_tcp = 00;
4536 opj_tccp_t *l_tccp = 00;
4537 OPJ_UINT32 l_comp_room;
4540 assert(p_j2k != 00);
4541 assert(p_manager != 00);
4542 assert(p_stream != 00);
4544 l_cp = &(p_j2k->m_cp);
4545 l_tcp = &l_cp->tcps[p_tile_no];
4546 l_tccp = &l_tcp->tccps[p_comp_no];
4548 l_nb_comp = l_image->numcomps;
4550 if (l_nb_comp <= 256) {
4557 l_rgn_size = 6 + l_comp_room;
4559 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
4561 opj_write_bytes(l_current_data,J2K_MS_RGN,2); /* RGN */
4562 l_current_data += 2;
4564 opj_write_bytes(l_current_data,l_rgn_size-2,2); /* Lrgn */
4565 l_current_data += 2;
4567 opj_write_bytes(l_current_data,p_comp_no,l_comp_room); /* Crgn */
4568 l_current_data+=l_comp_room;
4570 opj_write_bytes(l_current_data, 0,1); /* Srgn */
4573 opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
4576 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) {
4584 static void j2k_write_eoc(opj_j2k_t *j2k) {
4585 opj_cio_t *cio = j2k->cio;
4586 /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
4587 cio_write(cio, J2K_MS_EOC, 2);
4591 /* update markers struct */
4592 j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
4593 #endif /* USE_JPWL */
4598 * Writes the EOC marker (End of Codestream)
4600 * @param p_stream the stream to write data to.
4601 * @param p_j2k J2K codec.
4602 * @param p_manager the user event manager.
4604 opj_bool j2k_write_eoc_v2( opj_j2k_v2_t *p_j2k,
4605 struct opj_stream_private *p_stream,
4606 struct opj_event_mgr * p_manager )
4609 assert(p_j2k != 00);
4610 assert(p_manager != 00);
4611 assert(p_stream != 00);
4613 opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,J2K_MS_EOC,2); /* EOC */
4618 /* update markers struct */
4620 j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
4622 #endif /* USE_JPWL */
4624 if ( opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,2,p_manager) != 2) {
4628 if ( ! opj_stream_flush(p_stream,p_manager) ) {
4637 * Reads a RGN marker (Region Of Interest)
4639 * @param p_header_data the data contained in the POC box.
4640 * @param p_j2k the jpeg2000 codec.
4641 * @param p_header_size the size of the data contained in the POC marker.
4642 * @param p_manager the user event manager.
4644 static opj_bool opj_j2k_read_rgn (opj_j2k_v2_t *p_j2k,
4645 OPJ_BYTE * p_header_data,
4646 OPJ_UINT32 p_header_size,
4647 opj_event_mgr_t * p_manager
4650 OPJ_UINT32 l_nb_comp;
4651 opj_image_t * l_image = 00;
4653 opj_cp_v2_t *l_cp = 00;
4654 opj_tcp_v2_t *l_tcp = 00;
4655 OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
4658 assert(p_header_data != 00);
4659 assert(p_j2k != 00);
4660 assert(p_manager != 00);
4662 l_image = p_j2k->m_private_image;
4663 l_nb_comp = l_image->numcomps;
4665 if (l_nb_comp <= 256) {
4670 if (p_header_size != 2 + l_comp_room) {
4671 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading RGN marker\n");
4675 l_cp = &(p_j2k->m_cp);
4676 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
4677 &l_cp->tcps[p_j2k->m_current_tile_number] :
4678 p_j2k->m_specific_param.m_decoder.m_default_tcp;
4680 opj_read_bytes(p_header_data,&l_comp_no,l_comp_room); /* Crgn */
4681 p_header_data+=l_comp_room;
4682 opj_read_bytes(p_header_data,&l_roi_sty,1); /* Srgn */
4686 if (l_cp->correct) {
4687 /* totlen is negative or larger than the bytes left!!! */
4688 if (l_comp_room >= l_nb_comp) {
4689 opj_event_msg_v2(p_manager, EVT_ERROR,
4690 "JPWL: bad component number in RGN (%d when there are only %d)\n",
4691 l_comp_room, l_nb_comp);
4692 if (!JPWL_ASSUME || JPWL_ASSUME) {
4693 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
4698 #endif /* USE_JPWL */
4700 opj_read_bytes(p_header_data,(OPJ_UINT32 *) (&(l_tcp->tccps[l_comp_no].roishift)),1); /* SPrgn */
4707 static OPJ_FLOAT32 get_tp_stride (opj_tcp_v2_t * p_tcp)
4709 return (OPJ_FLOAT32) ((p_tcp->m_nb_tile_parts - 1) * 14);
4712 static OPJ_FLOAT32 get_default_stride (opj_tcp_v2_t * p_tcp)
4719 * Updates the rates of the tcp.
4721 * @param p_stream the stream to write data to.
4722 * @param p_j2k J2K codec.
4723 * @param p_manager the user event manager.
4725 opj_bool opj_j2k_update_rates( opj_j2k_v2_t *p_j2k,
4726 struct opj_stream_private *p_stream,
4727 struct opj_event_mgr * p_manager )
4729 opj_cp_v2_t * l_cp = 00;
4730 opj_image_t * l_image = 00;
4731 opj_tcp_v2_t * l_tcp = 00;
4732 opj_image_comp_t * l_img_comp = 00;
4735 OPJ_INT32 l_x0,l_y0,l_x1,l_y1;
4736 OPJ_FLOAT32 * l_rates = 0;
4737 OPJ_FLOAT32 l_sot_remove;
4738 OPJ_UINT32 l_bits_empty, l_size_pixel;
4739 OPJ_UINT32 l_tile_size = 0;
4740 OPJ_UINT32 l_last_res;
4741 OPJ_FLOAT32 (* l_tp_stride_func)(opj_tcp_v2_t *) = 00;
4744 assert(p_j2k != 00);
4745 assert(p_manager != 00);
4746 assert(p_stream != 00);
4749 l_cp = &(p_j2k->m_cp);
4750 l_image = p_j2k->m_private_image;
4753 l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
4754 l_size_pixel = l_image->numcomps * l_image->comps->prec;
4755 l_sot_remove = ((OPJ_FLOAT32) opj_stream_tell(p_stream)) / (l_cp->th * l_cp->tw);
4757 if (l_cp->m_specific_param.m_enc.m_tp_on) {
4758 l_tp_stride_func = get_tp_stride;
4761 l_tp_stride_func = get_default_stride;
4764 for (i=0;i<l_cp->th;++i) {
4765 for (j=0;j<l_cp->tw;++j) {
4766 OPJ_FLOAT32 l_offset = ((*l_tp_stride_func)(l_tcp)) / l_tcp->numlayers;
4768 /* 4 borders of the tile rescale on the image if necessary */
4769 l_x0 = int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
4770 l_y0 = int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
4771 l_x1 = int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
4772 l_y1 = int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
4774 l_rates = l_tcp->rates;
4776 /* Modification of the RATE >> */
4778 *l_rates = (( (float) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4780 ((*l_rates) * l_bits_empty)
4788 for (k = 1; k < l_tcp->numlayers; ++k) {
4790 *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
4792 ((*l_rates) * l_bits_empty)
4808 for (i=0;i<l_cp->th;++i) {
4809 for (j=0;j<l_cp->tw;++j) {
4810 l_rates = l_tcp->rates;
4813 *l_rates -= l_sot_remove;
4815 if (*l_rates < 30) {
4822 l_last_res = l_tcp->numlayers - 1;
4824 for (k = 1; k < l_last_res; ++k) {
4827 *l_rates -= l_sot_remove;
4829 if (*l_rates < *(l_rates - 1) + 10) {
4830 *l_rates = (*(l_rates - 1)) + 20;
4838 *l_rates -= (l_sot_remove + 2.f);
4840 if (*l_rates < *(l_rates - 1) + 10) {
4841 *l_rates = (*(l_rates - 1)) + 20;
4849 l_img_comp = l_image->comps;
4852 for (i=0;i<l_image->numcomps;++i) {
4853 l_tile_size += ( uint_ceildiv(l_cp->tdx,l_img_comp->dx)
4855 uint_ceildiv(l_cp->tdy,l_img_comp->dy)
4863 l_tile_size = (OPJ_UINT32) (l_tile_size * 0.1625); /* 1.3/8 = 0.1625 */
4865 l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
4867 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
4868 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
4869 (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
4870 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
4874 if (l_cp->m_specific_param.m_enc.m_cinema) {
4875 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
4876 (OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
4877 if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
4881 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
4882 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
4888 static void j2k_read_eoc(opj_j2k_t *j2k) {
4890 opj_bool success = OPJ_FALSE;
4892 /* if packets should be decoded */
4893 if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
4894 opj_tcd_t *tcd = tcd_create(j2k->cinfo);
4895 tcd_malloc_decode(tcd, j2k->image, j2k->cp);
4896 for (i = 0; i < j2k->cp->tileno_size; i++) {
4897 tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
4898 if (j2k->cp->tileno[i] != -1)
4900 tileno = j2k->cp->tileno[i];
4901 success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
4902 opj_free(j2k->tile_data[tileno]);
4903 j2k->tile_data[tileno] = NULL;
4904 tcd_free_decode_tile(tcd, i);
4907 success = OPJ_FALSE;
4908 if (success == OPJ_FALSE) {
4909 j2k->state |= J2K_STATE_ERR;
4913 tcd_free_decode(tcd);
4916 /* if packets should not be decoded */
4918 for (i = 0; i < j2k->cp->tileno_size; i++) {
4919 tileno = j2k->cp->tileno[i];
4920 opj_free(j2k->tile_data[tileno]);
4921 j2k->tile_data[tileno] = NULL;
4924 if (j2k->state & J2K_STATE_ERR)
4925 j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
4927 j2k->state = J2K_STATE_MT;
4931 * Reads a EOC marker (End Of Codestream)
4933 * @param p_header_data the data contained in the SOD box.
4934 * @param p_j2k the jpeg2000 codec.
4935 * @param p_header_size the size of the data contained in the SOD marker.
4936 * @param p_manager the user event manager.
4939 opj_bool j2k_read_eoc_v2 ( opj_j2k_v2_t *p_j2k,
4940 struct opj_stream_private *p_stream,
4941 struct opj_event_mgr * p_manager )
4944 opj_tcd_v2_t * l_tcd = 00;
4945 OPJ_UINT32 l_nb_tiles;
4946 opj_tcp_v2_t * l_tcp = 00;
4950 assert(p_j2k != 00);
4951 assert(p_manager != 00);
4952 assert(p_stream != 00);
4954 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
4955 l_tcp = p_j2k->m_cp.tcps;
4957 l_tcd = tcd_create_v2(OPJ_TRUE);
4959 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4963 for (i = 0; i < l_nb_tiles; ++i) {
4964 if (l_tcp->m_data) {
4965 if (! tcd_init_decode_tile(l_tcd, i)) {
4966 tcd_destroy_v2(l_tcd);
4967 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
4971 l_success = tcd_decode_tile_v2(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i, p_j2k->cstr_index);
4975 p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
4980 opj_j2k_tcp_destroy(l_tcp);
4984 tcd_destroy_v2(l_tcd);
4990 * Gets the offset of the header.
4992 * @param p_stream the stream to write data to.
4993 * @param p_j2k J2K codec.
4994 * @param p_manager the user event manager.
4996 opj_bool opj_j2k_get_end_header(opj_j2k_v2_t *p_j2k,
4997 struct opj_stream_private *p_stream,
4998 struct opj_event_mgr * p_manager )
5001 assert(p_j2k != 00);
5002 assert(p_manager != 00);
5003 assert(p_stream != 00);
5005 p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
5011 * Writes the MCT marker (Multiple Component Transform)
5013 * @param p_stream the stream to write data to.
5014 * @param p_j2k J2K codec.
5015 * @param p_manager the user event manager.
5017 opj_bool j2k_write_mct_data_group( opj_j2k_v2_t *p_j2k,
5018 struct opj_stream_private *p_stream,
5019 struct opj_event_mgr * p_manager )
5022 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5023 opj_mct_data_t * l_mct_record;
5024 opj_tcp_v2_t * l_tcp;
5027 assert(p_j2k != 00);
5028 assert(p_stream != 00);
5029 assert(p_manager != 00);
5031 if (! j2k_write_cbd(p_j2k,p_stream,p_manager)) {
5035 l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5036 l_mct_record = l_tcp->m_mct_records;
5038 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5040 if (! j2k_write_mct_record(p_j2k,l_mct_record,p_stream,p_manager)) {
5047 l_mcc_record = l_tcp->m_mcc_records;
5049 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5051 if (! j2k_write_mcc_record(p_j2k,l_mcc_record,p_stream,p_manager)) {
5058 if (! j2k_write_mco(p_j2k,p_stream,p_manager)) {
5066 * Writes the image components.
5068 * @param p_stream the stream to write data to.
5069 * @param p_j2k J2K codec.
5070 * @param p_manager the user event manager.
5072 opj_bool j2k_write_image_components(opj_j2k_v2_t *p_j2k,
5073 struct opj_stream_private *p_stream,
5074 struct opj_event_mgr * p_manager )
5079 assert(p_j2k != 00);
5080 assert(p_manager != 00);
5081 assert(p_stream != 00);
5083 for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno)
5085 if (! opj_j2k_write_coc(p_j2k,compno,p_stream, p_manager)) {
5089 if (! opj_j2k_write_qcc(p_j2k,compno,p_stream, p_manager)) {
5098 * Writes regions of interests.
5100 * @param p_stream the stream to write data to.
5101 * @param p_j2k J2K codec.
5102 * @param p_manager the user event manager.
5104 opj_bool j2k_write_regions( opj_j2k_v2_t *p_j2k,
5105 struct opj_stream_private *p_stream,
5106 struct opj_event_mgr * p_manager )
5109 const opj_tccp_t *l_tccp = 00;
5112 assert(p_j2k != 00);
5113 assert(p_manager != 00);
5114 assert(p_stream != 00);
5116 l_tccp = p_j2k->m_cp.tcps->tccps;
5118 for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
5119 if (l_tccp->roishift) {
5121 if (! j2k_write_rgn_v2(p_j2k,0,compno,p_stream,p_manager)) {
5135 * @param p_stream the stream to write data to.
5136 * @param p_j2k J2K codec.
5137 * @param p_manager the user event manager.
5139 opj_bool j2k_write_epc( opj_j2k_v2_t *p_j2k,
5140 struct opj_stream_private *p_stream,
5141 struct opj_event_mgr * p_manager )
5143 opj_codestream_index_t * l_cstr_index = 00;
5146 assert(p_j2k != 00);
5147 assert(p_manager != 00);
5148 assert(p_stream != 00);
5150 l_cstr_index = p_j2k->cstr_index;
5152 l_cstr_index->codestream_size = opj_stream_tell(p_stream);
5154 /* The following adjustment is done to adjust the codestream size */
5155 /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
5156 /* the first bunch of bytes is not in the codestream */
5157 l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
5162 /* preparation of JPWL marker segments */
5166 /* encode according to JPWL */
5167 jpwl_encode(p_j2k, p_stream, image);
5171 assert( 0 && "TODO" );
5172 #endif /* USE_JPWL */
5177 typedef struct opj_dec_mstabent {
5180 /** value of the state when the marker can appear */
5182 /** action linked to the marker */
5183 void (*handler) (opj_j2k_t *j2k);
5184 } opj_dec_mstabent_t;
5186 opj_dec_mstabent_t j2k_dec_mstab[] = {
5187 /*{J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},*/
5188 /*{J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},*/
5189 /*{J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},*/
5190 {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
5191 /*{J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},*/
5192 /*{J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},*/
5193 /*{J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},*/
5194 /*{J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},*/
5195 /*{J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},*/
5196 /*{J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},*/
5197 /*{J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},*/
5198 /*{J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},*/
5199 /*{J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},*/
5200 /*{J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},*/
5201 /*{J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},*/
5202 /*{J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},*/
5204 /*{J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},*/
5205 /*{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},*/
5208 {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
5209 {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
5210 {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
5211 {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
5212 #endif /* USE_JPWL */
5214 {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
5215 {J2K_MS_INSEC, 0, j2k_read_insec},
5216 #endif /* USE_JPSEC */
5218 /*{0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}*/
5224 * Reads an unknown marker
5226 * @param p_stream the stream object to read from.
5227 * @param p_j2k the jpeg2000 codec.
5228 * @param p_manager the user event manager.
5230 * @return true if the marker could be deduced.
5232 opj_bool opj_j2k_read_unk ( opj_j2k_v2_t *p_j2k,
5233 opj_stream_private_t *p_stream,
5234 OPJ_UINT32 *output_marker,
5235 opj_event_mgr_t * p_manager
5238 OPJ_UINT32 l_unknown_marker;
5239 const opj_dec_memory_marker_handler_t * l_marker_handler;
5240 OPJ_UINT32 l_size_unk = 2;
5243 assert(p_j2k != 00);
5244 assert(p_manager != 00);
5245 assert(p_stream != 00);
5247 opj_event_msg_v2(p_manager, EVT_WARNING, "Unknown marker\n");
5250 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
5251 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
5252 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
5256 /* read 2 bytes as the new marker ID*/
5257 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_unknown_marker,2);
5259 if (!(l_unknown_marker < 0xff00)) {
5261 /* Get the marker handler from the marker ID*/
5262 l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
5264 if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
5265 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
5269 if (l_marker_handler->id != J2K_MS_UNK) {
5270 /* Add the marker to the codestream index*/
5271 if (l_marker_handler->id != J2K_MS_SOT)
5272 j2k_add_mhmarker_v2(p_j2k->cstr_index, J2K_MS_UNK,
5273 (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
5275 break; /* next marker is known and well located */
5283 *output_marker = l_marker_handler->id ;
5289 Read the lookup table containing all the marker, status and action
5290 @param id Marker value
5292 static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
5293 opj_dec_mstabent_t *e;
5294 for (e = j2k_dec_mstab; e->id != 0; e++) {
5303 * Writes the MCT marker (Multiple Component Transform)
5305 * @param p_stream the stream to write data to.
5306 * @param p_j2k J2K codec.
5307 * @param p_manager the user event manager.
5309 opj_bool j2k_write_mct_record( opj_j2k_v2_t *p_j2k,
5310 opj_mct_data_t * p_mct_record,
5311 struct opj_stream_private *p_stream,
5312 struct opj_event_mgr * p_manager )
5314 OPJ_UINT32 l_mct_size;
5315 OPJ_BYTE * l_current_data = 00;
5319 assert(p_j2k != 00);
5320 assert(p_manager != 00);
5321 assert(p_stream != 00);
5323 l_mct_size = 10 + p_mct_record->m_data_size;
5325 if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5326 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5327 = (OPJ_BYTE*)opj_realloc(
5328 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5331 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5335 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
5338 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5340 opj_write_bytes(l_current_data,J2K_MS_MCT,2); /* MCT */
5341 l_current_data += 2;
5343 opj_write_bytes(l_current_data,l_mct_size-2,2); /* Lmct */
5344 l_current_data += 2;
5346 opj_write_bytes(l_current_data,0,2); /* Zmct */
5347 l_current_data += 2;
5349 /* only one marker atm */
5350 l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) | (p_mct_record->m_element_type << 10);
5352 opj_write_bytes(l_current_data,l_tmp,2);
5353 l_current_data += 2;
5355 opj_write_bytes(l_current_data,0,2); /* Ymct */
5358 memcpy(l_current_data,p_mct_record->m_data,p_mct_record->m_data_size);
5360 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) {
5368 * Reads a MCT marker (Multiple Component Transform)
5370 * @param p_header_data the data contained in the MCT box.
5371 * @param p_j2k the jpeg2000 codec.
5372 * @param p_header_size the size of the data contained in the MCT marker.
5373 * @param p_manager the user event manager.
5375 static opj_bool opj_j2k_read_mct ( opj_j2k_v2_t *p_j2k,
5376 OPJ_BYTE * p_header_data,
5377 OPJ_UINT32 p_header_size,
5378 opj_event_mgr_t * p_manager
5382 opj_tcp_v2_t *l_tcp = 00;
5385 opj_mct_data_t * l_mct_data;
5388 assert(p_header_data != 00);
5389 assert(p_j2k != 00);
5391 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5392 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5393 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5395 if (p_header_size < 2) {
5396 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5401 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmct */
5404 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge mct data within multiple MCT records\n");
5408 if(p_header_size <= 6) {
5409 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5413 /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
5414 opj_read_bytes(p_header_data,&l_tmp,2); /* Imct */
5417 l_indix = l_tmp & 0xff;
5418 l_mct_data = l_tcp->m_mct_records;
5420 for (i=0;i<l_tcp->m_nb_mct_records;++i) {
5421 if (l_mct_data->m_index == l_indix) {
5428 if (i == l_tcp->m_nb_mct_records) {
5429 if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
5430 l_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
5432 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));
5433 if(! l_tcp->m_mct_records) {
5434 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5438 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5439 memset(l_mct_data ,0,(l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
5442 l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
5445 if (l_mct_data->m_data) {
5446 opj_free(l_mct_data->m_data);
5447 l_mct_data->m_data = 00;
5450 l_mct_data->m_index = l_indix;
5451 l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
5452 l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
5454 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymct */
5457 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple MCT markers\n");
5463 l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
5464 if (! l_mct_data->m_data) {
5465 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCT marker\n");
5468 memcpy(l_mct_data->m_data,p_header_data,p_header_size);
5470 l_mct_data->m_data_size = p_header_size;
5471 ++l_tcp->m_nb_mct_records;
5477 * Writes the MCC marker (Multiple Component Collection)
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_mcc_record( opj_j2k_v2_t *p_j2k,
5484 struct opj_simple_mcc_decorrelation_data * p_mcc_record,
5485 struct opj_stream_private *p_stream,
5486 struct opj_event_mgr * p_manager )
5489 OPJ_UINT32 l_mcc_size;
5490 OPJ_BYTE * l_current_data = 00;
5491 OPJ_UINT32 l_nb_bytes_for_comp;
5496 assert(p_j2k != 00);
5497 assert(p_manager != 00);
5498 assert(p_stream != 00);
5500 if (p_mcc_record->m_nb_comps > 255 ) {
5501 l_nb_bytes_for_comp = 2;
5505 l_nb_bytes_for_comp = 1;
5509 l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
5510 if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size)
5512 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5513 = (OPJ_BYTE*)opj_realloc(
5514 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5516 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
5520 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
5523 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5525 opj_write_bytes(l_current_data,J2K_MS_MCC,2); /* MCC */
5526 l_current_data += 2;
5528 opj_write_bytes(l_current_data,l_mcc_size-2,2); /* Lmcc */
5529 l_current_data += 2;
5532 opj_write_bytes(l_current_data,0,2); /* Zmcc */
5533 l_current_data += 2;
5535 opj_write_bytes(l_current_data,p_mcc_record->m_index,1); /* Imcc -> no need for other values, take the first */
5538 /* only one marker atm */
5539 opj_write_bytes(l_current_data,0,2); /* Ymcc */
5542 opj_write_bytes(l_current_data,1,2); /* Qmcc -> number of collections -> 1 */
5545 opj_write_bytes(l_current_data,0x1,1); /* Xmcci type of component transformation -> array based decorrelation */
5548 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 */
5551 for (i=0;i<p_mcc_record->m_nb_comps;++i) {
5552 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Cmccij Component offset*/
5553 l_current_data+=l_nb_bytes_for_comp;
5556 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 */
5559 for (i=0;i<p_mcc_record->m_nb_comps;++i)
5561 opj_write_bytes(l_current_data,i,l_nb_bytes_for_comp); /* Wmccij Component offset*/
5562 l_current_data+=l_nb_bytes_for_comp;
5565 l_tmcc = ((!p_mcc_record->m_is_irreversible)&1)<<16;
5567 if (p_mcc_record->m_decorrelation_array) {
5568 l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
5571 if (p_mcc_record->m_offset_array) {
5572 l_tmcc |= ((p_mcc_record->m_offset_array->m_index)<<8);
5575 opj_write_bytes(l_current_data,l_tmcc,3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
5578 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) {
5586 * Reads a MCC marker (Multiple Component Collection)
5588 * @param p_header_data the data contained in the MCC box.
5589 * @param p_j2k the jpeg2000 codec.
5590 * @param p_header_size the size of the data contained in the MCC marker.
5591 * @param p_manager the user event manager.
5593 opj_bool opj_j2k_read_mcc ( opj_j2k_v2_t *p_j2k,
5594 OPJ_BYTE * p_header_data,
5595 OPJ_UINT32 p_header_size,
5596 opj_event_mgr_t * p_manager )
5601 opj_tcp_v2_t * l_tcp;
5602 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5603 opj_mct_data_t * l_mct_data;
5604 OPJ_UINT32 l_nb_collections;
5605 OPJ_UINT32 l_nb_comps;
5606 OPJ_UINT32 l_nb_bytes_by_comp;
5610 assert(p_header_data != 00);
5611 assert(p_j2k != 00);
5612 assert(p_manager != 00);
5614 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5615 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5616 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5618 if (p_header_size < 2) {
5619 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5624 opj_read_bytes(p_header_data,&l_tmp,2); /* Zmcc */
5627 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5631 if (p_header_size < 7) {
5632 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5636 opj_read_bytes(p_header_data,&l_indix,1); /* Imcc -> no need for other values, take the first */
5639 l_mcc_record = l_tcp->m_mcc_records;
5641 for(i=0;i<l_tcp->m_nb_mcc_records;++i) {
5642 if (l_mcc_record->m_index == l_indix) {
5649 if (i == l_tcp->m_nb_mcc_records) {
5650 if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
5651 l_tcp->m_nb_max_mcc_records += J2K_MCC_DEFAULT_NB_RECORDS;
5653 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
5654 opj_realloc(l_tcp->m_mcc_records,l_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
5655 if (! l_tcp->m_mcc_records) {
5656 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5659 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5660 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));
5662 l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
5664 l_mcc_record->m_index = l_indix;
5666 /* only one marker atm */
5667 opj_read_bytes(p_header_data,&l_tmp,2); /* Ymcc */
5670 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple data spanning\n");
5674 opj_read_bytes(p_header_data,&l_nb_collections,2); /* Qmcc -> number of collections -> 1 */
5677 if (l_nb_collections > 1) {
5678 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple collections\n");
5684 for (i=0;i<l_nb_collections;++i) {
5685 if (p_header_size < 3) {
5686 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5690 opj_read_bytes(p_header_data,&l_tmp,1); /* Xmcci type of component transformation -> array based decorrelation */
5694 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections other than array decorrelation\n");
5698 opj_read_bytes(p_header_data,&l_nb_comps,2);
5703 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5704 l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
5706 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
5707 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5711 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
5713 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5714 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Cmccij Component offset*/
5715 p_header_data+=l_nb_bytes_by_comp;
5718 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5723 opj_read_bytes(p_header_data,&l_nb_comps,2);
5726 l_nb_bytes_by_comp = 1 + (l_nb_comps>>15);
5727 l_nb_comps &= 0x7fff;
5729 if (l_nb_comps != l_mcc_record->m_nb_comps) {
5730 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections without same number of indixes\n");
5734 if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
5735 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5739 p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
5741 for (j=0;j<l_mcc_record->m_nb_comps;++j) {
5742 opj_read_bytes(p_header_data,&l_tmp,l_nb_bytes_by_comp); /* Wmccij Component offset*/
5743 p_header_data+=l_nb_bytes_by_comp;
5746 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge collections with indix shuffle\n");
5751 opj_read_bytes(p_header_data,&l_tmp,3); /* Wmccij Component offset*/
5754 l_mcc_record->m_is_irreversible = ! ((l_tmp>>16) & 1);
5755 l_mcc_record->m_decorrelation_array = 00;
5756 l_mcc_record->m_offset_array = 00;
5758 l_indix = l_tmp & 0xff;
5760 l_mct_data = l_tcp->m_mct_records;
5761 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5762 if (l_mct_data->m_index == l_indix) {
5763 l_mcc_record->m_decorrelation_array = l_mct_data;
5769 if (l_mcc_record->m_decorrelation_array == 00) {
5770 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5775 l_indix = (l_tmp >> 8) & 0xff;
5777 l_mct_data = l_tcp->m_mct_records;
5778 for (j=0;j<l_tcp->m_nb_mct_records;++j) {
5779 if (l_mct_data->m_index == l_indix) {
5780 l_mcc_record->m_offset_array = l_mct_data;
5786 if (l_mcc_record->m_offset_array == 00) {
5787 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5793 if (p_header_size != 0) {
5794 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCC marker\n");
5798 ++l_tcp->m_nb_mcc_records;
5805 * Writes the MCO marker (Multiple component transformation ordering)
5807 * @param p_stream the stream to write data to.
5808 * @param p_j2k J2K codec.
5809 * @param p_manager the user event manager.
5811 opj_bool j2k_write_mco( opj_j2k_v2_t *p_j2k,
5812 struct opj_stream_private *p_stream,
5813 struct opj_event_mgr * p_manager
5816 OPJ_BYTE * l_current_data = 00;
5817 OPJ_UINT32 l_mco_size;
5818 opj_tcp_v2_t * l_tcp = 00;
5819 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5823 assert(p_j2k != 00);
5824 assert(p_manager != 00);
5825 assert(p_stream != 00);
5827 l_tcp =&(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
5828 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
5830 l_mco_size = 5 + l_tcp->m_nb_mcc_records;
5831 if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
5833 p_j2k->m_specific_param.m_encoder.m_header_tile_data
5834 = (OPJ_BYTE*)opj_realloc(
5835 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
5837 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data)
5842 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
5845 opj_write_bytes(l_current_data,J2K_MS_MCO,2); /* MCO */
5846 l_current_data += 2;
5848 opj_write_bytes(l_current_data,l_mco_size-2,2); /* Lmco */
5849 l_current_data += 2;
5851 opj_write_bytes(l_current_data,l_tcp->m_nb_mcc_records,1); /* Nmco : only one tranform stage*/
5854 l_mcc_record = l_tcp->m_mcc_records;
5855 for (i=0;i<l_tcp->m_nb_mcc_records;++i) {
5856 opj_write_bytes(l_current_data,l_mcc_record->m_index,1); /* Imco -> use the mcc indicated by 1*/
5862 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) {
5870 * Reads a MCO marker (Multiple Component Transform Ordering)
5872 * @param p_header_data the data contained in the MCO box.
5873 * @param p_j2k the jpeg2000 codec.
5874 * @param p_header_size the size of the data contained in the MCO marker.
5875 * @param p_manager the user event manager.
5877 static opj_bool opj_j2k_read_mco ( opj_j2k_v2_t *p_j2k,
5878 OPJ_BYTE * p_header_data,
5879 OPJ_UINT32 p_header_size,
5880 opj_event_mgr_t * p_manager
5883 OPJ_UINT32 l_tmp, i;
5884 OPJ_UINT32 l_nb_stages;
5885 opj_tcp_v2_t * l_tcp;
5886 opj_tccp_t * l_tccp;
5887 opj_image_t * l_image;
5888 opj_image_comp_t * l_img_comp;
5891 assert(p_header_data != 00);
5892 assert(p_j2k != 00);
5893 assert(p_manager != 00);
5895 l_image = p_j2k->m_private_image;
5896 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
5897 &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
5898 p_j2k->m_specific_param.m_decoder.m_default_tcp;
5900 if (p_header_size < 1) {
5901 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading MCO marker\n");
5905 opj_read_bytes(p_header_data,&l_nb_stages,1); /* Nmco : only one tranform stage*/
5908 if (l_nb_stages > 1) {
5909 opj_event_msg_v2(p_manager, EVT_WARNING, "Cannot take in charge multiple transformation stages.\n");
5913 if (p_header_size != l_nb_stages + 1) {
5914 opj_event_msg_v2(p_manager, EVT_WARNING, "Error reading MCO marker\n");
5918 l_tccp = l_tcp->tccps;
5919 l_img_comp = l_image->comps;
5921 for (i=0;i<l_image->numcomps;++i) {
5922 l_tccp->m_dc_level_shift = 0;
5926 if (l_tcp->m_mct_decoding_matrix) {
5927 opj_free(l_tcp->m_mct_decoding_matrix);
5928 l_tcp->m_mct_decoding_matrix = 00;
5931 for (i=0;i<l_nb_stages;++i) {
5932 opj_read_bytes(p_header_data,&l_tmp,1);
5935 if (! j2k_add_mct(l_tcp,p_j2k->m_private_image,l_tmp)) {
5943 opj_bool j2k_add_mct(opj_tcp_v2_t * p_tcp, opj_image_t * p_image, OPJ_UINT32 p_index)
5946 opj_simple_mcc_decorrelation_data_t * l_mcc_record;
5947 opj_mct_data_t * l_deco_array, * l_offset_array;
5948 OPJ_UINT32 l_data_size,l_mct_size, l_offset_size;
5949 OPJ_UINT32 l_nb_elem;
5950 OPJ_UINT32 * l_offset_data, * l_current_offset_data;
5951 opj_tccp_t * l_tccp;
5954 assert(p_tcp != 00);
5956 l_mcc_record = p_tcp->m_mcc_records;
5958 for (i=0;i<p_tcp->m_nb_mcc_records;++i) {
5959 if (l_mcc_record->m_index == p_index) {
5964 if (i==p_tcp->m_nb_mcc_records) {
5965 /** element discarded **/
5969 if (l_mcc_record->m_nb_comps != p_image->numcomps) {
5970 /** do not support number of comps != image */
5974 l_deco_array = l_mcc_record->m_decorrelation_array;
5977 l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps * p_image->numcomps;
5978 if (l_deco_array->m_data_size != l_data_size) {
5982 l_nb_elem = p_image->numcomps * p_image->numcomps;
5983 l_mct_size = l_nb_elem * sizeof(OPJ_FLOAT32);
5984 p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
5986 if (! p_tcp->m_mct_decoding_matrix ) {
5990 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);
5993 l_offset_array = l_mcc_record->m_offset_array;
5995 if (l_offset_array) {
5996 l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] * p_image->numcomps;
5997 if (l_offset_array->m_data_size != l_data_size) {
6001 l_nb_elem = p_image->numcomps;
6002 l_offset_size = l_nb_elem * sizeof(OPJ_UINT32);
6003 l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
6005 if (! l_offset_data ) {
6009 j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](l_offset_array->m_data,l_offset_data,l_nb_elem);
6011 l_tccp = p_tcp->tccps;
6012 l_current_offset_data = l_offset_data;
6014 for (i=0;i<p_image->numcomps;++i) {
6015 l_tccp->m_dc_level_shift = *(l_current_offset_data++);
6019 opj_free(l_offset_data);
6026 * Writes the CBD marker (Component bit depth definition)
6028 * @param p_stream the stream to write data to.
6029 * @param p_j2k J2K codec.
6030 * @param p_manager the user event manager.
6032 opj_bool j2k_write_cbd( opj_j2k_v2_t *p_j2k,
6033 struct opj_stream_private *p_stream,
6034 struct opj_event_mgr * p_manager )
6037 OPJ_UINT32 l_cbd_size;
6038 OPJ_BYTE * l_current_data = 00;
6039 opj_image_t *l_image = 00;
6040 opj_image_comp_t * l_comp = 00;
6043 assert(p_j2k != 00);
6044 assert(p_manager != 00);
6045 assert(p_stream != 00);
6047 l_image = p_j2k->m_private_image;
6048 l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
6050 if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
6051 p_j2k->m_specific_param.m_encoder.m_header_tile_data
6052 = (OPJ_BYTE*)opj_realloc(
6053 p_j2k->m_specific_param.m_encoder.m_header_tile_data,
6056 if (! p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6060 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
6063 l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
6065 opj_write_bytes(l_current_data,J2K_MS_CBD,2); /* CBD */
6066 l_current_data += 2;
6068 opj_write_bytes(l_current_data,l_cbd_size-2,2); /* L_CBD */
6069 l_current_data += 2;
6071 opj_write_bytes(l_current_data,l_image->numcomps, 2); /* Ncbd */
6074 l_comp = l_image->comps;
6076 for (i=0;i<l_image->numcomps;++i) {
6077 opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1), 1); /* Component bit depth */
6083 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) {
6091 * Reads a CBD marker (Component bit depth definition)
6092 * @param p_header_data the data contained in the CBD box.
6093 * @param p_j2k the jpeg2000 codec.
6094 * @param p_header_size the size of the data contained in the CBD marker.
6095 * @param p_manager the user event manager.
6097 static opj_bool opj_j2k_read_cbd ( opj_j2k_v2_t *p_j2k,
6098 OPJ_BYTE * p_header_data,
6099 OPJ_UINT32 p_header_size,
6100 opj_event_mgr_t * p_manager
6103 OPJ_UINT32 l_nb_comp,l_num_comp;
6104 OPJ_UINT32 l_comp_def;
6106 opj_image_comp_t * l_comp = 00;
6109 assert(p_header_data != 00);
6110 assert(p_j2k != 00);
6111 assert(p_manager != 00);
6113 l_num_comp = p_j2k->m_private_image->numcomps;
6115 if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
6116 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6120 opj_read_bytes(p_header_data,&l_nb_comp,2); /* Ncbd */
6123 if (l_nb_comp != l_num_comp) {
6124 opj_event_msg_v2(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
6128 l_comp = p_j2k->m_private_image->comps;
6129 for (i=0;i<l_num_comp;++i) {
6130 opj_read_bytes(p_header_data,&l_comp_def,1); /* Component bit depth */
6132 l_comp->sgnd = (l_comp_def>>7) & 1;
6133 l_comp->prec = (l_comp_def&0x7f) + 1;
6141 /* ----------------------------------------------------------------------- */
6142 /* J2K / JPT decoder interface */
6143 /* ----------------------------------------------------------------------- */
6145 void opj_j2k_setup_decoder(opj_j2k_v2_t *j2k, opj_dparameters_t *parameters)
6147 if(j2k && parameters) {
6148 j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
6149 j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
6152 j2k->m_cp.correct = parameters->jpwl_correct;
6153 j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
6154 j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
6155 #endif /* USE_JPWL */
6160 /* ----------------------------------------------------------------------- */
6161 /* J2K encoder interface */
6162 /* ----------------------------------------------------------------------- */
6164 opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
6165 opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
6172 opj_j2k_v2_t* opj_j2k_create_compress_v2(void)
6174 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
6179 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
6181 l_j2k->m_is_decoder = 0;
6182 l_j2k->m_cp.m_is_decoder = 0;
6184 l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
6185 if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
6186 opj_j2k_destroy(l_j2k);
6190 l_j2k->m_specific_param.m_encoder.m_header_tile_data_size = J2K_DEFAULT_HEADER_SIZE;
6192 /* validation list creation*/
6193 l_j2k->m_validation_list = opj_procedure_list_create();
6194 if (! l_j2k->m_validation_list) {
6195 opj_j2k_destroy(l_j2k);
6199 /* execution list creation*/
6200 l_j2k->m_procedure_list = opj_procedure_list_create();
6201 if (! l_j2k->m_procedure_list) {
6202 opj_j2k_destroy(l_j2k);
6209 void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
6211 int tileno, numpocs_tile;
6212 opj_cp_t *cp = NULL;
6214 if(!j2k || !parameters || ! image) {
6218 /* create and initialize the coding parameters structure */
6219 cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
6221 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6224 /* set default values for cp */
6229 copy user encoding parameters
6231 cp->cinema = parameters->cp_cinema;
6232 cp->max_comp_size = parameters->max_comp_size;
6233 cp->rsiz = parameters->cp_rsiz;
6234 cp->disto_alloc = parameters->cp_disto_alloc;
6235 cp->fixed_alloc = parameters->cp_fixed_alloc;
6236 cp->fixed_quality = parameters->cp_fixed_quality;
6238 /* mod fixed_quality */
6239 if(parameters->cp_matrice) {
6240 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
6241 cp->matrice = (int *) opj_malloc(array_size);
6242 memcpy(cp->matrice, parameters->cp_matrice, array_size);
6246 cp->tdx = parameters->cp_tdx;
6247 cp->tdy = parameters->cp_tdy;
6250 cp->tx0 = parameters->cp_tx0;
6251 cp->ty0 = parameters->cp_ty0;
6253 /* comment string */
6254 if(parameters->cp_comment) {
6255 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6257 strcpy(cp->comment, parameters->cp_comment);
6262 calculate other encoding parameters
6265 if (parameters->tile_size_on) {
6266 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6267 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6269 cp->tdx = image->x1 - cp->tx0;
6270 cp->tdy = image->y1 - cp->ty0;
6273 if(parameters->tp_on){
6274 cp->tp_flag = parameters->tp_flag;
6279 for(i=0;i<image->numcomps ;i++){
6280 cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
6286 calculate JPWL encoding parameters
6289 if (parameters->jpwl_epc_on) {
6293 cp->epc_on = OPJ_TRUE;
6294 cp->info_on = OPJ_FALSE; /* no informative technique */
6297 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6298 cp->epb_on = OPJ_TRUE;
6300 cp->hprot_MH = parameters->jpwl_hprot_MH;
6301 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6302 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6303 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6305 /* if tile specs are not specified, copy MH specs */
6306 if (cp->hprot_TPH[0] == -1) {
6307 cp->hprot_TPH_tileno[0] = 0;
6308 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6310 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6311 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6312 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6313 cp->pprot[i] = parameters->jpwl_pprot[i];
6317 /* set ESD writing */
6318 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6319 cp->esd_on = OPJ_TRUE;
6321 cp->sens_size = parameters->jpwl_sens_size;
6322 cp->sens_addr = parameters->jpwl_sens_addr;
6323 cp->sens_range = parameters->jpwl_sens_range;
6325 cp->sens_MH = parameters->jpwl_sens_MH;
6326 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6327 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6328 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6332 /* always set RED writing to false: we are at the encoder */
6333 cp->red_on = OPJ_FALSE;
6336 cp->epc_on = OPJ_FALSE;
6338 #endif /* USE_JPWL */
6341 /* initialize the mutiple tiles */
6342 /* ---------------------------- */
6343 cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
6345 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6346 opj_tcp_t *tcp = &cp->tcps[tileno];
6347 tcp->numlayers = parameters->tcp_numlayers;
6348 assert ( tcp->numlayers >= 0 );
6349 for (j = 0; j < (OPJ_UINT32)tcp->numlayers; j++) {
6351 if (cp->fixed_quality) {
6352 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6354 tcp->rates[j] = parameters->tcp_rates[j];
6356 if (cp->fixed_quality) { /* add fixed_quality */
6357 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6359 tcp->rates[j] = parameters->tcp_rates[j];
6363 tcp->csty = parameters->csty;
6364 tcp->prg = parameters->prog_order;
6365 tcp->mct = parameters->tcp_mct;
6369 if (parameters->numpocs) {
6370 /* initialisation of POC */
6372 assert( parameters->numpocs >= 0 );
6373 for (i = 0; i < (OPJ_UINT32)parameters->numpocs; i++) {
6374 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6375 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6376 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6377 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6378 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6379 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6380 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6381 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6382 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6386 tcp->numpocs = numpocs_tile -1 ;
6391 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6393 for (i = 0; i < image->numcomps; i++) {
6394 opj_tccp_t *tccp = &tcp->tccps[i];
6395 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6396 tccp->numresolutions = parameters->numresolution;
6397 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6398 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6399 tccp->cblksty = parameters->mode;
6400 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6401 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6403 assert(parameters->roi_compno >= 0);
6404 if (i == (OPJ_UINT32)parameters->roi_compno) {
6405 tccp->roishift = parameters->roi_shift;
6410 if(parameters->cp_cinema)
6412 /*Precinct size for lowest frequency subband=128*/
6415 /*Precinct size at all other resolutions = 256*/
6416 for (j = 1; j < tccp->numresolutions; j++) {
6421 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6423 assert(tccp->numresolutions > 0);
6424 for (j = (OPJ_UINT32)(tccp->numresolutions - 1); (int)j >= 0; j--) {
6425 if (p < parameters->res_spec) {
6427 if (parameters->prcw_init[p] < 1) {
6430 tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
6433 if (parameters->prch_init[p] < 1) {
6436 tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
6440 int res_spec = parameters->res_spec;
6441 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6442 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6444 if (size_prcw < 1) {
6447 tccp->prcw[j] = int_floorlog2(size_prcw);
6450 if (size_prch < 1) {
6453 tccp->prch[j] = int_floorlog2(size_prch);
6457 /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
6460 for (j = 0; j < tccp->numresolutions; j++) {
6467 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6472 void opj_j2k_setup_encoder_v2( opj_j2k_v2_t *p_j2k,
6473 opj_cparameters_t *parameters,
6475 struct opj_event_mgr * p_manager)
6477 OPJ_UINT32 i, j, tileno, numpocs_tile;
6478 opj_cp_v2_t *cp = 00;
6481 if(!p_j2k || !parameters || ! image) {
6485 /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
6486 cp = &(p_j2k->m_cp);
6488 /* set default values for cp */
6493 copy user encoding parameters
6495 cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
6496 cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
6497 cp->rsiz = parameters->cp_rsiz;
6498 cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
6499 cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
6500 cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
6502 /* mod fixed_quality */
6503 if (parameters->cp_matrice) {
6504 size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
6505 cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
6506 memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
6510 cp->tdx = parameters->cp_tdx;
6511 cp->tdy = parameters->cp_tdy;
6514 cp->tx0 = parameters->cp_tx0;
6515 cp->ty0 = parameters->cp_ty0;
6517 /* comment string */
6518 if(parameters->cp_comment) {
6519 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
6521 strcpy(cp->comment, parameters->cp_comment);
6526 calculate other encoding parameters
6529 if (parameters->tile_size_on) {
6530 cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
6531 cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
6533 cp->tdx = image->x1 - cp->tx0;
6534 cp->tdy = image->y1 - cp->ty0;
6537 if (parameters->tp_on) {
6538 cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
6539 cp->m_specific_param.m_enc.m_tp_on = 1;
6544 calculate JPWL encoding parameters
6547 if (parameters->jpwl_epc_on) {
6551 cp->epc_on = OPJ_TRUE;
6552 cp->info_on = OPJ_FALSE; /* no informative technique */
6555 if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
6556 cp->epb_on = OPJ_TRUE;
6558 cp->hprot_MH = parameters->jpwl_hprot_MH;
6559 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6560 cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
6561 cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
6563 /* if tile specs are not specified, copy MH specs */
6564 if (cp->hprot_TPH[0] == -1) {
6565 cp->hprot_TPH_tileno[0] = 0;
6566 cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
6568 for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
6569 cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
6570 cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
6571 cp->pprot[i] = parameters->jpwl_pprot[i];
6575 /* set ESD writing */
6576 if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
6577 cp->esd_on = OPJ_TRUE;
6579 cp->sens_size = parameters->jpwl_sens_size;
6580 cp->sens_addr = parameters->jpwl_sens_addr;
6581 cp->sens_range = parameters->jpwl_sens_range;
6583 cp->sens_MH = parameters->jpwl_sens_MH;
6584 for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
6585 cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
6586 cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
6590 /* always set RED writing to false: we are at the encoder */
6591 cp->red_on = OPJ_FALSE;
6594 cp->epc_on = OPJ_FALSE;
6596 #endif /* USE_JPWL */
6599 /* initialize the mutiple tiles */
6600 /* ---------------------------- */
6601 cp->tcps = (opj_tcp_v2_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_v2_t));
6602 if (parameters->numpocs) {
6603 /* initialisation of POC */
6604 l_res = j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
6608 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
6609 opj_tcp_v2_t *tcp = &cp->tcps[tileno];
6610 tcp->numlayers = parameters->tcp_numlayers;
6612 for (j = 0; j < tcp->numlayers; j++) {
6613 if(cp->m_specific_param.m_enc.m_cinema){
6614 if (cp->m_specific_param.m_enc.m_fixed_quality) {
6615 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6617 tcp->rates[j] = parameters->tcp_rates[j];
6619 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
6620 tcp->distoratio[j] = parameters->tcp_distoratio[j];
6622 tcp->rates[j] = parameters->tcp_rates[j];
6627 tcp->csty = parameters->csty;
6628 tcp->prg = parameters->prog_order;
6629 tcp->mct = parameters->tcp_mct;
6634 if (parameters->numpocs) {
6635 /* initialisation of POC */
6638 for (i = 0; i < (unsigned int) parameters->numpocs; i++) {
6639 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
6640 opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
6642 tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
6643 tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
6644 tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
6645 tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
6646 tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
6647 tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
6648 tcp_poc->tile = parameters->POC[numpocs_tile].tile;
6654 tcp->numpocs = numpocs_tile -1 ;
6659 tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
6661 if (parameters->mct_data) {
6663 opj_event_msg_v2(p_manager, EVT_ERROR, "MCT not supported for now\n");
6666 /* TODO MSD : merge v2 add invert.c or used a external lib ?
6667 OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * sizeof(OPJ_FLOAT32);
6668 OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6669 OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
6672 tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6673 memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
6674 memcpy(lTmpBuf,parameters->mct_data,lMctSize);
6676 tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
6677 assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
6679 tcp->mct_norms = (OPJ_FLOAT64*)
6680 opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
6682 opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
6685 for (i = 0; i < image->numcomps; i++) {
6686 opj_tccp_t *tccp = &tcp->tccps[i];
6687 tccp->m_dc_level_shift = l_dc_shift[i];
6690 opj_j2k_setup_mct_encoding(tcp,image);
6694 for (i = 0; i < image->numcomps; i++) {
6695 opj_tccp_t *tccp = &tcp->tccps[i];
6696 opj_image_comp_t * l_comp = &(image->comps[i]);
6698 if (! l_comp->sgnd) {
6699 tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
6704 for (i = 0; i < image->numcomps; i++) {
6705 opj_tccp_t *tccp = &tcp->tccps[i];
6707 tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
6708 tccp->numresolutions = parameters->numresolution;
6709 tccp->cblkw = int_floorlog2(parameters->cblockw_init);
6710 tccp->cblkh = int_floorlog2(parameters->cblockh_init);
6711 tccp->cblksty = parameters->mode;
6712 tccp->qmfbid = parameters->irreversible ? 0 : 1;
6713 tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
6716 if (i == parameters->roi_compno) {
6717 tccp->roishift = parameters->roi_shift;
6722 if(parameters->cp_cinema) {
6723 //Precinct size for lowest frequency subband=128
6726 //Precinct size at all other resolutions = 256
6727 for (j = 1; j < tccp->numresolutions; j++) {
6732 if (parameters->csty & J2K_CCP_CSTY_PRT) {
6733 OPJ_INT32 p = 0, it_res;
6734 for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
6735 if (p < parameters->res_spec) {
6737 if (parameters->prcw_init[p] < 1) {
6738 tccp->prcw[it_res] = 1;
6740 tccp->prcw[it_res] = int_floorlog2(parameters->prcw_init[p]);
6743 if (parameters->prch_init[p] < 1) {
6744 tccp->prch[it_res] = 1;
6746 tccp->prch[it_res] = int_floorlog2(parameters->prch_init[p]);
6750 int res_spec = parameters->res_spec;
6751 int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
6752 int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
6754 if (size_prcw < 1) {
6755 tccp->prcw[it_res] = 1;
6757 tccp->prcw[it_res] = int_floorlog2(size_prcw);
6760 if (size_prch < 1) {
6761 tccp->prch[it_res] = 1;
6763 tccp->prch[it_res] = int_floorlog2(size_prch);
6767 /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
6770 for (j = 0; j < tccp->numresolutions; j++) {
6777 dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
6781 if (parameters->mct_data) {
6782 opj_free(parameters->mct_data);
6783 parameters->mct_data = 00;
6790 static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
6792 assert(cstr_info != 00);
6794 /* expand the list? */
6795 if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
6796 cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
6797 cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
6800 /* add the marker */
6801 cstr_info->marker[cstr_info->marknum].type = type;
6802 cstr_info->marker[cstr_info->marknum].pos = pos;
6803 cstr_info->marker[cstr_info->marknum].len = len;
6804 cstr_info->marknum++;
6808 static void j2k_add_mhmarker_v2(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
6810 assert(cstr_index != 00);
6812 /* expand the list? */
6813 if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
6814 cstr_index->maxmarknum = 100 + (int) ((float) cstr_index->maxmarknum * 1.0F);
6815 cstr_index->marker = (opj_marker_info_t*)opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
6818 /* add the marker */
6819 cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
6820 cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
6821 cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
6822 cstr_index->marknum++;
6826 static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len)
6828 opj_marker_info_t *marker;
6830 assert(cstr_info != 00);
6832 /* expand the list? */
6833 if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
6834 cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
6835 cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
6838 marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
6840 /* add the marker */
6841 marker->type = type;
6844 cstr_info->tile[tileno].marknum++;
6847 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)
6849 assert(cstr_index != 00);
6850 assert(cstr_index->tile_index != 00);
6852 /* expand the list? */
6853 if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
6854 cstr_index->tile_index[tileno].maxmarknum = 100 + (int) ((float) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
6855 cstr_index->tile_index[tileno].marker =
6856 (opj_marker_info_t*)opj_realloc(cstr_index->tile_index[tileno].marker,
6857 cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
6860 /* add the marker */
6861 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type = (OPJ_UINT16)type;
6862 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos = (OPJ_INT32)pos;
6863 cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len = (OPJ_INT32)len;
6864 cstr_index->tile_index[tileno].marknum++;
6866 if (type == J2K_MS_SOT) {
6867 OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
6869 if (cstr_index->tile_index[tileno].tp_index)
6870 cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
6877 * -----------------------------------------------------------------------
6878 * -----------------------------------------------------------------------
6879 * -----------------------------------------------------------------------
6883 * Ends the decompression procedures and possibiliy add data to be read after the
6886 opj_bool opj_j2k_end_decompress(opj_j2k_v2_t *p_j2k,
6887 opj_stream_private_t *p_stream,
6888 opj_event_mgr_t * p_manager
6898 * Reads a jpeg2000 codestream header structure.
6901 * @param p_stream the stream to read data from.
6902 * @param p_j2k the jpeg2000 codec.
6903 * @param p_manager the user event manager.
6905 * @return true if the box is valid.
6907 opj_bool opj_j2k_read_header( opj_stream_private_t *p_stream,
6908 opj_j2k_v2_t* p_j2k,
6909 opj_image_t** p_image,
6910 opj_event_mgr_t* p_manager )
6913 assert(p_j2k != 00);
6914 assert(p_stream != 00);
6915 assert(p_manager != 00);
6917 /* create an empty image header */
6918 p_j2k->m_private_image = opj_image_create0();
6919 if (! p_j2k->m_private_image) {
6923 /* customization of the validation */
6924 opj_j2k_setup_decoding_validation(p_j2k);
6926 /* validation of the parameters codec */
6927 if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream,p_manager)) {
6928 opj_image_destroy(p_j2k->m_private_image);
6929 p_j2k->m_private_image = NULL;
6933 /* customization of the encoding */
6934 opj_j2k_setup_header_reading(p_j2k);
6937 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
6938 opj_image_destroy(p_j2k->m_private_image);
6939 p_j2k->m_private_image = NULL;
6943 *p_image = opj_image_create0();
6948 /* Copy codestream image information to the output image */
6949 opj_copy_image_header(p_j2k->m_private_image, *p_image);
6951 /*Allocate and initialize some elements of codestrem index*/
6952 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
6960 * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
6962 void opj_j2k_setup_header_reading (opj_j2k_v2_t *p_j2k)
6965 assert(p_j2k != 00);
6967 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_read_header_procedure);
6969 /* DEVELOPER CORNER, add your custom procedures */
6970 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd);
6975 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
6976 * are valid. Developpers wanting to extend the library can add their own validation procedures.
6978 void opj_j2k_setup_decoding_validation (opj_j2k_v2_t *p_j2k)
6981 assert(p_j2k != 00);
6983 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_decoder);
6984 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_decoding_validation);
6985 /* DEVELOPER CORNER, add your custom validation procedure */
6991 * The mct encoding validation procedure.
6993 * @param p_j2k the jpeg2000 codec to validate.
6994 * @param p_stream the input stream to validate.
6995 * @param p_manager the user event manager.
6997 * @return true if the parameters are correct.
6999 opj_bool opj_j2k_mct_validation ( opj_j2k_v2_t * p_j2k,
7000 opj_stream_private_t *p_stream,
7001 opj_event_mgr_t * p_manager )
7003 opj_bool l_is_valid = OPJ_TRUE;
7007 assert(p_j2k != 00);
7008 assert(p_stream != 00);
7009 assert(p_manager != 00);
7011 if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
7012 OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7013 opj_tcp_v2_t * l_tcp = p_j2k->m_cp.tcps;
7015 for (i=0;i<l_nb_tiles;++i) {
7016 if (l_tcp->mct == 2) {
7017 opj_tccp_t * l_tccp = l_tcp->tccps;
7018 l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
7020 for (j=0;j<p_j2k->m_private_image->numcomps;++j) {
7021 l_is_valid &= ! (l_tccp->qmfbid & 1);
7032 opj_bool opj_j2k_setup_mct_encoding(opj_tcp_v2_t * p_tcp, opj_image_t * p_image)
7035 OPJ_UINT32 l_indix = 1;
7036 opj_mct_data_t * l_mct_deco_data = 00,* l_mct_offset_data = 00;
7037 opj_simple_mcc_decorrelation_data_t * l_mcc_data;
7038 OPJ_UINT32 l_mct_size,l_nb_elem;
7039 OPJ_FLOAT32 * l_data, * l_current_data;
7040 opj_tccp_t * l_tccp;
7043 assert(p_tcp != 00);
7045 if (p_tcp->mct != 2) {
7049 if (p_tcp->m_mct_decoding_matrix) {
7050 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7051 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7053 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));
7054 if (! p_tcp->m_mct_records) {
7057 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7059 memset(l_mct_deco_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7061 l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7063 if (l_mct_deco_data->m_data) {
7064 opj_free(l_mct_deco_data->m_data);
7065 l_mct_deco_data->m_data = 00;
7068 l_mct_deco_data->m_index = l_indix++;
7069 l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
7070 l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
7071 l_nb_elem = p_image->numcomps * p_image->numcomps;
7072 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
7073 l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7075 if (! l_mct_deco_data->m_data) {
7079 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);
7081 l_mct_deco_data->m_data_size = l_mct_size;
7082 ++p_tcp->m_nb_mct_records;
7085 if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
7086 p_tcp->m_nb_max_mct_records += J2K_MCT_DEFAULT_NB_RECORDS;
7087 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));
7089 if (! p_tcp->m_mct_records) {
7093 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7094 memset(l_mct_offset_data ,0,(p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(opj_mct_data_t));
7096 if (l_mct_deco_data) {
7097 l_mct_deco_data = l_mct_offset_data - 1;
7101 l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
7103 if (l_mct_offset_data->m_data) {
7104 opj_free(l_mct_offset_data->m_data);
7105 l_mct_offset_data->m_data = 00;
7108 l_mct_offset_data->m_index = l_indix++;
7109 l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
7110 l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
7111 l_nb_elem = p_image->numcomps;
7112 l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
7113 l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size );
7115 if (! l_mct_offset_data->m_data) {
7119 l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
7121 opj_free(l_mct_offset_data->m_data);
7122 l_mct_offset_data->m_data = 00;
7126 l_tccp = p_tcp->tccps;
7127 l_current_data = l_data;
7129 for (i=0;i<l_nb_elem;++i) {
7130 *(l_current_data++) = (OPJ_FLOAT32) (l_tccp->m_dc_level_shift);
7134 j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,l_mct_offset_data->m_data,l_nb_elem);
7138 l_mct_offset_data->m_data_size = l_mct_size;
7140 ++p_tcp->m_nb_mct_records;
7142 if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
7143 p_tcp->m_nb_max_mcc_records += J2K_MCT_DEFAULT_NB_RECORDS;
7144 p_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*)
7145 opj_realloc(p_tcp->m_mcc_records,p_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t));
7147 if (! p_tcp->m_mcc_records) {
7150 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7151 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));
7155 l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
7156 l_mcc_data->m_decorrelation_array = l_mct_deco_data;
7157 l_mcc_data->m_is_irreversible = 1;
7158 l_mcc_data->m_nb_comps = p_image->numcomps;
7159 l_mcc_data->m_index = l_indix++;
7160 l_mcc_data->m_offset_array = l_mct_offset_data;
7161 ++p_tcp->m_nb_mcc_records;
7167 * Builds the cp decoder parameters to use to decode tile.
7169 opj_bool opj_j2k_build_decoder (opj_j2k_v2_t * p_j2k,
7170 opj_stream_private_t *p_stream,
7171 opj_event_mgr_t * p_manager )
7173 /* add here initialization of cp
7174 copy paste of setup_decoder */
7182 * Builds the cp encoder parameters to use to encode tile.
7184 opj_bool opj_j2k_build_encoder (opj_j2k_v2_t * p_j2k,
7185 opj_stream_private_t *p_stream,
7186 opj_event_mgr_t * p_manager )
7188 /* add here initialization of cp
7189 copy paste of setup_encoder */
7197 * The default encoding validation procedure without any extension.
7199 * @param p_j2k the jpeg2000 codec to validate.
7200 * @param p_stream the input stream to validate.
7201 * @param p_manager the user event manager.
7203 * @return true if the parameters are correct.
7205 opj_bool opj_j2k_encoding_validation ( opj_j2k_v2_t * p_j2k,
7206 opj_stream_private_t *p_stream,
7207 opj_event_mgr_t * p_manager )
7209 opj_bool l_is_valid = OPJ_TRUE;
7212 assert(p_j2k != 00);
7213 assert(p_stream != 00);
7214 assert(p_manager != 00);
7216 /* STATE checking */
7217 /* make sure the state is at 0 */
7218 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
7220 /* POINTER validation */
7221 /* make sure a p_j2k codec is present */
7222 l_is_valid &= (p_j2k->m_procedure_list != 00);
7223 /* make sure a validation list is present */
7224 l_is_valid &= (p_j2k->m_validation_list != 00);
7226 if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7227 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7231 if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
7232 opj_event_msg_v2(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
7236 /* PARAMETER VALIDATION */
7241 * The default decoding validation procedure without any extension.
7243 * @param p_j2k the jpeg2000 codec to validate.
7244 * @param p_stream the input stream to validate.
7245 * @param p_manager the user event manager.
7247 * @return true if the parameters are correct.
7249 opj_bool opj_j2k_decoding_validation ( opj_j2k_v2_t *p_j2k,
7250 opj_stream_private_t *p_stream,
7251 opj_event_mgr_t * p_manager
7254 opj_bool l_is_valid = OPJ_TRUE;
7257 assert(p_j2k != 00);
7258 assert(p_stream != 00);
7259 assert(p_manager != 00);
7262 /* STATE checking */
7263 /* make sure the state is at 0 */
7265 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
7267 l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
7269 /* POINTER validation */
7270 /* make sure a p_j2k codec is present */
7271 /* make sure a procedure list is present */
7272 l_is_valid &= (p_j2k->m_procedure_list != 00);
7273 /* make sure a validation list is present */
7274 l_is_valid &= (p_j2k->m_validation_list != 00);
7276 /* PARAMETER VALIDATION */
7280 opj_bool opj_j2k_read_header_procedure( opj_j2k_v2_t *p_j2k,
7281 opj_stream_private_t *p_stream,
7282 opj_event_mgr_t * p_manager)
7284 OPJ_UINT32 l_current_marker;
7285 OPJ_UINT32 l_marker_size;
7286 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7289 assert(p_stream != 00);
7290 assert(p_j2k != 00);
7291 assert(p_manager != 00);
7293 /* We enter in the main header */
7294 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
7296 /* Try to read the SOC marker, the codestream must begin with SOC marker */
7297 if (! opj_j2k_read_soc(p_j2k,p_stream,p_manager)) {
7298 opj_event_msg_v2(p_manager, EVT_ERROR, "Expected a SOC marker \n");
7302 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7303 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7304 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7308 /* Read 2 bytes as the new marker ID */
7309 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7311 /* Try to read until the SOT is detected */
7312 while (l_current_marker != J2K_MS_SOT) {
7314 /* Check if the current marker ID is valid */
7315 if (l_current_marker < 0xff00) {
7316 opj_event_msg_v2(p_manager, EVT_ERROR, "We expected read a marker ID (0xff--) instead of %.8x\n", l_current_marker);
7320 /* Get the marker handler from the marker ID */
7321 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7323 /* Manage case where marker is unknown */
7324 if (l_marker_handler->id == J2K_MS_UNK) {
7325 if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)){
7326 opj_event_msg_v2(p_manager, EVT_ERROR, "Unknow marker have been detected and generated error.\n");
7330 if (l_current_marker == J2K_MS_SOT)
7331 break; /* SOT marker is detected main header is completely read */
7332 else /* Get the marker handler from the marker ID */
7333 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7336 /* Check if the marker is known and if it is the right place to find it */
7337 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7338 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7342 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7343 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7344 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7348 /* read 2 bytes as the marker size */
7349 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7350 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7352 /* Check if the marker size is compatible with the header data size */
7353 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7354 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
7355 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
7356 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
7359 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7362 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7363 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) {
7364 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7368 /* Read the marker segment with the correct marker handler */
7369 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7370 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker handler function failed to read the marker segment\n");
7374 /* Add the marker to the codestream index*/
7375 j2k_add_mhmarker_v2(p_j2k->cstr_index,
7376 l_marker_handler->id,
7377 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7378 l_marker_size + 4 );
7380 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7381 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7382 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7386 /* read 2 bytes as the new marker ID */
7387 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7390 opj_event_msg_v2(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
7392 /* Position of the last element if the main header */
7393 p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
7395 /* Next step: read a tile-part header */
7396 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7402 * Excutes the given procedures on the given codec.
7404 * @param p_procedure_list the list of procedures to execute
7405 * @param p_j2k the jpeg2000 codec to execute the procedures on.
7406 * @param p_stream the stream to execute the procedures on.
7407 * @param p_manager the user manager.
7409 * @return true if all the procedures were successfully executed.
7411 opj_bool opj_j2k_exec ( opj_j2k_v2_t * p_j2k,
7412 opj_procedure_list_t * p_procedure_list,
7413 opj_stream_private_t *p_stream,
7414 opj_event_mgr_t * p_manager )
7416 opj_bool (** l_procedure) (opj_j2k_v2_t * ,opj_stream_private_t *,opj_event_mgr_t *) = 00;
7417 opj_bool l_result = OPJ_TRUE;
7418 OPJ_UINT32 l_nb_proc, i;
7421 assert(p_procedure_list != 00);
7422 assert(p_j2k != 00);
7423 assert(p_stream != 00);
7424 assert(p_manager != 00);
7427 l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
7428 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);
7430 for (i=0;i<l_nb_proc;++i) {
7431 l_result = l_result && ((*l_procedure) (p_j2k,p_stream,p_manager));
7435 /* and clear the procedure list at the end.*/
7436 opj_procedure_list_clear(p_procedure_list);
7441 static opj_bool opj_j2k_copy_default_tcp_and_create_tcd ( opj_j2k_v2_t * p_j2k,
7442 opj_stream_private_t *p_stream,
7443 opj_event_mgr_t * p_manager
7446 opj_tcp_v2_t * l_tcp = 00;
7447 opj_tcp_v2_t * l_default_tcp = 00;
7448 OPJ_UINT32 l_nb_tiles;
7450 opj_tccp_t *l_current_tccp = 00;
7451 OPJ_UINT32 l_tccp_size;
7452 OPJ_UINT32 l_mct_size;
7453 opj_image_t * l_image;
7454 OPJ_UINT32 l_mcc_records_size,l_mct_records_size;
7455 opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
7456 opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
7457 OPJ_UINT32 l_offset;
7460 assert(p_j2k != 00);
7461 assert(p_stream != 00);
7462 assert(p_manager != 00);
7464 l_image = p_j2k->m_private_image;
7465 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
7466 l_tcp = p_j2k->m_cp.tcps;
7467 l_tccp_size = l_image->numcomps * sizeof(opj_tccp_t);
7468 l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
7469 l_mct_size = l_image->numcomps * l_image->numcomps * sizeof(OPJ_FLOAT32);
7472 for (i=0; i<l_nb_tiles; ++i) {
7473 /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
7474 l_current_tccp = l_tcp->tccps;
7475 /*Copy default coding parameters into the current tile coding parameters*/
7476 memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_v2_t));
7477 /* Initialize some values of the current tile coding parameters*/
7479 l_tcp->ppt_data = 00;
7480 /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
7481 l_tcp->tccps = l_current_tccp;
7483 /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
7484 if (l_default_tcp->m_mct_decoding_matrix) {
7485 l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
7486 if (! l_tcp->m_mct_decoding_matrix ) {
7489 memcpy(l_tcp->m_mct_decoding_matrix,l_default_tcp->m_mct_decoding_matrix,l_mct_size);
7492 /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
7493 l_mct_records_size = l_default_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t);
7494 l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
7495 if (! l_tcp->m_mct_records) {
7498 memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records,l_mct_records_size);
7500 /* Copy the mct record data from dflt_tile_cp to the current tile*/
7501 l_src_mct_rec = l_default_tcp->m_mct_records;
7502 l_dest_mct_rec = l_tcp->m_mct_records;
7504 for (j=0;j<l_default_tcp->m_nb_mct_records;++j) {
7506 if (l_src_mct_rec->m_data) {
7508 l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
7509 if(! l_dest_mct_rec->m_data) {
7512 memcpy(l_dest_mct_rec->m_data,l_src_mct_rec->m_data,l_src_mct_rec->m_data_size);
7519 /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
7520 l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * sizeof(opj_simple_mcc_decorrelation_data_t);
7521 l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(l_mcc_records_size);
7522 if (! l_tcp->m_mcc_records) {
7525 memcpy(l_tcp->m_mcc_records,l_default_tcp->m_mcc_records,l_mcc_records_size);
7527 /* Copy the mcc record data from dflt_tile_cp to the current tile*/
7528 l_src_mcc_rec = l_default_tcp->m_mcc_records;
7529 l_dest_mcc_rec = l_tcp->m_mcc_records;
7531 for (j=0;j<l_default_tcp->m_nb_max_mcc_records;++j) {
7533 if (l_src_mcc_rec->m_decorrelation_array) {
7534 l_offset = l_src_mcc_rec->m_decorrelation_array - l_default_tcp->m_mct_records;
7535 l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
7538 if (l_src_mcc_rec->m_offset_array) {
7539 l_offset = l_src_mcc_rec->m_offset_array - l_default_tcp->m_mct_records;
7540 l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
7547 /* Copy all the dflt_tile_compo_cp to the current tile cp */
7548 memcpy(l_current_tccp,l_default_tcp->tccps,l_tccp_size);
7550 /* Move to next tile cp*/
7554 /* Create the current tile decoder*/
7555 p_j2k->m_tcd = (opj_tcd_v2_t*)tcd_create_v2(OPJ_TRUE); /* FIXME why a cast ? */
7556 if (! p_j2k->m_tcd ) {
7560 if ( !tcd_init_v2(p_j2k->m_tcd, l_image, &(p_j2k->m_cp)) ) {
7561 tcd_destroy_v2(p_j2k->m_tcd);
7563 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
7571 * Reads the lookup table containing all the marker, status and action, and returns the handler associated
7572 * with the marker value.
7573 * @param p_id Marker value to look up
7575 * @return the handler associated with the id.
7577 const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler (OPJ_UINT32 p_id)
7579 const opj_dec_memory_marker_handler_t *e;
7580 for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
7581 if (e->id == p_id) {
7582 break; /* we find a handler corresponding to the marker ID*/
7590 * Destroys a jpeg2000 codec.
7592 * @param p_j2k the jpeg20000 structure to destroy.
7594 void opj_j2k_destroy (opj_j2k_v2_t *p_j2k)
7600 if (p_j2k->m_is_decoder) {
7602 if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
7603 opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7604 opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
7605 p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
7608 if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
7609 opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
7610 p_j2k->m_specific_param.m_decoder.m_header_data = 00;
7611 p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
7616 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
7617 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
7618 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
7621 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
7622 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
7623 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
7624 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
7627 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
7628 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
7629 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
7630 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
7634 tcd_destroy_v2(p_j2k->m_tcd);
7636 opj_j2k_cp_destroy(&(p_j2k->m_cp));
7637 memset(&(p_j2k->m_cp),0,sizeof(opj_cp_v2_t));
7639 opj_procedure_list_destroy(p_j2k->m_procedure_list);
7640 p_j2k->m_procedure_list = 00;
7642 opj_procedure_list_destroy(p_j2k->m_validation_list);
7643 p_j2k->m_procedure_list = 00;
7645 j2k_destroy_cstr_index(p_j2k->cstr_index);
7646 p_j2k->cstr_index = NULL;
7648 opj_image_destroy(p_j2k->m_private_image);
7649 p_j2k->m_private_image = NULL;
7651 opj_image_destroy(p_j2k->m_output_image);
7652 p_j2k->m_output_image = NULL;
7657 void j2k_destroy_cstr_index (opj_codestream_index_t *p_cstr_ind)
7661 if (p_cstr_ind->marker) {
7662 opj_free(p_cstr_ind->marker);
7663 p_cstr_ind->marker = NULL;
7666 if (p_cstr_ind->tile_index) {
7667 OPJ_UINT32 it_tile = 0;
7669 for (it_tile=0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
7671 if(p_cstr_ind->tile_index[it_tile].packet_index) {
7672 opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
7673 p_cstr_ind->tile_index[it_tile].packet_index = NULL;
7676 if(p_cstr_ind->tile_index[it_tile].tp_index){
7677 opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
7678 p_cstr_ind->tile_index[it_tile].tp_index = NULL;
7681 if(p_cstr_ind->tile_index[it_tile].marker){
7682 opj_free(p_cstr_ind->tile_index[it_tile].marker);
7683 p_cstr_ind->tile_index[it_tile].marker = NULL;
7688 opj_free( p_cstr_ind->tile_index);
7689 p_cstr_ind->tile_index = NULL;
7692 opj_free(p_cstr_ind);
7699 * Destroys a tile coding parameter structure.
7701 * @param p_tcp the tile coding parameter to destroy.
7703 void opj_j2k_tcp_destroy (opj_tcp_v2_t *p_tcp)
7709 if (p_tcp->ppt_buffer != 00) {
7710 opj_free(p_tcp->ppt_buffer);
7711 p_tcp->ppt_buffer = 00;
7714 if (p_tcp->tccps != 00) {
7715 opj_free(p_tcp->tccps);
7719 if (p_tcp->m_mct_coding_matrix != 00) {
7720 opj_free(p_tcp->m_mct_coding_matrix);
7721 p_tcp->m_mct_coding_matrix = 00;
7724 if (p_tcp->m_mct_decoding_matrix != 00) {
7725 opj_free(p_tcp->m_mct_decoding_matrix);
7726 p_tcp->m_mct_decoding_matrix = 00;
7729 if (p_tcp->m_mcc_records) {
7730 opj_free(p_tcp->m_mcc_records);
7731 p_tcp->m_mcc_records = 00;
7732 p_tcp->m_nb_max_mcc_records = 0;
7733 p_tcp->m_nb_mcc_records = 0;
7736 if (p_tcp->m_mct_records) {
7737 opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
7740 for (i=0;i<p_tcp->m_nb_mct_records;++i) {
7741 if (l_mct_data->m_data) {
7742 opj_free(l_mct_data->m_data);
7743 l_mct_data->m_data = 00;
7749 opj_free(p_tcp->m_mct_records);
7750 p_tcp->m_mct_records = 00;
7753 if (p_tcp->mct_norms != 00) {
7754 opj_free(p_tcp->mct_norms);
7755 p_tcp->mct_norms = 00;
7758 opj_j2k_tcp_data_destroy(p_tcp);
7763 * Destroys the data inside a tile coding parameter structure.
7765 * @param p_tcp the tile coding parameter which contain data to destroy.
7767 void opj_j2k_tcp_data_destroy (opj_tcp_v2_t *p_tcp)
7769 if (p_tcp->m_data) {
7770 opj_free(p_tcp->m_data);
7771 p_tcp->m_data = NULL;
7772 p_tcp->m_data_size = 0;
7777 * Destroys a coding parameter structure.
7779 * @param p_cp the coding parameter to destroy.
7781 void opj_j2k_cp_destroy (opj_cp_v2_t *p_cp)
7783 OPJ_UINT32 l_nb_tiles;
7784 opj_tcp_v2_t * l_current_tile = 00;
7795 l_current_tile = p_cp->tcps;
7796 l_nb_tiles = p_cp->th * p_cp->tw;
7799 (i = 0; i < l_nb_tiles; ++i)
7801 opj_j2k_tcp_destroy(l_current_tile);
7804 opj_free(p_cp->tcps);
7808 (p_cp->ppm_buffer != 00)
7810 opj_free(p_cp->ppm_buffer);
7811 p_cp->ppm_buffer = 00;
7814 (p_cp->comment != 00)
7816 opj_free(p_cp->comment);
7820 (! p_cp->m_is_decoder)
7823 (p_cp->m_specific_param.m_enc.m_matrice)
7825 opj_free(p_cp->m_specific_param.m_enc.m_matrice);
7826 p_cp->m_specific_param.m_enc.m_matrice = 00;
7834 * Reads a tile header.
7835 * @param p_j2k the jpeg2000 codec.
7836 * @param p_stream the stream to write data to.
7837 * @param p_manager the user event manager.
7839 opj_bool opj_j2k_read_tile_header( opj_j2k_v2_t * p_j2k,
7840 OPJ_UINT32 * p_tile_index,
7841 OPJ_UINT32 * p_data_size,
7842 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
7843 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
7844 OPJ_UINT32 * p_nb_comps,
7846 opj_stream_private_t *p_stream,
7847 opj_event_mgr_t * p_manager )
7849 OPJ_UINT32 l_current_marker = J2K_MS_SOT;
7850 OPJ_UINT32 l_marker_size;
7851 const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
7852 opj_tcp_v2_t * l_tcp = NULL;
7853 OPJ_UINT32 l_nb_tiles;
7856 assert(p_stream != 00);
7857 assert(p_j2k != 00);
7858 assert(p_manager != 00);
7860 /* Reach the End Of Codestream ?*/
7861 if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC){
7862 l_current_marker = J2K_MS_EOC;
7864 /* We need to encounter a SOT marker (a new tile-part header) */
7865 else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT){
7869 /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
7870 while ( (!p_j2k->m_specific_param.m_decoder.m_can_decode) && (l_current_marker != J2K_MS_EOC) ) {
7872 /* Try to read until the Start Of Data is detected */
7873 while (l_current_marker != J2K_MS_SOD) {
7875 /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
7876 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7877 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7881 /* Read 2 bytes from the buffer as the marker size */
7882 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
7884 /* Why this condition? FIXME */
7885 if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH){
7886 p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
7888 l_marker_size -= 2; /* Subtract the size of the marker ID already read */
7890 /* Get the marker handler from the marker ID */
7891 l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
7893 /* Check if the marker is known and if it is the right place to find it */
7894 if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
7895 opj_event_msg_v2(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
7898 /* FIXME manage case of unknown marker as in the main header ? */
7900 /* Check if the marker size is compatible with the header data size */
7901 if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
7902 p_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE*)
7903 opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size);
7904 if (p_j2k->m_specific_param.m_decoder.m_header_data == 00) {
7907 p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
7910 /* Try to read the rest of the marker segment from stream and copy them into the buffer */
7911 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) {
7912 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7916 /* Read the marker segment with the correct marker handler */
7917 if (! (*(l_marker_handler->handler))(p_j2k,p_j2k->m_specific_param.m_decoder.m_header_data,l_marker_size,p_manager)) {
7918 opj_event_msg_v2(p_manager, EVT_ERROR, "Fail to read the current marker segment (%#x)\n", l_current_marker);
7922 /* Add the marker to the codestream index*/
7923 j2k_add_tlmarker_v2(p_j2k->m_current_tile_number,
7925 l_marker_handler->id,
7926 (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
7927 l_marker_size + 4 );
7929 /* Keep the position of the last SOT marker read */
7930 if ( l_marker_handler->id == J2K_MS_SOT ) {
7931 OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4 ;
7932 if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos)
7934 p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
7939 if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
7940 /* Skip the rest of the tile part header*/
7941 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) {
7942 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7945 l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
7948 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
7949 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7950 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7953 /* Read 2 bytes from the buffer as the new marker ID */
7954 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7958 /* If we didn't skip data before, we need to read the SOD marker*/
7959 if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
7960 /* Try to read the SOD marker and skip data ? FIXME */
7961 if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
7967 if (! p_j2k->m_specific_param.m_decoder.m_can_decode){
7968 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7969 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7970 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7974 /* Read 2 bytes from buffer as the new marker ID */
7975 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7979 /* Indicate we will try to read a new tile-part header*/
7980 p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
7981 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
7982 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
7984 /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
7985 if (opj_stream_read_data(p_stream,p_j2k->m_specific_param.m_decoder.m_header_data,2,p_manager) != 2) {
7986 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
7990 /* Read 2 bytes from buffer as the new marker ID */
7991 opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
7995 /* Current marker is the EOC marker ?*/
7996 if (l_current_marker == J2K_MS_EOC) {
7997 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC ){
7998 p_j2k->m_current_tile_number = 0;
7999 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
8004 if ( ! p_j2k->m_specific_param.m_decoder.m_can_decode) {
8005 l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
8006 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
8008 while( (p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00) ) {
8009 ++p_j2k->m_current_tile_number;
8013 if (p_j2k->m_current_tile_number == l_nb_tiles) {
8014 *p_go_on = OPJ_FALSE;
8020 if (! tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
8021 opj_event_msg_v2(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
8025 opj_event_msg_v2(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
8026 p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
8028 *p_tile_index = p_j2k->m_current_tile_number;
8029 *p_go_on = OPJ_TRUE;
8030 *p_data_size = tcd_get_decoded_tile_size(p_j2k->m_tcd);
8031 *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
8032 *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
8033 *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
8034 *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
8035 *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
8037 p_j2k->m_specific_param.m_decoder.m_state |= 0x0080;/* FIXME J2K_DEC_STATE_DATA;*/
8043 opj_bool opj_j2k_decode_tile ( opj_j2k_v2_t * p_j2k,
8044 OPJ_UINT32 p_tile_index,
8046 OPJ_UINT32 p_data_size,
8047 opj_stream_private_t *p_stream,
8048 opj_event_mgr_t * p_manager )
8050 OPJ_UINT32 l_current_marker;
8051 OPJ_BYTE l_data [2];
8052 opj_tcp_v2_t * l_tcp;
8055 assert(p_stream != 00);
8056 assert(p_j2k != 00);
8057 assert(p_manager != 00);
8059 if ( !(p_j2k->m_specific_param.m_decoder.m_state & 0x0080/*FIXME J2K_DEC_STATE_DATA*/)
8060 || (p_tile_index != p_j2k->m_current_tile_number) ) {
8064 l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
8065 if (! l_tcp->m_data) {
8066 opj_j2k_tcp_destroy(l_tcp);
8070 if (! tcd_decode_tile_v2( p_j2k->m_tcd,
8074 p_j2k->cstr_index) ) {
8075 opj_j2k_tcp_destroy(l_tcp);
8076 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
8080 if (! tcd_update_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
8084 /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
8085 * we destroy just the data which will be re-read in read_tile_header*/
8086 /*opj_j2k_tcp_destroy(l_tcp);
8087 p_j2k->m_tcd->tcp = 0;*/
8088 opj_j2k_tcp_data_destroy(l_tcp);
8090 p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
8091 p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
8093 if (p_j2k->m_specific_param.m_decoder.m_state != 0x0100){ /*FIXME J2K_DEC_STATE_EOC)*/
8094 if (opj_stream_read_data(p_stream,l_data,2,p_manager) != 2) {
8095 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short\n");
8099 opj_read_bytes(l_data,&l_current_marker,2);
8101 if (l_current_marker == J2K_MS_EOC) {
8102 p_j2k->m_current_tile_number = 0;
8103 p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
8105 else if (l_current_marker != J2K_MS_SOT)
8107 opj_event_msg_v2(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
8116 opj_bool opj_j2k_update_image_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image)
8118 OPJ_UINT32 i,j,k = 0;
8119 OPJ_UINT32 l_width_src,l_height_src;
8120 OPJ_UINT32 l_width_dest,l_height_dest;
8121 OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
8122 OPJ_INT32 l_start_offset_src, l_line_offset_src, l_end_offset_src ;
8123 OPJ_UINT32 l_start_x_dest , l_start_y_dest;
8124 OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
8125 OPJ_INT32 l_start_offset_dest, l_line_offset_dest;
8127 opj_image_comp_t * l_img_comp_src = 00;
8128 opj_image_comp_t * l_img_comp_dest = 00;
8130 opj_tcd_tilecomp_v2_t * l_tilec = 00;
8131 opj_image_t * l_image_src = 00;
8132 OPJ_UINT32 l_size_comp, l_remaining;
8133 OPJ_INT32 * l_dest_ptr;
8134 opj_tcd_resolution_v2_t* l_res= 00;
8136 l_tilec = p_tcd->tcd_image->tiles->comps;
8137 l_image_src = p_tcd->image;
8138 l_img_comp_src = l_image_src->comps;
8140 l_img_comp_dest = p_output_image->comps;
8142 for (i=0; i<l_image_src->numcomps; i++) {
8144 /* Allocate output component buffer if necessary */
8145 if (!l_img_comp_dest->data) {
8147 l_img_comp_dest->data = (OPJ_INT32*) opj_calloc(l_img_comp_dest->w * l_img_comp_dest->h, sizeof(OPJ_INT32));
8148 if (! l_img_comp_dest->data) {
8153 /* Copy info from decoded comp image to output image */
8154 l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
8157 /* Compute the precision of the output buffer */
8158 l_size_comp = l_img_comp_src->prec >> 3; /*(/ 8)*/
8159 l_remaining = l_img_comp_src->prec & 7; /* (%8) */
8160 l_res = l_tilec->resolutions + l_img_comp_src->resno_decoded;
8166 if (l_size_comp == 3) {
8171 /* Current tile component size*/
8173 fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
8174 l_res->x0, l_res->x1, l_res->y0, l_res->y1);
8177 l_width_src = (l_res->x1 - l_res->x0);
8178 l_height_src = (l_res->y1 - l_res->y0);
8180 /* Border of the current output component*/
8181 l_x0_dest = int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
8182 l_y0_dest = int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
8183 l_x1_dest = l_x0_dest + l_img_comp_dest->w;
8184 l_y1_dest = l_y0_dest + l_img_comp_dest->h;
8187 fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
8188 l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
8192 /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
8193 * of the input buffer (decoded tile component) which will be move
8194 * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
8195 * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
8196 * by this input area.
8198 assert( l_res->x0 >= 0);
8199 assert( l_res->x1 >= 0);
8200 if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
8201 l_start_x_dest = l_res->x0 - l_x0_dest;
8202 l_offset_x0_src = 0;
8204 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8205 l_width_dest = l_width_src;
8206 l_offset_x1_src = 0;
8209 l_width_dest = l_x1_dest - l_res->x0 ;
8210 l_offset_x1_src = l_width_src - l_width_dest;
8214 l_start_x_dest = 0 ;
8215 l_offset_x0_src = l_x0_dest - l_res->x0;
8217 if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
8218 l_width_dest = l_width_src - l_offset_x0_src;
8219 l_offset_x1_src = 0;
8222 l_width_dest = l_img_comp_dest->w ;
8223 l_offset_x1_src = l_res->x1 - l_x1_dest;
8227 if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
8228 l_start_y_dest = l_res->y0 - l_y0_dest;
8229 l_offset_y0_src = 0;
8231 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8232 l_height_dest = l_height_src;
8233 l_offset_y1_src = 0;
8236 l_height_dest = l_y1_dest - l_res->y0 ;
8237 l_offset_y1_src = l_height_src - l_height_dest;
8241 l_start_y_dest = 0 ;
8242 l_offset_y0_src = l_y0_dest - l_res->y0;
8244 if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
8245 l_height_dest = l_height_src - l_offset_y0_src;
8246 l_offset_y1_src = 0;
8249 l_height_dest = l_img_comp_dest->h ;
8250 l_offset_y1_src = l_res->y1 - l_y1_dest;
8254 if( (l_offset_x0_src < 0 ) || (l_offset_y0_src < 0 ) || (l_offset_x1_src < 0 ) || (l_offset_y1_src < 0 ) ){
8259 /* Compute the input buffer offset */
8260 l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
8261 l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
8262 l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
8264 /* Compute the output buffer offset */
8265 l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
8266 l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
8268 /* Move the output buffer to the first place where we will write*/
8269 l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
8272 fprintf(stdout, "COMPO[%d]:\n",i);
8273 fprintf(stdout, "SRC: l_start_x_src=%d, l_start_y_src=%d, l_width_src=%d, l_height_src=%d\n"
8274 "\t tile offset:%d, %d, %d, %d\n"
8275 "\t buffer offset: %d; %d, %d\n",
8276 l_res->x0, l_res->y0, l_width_src, l_height_src,
8277 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src,
8278 l_start_offset_src, l_line_offset_src, l_end_offset_src);
8280 fprintf(stdout, "DEST: l_start_x_dest=%d, l_start_y_dest=%d, l_width_dest=%d, l_height_dest=%d\n"
8281 "\t start offset: %d, line offset= %d\n",
8282 l_start_x_dest, l_start_y_dest, l_width_dest, l_height_dest, l_start_offset_dest, l_line_offset_dest);
8286 switch (l_size_comp) {
8289 OPJ_CHAR * l_src_ptr = (OPJ_CHAR*) p_data;
8290 l_src_ptr += l_start_offset_src; /* Move to the first place where we will read*/
8292 if (l_img_comp_src->sgnd) {
8293 for (j = 0 ; j < l_height_dest ; ++j) {
8294 for ( k = 0 ; k < l_width_dest ; ++k) {
8295 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++)); /* Copy only the data needed for the output image */
8298 l_dest_ptr+= l_line_offset_dest; /* Move to the next place where we will write */
8299 l_src_ptr += l_line_offset_src ; /* Move to the next place where we will read */
8303 for ( j = 0 ; j < l_height_dest ; ++j ) {
8304 for ( k = 0 ; k < l_width_dest ; ++k) {
8305 *(l_dest_ptr++) = (OPJ_INT32) ((*(l_src_ptr++))&0xff);
8308 l_dest_ptr+= l_line_offset_dest;
8309 l_src_ptr += l_line_offset_src;
8313 l_src_ptr += l_end_offset_src; /* Move to the end of this component-part of the input buffer */
8314 p_data = (OPJ_BYTE*) l_src_ptr; /* Keep the current position for the next component-part */
8319 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_data;
8320 l_src_ptr += l_start_offset_src;
8322 if (l_img_comp_src->sgnd) {
8323 for (j=0;j<l_height_dest;++j) {
8324 for (k=0;k<l_width_dest;++k) {
8325 *(l_dest_ptr++) = *(l_src_ptr++);
8328 l_dest_ptr+= l_line_offset_dest;
8329 l_src_ptr += l_line_offset_src ;
8333 for (j=0;j<l_height_dest;++j) {
8334 for (k=0;k<l_width_dest;++k) {
8335 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
8338 l_dest_ptr+= l_line_offset_dest;
8339 l_src_ptr += l_line_offset_src ;
8343 l_src_ptr += l_end_offset_src;
8344 p_data = (OPJ_BYTE*) l_src_ptr;
8349 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_data;
8350 l_src_ptr += l_start_offset_src;
8352 for (j=0;j<l_height_dest;++j) {
8353 for (k=0;k<l_width_dest;++k) {
8354 *(l_dest_ptr++) = (*(l_src_ptr++));
8357 l_dest_ptr+= l_line_offset_dest;
8358 l_src_ptr += l_line_offset_src ;
8361 l_src_ptr += l_end_offset_src;
8362 p_data = (OPJ_BYTE*) l_src_ptr;
8376 * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
8378 * @param p_j2k the jpeg2000 codec.
8379 * @param p_start_x the left position of the rectangle to decode (in image coordinates).
8380 * @param p_end_x the right position of the rectangle to decode (in image coordinates).
8381 * @param p_start_y the up position of the rectangle to decode (in image coordinates).
8382 * @param p_end_y the bottom position of the rectangle to decode (in image coordinates).
8383 * @param p_manager the user event manager
8385 * @return true if the area could be set.
8387 opj_bool opj_j2k_set_decode_area( opj_j2k_v2_t *p_j2k,
8388 opj_image_t* p_image,
8389 OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
8390 OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
8391 opj_event_mgr_t * p_manager )
8393 opj_cp_v2_t * l_cp = &(p_j2k->m_cp);
8394 opj_image_t * l_image = p_j2k->m_private_image;
8397 OPJ_INT32 l_comp_x1, l_comp_y1;
8398 opj_image_comp_t* l_img_comp = NULL;
8400 /* Check if we are read the main header */
8401 if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) { /* FIXME J2K_DEC_STATE_TPHSOT)*/
8402 opj_event_msg_v2(p_manager, EVT_ERROR, "Need to decode the main header before begin to decode the remaining codestream");
8406 if ( !p_start_x && !p_start_y && !p_end_x && !p_end_y){
8407 opj_event_msg_v2(p_manager, EVT_INFO, "No decoded area parameters, set the decoded area to the whole image\n");
8409 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8410 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8411 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8412 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8418 /* Check if the positions provided by the user are correct */
8421 assert(p_start_x >= 0 );
8422 assert(p_start_y >= 0 );
8424 if ((OPJ_UINT32)p_start_x > l_image->x1 ) {
8425 opj_event_msg_v2(p_manager, EVT_ERROR,
8426 "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
8427 p_start_x, l_image->x1);
8430 else if ((OPJ_UINT32)p_start_x < l_image->x0){
8431 opj_event_msg_v2(p_manager, EVT_WARNING,
8432 "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
8433 p_start_x, l_image->x0);
8434 p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
8435 p_image->x0 = l_image->x0;
8438 p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
8439 p_image->x0 = p_start_x;
8443 if ((OPJ_UINT32)p_start_y > l_image->y1){
8444 opj_event_msg_v2(p_manager, EVT_ERROR,
8445 "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
8446 p_start_y, l_image->y1);
8449 else if ((OPJ_UINT32)p_start_y < l_image->y0){
8450 opj_event_msg_v2(p_manager, EVT_WARNING,
8451 "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
8452 p_start_y, l_image->y0);
8453 p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
8454 p_image->y0 = l_image->y0;
8457 p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
8458 p_image->y0 = p_start_y;
8462 assert((OPJ_UINT32)p_end_x > 0);
8463 assert((OPJ_UINT32)p_end_y > 0);
8464 if ((OPJ_UINT32)p_end_x < l_image->x0) {
8465 opj_event_msg_v2(p_manager, EVT_ERROR,
8466 "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
8467 p_end_x, l_image->x0);
8470 else if ((OPJ_UINT32)p_end_x > l_image->x1) {
8471 opj_event_msg_v2(p_manager, EVT_WARNING,
8472 "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
8473 p_end_x, l_image->x1);
8474 p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
8475 p_image->x1 = l_image->x1;
8478 p_j2k->m_specific_param.m_decoder.m_end_tile_x = int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
8479 p_image->x1 = p_end_x;
8483 if ((OPJ_UINT32)p_end_y < l_image->y0) {
8484 opj_event_msg_v2(p_manager, EVT_ERROR,
8485 "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
8486 p_end_y, l_image->y0);
8489 if ((OPJ_UINT32)p_end_y > l_image->y1){
8490 opj_event_msg_v2(p_manager, EVT_WARNING,
8491 "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
8492 p_end_y, l_image->y1);
8493 p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
8494 p_image->y1 = l_image->y1;
8497 p_j2k->m_specific_param.m_decoder.m_end_tile_y = int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
8498 p_image->y1 = p_end_y;
8502 p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
8504 l_img_comp = p_image->comps;
8505 for (it_comp=0; it_comp < p_image->numcomps; ++it_comp)
8509 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
8510 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
8511 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
8512 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
8514 l_w = int_ceildivpow2(l_comp_x1, l_img_comp->factor)
8515 - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
8517 opj_event_msg_v2(p_manager, EVT_ERROR,
8518 "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
8522 l_img_comp->w = l_w;
8524 l_h = int_ceildivpow2(l_comp_y1, l_img_comp->factor)
8525 - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
8527 opj_event_msg_v2(p_manager, EVT_ERROR,
8528 "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
8532 l_img_comp->h = l_h;
8537 opj_event_msg_v2( p_manager, EVT_INFO,"Setting decoding area to %d,%d,%d,%d\n",
8538 p_image->x0, p_image->y0, p_image->x1, p_image->y1);
8545 /* ----------------------------------------------------------------------- */
8546 /* J2K / JPT decoder interface */
8547 /* ----------------------------------------------------------------------- */
8549 * Creates a J2K decompression structure.
8551 * @return a handle to a J2K decompressor if successful, NULL otherwise.
8553 opj_j2k_v2_t* opj_j2k_create_decompress(void)
8555 opj_j2k_v2_t *l_j2k = (opj_j2k_v2_t*) opj_malloc(sizeof(opj_j2k_v2_t));
8559 memset(l_j2k,0,sizeof(opj_j2k_v2_t));
8561 l_j2k->m_is_decoder = 1;
8562 l_j2k->m_cp.m_is_decoder = 1;
8564 l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_v2_t*) opj_malloc(sizeof(opj_tcp_v2_t));
8565 if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
8566 opj_j2k_destroy(l_j2k);
8569 memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_v2_t));
8571 l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(J2K_DEFAULT_HEADER_SIZE);
8572 if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
8573 opj_j2k_destroy(l_j2k);
8577 l_j2k->m_specific_param.m_decoder.m_header_data_size = J2K_DEFAULT_HEADER_SIZE;
8579 l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
8581 l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
8583 /* codestream index creation */
8584 l_j2k->cstr_index = opj_j2k_create_cstr_index();
8586 /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
8587 if (!l_j2k->cstr_index){
8588 opj_j2k_destroy(l_j2k);
8592 l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
8595 /* validation list creation */
8596 l_j2k->m_validation_list = opj_procedure_list_create();
8597 if (! l_j2k->m_validation_list) {
8598 opj_j2k_destroy(l_j2k);
8602 /* execution list creation */
8603 l_j2k->m_procedure_list = opj_procedure_list_create();
8604 if (! l_j2k->m_procedure_list) {
8605 opj_j2k_destroy(l_j2k);
8613 opj_codestream_index_t* opj_j2k_create_cstr_index(void)
8615 opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
8616 opj_calloc(1,sizeof(opj_codestream_index_t));
8620 cstr_index->maxmarknum = 100;
8621 cstr_index->marknum = 0;
8622 cstr_index->marker = (opj_marker_info_t*)
8623 opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
8624 if (!cstr_index-> marker)
8627 cstr_index->tile_index = NULL;
8634 * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
8636 * @param p_tile_no the tile index.
8637 * @param p_comp_no the component being outputted.
8638 * @param p_j2k the J2K codec.
8640 * @return the number of bytes taken by the SPCod element.
8642 OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size ( opj_j2k_v2_t *p_j2k,
8643 OPJ_UINT32 p_tile_no,
8644 OPJ_UINT32 p_comp_no )
8646 opj_cp_v2_t *l_cp = 00;
8647 opj_tcp_v2_t *l_tcp = 00;
8648 opj_tccp_t *l_tccp = 00;
8651 assert(p_j2k != 00);
8653 l_cp = &(p_j2k->m_cp);
8654 l_tcp = &l_cp->tcps[p_tile_no];
8655 l_tccp = &l_tcp->tccps[p_comp_no];
8657 /* preconditions again */
8658 assert(p_tile_no < (l_cp->tw * l_cp->th));
8659 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8661 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8662 return 5 + l_tccp->numresolutions;
8670 * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8672 * @param p_comp_no the component number to output.
8673 * @param p_stream the stream to write data to.
8674 * @param p_j2k J2K codec.
8675 * @param p_manager the user event manager.
8678 opj_bool opj_j2k_write_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8679 OPJ_UINT32 p_tile_no,
8680 OPJ_UINT32 p_comp_no,
8682 OPJ_UINT32 * p_header_size,
8683 struct opj_event_mgr * p_manager )
8686 opj_cp_v2_t *l_cp = 00;
8687 opj_tcp_v2_t *l_tcp = 00;
8688 opj_tccp_t *l_tccp = 00;
8691 assert(p_j2k != 00);
8692 assert(p_header_size != 00);
8693 assert(p_manager != 00);
8694 assert(p_data != 00);
8696 l_cp = &(p_j2k->m_cp);
8697 l_tcp = &l_cp->tcps[p_tile_no];
8698 l_tccp = &l_tcp->tccps[p_comp_no];
8700 /* preconditions again */
8701 assert(p_tile_no < (l_cp->tw * l_cp->th));
8702 assert(p_comp_no <(p_j2k->m_private_image->numcomps));
8704 if (*p_header_size < 5) {
8705 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
8709 opj_write_bytes(p_data,l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
8712 opj_write_bytes(p_data,l_tccp->cblkw - 2, 1); /* SPcoc (E) */
8715 opj_write_bytes(p_data,l_tccp->cblkh - 2, 1); /* SPcoc (F) */
8718 opj_write_bytes(p_data,l_tccp->cblksty, 1); /* SPcoc (G) */
8721 opj_write_bytes(p_data,l_tccp->qmfbid, 1); /* SPcoc (H) */
8724 *p_header_size = *p_header_size - 5;
8726 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8728 if (*p_header_size < l_tccp->numresolutions) {
8729 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writting SPCod SPCoc element\n");
8733 for (i = 0; i < l_tccp->numresolutions; ++i) {
8734 opj_write_bytes(p_data,l_tccp->prcw[i] + (l_tccp->prch[i] << 4), 1); /* SPcoc (I_i) */
8738 *p_header_size = *p_header_size - l_tccp->numresolutions;
8745 * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
8746 * @param p_header_data the data contained in the COM box.
8747 * @param p_j2k the jpeg2000 codec.
8748 * @param p_header_size the size of the data contained in the COM marker.
8749 * @param p_manager the user event manager.
8751 opj_bool opj_j2k_read_SPCod_SPCoc( opj_j2k_v2_t *p_j2k,
8753 OPJ_BYTE * p_header_data,
8754 OPJ_UINT32 * p_header_size,
8755 opj_event_mgr_t * p_manager)
8757 OPJ_UINT32 i, l_tmp;
8758 opj_cp_v2_t *l_cp = NULL;
8759 opj_tcp_v2_t *l_tcp = NULL;
8760 opj_tccp_t *l_tccp = NULL;
8761 OPJ_BYTE * l_current_ptr = NULL;
8764 assert(p_j2k != 00);
8765 assert(p_manager != 00);
8766 assert(p_header_data != 00);
8768 l_cp = &(p_j2k->m_cp);
8769 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
8770 &l_cp->tcps[p_j2k->m_current_tile_number] :
8771 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8773 /* precondition again */
8774 assert(compno < p_j2k->m_private_image->numcomps);
8776 l_tccp = &l_tcp->tccps[compno];
8777 l_current_ptr = p_header_data;
8779 /* make sure room is sufficient */
8780 if (*p_header_size < 5) {
8781 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8785 opj_read_bytes(l_current_ptr, &l_tccp->numresolutions ,1); /* SPcox (D) */
8786 ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
8789 /* If user wants to remove more resolutions than the codestream contains, return error */
8790 if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
8791 opj_event_msg_v2(p_manager, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
8792 "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
8793 p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
8797 opj_read_bytes(l_current_ptr,&l_tccp->cblkw ,1); /* SPcoc (E) */
8801 opj_read_bytes(l_current_ptr,&l_tccp->cblkh ,1); /* SPcoc (F) */
8805 opj_read_bytes(l_current_ptr,&l_tccp->cblksty ,1); /* SPcoc (G) */
8808 opj_read_bytes(l_current_ptr,&l_tccp->qmfbid ,1); /* SPcoc (H) */
8811 *p_header_size = *p_header_size - 5;
8813 /* use custom precinct size ? */
8814 if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
8815 if (*p_header_size < l_tccp->numresolutions) {
8816 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
8820 for (i = 0; i < l_tccp->numresolutions; ++i) {
8821 opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
8823 l_tccp->prcw[i] = l_tmp & 0xf;
8824 l_tccp->prch[i] = l_tmp >> 4;
8827 *p_header_size = *p_header_size - l_tccp->numresolutions;
8830 /* set default size for the precinct width and height */
8831 for (i = 0; i < l_tccp->numresolutions; ++i) {
8832 l_tccp->prcw[i] = 15;
8833 l_tccp->prch[i] = 15;
8837 #ifdef WIP_REMOVE_MSD
8839 if (p_j2k->cstr_info && compno == 0) {
8840 OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
8842 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh = l_tccp->cblkh;
8843 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw = l_tccp->cblkw;
8844 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions = l_tccp->numresolutions;
8845 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty = l_tccp->cblksty;
8846 p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid = l_tccp->qmfbid;
8849 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx,l_tccp->prcw, l_data_size);
8850 memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy,l_tccp->prch, l_data_size);
8859 * Copies the tile component parameters of all the component from the first tile component.
8861 * @param p_j2k the J2k codec.
8863 void opj_j2k_copy_tile_component_parameters( opj_j2k_v2_t *p_j2k )
8867 opj_cp_v2_t *l_cp = NULL;
8868 opj_tcp_v2_t *l_tcp = NULL;
8869 opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
8870 OPJ_UINT32 l_prc_size;
8873 assert(p_j2k != 00);
8875 l_cp = &(p_j2k->m_cp);
8876 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /* FIXME J2K_DEC_STATE_TPH*/
8877 &l_cp->tcps[p_j2k->m_current_tile_number] :
8878 p_j2k->m_specific_param.m_decoder.m_default_tcp;
8880 l_ref_tccp = &l_tcp->tccps[0];
8881 l_copied_tccp = l_ref_tccp + 1;
8882 l_prc_size = l_ref_tccp->numresolutions * sizeof(OPJ_UINT32);
8884 for (i=1; i<p_j2k->m_private_image->numcomps; ++i) {
8885 l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
8886 l_copied_tccp->cblkw = l_ref_tccp->cblkw;
8887 l_copied_tccp->cblkh = l_ref_tccp->cblkh;
8888 l_copied_tccp->cblksty = l_ref_tccp->cblksty;
8889 l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
8890 memcpy(l_copied_tccp->prcw,l_ref_tccp->prcw,l_prc_size);
8891 memcpy(l_copied_tccp->prch,l_ref_tccp->prch,l_prc_size);
8897 * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
8899 * @param p_tile_no the tile index.
8900 * @param p_comp_no the component being outputted.
8901 * @param p_j2k the J2K codec.
8903 * @return the number of bytes taken by the SPCod element.
8905 OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size ( opj_j2k_v2_t *p_j2k,
8906 OPJ_UINT32 p_tile_no,
8907 OPJ_UINT32 p_comp_no )
8909 OPJ_UINT32 l_num_bands;
8911 opj_cp_v2_t *l_cp = 00;
8912 opj_tcp_v2_t *l_tcp = 00;
8913 opj_tccp_t *l_tccp = 00;
8916 assert(p_j2k != 00);
8918 l_cp = &(p_j2k->m_cp);
8919 l_tcp = &l_cp->tcps[p_tile_no];
8920 l_tccp = &l_tcp->tccps[p_comp_no];
8922 /* preconditions again */
8923 assert(p_tile_no < l_cp->tw * l_cp->th);
8924 assert(p_comp_no < p_j2k->m_private_image->numcomps);
8926 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8928 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8929 return 1 + l_num_bands;
8932 return 1 + 2*l_num_bands;
8937 * Writes a SQcd or SQcc element, i.e. the quantization values of a band.
8939 * @param p_tile_no the tile to output.
8940 * @param p_comp_no the component number to output.
8941 * @param p_data the data buffer.
8942 * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
8943 * @param p_j2k J2K codec.
8944 * @param p_manager the user event manager.
8947 opj_bool opj_j2k_write_SQcd_SQcc( opj_j2k_v2_t *p_j2k,
8948 OPJ_UINT32 p_tile_no,
8949 OPJ_UINT32 p_comp_no,
8951 OPJ_UINT32 * p_header_size,
8952 struct opj_event_mgr * p_manager )
8954 OPJ_UINT32 l_header_size;
8955 OPJ_UINT32 l_band_no, l_num_bands;
8956 OPJ_UINT32 l_expn,l_mant;
8958 opj_cp_v2_t *l_cp = 00;
8959 opj_tcp_v2_t *l_tcp = 00;
8960 opj_tccp_t *l_tccp = 00;
8963 assert(p_j2k != 00);
8964 assert(p_header_size != 00);
8965 assert(p_manager != 00);
8966 assert(p_data != 00);
8968 l_cp = &(p_j2k->m_cp);
8969 l_tcp = &l_cp->tcps[p_tile_no];
8970 l_tccp = &l_tcp->tccps[p_comp_no];
8972 /* preconditions again */
8973 assert(p_tile_no < l_cp->tw * l_cp->th);
8974 assert(p_comp_no <p_j2k->m_private_image->numcomps);
8976 l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (l_tccp->numresolutions * 3 - 2);
8978 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
8979 l_header_size = 1 + l_num_bands;
8981 if (*p_header_size < l_header_size) {
8982 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
8986 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
8989 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
8990 l_expn = l_tccp->stepsizes[l_band_no].expn;
8991 opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
8996 l_header_size = 1 + 2*l_num_bands;
8998 if (*p_header_size < l_header_size) {
8999 opj_event_msg_v2(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
9003 opj_write_bytes(p_data,l_tccp->qntsty + (l_tccp->numgbits << 5), 1); /* Sqcx */
9006 for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
9007 l_expn = l_tccp->stepsizes[l_band_no].expn;
9008 l_mant = l_tccp->stepsizes[l_band_no].mant;
9010 opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
9015 *p_header_size = *p_header_size - l_header_size;
9021 * Reads a SQcd or SQcc element, i.e. the quantization values of a band.
9023 * @param p_comp_no the component being targeted.
9024 * @param p_header_data the data contained in the COM box.
9025 * @param p_j2k the jpeg2000 codec.
9026 * @param p_header_size the size of the data contained in the COM marker.
9027 * @param p_manager the user event manager.
9029 opj_bool opj_j2k_read_SQcd_SQcc(opj_j2k_v2_t *p_j2k,
9030 OPJ_UINT32 p_comp_no,
9031 OPJ_BYTE* p_header_data,
9032 OPJ_UINT32 * p_header_size,
9033 opj_event_mgr_t * p_manager
9037 OPJ_UINT32 l_band_no;
9038 opj_cp_v2_t *l_cp = 00;
9039 opj_tcp_v2_t *l_tcp = 00;
9040 opj_tccp_t *l_tccp = 00;
9041 OPJ_BYTE * l_current_ptr = 00;
9042 OPJ_UINT32 l_tmp, l_num_band;
9045 assert(p_j2k != 00);
9046 assert(p_manager != 00);
9047 assert(p_header_data != 00);
9049 l_cp = &(p_j2k->m_cp);
9050 /* come from tile part header or main header ?*/
9051 l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ? /*FIXME J2K_DEC_STATE_TPH*/
9052 &l_cp->tcps[p_j2k->m_current_tile_number] :
9053 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9055 /* precondition again*/
9056 assert(p_comp_no < p_j2k->m_private_image->numcomps);
9058 l_tccp = &l_tcp->tccps[p_comp_no];
9059 l_current_ptr = p_header_data;
9061 if (*p_header_size < 1) {
9062 opj_event_msg_v2(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
9065 *p_header_size -= 1;
9067 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* Sqcx */
9070 l_tccp->qntsty = l_tmp & 0x1f;
9071 l_tccp->numgbits = l_tmp >> 5;
9072 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9076 l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
9078 (*p_header_size) / 2;
9080 if( l_num_band > J2K_MAXBANDS ) {
9081 opj_event_msg_v2(p_manager, EVT_WARNING, "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
9082 "number of subbands (%d) is greater to J2K_MAXBANDS (%d). So we limit the number of elements stored to "
9083 "J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, J2K_MAXBANDS, J2K_MAXBANDS);
9084 /*return OPJ_FALSE;*/
9089 if (l_cp->correct) {
9091 /* if JPWL is on, we check whether there are too many subbands */
9092 if (/*(l_num_band < 0) ||*/ (l_num_band >= J2K_MAXBANDS)) {
9093 opj_event_msg_v2(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
9094 "JPWL: bad number of subbands in Sqcx (%d)\n",
9097 opj_event_msg_v2(p_manager, EVT_ERROR, "JPWL: giving up\n");
9100 /* we try to correct */
9102 opj_event_msg_v2(p_manager, EVT_WARNING, "- trying to adjust them\n"
9103 "- setting number of bands to %d => HYPOTHESIS!!!\n",
9108 #endif /* USE_JPWL */
9110 if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
9111 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9112 opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
9114 if (l_band_no < J2K_MAXBANDS){
9115 l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
9116 l_tccp->stepsizes[l_band_no].mant = 0;
9119 *p_header_size = *p_header_size - l_num_band;
9122 for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
9123 opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
9125 if (l_band_no < J2K_MAXBANDS){
9126 l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
9127 l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
9130 *p_header_size = *p_header_size - 2*l_num_band;
9133 /* Add Antonin : if scalar_derived -> compute other stepsizes */
9134 if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
9135 for (l_band_no = 1; l_band_no < J2K_MAXBANDS; l_band_no++) {
9136 l_tccp->stepsizes[l_band_no].expn =
9137 ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
9138 (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
9139 l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
9147 * Copies the tile component parameters of all the component from the first tile component.
9149 * @param p_j2k the J2k codec.
9151 void opj_j2k_copy_tile_quantization_parameters( opj_j2k_v2_t *p_j2k )
9154 opj_cp_v2_t *l_cp = NULL;
9155 opj_tcp_v2_t *l_tcp = NULL;
9156 opj_tccp_t *l_ref_tccp = NULL;
9157 opj_tccp_t *l_copied_tccp = NULL;
9161 assert(p_j2k != 00);
9163 l_cp = &(p_j2k->m_cp);
9164 l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
9165 &l_cp->tcps[p_j2k->m_current_tile_number] :
9166 p_j2k->m_specific_param.m_decoder.m_default_tcp;
9168 l_ref_tccp = &l_tcp->tccps[0];
9169 l_copied_tccp = l_ref_tccp + 1;
9170 l_size = J2K_MAXBANDS * sizeof(opj_stepsize_t);
9172 for (i=1;i<p_j2k->m_private_image->numcomps;++i) {
9173 l_copied_tccp->qntsty = l_ref_tccp->qntsty;
9174 l_copied_tccp->numgbits = l_ref_tccp->numgbits;
9175 memcpy(l_copied_tccp->stepsizes,l_ref_tccp->stepsizes,l_size);
9181 * Dump some elements from the J2K decompression structure .
9183 *@param p_j2k the jpeg2000 codec.
9184 *@param flag flag to describe what elments are dump.
9185 *@param out_stream output stream where dump the elements.
9188 void j2k_dump (opj_j2k_v2_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
9190 /* Check if the flag is compatible with j2k file*/
9191 if ( (flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)){
9192 fprintf(out_stream, "Wrong flag\n");
9196 /* Dump the image_header */
9197 if (flag & OPJ_IMG_INFO){
9198 if (p_j2k->m_private_image)
9199 j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
9202 /* Dump the codestream info from main header */
9203 if (flag & OPJ_J2K_MH_INFO){
9204 opj_j2k_dump_MH_info(p_j2k, out_stream);
9208 /* Dump the codestream info of the current tile */
9209 if (flag & OPJ_J2K_TH_INFO){
9213 /* Dump the codestream index from main header */
9214 if (flag & OPJ_J2K_MH_IND){
9215 opj_j2k_dump_MH_index(p_j2k, out_stream);
9218 /* Dump the codestream index of the current tile */
9219 if (flag & OPJ_J2K_TH_IND){
9226 * Dump index elements of the codestream extract from the main header.
9228 *@param p_j2k the jpeg2000 codec.
9229 *@param out_stream output stream where dump the elements.
9232 void opj_j2k_dump_MH_index(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9234 opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
9235 OPJ_UINT32 it_marker, it_tile, it_tile_part;
9237 fprintf(out_stream, "Codestream index from main header: {\n");
9239 fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
9240 "\t Main header end position=%" PRIi64 "\n",
9241 cstr_index->main_head_start, cstr_index->main_head_end);
9243 fprintf(out_stream, "\t Marker list: {\n");
9245 if (cstr_index->marker){
9246 for (it_marker=0; it_marker < cstr_index->marknum ; it_marker++){
9247 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9248 cstr_index->marker[it_marker].type,
9249 cstr_index->marker[it_marker].pos,
9250 cstr_index->marker[it_marker].len );
9254 fprintf(out_stream, "\t }\n");
9257 if (cstr_index->tile_index){
9259 /* Simple test to avoid to write empty information*/
9260 OPJ_UINT32 l_acc_nb_of_tile_part = 0;
9261 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9262 l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
9265 if (l_acc_nb_of_tile_part)
9267 fprintf(out_stream, "\t Tile index: {\n");
9269 for (it_tile=0; it_tile < cstr_index->nb_of_tiles ; it_tile++){
9270 OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
9272 fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile, nb_of_tile_part);
9274 if (cstr_index->tile_index[it_tile].tp_index){
9275 for (it_tile_part =0; it_tile_part < nb_of_tile_part; it_tile_part++){
9276 fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%" PRIi64 ", end_pos=%" PRIi64 ".\n",
9278 cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
9279 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
9280 cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
9284 if (cstr_index->tile_index[it_tile].marker){
9285 for (it_marker=0; it_marker < cstr_index->tile_index[it_tile].marknum ; it_marker++){
9286 fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
9287 cstr_index->tile_index[it_tile].marker[it_marker].type,
9288 cstr_index->tile_index[it_tile].marker[it_marker].pos,
9289 cstr_index->tile_index[it_tile].marker[it_marker].len );
9293 fprintf(out_stream,"\t }\n");
9297 fprintf(out_stream,"}\n");
9302 * Dump info elements of the codestream extract from the main header.
9304 *@param p_j2k the jpeg2000 codec.
9305 *@param out_stream output stream where dump the elements.
9308 void opj_j2k_dump_MH_info(opj_j2k_v2_t* p_j2k, FILE* out_stream)
9310 opj_tcp_v2_t * l_default_tile=NULL;
9312 fprintf(out_stream, "Codestream info from main header: {\n");
9314 fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
9315 fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
9316 fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
9318 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9322 OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
9324 fprintf(out_stream, "\t default tile {\n");
9325 fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
9326 fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
9327 fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
9328 fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
9330 for (compno = 0; compno < numcomps; compno++) {
9331 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9333 OPJ_INT32 bandno, numbands;
9336 fprintf(out_stream, "\t\t comp %d {\n", compno);
9337 fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
9338 fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
9339 fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
9340 fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
9341 fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
9342 fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
9344 fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
9345 for (resno = 0; resno < l_tccp->numresolutions; resno++) {
9346 fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
9348 fprintf(out_stream, "\n");
9350 /* quantization style*/
9351 fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
9352 fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
9353 fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
9354 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9355 for (bandno = 0; bandno < numbands; bandno++) {
9356 fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
9357 l_tccp->stepsizes[bandno].expn);
9359 fprintf(out_stream, "\n");
9362 fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
9364 fprintf(out_stream, "\t\t }\n");
9365 } /*end of component of default tile*/
9366 fprintf(out_stream, "\t }\n"); /*end of default tile*/
9370 fprintf(out_stream, "}\n");
9375 * Dump an image header structure.
9377 *@param img_header the image header to dump.
9378 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9379 *@param out_stream output stream where dump the elements.
9381 void j2k_dump_image_header(opj_image_t* img_header, opj_bool dev_dump_flag, FILE* out_stream)
9386 fprintf(stdout, "[DEV] Dump an image_header struct {\n");
9390 fprintf(out_stream, "Image info {\n");
9391 tab[0] = '\t';tab[1] = '\0';
9394 fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
9395 fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1, img_header->y1);
9396 fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
9398 if (img_header->comps){
9400 for (compno = 0; compno < img_header->numcomps; compno++) {
9401 fprintf(out_stream, "%s\t component %d {\n", tab, compno);
9402 j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag, out_stream);
9403 fprintf(out_stream,"%s}\n",tab);
9407 fprintf(out_stream, "}\n");
9411 * Dump a component image header structure.
9413 *@param comp_header the component image header to dump.
9414 *@param dev_dump_flag flag to describe if we are in the case of this function is use outside j2k_dump function
9415 *@param out_stream output stream where dump the elements.
9417 void j2k_dump_image_comp_header(opj_image_comp_t* comp_header, opj_bool dev_dump_flag, FILE* out_stream)
9422 fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
9425 tab[0] = '\t';tab[1] = '\t';tab[2] = '\0';
9428 fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
9429 fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
9430 fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
9433 fprintf(out_stream, "}\n");
9438 * Get the codestream info from a JPEG2000 codec.
9440 *@param p_j2k the component image header to dump.
9442 *@return the codestream information extract from the jpg2000 codec
9444 opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_v2_t* p_j2k)
9447 OPJ_UINT16 numcomps = p_j2k->m_private_image->numcomps;
9448 opj_tcp_v2_t *l_default_tile;
9449 opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,sizeof(opj_codestream_info_v2_t));
9451 cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
9453 cstr_info->tx0 = p_j2k->m_cp.tx0;
9454 cstr_info->ty0 = p_j2k->m_cp.ty0;
9455 cstr_info->tdx = p_j2k->m_cp.tdx;
9456 cstr_info->tdy = p_j2k->m_cp.tdy;
9457 cstr_info->tw = p_j2k->m_cp.tw;
9458 cstr_info->th = p_j2k->m_cp.th;
9460 cstr_info->tile_info = NULL; /* Not fill from the main header*/
9462 l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
9464 cstr_info->m_default_tile_info.csty = l_default_tile->csty;
9465 cstr_info->m_default_tile_info.prg = l_default_tile->prg;
9466 cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
9467 cstr_info->m_default_tile_info.mct = l_default_tile->mct;
9469 cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(cstr_info->nbcomps, sizeof(opj_tccp_info_t));
9471 for (compno = 0; compno < numcomps; compno++) {
9472 opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
9473 opj_tccp_info_t *l_tccp_info = &(cstr_info->m_default_tile_info.tccp_info[compno]);
9474 OPJ_INT32 bandno, numbands;
9477 l_tccp_info->csty = l_tccp->csty;
9478 l_tccp_info->numresolutions = l_tccp->numresolutions;
9479 l_tccp_info->cblkw = l_tccp->cblkw;
9480 l_tccp_info->cblkh = l_tccp->cblkh;
9481 l_tccp_info->cblksty = l_tccp->cblksty;
9482 l_tccp_info->qmfbid = l_tccp->qmfbid;
9483 if (l_tccp->numresolutions < J2K_MAXRLVLS)
9485 memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
9486 memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
9489 /* quantization style*/
9490 l_tccp_info->qntsty = l_tccp->qntsty;
9491 l_tccp_info->numgbits = l_tccp->numgbits;
9493 numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
9494 if (numbands < J2K_MAXBANDS) {
9495 for (bandno = 0; bandno < numbands; bandno++) {
9496 l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
9497 l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
9502 l_tccp_info->roishift = l_tccp->roishift;
9510 * Get the codestream index from a JPEG2000 codec.
9512 *@param p_j2k the component image header to dump.
9514 *@return the codestream index extract from the jpg2000 codec
9516 opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_v2_t* p_j2k)
9518 opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
9519 opj_calloc(1,sizeof(opj_codestream_index_t));
9523 l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
9524 l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
9525 l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
9527 l_cstr_index->marknum = p_j2k->cstr_index->marknum;
9528 l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum*sizeof(opj_marker_info_t));
9529 if (!l_cstr_index->marker){
9530 opj_free( l_cstr_index);
9534 if (p_j2k->cstr_index->marker)
9535 memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker, l_cstr_index->marknum * sizeof(opj_marker_info_t) );
9537 opj_free(l_cstr_index->marker);
9538 l_cstr_index->marker = NULL;
9541 l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
9542 l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t) );
9543 if (!l_cstr_index->tile_index){
9544 opj_free( l_cstr_index->marker);
9545 opj_free( l_cstr_index);
9549 if (!p_j2k->cstr_index->tile_index){
9550 opj_free(l_cstr_index->tile_index);
9551 l_cstr_index->tile_index = NULL;
9554 OPJ_UINT32 it_tile = 0;
9555 for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++ ){
9558 l_cstr_index->tile_index[it_tile].marknum = p_j2k->cstr_index->tile_index[it_tile].marknum;
9560 l_cstr_index->tile_index[it_tile].marker =
9561 (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum*sizeof(opj_marker_info_t));
9563 if (!l_cstr_index->tile_index[it_tile].marker) {
9564 OPJ_UINT32 it_tile_free;
9566 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9567 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9570 opj_free( l_cstr_index->tile_index);
9571 opj_free( l_cstr_index->marker);
9572 opj_free( l_cstr_index);
9576 if (p_j2k->cstr_index->tile_index[it_tile].marker)
9577 memcpy( l_cstr_index->tile_index[it_tile].marker,
9578 p_j2k->cstr_index->tile_index[it_tile].marker,
9579 l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t) );
9581 opj_free(l_cstr_index->tile_index[it_tile].marker);
9582 l_cstr_index->tile_index[it_tile].marker = NULL;
9585 /* Tile part index*/
9586 l_cstr_index->tile_index[it_tile].nb_tps = p_j2k->cstr_index->tile_index[it_tile].nb_tps;
9588 l_cstr_index->tile_index[it_tile].tp_index =
9589 (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps*sizeof(opj_tp_index_t));
9591 if(!l_cstr_index->tile_index[it_tile].tp_index){
9592 OPJ_UINT32 it_tile_free;
9594 for (it_tile_free=0; it_tile_free < it_tile; it_tile_free++){
9595 opj_free(l_cstr_index->tile_index[it_tile_free].marker);
9596 opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
9599 opj_free( l_cstr_index->tile_index);
9600 opj_free( l_cstr_index->marker);
9601 opj_free( l_cstr_index);
9605 if (p_j2k->cstr_index->tile_index[it_tile].tp_index){
9606 memcpy( l_cstr_index->tile_index[it_tile].tp_index,
9607 p_j2k->cstr_index->tile_index[it_tile].tp_index,
9608 l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t) );
9611 opj_free(l_cstr_index->tile_index[it_tile].tp_index);
9612 l_cstr_index->tile_index[it_tile].tp_index = NULL;
9615 /* Packet index (NOT USED)*/
9616 l_cstr_index->tile_index[it_tile].nb_packet = 0;
9617 l_cstr_index->tile_index[it_tile].packet_index = NULL;
9622 return l_cstr_index;
9625 opj_bool opj_j2k_allocate_tile_element_cstr_index(opj_j2k_v2_t *p_j2k)
9627 OPJ_UINT32 it_tile=0;
9629 p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
9630 p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
9631 if (!p_j2k->cstr_index->tile_index)
9634 for (it_tile=0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++){
9635 p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
9636 p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
9637 p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
9638 opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum, sizeof(opj_marker_info_t));
9639 if (!p_j2k->cstr_index->tile_index[it_tile].marker)
9649 opj_bool opj_j2k_decode_tiles ( opj_j2k_v2_t *p_j2k,
9650 opj_stream_private_t *p_stream,
9651 opj_event_mgr_t * p_manager)
9653 opj_bool l_go_on = OPJ_TRUE;
9654 OPJ_UINT32 l_current_tile_no;
9655 OPJ_UINT32 l_data_size,l_max_data_size;
9656 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9657 OPJ_UINT32 l_nb_comps;
9658 OPJ_BYTE * l_current_data;
9660 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9661 if (! l_current_data) {
9664 l_max_data_size = 1000;
9669 if (! opj_j2k_read_tile_header( p_j2k,
9672 &l_tile_x0, &l_tile_y0,
9673 &l_tile_x1, &l_tile_y1,
9678 opj_free(l_current_data);
9686 if (l_data_size > l_max_data_size) {
9687 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9688 if (! l_current_data) {
9689 opj_free(l_current_data);
9693 l_max_data_size = l_data_size;
9696 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9697 opj_free(l_current_data);
9700 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);
9702 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9703 opj_free(l_current_data);
9706 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
9710 opj_free(l_current_data);
9716 * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
9718 static void opj_j2k_setup_decoding (opj_j2k_v2_t *p_j2k)
9721 assert(p_j2k != 00);
9723 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_tiles);
9724 /* DEVELOPER CORNER, add your custom procedures */
9729 * Read and decode one tile.
9731 static opj_bool opj_j2k_decode_one_tile ( opj_j2k_v2_t *p_j2k,
9732 opj_stream_private_t *p_stream,
9733 opj_event_mgr_t * p_manager)
9735 opj_bool l_go_on = OPJ_TRUE;
9736 OPJ_UINT32 l_current_tile_no;
9737 OPJ_UINT32 l_tile_no_to_dec;
9738 OPJ_UINT32 l_data_size,l_max_data_size;
9739 OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1;
9740 OPJ_UINT32 l_nb_comps;
9741 OPJ_BYTE * l_current_data;
9743 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
9744 if (! l_current_data) {
9747 l_max_data_size = 1000;
9749 /*Allocate and initialize some elements of codestrem index if not already done*/
9750 if( !p_j2k->cstr_index->tile_index)
9752 if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)){
9753 opj_free(l_current_data);
9757 /* Move into the codestream to the first SOT used to decode the desired tile */
9758 l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
9759 if (p_j2k->cstr_index->tile_index)
9760 if(p_j2k->cstr_index->tile_index->tp_index)
9762 if ( ! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
9763 /* the index for this tile has not been built,
9764 * so move to the last SOT read */
9765 if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
9766 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9771 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)) ) {
9772 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9776 /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
9777 if(p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC)
9778 p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
9782 if (! opj_j2k_read_tile_header( p_j2k,
9785 &l_tile_x0, &l_tile_y0,
9786 &l_tile_x1, &l_tile_y1,
9791 opj_free(l_current_data);
9800 if (l_data_size > l_max_data_size) {
9801 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_data_size);
9802 if (! l_current_data) {
9803 opj_free(l_current_data);
9807 l_max_data_size = l_data_size;
9812 if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
9813 opj_free(l_current_data);
9816 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);
9818 if (! opj_j2k_update_image_data(p_j2k->m_tcd,l_current_data, p_j2k->m_output_image)) {
9819 opj_free(l_current_data);
9822 opj_event_msg_v2(p_manager, EVT_INFO, "Image data has been updated with tile %d.\n\n", l_current_tile_no);
9824 if(l_current_tile_no == l_tile_no_to_dec)
9826 /* move into the codestream to the the first SOT (FIXME or not move?)*/
9827 if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2, p_manager) ) ) {
9828 opj_event_msg_v2(p_manager, EVT_ERROR, "Problem with seek function\n");
9834 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);
9839 opj_free(l_current_data);
9846 * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
9848 static void opj_j2k_setup_decoding_tile (opj_j2k_v2_t *p_j2k)
9851 assert(p_j2k != 00);
9853 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_decode_one_tile);
9854 /* DEVELOPER CORNER, add your custom procedures */
9860 * Decodes the tiles of the stream.
9862 opj_bool opj_j2k_decode(opj_j2k_v2_t * p_j2k,
9863 opj_stream_private_t * p_stream,
9864 opj_image_t * p_image,
9865 opj_event_mgr_t * p_manager)
9872 p_j2k->m_output_image = opj_image_create0();
9873 if (! (p_j2k->m_output_image)) {
9876 opj_copy_image_header(p_image, p_j2k->m_output_image);
9878 /* customization of the decoding */
9879 opj_j2k_setup_decoding(p_j2k);
9881 /* Decode the codestream */
9882 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9883 opj_image_destroy(p_j2k->m_private_image);
9884 p_j2k->m_private_image = NULL;
9888 /* Move data and copy one information from codec to output image*/
9889 for (compno = 0; compno < p_image->numcomps; compno++) {
9890 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9891 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9892 p_j2k->m_output_image->comps[compno].data = NULL;
9900 * Get the decoded tile.
9902 * @param p_j2k the jpeg2000 codestream codec.
9903 * @param p_stream input_stream
9904 * @param p_image output image. .
9905 * @param p_manager the user event manager
9906 * @param tile_index index of the tile we want decode
9908 * @return true if succeed.
9910 opj_bool opj_j2k_get_tile( opj_j2k_v2_t *p_j2k,
9911 opj_stream_private_t *p_stream,
9912 opj_image_t* p_image,
9913 opj_event_mgr_t * p_manager,
9914 OPJ_UINT32 tile_index )
9917 OPJ_UINT32 l_tile_x, l_tile_y;
9918 opj_image_comp_t* l_img_comp;
9921 opj_event_msg_v2(p_manager, EVT_ERROR, "We need an image previously created.\n");
9925 if ( /*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th) ){
9926 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);
9930 /* Compute the dimension of the desired tile*/
9931 l_tile_x = tile_index % p_j2k->m_cp.tw;
9932 l_tile_y = tile_index / p_j2k->m_cp.tw;
9934 p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9935 if (p_image->x0 < p_j2k->m_private_image->x0)
9936 p_image->x0 = p_j2k->m_private_image->x0;
9937 p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
9938 if (p_image->x1 > p_j2k->m_private_image->x1)
9939 p_image->x1 = p_j2k->m_private_image->x1;
9941 p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9942 if (p_image->y0 < p_j2k->m_private_image->y0)
9943 p_image->y0 = p_j2k->m_private_image->y0;
9944 p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
9945 if (p_image->y1 > p_j2k->m_private_image->y1)
9946 p_image->y1 = p_j2k->m_private_image->y1;
9948 l_img_comp = p_image->comps;
9949 for (compno=0; compno < p_image->numcomps; ++compno)
9951 OPJ_INT32 l_comp_x1, l_comp_y1;
9953 l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
9955 l_img_comp->x0 = int_ceildiv(p_image->x0, l_img_comp->dx);
9956 l_img_comp->y0 = int_ceildiv(p_image->y0, l_img_comp->dy);
9957 l_comp_x1 = int_ceildiv(p_image->x1, l_img_comp->dx);
9958 l_comp_y1 = int_ceildiv(p_image->y1, l_img_comp->dy);
9960 l_img_comp->w = int_ceildivpow2(l_comp_x1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
9961 l_img_comp->h = int_ceildivpow2(l_comp_y1, l_img_comp->factor) - int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
9966 /* Destroy the previous output image*/
9967 if (p_j2k->m_output_image)
9968 opj_image_destroy(p_j2k->m_output_image);
9970 /* Create the ouput image from the information previously computed*/
9971 p_j2k->m_output_image = opj_image_create0();
9972 if (! (p_j2k->m_output_image)) {
9975 opj_copy_image_header(p_image, p_j2k->m_output_image);
9977 p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
9979 /* customization of the decoding */
9980 opj_j2k_setup_decoding_tile(p_j2k);
9982 /* Decode the codestream */
9983 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
9984 opj_image_destroy(p_j2k->m_private_image);
9985 p_j2k->m_private_image = NULL;
9989 /* Move data and copy one information from codec to output image*/
9990 for (compno = 0; compno < p_image->numcomps; compno++) {
9991 p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
9993 if (p_image->comps[compno].data)
9994 opj_free(p_image->comps[compno].data);
9996 p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
9998 p_j2k->m_output_image->comps[compno].data = NULL;
10004 opj_bool opj_j2k_set_decoded_resolution_factor(opj_j2k_v2_t *p_j2k,
10005 OPJ_UINT32 res_factor,
10006 opj_event_mgr_t * p_manager)
10008 OPJ_UINT32 it_comp;
10010 p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
10012 if (p_j2k->m_private_image) {
10013 if (p_j2k->m_private_image->comps) {
10014 if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
10015 if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
10016 for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
10017 OPJ_UINT32 max_res = p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
10018 if ( res_factor >= max_res){
10019 opj_event_msg_v2(p_manager, EVT_ERROR, "Resolution factor is greater than the maximum resolution in the component.\n");
10022 p_j2k->m_private_image->comps[it_comp].factor = res_factor;
10035 * Encodes all the tiles in a row.
10037 opj_bool opj_j2k_encode_v2( opj_j2k_v2_t * p_j2k,
10038 opj_stream_private_t *p_stream,
10039 opj_event_mgr_t * p_manager )
10042 OPJ_UINT32 l_nb_tiles;
10043 OPJ_UINT32 l_max_tile_size, l_current_tile_size;
10044 OPJ_BYTE * l_current_data;
10046 /* preconditions */
10047 assert(p_j2k != 00);
10048 assert(p_stream != 00);
10049 assert(p_manager != 00);
10051 l_current_data = (OPJ_BYTE*)opj_malloc(1000);
10052 if (! l_current_data) {
10055 l_max_tile_size = 1000;
10057 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
10058 for (i=0;i<l_nb_tiles;++i) {
10059 if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
10060 opj_free(l_current_data);
10064 l_current_tile_size = tcd_get_encoded_tile_size(p_j2k->m_tcd);
10065 if (l_current_tile_size > l_max_tile_size) {
10066 l_current_data = (OPJ_BYTE*)opj_realloc(l_current_data,l_current_tile_size);
10067 if (! l_current_data) {
10070 l_max_tile_size = l_current_tile_size;
10073 opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
10075 if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
10080 opj_free(l_current_data);
10085 * Ends the compression procedures and possibility add data to be read after the
10088 opj_bool opj_j2k_end_compress( opj_j2k_v2_t *p_j2k,
10089 opj_stream_private_t *p_stream,
10090 opj_event_mgr_t * p_manager)
10092 /* customization of the encoding */
10093 opj_j2k_setup_end_compress(p_j2k);
10095 if (! opj_j2k_exec (p_j2k, p_j2k->m_procedure_list, p_stream, p_manager))
10105 * Starts a compression scheme, i.e. validates the codec parameters, writes the header.
10107 * @param p_j2k the jpeg2000 codec.
10108 * @param p_stream the stream object.
10109 * @param p_manager the user event manager.
10111 * @return true if the codec is valid.
10113 opj_bool opj_j2k_start_compress(opj_j2k_v2_t *p_j2k,
10114 opj_stream_private_t *p_stream,
10115 opj_image_t * p_image,
10116 opj_event_mgr_t * p_manager)
10118 /* preconditions */
10119 assert(p_j2k != 00);
10120 assert(p_stream != 00);
10121 assert(p_manager != 00);
10123 p_j2k->m_private_image = opj_image_create0();
10124 opj_copy_image_header(p_image, p_j2k->m_private_image);
10126 // TODO_MSD: Find a better way
10127 if (p_image->comps) {
10128 OPJ_UINT32 it_comp;
10129 for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
10130 if (p_image->comps[it_comp].data) {
10131 p_j2k->m_private_image->comps[it_comp].data =p_image->comps[it_comp].data;
10132 p_image->comps[it_comp].data = NULL;
10138 /* customization of the validation */
10139 opj_j2k_setup_encoding_validation (p_j2k);
10141 /* validation of the parameters codec */
10142 if (! opj_j2k_exec(p_j2k,p_j2k->m_validation_list,p_stream,p_manager)) {
10146 /* customization of the encoding */
10147 opj_j2k_setup_header_writting(p_j2k);
10150 if (! opj_j2k_exec (p_j2k,p_j2k->m_procedure_list,p_stream,p_manager)) {
10160 opj_bool opj_j2k_pre_write_tile ( opj_j2k_v2_t * p_j2k,
10161 OPJ_UINT32 p_tile_index,
10162 opj_stream_private_t *p_stream,
10163 opj_event_mgr_t * p_manager )
10166 if (p_tile_index != p_j2k->m_current_tile_number) {
10167 opj_event_msg_v2(p_manager, EVT_ERROR, "The given tile index does not match." );
10171 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);
10173 p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
10174 p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
10175 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10177 /* initialisation before tile encoding */
10178 if (! tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number)) {
10188 void opj_j2k_get_tile_data (opj_tcd_v2_t * p_tcd, OPJ_BYTE * p_data)
10190 OPJ_UINT32 i,j,k = 0;
10191 OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
10192 opj_image_comp_t * l_img_comp = 00;
10193 opj_tcd_tilecomp_v2_t * l_tilec = 00;
10194 opj_image_t * l_image = 00;
10195 OPJ_UINT32 l_size_comp, l_remaining;
10196 OPJ_INT32 * l_src_ptr;
10197 l_tilec = p_tcd->tcd_image->tiles->comps;
10198 l_image = p_tcd->image;
10199 l_img_comp = l_image->comps;
10201 for (i=0;i<p_tcd->image->numcomps;++i) {
10202 l_size_comp = l_img_comp->prec >> 3; /* (/8) */
10203 l_remaining = l_img_comp->prec & 7; /* (%8) */
10208 if (l_size_comp == 3) {
10212 l_width = (l_tilec->x1 - l_tilec->x0);
10213 l_height = (l_tilec->y1 - l_tilec->y0);
10214 l_offset_x = int_ceildiv(l_image->x0, l_img_comp->dx);
10215 l_offset_y = int_ceildiv(l_image->y0, l_img_comp->dy);
10216 l_image_width = int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
10217 l_stride = l_image_width - l_width;
10218 l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
10220 switch (l_size_comp) {
10223 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
10224 if (l_img_comp->sgnd) {
10225 for (j=0;j<l_height;++j) {
10226 for (k=0;k<l_width;++k) {
10227 *(l_dest_ptr) = (OPJ_CHAR) (*l_src_ptr);
10231 l_src_ptr += l_stride;
10235 for (j=0;j<l_height;++j) {
10236 for (k=0;k<l_width;++k) {
10237 *(l_dest_ptr) = (*l_src_ptr)&0xff;
10241 l_src_ptr += l_stride;
10245 p_data = (OPJ_BYTE*) l_dest_ptr;
10250 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
10251 if (l_img_comp->sgnd) {
10252 for (j=0;j<l_height;++j) {
10253 for (k=0;k<l_width;++k) {
10254 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
10256 l_src_ptr += l_stride;
10260 for (j=0;j<l_height;++j) {
10261 for (k=0;k<l_width;++k) {
10262 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
10264 l_src_ptr += l_stride;
10268 p_data = (OPJ_BYTE*) l_dest_ptr;
10273 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
10274 for (j=0;j<l_height;++j) {
10275 for (k=0;k<l_width;++k) {
10276 *(l_dest_ptr++) = *(l_src_ptr++);
10278 l_src_ptr += l_stride;
10281 p_data = (OPJ_BYTE*) l_dest_ptr;
10294 * @param p_j2k the jpeg2000 codec.
10295 * @param p_stream the stream to write data to.
10296 * @param p_manager the user event manager.
10298 opj_bool opj_j2k_post_write_tile ( opj_j2k_v2_t * p_j2k,
10300 OPJ_UINT32 p_data_size,
10301 opj_stream_private_t *p_stream,
10302 opj_event_mgr_t * p_manager )
10304 opj_tcd_v2_t * l_tcd = 00;
10305 opj_cp_v2_t * l_cp = 00;
10306 opj_tcp_v2_t * l_tcp = 00;
10307 OPJ_UINT32 l_nb_bytes_written;
10308 OPJ_BYTE * l_current_data = 00;
10309 OPJ_UINT32 l_tile_size = 0;
10310 OPJ_UINT32 l_available_data;
10312 /* preconditions */
10313 assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10315 l_tcd = p_j2k->m_tcd;
10316 l_cp = &(p_j2k->m_cp);
10317 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10319 l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
10320 l_available_data = l_tile_size;
10321 l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
10323 if (! tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
10324 opj_event_msg_v2(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
10328 l_nb_bytes_written = 0;
10329 if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10332 l_current_data += l_nb_bytes_written;
10333 l_available_data -= l_nb_bytes_written;
10335 l_nb_bytes_written = 0;
10336 if (! opj_j2k_write_all_tile_parts(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
10340 l_available_data -= l_nb_bytes_written;
10341 l_nb_bytes_written = l_tile_size - l_available_data;
10343 if ( opj_stream_write_data( p_stream,
10344 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
10345 l_nb_bytes_written,p_manager) != l_nb_bytes_written) {
10349 ++p_j2k->m_current_tile_number;
10356 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
10357 * are valid. Developers wanting to extend the library can add their own validation procedures.
10359 void opj_j2k_setup_end_compress (opj_j2k_v2_t *p_j2k)
10361 /* preconditions */
10362 assert(p_j2k != 00);
10364 /* DEVELOPER CORNER, insert your custom procedures */
10365 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_eoc_v2 );
10367 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10368 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm);
10371 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_epc );
10372 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_end_encoding );
10373 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_destroy_header_memory);
10377 * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
10378 * are valid. Developers wanting to extend the library can add their own validation procedures.
10380 void opj_j2k_setup_encoding_validation (opj_j2k_v2_t *p_j2k)
10382 /* preconditions */
10383 assert(p_j2k != 00);
10385 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_build_encoder);
10386 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_encoding_validation);
10388 /* DEVELOPER CORNER, add your custom validation procedure */
10389 opj_procedure_list_add_procedure(p_j2k->m_validation_list, (opj_procedure)opj_j2k_mct_validation);
10394 * Sets up the procedures to do on writing header.
10395 * Developers wanting to extend the library can add their own writing procedures.
10397 void opj_j2k_setup_header_writting (opj_j2k_v2_t *p_j2k)
10399 /* preconditions */
10400 assert(p_j2k != 00);
10402 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_init_info );
10403 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_soc );
10404 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_siz );
10405 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );
10406 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );
10409 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
10410 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_image_components );
10411 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm );
10413 if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == CINEMA4K_24) {
10414 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc );
10418 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_regions);
10420 if (p_j2k->m_cp.comment != 00) {
10421 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_com);
10424 /* DEVELOPER CORNER, insert your custom procedures */
10425 if (p_j2k->m_cp.rsiz & MCT) {
10426 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)j2k_write_mct_data_group );
10428 /* End of Developer Corner */
10430 if (p_j2k->cstr_index) {
10431 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_get_end_header );
10434 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_create_tcd);
10435 opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_update_rates);
10439 opj_bool opj_j2k_write_first_tile_part (opj_j2k_v2_t *p_j2k,
10441 OPJ_UINT32 * p_data_written,
10442 OPJ_UINT32 p_total_data_size,
10443 opj_stream_private_t *p_stream,
10444 struct opj_event_mgr * p_manager )
10447 OPJ_UINT32 l_nb_bytes_written = 0;
10448 OPJ_UINT32 l_current_nb_bytes_written;
10449 OPJ_BYTE * l_begin_data = 00;
10451 opj_tcp_v2_t *l_tcp = 00;
10452 opj_tcd_v2_t * l_tcd = 00;
10453 opj_cp_v2_t * l_cp = 00;
10455 l_tcd = p_j2k->m_tcd;
10456 l_cp = &(p_j2k->m_cp);
10457 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10459 l_tcd->cur_pino = 0;
10461 /*Get number of tile parts*/
10462 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
10467 l_current_nb_bytes_written = 0;
10468 l_begin_data = p_data;
10469 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager))
10474 l_nb_bytes_written += l_current_nb_bytes_written;
10475 p_data += l_current_nb_bytes_written;
10476 p_total_data_size -= l_current_nb_bytes_written;
10478 if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
10479 for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
10480 l_current_nb_bytes_written = 0;
10481 opj_j2k_write_coc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10482 l_nb_bytes_written += l_current_nb_bytes_written;
10483 p_data += l_current_nb_bytes_written;
10484 p_total_data_size -= l_current_nb_bytes_written;
10486 l_current_nb_bytes_written = 0;
10487 opj_j2k_write_qcc_in_memory(p_j2k,compno,p_data,&l_current_nb_bytes_written,p_manager);
10488 l_nb_bytes_written += l_current_nb_bytes_written;
10489 p_data += l_current_nb_bytes_written;
10490 p_total_data_size -= l_current_nb_bytes_written;
10493 if (l_cp->tcps[p_j2k->m_current_tile_number].numpocs) {
10494 l_current_nb_bytes_written = 0;
10495 opj_j2k_write_poc_in_memory(p_j2k,p_data,&l_current_nb_bytes_written,p_manager);
10496 l_nb_bytes_written += l_current_nb_bytes_written;
10497 p_data += l_current_nb_bytes_written;
10498 p_total_data_size -= l_current_nb_bytes_written;
10502 l_current_nb_bytes_written = 0;
10503 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10507 l_nb_bytes_written += l_current_nb_bytes_written;
10508 * p_data_written = l_nb_bytes_written;
10510 /* Writing Psot in SOT marker */
10511 opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
10513 if (l_cp->m_specific_param.m_enc.m_cinema){
10514 opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
10520 opj_bool opj_j2k_write_all_tile_parts( opj_j2k_v2_t *p_j2k,
10522 OPJ_UINT32 * p_data_written,
10523 OPJ_UINT32 p_total_data_size,
10524 opj_stream_private_t *p_stream,
10525 struct opj_event_mgr * p_manager
10528 OPJ_UINT32 tilepartno=0;
10529 OPJ_UINT32 l_nb_bytes_written = 0;
10530 OPJ_UINT32 l_current_nb_bytes_written;
10531 OPJ_UINT32 l_part_tile_size;
10532 OPJ_UINT32 tot_num_tp;
10535 OPJ_BYTE * l_begin_data;
10536 opj_tcp_v2_t *l_tcp = 00;
10537 opj_tcd_v2_t * l_tcd = 00;
10538 opj_cp_v2_t * l_cp = 00;
10541 l_tcd = p_j2k->m_tcd;
10542 l_cp = &(p_j2k->m_cp);
10543 l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
10545 /*Get number of tile parts*/
10546 tot_num_tp = j2k_get_num_tp_v2(l_cp,0,p_j2k->m_current_tile_number);
10548 for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
10549 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10550 l_current_nb_bytes_written = 0;
10551 l_part_tile_size = 0;
10552 l_begin_data = p_data;
10554 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10558 l_nb_bytes_written += l_current_nb_bytes_written;
10559 p_data += l_current_nb_bytes_written;
10560 p_total_data_size -= l_current_nb_bytes_written;
10561 l_part_tile_size += l_nb_bytes_written;
10563 l_current_nb_bytes_written = 0;
10564 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10568 p_data += l_current_nb_bytes_written;
10569 l_nb_bytes_written += l_current_nb_bytes_written;
10570 p_total_data_size -= l_current_nb_bytes_written;
10571 l_part_tile_size += l_nb_bytes_written;
10573 /* Writing Psot in SOT marker */
10574 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10576 if (l_cp->m_specific_param.m_enc.m_cinema) {
10577 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10580 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10583 for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
10584 l_tcd->cur_pino = pino;
10586 /*Get number of tile parts*/
10587 tot_num_tp = j2k_get_num_tp_v2(l_cp,pino,p_j2k->m_current_tile_number);
10588 for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
10589 p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
10590 l_current_nb_bytes_written = 0;
10591 l_part_tile_size = 0;
10592 l_begin_data = p_data;
10594 if (! opj_j2k_write_sot(p_j2k,p_data,&l_current_nb_bytes_written,p_stream,p_manager)) {
10598 l_nb_bytes_written += l_current_nb_bytes_written;
10599 p_data += l_current_nb_bytes_written;
10600 p_total_data_size -= l_current_nb_bytes_written;
10601 l_part_tile_size += l_current_nb_bytes_written;
10603 l_current_nb_bytes_written = 0;
10605 if (! opj_j2k_write_sod(p_j2k,l_tcd,p_data,&l_current_nb_bytes_written,p_total_data_size,p_stream,p_manager)) {
10609 l_nb_bytes_written += l_current_nb_bytes_written;
10610 p_data += l_current_nb_bytes_written;
10611 p_total_data_size -= l_current_nb_bytes_written;
10612 l_part_tile_size += l_current_nb_bytes_written;
10614 /* Writing Psot in SOT marker */
10615 opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
10617 if (l_cp->m_specific_param.m_enc.m_cinema) {
10618 opj_j2k_update_tlm(p_j2k,l_part_tile_size);
10621 ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
10625 *p_data_written = l_nb_bytes_written;
10631 * Writes the updated tlm.
10633 * @param p_stream the stream to write data to.
10634 * @param p_j2k J2K codec.
10635 * @param p_manager the user event manager.
10637 opj_bool opj_j2k_write_updated_tlm( opj_j2k_v2_t *p_j2k,
10638 struct opj_stream_private *p_stream,
10639 struct opj_event_mgr * p_manager )
10641 OPJ_UINT32 l_tlm_size;
10642 OPJ_SIZE_T l_tlm_position, l_current_position;
10644 /* preconditions */
10645 assert(p_j2k != 00);
10646 assert(p_manager != 00);
10647 assert(p_stream != 00);
10649 l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
10650 l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
10651 l_current_position = opj_stream_tell(p_stream);
10653 if (! opj_stream_seek(p_stream,l_tlm_position,p_manager)) {
10657 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) {
10661 if (! opj_stream_seek(p_stream,l_current_position,p_manager)) {
10670 * Ends the encoding, i.e. frees memory.
10672 * @param p_stream the stream to write data to.
10673 * @param p_j2k J2K codec.
10674 * @param p_manager the user event manager.
10676 opj_bool j2k_end_encoding( opj_j2k_v2_t *p_j2k,
10677 struct opj_stream_private *p_stream,
10678 struct opj_event_mgr * p_manager )
10680 /* preconditions */
10681 assert(p_j2k != 00);
10682 assert(p_manager != 00);
10683 assert(p_stream != 00);
10685 tcd_destroy_v2(p_j2k->m_tcd);
10688 if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
10689 opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
10690 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
10691 p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
10694 if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
10695 opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
10696 p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
10699 p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
10705 * Destroys the memory associated with the decoding of headers.
10707 static opj_bool opj_j2k_destroy_header_memory ( opj_j2k_v2_t * p_j2k,
10708 opj_stream_private_t *p_stream,
10709 opj_event_mgr_t * p_manager
10712 /* preconditions */
10713 assert(p_j2k != 00);
10714 assert(p_stream != 00);
10715 assert(p_manager != 00);
10717 if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
10718 opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
10719 p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
10722 p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
10731 * @param p_stream the stream to write data to.
10732 * @param p_j2k J2K codec.
10733 * @param p_manager the user event manager.
10735 opj_bool j2k_init_info( opj_j2k_v2_t *p_j2k,
10736 struct opj_stream_private *p_stream,
10737 struct opj_event_mgr * p_manager )
10739 opj_codestream_info_t * l_cstr_info = 00;
10741 /* preconditions */
10742 assert(p_j2k != 00);
10743 assert(p_manager != 00);
10744 assert(p_stream != 00);
10747 /* TODO mergeV2: check this part which use cstr_info */
10748 /*l_cstr_info = p_j2k->cstr_info;
10752 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));
10754 l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
10755 l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
10757 l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
10759 l_cstr_info->tw = p_j2k->m_cp.tw;
10760 l_cstr_info->th = p_j2k->m_cp.th;
10762 l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
10763 /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
10764 /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
10765 /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
10767 /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
10769 l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
10771 l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
10773 for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
10774 l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
10777 l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
10779 /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
10781 /*l_cstr_info->maxmarknum = 100;
10782 l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
10783 l_cstr_info->marknum = 0;
10786 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);
10790 * Creates a tile-coder decoder.
10792 * @param p_stream the stream to write data to.
10793 * @param p_j2k J2K codec.
10794 * @param p_manager the user event manager.
10796 static opj_bool opj_j2k_create_tcd( opj_j2k_v2_t *p_j2k,
10797 opj_stream_private_t *p_stream,
10798 opj_event_mgr_t * p_manager
10801 /* preconditions */
10802 assert(p_j2k != 00);
10803 assert(p_manager != 00);
10804 assert(p_stream != 00);
10806 p_j2k->m_tcd = tcd_create_v2(OPJ_FALSE);
10808 if (! p_j2k->m_tcd) {
10809 opj_event_msg_v2(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
10813 if (! tcd_init_v2(p_j2k->m_tcd,p_j2k->m_private_image,&p_j2k->m_cp)) {
10814 tcd_destroy_v2(p_j2k->m_tcd);
10825 * @param p_j2k the jpeg2000 codec.
10826 * @param p_stream the stream to write data to.
10827 * @param p_manager the user event manager.
10829 opj_bool opj_j2k_write_tile (opj_j2k_v2_t * p_j2k,
10830 OPJ_UINT32 p_tile_index,
10832 OPJ_UINT32 p_data_size,
10833 opj_stream_private_t *p_stream,
10834 opj_event_mgr_t * p_manager )
10836 if (! opj_j2k_pre_write_tile(p_j2k,p_tile_index,p_stream,p_manager)) {
10837 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
10841 if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
10842 opj_event_msg_v2(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);